diff --git a/crates/goose/src/agents/router_tool_selector.rs b/crates/goose/src/agents/router_tool_selector.rs index 8dd4ac0aa89e..d3090d8b7826 100644 --- a/crates/goose/src/agents/router_tool_selector.rs +++ b/crates/goose/src/agents/router_tool_selector.rs @@ -4,6 +4,7 @@ use rmcp::model::Tool; use anyhow::{Context, Result}; use async_trait::async_trait; +use serde::Serialize; use serde_json::Value; use std::collections::HashMap; use std::collections::VecDeque; @@ -14,8 +15,15 @@ use tokio::sync::RwLock; use crate::agents::tool_vectordb::ToolVectorDB; use crate::conversation::message::Message; use crate::model::ModelConfig; +use crate::prompt_template::render_global_file; use crate::providers::{self, base::Provider}; +#[derive(Serialize)] +struct ToolSelectorContext { + tools: String, + query: String, +} + #[derive(Debug, Clone, PartialEq)] pub enum RouterToolSelectionStrategy { Vector, @@ -282,15 +290,21 @@ impl RouterToolSelector for LLMToolSelector { }; if let Some(tools) = relevant_tools { - // Use LLM to search through tools - let prompt = format!( - "Given the following tools:\n{}\n\nFind the most relevant tools for the query: {}\n\nReturn the tools in this exact format for each tool:\nTool: \nDescription: \nSchema: ", - tools, query - ); - let system_message = Message::user().with_text("You are a tool selection assistant. Your task is to find the most relevant tools based on the user's query."); + // Use template to generate the prompt + let context = ToolSelectorContext { + tools: tools.clone(), + query: query.to_string(), + }; + + let user_prompt = + render_global_file("router_tool_selector.md", &context).map_err(|e| { + ToolError::ExecutionError(format!("Failed to render prompt template: {}", e)) + })?; + + let user_message = Message::user().with_text(&user_prompt); let response = self .llm_provider - .complete(&prompt, &[system_message], &[]) + .complete("", &[user_message], &[]) .await .map_err(|e| ToolError::ExecutionError(format!("Failed to search tools: {}", e)))?; diff --git a/crates/goose/src/permission/permission_judge.rs b/crates/goose/src/permission/permission_judge.rs index f37f1589d75a..71438d38cb4c 100644 --- a/crates/goose/src/permission/permission_judge.rs +++ b/crates/goose/src/permission/permission_judge.rs @@ -3,6 +3,7 @@ use crate::config::permission::PermissionLevel; use crate::config::PermissionManager; use crate::conversation::message::{Message, MessageContent, ToolRequest}; use crate::conversation::Conversation; +use crate::prompt_template::render_global_file; use crate::providers::base::Provider; use chrono::Utc; use indoc::indoc; @@ -13,6 +14,11 @@ use serde_json::Value; use std::collections::HashSet; use std::sync::Arc; +#[derive(Serialize)] +struct PermissionJudgeContext { + // Empty struct for now since the current template doesn't need variables +} + /// Creates the tool definition for checking read-only permissions. fn create_read_only_tool() -> Tool { Tool::new( @@ -133,12 +139,12 @@ pub async fn detect_read_only_tools( let tool = create_read_only_tool(); let check_messages = create_check_messages(tool_requests); + let context = PermissionJudgeContext {}; + let system_prompt = render_global_file("permission_judge.md", &context) + .unwrap_or_else(|_| "You are a good analyst and can detect operations whether they have read-only operations.".to_string()); + let res = provider - .complete( - "You are a good analyst and can detect operations whether they have read-only operations.", - check_messages.messages(), - &[tool.clone()], - ) + .complete(&system_prompt, check_messages.messages(), &[tool.clone()]) .await; // Process the response and return an empty vector if the response is invalid diff --git a/crates/goose/src/prompts/permission_judge.md b/crates/goose/src/prompts/permission_judge.md new file mode 100644 index 000000000000..3d03ff2ceee5 --- /dev/null +++ b/crates/goose/src/prompts/permission_judge.md @@ -0,0 +1 @@ +You are a good analyst and can detect operations whether they have read-only operations. \ No newline at end of file diff --git a/crates/goose/src/prompts/router_tool_selector.md b/crates/goose/src/prompts/router_tool_selector.md new file mode 100644 index 000000000000..d551ccf3f56c --- /dev/null +++ b/crates/goose/src/prompts/router_tool_selector.md @@ -0,0 +1,11 @@ +You are a tool selection assistant. Your task is to find the most relevant tools based on the user's query. + +Given the following tools: +{{ tools }} + +Find the most relevant tools for the query: {{ query }} + +Return the tools in this exact format for each tool: +Tool: +Description: +Schema: \ No newline at end of file