Skip to content

Commit 803de05

Browse files
committed
port(openclaw): anomalyco#8432 tool events + pi stream fixes
1 parent fb1343c commit 803de05

17 files changed

Lines changed: 516 additions & 36 deletions

‎docs/architecture/openclaw-delta-map.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ Upstream PR triage (OpenClaw):
227227
| --- | --- | --- | --- | --- |
228228
| openclaw/openclaw#10818 | performance | adapt | Voyage embedding input_type improves retrieval; provider wiring differs. | Done (already implemented: embedding.ts uses input_type "query" vs "document") |
229229
| openclaw/openclaw#5332 | performance | adapt | L2-normalize embedding vectors to fix semantic search quality. | Done (ported: sanitizeAndNormalizeEmbedding in `src/memory/embeddings.ts` + normalization tests in `src/memory/embeddings.test.ts`) |
230-
| openclaw/openclaw#2576 | reliability | adapt | modelDefault bug when provider=="auto" may have an Zee analogue. | TODO (needs deeper investigation of auto-provider model selection) |
230+
| openclaw/openclaw#2576 | reliability | adapt | modelDefault bug when provider=="auto" may have an Zee analogue. | Done (already implemented: hasRemoteConfig/includeRemote + embedding provider auto selection tests) |
231231
| openclaw/openclaw#1272 | security | adapt | Enforce plugin config schemas; Zee plugin system differs. | Done (already implemented: schema-validator.ts with AJV in loader.ts) |
232232
| openclaw/openclaw#7078 | feature | defer | Full native Voyage support can be revisited after correctness fixes. | TODO |
233233
| openclaw/openclaw#819 | feature | defer | Memory search remote overrides may not match Zee memory model. | TODO |
@@ -248,7 +248,7 @@ Auth gating: see lane 01 (`openclaw/openclaw#9518`).
248248

249249
| Upstream PR | Category | Decision | Rationale | Zee follow-up |
250250
| --- | --- | --- | --- | --- |
251-
| openclaw/openclaw#8432 | reliability | adapt | Fix tool routing/model display/msg updates; upstream Pi stack differs. | TODO (canvas/A2UI exists but specific routing fixes need mapping) |
251+
| openclaw/openclaw#8432 | reliability | adapt | Fix tool routing/model display/msg updates; upstream Pi stack differs. | Done (ported: pi streaming agent updates + targeted tool-event routing/caps in gateway) |
252252
| openclaw/openclaw#2900 | feature | non-goal | Removed-channel quote replies are not in scope for canvas lane. | None |
253253
| openclaw/openclaw#1757 | security | adapt | Per-sender group tool policies and precedence; needs mapping to Zee. | TODO (tool policy is profile-based, not per-sender; needs design work) |
254254
| openclaw/openclaw#2455 | reliability | port | Restore A2UI scaffold assets; prevent runtime breakage. | Done (already implemented in canvas-host/a2ui/) |

‎packages/zee/Swabble/src/agents/pi-embedded-runner.sanitize-session-history.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,16 @@ describe("sanitizeSessionHistory", () => {
203203
sessionId: "test-session",
204204
});
205205

206-
expect(result).toEqual(messages);
206+
expect(result).toEqual([
207+
{
208+
...messages[0],
209+
providerOptions: {
210+
openaiCompatible: {
211+
reasoning_content: "reasoning",
212+
},
213+
},
214+
},
215+
]);
207216
});
208217

209218
it("downgrades openai reasoning only when the model changes", async () => {

‎packages/zee/Swabble/src/agents/pi-embedded-subscribe.handlers.messages.ts‎

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ import {
1919
} from "./pi-embedded-utils.js";
2020
import { createInlineCodeState } from "../markdown/code-spans.js";
2121

22+
const stripTrailingDirective = (text: string): string => {
23+
const openIndex = text.lastIndexOf("[[");
24+
if (openIndex < 0) return text;
25+
const closeIndex = text.indexOf("]]", openIndex + 2);
26+
if (closeIndex >= 0) return text;
27+
return text.slice(0, openIndex);
28+
};
29+
2230
export function handleMessageStart(
2331
ctx: EmbeddedPiSubscribeContext,
2432
evt: AgentEvent & { message: AgentMessage },
@@ -107,34 +115,53 @@ export function handleMessageUpdate(
107115
inlineCode: createInlineCodeState(),
108116
})
109117
.trim();
110-
if (next && next !== ctx.state.lastStreamedAssistant) {
111-
const previousText = ctx.state.lastStreamedAssistant ?? "";
112-
const { text: cleanedText, mediaUrls } = parseReplyDirectives(next);
113-
const { text: previousCleanedText } = parseReplyDirectives(previousText);
114-
if (cleanedText.startsWith(previousCleanedText)) {
115-
const deltaText = cleanedText.slice(previousCleanedText.length);
116-
ctx.state.lastStreamedAssistant = next;
118+
if (next) {
119+
const visibleDelta = chunk ? ctx.stripBlockTags(chunk, ctx.state.partialBlockState) : "";
120+
const parsedDelta = visibleDelta ? ctx.consumePartialReplyDirectives(visibleDelta) : null;
121+
const parsedFull = parseReplyDirectives(stripTrailingDirective(next));
122+
const cleanedText = parsedFull.text;
123+
const mediaUrls = parsedDelta?.mediaUrls;
124+
const hasMedia = Boolean(mediaUrls && mediaUrls.length > 0);
125+
const hasAudio = Boolean(parsedDelta?.audioAsVoice);
126+
const previousCleaned = ctx.state.lastStreamedAssistantCleaned ?? "";
127+
128+
let shouldEmit = false;
129+
let deltaText = "";
130+
if (!cleanedText && !hasMedia && !hasAudio) {
131+
shouldEmit = false;
132+
} else if (previousCleaned && !cleanedText.startsWith(previousCleaned)) {
133+
shouldEmit = false;
134+
} else {
135+
deltaText = cleanedText.slice(previousCleaned.length);
136+
shouldEmit = Boolean(deltaText || hasMedia || hasAudio);
137+
}
138+
139+
ctx.state.lastStreamedAssistant = next;
140+
ctx.state.lastStreamedAssistantCleaned = cleanedText;
141+
142+
if (shouldEmit) {
117143
emitAgentEvent({
118144
runId: ctx.params.runId,
119145
stream: "assistant",
120146
data: {
121147
text: cleanedText,
122148
delta: deltaText,
123-
mediaUrls: mediaUrls?.length ? mediaUrls : undefined,
149+
mediaUrls: hasMedia ? mediaUrls : undefined,
124150
},
125151
});
126152
void ctx.params.onAgentEvent?.({
127153
stream: "assistant",
128154
data: {
129155
text: cleanedText,
130156
delta: deltaText,
131-
mediaUrls: mediaUrls?.length ? mediaUrls : undefined,
157+
mediaUrls: hasMedia ? mediaUrls : undefined,
132158
},
133159
});
160+
ctx.state.emittedAssistantUpdate = true;
134161
if (ctx.params.onPartialReply && ctx.state.shouldEmitPartialReplies) {
135162
void ctx.params.onPartialReply({
136163
text: cleanedText,
137-
mediaUrls: mediaUrls?.length ? mediaUrls : undefined,
164+
mediaUrls: hasMedia ? mediaUrls : undefined,
138165
});
139166
}
140167
}
@@ -182,6 +209,45 @@ export function handleMessageEnd(
182209
: "";
183210
const formattedReasoning = rawThinking ? formatReasoningMessage(rawThinking) : "";
184211

212+
const trimmedText = text.trim();
213+
const parsedText = trimmedText ? parseReplyDirectives(stripTrailingDirective(trimmedText)) : null;
214+
let cleanedText = parsedText?.text ?? "";
215+
let mediaUrls = parsedText?.mediaUrls;
216+
let hasMedia = Boolean(mediaUrls && mediaUrls.length > 0);
217+
218+
if (!cleanedText && !hasMedia) {
219+
const rawTrimmed = rawText.trim();
220+
const rawStrippedFinal = rawTrimmed.replace(/<\s*\/?\s*final\s*>/gi, "").trim();
221+
const rawCandidate = rawStrippedFinal || rawTrimmed;
222+
if (rawCandidate) {
223+
const parsedFallback = parseReplyDirectives(stripTrailingDirective(rawCandidate));
224+
cleanedText = parsedFallback.text ?? rawCandidate;
225+
mediaUrls = parsedFallback.mediaUrls;
226+
hasMedia = Boolean(mediaUrls && mediaUrls.length > 0);
227+
}
228+
}
229+
230+
if (!ctx.state.emittedAssistantUpdate && (cleanedText || hasMedia)) {
231+
emitAgentEvent({
232+
runId: ctx.params.runId,
233+
stream: "assistant",
234+
data: {
235+
text: cleanedText,
236+
delta: cleanedText,
237+
mediaUrls: hasMedia ? mediaUrls : undefined,
238+
},
239+
});
240+
void ctx.params.onAgentEvent?.({
241+
stream: "assistant",
242+
data: {
243+
text: cleanedText,
244+
delta: cleanedText,
245+
mediaUrls: hasMedia ? mediaUrls : undefined,
246+
},
247+
});
248+
ctx.state.emittedAssistantUpdate = true;
249+
}
250+
185251
const addedDuringMessage = ctx.state.assistantTexts.length > ctx.state.assistantTextBaseline;
186252
const chunkerHasBuffered = ctx.blockChunker?.hasBuffered() ?? false;
187253
ctx.finalizeAssistantTexts({ text, addedDuringMessage, chunkerHasBuffered });
@@ -288,4 +354,5 @@ export function handleMessageEnd(
288354
ctx.state.blockState.final = false;
289355
ctx.state.blockState.inlineCode = createInlineCodeState();
290356
ctx.state.lastStreamedAssistant = undefined;
357+
ctx.state.lastStreamedAssistantCleaned = undefined;
291358
}

‎packages/zee/Swabble/src/agents/pi-embedded-subscribe.handlers.types.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ export type EmbeddedPiSubscribeState = {
3737
deltaBuffer: string;
3838
blockBuffer: string;
3939
blockState: { thinking: boolean; final: boolean; inlineCode: InlineCodeState };
40+
partialBlockState: { thinking: boolean; final: boolean; inlineCode: InlineCodeState };
4041
lastStreamedAssistant?: string;
42+
lastStreamedAssistantCleaned?: string;
43+
emittedAssistantUpdate: boolean;
4144
lastStreamedReasoning?: string;
4245
lastBlockReplyText?: string;
4346
assistantMessageIndex: number;
@@ -82,6 +85,10 @@ export type EmbeddedPiSubscribeContext = {
8285
text: string,
8386
options?: { final?: boolean },
8487
) => ReplyDirectiveParseResult | null;
88+
consumePartialReplyDirectives: (
89+
text: string,
90+
options?: { final?: boolean },
91+
) => ReplyDirectiveParseResult | null;
8592
resetAssistantMessageState: (nextAssistantTextBaseline: number) => void;
8693
resetForCompactionRetry: () => void;
8794
finalizeAssistantTexts: (args: {

‎packages/zee/Swabble/src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.filters-final-suppresses-output-without-start-tag.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ describe("subscribeEmbeddedPiSession", () => {
6262

6363
expect(onPartialReply).not.toHaveBeenCalled();
6464
});
65+
it("emits agent events on message_end even without <final> tags", () => {
66+
let handler: ((evt: unknown) => void) | undefined;
67+
const session: StubSession = {
68+
subscribe: (fn) => {
69+
handler = fn;
70+
return () => {};
71+
},
72+
};
73+
74+
const onAgentEvent = vi.fn();
75+
76+
subscribeEmbeddedPiSession({
77+
session: session as unknown as Parameters<typeof subscribeEmbeddedPiSession>[0]["session"],
78+
runId: "run",
79+
enforceFinalTag: true,
80+
onAgentEvent,
81+
});
82+
83+
const assistantMessage = {
84+
role: "assistant",
85+
content: [{ type: "text", text: "Hello world" }],
86+
} as AssistantMessage;
87+
88+
handler?.({ type: "message_start", message: assistantMessage });
89+
handler?.({ type: "message_end", message: assistantMessage });
90+
91+
const payloads = onAgentEvent.mock.calls
92+
.map((call) => call[0]?.data as Record<string, unknown> | undefined)
93+
.filter((value): value is Record<string, unknown> => Boolean(value));
94+
expect(payloads).toHaveLength(1);
95+
expect(payloads[0]?.text).toBe("Hello world");
96+
expect(payloads[0]?.delta).toBe("Hello world");
97+
});
6598
it("does not require <final> when enforcement is off", () => {
6699
let handler: ((evt: unknown) => void) | undefined;
67100
const session: StubSession = {

‎packages/zee/Swabble/src/agents/pi-embedded-subscribe.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
4646
blockBuffer: "",
4747
// Track if a streamed chunk opened a <think> block (stateful across chunks).
4848
blockState: { thinking: false, final: false, inlineCode: createInlineCodeState() },
49+
partialBlockState: { thinking: false, final: false, inlineCode: createInlineCodeState() },
4950
lastStreamedAssistant: undefined,
51+
lastStreamedAssistantCleaned: undefined,
52+
emittedAssistantUpdate: false,
5053
lastStreamedReasoning: undefined,
5154
lastBlockReplyText: undefined,
5255
assistantMessageIndex: 0,
@@ -77,16 +80,23 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
7780
const pendingMessagingTexts = state.pendingMessagingTexts;
7881
const pendingMessagingTargets = state.pendingMessagingTargets;
7982
const replyDirectiveAccumulator = createStreamingDirectiveAccumulator();
83+
const partialReplyDirectiveAccumulator = createStreamingDirectiveAccumulator();
8084

8185
const resetAssistantMessageState = (nextAssistantTextBaseline: number) => {
8286
state.deltaBuffer = "";
8387
state.blockBuffer = "";
8488
blockChunker?.reset();
8589
replyDirectiveAccumulator.reset();
90+
partialReplyDirectiveAccumulator.reset();
8691
state.blockState.thinking = false;
8792
state.blockState.final = false;
8893
state.blockState.inlineCode = createInlineCodeState();
94+
state.partialBlockState.thinking = false;
95+
state.partialBlockState.final = false;
96+
state.partialBlockState.inlineCode = createInlineCodeState();
8997
state.lastStreamedAssistant = undefined;
98+
state.lastStreamedAssistantCleaned = undefined;
99+
state.emittedAssistantUpdate = false;
90100
state.lastBlockReplyText = undefined;
91101
state.lastStreamedReasoning = undefined;
92102
state.lastReasoningSent = undefined;
@@ -402,6 +412,9 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
402412
const consumeReplyDirectives = (text: string, options?: { final?: boolean }) =>
403413
replyDirectiveAccumulator.consume(text, options);
404414

415+
const consumePartialReplyDirectives = (text: string, options?: { final?: boolean }) =>
416+
partialReplyDirectiveAccumulator.consume(text, options);
417+
405418
const flushBlockReplyBuffer = () => {
406419
if (!params.onBlockReply) return;
407420
if (blockChunker?.hasBuffered()) {
@@ -455,6 +468,7 @@ export function subscribeEmbeddedPiSession(params: SubscribeEmbeddedPiSessionPar
455468
flushBlockReplyBuffer,
456469
emitReasoningStream,
457470
consumeReplyDirectives,
471+
consumePartialReplyDirectives,
458472
resetAssistantMessageState,
459473
resetForCompactionRetry,
460474
finalizeAssistantTexts,

‎packages/zee/Swabble/src/gateway/protocol/client-info.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export type GatewayClientInfo = {
3535
instanceId?: string;
3636
};
3737

38+
export const GATEWAY_CLIENT_CAPS = {
39+
TOOL_EVENTS: "tool-events",
40+
} as const;
41+
42+
export type GatewayClientCap = (typeof GATEWAY_CLIENT_CAPS)[keyof typeof GATEWAY_CLIENT_CAPS];
43+
3844
const GATEWAY_CLIENT_ID_SET = new Set<GatewayClientId>(Object.values(GATEWAY_CLIENT_IDS));
3945
const GATEWAY_CLIENT_MODE_SET = new Set<GatewayClientMode>(Object.values(GATEWAY_CLIENT_MODES));
4046

@@ -57,3 +63,11 @@ export function normalizeGatewayClientMode(raw?: string | null): GatewayClientMo
5763
? (normalized as GatewayClientMode)
5864
: undefined;
5965
}
66+
67+
export function hasGatewayClientCap(
68+
caps: string[] | null | undefined,
69+
cap: GatewayClientCap,
70+
): boolean {
71+
if (!Array.isArray(caps)) return false;
72+
return caps.includes(cap);
73+
}

‎packages/zee/Swabble/src/gateway/server-broadcast.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ describe("gateway broadcaster", () => {
4545
},
4646
]);
4747

48-
const { broadcast } = createGatewayBroadcaster({ clients });
48+
const { broadcast, broadcastToConnIds } = createGatewayBroadcaster({ clients });
4949

5050
broadcast("exec.approval.requested", { id: "1" });
5151
broadcast("device.pair.requested", { requestId: "r1" });
5252

5353
expect(approvalsSocket.send).toHaveBeenCalledTimes(1);
5454
expect(pairingSocket.send).toHaveBeenCalledTimes(1);
5555
expect(readSocket.send).toHaveBeenCalledTimes(0);
56+
57+
broadcastToConnIds("tick", { ts: 1 }, new Set(["c-read"]));
58+
expect(readSocket.send).toHaveBeenCalledTimes(1);
59+
expect(approvalsSocket.send).toHaveBeenCalledTimes(1);
60+
expect(pairingSocket.send).toHaveBeenCalledTimes(1);
5661
});
5762
});

‎packages/zee/Swabble/src/gateway/server-broadcast.ts‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,18 @@ function hasEventScope(client: GatewayWsClient, event: string): boolean {
2727

2828
export function createGatewayBroadcaster(params: { clients: Set<GatewayWsClient> }) {
2929
let seq = 0;
30-
const broadcast = (
30+
31+
const broadcastInternal = (
3132
event: string,
3233
payload: unknown,
3334
opts?: {
3435
dropIfSlow?: boolean;
3536
stateVersion?: { presence?: number; health?: number };
3637
},
38+
targetConnIds?: ReadonlySet<string>,
3739
) => {
38-
const eventSeq = ++seq;
40+
const isTargeted = Boolean(targetConnIds);
41+
const eventSeq = isTargeted ? undefined : ++seq;
3942
const frame = JSON.stringify({
4043
type: "event",
4144
event,
@@ -45,8 +48,9 @@ export function createGatewayBroadcaster(params: { clients: Set<GatewayWsClient>
4548
});
4649
const logMeta: Record<string, unknown> = {
4750
event,
48-
seq: eventSeq,
51+
seq: eventSeq ?? "targeted",
4952
clients: params.clients.size,
53+
targets: targetConnIds ? targetConnIds.size : undefined,
5054
dropIfSlow: opts?.dropIfSlow,
5155
presenceVersion: opts?.stateVersion?.presence,
5256
healthVersion: opts?.stateVersion?.health,
@@ -56,6 +60,7 @@ export function createGatewayBroadcaster(params: { clients: Set<GatewayWsClient>
5660
}
5761
logWs("out", "event", logMeta);
5862
for (const c of params.clients) {
63+
if (targetConnIds && !targetConnIds.has(c.connId)) continue;
5964
if (!hasEventScope(c, event)) continue;
6065
const slow = c.socket.bufferedAmount > MAX_BUFFERED_BYTES;
6166
if (slow && opts?.dropIfSlow) continue;
@@ -74,5 +79,28 @@ export function createGatewayBroadcaster(params: { clients: Set<GatewayWsClient>
7479
}
7580
}
7681
};
77-
return { broadcast };
82+
83+
const broadcast = (
84+
event: string,
85+
payload: unknown,
86+
opts?: {
87+
dropIfSlow?: boolean;
88+
stateVersion?: { presence?: number; health?: number };
89+
},
90+
) => broadcastInternal(event, payload, opts);
91+
92+
const broadcastToConnIds = (
93+
event: string,
94+
payload: unknown,
95+
connIds: ReadonlySet<string>,
96+
opts?: {
97+
dropIfSlow?: boolean;
98+
stateVersion?: { presence?: number; health?: number };
99+
},
100+
) => {
101+
if (connIds.size === 0) return;
102+
broadcastInternal(event, payload, opts, connIds);
103+
};
104+
105+
return { broadcast, broadcastToConnIds };
78106
}

0 commit comments

Comments
 (0)