Skip to content

Commit 3415e0b

Browse files
committed
chore: parse config method
1 parent abe58e5 commit 3415e0b

3 files changed

Lines changed: 15 additions & 16 deletions

File tree

src/config/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ import type { DomConfig } from '@/dom'
22
import type { SupportedLanguage } from '@/i18n'
33
import type { LLMConfig } from '@/llms'
44

5+
import { DEFAULT_API_KEY, DEFAULT_BASE_URL, DEFAULT_MODEL_NAME, LLM_MAX_RETRIES } from './constants'
6+
57
export interface UIConfig {
68
// theme?: 'light' | 'dark'
79
language?: SupportedLanguage
810
}
911

1012
export type PageAgentConfig = LLMConfig & DomConfig & UIConfig
13+
14+
export function parseLLMConfig(config: LLMConfig): Required<LLMConfig> {
15+
return {
16+
baseURL: config.baseURL ?? DEFAULT_BASE_URL,
17+
apiKey: config.apiKey ?? DEFAULT_API_KEY,
18+
modelName: config.modelName ?? DEFAULT_MODEL_NAME,
19+
maxRetries: config.maxRetries ?? LLM_MAX_RETRIES,
20+
}
21+
}

src/entry.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* Auto-run entry for page-agent.js. Insert this script into your page to get page-agent functionality.
33
*/
44
import { PageAgent, type PageAgentConfig } from './PageAgent'
5-
import { DEFAULT_MODEL_NAME } from './config/constants'
65

76
// Clean up existing instances to prevent multiple injections from bookmarklet
87
if (window.pageAgent) {
@@ -20,7 +19,7 @@ console.log('🚀 page-agent.js loaded!')
2019
const currentScript = document.currentScript as HTMLScriptElement | null
2120
if (currentScript) {
2221
const url = new URL(currentScript.src)
23-
const modelName = url.searchParams.get('model') || DEFAULT_MODEL_NAME
22+
const modelName = url.searchParams.get('model')
2423
const language = (url.searchParams.get('lang') as 'zh-CN' | 'en-US') || 'zh-CN'
2524
const config = { modelName, language } as PageAgentConfig
2625
window.pageAgent = new PageAgent(config)

src/llms/index.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,8 @@ import type { LanguageModelUsage, ModelMessage, TypedToolCall, TypedToolResult }
3737
import { ToolSet, generateText, stepCountIs } from 'ai'
3838
import chalk from 'chalk'
3939

40-
import {
41-
DEFAULT_API_KEY,
42-
DEFAULT_BASE_URL,
43-
DEFAULT_MODEL_NAME,
44-
LLM_MAX_RETRIES,
45-
MACRO_TOOL_NAME,
46-
} from '@/config/constants'
40+
import { parseLLMConfig } from '@/config'
41+
import { MACRO_TOOL_NAME } from '@/config/constants'
4742
import { assert } from '@/utils/assert'
4843
import { EventBus, getEventBus } from '@/utils/bus'
4944

@@ -62,13 +57,7 @@ export class LLM {
6257
#bus: EventBus
6358

6459
constructor(config: LLMConfig, id: string) {
65-
this.config = {
66-
baseURL: DEFAULT_BASE_URL,
67-
apiKey: DEFAULT_API_KEY,
68-
modelName: DEFAULT_MODEL_NAME,
69-
maxRetries: LLM_MAX_RETRIES,
70-
...config,
71-
}
60+
this.config = parseLLMConfig(config)
7261
this.id = id
7362

7463
this.#bus = getEventBus(id)

0 commit comments

Comments
 (0)