refactor(llms): split AbortError out of InvokeError

This commit is contained in:
Simon
2026-06-03 23:00:33 +08:00
parent fb8de08c39
commit fd12fb9f1b
4 changed files with 6 additions and 14 deletions

View File

@@ -368,7 +368,6 @@ export class PageAgentCore extends EventTarget {
description: 'You MUST call this tool every step!',
inputSchema: macroToolSchema as z.ZodType<MacroToolInput>,
execute: async (input: MacroToolInput): Promise<MacroToolResult> => {
// abort — throws DOMException whose .name === 'AbortError'
this.#abortController.signal.throwIfAborted()
console.log(chalk.blue.bold('MacroTool input'), input)

View File

@@ -25,8 +25,7 @@ export class OpenAIClient implements LLMClient {
abortSignal?: AbortSignal,
options?: InvokeOptions
): Promise<InvokeResult> {
// in case user aborted before invoking
if (abortSignal?.aborted) throw new InvokeError(InvokeErrorTypes.ABORTED, 'Aborted')
abortSignal?.throwIfAborted()
// 1. Convert tools to OpenAI format
const openaiTools = Object.entries(tools).map(([name, t]) => zodToOpenAITool(name, t))
@@ -73,9 +72,7 @@ export class OpenAIClient implements LLMClient {
signal: abortSignal,
})
} catch (error: unknown) {
if ((error as any)?.name === 'AbortError') {
throw new InvokeError(InvokeErrorTypes.ABORTED, 'Aborted', error)
}
if ((error as any)?.name === 'AbortError') throw error
console.error(error)
throw new InvokeError(InvokeErrorTypes.NETWORK_ERROR, 'Network request failed', error)
}
@@ -217,9 +214,7 @@ export class OpenAIClient implements LLMClient {
try {
toolResult = await tool.execute(toolInput)
} catch (error: unknown) {
if ((error as any)?.name === 'AbortError') {
throw new InvokeError(InvokeErrorTypes.ABORTED, 'Aborted', error)
}
if ((error as any)?.name === 'AbortError') throw error
throw new InvokeError(
InvokeErrorTypes.TOOL_EXECUTION_ERROR,
`Tool execution failed: ${(error as Error)?.message}`,

View File

@@ -1,5 +1,5 @@
/**
* Error types and error handling for LLM invocations
* Error types and error handling for LLM invocations.
*/
export const InvokeErrorTypes = {
@@ -14,7 +14,6 @@ export const InvokeErrorTypes = {
UNKNOWN: 'unknown',
// Non-retryable
ABORTED: 'aborted', // User aborted via AbortSignal — instance has name='AbortError'
CONFIG_ERROR: 'config_error', // Invalid local configuration or hook
AUTH_ERROR: 'auth_error', // Authentication failed
CONTEXT_LENGTH: 'context_length', // Prompt too long
@@ -44,9 +43,7 @@ export class InvokeError extends Error {
constructor(type: InvokeErrorType, message: string, rawError?: unknown, rawResponse?: unknown) {
super(message)
// ABORTED conforms to the web platform convention so any consumer using
// `err.name === 'AbortError'` (including native DOMException handlers) Just Works.
this.name = type === InvokeErrorTypes.ABORTED ? 'AbortError' : 'InvokeError'
this.name = 'InvokeError'
this.type = type
this.retryable = RETRYABLE_TYPES.includes(type)
this.rawError = rawError

View File

@@ -78,6 +78,7 @@ async function withRetry<T>(
try {
return await fn()
} catch (error: unknown) {
if ((error as any)?.name === 'AbortError') throw error
if (error instanceof InvokeError && !error.retryable) throw error
attempt++
if (attempt > settings.maxRetries) throw error