Import packages/plugin/src/codex/turn-end-reason.ts

This commit is contained in:
QQ 2026-11-14 14:54:37 +08:00
parent ce31dacf72
commit e9e3479400
1 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,38 @@
import type { JsonRecord } from './virtual-values.js'
import { record, string } from './virtual-values.js'
/**
* Map an upstream CodeX turn into the virtual Harness `turn/end` reason.
* Preserve provider failure text and `codex_error_info` so Remote clients can
* show usage limits / rate limits instead of a generic "CodeX turn failed."
*/
export function turnEndReason(turn: JsonRecord): JsonRecord {
if (turn.status === 'failed' || turn.error !== undefined && turn.error !== null) {
return { kind: 'error', error: projectTurnError(turn.error) }
}
if (turn.status === 'interrupted' || turn.status === 'cancelled' || turn.status === 'canceled' || turn.status === 'aborted') {
return { kind: 'interrupted' }
}
return { kind: 'completed' }
}
export function projectTurnError(error: unknown): JsonRecord {
if (typeof error === 'string') {
const message = error.trim()
return {
message: message === '' ? 'CodeX turn failed.' : message,
code: 'codex-turn-failed',
}
}
const value = record(error)
const message = string(value.message)?.trim()
const codexInfo = string(value.codex_error_info)?.trim()
const code = (codexInfo && codexInfo !== '' ? codexInfo : undefined)
?? string(value.code)?.trim()
?? 'codex-turn-failed'
return {
message: message && message !== '' ? message : 'CodeX turn failed.',
code: code === '' ? 'codex-turn-failed' : code,
...(codexInfo && codexInfo !== '' ? { codex_error_info: codexInfo } : {}),
}
}