Expand description
Text-based tool simulation for CLI runners without native function calling
§Text-Based Tool Simulation
CLI LLM runners (Claude Code, Copilot, Cursor Agent, OpenCode) communicate via plain text and do not support native function calling. This module provides a text-based tool simulation layer that enables tool calling by:
- Generating a markdown tool catalog from function declarations and injecting it into the system prompt
- Parsing
<tool_call>XML blocks from LLM text output - Formatting tool results as
<tool_result>XML blocks for re-injection - Running a full multi-turn tool loop that iterates until the LLM produces a final text response
This is the CLI counterpart to the SDK-managed tool calling in
CopilotHeadlessRunner (requires copilot-headless feature).
§Quick Start
use embacle::tool_simulation::*;
use embacle::types::{ChatMessage, ChatRequest, LlmProvider};
use serde_json::json;
use std::sync::Arc;
let declarations = vec![
FunctionDeclaration {
name: "get_weather".into(),
description: "Get weather for a city".into(),
parameters: Some(json!({"type": "object", "properties": {"city": {"type": "string"}}})),
},
];
let handler: TextToolHandler = Arc::new(|name, args| {
FunctionResponse {
name: name.to_owned(),
response: json!({"temperature": 72}),
}
});
let mut messages = vec![
ChatMessage::system("You are a helpful assistant."),
ChatMessage::user("What's the weather in Paris?"),
];
let result = execute_with_text_tools(
provider, &mut messages, &declarations, handler, 5,
).await?;
println!("{}", result.content);Structs§
- Function
Call - A parsed tool call extracted from LLM text output.
- Function
Response - A tool execution result to feed back to the LLM.
- Text
Tool Response - Result of a text-based tool-calling conversation.
Functions§
- execute_
with_ text_ tools - Execute a full text-based tool-calling conversation with a CLI provider.
- format_
tool_ results_ as_ text - Format function responses as text for injection into follow-up messages.
- generate_
tool_ catalog - Generate a text-based tool catalog from function declarations.
- inject_
tool_ catalog - Inject a tool catalog into the system prompt of a message list.
- parse_
tool_ call_ blocks - Parse
<tool_call>blocks from LLM text output into structured function calls. - strip_
simulation_ artifacts - Strip all simulation scaffolding from a model’s user-facing reply: both
<tool_call>blocks (viastrip_tool_call_blocks) and echoed tool-result scaffolding (viastrip_tool_result_echo). - strip_
tool_ call_ blocks - Strip
<tool_call>...</tool_call>blocks from text, returning remaining content. - strip_
tool_ result_ echo - Strip echoed tool-result scaffolding from a model’s reply.
Type Aliases§
- Function
Declaration - A tool definition describing a callable function.
- Text
Tool Handler - Callback type for executing tool calls.