Skip to content

Commit ed44e86

Browse files
committed
WIP, custom API Modes
1 parent e4cd838 commit ed44e86

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

src/config/index.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const Models = {
169169
chatgptApi35: { value: 'gpt-3.5-turbo', desc: 'ChatGPT (GPT-3.5-turbo)' },
170170
chatgptApi35_16k: { value: 'gpt-3.5-turbo-16k', desc: 'ChatGPT (GPT-3.5-turbo-16k)' },
171171

172-
chatgptApi4o_128k: { value: 'gpt-4o', desc: 'ChatGPT (GPT-4o)' },
172+
chatgptApi4o_128k: { value: 'gpt-4o', desc: 'ChatGPT (GPT-4o, 128k)' },
173173
chatgptApi4oMini: { value: 'gpt-4o-mini', desc: 'ChatGPT (GPT-4o mini)' },
174174
chatgptApi4_8k: { value: 'gpt-4', desc: 'ChatGPT (GPT-4-8k)' },
175175
chatgptApi4_32k: { value: 'gpt-4-32k', desc: 'ChatGPT (GPT-4-32k)' },
@@ -341,10 +341,13 @@ export const defaultConfig = {
341341
// others
342342

343343
alwaysCreateNewConversationWindow: false,
344+
// The handling of activeApiModes and customApiModes is somewhat complex.
345+
// It does not directly convert activeApiModes into customApiModes, which is for compatibility considerations.
346+
// It allows the content of activeApiModes to change with version updates when the user has not customized ApiModes.
347+
// If it were directly written into customApiModes, the value would become fixed, even if the user has not made any customizations.
344348
activeApiModes: [
345349
'chatgptFree35',
346350
'chatgptFree4o',
347-
'chatgptPlus4',
348351
'chatgptApi35',
349352
'chatgptApi4o_128k',
350353
'claude2WebFree',
@@ -354,7 +357,6 @@ export const defaultConfig = {
354357
'moonshot_v1_8k',
355358
'chatglmTurbo',
356359
'customModel',
357-
'ollamaModel',
358360
'azureOpenAi',
359361
],
360362
customApiModes: [

src/popup/sections/ApiModes.jsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useTranslation } from 'react-i18next'
22
import PropTypes from 'prop-types'
3-
import { apiModeToModelName, modelNameToDesc } from '../../utils/index.mjs'
3+
import { apiModeToModelName, modelNameToApiMode, modelNameToDesc } from '../../utils/index.mjs'
44
import { PencilIcon, TrashIcon } from '@primer/octicons-react'
55
import { useState } from 'react'
66
import {
@@ -31,6 +31,20 @@ export function ApiModes({ config, updateConfig }) {
3131
const [editingApiMode, setEditingApiMode] = useState(defaultApiMode)
3232
const [editingIndex, setEditingIndex] = useState(-1)
3333

34+
const stringApiModes = config.customApiModes.map(apiModeToModelName)
35+
const originalApiModes = config.activeApiModes
36+
.map((modelName) => {
37+
// 'customModel' is always active
38+
if (stringApiModes.includes(modelName) || modelName === 'customModel') {
39+
return
40+
}
41+
if (modelName === 'azureOpenAi') modelName += '-' + config.azureDeploymentName
42+
if (modelName === 'ollama') modelName += '-' + config.ollamaModelName
43+
return modelNameToApiMode(modelName)
44+
})
45+
.filter((apiMode) => apiMode)
46+
const apiModes = [...originalApiModes, ...config.customApiModes]
47+
3448
const editingComponent = (
3549
<div style={{ display: 'flex', flexDirection: 'column', '--spacing': '4px' }}>
3650
<div style={{ display: 'flex', gap: '12px' }}>
@@ -47,12 +61,13 @@ export function ApiModes({ config, updateConfig }) {
4761
e.preventDefault()
4862
if (editingIndex === -1) {
4963
updateConfig({
50-
customApiModes: [...config.customApiModes, editingApiMode],
64+
activeApiModes: [],
65+
customApiModes: [...apiModes, editingApiMode],
5166
})
5267
} else {
53-
const customApiModes = [...config.customApiModes]
68+
const customApiModes = [...apiModes]
5469
customApiModes[editingIndex] = editingApiMode
55-
updateConfig({ customApiModes })
70+
updateConfig({ activeApiModes: [], customApiModes })
5671
}
5772
setEditing(false)
5873
}}
@@ -128,7 +143,7 @@ export function ApiModes({ config, updateConfig }) {
128143

129144
return (
130145
<>
131-
{config.customApiModes.map(
146+
{apiModes.map(
132147
(apiMode, index) =>
133148
apiMode.groupName &&
134149
apiMode.itemName &&
@@ -140,9 +155,9 @@ export function ApiModes({ config, updateConfig }) {
140155
type="checkbox"
141156
checked={apiMode.active}
142157
onChange={(e) => {
143-
const customApiModes = [...config.customApiModes]
158+
const customApiModes = [...apiModes]
144159
customApiModes[index] = { ...apiMode, active: e.target.checked }
145-
updateConfig({ customApiModes })
160+
updateConfig({ activeApiModes: [], customApiModes })
146161
}}
147162
/>
148163
{modelNameToDesc(apiModeToModelName(apiMode), t)}
@@ -163,9 +178,9 @@ export function ApiModes({ config, updateConfig }) {
163178
style={{ cursor: 'pointer' }}
164179
onClick={(e) => {
165180
e.preventDefault()
166-
const customApiModes = [...config.customApiModes]
181+
const customApiModes = [...apiModes]
167182
customApiModes.splice(index, 1)
168-
updateConfig({ customApiModes })
183+
updateConfig({ activeApiModes: [], customApiModes })
169184
}}
170185
>
171186
<TrashIcon />

src/utils/model-name-convert.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ export function modelNameToValue(modelName) {
4242
}
4343

4444
export function isCustomModelName(modelName) {
45-
return modelName.includes('-')
45+
return modelName ? modelName.includes('-') : false
4646
}
4747

4848
export function modelNameToApiMode(modelName) {
4949
const presetPart = modelNameToPresetPart(modelName)
5050
const found =
5151
Object.entries(ModelGroups).find(([k]) => presetPart === k) ||
52-
Object.entries(ModelGroups).find(([, g]) => presetPart in g.value)
52+
Object.entries(ModelGroups).find(([, g]) => g.value.includes(presetPart))
5353
if (found) {
5454
const [groupName] = found
5555
const isCustom = isCustomModelName(modelName)
5656
let customName = ''
57-
if (isCustom) customName = modelNameToCustomPart()
57+
if (isCustom) customName = modelNameToCustomPart(modelName)
5858
return {
5959
groupName,
6060
itemName: presetPart,

0 commit comments

Comments
 (0)