1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Tool and types for requesting user input via the TUI.
use super::spec::{
ApprovalRequirement, ToolCapability, ToolContext, ToolError, ToolResult, ToolSpec,
};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
pub use zagens_core::user_input::UserInputRequest;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInputAnswer {
pub id: String,
pub label: String,
pub value: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserInputResponse {
pub answers: Vec<UserInputAnswer>,
}
pub struct RequestUserInputTool;
#[async_trait]
impl ToolSpec for RequestUserInputTool {
fn name(&self) -> &'static str {
"request_user_input"
}
fn description(&self) -> &'static str {
"Ask the user 1-3 short questions and return their selections."
}
fn input_schema(&self) -> Value {
json!({
"type": "object",
"properties": {
"questions": {
"type": "array",
"items": {
"type": "object",
"properties": {
"header": { "type": "string" },
"id": { "type": "string" },
"question": { "type": "string" },
"options": {
"type": "array",
"items": {
"type": "object",
"properties": {
"label": { "type": "string" },
"description": { "type": "string" }
},
"required": ["label", "description"]
},
"minItems": 2,
"maxItems": 3
}
},
"required": ["header", "id", "question", "options"]
},
"minItems": 1,
"maxItems": 3
}
},
"required": ["questions"]
})
}
fn capabilities(&self) -> Vec<ToolCapability> {
vec![ToolCapability::ReadOnly]
}
fn approval_requirement(&self) -> ApprovalRequirement {
ApprovalRequirement::Auto
}
async fn execute(
&self,
_input: Value,
_context: &ToolContext,
) -> Result<ToolResult, ToolError> {
Err(ToolError::execution_failed(
"request_user_input must be handled by the engine",
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use zagens_core::user_input::{UserInputOption, UserInputQuestion};
#[test]
fn validates_request_shape() {
let request = UserInputRequest {
questions: vec![UserInputQuestion {
header: "Pick".to_string(),
id: "choice".to_string(),
question: "Which option?".to_string(),
options: vec![
UserInputOption {
label: "A".to_string(),
description: "Option A".to_string(),
},
UserInputOption {
label: "B".to_string(),
description: "Option B".to_string(),
},
],
}],
};
assert!(request.validate().is_ok());
}
#[test]
fn rejects_too_many_questions() {
let request = UserInputRequest {
questions: vec![
UserInputQuestion {
header: "Q1".to_string(),
id: "q1".to_string(),
question: "?".to_string(),
options: vec![
UserInputOption {
label: "A".to_string(),
description: "A".to_string(),
},
UserInputOption {
label: "B".to_string(),
description: "B".to_string(),
},
],
},
UserInputQuestion {
header: "Q2".to_string(),
id: "q2".to_string(),
question: "?".to_string(),
options: vec![
UserInputOption {
label: "A".to_string(),
description: "A".to_string(),
},
UserInputOption {
label: "B".to_string(),
description: "B".to_string(),
},
],
},
UserInputQuestion {
header: "Q3".to_string(),
id: "q3".to_string(),
question: "?".to_string(),
options: vec![
UserInputOption {
label: "A".to_string(),
description: "A".to_string(),
},
UserInputOption {
label: "B".to_string(),
description: "B".to_string(),
},
],
},
UserInputQuestion {
header: "Q4".to_string(),
id: "q4".to_string(),
question: "?".to_string(),
options: vec![
UserInputOption {
label: "A".to_string(),
description: "A".to_string(),
},
UserInputOption {
label: "B".to_string(),
description: "B".to_string(),
},
],
},
],
};
assert!(request.validate().is_err());
}
}