@evandu/super-pi 0.1.183 → 0.1.186
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/agents/coder.md +1 -1
- package/agents/planner.md +1 -1
- package/agents/project-leader.md +2 -2
- package/agents/reviewer.md +1 -1
- package/agents/super-pi-task/core.ts +164 -7
- package/agents/super-pi-task/index.ts +33 -1
- package/dist/client/.super-pi-client-sources.json +7 -7
- package/dist/client/assets/{CodeMirrorEditor-DUyqaSOB.js → CodeMirrorEditor-Cmx7-z0f.js} +2 -2
- package/dist/client/assets/{dist-CPmDYAQT.js → dist-B8CAEuXW.js} +1 -1
- package/dist/client/assets/{dist-nbHIDjzw.js → dist-BKx3CpQJ.js} +1 -1
- package/dist/client/assets/{dist-CB124C6Z.js → dist-BMSVWA_A.js} +1 -1
- package/dist/client/assets/{dist-CU8JEI5K.js → dist-BdiInYmk.js} +1 -1
- package/dist/client/assets/{dist-D3HMZhM-.js → dist-Bq7p17eF.js} +1 -1
- package/dist/client/assets/{dist-CvLzvDh7.js → dist-BzqUlzAf.js} +1 -1
- package/dist/client/assets/{dist-Dkm31GRe.js → dist-C0PMHwdm.js} +1 -1
- package/dist/client/assets/{dist-CeU4Svbh.js → dist-C2ltv_8u.js} +1 -1
- package/dist/client/assets/{dist-DTd2PCza.js → dist-CDSDxXju.js} +1 -1
- package/dist/client/assets/{dist-DLOUy-hB.js → dist-CKZOnuRU.js} +1 -1
- package/dist/client/assets/{dist-ka17rnWs.js → dist-CWlI6qO0.js} +1 -1
- package/dist/client/assets/{dist-AQ8CYp6b.js → dist-CXDhHY7N.js} +1 -1
- package/dist/client/assets/{dist-Dnx8LlEL.js → dist-Cblc22Ag.js} +1 -1
- package/dist/client/assets/{dist-Bj218gfa.js → dist-CcuBp_Kn.js} +1 -1
- package/dist/client/assets/{dist-CNem2r_T.js → dist-DHFt0Hvo.js} +1 -1
- package/dist/client/assets/{dist-NLNoAzKl.js → dist-D_7x3oZt.js} +1 -1
- package/dist/client/assets/{dist-D4lWO1EF.js → dist-RHYiODih.js} +1 -1
- package/dist/client/assets/{dist-CkTZUfAR.js → dist-V27Fcf8p.js} +1 -1
- package/dist/client/assets/{dist-Dlg4MpdE.js → dist-WfagMkC4.js} +1 -1
- package/dist/client/assets/{dist-dnyR0Vf6.js → dist-akJoBtjs.js} +1 -1
- package/dist/client/assets/{dist-C7PhA7pe.js → dist-frBiV55p.js} +1 -1
- package/dist/client/assets/{dist-TX6Uhfee.js → dist-ibsGPa_S.js} +1 -1
- package/dist/client/assets/{index-Ui6aXMVV.css → index-BXQjUHV_.css} +1 -1
- package/dist/client/assets/{index-BplDWdZp.js → index-Dx7HAZ5j.js} +14 -13
- package/dist/client/index.html +2 -2
- package/dist/server/agents.d.ts +1 -1
- package/dist/server/agents.js +1 -1
- package/dist/server/application/conversation-service.d.ts +13 -0
- package/dist/server/application/conversation-service.js +21 -0
- package/dist/server/application/project-service.d.ts +2 -70
- package/dist/server/application/project-service.js +102 -205
- package/dist/server/application/run-service.d.ts +39 -3
- package/dist/server/application/run-service.js +166 -18
- package/dist/server/application/session-orchestrator-service.d.ts +38 -5
- package/dist/server/application/session-orchestrator-service.js +167 -5
- package/dist/server/command-handler.js +5 -1
- package/dist/server/http-server.js +45 -18
- package/dist/server/index.js +1 -1
- package/dist/server/pi-session.d.ts +3 -2
- package/dist/server/runtime.js +9 -6
- package/dist/server/session-tree-scope.d.ts +1 -1
- package/dist/server/store.d.ts +24 -0
- package/dist/server/store.js +229 -4
- package/dist/server/task-tool.d.ts +5 -2
- package/dist/server/task-tool.js +25 -2
- package/dist/server/types.d.ts +43 -13
- package/dist/server/upload.d.ts +2 -6
- package/dist/server/upload.js +7 -5
- package/dist/shared/session-reporting.d.ts +13 -2
- package/dist/shared/session-reporting.js +62 -15
- package/package.json +8 -3
|
@@ -16,6 +16,17 @@ import { execFile } from "node:child_process";
|
|
|
16
16
|
import { promisify } from "node:util";
|
|
17
17
|
import { randomUUID } from "node:crypto";
|
|
18
18
|
const execFileAsync = promisify(execFile);
|
|
19
|
+
/**
|
|
20
|
+
* Validates a caller-supplied delivery mode without silently falling back on bogus values.
|
|
21
|
+
* Undefined defaults to `"queue"`; any other value is rejected explicitly.
|
|
22
|
+
*/
|
|
23
|
+
export function parseMessageDeliveryMode(mode) {
|
|
24
|
+
if (mode === undefined)
|
|
25
|
+
return "queue";
|
|
26
|
+
if (mode === "queue" || mode === "steer")
|
|
27
|
+
return mode;
|
|
28
|
+
throw new Error(`不支持的消息投递模式:${JSON.stringify(mode)}(仅支持 "queue" 或 "steer")`);
|
|
29
|
+
}
|
|
19
30
|
/** Expands composer references into explicit context for the model. */
|
|
20
31
|
async function expandAtReferences(store, workspace, content) {
|
|
21
32
|
const refs = [...content.matchAll(/@(worktree|session):([^\s]+)/gi)];
|
|
@@ -1004,26 +1015,42 @@ export class RunService {
|
|
|
1004
1015
|
* the user asked to be answered NOW - so the ones Pi had not delivered go back in, and clearing the
|
|
1005
1016
|
* queues for one message does not silently un-steer another.
|
|
1006
1017
|
*/
|
|
1007
|
-
|
|
1018
|
+
/**
|
|
1019
|
+
* Empties Pi's steering and follow-up queues for a conversation, returning any steering message
|
|
1020
|
+
* IDs that were intentionally withheld because they appear in `excludeMessageIds`.
|
|
1021
|
+
*
|
|
1022
|
+
* Remaining steering messages not in `excludeMessageIds` are put back into Pi's steering queue
|
|
1023
|
+
* in FIFO order (matching at most once per message ID even when two messages share identical text).
|
|
1024
|
+
* Remaining follow-up messages stay in Super Pi's waiting list and run after the active turn.
|
|
1025
|
+
*/
|
|
1026
|
+
async clearPiQueues(conversationId, options) {
|
|
1027
|
+
const evictedSteerIds = new Set();
|
|
1008
1028
|
if (!this.runtime.clearQueues)
|
|
1009
|
-
return;
|
|
1029
|
+
return evictedSteerIds;
|
|
1010
1030
|
let cleared;
|
|
1011
1031
|
try {
|
|
1012
1032
|
cleared = await this.runtime.clearQueues(conversationId);
|
|
1013
1033
|
}
|
|
1014
1034
|
catch (error) {
|
|
1015
1035
|
console.warn(`Super Pi: clearing Pi's queues for ${conversationId} failed: ${error instanceof Error ? error.message : "unknown error"}`);
|
|
1016
|
-
return;
|
|
1036
|
+
return evictedSteerIds;
|
|
1017
1037
|
}
|
|
1038
|
+
const matchedSteerIds = new Set();
|
|
1039
|
+
const excludeIds = options?.excludeMessageIds;
|
|
1018
1040
|
for (const text of cleared.steering ?? []) {
|
|
1041
|
+
const message = this.store.listWaitingMessages(conversationId)
|
|
1042
|
+
.find((item) => item.delivery === "sending" && composePrompt(item) === text && !matchedSteerIds.has(item.id));
|
|
1043
|
+
if (!message)
|
|
1044
|
+
continue;
|
|
1045
|
+
matchedSteerIds.add(message.id);
|
|
1046
|
+
if (excludeIds?.has(message.id)) {
|
|
1047
|
+
evictedSteerIds.add(message.id);
|
|
1048
|
+
continue;
|
|
1049
|
+
}
|
|
1019
1050
|
// A STEER NEEDS A TURN TO GO INTO. With no turn running there is nothing to steer, and the message
|
|
1020
1051
|
// is released back to `waiting` when the run ends anyway - so it is answered as a turn of its own.
|
|
1021
1052
|
if (this.runtime.canSteer && !this.runtime.canSteer(conversationId))
|
|
1022
1053
|
continue;
|
|
1023
|
-
const message = this.store.listWaitingMessages(conversationId)
|
|
1024
|
-
.find((item) => item.delivery === "sending" && composePrompt(item) === text);
|
|
1025
|
-
if (!message)
|
|
1026
|
-
continue;
|
|
1027
1054
|
try {
|
|
1028
1055
|
await this.runtime.steer?.(conversationId, text, message.images);
|
|
1029
1056
|
}
|
|
@@ -1031,6 +1058,7 @@ export class RunService {
|
|
|
1031
1058
|
console.warn(`Super Pi: putting a steered message back failed: ${error instanceof Error ? error.message : "unknown error"}`);
|
|
1032
1059
|
}
|
|
1033
1060
|
}
|
|
1061
|
+
return evictedSteerIds;
|
|
1034
1062
|
}
|
|
1035
1063
|
/**
|
|
1036
1064
|
* Opens a turn on a message that is ALREADY IN THE TRANSCRIPT.
|
|
@@ -1220,7 +1248,7 @@ export class RunService {
|
|
|
1220
1248
|
let thinkingInterrupted = false;
|
|
1221
1249
|
let answerAfterThinking = false;
|
|
1222
1250
|
const currentThinkingBlock = () => {
|
|
1223
|
-
if (
|
|
1251
|
+
if (thinkingBlock >= 0 && !thinkingInterrupted && !answerAfterThinking)
|
|
1224
1252
|
return thinkingBlock;
|
|
1225
1253
|
// An outside call separates two thoughts even if no answer text was emitted.
|
|
1226
1254
|
if (thinkingBlock >= 0)
|
|
@@ -1306,8 +1334,9 @@ export class RunService {
|
|
|
1306
1334
|
// spends writing its replacement text - and by the time the file path was on screen, the block looked
|
|
1307
1335
|
// broken rather than early. The placeholder word is now only the FALLBACK for the first fragment, when
|
|
1308
1336
|
// Pi has not yet said what the call is or what it opens.
|
|
1309
|
-
//
|
|
1310
|
-
|
|
1337
|
+
// Ownership is fixed when the call is announced: once a thinking block is open on this message,
|
|
1338
|
+
// the thinking stage remains open across thinking_end until answer text appears.
|
|
1339
|
+
const callThinkingBlock = (thinkingBlock >= 0 && !thinkingInterrupted && !answerAfterThinking) ? thinkingBlock : null;
|
|
1311
1340
|
enqueueTools("a tool call being prepared", () => this.store.appendToolCall(conversation.id, assistant.id, {
|
|
1312
1341
|
label: call.name || "工具",
|
|
1313
1342
|
...(call.detail ? { detail: call.detail } : { detail: "正在生成参数…" }),
|
|
@@ -1459,9 +1488,8 @@ export class RunService {
|
|
|
1459
1488
|
//
|
|
1460
1489
|
// THIS IS THE PATH WITH NO PLACEHOLDER - Pi never announced a `toolcall_start` for this call,
|
|
1461
1490
|
// so there is no earlier moment to compare offsets against, and the reasoning offset is taken
|
|
1462
|
-
// here, at the announcement.
|
|
1463
|
-
//
|
|
1464
|
-
// `thinkingActive`, because a call with no placeholder carries no earlier offset to compare.
|
|
1491
|
+
// here, at the announcement. Ownership follows the same stage boundary as `onToolPreparing`:
|
|
1492
|
+
// once a thinking block is open on this message, the call belongs to it until answer text arrives.
|
|
1465
1493
|
const thinkingAt = thought;
|
|
1466
1494
|
// A CALL ANNOUNCED AFTER THE ANSWER BELONGS TO NO FOLD, even when the reasoning is still running.
|
|
1467
1495
|
// `currentThinkingBlock()` also marks the thinking as interrupted when it is not active, so it
|
|
@@ -1845,7 +1873,8 @@ export class RunService {
|
|
|
1845
1873
|
* and the turn runs on them exactly as before. `composePrompt` is what tells the model who is
|
|
1846
1874
|
* speaking, so the model is addressed in the same words.
|
|
1847
1875
|
*/
|
|
1848
|
-
async enqueueAndRunAsAgent(conversationId, content) {
|
|
1876
|
+
async enqueueAndRunAsAgent(conversationId, content, options) {
|
|
1877
|
+
const deliveryMode = parseMessageDeliveryMode(options?.mode);
|
|
1849
1878
|
this.assertHistoryAvailable(conversationId);
|
|
1850
1879
|
this.memory?.cancel(conversationId);
|
|
1851
1880
|
const trimmed = content.trim();
|
|
@@ -1875,14 +1904,20 @@ export class RunService {
|
|
|
1875
1904
|
this.broadcaster.broadcastState();
|
|
1876
1905
|
if (!reservedController) {
|
|
1877
1906
|
const waiting = this.store.listWaitingMessages(conversationId).find((item) => item.id === message.id && item.delivery === "waiting");
|
|
1878
|
-
if (!waiting)
|
|
1907
|
+
if (!waiting) {
|
|
1908
|
+
options?.onAccepted?.(message.id);
|
|
1879
1909
|
return;
|
|
1910
|
+
}
|
|
1880
1911
|
if (!this.activeRuns.has(conversationId) && !this.interruptTargets.has(conversationId))
|
|
1881
1912
|
await this.processWaitingMessage(conversationId, message.id);
|
|
1913
|
+
else if (deliveryMode === "steer")
|
|
1914
|
+
await this.steerWaitingMessageOnBusyRun(conversationId, message.id);
|
|
1882
1915
|
else
|
|
1883
1916
|
await this.submitToPiQueue(conversationId, message);
|
|
1917
|
+
options?.onAccepted?.(message.id);
|
|
1884
1918
|
return;
|
|
1885
1919
|
}
|
|
1920
|
+
options?.onAccepted?.(message.id);
|
|
1886
1921
|
const controller = reservedController;
|
|
1887
1922
|
try {
|
|
1888
1923
|
if (controller.signal.aborted) {
|
|
@@ -1918,7 +1953,8 @@ export class RunService {
|
|
|
1918
1953
|
this.broadcaster.broadcastState();
|
|
1919
1954
|
}
|
|
1920
1955
|
}
|
|
1921
|
-
async enqueueAndRun(conversationId, content, images, attachments, clientId) {
|
|
1956
|
+
async enqueueAndRun(conversationId, content, images, attachments, clientId, mode) {
|
|
1957
|
+
const deliveryMode = parseMessageDeliveryMode(mode);
|
|
1922
1958
|
this.assertHistoryAvailable(conversationId);
|
|
1923
1959
|
/**
|
|
1924
1960
|
* A CLIENT ID THIS CONVERSATION ALREADY HAS IS A RESEND, NOT A SECOND MESSAGE.
|
|
@@ -1999,6 +2035,9 @@ export class RunService {
|
|
|
1999
2035
|
if (!this.activeRuns.has(conversationId) && !this.interruptTargets.has(conversationId)) {
|
|
2000
2036
|
await this.processWaitingMessage(conversationId, message.id);
|
|
2001
2037
|
}
|
|
2038
|
+
else if (deliveryMode === "steer") {
|
|
2039
|
+
await this.steerWaitingMessageOnBusyRun(conversationId, message.id);
|
|
2040
|
+
}
|
|
2002
2041
|
else
|
|
2003
2042
|
await this.submitToPiQueue(conversationId, message);
|
|
2004
2043
|
return;
|
|
@@ -2311,6 +2350,49 @@ export class RunService {
|
|
|
2311
2350
|
throw error;
|
|
2312
2351
|
}
|
|
2313
2352
|
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Steers a newly queued message into the running turn at the next turn boundary.
|
|
2355
|
+
* Mirrors `dispatchBackgroundResult`'s lock-free fallback if the active run ends before the
|
|
2356
|
+
* conversation lane is entered.
|
|
2357
|
+
*/
|
|
2358
|
+
async steerWaitingMessageOnBusyRun(conversationId, messageId) {
|
|
2359
|
+
const startWaiting = await this.serializeConversation(conversationId, async () => {
|
|
2360
|
+
if (this.shuttingDown || this.interruptTargets.has(conversationId)) {
|
|
2361
|
+
if (!this.shuttingDown) {
|
|
2362
|
+
await this.moveAheadOfUnclaimed(conversationId, messageId);
|
|
2363
|
+
this.broadcaster.broadcastState();
|
|
2364
|
+
}
|
|
2365
|
+
return false;
|
|
2366
|
+
}
|
|
2367
|
+
if (!this.activeRuns.has(conversationId))
|
|
2368
|
+
return true;
|
|
2369
|
+
const waiting = this.store.listWaitingMessages(conversationId);
|
|
2370
|
+
if (!waiting.some((item) => item.id === messageId && item.delivery === "waiting"))
|
|
2371
|
+
return false;
|
|
2372
|
+
await this.moveAheadOfUnclaimed(conversationId, messageId);
|
|
2373
|
+
this.broadcaster.broadcastState();
|
|
2374
|
+
if (!this.runtime.steer)
|
|
2375
|
+
return false;
|
|
2376
|
+
if (this.runtime.canSteer && !this.runtime.canSteer(conversationId))
|
|
2377
|
+
return false;
|
|
2378
|
+
const claimed = await this.store.claimWaitingMessage(conversationId, messageId);
|
|
2379
|
+
this.broadcaster.broadcastState();
|
|
2380
|
+
try {
|
|
2381
|
+
await this.runtime.steer(conversationId, composePrompt(claimed), claimed.images);
|
|
2382
|
+
return false;
|
|
2383
|
+
}
|
|
2384
|
+
catch (error) {
|
|
2385
|
+
await this.store.releaseWaitingMessage(conversationId, claimed.id);
|
|
2386
|
+
this.broadcaster.broadcastState();
|
|
2387
|
+
if (error instanceof SteerUnavailableError)
|
|
2388
|
+
return !this.activeRuns.has(conversationId);
|
|
2389
|
+
throw error;
|
|
2390
|
+
}
|
|
2391
|
+
});
|
|
2392
|
+
if (startWaiting) {
|
|
2393
|
+
await this.processWaitingMessage(conversationId, messageId);
|
|
2394
|
+
}
|
|
2395
|
+
}
|
|
2314
2396
|
/**
|
|
2315
2397
|
* Withdraws a waiting message.
|
|
2316
2398
|
*/
|
|
@@ -2325,9 +2407,75 @@ export class RunService {
|
|
|
2325
2407
|
// ends - the model would answer words the user believes they withdrew. Pi cannot take back one
|
|
2326
2408
|
// message, so its queues are emptied; the other messages Pi was holding are still in Super Pi's
|
|
2327
2409
|
// waiting list and are answered as turns of their own.
|
|
2328
|
-
await this.clearPiQueues(conversationId);
|
|
2329
|
-
await this.store.cancelWaitingMessage(conversationId, messageId);
|
|
2410
|
+
await this.clearPiQueues(conversationId, { excludeMessageIds: new Set([messageId]) });
|
|
2411
|
+
const removed = await this.store.cancelWaitingMessage(conversationId, messageId);
|
|
2330
2412
|
this.broadcaster.broadcastState();
|
|
2413
|
+
if (removed) {
|
|
2414
|
+
this.broadcaster.broadcast({
|
|
2415
|
+
type: "queue.withdrawn",
|
|
2416
|
+
conversationId,
|
|
2417
|
+
withdrawn: [{ id: removed.id, content: removed.content, role: removed.role === "tool" ? "tool" : "user" }],
|
|
2418
|
+
actor: "user",
|
|
2419
|
+
});
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
/**
|
|
2423
|
+
* Selectively withdraws a specific subset of queued/steered messages by id while preserving
|
|
2424
|
+
* all remaining waiting and steered messages.
|
|
2425
|
+
*/
|
|
2426
|
+
async cancelQueuedMany(conversationId, messageIds, options) {
|
|
2427
|
+
const normalizedIds = [];
|
|
2428
|
+
const seen = new Set();
|
|
2429
|
+
for (const raw of messageIds) {
|
|
2430
|
+
const id = typeof raw === "string" ? raw.trim() : "";
|
|
2431
|
+
if (!id || seen.has(id))
|
|
2432
|
+
continue;
|
|
2433
|
+
seen.add(id);
|
|
2434
|
+
normalizedIds.push(id);
|
|
2435
|
+
}
|
|
2436
|
+
if (normalizedIds.length === 0) {
|
|
2437
|
+
throw new Error("请至少选择一条要撤回的排队消息");
|
|
2438
|
+
}
|
|
2439
|
+
return this.serializeConversation(conversationId, async () => {
|
|
2440
|
+
const interruptTarget = this.interruptTargets.get(conversationId);
|
|
2441
|
+
if (interruptTarget && seen.has(interruptTarget)) {
|
|
2442
|
+
throw new Error("所选消息中有一条正在打断并回答,无法撤回");
|
|
2443
|
+
}
|
|
2444
|
+
const waitingBefore = this.store.listWaitingMessages(conversationId);
|
|
2445
|
+
const evictedSteerIds = await this.clearPiQueues(conversationId, { excludeMessageIds: seen });
|
|
2446
|
+
const unwithdrawable = waitingBefore.find((item) => seen.has(item.id) && item.delivery === "sending" && !evictedSteerIds.has(item.id));
|
|
2447
|
+
if (unwithdrawable) {
|
|
2448
|
+
throw new Error("所选消息中有已经交给 Agent 的消息,取消不掉(Pi 没有撤回单条队列消息的能力)");
|
|
2449
|
+
}
|
|
2450
|
+
for (const id of evictedSteerIds) {
|
|
2451
|
+
await this.store.releaseWaitingMessage(conversationId, id);
|
|
2452
|
+
}
|
|
2453
|
+
const withdrawn = [];
|
|
2454
|
+
const missingIds = [];
|
|
2455
|
+
for (const id of normalizedIds) {
|
|
2456
|
+
const removed = await this.store.cancelWaitingMessage(conversationId, id);
|
|
2457
|
+
if (removed) {
|
|
2458
|
+
withdrawn.push({
|
|
2459
|
+
id: removed.id,
|
|
2460
|
+
content: removed.content,
|
|
2461
|
+
role: removed.role === "tool" ? "tool" : "user",
|
|
2462
|
+
});
|
|
2463
|
+
}
|
|
2464
|
+
else {
|
|
2465
|
+
missingIds.push(id);
|
|
2466
|
+
}
|
|
2467
|
+
}
|
|
2468
|
+
this.broadcaster.broadcastState();
|
|
2469
|
+
if (withdrawn.length > 0) {
|
|
2470
|
+
this.broadcaster.broadcast({
|
|
2471
|
+
type: "queue.withdrawn",
|
|
2472
|
+
conversationId,
|
|
2473
|
+
withdrawn,
|
|
2474
|
+
actor: options?.actor ?? "user",
|
|
2475
|
+
});
|
|
2476
|
+
}
|
|
2477
|
+
return { withdrawn, missingIds };
|
|
2478
|
+
});
|
|
2331
2479
|
}
|
|
2332
2480
|
/**
|
|
2333
2481
|
* Rewrites a waiting message, in place, where the user can see it change.
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { Store } from "../store.js";
|
|
2
2
|
import type { ConversationService } from "./conversation-service.js";
|
|
3
|
-
import type
|
|
3
|
+
import { type RunService } from "./run-service.js";
|
|
4
4
|
import type { SessionLedger } from "../session-ledger.js";
|
|
5
5
|
import type { EventBroadcaster } from "../server-context.js";
|
|
6
|
-
import type { RunStatus } from "../types.js";
|
|
6
|
+
import type { MessageDeliveryMode, RunStatus } from "../types.js";
|
|
7
7
|
import { SessionWatchdog } from "./session-watchdog.js";
|
|
8
8
|
export declare const SESSION_OUTPUT_CHAR_LIMIT: number;
|
|
9
9
|
export declare const sessionResultRelativePath: (conversationId: string) => string;
|
|
@@ -66,10 +66,12 @@ export interface ReportSessionOptions {
|
|
|
66
66
|
status?: "in_progress" | "completed" | "failed";
|
|
67
67
|
}
|
|
68
68
|
export interface StopSessionOptions {
|
|
69
|
-
/**
|
|
69
|
+
/** Whether to stop only the running turn ('active'), only queued messages ('queued'), or both ('all', default). */
|
|
70
70
|
mode?: "active" | "queued" | "all";
|
|
71
|
-
/** Optional specific
|
|
71
|
+
/** Optional specific queued message ID to interrupt or cancel. */
|
|
72
72
|
messageId?: string;
|
|
73
|
+
/** Optional list of specific queued message IDs to selectively withdraw while keeping other queued/steered messages intact. */
|
|
74
|
+
messageIds?: string[];
|
|
73
75
|
}
|
|
74
76
|
export interface SessionSummary {
|
|
75
77
|
conversationId: string;
|
|
@@ -95,6 +97,12 @@ export interface SessionSummary {
|
|
|
95
97
|
output?: string;
|
|
96
98
|
outputPath?: string;
|
|
97
99
|
outputTruncated?: boolean;
|
|
100
|
+
queuedMessages?: {
|
|
101
|
+
id: string;
|
|
102
|
+
delivery: "waiting" | "sending";
|
|
103
|
+
role: "user" | "tool";
|
|
104
|
+
snippet: string;
|
|
105
|
+
}[];
|
|
98
106
|
}
|
|
99
107
|
/**
|
|
100
108
|
* Orchestrator service coordinating sibling and child conversations.
|
|
@@ -108,6 +116,7 @@ export declare class SessionOrchestratorService {
|
|
|
108
116
|
private readonly runService;
|
|
109
117
|
private readonly ledger;
|
|
110
118
|
private readonly broadcaster?;
|
|
119
|
+
private readonly uploadsDir?;
|
|
111
120
|
readonly watchdog: SessionWatchdog;
|
|
112
121
|
/**
|
|
113
122
|
* HOW MANY TIMES EACH CHILD HAS BEEN TOLD TO CONTINUE AFTER A TRUNCATION.
|
|
@@ -142,7 +151,7 @@ export declare class SessionOrchestratorService {
|
|
|
142
151
|
* result is still its own.
|
|
143
152
|
*/
|
|
144
153
|
private readonly reportedChildren;
|
|
145
|
-
constructor(store: Store, conversationService: ConversationService, runService: RunService, ledger: SessionLedger, broadcaster?: EventBroadcaster | undefined, watchdog?: SessionWatchdog);
|
|
154
|
+
constructor(store: Store, conversationService: ConversationService, runService: RunService, ledger: SessionLedger, broadcaster?: EventBroadcaster | undefined, watchdog?: SessionWatchdog, uploadsDir?: string | undefined);
|
|
146
155
|
/**
|
|
147
156
|
* The watchdog timer fired: the child has been silent for longer than the parent declared.
|
|
148
157
|
* Delivers a reminder message to the parent session. Does NOT interrupt or abort the child session.
|
|
@@ -160,8 +169,11 @@ export declare class SessionOrchestratorService {
|
|
|
160
169
|
*/
|
|
161
170
|
dispatch(conversationId: string, content: string, options?: {
|
|
162
171
|
wait?: boolean;
|
|
172
|
+
mode?: MessageDeliveryMode;
|
|
163
173
|
}): Promise<{
|
|
164
174
|
conversationId: string;
|
|
175
|
+
messageId?: string;
|
|
176
|
+
mode: MessageDeliveryMode;
|
|
165
177
|
}>;
|
|
166
178
|
/**
|
|
167
179
|
* THE CHILD'S OUTPUT WAS CUT OFF. TELL IT TO CARRY ON, AND LET IT.
|
|
@@ -238,6 +250,12 @@ export declare class SessionOrchestratorService {
|
|
|
238
250
|
stop(conversationId: string, options?: StopSessionOptions): Promise<{
|
|
239
251
|
stopped: boolean;
|
|
240
252
|
mode: string;
|
|
253
|
+
withdrawn?: {
|
|
254
|
+
id: string;
|
|
255
|
+
content: string;
|
|
256
|
+
role: "user" | "tool";
|
|
257
|
+
}[];
|
|
258
|
+
missingIds?: string[];
|
|
241
259
|
}>;
|
|
242
260
|
/**
|
|
243
261
|
* THE SESSIONS UNDER ONE SESSION, DEEPEST LAST.
|
|
@@ -275,6 +293,21 @@ export declare class SessionOrchestratorService {
|
|
|
275
293
|
stoppedIds: string[];
|
|
276
294
|
descendantCount: number;
|
|
277
295
|
}>;
|
|
296
|
+
/**
|
|
297
|
+
* Permanently destroys a direct child session and its entire descendant subtree (stopping any
|
|
298
|
+
* running/queued turns first) and removes all transcripts, outputs, runs, and auxiliary state.
|
|
299
|
+
*/
|
|
300
|
+
remove(conversationId: string, options?: {
|
|
301
|
+
reason?: string;
|
|
302
|
+
}): Promise<{
|
|
303
|
+
removed: boolean;
|
|
304
|
+
alreadyRemoved: boolean;
|
|
305
|
+
conversationId: string;
|
|
306
|
+
title?: string;
|
|
307
|
+
destroyedIds: string[];
|
|
308
|
+
stoppedFirst: boolean;
|
|
309
|
+
reason?: string;
|
|
310
|
+
}>;
|
|
278
311
|
/**
|
|
279
312
|
* Resumes execution in a conversation.
|
|
280
313
|
* If prompt is provided, sends it as a new turn.
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
1
|
+
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
+
import { parseMessageDeliveryMode } from "./run-service.js";
|
|
4
|
+
import { deleteConversationUploads } from "../upload.js";
|
|
3
5
|
import { capText } from "../workflow.js";
|
|
4
6
|
import { assertSessionCwdInsideWorkspace } from "../session-cwd.js";
|
|
5
7
|
import { SessionWatchdog } from "./session-watchdog.js";
|
|
@@ -28,6 +30,7 @@ export class SessionOrchestratorService {
|
|
|
28
30
|
runService;
|
|
29
31
|
ledger;
|
|
30
32
|
broadcaster;
|
|
33
|
+
uploadsDir;
|
|
31
34
|
watchdog;
|
|
32
35
|
/**
|
|
33
36
|
* HOW MANY TIMES EACH CHILD HAS BEEN TOLD TO CONTINUE AFTER A TRUNCATION.
|
|
@@ -62,12 +65,13 @@ export class SessionOrchestratorService {
|
|
|
62
65
|
* result is still its own.
|
|
63
66
|
*/
|
|
64
67
|
reportedChildren = new Map();
|
|
65
|
-
constructor(store, conversationService, runService, ledger, broadcaster, watchdog) {
|
|
68
|
+
constructor(store, conversationService, runService, ledger, broadcaster, watchdog, uploadsDir) {
|
|
66
69
|
this.store = store;
|
|
67
70
|
this.conversationService = conversationService;
|
|
68
71
|
this.runService = runService;
|
|
69
72
|
this.ledger = ledger;
|
|
70
73
|
this.broadcaster = broadcaster;
|
|
74
|
+
this.uploadsDir = uploadsDir;
|
|
71
75
|
this.watchdog = watchdog ?? new SessionWatchdog({
|
|
72
76
|
onTimeout: async (event) => {
|
|
73
77
|
await this.handleWatchdogTimeout(event);
|
|
@@ -181,6 +185,10 @@ export class SessionOrchestratorService {
|
|
|
181
185
|
* By default dispatches asynchronously so the orchestrator caller is not blocked.
|
|
182
186
|
*/
|
|
183
187
|
async dispatch(conversationId, content, options) {
|
|
188
|
+
const mode = parseMessageDeliveryMode(options?.mode);
|
|
189
|
+
if (!content.trim()) {
|
|
190
|
+
throw new Error("dispatch requires non-empty content");
|
|
191
|
+
}
|
|
184
192
|
const conversation = this.store.conversation(conversationId);
|
|
185
193
|
if (!conversation) {
|
|
186
194
|
throw new Error(`找不到会话:${conversationId}`);
|
|
@@ -197,7 +205,19 @@ export class SessionOrchestratorService {
|
|
|
197
205
|
// turn?" is answered by comparing the report's time against it. A report made inside the turn
|
|
198
206
|
// speaks for the turn; a report made before it speaks for an earlier one.
|
|
199
207
|
const turnStartedAt = Date.now();
|
|
200
|
-
|
|
208
|
+
let acceptedMessageId;
|
|
209
|
+
let resolveAccepted;
|
|
210
|
+
const acceptedPromise = new Promise((resolve) => {
|
|
211
|
+
resolveAccepted = resolve;
|
|
212
|
+
});
|
|
213
|
+
const runPromise = this.runService.enqueueAndRunAsAgent(conversationId, content, {
|
|
214
|
+
mode,
|
|
215
|
+
onAccepted: (messageId) => {
|
|
216
|
+
acceptedMessageId = messageId;
|
|
217
|
+
resolveAccepted?.(messageId);
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
void runPromise.then(() => { resolveAccepted?.(acceptedMessageId ?? ""); }, () => { resolveAccepted?.(acceptedMessageId ?? ""); });
|
|
201
221
|
const completion = (async () => {
|
|
202
222
|
let failure;
|
|
203
223
|
try {
|
|
@@ -299,9 +319,11 @@ export class SessionOrchestratorService {
|
|
|
299
319
|
})();
|
|
300
320
|
if (options?.wait)
|
|
301
321
|
await completion;
|
|
302
|
-
else
|
|
322
|
+
else {
|
|
303
323
|
void completion.catch(() => undefined);
|
|
304
|
-
|
|
324
|
+
await acceptedPromise;
|
|
325
|
+
}
|
|
326
|
+
return { conversationId, ...(acceptedMessageId ? { messageId: acceptedMessageId } : {}), mode };
|
|
305
327
|
}
|
|
306
328
|
/**
|
|
307
329
|
* THE CHILD'S OUTPUT WAS CUT OFF. TELL IT TO CARRY ON, AND LET IT.
|
|
@@ -565,6 +587,15 @@ export class SessionOrchestratorService {
|
|
|
565
587
|
...(conversation.error ? { error: conversation.error } : {}),
|
|
566
588
|
});
|
|
567
589
|
}
|
|
590
|
+
const waiting = this.store.listWaitingMessages(conversationId);
|
|
591
|
+
const queuedMessages = waiting.length > 0
|
|
592
|
+
? waiting.map((item) => ({
|
|
593
|
+
id: item.id,
|
|
594
|
+
delivery: item.delivery === "sending" ? "sending" : "waiting",
|
|
595
|
+
role: item.role === "tool" ? "tool" : "user",
|
|
596
|
+
snippet: item.content.replace(/\s+/g, " ").trim().slice(0, 120),
|
|
597
|
+
}))
|
|
598
|
+
: undefined;
|
|
568
599
|
return {
|
|
569
600
|
conversationId: conversation.id,
|
|
570
601
|
workspaceId: conversation.workspaceId,
|
|
@@ -583,6 +614,7 @@ export class SessionOrchestratorService {
|
|
|
583
614
|
updatedAt: conversation.updatedAt,
|
|
584
615
|
createdAt: ledgerEntry?.createdAt ?? (conversation.messages?.[0]?.createdAt ? new Date(conversation.messages[0].createdAt).getTime() : (conversation.updatedAt ? new Date(conversation.updatedAt).getTime() : Date.now())),
|
|
585
616
|
lastProgress: finalProgress,
|
|
617
|
+
...(queuedMessages ? { queuedMessages } : {}),
|
|
586
618
|
...(options?.includeOutput ? { outputPath: resultPath } : {}),
|
|
587
619
|
...(output !== undefined ? { output, outputTruncated } : {}),
|
|
588
620
|
};
|
|
@@ -637,6 +669,15 @@ export class SessionOrchestratorService {
|
|
|
637
669
|
const parentConv = targetParentId ? this.store.conversation(targetParentId) : undefined;
|
|
638
670
|
const summaries = [];
|
|
639
671
|
for (const conversationId of candidateIds) {
|
|
672
|
+
const conv = this.store.conversation(conversationId);
|
|
673
|
+
if (!conv) {
|
|
674
|
+
// Conversation was removed from store; prune ledger
|
|
675
|
+
this.ledger.delete(conversationId);
|
|
676
|
+
continue;
|
|
677
|
+
}
|
|
678
|
+
if (conv.archivedAt) {
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
640
681
|
try {
|
|
641
682
|
const summary = await this.status(conversationId);
|
|
642
683
|
// Workspace boundary enforcement: do not leak sessions across different workspaces
|
|
@@ -669,6 +710,29 @@ export class SessionOrchestratorService {
|
|
|
669
710
|
if (!conversation) {
|
|
670
711
|
throw new Error(`找不到会话:${conversationId}`);
|
|
671
712
|
}
|
|
713
|
+
if (options?.messageIds !== undefined) {
|
|
714
|
+
if (options.messageId) {
|
|
715
|
+
throw new Error("不能同时传入 messageId 和 messageIds。");
|
|
716
|
+
}
|
|
717
|
+
if (!Array.isArray(options.messageIds) || options.messageIds.length === 0) {
|
|
718
|
+
throw new Error("messageIds 必须是非空字符串数组。");
|
|
719
|
+
}
|
|
720
|
+
for (const rawId of options.messageIds) {
|
|
721
|
+
if (typeof rawId !== "string" || !rawId.trim()) {
|
|
722
|
+
throw new Error("messageIds 中的每一项都必须是非空消息 ID。");
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
const { withdrawn, missingIds } = await this.runService.cancelQueuedMany(conversationId, options.messageIds, { actor: "agent" });
|
|
726
|
+
if (withdrawn.length === 0) {
|
|
727
|
+
throw new Error("指定的排队消息不存在、已发送或已被撤回。");
|
|
728
|
+
}
|
|
729
|
+
return {
|
|
730
|
+
stopped: false,
|
|
731
|
+
mode: "queued-withdraw",
|
|
732
|
+
withdrawn,
|
|
733
|
+
...(missingIds.length > 0 ? { missingIds } : {}),
|
|
734
|
+
};
|
|
735
|
+
}
|
|
672
736
|
let mode = options?.mode ?? "all";
|
|
673
737
|
if (options?.messageId) {
|
|
674
738
|
await this.runService.interruptQueued(conversationId, options.messageId);
|
|
@@ -787,6 +851,104 @@ export class SessionOrchestratorService {
|
|
|
787
851
|
descendantCount: descendants.length,
|
|
788
852
|
};
|
|
789
853
|
}
|
|
854
|
+
/**
|
|
855
|
+
* Permanently destroys a direct child session and its entire descendant subtree (stopping any
|
|
856
|
+
* running/queued turns first) and removes all transcripts, outputs, runs, and auxiliary state.
|
|
857
|
+
*/
|
|
858
|
+
async remove(conversationId, options) {
|
|
859
|
+
const reason = options?.reason?.trim() || undefined;
|
|
860
|
+
const conversation = this.store.conversation(conversationId);
|
|
861
|
+
if (!conversation) {
|
|
862
|
+
this.ledger.delete(conversationId);
|
|
863
|
+
this.reportedChildren.delete(conversationId);
|
|
864
|
+
await this.watchdog.cancel(conversationId);
|
|
865
|
+
return {
|
|
866
|
+
removed: false,
|
|
867
|
+
alreadyRemoved: true,
|
|
868
|
+
conversationId,
|
|
869
|
+
destroyedIds: [],
|
|
870
|
+
stoppedFirst: false,
|
|
871
|
+
...(reason ? { reason } : {}),
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
if (!conversation.parentConversationId) {
|
|
875
|
+
throw new Error("顶层会话不能销毁,只能由用户归档。");
|
|
876
|
+
}
|
|
877
|
+
const descendants = this.descendantsOf(conversationId);
|
|
878
|
+
const subtreeIdsDeepestFirst = [...descendants.reverse(), conversationId];
|
|
879
|
+
let stoppedFirst = false;
|
|
880
|
+
for (const id of subtreeIdsDeepestFirst) {
|
|
881
|
+
const target = this.store.conversation(id);
|
|
882
|
+
if (!target)
|
|
883
|
+
continue;
|
|
884
|
+
const waiting = this.store.listWaitingMessages(id);
|
|
885
|
+
const isActive = target.status === "running" ||
|
|
886
|
+
target.status === "waiting" ||
|
|
887
|
+
target.status === "awaiting_background" ||
|
|
888
|
+
Boolean(this.runService.isRunning?.(id)) ||
|
|
889
|
+
waiting.length > 0;
|
|
890
|
+
if (isActive) {
|
|
891
|
+
await this.stop(id, { mode: "all" });
|
|
892
|
+
stoppedFirst = true;
|
|
893
|
+
}
|
|
894
|
+
else {
|
|
895
|
+
await this.watchdog.cancel(id);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
for (const id of subtreeIdsDeepestFirst) {
|
|
899
|
+
const afterStop = this.store.conversation(id);
|
|
900
|
+
if (!afterStop)
|
|
901
|
+
continue;
|
|
902
|
+
if (afterStop.status === "running" ||
|
|
903
|
+
afterStop.status === "awaiting_background" ||
|
|
904
|
+
Boolean(this.runService.isRunning?.(id))) {
|
|
905
|
+
throw new Error("目标会话或其下属会话仍在运行且未能停止,无法销毁。");
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
for (const id of subtreeIdsDeepestFirst) {
|
|
909
|
+
this.runService.reviewMemoryBoundary?.(id);
|
|
910
|
+
await this.conversationService.prepareDestroyConversation?.(id);
|
|
911
|
+
}
|
|
912
|
+
const { destroyed } = await this.store.deleteConversationSubtree(conversationId);
|
|
913
|
+
if (destroyed.length === 0) {
|
|
914
|
+
return {
|
|
915
|
+
removed: false,
|
|
916
|
+
alreadyRemoved: true,
|
|
917
|
+
conversationId,
|
|
918
|
+
title: conversation.title,
|
|
919
|
+
destroyedIds: [],
|
|
920
|
+
stoppedFirst,
|
|
921
|
+
...(reason ? { reason } : {}),
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
const remainingSessionFiles = new Set(this.store.snapshot().conversations.map((item) => item.piSessionFile).filter((file) => Boolean(file)));
|
|
925
|
+
for (const item of destroyed) {
|
|
926
|
+
this.ledger.delete(item.id);
|
|
927
|
+
this.reportedChildren.delete(item.id);
|
|
928
|
+
await this.watchdog.cancel(item.id);
|
|
929
|
+
const workspace = this.store.workspace(item.workspaceId);
|
|
930
|
+
if (workspace) {
|
|
931
|
+
await rm(join(workspace.path, ".super-pi", "session-results", `${item.id}.md`), { force: true }).catch(() => { });
|
|
932
|
+
}
|
|
933
|
+
if (item.piSessionFile && !remainingSessionFiles.has(item.piSessionFile)) {
|
|
934
|
+
await rm(item.piSessionFile, { force: true }).catch(() => { });
|
|
935
|
+
}
|
|
936
|
+
if (this.uploadsDir) {
|
|
937
|
+
await deleteConversationUploads(this.uploadsDir, item.id).catch(() => { });
|
|
938
|
+
}
|
|
939
|
+
await this.conversationService.cleanupDestroyedConversation?.(item);
|
|
940
|
+
}
|
|
941
|
+
this.broadcaster?.broadcastState();
|
|
942
|
+
return {
|
|
943
|
+
removed: true,
|
|
944
|
+
alreadyRemoved: false,
|
|
945
|
+
conversationId,
|
|
946
|
+
title: conversation.title,
|
|
947
|
+
destroyedIds: destroyed.map((item) => item.id),
|
|
948
|
+
stoppedFirst,
|
|
949
|
+
...(reason ? { reason } : {}),
|
|
950
|
+
};
|
|
951
|
+
}
|
|
790
952
|
/**
|
|
791
953
|
* Resumes execution in a conversation.
|
|
792
954
|
* If prompt is provided, sends it as a new turn.
|
|
@@ -273,7 +273,7 @@ export class CommandHandler {
|
|
|
273
273
|
// attachment lost it on arrival - the browser drew the attachment, the transcript never had it, and
|
|
274
274
|
// the model was handed the bare prompt. Forwarded now because this is the one line that carries
|
|
275
275
|
// every field of the send.
|
|
276
|
-
await this.runService.enqueueAndRun(command.conversationId, command.content, command.images, command.attachments, command.clientId);
|
|
276
|
+
await this.runService.enqueueAndRun(command.conversationId, command.content, command.images, command.attachments, command.clientId, command.mode);
|
|
277
277
|
return;
|
|
278
278
|
}
|
|
279
279
|
if (command.type === "message.steer") {
|
|
@@ -292,6 +292,10 @@ export class CommandHandler {
|
|
|
292
292
|
await this.runService.cancelQueued(command.conversationId, command.messageId);
|
|
293
293
|
return;
|
|
294
294
|
}
|
|
295
|
+
if (command.type === "message.cancelMany") {
|
|
296
|
+
await this.runService.cancelQueuedMany(command.conversationId, command.messageIds, { actor: "user" });
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
295
299
|
if (command.type === "message.edit") {
|
|
296
300
|
await this.runService.editQueued(command.conversationId, command.messageId, command.content);
|
|
297
301
|
return;
|