refractor: dont import chatEntry into each api

This commit is contained in:
end-4
2025-04-03 16:41:37 +02:00
parent 98fd391b88
commit c03d24741d
5 changed files with 78 additions and 80 deletions
+3 -4
View File
@@ -8,7 +8,6 @@ import { MaterialIcon } from '../../.commonwidgets/materialicon.js';
import { MarginRevealer } from '../../.widgethacks/advancedrevealers.js';
import { setupCursorHover, setupCursorHoverInfo } from '../../.widgetutils/cursorhover.js';
import BooruService from '../../../services/booru.js';
import { chatEntry } from '../apiwidgets.js';
import { ConfigToggle } from '../../.commonwidgets/configwidgets.js';
import { SystemMessage } from './ai_chatmessage.js';
@@ -19,7 +18,7 @@ const USER_CACHE_DIR = GLib.get_user_cache_dir();
Utils.exec(`bash -c 'mkdir -p ${USER_CACHE_DIR}/ags/media/waifus'`);
Utils.exec(`bash -c 'rm ${USER_CACHE_DIR}/ags/media/waifus/*'`);
const TagButton = (command) => Button({
const TagButton = (command, chatEntry) => Button({
className: 'sidebar-chat-chip sidebar-chat-chip-action txt txt-small',
onClicked: () => { chatEntry.buffer.text += `${command} ` },
setup: setupCursorHover,
@@ -453,7 +452,7 @@ const booruContent = Box({
,
});
export const booruView = Scrollable({
export const BooruView = (chatEntry) => Scrollable({
className: 'sidebar-chat-viewport',
vexpand: true,
child: Box({
@@ -533,7 +532,7 @@ const clearChat = () => { // destroy!!
});
}
export const sendMessage = (text) => {
export const sendMessage = (text, booruView) => {
// Commands
if (text.startsWith('+')) { // Next page
const lastQuery = BooruService.queries.at(-1);
+8 -10
View File
@@ -1,5 +1,4 @@
const { Gtk } = imports.gi;
import App from 'resource:///com/github/Aylur/ags/app.js';
import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
@@ -11,7 +10,6 @@ import { ConfigToggle, ConfigSegmentedSelection, ConfigGap } from '../../.common
import { markdownTest } from '../../.miscutils/md2pango.js';
import { MarginRevealer } from '../../.widgethacks/advancedrevealers.js';
import { MaterialIcon } from '../../.commonwidgets/materialicon.js';
import { chatEntry } from '../apiwidgets.js';
export const chatGPTTabIcon = Icon({
hpack: 'center',
@@ -280,19 +278,19 @@ export const sendMessage = (text) => {
if (text.length == 0) return;
if (GPTService.key.length == 0) {
GPTService.key = text;
chatContent.add(SystemMessage(`Key saved to\n\`${GPTService.keyPath}\``, 'API Key', chatGPTView));
chatContent.add(SystemMessage(`Key saved to\n\`${GPTService.keyPath}\``, 'API Key', ChatGPTView));
text = '';
return;
}
// Commands
if (text.startsWith('/')) {
if (text.startsWith('/clear')) clearChat();
else if (text.startsWith('/model')) chatContent.add(SystemMessage(`${getString("Currently using")} \`${GPTService.modelName}\``, '/model', chatGPTView))
else if (text.startsWith('/model')) chatContent.add(SystemMessage(`${getString("Currently using")} \`${GPTService.modelName}\``, '/model', ChatGPTView))
else if (text.startsWith('/prompt')) {
const firstSpaceIndex = text.indexOf(' ');
const prompt = text.slice(firstSpaceIndex + 1);
if (firstSpaceIndex == -1 || prompt.length < 1) {
chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', chatGPTView))
chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', ChatGPTView))
}
else {
GPTService.addMessage('user', prompt)
@@ -303,23 +301,23 @@ export const sendMessage = (text) => {
if (parts.length == 1) chatContent.add(SystemMessage(
`${getString("Key stored in:")}\n\`${GPTService.keyPath}\`\n${getString("To update this key, type")} \`/key YOUR_API_KEY\``,
'/key',
chatGPTView));
ChatGPTView));
else {
GPTService.key = parts[1];
chatContent.add(SystemMessage(`${getString("Updated API Key at")}\n\`${GPTService.keyPath}\``, '/key', chatGPTView));
chatContent.add(SystemMessage(`${getString("Updated API Key at")}\n\`${GPTService.keyPath}\``, '/key', ChatGPTView));
}
}
else if (text.startsWith('/test'))
chatContent.add(SystemMessage(markdownTest, `Markdown test`, chatGPTView));
chatContent.add(SystemMessage(markdownTest, `Markdown test`, ChatGPTView));
else
chatContent.add(SystemMessage(getString("Invalid command."), 'Error', chatGPTView))
chatContent.add(SystemMessage(getString("Invalid command."), 'Error', ChatGPTView))
}
else {
GPTService.send(text);
}
}
export const chatGPTView = Box({
export const ChatGPTView = (chatEntry) => Box({
vertical: true,
children: [
ProviderSwitcher(),
+8 -10
View File
@@ -1,5 +1,4 @@
const { Gtk } = imports.gi;
import App from 'resource:///com/github/Aylur/ags/app.js';
import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
@@ -10,7 +9,6 @@ import { SystemMessage, ChatMessage } from "./ai_chatmessage.js";
import { ConfigToggle, ConfigSegmentedSelection, ConfigGap } from '../../.commonwidgets/configwidgets.js';
import { markdownTest } from '../../.miscutils/md2pango.js';
import { MarginRevealer } from '../../.widgethacks/advancedrevealers.js';
import { chatEntry } from '../apiwidgets.js';
const MODEL_NAME = `Gemini`;
@@ -212,7 +210,7 @@ export const sendMessage = (text) => {
if (text.length == 0) return;
if (GeminiService.key.length == 0) {
GeminiService.key = text;
chatContent.add(SystemMessage(`Key saved to\n\`${GeminiService.keyPath}\``, 'API Key', geminiView));
chatContent.add(SystemMessage(`Key saved to\n\`${GeminiService.keyPath}\``, 'API Key', GeminiView));
text = '';
return;
}
@@ -223,12 +221,12 @@ export const sendMessage = (text) => {
clearChat();
GeminiService.loadHistory();
}
else if (text.startsWith('/model')) chatContent.add(SystemMessage(`${getString("Currently using")} \`${GeminiService.modelName}\``, '/model', geminiView))
else if (text.startsWith('/model')) chatContent.add(SystemMessage(`${getString("Currently using")} \`${GeminiService.modelName}\``, '/model', GeminiView))
else if (text.startsWith('/prompt')) {
const firstSpaceIndex = text.indexOf(' ');
const prompt = text.slice(firstSpaceIndex + 1);
if (firstSpaceIndex == -1 || prompt.length < 1) {
chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', geminiView))
chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', GeminiView))
}
else {
GeminiService.addMessage('user', prompt)
@@ -239,23 +237,23 @@ export const sendMessage = (text) => {
if (parts.length == 1) chatContent.add(SystemMessage(
`${getString("Key stored in:")} \n\`${GeminiService.keyPath}\`\n${getString("To update this key, type")} \`/key YOUR_API_KEY\``,
'/key',
geminiView));
GeminiView));
else {
GeminiService.key = parts[1];
chatContent.add(SystemMessage(`${getString("Updated API Key at")}\n\`${GeminiService.keyPath}\``, '/key', geminiView));
chatContent.add(SystemMessage(`${getString("Updated API Key at")}\n\`${GeminiService.keyPath}\``, '/key', GeminiView));
}
}
else if (text.startsWith('/test'))
chatContent.add(SystemMessage(markdownTest, `Markdown test`, geminiView));
chatContent.add(SystemMessage(markdownTest, `Markdown test`, GeminiView));
else
chatContent.add(SystemMessage(getString(`Invalid command.`), 'Error', geminiView))
chatContent.add(SystemMessage(getString(`Invalid command.`), 'Error', GeminiView))
}
else {
GeminiService.send(text);
}
}
export const geminiView = Box({
export const GeminiView = (chatEntry) => Box({
homogeneous: true,
children: [Scrollable({
className: 'sidebar-chat-viewport',
+1 -2
View File
@@ -12,7 +12,6 @@ import { MarginRevealer } from '../../.widgethacks/advancedrevealers.js';
import { setupCursorHover, setupCursorHoverInfo } from '../../.widgetutils/cursorhover.js';
import WaifuService from '../../../services/waifus.js';
import { darkMode } from '../../.miscutils/system.js';
import { chatEntry } from '../apiwidgets.js';
async function getImageViewerApp(preferredApp) {
Utils.execAsync(['bash', '-c', `command -v ${preferredApp}`])
@@ -302,7 +301,7 @@ const waifuContent = Box({
,
});
export const waifuView = Scrollable({
export const WaifuView = (chatEntry) => Scrollable({
className: 'sidebar-chat-viewport',
vexpand: true,
child: Box({