Unified · Split
| @@ -1,3 +1,4 @@ | ||
| 1 | +import { z } from "zod"; | |
| 1 | 2 | import type { ToolCallResult } from "./types"; |
| 2 | 3 | |
| 3 | 4 | const UNDEFINED_SENTINEL = "__executor_tool_result_undefined__"; |
| @@ -14,6 +15,30 @@ type ToolCallTransportResult = · 10 unmodified lines | ||
| 14 | 15 | | { ok: false; kind: "denied"; error: string } |
| 15 | 16 | | { ok: false; kind: "failed"; error: string }; |
| 16 | 17 | |
| 18 | +const toolCallTransportResultSchema = z.union([ | |
| 19 | + z.object({ | |
| 20 | + ok: z.literal(true), | |
| 21 | + valueJson: z.string(), | |
| 22 | + }), | |
| 23 | + z.object({ | |
| 24 | + ok: z.literal(false), | |
| 25 | + kind: z.literal("pending"), | |
| 26 | + approvalId: z.string(), | |
| 27 | + retryAfterMs: z.number().optional(), | |
| 28 | + error: z.string().optional(), | |
| 29 | + }), | |
| 30 | + z.object({ | |
| 31 | + ok: z.literal(false), | |
| 32 | + kind: z.literal("denied"), | |
| 33 | + error: z.string(), | |
| 34 | + }), | |
| 35 | + z.object({ | |
| 36 | + ok: z.literal(false), | |
| 37 | + kind: z.literal("failed"), | |
| 38 | + error: z.string(), | |
| 39 | + }), | |
| 40 | +]); | |
| 41 | + | |
| 17 | 42 | function encodeValue(value: unknown): string { |
| 18 | 43 | if (value === undefined) { |
| 19 | 44 | return UNDEFINED_SENTINEL; |
| @@ -51,7 +76,7 @@ export function encodeToolCallResultForTransport(result: ToolCallResult): string · 31 unmodified lines | ||
| 51 | 76 | } |
| 52 | 77 | |
| 53 | 78 | export function decodeToolCallResultFromTransport(value: unknown): ToolCallResult | null { |
| 54 | − const parsed: unknown = typeof value === "string" | |
| 79 | + const parsedJson: unknown = typeof value === "string" | |
| 55 | 80 | ? (() => { |
| 56 | 81 | try { |
| 57 | 82 | return JSON.parse(value) as unknown; |
| @@ -61,32 +86,30 @@ export function decodeToolCallResultFromTransport(value: unknown): ToolCallResul · 3 unmodified lines | ||
| 61 | 86 | })() |
| 62 | 87 | : value; |
| 63 | 88 | |
| 64 | − if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) { | |
| 89 | + const parsed = toolCallTransportResultSchema.safeParse(parsedJson); | |
| 90 | + if (!parsed.success) { | |
| 65 | 91 | return null; |
| 66 | 92 | } |
| 67 | 93 | |
| 68 | − const record = parsed as Record<string, unknown>; | |
| 69 | − if (record.ok === true) { | |
| 70 | − if (typeof record.valueJson !== "string") { | |
| 71 | − return null; | |
| 72 | − } | |
| 94 | + const record = parsed.data; | |
| 95 | + if (record.ok) { | |
| 73 | 96 | return { |
| 74 | 97 | ok: true, |
| 75 | 98 | value: decodeValue(record.valueJson), |
| 76 | 99 | }; |
| 77 | 100 | } |
| 78 | 101 | |
| 79 | − if (record.ok === false && record.kind === "pending" && typeof record.approvalId === "string") { | |
| 102 | + if (record.kind === "pending") { | |
| 80 | 103 | return { |
| 81 | 104 | ok: false, |
| 82 | 105 | kind: "pending", |
| 83 | 106 | approvalId: record.approvalId, |
| 84 | − ...(typeof record.retryAfterMs === "number" ? { retryAfterMs: record.retryAfterMs } : {}), | |
| 85 | − ...(typeof record.error === "string" ? { error: record.error } : {}), | |
| 107 | + ...(record.retryAfterMs !== undefined ? { retryAfterMs: record.retryAfterMs } : {}), | |
| 108 | + ...(record.error !== undefined ? { error: record.error } : {}), | |
| 86 | 109 | }; |
| 87 | 110 | } |
| 88 | 111 | |
| 89 | − if (record.ok === false && record.kind === "denied" && typeof record.error === "string") { | |
| 112 | + if (record.kind === "denied") { | |
| 90 | 113 | return { |
| 91 | 114 | ok: false, |
| 92 | 115 | kind: "denied", |
| @@ -94,7 +117,7 @@ export function decodeToolCallResultFromTransport(value: unknown): ToolCallResul · 1 unmodified line | ||
| 94 | 117 | }; |
| 95 | 118 | } |
| 96 | 119 | |
| 97 | − if (record.ok === false && record.kind === "failed" && typeof record.error === "string") { | |
| 120 | + if (record.kind === "failed") { | |
| 98 | 121 | return { |
| 99 | 122 | ok: false, |
| 100 | 123 | kind: "failed", |