use super::history::enrich_thread_history_from_rollout;
use super::*;
impl BridgeState {
pub(super) fn thread_response_payload(
&self,
runtime_id: &str,
result: &Value,
archived: bool,
) -> Result<Value> {
let thread_value = result.get("thread").context("返回缺少 thread 字段")?;
let thread = normalize_thread(runtime_id, thread_value, archived)?;
self.storage.upsert_thread_index(&thread)?;
let _ = self.storage.record_directory_usage(Path::new(&thread.cwd));
let _ = self.emit_directory_state();
let thread = self.storage.get_thread_index(&thread.id)?.unwrap_or(thread);
let runtime_codex_home = self
.storage
.get_runtime(runtime_id)?
.and_then(|runtime| runtime.codex_home);
let mut render_thread = thread_value.clone();
enrich_thread_history_from_rollout(&mut render_thread, runtime_codex_home.as_deref());
let render_snapshot = self.cache_thread_render_snapshot(build_thread_render_snapshot(
runtime_id,
&render_thread,
)?);
Ok(json!({
"runtimeId": runtime_id,
"thread": thread,
"renderSnapshot": render_snapshot,
}))
}
}