| 28 | } |
| 29 | |
| 30 | export class LLM extends EventTarget { |
| 31 | config: Required<LLMConfig> |
| 32 | client: LLMClient |
| 33 | |
| 34 | constructor(config: LLMConfig) { |
| 35 | super() |
| 36 | this.config = parseLLMConfig(config) |
| 37 | |
| 38 | // Default to OpenAI client |
| 39 | this.client = new OpenAIClient(this.config) |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * - call llm api *once* |
| 44 | * - invoke tool call *once* |
| 45 | * - return the result of the tool |
| 46 | */ |
| 47 | async invoke( |
| 48 | messages: Message[], |
| 49 | tools: Record<string, Tool>, |
| 50 | abortSignal: AbortSignal, |
| 51 | options?: InvokeOptions |
| 52 | ): Promise<InvokeResult> { |
| 53 | return await withRetry(async () => this.client.invoke(messages, tools, abortSignal, options), { |
| 54 | maxRetries: this.config.maxRetries, |
| 55 | onRetry: (attempt, lastError) => { |
| 56 | this.dispatchEvent( |
| 57 | new CustomEvent('retry', { |
| 58 | detail: { attempt, maxAttempts: this.config.maxRetries, lastError }, |
| 59 | }) |
| 60 | ) |
| 61 | }, |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Retry a function until it succeeds or reaches the maximum number of retries. |
nothing calls this directly
no outgoing calls
no test coverage detected