Skip to main content

ag_ui/encode/
protobuf.rs

1//! The AG-UI binary transport — **not implemented**.
2//!
3//! This module exists so that a build with the `protobuf` feature can still
4//! negotiate and name the media type, and so the reason it does nothing is
5//! written down next to the code rather than discovered at runtime.
6//!
7//! # Why there is no encoder here
8//!
9//! The binary transport is defined by `events.proto` in the upstream
10//! `@ag-ui/proto` package. Its `Event` message is a `oneof` over **18** of the
11//! protocol's **33** event types:
12//!
13//! `TEXT_MESSAGE_START`, `TEXT_MESSAGE_CONTENT`, `TEXT_MESSAGE_END`,
14//! `TEXT_MESSAGE_CHUNK`, `TOOL_CALL_START`, `TOOL_CALL_ARGS`, `TOOL_CALL_END`,
15//! `TOOL_CALL_CHUNK`, `STATE_SNAPSHOT`, `STATE_DELTA`, `MESSAGES_SNAPSHOT`,
16//! `RAW`, `CUSTOM`, `RUN_STARTED`, `RUN_FINISHED`, `RUN_ERROR`, `STEP_STARTED`,
17//! `STEP_FINISHED`.
18//!
19//! The other 15 have no wire representation at all: every `REASONING_*` event,
20//! both `ACTIVITY_*` events, all five deprecated `THINKING_*` events, and
21//! `TOOL_CALL_RESULT`. An agent that reasons, reports activities, or returns a
22//! tool result — which is most of them — cannot express its stream in this
23//! format. Encoding such a run would mean silently dropping events, so this
24//! crate declines to encode any.
25//!
26//! Generating the Rust types would also mean either a `build.rs` that requires
27//! `protoc` on every consumer's machine or checked-in generated code that
28//! drifts from upstream. Neither is worth it for a format that cannot carry the
29//! protocol.
30//!
31//! # What to do instead
32//!
33#![cfg_attr(
34    feature = "sse",
35    doc = "Use [`sse`](crate::encode::sse), which carries all 33 event types. Revisit"
36)]
37#![cfg_attr(
38    not(feature = "sse"),
39    doc = "Use the `sse` module — enable the `sse` feature — which carries all 33",
40    doc = "event types. Revisit"
41)]
42//! this module when upstream `events.proto` covers the full set.
43
44use crate::encode::{EventStreamFormatter, PROTOBUF_MEDIA_TYPE};
45use crate::error::{Error, Result};
46use crate::event::{Event, EventType};
47
48/// Placeholder for the binary formatter.
49///
50/// [`content_type`](EventStreamFormatter::content_type) reports the negotiated
51/// media type, but [`encode`](EventStreamFormatter::encode) always fails with
52/// [`Error::UnsupportedTransport`] — see the [module docs](self) for why.
53#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
54pub struct ProtobufFormatter;
55
56impl ProtobufFormatter {
57    /// Builds the placeholder formatter.
58    pub const fn new() -> Self {
59        Self
60    }
61}
62
63impl EventStreamFormatter for ProtobufFormatter {
64    fn content_type(&self) -> &'static str {
65        PROTOBUF_MEDIA_TYPE
66    }
67
68    fn encode(&self, _event: &Event) -> Result<Vec<u8>> {
69        Err(Error::UnsupportedTransport(
70            "the AG-UI protobuf schema covers only 18 of 33 event types; use SSE",
71        ))
72    }
73}
74
75/// The event types upstream `events.proto` can represent.
76///
77/// Everything not listed here has no binary encoding. Useful for asserting in a
78/// test that a given stream would survive the binary transport.
79pub const COVERED_EVENT_TYPES: &[EventType] = &[
80    EventType::TextMessageStart,
81    EventType::TextMessageContent,
82    EventType::TextMessageEnd,
83    EventType::TextMessageChunk,
84    EventType::ToolCallStart,
85    EventType::ToolCallArgs,
86    EventType::ToolCallEnd,
87    EventType::ToolCallChunk,
88    EventType::StateSnapshot,
89    EventType::StateDelta,
90    EventType::MessagesSnapshot,
91    EventType::Raw,
92    EventType::Custom,
93    EventType::RunStarted,
94    EventType::RunFinished,
95    EventType::RunError,
96    EventType::StepStarted,
97    EventType::StepFinished,
98];
99
100/// Whether the binary transport can represent `event_type`.
101pub fn is_covered(event_type: EventType) -> bool {
102    COVERED_EVENT_TYPES.contains(&event_type)
103}