Skip to main content

Module tooling

Module tooling 

Source
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:

  1. Front-of-tool: create_tool_call, tool_title, normalize_tool_kind.
  2. Result extraction: build_tool_result_fields per-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.
  3. 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Β§

UnwrappedToolResult
Mirrors unwrapToolUseResult(rawResult). Walks the tool_result envelope: if the value is an object, surfaces the inner content / result / text field and the is_error / error flag.

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_TYPES set 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 arbitrary tool_result block into the structured ToolCallUpdateFields upstream’s TUI consumes.
create_tool_call
Mirrors createToolCall(toolUseId, name, input, parentToolUseId). Builds the rich ToolCall envelope 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 a tool_result content payload into a single String. Accepts string, array of { type: "text", text } blocks, or a single object carrying prose under one of plan / 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 typed ToolKind consumed 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_result carries 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.content array looking for the CLIENT ToolSearch result 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 πŸ”’