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
//! Frontend-neutral clarification question models and selection helpers.
use std::fmt::Write as _;
use ag_protocol::AgentResponse;
pub use ag_protocol::QuestionItem;
/// Returns the initial highlighted option for a clarification question.
///
/// Questions with predefined options begin on the first option; free-text
/// questions begin with no highlighted option.
#[must_use]
pub fn default_option_index(questions: &[QuestionItem], question_index: usize) -> Option<usize> {
questions
.get(question_index)
.filter(|item| !item.options.is_empty())
.map(|_| 0)
}
/// Returns the chat message announcing one graph node's clarification
/// questions, after the answer they refer to, or `None` when every question
/// is blank. The numbered questions record what was asked; the frontend
/// answers them in its question panel, as for an ordinary turn.
///
/// Storage records it with the node's response so the visible question and
/// the reply routing that depends on it commit together.
#[must_use]
pub fn node_question_notice(node_id: &str, response: &AgentResponse) -> Option<String> {
if response
.questions
.iter()
.all(|question| question.text.trim().is_empty())
{
return None;
}
let mut text = String::new();
let answer = response.answer.trim();
if !answer.is_empty() {
let _ = write!(text, "{answer}\n\n");
}
let _ = write!(text, "`{node_id}` needs your input:");
push_numbered_questions(&mut text, &response.questions);
Some(text)
}
/// Appends each question on its own numbered line, with any predefined
/// options in parentheses.
pub fn push_numbered_questions(text: &mut String, questions: &[QuestionItem]) {
for (index, question) in questions.iter().enumerate() {
let _ = write!(text, "\n{}. {}", index + 1, question.text);
if !question.options.is_empty() {
let _ = write!(text, " ({})", question.options.join(" / "));
}
}
}
#[cfg(test)]
#[path = "question_test.rs"]
mod tests;