internal: rename chatgpt to gpt

This commit is contained in:
end-4
2024-02-29 17:07:49 +07:00
parent 8d00b16b65
commit dd5e0cb9a0
4 changed files with 66 additions and 73 deletions
@@ -12,7 +12,7 @@ const LATEX_DIR = `${GLib.get_user_cache_dir()}/ags/media/latex`;
const CUSTOM_SOURCEVIEW_SCHEME_PATH = `${App.configDir}/assets/themes/sourceviewtheme.xml`; const CUSTOM_SOURCEVIEW_SCHEME_PATH = `${App.configDir}/assets/themes/sourceviewtheme.xml`;
const CUSTOM_SCHEME_ID = 'custom'; const CUSTOM_SCHEME_ID = 'custom';
const USERNAME = GLib.get_user_name(); const USERNAME = GLib.get_user_name();
const CHATGPT_CURSOR = ' ...'; const AI_MESSAGE_CURSOR = ' ...';
/////////////////////// Custom source view colorscheme ///////////////////////// /////////////////////// Custom source view colorscheme /////////////////////////
@@ -34,13 +34,6 @@ loadCustomColorScheme(CUSTOM_SOURCEVIEW_SCHEME_PATH);
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
function copyToClipboard(text) {
const clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD);
const textVariant = new GLib.Variant('s', text);
clipboard.set_text(textVariant, -1);
clipboard.store();
}
function substituteLang(str) { function substituteLang(str) {
const subs = [ const subs = [
{ from: 'javascript', to: 'js' }, { from: 'javascript', to: 'js' },
@@ -265,7 +258,7 @@ const MessageContent = (content) => {
const lastLabel = kids[kids.length - 1]; const lastLabel = kids[kids.length - 1];
let blockContent = lines.slice(lastProcessed, lines.length).join('\n'); let blockContent = lines.slice(lastProcessed, lines.length).join('\n');
if (!inCode) if (!inCode)
lastLabel.label = `${md2pango(blockContent)}${useCursor ? CHATGPT_CURSOR : ''}`; lastLabel.label = `${md2pango(blockContent)}${useCursor ? AI_MESSAGE_CURSOR : ''}`;
else else
lastLabel.attribute.updateText(blockContent); lastLabel.attribute.updateText(blockContent);
} }
+40 -40
View File
@@ -4,7 +4,7 @@ import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'; import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
const { Box, Button, Icon, Label, Revealer, Scrollable } = Widget; const { Box, Button, Icon, Label, Revealer, Scrollable } = Widget;
import ChatGPT from '../../../services/gpt.js'; import GPTService from '../../../services/gpt.js';
import { setupCursorHover, setupCursorHoverInfo } from '../../.widgetutils/cursorhover.js'; import { setupCursorHover, setupCursorHoverInfo } from '../../.widgetutils/cursorhover.js';
import { SystemMessage, ChatMessage } from "./ai_chatmessage.js"; import { SystemMessage, ChatMessage } from "./ai_chatmessage.js";
import { ConfigToggle, ConfigSegmentedSelection, ConfigGap } from '../../.commonwidgets/configwidgets.js'; import { ConfigToggle, ConfigSegmentedSelection, ConfigGap } from '../../.commonwidgets/configwidgets.js';
@@ -23,14 +23,14 @@ export const chatGPTTabIcon = Icon({
const ProviderSwitcher = () => { const ProviderSwitcher = () => {
const ProviderChoice = (id, provider) => { const ProviderChoice = (id, provider) => {
const providerSelected = MaterialIcon('check', 'norm', { const providerSelected = MaterialIcon('check', 'norm', {
setup: (self) => self.hook(ChatGPT, (self) => { setup: (self) => self.hook(GPTService, (self) => {
self.toggleClassName('invisible', ChatGPT.providerID !== id); self.toggleClassName('invisible', GPTService.providerID !== id);
}, 'providerChanged') }, 'providerChanged')
}); });
return Button({ return Button({
tooltipText: provider.description, tooltipText: provider.description,
onClicked: () => { onClicked: () => {
ChatGPT.providerID = id; GPTService.providerID = id;
providerList.revealChild = false; providerList.revealChild = false;
indicatorChevron.label = 'expand_more'; indicatorChevron.label = 'expand_more';
}, },
@@ -64,9 +64,9 @@ const ProviderSwitcher = () => {
hexpand: true, hexpand: true,
xalign: 0, xalign: 0,
className: 'txt-small', className: 'txt-small',
label: ChatGPT.providerID, label: GPTService.providerID,
setup: (self) => self.hook(ChatGPT, (self) => { setup: (self) => self.hook(GPTService, (self) => {
self.label = `${ChatGPT.providers[ChatGPT.providerID]['name']}`; self.label = `${GPTService.providers[GPTService.providerID]['name']}`;
}, 'providerChanged') }, 'providerChanged')
}), }),
indicatorChevron, indicatorChevron,
@@ -89,8 +89,8 @@ const ProviderSwitcher = () => {
Box({ Box({
className: 'spacing-v-5', className: 'spacing-v-5',
vertical: true, vertical: true,
setup: (self) => self.hook(ChatGPT, (self) => { setup: (self) => self.hook(GPTService, (self) => {
self.children = Object.entries(ChatGPT.providers) self.children = Object.entries(GPTService.providers)
.map(([id, provider]) => ProviderChoice(id, provider)); .map(([id, provider]) => ProviderChoice(id, provider));
}, 'initialized'), }, 'initialized'),
}) })
@@ -108,7 +108,7 @@ const ProviderSwitcher = () => {
}) })
} }
const ChatGPTInfo = () => { const GPTInfo = () => {
const openAiLogo = Icon({ const openAiLogo = Icon({
hpack: 'center', hpack: 'center',
className: 'sidebar-chat-welcome-logo', className: 'sidebar-chat-welcome-logo',
@@ -147,14 +147,14 @@ const ChatGPTInfo = () => {
}); });
} }
export const ChatGPTSettings = () => MarginRevealer({ const GPTSettings = () => MarginRevealer({
transition: 'slide_down', transition: 'slide_down',
revealChild: true, revealChild: true,
extraSetup: (self) => self extraSetup: (self) => self
.hook(ChatGPT, (self) => Utils.timeout(200, () => { .hook(GPTService, (self) => Utils.timeout(200, () => {
self.attribute.hide(); self.attribute.hide();
}), 'newMsg') }), 'newMsg')
.hook(ChatGPT, (self) => Utils.timeout(200, () => { .hook(GPTService, (self) => Utils.timeout(200, () => {
self.attribute.show(); self.attribute.show();
}), 'clear') }), 'clear')
, ,
@@ -166,7 +166,7 @@ export const ChatGPTSettings = () => MarginRevealer({
hpack: 'center', hpack: 'center',
icon: 'casino', icon: 'casino',
name: 'Randomness', name: 'Randomness',
desc: 'ChatGPT\'s temperature value.\n Precise = 0\n Balanced = 0.5\n Creative = 1', desc: 'The model\'s temperature value.\n Precise = 0\n Balanced = 0.5\n Creative = 1',
options: [ options: [
{ value: 0.00, name: 'Precise', }, { value: 0.00, name: 'Precise', },
{ value: 0.50, name: 'Balanced', }, { value: 0.50, name: 'Balanced', },
@@ -174,7 +174,7 @@ export const ChatGPTSettings = () => MarginRevealer({
], ],
initIndex: 2, initIndex: 2,
onChange: (value, name) => { onChange: (value, name) => {
ChatGPT.temperature = value; GPTService.temperature = value;
}, },
}), }),
ConfigGap({ vertical: true, size: 10 }), // Note: size can only be 5, 10, or 15 ConfigGap({ vertical: true, size: 10 }), // Note: size can only be 5, 10, or 15
@@ -187,18 +187,18 @@ export const ChatGPTSettings = () => MarginRevealer({
icon: 'cycle', icon: 'cycle',
name: 'Cycle models', name: 'Cycle models',
desc: 'Helps avoid exceeding the API rate of 3 messages per minute.\nTurn this on if you message rapidly.', desc: 'Helps avoid exceeding the API rate of 3 messages per minute.\nTurn this on if you message rapidly.',
initValue: ChatGPT.cycleModels, initValue: GPTService.cycleModels,
onChange: (self, newValue) => { onChange: (self, newValue) => {
ChatGPT.cycleModels = newValue; GPTService.cycleModels = newValue;
}, },
}), }),
ConfigToggle({ ConfigToggle({
icon: 'model_training', icon: 'model_training',
name: 'Enhancements', name: 'Enhancements',
desc: 'Tells ChatGPT:\n- It\'s a Linux sidebar assistant\n- Be brief and use bullet points', desc: 'Tells the model:\n- It\'s a Linux sidebar assistant\n- Be brief and use bullet points',
initValue: ChatGPT.assistantPrompt, initValue: GPTService.assistantPrompt,
onChange: (self, newValue) => { onChange: (self, newValue) => {
ChatGPT.assistantPrompt = newValue; GPTService.assistantPrompt = newValue;
}, },
}), }),
] ]
@@ -213,8 +213,8 @@ export const OpenaiApiKeyInstructions = () => Box({
transition: 'slide_down', transition: 'slide_down',
transitionDuration: 150, transitionDuration: 150,
setup: (self) => self setup: (self) => self
.hook(ChatGPT, (self, hasKey) => { .hook(GPTService, (self, hasKey) => {
self.revealChild = (ChatGPT.key.length == 0); self.revealChild = (GPTService.key.length == 0);
}, 'hasKey') }, 'hasKey')
, ,
child: Button({ child: Button({
@@ -227,13 +227,13 @@ export const OpenaiApiKeyInstructions = () => Box({
}), }),
setup: setupCursorHover, setup: setupCursorHover,
onClicked: () => { onClicked: () => {
Utils.execAsync(['bash', '-c', `xdg-open ${ChatGPT.getKeyUrl}`]); Utils.execAsync(['bash', '-c', `xdg-open ${GPTService.getKeyUrl}`]);
} }
}) })
})] })]
}); });
const chatGPTWelcome = Box({ const GPTWelcome = () => Box({
vexpand: true, vexpand: true,
homogeneous: true, homogeneous: true,
child: Box({ child: Box({
@@ -241,9 +241,9 @@ const chatGPTWelcome = Box({
vpack: 'center', vpack: 'center',
vertical: true, vertical: true,
children: [ children: [
ChatGPTInfo(), GPTInfo(),
OpenaiApiKeyInstructions(), OpenaiApiKeyInstructions(),
ChatGPTSettings(), GPTSettings(),
] ]
}) })
}); });
@@ -252,16 +252,16 @@ export const chatContent = Box({
className: 'spacing-v-15', className: 'spacing-v-15',
vertical: true, vertical: true,
setup: (self) => self setup: (self) => self
.hook(ChatGPT, (box, id) => { .hook(GPTService, (box, id) => {
const message = ChatGPT.messages[id]; const message = GPTService.messages[id];
if (!message) return; if (!message) return;
box.add(ChatMessage(message, 'ChatGPT')) box.add(ChatMessage(message, `Model (${GPTService.providers[GPTService.providerID]['name']})`))
}, 'newMsg') }, 'newMsg')
, ,
}); });
const clearChat = () => { const clearChat = () => {
ChatGPT.clear(); GPTService.clear();
const children = chatContent.get_children(); const children = chatContent.get_children();
for (let i = 0; i < children.length; i++) { for (let i = 0; i < children.length; i++) {
const child = children[i]; const child = children[i];
@@ -289,16 +289,16 @@ export const chatGPTCommands = Box({
export const sendMessage = (text) => { export const sendMessage = (text) => {
// Check if text or API key is empty // Check if text or API key is empty
if (text.length == 0) return; if (text.length == 0) return;
if (ChatGPT.key.length == 0) { if (GPTService.key.length == 0) {
ChatGPT.key = text; GPTService.key = text;
chatContent.add(SystemMessage(`Key saved to\n\`${ChatGPT.keyPath}\``, 'API Key', chatGPTView)); chatContent.add(SystemMessage(`Key saved to\n\`${GPTService.keyPath}\``, 'API Key', chatGPTView));
text = ''; text = '';
return; return;
} }
// Commands // Commands
if (text.startsWith('/')) { if (text.startsWith('/')) {
if (text.startsWith('/clear')) clearChat(); if (text.startsWith('/clear')) clearChat();
else if (text.startsWith('/model')) chatContent.add(SystemMessage(`Currently using \`${ChatGPT.modelName}\``, '/model', chatGPTView)) else if (text.startsWith('/model')) chatContent.add(SystemMessage(`Currently using \`${GPTService.modelName}\``, '/model', chatGPTView))
else if (text.startsWith('/prompt')) { else if (text.startsWith('/prompt')) {
const firstSpaceIndex = text.indexOf(' '); const firstSpaceIndex = text.indexOf(' ');
const prompt = text.slice(firstSpaceIndex + 1); const prompt = text.slice(firstSpaceIndex + 1);
@@ -306,18 +306,18 @@ export const sendMessage = (text) => {
chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', chatGPTView)) chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', chatGPTView))
} }
else { else {
ChatGPT.addMessage('user', prompt) GPTService.addMessage('user', prompt)
} }
} }
else if (text.startsWith('/key')) { else if (text.startsWith('/key')) {
const parts = text.split(' '); const parts = text.split(' ');
if (parts.length == 1) chatContent.add(SystemMessage( if (parts.length == 1) chatContent.add(SystemMessage(
`Key stored in:\n\`${ChatGPT.keyPath}\`\nTo update this key, type \`/key YOUR_API_KEY\``, `Key stored in:\n\`${GPTService.keyPath}\`\nTo update this key, type \`/key YOUR_API_KEY\``,
'/key', '/key',
chatGPTView)); chatGPTView));
else { else {
ChatGPT.key = parts[1]; GPTService.key = parts[1];
chatContent.add(SystemMessage(`Updated API Key at\n\`${ChatGPT.keyPath}\``, '/key', chatGPTView)); chatContent.add(SystemMessage(`Updated API Key at\n\`${GPTService.keyPath}\``, '/key', chatGPTView));
} }
} }
else if (text.startsWith('/test')) else if (text.startsWith('/test'))
@@ -326,7 +326,7 @@ export const sendMessage = (text) => {
chatContent.add(SystemMessage(`Invalid command.`, 'Error', chatGPTView)) chatContent.add(SystemMessage(`Invalid command.`, 'Error', chatGPTView))
} }
else { else {
ChatGPT.send(text); GPTService.send(text);
} }
} }
@@ -340,7 +340,7 @@ export const chatGPTView = Box({
child: Box({ child: Box({
vertical: true, vertical: true,
children: [ children: [
chatGPTWelcome, GPTWelcome(),
chatContent, chatContent,
] ]
}), }),
+20 -20
View File
@@ -4,7 +4,7 @@ import Widget from 'resource:///com/github/Aylur/ags/widget.js';
import * as Utils from 'resource:///com/github/Aylur/ags/utils.js'; import * as Utils from 'resource:///com/github/Aylur/ags/utils.js';
const { Box, Button, Icon, Label, Revealer, Scrollable } = Widget; const { Box, Button, Icon, Label, Revealer, Scrollable } = Widget;
import Gemini from '../../../services/gemini.js'; import GeminiService from '../../../services/gemini.js';
import { setupCursorHover, setupCursorHoverInfo } from '../../.widgetutils/cursorhover.js'; import { setupCursorHover, setupCursorHoverInfo } from '../../.widgetutils/cursorhover.js';
import { SystemMessage, ChatMessage } from "./ai_chatmessage.js"; import { SystemMessage, ChatMessage } from "./ai_chatmessage.js";
import { ConfigToggle, ConfigSegmentedSelection, ConfigGap } from '../../.commonwidgets/configwidgets.js'; import { ConfigToggle, ConfigSegmentedSelection, ConfigGap } from '../../.commonwidgets/configwidgets.js';
@@ -63,10 +63,10 @@ export const GeminiSettings = () => MarginRevealer({
transition: 'slide_down', transition: 'slide_down',
revealChild: true, revealChild: true,
extraSetup: (self) => self extraSetup: (self) => self
.hook(Gemini, (self) => Utils.timeout(200, () => { .hook(GeminiService, (self) => Utils.timeout(200, () => {
self.attribute.hide(); self.attribute.hide();
}), 'newMsg') }), 'newMsg')
.hook(Gemini, (self) => Utils.timeout(200, () => { .hook(GeminiService, (self) => Utils.timeout(200, () => {
self.attribute.show(); self.attribute.show();
}), 'clear') }), 'clear')
, ,
@@ -86,7 +86,7 @@ export const GeminiSettings = () => MarginRevealer({
], ],
initIndex: 2, initIndex: 2,
onChange: (value, name) => { onChange: (value, name) => {
Gemini.temperature = value; GeminiService.temperature = value;
}, },
}), }),
ConfigGap({ vertical: true, size: 10 }), // Note: size can only be 5, 10, or 15 ConfigGap({ vertical: true, size: 10 }), // Note: size can only be 5, 10, or 15
@@ -99,9 +99,9 @@ export const GeminiSettings = () => MarginRevealer({
icon: 'model_training', icon: 'model_training',
name: 'Enhancements', name: 'Enhancements',
desc: 'Tells Gemini:\n- It\'s a Linux sidebar assistant\n- Be brief and use bullet points', desc: 'Tells Gemini:\n- It\'s a Linux sidebar assistant\n- Be brief and use bullet points',
initValue: Gemini.assistantPrompt, initValue: GeminiService.assistantPrompt,
onChange: (self, newValue) => { onChange: (self, newValue) => {
Gemini.assistantPrompt = newValue; GeminiService.assistantPrompt = newValue;
}, },
}), }),
] ]
@@ -116,8 +116,8 @@ export const GoogleAiInstructions = () => Box({
transition: 'slide_down', transition: 'slide_down',
transitionDuration: 150, transitionDuration: 150,
setup: (self) => self setup: (self) => self
.hook(Gemini, (self, hasKey) => { .hook(GeminiService, (self, hasKey) => {
self.revealChild = (Gemini.key.length == 0); self.revealChild = (GeminiService.key.length == 0);
}, 'hasKey') }, 'hasKey')
, ,
child: Button({ child: Button({
@@ -155,8 +155,8 @@ export const chatContent = Box({
className: 'spacing-v-15', className: 'spacing-v-15',
vertical: true, vertical: true,
setup: (self) => self setup: (self) => self
.hook(Gemini, (box, id) => { .hook(GeminiService, (box, id) => {
const message = Gemini.messages[id]; const message = GeminiService.messages[id];
if (!message) return; if (!message) return;
box.add(ChatMessage(message, MODEL_NAME)) box.add(ChatMessage(message, MODEL_NAME))
}, 'newMsg') }, 'newMsg')
@@ -164,7 +164,7 @@ export const chatContent = Box({
}); });
const clearChat = () => { const clearChat = () => {
Gemini.clear(); GeminiService.clear();
const children = chatContent.get_children(); const children = chatContent.get_children();
for (let i = 0; i < children.length; i++) { for (let i = 0; i < children.length; i++) {
const child = children[i]; const child = children[i];
@@ -192,16 +192,16 @@ export const geminiCommands = Box({
export const sendMessage = (text) => { export const sendMessage = (text) => {
// Check if text or API key is empty // Check if text or API key is empty
if (text.length == 0) return; if (text.length == 0) return;
if (Gemini.key.length == 0) { if (GeminiService.key.length == 0) {
Gemini.key = text; GeminiService.key = text;
chatContent.add(SystemMessage(`Key saved to\n\`${Gemini.keyPath}\``, 'API Key', geminiView)); chatContent.add(SystemMessage(`Key saved to\n\`${GeminiService.keyPath}\``, 'API Key', geminiView));
text = ''; text = '';
return; return;
} }
// Commands // Commands
if (text.startsWith('/')) { if (text.startsWith('/')) {
if (text.startsWith('/clear')) clearChat(); if (text.startsWith('/clear')) clearChat();
else if (text.startsWith('/model')) chatContent.add(SystemMessage(`Currently using \`${Gemini.modelName}\``, '/model', geminiView)) else if (text.startsWith('/model')) chatContent.add(SystemMessage(`Currently using \`${GeminiService.modelName}\``, '/model', geminiView))
else if (text.startsWith('/prompt')) { else if (text.startsWith('/prompt')) {
const firstSpaceIndex = text.indexOf(' '); const firstSpaceIndex = text.indexOf(' ');
const prompt = text.slice(firstSpaceIndex + 1); const prompt = text.slice(firstSpaceIndex + 1);
@@ -209,18 +209,18 @@ export const sendMessage = (text) => {
chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', geminiView)) chatContent.add(SystemMessage(`Usage: \`/prompt MESSAGE\``, '/prompt', geminiView))
} }
else { else {
Gemini.addMessage('user', prompt) GeminiService.addMessage('user', prompt)
} }
} }
else if (text.startsWith('/key')) { else if (text.startsWith('/key')) {
const parts = text.split(' '); const parts = text.split(' ');
if (parts.length == 1) chatContent.add(SystemMessage( if (parts.length == 1) chatContent.add(SystemMessage(
`Key stored in:\n\`${Gemini.keyPath}\`\nTo update this key, type \`/key YOUR_API_KEY\``, `Key stored in:\n\`${GeminiService.keyPath}\`\nTo update this key, type \`/key YOUR_API_KEY\``,
'/key', '/key',
geminiView)); geminiView));
else { else {
Gemini.key = parts[1]; GeminiService.key = parts[1];
chatContent.add(SystemMessage(`Updated API Key at\n\`${Gemini.keyPath}\``, '/key', geminiView)); chatContent.add(SystemMessage(`Updated API Key at\n\`${GeminiService.keyPath}\``, '/key', geminiView));
} }
} }
else if (text.startsWith('/test')) else if (text.startsWith('/test'))
@@ -229,7 +229,7 @@ export const sendMessage = (text) => {
chatContent.add(SystemMessage(`Invalid command.`, 'Error', geminiView)) chatContent.add(SystemMessage(`Invalid command.`, 'Error', geminiView))
} }
else { else {
Gemini.send(text); GeminiService.send(text);
} }
} }
+4 -4
View File
@@ -6,7 +6,7 @@ const { execAsync, exec } = Utils;
import { setupCursorHover, setupCursorHoverInfo } from '../.widgetutils/cursorhover.js'; import { setupCursorHover, setupCursorHoverInfo } from '../.widgetutils/cursorhover.js';
import { contentStack } from './sideleft.js'; import { contentStack } from './sideleft.js';
// APIs // APIs
import ChatGPT from '../../services/gpt.js'; import GPTService from '../../services/gpt.js';
import Gemini from '../../services/gemini.js'; import Gemini from '../../services/gemini.js';
import { geminiView, geminiCommands, sendMessage as geminiSendMessage, geminiTabIcon } from './apis/gemini.js'; import { geminiView, geminiCommands, sendMessage as geminiSendMessage, geminiTabIcon } from './apis/gemini.js';
import { chatGPTView, chatGPTCommands, sendMessage as chatGPTSendMessage, chatGPTTabIcon } from './apis/chatgpt.js'; import { chatGPTView, chatGPTCommands, sendMessage as chatGPTSendMessage, chatGPTTabIcon } from './apis/chatgpt.js';
@@ -65,9 +65,9 @@ export const chatEntry = TextView({
acceptsTab: false, acceptsTab: false,
className: 'sidebar-chat-entry txt txt-smallie', className: 'sidebar-chat-entry txt txt-smallie',
setup: (self) => self setup: (self) => self
.hook(ChatGPT, (self) => { .hook(GPTService, (self) => {
if (APIS[currentApiId].name != 'Assistant (ChatGPT 3.5)') return; if (APIS[currentApiId].name != 'Assistant (GPTs)') return;
self.placeholderText = (ChatGPT.key.length > 0 ? 'Message ChatGPT...' : 'Enter OpenAI API Key...'); self.placeholderText = (GPTService.key.length > 0 ? 'Message the model...' : 'Enter API Key...');
}, 'hasKey') }, 'hasKey')
.hook(Gemini, (self) => { .hook(Gemini, (self) => {
if (APIS[currentApiId].name != 'Assistant (Gemini Pro)') return; if (APIS[currentApiId].name != 'Assistant (Gemini Pro)') return;