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 { SupportedLanguage } from '@/i18n'
|
||||||
import type { LLMConfig } from '@/llms'
|
import type { LLMConfig } from '@/llms'
|
||||||
|
|
||||||
|
import { DEFAULT_API_KEY, DEFAULT_BASE_URL, DEFAULT_MODEL_NAME, LLM_MAX_RETRIES } from './constants'
|
||||||
|
|
||||||
export interface UIConfig {
|
export interface UIConfig {
|
||||||
// theme?: 'light' | 'dark'
|
// theme?: 'light' | 'dark'
|
||||||
language?: SupportedLanguage
|
language?: SupportedLanguage
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PageAgentConfig = LLMConfig & DomConfig & UIConfig
|
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.
|
* 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 { PageAgent, type PageAgentConfig } from './PageAgent'
|
||||||
import { DEFAULT_MODEL_NAME } from './config/constants'
|
|
||||||
|
|
||||||
// Clean up existing instances to prevent multiple injections from bookmarklet
|
// Clean up existing instances to prevent multiple injections from bookmarklet
|
||||||
if (window.pageAgent) {
|
if (window.pageAgent) {
|
||||||
@@ -20,7 +19,7 @@ console.log('🚀 page-agent.js loaded!')
|
|||||||
const currentScript = document.currentScript as HTMLScriptElement | null
|
const currentScript = document.currentScript as HTMLScriptElement | null
|
||||||
if (currentScript) {
|
if (currentScript) {
|
||||||
const url = new URL(currentScript.src)
|
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 language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN'
|
||||||
const config = { modelName, language } as PageAgentConfig
|
const config = { modelName, language } as PageAgentConfig
|
||||||
window.pageAgent = new PageAgent(config)
|
window.pageAgent = new PageAgent(config)
|
||||||
|
|||||||
@@ -37,13 +37,8 @@ import type { LanguageModelUsage, ModelMessage, TypedToolCall, TypedToolResult }
|
|||||||
import { ToolSet, generateText, stepCountIs } from 'ai'
|
import { ToolSet, generateText, stepCountIs } from 'ai'
|
||||||
import chalk from 'chalk'
|
import chalk from 'chalk'
|
||||||
|
|
||||||
import {
|
import { parseLLMConfig } from '@/config'
|
||||||
DEFAULT_API_KEY,
|
import { MACRO_TOOL_NAME } from '@/config/constants'
|
||||||
DEFAULT_BASE_URL,
|
|
||||||
DEFAULT_MODEL_NAME,
|
|
||||||
LLM_MAX_RETRIES,
|
|
||||||
MACRO_TOOL_NAME,
|
|
||||||
} from '@/config/constants'
|
|
||||||
import { assert } from '@/utils/assert'
|
import { assert } from '@/utils/assert'
|
||||||
import { EventBus, getEventBus } from '@/utils/bus'
|
import { EventBus, getEventBus } from '@/utils/bus'
|
||||||
|
|
||||||
@@ -62,13 +57,7 @@ export class LLM {
|
|||||||
#bus: EventBus
|
#bus: EventBus
|
||||||
|
|
||||||
constructor(config: LLMConfig, id: string) {
|
constructor(config: LLMConfig, id: string) {
|
||||||
this.config = {
|
this.config = parseLLMConfig(config)
|
||||||
baseURL: DEFAULT_BASE_URL,
|
|
||||||
apiKey: DEFAULT_API_KEY,
|
|
||||||
modelName: DEFAULT_MODEL_NAME,
|
|
||||||
maxRetries: LLM_MAX_RETRIES,
|
|
||||||
...config,
|
|
||||||
}
|
|
||||||
this.id = id
|
this.id = id
|
||||||
|
|
||||||
this.#bus = getEventBus(id)
|
this.#bus = getEventBus(id)
|
||||||
|
|||||||
Reference in New Issue
Block a user