Skip to content

Commit c00b8ff

Browse files
committed
improve moonshot api support (#623)
1 parent eb88fc2 commit c00b8ff

File tree

3 files changed

+49
-43
lines changed

3 files changed

+49
-43
lines changed

src/background/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async function executeApi(session, port, config) {
158158
port,
159159
session.question,
160160
session,
161-
config.apiKey,
161+
config.moonshotApiKey,
162162
session.modelName,
163163
)
164164
}

src/config/index.mjs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ export const poeWebModelKeys = [
6666
'poeAiWeb_Llama_2_70b',
6767
]
6868
export const moonshotApiModelKeys = ['moonshot_v1_8k', 'moonshot_v1_32k', 'moonshot_v1_128k']
69-
const moonshotApiKeyGenerateUrl = 'https://platform.moonshot.cn/console/api-keys'
7069

7170
/**
7271
* @typedef {object} Model
7372
* @property {string} value
7473
* @property {string} desc
75-
* @property {string} [keyGenerateUrl]
7674
*/
7775
/**
7876
* @type {Object.<string,Model>}
@@ -143,17 +141,14 @@ export const Models = {
143141
moonshot_v1_8k: {
144142
value: 'moonshot-v1-8k',
145143
desc: 'Moonshot (8k)',
146-
keyGenerateUrl: moonshotApiKeyGenerateUrl,
147144
},
148145
moonshot_v1_32k: {
149146
value: 'moonshot-v1-32k',
150147
desc: 'Moonshot (32k)',
151-
keyGenerateUrl: moonshotApiKeyGenerateUrl,
152148
},
153149
moonshot_v1_128k: {
154150
value: 'moonshot-v1-128k',
155151
desc: 'Moonshot (128k)',
156-
keyGenerateUrl: moonshotApiKeyGenerateUrl,
157152
},
158153
}
159154

@@ -199,6 +194,7 @@ export const defaultConfig = {
199194

200195
claudeApiKey: '',
201196
chatglmApiKey: '',
197+
moonshotApiKey: '',
202198

203199
customApiKey: '',
204200

@@ -309,11 +305,10 @@ export function getNavigatorLanguage() {
309305
return navigator.language.substring(0, 2)
310306
}
311307

312-
export function isUsingApiKey(configOrSession) {
308+
export function isUsingOpenAiApiKey(configOrSession) {
313309
return (
314310
gptApiModelKeys.includes(configOrSession.modelName) ||
315-
chatgptApiModelKeys.includes(configOrSession.modelName) ||
316-
moonshotApiModelKeys.includes(configOrSession.modelName)
311+
chatgptApiModelKeys.includes(configOrSession.modelName)
317312
)
318313
}
319314

@@ -329,6 +324,10 @@ export function isUsingChatGLMApi(configOrSession) {
329324
return chatglmApiModelKeys.includes(configOrSession.modelName)
330325
}
331326

327+
export function isUsingMoonshotApi(configOrSession) {
328+
return moonshotApiModelKeys.includes(configOrSession.modelName)
329+
}
330+
332331
export function isUsingCustomNameOnlyModel(configOrSession) {
333332
return configOrSession.modelName === 'poeAiWebCustom'
334333
}
@@ -344,13 +343,6 @@ export function isUsingGithubThirdPartyApi(configOrSession) {
344343
return githubThirdPartyApiModelKeys.includes(configOrSession.modelName)
345344
}
346345

347-
export function isSupportBalance(configOrSession) {
348-
return (
349-
gptApiModelKeys.includes(configOrSession.modelName) ||
350-
chatgptApiModelKeys.includes(configOrSession.modelName)
351-
)
352-
}
353-
354346
export async function getPreferredLanguageKey() {
355347
const config = await getUserConfig()
356348
if (config.preferredLanguage === 'auto') return config.userLanguage

src/popup/sections/GeneralPart.jsx

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { useTranslation } from 'react-i18next'
2-
import { useMemo, useState } from 'react'
2+
import { useState } from 'react'
33
import { openUrl } from '../../utils/index.mjs'
44
import {
5-
isSupportBalance,
6-
isUsingApiKey,
5+
isUsingOpenAiApiKey,
76
isUsingAzureOpenAi,
87
isUsingChatGLMApi,
98
isUsingClaude2Api,
@@ -15,6 +14,7 @@ import {
1514
Models,
1615
ThemeMode,
1716
TriggerMode,
17+
isUsingMoonshotApi,
1818
} from '../../config/index.mjs'
1919
import Browser from 'webextension-polyfill'
2020
import { languageList } from '../../config/language.mjs'
@@ -82,10 +82,6 @@ async function checkBilling(apiKey, apiUrl) {
8282
export function GeneralPart({ config, updateConfig }) {
8383
const { t, i18n } = useTranslation()
8484
const [balance, setBalance] = useState(null)
85-
const [currentModel, setCurrentModel] = useState(null)
86-
const showBalance = useMemo(() => {
87-
return isSupportBalance(config)
88-
}, [config])
8985

9086
const getBalance = async () => {
9187
const response = await fetch(`${config.customOpenAiApiUrl}/dashboard/billing/credit_grants`, {
@@ -145,20 +141,20 @@ export function GeneralPart({ config, updateConfig }) {
145141
<span style="display: flex; gap: 15px;">
146142
<select
147143
style={
148-
isUsingApiKey(config) ||
144+
isUsingOpenAiApiKey(config) ||
149145
isUsingMultiModeModel(config) ||
150146
isUsingCustomModel(config) ||
151147
isUsingAzureOpenAi(config) ||
152148
isUsingClaude2Api(config) ||
153-
isUsingCustomNameOnlyModel(config)
149+
isUsingCustomNameOnlyModel(config) ||
150+
isUsingMoonshotApi(config)
154151
? 'width: 50%;'
155152
: undefined
156153
}
157154
required
158155
onChange={(e) => {
159156
const modelName = e.target.value
160157
updateConfig({ modelName: modelName })
161-
setCurrentModel(Models[modelName])
162158
}}
163159
>
164160
{config.activeApiModes.map((modelName) => {
@@ -200,7 +196,7 @@ export function GeneralPart({ config, updateConfig }) {
200196
})}
201197
</select>
202198
)}
203-
{isUsingApiKey(config) && (
199+
{isUsingOpenAiApiKey(config) && (
204200
<span style="width: 50%; display: flex; gap: 5px;">
205201
<input
206202
type="password"
@@ -213,29 +209,23 @@ export function GeneralPart({ config, updateConfig }) {
213209
/>
214210
{config.apiKey.length === 0 ? (
215211
<a
216-
href={
217-
currentModel && 'keyGenerateUrl' in currentModel
218-
? currentModel.keyGenerateUrl
219-
: 'https://platform.openai.com/account/api-keys'
220-
}
212+
href="https://platform.openai.com/account/api-keys"
221213
target="_blank"
222214
rel="nofollow noopener noreferrer"
223215
>
224216
<button style="white-space: nowrap;" type="button">
225217
{t('Get')}
226218
</button>
227219
</a>
228-
) : showBalance ? (
229-
balance ? (
230-
<button type="button" onClick={getBalance}>
231-
{balance}
232-
</button>
233-
) : (
234-
<button type="button" onClick={getBalance}>
235-
{t('Balance')}
236-
</button>
237-
)
238-
) : null}
220+
) : balance ? (
221+
<button type="button" onClick={getBalance}>
222+
{balance}
223+
</button>
224+
) : (
225+
<button type="button" onClick={getBalance}>
226+
{t('Balance')}
227+
</button>
228+
)}
239229
</span>
240230
)}
241231
{isUsingCustomModel(config) && (
@@ -298,6 +288,30 @@ export function GeneralPart({ config, updateConfig }) {
298288
}}
299289
/>
300290
)}
291+
{isUsingMoonshotApi(config) && (
292+
<span style="width: 50%; display: flex; gap: 5px;">
293+
<input
294+
type="password"
295+
value={config.moonshotApiKey}
296+
placeholder={t('Moonshot API Key')}
297+
onChange={(e) => {
298+
const apiKey = e.target.value
299+
updateConfig({ moonshotApiKey: apiKey })
300+
}}
301+
/>
302+
{config.moonshotApiKey.length === 0 && (
303+
<a
304+
href="https://platform.moonshot.cn/console/api-keys"
305+
target="_blank"
306+
rel="nofollow noopener noreferrer"
307+
>
308+
<button style="white-space: nowrap;" type="button">
309+
{t('Get')}
310+
</button>
311+
</a>
312+
)}
313+
</span>
314+
)}
301315
</span>
302316
{isUsingCustomModel(config) && (
303317
<input

0 commit comments

Comments
 (0)