@wherever-dev/client 0.3.1 → 0.3.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 +51 -0
- package/dist/client.d.ts +13 -0
- package/dist/client.js +220 -59
- package/dist/client.js.map +1 -1
- package/dist/types.d.ts +11 -0
- package/package.json +1 -1
- package/src/client.ts +222 -61
- package/src/types.ts +28 -0
- package/test/tool-aborted-render.test.ts +170 -0
- package/test/tool-interrupted-history.test.ts +294 -0
package/src/client.ts
CHANGED
|
@@ -26,6 +26,7 @@ const defaultState: WhereverState = {
|
|
|
26
26
|
clientId: null,
|
|
27
27
|
conflict: null,
|
|
28
28
|
isInterrupted: false,
|
|
29
|
+
notice: null,
|
|
29
30
|
sessionError: null,
|
|
30
31
|
readOnly: false,
|
|
31
32
|
activeSessionFile: null,
|
|
@@ -790,6 +791,13 @@ export class WhereverClient {
|
|
|
790
791
|
? {
|
|
791
792
|
...m,
|
|
792
793
|
isStreaming: false,
|
|
794
|
+
// A tool still streaming when the turn ends never got a
|
|
795
|
+
// tool_end (e.g. the turn was aborted, or its result frame
|
|
796
|
+
// was lost): its outcome is unknown. Mark it interrupted
|
|
797
|
+
// (neutral), never let it fall through to a green success
|
|
798
|
+
// tick, which would falsely claim it succeeded. Assistant/
|
|
799
|
+
// thinking messages just stop streaming.
|
|
800
|
+
...(m.role === 'tool' ? {interrupted: true} : {}),
|
|
793
801
|
// Freeze a still-running tool's duration if it never got a
|
|
794
802
|
// tool_end (turn ended between steps), so "Elapsed" stops
|
|
795
803
|
// ticking and shows the final "Took".
|
|
@@ -876,18 +884,43 @@ export class WhereverClient {
|
|
|
876
884
|
|
|
877
885
|
case 'tool_end': {
|
|
878
886
|
const result = msg.result ? `${msg.result}` : '';
|
|
887
|
+
// An abort (e.g. the web "abort" button killed the tool mid-run) surfaces
|
|
888
|
+
// as isError with a trailing "...aborted" status. That is not a tool
|
|
889
|
+
// FAILURE, so render it as the neutral interrupted state, not a red
|
|
890
|
+
// error.
|
|
891
|
+
const aborted = !!msg.isError && this.isAbortedToolResult(result);
|
|
892
|
+
const effectiveIsError = !!msg.isError && !aborted;
|
|
893
|
+
const toolImages =
|
|
894
|
+
Array.isArray(msg.images) && msg.images.length > 0
|
|
895
|
+
? msg.images
|
|
896
|
+
: undefined;
|
|
879
897
|
this.stateStore.update((s: WhereverState) => {
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
898
|
+
// Claim the OLDEST still-streaming tool of this name (FIFO). Parallel
|
|
899
|
+
// tool calls share a name, so matching the LAST one (or any already
|
|
900
|
+
// finalized one) would let two tool_end frames land on the SAME
|
|
901
|
+
// message and leave the other tool stuck streaming: it would then be
|
|
902
|
+
// finalized by the 'aborted'/agent_end sweep with no result and render
|
|
903
|
+
// as a bogus green success. Preferring a streaming message means each
|
|
904
|
+
// tool_end settles a distinct in-flight call. Fall back to the newest
|
|
905
|
+
// non-error tool of this name only if none are still streaming.
|
|
906
|
+
const toolMsg =
|
|
907
|
+
s.messages.find(
|
|
883
908
|
(m: ChatMessage) =>
|
|
884
909
|
m.role === 'tool' &&
|
|
885
910
|
m.toolName === msg.toolName &&
|
|
886
|
-
|
|
887
|
-
)
|
|
911
|
+
m.isStreaming,
|
|
912
|
+
) ??
|
|
913
|
+
[...s.messages]
|
|
914
|
+
.reverse()
|
|
915
|
+
.find(
|
|
916
|
+
(m: ChatMessage) =>
|
|
917
|
+
m.role === 'tool' &&
|
|
918
|
+
m.toolName === msg.toolName &&
|
|
919
|
+
!m.content.startsWith('Tool error:'),
|
|
920
|
+
);
|
|
888
921
|
const endedAt = Date.now();
|
|
889
922
|
if (toolMsg) {
|
|
890
|
-
const errorPrefix =
|
|
923
|
+
const errorPrefix = effectiveIsError ? 'Error: ' : '';
|
|
891
924
|
return {
|
|
892
925
|
...s,
|
|
893
926
|
messages: s.messages.map((m: ChatMessage) =>
|
|
@@ -896,15 +929,17 @@ export class WhereverClient {
|
|
|
896
929
|
...m,
|
|
897
930
|
content: `${errorPrefix}${m.content}\n${result}`,
|
|
898
931
|
isStreaming: false,
|
|
899
|
-
isError:
|
|
932
|
+
isError: effectiveIsError,
|
|
933
|
+
interrupted: aborted,
|
|
900
934
|
toolOutput: result,
|
|
935
|
+
...(toolImages ? {images: toolImages} : {}),
|
|
901
936
|
endedAt,
|
|
902
937
|
}
|
|
903
938
|
: m,
|
|
904
939
|
),
|
|
905
940
|
};
|
|
906
941
|
} else {
|
|
907
|
-
const content =
|
|
942
|
+
const content = effectiveIsError
|
|
908
943
|
? `Tool error: ${msg.toolName}\n${result}`
|
|
909
944
|
: `${msg.toolName}\n${result}`;
|
|
910
945
|
const message: ChatMessage = {
|
|
@@ -916,7 +951,9 @@ export class WhereverClient {
|
|
|
916
951
|
toolName: msg.toolName,
|
|
917
952
|
toolArgs: '',
|
|
918
953
|
toolOutput: result,
|
|
919
|
-
|
|
954
|
+
...(toolImages ? {images: toolImages} : {}),
|
|
955
|
+
isError: effectiveIsError,
|
|
956
|
+
interrupted: aborted,
|
|
920
957
|
endedAt,
|
|
921
958
|
};
|
|
922
959
|
return {
|
|
@@ -938,6 +975,11 @@ export class WhereverClient {
|
|
|
938
975
|
? {
|
|
939
976
|
...m,
|
|
940
977
|
isStreaming: false,
|
|
978
|
+
// A tool still streaming when the turn is aborted was killed
|
|
979
|
+
// with no result: mark it interrupted (neutral), never leave
|
|
980
|
+
// it to fall through to a green success tick. Assistant/
|
|
981
|
+
// thinking messages just stop streaming.
|
|
982
|
+
...(m.role === 'tool' ? {interrupted: true} : {}),
|
|
941
983
|
endedAt:
|
|
942
984
|
m.role === 'tool' && m.endedAt === undefined
|
|
943
985
|
? abortedAt
|
|
@@ -998,6 +1040,17 @@ export class WhereverClient {
|
|
|
998
1040
|
}));
|
|
999
1041
|
break;
|
|
1000
1042
|
|
|
1043
|
+
case 'session_notice':
|
|
1044
|
+
// A non-fatal notice for the active session (e.g. a CLI took over while
|
|
1045
|
+
// this session was mid-turn here, discarding the in-flight tool call or
|
|
1046
|
+
// streaming reply). Keep the session attached; surface it as a
|
|
1047
|
+
// dismissible banner. Ignore a notice for a session we switched away from.
|
|
1048
|
+
this.stateStore.update((s: WhereverState) => {
|
|
1049
|
+
if (s.sessionId && msg.sessionId && s.sessionId !== msg.sessionId) return s;
|
|
1050
|
+
return { ...s, notice: { level: msg.level, message: msg.message } };
|
|
1051
|
+
});
|
|
1052
|
+
break;
|
|
1053
|
+
|
|
1001
1054
|
case 'session_destroyed':
|
|
1002
1055
|
this.stateStore.update((s: WhereverState) => {
|
|
1003
1056
|
if (s.sessionId === msg.sessionId) {
|
|
@@ -1011,6 +1064,7 @@ export class WhereverClient {
|
|
|
1011
1064
|
activeModel: null,
|
|
1012
1065
|
loadingSession: false,
|
|
1013
1066
|
agentPending: false,
|
|
1067
|
+
notice: null,
|
|
1014
1068
|
};
|
|
1015
1069
|
}
|
|
1016
1070
|
return s;
|
|
@@ -1061,6 +1115,7 @@ export class WhereverClient {
|
|
|
1061
1115
|
creatingSession: false,
|
|
1062
1116
|
loadingSession: false,
|
|
1063
1117
|
agentPending: false,
|
|
1118
|
+
notice: null,
|
|
1064
1119
|
}));
|
|
1065
1120
|
break;
|
|
1066
1121
|
|
|
@@ -1142,6 +1197,24 @@ export class WhereverClient {
|
|
|
1142
1197
|
}
|
|
1143
1198
|
}
|
|
1144
1199
|
|
|
1200
|
+
/**
|
|
1201
|
+
* True when an errored tool result is the product of a USER ABORT (e.g. the
|
|
1202
|
+
* web "abort" button killed a running tool), rather than a genuine tool
|
|
1203
|
+
* failure. The pi tools append a trailing status line on abort ("Command
|
|
1204
|
+
* aborted" for bash, "Operation aborted" for edit/write, bare "aborted" for a
|
|
1205
|
+
* pre-execution abort). We match that trailing status so the UI can render a
|
|
1206
|
+
* neutral "interrupted" state instead of a red error: you cancelling a tool is
|
|
1207
|
+
* not the tool failing. Only meaningful when isError is already true; matched
|
|
1208
|
+
* as the LAST non-empty line to avoid false positives from command output that
|
|
1209
|
+
* merely contains the word "aborted".
|
|
1210
|
+
*/
|
|
1211
|
+
private isAbortedToolResult(result: string | undefined): boolean {
|
|
1212
|
+
if (!result) return false;
|
|
1213
|
+
const lines = result.trimEnd().split('\n');
|
|
1214
|
+
const last = (lines[lines.length - 1] || '').trim();
|
|
1215
|
+
return /^(command|operation)?\s*aborted$/i.test(last);
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1145
1218
|
/**
|
|
1146
1219
|
* Map a server `HistoryMessage[]` window into UI `ChatMessage[]`. When
|
|
1147
1220
|
* `applyStreamingTail` is true, the last assistant/thinking message and any
|
|
@@ -1154,49 +1227,131 @@ export class WhereverClient {
|
|
|
1154
1227
|
applyStreamingTail: boolean,
|
|
1155
1228
|
): ChatMessage[] {
|
|
1156
1229
|
const mapped: ChatMessage[] = [];
|
|
1157
|
-
//
|
|
1158
|
-
//
|
|
1159
|
-
//
|
|
1160
|
-
//
|
|
1161
|
-
|
|
1230
|
+
// A tool_call is rendered IN PLACE, at its position in the stream, as a tool
|
|
1231
|
+
// message that is initially result-less. Its matching tool_result fills it
|
|
1232
|
+
// in later. We track the still-open calls per tool name as indices into
|
|
1233
|
+
// `mapped`, so a call that never receives a result simply stays where it was
|
|
1234
|
+
// issued (correctly marked interrupted below) instead of being hoisted to
|
|
1235
|
+
// the very end of the transcript. The end-hoisting was the bug: dangling
|
|
1236
|
+
// tool calls from EARLIER in the conversation (e.g. an interrupted long-
|
|
1237
|
+
// running bash, later superseded by a new user turn) would all pile up as a
|
|
1238
|
+
// "series of aborted tool calls" AFTER the latest reply, even though the CLI
|
|
1239
|
+
// shows them inline where they happened.
|
|
1240
|
+
//
|
|
1241
|
+
// Matching is by tool-call id when present (the server forwards it on both
|
|
1242
|
+
// tool_call and tool_result), which pairs a result to its EXACT call even
|
|
1243
|
+
// when same-named calls interleave with some left dangling. When the id is
|
|
1244
|
+
// absent (older sessions, or the synthesized bashExecution pair) we fall
|
|
1245
|
+
// back to oldest-open-first per tool name.
|
|
1246
|
+
const openCalls: Record<string, number[]> = {};
|
|
1247
|
+
const openById: Record<string, number> = {};
|
|
1248
|
+
|
|
1249
|
+
// Drop a resolved call's index from the name-FIFO list so an id match and a
|
|
1250
|
+
// later name-fallback can never both consume the same call.
|
|
1251
|
+
const removeFromNameQueue = (tName: string, idx: number) => {
|
|
1252
|
+
const q = openCalls[tName];
|
|
1253
|
+
if (!q) return;
|
|
1254
|
+
const at = q.indexOf(idx);
|
|
1255
|
+
if (at !== -1) q.splice(at, 1);
|
|
1256
|
+
};
|
|
1162
1257
|
|
|
1163
1258
|
for (const m of rawMessages) {
|
|
1164
1259
|
if (m.role === 'tool_call') {
|
|
1165
1260
|
const tName = m.toolName || 'unknown';
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
}
|
|
1169
|
-
pendingCalls[tName].push({args: m.content || '', startedAt: m.timestamp});
|
|
1170
|
-
} else if (m.role === 'tool_result') {
|
|
1171
|
-
const tName = m.toolName || 'unknown';
|
|
1172
|
-
const call =
|
|
1173
|
-
pendingCalls[tName] && pendingCalls[tName].length > 0
|
|
1174
|
-
? pendingCalls[tName].shift()!
|
|
1175
|
-
: undefined;
|
|
1176
|
-
const tArgs = call?.args ?? '';
|
|
1261
|
+
const args = m.content || '';
|
|
1262
|
+
const idx = mapped.length;
|
|
1177
1263
|
mapped.push({
|
|
1178
1264
|
id: this.generateId(),
|
|
1179
1265
|
role: 'tool',
|
|
1180
|
-
content:
|
|
1181
|
-
? `$ ${tName} ${tArgs}\n${m.content}`
|
|
1182
|
-
: `$ ${tName}\n${m.content}`,
|
|
1266
|
+
content: args ? `$ ${tName} ${args}` : `$ ${tName}`,
|
|
1183
1267
|
timestamp: m.timestamp,
|
|
1268
|
+
// No result yet. Marked as interrupted below if it stays unmatched;
|
|
1269
|
+
// the streaming tail promotes the last one back to streaming.
|
|
1184
1270
|
isStreaming: false,
|
|
1185
1271
|
toolName: tName,
|
|
1186
|
-
toolArgs:
|
|
1187
|
-
toolOutput:
|
|
1188
|
-
isError: m.isError,
|
|
1272
|
+
toolArgs: args,
|
|
1273
|
+
toolOutput: '',
|
|
1189
1274
|
sessionId,
|
|
1190
|
-
//
|
|
1191
|
-
//
|
|
1192
|
-
|
|
1193
|
-
...(call &&
|
|
1194
|
-
Number.isFinite(call.startedAt) &&
|
|
1195
|
-
Number.isFinite(m.timestamp) &&
|
|
1196
|
-
m.timestamp >= call.startedAt
|
|
1197
|
-
? {startedAt: call.startedAt, endedAt: m.timestamp}
|
|
1198
|
-
: {}),
|
|
1275
|
+
// Keep the start so a matched result (or a still-running streaming
|
|
1276
|
+
// tail) can show a coherent duration / "Elapsed".
|
|
1277
|
+
...(Number.isFinite(m.timestamp) ? {startedAt: m.timestamp} : {}),
|
|
1199
1278
|
});
|
|
1279
|
+
if (!openCalls[tName]) openCalls[tName] = [];
|
|
1280
|
+
openCalls[tName].push(idx);
|
|
1281
|
+
if (typeof m.toolCallId === 'string' && m.toolCallId) {
|
|
1282
|
+
openById[m.toolCallId] = idx;
|
|
1283
|
+
}
|
|
1284
|
+
} else if (m.role === 'tool_result') {
|
|
1285
|
+
const tName = m.toolName || 'unknown';
|
|
1286
|
+
// Prefer an exact id match; else fill the OLDEST still-open call of this
|
|
1287
|
+
// name (FIFO), mirroring how parallel same-named calls resolve in order.
|
|
1288
|
+
let callIdx: number | undefined;
|
|
1289
|
+
if (
|
|
1290
|
+
typeof m.toolCallId === 'string' &&
|
|
1291
|
+
m.toolCallId &&
|
|
1292
|
+
openById[m.toolCallId] !== undefined
|
|
1293
|
+
) {
|
|
1294
|
+
callIdx = openById[m.toolCallId];
|
|
1295
|
+
delete openById[m.toolCallId];
|
|
1296
|
+
removeFromNameQueue(tName, callIdx);
|
|
1297
|
+
} else if (openCalls[tName] && openCalls[tName].length > 0) {
|
|
1298
|
+
callIdx = openCalls[tName].shift()!;
|
|
1299
|
+
// Keep openById consistent if this call had also registered an id.
|
|
1300
|
+
for (const [id, i] of Object.entries(openById)) {
|
|
1301
|
+
if (i === callIdx) delete openById[id];
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
const isError = m.isError && !this.isAbortedToolResult(m.content);
|
|
1305
|
+
const interrupted = !!m.isError && this.isAbortedToolResult(m.content);
|
|
1306
|
+
if (callIdx !== undefined) {
|
|
1307
|
+
const call = mapped[callIdx];
|
|
1308
|
+
const tArgs = call.toolArgs || '';
|
|
1309
|
+
const startedAt = call.startedAt;
|
|
1310
|
+
mapped[callIdx] = {
|
|
1311
|
+
...call,
|
|
1312
|
+
content: tArgs
|
|
1313
|
+
? `$ ${tName} ${tArgs}\n${m.content}`
|
|
1314
|
+
: `$ ${tName}\n${m.content}`,
|
|
1315
|
+
isStreaming: false,
|
|
1316
|
+
toolOutput: m.content,
|
|
1317
|
+
...(Array.isArray(m.images) && m.images.length > 0
|
|
1318
|
+
? {images: m.images}
|
|
1319
|
+
: {}),
|
|
1320
|
+
// A user abort surfaces as an errored result with a trailing
|
|
1321
|
+
// "...aborted" status. Render it as interrupted (neutral), not a red
|
|
1322
|
+
// error: aborting a tool is not the tool failing.
|
|
1323
|
+
isError,
|
|
1324
|
+
interrupted,
|
|
1325
|
+
timestamp: m.timestamp,
|
|
1326
|
+
// Duration only when both timestamps are finite and coherent
|
|
1327
|
+
// (end >= start); otherwise leave unset rather than show a bogus one.
|
|
1328
|
+
...(startedAt !== undefined &&
|
|
1329
|
+
Number.isFinite(startedAt) &&
|
|
1330
|
+
Number.isFinite(m.timestamp) &&
|
|
1331
|
+
m.timestamp >= startedAt
|
|
1332
|
+
? {endedAt: m.timestamp}
|
|
1333
|
+
: {}),
|
|
1334
|
+
};
|
|
1335
|
+
} else {
|
|
1336
|
+
// A result with no preceding open call (shouldn't normally happen).
|
|
1337
|
+
// Render it in place as a standalone tool message so nothing is lost.
|
|
1338
|
+
mapped.push({
|
|
1339
|
+
id: this.generateId(),
|
|
1340
|
+
role: 'tool',
|
|
1341
|
+
content: `$ ${tName}\n${m.content}`,
|
|
1342
|
+
timestamp: m.timestamp,
|
|
1343
|
+
isStreaming: false,
|
|
1344
|
+
toolName: tName,
|
|
1345
|
+
toolArgs: '',
|
|
1346
|
+
toolOutput: m.content,
|
|
1347
|
+
...(Array.isArray(m.images) && m.images.length > 0
|
|
1348
|
+
? {images: m.images}
|
|
1349
|
+
: {}),
|
|
1350
|
+
isError,
|
|
1351
|
+
interrupted,
|
|
1352
|
+
sessionId,
|
|
1353
|
+
});
|
|
1354
|
+
}
|
|
1200
1355
|
} else {
|
|
1201
1356
|
mapped.push({
|
|
1202
1357
|
id: this.generateId(),
|
|
@@ -1210,27 +1365,27 @@ export class WhereverClient {
|
|
|
1210
1365
|
}
|
|
1211
1366
|
}
|
|
1212
1367
|
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
}
|
|
1368
|
+
// Any call still open (issued a toolCall, never got a tool_result) is FROZEN
|
|
1369
|
+
// in place with no result. Meaning depends on context:
|
|
1370
|
+
// - streaming tail (applyStreamingTail): only the MOST RECENT open call is
|
|
1371
|
+
// plausibly still running; keep it streaming so the UI ticks "Elapsed".
|
|
1372
|
+
// Earlier open calls in the same window are already superseded and are
|
|
1373
|
+
// interrupted.
|
|
1374
|
+
// - otherwise: its outcome is unknown (e.g. a CLI takeover / interruption
|
|
1375
|
+
// killed it mid-run), so mark it `interrupted` for a neutral "no result"
|
|
1376
|
+
// state instead of a bogus green success tick.
|
|
1377
|
+
const allOpen: number[] = [];
|
|
1378
|
+
for (const idxs of Object.values(openCalls)) allOpen.push(...idxs);
|
|
1379
|
+
allOpen.sort((a, b) => a - b);
|
|
1380
|
+
const lastOpenIdx =
|
|
1381
|
+
applyStreamingTail && allOpen.length > 0
|
|
1382
|
+
? allOpen[allOpen.length - 1]
|
|
1383
|
+
: -1;
|
|
1384
|
+
for (const idx of allOpen) {
|
|
1385
|
+
if (idx === lastOpenIdx) {
|
|
1386
|
+
mapped[idx] = {...mapped[idx], isStreaming: true};
|
|
1387
|
+
} else {
|
|
1388
|
+
mapped[idx] = {...mapped[idx], interrupted: true};
|
|
1234
1389
|
}
|
|
1235
1390
|
}
|
|
1236
1391
|
|
|
@@ -1583,6 +1738,7 @@ export class WhereverClient {
|
|
|
1583
1738
|
readOnly: false,
|
|
1584
1739
|
conflict: null,
|
|
1585
1740
|
sessionError: null,
|
|
1741
|
+
notice: null,
|
|
1586
1742
|
creatingSession: false,
|
|
1587
1743
|
resyncing: false,
|
|
1588
1744
|
agentPending: false,
|
|
@@ -1650,6 +1806,7 @@ export class WhereverClient {
|
|
|
1650
1806
|
activeCwd: null,
|
|
1651
1807
|
activeModel: null,
|
|
1652
1808
|
readOnly: false,
|
|
1809
|
+
notice: null,
|
|
1653
1810
|
creatingSession: false,
|
|
1654
1811
|
loadingSession: false,
|
|
1655
1812
|
resyncing: false,
|
|
@@ -1692,6 +1849,10 @@ export class WhereverClient {
|
|
|
1692
1849
|
this.stateStore.update((s: WhereverState) => ({...s, sessionError: null}));
|
|
1693
1850
|
}
|
|
1694
1851
|
|
|
1852
|
+
public dismissNotice() {
|
|
1853
|
+
this.stateStore.update((s: WhereverState) => ({...s, notice: null}));
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1695
1856
|
public changeModel(model: string) {
|
|
1696
1857
|
this.send({type: 'model_change', model});
|
|
1697
1858
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
export interface ToolImage {
|
|
2
|
+
mimeType: string;
|
|
3
|
+
data: string;
|
|
4
|
+
}
|
|
5
|
+
|
|
1
6
|
export interface ChatMessage {
|
|
2
7
|
id: string;
|
|
3
8
|
role: 'user' | 'assistant' | 'thinking' | 'tool';
|
|
@@ -8,7 +13,19 @@ export interface ChatMessage {
|
|
|
8
13
|
toolName?: string;
|
|
9
14
|
toolArgs?: string;
|
|
10
15
|
toolOutput?: string;
|
|
16
|
+
// Image attachments extracted from a tool result (e.g. `read` on an image
|
|
17
|
+
// file). `data` is base64 (no data-URI prefix); rendered inline in the tool
|
|
18
|
+
// output, mirroring the CLI's inline image display.
|
|
19
|
+
images?: ToolImage[];
|
|
11
20
|
isError?: boolean;
|
|
21
|
+
// A tool call that ended WITHOUT a result: the assistant issued the toolCall
|
|
22
|
+
// but no toolResult was ever persisted (e.g. a CLI took over the session and
|
|
23
|
+
// the running tool was killed mid-execution). Its outcome is unknown, so the
|
|
24
|
+
// UI must show a distinct "interrupted / no result" state, NOT the green
|
|
25
|
+
// success tick (we do not know if it succeeded or failed) and NOT the red
|
|
26
|
+
// error tick (it was not an error, it just never finished). Only meaningful
|
|
27
|
+
// for role: 'tool'; mutually exclusive with a live isStreaming tool.
|
|
28
|
+
interrupted?: boolean;
|
|
12
29
|
// Wall-clock start of a tool call (ms epoch), set when the tool_start frame
|
|
13
30
|
// arrives. `timestamp` also carries this, but startedAt is kept explicit so
|
|
14
31
|
// the elapsed/took duration is unambiguous and survives content rewrites.
|
|
@@ -31,6 +48,11 @@ export interface ChatMessage {
|
|
|
31
48
|
delivery?: 'sending' | 'failed';
|
|
32
49
|
}
|
|
33
50
|
|
|
51
|
+
export interface SessionNotice {
|
|
52
|
+
level: 'info' | 'warning';
|
|
53
|
+
message: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
34
56
|
export interface ConflictInfo {
|
|
35
57
|
targetSessionId: string;
|
|
36
58
|
conflictingSessionId: string;
|
|
@@ -66,6 +88,12 @@ export interface WhereverState {
|
|
|
66
88
|
clientId: string | null;
|
|
67
89
|
conflict: ConflictInfo | null;
|
|
68
90
|
isInterrupted: boolean;
|
|
91
|
+
// A dismissible, non-fatal notice about the active session (e.g. a CLI bridge
|
|
92
|
+
// took over while this session was mid-turn here, discarding the in-flight
|
|
93
|
+
// tool call or streaming reply). Rendered as a banner and cleared on dismiss,
|
|
94
|
+
// on leaving the session, or when a fresh notice replaces it. null when there
|
|
95
|
+
// is nothing to show.
|
|
96
|
+
notice: SessionNotice | null;
|
|
69
97
|
sessionError: string | null;
|
|
70
98
|
readOnly: boolean;
|
|
71
99
|
activeSessionFile: string | null;
|
|
@@ -0,0 +1,170 @@
|
|
|
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
|
+
// When a running tool is ABORTED (e.g. the web "abort" button kills it mid-run),
|
|
8
|
+
// pi surfaces it as an errored result with a trailing "...aborted" status. That
|
|
9
|
+
// is NOT a tool failure, so the UI must render a neutral "interrupted" state,
|
|
10
|
+
// not a red error. A tool that genuinely completed (or genuinely failed) before
|
|
11
|
+
// the abort keeps its real success/error state.
|
|
12
|
+
|
|
13
|
+
function setup(ws: ReturnType<typeof makeWSFactory>, isStreaming = true) {
|
|
14
|
+
const client = new WhereverClient({
|
|
15
|
+
host: 'localhost',
|
|
16
|
+
port: 1234,
|
|
17
|
+
secure: false,
|
|
18
|
+
WebSocketCtor: ws.ctor,
|
|
19
|
+
});
|
|
20
|
+
client.connect();
|
|
21
|
+
ws.last().open();
|
|
22
|
+
client.joinSession('/tmp/project/session.jsonl');
|
|
23
|
+
ws.last().receive({
|
|
24
|
+
type: 'session_created',
|
|
25
|
+
sessionId: 'sid-1',
|
|
26
|
+
sessionFile: '/tmp/project/session.jsonl',
|
|
27
|
+
cwd: '/tmp/project',
|
|
28
|
+
model: 'fake:model',
|
|
29
|
+
isStreaming,
|
|
30
|
+
});
|
|
31
|
+
return client;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
describe('aborted tool call rendering', () => {
|
|
35
|
+
let ws: ReturnType<typeof makeWSFactory>;
|
|
36
|
+
let client: WhereverClient;
|
|
37
|
+
const state = () => get<WhereverState>(client.stateStore);
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
vi.useFakeTimers();
|
|
41
|
+
ws = makeWSFactory();
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
afterEach(() => {
|
|
45
|
+
client.disconnect(true);
|
|
46
|
+
vi.useRealTimers();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('live tool_end: an aborted result is interrupted, not an error', () => {
|
|
50
|
+
client = setup(ws);
|
|
51
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
|
|
52
|
+
ws.last().receive({
|
|
53
|
+
type: 'tool_end',
|
|
54
|
+
sessionId: 'sid-1',
|
|
55
|
+
toolName: 'bash',
|
|
56
|
+
isError: true,
|
|
57
|
+
result: 'Command aborted',
|
|
58
|
+
});
|
|
59
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
60
|
+
expect(tool.interrupted).toBe(true);
|
|
61
|
+
expect(tool.isError).toBe(false);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('live tool_end: a genuine error stays an error', () => {
|
|
65
|
+
client = setup(ws);
|
|
66
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'false'}});
|
|
67
|
+
ws.last().receive({
|
|
68
|
+
type: 'tool_end',
|
|
69
|
+
sessionId: 'sid-1',
|
|
70
|
+
toolName: 'bash',
|
|
71
|
+
isError: true,
|
|
72
|
+
result: 'boom\n\nCommand exited with code 1',
|
|
73
|
+
});
|
|
74
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
75
|
+
expect(tool.isError).toBe(true);
|
|
76
|
+
expect(tool.interrupted).toBeFalsy();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('live tool_end: a genuine success stays a success', () => {
|
|
80
|
+
client = setup(ws);
|
|
81
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'echo quick'}});
|
|
82
|
+
ws.last().receive({
|
|
83
|
+
type: 'tool_end',
|
|
84
|
+
sessionId: 'sid-1',
|
|
85
|
+
toolName: 'bash',
|
|
86
|
+
isError: false,
|
|
87
|
+
result: 'quick',
|
|
88
|
+
});
|
|
89
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
90
|
+
expect(tool.isError).toBe(false);
|
|
91
|
+
expect(tool.interrupted).toBeFalsy();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('history: two parallel tools, one completed (green) and one aborted (interrupted)', () => {
|
|
95
|
+
client = setup(ws, false);
|
|
96
|
+
ws.last().receive({
|
|
97
|
+
type: 'message_history',
|
|
98
|
+
sessionId: 'sid-1',
|
|
99
|
+
messages: [
|
|
100
|
+
{role: 'user', content: 'go', timestamp: 1_000},
|
|
101
|
+
{role: 'tool_call', content: '{"command":"echo quick"}', timestamp: 2_000, toolName: 'bash'},
|
|
102
|
+
{role: 'tool_call', content: '{"command":"sleep 8"}', timestamp: 2_001, toolName: 'bash'},
|
|
103
|
+
{role: 'tool_result', content: 'quick', timestamp: 3_000, toolName: 'bash', isError: false},
|
|
104
|
+
{role: 'tool_result', content: 'Command aborted', timestamp: 3_100, toolName: 'bash', isError: true},
|
|
105
|
+
],
|
|
106
|
+
totalCount: 5,
|
|
107
|
+
offset: 0,
|
|
108
|
+
});
|
|
109
|
+
const tools = state().messages.filter((m) => m.role === 'tool');
|
|
110
|
+
expect(tools.length).toBe(2);
|
|
111
|
+
// FIFO pairing by tool name: first result -> first call (echo, success),
|
|
112
|
+
// second result -> second call (sleep, aborted -> interrupted).
|
|
113
|
+
const succeeded = tools.find((t) => t.toolOutput === 'quick')!;
|
|
114
|
+
const aborted = tools.find((t) => (t.toolOutput || '').includes('aborted'))!;
|
|
115
|
+
expect(succeeded.isError).toBeFalsy();
|
|
116
|
+
expect(succeeded.interrupted).toBeFalsy();
|
|
117
|
+
expect(aborted.isError).toBe(false);
|
|
118
|
+
expect(aborted.interrupted).toBe(true);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('two parallel same-named tools both aborted: BOTH interrupted (no stray green)', () => {
|
|
122
|
+
client = setup(ws);
|
|
123
|
+
// Two parallel bash calls start (same tool name).
|
|
124
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
|
|
125
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
|
|
126
|
+
// Both settle as aborted. FIFO matching must route each tool_end to a
|
|
127
|
+
// DISTINCT streaming message, so neither is left to be finalized green.
|
|
128
|
+
ws.last().receive({type: 'tool_end', sessionId: 'sid-1', toolName: 'bash', isError: true, result: 'Command aborted'});
|
|
129
|
+
ws.last().receive({type: 'tool_end', sessionId: 'sid-1', toolName: 'bash', isError: true, result: 'Command aborted'});
|
|
130
|
+
|
|
131
|
+
const tools = state().messages.filter((m) => m.role === 'tool');
|
|
132
|
+
expect(tools.length).toBe(2);
|
|
133
|
+
for (const t of tools) {
|
|
134
|
+
expect(t.isStreaming).toBe(false);
|
|
135
|
+
expect(t.interrupted).toBe(true);
|
|
136
|
+
expect(t.isError).toBe(false);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('a tool still streaming at agent_end is interrupted, not a green success', () => {
|
|
141
|
+
client = setup(ws);
|
|
142
|
+
vi.useFakeTimers();
|
|
143
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'sleep 8'}});
|
|
144
|
+
// The turn ends WITHOUT a tool_end for this tool (e.g. aborted, or the
|
|
145
|
+
// result frame was lost). agent_end finalizes after a 300ms debounce.
|
|
146
|
+
ws.last().receive({type: 'agent_end', sessionId: 'sid-1'});
|
|
147
|
+
vi.advanceTimersByTime(350);
|
|
148
|
+
|
|
149
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
150
|
+
expect(tool.isStreaming).toBe(false);
|
|
151
|
+
expect(tool.interrupted).toBe(true);
|
|
152
|
+
expect(tool.isError).toBeFalsy();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('an "aborted" substring in normal output is NOT treated as an abort', () => {
|
|
156
|
+
client = setup(ws);
|
|
157
|
+
ws.last().receive({type: 'tool_start', sessionId: 'sid-1', toolName: 'bash', args: {command: 'echo'}});
|
|
158
|
+
ws.last().receive({
|
|
159
|
+
type: 'tool_end',
|
|
160
|
+
sessionId: 'sid-1',
|
|
161
|
+
toolName: 'bash',
|
|
162
|
+
isError: true,
|
|
163
|
+
result: 'the build was aborted by the linter\n\nCommand exited with code 2',
|
|
164
|
+
});
|
|
165
|
+
const tool = state().messages.find((m) => m.role === 'tool')!;
|
|
166
|
+
// Trailing status line is "Command exited with code 2", so it is a real error.
|
|
167
|
+
expect(tool.isError).toBe(true);
|
|
168
|
+
expect(tool.interrupted).toBeFalsy();
|
|
169
|
+
});
|
|
170
|
+
});
|