Skip to content

Commit

Permalink
feat: independent Claude.ai model support (#457, #476, #477, #503)
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Sep 23, 2023
1 parent 40ae457 commit 1b63f11
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 8 deletions.
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@nem035/gpt-3-encoder": "^1.1.7",
"@picocss/pico": "^1.5.9",
"@primer/octicons-react": "^18.3.0",
"claude-ai": "^1.2.2",
"countries-list": "^2.6.1",
"diff": "^5.1.0",
"eventsource-parser": "^1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/background/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
bingWebModelKeys,
chatgptApiModelKeys,
chatgptWebModelKeys,
claudeWebModelKeys,
customApiModelKeys,
defaultConfig,
getUserConfig,
Expand All @@ -34,11 +35,13 @@ import {
getBardCookies,
getBingAccessToken,
getChatGptAccessToken,
getClaudeSessionKey,
registerPortListener,
} from '../services/wrappers.mjs'
import { refreshMenu } from './menus.mjs'
import { registerCommands } from './commands.mjs'
import { generateAnswersWithBardWebApi } from '../services/apis/bard-web.mjs'
import { generateAnswersWithClaudeWebApi } from '../services/apis/claude-web.mjs'

function setPortProxy(port, proxyTabId) {
port.proxy = Browser.tabs.connect(proxyTabId)
Expand Down Expand Up @@ -123,6 +126,9 @@ async function executeApi(session, port, config) {
} else if (bardWebModelKeys.includes(session.modelName)) {
const cookies = await getBardCookies()
await generateAnswersWithBardWebApi(port, session.question, session, cookies)
} else if (claudeWebModelKeys.includes(session.modelName)) {
const sessionKey = await getClaudeSessionKey()
await generateAnswersWithClaudeWebApi(port, session.question, session, sessionKey)
}
}

Expand Down
19 changes: 11 additions & 8 deletions src/config/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const chatgptWebModelKeys = [
]
export const bingWebModelKeys = ['bingFree4', 'bingFreeSydney']
export const bardWebModelKeys = ['bardWebFree']
export const claudeWebModelKeys = ['claude2WebFree']
export const gptApiModelKeys = ['gptApiDavinci']
export const chatgptApiModelKeys = [
'chatgptApi35',
Expand Down Expand Up @@ -81,6 +82,7 @@ export const Models = {
value: 'gpt-3.5-turbo-16k-0613',
desc: 'ChatGPT (GPT-3.5-turbo-16k 0613)',
},
claude2WebFree: { value: 'claude-2', desc: 'Claude.ai (Web, Claude 2)' },
bingFree4: { value: '', desc: 'Bing (Web, GPT-4)' },
bingFreeSydney: { value: '', desc: 'Bing (Web, GPT-4, Sydney)' },
bardWebFree: { value: '', desc: 'Bard (Web)' },
Expand Down Expand Up @@ -175,21 +177,22 @@ export const defaultConfig = {
activeApiModes: [
'chatgptFree35',
'chatgptFree35Mobile',
'chatgptPlus4',
'chatgptPlus4Mobile',
// 'chatgptPlus4',
// 'chatgptPlus4Mobile',
'chatgptApi35',
'chatgptApi35_16k',
'claude2WebFree',
'bingFree4',
'bingFreeSydney',
'poeAiWebSage', //poe.com/Assistant
'poeAiWebGPT4',
'poeAiWebGPT4_32k',
'poeAiWebClaudePlus',
'poeAiWebClaude100k',
// 'poeAiWebSage', //poe.com/Assistant
// 'poeAiWebGPT4',
// 'poeAiWebGPT4_32k',
// 'poeAiWebClaudePlus',
// 'poeAiWebClaude100k',
'chatgptApi4_8k',
'customModel',
'azureOpenAi',
'poeAiWebCustom',
// 'poeAiWebCustom',
],
activeSelectionTools: ['translate', 'summary', 'polish', 'code', 'ask'],
activeSiteAdapters: [
Expand Down
42 changes: 42 additions & 0 deletions src/services/apis/claude-web.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { pushRecord } from './shared.mjs'
import Claude from 'claude-ai'

/**
* @param {Runtime.Port} port
* @param {string} question
* @param {Session} session
* @param {string} sessionKey
*/
export async function generateAnswersWithClaudeWebApi(port, question, session, sessionKey) {
const bot = new Claude({ sessionKey })
await bot.init()

let answer = ''
const progressFunc = ({ completion }) => {
answer = completion
port.postMessage({ answer: answer, done: false, session: null })
}

const doneFunc = () => {
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: answer, done: true, session: session })
}

if (!session.claude_conversation)
await bot
.startConversation(question, {
progress: progressFunc,
done: doneFunc,
})
.then((conversation) => {
session.claude_conversation = conversation
port.postMessage({ answer: answer, done: true, session: session })
})
else
await bot.sendMessage(question, {
conversation: session.claude_conversation,
progress: progressFunc,
done: doneFunc,
})
}
4 changes: 4 additions & 0 deletions src/services/init-session.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { v4 as uuidv4 } from 'uuid'
* @property {Object|null} bingWeb_jailbreakConversationCache
* @property {number|null} poe_chatId
* @property {object|null} bard_conversationObj
* @property {object|null} claude_conversation
*/
/**
* @param {string|null} question
Expand Down Expand Up @@ -78,5 +79,8 @@ export function initSession({

// bard
bard_conversationObj: null,

// claude.ai
claude_conversation: null,
}
}
4 changes: 4 additions & 0 deletions src/services/wrappers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ export async function getBardCookies() {
return '__Secure-1PSID=' + token
}

export async function getClaudeSessionKey() {
return (await Browser.cookies.get({ url: 'https://claude.ai/', name: 'sessionKey' }))?.value
}

export function registerPortListener(executor) {
Browser.runtime.onConnect.addListener((port) => {
console.debug('connected')
Expand Down

0 comments on commit 1b63f11

Please sign in to comment.