# Tool System Refactor - Centralizes tool definitions and execution in `core/src/tools/*`: specs (`spec.rs`), handlers (`handlers/*`), router (`router.rs`), registry/dispatch (`registry.rs`), and shared context (`context.rs`). One registry now builds the model-visible tool list and binds handlers. - Router converts model responses to tool calls; Registry dispatches with consistent telemetry via `codex-rs/otel` and unified error handling. Function, Local Shell, MCP, and experimental `unified_exec` all flow through this path; legacy shell aliases still work. - Rationale: reduce per‑tool boilerplate, keep spec/handler in sync, and make adding tools predictable and testable. Example: `read_file` - Spec: `core/src/tools/spec.rs` (see `create_read_file_tool`, registered by `build_specs`). - Handler: `core/src/tools/handlers/read_file.rs` (absolute `file_path`, 1‑indexed `offset`, `limit`, `L#: ` prefixes, safe truncation). - E2E test: `core/tests/suite/read_file.rs` validates the tool returns the requested lines. ## Next steps: - Decompose `handle_container_exec_with_params` - Add parallel tool calls
39 lines
895 B
Rust
39 lines
895 B
Rust
// Truncate a &str to a byte budget at a char boundary (prefix)
|
|
#[inline]
|
|
pub fn take_bytes_at_char_boundary(s: &str, maxb: usize) -> &str {
|
|
if s.len() <= maxb {
|
|
return s;
|
|
}
|
|
let mut last_ok = 0;
|
|
for (i, ch) in s.char_indices() {
|
|
let nb = i + ch.len_utf8();
|
|
if nb > maxb {
|
|
break;
|
|
}
|
|
last_ok = nb;
|
|
}
|
|
&s[..last_ok]
|
|
}
|
|
|
|
// Take a suffix of a &str within a byte budget at a char boundary
|
|
#[inline]
|
|
pub fn take_last_bytes_at_char_boundary(s: &str, maxb: usize) -> &str {
|
|
if s.len() <= maxb {
|
|
return s;
|
|
}
|
|
let mut start = s.len();
|
|
let mut used = 0usize;
|
|
for (i, ch) in s.char_indices().rev() {
|
|
let nb = ch.len_utf8();
|
|
if used + nb > maxb {
|
|
break;
|
|
}
|
|
start = i;
|
|
used += nb;
|
|
if start == 0 {
|
|
break;
|
|
}
|
|
}
|
|
&s[start..]
|
|
}
|