Skip to content

Commit c5b73b1

Browse files
committed
feat: make the chat box display the correct model name, and allow multiple different models to be used at the same time (#49)
1 parent 77cbe1e commit c5b73b1

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

src/background/apis/bing-web.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export async function generateAnswersWithBingWebApi(
4747
answer += token
4848
// remove reference markers [^number^]
4949
answer = answer.replaceAll(/\[\^\d+\^\]/g, '')
50-
port.postMessage({ answer: answer, done: false, session: session })
50+
port.postMessage({ answer: answer, done: false, session: null })
5151
},
5252
})
5353
.catch((err) => {

src/background/apis/chatgpt-web.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ export async function generateAnswersWithChatgptWebApi(port, question, session,
122122

123123
answer = data.message?.content?.parts?.[0]
124124
if (answer) {
125-
port.postMessage({ answer: answer, done: false, session: session })
125+
port.postMessage({ answer: answer, done: false, session: null })
126126
}
127127
},
128128
async onStart() {

src/background/index.mjs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
defaultConfig,
1616
getUserConfig,
1717
gptApiModelKeys,
18+
Models,
1819
} from '../config/index.mjs'
1920
import { isSafari } from '../utils/is-safari'
2021
import { config as menuConfig } from '../content-script/menu-tools'
@@ -58,47 +59,50 @@ Browser.runtime.onConnect.addListener((port) => {
5859
const session = msg.session
5960
if (!session) return
6061
const config = await getUserConfig()
62+
if (!session.modelName) session.modelName = config.modelName
63+
if (!session.aiName) session.aiName = Models[session.modelName].desc
64+
port.postMessage({ session })
6165

6266
try {
63-
if (chatgptWebModelKeys.includes(config.modelName)) {
67+
if (chatgptWebModelKeys.includes(session.modelName)) {
6468
const accessToken = await getChatGptAccessToken()
6569
session.messageId = crypto.randomUUID()
6670
if (session.parentMessageId == null) {
6771
session.parentMessageId = crypto.randomUUID()
6872
}
6973
await generateAnswersWithChatgptWebApi(port, session.question, session, accessToken)
70-
} else if (bingWebModelKeys.includes(config.modelName)) {
74+
} else if (bingWebModelKeys.includes(session.modelName)) {
7175
const accessToken = await getBingAccessToken()
7276
await generateAnswersWithBingWebApi(
7377
port,
7478
session.question,
7579
session,
7680
accessToken,
77-
config.modelName,
81+
session.modelName,
7882
)
79-
} else if (gptApiModelKeys.includes(config.modelName)) {
83+
} else if (gptApiModelKeys.includes(session.modelName)) {
8084
await generateAnswersWithGptCompletionApi(
8185
port,
8286
session.question,
8387
session,
8488
config.apiKey,
85-
config.modelName,
89+
session.modelName,
8690
)
87-
} else if (chatgptApiModelKeys.includes(config.modelName)) {
91+
} else if (chatgptApiModelKeys.includes(session.modelName)) {
8892
await generateAnswersWithChatgptApi(
8993
port,
9094
session.question,
9195
session,
9296
config.apiKey,
93-
config.modelName,
97+
session.modelName,
9498
)
95-
} else if (customApiModelKeys.includes(config.modelName)) {
99+
} else if (customApiModelKeys.includes(session.modelName)) {
96100
await generateAnswersWithCustomApi(
97101
port,
98102
session.question,
99103
session,
100104
config.apiKey,
101-
config.modelName,
105+
session.modelName,
102106
)
103107
}
104108
} catch (err) {

src/components/ConversationCard/index.jsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ class ConversationItemData extends Object {
1818
/**
1919
* @param {'question'|'answer'|'error'} type
2020
* @param {string} content
21-
* @param {object} session
2221
* @param {bool} done
2322
*/
24-
constructor(type, content, session = null, done = false) {
23+
constructor(type, content, done = false) {
2524
super()
2625
this.type = type
2726
this.content = content
28-
this.session = session
2927
this.done = done
3028
}
3129
}
@@ -41,7 +39,7 @@ function ConversationCard(props) {
4139
*/
4240
const [conversationItemData, setConversationItemData] = useState(
4341
(() => {
44-
if (props.session.conversationRecords.length === 0)
42+
if (session.conversationRecords.length === 0)
4543
if (props.question)
4644
return [
4745
new ConversationItemData(
@@ -52,13 +50,9 @@ function ConversationCard(props) {
5250
else return []
5351
else {
5452
const ret = []
55-
for (const record of props.session.conversationRecords) {
56-
ret.push(
57-
new ConversationItemData('question', record.question + '\n<hr/>', props.session, true),
58-
)
59-
ret.push(
60-
new ConversationItemData('answer', record.answer + '\n<hr/>', props.session, true),
61-
)
53+
for (const record of session.conversationRecords) {
54+
ret.push(new ConversationItemData('question', record.question + '\n<hr/>', true))
55+
ret.push(new ConversationItemData('answer', record.answer + '\n<hr/>', true))
6256
}
6357
return ret
6458
}
@@ -106,7 +100,6 @@ function ConversationCard(props) {
106100
newType,
107101
appended ? copy[index].content + value : value,
108102
)
109-
copy[index].session = { ...session }
110103
copy[index].done = done
111104
return copy
112105
})
@@ -249,7 +242,7 @@ function ConversationCard(props) {
249242
content={data.content}
250243
key={idx}
251244
type={data.type}
252-
session={data.session}
245+
session={session}
253246
done={data.done}
254247
port={port}
255248
/>

src/components/ConversationItem/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function ConversationItem({ type, content, session, done, port }) {
3434
return (
3535
<div className={type} dir="auto">
3636
<div className="gpt-header">
37-
<p>{session ? 'ChatGPT:' : 'Loading...'}</p>
37+
<p>{session && session.aiName ? session.aiName : 'Loading...'}</p>
3838
<div style="display: flex; gap: 15px;">
3939
{!done && (
4040
<button

src/utils/init-session.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* @typedef {object} Session
1010
* @property {string|null} question
1111
* @property {Object[]|null} conversationRecords
12+
* @property {string|null} aiName
13+
* @property {string|null} modelName
1214
* @property {string|null} conversationId - chatGPT web mode
1315
* @property {string|null} messageId - chatGPT web mode
1416
* @property {string|null} parentMessageId - chatGPT web mode
@@ -24,6 +26,8 @@ export function initSession({ question = null, conversationRecords = [] } = {})
2426
// common
2527
question,
2628
conversationRecords,
29+
aiName: null,
30+
modelName: null,
2731

2832
// chatgpt-web
2933
conversationId: null,

0 commit comments

Comments
 (0)