Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/agentic-server-core/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod normalize;
pub mod types;

pub use normalize::normalize_sse_line;
pub use types::{EventFrame, EventPayload, SSEEventType, SSEItemType};
pub use types::{EventFrame, EventPayload, SSEEventType, SSEItemType, WireEvent};
15 changes: 6 additions & 9 deletions crates/agentic-server-core/src/events/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde_json::Value;

use super::types::{EventFrame, EventPayload, SSEEventType, SSEItemType};
use super::types::{EventFrame, EventPayload, SSEEventType, SSEItemType, WireEvent};
use crate::utils::common::{deserialize_from_str_opt, deserialize_from_value_opt};

/// Normalize a raw SSE data line into a typed [`EventFrame`].
Expand All @@ -15,21 +15,18 @@ pub fn normalize_sse_line(line: &str) -> Option<EventFrame> {
return None;
}

let json: Value = deserialize_from_str_opt(data_str)?;
let wire: WireEvent = deserialize_from_str_opt(data_str)?;
let json = wire.to_value();

let event_type = json
.get("type")
.and_then(Value::as_str)
.map_or(SSEEventType::Other, classify_event_type);

let sequence_number = json.get("sequence_number").and_then(Value::as_u64);
let event_type = classify_event_type(&wire.event_type);

let payload = extract_payload(event_type, &json);

Some(EventFrame {
event_type,
payload,
sequence_number,
sequence_number: wire.sequence_number,
wire,
})
}

Expand Down
89 changes: 88 additions & 1 deletion crates/agentic-server-core/src/events/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde_json::Value;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

use crate::types::io::ResponseUsage;

Expand Down Expand Up @@ -109,6 +110,77 @@ pub enum SSEEventType {
Other,
}

impl SSEEventType {
#[must_use]
pub fn as_str(self) -> &'static str {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintaining two parallel 29-arm tables, classify_event_type (normalize.rs:34) and SSEEventType::as_str (types.rs:115), is troublesome; they live in different files and will drift. Implement conversion traits on SSEEventType instead: From<&str> for parsing and TryFrom for &'static str for the wire name (fallible because Other has no wire form and must not serialize as a made-up "unknown"). Then classify_event_type and as_str are both no longer needed, normalize_sse_line just calls SSEEventType::from(wire.event_type.as_str()), and the two impls sit adjacent in types.rs with a round-trip test to keep them in sync.

match self {
Self::ResponseCreated => "response.created",
Self::ResponseInProgress => "response.in_progress",
Self::ResponseCompleted => "response.completed",
Self::ResponseFailed => "response.failed",
Self::ResponseIncomplete => "response.incomplete",
Self::OutputItemAdded => "response.output_item.added",
Self::OutputItemDone => "response.output_item.done",
Self::OutputTextDelta => "response.output_text.delta",
Self::OutputTextDone => "response.output_text.done",
Self::ContentPartAdded => "response.content_part.added",
Self::ContentPartDone => "response.content_part.done",
Self::FunctionCallArgumentsDelta => "response.function_call_arguments.delta",
Self::FunctionCallArgumentsDone => "response.function_call_arguments.done",
Self::CustomToolCallInputDelta => "response.custom_tool_call_input.delta",
Self::CustomToolCallInputDone => "response.custom_tool_call_input.done",
Self::ReasoningTextDelta => "response.reasoning_text.delta",
Self::ReasoningTextDone => "response.reasoning_text.done",
Self::ReasoningPartAdded => "response.reasoning_part.added",
Self::ReasoningPartDone => "response.reasoning_part.done",
Self::ReasoningSummaryTextDelta => "response.reasoning_summary_text.delta",
Self::ReasoningSummaryTextDone => "response.reasoning_summary_text.done",
Self::FileSearchCallSearching => "response.file_search_call.searching",
Self::FileSearchCallCompleted => "response.file_search_call.completed",
Self::WebSearchCallInProgress => "response.web_search_call.in_progress",
Self::WebSearchCallSearching => "response.web_search_call.searching",
Self::WebSearchCallCompleted => "response.web_search_call.completed",
Self::McpToolCallInProgress => "response.mcp_tool_call.in_progress",
Self::McpToolCallCompleted => "response.mcp_tool_call.completed",
Self::Other => "unknown",
}
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WireEvent {
#[serde(rename = "type")]
pub event_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub sequence_number: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub output_index: Option<u64>,
#[serde(flatten)]
pub rest: Map<String, Value>,
}

impl WireEvent {
#[must_use]
pub fn new(event_type: impl Into<String>) -> Self {
Self {
event_type: event_type.into(),
sequence_number: None,
output_index: None,
rest: Map::new(),
}
}

#[must_use]
pub fn into_value(self) -> Value {
serde_json::to_value(self).unwrap_or_else(|_| Value::Object(Map::new()))
}

#[must_use]
pub fn to_value(&self) -> Value {
serde_json::to_value(self).unwrap_or_else(|_| Value::Object(Map::new()))
}
}

/// Typed payload extracted from an SSE event's JSON data.
#[derive(Debug, Clone)]
#[non_exhaustive]
Expand Down Expand Up @@ -206,4 +278,19 @@ pub struct EventFrame {
pub event_type: SSEEventType,
pub payload: EventPayload,
pub sequence_number: Option<u64>,
pub wire: WireEvent,
}

impl EventFrame {
#[must_use]
pub fn synthetic(event_type: SSEEventType, mut wire: WireEvent) -> Self {
event_type.as_str().clone_into(&mut wire.event_type);
let payload = EventPayload::Raw(wire.to_value());
Self {
event_type,
payload,
sequence_number: wire.sequence_number,
wire,
}
}
}
Loading
Loading