feat: add customFetch config
This commit is contained in:
@@ -123,3 +123,4 @@ Query params configure `PageAgentConfig` in `src/umd.ts`.
|
|||||||
|
|
||||||
- Explicit typing for exported/public APIs
|
- Explicit typing for exported/public APIs
|
||||||
- ESLint relaxes some unsafe rules for rapid iteration
|
- ESLint relaxes some unsafe rules for rapid iteration
|
||||||
|
- All code and comments must be in English.
|
||||||
|
|||||||
@@ -6,10 +6,12 @@ import type { InvokeResult, LLMClient, LLMConfig, MacroToolInput, Message, Tool
|
|||||||
import { lenientParseMacroToolCall, modelPatch, zodToOpenAITool } from './utils'
|
import { lenientParseMacroToolCall, modelPatch, zodToOpenAITool } from './utils'
|
||||||
|
|
||||||
export class OpenAIClient implements LLMClient {
|
export class OpenAIClient implements LLMClient {
|
||||||
config: LLMConfig
|
config: Required<LLMConfig>
|
||||||
|
private fetch: typeof globalThis.fetch
|
||||||
|
|
||||||
constructor(config: LLMConfig) {
|
constructor(config: Required<LLMConfig>) {
|
||||||
this.config = config
|
this.config = config
|
||||||
|
this.fetch = config.customFetch
|
||||||
}
|
}
|
||||||
|
|
||||||
async invoke(
|
async invoke(
|
||||||
@@ -23,7 +25,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
// 2. Call API
|
// 2. Call API
|
||||||
let response: Response
|
let response: Response
|
||||||
try {
|
try {
|
||||||
response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
response = await this.fetch(`${this.config.baseURL}/chat/completions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ export function parseLLMConfig(config: LLMConfig): Required<LLMConfig> {
|
|||||||
temperature: config.temperature ?? DEFAULT_TEMPERATURE,
|
temperature: config.temperature ?? DEFAULT_TEMPERATURE,
|
||||||
maxTokens: config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
maxTokens: config.maxTokens ?? DEFAULT_MAX_TOKENS,
|
||||||
maxRetries: config.maxRetries ?? LLM_MAX_RETRIES,
|
maxRetries: config.maxRetries ?? LLM_MAX_RETRIES,
|
||||||
|
customFetch: config.customFetch ?? globalThis.fetch,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,13 +84,7 @@ export class LLM extends EventTarget {
|
|||||||
this.config = parseLLMConfig(config)
|
this.config = parseLLMConfig(config)
|
||||||
|
|
||||||
// Default to OpenAI client
|
// Default to OpenAI client
|
||||||
this.client = new OpenAIClient({
|
this.client = new OpenAIClient(this.config)
|
||||||
model: this.config.model,
|
|
||||||
apiKey: this.config.apiKey,
|
|
||||||
baseURL: this.config.baseURL,
|
|
||||||
temperature: this.config.temperature,
|
|
||||||
maxTokens: this.config.maxTokens,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -71,9 +71,17 @@ export interface LLMConfig {
|
|||||||
baseURL?: string
|
baseURL?: string
|
||||||
apiKey?: string
|
apiKey?: string
|
||||||
model?: string
|
model?: string
|
||||||
|
|
||||||
temperature?: number
|
temperature?: number
|
||||||
maxTokens?: number
|
maxTokens?: number
|
||||||
maxRetries?: 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
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -18,6 +18,13 @@ interface LLMConfig {
|
|||||||
temperature?: number
|
temperature?: number
|
||||||
maxTokens?: number
|
maxTokens?: number
|
||||||
maxRetries?: 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 {
|
interface AgentConfig {
|
||||||
|
|||||||
Reference in New Issue
Block a user