From 66af38a82802bdec390e54cf451307c4cda05313 Mon Sep 17 00:00:00 2001 From: josc146 Date: Mon, 27 Mar 2023 13:50:26 +0800 Subject: [PATCH] feat: make the chat box display the correct model name, and allow multiple different models to be used at the same time (#49) --- src/background/apis/bing-web.mjs | 2 +- src/background/apis/chatgpt-web.mjs | 2 +- src/background/index.mjs | 22 +++++++++++++--------- src/components/ConversationCard/index.jsx | 19 ++++++------------- src/components/ConversationItem/index.jsx | 2 +- src/utils/init-session.mjs | 4 ++++ 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/background/apis/bing-web.mjs b/src/background/apis/bing-web.mjs index 304b523..fdb4d61 100644 --- a/src/background/apis/bing-web.mjs +++ b/src/background/apis/bing-web.mjs @@ -47,7 +47,7 @@ export async function generateAnswersWithBingWebApi( answer += token // remove reference markers [^number^] answer = answer.replaceAll(/\[\^\d+\^\]/g, '') - port.postMessage({ answer: answer, done: false, session: session }) + port.postMessage({ answer: answer, done: false, session: null }) }, }) .catch((err) => { diff --git a/src/background/apis/chatgpt-web.mjs b/src/background/apis/chatgpt-web.mjs index 0ffb3db..557bf10 100644 --- a/src/background/apis/chatgpt-web.mjs +++ b/src/background/apis/chatgpt-web.mjs @@ -122,7 +122,7 @@ export async function generateAnswersWithChatgptWebApi(port, question, session, answer = data.message?.content?.parts?.[0] if (answer) { - port.postMessage({ answer: answer, done: false, session: session }) + port.postMessage({ answer: answer, done: false, session: null }) } }, async onStart() { diff --git a/src/background/index.mjs b/src/background/index.mjs index a81e2c8..7ab8f24 100644 --- a/src/background/index.mjs +++ b/src/background/index.mjs @@ -15,6 +15,7 @@ import { defaultConfig, getUserConfig, gptApiModelKeys, + Models, } from '../config/index.mjs' import { isSafari } from '../utils/is-safari' import { config as menuConfig } from '../content-script/menu-tools' @@ -58,47 +59,50 @@ Browser.runtime.onConnect.addListener((port) => { const session = msg.session if (!session) return const config = await getUserConfig() + if (!session.modelName) session.modelName = config.modelName + if (!session.aiName) session.aiName = Models[session.modelName].desc + port.postMessage({ session }) try { - if (chatgptWebModelKeys.includes(config.modelName)) { + if (chatgptWebModelKeys.includes(session.modelName)) { const accessToken = await getChatGptAccessToken() session.messageId = crypto.randomUUID() if (session.parentMessageId == null) { session.parentMessageId = crypto.randomUUID() } await generateAnswersWithChatgptWebApi(port, session.question, session, accessToken) - } else if (bingWebModelKeys.includes(config.modelName)) { + } else if (bingWebModelKeys.includes(session.modelName)) { const accessToken = await getBingAccessToken() await generateAnswersWithBingWebApi( port, session.question, session, accessToken, - config.modelName, + session.modelName, ) - } else if (gptApiModelKeys.includes(config.modelName)) { + } else if (gptApiModelKeys.includes(session.modelName)) { await generateAnswersWithGptCompletionApi( port, session.question, session, config.apiKey, - config.modelName, + session.modelName, ) - } else if (chatgptApiModelKeys.includes(config.modelName)) { + } else if (chatgptApiModelKeys.includes(session.modelName)) { await generateAnswersWithChatgptApi( port, session.question, session, config.apiKey, - config.modelName, + session.modelName, ) - } else if (customApiModelKeys.includes(config.modelName)) { + } else if (customApiModelKeys.includes(session.modelName)) { await generateAnswersWithCustomApi( port, session.question, session, config.apiKey, - config.modelName, + session.modelName, ) } } catch (err) { diff --git a/src/components/ConversationCard/index.jsx b/src/components/ConversationCard/index.jsx index e39026c..fd4b5a9 100644 --- a/src/components/ConversationCard/index.jsx +++ b/src/components/ConversationCard/index.jsx @@ -18,14 +18,12 @@ class ConversationItemData extends Object { /** * @param {'question'|'answer'|'error'} type * @param {string} content - * @param {object} session * @param {bool} done */ - constructor(type, content, session = null, done = false) { + constructor(type, content, done = false) { super() this.type = type this.content = content - this.session = session this.done = done } } @@ -41,7 +39,7 @@ function ConversationCard(props) { */ const [conversationItemData, setConversationItemData] = useState( (() => { - if (props.session.conversationRecords.length === 0) + if (session.conversationRecords.length === 0) if (props.question) return [ new ConversationItemData( @@ -52,13 +50,9 @@ function ConversationCard(props) { else return [] else { const ret = [] - for (const record of props.session.conversationRecords) { - ret.push( - new ConversationItemData('question', record.question + '\n
', props.session, true), - ) - ret.push( - new ConversationItemData('answer', record.answer + '\n
', props.session, true), - ) + for (const record of session.conversationRecords) { + ret.push(new ConversationItemData('question', record.question + '\n
', true)) + ret.push(new ConversationItemData('answer', record.answer + '\n
', true)) } return ret } @@ -106,7 +100,6 @@ function ConversationCard(props) { newType, appended ? copy[index].content + value : value, ) - copy[index].session = { ...session } copy[index].done = done return copy }) @@ -249,7 +242,7 @@ function ConversationCard(props) { content={data.content} key={idx} type={data.type} - session={data.session} + session={session} done={data.done} port={port} /> diff --git a/src/components/ConversationItem/index.jsx b/src/components/ConversationItem/index.jsx index 5896bd4..114af1b 100644 --- a/src/components/ConversationItem/index.jsx +++ b/src/components/ConversationItem/index.jsx @@ -34,7 +34,7 @@ export function ConversationItem({ type, content, session, done, port }) { return (
-

{session ? 'ChatGPT:' : 'Loading...'}

+

{session && session.aiName ? session.aiName : 'Loading...'}

{!done && (