tools: static string definitions, no runtime JSON construction

Tool definitions are now &'static str (name, description,
parameters_json) instead of runtime-constructed serde_json::Value.
No more json!() macro, no more ToolDef::new() for tool definitions.

The JSON schema strings are written directly as string literals.
When sent to the API, they can be interpolated without
serialization/deserialization.

Multi-tool modules return fixed-size arrays instead of Vecs:
- memory: [Tool; 12], journal: [Tool; 3]
- channels: [Tool; 4]
- control: [Tool; 3]
- web: [Tool; 2]

ToolDef/FunctionDef remain for backward compat (API wire format,
summarize_args) but are no longer used in tool definitions.

Co-Authored-By: Proof of Concept <poc@bcachefs.org>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
ProofOfConcept 2026-04-04 15:50:14 -04:00 committed by Kent Overstreet
parent ed150df628
commit 53ad8cc9df
13 changed files with 205 additions and 561 deletions

View file

@ -2,41 +2,26 @@
use anyhow::{Context, Result};
use serde::Deserialize;
use serde_json::json;
use super::ToolDef;
pub fn tools() -> [super::Tool; 2] {
[
super::Tool { def: fetch_definition(), handler: |_a, v| Box::pin(async move { web_fetch(&v).await }) },
super::Tool { def: search_definition(), handler: |_a, v| Box::pin(async move { web_search(&v).await }) },
super::Tool {
name: "web_fetch",
description: "Fetch content from a URL and return it as text. Use for reading web pages, API responses, documentation.",
parameters_json: r#"{"type":"object","properties":{"url":{"type":"string","description":"The URL to fetch"}},"required":["url"]}"#,
handler: |_a, v| Box::pin(async move { web_fetch(&v).await }),
},
super::Tool {
name: "web_search",
description: "Search the web and return results. Use for finding documentation, looking up APIs, researching topics.",
parameters_json: r#"{"type":"object","properties":{"query":{"type":"string","description":"The search query"},"num_results":{"type":"integer","description":"Number of results to return (default 5)"}},"required":["query"]}"#,
handler: |_a, v| Box::pin(async move { web_search(&v).await }),
},
]
}
// ── Fetch ───────────────────────────────────────────────────────
#[derive(Deserialize)]
struct FetchArgs {
url: String,
}
fn fetch_definition() -> ToolDef {
ToolDef::new(
"web_fetch",
"Fetch content from a URL and return it as text. \
Use for reading web pages, API responses, documentation.",
json!({
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "The URL to fetch"
}
},
"required": ["url"]
}),
)
}
struct FetchArgs { url: String }
async fn web_fetch(args: &serde_json::Value) -> Result<String> {
let a: FetchArgs = serde_json::from_value(args.clone())
@ -71,28 +56,6 @@ struct SearchArgs {
fn default_num_results() -> usize { 5 }
fn search_definition() -> ToolDef {
ToolDef::new(
"web_search",
"Search the web and return results. Use for finding \
documentation, looking up APIs, researching topics.",
json!({
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The search query"
},
"num_results": {
"type": "integer",
"description": "Number of results to return (default 5)"
}
},
"required": ["query"]
}),
)
}
async fn web_search(args: &serde_json::Value) -> Result<String> {
let a: SearchArgs = serde_json::from_value(args.clone())
.context("invalid web_search arguments")?;