* Validate a tool's raw return against the ExecutableToolResult contract. * A tool that returns `undefined`, a primitive, or an object without a valid * `output` field is coerced into an `isError: true` result so the loop can still * emit a paired `tool.result` event. This is the trust bo
(value: unknown, toolName: string)
| 617 | * arbitrary tool implementations and the rest of the loop. |
| 618 | */ |
| 619 | function coerceToolResult(value: unknown, toolName: string): ExecutableToolResult { |
| 620 | if (value === null || value === undefined) { |
| 621 | return { output: `Tool "${toolName}" returned no result.`, isError: true }; |
| 622 | } |
| 623 | if (typeof value !== 'object') { |
| 624 | return { |
| 625 | output: `Tool "${toolName}" returned a ${typeof value} instead of a tool result.`, |
| 626 | isError: true, |
| 627 | }; |
| 628 | } |
| 629 | const candidate = value as { output?: unknown }; |
| 630 | if (typeof candidate.output !== 'string' && !Array.isArray(candidate.output)) { |
| 631 | return { |
| 632 | output: `Tool "${toolName}" returned a result with a missing or malformed "output" field.`, |
| 633 | isError: true, |
| 634 | }; |
| 635 | } |
| 636 | return value as ExecutableToolResult; |
| 637 | } |
| 638 | |
| 639 | function normalizeToolResult(r: ExecutableToolResult): ExecutableToolResult { |
| 640 | let output: ExecutableToolResult['output']; |
no outgoing calls
no test coverage detected