@wherever-dev/client 0.4.0 → 0.5.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 +14 -0
- package/dist/client.d.ts +3 -1
- package/dist/client.js +259 -41
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +19 -5
- package/dist/types.js +49 -1
- package/dist/types.js.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +274 -42
- package/src/types.ts +100 -5
- package/test/message-delivery-confirmation.test.ts +126 -0
- package/test/optimistic-force-command.test.ts +127 -0
- package/test/steer-queue-cancel.test.ts +130 -0
|
@@ -93,6 +93,132 @@ describe('outbound message delivery confirmation', () => {
|
|
|
93
93
|
).toBe(1);
|
|
94
94
|
});
|
|
95
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
|
+
|
|
96
222
|
it('flips to failed when no echo arrives within the window (half-open loss)', () => {
|
|
97
223
|
// Half-open: send() reports success but the server never processes it, so
|
|
98
224
|
// no echo is ever received.
|
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
// A web `!command` / `!!command` (force command) is intercepted by the server
|
|
8
|
+
// and run as a bash tool call; it is NOT echoed back as a user message. Without
|
|
9
|
+
// any local feedback the tool bubble only appeared after a full server
|
|
10
|
+
// round-trip (client -> server -> tool_start -> client), so the command did not
|
|
11
|
+
// show up instantly. The client now renders an OPTIMISTIC bash tool bubble at
|
|
12
|
+
// send time and RECONCILES the server's real tool_start onto it (no duplicate).
|
|
13
|
+
|
|
14
|
+
function setup(ws: ReturnType<typeof makeWSFactory>, isStreaming = false) {
|
|
15
|
+
const client = new WhereverClient({
|
|
16
|
+
host: 'localhost',
|
|
17
|
+
port: 1234,
|
|
18
|
+
secure: false,
|
|
19
|
+
WebSocketCtor: ws.ctor,
|
|
20
|
+
});
|
|
21
|
+
client.connect();
|
|
22
|
+
ws.last().open();
|
|
23
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
24
|
+
ws.last().receive({
|
|
25
|
+
type: 'session_created',
|
|
26
|
+
sessionId: 'sid-1',
|
|
27
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
28
|
+
cwd: '/tmp/project',
|
|
29
|
+
model: 'fake:model',
|
|
30
|
+
isStreaming,
|
|
31
|
+
});
|
|
32
|
+
return client;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
describe('optimistic force-command tool bubble', () => {
|
|
36
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
37
|
+
let client: WhereverClient;
|
|
38
|
+
const state = () => get<WhereverState>(client.stateStore);
|
|
39
|
+
const tools = () => state().messages.filter((m) => m.role === 'tool');
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
vi.useFakeTimers();
|
|
43
|
+
ws = makeWSFactory();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
afterEach(() => {
|
|
47
|
+
client.disconnect(true);
|
|
48
|
+
vi.useRealTimers();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('renders an optimistic bash bubble instantly on a !command (no user echo)', () => {
|
|
52
|
+
client = setup(ws);
|
|
53
|
+
client.sendMessage('!ls -la');
|
|
54
|
+
|
|
55
|
+
const t = tools();
|
|
56
|
+
expect(t.length).toBe(1);
|
|
57
|
+
expect(t[0].toolName).toBe('bash');
|
|
58
|
+
expect(t[0].forceCommand).toBe(true);
|
|
59
|
+
expect(t[0].optimistic).toBe(true);
|
|
60
|
+
expect(t[0].isStreaming).toBe(true);
|
|
61
|
+
expect(t[0].content).toBe('$ bash command="ls -la"');
|
|
62
|
+
// It is NOT a user message (no user echo, no delivery tracking).
|
|
63
|
+
expect(state().messages.some((m) => m.role === 'user')).toBe(false);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('strips the !! prefix for an excluded-from-context command', () => {
|
|
67
|
+
client = setup(ws);
|
|
68
|
+
client.sendMessage('!!git status');
|
|
69
|
+
const t = tools();
|
|
70
|
+
expect(t.length).toBe(1);
|
|
71
|
+
expect(t[0].content).toBe('$ bash command="git status"');
|
|
72
|
+
expect(t[0].optimistic).toBe(true);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('reconciles the server tool_start onto the optimistic bubble (no duplicate)', () => {
|
|
76
|
+
client = setup(ws);
|
|
77
|
+
client.sendMessage('!ls -la');
|
|
78
|
+
expect(tools().length).toBe(1);
|
|
79
|
+
|
|
80
|
+
// The server runs it and emits the real tool_start (forceCommand).
|
|
81
|
+
ws.last().receive({
|
|
82
|
+
type: 'tool_start',
|
|
83
|
+
sessionId: 'sid-1',
|
|
84
|
+
toolName: 'bash',
|
|
85
|
+
args: {command: 'ls -la'},
|
|
86
|
+
forceCommand: true,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const t = tools();
|
|
90
|
+
expect(t.length).toBe(1); // reconciled, not duplicated
|
|
91
|
+
expect(t[0].optimistic).toBeFalsy(); // flag cleared
|
|
92
|
+
expect(t[0].forceCommand).toBe(true);
|
|
93
|
+
expect(t[0].isStreaming).toBe(true);
|
|
94
|
+
|
|
95
|
+
// Output then streams and settles onto the SAME bubble.
|
|
96
|
+
ws.last().receive({type: 'tool_update', sessionId: 'sid-1', toolName: 'bash', delta: 'total 0\n'});
|
|
97
|
+
ws.last().receive({
|
|
98
|
+
type: 'tool_end',
|
|
99
|
+
sessionId: 'sid-1',
|
|
100
|
+
toolName: 'bash',
|
|
101
|
+
isError: false,
|
|
102
|
+
result: 'total 0\n',
|
|
103
|
+
forceCommand: true,
|
|
104
|
+
});
|
|
105
|
+
const done = tools();
|
|
106
|
+
expect(done.length).toBe(1);
|
|
107
|
+
expect(done[0].isStreaming).toBe(false);
|
|
108
|
+
expect(done[0].toolOutput).toContain('total 0');
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it('does NOT render an optimistic bubble for !sudo (deferred behind a password prompt)', () => {
|
|
112
|
+
client = setup(ws);
|
|
113
|
+
client.sendMessage('!sudo apt update');
|
|
114
|
+
expect(tools().length).toBe(0);
|
|
115
|
+
expect(state().sessionError).toBeNull();
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('an agent-issued bash tool_start still appends its own bubble', () => {
|
|
119
|
+
client = setup(ws);
|
|
120
|
+
// No optimistic bubble exists; a plain agent tool_start (no forceCommand).
|
|
121
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'echo hi'}});
|
|
122
|
+
const t = tools();
|
|
123
|
+
expect(t.length).toBe(1);
|
|
124
|
+
expect(t[0].forceCommand).toBeFalsy();
|
|
125
|
+
expect(t[0].optimistic).toBeFalsy();
|
|
126
|
+
});
|
|
127
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
// A mid-stream steer is queued on the server (pi injects it at the next step
|
|
8
|
+
// boundary). The server relays the current queue via `queue_update`, and the
|
|
9
|
+
// client tracks it as `pendingSteering` so the UI can offer a per-message
|
|
10
|
+
// "Cancel". Cancelling sends `cancel_steer` (which clears the server queue via
|
|
11
|
+
// pi's clearQueue) WITHOUT aborting the running turn.
|
|
12
|
+
|
|
13
|
+
function createAndLoad(
|
|
14
|
+
ws: ReturnType<typeof makeWSFactory>,
|
|
15
|
+
client: WhereverClient,
|
|
16
|
+
{streaming = true}: {streaming?: boolean} = {},
|
|
17
|
+
) {
|
|
18
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
19
|
+
ws.last().receive({
|
|
20
|
+
type: 'session_created',
|
|
21
|
+
sessionId: 'sid-1',
|
|
22
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
23
|
+
cwd: '/tmp/project',
|
|
24
|
+
model: 'fake:model',
|
|
25
|
+
isStreaming: streaming,
|
|
26
|
+
});
|
|
27
|
+
ws.last().receive({
|
|
28
|
+
type: 'message_history',
|
|
29
|
+
sessionId: 'sid-1',
|
|
30
|
+
messages: [{role: 'user', content: 'go build it', timestamp: 1}],
|
|
31
|
+
totalCount: 1,
|
|
32
|
+
offset: 0,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('queued steer tracking + cancel', () => {
|
|
37
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
38
|
+
let client: WhereverClient;
|
|
39
|
+
const state = () => get<WhereverState>(client.stateStore);
|
|
40
|
+
|
|
41
|
+
beforeEach(() => {
|
|
42
|
+
vi.useFakeTimers();
|
|
43
|
+
ws = makeWSFactory();
|
|
44
|
+
client = new WhereverClient({
|
|
45
|
+
host: 'localhost',
|
|
46
|
+
port: 1234,
|
|
47
|
+
secure: false,
|
|
48
|
+
WebSocketCtor: ws.ctor,
|
|
49
|
+
});
|
|
50
|
+
client.connect();
|
|
51
|
+
ws.last().open();
|
|
52
|
+
createAndLoad(ws, client);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
client.disconnect(true);
|
|
57
|
+
vi.useRealTimers();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('tracks the queued steer from queue_update', () => {
|
|
61
|
+
expect(state().pendingSteering).toEqual([]);
|
|
62
|
+
ws.last().receive({
|
|
63
|
+
type: 'queue_update',
|
|
64
|
+
sessionId: 'sid-1',
|
|
65
|
+
steering: ['do X instead'],
|
|
66
|
+
});
|
|
67
|
+
expect(state().pendingSteering).toEqual(['do X instead']);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('replaces the pending set outright on each queue_update', () => {
|
|
71
|
+
ws.last().receive({
|
|
72
|
+
type: 'queue_update',
|
|
73
|
+
sessionId: 'sid-1',
|
|
74
|
+
steering: ['first steer', 'second steer'],
|
|
75
|
+
});
|
|
76
|
+
expect(state().pendingSteering).toEqual(['first steer', 'second steer']);
|
|
77
|
+
// A later update with the first delivered leaves only the second.
|
|
78
|
+
ws.last().receive({
|
|
79
|
+
type: 'queue_update',
|
|
80
|
+
sessionId: 'sid-1',
|
|
81
|
+
steering: ['second steer'],
|
|
82
|
+
});
|
|
83
|
+
expect(state().pendingSteering).toEqual(['second steer']);
|
|
84
|
+
// Emptied when the queue drains.
|
|
85
|
+
ws.last().receive({type: 'queue_update', sessionId: 'sid-1', steering: []});
|
|
86
|
+
expect(state().pendingSteering).toEqual([]);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('cancelSteer sends cancel_steer and optimistically clears the pending set', () => {
|
|
90
|
+
ws.last().receive({
|
|
91
|
+
type: 'queue_update',
|
|
92
|
+
sessionId: 'sid-1',
|
|
93
|
+
steering: ['retract me'],
|
|
94
|
+
});
|
|
95
|
+
expect(state().pendingSteering).toEqual(['retract me']);
|
|
96
|
+
|
|
97
|
+
const sock = ws.last();
|
|
98
|
+
client.cancelSteer();
|
|
99
|
+
|
|
100
|
+
const frame = sock.lastSentOfType('cancel_steer');
|
|
101
|
+
expect(frame).toBeTruthy();
|
|
102
|
+
expect(frame.sessionId).toBe('sid-1');
|
|
103
|
+
// Optimistically cleared so the Cancel affordance disappears immediately.
|
|
104
|
+
expect(state().pendingSteering).toEqual([]);
|
|
105
|
+
// It does NOT abort the turn.
|
|
106
|
+
expect(sock.lastSentOfType('abort')).toBeUndefined();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('clears the pending set when the turn ends', () => {
|
|
110
|
+
ws.last().receive({
|
|
111
|
+
type: 'queue_update',
|
|
112
|
+
sessionId: 'sid-1',
|
|
113
|
+
steering: ['queued steer'],
|
|
114
|
+
});
|
|
115
|
+
expect(state().pendingSteering).toEqual(['queued steer']);
|
|
116
|
+
|
|
117
|
+
ws.last().receive({type: 'agent_end', sessionId: 'sid-1'});
|
|
118
|
+
// agent_end resolves on a short debounce; advance it.
|
|
119
|
+
vi.advanceTimersByTime(500);
|
|
120
|
+
expect(state().pendingSteering).toEqual([]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('cancelSteer with no session is a no-op (no frame sent)', () => {
|
|
124
|
+
client.leaveSession();
|
|
125
|
+
const sock = ws.last();
|
|
126
|
+
const before = sock.sent.length;
|
|
127
|
+
client.cancelSteer();
|
|
128
|
+
expect(sock.sent.length).toBe(before);
|
|
129
|
+
});
|
|
130
|
+
});
|