Files
g3/crates/g3-core/src/tool_dispatch.rs
Dhanji R. Prasanna f415dbb84b Fix ACD turn summary loss and add /dump command
ACD (Aggressive Context Dehydration) fixes:
- Fixed dehydrate_context() to extract turn summary from context window
  instead of using the passed-in final_response (which contained only
  the timing footer, not the actual LLM response)
- Removed final_response parameter from dehydrate_context() since it
  now self-extracts the last assistant message as the summary
- This ensures the actual turn summary is preserved after dehydration,
  not just the timing footer

New /dump command:
- Added /dump command to dump entire context window to tmp/ for debugging
- Shows message index, role, kind, content length, and full content
- Available in both console and machine modes

UTF-8 safety:
- Fixed truncate_to_word_boundary() to use character indices instead of
  byte indices, preventing panics on multi-byte UTF-8 characters
- Added UTF-8 string slicing guidance to AGENTS.md

Agent: g3
2026-01-12 05:13:02 +05:30

77 lines
3.7 KiB
Rust

//! Tool dispatch module - routes tool calls to their implementations.
//!
//! This module provides a clean dispatch mechanism that routes tool calls
//! to the appropriate handler in the `tools/` module.
use anyhow::Result;
use tracing::{debug, warn};
use crate::tools::executor::ToolContext;
use crate::tools::{acd, file_ops, memory, misc, research, shell, todo, webdriver};
use crate::ui_writer::UiWriter;
use crate::ToolCall;
/// Dispatch a tool call to the appropriate handler.
///
/// This function routes tool calls to their implementations in the `tools/` module,
/// providing a single point of dispatch for all tool execution.
pub async fn dispatch_tool<W: UiWriter>(
tool_call: &ToolCall,
ctx: &mut ToolContext<'_, W>,
) -> Result<String> {
debug!("Dispatching tool: {}", tool_call.tool);
match tool_call.tool.as_str() {
// Shell tools
"shell" => shell::execute_shell(tool_call, ctx).await,
"background_process" => shell::execute_background_process(tool_call, ctx).await,
// File operations
"read_file" => file_ops::execute_read_file(tool_call, ctx).await,
"read_image" => file_ops::execute_read_image(tool_call, ctx).await,
"write_file" => file_ops::execute_write_file(tool_call, ctx).await,
"str_replace" => file_ops::execute_str_replace(tool_call, ctx).await,
// TODO management
"todo_read" => todo::execute_todo_read(tool_call, ctx).await,
"todo_write" => todo::execute_todo_write(tool_call, ctx).await,
// Miscellaneous tools
"take_screenshot" => misc::execute_take_screenshot(tool_call, ctx).await,
"code_coverage" => misc::execute_code_coverage(tool_call, ctx).await,
"code_search" => misc::execute_code_search(tool_call, ctx).await,
// Research tool
"research" => research::execute_research(tool_call, ctx).await,
// Project memory tools
"remember" => memory::execute_remember(tool_call, ctx).await,
// ACD (Aggressive Context Dehydration) tools
"rehydrate" => acd::execute_rehydrate(tool_call, ctx).await,
// WebDriver tools
"webdriver_start" => webdriver::execute_webdriver_start(tool_call, ctx).await,
"webdriver_navigate" => webdriver::execute_webdriver_navigate(tool_call, ctx).await,
"webdriver_get_url" => webdriver::execute_webdriver_get_url(tool_call, ctx).await,
"webdriver_get_title" => webdriver::execute_webdriver_get_title(tool_call, ctx).await,
"webdriver_find_element" => webdriver::execute_webdriver_find_element(tool_call, ctx).await,
"webdriver_find_elements" => webdriver::execute_webdriver_find_elements(tool_call, ctx).await,
"webdriver_click" => webdriver::execute_webdriver_click(tool_call, ctx).await,
"webdriver_send_keys" => webdriver::execute_webdriver_send_keys(tool_call, ctx).await,
"webdriver_execute_script" => webdriver::execute_webdriver_execute_script(tool_call, ctx).await,
"webdriver_get_page_source" => webdriver::execute_webdriver_get_page_source(tool_call, ctx).await,
"webdriver_screenshot" => webdriver::execute_webdriver_screenshot(tool_call, ctx).await,
"webdriver_back" => webdriver::execute_webdriver_back(tool_call, ctx).await,
"webdriver_forward" => webdriver::execute_webdriver_forward(tool_call, ctx).await,
"webdriver_refresh" => webdriver::execute_webdriver_refresh(tool_call, ctx).await,
"webdriver_quit" => webdriver::execute_webdriver_quit(tool_call, ctx).await,
// Unknown tool
_ => {
warn!("Unknown tool: {}", tool_call.tool);
Ok(format!("❓ Unknown tool: {}", tool_call.tool))
}
}
}