Files
codex/codex-rs/core/src/context/memory.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

48 lines
1.5 KiB
Rust

//! Typed, bounded v2 memory context.
use super::ContextualUserFragment;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::input_text;
use codex_utils_output_truncation::TruncationPolicy;
use codex_utils_output_truncation::truncate_text;
/// Memory-owned model context, capped below 10k tokens even for byte-heavy text.
pub enum MemoryContextFragment {
ReadInstructions(String),
ExtractionEvidence(String),
}
impl MemoryContextFragment {
/// Content classification for memory read-path instructions.
pub const READ_INSTRUCTIONS_KIND: &str = "memories.instructions";
}
impl ContextualUserFragment for MemoryContextFragment {
fn role(&self) -> &'static str {
match self {
Self::ReadInstructions(_) => "developer",
Self::ExtractionEvidence(_) => "user",
}
}
fn content_kind(&self) -> ContentItemKind {
ContentItemKind(
match self {
Self::ReadInstructions(_) => Self::READ_INSTRUCTIONS_KIND,
Self::ExtractionEvidence(_) => "memories.extraction_evidence",
}
.to_string(),
)
}
fn markers(&self) -> (&'static str, &'static str) {
Self::type_markers()
}
fn type_markers() -> (&'static str, &'static str) {
("", "")
}
fn body(&self) -> ContentItem {
let (Self::ReadInstructions(text) | Self::ExtractionEvidence(text)) = self;
input_text(truncate_text(text, TruncationPolicy::Bytes(8_900)))
}
}