| 1 | import type { HistoryMessage, HistoryToolCall } from "./types"; |
| 2 | import type { ToolStatus } from "./useController"; |
| 3 | import { isShellToolName } from "./shellToolIdentity"; |
| 4 | |
| 5 | /** Missing resident payload is not evidence that execution stopped. */ |
| 6 | export function historyToolStatus(result: HistoryMessage | undefined, call?: HistoryToolCall, error?: string): ToolStatus { |
| 7 | const name = call?.name || result?.toolName; |
| 8 | const shell = !name || isShellToolName(name) || (call?.id || result?.toolCallId || "").startsWith("shell-"); |
| 9 | // A job-output read can carry a snapshot of a still-running (or failed) |
| 10 | // process. That process is not the invocation whose history we are restoring. |
| 11 | const evidence = (shell ? result?.execution?.state : undefined) ?? call?.resultObservation?.state; |
| 12 | if (evidence === "cancelled" || evidence === "not_started") return "stopped"; |
| 13 | if (evidence === "failed" || evidence === "error") return "error"; |
| 14 | if (error) return "error"; |
| 15 | if (evidence === "completed" || evidence === "user_confirmed") return "done"; |
| 16 | if (evidence === "running" || evidence === "started" || evidence === "pending" || call?.pending) return "running"; |
| 17 | return result ? "done" : "unknown"; |
| 18 | } |
| 19 |