fix(llms): fix abort handling and clean up retry logic
This commit is contained in:
@@ -103,11 +103,14 @@ export class PageAgentCore extends EventTarget {
|
|||||||
this.tools = new Map(tools)
|
this.tools = new Map(tools)
|
||||||
this.pageController = config.pageController
|
this.pageController = config.pageController
|
||||||
|
|
||||||
// Listen to LLM retry events
|
|
||||||
this.#llm.addEventListener('retry', (e) => {
|
this.#llm.addEventListener('retry', (e) => {
|
||||||
const { attempt, maxAttempts } = (e as CustomEvent).detail
|
const { attempt, maxAttempts, lastError } = (e as CustomEvent).detail
|
||||||
this.#emitActivity({ type: 'retrying', attempt, maxAttempts })
|
this.#emitActivity({ type: 'retrying', attempt, maxAttempts })
|
||||||
// Also push to history for panel rendering
|
this.history.push({
|
||||||
|
type: 'error',
|
||||||
|
message: String(lastError),
|
||||||
|
rawResponse: (lastError as InvokeError).rawResponse,
|
||||||
|
})
|
||||||
this.history.push({
|
this.history.push({
|
||||||
type: 'retry',
|
type: 'retry',
|
||||||
message: `LLM retry attempt ${attempt} of ${maxAttempts}`,
|
message: `LLM retry attempt ${attempt} of ${maxAttempts}`,
|
||||||
@@ -116,19 +119,6 @@ export class PageAgentCore extends EventTarget {
|
|||||||
})
|
})
|
||||||
this.#emitHistoryChange()
|
this.#emitHistoryChange()
|
||||||
})
|
})
|
||||||
this.#llm.addEventListener('error', (e) => {
|
|
||||||
const error = (e as CustomEvent).detail.error as Error | InvokeError
|
|
||||||
if ((error as any)?.rawError?.name === 'AbortError') return
|
|
||||||
const message = String(error)
|
|
||||||
this.#emitActivity({ type: 'error', message })
|
|
||||||
// Also push to history for panel rendering
|
|
||||||
this.history.push({
|
|
||||||
type: 'error',
|
|
||||||
message,
|
|
||||||
rawResponse: (error as InvokeError).rawResponse,
|
|
||||||
})
|
|
||||||
this.#emitHistoryChange()
|
|
||||||
})
|
|
||||||
|
|
||||||
if (this.config.customTools) {
|
if (this.config.customTools) {
|
||||||
for (const [name, tool] of Object.entries(this.config.customTools)) {
|
for (const [name, tool] of Object.entries(this.config.customTools)) {
|
||||||
@@ -312,9 +302,9 @@ export class PageAgentCore extends EventTarget {
|
|||||||
}
|
}
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
console.groupEnd() // to prevent nested groups
|
console.groupEnd() // to prevent nested groups
|
||||||
const isAbortError = (error as any)?.rawError?.name === 'AbortError'
|
const isAbortError = (error as any)?.name === 'AbortError'
|
||||||
|
|
||||||
console.error('Task failed', error)
|
if (!isAbortError) console.error('Task failed', error)
|
||||||
const errorMessage = isAbortError ? 'Task stopped' : String(error)
|
const errorMessage = isAbortError ? 'Task stopped' : String(error)
|
||||||
this.#emitActivity({ type: 'error', message: errorMessage })
|
this.#emitActivity({ type: 'error', message: errorMessage })
|
||||||
this.history.push({ type: 'error', message: errorMessage, rawResponse: error })
|
this.history.push({ type: 'error', message: errorMessage, rawResponse: error })
|
||||||
@@ -378,8 +368,8 @@ export class PageAgentCore extends EventTarget {
|
|||||||
description: 'You MUST call this tool every step!',
|
description: 'You MUST call this tool every step!',
|
||||||
inputSchema: macroToolSchema as z.ZodType<MacroToolInput>,
|
inputSchema: macroToolSchema as z.ZodType<MacroToolInput>,
|
||||||
execute: async (input: MacroToolInput): Promise<MacroToolResult> => {
|
execute: async (input: MacroToolInput): Promise<MacroToolResult> => {
|
||||||
// abort
|
// abort — throws DOMException whose .name === 'AbortError'
|
||||||
if (this.#abortController.signal.aborted) throw new Error('AbortError')
|
this.#abortController.signal.throwIfAborted()
|
||||||
|
|
||||||
console.log(chalk.blue.bold('MacroTool input'), input)
|
console.log(chalk.blue.bold('MacroTool input'), input)
|
||||||
const action = input.action
|
const action = input.action
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ export class OpenAIClient implements LLMClient {
|
|||||||
abortSignal?: AbortSignal,
|
abortSignal?: AbortSignal,
|
||||||
options?: InvokeOptions
|
options?: InvokeOptions
|
||||||
): Promise<InvokeResult> {
|
): Promise<InvokeResult> {
|
||||||
|
// in case user aborted before invoking
|
||||||
|
if (abortSignal?.aborted) throw new InvokeError(InvokeErrorTypes.ABORTED, 'Aborted')
|
||||||
|
|
||||||
// 1. Convert tools to OpenAI format
|
// 1. Convert tools to OpenAI format
|
||||||
const openaiTools = Object.entries(tools).map(([name, t]) => zodToOpenAITool(name, t))
|
const openaiTools = Object.entries(tools).map(([name, t]) => zodToOpenAITool(name, t))
|
||||||
|
|
||||||
@@ -70,10 +73,11 @@ export class OpenAIClient implements LLMClient {
|
|||||||
signal: abortSignal,
|
signal: abortSignal,
|
||||||
})
|
})
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
const isAbortError = (error as any)?.name === 'AbortError'
|
if ((error as any)?.name === 'AbortError') {
|
||||||
const errorMessage = isAbortError ? 'Network request aborted' : 'Network request failed'
|
throw new InvokeError(InvokeErrorTypes.ABORTED, 'Aborted', error)
|
||||||
if (!isAbortError) console.error(error)
|
}
|
||||||
throw new InvokeError(InvokeErrorTypes.NETWORK_ERROR, errorMessage, error)
|
console.error(error)
|
||||||
|
throw new InvokeError(InvokeErrorTypes.NETWORK_ERROR, 'Network request failed', error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Handle HTTP errors
|
// 3. Handle HTTP errors
|
||||||
@@ -212,11 +216,14 @@ export class OpenAIClient implements LLMClient {
|
|||||||
let toolResult: unknown
|
let toolResult: unknown
|
||||||
try {
|
try {
|
||||||
toolResult = await tool.execute(toolInput)
|
toolResult = await tool.execute(toolInput)
|
||||||
} catch (e) {
|
} catch (error: unknown) {
|
||||||
|
if ((error as any)?.name === 'AbortError') {
|
||||||
|
throw new InvokeError(InvokeErrorTypes.ABORTED, 'Aborted', error)
|
||||||
|
}
|
||||||
throw new InvokeError(
|
throw new InvokeError(
|
||||||
InvokeErrorTypes.TOOL_EXECUTION_ERROR,
|
InvokeErrorTypes.TOOL_EXECUTION_ERROR,
|
||||||
`Tool execution failed: ${(e as Error).message}`,
|
`Tool execution failed: ${(error as Error)?.message}`,
|
||||||
e,
|
error,
|
||||||
data
|
data
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ export const InvokeErrorTypes = {
|
|||||||
UNKNOWN: 'unknown',
|
UNKNOWN: 'unknown',
|
||||||
|
|
||||||
// Non-retryable
|
// Non-retryable
|
||||||
|
ABORTED: 'aborted', // User aborted via AbortSignal — instance has name='AbortError'
|
||||||
CONFIG_ERROR: 'config_error', // Invalid local configuration or hook
|
CONFIG_ERROR: 'config_error', // Invalid local configuration or hook
|
||||||
AUTH_ERROR: 'auth_error', // Authentication failed
|
AUTH_ERROR: 'auth_error', // Authentication failed
|
||||||
CONTEXT_LENGTH: 'context_length', // Prompt too long
|
CONTEXT_LENGTH: 'context_length', // Prompt too long
|
||||||
@@ -22,6 +23,16 @@ export const InvokeErrorTypes = {
|
|||||||
|
|
||||||
type InvokeErrorType = (typeof InvokeErrorTypes)[keyof typeof InvokeErrorTypes]
|
type InvokeErrorType = (typeof InvokeErrorTypes)[keyof typeof InvokeErrorTypes]
|
||||||
|
|
||||||
|
const RETRYABLE_TYPES: readonly InvokeErrorType[] = [
|
||||||
|
InvokeErrorTypes.NETWORK_ERROR,
|
||||||
|
InvokeErrorTypes.RATE_LIMIT,
|
||||||
|
InvokeErrorTypes.SERVER_ERROR,
|
||||||
|
InvokeErrorTypes.NO_TOOL_CALL,
|
||||||
|
InvokeErrorTypes.INVALID_TOOL_ARGS,
|
||||||
|
InvokeErrorTypes.TOOL_EXECUTION_ERROR,
|
||||||
|
InvokeErrorTypes.UNKNOWN,
|
||||||
|
]
|
||||||
|
|
||||||
export class InvokeError extends Error {
|
export class InvokeError extends Error {
|
||||||
type: InvokeErrorType
|
type: InvokeErrorType
|
||||||
retryable: boolean
|
retryable: boolean
|
||||||
@@ -33,26 +44,12 @@ export class InvokeError extends Error {
|
|||||||
|
|
||||||
constructor(type: InvokeErrorType, message: string, rawError?: unknown, rawResponse?: unknown) {
|
constructor(type: InvokeErrorType, message: string, rawError?: unknown, rawResponse?: unknown) {
|
||||||
super(message)
|
super(message)
|
||||||
this.name = 'InvokeError'
|
// 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.type = type
|
this.type = type
|
||||||
this.retryable = this.isRetryable(type, rawError)
|
this.retryable = RETRYABLE_TYPES.includes(type)
|
||||||
this.rawError = rawError
|
this.rawError = rawError
|
||||||
this.rawResponse = rawResponse
|
this.rawResponse = rawResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
private isRetryable(type: InvokeErrorType, rawError?: unknown): boolean {
|
|
||||||
const isAbortError = (rawError as any)?.name === 'AbortError'
|
|
||||||
if (isAbortError) return false
|
|
||||||
|
|
||||||
const retryableTypes: InvokeErrorType[] = [
|
|
||||||
InvokeErrorTypes.NETWORK_ERROR,
|
|
||||||
InvokeErrorTypes.RATE_LIMIT,
|
|
||||||
InvokeErrorTypes.SERVER_ERROR,
|
|
||||||
InvokeErrorTypes.NO_TOOL_CALL,
|
|
||||||
InvokeErrorTypes.INVALID_TOOL_ARGS,
|
|
||||||
InvokeErrorTypes.TOOL_EXECUTION_ERROR,
|
|
||||||
InvokeErrorTypes.UNKNOWN,
|
|
||||||
]
|
|
||||||
return retryableTypes.includes(type)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,65 +50,42 @@ export class LLM extends EventTarget {
|
|||||||
abortSignal: AbortSignal,
|
abortSignal: AbortSignal,
|
||||||
options?: InvokeOptions
|
options?: InvokeOptions
|
||||||
): Promise<InvokeResult> {
|
): Promise<InvokeResult> {
|
||||||
return await withRetry(
|
return await withRetry(async () => this.client.invoke(messages, tools, abortSignal, options), {
|
||||||
async () => {
|
maxRetries: this.config.maxRetries,
|
||||||
// in case user aborted before invoking
|
onRetry: (attempt, lastError) => {
|
||||||
if (abortSignal.aborted) throw new Error('AbortError')
|
this.dispatchEvent(
|
||||||
|
new CustomEvent('retry', {
|
||||||
const result = await this.client.invoke(messages, tools, abortSignal, options)
|
detail: { attempt, maxAttempts: this.config.maxRetries, lastError },
|
||||||
|
})
|
||||||
return result
|
)
|
||||||
},
|
},
|
||||||
// retry settings
|
})
|
||||||
{
|
|
||||||
maxRetries: this.config.maxRetries,
|
|
||||||
onRetry: (attempt: number) => {
|
|
||||||
this.dispatchEvent(
|
|
||||||
new CustomEvent('retry', { detail: { attempt, maxAttempts: this.config.maxRetries } })
|
|
||||||
)
|
|
||||||
},
|
|
||||||
onError: (error: Error) => {
|
|
||||||
this.dispatchEvent(new CustomEvent('error', { detail: { error } }))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retry a function until it succeeds or reaches the maximum number of retries.
|
||||||
|
*/
|
||||||
async function withRetry<T>(
|
async function withRetry<T>(
|
||||||
fn: () => Promise<T>,
|
fn: () => Promise<T>,
|
||||||
settings: {
|
settings: {
|
||||||
maxRetries: number
|
maxRetries: number
|
||||||
onRetry: (attempt: number) => void
|
onRetry: (attempt: number, lastError: Error) => void
|
||||||
onError: (error: Error) => void
|
|
||||||
}
|
}
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
let attempt = 0
|
let attempt = 0
|
||||||
let lastError: Error | null = null
|
while (true) {
|
||||||
while (attempt <= settings.maxRetries) {
|
|
||||||
if (attempt > 0) {
|
|
||||||
settings.onRetry(attempt)
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return await fn()
|
return await fn()
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
// do not retry if aborted by user
|
|
||||||
if ((error as any)?.rawError?.name === 'AbortError') throw error
|
|
||||||
|
|
||||||
console.error(error)
|
|
||||||
settings.onError(error as Error)
|
|
||||||
|
|
||||||
// do not retry if error is not retryable (InvokeError)
|
|
||||||
if (error instanceof InvokeError && !error.retryable) throw error
|
if (error instanceof InvokeError && !error.retryable) throw error
|
||||||
|
|
||||||
lastError = error as Error
|
|
||||||
attempt++
|
attempt++
|
||||||
|
if (attempt > settings.maxRetries) throw error
|
||||||
|
|
||||||
|
console.debug('[LLM] retryable failure, will retry:', error)
|
||||||
|
settings.onRetry(attempt, error as Error)
|
||||||
|
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw lastError!
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user