MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / coerceToolResult

Function coerceToolResult

packages/agent-core/src/loop/tool-call.ts:619–637  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

617 * arbitrary tool implementations and the rest of the loop.
618 */
619function 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
639function normalizeToolResult(r: ExecutableToolResult): ExecutableToolResult {
640 let output: ExecutableToolResult['output'];

Callers 3

settleSyntheticFunction · 0.85
runRunnableToolCallFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected