Files
oh-my-opencode/src/features/background-agent/error-classifier.ts
T
Choi Kijin / 최 기진 / チョイ キジン 25548f2561 fix(model-fallback): retry forbidden provider errors
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-04-28 15:29:33 +09:00

85 lines
2.3 KiB
TypeScript

export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null
}
export function isAbortedSessionError(error: unknown): boolean {
const message = getErrorText(error)
return message.toLowerCase().includes("aborted")
}
export function getErrorText(error: unknown): string {
if (!error) return ""
if (typeof error === "string") return error
if (error instanceof Error) {
return `${error.name}: ${error.message}`
}
if (typeof error === "object" && error !== null) {
if ("message" in error && typeof error.message === "string") {
return error.message
}
if ("name" in error && typeof error.name === "string") {
return error.name
}
}
return ""
}
export function extractErrorName(error: unknown): string | undefined {
if (isRecord(error) && typeof error["name"] === "string") return error["name"]
if (error instanceof Error) return error.name
return undefined
}
export function extractErrorMessage(error: unknown): string | undefined {
if (!error) return undefined
if (typeof error === "string") return error
if (isRecord(error)) {
const dataRaw = error["data"]
const candidates: unknown[] = [
dataRaw,
isRecord(dataRaw) ? (dataRaw as Record<string, unknown>)["error"] : undefined,
error["error"],
error["cause"],
error,
]
for (const candidate of candidates) {
if (typeof candidate === "string" && candidate.length > 0) return candidate
if (
isRecord(candidate) &&
typeof candidate["message"] === "string" &&
candidate["message"].length > 0
) {
return candidate["message"]
}
}
}
if (error instanceof Error) return error.message
try {
return JSON.stringify(error)
} catch {
return String(error)
}
}
interface EventPropertiesLike {
[key: string]: unknown
}
export function getSessionErrorMessage(properties: EventPropertiesLike): string | undefined {
const errorRaw = properties["error"]
if (!isRecord(errorRaw)) return undefined
const dataRaw = errorRaw["data"]
if (isRecord(dataRaw)) {
const message = dataRaw["message"]
if (typeof message === "string") return message
}
const message = errorRaw["message"]
return typeof message === "string" ? message : undefined
}