chore: parse config method
This commit is contained in:
@@ -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<LLMConfig> {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user