Skip to main content

Module tool_simulation

Module tool_simulation 

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

  1. Generating a markdown tool catalog from function declarations and injecting it into the system prompt
  2. Parsing <tool_call> XML blocks from LLM text output
  3. Formatting tool results as <tool_result> XML blocks for re-injection
  4. 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§

FunctionCall
A parsed tool call extracted from LLM text output.
FunctionResponse
A tool execution result to feed back to the LLM.
TextToolResponse
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 (via strip_tool_call_blocks) and echoed tool-result scaffolding (via strip_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§

FunctionDeclaration
A tool definition describing a callable function.
TextToolHandler
Callback type for executing tool calls.