Files
codex/codex-rs/context-fragments/src/answered_question.rs
T
pakrym-oai ce0323f2f2 Support typed content in contextual prompt fragments (#54776)
## Why

Contextual fragments and extension-owned world-state sections currently return strings, limiting them to text content.

## What changed

- Return `ContentItem` from fragment bodies and rendering, preserving classification and attribution. Apply text markers only to `InputText`; pass other content through unchanged.
- Allow extension prompt and world-state contributions to carry typed content, and preserve media in context-window hints.
- Hash non-text world-state fragments by their serialized content and role while retaining the existing text hashing behavior.
- Add shared content-to-tool-output conversion that preserves image references, image detail, and audio.

## Testing

Add coverage for typed world-state hashing and preservation of image content and attribution. Expand code-mode output tests to cover input text, output text, file-backed and inline images, and audio.

GitOrigin-RevId: 2ed00d466a3f169b151202f3eef71e667ea64be1
2026-10-17 04:02:21 +00:00

63 lines
2.0 KiB
Rust

//! Async answers use the desktop's existing reply envelope and stable question identity.
//! Model-authored framing is bounded; oversized identities use the previous plain-text format.
use super::ContextualUserFragment;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::input_text;
/// Identifies an answered question without repeating an unbounded model-authored prompt.
pub struct AnsweredQuestion<'a> {
question_id: Option<&'a str>,
question: String,
answer: &'a str,
}
impl<'a> AnsweredQuestion<'a> {
pub fn new(question_id: &'a str, question: &str, answer: &'a str) -> Self {
let end = question.floor_char_boundary(question.len().min(512));
Self {
question_id: (question_id.len() <= 512).then_some(question_id),
question: question[..end].replace(['\n', '\r'], " "),
answer,
}
}
}
impl ContextualUserFragment for AnsweredQuestion<'_> {
fn content_kind(&self) -> ContentItemKind {
ContentItemKind("user.answered_question".into())
}
fn role(&self) -> &'static str {
"user"
}
fn markers(&self) -> (&'static str, &'static str) {
if self.question_id.is_some() {
Self::type_markers()
} else {
("", "")
}
}
fn type_markers() -> (&'static str, &'static str) {
(
"<send_user_message_question_reply>",
"</send_user_message_question_reply>",
)
}
fn body(&self) -> ContentItem {
let Some(question_id) = self.question_id else {
return input_text(format!("> {}\n\n{}", self.question, self.answer));
};
let replies = serde_json::json!([{
"answer": self.answer,
"question": self.question,
"questionItemId": question_id,
}]);
input_text(format!("\n{replies}\n"))
}
}
#[cfg(test)]
#[path = "answered_question_tests.rs"]
mod tests;