|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +export const AgentStartupPhase = z.enum(['initial_prompt']); |
| 4 | +export type AgentStartupPhase = z.infer<typeof AgentStartupPhase>; |
| 5 | + |
| 6 | +export type AgentStartupErrorPayload = { |
| 7 | + error: string; |
| 8 | + phase?: AgentStartupPhase; |
| 9 | + status?: number; |
| 10 | + error_type?: string; |
| 11 | + action?: string; |
| 12 | +}; |
| 13 | + |
| 14 | +const StringRecord = z.record(z.string(), z.unknown()); |
| 15 | +const GatewayErrorBody = z |
| 16 | + .object({ |
| 17 | + error: z.unknown().optional(), |
| 18 | + error_type: z.string().optional(), |
| 19 | + message: z.string().optional(), |
| 20 | + }) |
| 21 | + .passthrough(); |
| 22 | +const ErrorObject = z |
| 23 | + .object({ |
| 24 | + message: z.string().optional(), |
| 25 | + status: z.number().optional(), |
| 26 | + statusCode: z.number().optional(), |
| 27 | + code: z.union([z.string(), z.number()]).optional(), |
| 28 | + body: z.unknown().optional(), |
| 29 | + data: z.unknown().optional(), |
| 30 | + response: z |
| 31 | + .object({ |
| 32 | + status: z.number().optional(), |
| 33 | + statusCode: z.number().optional(), |
| 34 | + body: z.unknown().optional(), |
| 35 | + data: z.unknown().optional(), |
| 36 | + }) |
| 37 | + .passthrough() |
| 38 | + .optional(), |
| 39 | + }) |
| 40 | + .passthrough(); |
| 41 | + |
| 42 | +export class AgentStartupError extends Error { |
| 43 | + readonly payload: AgentStartupErrorPayload; |
| 44 | + |
| 45 | + constructor(payload: AgentStartupErrorPayload) { |
| 46 | + super(payload.error); |
| 47 | + this.name = 'AgentStartupError'; |
| 48 | + this.payload = payload; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +type JsonParseResult = { success: true; data: unknown } | { success: false }; |
| 53 | + |
| 54 | +function parseJsonFromString(value: string): JsonParseResult { |
| 55 | + const trimmed = value.trim(); |
| 56 | + if (!trimmed) return { success: false }; |
| 57 | + |
| 58 | + try { |
| 59 | + return { success: true, data: JSON.parse(trimmed) }; |
| 60 | + } catch { |
| 61 | + const start = trimmed.indexOf('{'); |
| 62 | + const end = trimmed.lastIndexOf('}'); |
| 63 | + if (start === -1 || end <= start) return { success: false }; |
| 64 | + try { |
| 65 | + return { success: true, data: JSON.parse(trimmed.slice(start, end + 1)) }; |
| 66 | + } catch { |
| 67 | + return { success: false }; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function readGatewayBody(value: unknown): z.infer<typeof GatewayErrorBody> | null { |
| 73 | + if (typeof value === 'string') { |
| 74 | + const parsed = parseJsonFromString(value); |
| 75 | + if (!parsed.success) return null; |
| 76 | + return readGatewayBody(parsed.data); |
| 77 | + } |
| 78 | + |
| 79 | + const parsed = GatewayErrorBody.safeParse(value); |
| 80 | + if (!parsed.success) return null; |
| 81 | + if ( |
| 82 | + parsed.data.error === undefined && |
| 83 | + parsed.data.error_type === undefined && |
| 84 | + parsed.data.message === undefined |
| 85 | + ) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + return parsed.data; |
| 89 | +} |
| 90 | + |
| 91 | +function readBodyMessage(errorValue: unknown): string | undefined { |
| 92 | + if (typeof errorValue === 'string') return errorValue; |
| 93 | + |
| 94 | + const record = StringRecord.safeParse(errorValue); |
| 95 | + if (!record.success) return undefined; |
| 96 | + |
| 97 | + const message = record.data.message; |
| 98 | + if (typeof message === 'string') return message; |
| 99 | + |
| 100 | + const code = record.data.code; |
| 101 | + if (typeof code === 'string') return code; |
| 102 | + |
| 103 | + return undefined; |
| 104 | +} |
| 105 | + |
| 106 | +function gatewayBodyFromError(err: unknown): z.infer<typeof GatewayErrorBody> | null { |
| 107 | + if (err instanceof Error) { |
| 108 | + const fromMessage = readGatewayBody(err.message); |
| 109 | + if (fromMessage) return fromMessage; |
| 110 | + } |
| 111 | + |
| 112 | + const direct = readGatewayBody(err); |
| 113 | + if (direct) return direct; |
| 114 | + |
| 115 | + const parsed = ErrorObject.safeParse(err); |
| 116 | + if (!parsed.success) return null; |
| 117 | + |
| 118 | + return ( |
| 119 | + readGatewayBody(parsed.data.body) ?? |
| 120 | + readGatewayBody(parsed.data.data) ?? |
| 121 | + readGatewayBody(parsed.data.response?.body) ?? |
| 122 | + readGatewayBody(parsed.data.response?.data) ?? |
| 123 | + (parsed.data.message ? readGatewayBody(parsed.data.message) : null) |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +function statusFromError(err: unknown): number | undefined { |
| 128 | + const parsed = ErrorObject.safeParse(err); |
| 129 | + if (!parsed.success) return undefined; |
| 130 | + |
| 131 | + const status = |
| 132 | + parsed.data.status ?? |
| 133 | + parsed.data.statusCode ?? |
| 134 | + parsed.data.response?.status ?? |
| 135 | + parsed.data.response?.statusCode; |
| 136 | + if (status) return status; |
| 137 | + |
| 138 | + const code = parsed.data.code; |
| 139 | + if (typeof code === 'number') return code; |
| 140 | + if (typeof code === 'string' && /^\d{3}$/.test(code)) return Number(code); |
| 141 | + return undefined; |
| 142 | +} |
| 143 | + |
| 144 | +function messageFromError(err: unknown): string { |
| 145 | + if (err instanceof Error) return err.message; |
| 146 | + if (typeof err === 'string') return err; |
| 147 | + return 'Agent startup failed'; |
| 148 | +} |
| 149 | + |
| 150 | +export function classifyStartupError( |
| 151 | + err: unknown, |
| 152 | + phase?: AgentStartupPhase |
| 153 | +): AgentStartupErrorPayload { |
| 154 | + if (err instanceof AgentStartupError) return err.payload; |
| 155 | + |
| 156 | + const body = gatewayBodyFromError(err); |
| 157 | + const status = statusFromError(err); |
| 158 | + const errorType = body?.error_type; |
| 159 | + const gatewayMessage = readBodyMessage(body?.error) ?? body?.message; |
| 160 | + |
| 161 | + if (phase === 'initial_prompt' && status === 429 && errorType === 'rate_limit_exceeded') { |
| 162 | + return { |
| 163 | + error: |
| 164 | + gatewayMessage ?? |
| 165 | + 'Kilo gateway rejected the initial prompt because the selected model is rate limited.', |
| 166 | + phase, |
| 167 | + status, |
| 168 | + error_type: errorType, |
| 169 | + action: 'Wait and retry, or switch the town/rig to a model with available quota.', |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + return { |
| 174 | + error: gatewayMessage ?? messageFromError(err), |
| 175 | + ...(phase ? { phase } : {}), |
| 176 | + ...(status ? { status } : {}), |
| 177 | + ...(errorType ? { error_type: errorType } : {}), |
| 178 | + }; |
| 179 | +} |
0 commit comments