refactor(llms): split AbortError out of InvokeError
This commit is contained in:
@@ -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}`,
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user