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
17 changes: 17 additions & 0 deletions provider/aguiprovider/agui.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ type pendingToolCall struct {

type toolCallAccumulator struct {
pending map[string]*pendingToolCall
// customSeq counts emitted custom events so each one is assigned a unique
// synthetic MessageID, keeping it in its own message rather than being merged
// into (and overwriting) an unrelated assistant message when collected.
customSeq int
// lastChunkMessageID is the MessageID of the most recent
// TextMessageChunkEvent that carried one. Chunks that omit MessageID
// continue that message.
Expand Down Expand Up @@ -510,6 +514,19 @@ func (a *toolCallAccumulator) onEvent(evt aguiEvents.Event) ([]*agent.ResponseUp
CreatedAt: eventTime(evt),
Contents: message.Contents{newJSONDataContent(e.Delta, "application/json-patch+json")},
}}, nil
case *aguiEvents.CustomEvent:
a.customSeq++
return []*agent.ResponseUpdate{{
Role: message.RoleAssistant,
MessageID: fmt.Sprintf("agui-custom-%d", a.customSeq),
CreatedAt: eventTime(evt),
AdditionalProperties: map[string]any{
"agui_custom_event": map[string]any{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parity issue — key name diverges from Python SDK

The upstream Python _handle_custom_event uses "ag_ui_custom_event" (with underscore after ag) as the additional_properties key. This PR uses "agui_custom_event" (no underscore). Callers inspecting AdditionalProperties cross-SDK will need different spellings.

Suggested rename to "ag_ui_custom_event" to match microsoft/agent-framework_event_converters.py.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parity issue — key name and payload shape diverge from the Python client

The Python AG-UI client (_event_converters.py → _handle_custom_event) surfaces custom events under the key "ag_ui_custom_event" (with an underscore between ag and ui), whereas this Go implementation uses "agui_custom_event". Callers consuming both SDKs will need to check different keys for the same concept.

The Python payload also includes thread_id, run_id, and raw_type alongside name/value:

return ChatResponseUpdate(
    role="assistant",
    contents=[],
    additional_properties={
        "thread_id": self.thread_id,
        "run_id": self.run_id,
        "ag_ui_custom_event": {
            "name": event.get("name"),
            "value": event.get("value"),
            "raw_type": raw_event_type,
        },
    },
)

Upstream Python reference: python/packages/ag-ui/agent_framework_ag_ui/_event_converters.py

Suggested fixes:

  1. Rename the key to "ag_ui_custom_event" to match the Python SDK.
  2. Add thread_id and run_id to AdditionalProperties on the same ResponseUpdate (the Go run() loop already injects agui_thread_id; consider threading it through to the accumulator for symmetry).
  3. Consider exposing a raw_type field if the Go CustomEvent type carries equivalent information.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

PratikDhanave (@PratikDhanave) can you resolve the parity gap?

"name": e.Name,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parity issue — missing thread_id/run_id correlation fields

The Python _handle_custom_event includes thread_id and run_id in the additional_properties alongside the custom-event payload (they are tracked state in the converter). Go omits them, so consumers of this update lose run-correlation context that Python callers receive.

Consider adding the AG-UI thread/run IDs here to keep observability parity with the Python SDK.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parity gap: missing context metadata and raw_type field

The Python reference implementation (_event_converters.py → _handle_custom_event) includes thread_id, run_id, and raw_type alongside name/value:

additional_properties={
    "thread_id": self.thread_id,
    "run_id": self.run_id,
    "ag_ui_custom_event": {
        "name": event.get("name"),
        "value": event.get("value"),
        "raw_type": raw_event_type,  # "CUSTOM" or "CUSTOM_EVENT"
    },
}

This Go implementation differs in three ways:

  1. Missing agui_thread_id / agui_run_id: Every other ResponseUpdate emitted here (RunStarted, RunFinished, StateSnapshot, etc.) includes agui_thread_id and agui_run_id for consumer correlation. The custom-event update omits them, breaking session correlation for callers that rely on these keys.

  2. Missing raw_type: Python surfaces the wire-level event type string ("CUSTOM" or "CUSTOM_EVENT") so consumers can distinguish sub-variants. Go discards it.

  3. Key name divergence ("agui_custom_event" vs Python "ag_ui_custom_event"): The agui_ prefix is consistent with Go's own convention, so this may be intentional — but it should be documented as an explicit SDK divergence so consumers know cross-language payloads are not interchangeable.

Suggested fix — align with the pattern used for other metadata updates:

case *aguiEvents.CustomEvent:
    a.customSeq++
    return []*agent.ResponseUpdate{{
        Role:      message.RoleAssistant,
        MessageID: fmt.Sprintf("agui-custom-%d", a.customSeq),
        CreatedAt: eventTime(evt),
        AdditionalProperties: map[string]any{
            "agui_thread_id": a.threadID,  // thread in from run() closure or accumulator field
            "agui_run_id":    a.runID,
            "agui_custom_event": map[string]any{
                "name":  e.Name,
                "value": e.Value,
            },
        },
    }}, nil

"value": e.Value,
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cross-SDK parity gap: agui_custom_event metadata shape diverges from Python upstream

The Python implementation in python/packages/ag-ui/agent_framework_ag_ui/_event_converters.py (method _handle_custom_event) surfaces custom events with two extra fields that the Go implementation omits:

  1. raw_type — the original wire event-type string (e.g. "CUSTOM" vs "CUSTOM_EVENT"). Python records "raw_type": raw_event_type alongside name and value so consumers can distinguish protocol spellings. Go drops this field entirely.
  2. thread_id / run_id — the Python response always includes the current thread and run identifiers in the additional_properties of the custom-event update (just as _handle_run_finished does). Go omits them from the agui-custom-* update, so consumers who rely on those IDs to correlate events cannot do so from the custom update alone.

In addition, the key name itself differs: Python uses ag_ui_custom_event while Go uses agui_custom_event. This is a breaking difference for consumers that read the property by name across SDKs.

Suggested remediation:

  • Rename the key to ag_ui_custom_event (or choose a canonical cross-SDK spelling and update the Python side) and document the chosen name.
  • Add thread_id / run_id to the AdditionalProperties of the returned ResponseUpdate, sourced from the accumulator's current run state (analogous to how agui_thread_id is already injected elsewhere).
  • Add a raw_type field populated from the wire event type string if cross-SDK portability of the event-type spelling matters to callers.

},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parity gap: custom event metadata key diverges from Python upstream

The Python _handle_custom_event uses "ag_ui_custom_event" as the AdditionalProperties key (_event_converters.py L272), while this PR uses "agui_custom_event" (missing underscore between ag and ui). Any caller normalizing across SDKs using the Python key convention will silently find nothing from the Go provider.

Suggested fix: rename to "ag_ui_custom_event".

}}, nil
case *aguiEvents.MessagesSnapshotEvent:
return []*agent.ResponseUpdate{{
Role: message.RoleAssistant,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Parity gap: missing thread_id, run_id, and raw_type fields

The Python upstream surfaces three additional fields alongside ag_ui_custom_event in the AdditionalProperties of the custom event update (_event_converters.py L269–276):

additional_properties={
    "thread_id": self.thread_id,
    "run_id": self.run_id,
    "ag_ui_custom_event": {
        "name": ...,
        "value": ...,
        "raw_type": raw_event_type,   # distinguishes CUSTOM vs CUSTOM_EVENT
    },
}

The Go implementation omits thread_id, run_id, and the raw_type sub-field. thread_id and run_id are already surfaced on other updates (e.g. RunStartedEvent and MessagesSnapshotEvent), and raw_type lets callers distinguish the two CUSTOM wire variants. Consider adding at minimum raw_type inside agui_custom_event and the session IDs at the top level to stay aligned with the Python contract.

Expand Down
93 changes: 93 additions & 0 deletions provider/aguiprovider/agui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,52 @@ func TestAGUIAgentRun_MapsReasoningEvents(t *testing.T) {
}
}

func TestAGUIAgentRun_SurfacesCustomEventAsMetadata(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var input aguiTypes.RunAgentInput
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
t.Fatalf("decode request: %v", err)
}

w.Header().Set("Content-Type", "text/event-stream")
writeSSE(t, w, aguiEvents.NewRunStartedEvent(input.ThreadID, input.RunID))
writeSSE(t, w, aguiEvents.NewCustomEvent("predictive_state", aguiEvents.WithValue(map[string]any{"foo": "bar"})))
writeSSE(t, w, aguiEvents.NewRunFinishedEvent(input.ThreadID, input.RunID))
}))
defer server.Close()

a := aguiprovider.NewAgent(newTestClient(server.URL), aguiprovider.AgentConfig{})
resp, err := a.Run(context.Background(), []*message.Message{message.NewText("hi")}).Collect()
if err != nil {
t.Fatalf("run error: %v", err)
}

var custom map[string]any
for _, msg := range resp.Messages {
if v, ok := msg.AdditionalProperties["agui_custom_event"]; ok {
m, ok := v.(map[string]any)
if !ok {
t.Fatalf("agui_custom_event = %T, want map[string]any", v)
}
custom = m
break
}
}
if custom == nil {
t.Fatal("expected an update carrying agui_custom_event metadata")
}
if custom["name"] != "predictive_state" {
t.Errorf("custom event name = %v, want %q", custom["name"], "predictive_state")
}
value, ok := custom["value"].(map[string]any)
if !ok {
t.Fatalf("custom event value = %T, want map[string]any", custom["value"])
}
if value["foo"] != "bar" {
t.Errorf("custom event value[foo] = %v, want %q", value["foo"], "bar")
}
}

func TestAGUIAgentRun_InvokesTools_WhenFunctionCallsReturned(t *testing.T) {
var mu sync.Mutex
requestCount := 0
Expand Down Expand Up @@ -638,6 +684,53 @@ func TestAGUIAgentRun_ConvertsStateSnapshotEventToDataContent(t *testing.T) {
}
}

func TestAGUIAgentRun_PreservesMultipleCustomEvents(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
writeSSE(t, w, aguiEvents.NewRunStartedEvent("thread-1", "run-1"))
// An assistant text message precedes the custom events; the custom
// events must not be merged into (and thus mutate) it.
writeSSE(t, w, aguiEvents.NewTextMessageStartEvent("msg-1", aguiEvents.WithRole("assistant")))
writeSSE(t, w, aguiEvents.NewTextMessageContentEvent("msg-1", "Hello"))
writeSSE(t, w, aguiEvents.NewTextMessageEndEvent("msg-1"))
writeSSE(t, w, aguiEvents.NewCustomEvent("progress", aguiEvents.WithValue("first")))
writeSSE(t, w, aguiEvents.NewCustomEvent("progress", aguiEvents.WithValue("second")))
writeSSE(t, w, aguiEvents.NewRunFinishedEvent("thread-1", "run-1"))
}))
defer server.Close()

a := aguiprovider.NewAgent(newTestClient(server.URL), aguiprovider.AgentConfig{})
resp, err := a.RunText(context.Background(), "hi").Collect()
if err != nil {
t.Fatalf("run error: %v", err)
}

var values []any
for _, m := range resp.Messages {
raw, ok := m.AdditionalProperties["agui_custom_event"]
if !ok {
continue
}
ce, ok := raw.(map[string]any)
if !ok {
t.Fatalf("agui_custom_event = %T, want map[string]any", raw)
}
values = append(values, ce["value"])
// A custom event must live in its own message, not be attached to the
// assistant text message.
if got := m.String(); got != "" {
t.Fatalf("custom event message has unexpected text content %q", got)
}
}

if len(values) != 2 {
t.Fatalf("preserved custom events = %d (%v), want 2", len(values), values)
}
if values[0] != "first" || values[1] != "second" {
t.Fatalf("custom event values = %v, want [first second]", values)
}
}

func TestAGUIAgentRun_SurfacesMessagesSnapshotEvent(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
Expand Down
Loading