@parallelworks/code 0.4.0 → 0.5.0
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/README.md +39 -12
- package/dist/agentSwitcher.d.ts +13 -2
- package/dist/agentSwitcher.js +28 -6
- package/dist/ago.d.ts +1 -3
- package/dist/ago.js +1 -3
- package/dist/api.d.ts +200 -0
- package/dist/chatStrings.d.ts +1 -0
- package/dist/chatStrings.js +2 -1
- package/dist/claudeAccounts.d.ts +20 -2
- package/dist/claudeAccounts.js +51 -7
- package/dist/claudeApi.d.ts +2 -1
- package/dist/claudeApi.js +49 -3
- package/dist/command.d.ts +5 -1
- package/dist/command.js +6 -1
- package/dist/createPwCodeApi.d.ts +70 -1
- package/dist/createPwCodeApi.js +4 -2
- package/dist/index.d.ts +5 -4
- package/dist/index.js +5 -4
- package/dist/locales/en/agentTranscript.js +2 -1
- package/dist/locales/en/agents.js +16 -3
- package/dist/locales/en.d.ts +13 -0
- package/dist/locales/es/agentTranscript.js +2 -1
- package/dist/locales/es/agents.js +16 -3
- package/dist/locales/ja/agentTranscript.js +2 -1
- package/dist/locales/ja/agents.js +16 -3
- package/dist/locales/ko/agentTranscript.js +2 -1
- package/dist/locales/ko/agents.js +16 -3
- package/dist/locales/zh/agentTranscript.js +2 -1
- package/dist/locales/zh/agents.js +16 -3
- package/dist/notices.d.ts +2 -0
- package/dist/notices.js +20 -13
- package/dist/query.d.ts +4 -1
- package/dist/query.js +9 -3
- package/dist/session.d.ts +6 -0
- package/dist/session.js +5 -1
- package/dist/sessionEvents.d.ts +29 -2
- package/dist/sessionEvents.js +187 -73
- package/dist/sessionGroups.d.ts +3 -3
- package/dist/sessionGroups.js +3 -3
- package/dist/sessionHeader.js +6 -2
- package/dist/sessionList.d.ts +13 -8
- package/dist/sessionList.js +20 -10
- package/dist/sessionScreen.d.ts +5 -1
- package/dist/sessionScreen.js +12 -7
- package/dist/sessionsApi.d.ts +19 -3
- package/dist/sessionsApi.js +100 -12
- package/dist/sessionsFeed.d.ts +22 -0
- package/dist/sessionsFeed.js +125 -0
- package/dist/todos.d.ts +42 -0
- package/dist/todos.js +104 -0
- package/dist/transcript.d.ts +1 -2
- package/dist/transcript.js +63 -102
- package/dist/transcriptRead.d.ts +56 -0
- package/dist/transcriptRead.js +121 -0
- package/dist/useComposer.d.ts +14 -5
- package/dist/useComposer.js +20 -7
- package/dist/useComposerImages.js +3 -4
- package/dist/useComposerPastes.js +3 -8
- package/dist/useConversation.d.ts +6 -0
- package/dist/useConversation.js +72 -5
- package/dist/useMounted.d.ts +3 -0
- package/dist/useMounted.js +14 -0
- package/dist/useSubagent.d.ts +2 -0
- package/dist/useSubagent.js +2 -0
- package/dist/wording.d.ts +5 -2
- package/dist/wording.js +6 -4
- package/locales/en/agentTranscript.json +2 -1
- package/locales/en/agents.json +14 -2
- package/locales/es/agentTranscript.json +2 -1
- package/locales/es/agents.json +14 -2
- package/locales/ja/agentTranscript.json +2 -1
- package/locales/ja/agents.json +14 -2
- package/locales/ko/agentTranscript.json +2 -1
- package/locales/ko/agents.json +14 -2
- package/locales/zh/agentTranscript.json +2 -1
- package/locales/zh/agents.json +14 -2
- package/package.json +2 -2
package/dist/transcript.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { runsInBackgroundTool } from "./commandsApi.js";
|
|
2
2
|
import { sessionImagePreviewUrl } from "./images.js";
|
|
3
|
+
import { pinnedTodos, todosFromHistory } from "./todos.js";
|
|
3
4
|
import { INTERRUPTED_NOTICE, applyPartDelta, askQuestionsFromArgs, classifyPseudoUserMessage, finalizeParts, firstNonEmptyLine, hiddenTranscriptTool, interruptionNotice, parseSubagentNotice, planFromArgs, resultFailed, subagentDeliveryLine, taskCallFromArgs } from "@parallelworks/ui/ai/headless";
|
|
4
5
|
import { DateTime } from "luxon";
|
|
5
6
|
// src/transcript.ts
|
|
@@ -96,16 +97,6 @@ function appendShellRun(state, command, output) {
|
|
|
96
97
|
messages: [...state.messages, assistantMessage([shellRunPart(command, output)])]
|
|
97
98
|
};
|
|
98
99
|
}
|
|
99
|
-
/** The daemon pairs results to calls positionally; the wire carries no ids. */
|
|
100
|
-
function completeOldestRunning(parts, result) {
|
|
101
|
-
const idx = parts.findIndex((p) => p.kind === "tool_call" && p.status === "running");
|
|
102
|
-
if (idx === -1) return parts;
|
|
103
|
-
return parts.map((p, i) => i === idx && p.kind === "tool_call" ? {
|
|
104
|
-
...p,
|
|
105
|
-
status: resultFailed(p.name, result) ? "error" : "ok",
|
|
106
|
-
result
|
|
107
|
-
} : p);
|
|
108
|
-
}
|
|
109
100
|
function approvalSubject(req) {
|
|
110
101
|
if (req.reason === "file is outside the workspace") return "path";
|
|
111
102
|
if (req.tool === "NotebookRun" || req.reason === "code in the notebook kernel") return "code";
|
|
@@ -188,26 +179,29 @@ function historyToMessages(history, options = {}) {
|
|
|
188
179
|
const lastSettledTurnEnd = history.findLastIndex((item) => item.kind === "turn_end" && !item.turnEnd?.pendingAgents);
|
|
189
180
|
const messages = [];
|
|
190
181
|
let current = null;
|
|
182
|
+
let parts = [];
|
|
183
|
+
const running = [];
|
|
184
|
+
const cards = /* @__PURE__ */ new Map();
|
|
191
185
|
const flush = (finalize) => {
|
|
192
186
|
if (current) {
|
|
193
|
-
|
|
187
|
+
messages.push({
|
|
194
188
|
...current,
|
|
195
|
-
parts: finalizeParts(
|
|
196
|
-
};
|
|
197
|
-
messages.push(current);
|
|
189
|
+
parts: finalize ? finalizeParts(parts) : parts
|
|
190
|
+
});
|
|
198
191
|
current = null;
|
|
199
192
|
}
|
|
200
193
|
};
|
|
201
194
|
const open = (index) => {
|
|
202
|
-
if (!current)
|
|
203
|
-
|
|
195
|
+
if (!current) {
|
|
196
|
+
current = assistantMessage([], `h-a-${index}`);
|
|
197
|
+
parts = [];
|
|
198
|
+
running.length = 0;
|
|
199
|
+
cards.clear();
|
|
200
|
+
}
|
|
204
201
|
};
|
|
205
202
|
const append = (part, index) => {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
...message,
|
|
209
|
-
parts: [...message.parts ?? [], part]
|
|
210
|
-
};
|
|
203
|
+
open(index);
|
|
204
|
+
return parts.push(part) - 1;
|
|
211
205
|
};
|
|
212
206
|
const ranCommand = (command, index, answer) => {
|
|
213
207
|
if (!command) {
|
|
@@ -229,18 +223,14 @@ function historyToMessages(history, options = {}) {
|
|
|
229
223
|
};
|
|
230
224
|
const pending = [];
|
|
231
225
|
const settle = (call, result) => {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
};
|
|
226
|
+
open(-1);
|
|
227
|
+
const at = call.kind === "tool" ? running.shift() : call.kind === "hidden" ? void 0 : cards.get(call.id);
|
|
228
|
+
const part = at === void 0 ? void 0 : parts[at];
|
|
229
|
+
if (at !== void 0 && part) parts[at] = settledPart(part, result);
|
|
237
230
|
};
|
|
238
231
|
const dropWaitingAsks = () => {
|
|
239
232
|
const waiting = new Set(pending.flatMap((call) => call.kind === "ask" ? [call.id] : []));
|
|
240
|
-
if (current && waiting.size > 0)
|
|
241
|
-
...current,
|
|
242
|
-
parts: (current.parts ?? []).filter((p) => !(p.kind === "approval" && waiting.has(p.id)))
|
|
243
|
-
};
|
|
233
|
+
if (current && waiting.size > 0) parts = parts.filter((p) => !(p.kind === "approval" && waiting.has(p.id)));
|
|
244
234
|
};
|
|
245
235
|
history.forEach((item, index) => {
|
|
246
236
|
switch (item.kind) {
|
|
@@ -278,13 +268,13 @@ function historyToMessages(history, options = {}) {
|
|
|
278
268
|
const id = `h-${index}`;
|
|
279
269
|
if (name === "Task") {
|
|
280
270
|
const { description, agentType } = taskCallFromArgs(args);
|
|
281
|
-
append({
|
|
271
|
+
cards.set(id, append({
|
|
282
272
|
kind: "subagent",
|
|
283
273
|
id,
|
|
284
274
|
status: "running",
|
|
285
275
|
description,
|
|
286
276
|
...agentType ? { agentType } : {}
|
|
287
|
-
}, index);
|
|
277
|
+
}, index));
|
|
288
278
|
pending.push({
|
|
289
279
|
kind: "subagent",
|
|
290
280
|
id
|
|
@@ -293,7 +283,7 @@ function historyToMessages(history, options = {}) {
|
|
|
293
283
|
}
|
|
294
284
|
if (name === "AskUserQuestion") {
|
|
295
285
|
const [card, call] = askCard(id, args, item.decision);
|
|
296
|
-
append(card, index);
|
|
286
|
+
cards.set(id, append(card, index));
|
|
297
287
|
pending.push(call);
|
|
298
288
|
return;
|
|
299
289
|
}
|
|
@@ -305,25 +295,12 @@ function historyToMessages(history, options = {}) {
|
|
|
305
295
|
pending.push({ kind: "hidden" });
|
|
306
296
|
return;
|
|
307
297
|
}
|
|
308
|
-
const todos = name === "TodoWrite" ? todosFromArgs(args) : null;
|
|
309
|
-
if (todos?.length) {
|
|
310
|
-
const message = open(index);
|
|
311
|
-
current = {
|
|
312
|
-
...message,
|
|
313
|
-
parts: applyPartDelta(message.parts ?? [], {
|
|
314
|
-
type: "todo_snapshot",
|
|
315
|
-
todos
|
|
316
|
-
})
|
|
317
|
-
};
|
|
318
|
-
pending.push({ kind: "hidden" });
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
298
|
if (hiddenTranscriptTool(name)) {
|
|
322
299
|
pending.push({ kind: "hidden" });
|
|
323
300
|
return;
|
|
324
301
|
}
|
|
325
302
|
const decision = item.decision && toolDecision(item.decision);
|
|
326
|
-
append({
|
|
303
|
+
running.push(append({
|
|
327
304
|
kind: "tool_call",
|
|
328
305
|
id: item.callId || id,
|
|
329
306
|
name,
|
|
@@ -332,7 +309,7 @@ function historyToMessages(history, options = {}) {
|
|
|
332
309
|
...item.startLine ? { startLine: item.startLine } : {},
|
|
333
310
|
...planFileEdit(name, args) ? { foldDiff: true } : {},
|
|
334
311
|
...decision ? { decision } : {}
|
|
335
|
-
}, index);
|
|
312
|
+
}, index));
|
|
336
313
|
pending.push({ kind: "tool" });
|
|
337
314
|
return;
|
|
338
315
|
}
|
|
@@ -449,27 +426,30 @@ function askCard(id, args, d) {
|
|
|
449
426
|
}, { kind: "hidden" }];
|
|
450
427
|
}
|
|
451
428
|
var BACKGROUND_STARTED_PREFIX = "Started background subagent";
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
case "
|
|
456
|
-
|
|
429
|
+
/** The block a call drew, as its result leaves it. */
|
|
430
|
+
function settledPart(part, result) {
|
|
431
|
+
switch (part.kind) {
|
|
432
|
+
case "tool_call": return {
|
|
433
|
+
...part,
|
|
434
|
+
status: resultFailed(part.name, result) ? "error" : "ok",
|
|
435
|
+
result
|
|
436
|
+
};
|
|
437
|
+
case "subagent":
|
|
457
438
|
if (result.startsWith(BACKGROUND_STARTED_PREFIX)) return {
|
|
458
|
-
...
|
|
439
|
+
...part,
|
|
459
440
|
background: true
|
|
460
441
|
};
|
|
461
442
|
return {
|
|
462
|
-
...
|
|
443
|
+
...part,
|
|
463
444
|
status: resultFailed("Task", result) ? "failed" : "done",
|
|
464
445
|
summary: firstNonEmptyLine(result, 60)
|
|
465
446
|
};
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
...p,
|
|
447
|
+
case "approval": return {
|
|
448
|
+
...part,
|
|
469
449
|
resolved: true,
|
|
470
450
|
answer: result.trim()
|
|
471
|
-
}
|
|
472
|
-
default: return
|
|
451
|
+
};
|
|
452
|
+
default: return part;
|
|
473
453
|
}
|
|
474
454
|
}
|
|
475
455
|
/** A session with no history resumes from the start: the daemon stamps a seq
|
|
@@ -514,7 +494,8 @@ function initialTranscript(detail, options = {}) {
|
|
|
514
494
|
turnStartedAt: running ? detail.turnStartedAt ?? null : null,
|
|
515
495
|
waitingForUsageLimit: running && !!detail.waitingForUsageLimit,
|
|
516
496
|
usageLimitResumeAt: running && detail.waitingForUsageLimit ? detail.usageLimitResumeAt ?? null : null,
|
|
517
|
-
seededSeq: detail.lastSeq ?? 0
|
|
497
|
+
seededSeq: detail.lastSeq ?? 0,
|
|
498
|
+
todos: todosFromHistory(detail.history ?? [])
|
|
518
499
|
};
|
|
519
500
|
}
|
|
520
501
|
/** A subagent's detail as the session detail its transcript seeds from. Its run
|
|
@@ -692,31 +673,6 @@ function interruptRunningCalls(parts) {
|
|
|
692
673
|
isError: false
|
|
693
674
|
}) : settled, parts);
|
|
694
675
|
}
|
|
695
|
-
var TODO_STATUSES = /* @__PURE__ */ new Set([
|
|
696
|
-
"pending",
|
|
697
|
-
"in_progress",
|
|
698
|
-
"completed"
|
|
699
|
-
]);
|
|
700
|
-
/** TodoWrite's whole list, which its arguments carry; null when they hold none. */
|
|
701
|
-
function todosFromArgs(argsJSON) {
|
|
702
|
-
let args;
|
|
703
|
-
try {
|
|
704
|
-
args = JSON.parse(argsJSON);
|
|
705
|
-
} catch {
|
|
706
|
-
return null;
|
|
707
|
-
}
|
|
708
|
-
const raw = args && typeof args === "object" ? args["todos"] : void 0;
|
|
709
|
-
if (!Array.isArray(raw)) return null;
|
|
710
|
-
return raw.flatMap((t) => {
|
|
711
|
-
const { content, status, activeForm } = t && typeof t === "object" ? t : {};
|
|
712
|
-
if (typeof content !== "string" || typeof status !== "string" || !TODO_STATUSES.has(status)) return [];
|
|
713
|
-
return [{
|
|
714
|
-
content,
|
|
715
|
-
status,
|
|
716
|
-
...typeof activeForm === "string" ? { activeForm } : {}
|
|
717
|
-
}];
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
676
|
function applyPartEvent(parts, ev, imageUrl, words) {
|
|
721
677
|
switch (ev.type) {
|
|
722
678
|
case "chunk": return applyPartDelta(parts, {
|
|
@@ -729,11 +685,6 @@ function applyPartEvent(parts, ev, imageUrl, words) {
|
|
|
729
685
|
});
|
|
730
686
|
case "retract": return applyPartDelta(parts, { type: "retract" });
|
|
731
687
|
case "tool_start": {
|
|
732
|
-
const todos = ev.name === "TodoWrite" ? todosFromArgs(ev.args ?? "") : null;
|
|
733
|
-
if (todos?.length) return applyPartDelta(parts, {
|
|
734
|
-
type: "todo_snapshot",
|
|
735
|
-
todos
|
|
736
|
-
});
|
|
737
688
|
if (hiddenTranscriptTool(ev.name ?? "")) return parts;
|
|
738
689
|
const id = ev.callId || String(ev.seq);
|
|
739
690
|
const started = applyPartDelta(parts, {
|
|
@@ -775,14 +726,21 @@ function applyPartEvent(parts, ev, imageUrl, words) {
|
|
|
775
726
|
images
|
|
776
727
|
}] : parts;
|
|
777
728
|
}
|
|
778
|
-
case "subagent_start":
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
729
|
+
case "subagent_start": {
|
|
730
|
+
const id = ev.id ?? String(ev.seq);
|
|
731
|
+
const started = applyPartDelta(parts, {
|
|
732
|
+
type: "subagent_start",
|
|
733
|
+
id,
|
|
734
|
+
...ev.agentType !== void 0 ? { agentType: ev.agentType } : {},
|
|
735
|
+
...ev.color !== void 0 ? { color: ev.color } : {},
|
|
736
|
+
...ev.background !== void 0 ? { background: ev.background } : {},
|
|
737
|
+
...ev.name !== void 0 ? { description: ev.name } : {}
|
|
738
|
+
});
|
|
739
|
+
return ev.turnId ? started : started.map((p) => p.kind === "subagent" && p.id === id ? {
|
|
740
|
+
...p,
|
|
741
|
+
detached: true
|
|
742
|
+
} : p);
|
|
743
|
+
}
|
|
786
744
|
case "subagent_action": return applyPartDelta(parts, {
|
|
787
745
|
type: "subagent_action",
|
|
788
746
|
id: ev.id ?? "",
|
|
@@ -1180,7 +1138,8 @@ function applyStreamEvent(state, ev, options = {}) {
|
|
|
1180
1138
|
turnStartedAt: null,
|
|
1181
1139
|
waitingForUsageLimit: false,
|
|
1182
1140
|
usageLimitResumeAt: null,
|
|
1183
|
-
cursor: ev.seq
|
|
1141
|
+
cursor: ev.seq,
|
|
1142
|
+
todos: []
|
|
1184
1143
|
},
|
|
1185
1144
|
effects: []
|
|
1186
1145
|
};
|
|
@@ -1217,10 +1176,12 @@ function applyStreamEvent(state, ev, options = {}) {
|
|
|
1217
1176
|
},
|
|
1218
1177
|
effects: []
|
|
1219
1178
|
};
|
|
1179
|
+
const todos = ev.type === "tool_start" && ev.name === "TodoWrite" ? pinnedTodos(ev.args ?? "") : null;
|
|
1220
1180
|
let next = {
|
|
1221
1181
|
...state,
|
|
1222
1182
|
cursor: ev.seq,
|
|
1223
|
-
...ev.turnEnd ? { turnEndSeq: ev.seq } : {}
|
|
1183
|
+
...ev.turnEnd ? { turnEndSeq: ev.seq } : {},
|
|
1184
|
+
...todos ? { todos } : {}
|
|
1224
1185
|
};
|
|
1225
1186
|
if (next.notes.length > 0 && speaks(ev)) next = placeNotes(next);
|
|
1226
1187
|
if (ev.type === "notice" && DISCARDED_QUEUE.test(ev.content ?? "")) next = {
|
|
@@ -1348,4 +1309,4 @@ function applyStreamEvent(state, ev, options = {}) {
|
|
|
1348
1309
|
effects: controlEffects(ev)
|
|
1349
1310
|
};
|
|
1350
1311
|
}
|
|
1351
|
-
export { TURN_VERBS, appendOwnMessage, appendShellRun, appendSteeredMessage, appendTurnMessage, applyStreamEvent, approvalAnswerBody, approvalToPart, bashRunning, dropMessage, historyToMessages, initialTranscript, liveSessionStatus, markQueued, pendingApprovals, pendingMessages, resolveApprovalEverywhere, resumeCursor, reviewingCount, subagentFrame, subagentSessionDetail,
|
|
1312
|
+
export { TURN_VERBS, appendOwnMessage, appendShellRun, appendSteeredMessage, appendTurnMessage, applyStreamEvent, approvalAnswerBody, approvalToPart, bashRunning, dropMessage, historyToMessages, initialTranscript, liveSessionStatus, markQueued, pendingApprovals, pendingMessages, resolveApprovalEverywhere, resumeCursor, reviewingCount, subagentFrame, subagentSessionDetail, toolDecision, turnRunning, turnVerb, userInterruptedTool };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { PwCodeClient, Schemas } from './apiClient.js';
|
|
2
|
+
type SessionDetail = Schemas['AgentSessionDetail'];
|
|
3
|
+
/** Reads a transcript answered tail first and resolves to its text. `onTail` is
|
|
4
|
+
* handed the tail once it has arrived, if the rest is still on its way a
|
|
5
|
+
* moment later. */
|
|
6
|
+
export declare function readTailFirst(stream: ReadableStream<Uint8Array>, onTail: (tail: SessionDetail) => void): Promise<string>;
|
|
7
|
+
/** Reads sessions' transcripts so the end of one can be shown while the rest
|
|
8
|
+
* of it arrives, which over a slow connection is most of the wait. */
|
|
9
|
+
export declare function createTranscriptReads(client: PwCodeClient, previewsByRef: boolean): {
|
|
10
|
+
/** The end of the transcript being read under `name`, until the read ends. */
|
|
11
|
+
tail: (name: string) => {
|
|
12
|
+
addDirs?: import("./api.js").components["schemas"]["AddedDirectory"][] | null;
|
|
13
|
+
allocation?: string;
|
|
14
|
+
allowedDirs?: import("./api.js").components["schemas"]["AllowedDirectory"][] | null;
|
|
15
|
+
approvals?: import("./api.js").components["schemas"]["ApprovalRequest"][] | null;
|
|
16
|
+
archivedAt?: string;
|
|
17
|
+
autoName?: string;
|
|
18
|
+
branch?: string;
|
|
19
|
+
claudeAccount?: string;
|
|
20
|
+
createdAt: string;
|
|
21
|
+
effort?: string;
|
|
22
|
+
goal?: import("./api.js").components["schemas"]["GoalStatus"];
|
|
23
|
+
group?: string;
|
|
24
|
+
history: import("./api.js").components["schemas"]["HistoryItem"][] | null;
|
|
25
|
+
id: string;
|
|
26
|
+
inputTokens?: number;
|
|
27
|
+
lastError?: string;
|
|
28
|
+
lastSeq?: number;
|
|
29
|
+
liveHost?: string;
|
|
30
|
+
machineId: string;
|
|
31
|
+
messages: number;
|
|
32
|
+
model: string;
|
|
33
|
+
name?: string;
|
|
34
|
+
outputTokens?: number;
|
|
35
|
+
permissionMode?: string;
|
|
36
|
+
pinnedAt?: string;
|
|
37
|
+
preview: string;
|
|
38
|
+
pullRequestStates?: {
|
|
39
|
+
[key: string]: string;
|
|
40
|
+
};
|
|
41
|
+
pullRequests?: string[] | null;
|
|
42
|
+
remoteControl?: boolean;
|
|
43
|
+
status: "idle" | "running" | "awaiting_approval" | "live_elsewhere";
|
|
44
|
+
turnId?: string;
|
|
45
|
+
turnStartedAt?: string;
|
|
46
|
+
updatedAt: string;
|
|
47
|
+
usageLimitResumeAt?: string;
|
|
48
|
+
waitingForUsageLimit?: boolean;
|
|
49
|
+
workspace: string;
|
|
50
|
+
workspaceUntrusted?: boolean;
|
|
51
|
+
} | undefined;
|
|
52
|
+
subscribe: (listener: () => void) => () => void;
|
|
53
|
+
/** The session, whole. `name` is what its end is kept under meanwhile. */
|
|
54
|
+
read(machine: string, id: string, name: string, signal: AbortSignal | undefined): Promise<SessionDetail>;
|
|
55
|
+
};
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { PREVIEWS_BY_REF } from "./images.js";
|
|
2
|
+
// src/transcriptRead.ts
|
|
3
|
+
/** About how many history items the end of a transcript holds. */
|
|
4
|
+
var TAIL_ITEMS = 200;
|
|
5
|
+
/** How long the rest of a transcript may take before its end is shown without
|
|
6
|
+
* it: one that arrives whole is drawn once. */
|
|
7
|
+
var TAIL_GRACE_MS = 80;
|
|
8
|
+
var OLDER = ",\"older\":[";
|
|
9
|
+
function tailBefore(text, at) {
|
|
10
|
+
try {
|
|
11
|
+
return JSON.parse(`${text.slice(0, at)}}`).tail;
|
|
12
|
+
} catch {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** Reads a transcript answered tail first and resolves to its text. `onTail` is
|
|
17
|
+
* handed the tail once it has arrived, if the rest is still on its way a
|
|
18
|
+
* moment later. */
|
|
19
|
+
async function readTailFirst(stream, onTail) {
|
|
20
|
+
const reader = stream.getReader();
|
|
21
|
+
const decoder = new TextDecoder();
|
|
22
|
+
let text = "";
|
|
23
|
+
let from = 0;
|
|
24
|
+
let found = false;
|
|
25
|
+
let grace;
|
|
26
|
+
try {
|
|
27
|
+
for (;;) {
|
|
28
|
+
const { done, value } = await reader.read();
|
|
29
|
+
if (done) break;
|
|
30
|
+
text += decoder.decode(value, { stream: true });
|
|
31
|
+
while (!found) {
|
|
32
|
+
const at = text.indexOf(OLDER, from);
|
|
33
|
+
if (at < 0) {
|
|
34
|
+
from = Math.max(0, text.length - 10);
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
const tail = tailBefore(text, at);
|
|
38
|
+
if (tail) {
|
|
39
|
+
found = true;
|
|
40
|
+
grace = setTimeout(() => onTail(tail), TAIL_GRACE_MS);
|
|
41
|
+
} else from = at + 1;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
} finally {
|
|
45
|
+
clearTimeout(grace);
|
|
46
|
+
}
|
|
47
|
+
return text + decoder.decode();
|
|
48
|
+
}
|
|
49
|
+
/** Reads sessions' transcripts so the end of one can be shown while the rest
|
|
50
|
+
* of it arrives, which over a slow connection is most of the wait. */
|
|
51
|
+
function createTranscriptReads(client, previewsByRef) {
|
|
52
|
+
const tails = /* @__PURE__ */ new Map();
|
|
53
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
54
|
+
const tell = () => {
|
|
55
|
+
for (const listener of listeners) listener();
|
|
56
|
+
};
|
|
57
|
+
const query = previewsByRef ? { previews: PREVIEWS_BY_REF } : {};
|
|
58
|
+
const readWhole = async (machine, id, aborts) => {
|
|
59
|
+
const { data, error } = await client.GET("/api/code/machines/{machine}/sessions/{id}", {
|
|
60
|
+
params: {
|
|
61
|
+
path: {
|
|
62
|
+
machine,
|
|
63
|
+
id
|
|
64
|
+
},
|
|
65
|
+
...previewsByRef ? { query } : {}
|
|
66
|
+
},
|
|
67
|
+
...aborts
|
|
68
|
+
});
|
|
69
|
+
if (error !== void 0) throw error;
|
|
70
|
+
return data;
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
/** The end of the transcript being read under `name`, until the read ends. */
|
|
74
|
+
tail: (name) => tails.get(name),
|
|
75
|
+
subscribe: (listener) => {
|
|
76
|
+
listeners.add(listener);
|
|
77
|
+
return () => {
|
|
78
|
+
listeners.delete(listener);
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
/** The session, whole. `name` is what its end is kept under meanwhile. */
|
|
82
|
+
async read(machine, id, name, signal) {
|
|
83
|
+
const aborts = signal ? { signal } : {};
|
|
84
|
+
const { data, error, response } = await client.GET("/api/code/machines/{machine}/sessions/{id}/transcript", {
|
|
85
|
+
params: {
|
|
86
|
+
path: {
|
|
87
|
+
machine,
|
|
88
|
+
id
|
|
89
|
+
},
|
|
90
|
+
query: {
|
|
91
|
+
...query,
|
|
92
|
+
tail: TAIL_ITEMS
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
parseAs: "stream",
|
|
96
|
+
...aborts
|
|
97
|
+
});
|
|
98
|
+
if (error !== void 0) {
|
|
99
|
+
if (response.status !== 404 && response.status !== 405) throw error;
|
|
100
|
+
return readWhole(machine, id, aborts);
|
|
101
|
+
}
|
|
102
|
+
if (!data) return readWhole(machine, id, aborts);
|
|
103
|
+
try {
|
|
104
|
+
const text = await readTailFirst(data, (tail) => {
|
|
105
|
+
tails.set(name, tail);
|
|
106
|
+
tell();
|
|
107
|
+
});
|
|
108
|
+
const { tail, older } = JSON.parse(text);
|
|
109
|
+
return {
|
|
110
|
+
...tail,
|
|
111
|
+
history: [...older ?? [], ...tail.history ?? []]
|
|
112
|
+
};
|
|
113
|
+
} finally {
|
|
114
|
+
setTimeout(() => {
|
|
115
|
+
if (tails.delete(name)) tell();
|
|
116
|
+
}, 0);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export { createTranscriptReads, readTailFirst };
|
package/dist/useComposer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { MessagePaste } from '@parallelworks/ui/ai/headless';
|
|
1
|
+
import type { ChatMessage, MessagePaste } from '@parallelworks/ui/ai/headless';
|
|
2
2
|
import type { Schemas } from './apiClient.js';
|
|
3
3
|
import type { PwCodeApi } from './createPwCodeApi.js';
|
|
4
4
|
import type { PwCodeConversation } from './useConversation.js';
|
|
@@ -8,12 +8,21 @@ import { type PwCodeSavedImage, type PwCodeTakenImages } from './useComposerImag
|
|
|
8
8
|
export interface PwCodeComposerHistory {
|
|
9
9
|
/** Composers with the same name share one history. */
|
|
10
10
|
name: string;
|
|
11
|
+
/** What was sent before this tab, oldest first. */
|
|
12
|
+
seed?: readonly string[] | undefined;
|
|
11
13
|
/** Text to forget, such as a command carrying a secret. */
|
|
12
14
|
skip?: ((text: string) => boolean) | undefined;
|
|
13
15
|
}
|
|
16
|
+
/** What the reader typed into a conversation, oldest first: not the recorded
|
|
17
|
+
* commands and `!` runs, nor what the main session told a subagent. */
|
|
18
|
+
export declare function typedPrompts(messages: readonly ChatMessage[]): string[];
|
|
14
19
|
/** Per person, since someone else can sign in where the history is kept, and
|
|
15
|
-
*
|
|
16
|
-
|
|
20
|
+
* per conversation, such as a session or one of its subagents, so ↑ in one
|
|
21
|
+
* recalls only what was sent to it. Without `/auth`, which can carry a key. */
|
|
22
|
+
export declare function promptHistory(username: string, conversation?: {
|
|
23
|
+
id: string;
|
|
24
|
+
messages: readonly ChatMessage[] | undefined;
|
|
25
|
+
}): PwCodeComposerHistory;
|
|
17
26
|
/** What a command printed, shown below the transcript under the command as typed. */
|
|
18
27
|
export interface PwCodeCommandOutput {
|
|
19
28
|
id: number;
|
|
@@ -57,9 +66,9 @@ export declare function usePwCodeComposer({ api, machine, sessionId, conversatio
|
|
|
57
66
|
handleLocally?: ((typed: string) => boolean) | undefined;
|
|
58
67
|
/** Tells the reader something in passing, such as in a toast. */
|
|
59
68
|
notify: (text: string) => void;
|
|
60
|
-
/** Shows the session `/clear` started in place of this one. */
|
|
69
|
+
/** Shows the session `/clear` started in place of this one, while this screen is still open. */
|
|
61
70
|
onCleared: (newId: string) => void;
|
|
62
|
-
/** Leaves the session `/archive` archived. */
|
|
71
|
+
/** Leaves the session `/archive` archived, while this screen is still open. */
|
|
63
72
|
onArchived: () => void;
|
|
64
73
|
}): {
|
|
65
74
|
/** Sends what was typed; false hands it back to the composer. */
|
package/dist/useComposer.js
CHANGED
|
@@ -3,14 +3,22 @@ import { isMachineDown } from "./machine.js";
|
|
|
3
3
|
import { usePwCodeTranslations } from "./messages.js";
|
|
4
4
|
import { bashRunning, liveSessionStatus, pendingApprovals, turnRunning } from "./transcript.js";
|
|
5
5
|
import { withoutAppendedImages } from "./useComposerImages.js";
|
|
6
|
+
import { useMounted } from "./useMounted.js";
|
|
6
7
|
import { composerState, sessionLabel } from "./wording.js";
|
|
7
8
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
8
9
|
// src/useComposer.ts
|
|
10
|
+
/** What the reader typed into a conversation, oldest first: not the recorded
|
|
11
|
+
* commands and `!` runs, nor what the main session told a subagent. */
|
|
12
|
+
function typedPrompts(messages) {
|
|
13
|
+
return messages.flatMap((m) => m.role === "user" && m.content && !m.pseudo && !m.commandAnswer && !m.author ? [m.content] : []);
|
|
14
|
+
}
|
|
9
15
|
/** Per person, since someone else can sign in where the history is kept, and
|
|
10
|
-
*
|
|
11
|
-
|
|
16
|
+
* per conversation, such as a session or one of its subagents, so ↑ in one
|
|
17
|
+
* recalls only what was sent to it. Without `/auth`, which can carry a key. */
|
|
18
|
+
function promptHistory(username, conversation) {
|
|
12
19
|
return {
|
|
13
|
-
name: `agents:${username}`,
|
|
20
|
+
name: conversation ? `agents:${username}:${conversation.id}` : `agents:${username}`,
|
|
21
|
+
seed: conversation?.messages && typedPrompts(conversation.messages),
|
|
14
22
|
skip: (text) => /^\/auth(\s|$)/.test(text)
|
|
15
23
|
};
|
|
16
24
|
}
|
|
@@ -65,6 +73,7 @@ function usePwCodeComposer({ api, machine, sessionId, conversation, machineInfo,
|
|
|
65
73
|
warn
|
|
66
74
|
]);
|
|
67
75
|
const { archiveSession, archiveAfterTurn, stopSession } = api.useSessionMutations();
|
|
76
|
+
const mounted = useMounted();
|
|
68
77
|
/** Resolves to the machine's refusal. A session archived once its turn ends stays open, and the machine says so in the transcript. */
|
|
69
78
|
const archiveTyped = useCallback(async (s, waits) => {
|
|
70
79
|
const { refusal, deferred } = waits ? await archiveAfterTurn(s) : {
|
|
@@ -73,12 +82,13 @@ function usePwCodeComposer({ api, machine, sessionId, conversation, machineInfo,
|
|
|
73
82
|
};
|
|
74
83
|
if (!refusal && !deferred) {
|
|
75
84
|
notify(t("detail.archived", { name: sessionLabel(s, t) }));
|
|
76
|
-
onArchived();
|
|
85
|
+
if (mounted.current) onArchived();
|
|
77
86
|
}
|
|
78
87
|
return refusal;
|
|
79
88
|
}, [
|
|
80
89
|
archiveAfterTurn,
|
|
81
90
|
archiveSession,
|
|
91
|
+
mounted,
|
|
82
92
|
notify,
|
|
83
93
|
onArchived,
|
|
84
94
|
t
|
|
@@ -106,7 +116,9 @@ function usePwCodeComposer({ api, machine, sessionId, conversation, machineInfo,
|
|
|
106
116
|
onOutput: commandOutputs.add,
|
|
107
117
|
onShellRun,
|
|
108
118
|
onCompacted: () => void refetch(),
|
|
109
|
-
onCleared
|
|
119
|
+
onCleared: (newId) => {
|
|
120
|
+
if (mounted.current) onCleared(newId);
|
|
121
|
+
},
|
|
110
122
|
onArchive: async () => {
|
|
111
123
|
const waits = machineInfo?.archiveAfterTurn === true;
|
|
112
124
|
if (running && !waits) {
|
|
@@ -152,6 +164,7 @@ function usePwCodeComposer({ api, machine, sessionId, conversation, machineInfo,
|
|
|
152
164
|
loadPalette,
|
|
153
165
|
machine,
|
|
154
166
|
machineInfo?.archiveAfterTurn,
|
|
167
|
+
mounted,
|
|
155
168
|
onCleared,
|
|
156
169
|
onShellRun,
|
|
157
170
|
palette,
|
|
@@ -241,7 +254,7 @@ function usePwCodeComposer({ api, machine, sessionId, conversation, machineInfo,
|
|
|
241
254
|
},
|
|
242
255
|
composer: {
|
|
243
256
|
...state,
|
|
244
|
-
disabled: sending || !!images?.taking || state.locked
|
|
257
|
+
disabled: sending || !!images?.taking || state.locked || conversation.endOnly
|
|
245
258
|
},
|
|
246
259
|
/** The composer's `recall`: ↑ in it, or Edit under the queue, takes back what waits. */
|
|
247
260
|
recall: {
|
|
@@ -292,4 +305,4 @@ async function stopTurn(composer, takeBack) {
|
|
|
292
305
|
await composer.turn.stop();
|
|
293
306
|
return failure;
|
|
294
307
|
}
|
|
295
|
-
export { promptHistory, stopTurn, useCommandOutputs, usePwCodeComposer };
|
|
308
|
+
export { promptHistory, stopTurn, typedPrompts, useCommandOutputs, usePwCodeComposer };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { usePwCodeTranslations } from "./messages.js";
|
|
2
|
+
import { useMounted } from "./useMounted.js";
|
|
2
3
|
import { IMAGE_PLACEHOLDER } from "./wording.js";
|
|
3
4
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
4
5
|
// src/useComposerImages.ts
|
|
@@ -80,11 +81,9 @@ function useComposerImages({ api, machine, sessionId, messages, onError, describ
|
|
|
80
81
|
}
|
|
81
82
|
};
|
|
82
83
|
}, []);
|
|
83
|
-
const mounted =
|
|
84
|
+
const mounted = useMounted();
|
|
84
85
|
useEffect(() => {
|
|
85
|
-
mounted.current = true;
|
|
86
86
|
return () => {
|
|
87
|
-
mounted.current = false;
|
|
88
87
|
for (const image of imagesRef.current) releasePreview(image.previewUrl);
|
|
89
88
|
};
|
|
90
89
|
}, [releasePreview]);
|
|
@@ -196,7 +195,7 @@ function useComposerImages({ api, machine, sessionId, messages, onError, describ
|
|
|
196
195
|
}
|
|
197
196
|
}
|
|
198
197
|
};
|
|
199
|
-
}, [releasePreview]),
|
|
198
|
+
}, [mounted, releasePreview]),
|
|
200
199
|
restore: useCallback((saved) => {
|
|
201
200
|
if (saved.length === 0) return;
|
|
202
201
|
lastSeq.current = nextImageSeq(saved.map((s) => s.ref.placeholder), lastSeq.current) - 1;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { useMounted } from "./useMounted.js";
|
|
1
2
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
3
|
import { createPasteCards, joinPastes, pasteInlineMaxBytes } from "@parallelworks/ui/ai/headless";
|
|
3
4
|
import { formatBytes } from "@parallelworks/ui/headless";
|
|
@@ -84,13 +85,7 @@ function usePasteCards({ pastes, input, setInput, composer }) {
|
|
|
84
85
|
useEffect(() => {
|
|
85
86
|
if (inlineMax !== void 0) controller.applyInlineLimit(inlineMax);
|
|
86
87
|
}, [inlineMax, controller]);
|
|
87
|
-
const mounted =
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
mounted.current = true;
|
|
90
|
-
return () => {
|
|
91
|
-
mounted.current = false;
|
|
92
|
-
};
|
|
93
|
-
}, []);
|
|
88
|
+
const mounted = useMounted();
|
|
94
89
|
const send = useCallback(() => {
|
|
95
90
|
const { input: sent, composer: from, cards: taken } = latest.current;
|
|
96
91
|
if (taken.some((c) => !c.paste)) return null;
|
|
@@ -110,7 +105,7 @@ function usePasteCards({ pastes, input, setInput, composer }) {
|
|
|
110
105
|
return true;
|
|
111
106
|
}
|
|
112
107
|
};
|
|
113
|
-
}, [controller]);
|
|
108
|
+
}, [controller, mounted]);
|
|
114
109
|
return {
|
|
115
110
|
cards,
|
|
116
111
|
blocking: cards.some((c) => !c.paste),
|