@bastani/atomic-acp 0.0.16 → 0.0.17
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 +14 -22
- package/dist/index.js +80 -316
- package/dist/index.js.map +1 -1
- package/package.json +3 -4
package/dist/index.js
CHANGED
|
@@ -419,6 +419,11 @@ var AtomicRpcProcess = class _AtomicRpcProcess {
|
|
|
419
419
|
const res = await this.request({ type: "abort" });
|
|
420
420
|
if (!res.success) throw new Error(`Atomic abort failed: ${res.error ?? JSON.stringify(res.data)}`);
|
|
421
421
|
}
|
|
422
|
+
/** Take back the steering and follow-up messages Atomic has queued, so no later run reads them. */
|
|
423
|
+
async clearQueue() {
|
|
424
|
+
const res = await this.request({ type: "clear_queue" });
|
|
425
|
+
if (!res.success) throw new Error(`Atomic clear_queue failed: ${res.error ?? JSON.stringify(res.data)}`);
|
|
426
|
+
}
|
|
422
427
|
writeExtensionUiResponse(response) {
|
|
423
428
|
return this.writeJsonLine(response);
|
|
424
429
|
}
|
|
@@ -529,17 +534,6 @@ var AtomicRpcProcess = class _AtomicRpcProcess {
|
|
|
529
534
|
}
|
|
530
535
|
});
|
|
531
536
|
}
|
|
532
|
-
async listWorkflowRuns(status) {
|
|
533
|
-
const res = await this.request({ type: "list_workflow_runs", ...status ? { status } : {} });
|
|
534
|
-
if (!res.success) throw new Error(`Atomic list_workflow_runs failed: ${res.error ?? JSON.stringify(res.data)}`);
|
|
535
|
-
return res.data;
|
|
536
|
-
}
|
|
537
|
-
async getWorkflowRunSummary(runId) {
|
|
538
|
-
const res = await this.request({ type: "get_workflow_run_summary", runId });
|
|
539
|
-
if (!res.success)
|
|
540
|
-
throw new Error(`Atomic get_workflow_run_summary failed: ${res.error ?? JSON.stringify(res.data)}`);
|
|
541
|
-
return res.data;
|
|
542
|
-
}
|
|
543
537
|
request(cmd, opts) {
|
|
544
538
|
if (this.exitError) return Promise.reject(this.exitError);
|
|
545
539
|
const id = crypto.randomUUID();
|
|
@@ -1345,29 +1339,29 @@ function isBashTool(name) {
|
|
|
1345
1339
|
return name.toLowerCase() === "bash";
|
|
1346
1340
|
}
|
|
1347
1341
|
function bashCommand(value) {
|
|
1348
|
-
const
|
|
1349
|
-
for (const source of [
|
|
1342
|
+
const record = value;
|
|
1343
|
+
for (const source of [record, record?.args, record?.input, record?.rawInput, record?.toolInput, record?.details]) {
|
|
1350
1344
|
const command = source?.command ?? source?.cmd;
|
|
1351
1345
|
if (typeof command === "string" && command.trim()) return command;
|
|
1352
1346
|
}
|
|
1353
1347
|
return void 0;
|
|
1354
1348
|
}
|
|
1355
1349
|
function bashResultText(value) {
|
|
1356
|
-
const
|
|
1357
|
-
if (Array.isArray(
|
|
1358
|
-
const text =
|
|
1350
|
+
const record = value;
|
|
1351
|
+
if (Array.isArray(record?.content)) {
|
|
1352
|
+
const text = record.content.map(
|
|
1359
1353
|
(block) => block.type === "text" && typeof block.text === "string" ? block.text : ""
|
|
1360
1354
|
).join("");
|
|
1361
1355
|
if (text) return text;
|
|
1362
1356
|
}
|
|
1363
|
-
const details =
|
|
1364
|
-
const stdout = details?.stdout ??
|
|
1365
|
-
const stderr = details?.stderr ??
|
|
1357
|
+
const details = record?.details;
|
|
1358
|
+
const stdout = details?.stdout ?? record?.stdout ?? details?.output ?? record?.output;
|
|
1359
|
+
const stderr = details?.stderr ?? record?.stderr;
|
|
1366
1360
|
return [stdout, stderr].filter((part) => typeof part === "string" && part.length > 0).join("\n");
|
|
1367
1361
|
}
|
|
1368
1362
|
function bashExitCode(value, isError) {
|
|
1369
|
-
const
|
|
1370
|
-
const code =
|
|
1363
|
+
const record = value;
|
|
1364
|
+
const code = record?.details?.exitCode ?? record?.exitCode ?? record?.details?.code ?? record?.code;
|
|
1371
1365
|
return typeof code === "number" ? code : isError ? 1 : 0;
|
|
1372
1366
|
}
|
|
1373
1367
|
function bashOutputDelta(previous, next) {
|
|
@@ -1386,74 +1380,10 @@ function bashTerminalExitMeta(id, exitCode) {
|
|
|
1386
1380
|
return { terminal_exit: { terminal_id: id, exit_code: exitCode, signal: null } };
|
|
1387
1381
|
}
|
|
1388
1382
|
|
|
1389
|
-
// src/acp/translate/workflow-events.ts
|
|
1390
|
-
var TERMINAL_NOTICE_KINDS = /* @__PURE__ */ new Set(["completed", "failed", "blocked", "quit", "cancelled", "killed", "skipped"]);
|
|
1391
|
-
function str2(value) {
|
|
1392
|
-
return typeof value === "string" && value ? value : void 0;
|
|
1393
|
-
}
|
|
1394
|
-
function record(value) {
|
|
1395
|
-
return value !== null && typeof value === "object" && !Array.isArray(value) ? value : void 0;
|
|
1396
|
-
}
|
|
1397
|
-
function parseWorkflowEvent(ev) {
|
|
1398
|
-
if (ev.type === "entry_appended") {
|
|
1399
|
-
const entry = record(ev.entry);
|
|
1400
|
-
const data = record(entry?.data);
|
|
1401
|
-
const runId = str2(data?.runId);
|
|
1402
|
-
if (!entry || !data || !runId) return null;
|
|
1403
|
-
switch (entry.customType) {
|
|
1404
|
-
case "workflow.run.start":
|
|
1405
|
-
return { kind: "run_start", runId, name: str2(data.name) };
|
|
1406
|
-
case "workflow.run.end":
|
|
1407
|
-
return {
|
|
1408
|
-
kind: "run_end",
|
|
1409
|
-
runId,
|
|
1410
|
-
status: str2(data.status) ?? "completed",
|
|
1411
|
-
...data.result !== void 0 ? { result: data.result } : {},
|
|
1412
|
-
...str2(data.error) ? { error: str2(data.error) } : {}
|
|
1413
|
-
};
|
|
1414
|
-
case "workflow.run.blocked":
|
|
1415
|
-
return {
|
|
1416
|
-
kind: "run_end",
|
|
1417
|
-
runId,
|
|
1418
|
-
status: "blocked",
|
|
1419
|
-
...str2(data.error) ? { error: str2(data.error) } : {}
|
|
1420
|
-
};
|
|
1421
|
-
case "workflow.stage.start":
|
|
1422
|
-
return {
|
|
1423
|
-
kind: "stage_start",
|
|
1424
|
-
runId,
|
|
1425
|
-
stageId: str2(data.stageId),
|
|
1426
|
-
name: str2(data.name),
|
|
1427
|
-
// Human-input prompt nodes (ctx.ui.input/confirm/select/editor/custom) use `prompt:` replay keys.
|
|
1428
|
-
humanInput: str2(data.replayKey)?.startsWith("prompt:") ?? false
|
|
1429
|
-
};
|
|
1430
|
-
case "workflow.stage.end":
|
|
1431
|
-
return { kind: "stage_end", runId, stageId: str2(data.stageId), status: str2(data.status) };
|
|
1432
|
-
default:
|
|
1433
|
-
return null;
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1436
|
-
if (ev.type === "message_end") {
|
|
1437
|
-
const message = record(ev.message);
|
|
1438
|
-
if (message?.role !== "custom" || message.customType !== "workflows:lifecycle-notice") return null;
|
|
1439
|
-
const details = record(message.details);
|
|
1440
|
-
const runId = str2(details?.runId);
|
|
1441
|
-
const kind = str2(details?.kind);
|
|
1442
|
-
if (!runId || !kind || details?.scope === "stage") return null;
|
|
1443
|
-
if (kind === "started") return { kind: "run_start", runId, name: str2(details?.workflowName) };
|
|
1444
|
-
if (TERMINAL_NOTICE_KINDS.has(kind)) return { kind: "run_end", runId, status: kind };
|
|
1445
|
-
}
|
|
1446
|
-
return null;
|
|
1447
|
-
}
|
|
1448
|
-
|
|
1449
1383
|
// src/acp/session.ts
|
|
1450
1384
|
function atomicExitReason(err) {
|
|
1451
1385
|
return err instanceof AtomicProcessExitError ? `Atomic exited (code=${err.exitCode}, signal=${err.signal})` : `Atomic exited: ${err.message}`;
|
|
1452
1386
|
}
|
|
1453
|
-
var WORKFLOW_FOLLOW_UP_GRACE_MS = 3e3;
|
|
1454
|
-
function waitForWorkflowsFromEnv() {
|
|
1455
|
-
return !/^(0|false|no)$/i.test(process.env.ATOMIC_ACP_WAIT_FOR_WORKFLOWS?.trim() ?? "");
|
|
1456
|
-
}
|
|
1457
1387
|
function toUsageUpdate(stats) {
|
|
1458
1388
|
const used = stats?.contextUsage?.tokens;
|
|
1459
1389
|
const size = stats?.contextUsage?.contextWindow;
|
|
@@ -1474,11 +1404,11 @@ function findUniqueLineNumber(text, needle) {
|
|
|
1474
1404
|
return line;
|
|
1475
1405
|
}
|
|
1476
1406
|
function getMutationPaths(args) {
|
|
1477
|
-
const
|
|
1478
|
-
const path =
|
|
1407
|
+
const record = args;
|
|
1408
|
+
const path = record?.path ?? record?.file_path;
|
|
1479
1409
|
if (typeof path === "string") return [path];
|
|
1480
|
-
if (typeof
|
|
1481
|
-
return [...
|
|
1410
|
+
if (typeof record?.input !== "string") return [];
|
|
1411
|
+
return [...record.input.matchAll(/^\[(.+)#[0-9a-fA-F]{4}\]\s*$/gm)].map((match) => match[1]);
|
|
1482
1412
|
}
|
|
1483
1413
|
function toToolCallLocations(args, cwd, line) {
|
|
1484
1414
|
const paths = getMutationPaths(args);
|
|
@@ -1513,6 +1443,7 @@ var SessionManager = class {
|
|
|
1513
1443
|
const s = this.sessions.get(sessionId);
|
|
1514
1444
|
if (!s) return Promise.resolve();
|
|
1515
1445
|
this.sessions.delete(sessionId);
|
|
1446
|
+
s.close();
|
|
1516
1447
|
try {
|
|
1517
1448
|
return Promise.resolve(s.proc.dispose?.()).catch(() => {
|
|
1518
1449
|
});
|
|
@@ -1627,13 +1558,11 @@ var AtomicAcpSession = class {
|
|
|
1627
1558
|
// Set once Atomic exits; every later prompt to this process fails with it.
|
|
1628
1559
|
atomicExit = null;
|
|
1629
1560
|
pendingExtensionDialogs = /* @__PURE__ */ new Map();
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
waitingForWorkflows = false;
|
|
1636
|
-
workflowGraceTimer;
|
|
1561
|
+
// The turn whose settlement is under way, so a cancel does not settle it a second time.
|
|
1562
|
+
settlingTurn = null;
|
|
1563
|
+
// Set once the adapter closed this session: nothing more is sent for it.
|
|
1564
|
+
closed = false;
|
|
1565
|
+
detach = [];
|
|
1637
1566
|
constructor(opts) {
|
|
1638
1567
|
this.sessionId = opts.sessionId;
|
|
1639
1568
|
this.cwd = opts.cwd;
|
|
@@ -1642,12 +1571,11 @@ var AtomicAcpSession = class {
|
|
|
1642
1571
|
this.conn = opts.conn;
|
|
1643
1572
|
this.clientElicitationSupport = opts.clientElicitationSupport ?? NO_CLIENT_ELICITATION_SUPPORT;
|
|
1644
1573
|
this.headless = opts.headless ?? false;
|
|
1645
|
-
this.waitForWorkflows = opts.waitForWorkflows ?? waitForWorkflowsFromEnv();
|
|
1646
|
-
this.workflowFollowUpGraceMs = opts.workflowFollowUpGraceMs ?? WORKFLOW_FOLLOW_UP_GRACE_MS;
|
|
1647
1574
|
this.fileCommands = opts.fileCommands ?? [];
|
|
1648
1575
|
this.initialTitlePending = opts.autoTitle ?? false;
|
|
1649
|
-
this.proc.onEvent((ev) => this.handleAtomicEvent(ev));
|
|
1650
|
-
this.proc.onExit?.((err) => this.handleAtomicExit(err));
|
|
1576
|
+
this.detach.push(this.proc.onEvent((ev) => this.handleAtomicEvent(ev)));
|
|
1577
|
+
const offExit = this.proc.onExit?.((err) => this.handleAtomicExit(err));
|
|
1578
|
+
if (offExit) this.detach.push(offExit);
|
|
1651
1579
|
}
|
|
1652
1580
|
setStartupInfo(text) {
|
|
1653
1581
|
this.startupInfo = text;
|
|
@@ -1696,6 +1624,10 @@ var AtomicAcpSession = class {
|
|
|
1696
1624
|
});
|
|
1697
1625
|
return turnPromise;
|
|
1698
1626
|
}
|
|
1627
|
+
/**
|
|
1628
|
+
* Cancel the running prompt and the queued ones. Like Esc in Atomic, this stops the session's current
|
|
1629
|
+
* run only: subagents and background shells it launched keep running.
|
|
1630
|
+
*/
|
|
1699
1631
|
async cancel() {
|
|
1700
1632
|
this.cancelRequested = true;
|
|
1701
1633
|
if (this.turnQueue.length) {
|
|
@@ -1711,19 +1643,30 @@ var AtomicAcpSession = class {
|
|
|
1711
1643
|
});
|
|
1712
1644
|
}
|
|
1713
1645
|
await this.cancelPendingExtensionDialogs();
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
run.lines.push("run cancelled");
|
|
1719
|
-
this.publishWorkflowRun(runId, run);
|
|
1720
|
-
this.emitAgentText(`Quitting workflow "${run.name}" (run ${runId}).`);
|
|
1721
|
-
void this.proc.prompt(`/workflow quit ${runId}`, [], "followUp").catch(() => {
|
|
1722
|
-
this.emitAgentText(`Could not quit workflow run ${runId}.`);
|
|
1723
|
-
});
|
|
1646
|
+
const target = this.pendingTurn;
|
|
1647
|
+
if (!target) {
|
|
1648
|
+
await this.proc.abort();
|
|
1649
|
+
return;
|
|
1724
1650
|
}
|
|
1651
|
+
await target.accepted;
|
|
1652
|
+
if (this.pendingTurn !== target) return;
|
|
1653
|
+
await this.proc.clearQueue().catch(() => {
|
|
1654
|
+
});
|
|
1725
1655
|
await this.proc.abort();
|
|
1726
|
-
if (this.pendingTurn && this.
|
|
1656
|
+
if (this.pendingTurn === target && this.settlingTurn !== target && !this.inAgentLoop) await this.settleTurn();
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* The adapter stopped this session's Atomic (a reload, another session, a delete): its prompts end as
|
|
1660
|
+
* cancelled now, and nothing more is sent for it, since a session loaded again keeps the same id.
|
|
1661
|
+
*/
|
|
1662
|
+
close() {
|
|
1663
|
+
if (this.closed) return;
|
|
1664
|
+
this.closed = true;
|
|
1665
|
+
for (const off of this.detach.splice(0)) off();
|
|
1666
|
+
const turns = [this.pendingTurn, ...this.turnQueue.splice(0)];
|
|
1667
|
+
this.pendingTurn = null;
|
|
1668
|
+
for (const turn of turns) turn?.resolve("cancelled");
|
|
1669
|
+
void this.cancelPendingExtensionDialogs();
|
|
1727
1670
|
}
|
|
1728
1671
|
async setSessionName(name) {
|
|
1729
1672
|
return this.enqueueTitleUpdate(async () => {
|
|
@@ -1766,6 +1709,7 @@ var AtomicAcpSession = class {
|
|
|
1766
1709
|
}
|
|
1767
1710
|
}
|
|
1768
1711
|
emit(update) {
|
|
1712
|
+
if (this.closed) return;
|
|
1769
1713
|
this.lastEmit = this.lastEmit.then(
|
|
1770
1714
|
() => this.conn.notify(methods2.client.session.update, {
|
|
1771
1715
|
sessionId: this.sessionId,
|
|
@@ -1822,21 +1766,24 @@ var AtomicAcpSession = class {
|
|
|
1822
1766
|
}
|
|
1823
1767
|
startTurn(t) {
|
|
1824
1768
|
this.cancelRequested = false;
|
|
1825
|
-
|
|
1826
|
-
|
|
1769
|
+
let accept;
|
|
1770
|
+
const pending = {
|
|
1771
|
+
resolve: t.resolve,
|
|
1772
|
+
reject: t.reject,
|
|
1773
|
+
accepted: new Promise((resolve2) => {
|
|
1774
|
+
accept = resolve2;
|
|
1775
|
+
})
|
|
1776
|
+
};
|
|
1777
|
+
this.pendingTurn = pending;
|
|
1827
1778
|
void this.maybeAutoTitle(t.title);
|
|
1828
1779
|
this.emit({
|
|
1829
1780
|
sessionUpdate: "session_info_update",
|
|
1830
1781
|
_meta: { atomicAcp: { queueDepth: this.turnQueue.length, running: true } }
|
|
1831
1782
|
});
|
|
1832
|
-
const pending = this.pendingTurn;
|
|
1833
1783
|
this.proc.prompt(t.message, t.images, "followUp").then(async (disposition) => {
|
|
1784
|
+
accept();
|
|
1834
1785
|
if (disposition !== "handled" || this.pendingTurn !== pending) return;
|
|
1835
1786
|
if (this.inAgentLoop) this.backgroundTurn = true;
|
|
1836
|
-
if (this.shouldWaitForWorkflows()) {
|
|
1837
|
-
this.beginWorkflowWait();
|
|
1838
|
-
return;
|
|
1839
|
-
}
|
|
1840
1787
|
await this.publishContextUsage();
|
|
1841
1788
|
if (this.pendingTurn !== pending) return;
|
|
1842
1789
|
pending.resolve(this.cancelRequested ? "cancelled" : "end_turn");
|
|
@@ -1850,6 +1797,7 @@ var AtomicAcpSession = class {
|
|
|
1850
1797
|
});
|
|
1851
1798
|
}
|
|
1852
1799
|
}).catch((err) => {
|
|
1800
|
+
accept();
|
|
1853
1801
|
void this.finishActiveAndQueuedFromFailure(err);
|
|
1854
1802
|
});
|
|
1855
1803
|
}
|
|
@@ -1857,18 +1805,13 @@ var AtomicAcpSession = class {
|
|
|
1857
1805
|
await this.cancelPendingExtensionDialogs();
|
|
1858
1806
|
await this.flushEmits();
|
|
1859
1807
|
if (!this.pendingTurn && this.turnQueue.length === 0) return;
|
|
1860
|
-
const failure = maybeAuthRequiredError(err) ??
|
|
1808
|
+
const failure = maybeAuthRequiredError(err) ?? this.promptFailure(err);
|
|
1861
1809
|
const queued = this.turnQueue.splice(0);
|
|
1862
|
-
if (
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
} else {
|
|
1866
|
-
this.pendingTurn?.resolve("cancelled");
|
|
1867
|
-
for (const queuedTurn of queued) queuedTurn.resolve("cancelled");
|
|
1868
|
-
}
|
|
1810
|
+
if (this.cancelRequested && !maybeAuthRequiredError(err)) this.pendingTurn?.resolve("cancelled");
|
|
1811
|
+
else this.pendingTurn?.reject(failure);
|
|
1812
|
+
for (const queuedTurn of queued) queuedTurn.reject(failure);
|
|
1869
1813
|
this.pendingTurn = null;
|
|
1870
1814
|
this.inAgentLoop = false;
|
|
1871
|
-
this.resetWorkflowWait();
|
|
1872
1815
|
this.emit({
|
|
1873
1816
|
sessionUpdate: "session_info_update",
|
|
1874
1817
|
_meta: { atomicAcp: { queueDepth: 0, running: false } }
|
|
@@ -1886,7 +1829,7 @@ var AtomicAcpSession = class {
|
|
|
1886
1829
|
if (err instanceof RequestError3) return err;
|
|
1887
1830
|
return RequestError3.internalError(void 0, err instanceof Error ? err.message : String(err));
|
|
1888
1831
|
}
|
|
1889
|
-
/** Close every tool call
|
|
1832
|
+
/** Close every tool call still open, so none is left in progress after Atomic dies. */
|
|
1890
1833
|
failOpenToolCalls(reason) {
|
|
1891
1834
|
for (const toolCallId of this.currentToolCalls.keys()) {
|
|
1892
1835
|
const bash = this.bashToolCallIds.has(toolCallId);
|
|
@@ -1904,13 +1847,6 @@ var AtomicAcpSession = class {
|
|
|
1904
1847
|
this.bashOutputSnapshots.clear();
|
|
1905
1848
|
this.fileSnapshots.clear();
|
|
1906
1849
|
this.fileMutationToolCallIds.clear();
|
|
1907
|
-
for (const [runId, run] of this.turnWorkflowRuns) {
|
|
1908
|
-
if (run.done) continue;
|
|
1909
|
-
run.done = true;
|
|
1910
|
-
run.status = "failed";
|
|
1911
|
-
run.lines.push(reason);
|
|
1912
|
-
this.publishWorkflowRun(runId, run);
|
|
1913
|
-
}
|
|
1914
1850
|
}
|
|
1915
1851
|
handleAtomicExit(err) {
|
|
1916
1852
|
this.atomicExit ??= err;
|
|
@@ -1993,12 +1929,13 @@ var AtomicAcpSession = class {
|
|
|
1993
1929
|
await completion;
|
|
1994
1930
|
}
|
|
1995
1931
|
async settleTurn() {
|
|
1996
|
-
this.
|
|
1932
|
+
this.settlingTurn = this.pendingTurn;
|
|
1997
1933
|
await this.cancelPendingExtensionDialogs();
|
|
1998
1934
|
await this.publishContextUsage();
|
|
1999
1935
|
const reason = this.cancelRequested ? "cancelled" : "end_turn";
|
|
2000
1936
|
this.pendingTurn?.resolve(reason);
|
|
2001
1937
|
this.pendingTurn = null;
|
|
1938
|
+
this.settlingTurn = null;
|
|
2002
1939
|
this.inAgentLoop = false;
|
|
2003
1940
|
const next = this.turnQueue.shift();
|
|
2004
1941
|
if (next) {
|
|
@@ -2014,117 +1951,8 @@ var AtomicAcpSession = class {
|
|
|
2014
1951
|
});
|
|
2015
1952
|
}
|
|
2016
1953
|
}
|
|
2017
|
-
shouldWaitForWorkflows() {
|
|
2018
|
-
if (!this.waitForWorkflows || this.cancelRequested) return false;
|
|
2019
|
-
return [...this.turnWorkflowRuns.values()].some((run) => !run.done);
|
|
2020
|
-
}
|
|
2021
|
-
beginWorkflowWait() {
|
|
2022
|
-
if (this.waitingForWorkflows) return;
|
|
2023
|
-
this.waitingForWorkflows = true;
|
|
2024
|
-
const open = [...this.turnWorkflowRuns].filter(([, run]) => !run.done);
|
|
2025
|
-
const list = open.map(([runId, run]) => `"${run.name}" (run ${runId})`).join(", ");
|
|
2026
|
-
this.emitAgentText(`Waiting for workflow ${list} to finish.`);
|
|
2027
|
-
}
|
|
2028
|
-
clearWorkflowGraceTimer() {
|
|
2029
|
-
if (this.workflowGraceTimer) clearTimeout(this.workflowGraceTimer);
|
|
2030
|
-
this.workflowGraceTimer = void 0;
|
|
2031
|
-
}
|
|
2032
|
-
resetWorkflowWait() {
|
|
2033
|
-
this.clearWorkflowGraceTimer();
|
|
2034
|
-
this.turnWorkflowRuns.clear();
|
|
2035
|
-
this.waitingForWorkflows = false;
|
|
2036
|
-
}
|
|
2037
|
-
/** Once every run has ended, end the turn after Atomic's follow-up turn, or after a short grace without one. */
|
|
2038
|
-
maybeEndWorkflowWait() {
|
|
2039
|
-
if (!this.waitingForWorkflows || !this.pendingTurn || this.shouldWaitForWorkflows()) return;
|
|
2040
|
-
if (this.inAgentLoop) return;
|
|
2041
|
-
this.clearWorkflowGraceTimer();
|
|
2042
|
-
this.workflowGraceTimer = setTimeout(() => {
|
|
2043
|
-
this.workflowGraceTimer = void 0;
|
|
2044
|
-
if (this.waitingForWorkflows && this.pendingTurn && !this.inAgentLoop) void this.settleTurn();
|
|
2045
|
-
}, this.workflowFollowUpGraceMs);
|
|
2046
|
-
}
|
|
2047
|
-
publishWorkflowRun(runId, run) {
|
|
2048
|
-
const status = !run.done ? "in_progress" : run.status === "completed" || run.status === "skipped" ? "completed" : "failed";
|
|
2049
|
-
this.emit({
|
|
2050
|
-
sessionUpdate: "tool_call_update",
|
|
2051
|
-
toolCallId: `workflow-run-${runId}`,
|
|
2052
|
-
status,
|
|
2053
|
-
content: [{ type: "content", content: { type: "text", text: run.lines.join("\n") } }]
|
|
2054
|
-
});
|
|
2055
|
-
}
|
|
2056
|
-
trackWorkflowEvent(event) {
|
|
2057
|
-
if (event.kind === "run_start") {
|
|
2058
|
-
if (!this.waitForWorkflows || !this.pendingTurn || this.turnWorkflowRuns.has(event.runId)) return;
|
|
2059
|
-
const name = event.name ?? this.workflowRunNames.get(event.runId) ?? "workflow";
|
|
2060
|
-
this.turnWorkflowRuns.set(event.runId, {
|
|
2061
|
-
name,
|
|
2062
|
-
done: false,
|
|
2063
|
-
lines: [`run ${event.runId} started`],
|
|
2064
|
-
stageNames: /* @__PURE__ */ new Map(),
|
|
2065
|
-
resultReported: false
|
|
2066
|
-
});
|
|
2067
|
-
this.emit({
|
|
2068
|
-
sessionUpdate: "tool_call",
|
|
2069
|
-
toolCallId: `workflow-run-${event.runId}`,
|
|
2070
|
-
title: `Workflow ${name}`,
|
|
2071
|
-
kind: "other",
|
|
2072
|
-
status: "in_progress",
|
|
2073
|
-
rawInput: { runId: event.runId, workflow: name }
|
|
2074
|
-
});
|
|
2075
|
-
return;
|
|
2076
|
-
}
|
|
2077
|
-
const run = this.turnWorkflowRuns.get(event.runId);
|
|
2078
|
-
if (!run) return;
|
|
2079
|
-
if (event.kind === "stage_start") {
|
|
2080
|
-
const stage = event.name ?? event.stageId ?? "stage";
|
|
2081
|
-
if (event.stageId) run.stageNames.set(event.stageId, stage);
|
|
2082
|
-
run.lines.push(`stage ${stage} started`);
|
|
2083
|
-
if (event.humanInput && !run.done) {
|
|
2084
|
-
run.done = true;
|
|
2085
|
-
run.status = "awaiting_input";
|
|
2086
|
-
run.lines.push(`waiting for input at stage ${stage}`);
|
|
2087
|
-
if (!this.headless) {
|
|
2088
|
-
this.emitAgentText(
|
|
2089
|
-
`Workflow "${run.name}" is waiting for input (stage ${stage}, run ${event.runId}). This turn won't wait for it; reply here to answer it.`
|
|
2090
|
-
);
|
|
2091
|
-
}
|
|
2092
|
-
}
|
|
2093
|
-
} else if (event.kind === "stage_end") {
|
|
2094
|
-
const stage = (event.stageId && run.stageNames.get(event.stageId)) ?? event.stageId ?? "stage";
|
|
2095
|
-
run.lines.push(`stage ${stage} ${event.status ?? "ended"}`);
|
|
2096
|
-
} else {
|
|
2097
|
-
if (!run.done || run.status === "awaiting_input") {
|
|
2098
|
-
run.done = true;
|
|
2099
|
-
run.status = event.status;
|
|
2100
|
-
run.lines.push(`run ${event.status}`);
|
|
2101
|
-
}
|
|
2102
|
-
if (event.result !== void 0 && !run.resultReported) {
|
|
2103
|
-
run.resultReported = true;
|
|
2104
|
-
run.lines.push(`result: ${JSON.stringify(event.result)}`);
|
|
2105
|
-
}
|
|
2106
|
-
if (event.error) run.lines.push(`error: ${event.error}`);
|
|
2107
|
-
}
|
|
2108
|
-
this.publishWorkflowRun(event.runId, run);
|
|
2109
|
-
this.maybeEndWorkflowWait();
|
|
2110
|
-
}
|
|
2111
|
-
handleWorkflowEvent(event) {
|
|
2112
|
-
if (event.kind === "run_start" && event.name) this.workflowRunNames.set(event.runId, event.name);
|
|
2113
|
-
this.trackWorkflowEvent(event);
|
|
2114
|
-
if (event.kind === "stage_start" && event.humanInput && this.headless) {
|
|
2115
|
-
const name = this.workflowRunNames.get(event.runId) ?? event.runId;
|
|
2116
|
-
this.emitAgentText(
|
|
2117
|
-
`No person is available to answer workflow "${name}" (stage ${event.name ?? event.stageId ?? "input"}), so run ${event.runId} is being quit.`
|
|
2118
|
-
);
|
|
2119
|
-
void this.proc.prompt(`/workflow quit ${event.runId}`, [], "followUp").catch(() => {
|
|
2120
|
-
this.emitAgentText(`Could not quit workflow run ${event.runId}.`);
|
|
2121
|
-
});
|
|
2122
|
-
}
|
|
2123
|
-
}
|
|
2124
1954
|
handleAtomicEvent(ev) {
|
|
2125
1955
|
const type = String(ev.type ?? "");
|
|
2126
|
-
const workflowEvent = parseWorkflowEvent(ev);
|
|
2127
|
-
if (workflowEvent) this.handleWorkflowEvent(workflowEvent);
|
|
2128
1956
|
switch (type) {
|
|
2129
1957
|
case "session_info_changed": {
|
|
2130
1958
|
const name = ev.name;
|
|
@@ -2141,6 +1969,11 @@ var AtomicAcpSession = class {
|
|
|
2141
1969
|
}
|
|
2142
1970
|
break;
|
|
2143
1971
|
}
|
|
1972
|
+
case "message_start": {
|
|
1973
|
+
const role = ev.message?.role;
|
|
1974
|
+
if (role === "user" && this.backgroundTurn && this.pendingTurn) this.backgroundTurn = false;
|
|
1975
|
+
break;
|
|
1976
|
+
}
|
|
2144
1977
|
case "message_end": {
|
|
2145
1978
|
const message = ev.message;
|
|
2146
1979
|
if (message?.role === "custom" && message.display === true) {
|
|
@@ -2193,7 +2026,7 @@ var AtomicAcpSession = class {
|
|
|
2193
2026
|
this.emit({
|
|
2194
2027
|
sessionUpdate: "tool_call",
|
|
2195
2028
|
toolCallId,
|
|
2196
|
-
title:
|
|
2029
|
+
title: toolName,
|
|
2197
2030
|
kind: toToolKind(toolName),
|
|
2198
2031
|
status,
|
|
2199
2032
|
locations,
|
|
@@ -2203,7 +2036,6 @@ var AtomicAcpSession = class {
|
|
|
2203
2036
|
this.emit({
|
|
2204
2037
|
sessionUpdate: "tool_call_update",
|
|
2205
2038
|
toolCallId,
|
|
2206
|
-
...toolName === "workflow" ? { title: toolTitle(toolName, rawInput), kind: toToolKind(toolName) } : {},
|
|
2207
2039
|
status,
|
|
2208
2040
|
locations,
|
|
2209
2041
|
rawInput
|
|
@@ -2243,7 +2075,7 @@ var AtomicAcpSession = class {
|
|
|
2243
2075
|
this.emit({
|
|
2244
2076
|
sessionUpdate: "tool_call",
|
|
2245
2077
|
toolCallId,
|
|
2246
|
-
title:
|
|
2078
|
+
title: toolName,
|
|
2247
2079
|
kind: toToolKind(toolName),
|
|
2248
2080
|
status: "in_progress",
|
|
2249
2081
|
locations,
|
|
@@ -2254,7 +2086,6 @@ var AtomicAcpSession = class {
|
|
|
2254
2086
|
this.emit({
|
|
2255
2087
|
sessionUpdate: "tool_call_update",
|
|
2256
2088
|
toolCallId,
|
|
2257
|
-
...toolName === "workflow" ? { title: toolTitle(toolName, args), kind: toToolKind(toolName) } : {},
|
|
2258
2089
|
status: "in_progress",
|
|
2259
2090
|
locations,
|
|
2260
2091
|
rawInput: args
|
|
@@ -2363,7 +2194,6 @@ var AtomicAcpSession = class {
|
|
|
2363
2194
|
case "agent_start": {
|
|
2364
2195
|
this.inAgentLoop = true;
|
|
2365
2196
|
this.backgroundTurn = !this.pendingTurn;
|
|
2366
|
-
this.clearWorkflowGraceTimer();
|
|
2367
2197
|
break;
|
|
2368
2198
|
}
|
|
2369
2199
|
case "turn_end": {
|
|
@@ -2377,12 +2207,6 @@ var AtomicAcpSession = class {
|
|
|
2377
2207
|
if (this.backgroundTurn || !this.pendingTurn) {
|
|
2378
2208
|
this.backgroundTurn = false;
|
|
2379
2209
|
this.inAgentLoop = false;
|
|
2380
|
-
this.maybeEndWorkflowWait();
|
|
2381
|
-
break;
|
|
2382
|
-
}
|
|
2383
|
-
if (this.shouldWaitForWorkflows()) {
|
|
2384
|
-
this.inAgentLoop = false;
|
|
2385
|
-
this.beginWorkflowWait();
|
|
2386
2210
|
break;
|
|
2387
2211
|
}
|
|
2388
2212
|
void this.settleTurn();
|
|
@@ -2404,14 +2228,6 @@ function formatAutoRetryMessage(ev) {
|
|
|
2404
2228
|
if (delayMs > 0 && delaySeconds === 0) delaySeconds = 1;
|
|
2405
2229
|
return `Retrying (attempt ${attempt}/${maxAttempts}, waiting ${delaySeconds}s)...`;
|
|
2406
2230
|
}
|
|
2407
|
-
function toolTitle(name, input2) {
|
|
2408
|
-
if (name !== "workflow") return name;
|
|
2409
|
-
const args = input2;
|
|
2410
|
-
const details = [args?.action ?? (args?.workflow ? "run" : void 0), args?.workflow ?? args?.runId].filter(
|
|
2411
|
-
(value) => typeof value === "string" && value
|
|
2412
|
-
);
|
|
2413
|
-
return details.length ? `workflow: ${details.join(" ")}` : name;
|
|
2414
|
-
}
|
|
2415
2231
|
function toToolKind(toolName) {
|
|
2416
2232
|
switch (toolName) {
|
|
2417
2233
|
case "read":
|
|
@@ -2503,43 +2319,12 @@ function normalizeAtomicAssistantText(content) {
|
|
|
2503
2319
|
return content.map((c) => c?.type === "text" && typeof c.text === "string" ? c.text : "").filter(Boolean).join("");
|
|
2504
2320
|
}
|
|
2505
2321
|
|
|
2506
|
-
// src/acp/workflow-history.ts
|
|
2507
|
-
function record2(value) {
|
|
2508
|
-
return value !== null && typeof value === "object" ? value : {};
|
|
2509
|
-
}
|
|
2510
|
-
function renderRun(value) {
|
|
2511
|
-
const run = record2(value);
|
|
2512
|
-
const title = [run.name, run.runId].filter((v) => typeof v === "string").join(" \u2014 ");
|
|
2513
|
-
const heading = (title || "Workflow run").replace(/[\r\n\u2028\u2029]/g, " ").replace(/[\\`*_{}[\]()<>#+\-.!|&]/g, "\\$&");
|
|
2514
|
-
const lines = [];
|
|
2515
|
-
for (const key of ["status", "label", "identity", "summarizedThrough"]) {
|
|
2516
|
-
if (run[key] !== void 0)
|
|
2517
|
-
lines.push(`${key}: ${typeof run[key] === "object" ? JSON.stringify(run[key]) : String(run[key])}`);
|
|
2518
|
-
}
|
|
2519
|
-
if (run.summary !== void 0)
|
|
2520
|
-
lines.push("", typeof run.summary === "string" ? run.summary : JSON.stringify(run.summary, null, 2));
|
|
2521
|
-
const body = lines.join("\n");
|
|
2522
|
-
const fence = "`".repeat(Math.max(3, ...[...body.matchAll(/`+/g)].map((match) => match[0].length + 1)));
|
|
2523
|
-
return `## ${heading}
|
|
2524
|
-
|
|
2525
|
-
${fence}text
|
|
2526
|
-
${body}
|
|
2527
|
-
${fence}`;
|
|
2528
|
-
}
|
|
2529
|
-
function renderWorkflowHistory(data, list) {
|
|
2530
|
-
if (!list) return renderRun(data);
|
|
2531
|
-
const runs = record2(data).runs;
|
|
2532
|
-
return Array.isArray(runs) && runs.length ? runs.map(renderRun).join("\n\n") : "No workflow runs found.";
|
|
2533
|
-
}
|
|
2534
|
-
|
|
2535
2322
|
// src/acp/agent.ts
|
|
2536
2323
|
import { fileURLToPath } from "url";
|
|
2537
2324
|
var MODEL_CONFIG_ID = "model";
|
|
2538
2325
|
var THOUGHT_LEVEL_CONFIG_ID = "thought_level";
|
|
2539
2326
|
function builtinAvailableCommands() {
|
|
2540
2327
|
return [
|
|
2541
|
-
{ name: "workflow-runs", description: "List retained workflow runs", input: { hint: "[status]" } },
|
|
2542
|
-
{ name: "workflow-run", description: "Show a workflow run summary", input: { hint: "<runId>" } },
|
|
2543
2328
|
{
|
|
2544
2329
|
name: "compact",
|
|
2545
2330
|
description: "Manually compact the session context",
|
|
@@ -2896,27 +2681,6 @@ var AtomicAcpAgent = class {
|
|
|
2896
2681
|
const cmd = space === -1 ? trimmed.slice(1) : trimmed.slice(1, space);
|
|
2897
2682
|
const argsString = space === -1 ? "" : trimmed.slice(space + 1);
|
|
2898
2683
|
const args = parseCommandArgs(argsString);
|
|
2899
|
-
if (cmd === "workflow-runs" || cmd === "workflow-run") {
|
|
2900
|
-
let text;
|
|
2901
|
-
if (args.length > 1 || cmd === "workflow-run" && !args[0]) {
|
|
2902
|
-
text = cmd === "workflow-run" ? "Usage: /workflow-run <runId>" : "Usage: /workflow-runs [status]";
|
|
2903
|
-
} else {
|
|
2904
|
-
try {
|
|
2905
|
-
const data = cmd === "workflow-runs" ? await session.proc.listWorkflowRuns(args[0]) : await session.proc.getWorkflowRunSummary(args[0]);
|
|
2906
|
-
text = renderWorkflowHistory(data, cmd === "workflow-runs");
|
|
2907
|
-
} catch (error) {
|
|
2908
|
-
text = `Could not read workflow history: ${error instanceof Error ? error.message : String(error)}`;
|
|
2909
|
-
}
|
|
2910
|
-
}
|
|
2911
|
-
await this.conn.notify(methods3.client.session.update, {
|
|
2912
|
-
sessionId: session.sessionId,
|
|
2913
|
-
update: {
|
|
2914
|
-
sessionUpdate: "agent_message_chunk",
|
|
2915
|
-
content: { type: "text", text }
|
|
2916
|
-
}
|
|
2917
|
-
});
|
|
2918
|
-
return { stopReason: "end_turn" };
|
|
2919
|
-
}
|
|
2920
2684
|
if (cmd === "compact") {
|
|
2921
2685
|
const customInstructions = args.join(" ").trim() || void 0;
|
|
2922
2686
|
const res = await session.proc.compact(customInstructions);
|