diff --git a/dist/buildinfo.json b/dist/buildinfo.json index efe41dad..55690629 100644 --- a/dist/buildinfo.json +++ b/dist/buildinfo.json @@ -1 +1 @@ -{"sha":"ce28808","timestamp":1734938027} \ No newline at end of file +{"sha":"ba81a00","timestamp":1734938667} \ No newline at end of file diff --git a/dist/index.js b/dist/index.js index dab4feaf..f8cb5dcb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -193,8 +193,8 @@ class ConfigMerger { } } } -const BUILD_TIMESTAMP = 1734938027; -const BUILD_VERSION = "ce28808"; +const BUILD_TIMESTAMP = 1734938667; +const BUILD_VERSION = "ba81a00"; function createAgentUserConfig() { return Object.assign( {}, @@ -1412,14 +1412,18 @@ async function renderOpenAIMessages(prompt, items, supportImage) { } return messages; } -class OpenAIBase { - name = "openai"; - apikey = (context) => { - const length = context.OPENAI_API_KEY.length; - return context.OPENAI_API_KEY[Math.floor(Math.random() * length)]; +function openAIApiKey(context) { + const length = context.OPENAI_API_KEY.length; + return context.OPENAI_API_KEY[Math.floor(Math.random() * length)]; +} +function openAIHeaders(context) { + return { + "Content-Type": "application/json", + "Authorization": `Bearer ${openAIApiKey(context)}` }; } -class OpenAI extends OpenAIBase { +class OpenAI { + name = "openai"; modelKey = "OPENAI_CHAT_MODEL"; enable = (context) => { return context.OPENAI_API_KEY.length > 0; @@ -1430,10 +1434,7 @@ class OpenAI extends OpenAIBase { request = async (params, context, onStream) => { const { prompt, messages } = params; const url = `${context.OPENAI_API_BASE}/chat/completions`; - const header = { - "Content-Type": "application/json", - "Authorization": `Bearer ${this.apikey(context)}` - }; + const header = openAIHeaders(context); const body = { model: context.OPENAI_CHAT_MODEL, ...context.OPENAI_API_EXTRA_PARAMS, @@ -1448,13 +1449,14 @@ class OpenAI extends OpenAIBase { } return loadModelsList(context.OPENAI_CHAT_MODELS_LIST, async (url) => { const data = await fetch(url, { - headers: { Authorization: `Bearer ${this.apikey(context)}` } + headers: openAIHeaders(context) }).then((res) => res.json()); return data.data?.map((model) => model.id) || []; }); }; } -class Dalle extends OpenAIBase { +class Dalle { + name = "openai"; modelKey = "OPENAI_DALLE_API"; enable = (context) => { return context.OPENAI_API_KEY.length > 0; @@ -1464,10 +1466,7 @@ class Dalle extends OpenAIBase { }; request = async (prompt, context) => { const url = `${context.OPENAI_API_BASE}/images/generations`; - const header = { - "Content-Type": "application/json", - "Authorization": `Bearer ${this.apikey(context)}` - }; + const header = openAIHeaders(context); const body = { prompt, n: 1, diff --git a/packages/lib/core/src/agent/chat.ts b/packages/lib/core/src/agent/chat.ts index 34053139..e653ac6c 100644 --- a/packages/lib/core/src/agent/chat.ts +++ b/packages/lib/core/src/agent/chat.ts @@ -3,9 +3,6 @@ import type { ChatAgent, HistoryItem, HistoryModifier, LLMChatParams, UserMessag import { ENV } from '#/config'; import { extractTextContent } from './utils'; -/** - * @returns {(function(string): number)} - */ function tokensCounter(): (text: string) => number { return (text) => { return text.length; diff --git a/packages/lib/core/src/agent/openai.ts b/packages/lib/core/src/agent/openai.ts index 25ad10b5..77ef89a0 100644 --- a/packages/lib/core/src/agent/openai.ts +++ b/packages/lib/core/src/agent/openai.ts @@ -71,15 +71,20 @@ export async function renderOpenAIMessages(prompt: string | undefined, items: Hi return messages; } -class OpenAIBase { - readonly name = 'openai'; - apikey = (context: AgentUserConfig): string => { - const length = context.OPENAI_API_KEY.length; - return context.OPENAI_API_KEY[Math.floor(Math.random() * length)]; +function openAIApiKey(context: AgentUserConfig): string { + const length = context.OPENAI_API_KEY.length; + return context.OPENAI_API_KEY[Math.floor(Math.random() * length)]; +} + +function openAIHeaders(context: AgentUserConfig): Record { + return { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${openAIApiKey(context)}` }; } -export class OpenAI extends OpenAIBase implements ChatAgent { +export class OpenAI implements ChatAgent { + readonly name = 'openai'; readonly modelKey = 'OPENAI_CHAT_MODEL'; readonly enable: AgentEnable = (context: AgentUserConfig): boolean => { @@ -93,10 +98,7 @@ export class OpenAI extends OpenAIBase implements ChatAgent { readonly request: ChatAgentRequest = async (params: LLMChatParams, context: AgentUserConfig, onStream: ChatStreamTextHandler | null): Promise => { const { prompt, messages } = params; const url = `${context.OPENAI_API_BASE}/chat/completions`; - const header = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${this.apikey(context)}`, - }; + const header = openAIHeaders(context); const body = { model: context.OPENAI_CHAT_MODEL, ...context.OPENAI_API_EXTRA_PARAMS, @@ -113,14 +115,15 @@ export class OpenAI extends OpenAIBase implements ChatAgent { } return loadModelsList(context.OPENAI_CHAT_MODELS_LIST, async (url): Promise => { const data = await fetch(url, { - headers: { Authorization: `Bearer ${this.apikey(context)}` }, + headers: openAIHeaders(context), }).then(res => res.json()) as any; return data.data?.map((model: any) => model.id) || []; }); }; } -export class Dalle extends OpenAIBase implements ImageAgent { +export class Dalle implements ImageAgent { + readonly name = 'openai'; readonly modelKey = 'OPENAI_DALLE_API'; readonly enable: AgentEnable = (context: AgentUserConfig): boolean => { @@ -133,10 +136,7 @@ export class Dalle extends OpenAIBase implements ImageAgent { readonly request: ImageAgentRequest = async (prompt: string, context: AgentUserConfig): Promise => { const url = `${context.OPENAI_API_BASE}/images/generations`; - const header = { - 'Content-Type': 'application/json', - 'Authorization': `Bearer ${this.apikey(context)}`, - }; + const header = openAIHeaders(context); const body: any = { prompt, n: 1, diff --git a/packages/lib/core/src/config/version.ts b/packages/lib/core/src/config/version.ts index 299f25a0..8c735335 100644 --- a/packages/lib/core/src/config/version.ts +++ b/packages/lib/core/src/config/version.ts @@ -1,2 +1,2 @@ -export const BUILD_TIMESTAMP = 1734938027; -export const BUILD_VERSION = 'ce28808'; +export const BUILD_TIMESTAMP = 1734938667; +export const BUILD_VERSION = 'ba81a00';