mirror of
https://github.com/openai/codex.git
synced 2026-12-04 15:51:14 +00:00
Preserve agent message phases in codex exec --json (#65606)
## Why JSON event consumers need the model-reported phase to distinguish commentary, partial answers, and final answers. ## What changed Include `phase` on completed `agent_message` items when the model reports one. Expose the optional field and export the `MessagePhase` type in the TypeScript SDK. ## Testing Extend the JSON event test to verify serialization for `commentary`, `partial_answer`, and `final_answer`, omission when no phase is reported, and final-message text tracking in all four cases. GitOrigin-RevId: 96793f40708f0257fec8bb8650eb03b8bec6447b
This commit is contained in:
1 parent
8dc13d36b4
commit
02703997cb
5 files changed
+51
-39
No files matched your search
@@ -147,9 +147,9 @@ impl EventProcessorWithJsonOutput {
|
||||
make_id: impl FnOnce() -> String,
|
||||
) -> Option<ExecThreadItem> {
|
||||
match item {
|
||||
ThreadItemPayload::AgentMessage { text, .. } => Some(ExecThreadItem {
|
||||
ThreadItemPayload::AgentMessage { text, phase, .. } => Some(ExecThreadItem {
|
||||
id: make_id(),
|
||||
details: ThreadItemDetails::AgentMessage(AgentMessageItem { text }),
|
||||
details: ThreadItemDetails::AgentMessage(AgentMessageItem { text, phase }),
|
||||
}),
|
||||
ThreadItemPayload::Reasoning { summary, .. } => {
|
||||
let text = summary.join("\n");
|
||||
@@ -498,10 +498,8 @@ impl EventProcessorWithJsonOutput {
|
||||
}
|
||||
ServerNotification::ItemCompleted(notification) => {
|
||||
if let Some(item) = self.map_completed_item_mut(notification.item.payload) {
|
||||
if let ThreadItemDetails::AgentMessage(AgentMessageItem { text }) =
|
||||
&item.details
|
||||
{
|
||||
self.final_message = Some(text.clone());
|
||||
if let ThreadItemDetails::AgentMessage(message) = &item.details {
|
||||
self.final_message = Some(message.text.clone());
|
||||
}
|
||||
events.push(ThreadEvent::ItemCompleted(ItemCompletedEvent { item }));
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use codex_protocol::models::MessagePhase;
|
||||
use codex_protocol::models::WebSearchAction;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
@@ -137,6 +138,10 @@ pub enum ThreadItemDetails {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, TS)]
|
||||
pub struct AgentMessageItem {
|
||||
pub text: String,
|
||||
/// Message phase, when reported by the model.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
#[ts(optional)]
|
||||
pub phase: Option<MessagePhase>,
|
||||
}
|
||||
|
||||
/// Agent's reasoning summary.
|
||||
|
||||
@@ -31,6 +31,7 @@ use codex_app_server_protocol::WebSearchAction as ApiWebSearchAction;
|
||||
use codex_app_server_protocol::WebSearchItem as ApiWebSearchItem;
|
||||
use codex_protocol::SessionId;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::models::MessagePhase;
|
||||
use codex_protocol::models::PermissionProfile;
|
||||
use codex_protocol::models::WebSearchAction;
|
||||
use codex_protocol::protocol::AskForApproval;
|
||||
@@ -333,6 +334,7 @@ fn unsupported_items_do_not_consume_synthetic_ids() {
|
||||
id: "item_0".to_string(),
|
||||
details: ThreadItemDetails::AgentMessage(AgentMessageItem {
|
||||
text: "hello".to_string(),
|
||||
phase: None,
|
||||
}),
|
||||
},
|
||||
})],
|
||||
@@ -1012,42 +1014,44 @@ fn file_change_declined_maps_to_failed_status() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn agent_message_item_updates_final_message() {
|
||||
let mut processor = EventProcessorWithJsonOutput::new(/*last_message_path*/ None);
|
||||
fn agent_message_item_preserves_phase_and_updates_final_message() {
|
||||
for (phase, expected_phase) in [
|
||||
(None, None),
|
||||
(Some(MessagePhase::Commentary), Some("commentary")),
|
||||
(Some(MessagePhase::PartialAnswer), Some("partial_answer")),
|
||||
(Some(MessagePhase::FinalAnswer), Some("final_answer")),
|
||||
] {
|
||||
let mut processor = EventProcessorWithJsonOutput::new(/*last_message_path*/ None);
|
||||
|
||||
let collected = processor.collect_thread_events(ServerNotification::ItemCompleted(
|
||||
ItemCompletedNotification::new(
|
||||
"thread-1".to_string(),
|
||||
"turn-1".to_string(),
|
||||
ThreadItemPayload::AgentMessage {
|
||||
moderation: None,
|
||||
id: "msg-1".to_string(),
|
||||
text: "hello".to_string(),
|
||||
phase: None,
|
||||
memory_citation: None,
|
||||
delivery: None,
|
||||
questions: None,
|
||||
},
|
||||
/*started_at_ms*/ None,
|
||||
/*completed_at_ms*/ 0,
|
||||
),
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
collected,
|
||||
CollectedThreadEvents {
|
||||
events: vec![ThreadEvent::ItemCompleted(ItemCompletedEvent {
|
||||
item: ExecThreadItem {
|
||||
id: "item_0".to_string(),
|
||||
details: ThreadItemDetails::AgentMessage(AgentMessageItem {
|
||||
text: "hello".to_string(),
|
||||
}),
|
||||
let collected = processor.collect_thread_events(ServerNotification::ItemCompleted(
|
||||
ItemCompletedNotification::new(
|
||||
"thread-1".to_string(),
|
||||
"turn-1".to_string(),
|
||||
ThreadItemPayload::AgentMessage {
|
||||
moderation: None,
|
||||
id: "msg-1".to_string(),
|
||||
text: "hello".to_string(),
|
||||
phase,
|
||||
memory_citation: None,
|
||||
delivery: None,
|
||||
questions: None,
|
||||
},
|
||||
})],
|
||||
status: CodexStatus::Running,
|
||||
/*started_at_ms*/ None,
|
||||
/*completed_at_ms*/ 0,
|
||||
),
|
||||
));
|
||||
|
||||
let mut expected_item = json!({"id": "item_0", "type": "agent_message", "text": "hello"});
|
||||
if let Some(expected_phase) = expected_phase {
|
||||
expected_item["phase"] = json!(expected_phase);
|
||||
}
|
||||
);
|
||||
assert_eq!(processor.final_message(), Some("hello"));
|
||||
assert_eq!(
|
||||
serde_json::to_value(collected.events).expect("serialize agent message events"),
|
||||
json!([{"type": "item.completed", "item": expected_item}]),
|
||||
);
|
||||
assert_eq!(collected.status, CodexStatus::Running);
|
||||
assert_eq!(processor.final_message(), Some("hello"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -14,6 +14,7 @@ export type {
|
||||
export type {
|
||||
ThreadItem,
|
||||
AgentMessageItem,
|
||||
MessagePhase,
|
||||
ReasoningItem,
|
||||
CommandExecutionItem,
|
||||
FileChangeItem,
|
||||
|
||||
@@ -71,12 +71,16 @@ export type McpToolCallItem = {
|
||||
status: McpToolCallStatus;
|
||||
};
|
||||
|
||||
export type MessagePhase = "commentary" | "partial_answer" | "final_answer";
|
||||
|
||||
/** Response from the agent. Either natural-language text or JSON when structured output is requested. */
|
||||
export type AgentMessageItem = {
|
||||
id: string;
|
||||
type: "agent_message";
|
||||
/** Either natural-language text or JSON when structured output is requested. */
|
||||
text: string;
|
||||
/** Message phase, when reported by the model. */
|
||||
phase?: MessagePhase;
|
||||
};
|
||||
|
||||
/** Agent's reasoning summary. */
|
||||
|
||||
Reference in new issue
Block a user