diff --git a/AGENTS.md b/AGENTS.md index 99ee047..c0d4306 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -123,3 +123,4 @@ Query params configure `PageAgentConfig` in `src/umd.ts`. - Explicit typing for exported/public APIs - ESLint relaxes some unsafe rules for rapid iteration +- All code and comments must be in English. diff --git a/packages/llms/src/OpenAILenientClient.ts b/packages/llms/src/OpenAILenientClient.ts index 64c723d..b35f3f1 100644 --- a/packages/llms/src/OpenAILenientClient.ts +++ b/packages/llms/src/OpenAILenientClient.ts @@ -6,10 +6,12 @@ import type { InvokeResult, LLMClient, LLMConfig, MacroToolInput, Message, Tool import { lenientParseMacroToolCall, modelPatch, zodToOpenAITool } from './utils' export class OpenAIClient implements LLMClient { - config: LLMConfig + config: Required + private fetch: typeof globalThis.fetch - constructor(config: LLMConfig) { + constructor(config: Required) { this.config = config + this.fetch = config.customFetch } async invoke( @@ -23,7 +25,7 @@ export class OpenAIClient implements LLMClient { // 2. Call API let response: Response try { - response = await fetch(`${this.config.baseURL}/chat/completions`, { + response = await this.fetch(`${this.config.baseURL}/chat/completions`, { method: 'POST', headers: { 'Content-Type': 'application/json', diff --git a/packages/llms/src/index.ts b/packages/llms/src/index.ts index 644b308..38f2527 100644 --- a/packages/llms/src/index.ts +++ b/packages/llms/src/index.ts @@ -71,6 +71,7 @@ export function parseLLMConfig(config: LLMConfig): Required { temperature: config.temperature ?? DEFAULT_TEMPERATURE, maxTokens: config.maxTokens ?? DEFAULT_MAX_TOKENS, maxRetries: config.maxRetries ?? LLM_MAX_RETRIES, + customFetch: config.customFetch ?? globalThis.fetch, } } @@ -83,13 +84,7 @@ export class LLM extends EventTarget { this.config = parseLLMConfig(config) // Default to OpenAI client - this.client = new OpenAIClient({ - model: this.config.model, - apiKey: this.config.apiKey, - baseURL: this.config.baseURL, - temperature: this.config.temperature, - maxTokens: this.config.maxTokens, - }) + this.client = new OpenAIClient(this.config) } /** diff --git a/packages/llms/src/types.ts b/packages/llms/src/types.ts index d978417..10e547c 100644 --- a/packages/llms/src/types.ts +++ b/packages/llms/src/types.ts @@ -71,9 +71,17 @@ export interface LLMConfig { baseURL?: string apiKey?: string model?: string + temperature?: number maxTokens?: number maxRetries?: number + + /** + * Custom fetch function for LLM API requests. + * Use this to customize headers, credentials, proxy, etc. + * The response should follow OpenAI API format. + */ + customFetch?: typeof globalThis.fetch } /** diff --git a/packages/website/src/docs/integration/configuration/page.tsx b/packages/website/src/docs/integration/configuration/page.tsx index 1af83a9..9b6ef44 100644 --- a/packages/website/src/docs/integration/configuration/page.tsx +++ b/packages/website/src/docs/integration/configuration/page.tsx @@ -18,6 +18,13 @@ interface LLMConfig { temperature?: number maxTokens?: number maxRetries?: number + + /** + * Custom fetch function for LLM API requests. + * Use this to customize headers, credentials, proxy, etc. + * The response should follow OpenAI API format. + */ + customFetch?: typeof globalThis.fetch } interface AgentConfig {