refactor(events): unify scoped runtime notifications across clients (#5670)

* refactor(events): unify scoped runtime notifications across clients

* style(tests): sort auto-compaction event imports

* refactor(events): scope connections and remove retry callback plumbing

* docs: remove runtime notification design write-up

* refactor(bus): route operation events through the message bus

* fix(channels): isolate outbound sends by destination

* style(events): group shared notification imports

* refactor(events): converge runtime and hook delivery on MessageBus

* test(events): migrate fixtures to unified message bus
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-06 02:11:19 +08:00
1 parent 3495281282
commit 192e2e90dc
78 files changed
+1620 -1194

No files matched your search

+2
View File
@@ -1,3 +1,4 @@
import { acceptsCompactionPhase } from "../../../packages/client-events/notifications";
import { useCallback, useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
@@ -782,6 +783,7 @@ export function useNanobotStream(
setMessages((prev) => {
const id = `compaction-${ev.compaction_id}`;
const existing = prev.findIndex((message) => message.id === id);
if (!acceptsCompactionPhase(prev[existing]?.compaction?.phase, ev.phase)) return prev;
const next = {
id,
role: "assistant" as const,
+2
View File
@@ -1,3 +1,4 @@
import { decodeNotification } from "../../../packages/client-events/notifications";
import type {
ConnectionStatus,
InboundEvent,
@@ -1072,6 +1073,7 @@ export class NanobotClient {
let parsed: InboundEvent;
try {
parsed = JSON.parse(typeof ev.data === "string" ? ev.data : "") as InboundEvent;
if (decodeNotification(parsed) === null) return;
} catch {
if (wsInboundDebugEnabled()) {
const raw = typeof ev.data === "string" ? ev.data : String(ev.data);
+5 -23
View File
@@ -1,12 +1,13 @@
import type { ContextCompaction, NotificationEvent, RecoveryState } from "../../../packages/client-events/notifications";
export type { RecoveryState, RecoveryStatus } from "../../../packages/client-events/notifications";
type Role = "user" | "assistant" | "tool" | "system";
/** "trace" rows are intermediate agent breadcrumbs (tool-call hints,
* progress pings) that should not be rendered as conversational replies. */
type MessageKind = "message" | "trace" | "compaction";
export interface UIContextCompaction {
id: string;
phase: "started" | "succeeded" | "failed" | "cancelled";
export interface UIContextCompaction extends ContextCompaction {
/** Live wire transitions announce; hydrated transcript rows stay silent. */
announce?: boolean;
}
@@ -63,16 +64,6 @@ export interface TurnUsage {
export type RoundUsage = TurnUsage;
export type RecoveryStatus = "resuming" | "awaiting_user" | "recovered" | "failed";
export interface RecoveryState {
status: RecoveryStatus;
recovery_id: string;
reason?: string;
attempts?: number;
can_continue?: boolean;
}
export interface UIMessage {
id: string;
role: Role;
@@ -1373,16 +1364,7 @@ export type InboundEvent =
/** Optional structured payload on progress frames (channel-specific). */
agent_ui?: AgentUIBlob;
} & InboundTurnMetadata)
| ({
event: "recovery_state";
chat_id: string;
} & RecoveryState)
| ({
event: "context_compaction";
chat_id: string;
compaction_id: string;
phase: UIContextCompaction["phase"];
} & InboundTurnMetadata)
| (NotificationEvent & InboundTurnMetadata)
| ({
event: "file_edit";
chat_id: string;
+28
View File
@@ -0,0 +1,28 @@
import { describe, expect, it } from "vitest";
import fixtures from "../../../packages/client-events/fixtures.json";
import { acceptsCompactionPhase, decodeNotification } from "../../../packages/client-events/notifications";
describe("shared notification contract", () => {
it.each(fixtures)("accepts the Python wire fixture %#", (event) => {
expect(decodeNotification(event)).toEqual(event);
expect(decodeNotification({ ...event, turn_id: "turn" })).toMatchObject(event);
expect(decodeNotification({ ...event, chat_id: 42 })).toBeNull();
expect(decodeNotification({ ...event, turn_id: 42 })).toBeNull();
});
it("does not mistake other event families for invalid notifications", () => {
expect(decodeNotification({ event: "delta", text: "hello" })).toBeUndefined();
expect(decodeNotification({ ...fixtures[0], phase: "unknown" })).toBeNull();
expect(decodeNotification({ ...fixtures[0], compaction_id: "" })).toBeNull();
});
it("allows one terminal transition, including terminal-first hydration", () => {
for (const phase of ["started", "succeeded", "failed", "cancelled"] as const) {
expect(acceptsCompactionPhase(undefined, phase)).toBe(true);
expect(acceptsCompactionPhase("started", phase)).toBe(phase !== "started");
for (const terminal of ["succeeded", "failed", "cancelled"] as const) {
expect(acceptsCompactionPhase(terminal, phase)).toBe(false);
}
}
});
});
+13
View File
@@ -245,6 +245,19 @@ describe("useNanobotStream", () => {
});
});
it("keeps a hydrated terminal compaction when a queued start arrives", () => {
const fake = fakeClient();
const initial = [{ id: "compaction-c1", role: "assistant" as const, content: "",
kind: "compaction" as const, createdAt: 123,
compaction: { id: "c1", phase: "succeeded" as const } }];
const { result } = renderHook(() => useNanobotStream("chat", initial), {
wrapper: wrap(fake.client),
});
act(() => fake.emit("chat", { event: "context_compaction", chat_id: "chat",
compaction_id: "c1", phase: "started" }));
expect(result.current.messages).toEqual(initial);
});
it("batches answer deltas into one animation-frame update", async () => {
const fake = fakeClient();
const requestFrame = vi.spyOn(window, "requestAnimationFrame");