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 name the
4//! media type, and so the reason it does nothing is written down next to the
5//! code rather than discovered at runtime. [`ProtobufFormatter`] reports the
6//! binary media type and refuses every event; negotiation never picks that
7//! media type, with or without this feature.
8//!
9//! # Why there is no encoder here
10//!
11//! The binary transport is defined by `events.proto` in the upstream
12//! `@ag-ui/proto` package. When this module was written, its `Event` message
13//! was a `oneof` over 21 of the 36 event types this crate defines, with no
14//! `REASONING_*`, `ACTIVITY_*` or `THINKING_*` event and no
15//! `TOOL_CALL_RESULT`. Encoding a run would have meant silently dropping
16//! events, so this crate declined to encode any.
17//!
18//! That reason is gone. Upstream now generates `events.proto` from the AG-UI
19//! 1.0 JSON Schema, and both its `EventType` enum and its `oneof` cover all 31
20//! 1.0 event types. Only the five deprecated `THINKING_*` events, which 1.0
21//! retired, are absent.
22//!
23//! The encoder is still not written
24//! ([#46](https://github.com/KimSoungRyoul/ag-ui-rust/issues/46)), for two
25//! reasons:
26//!
27//! - Nothing would ask for it. The stock TypeScript client, `HttpAgent` in
28//!   `@ag-ui/client`, sends `Accept: text/event-stream`, so a server that
29//!   could encode protobuf would still not be asked to.
30//! - It is a second encoding to keep in step with a generated schema, for a
31//!   path nobody has asked this SDK for. Generating the Rust types means
32//!   either a `build.rs` that requires `protoc` on every consumer's machine or
33//!   checked-in generated code to refresh each time upstream regenerates.
34//!
35//! An encoder can be added behind this same feature later without a breaking
36//! change.
37//!
38//! # What to do instead
39//!
40#![cfg_attr(
41    feature = "sse",
42    doc = "Use [`sse`](crate::encode::sse), which carries every event type this crate defines."
43)]
44#![cfg_attr(
45    not(feature = "sse"),
46    doc = "Use the `sse` module — enable the `sse` feature — which carries every event",
47    doc = "type this crate defines."
48)]
49
50use crate::encode::{EventStreamFormatter, PROTOBUF_MEDIA_TYPE};
51use crate::error::{Error, Result};
52use crate::event::Event;
53
54/// Placeholder for the binary formatter.
55///
56/// [`content_type`](EventStreamFormatter::content_type) reports the binary
57/// media type, but [`encode`](EventStreamFormatter::encode) always fails with
58/// [`Error::UnsupportedTransport`] — see the [module docs](self) for why.
59#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
60pub struct ProtobufFormatter;
61
62impl ProtobufFormatter {
63    /// Builds the placeholder formatter.
64    pub const fn new() -> Self {
65        Self
66    }
67}
68
69impl EventStreamFormatter for ProtobufFormatter {
70    fn content_type(&self) -> &'static str {
71        PROTOBUF_MEDIA_TYPE
72    }
73
74    fn encode(&self, _event: &Event) -> Result<Vec<u8>> {
75        Err(Error::UnsupportedTransport(
76            "this crate has no AG-UI protobuf encoder; use SSE",
77        ))
78    }
79}