agent_works/script_tool/
provider.rs1use std::collections::BTreeMap;
2use std::path::Path;
3
4use agent_base::tool::Tool;
5use agent_base::{AgentError, AgentResult, Content, ToolContext, ToolExposure};
6use async_trait::async_trait;
7use serde_json::Value;
8
9use crate::tool_provider::{ToolDescriptor, ToolIdentity, ToolProvider};
10
11use super::exec::execute_entry;
12use super::law::script_file_index;
13use super::types::*;
14
15#[derive(Debug, Default)]
20pub struct ScriptProvider {
21 entries: BTreeMap<String, ScriptToolEntry>,
22}
23
24impl ScriptProvider {
25 pub fn new(entries: impl IntoIterator<Item = ScriptToolEntry>) -> Self {
28 let mut provider = Self::default();
29 for entry in entries {
30 provider.entries.entry(entry.name.clone()).or_insert(entry);
31 }
32 provider
33 }
34
35 pub fn insert(&mut self, entry: ScriptToolEntry) -> Option<ScriptToolEntry> {
37 self.entries.insert(entry.name.clone(), entry)
38 }
39
40 pub fn remove(&mut self, name: &str) -> Option<ScriptToolEntry> {
42 self.entries.remove(name)
43 }
44
45 pub fn get(&self, name: &str) -> Option<&ScriptToolEntry> {
46 self.entries.get(name)
47 }
48
49 pub fn len(&self) -> usize {
50 self.entries.len()
51 }
52
53 pub fn is_empty(&self) -> bool {
54 self.entries.is_empty()
55 }
56}
57
58#[async_trait]
59impl ToolProvider for ScriptProvider {
60 async fn list(&self) -> AgentResult<Vec<ToolDescriptor>> {
61 Ok(self
62 .entries
63 .values()
64 .map(|entry| ToolDescriptor {
65 identity: entry.identity(),
66 description: entry.description.clone(),
67 parameters: entry.parameters_envelope(),
68 timeout_ms: Some(entry.timeout_ms()),
69 exposure: entry.exposure.into(),
70 })
71 .collect())
72 }
73
74 async fn call(
75 &self,
76 identity: &ToolIdentity,
77 args: &Value,
78 ctx: &ToolContext,
79 ) -> AgentResult<Vec<Content>> {
80 let ToolIdentity::Script { name } = identity else {
81 return Err(AgentError::ToolNotFound {
82 name: identity.to_key(),
83 });
84 };
85 let Some(entry) = self.entries.get(name) else {
86 return Err(AgentError::ToolNotFound {
87 name: identity.to_key(),
88 });
89 };
90 execute_entry(&identity.to_key(), entry, args, ctx).await
91 }
92
93 async fn health(&self) -> AgentResult<()> {
94 for entry in self.entries.values() {
96 let index = script_file_index(&entry.run)
97 .map_err(|e| AgentError::Internal(format!("script.{}: {e}", entry.name)))?;
98 let path = Path::new(&entry.run[index]);
99 if !path.is_file() {
100 return Err(AgentError::Internal(format!(
101 "script.{} is gone: {}",
102 entry.name,
103 path.display()
104 )));
105 }
106 }
107 Ok(())
108 }
109}
110
111pub struct ScriptTool {
117 name: &'static str,
118 description: &'static str,
119 schema: Value,
120 entry: ScriptToolEntry,
121}
122
123impl ScriptTool {
124 pub fn new(entry: ScriptToolEntry) -> Self {
126 let key = format!("script.{}", entry.name);
127 Self::with_key(&key, entry)
128 }
129
130 pub fn with_key(identity_key: &str, entry: ScriptToolEntry) -> Self {
132 let name: &'static str = Box::leak(identity_key.to_string().into_boxed_str());
133 let description: &'static str = Box::leak(entry.description.clone().into_boxed_str());
134 let schema = entry.parameters_envelope();
135 Self {
136 name,
137 description,
138 schema,
139 entry,
140 }
141 }
142
143 pub fn entry(&self) -> &ScriptToolEntry {
144 &self.entry
145 }
146}
147
148#[async_trait]
149impl Tool for ScriptTool {
150 fn name(&self) -> &'static str {
151 self.name
152 }
153
154 fn description(&self) -> &'static str {
155 self.description
156 }
157
158 fn schema(&self) -> Value {
159 self.schema.clone()
160 }
161
162 async fn call(&self, args: &Value, ctx: &ToolContext) -> AgentResult<Vec<Content>> {
163 execute_entry(self.name, &self.entry, args, ctx).await
164 }
165
166 fn timeout_ms(&self) -> Option<u64> {
167 Some(self.entry.timeout_ms())
168 }
169
170 fn metadata(&self) -> agent_base::ToolMetadata {
171 agent_base::ToolMetadata {
172 name: self.name.to_string(),
173 description: self.description.to_string(),
174 origin: "script".to_string(),
175 version: "unknown".to_string(),
176 requirements: vec![],
177 }
178 }
179
180 fn exposure(&self) -> ToolExposure {
181 self.entry.exposure.into()
182 }
183}