@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
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;
|
|
@@ -24,7 +24,7 @@ const defaultState: WhereverState = {
|
|
|
24
24
|
isStreaming: false,
|
|
25
25
|
messages: [],
|
|
26
26
|
clientId: null,
|
|
27
|
-
|
|
27
|
+
folderConflict: null,
|
|
28
28
|
isInterrupted: false,
|
|
29
29
|
notice: null,
|
|
30
30
|
sudoPrompt: null,
|
|
@@ -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 {
|
|
@@ -59,7 +61,7 @@ export class WhereverClient {
|
|
|
59
61
|
// Watchdog for an in-flight session_new. Symmetrical to loadWatchdog: creating
|
|
60
62
|
// a session sets creatingSession=true and shows a BLOCKING full-screen overlay,
|
|
61
63
|
// and the only things that clear it are session_created/session_error/
|
|
62
|
-
//
|
|
64
|
+
// session_interrupted (or a socket close). If none of those
|
|
63
65
|
// ever arrives (a slow git init / remote-repo creation, an error thrown before
|
|
64
66
|
// the reply is sent, or a half-open socket the liveness watchdog has not yet
|
|
65
67
|
// reaped), the overlay spins forever and the only recovery is a reload. This
|
|
@@ -544,7 +546,7 @@ export class WhereverClient {
|
|
|
544
546
|
}
|
|
545
547
|
|
|
546
548
|
// Clear the load watchdog. Called at every point the load resolves
|
|
547
|
-
// (message_history, session_error,
|
|
549
|
+
// (message_history, session_error, disconnect, leave).
|
|
548
550
|
private clearLoadWatchdog() {
|
|
549
551
|
if (this.loadWatchdog) {
|
|
550
552
|
clearTimeout(this.loadWatchdog);
|
|
@@ -574,7 +576,7 @@ export class WhereverClient {
|
|
|
574
576
|
}
|
|
575
577
|
|
|
576
578
|
// Clear the create watchdog. Called at every point a create resolves
|
|
577
|
-
// (session_created, session_error,
|
|
579
|
+
// (session_created, session_error, session_interrupted,
|
|
578
580
|
// disconnect, leave).
|
|
579
581
|
private clearCreateWatchdog() {
|
|
580
582
|
if (this.createWatchdog) {
|
|
@@ -642,11 +644,10 @@ export class WhereverClient {
|
|
|
642
644
|
|
|
643
645
|
// Any message that resolves an in-flight session_load disarms the watchdog.
|
|
644
646
|
// session_created arrives just before message_history; message_history is the
|
|
645
|
-
// real completion, but an error/
|
|
647
|
+
// real completion, but an error/destroy/interrupt also resolves it.
|
|
646
648
|
switch (msg.type) {
|
|
647
649
|
case 'message_history':
|
|
648
650
|
case 'session_error':
|
|
649
|
-
case 'session_conflict':
|
|
650
651
|
case 'session_destroyed':
|
|
651
652
|
case 'session_interrupted':
|
|
652
653
|
this.clearLoadWatchdog();
|
|
@@ -654,12 +655,10 @@ export class WhereverClient {
|
|
|
654
655
|
}
|
|
655
656
|
|
|
656
657
|
// Any message that resolves an in-flight session_new disarms the create
|
|
657
|
-
// watchdog. session_created is the success path; error/
|
|
658
|
-
// it too.
|
|
658
|
+
// watchdog. session_created is the success path; error/interrupt end it too.
|
|
659
659
|
switch (msg.type) {
|
|
660
660
|
case 'session_created':
|
|
661
661
|
case 'session_error':
|
|
662
|
-
case 'session_conflict':
|
|
663
662
|
case 'session_interrupted':
|
|
664
663
|
this.clearCreateWatchdog();
|
|
665
664
|
break;
|
|
@@ -756,6 +755,31 @@ export class WhereverClient {
|
|
|
756
755
|
});
|
|
757
756
|
break;
|
|
758
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
|
+
|
|
759
783
|
case 'message_end':
|
|
760
784
|
this.stateStore.update((s: WhereverState) => {
|
|
761
785
|
let newMessages = s.messages;
|
|
@@ -798,6 +822,32 @@ export class WhereverClient {
|
|
|
798
822
|
return get(this.stateStore);
|
|
799
823
|
}
|
|
800
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
|
+
}
|
|
801
851
|
const lastUserMessage = [...newMessages]
|
|
802
852
|
.reverse()
|
|
803
853
|
.find((m) => m.role === 'user');
|
|
@@ -834,6 +884,11 @@ export class WhereverClient {
|
|
|
834
884
|
this.stateStore.update((s: WhereverState) => ({
|
|
835
885
|
...s,
|
|
836
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: [],
|
|
837
892
|
messages: s.messages.map((m: ChatMessage) =>
|
|
838
893
|
m.isStreaming
|
|
839
894
|
? {
|
|
@@ -879,6 +934,44 @@ export class WhereverClient {
|
|
|
879
934
|
: m,
|
|
880
935
|
);
|
|
881
936
|
const startedAt = Date.now();
|
|
937
|
+
const isForce = !!(msg as any).forceCommand;
|
|
938
|
+
|
|
939
|
+
// A `!command` / `!!command` already rendered an OPTIMISTIC bash bubble
|
|
940
|
+
// locally at send time (so it appeared instantly, without waiting for
|
|
941
|
+
// this server round-trip). Reconcile onto the OLDEST such pending bubble
|
|
942
|
+
// instead of appending a duplicate: adopt the real startedAt and drop
|
|
943
|
+
// the optimistic flag, keeping any output the user's bubble may already
|
|
944
|
+
// carry empty (tool_update/tool_end will fill it). Fall back to append
|
|
945
|
+
// when there is no pending optimistic bubble (agent tool call, or the
|
|
946
|
+
// optimistic one was already reconciled).
|
|
947
|
+
if (isForce) {
|
|
948
|
+
const pending = finalizedMessages.find(
|
|
949
|
+
(m: ChatMessage) =>
|
|
950
|
+
m.role === 'tool' && m.optimistic && m.isStreaming,
|
|
951
|
+
);
|
|
952
|
+
if (pending) {
|
|
953
|
+
return {
|
|
954
|
+
...s,
|
|
955
|
+
messages: finalizedMessages.map((m: ChatMessage) =>
|
|
956
|
+
m.id === pending.id
|
|
957
|
+
? {
|
|
958
|
+
...m,
|
|
959
|
+
optimistic: false,
|
|
960
|
+
toolName: msg.toolName,
|
|
961
|
+
toolArgs,
|
|
962
|
+
content: toolArgs
|
|
963
|
+
? `$ ${msg.toolName} ${toolArgs}`
|
|
964
|
+
: `$ ${msg.toolName}`,
|
|
965
|
+
startedAt,
|
|
966
|
+
timestamp: startedAt,
|
|
967
|
+
forceCommand: true,
|
|
968
|
+
}
|
|
969
|
+
: m,
|
|
970
|
+
),
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
882
975
|
const newMsg: ChatMessage = {
|
|
883
976
|
id: this.generateId(),
|
|
884
977
|
role: 'tool',
|
|
@@ -891,7 +984,7 @@ export class WhereverClient {
|
|
|
891
984
|
toolArgs: toolArgs,
|
|
892
985
|
toolOutput: '',
|
|
893
986
|
startedAt,
|
|
894
|
-
...(
|
|
987
|
+
...(isForce ? { forceCommand: true } : {}),
|
|
895
988
|
};
|
|
896
989
|
return {
|
|
897
990
|
...s,
|
|
@@ -1051,8 +1144,16 @@ export class WhereverClient {
|
|
|
1051
1144
|
isStreaming: msg.isStreaming ?? false,
|
|
1052
1145
|
creatingSession: false,
|
|
1053
1146
|
// The server forces read-only for sessions in a configured
|
|
1054
|
-
// sessions.readOnly folder (observe-only fleet view)
|
|
1147
|
+
// sessions.readOnly folder (observe-only fleet view) AND, transiently,
|
|
1148
|
+
// for a folder conflict until the user clicks "Continue anyway".
|
|
1055
1149
|
readOnly: msg.readOnly ?? false,
|
|
1150
|
+
// Folder-conflict warning banner. Set when attaching to a session in a
|
|
1151
|
+
// folder that already has another active session. `continued` starts
|
|
1152
|
+
// false (composer stays read-only, banner offers "Continue anyway");
|
|
1153
|
+
// `active` is refreshed by live folder_conflict updates.
|
|
1154
|
+
folderConflict: msg.folderConflict
|
|
1155
|
+
? {cwd: msg.cwd, active: true, continued: false}
|
|
1156
|
+
: null,
|
|
1056
1157
|
// pending -> the history is painted now but the live agent is still
|
|
1057
1158
|
// building; keep the composer disabled (agentPending) until
|
|
1058
1159
|
// session_ready. A non-pending create (new session, warm reload) is
|
|
@@ -1061,6 +1162,9 @@ export class WhereverClient {
|
|
|
1061
1162
|
// Initial context-usage snapshot for the new session (null if unknown;
|
|
1062
1163
|
// live updates arrive via 'context_usage').
|
|
1063
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: [],
|
|
1064
1168
|
}));
|
|
1065
1169
|
break;
|
|
1066
1170
|
|
|
@@ -1080,6 +1184,22 @@ export class WhereverClient {
|
|
|
1080
1184
|
contextUsage: msg.contextUsage ?? s.contextUsage,
|
|
1081
1185
|
};
|
|
1082
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
|
+
});
|
|
1083
1203
|
break;
|
|
1084
1204
|
|
|
1085
1205
|
case 'context_usage':
|
|
@@ -1153,19 +1273,35 @@ export class WhereverClient {
|
|
|
1153
1273
|
}));
|
|
1154
1274
|
break;
|
|
1155
1275
|
|
|
1156
|
-
case '
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1276
|
+
case 'folder_conflict':
|
|
1277
|
+
// Live update of whether another active session still exists in this
|
|
1278
|
+
// client's folder. The banner appears when a second session shows up in
|
|
1279
|
+
// the folder and disappears once it is gone. Applies only to the client's
|
|
1280
|
+
// OWN folder; a stale update for a different cwd is ignored.
|
|
1281
|
+
this.stateStore.update((s: WhereverState) => {
|
|
1282
|
+
if (!s.sessionId) return s;
|
|
1283
|
+
if (s.activeCwd && msg.cwd !== s.activeCwd) return s;
|
|
1284
|
+
if (!msg.active) {
|
|
1285
|
+
// No other session remains: fully resolved, drop the banner.
|
|
1286
|
+
return s.folderConflict ? {...s, folderConflict: null} : s;
|
|
1287
|
+
}
|
|
1288
|
+
if (s.folderConflict) {
|
|
1289
|
+
// Already showing: just refresh, preserving the user's `continued`
|
|
1290
|
+
// choice (so we don't re-disable a composer they already enabled).
|
|
1291
|
+
return {
|
|
1292
|
+
...s,
|
|
1293
|
+
folderConflict: {...s.folderConflict, cwd: msg.cwd, active: true},
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
// A conflict appeared AFTER we were already attached (another client
|
|
1297
|
+
// opened a second session in our folder). Raise the banner. We were
|
|
1298
|
+
// sending fine until now, so treat it as already-`continued` rather
|
|
1299
|
+
// than yanking the composer into read-only underneath the user.
|
|
1300
|
+
return {
|
|
1301
|
+
...s,
|
|
1302
|
+
folderConflict: {cwd: msg.cwd, active: true, continued: true},
|
|
1303
|
+
};
|
|
1304
|
+
});
|
|
1169
1305
|
break;
|
|
1170
1306
|
|
|
1171
1307
|
case 'session_interrupted':
|
|
@@ -1432,6 +1568,9 @@ export class WhereverClient {
|
|
|
1432
1568
|
isStreaming: false,
|
|
1433
1569
|
toolName: m.toolName,
|
|
1434
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} : {}),
|
|
1435
1574
|
});
|
|
1436
1575
|
}
|
|
1437
1576
|
}
|
|
@@ -1568,12 +1707,47 @@ export class WhereverClient {
|
|
|
1568
1707
|
// A `!command` / `!!command` is intercepted by the server and run as a bash
|
|
1569
1708
|
// tool call, NOT delivered to the agent. The server never echoes it back as a
|
|
1570
1709
|
// user message, and the bash tool_start/tool_end events are the real, durable
|
|
1571
|
-
// feedback (and are recorded in history). So do NOT add any local
|
|
1572
|
-
// for it: a bubble here would be a transient duplicate that vanishes on
|
|
1710
|
+
// feedback (and are recorded in history). So do NOT add any local USER echo
|
|
1711
|
+
// for it: a user bubble here would be a transient duplicate that vanishes on
|
|
1573
1712
|
// reload (bash runs are not part of the user-message transcript) and, if
|
|
1574
|
-
// delivery-tracked, would wrongly time out into a retry/discard banner.
|
|
1575
|
-
//
|
|
1713
|
+
// delivery-tracked, would wrongly time out into a retry/discard banner.
|
|
1714
|
+
//
|
|
1715
|
+
// We DO, however, render an optimistic bash TOOL bubble immediately so the
|
|
1716
|
+
// command shows up instantly instead of only after the server round-trip
|
|
1717
|
+
// (client -> server -> tool_start -> client). It is tagged `optimistic` so
|
|
1718
|
+
// the real `tool_start` reconciles onto it (no duplicate) and is NOT
|
|
1719
|
+
// delivery-tracked (no watchdog/banner). Skip the optimistic bubble for
|
|
1720
|
+
// `!sudo ...`: the server defers those behind a password prompt and does not
|
|
1721
|
+
// emit `tool_start` until the password arrives, so a pending bubble would sit
|
|
1722
|
+
// there streaming (and never reconcile if the prompt is cancelled).
|
|
1576
1723
|
if (text.trimStart().startsWith('!')) {
|
|
1724
|
+
const raw = text.trimStart();
|
|
1725
|
+
const isExcluded = raw.startsWith('!!');
|
|
1726
|
+
const command = (isExcluded ? raw.slice(2) : raw.slice(1)).trim();
|
|
1727
|
+
const isSudo = /^sudo(\s|$)/.test(command);
|
|
1728
|
+
if (command && !isSudo) {
|
|
1729
|
+
const toolArgs = `command=${JSON.stringify(command)}`;
|
|
1730
|
+
const startedAt = Date.now();
|
|
1731
|
+
const optimisticTool: ChatMessage = {
|
|
1732
|
+
id: this.generateId(),
|
|
1733
|
+
role: 'tool',
|
|
1734
|
+
content: `$ bash ${toolArgs}`,
|
|
1735
|
+
timestamp: startedAt,
|
|
1736
|
+
isStreaming: true,
|
|
1737
|
+
toolName: 'bash',
|
|
1738
|
+
toolArgs,
|
|
1739
|
+
toolOutput: '',
|
|
1740
|
+
startedAt,
|
|
1741
|
+
forceCommand: true,
|
|
1742
|
+
optimistic: true,
|
|
1743
|
+
};
|
|
1744
|
+
this.stateStore.update((st: WhereverState) => ({
|
|
1745
|
+
...st,
|
|
1746
|
+
sessionError: null,
|
|
1747
|
+
messages: [...st.messages, optimisticTool],
|
|
1748
|
+
}));
|
|
1749
|
+
return true;
|
|
1750
|
+
}
|
|
1577
1751
|
this.stateStore.update((st: WhereverState) => ({...st, sessionError: null}));
|
|
1578
1752
|
return true;
|
|
1579
1753
|
}
|
|
@@ -1708,20 +1882,49 @@ export class WhereverClient {
|
|
|
1708
1882
|
// Mark the oldest still-unconfirmed user message with this content as
|
|
1709
1883
|
// delivered (the server echoed it back). Content-matched because the server
|
|
1710
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.
|
|
1711
1894
|
private confirmDeliveredByContent(content: string): boolean {
|
|
1712
1895
|
let confirmedId: string | null = null;
|
|
1713
1896
|
let sessionId: string | null = null;
|
|
1897
|
+
const echoIdentity = skillInvocationIdentity(content);
|
|
1714
1898
|
this.stateStore.update((st: WhereverState) => {
|
|
1715
|
-
|
|
1899
|
+
let target = st.messages.find(
|
|
1716
1900
|
(m) => m.role === 'user' && m.delivery !== undefined && m.content === content,
|
|
1717
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
|
+
}
|
|
1718
1912
|
if (!target) return st;
|
|
1719
1913
|
confirmedId = target.id;
|
|
1720
1914
|
sessionId = st.sessionId;
|
|
1721
1915
|
return {
|
|
1722
1916
|
...st,
|
|
1723
1917
|
messages: st.messages.map((m) =>
|
|
1724
|
-
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,
|
|
1725
1928
|
),
|
|
1726
1929
|
};
|
|
1727
1930
|
});
|
|
@@ -1779,10 +1982,21 @@ export class WhereverClient {
|
|
|
1779
1982
|
this.send({type: 'abort', sessionId: s.sessionId});
|
|
1780
1983
|
}
|
|
1781
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
|
+
|
|
1782
1996
|
public joinSession(sessionFile: string, cwd?: string, model?: string) {
|
|
1783
1997
|
this.stateStore.update((s: WhereverState) => ({
|
|
1784
1998
|
...s,
|
|
1785
|
-
|
|
1999
|
+
folderConflict: null,
|
|
1786
2000
|
sessionError: null,
|
|
1787
2001
|
loadingSession: true,
|
|
1788
2002
|
agentPending: false,
|
|
@@ -1820,7 +2034,7 @@ export class WhereverClient {
|
|
|
1820
2034
|
activeCwd: null,
|
|
1821
2035
|
activeModel: null,
|
|
1822
2036
|
readOnly: false,
|
|
1823
|
-
|
|
2037
|
+
folderConflict: null,
|
|
1824
2038
|
sessionError: null,
|
|
1825
2039
|
notice: null,
|
|
1826
2040
|
sudoPrompt: null,
|
|
@@ -1828,6 +2042,7 @@ export class WhereverClient {
|
|
|
1828
2042
|
resyncing: false,
|
|
1829
2043
|
agentPending: false,
|
|
1830
2044
|
contextUsage: null,
|
|
2045
|
+
pendingSteering: [],
|
|
1831
2046
|
loadingSession: true,
|
|
1832
2047
|
}));
|
|
1833
2048
|
this.resumeSessionFile = null;
|
|
@@ -1840,6 +2055,17 @@ export class WhereverClient {
|
|
|
1840
2055
|
this.armLoadWatchdog();
|
|
1841
2056
|
}
|
|
1842
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
|
+
|
|
1843
2069
|
public createSession(
|
|
1844
2070
|
cwd: string,
|
|
1845
2071
|
model?: string,
|
|
@@ -1850,7 +2076,7 @@ export class WhereverClient {
|
|
|
1850
2076
|
) {
|
|
1851
2077
|
this.stateStore.update((s: WhereverState) => ({
|
|
1852
2078
|
...s,
|
|
1853
|
-
|
|
2079
|
+
folderConflict: null,
|
|
1854
2080
|
sessionError: null,
|
|
1855
2081
|
creatingSession: true,
|
|
1856
2082
|
loadingSession: false,
|
|
@@ -1900,6 +2126,7 @@ export class WhereverClient {
|
|
|
1900
2126
|
resyncing: false,
|
|
1901
2127
|
agentPending: false,
|
|
1902
2128
|
contextUsage: null,
|
|
2129
|
+
pendingSteering: [],
|
|
1903
2130
|
}));
|
|
1904
2131
|
this.clearLoadWatchdog();
|
|
1905
2132
|
this.resumeSessionFile = null;
|
|
@@ -1907,21 +2134,26 @@ export class WhereverClient {
|
|
|
1907
2134
|
this.resumeModel = undefined;
|
|
1908
2135
|
}
|
|
1909
2136
|
|
|
1910
|
-
|
|
2137
|
+
// "Continue anyway" on the folder-conflict warning banner: ask the server to
|
|
2138
|
+
// lift read-only for this client's current session (both sessions in the
|
|
2139
|
+
// folder then run concurrently; the other is NOT aborted). Optimistically
|
|
2140
|
+
// enable the composer and mark the banner `continued` so it drops its button
|
|
2141
|
+
// but stays as a passive warning until the other session leaves the folder.
|
|
2142
|
+
public continueFolderConflict() {
|
|
1911
2143
|
const s = get(this.stateStore);
|
|
1912
|
-
if (!s.
|
|
2144
|
+
if (!s.folderConflict || !s.sessionId) return;
|
|
1913
2145
|
|
|
1914
2146
|
this.send({
|
|
1915
|
-
type: '
|
|
1916
|
-
|
|
1917
|
-
sessionId: s.conflict.targetSessionId,
|
|
1918
|
-
cwd: cwd || s.conflict.conflictingCwd,
|
|
2147
|
+
type: 'folder_conflict_continue',
|
|
2148
|
+
sessionId: s.sessionId,
|
|
1919
2149
|
});
|
|
1920
2150
|
|
|
1921
2151
|
this.stateStore.update((s: WhereverState) => ({
|
|
1922
2152
|
...s,
|
|
1923
|
-
|
|
1924
|
-
|
|
2153
|
+
readOnly: false,
|
|
2154
|
+
folderConflict: s.folderConflict
|
|
2155
|
+
? {...s.folderConflict, continued: true}
|
|
2156
|
+
: null,
|
|
1925
2157
|
}));
|
|
1926
2158
|
}
|
|
1927
2159
|
|
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.
|
|
@@ -31,6 +36,14 @@ export interface ChatMessage {
|
|
|
31
36
|
// frame (live) or the history tool_call (reload). The web UI auto-expands
|
|
32
37
|
// force-command bash output. Only meaningful for role: 'tool', toolName 'bash'.
|
|
33
38
|
forceCommand?: boolean;
|
|
39
|
+
// True on an OPTIMISTIC force-command (`!command` / `!!command`) tool bubble
|
|
40
|
+
// the client rendered locally at send time, BEFORE the server's real
|
|
41
|
+
// `tool_start` arrives. It gives instant feedback (the server no longer echoes
|
|
42
|
+
// `!`-commands as a user message, so without this nothing shows until the
|
|
43
|
+
// server round-trip completes). When the real `tool_start` for the same
|
|
44
|
+
// command arrives it RECONCILES onto this bubble (clearing this flag) instead
|
|
45
|
+
// of appending a duplicate. Only meaningful for role: 'tool', toolName 'bash'.
|
|
46
|
+
optimistic?: boolean;
|
|
34
47
|
// Wall-clock start of a tool call (ms epoch), set when the tool_start frame
|
|
35
48
|
// arrives. `timestamp` also carries this, but startedAt is kept explicit so
|
|
36
49
|
// the elapsed/took duration is unambiguous and survives content rewrites.
|
|
@@ -68,10 +81,16 @@ export interface SudoPrompt {
|
|
|
68
81
|
sessionId: string;
|
|
69
82
|
}
|
|
70
83
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
84
|
+
// State for the folder-conflict warning banner. Set when this client attaches
|
|
85
|
+
// to a session in a folder that already has ANOTHER active session. There is no
|
|
86
|
+
// take-over/read-only protection anymore: `active` reflects whether another
|
|
87
|
+
// session is still live in the folder (the banner shows while true), and
|
|
88
|
+
// `continued` is true once the user clicked "Continue anyway" (so the composer
|
|
89
|
+
// is enabled and the banner drops its button, staying only as a passive notice).
|
|
90
|
+
export interface FolderConflictInfo {
|
|
91
|
+
cwd: string;
|
|
92
|
+
active: boolean;
|
|
93
|
+
continued: boolean;
|
|
75
94
|
}
|
|
76
95
|
|
|
77
96
|
export interface WhereverState {
|
|
@@ -101,7 +120,7 @@ export interface WhereverState {
|
|
|
101
120
|
isStreaming: boolean;
|
|
102
121
|
messages: ChatMessage[];
|
|
103
122
|
clientId: string | null;
|
|
104
|
-
|
|
123
|
+
folderConflict: FolderConflictInfo | null;
|
|
105
124
|
isInterrupted: boolean;
|
|
106
125
|
// A dismissible, non-fatal notice about the active session (e.g. a CLI bridge
|
|
107
126
|
// took over while this session was mid-turn here, discarding the in-flight
|
|
@@ -130,6 +149,25 @@ export interface WhereverState {
|
|
|
130
149
|
// Context-window usage for the active session (drives the "11.3% / 1.0M"
|
|
131
150
|
// indicator). null when unknown (no model, no usage yet, or just compacted).
|
|
132
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;
|
|
133
171
|
}
|
|
134
172
|
|
|
135
173
|
export interface ContextUsage {
|
|
@@ -137,3 +175,60 @@ export interface ContextUsage {
|
|
|
137
175
|
contextWindow: number;
|
|
138
176
|
percent: number | null;
|
|
139
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
|
+
}
|