From e9e3479400512943a2a302cbf772c5bf9e23cfcf Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:54:37 +0800 Subject: [PATCH] Import packages/plugin/src/codex/turn-end-reason.ts --- packages/plugin/src/codex/turn-end-reason.ts | 38 ++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 packages/plugin/src/codex/turn-end-reason.ts diff --git a/packages/plugin/src/codex/turn-end-reason.ts b/packages/plugin/src/codex/turn-end-reason.ts new file mode 100644 index 0000000..a394884 --- /dev/null +++ b/packages/plugin/src/codex/turn-end-reason.ts @@ -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 } : {}), + } +}