@keyou007/agent-bot 0.1.27-alpha.3 → 0.1.27-alpha.5
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 +32 -0
- package/README.md +102 -18
- package/README.zh.md +102 -18
- package/assets/agent-bot.ico +0 -0
- package/assets/roboto-mono-OFL.txt +93 -0
- package/assets/roboto-mono-latin.woff2 +0 -0
- package/assets/workspace-drafts.js +88 -0
- package/assets/workspace-sessions.js +2676 -243
- package/assets/workspace.css +553 -44
- package/assets/workspace.js +3429 -327
- package/dist/cli/SelfUpdateObserver.js +124 -0
- package/dist/cli/SelfUpdateObserver.js.map +1 -0
- package/dist/cli/SelfUpdateRunner.js +267 -70
- package/dist/cli/SelfUpdateRunner.js.map +1 -1
- package/dist/cli/SelfUpdater.js +54 -15
- package/dist/cli/SelfUpdater.js.map +1 -1
- package/dist/cli/controlProtocol.js.map +1 -1
- package/dist/cli/help.js +6 -0
- package/dist/cli/help.js.map +1 -1
- package/dist/cli.js +85 -6
- package/dist/cli.js.map +1 -1
- package/dist/codex/CodexEventMapper.js +29 -5
- package/dist/codex/CodexEventMapper.js.map +1 -1
- package/dist/codex/CodexRuntime.js +3 -0
- package/dist/codex/CodexRuntime.js.map +1 -1
- package/dist/feishu/CardRenderer.js +45 -13
- package/dist/feishu/CardRenderer.js.map +1 -1
- package/dist/feishu/ConsoleFeishuClient.js +4 -0
- package/dist/feishu/ConsoleFeishuClient.js.map +1 -1
- package/dist/feishu/FeishuMarkdown.js +2 -14
- package/dist/feishu/FeishuMarkdown.js.map +1 -1
- package/dist/feishu/FeishuMessageClient.js +9 -0
- package/dist/feishu/FeishuMessageClient.js.map +1 -1
- package/dist/feishu/FeishuTurnPresenter.js +51 -22
- package/dist/feishu/FeishuTurnPresenter.js.map +1 -1
- package/dist/index.js +47 -12
- package/dist/index.js.map +1 -1
- package/dist/local-files/DiagramPreview.js +11 -6
- package/dist/local-files/DiagramPreview.js.map +1 -1
- package/dist/local-files/GitDiffPreview.js +106 -14
- package/dist/local-files/GitDiffPreview.js.map +1 -1
- package/dist/local-files/LiveMarkup.js +104 -0
- package/dist/local-files/LiveMarkup.js.map +1 -0
- package/dist/local-files/LocalFileViewerServer.js +141 -122
- package/dist/local-files/LocalFileViewerServer.js.map +1 -1
- package/dist/local-files/MarkdownExport.js +54 -0
- package/dist/local-files/MarkdownExport.js.map +1 -0
- package/dist/local-files/PreviewCodeFont.js +11 -0
- package/dist/local-files/PreviewCodeFont.js.map +1 -0
- package/dist/local-files/PreviewSyntaxHighlight.js +73 -0
- package/dist/local-files/PreviewSyntaxHighlight.js.map +1 -1
- package/dist/local-files/ToolCommandDisplay.js +111 -0
- package/dist/local-files/ToolCommandDisplay.js.map +1 -0
- package/dist/local-files/TurnPreviewPage.js +53 -276
- package/dist/local-files/TurnPreviewPage.js.map +1 -1
- package/dist/local-files/WorkspaceFileEvents.js +206 -0
- package/dist/local-files/WorkspaceFileEvents.js.map +1 -0
- package/dist/local-files/WorkspaceFiles.js +79 -3
- package/dist/local-files/WorkspaceFiles.js.map +1 -1
- package/dist/local-files/WorkspaceImages.js +43 -0
- package/dist/local-files/WorkspaceImages.js.map +1 -0
- package/dist/local-files/WorkspacePage.js +24 -6
- package/dist/local-files/WorkspacePage.js.map +1 -1
- package/dist/local-files/WorkspaceSessions.js +1153 -104
- package/dist/local-files/WorkspaceSessions.js.map +1 -1
- package/dist/local-files/WorkspaceTimeline.js +129 -0
- package/dist/local-files/WorkspaceTimeline.js.map +1 -0
- package/dist/presentation/OutboundRouter.js +15 -6
- package/dist/presentation/OutboundRouter.js.map +1 -1
- package/dist/presentation/TurnStateReducer.js +31 -22
- package/dist/presentation/TurnStateReducer.js.map +1 -1
- package/dist/proxy/ProxySessionController.js +335 -113
- package/dist/proxy/ProxySessionController.js.map +1 -1
- package/dist/proxy/TaskMetadata.js +86 -0
- package/dist/proxy/TaskMetadata.js.map +1 -0
- package/dist/scheduling/ScheduleCards.js +10 -4
- package/dist/scheduling/ScheduleCards.js.map +1 -1
- package/dist/scheduling/ScheduleCommand.js +4 -0
- package/dist/scheduling/ScheduleCommand.js.map +1 -1
- package/dist/scheduling/ScheduledTaskStore.js +53 -12
- package/dist/scheduling/ScheduledTaskStore.js.map +1 -1
- package/dist/scheduling/types.js +19 -4
- package/dist/scheduling/types.js.map +1 -1
- package/dist/state/StateStore.js +213 -23
- package/dist/state/StateStore.js.map +1 -1
- package/dist/state/TurnPreviewJournal.js +157 -39
- package/dist/state/TurnPreviewJournal.js.map +1 -1
- package/dist/state/TurnToolText.js +172 -0
- package/dist/state/TurnToolText.js.map +1 -0
- package/dist/state/migrations.js +4 -0
- package/dist/state/migrations.js.map +1 -1
- package/dist/supervision/SafeRestartNotifier.js +9 -6
- package/dist/supervision/SafeRestartNotifier.js.map +1 -1
- package/dist/terminal/TerminalPage.js +5 -0
- package/dist/terminal/TerminalPage.js.map +1 -1
- package/dist/updates/AutomaticUpdatePreparer.js +63 -12
- package/dist/updates/AutomaticUpdatePreparer.js.map +1 -1
- package/dist/updates/ManualUpdateController.js +7 -2
- package/dist/updates/ManualUpdateController.js.map +1 -1
- package/dist/utils/promptAttachments.js +22 -0
- package/dist/utils/promptAttachments.js.map +1 -0
- package/dist/utils/promptQuotes.js +47 -0
- package/dist/utils/promptQuotes.js.map +1 -0
- package/dist/utils/toolTimestamp.js +6 -0
- package/dist/utils/toolTimestamp.js.map +1 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/skills/agent-bot/SKILL.md +23 -2
- package/src/cli/SelfUpdateObserver.ts +111 -0
- package/src/cli/SelfUpdateRunner.ts +271 -85
- package/src/cli/SelfUpdater.ts +53 -16
- package/src/cli/controlProtocol.ts +1 -0
- package/src/cli/help.ts +6 -0
- package/src/cli.ts +71 -5
- package/src/codex/CodexEventMapper.ts +25 -5
- package/src/codex/CodexRuntime.ts +4 -0
- package/src/feishu/CardRenderer.ts +48 -13
- package/src/feishu/ConsoleFeishuClient.ts +6 -1
- package/src/feishu/FeishuMarkdown.ts +2 -14
- package/src/feishu/FeishuMessageClient.ts +10 -0
- package/src/feishu/FeishuTurnPresenter.ts +49 -16
- package/src/feishu/types.ts +1 -0
- package/src/index.ts +47 -12
- package/src/local-files/DiagramPreview.ts +11 -6
- package/src/local-files/GitDiffPreview.ts +107 -15
- package/src/local-files/LiveMarkup.ts +103 -0
- package/src/local-files/LocalFileViewerServer.ts +128 -125
- package/src/local-files/MarkdownExport.ts +54 -0
- package/src/local-files/PreviewCodeFont.ts +12 -0
- package/src/local-files/PreviewSyntaxHighlight.ts +75 -0
- package/src/local-files/ToolCommandDisplay.ts +104 -0
- package/src/local-files/TurnPreviewPage.ts +58 -261
- package/src/local-files/WorkspaceFileEvents.ts +186 -0
- package/src/local-files/WorkspaceFiles.ts +72 -5
- package/src/local-files/WorkspaceImages.ts +35 -0
- package/src/local-files/WorkspacePage.ts +24 -6
- package/src/local-files/WorkspaceSessions.ts +1057 -111
- package/src/local-files/WorkspaceTimeline.ts +135 -0
- package/src/presentation/OutboundRouter.ts +18 -4
- package/src/presentation/TurnStateReducer.ts +30 -19
- package/src/presentation/turnViewTypes.ts +3 -1
- package/src/proxy/ProxySessionController.ts +336 -96
- package/src/proxy/TaskMetadata.ts +81 -0
- package/src/runtime/types.ts +2 -1
- package/src/scheduling/ScheduleCards.ts +10 -4
- package/src/scheduling/ScheduleCommand.ts +4 -0
- package/src/scheduling/ScheduledTaskStore.ts +56 -9
- package/src/scheduling/types.ts +27 -4
- package/src/state/StateStore.ts +227 -24
- package/src/state/TurnPreviewJournal.ts +155 -38
- package/src/state/TurnToolText.ts +156 -0
- package/src/state/migrations.ts +4 -0
- package/src/supervision/SafeRestartNotifier.ts +9 -6
- package/src/terminal/TerminalPage.ts +5 -0
- package/src/updates/AutomaticUpdatePreparer.ts +52 -11
- package/src/updates/ManualUpdateController.ts +8 -3
- package/src/utils/promptAttachments.ts +27 -0
- package/src/utils/promptQuotes.ts +48 -0
- package/src/utils/toolTimestamp.ts +5 -0
package/dist/state/StateStore.js
CHANGED
|
@@ -7,6 +7,7 @@ import { ScheduledTaskStore } from "../scheduling/ScheduledTaskStore.js";
|
|
|
7
7
|
import { migrations } from "./migrations.js";
|
|
8
8
|
import { ExecutionProvenanceStore } from "./ExecutionProvenanceStore.js";
|
|
9
9
|
import { InboundMessageStore } from "./InboundMessageStore.js";
|
|
10
|
+
import { previewToolHeading } from "../local-files/ToolCommandDisplay.js";
|
|
10
11
|
// Scheduled queue entries survive a safe restart, but an in-flight submission must block it.
|
|
11
12
|
const RESTART_BLOCKING_WORK_SQL = `sessions.status = 'running'
|
|
12
13
|
OR EXISTS (
|
|
@@ -19,6 +20,7 @@ const RESTART_BLOCKING_WORK_SQL = `sessions.status = 'running'
|
|
|
19
20
|
)`;
|
|
20
21
|
export class StateStore {
|
|
21
22
|
db;
|
|
23
|
+
queuedPromptDispatches = new Map();
|
|
22
24
|
previews;
|
|
23
25
|
schedules;
|
|
24
26
|
provenance;
|
|
@@ -27,6 +29,10 @@ export class StateStore {
|
|
|
27
29
|
this.previews = new TurnPreviewJournal(path.join(path.dirname(sqlitePath), "turn-previews"), onPreviewError);
|
|
28
30
|
fs.mkdirSync(path.dirname(sqlitePath), { recursive: true });
|
|
29
31
|
this.db = new Database(sqlitePath);
|
|
32
|
+
this.db.function("workspace_tool_heading", { deterministic: true }, (title, kind, command) => {
|
|
33
|
+
const heading = previewToolHeading({ title: title ?? "", kind: kind ?? "工具", command: command ?? undefined }, "命令");
|
|
34
|
+
return JSON.stringify({ ...heading, title: heading.title.slice(0, 240) });
|
|
35
|
+
});
|
|
30
36
|
this.db.pragma("journal_mode = WAL");
|
|
31
37
|
for (const migration of migrations) {
|
|
32
38
|
this.db.exec(migration);
|
|
@@ -310,6 +316,29 @@ export class StateStore {
|
|
|
310
316
|
`).all(localSessionId);
|
|
311
317
|
return rows.map(mapQueuedPrompt);
|
|
312
318
|
}
|
|
319
|
+
// Keep the claimed prompt readable in Workspace while its chat entry is being published.
|
|
320
|
+
trackQueuedPromptDispatch(prompt) {
|
|
321
|
+
this.queuedPromptDispatches.set(prompt.promptId, prompt);
|
|
322
|
+
return () => {
|
|
323
|
+
if (this.queuedPromptDispatches.get(prompt.promptId) === prompt)
|
|
324
|
+
this.queuedPromptDispatches.delete(prompt.promptId);
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
listQueuedPromptDispatches(localSessionId) {
|
|
328
|
+
return [...this.queuedPromptDispatches.values()].filter(prompt => prompt.localSessionId === localSessionId);
|
|
329
|
+
}
|
|
330
|
+
getQueuedPrompt(localSessionId, promptId) {
|
|
331
|
+
const row = this.db.prepare("SELECT * FROM queued_prompts WHERE local_session_id = ? AND prompt_id = ?")
|
|
332
|
+
.get(localSessionId, promptId);
|
|
333
|
+
return row ? mapQueuedPrompt(row) : undefined;
|
|
334
|
+
}
|
|
335
|
+
updateQueuedPrompt(previous, content) {
|
|
336
|
+
const updated = this.db.prepare(`
|
|
337
|
+
UPDATE queued_prompts SET prompt_text = ?, display_prompt = ?
|
|
338
|
+
WHERE local_session_id = ? AND prompt_id = ? AND prompt_text = ? AND display_prompt IS ?
|
|
339
|
+
`).run(content.text, content.displayPrompt ?? null, previous.localSessionId, previous.promptId, previous.text, previous.displayPrompt ?? null);
|
|
340
|
+
return updated.changes === 1;
|
|
341
|
+
}
|
|
313
342
|
hasQueuedPrompt(promptId) {
|
|
314
343
|
return Boolean(this.db.prepare("SELECT 1 FROM queued_prompts WHERE prompt_id = ?").get(promptId));
|
|
315
344
|
}
|
|
@@ -347,6 +376,7 @@ export class StateStore {
|
|
|
347
376
|
this.db.prepare("DELETE FROM queued_prompts WHERE prompt_id = ?").run(row.prompt_id);
|
|
348
377
|
if (!this.schedules.claimStart(row.prompt_id))
|
|
349
378
|
continue;
|
|
379
|
+
const prompt = mapQueuedPrompt(row);
|
|
350
380
|
if (this.schedules.getRun(row.prompt_id)) {
|
|
351
381
|
const existing = this.getTurnAttempt(row.prompt_id);
|
|
352
382
|
if (existing) {
|
|
@@ -359,14 +389,47 @@ export class StateStore {
|
|
|
359
389
|
}
|
|
360
390
|
else {
|
|
361
391
|
this.createTurnAttempt({ attemptId: row.prompt_id, localSessionId, contextKey: row.context_key,
|
|
362
|
-
promptText: row.prompt_text,
|
|
392
|
+
promptText: row.prompt_text, localImagePaths: prompt.localImagePaths,
|
|
393
|
+
replyMessageId: row.reply_message_id ?? undefined });
|
|
363
394
|
}
|
|
364
395
|
}
|
|
365
|
-
return
|
|
396
|
+
return prompt;
|
|
366
397
|
}
|
|
367
398
|
});
|
|
368
399
|
return take();
|
|
369
400
|
}
|
|
401
|
+
claimQueuedPromptForImmediateSend(prompt) {
|
|
402
|
+
return this.db.transaction(() => {
|
|
403
|
+
const current = this.getQueuedPrompt(prompt.localSessionId, prompt.promptId);
|
|
404
|
+
if (!current || current.text !== prompt.text || current.displayPrompt !== prompt.displayPrompt)
|
|
405
|
+
return false;
|
|
406
|
+
const run = this.schedules.getRun(prompt.promptId);
|
|
407
|
+
if (run) {
|
|
408
|
+
const attempt = this.getTurnAttempt(run.id);
|
|
409
|
+
if (attempt && (attempt.submissionState !== "not_submitted" || attempt.turnId))
|
|
410
|
+
return false;
|
|
411
|
+
if (!this.schedules.claimStart(run.id))
|
|
412
|
+
return false;
|
|
413
|
+
this.schedules.saveRun({ ...this.schedules.getRun(run.id), status: "submitting" });
|
|
414
|
+
}
|
|
415
|
+
this.db.prepare("DELETE FROM queued_prompts WHERE local_session_id = ? AND prompt_id = ?").run(prompt.localSessionId, prompt.promptId);
|
|
416
|
+
return true;
|
|
417
|
+
})();
|
|
418
|
+
}
|
|
419
|
+
restoreQueuedPromptAfterImmediateSend(prompt) {
|
|
420
|
+
this.db.transaction(() => {
|
|
421
|
+
const run = this.schedules.getRun(prompt.promptId);
|
|
422
|
+
if (run) {
|
|
423
|
+
if (run.status !== "submitting")
|
|
424
|
+
return;
|
|
425
|
+
this.schedules.saveRun({ ...run, status: "queued", startedAt: undefined });
|
|
426
|
+
if (!this.schedules.canSubmit(run.id))
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (!this.hasQueuedPrompt(prompt.promptId))
|
|
430
|
+
this.enqueuePrompt(prompt);
|
|
431
|
+
})();
|
|
432
|
+
}
|
|
370
433
|
cancelQueuedPrompt(promptId, localSessionId) {
|
|
371
434
|
const cancel = this.db.transaction(() => {
|
|
372
435
|
const row = this.db.prepare(`
|
|
@@ -382,7 +445,27 @@ export class StateStore {
|
|
|
382
445
|
});
|
|
383
446
|
return cancel();
|
|
384
447
|
}
|
|
385
|
-
|
|
448
|
+
cancelWorkspaceQueuedPrompt(promptId, localSessionId, request) {
|
|
449
|
+
return this.db.transaction(() => {
|
|
450
|
+
const fingerprint = JSON.stringify(["cancel-queued-prompt", localSessionId, promptId]);
|
|
451
|
+
const previous = this.workspaceRequest(request);
|
|
452
|
+
if (previous) {
|
|
453
|
+
if (previous.fingerprint !== fingerprint)
|
|
454
|
+
throw new Error("该请求标识已用于另一项操作。");
|
|
455
|
+
return { cancelled: previous.status === "accepted", prompt: undefined };
|
|
456
|
+
}
|
|
457
|
+
this.beginWorkspaceRequest(request, localSessionId, fingerprint);
|
|
458
|
+
const prompt = this.cancelQueuedPrompt(promptId, localSessionId);
|
|
459
|
+
this.updateWorkspaceRequest(request, prompt ? "accepted" : "stale");
|
|
460
|
+
return { cancelled: Boolean(prompt), prompt };
|
|
461
|
+
})();
|
|
462
|
+
}
|
|
463
|
+
createTurnAttempt(input, beforeCreate) {
|
|
464
|
+
if (beforeCreate)
|
|
465
|
+
return this.db.transaction(() => {
|
|
466
|
+
beforeCreate();
|
|
467
|
+
return this.createTurnAttempt(input);
|
|
468
|
+
})();
|
|
386
469
|
const createdAt = input.createdAt ?? new Date().toISOString();
|
|
387
470
|
const updatedAt = input.updatedAt ?? createdAt;
|
|
388
471
|
const status = input.status ?? (input.turnId ? "running" : "accepted");
|
|
@@ -798,6 +881,23 @@ export class StateStore {
|
|
|
798
881
|
this.db.prepare("DELETE FROM turn_snapshots WHERE turn_id = ?").run(pendingTurnId);
|
|
799
882
|
})();
|
|
800
883
|
}
|
|
884
|
+
listTaskMetadata(localSessionId, kind) {
|
|
885
|
+
return this.db.prepare("SELECT key, value FROM task_metadata WHERE local_session_id = ? AND kind = ? ORDER BY value")
|
|
886
|
+
.all(localSessionId, kind);
|
|
887
|
+
}
|
|
888
|
+
getTaskMetadata(localSessionId, kind, key) {
|
|
889
|
+
const row = this.db.prepare("SELECT value FROM task_metadata WHERE local_session_id = ? AND kind = ? AND key = ?")
|
|
890
|
+
.get(localSessionId, kind, key);
|
|
891
|
+
return row?.value;
|
|
892
|
+
}
|
|
893
|
+
setTaskMetadata(localSessionId, kind, key, value) {
|
|
894
|
+
this.db.prepare(`INSERT INTO task_metadata (local_session_id, kind, key, value) VALUES (?, ?, ?, ?)
|
|
895
|
+
ON CONFLICT (local_session_id, kind, key) DO UPDATE SET value = excluded.value`).run(localSessionId, kind, key, value);
|
|
896
|
+
}
|
|
897
|
+
removeTaskMetadata(localSessionId, kind, key) {
|
|
898
|
+
return this.db.prepare("DELETE FROM task_metadata WHERE local_session_id = ? AND kind = ? AND key = ?")
|
|
899
|
+
.run(localSessionId, kind, key).changes > 0;
|
|
900
|
+
}
|
|
801
901
|
workspaceRequest(id) {
|
|
802
902
|
return this.db.prepare(`SELECT local_session_id AS localSessionId, fingerprint, status, posted, message
|
|
803
903
|
FROM workspace_requests WHERE id = ?`).get(id);
|
|
@@ -810,6 +910,9 @@ export class StateStore {
|
|
|
810
910
|
this.db.prepare("UPDATE workspace_requests SET status = ?, posted = max(posted, ?), message = ? WHERE id = ?")
|
|
811
911
|
.run(status, Number(posted), message ?? null, id);
|
|
812
912
|
}
|
|
913
|
+
markWorkspaceRequestPosted(id) {
|
|
914
|
+
this.db.prepare("UPDATE workspace_requests SET posted = 1 WHERE id = ?").run(id);
|
|
915
|
+
}
|
|
813
916
|
workspaceTurnHead(task) {
|
|
814
917
|
if (task.lastTurnId && task.status !== "running" && task.status !== "starting"
|
|
815
918
|
&& this.db.prepare("SELECT 1 FROM turn_snapshots WHERE turn_id = ?").get(task.lastTurnId))
|
|
@@ -820,50 +923,133 @@ export class StateStore {
|
|
|
820
923
|
return row?.id ?? this.findForkSourceTurnId(task.localSessionId);
|
|
821
924
|
}
|
|
822
925
|
workspaceTurnPage(localSessionId, head, before, limit = 20) {
|
|
926
|
+
const ids = [];
|
|
927
|
+
let found = !before;
|
|
928
|
+
for (const id of this.workspaceTurnChain(localSessionId, head)) {
|
|
929
|
+
if (found)
|
|
930
|
+
ids.push(id);
|
|
931
|
+
if (id === before)
|
|
932
|
+
found = true;
|
|
933
|
+
if (ids.length > limit)
|
|
934
|
+
break;
|
|
935
|
+
}
|
|
936
|
+
return { ids: ids.slice(0, limit), more: ids.length > limit, beforeMissing: !found };
|
|
937
|
+
}
|
|
938
|
+
workspaceTurnsAfter(localSessionId, head, after, limit) {
|
|
939
|
+
if (after === head)
|
|
940
|
+
return { ids: [], more: false, cursorMissing: false };
|
|
941
|
+
// Walk the current branch from its head, retaining only the closest successors of the cursor.
|
|
942
|
+
const nearest = [];
|
|
943
|
+
for (const id of this.workspaceTurnChain(localSessionId, head)) {
|
|
944
|
+
if (id === after)
|
|
945
|
+
return { ids: nearest.slice(-limit).reverse(), more: nearest.length > limit, cursorMissing: false };
|
|
946
|
+
if (nearest.length > limit)
|
|
947
|
+
nearest.shift();
|
|
948
|
+
nearest.push(id);
|
|
949
|
+
}
|
|
950
|
+
return { ids: [], more: false, cursorMissing: true };
|
|
951
|
+
}
|
|
952
|
+
workspaceConversationMetrics(localSessionId, head) {
|
|
953
|
+
const readUsage = this.db.prepare(`SELECT json_extract(snapshot_json, '$.tokenUsageTotalCumulative') AS totalTokens
|
|
954
|
+
FROM turn_snapshots WHERE turn_id = ? AND local_session_id = ?`);
|
|
955
|
+
let turnCount = 0;
|
|
956
|
+
let totalTokens;
|
|
957
|
+
for (const id of this.workspaceTurnChain(localSessionId, head)) {
|
|
958
|
+
turnCount++;
|
|
959
|
+
if (totalTokens !== undefined || turnCount > 20)
|
|
960
|
+
continue;
|
|
961
|
+
// Use the latest published total from this task, including while its next turn is starting.
|
|
962
|
+
const tokens = readUsage.get(id, localSessionId)?.totalTokens;
|
|
963
|
+
if (typeof tokens === "number" && Number.isSafeInteger(tokens) && tokens >= 0)
|
|
964
|
+
totalTokens = tokens;
|
|
965
|
+
}
|
|
966
|
+
return { turnCount, totalTokens };
|
|
967
|
+
}
|
|
968
|
+
*workspaceTurnChain(localSessionId, head) {
|
|
823
969
|
const read = this.db.prepare(`SELECT s.local_session_id AS owner, p.turn_id AS linked, p.parent_turn_id AS parent
|
|
824
970
|
FROM turn_snapshots s LEFT JOIN turn_parent_links p ON p.turn_id = s.turn_id WHERE s.turn_id = ?`);
|
|
825
971
|
let id = head;
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
let found = !before;
|
|
829
|
-
while (id && !seen.has(id) && seen.size < 20_000 && ids.length <= limit) {
|
|
972
|
+
const seen = new Set(), backfilled = new Set();
|
|
973
|
+
while (id && !seen.has(id) && seen.size < 20_000) {
|
|
830
974
|
seen.add(id);
|
|
831
975
|
let node = read.get(id);
|
|
832
976
|
if (!node || seen.size === 1 && node.owner !== localSessionId && !this.hasTaskHistoryTurn(localSessionId, id))
|
|
833
977
|
break;
|
|
834
|
-
if (
|
|
978
|
+
if (!node.linked && !backfilled.has(node.owner)) {
|
|
835
979
|
this.backfillTurnParents(node.owner);
|
|
980
|
+
backfilled.add(node.owner);
|
|
836
981
|
node = read.get(id);
|
|
837
|
-
fallback = false;
|
|
838
982
|
}
|
|
839
|
-
|
|
840
|
-
ids.push(id);
|
|
841
|
-
if (id === before)
|
|
842
|
-
found = true;
|
|
983
|
+
yield id;
|
|
843
984
|
id = node.parent ?? (seen.size === 1 && node.owner === localSessionId
|
|
844
985
|
? this.findForkSourceTurnId(localSessionId) : undefined);
|
|
845
986
|
}
|
|
846
|
-
return { ids: ids.slice(0, limit), more: ids.length > limit, beforeMissing: !found };
|
|
847
987
|
}
|
|
848
988
|
workspaceTurnText(turnId) {
|
|
849
|
-
return this.db.prepare(`SELECT updated_at AS revision,
|
|
989
|
+
return this.db.prepare(`SELECT updated_at AS revision, local_session_id AS ownerTask,
|
|
850
990
|
substr(json_extract(snapshot_json, '$.prompt'), 1, 24001) AS prompt,
|
|
851
991
|
snapshot_json -> '$.promptImagePaths' AS promptImages,
|
|
852
|
-
|
|
853
|
-
|
|
992
|
+
json_extract(snapshot_json, '$.promptRequestId') AS promptRequestId,
|
|
993
|
+
json_extract(snapshot_json, '$.finalResponse') AS answer,
|
|
994
|
+
json_extract(snapshot_json, '$.assistantText') AS live,
|
|
854
995
|
substr(json_extract(snapshot_json, '$.error'), 1, 3000) AS error,
|
|
855
996
|
substr(json_extract(snapshot_json, '$.statusMessage'), 1, 3000) AS notice,
|
|
856
997
|
json_extract(snapshot_json, '$.status') AS status,
|
|
857
998
|
json_extract(snapshot_json, '$.startedAt') AS startedAt,
|
|
999
|
+
json_extract(snapshot_json, '$.completedAt') AS completedAt,
|
|
1000
|
+
json_extract(snapshot_json, '$.durationMs') AS durationMs,
|
|
1001
|
+
json_extract(snapshot_json, '$.totalTokens') AS totalTokens,
|
|
1002
|
+
json_extract(snapshot_json, '$.cachedInputTokens') AS cachedInputTokens,
|
|
1003
|
+
json_extract(snapshot_json, '$.totalTokensIncludingCache') AS totalTokensIncludingCache,
|
|
1004
|
+
json_extract(snapshot_json, '$.modelCallCount') AS modelCallCount,
|
|
1005
|
+
json_extract(snapshot_json, '$.totalToolCount') AS totalToolCount,
|
|
858
1006
|
json_extract(snapshot_json, '$.historyDetail') AS detail,
|
|
859
|
-
(
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
1007
|
+
coalesce(json_array_length(snapshot_json, '$.fileSummary'), 0) AS changeCount,
|
|
1008
|
+
snapshot_json -> '$.fileSummary' AS changeSummary,
|
|
1009
|
+
json_extract(snapshot_json, '$.previewTurnId') AS journalId,
|
|
1010
|
+
json_extract(snapshot_json, '$.continuedToTurnId') AS continuedToTurnId,
|
|
1011
|
+
coalesce(json_extract(snapshot_json, '$.activitiesTruncated'), 0) AS compact,
|
|
1012
|
+
(SELECT json_group_array(json_object('kind', kind, 'id', id, 'text', body, 'images', json(images),
|
|
1013
|
+
'heading', json(heading), 'toolKind', toolKind, 'status', status, 'startedAt', activityStart, 'completedAt', toolEnd) ORDER BY ordinal) FROM (
|
|
1014
|
+
SELECT key AS ordinal, json_extract(value, '$.kind') AS kind, json_extract(value, '$.id') AS id,
|
|
1015
|
+
json_extract(value, '$.text') AS body, value -> '$.localImagePaths' AS images,
|
|
1016
|
+
CASE WHEN json_extract(value, '$.kind') = 'tool' THEN workspace_tool_heading(json_extract(value, '$.tool.title'),
|
|
1017
|
+
json_extract(value, '$.tool.kind'), json_extract(value, '$.tool.command')) END AS heading,
|
|
1018
|
+
json_extract(value, '$.tool.kind') AS toolKind, json_extract(value, '$.tool.status') AS status,
|
|
1019
|
+
coalesce(json_extract(value, '$.tool.startedAt'), json_extract(value, '$.startedAt')) AS activityStart,
|
|
1020
|
+
json_extract(value, '$.tool.completedAt') AS toolEnd
|
|
1021
|
+
FROM json_each(snapshot_json, '$.activities'))) AS activities,
|
|
1022
|
+
(SELECT json_group_array(json_object('id', id, 'after', after, 'parts', json(parts))
|
|
1023
|
+
ORDER BY ordinal) FROM (
|
|
1024
|
+
SELECT item.key AS ordinal, json_extract(item.value, '$.itemId') AS id,
|
|
1025
|
+
json_extract(item.value, '$.afterActivityId') AS after,
|
|
1026
|
+
(SELECT json_group_array(json_object('index', section, 'text', snippet) ORDER BY section) FROM (
|
|
1027
|
+
SELECT part.key AS section, substr(part.value, 1, 512) AS snippet
|
|
1028
|
+
FROM json_each(item.value, '$.summary') AS part
|
|
1029
|
+
WHERE part.type = 'text' AND trim(part.value) != '')) AS parts
|
|
1030
|
+
FROM json_each(snapshot_json, '$.reasoningItems') AS item)) AS reasoning
|
|
865
1031
|
FROM turn_snapshots WHERE turn_id = ?`).get(turnId);
|
|
866
1032
|
}
|
|
1033
|
+
workspaceTurnPlan(turnId) {
|
|
1034
|
+
const row = this.db.prepare("SELECT snapshot_json -> '$.plan' AS plan FROM turn_snapshots WHERE turn_id = ?")
|
|
1035
|
+
.get(turnId);
|
|
1036
|
+
return row?.plan ? JSON.parse(row.plan) ?? [] : [];
|
|
1037
|
+
}
|
|
1038
|
+
workspaceTurnExecution(turnId) {
|
|
1039
|
+
return this.db.prepare(`SELECT
|
|
1040
|
+
json_extract(snapshot_json, '$.startedAt') AS startedAt,
|
|
1041
|
+
json_extract(snapshot_json, '$.model') AS model,
|
|
1042
|
+
json_extract(snapshot_json, '$.reasoningEffort') AS thinking,
|
|
1043
|
+
json_extract(snapshot_json, '$.totalTokens') AS totalTokens,
|
|
1044
|
+
json_extract(snapshot_json, '$.cachedInputTokens') AS cachedInputTokens,
|
|
1045
|
+
json_extract(snapshot_json, '$.totalTokensIncludingCache') AS totalTokensIncludingCache
|
|
1046
|
+
FROM turn_snapshots WHERE turn_id = ?`).get(turnId);
|
|
1047
|
+
}
|
|
1048
|
+
workspaceTurnAnswer(turnId) {
|
|
1049
|
+
const row = this.db.prepare("SELECT json_extract(snapshot_json, '$.finalResponse') AS answer FROM turn_snapshots WHERE turn_id = ?")
|
|
1050
|
+
.get(turnId);
|
|
1051
|
+
return row?.answer ?? undefined;
|
|
1052
|
+
}
|
|
867
1053
|
getTurnSnapshot(turnId) {
|
|
868
1054
|
const row = this.db
|
|
869
1055
|
.prepare("SELECT snapshot_json FROM turn_snapshots WHERE turn_id = ?")
|
|
@@ -1795,6 +1981,10 @@ export class StateStore {
|
|
|
1795
1981
|
.run(canonical.local_session_id, duplicate.local_session_id);
|
|
1796
1982
|
this.db.prepare("DELETE FROM context_sessions WHERE local_session_id = ?")
|
|
1797
1983
|
.run(duplicate.local_session_id);
|
|
1984
|
+
this.db.prepare(`INSERT OR IGNORE INTO task_metadata (local_session_id, kind, key, value)
|
|
1985
|
+
SELECT ?, kind, key, value FROM task_metadata WHERE local_session_id = ?`)
|
|
1986
|
+
.run(canonical.local_session_id, duplicate.local_session_id);
|
|
1987
|
+
this.db.prepare("DELETE FROM task_metadata WHERE local_session_id = ?").run(duplicate.local_session_id);
|
|
1798
1988
|
this.db.prepare("DELETE FROM sessions WHERE local_session_id = ?")
|
|
1799
1989
|
.run(duplicate.local_session_id);
|
|
1800
1990
|
}
|