tycode_core/tools/
ask_user_question.rs1use crate::chat::events::{ToolRequest as ToolRequestEvent, ToolRequestType};
2use crate::tools::r#trait::{ToolCallHandle, ToolCategory, ToolExecutor, ToolOutput, ToolRequest};
3use crate::tools::ToolName;
4use anyhow::{bail, Result};
5use serde_json::{json, Value};
6
7pub struct AskUserQuestion;
8
9impl AskUserQuestion {
10 pub fn tool_name() -> ToolName {
11 ToolName::new("ask_user_question")
12 }
13}
14
15struct AskUserQuestionHandle {
16 question: String,
17 tool_use_id: String,
18}
19
20#[async_trait::async_trait(?Send)]
21impl ToolCallHandle for AskUserQuestionHandle {
22 fn tool_request(&self) -> ToolRequestEvent {
23 ToolRequestEvent {
24 tool_call_id: self.tool_use_id.clone(),
25 tool_name: "ask_user_question".to_string(),
26 tool_type: ToolRequestType::Other {
27 args: json!({ "question": self.question }),
28 },
29 }
30 }
31
32 async fn execute(self: Box<Self>) -> ToolOutput {
33 ToolOutput::PromptUser {
34 question: self.question,
35 }
36 }
37}
38
39#[async_trait::async_trait(?Send)]
40impl ToolExecutor for AskUserQuestion {
41 fn name(&self) -> String {
42 "ask_user_question".to_string()
43 }
44
45 fn description(&self) -> String {
46 "Ask the user a question to get clarification or additional information. Use this when you need specific input from the user to proceed with the task or are stuck and are unsure how to make progress.".to_string()
47 }
48
49 fn input_schema(&self) -> Value {
50 serde_json::json!({
51 "type": "object",
52 "properties": {
53 "question": {
54 "type": "string",
55 "description": "The question to ask the user"
56 },
57 },
58 "required": ["question"]
59 })
60 }
61
62 fn category(&self) -> ToolCategory {
63 ToolCategory::Meta
64 }
65
66 async fn process(&self, request: &ToolRequest) -> Result<Box<dyn ToolCallHandle>> {
67 let Some(question) = request.arguments["question"].as_str() else {
68 bail!("Missing required argument \"question\"");
69 };
70
71 Ok(Box::new(AskUserQuestionHandle {
72 question: question.to_string(),
73 tool_use_id: request.tool_use_id.clone(),
74 }))
75 }
76}