matrixcode_core/tools/
toolproxy.rs1use anyhow::Result;
14use async_trait::async_trait;
15use serde::{Deserialize, Serialize};
16use serde_json::Value;
17
18use super::ToolDefinition;
19
20#[derive(Debug, Clone, Serialize, Deserialize)]
22pub struct ProxyToolDef {
23 pub definition: ToolDefinition,
24 pub timeout_ms: u64,
25}
26
27impl ProxyToolDef {
28 pub fn new(name: impl Into<String>, description: impl Into<String>, parameters: Value) -> Self {
29 Self {
30 definition: ToolDefinition {
31 name: name.into(),
32 description: description.into(),
33 parameters,
34 is_priority: false,
35 },
36 timeout_ms: 30000,
37 }
38 }
39
40 pub fn with_priority(mut self, is_priority: bool) -> Self {
41 self.definition.is_priority = is_priority;
42 self
43 }
44
45 pub fn with_timeout(mut self, timeout_ms: u64) -> Self {
46 self.timeout_ms = timeout_ms;
47 self
48 }
49}
50
51#[async_trait]
90pub trait ProxyToolExecutor: Send + Sync {
91 async fn exec(&self, tool_name: &str, input: Value) -> Result<String>;
93
94 fn tool_definitions() -> Vec<ProxyToolDef>
96 where
97 Self: Sized;
98}
99
100#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
105pub struct ProxyMetadata {
106 pub tool_type: String,
107 pub endpoint: Option<String>,
108 pub timeout_ms: u64,
109 pub custom: Option<Value>,
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct ProxyToolRequest {
114 pub request_id: String,
115 pub tool_name: String,
116 pub tool_input: Value,
117 pub metadata: ProxyMetadata,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
121pub struct ProxyToolResponse {
122 pub request_id: String,
123 pub result: String,
124 pub is_error: bool,
125}
126
127#[derive(Debug)]
128pub struct ProxyTool {
129 definition: ToolDefinition,
130 metadata: ProxyMetadata,
131}
132
133impl ProxyTool {
134 pub fn new(definition: ToolDefinition, metadata: ProxyMetadata) -> Self {
135 Self {
136 definition,
137 metadata,
138 }
139 }
140
141 pub fn metadata(&self) -> &ProxyMetadata {
142 &self.metadata
143 }
144
145 pub fn definition(&self) -> ToolDefinition {
146 self.definition.clone()
147 }
148}
149
150#[cfg(test)]
151mod tests {
152 use super::*;
153
154 #[test]
155 fn test_proxy_tool_def_creation() {
156 let def = ProxyToolDef::new("test", "测试工具", serde_json::json!({}))
157 .with_priority(true)
158 .with_timeout(60000);
159
160 assert_eq!(def.definition.name, "test");
161 assert!(def.definition.is_priority);
162 assert_eq!(def.timeout_ms, 60000);
163 }
164}