Expand description
Tool-call construction + tool-result extraction. 1:1 port of
upstreamβs agent-sdk/src/bridge/tooling.ts (714 LoC).
Three groups of helpers:
- Front-of-tool:
create_tool_call,tool_title,normalize_tool_kind. - Result extraction:
build_tool_result_fieldsper-tool branches (Bash / Read / Write / Edit / Agent /ReadMcpResource),normalize_tool_result_text,extract_text,persisted_output_first_line,sanitize_sdk_rejection_text,extract_tool_output_metadata. - Helpers:
unwrap_tool_use_result.
Re-exportsΒ§
use serde_json::Map;πuse serde_json::Value;πuse serde_json::json;πuse forge_primitives::BashOutputMetadata;πuse forge_primitives::ChunkContent;πuse forge_primitives::ToolCall;πuse forge_primitives::ToolCallContent;πuse forge_primitives::ToolCallLocation;πuse forge_primitives::ToolCallUpdateFields;πuse forge_primitives::ToolOutputMetadata;π
StructsΒ§
- Unwrapped
Tool Result - Mirrors
unwrapToolUseResult(rawResult). Walks thetool_resultenvelope: if the value is an object, surfaces the innercontent/result/textfield and theis_error/errorflag.
ConstantsΒ§
- CACHE_
PREVIEW_ πLIMIT_ BYTES - PERMISSION_
DENIED_ πTOOL_ USE_ EXACT - PERMISSION_
DENIED_ πTOOL_ USE_ PREFIX - PERSISTED_
OUTPUT_ πCLOSE_ TAG - PERSISTED_
OUTPUT_ πOPEN_ TAG - TOOL_
RESULT_ πTYPES - Block types the CLI uses for tool results. Mirrors upstreamβs
TOOL_RESULT_TYPESset in tooling.ts:5. - USER_
REJECTED_ πTOOL_ USE_ EXACT - USER_
REJECTED_ πTOOL_ USE_ PREFIX
FunctionsΒ§
- agent_
title_ πfrom_ agent_ output - bash_
background_ πmessage - build_
bash_ πdisplay_ output - build_
tool_ result_ fields - Mirrors
buildToolResultFields(isError, rawContent, base?, rawResult?). The high-level entry that turns an arbitrarytool_resultblock into the structuredToolCallUpdateFieldsupstreamβs TUI consumes. - create_
tool_ call - Mirrors
createToolCall(toolUseId, name, input, parentToolUseId). Builds the richToolCallenvelope upstream emits when the assistant first invokes a tool. - edit_
diff_ πcontent - Mirrors
editDiffContent(name, input)- initial diff content for Edit / Write tool cards (before the result lands). Empty Vec for other tools. - edit_
diff_ πfrom_ input - edit_
diff_ πfrom_ result - extract_
text π - Mirrors
extractText(value)- flattens atool_resultcontent payload into a singleString. Accepts string, array of{ type: "text", text }blocks, or a single object carrying prose under one ofplan/description/summary/message/text(priority order: plan-mode-specific first, generic last). - extract_
tool_ πoutput_ metadata - file_
unchanged_ πresult_ text - find_
bash_ πresult_ record - is_
tool_ result_ block_ type - mcp_
resource_ πcontent_ from_ result - normalize_
tool_ πkind - Mirrors
normalizeToolKind. Maps tool name to the typedToolKindconsumed by a viewβs tool-card renderer. - normalize_
tool_ πresult_ text - parse_
json_ πcandidate - persisted_
output_ πfirst_ line - persisted_
output_ πinner_ text - preview_
kilobyte_ πlabel - push_
nested_ πrecords - push_
record π - push_
records π - push_
structured_ πrecord_ candidates - resolve_
tool_ πname - result_
indicates_ πbackground - True when a
tool_resultcarries a background sentinel - the same fields Bash keys on (backgroundTaskId/assistantAutoBackgrounded/backgroundedByUser). - result_
record_ πcandidates - sanitize_
sdk_ πrejection_ text - tool_
search_ πmatches_ from_ content - Walk a
tool_result.contentarray looking for the CLIENTToolSearchresult wire shape -[{type: "tool_reference", tool_name: "..."}, ...]- and return the matched tool names in source order. Anything not a tool_reference entry is skipped, so a mixed-content result still surfaces the references it carried. - tool_
title π - Mirrors
toolTitle(name, input). Produces the human-friendly title shown on the tool card header (e.g. Bash β command, Glob β pattern + path, etc.). - unwrap_
tool_ use_ result - write_
diff_ πfrom_ input - write_
diff_ πfrom_ result