|
| 1 | +use std::borrow::Cow; |
| 2 | + |
| 3 | +use futures::future::BoxFuture; |
| 4 | +use schemars::JsonSchema; |
| 5 | + |
| 6 | +use crate::model::{CallToolResult, Tool, ToolAnnotations}; |
| 7 | + |
| 8 | +use crate::handler::server::tool::{ |
| 9 | + CallToolHandler, DynCallToolHandler, ToolCallContext, schema_for_type, |
| 10 | +}; |
| 11 | + |
| 12 | +pub struct ToolRoute<S> { |
| 13 | + #[allow(clippy::type_complexity)] |
| 14 | + pub call: Box<DynCallToolHandler<S>>, |
| 15 | + pub attr: crate::model::Tool, |
| 16 | +} |
| 17 | + |
| 18 | +impl<S: Send + Sync + 'static> ToolRoute<S> { |
| 19 | + pub fn new<C, A>(attr: impl Into<Tool>, call: C) -> Self |
| 20 | + where |
| 21 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 22 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 23 | + { |
| 24 | + Self { |
| 25 | + call: Box::new(move |context: ToolCallContext<S>| { |
| 26 | + let call = call.clone(); |
| 27 | + Box::pin(async move { context.invoke(call).await }) |
| 28 | + }), |
| 29 | + attr: attr.into(), |
| 30 | + } |
| 31 | + } |
| 32 | + pub fn new_dyn<C>(attr: impl Into<Tool>, call: C) -> Self |
| 33 | + where |
| 34 | + C: Fn(ToolCallContext<S>) -> BoxFuture<'static, Result<CallToolResult, crate::Error>> |
| 35 | + + Send |
| 36 | + + Sync |
| 37 | + + 'static, |
| 38 | + { |
| 39 | + Self { |
| 40 | + call: Box::new(call), |
| 41 | + attr: attr.into(), |
| 42 | + } |
| 43 | + } |
| 44 | + pub fn name(&self) -> &str { |
| 45 | + &self.attr.name |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub trait IntoToolRoute<S, A> { |
| 50 | + fn into_tool_route(self) -> ToolRoute<S>; |
| 51 | +} |
| 52 | + |
| 53 | +impl<S, C, A, T> IntoToolRoute<S, A> for (T, C) |
| 54 | +where |
| 55 | + S: Send + Sync + 'static, |
| 56 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 57 | + T: Into<Tool>, |
| 58 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 59 | +{ |
| 60 | + fn into_tool_route(self) -> ToolRoute<S> { |
| 61 | + ToolRoute::new(self.0.into(), self.1) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<S> IntoToolRoute<S, ()> for ToolRoute<S> |
| 66 | +where |
| 67 | + S: Send + Sync + 'static, |
| 68 | +{ |
| 69 | + fn into_tool_route(self) -> ToolRoute<S> { |
| 70 | + self |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub struct ToolAttrGenerateFunctionAdapter; |
| 75 | +impl<S, F> IntoToolRoute<S, ToolAttrGenerateFunctionAdapter> for F |
| 76 | +where |
| 77 | + S: Send + Sync + 'static, |
| 78 | + F: Fn() -> ToolRoute<S>, |
| 79 | +{ |
| 80 | + fn into_tool_route(self) -> ToolRoute<S> { |
| 81 | + (self)() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +pub trait CallToolHandlerExt<S, A>: Sized |
| 86 | +where |
| 87 | + Self: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 88 | + <Self as CallToolHandler<S, A>>::Fut: 'static, |
| 89 | +{ |
| 90 | + fn name(self, name: impl Into<Cow<'static, str>>) -> WithToolAttr<Self, S, A>; |
| 91 | +} |
| 92 | + |
| 93 | +impl<C, S, A> CallToolHandlerExt<S, A> for C |
| 94 | +where |
| 95 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 96 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 97 | +{ |
| 98 | + fn name(self, name: impl Into<Cow<'static, str>>) -> WithToolAttr<Self, S, A> { |
| 99 | + WithToolAttr { |
| 100 | + attr: Tool::new( |
| 101 | + name.into(), |
| 102 | + "", |
| 103 | + schema_for_type::<crate::model::JsonObject>(), |
| 104 | + ), |
| 105 | + call: self, |
| 106 | + _marker: std::marker::PhantomData, |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +pub struct WithToolAttr<C, S, A> |
| 112 | +where |
| 113 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 114 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 115 | +{ |
| 116 | + pub attr: crate::model::Tool, |
| 117 | + pub call: C, |
| 118 | + pub _marker: std::marker::PhantomData<fn(S, A)>, |
| 119 | +} |
| 120 | + |
| 121 | +impl<C, S, A> IntoToolRoute<S, A> for WithToolAttr<C, S, A> |
| 122 | +where |
| 123 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 124 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 125 | + S: Send + Sync + 'static, |
| 126 | +{ |
| 127 | + fn into_tool_route(self) -> ToolRoute<S> { |
| 128 | + ToolRoute::new(self.attr, self.call) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +impl<C, S, A> WithToolAttr<C, S, A> |
| 133 | +where |
| 134 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 135 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 136 | +{ |
| 137 | + pub fn description(mut self, description: impl Into<Cow<'static, str>>) -> Self { |
| 138 | + self.attr.description = Some(description.into()); |
| 139 | + self |
| 140 | + } |
| 141 | + pub fn parameters<T: JsonSchema>(mut self) -> Self { |
| 142 | + self.attr.input_schema = schema_for_type::<T>().into(); |
| 143 | + self |
| 144 | + } |
| 145 | + pub fn parameters_value(mut self, schema: serde_json::Value) -> Self { |
| 146 | + self.attr.input_schema = crate::model::object(schema).into(); |
| 147 | + self |
| 148 | + } |
| 149 | + pub fn annotation(mut self, annotation: impl Into<ToolAnnotations>) -> Self { |
| 150 | + self.attr.annotations = Some(annotation.into()); |
| 151 | + self |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +#[derive(Default)] |
| 156 | +pub struct ToolRouter<S> { |
| 157 | + #[allow(clippy::type_complexity)] |
| 158 | + pub map: std::collections::HashMap<Cow<'static, str>, ToolRoute<S>>, |
| 159 | + |
| 160 | + pub transparent_when_not_found: bool, |
| 161 | +} |
| 162 | + |
| 163 | +impl<S> IntoIterator for ToolRouter<S> { |
| 164 | + type Item = ToolRoute<S>; |
| 165 | + type IntoIter = std::collections::hash_map::IntoValues<Cow<'static, str>, ToolRoute<S>>; |
| 166 | + |
| 167 | + fn into_iter(self) -> Self::IntoIter { |
| 168 | + self.map.into_values() |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +impl<S> ToolRouter<S> |
| 173 | +where |
| 174 | + S: Send + Sync + 'static, |
| 175 | +{ |
| 176 | + pub fn new() -> Self { |
| 177 | + Self { |
| 178 | + map: std::collections::HashMap::new(), |
| 179 | + transparent_when_not_found: false, |
| 180 | + } |
| 181 | + } |
| 182 | + pub fn with<C, A>(mut self, attr: crate::model::Tool, call: C) -> Self |
| 183 | + where |
| 184 | + C: CallToolHandler<S, A> + Send + Sync + Clone + 'static, |
| 185 | + <C as CallToolHandler<S, A>>::Fut: 'static, |
| 186 | + { |
| 187 | + self.add(ToolRoute::new(attr, call)); |
| 188 | + self |
| 189 | + } |
| 190 | + |
| 191 | + pub fn add(&mut self, item: ToolRoute<S>) { |
| 192 | + self.map.insert(item.attr.name.clone(), item); |
| 193 | + } |
| 194 | + |
| 195 | + pub fn remove<H, A>(&mut self, name: &str) { |
| 196 | + self.map.remove(name); |
| 197 | + } |
| 198 | + pub fn has(&self, name: &str) -> bool { |
| 199 | + self.map.contains_key(name) |
| 200 | + } |
| 201 | + pub async fn call(&self, context: ToolCallContext<S>) -> Result<CallToolResult, crate::Error> { |
| 202 | + let item = self |
| 203 | + .map |
| 204 | + .get(context.name()) |
| 205 | + .ok_or_else(|| crate::Error::invalid_params("tool not found", None))?; |
| 206 | + (item.call)(context).await |
| 207 | + } |
| 208 | + |
| 209 | + pub fn list_all(&self) -> Vec<crate::model::Tool> { |
| 210 | + self.map.values().map(|item| item.attr.clone()).collect() |
| 211 | + } |
| 212 | +} |
0 commit comments