Skip to content

Commit

Permalink
Add support for kimi.moonshot.cn (#656)
Browse files Browse the repository at this point in the history
* Add support for kimi.moonshot.cn

* some improvements

---------

Co-authored-by: josc146 <[email protected]>
  • Loading branch information
xxcdd and josStorer authored Mar 22, 2024
1 parent c00b8ff commit 21b8468
Show file tree
Hide file tree
Showing 7 changed files with 659 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
chatgptApiModelKeys,
chatgptWebModelKeys,
claudeWebModelKeys,
moonshotWebModelKeys,
customApiModelKeys,
defaultConfig,
getUserConfig,
Expand All @@ -46,6 +47,7 @@ import { registerCommands } from './commands.mjs'
import { generateAnswersWithBardWebApi } from '../services/apis/bard-web.mjs'
import { generateAnswersWithClaudeWebApi } from '../services/apis/claude-web.mjs'
import { generateAnswersWithMoonshotCompletionApi } from '../services/apis/moonshot-api.mjs'
import { generateAnswersWithMoonshotWebApi } from '../services/apis/moonshot-web.mjs'

function setPortProxy(port, proxyTabId) {
port.proxy = Browser.tabs.connect(proxyTabId)
Expand Down Expand Up @@ -161,6 +163,14 @@ async function executeApi(session, port, config) {
config.moonshotApiKey,
session.modelName,
)
} else if (moonshotWebModelKeys.includes(session.modelName)) {
await generateAnswersWithMoonshotWebApi(
port,
session.question,
session,
config,
session.modelName,
)
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/config/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const chatgptWebModelKeys = [
export const bingWebModelKeys = ['bingFree4', 'bingFreeSydney']
export const bardWebModelKeys = ['bardWebFree']
export const claudeWebModelKeys = ['claude2WebFree']
export const moonshotWebModelKeys = ['moonshotWebFree']
export const gptApiModelKeys = ['gptApiInstruct', 'gptApiDavinci']
export const chatgptApiModelKeys = [
'chatgptApi35',
Expand Down Expand Up @@ -105,6 +106,8 @@ export const Models = {
bingFree4: { value: '', desc: 'Bing (Web, GPT-4)' },
bingFreeSydney: { value: '', desc: 'Bing (Web, GPT-4, Sydney)' },

moonshotWebFree: { value: '', desc: 'Kimi.Moonshot (Web, 100k)' },

bardWebFree: { value: '', desc: 'Gemini (Web)' },

chatglmTurbo: { value: 'chatglm_turbo', desc: 'ChatGLM (ChatGLM-Turbo)' },
Expand Down Expand Up @@ -230,6 +233,7 @@ export const defaultConfig = {
'chatgptApi4_8k',
'claude2WebFree',
'bingFree4',
'moonshotWebFree',
'chatglmTurbo',
'customModel',
'azureOpenAi',
Expand All @@ -255,6 +259,8 @@ export const defaultConfig = {
chatgptTabId: 0,
chatgptArkoseReqUrl: '',
chatgptArkoseReqForm: '',
kimiMoonShotRefreshToken: '',
kimiMoonShotAccessToken: '',

// unchangeable

Expand Down Expand Up @@ -339,6 +345,11 @@ export function isUsingAzureOpenAi(configOrSession) {
export function isUsingClaude2Api(configOrSession) {
return claudeApiModelKeys.includes(configOrSession.modelName)
}

export function isUsingMoonshotWeb(configOrSession) {
return moonshotWebModelKeys.includes(configOrSession.modelName)
}

export function isUsingGithubThirdPartyApi(configOrSession) {
return githubThirdPartyApiModelKeys.includes(configOrSession.modelName)
}
Expand Down
10 changes: 9 additions & 1 deletion src/content-script/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getPreferredLanguageKey,
getUserConfig,
setAccessToken,
setUserConfig,
} from '../config/index.mjs'
import {
createElementAtPosition,
Expand Down Expand Up @@ -289,7 +290,14 @@ async function prepareForStaticCard() {
}

async function overwriteAccessToken() {
if (location.hostname !== 'chat.openai.com') return
if (location.hostname !== 'chat.openai.com') {
if (location.hostname === 'kimi.moonshot.cn') {
setUserConfig({
kimiMoonShotRefreshToken: window.localStorage.refresh_token,
})
}
return
}

let data
if (location.pathname === '/api/auth/session') {
Expand Down
1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"https://*.poe.com/*",
"https://*.google.com/*",
"https://claude.ai/*",
"https://*.moonshot.cn/*",
"<all_urls>"
],
"permissions": [
Expand Down
1 change: 1 addition & 0 deletions src/manifest.v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"https://*.poe.com/",
"https://*.google.com/",
"https://claude.ai/",
"https://*.moonshot.cn/*",
"<all_urls>"
],
"background": {
Expand Down
Loading

0 comments on commit 21b8468

Please sign in to comment.