1use std::fmt;
4
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8use super::question::QuestionItem;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17#[non_exhaustive]
18pub enum ProtocolRequestProfile {
19 FocusedReview,
21 ReviewMetadata,
23 SessionTurn,
25 UtilityPrompt,
27}
28
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
31#[serde(deny_unknown_fields)]
32pub struct UtilityResponse {
33 pub answer: String,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
39#[serde(deny_unknown_fields)]
40pub struct ReviewMetadata {
41 pub description: String,
43 pub is_title_change_significant: bool,
45 pub title: String,
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
51#[serde(rename_all = "snake_case")]
52#[schemars(
53 title = "ReviewCommentResolution",
54 description = "Disposition reported for one targeted forge review thread."
55)]
56pub enum ReviewCommentResolution {
57 Fixed,
60 NoChangeNeeded,
63}
64
65#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
67#[schemars(
68 title = "ReviewCommentOutcome",
69 description = "Structured outcome for one forge review thread explicitly included in the turn \
70 prompt."
71)]
72pub struct ReviewCommentOutcome {
73 #[schemars(
75 title = "reply",
76 description = "Concise reply suitable for posting to the forge review thread."
77 )]
78 pub reply: String,
79 #[schemars(
81 title = "resolution",
82 description = "Whether the targeted thread was fixed or required no change."
83 )]
84 pub resolution: ReviewCommentResolution,
85 #[schemars(
87 title = "thread_id",
88 description = "Opaque forge thread identifier copied exactly from the turn prompt."
89 )]
90 pub thread_id: String,
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
98#[schemars(
99 title = "AgentResponse",
100 description = "Wire-format protocol payload used for schema-driven provider output. Return \
101 this object as the entire assistant response payload. Providers that support \
102 output schemas (for example, Codex app-server) are asked to emit this object \
103 directly."
104)]
105pub struct AgentResponse {
106 #[serde(default)]
108 #[schemars(
109 title = "answer",
110 description = "Markdown answer text for delivered work, status updates, or concise \
111 completion notes. Keep clarification requests out of this field and emit \
112 them through `questions` instead."
113 )]
114 pub answer: String,
115 #[serde(default)]
123 #[schemars(title = "questions")]
124 pub questions: Vec<QuestionItem>,
125 #[serde(default)]
131 #[schemars(
132 title = "review_comment_outcomes",
133 description = "Per-thread outcomes for an agent-driven forge comment-resolution turn. \
134 Emit an empty array unless the prompt explicitly supplies forge thread \
135 IDs. Copy each reported `thread_id` exactly from the prompt."
136 )]
137 pub review_comment_outcomes: Vec<ReviewCommentOutcome>,
138}
139
140impl AgentResponse {
141 pub fn plain(text: impl Into<String>) -> Self {
143 Self {
144 answer: text.into(),
145 questions: Vec::new(),
146 review_comment_outcomes: Vec::new(),
147 }
148 }
149
150 pub fn to_display_text(&self) -> String {
153 let mut display_messages = Vec::new();
154 push_display_message(&mut display_messages, &self.answer);
155 push_question_display_messages(&mut display_messages, &self.questions);
156
157 display_messages.join("\n\n")
158 }
159
160 pub fn to_answer_display_text(&self) -> String {
163 let mut display_messages = Vec::new();
164 push_display_message(&mut display_messages, &self.answer);
165
166 display_messages.join("\n\n")
167 }
168
169 pub fn answers(&self) -> Vec<String> {
171 let answer = self.to_answer_display_text();
172 if answer.is_empty() {
173 return Vec::new();
174 }
175
176 vec![answer]
177 }
178
179 pub fn question_items(&self) -> Vec<QuestionItem> {
182 self.questions.iter().take(MAX_QUESTIONS).cloned().collect()
183 }
184}
185
186#[derive(Debug, Clone, PartialEq, Eq)]
188pub enum AgentResponseParseError {
189 Empty,
191 InvalidFormat {
194 reason: String,
196 },
197}
198
199impl fmt::Display for AgentResponseParseError {
200 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
201 match self {
202 Self::Empty => write!(formatter, "response is empty"),
203 Self::InvalidFormat { reason } => {
204 write!(formatter, "response is not valid protocol JSON: {reason}")
205 }
206 }
207 }
208}
209
210impl std::error::Error for AgentResponseParseError {}
211
212pub(crate) const MAX_QUESTIONS: usize = 5;
220pub(crate) fn questions_field_description() -> String {
229 render_field_description_template(
230 QUESTIONS_FIELD_DESCRIPTION_TEMPLATE,
231 "{{ max_questions }}",
232 MAX_QUESTIONS,
233 )
234}
235
236const QUESTIONS_FIELD_DESCRIPTION_TEMPLATE: &str =
237 include_str!("template/questions_field_description.md");
238
239fn push_display_message(display_messages: &mut Vec<String>, text: &str) {
241 if text.trim().is_empty() {
242 return;
243 }
244
245 display_messages.push(text.to_string());
246}
247
248fn push_question_display_messages(display_messages: &mut Vec<String>, questions: &[QuestionItem]) {
250 for question in questions {
251 push_display_message(display_messages, &question.text);
252 }
253}
254
255fn render_field_description_template(template: &str, placeholder: &str, value: usize) -> String {
263 let mut rendered = String::with_capacity(template.len());
264 let mut remaining = template.trim_end();
265
266 while let Some(open_index) = remaining.find("{{") {
267 let after_open = &remaining[open_index..];
268 let Some(close_end) = after_open.find("}}").map(|index| index + "}}".len()) else {
269 break;
270 };
271
272 rendered.push_str(&remaining[..open_index]);
273 rendered.push_str(&collapse_whitespace(&after_open[..close_end]));
274 remaining = &after_open[close_end..];
275 }
276 rendered.push_str(remaining);
277
278 rendered.replace(placeholder, &value.to_string())
279}
280
281fn collapse_whitespace(text: &str) -> String {
283 text.split_whitespace().collect::<Vec<_>>().join(" ")
284}
285
286#[cfg(test)]
287#[path = "model_test.rs"]
288mod tests;