@wherever-dev/client 0.7.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +63 -0
- package/dist/client.d.ts +93 -2
- package/dist/client.js +960 -63
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +69 -0
- package/dist/types.js.map +1 -1
- package/package.json +23 -3
- package/src/client.ts +1031 -63
- package/src/types.ts +97 -0
- package/test/agent-pending.test.ts +0 -107
- package/test/conversation-mode-flag.test.ts +0 -111
- package/test/creating-session-watchdog.test.ts +0 -82
- package/test/folder-conflict-read-only.test.ts +0 -145
- package/test/folder-missing-read-only.test.ts +0 -114
- package/test/harness.ts +0 -97
- package/test/message-delivery-confirmation.test.ts +0 -320
- package/test/mid-stream-steer.test.ts +0 -91
- package/test/new-session-clone-progress.test.ts +0 -263
- package/test/optimistic-force-command.test.ts +0 -127
- package/test/queued-steer-restored.test.ts +0 -175
- package/test/reconnect-rejoins-session.test.ts +0 -136
- package/test/restore-panel.test.ts +0 -324
- package/test/resume-keeps-session.test.ts +0 -118
- package/test/session-load-by-id.test.ts +0 -100
- package/test/session-reload.test.ts +0 -82
- package/test/skills-autocomplete.test.ts +0 -113
- package/test/steer-queue-cancel.test.ts +0 -130
- package/test/superseded-connection-key.test.ts +0 -70
- package/test/superseded-session-load.test.ts +0 -180
- package/test/tool-aborted-render.test.ts +0 -170
- package/test/tool-duration-history.test.ts +0 -93
- package/test/tool-duration.test.ts +0 -86
- package/test/tool-interrupted-history.test.ts +0 -294
- package/tsconfig.json +0 -16
package/test/harness.ts
DELETED
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
// Minimal fake WebSocket for driving WhereverClient.handleMessage deterministically
|
|
2
|
-
// in unit tests. It records outbound frames and lets a test push inbound frames
|
|
3
|
-
// (server -> client) synchronously. No real network. Mirrors the tiny slice of
|
|
4
|
-
// the WHATWG WebSocket API that the client uses (readyState, addEventListener for
|
|
5
|
-
// open/message/close/error, send, close).
|
|
6
|
-
|
|
7
|
-
export type WSListener = (event: any) => void;
|
|
8
|
-
|
|
9
|
-
export class FakeWebSocket {
|
|
10
|
-
// WHATWG readyState constants.
|
|
11
|
-
static CONNECTING = 0;
|
|
12
|
-
static OPEN = 1;
|
|
13
|
-
static CLOSING = 2;
|
|
14
|
-
static CLOSED = 3;
|
|
15
|
-
|
|
16
|
-
url: string;
|
|
17
|
-
readyState = FakeWebSocket.CONNECTING;
|
|
18
|
-
sent: any[] = [];
|
|
19
|
-
private listeners: Record<string, Set<WSListener>> = {
|
|
20
|
-
open: new Set(),
|
|
21
|
-
message: new Set(),
|
|
22
|
-
close: new Set(),
|
|
23
|
-
error: new Set(),
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
constructor(url: string) {
|
|
27
|
-
this.url = url;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
addEventListener(type: string, cb: WSListener) {
|
|
31
|
-
this.listeners[type]?.add(cb);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
removeEventListener(type: string, cb: WSListener) {
|
|
35
|
-
this.listeners[type]?.delete(cb);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
removeAllListeners() {
|
|
39
|
-
for (const key of Object.keys(this.listeners)) {
|
|
40
|
-
this.listeners[key].clear();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
send(data: string) {
|
|
45
|
-
this.sent.push(JSON.parse(data));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
close() {
|
|
49
|
-
if (this.readyState === FakeWebSocket.CLOSED) return;
|
|
50
|
-
this.readyState = FakeWebSocket.CLOSED;
|
|
51
|
-
this.emit('close', {});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// terminate() is used by the liveness watchdog's forcible teardown path.
|
|
55
|
-
terminate() {
|
|
56
|
-
this.close();
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// --- test-only helpers -------------------------------------------------
|
|
60
|
-
|
|
61
|
-
/** Simulate the socket opening (server accepted the connection). */
|
|
62
|
-
open() {
|
|
63
|
-
this.readyState = FakeWebSocket.OPEN;
|
|
64
|
-
this.emit('open', {});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/** Push a server -> client frame. */
|
|
68
|
-
receive(msg: any) {
|
|
69
|
-
this.emit('message', {data: JSON.stringify(msg)});
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** Last outbound frame of a given type, or undefined. */
|
|
73
|
-
lastSentOfType(type: string) {
|
|
74
|
-
return [...this.sent].reverse().find((m) => m.type === type);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private emit(type: string, event: any) {
|
|
78
|
-
for (const cb of this.listeners[type] ?? []) cb(event);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// A WebSocket constructor that records every instance it creates, so a test can
|
|
83
|
-
// grab the live socket the client just opened and drive it. Assigned to
|
|
84
|
-
// config.WebSocketCtor.
|
|
85
|
-
export function makeWSFactory() {
|
|
86
|
-
const instances: FakeWebSocket[] = [];
|
|
87
|
-
const ctor = function (url: string) {
|
|
88
|
-
const ws = new FakeWebSocket(url);
|
|
89
|
-
instances.push(ws);
|
|
90
|
-
return ws;
|
|
91
|
-
} as unknown as {new (url: string): FakeWebSocket};
|
|
92
|
-
return {
|
|
93
|
-
ctor,
|
|
94
|
-
instances,
|
|
95
|
-
last: () => instances[instances.length - 1],
|
|
96
|
-
};
|
|
97
|
-
}
|
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
|
|
2
|
-
import {get} from 'sveltore';
|
|
3
|
-
import {WhereverClient} from '../src/client.js';
|
|
4
|
-
import type {WhereverState} from '../src/types.js';
|
|
5
|
-
import {makeWSFactory} from './harness.js';
|
|
6
|
-
|
|
7
|
-
// The half-open loss: the socket reports OPEN, ws.send() succeeds (buffers, no
|
|
8
|
-
// throw), but the frame never reaches the server. The optimistic user echo must
|
|
9
|
-
// NOT be treated as delivered: it stays unconfirmed, is recoverable on reload,
|
|
10
|
-
// and flips to a "failed / needs retry" state instead of vanishing.
|
|
11
|
-
|
|
12
|
-
// A localStorage shim so persistence works under the node test env.
|
|
13
|
-
function installLocalStorage() {
|
|
14
|
-
const store = new Map<string, string>();
|
|
15
|
-
(globalThis as any).localStorage = {
|
|
16
|
-
getItem: (k: string) => (store.has(k) ? store.get(k)! : null),
|
|
17
|
-
setItem: (k: string, v: string) => void store.set(k, v),
|
|
18
|
-
removeItem: (k: string) => void store.delete(k),
|
|
19
|
-
clear: () => store.clear(),
|
|
20
|
-
};
|
|
21
|
-
return store;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function createAndLoad(
|
|
25
|
-
ws: ReturnType<typeof makeWSFactory>,
|
|
26
|
-
client: WhereverClient,
|
|
27
|
-
messages: any[] = [],
|
|
28
|
-
) {
|
|
29
|
-
client.joinSession('/tmp/project/session.jsonl');
|
|
30
|
-
ws.last().receive({
|
|
31
|
-
type: 'session_created',
|
|
32
|
-
sessionId: 'sid-1',
|
|
33
|
-
sessionFile: '/tmp/project/session.jsonl',
|
|
34
|
-
cwd: '/tmp/project',
|
|
35
|
-
model: 'fake:model',
|
|
36
|
-
isStreaming: false,
|
|
37
|
-
});
|
|
38
|
-
ws.last().receive({
|
|
39
|
-
type: 'message_history',
|
|
40
|
-
sessionId: 'sid-1',
|
|
41
|
-
messages,
|
|
42
|
-
totalCount: messages.length,
|
|
43
|
-
offset: 0,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
describe('outbound message delivery confirmation', () => {
|
|
48
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
49
|
-
let client: WhereverClient;
|
|
50
|
-
const state = () => get<WhereverState>(client.stateStore);
|
|
51
|
-
|
|
52
|
-
beforeEach(() => {
|
|
53
|
-
vi.useFakeTimers();
|
|
54
|
-
installLocalStorage();
|
|
55
|
-
(globalThis as any).localStorage.clear();
|
|
56
|
-
ws = makeWSFactory();
|
|
57
|
-
client = new WhereverClient({
|
|
58
|
-
host: 'localhost',
|
|
59
|
-
port: 1234,
|
|
60
|
-
secure: false,
|
|
61
|
-
WebSocketCtor: ws.ctor,
|
|
62
|
-
});
|
|
63
|
-
client.connect();
|
|
64
|
-
ws.last().open();
|
|
65
|
-
createAndLoad(ws, client);
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
afterEach(() => {
|
|
69
|
-
client.disconnect(true);
|
|
70
|
-
vi.useRealTimers();
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
it('marks a sent message as unconfirmed (sending), not delivered', () => {
|
|
74
|
-
const ok = client.sendMessage('hello there');
|
|
75
|
-
expect(ok).toBe(true);
|
|
76
|
-
const m = state().messages.find((x) => x.content === 'hello there')!;
|
|
77
|
-
expect(m.delivery).toBe('sending');
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('confirms the message when the server echoes it back', () => {
|
|
81
|
-
client.sendMessage('confirm me');
|
|
82
|
-
ws.last().receive({
|
|
83
|
-
type: 'message_end',
|
|
84
|
-
sessionId: 'sid-1',
|
|
85
|
-
role: 'user',
|
|
86
|
-
content: 'confirm me',
|
|
87
|
-
});
|
|
88
|
-
const m = state().messages.find((x) => x.content === 'confirm me')!;
|
|
89
|
-
expect(m.delivery).toBeUndefined();
|
|
90
|
-
// No duplicate echo added.
|
|
91
|
-
expect(
|
|
92
|
-
state().messages.filter((x) => x.content === 'confirm me').length,
|
|
93
|
-
).toBe(1);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it('confirms on a message_ack, before any user echo (the steer case)', () => {
|
|
97
|
-
// A mid-stream steer is accepted immediately but pi only echoes it back
|
|
98
|
-
// (message_end role:user) at the NEXT model call, which can be far beyond
|
|
99
|
-
// the confirmation window. The server sends message_ack the moment it hands
|
|
100
|
-
// the message to the agent, so delivery is confirmed right away.
|
|
101
|
-
client.sendMessage('steer me now');
|
|
102
|
-
expect(
|
|
103
|
-
state().messages.find((x) => x.content === 'steer me now')!.delivery,
|
|
104
|
-
).toBe('sending');
|
|
105
|
-
|
|
106
|
-
ws.last().receive({
|
|
107
|
-
type: 'message_ack',
|
|
108
|
-
sessionId: 'sid-1',
|
|
109
|
-
content: 'steer me now',
|
|
110
|
-
});
|
|
111
|
-
expect(
|
|
112
|
-
state().messages.find((x) => x.content === 'steer me now')!.delivery,
|
|
113
|
-
).toBeUndefined();
|
|
114
|
-
|
|
115
|
-
// The watchdog is cleared: even past the window it stays confirmed.
|
|
116
|
-
vi.advanceTimersByTime(12_000);
|
|
117
|
-
expect(
|
|
118
|
-
state().messages.find((x) => x.content === 'steer me now')!.delivery,
|
|
119
|
-
).toBeUndefined();
|
|
120
|
-
// No duplicate bubble.
|
|
121
|
-
expect(
|
|
122
|
-
state().messages.filter((x) => x.content === 'steer me now').length,
|
|
123
|
-
).toBe(1);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it('a message_ack for the same content later (the user echo) adds no duplicate', () => {
|
|
127
|
-
client.sendMessage('acked then echoed');
|
|
128
|
-
ws.last().receive({
|
|
129
|
-
type: 'message_ack',
|
|
130
|
-
sessionId: 'sid-1',
|
|
131
|
-
content: 'acked then echoed',
|
|
132
|
-
});
|
|
133
|
-
// The real pi user-echo arrives at the next turn; it must not duplicate.
|
|
134
|
-
ws.last().receive({
|
|
135
|
-
type: 'message_end',
|
|
136
|
-
sessionId: 'sid-1',
|
|
137
|
-
role: 'user',
|
|
138
|
-
content: 'acked then echoed',
|
|
139
|
-
});
|
|
140
|
-
expect(
|
|
141
|
-
state().messages.filter((x) => x.content === 'acked then echoed').length,
|
|
142
|
-
).toBe(1);
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
// A `/skill:<name> <args>` invocation is optimistically echoed RAW, but the
|
|
146
|
-
// server confirms it via a RAW message_ack (mid-stream steer) and then echoes
|
|
147
|
-
// the EXPANDED skill block back as message_end role:user. Both refer to the
|
|
148
|
-
// same invocation: the client must confirm the one bubble and rewrite it to
|
|
149
|
-
// the expanded form, NEVER leave the raw one orphaned as failed nor append the
|
|
150
|
-
// expanded one as a duplicate.
|
|
151
|
-
const EXPANDED_SETUP =
|
|
152
|
-
'<skill name="setup" location="/a/b/SKILL.md">\nReferences are relative to /a/b.\n\nBody here.\n</skill>';
|
|
153
|
-
|
|
154
|
-
it('skill: message_end (expanded) confirms the raw optimistic bubble, no duplicate', () => {
|
|
155
|
-
client.sendMessage('/skill:setup');
|
|
156
|
-
expect(
|
|
157
|
-
state().messages.find((x) => x.content === '/skill:setup')!.delivery,
|
|
158
|
-
).toBe('sending');
|
|
159
|
-
|
|
160
|
-
// Server echoes the EXPANDED block (no prior message_ack, e.g. not streaming).
|
|
161
|
-
ws.last().receive({
|
|
162
|
-
type: 'message_end',
|
|
163
|
-
sessionId: 'sid-1',
|
|
164
|
-
role: 'user',
|
|
165
|
-
content: EXPANDED_SETUP,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const users = state().messages.filter((x) => x.role === 'user');
|
|
169
|
-
expect(users.length).toBe(1);
|
|
170
|
-
expect(users[0].delivery).toBeUndefined();
|
|
171
|
-
// Rewritten to the expanded form so the skill chip renders.
|
|
172
|
-
expect(users[0].content).toBe(EXPANDED_SETUP);
|
|
173
|
-
|
|
174
|
-
// Even past the window it stays confirmed (watchdog cleared).
|
|
175
|
-
vi.advanceTimersByTime(12_000);
|
|
176
|
-
expect(
|
|
177
|
-
state().messages.find((x) => x.role === 'user')!.delivery,
|
|
178
|
-
).toBeUndefined();
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
it('skill: raw message_ack then expanded message_end yields one confirmed chip', () => {
|
|
182
|
-
client.sendMessage('/skill:setup');
|
|
183
|
-
// Mid-stream steer path: server acks the RAW content immediately.
|
|
184
|
-
ws.last().receive({
|
|
185
|
-
type: 'message_ack',
|
|
186
|
-
sessionId: 'sid-1',
|
|
187
|
-
content: '/skill:setup',
|
|
188
|
-
});
|
|
189
|
-
expect(
|
|
190
|
-
state().messages.find((x) => x.role === 'user')!.delivery,
|
|
191
|
-
).toBeUndefined();
|
|
192
|
-
|
|
193
|
-
// Later, pi echoes the EXPANDED block at the next model call.
|
|
194
|
-
ws.last().receive({
|
|
195
|
-
type: 'message_end',
|
|
196
|
-
sessionId: 'sid-1',
|
|
197
|
-
role: 'user',
|
|
198
|
-
content: EXPANDED_SETUP,
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
const users = state().messages.filter((x) => x.role === 'user');
|
|
202
|
-
expect(users.length).toBe(1);
|
|
203
|
-
expect(users[0].delivery).toBeUndefined();
|
|
204
|
-
expect(users[0].content).toBe(EXPANDED_SETUP);
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('skill: preserves the trailing args when matching raw to expanded', () => {
|
|
208
|
-
client.sendMessage('/skill:setup do the thing');
|
|
209
|
-
const expandedWithArgs = `${EXPANDED_SETUP}\n\ndo the thing`;
|
|
210
|
-
ws.last().receive({
|
|
211
|
-
type: 'message_end',
|
|
212
|
-
sessionId: 'sid-1',
|
|
213
|
-
role: 'user',
|
|
214
|
-
content: expandedWithArgs,
|
|
215
|
-
});
|
|
216
|
-
const users = state().messages.filter((x) => x.role === 'user');
|
|
217
|
-
expect(users.length).toBe(1);
|
|
218
|
-
expect(users[0].delivery).toBeUndefined();
|
|
219
|
-
expect(users[0].content).toBe(expandedWithArgs);
|
|
220
|
-
});
|
|
221
|
-
|
|
222
|
-
it('flips to failed when no echo arrives within the window (half-open loss)', () => {
|
|
223
|
-
// Half-open: send() reports success but the server never processes it, so
|
|
224
|
-
// no echo is ever received.
|
|
225
|
-
client.sendMessage('lost message');
|
|
226
|
-
expect(
|
|
227
|
-
state().messages.find((x) => x.content === 'lost message')!.delivery,
|
|
228
|
-
).toBe('sending');
|
|
229
|
-
|
|
230
|
-
vi.advanceTimersByTime(12_000);
|
|
231
|
-
const m = state().messages.find((x) => x.content === 'lost message')!;
|
|
232
|
-
expect(m.delivery).toBe('failed');
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
it('persists an unconfirmed message so a reload can recover it', () => {
|
|
236
|
-
client.sendMessage('recover me on reload');
|
|
237
|
-
// Simulate a reload: a fresh client instance against the same localStorage.
|
|
238
|
-
const ws2 = makeWSFactory();
|
|
239
|
-
const client2 = new WhereverClient({
|
|
240
|
-
host: 'localhost',
|
|
241
|
-
port: 1234,
|
|
242
|
-
secure: false,
|
|
243
|
-
WebSocketCtor: ws2.ctor,
|
|
244
|
-
});
|
|
245
|
-
client2.connect();
|
|
246
|
-
ws2.last().open();
|
|
247
|
-
// History comes back WITHOUT the message (the server never got it).
|
|
248
|
-
createAndLoad(ws2, client2, []);
|
|
249
|
-
|
|
250
|
-
const recovered = get<WhereverState>(client2.stateStore).messages.find(
|
|
251
|
-
(x) => x.content === 'recover me on reload',
|
|
252
|
-
);
|
|
253
|
-
expect(recovered).toBeTruthy();
|
|
254
|
-
expect(recovered!.delivery).toBe('failed');
|
|
255
|
-
client2.disconnect(true);
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
it('does NOT re-surface a message that history confirms was delivered', () => {
|
|
259
|
-
client.sendMessage('this one landed');
|
|
260
|
-
// Reload; history DOES contain it (it was delivered before the drop).
|
|
261
|
-
const ws2 = makeWSFactory();
|
|
262
|
-
const client2 = new WhereverClient({
|
|
263
|
-
host: 'localhost',
|
|
264
|
-
port: 1234,
|
|
265
|
-
secure: false,
|
|
266
|
-
WebSocketCtor: ws2.ctor,
|
|
267
|
-
});
|
|
268
|
-
client2.connect();
|
|
269
|
-
ws2.last().open();
|
|
270
|
-
createAndLoad(ws2, client2, [
|
|
271
|
-
{role: 'user', content: 'this one landed', timestamp: 1_000},
|
|
272
|
-
]);
|
|
273
|
-
|
|
274
|
-
const msgs = get<WhereverState>(client2.stateStore).messages.filter(
|
|
275
|
-
(x) => x.content === 'this one landed',
|
|
276
|
-
);
|
|
277
|
-
// Exactly one, and delivered (no failed duplicate).
|
|
278
|
-
expect(msgs.length).toBe(1);
|
|
279
|
-
expect(msgs[0].delivery).toBeUndefined();
|
|
280
|
-
client2.disconnect(true);
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
it('resendMessage re-sends a failed message and re-arms confirmation', () => {
|
|
284
|
-
client.sendMessage('retry me');
|
|
285
|
-
vi.advanceTimersByTime(12_000);
|
|
286
|
-
const failed = state().messages.find((x) => x.content === 'retry me')!;
|
|
287
|
-
expect(failed.delivery).toBe('failed');
|
|
288
|
-
|
|
289
|
-
const sock = ws.last();
|
|
290
|
-
const before = sock.sent.filter((f) => f.type === 'message').length;
|
|
291
|
-
const ok = client.resendMessage(failed.id);
|
|
292
|
-
expect(ok).toBe(true);
|
|
293
|
-
expect(sock.sent.filter((f) => f.type === 'message').length).toBe(before + 1);
|
|
294
|
-
expect(
|
|
295
|
-
state().messages.find((x) => x.id === failed.id)!.delivery,
|
|
296
|
-
).toBe('sending');
|
|
297
|
-
|
|
298
|
-
// The echo now confirms it.
|
|
299
|
-
sock.receive({
|
|
300
|
-
type: 'message_end',
|
|
301
|
-
sessionId: 'sid-1',
|
|
302
|
-
role: 'user',
|
|
303
|
-
content: 'retry me',
|
|
304
|
-
});
|
|
305
|
-
expect(
|
|
306
|
-
state().messages.find((x) => x.id === failed.id)!.delivery,
|
|
307
|
-
).toBeUndefined();
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
it('discardMessage removes a failed message and clears it from persistence', () => {
|
|
311
|
-
client.sendMessage('drop me');
|
|
312
|
-
vi.advanceTimersByTime(12_000);
|
|
313
|
-
const failed = state().messages.find((x) => x.content === 'drop me')!;
|
|
314
|
-
client.discardMessage(failed.id);
|
|
315
|
-
expect(state().messages.find((x) => x.content === 'drop me')).toBeUndefined();
|
|
316
|
-
expect(
|
|
317
|
-
(globalThis as any).localStorage.getItem('wherever-pending:sid-1'),
|
|
318
|
-
).toBeNull();
|
|
319
|
-
});
|
|
320
|
-
});
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
|
|
2
|
-
import {get} from 'sveltore';
|
|
3
|
-
import {WhereverClient} from '../src/client.js';
|
|
4
|
-
import {makeWSFactory} from './harness.js';
|
|
5
|
-
|
|
6
|
-
// A message submitted WHILE the agent is streaming must be sent to the relay
|
|
7
|
-
// IMMEDIATELY (the server turns a mid-stream `message` into a steer, injected at
|
|
8
|
-
// the next tool/step boundary). This is the pi-CLI default (Enter -> steer). It
|
|
9
|
-
// must NOT be held locally waiting for the turn to finish -- the old behaviour
|
|
10
|
-
// that also mis-fired mid-turn on the flaky isStreaming debounce (docs/adr/0003).
|
|
11
|
-
|
|
12
|
-
function createAndLoad(
|
|
13
|
-
ws: ReturnType<typeof makeWSFactory>,
|
|
14
|
-
client: WhereverClient,
|
|
15
|
-
{streaming = false}: {streaming?: boolean} = {},
|
|
16
|
-
) {
|
|
17
|
-
client.joinSession('/tmp/project/session.jsonl');
|
|
18
|
-
ws.last().receive({
|
|
19
|
-
type: 'session_created',
|
|
20
|
-
sessionId: 'sid-1',
|
|
21
|
-
sessionFile: '/tmp/project/session.jsonl',
|
|
22
|
-
cwd: '/tmp/project',
|
|
23
|
-
model: 'fake:model',
|
|
24
|
-
isStreaming: streaming,
|
|
25
|
-
});
|
|
26
|
-
ws.last().receive({
|
|
27
|
-
type: 'message_history',
|
|
28
|
-
sessionId: 'sid-1',
|
|
29
|
-
messages: [{role: 'user', content: 'go build it', timestamp: 1}],
|
|
30
|
-
totalCount: 1,
|
|
31
|
-
offset: 0,
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
describe('mid-stream submit steers immediately', () => {
|
|
36
|
-
let ws: ReturnType<typeof makeWSFactory>;
|
|
37
|
-
let client: WhereverClient;
|
|
38
|
-
|
|
39
|
-
beforeEach(() => {
|
|
40
|
-
vi.useFakeTimers();
|
|
41
|
-
ws = makeWSFactory();
|
|
42
|
-
client = new WhereverClient({
|
|
43
|
-
host: 'localhost',
|
|
44
|
-
port: 1234,
|
|
45
|
-
secure: false,
|
|
46
|
-
WebSocketCtor: ws.ctor,
|
|
47
|
-
});
|
|
48
|
-
client.connect();
|
|
49
|
-
ws.last().open();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
afterEach(() => {
|
|
53
|
-
client.disconnect(true);
|
|
54
|
-
vi.useRealTimers();
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
it('sends the message frame NOW while the agent is mid-turn', () => {
|
|
58
|
-
createAndLoad(ws, client, {streaming: true});
|
|
59
|
-
const sock = ws.last();
|
|
60
|
-
expect(get(client.stateStore).isStreaming).toBe(true);
|
|
61
|
-
|
|
62
|
-
// User submits mid-stream. sendMessage must hand a `message` frame to the
|
|
63
|
-
// OPEN socket right away, not park it.
|
|
64
|
-
const ok = client.sendMessage('actually, do X instead');
|
|
65
|
-
expect(ok).toBe(true);
|
|
66
|
-
|
|
67
|
-
const frame = sock.lastSentOfType('message');
|
|
68
|
-
expect(frame).toBeTruthy();
|
|
69
|
-
expect(frame.message).toBe('actually, do X instead');
|
|
70
|
-
|
|
71
|
-
// The optimistic user echo is committed immediately (not withheld pending
|
|
72
|
-
// a later drain), and the turn is still in flight.
|
|
73
|
-
const s = get(client.stateStore);
|
|
74
|
-
expect(s.messages.some((m) => m.role === 'user' && m.content === 'actually, do X instead')).toBe(true);
|
|
75
|
-
expect(s.isStreaming).toBe(true);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('returns false and keeps no phantom echo when disconnected mid-stream', () => {
|
|
79
|
-
createAndLoad(ws, client, {streaming: true});
|
|
80
|
-
const sock = ws.last();
|
|
81
|
-
// Socket drops just as the user submits.
|
|
82
|
-
sock.close();
|
|
83
|
-
|
|
84
|
-
const before = get(client.stateStore).messages.length;
|
|
85
|
-
const ok = client.sendMessage('do X instead');
|
|
86
|
-
expect(ok).toBe(false);
|
|
87
|
-
// No optimistic echo for a send that never left; caller keeps the text.
|
|
88
|
-
expect(get(client.stateStore).messages.length).toBe(before);
|
|
89
|
-
expect(get(client.stateStore).sessionError).toBeTruthy();
|
|
90
|
-
});
|
|
91
|
-
});
|