Files
page-agent/packages/llms/src/errors.ts
T

59 lines
1.9 KiB
TypeScript
Raw Normal View History

2025-10-17 18:43:41 +08:00
/**
* Error types and error handling for LLM invocations
*/
export const InvokeErrorTypes = {
2025-10-17 18:43:41 +08:00
// Retryable
NETWORK_ERROR: 'network_error', // Network error, retry
RATE_LIMIT: 'rate_limit', // Rate limit, retry
SERVER_ERROR: 'server_error', // 5xx, retry
NO_TOOL_CALL: 'no_tool_call', // Model did not call tool
INVALID_TOOL_ARGS: 'invalid_tool_args', // Tool args don't match schema
TOOL_EXECUTION_ERROR: 'tool_execution_error', // Tool execution error
UNKNOWN: 'unknown',
// Non-retryable
CONFIG_ERROR: 'config_error', // Invalid local configuration or hook
2025-10-17 18:43:41 +08:00
AUTH_ERROR: 'auth_error', // Authentication failed
CONTEXT_LENGTH: 'context_length', // Prompt too long
CONTENT_FILTER: 'content_filter', // Content filtered
} as const
type InvokeErrorType = (typeof InvokeErrorTypes)[keyof typeof InvokeErrorTypes]
2025-10-17 18:43:41 +08:00
export class InvokeError extends Error {
type: InvokeErrorType
retryable: boolean
statusCode?: number
/* raw error (provided if this error is caused by another error) */
2025-10-17 18:43:41 +08:00
rawError?: unknown
/* raw response from the API (provided if this error is caused by an API calling) */
rawResponse?: unknown
2025-10-17 18:43:41 +08:00
constructor(type: InvokeErrorType, message: string, rawError?: unknown, rawResponse?: unknown) {
2025-10-17 18:43:41 +08:00
super(message)
this.name = 'InvokeError'
this.type = type
this.retryable = this.isRetryable(type, rawError)
2025-10-17 18:43:41 +08:00
this.rawError = rawError
this.rawResponse = rawResponse
2025-10-17 18:43:41 +08:00
}
private isRetryable(type: InvokeErrorType, rawError?: unknown): boolean {
const isAbortError = (rawError as any)?.name === 'AbortError'
if (isAbortError) return false
2025-10-17 18:43:41 +08:00
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,
2025-10-17 18:43:41 +08:00
]
return retryableTypes.includes(type)
}
}