context_server_tool.rs

  1use std::sync::Arc;
  2
  3use anyhow::{anyhow, bail, Result};
  4use assistant_tool::Tool;
  5use gpui::{App, Entity, Task};
  6use project::Project;
  7
  8use crate::manager::ContextServerManager;
  9use crate::types;
 10
 11pub struct ContextServerTool {
 12    server_manager: Entity<ContextServerManager>,
 13    server_id: Arc<str>,
 14    tool: types::Tool,
 15}
 16
 17impl ContextServerTool {
 18    pub fn new(
 19        server_manager: Entity<ContextServerManager>,
 20        server_id: impl Into<Arc<str>>,
 21        tool: types::Tool,
 22    ) -> Self {
 23        Self {
 24            server_manager,
 25            server_id: server_id.into(),
 26            tool,
 27        }
 28    }
 29}
 30
 31impl Tool for ContextServerTool {
 32    fn name(&self) -> String {
 33        self.tool.name.clone()
 34    }
 35
 36    fn description(&self) -> String {
 37        self.tool.description.clone().unwrap_or_default()
 38    }
 39
 40    fn input_schema(&self) -> serde_json::Value {
 41        match &self.tool.input_schema {
 42            serde_json::Value::Null => {
 43                serde_json::json!({ "type": "object", "properties": [] })
 44            }
 45            serde_json::Value::Object(map) if map.is_empty() => {
 46                serde_json::json!({ "type": "object", "properties": [] })
 47            }
 48            _ => self.tool.input_schema.clone(),
 49        }
 50    }
 51
 52    fn run(
 53        self: Arc<Self>,
 54        input: serde_json::Value,
 55        _project: Entity<Project>,
 56        cx: &mut App,
 57    ) -> Task<Result<String>> {
 58        if let Some(server) = self.server_manager.read(cx).get_server(&self.server_id) {
 59            cx.foreground_executor().spawn({
 60                let tool_name = self.tool.name.clone();
 61                async move {
 62                    let Some(protocol) = server.client() else {
 63                        bail!("Context server not initialized");
 64                    };
 65
 66                    let arguments = if let serde_json::Value::Object(map) = input {
 67                        Some(map.into_iter().collect())
 68                    } else {
 69                        None
 70                    };
 71
 72                    log::trace!(
 73                        "Running tool: {} with arguments: {:?}",
 74                        tool_name,
 75                        arguments
 76                    );
 77                    let response = protocol.run_tool(tool_name, arguments).await?;
 78
 79                    let mut result = String::new();
 80                    for content in response.content {
 81                        match content {
 82                            types::ToolResponseContent::Text { text } => {
 83                                result.push_str(&text);
 84                            }
 85                            types::ToolResponseContent::Image { .. } => {
 86                                log::warn!("Ignoring image content from tool response");
 87                            }
 88                            types::ToolResponseContent::Resource { .. } => {
 89                                log::warn!("Ignoring resource content from tool response");
 90                            }
 91                        }
 92                    }
 93                    Ok(result)
 94                }
 95            })
 96        } else {
 97            Task::ready(Err(anyhow!("Context server not found")))
 98        }
 99    }
100}