diff --git a/src/config/index.ts b/src/config/index.ts index 97d4937..860776b 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -2,9 +2,20 @@ import type { DomConfig } from '@/dom' import type { SupportedLanguage } from '@/i18n' import type { LLMConfig } from '@/llms' +import { DEFAULT_API_KEY, DEFAULT_BASE_URL, DEFAULT_MODEL_NAME, LLM_MAX_RETRIES } from './constants' + export interface UIConfig { // theme?: 'light' | 'dark' language?: SupportedLanguage } export type PageAgentConfig = LLMConfig & DomConfig & UIConfig + +export function parseLLMConfig(config: LLMConfig): Required { + return { + baseURL: config.baseURL ?? DEFAULT_BASE_URL, + apiKey: config.apiKey ?? DEFAULT_API_KEY, + modelName: config.modelName ?? DEFAULT_MODEL_NAME, + maxRetries: config.maxRetries ?? LLM_MAX_RETRIES, + } +} diff --git a/src/entry.ts b/src/entry.ts index 2b353ba..c771685 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -2,7 +2,6 @@ * Auto-run entry for page-agent.js. Insert this script into your page to get page-agent functionality. */ import { PageAgent, type PageAgentConfig } from './PageAgent' -import { DEFAULT_MODEL_NAME } from './config/constants' // Clean up existing instances to prevent multiple injections from bookmarklet if (window.pageAgent) { @@ -20,7 +19,7 @@ console.log('🚀 page-agent.js loaded!') const currentScript = document.currentScript as HTMLScriptElement | null if (currentScript) { const url = new URL(currentScript.src) - const modelName = url.searchParams.get('model') || DEFAULT_MODEL_NAME + const modelName = url.searchParams.get('model') const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN' const config = { modelName, language } as PageAgentConfig window.pageAgent = new PageAgent(config) diff --git a/src/llms/index.ts b/src/llms/index.ts index 81ba965..caa2dff 100644 --- a/src/llms/index.ts +++ b/src/llms/index.ts @@ -37,13 +37,8 @@ import type { LanguageModelUsage, ModelMessage, TypedToolCall, TypedToolResult } import { ToolSet, generateText, stepCountIs } from 'ai' import chalk from 'chalk' -import { - DEFAULT_API_KEY, - DEFAULT_BASE_URL, - DEFAULT_MODEL_NAME, - LLM_MAX_RETRIES, - MACRO_TOOL_NAME, -} from '@/config/constants' +import { parseLLMConfig } from '@/config' +import { MACRO_TOOL_NAME } from '@/config/constants' import { assert } from '@/utils/assert' import { EventBus, getEventBus } from '@/utils/bus' @@ -62,13 +57,7 @@ export class LLM { #bus: EventBus constructor(config: LLMConfig, id: string) { - this.config = { - baseURL: DEFAULT_BASE_URL, - apiKey: DEFAULT_API_KEY, - modelName: DEFAULT_MODEL_NAME, - maxRetries: LLM_MAX_RETRIES, - ...config, - } + this.config = parseLLMConfig(config) this.id = id this.#bus = getEventBus(id)