@wherever-dev/client 0.5.0 → 0.5.2
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 +12 -0
- package/LICENSE +661 -21
- package/dist/client.d.ts +2 -0
- package/dist/client.js +126 -2
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +13 -0
- package/dist/types.js +49 -1
- package/dist/types.js.map +1 -1
- package/package.json +2 -1
- package/src/client.ts +136 -3
- package/src/types.ts +81 -0
- package/test/message-delivery-confirmation.test.ts +126 -0
- package/test/steer-queue-cancel.test.ts +130 -0
package/src/client.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { writable, get, type Writable } from "sveltore";
|
|
2
|
-
import { type ChatMessage, type WhereverState } from "./types.js";
|
|
2
|
+
import { type ChatMessage, type WhereverState, skillInvocationIdentity } from "./types.js";
|
|
3
3
|
|
|
4
4
|
export interface WhereverClientConfig {
|
|
5
5
|
host: string;
|
|
@@ -39,6 +39,8 @@ const defaultState: WhereverState = {
|
|
|
39
39
|
historyOffset: 0,
|
|
40
40
|
loadingMoreHistory: false,
|
|
41
41
|
contextUsage: null,
|
|
42
|
+
pendingSteering: [],
|
|
43
|
+
skills: [],
|
|
42
44
|
};
|
|
43
45
|
|
|
44
46
|
export class WhereverClient {
|
|
@@ -753,6 +755,31 @@ export class WhereverClient {
|
|
|
753
755
|
});
|
|
754
756
|
break;
|
|
755
757
|
|
|
758
|
+
case 'message_ack':
|
|
759
|
+
// The server accepted this outbound user message (a normal turn, or a
|
|
760
|
+
// mid-stream steer queued for the next step) and handed it to the agent.
|
|
761
|
+
// This is the delivery CONFIRMATION: it arrives immediately, so an
|
|
762
|
+
// accepted steer is not left waiting on the message_end (role:user)
|
|
763
|
+
// echo, which for a steer only comes at the next model call and can miss
|
|
764
|
+
// the confirmation window (flipping the message to a false "Retry").
|
|
765
|
+
// Content-matched to the oldest still-unconfirmed pending message.
|
|
766
|
+
if (typeof msg.content === 'string') {
|
|
767
|
+
this.confirmDeliveredByContent(msg.content);
|
|
768
|
+
}
|
|
769
|
+
break;
|
|
770
|
+
|
|
771
|
+
case 'queue_update':
|
|
772
|
+
// The server relayed pi's current steer queue. Replace our pending-steer
|
|
773
|
+
// set outright (it is the full queue): a message whose content is still
|
|
774
|
+
// in this set is a queued steer that has NOT yet been injected, so the
|
|
775
|
+
// UI can offer to cancel it. When a queued steer is delivered or
|
|
776
|
+
// cancelled, a fresh queue_update arrives with it removed.
|
|
777
|
+
this.stateStore.update((s: WhereverState) => ({
|
|
778
|
+
...s,
|
|
779
|
+
pendingSteering: Array.isArray(msg.steering) ? [...msg.steering] : [],
|
|
780
|
+
}));
|
|
781
|
+
break;
|
|
782
|
+
|
|
756
783
|
case 'message_end':
|
|
757
784
|
this.stateStore.update((s: WhereverState) => {
|
|
758
785
|
let newMessages = s.messages;
|
|
@@ -795,6 +822,32 @@ export class WhereverClient {
|
|
|
795
822
|
return get(this.stateStore);
|
|
796
823
|
}
|
|
797
824
|
}
|
|
825
|
+
// Skill invocations arrive TWICE from the server: first the RAW
|
|
826
|
+
// `/skill:<name> <args>` (as the immediate message_ack, which already
|
|
827
|
+
// confirmed the optimistic bubble by exact content), then the
|
|
828
|
+
// EXPANDED skill block (this message_end echo). They are the same
|
|
829
|
+
// invocation, so instead of appending the expanded form as a
|
|
830
|
+
// duplicate, REWRITE the already-present raw bubble to the expanded
|
|
831
|
+
// content so it renders as the skill chip.
|
|
832
|
+
const echoIdentity =
|
|
833
|
+
msg.role === 'user' ? skillInvocationIdentity(msg.content) : null;
|
|
834
|
+
if (echoIdentity) {
|
|
835
|
+
const existing = [...newMessages]
|
|
836
|
+
.reverse()
|
|
837
|
+
.find(
|
|
838
|
+
(m) =>
|
|
839
|
+
m.role === 'user' &&
|
|
840
|
+
skillInvocationIdentity(m.content) === echoIdentity,
|
|
841
|
+
);
|
|
842
|
+
if (existing) {
|
|
843
|
+
if (existing.content !== msg.content) {
|
|
844
|
+
newMessages = newMessages.map((m) =>
|
|
845
|
+
m.id === existing.id ? {...m, content: msg.content} : m,
|
|
846
|
+
);
|
|
847
|
+
}
|
|
848
|
+
return {...s, messages: newMessages};
|
|
849
|
+
}
|
|
850
|
+
}
|
|
798
851
|
const lastUserMessage = [...newMessages]
|
|
799
852
|
.reverse()
|
|
800
853
|
.find((m) => m.role === 'user');
|
|
@@ -831,6 +884,11 @@ export class WhereverClient {
|
|
|
831
884
|
this.stateStore.update((s: WhereverState) => ({
|
|
832
885
|
...s,
|
|
833
886
|
isStreaming: false,
|
|
887
|
+
// The run ended: any steer that was queued has been delivered (or
|
|
888
|
+
// dropped), so nothing is cancellable anymore. The server also emits
|
|
889
|
+
// a final queue_update, but clear here defensively in case it is
|
|
890
|
+
// missed, so a stale "Cancel" affordance cannot linger.
|
|
891
|
+
pendingSteering: [],
|
|
834
892
|
messages: s.messages.map((m: ChatMessage) =>
|
|
835
893
|
m.isStreaming
|
|
836
894
|
? {
|
|
@@ -1104,6 +1162,9 @@ export class WhereverClient {
|
|
|
1104
1162
|
// Initial context-usage snapshot for the new session (null if unknown;
|
|
1105
1163
|
// live updates arrive via 'context_usage').
|
|
1106
1164
|
contextUsage: msg.contextUsage ?? null,
|
|
1165
|
+
// Reset skill-command autocomplete for the newly attached session; the
|
|
1166
|
+
// authoritative list is (re-)requested on session_ready.
|
|
1167
|
+
skills: [],
|
|
1107
1168
|
}));
|
|
1108
1169
|
break;
|
|
1109
1170
|
|
|
@@ -1123,6 +1184,22 @@ export class WhereverClient {
|
|
|
1123
1184
|
contextUsage: msg.contextUsage ?? s.contextUsage,
|
|
1124
1185
|
};
|
|
1125
1186
|
});
|
|
1187
|
+
// The live agent (with its resource loader) now exists, so its skill
|
|
1188
|
+
// commands are resolvable. Ask for them to populate `/skill:` composer
|
|
1189
|
+
// autocomplete. Safe to re-request; the server replies with the full list.
|
|
1190
|
+
{
|
|
1191
|
+
const sid = get(this.stateStore).sessionId;
|
|
1192
|
+
if (sid) this.send({ type: 'skills_request', sessionId: sid });
|
|
1193
|
+
}
|
|
1194
|
+
break;
|
|
1195
|
+
|
|
1196
|
+
case 'skills_list':
|
|
1197
|
+
// The set of `/skill:<name>` commands for the active session. Replace
|
|
1198
|
+
// outright; ignore a list for a session we already switched away from.
|
|
1199
|
+
this.stateStore.update((s: WhereverState) => {
|
|
1200
|
+
if (s.sessionId && msg.sessionId && s.sessionId !== msg.sessionId) return s;
|
|
1201
|
+
return { ...s, skills: Array.isArray(msg.skills) ? msg.skills : [] };
|
|
1202
|
+
});
|
|
1126
1203
|
break;
|
|
1127
1204
|
|
|
1128
1205
|
case 'context_usage':
|
|
@@ -1491,6 +1568,9 @@ export class WhereverClient {
|
|
|
1491
1568
|
isStreaming: false,
|
|
1492
1569
|
toolName: m.toolName,
|
|
1493
1570
|
sessionId,
|
|
1571
|
+
// Carry the source entry id (set on user messages by the server) so the
|
|
1572
|
+
// UI can offer "Fork from here" on each user message.
|
|
1573
|
+
...(typeof m.entryId === 'string' && m.entryId ? {entryId: m.entryId} : {}),
|
|
1494
1574
|
});
|
|
1495
1575
|
}
|
|
1496
1576
|
}
|
|
@@ -1802,20 +1882,49 @@ export class WhereverClient {
|
|
|
1802
1882
|
// Mark the oldest still-unconfirmed user message with this content as
|
|
1803
1883
|
// delivered (the server echoed it back). Content-matched because the server
|
|
1804
1884
|
// echo carries no client message id. Returns true if one was confirmed.
|
|
1885
|
+
//
|
|
1886
|
+
// Skill invocations need a looser match: the client optimistically echoes the
|
|
1887
|
+
// RAW `/skill:<name> <args>`, but the server echoes the EXPANDED skill block
|
|
1888
|
+
// (pi inlines the skill body before storing/echoing). Exact-content matching
|
|
1889
|
+
// would miss, leaving the raw bubble orphaned as "failed" AND appending the
|
|
1890
|
+
// expanded echo as a duplicate. So fall back to matching a pending message
|
|
1891
|
+
// whose skill-invocation IDENTITY (name + args) equals the echo's, and REWRITE
|
|
1892
|
+
// its content to the confirmed (expanded) form so it renders as the skill chip
|
|
1893
|
+
// and the server's own echo is then deduped against it.
|
|
1805
1894
|
private confirmDeliveredByContent(content: string): boolean {
|
|
1806
1895
|
let confirmedId: string | null = null;
|
|
1807
1896
|
let sessionId: string | null = null;
|
|
1897
|
+
const echoIdentity = skillInvocationIdentity(content);
|
|
1808
1898
|
this.stateStore.update((st: WhereverState) => {
|
|
1809
|
-
|
|
1899
|
+
let target = st.messages.find(
|
|
1810
1900
|
(m) => m.role === 'user' && m.delivery !== undefined && m.content === content,
|
|
1811
1901
|
);
|
|
1902
|
+
// Skill-identity fallback: match the RAW optimistic echo to this EXPANDED
|
|
1903
|
+
// server echo when they are the same invocation.
|
|
1904
|
+
if (!target && echoIdentity) {
|
|
1905
|
+
target = st.messages.find(
|
|
1906
|
+
(m) =>
|
|
1907
|
+
m.role === 'user' &&
|
|
1908
|
+
m.delivery !== undefined &&
|
|
1909
|
+
skillInvocationIdentity(m.content) === echoIdentity,
|
|
1910
|
+
);
|
|
1911
|
+
}
|
|
1812
1912
|
if (!target) return st;
|
|
1813
1913
|
confirmedId = target.id;
|
|
1814
1914
|
sessionId = st.sessionId;
|
|
1815
1915
|
return {
|
|
1816
1916
|
...st,
|
|
1817
1917
|
messages: st.messages.map((m) =>
|
|
1818
|
-
m.id === target
|
|
1918
|
+
m.id === target!.id
|
|
1919
|
+
? {
|
|
1920
|
+
...m,
|
|
1921
|
+
delivery: undefined,
|
|
1922
|
+
// Adopt the expanded content so the skill chip renders (and the
|
|
1923
|
+
// server's message_end echo dedupes against it instead of
|
|
1924
|
+
// appending a second bubble).
|
|
1925
|
+
...(echoIdentity ? {content} : {}),
|
|
1926
|
+
}
|
|
1927
|
+
: m,
|
|
1819
1928
|
),
|
|
1820
1929
|
};
|
|
1821
1930
|
});
|
|
@@ -1873,6 +1982,17 @@ export class WhereverClient {
|
|
|
1873
1982
|
this.send({type: 'abort', sessionId: s.sessionId});
|
|
1874
1983
|
}
|
|
1875
1984
|
|
|
1985
|
+
// Cancel the queued mid-stream steer messages for the active session without
|
|
1986
|
+
// aborting the in-flight turn. Optimistically clears our local pending-steer
|
|
1987
|
+
// set so the "Cancel" affordance disappears immediately; the server's fresh
|
|
1988
|
+
// queue_update (after clearQueue) reconciles the authoritative state.
|
|
1989
|
+
public cancelSteer(): void {
|
|
1990
|
+
const s = get(this.stateStore);
|
|
1991
|
+
if (!s.sessionId) return;
|
|
1992
|
+
this.send({type: 'cancel_steer', sessionId: s.sessionId});
|
|
1993
|
+
this.stateStore.update((st: WhereverState) => ({...st, pendingSteering: []}));
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1876
1996
|
public joinSession(sessionFile: string, cwd?: string, model?: string) {
|
|
1877
1997
|
this.stateStore.update((s: WhereverState) => ({
|
|
1878
1998
|
...s,
|
|
@@ -1922,6 +2042,7 @@ export class WhereverClient {
|
|
|
1922
2042
|
resyncing: false,
|
|
1923
2043
|
agentPending: false,
|
|
1924
2044
|
contextUsage: null,
|
|
2045
|
+
pendingSteering: [],
|
|
1925
2046
|
loadingSession: true,
|
|
1926
2047
|
}));
|
|
1927
2048
|
this.resumeSessionFile = null;
|
|
@@ -1934,6 +2055,17 @@ export class WhereverClient {
|
|
|
1934
2055
|
this.armLoadWatchdog();
|
|
1935
2056
|
}
|
|
1936
2057
|
|
|
2058
|
+
// Fork the given session at a specific user-message entry, mirroring pi's
|
|
2059
|
+
// `/fork` (position 'before'). The server creates a new branched session file
|
|
2060
|
+
// and replies with `session_forked` (carrying the new file path + the chosen
|
|
2061
|
+
// message's text). The app layer listens for that reply, switches to the new
|
|
2062
|
+
// session, and pre-fills the composer. This method only issues the request;
|
|
2063
|
+
// it does not itself change the active session.
|
|
2064
|
+
public forkSession(sessionId: string, entryId: string): boolean {
|
|
2065
|
+
if (!sessionId || !entryId) return false;
|
|
2066
|
+
return this.send({type: 'session_fork', sessionId, entryId});
|
|
2067
|
+
}
|
|
2068
|
+
|
|
1937
2069
|
public createSession(
|
|
1938
2070
|
cwd: string,
|
|
1939
2071
|
model?: string,
|
|
@@ -1994,6 +2126,7 @@ export class WhereverClient {
|
|
|
1994
2126
|
resyncing: false,
|
|
1995
2127
|
agentPending: false,
|
|
1996
2128
|
contextUsage: null,
|
|
2129
|
+
pendingSteering: [],
|
|
1997
2130
|
}));
|
|
1998
2131
|
this.clearLoadWatchdog();
|
|
1999
2132
|
this.resumeSessionFile = null;
|
package/src/types.ts
CHANGED
|
@@ -13,6 +13,11 @@ export interface ChatMessage {
|
|
|
13
13
|
toolName?: string;
|
|
14
14
|
toolArgs?: string;
|
|
15
15
|
toolOutput?: string;
|
|
16
|
+
// The source session-tree entry id this message maps to. Set on `user`
|
|
17
|
+
// messages (from history) so the UI can offer a "Fork from here" action that
|
|
18
|
+
// forks the session BEFORE this entry (pi's position:'before'), pre-filling
|
|
19
|
+
// the composer with this message's text to edit and resend.
|
|
20
|
+
entryId?: string;
|
|
16
21
|
// Image attachments extracted from a tool result (e.g. `read` on an image
|
|
17
22
|
// file). `data` is base64 (no data-URI prefix); rendered inline in the tool
|
|
18
23
|
// output, mirroring the CLI's inline image display.
|
|
@@ -144,6 +149,25 @@ export interface WhereverState {
|
|
|
144
149
|
// Context-window usage for the active session (drives the "11.3% / 1.0M"
|
|
145
150
|
// indicator). null when unknown (no model, no usage yet, or just compacted).
|
|
146
151
|
contextUsage: ContextUsage | null;
|
|
152
|
+
// The mid-stream STEER messages currently queued on the server (pi injects
|
|
153
|
+
// them at the next step boundary). Mirrors the server's queue_update: it is
|
|
154
|
+
// the FULL current queue, replaced outright on each update. Used to badge a
|
|
155
|
+
// still-pending steer bubble and to offer a session-level cancel (pi's
|
|
156
|
+
// clearQueue() drops the whole queue at once); empty when nothing is queued.
|
|
157
|
+
// Only server-type sessions populate this (CLI bridges do not emit
|
|
158
|
+
// queue_update), so CLI steers are not cancellable from the web.
|
|
159
|
+
pendingSteering: string[];
|
|
160
|
+
// Skill commands available for the active session, for the composer's
|
|
161
|
+
// `/skill:<name>` autocomplete. Each `name` is the full invocation without
|
|
162
|
+
// the leading slash (e.g. "skill:setup"). Requested on session_ready and
|
|
163
|
+
// replaced outright by each skills_list; empty for CLI bridges or before the
|
|
164
|
+
// list arrives.
|
|
165
|
+
skills: SkillCommand[];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export interface SkillCommand {
|
|
169
|
+
name: string;
|
|
170
|
+
description?: string;
|
|
147
171
|
}
|
|
148
172
|
|
|
149
173
|
export interface ContextUsage {
|
|
@@ -151,3 +175,60 @@ export interface ContextUsage {
|
|
|
151
175
|
contextWindow: number;
|
|
152
176
|
percent: number | null;
|
|
153
177
|
}
|
|
178
|
+
|
|
179
|
+
// A parsed `/skill:<name> <args>` invocation. `skillName` is the bare skill
|
|
180
|
+
// name; `args` is the trailing text the user typed after `/skill:<name>` (empty
|
|
181
|
+
// when none).
|
|
182
|
+
export interface SkillInvocation {
|
|
183
|
+
skillName: string;
|
|
184
|
+
args: string;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Recognize a `/skill:<name> <args>` invocation in EITHER form and return its
|
|
188
|
+
// parsed parts, or null when the content is not a skill invocation.
|
|
189
|
+
//
|
|
190
|
+
// Two forms carry the same logical invocation and must be treated as one:
|
|
191
|
+
// - RAW: what the client sends and optimistically echoes, e.g.
|
|
192
|
+
// `/skill:setup do the thing`
|
|
193
|
+
// - EXPANDED: what the server stores/echoes back after pi inlines the skill
|
|
194
|
+
// body (pi's _expandSkillCommand), e.g.
|
|
195
|
+
// `<skill name="setup" location="...">\n...body...\n</skill>\n\ndo the thing`
|
|
196
|
+
// Both resolve to { skillName: 'setup', args: 'do the thing' }. This lets the
|
|
197
|
+
// delivery-confirmation match the optimistic RAW echo to the server's EXPANDED
|
|
198
|
+
// echo (same invocation) instead of appending a duplicate and orphaning the
|
|
199
|
+
// optimistic bubble as "failed".
|
|
200
|
+
export function parseSkillInvocation(
|
|
201
|
+
content: string,
|
|
202
|
+
): SkillInvocation | null {
|
|
203
|
+
if (!content) return null;
|
|
204
|
+
const trimmed = content.trimStart();
|
|
205
|
+
// Expanded form: the stored/echoed skill block.
|
|
206
|
+
const open = trimmed.match(/^<skill\s+name="([^"]*)"[^>]*>/);
|
|
207
|
+
if (open) {
|
|
208
|
+
const closeIdx = trimmed.indexOf('</skill>');
|
|
209
|
+
if (closeIdx === -1) return null;
|
|
210
|
+
return {
|
|
211
|
+
skillName: open[1],
|
|
212
|
+
args: trimmed.slice(closeIdx + '</skill>'.length).trim(),
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
// Raw form: the `/skill:<name> <args>` the user typed / the client sent.
|
|
216
|
+
if (trimmed.startsWith('/skill:')) {
|
|
217
|
+
const rest = trimmed.slice('/skill:'.length);
|
|
218
|
+
const spaceIdx = rest.indexOf(' ');
|
|
219
|
+
const skillName = spaceIdx === -1 ? rest : rest.slice(0, spaceIdx);
|
|
220
|
+
if (!skillName) return null;
|
|
221
|
+
const args = spaceIdx === -1 ? '' : rest.slice(spaceIdx + 1).trim();
|
|
222
|
+
return {skillName, args};
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// A stable identity string for a skill invocation, or null when `content` is not
|
|
228
|
+
// one. Equal identities mean the RAW and EXPANDED forms are the same invocation.
|
|
229
|
+
// The NUL separator cannot appear in either the name or the args.
|
|
230
|
+
export function skillInvocationIdentity(content: string): string | null {
|
|
231
|
+
const parsed = parseSkillInvocation(content);
|
|
232
|
+
if (!parsed) return null;
|
|
233
|
+
return `skill:${parsed.skillName}\u0000${parsed.args}`;
|
|
234
|
+
}
|
|
@@ -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,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
|
+
});
|