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