Files
codex/codex-rs/state/src/delegated_preview.rs
T
Benjamin Carlsson 09bced5ad9 Populate thread previews from delegated task inputs (#50462)
## Why

Delegated turns can begin without a user message, leaving threads without a task preview. Populate the preview from delegated input so these threads are discoverable before a user follow-up.

## What changed

- Extract previews from completed `create_thread` and `send_message_to_thread` outputs in the `codex_app` and `codex_tui` namespaces.
- Unwrap recognized `<codex_delegation>` inputs and decode their escaped task text for display, preserving model content. Keep plain or unrecognized wrappers as text and ignore blank inputs.
- Fill missing previews in state metadata extraction and live thread metadata updates without replacing existing previews or setting the title or `first_user_message`.

## Testing

Add tests for delegation decoding and tool filtering, preview-only metadata updates, preservation of later user-message metadata, and database listing before a user follow-up in both legacy and paginated history modes.

GitOrigin-RevId: f7b3c60ef75e08c76b4084d043e3967bf2bade22
2026-10-02 23:34:11 +00:00

33 lines
1.1 KiB
Rust

//! Display-only previews for recognized delegated inputs; model content is never rewritten.
use codex_protocol::items::FunctionCallOutputItem;
/// Extract the task text from a delegated input, ignoring unrelated tool outputs.
pub fn delegated_output_preview(output: &FunctionCallOutputItem) -> Option<String> {
if !matches!(output.namespace.as_deref(), Some("codex_app" | "codex_tui"))
|| !matches!(
output.name.as_str(),
"create_thread" | "send_message_to_thread"
)
{
return None;
}
let text = output.output.to_text()?;
let preview = text
.strip_prefix("<codex_delegation>\n <source_thread_id>")
.and_then(|delegation| delegation.split_once("</source_thread_id>\n <input>"))
.and_then(|(_, input)| input.strip_suffix("</input>\n</codex_delegation>"))
.map(|input| {
input
.replace("&lt;", "<")
.replace("&gt;", ">")
.replace("&amp;", "&")
})
.unwrap_or(text);
(!preview.trim().is_empty()).then_some(preview)
}
#[cfg(test)]
#[path = "delegated_preview_tests.rs"]
mod tests;