Skip to main content

tycode_core/tools/
trait.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3
4use anyhow::Result;
5use serde_json::Value;
6
7use crate::agents::agent::Agent;
8use crate::chat::events::{ToolExecutionResult, ToolRequest as ToolRequestEvent};
9
10/// Tool category that determines the type of operation
11#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
12pub enum ToolCategory {
13    TaskList,
14    Execution,
15    Meta,
16}
17
18/// Request passed to tool execution
19#[derive(Debug, Clone)]
20pub struct ToolRequest {
21    /// The arguments for the tool
22    pub arguments: Value,
23    /// The unique ID for this tool use
24    pub tool_use_id: String,
25}
26
27impl ToolRequest {
28    /// Create a new tool request
29    pub fn new(arguments: Value, tool_use_id: String) -> Self {
30        Self {
31            arguments,
32            tool_use_id,
33        }
34    }
35}
36
37/// Preference for whether the conversation should continue after tool execution
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub enum ContinuationPreference {
40    Continue,
41    Stop,
42}
43
44/// Output from tool execution - either a direct result or an action for the orchestrator
45pub enum ToolOutput {
46    /// Standard result - add to conversation
47    Result {
48        content: String,
49        is_error: bool,
50        continuation: ContinuationPreference,
51        ui_result: ToolExecutionResult,
52    },
53    /// Push agent onto stack (spawn_coder, spawn_agent, spawn_recon)
54    PushAgent { agent: Arc<dyn Agent>, task: String },
55    /// Pop agent from stack (complete_task)
56    PopAgent { success: bool, result: String },
57    /// Stop and prompt user (ask_user_question)
58    PromptUser { question: String },
59}
60
61/// Handle for a validated tool call, encapsulating request generation and execution
62#[async_trait::async_trait(?Send)]
63pub trait ToolCallHandle: Send {
64    fn tool_request(&self) -> ToolRequestEvent;
65    async fn execute(self: Box<Self>) -> ToolOutput;
66}
67
68/// File modification operation type
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub enum FileOperation {
71    Create,
72    Update,
73    Delete,
74}
75
76/// File modification details
77#[derive(Debug, Clone)]
78pub struct FileModification {
79    pub path: PathBuf,
80    pub operation: FileOperation,
81    pub original_content: Option<String>,
82    pub new_content: Option<String>,
83    pub warning: Option<String>,
84}
85
86#[async_trait::async_trait(?Send)]
87pub trait ToolExecutor {
88    fn name(&self) -> String;
89    fn description(&self) -> String;
90    fn input_schema(&self) -> Value;
91    fn category(&self) -> ToolCategory;
92    async fn process(&self, request: &ToolRequest) -> Result<Box<dyn ToolCallHandle>>;
93}