Skip to content

Commit e7cfff2

Browse files
authored
feat(control-ui): stream live draft previews in the typing indicator (#126994)
* feat(control-ui): stream live draft previews in the typing indicator Multi-identity sessions now show what a teammate is typing, not just that they are typing: the composer's per-keystroke session.typing sends carry a bounded tail of the draft (optional preview field, 400 code points max), the gateway throttle re-emits on changed payloads at 250ms (boolean-only stays at 1s, trailing edge keeps the latest draft), and the transcript renders a per-actor bubble with the live text plus a blinking caret. Actors without preview data keep the three-dot bubble. Previews are ephemeral presence: never persisted, never part of the session transcript or model context, excluded from aria-live regions, and gated by the existing >=2-live-viewers, sharing-role, and incognito checks. No new config surface. * chore(protocol): regenerate Swift gateway models for typing preview * fix(gateway): aggregate typing previews across same-actor connections A boolean-only session.typing update from a second connection of the same actor (another tab or device) erased their live draft preview, because typing liveness aggregated per actor while the broadcast preview came only from the latest request. Preview aggregation now lives with the connection aggregation owner: updateTypingConnections tracks per-connection previews and returns the newest non-empty preview among live connections, so the broadcast keeps the active draft until its connection stops or expires. Regression fails pre-fix (event lost its preview field).
1 parent 5c9b734 commit e7cfff2

21 files changed

Lines changed: 681 additions & 51 deletions

‎apps/shared/OpenClawKit/Sources/OpenClawProtocol/GatewayModels.swift‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7341,24 +7341,28 @@ public struct SessionTypingParams: Codable, Sendable {
73417341
public let agentid: String?
73427342
public let sessionid: String
73437343
public let typing: Bool
7344+
public let preview: String?
73447345

73457346
public init(
73467347
sessionkey: String,
73477348
agentid: String? = nil,
73487349
sessionid: String,
7349-
typing: Bool)
7350+
typing: Bool,
7351+
preview: String? = nil)
73507352
{
73517353
self.sessionkey = sessionkey
73527354
self.agentid = agentid
73537355
self.sessionid = sessionid
73547356
self.typing = typing
7357+
self.preview = preview
73557358
}
73567359

73577360
private enum CodingKeys: String, CodingKey {
73587361
case sessionkey = "sessionKey"
73597362
case agentid = "agentId"
73607363
case sessionid = "sessionId"
73617364
case typing
7365+
case preview
73627366
}
73637367
}
73647368

@@ -7386,6 +7390,7 @@ public struct SessionTypingEvent: Codable, Sendable {
73867390
public let agentid: String
73877391
public let actor: SessionSharingIdentity
73887392
public let typing: Bool
7393+
public let preview: String?
73897394
public let ts: Int
73907395

73917396
public init(
@@ -7394,13 +7399,15 @@ public struct SessionTypingEvent: Codable, Sendable {
73947399
agentid: String,
73957400
actor: SessionSharingIdentity,
73967401
typing: Bool,
7402+
preview: String? = nil,
73977403
ts: Int)
73987404
{
73997405
self.sessionkey = sessionkey
74007406
self.sessionid = sessionid
74017407
self.agentid = agentid
74027408
self.actor = actor
74037409
self.typing = typing
7410+
self.preview = preview
74047411
self.ts = ts
74057412
}
74067413

@@ -7410,6 +7417,7 @@ public struct SessionTypingEvent: Codable, Sendable {
74107417
case agentid = "agentId"
74117418
case actor
74127419
case typing
7420+
case preview
74137421
case ts
74147422
}
74157423
}

‎docs/concepts/multi-user.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ The Control UI keeps ownership and presence visually distinct:
5656
- When other people or agents have prompted the session, the row avatar becomes a **pair-stack**: the owner stays in front, and either the single other participant peeks out behind, or a **+N** count summarizes several. The chat header shows the owner chip plus a participant facepile of up to four avatars. The owner is excluded from the participant display.
5757
- Ringed or translucent presence avatars show people who are currently connected or watching; they come from live presence, not ownership, and disappear when those viewers leave.
5858

59+
When several people watch the same session, the transcript also shows a live typing indicator above the composer. Someone typing in the Control UI streams their draft text into the indicator bubble as they type; other typists show a three-dot bubble. Drafts are ephemeral presence: they are never persisted, never enter the session transcript or the model's context, and fade a moment after the typist pauses or sends.
60+
5961
When the loaded session list contains fewer than two distinct owner identities and no session has recorded outside participants, OpenClaw hides all ownership and owner-filter chrome. A single-user gateway therefore looks unchanged.
6062

6163
## Agent-spawned sessions

‎packages/gateway-protocol/src/schema/sessions-suggestions.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,27 @@ describe("session suggestions protocol", () => {
7575
}),
7676
).toBe(false);
7777
});
78+
79+
it("accepts optional bounded typing previews without requiring them", () => {
80+
const params = {
81+
sessionKey: "agent:main:main",
82+
sessionId: "session-main",
83+
typing: true,
84+
};
85+
const event = {
86+
...params,
87+
agentId: "main",
88+
actor: { type: "human", id: "alice", label: "Alice" },
89+
ts: 1,
90+
};
91+
92+
expect(Value.Check(SessionTypingParamsSchema, { ...params, preview: "draft" })).toBe(true);
93+
expect(Value.Check(SessionTypingEventSchema, { ...event, preview: "draft" })).toBe(true);
94+
expect(Value.Check(SessionTypingParamsSchema, { ...params, preview: "x".repeat(401) })).toBe(
95+
false,
96+
);
97+
expect(Value.Check(SessionTypingEventSchema, { ...event, preview: "x".repeat(401) })).toBe(
98+
false,
99+
);
100+
});
78101
});

‎packages/gateway-protocol/src/schema/sessions-suggestions.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ export const SessionTypingParamsSchema = closedObject({
7272
...SessionSuggestionTargetParamsSchema,
7373
sessionId: NonEmptyString,
7474
typing: Type.Boolean(),
75+
preview: Type.Optional(Type.String({ maxLength: 400 })),
7576
});
7677

7778
export const SessionTypingResultSchema = closedObject({
@@ -85,6 +86,7 @@ export const SessionTypingEventSchema = closedObject({
8586
agentId: NonEmptyString,
8687
actor: SessionSharingIdentitySchema,
8788
typing: Type.Boolean(),
89+
preview: Type.Optional(Type.String({ maxLength: 400 })),
8890
ts: Type.Integer({ minimum: 0 }),
8991
});
9092

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2+
import {
3+
broadcastTypingThrottled,
4+
clearSessionTypingState,
5+
updateTypingConnections,
6+
} from "./session-typing-state.js";
7+
8+
beforeEach(() => {
9+
vi.useFakeTimers();
10+
vi.setSystemTime(10_000);
11+
clearSessionTypingState();
12+
});
13+
14+
afterEach(() => {
15+
clearSessionTypingState();
16+
vi.useRealTimers();
17+
});
18+
19+
describe("session typing connection state", () => {
20+
it("retains the newest live preview across an actor's connections", () => {
21+
const key = "shared-preview";
22+
23+
expect(
24+
updateTypingConnections({
25+
key,
26+
connectionId: "preview-tab",
27+
typing: true,
28+
preview: "first draft",
29+
now: 10_000,
30+
}),
31+
).toEqual({ typing: true, preview: "first draft" });
32+
expect(
33+
updateTypingConnections({
34+
key,
35+
connectionId: "presence-tab",
36+
typing: true,
37+
now: 10_100,
38+
}),
39+
).toEqual({ typing: true, preview: "first draft" });
40+
expect(
41+
updateTypingConnections({
42+
key,
43+
connectionId: "presence-tab",
44+
typing: true,
45+
preview: "newer draft",
46+
now: 10_200,
47+
}),
48+
).toEqual({ typing: true, preview: "newer draft" });
49+
});
50+
51+
it("removes a stopped connection's preview while preserving another connection's liveness", () => {
52+
const key = "stopped-preview";
53+
54+
updateTypingConnections({
55+
key,
56+
connectionId: "preview-tab",
57+
typing: true,
58+
preview: "draft",
59+
now: 10_000,
60+
});
61+
updateTypingConnections({
62+
key,
63+
connectionId: "presence-tab",
64+
typing: true,
65+
now: 10_100,
66+
});
67+
68+
expect(
69+
updateTypingConnections({
70+
key,
71+
connectionId: "preview-tab",
72+
typing: false,
73+
now: 10_200,
74+
}),
75+
).toEqual({ typing: true });
76+
expect(
77+
updateTypingConnections({
78+
key,
79+
connectionId: "presence-tab",
80+
typing: false,
81+
now: 10_300,
82+
}),
83+
).toEqual({ typing: false });
84+
expect(
85+
updateTypingConnections({
86+
key,
87+
connectionId: "missing-tab",
88+
typing: false,
89+
now: 10_400,
90+
}),
91+
).toEqual({ typing: false });
92+
});
93+
94+
it("expires stale previews while a refreshed boolean-only connection remains live", () => {
95+
const key = "expired-preview";
96+
97+
updateTypingConnections({
98+
key,
99+
connectionId: "preview-tab",
100+
typing: true,
101+
preview: "expired draft",
102+
now: 10_000,
103+
});
104+
updateTypingConnections({
105+
key,
106+
connectionId: "presence-tab",
107+
typing: true,
108+
now: 11_000,
109+
});
110+
111+
expect(
112+
updateTypingConnections({
113+
key,
114+
connectionId: "presence-tab",
115+
typing: true,
116+
now: 12_500,
117+
}),
118+
).toEqual({ typing: true });
119+
});
120+
});
121+
122+
describe("session typing broadcast throttle", () => {
123+
it("broadcasts a changed preview while the actor remains typing", () => {
124+
const emit = vi.fn(() => true);
125+
const broadcast = (preview: string) =>
126+
broadcastTypingThrottled({
127+
key: "preview-change",
128+
typing: true,
129+
signature: `true\0${preview}`,
130+
intervalMs: 250,
131+
now: Date.now(),
132+
emit,
133+
});
134+
135+
expect(broadcast("first")).toBe(true);
136+
vi.advanceTimersByTime(100);
137+
expect(broadcast("second")).toBe(false);
138+
vi.advanceTimersByTime(150);
139+
140+
expect(emit).toHaveBeenCalledTimes(2);
141+
});
142+
143+
it.each([
144+
{ label: "draft previews", intervalMs: 250, signature: "true\0draft" },
145+
{ label: "boolean-only presence", intervalMs: 1_000, signature: "true\0" },
146+
])("throttles $label at $intervalMs ms", ({ intervalMs, signature }) => {
147+
const emit = vi.fn(() => true);
148+
const broadcast = () =>
149+
broadcastTypingThrottled({
150+
key: signature,
151+
typing: true,
152+
signature,
153+
intervalMs,
154+
now: Date.now(),
155+
emit,
156+
});
157+
158+
broadcast();
159+
vi.advanceTimersByTime(25);
160+
broadcast();
161+
vi.advanceTimersByTime(intervalMs - 26);
162+
expect(emit).toHaveBeenCalledTimes(1);
163+
vi.advanceTimersByTime(1);
164+
expect(emit).toHaveBeenCalledTimes(2);
165+
});
166+
167+
it("emits only the latest draft at the trailing edge of a burst", () => {
168+
const previews: string[] = [];
169+
const broadcast = (preview: string) =>
170+
broadcastTypingThrottled({
171+
key: "preview-burst",
172+
typing: true,
173+
signature: `true\0${preview}`,
174+
intervalMs: 250,
175+
now: Date.now(),
176+
emit: () => {
177+
previews.push(preview);
178+
return true;
179+
},
180+
});
181+
182+
broadcast("first");
183+
vi.advanceTimersByTime(50);
184+
broadcast("second");
185+
vi.advanceTimersByTime(50);
186+
broadcast("latest");
187+
vi.advanceTimersByTime(150);
188+
189+
expect(previews).toEqual(["first", "latest"]);
190+
});
191+
192+
it("preserves boolean-only cancellation and trailing stop behavior", () => {
193+
const updates: boolean[] = [];
194+
const broadcast = (typing: boolean) =>
195+
broadcastTypingThrottled({
196+
key: "boolean-only",
197+
typing,
198+
signature: `${typing}\0`,
199+
intervalMs: 1_000,
200+
now: Date.now(),
201+
emit: () => {
202+
updates.push(typing);
203+
return true;
204+
},
205+
});
206+
207+
broadcast(true);
208+
vi.advanceTimersByTime(100);
209+
broadcast(false);
210+
vi.advanceTimersByTime(100);
211+
broadcast(true);
212+
vi.advanceTimersByTime(800);
213+
expect(updates).toEqual([true, true]);
214+
215+
vi.advanceTimersByTime(100);
216+
broadcast(false);
217+
vi.advanceTimersByTime(900);
218+
expect(updates).toEqual([true, true, false]);
219+
});
220+
});

0 commit comments

Comments
 (0)