@dudko.dev/agent-web-ui 0.0.3 → 0.0.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/README.md +78 -6
- package/dist/{chunk-UNIXGP4N.js → chunk-627OXMFG.js} +2 -0
- package/dist/{chunk-G7LQTOA2.js → chunk-NHU6WQM5.js} +61 -6
- package/dist/controller.cjs +621 -20
- package/dist/controller.d.cts +110 -3
- package/dist/controller.d.ts +110 -3
- package/dist/controller.js +575 -17
- package/dist/index.cjs +81 -14
- package/dist/index.d.cts +28 -10
- package/dist/index.d.ts +28 -10
- package/dist/index.js +27 -8
- package/dist/{labels-DH1ESUtm.d.ts → labels-CCPL7t17.d.ts} +8 -1
- package/dist/{labels-CmiTBLWD.d.cts → labels-ipy-ydkp.d.cts} +8 -1
- package/dist/locales/be.cjs +2 -0
- package/dist/locales/be.js +1 -1
- package/dist/locales/bg.cjs +2 -0
- package/dist/locales/bg.js +1 -1
- package/dist/locales/ca.cjs +2 -0
- package/dist/locales/ca.js +1 -1
- package/dist/locales/cs.cjs +2 -0
- package/dist/locales/cs.js +1 -1
- package/dist/locales/da.cjs +2 -0
- package/dist/locales/da.js +1 -1
- package/dist/locales/de.cjs +2 -0
- package/dist/locales/de.js +1 -1
- package/dist/locales/el.cjs +2 -0
- package/dist/locales/el.js +1 -1
- package/dist/locales/en.cjs +2 -0
- package/dist/locales/en.js +1 -1
- package/dist/locales/es-419.cjs +2 -0
- package/dist/locales/es-419.js +1 -1
- package/dist/locales/es.cjs +2 -0
- package/dist/locales/es.js +1 -1
- package/dist/locales/fi.cjs +2 -0
- package/dist/locales/fi.js +1 -1
- package/dist/locales/fr-CA.cjs +2 -0
- package/dist/locales/fr-CA.js +1 -1
- package/dist/locales/fr.cjs +2 -0
- package/dist/locales/fr.js +1 -1
- package/dist/locales/gn.cjs +2 -0
- package/dist/locales/gn.js +1 -1
- package/dist/locales/hr.cjs +2 -0
- package/dist/locales/hr.js +1 -1
- package/dist/locales/ht.cjs +2 -0
- package/dist/locales/ht.js +1 -1
- package/dist/locales/hu.cjs +2 -0
- package/dist/locales/hu.js +1 -1
- package/dist/locales/index.cjs +2 -0
- package/dist/locales/index.js +1 -1
- package/dist/locales/it.cjs +2 -0
- package/dist/locales/it.js +1 -1
- package/dist/locales/nb.cjs +2 -0
- package/dist/locales/nb.js +1 -1
- package/dist/locales/nl.cjs +2 -0
- package/dist/locales/nl.js +1 -1
- package/dist/locales/pl.cjs +2 -0
- package/dist/locales/pl.js +1 -1
- package/dist/locales/pt-BR.cjs +2 -0
- package/dist/locales/pt-BR.js +1 -1
- package/dist/locales/pt.cjs +2 -0
- package/dist/locales/pt.js +1 -1
- package/dist/locales/qu.cjs +2 -0
- package/dist/locales/qu.js +1 -1
- package/dist/locales/ro.cjs +2 -0
- package/dist/locales/ro.js +1 -1
- package/dist/locales/ru.cjs +2 -0
- package/dist/locales/ru.js +1 -1
- package/dist/locales/sk.cjs +2 -0
- package/dist/locales/sk.js +1 -1
- package/dist/locales/sq.cjs +2 -0
- package/dist/locales/sq.js +1 -1
- package/dist/locales/sr-Latn.cjs +2 -0
- package/dist/locales/sr-Latn.js +1 -1
- package/dist/locales/sr.cjs +2 -0
- package/dist/locales/sr.js +1 -1
- package/dist/locales/sv.cjs +2 -0
- package/dist/locales/sv.js +1 -1
- package/dist/locales/tr.cjs +2 -0
- package/dist/locales/tr.js +1 -1
- package/dist/locales/uk.cjs +2 -0
- package/dist/locales/uk.js +1 -1
- package/dist/markdown.cjs +36 -0
- package/dist/markdown.d.cts +27 -1
- package/dist/markdown.d.ts +27 -1
- package/dist/markdown.js +33 -0
- package/dist/mcp-app.cjs +1 -4
- package/dist/mcp-app.js +1 -2
- package/package.json +1 -1
package/dist/controller.cjs
CHANGED
|
@@ -31,11 +31,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var controller_exports = {};
|
|
32
32
|
__export(controller_exports, {
|
|
33
33
|
createAgentController: () => createAgentController,
|
|
34
|
-
createInitialAgentSnapshot: () => createInitialAgentSnapshot
|
|
34
|
+
createInitialAgentSnapshot: () => createInitialAgentSnapshot,
|
|
35
|
+
createInitialRemoteSnapshot: () => createInitialRemoteSnapshot,
|
|
36
|
+
createRemoteAgentController: () => createRemoteAgentController,
|
|
37
|
+
createSseParser: () => createSseParser,
|
|
38
|
+
parseRemoteCursor: () => parseRemoteCursor,
|
|
39
|
+
remoteEventOf: () => remoteEventOf,
|
|
40
|
+
remoteEventScope: () => remoteEventScope
|
|
35
41
|
});
|
|
36
42
|
module.exports = __toCommonJS(controller_exports);
|
|
37
|
-
var core = __toESM(require("@dudko.dev/agent-web"), 1);
|
|
38
|
-
var import_agent_web2 = require("@dudko.dev/agent-web");
|
|
39
43
|
|
|
40
44
|
// src/approval-queue.ts
|
|
41
45
|
var createApprovalQueue = () => {
|
|
@@ -190,8 +194,11 @@ var withoutOwnWords = (event) => {
|
|
|
190
194
|
}
|
|
191
195
|
};
|
|
192
196
|
|
|
197
|
+
// src/core-copies.ts
|
|
198
|
+
var REPLAY_NOTE_OF_CORE = "[not run again: this exact call already succeeded in an earlier attempt at this step, and its effect is in place. Its result then:]";
|
|
199
|
+
var isReplayedOutput = (output) => typeof output === "string" && output.startsWith(REPLAY_NOTE_OF_CORE);
|
|
200
|
+
|
|
193
201
|
// src/state.ts
|
|
194
|
-
var import_agent_web = require("@dudko.dev/agent-web");
|
|
195
202
|
var approxTokens = (value) => {
|
|
196
203
|
if (value === void 0) return 0;
|
|
197
204
|
let text;
|
|
@@ -908,7 +915,7 @@ var applyEvent = (state, event, ctx) => {
|
|
|
908
915
|
ok: event.ok,
|
|
909
916
|
status: "done",
|
|
910
917
|
outputTokens: approxTokens(event.output),
|
|
911
|
-
...(
|
|
918
|
+
...isReplayedOutput(event.output) ? { replayed: true } : {},
|
|
912
919
|
...ctx.at !== void 0 && c.startedAt !== void 0 ? { durationMs: Math.max(0, ctx.at - c.startedAt) } : {}
|
|
913
920
|
};
|
|
914
921
|
}
|
|
@@ -955,7 +962,7 @@ var applyEvent = (state, event, ctx) => {
|
|
|
955
962
|
...streamed?.durationMs !== void 0 ? { durationMs: streamed.durationMs } : {},
|
|
956
963
|
// With tiers the list holds every attempt's calls, in the order they were made.
|
|
957
964
|
...streamed?.attempt !== void 0 ? { attempt: streamed.attempt } : {},
|
|
958
|
-
...(
|
|
965
|
+
...isReplayedOutput(c.output) ? { replayed: true } : {},
|
|
959
966
|
// The MCP App the call came with (only the stream told it).
|
|
960
967
|
...streamed?.app !== void 0 ? { app: streamed.app } : {}
|
|
961
968
|
};
|
|
@@ -1395,6 +1402,21 @@ var agentStateReducer = (state, action) => {
|
|
|
1395
1402
|
}
|
|
1396
1403
|
};
|
|
1397
1404
|
|
|
1405
|
+
// src/core-later.ts
|
|
1406
|
+
var loading;
|
|
1407
|
+
var loaded;
|
|
1408
|
+
var loadAgentCore = () => {
|
|
1409
|
+
if (!loading) {
|
|
1410
|
+
const next = import("@dudko.dev/agent-web").then((core) => loaded = core);
|
|
1411
|
+
loading = next;
|
|
1412
|
+
next.catch(() => {
|
|
1413
|
+
if (loading === next) loading = void 0;
|
|
1414
|
+
});
|
|
1415
|
+
}
|
|
1416
|
+
return loading;
|
|
1417
|
+
};
|
|
1418
|
+
var agentCoreIfLoaded = () => loaded;
|
|
1419
|
+
|
|
1398
1420
|
// src/util.ts
|
|
1399
1421
|
var errMessage = (err) => err instanceof Error ? err.message : String(err);
|
|
1400
1422
|
var coalesce = (fn, ms = 100) => {
|
|
@@ -1413,10 +1435,28 @@ var coalesce = (fn, ms = 100) => {
|
|
|
1413
1435
|
}
|
|
1414
1436
|
};
|
|
1415
1437
|
};
|
|
1416
|
-
var listenToOtherTabs = (
|
|
1417
|
-
|
|
1438
|
+
var listenToOtherTabs = (core, dbName, listener) => {
|
|
1439
|
+
if (!dbName) return () => void 0;
|
|
1440
|
+
if (typeof core === "function") {
|
|
1441
|
+
let closed = false;
|
|
1442
|
+
let off;
|
|
1443
|
+
core().then(
|
|
1444
|
+
(module2) => {
|
|
1445
|
+
if (!closed) off = listenToOtherTabs(module2, dbName, listener);
|
|
1446
|
+
},
|
|
1447
|
+
() => {
|
|
1448
|
+
}
|
|
1449
|
+
);
|
|
1450
|
+
return () => {
|
|
1451
|
+
closed = true;
|
|
1452
|
+
off?.();
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
return typeof core.onStorageChange === "function" ? core.onStorageChange(dbName, listener) : () => void 0;
|
|
1456
|
+
};
|
|
1457
|
+
var followOtherTabs = (core, dbName, accept, settle, ms = 100) => {
|
|
1418
1458
|
const later = coalesce(settle, ms);
|
|
1419
|
-
const off = listenToOtherTabs(
|
|
1459
|
+
const off = listenToOtherTabs(core, dbName, (change) => {
|
|
1420
1460
|
if (accept(change)) later.call();
|
|
1421
1461
|
});
|
|
1422
1462
|
return () => {
|
|
@@ -1426,6 +1466,549 @@ var followOtherTabs = (core2, dbName, accept, settle, ms = 100) => {
|
|
|
1426
1466
|
};
|
|
1427
1467
|
var remoteAnswerOf = (ok, stillWatching) => ok ? "taken" : stillWatching ? "refused" : "unknown";
|
|
1428
1468
|
|
|
1469
|
+
// src/remote-controller.ts
|
|
1470
|
+
var createSseParser = (onFrame, onRetry) => {
|
|
1471
|
+
let buffer = "";
|
|
1472
|
+
let data = [];
|
|
1473
|
+
let id;
|
|
1474
|
+
let sawCr = false;
|
|
1475
|
+
const line = (text) => {
|
|
1476
|
+
if (text === "") {
|
|
1477
|
+
if (data.length > 0) onFrame({ ...id !== void 0 ? { id } : {}, data: data.join("\n") });
|
|
1478
|
+
data = [];
|
|
1479
|
+
id = void 0;
|
|
1480
|
+
return;
|
|
1481
|
+
}
|
|
1482
|
+
if (text.startsWith(":")) return;
|
|
1483
|
+
const colon = text.indexOf(":");
|
|
1484
|
+
const field = colon < 0 ? text : text.slice(0, colon);
|
|
1485
|
+
let value = colon < 0 ? "" : text.slice(colon + 1);
|
|
1486
|
+
if (value.startsWith(" ")) value = value.slice(1);
|
|
1487
|
+
if (field === "data") data.push(value);
|
|
1488
|
+
else if (field === "id" && !value.includes("\0")) id = value;
|
|
1489
|
+
else if (field === "retry" && /^\d+$/.test(value)) onRetry?.(Number(value));
|
|
1490
|
+
};
|
|
1491
|
+
return {
|
|
1492
|
+
push: (chunk) => {
|
|
1493
|
+
let text = buffer + chunk;
|
|
1494
|
+
buffer = "";
|
|
1495
|
+
if (sawCr && text.startsWith("\n")) text = text.slice(1);
|
|
1496
|
+
sawCr = false;
|
|
1497
|
+
let start = 0;
|
|
1498
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
1499
|
+
const c = text[i];
|
|
1500
|
+
if (c !== "\n" && c !== "\r") continue;
|
|
1501
|
+
line(text.slice(start, i));
|
|
1502
|
+
if (c === "\r") {
|
|
1503
|
+
if (i + 1 === text.length) sawCr = true;
|
|
1504
|
+
else if (text[i + 1] === "\n") i += 1;
|
|
1505
|
+
}
|
|
1506
|
+
start = i + 1;
|
|
1507
|
+
}
|
|
1508
|
+
buffer = text.slice(start);
|
|
1509
|
+
}
|
|
1510
|
+
};
|
|
1511
|
+
};
|
|
1512
|
+
var parseRemoteCursor = (raw) => {
|
|
1513
|
+
const m = typeof raw === "string" ? /^(\d+):(\d+):(\d+)$/.exec(raw.trim()) : null;
|
|
1514
|
+
return m ? { turn: Number(m[1]), attempt: Number(m[2]), seq: Number(m[3]) } : void 0;
|
|
1515
|
+
};
|
|
1516
|
+
var compareCursors = (a, b) => a.turn - b.turn || a.attempt - b.attempt || a.seq - b.seq;
|
|
1517
|
+
var remoteEventScope = (event) => {
|
|
1518
|
+
const step = event.step?.id;
|
|
1519
|
+
if (event.type.startsWith("step.") && typeof step === "string") return `step:${step}`;
|
|
1520
|
+
if (event.type === "final" || event.type.startsWith("final.")) return "final";
|
|
1521
|
+
if (event.type.startsWith("plan.") || event.type === "replan.decision") return "plan";
|
|
1522
|
+
return void 0;
|
|
1523
|
+
};
|
|
1524
|
+
var remoteEventOf = (event) => {
|
|
1525
|
+
if (event.type === "error" && typeof event.error !== "string") {
|
|
1526
|
+
const err = event.error;
|
|
1527
|
+
return {
|
|
1528
|
+
...event,
|
|
1529
|
+
error: typeof err?.message === "string" ? err.message : "error"
|
|
1530
|
+
};
|
|
1531
|
+
}
|
|
1532
|
+
return event;
|
|
1533
|
+
};
|
|
1534
|
+
var emptyUsage2 = () => ({ inputTokens: 0, outputTokens: 0, totalTokens: 0 });
|
|
1535
|
+
var newMessageId = () => globalThis.crypto?.randomUUID?.() ?? `m-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
|
|
1536
|
+
var GatewayError = class extends Error {
|
|
1537
|
+
constructor(status, code, message) {
|
|
1538
|
+
super(message);
|
|
1539
|
+
this.status = status;
|
|
1540
|
+
this.code = code;
|
|
1541
|
+
this.name = "GatewayError";
|
|
1542
|
+
}
|
|
1543
|
+
status;
|
|
1544
|
+
code;
|
|
1545
|
+
};
|
|
1546
|
+
var createInitialRemoteSnapshot = (options = {}) => snapshotOf({ ...createInitialAgentState(), status: "ready" }, options.sessionId ?? "", /* @__PURE__ */ new Set());
|
|
1547
|
+
var snapshotOf = (state, sessionId, answering) => Object.freeze({
|
|
1548
|
+
...state,
|
|
1549
|
+
isRunning: state.status === "running",
|
|
1550
|
+
isReady: state.status === "ready",
|
|
1551
|
+
// A request answered here waits for the stream to say so: not offered twice meanwhile.
|
|
1552
|
+
pendingApprovals: state.approvals.filter(
|
|
1553
|
+
(a) => a.status === "pending" && !a.remote && !answering.has(a.id)
|
|
1554
|
+
),
|
|
1555
|
+
pendingQuestions: [],
|
|
1556
|
+
// The server's policy decides; a view hides the switch for a remote agent.
|
|
1557
|
+
approvalMode: "ask-writes",
|
|
1558
|
+
sessionId
|
|
1559
|
+
});
|
|
1560
|
+
var createRemoteAgentController = (options) => {
|
|
1561
|
+
const base = options.baseUrl.replace(/\/+$/, "");
|
|
1562
|
+
const doFetch = options.fetch ?? ((...args) => globalThis.fetch(...args));
|
|
1563
|
+
const minWait = Math.max(0, options.reconnect?.minMs ?? 1e3);
|
|
1564
|
+
const maxWait = Math.max(minWait, options.reconnect?.maxMs ?? 3e4);
|
|
1565
|
+
const maxEvents = options.maxEvents;
|
|
1566
|
+
let disposed = false;
|
|
1567
|
+
let sessionId = options.sessionId?.trim() || void 0;
|
|
1568
|
+
let state = { ...createInitialAgentState(), status: "ready" };
|
|
1569
|
+
let snapshot;
|
|
1570
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
1571
|
+
const answering = /* @__PURE__ */ new Set();
|
|
1572
|
+
const changed = () => {
|
|
1573
|
+
snapshot = void 0;
|
|
1574
|
+
for (const listener of [...listeners]) {
|
|
1575
|
+
try {
|
|
1576
|
+
listener();
|
|
1577
|
+
} catch (err) {
|
|
1578
|
+
queueMicrotask(() => {
|
|
1579
|
+
throw err;
|
|
1580
|
+
});
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
};
|
|
1584
|
+
const apply = (action) => {
|
|
1585
|
+
if (disposed) return;
|
|
1586
|
+
const next = agentStateReducer(state, action);
|
|
1587
|
+
if (next === state) return;
|
|
1588
|
+
state = next;
|
|
1589
|
+
changed();
|
|
1590
|
+
};
|
|
1591
|
+
let turn;
|
|
1592
|
+
let turnIds = [];
|
|
1593
|
+
let turnFinal = false;
|
|
1594
|
+
let waiting = [];
|
|
1595
|
+
const runs = /* @__PURE__ */ new Map();
|
|
1596
|
+
const fold = (action, attempt = 0, scope) => {
|
|
1597
|
+
if (turn) {
|
|
1598
|
+
if (scope !== void 0 && turn.entries.some((e) => e.scope === scope && e.attempt < attempt)) {
|
|
1599
|
+
turn.entries = turn.entries.filter((e) => !(e.scope === scope && e.attempt < attempt));
|
|
1600
|
+
let s = turn.start;
|
|
1601
|
+
for (const e of turn.entries) s = agentStateReducer(s, e.action);
|
|
1602
|
+
state = s;
|
|
1603
|
+
changed();
|
|
1604
|
+
}
|
|
1605
|
+
turn.entries.push({ action, attempt, ...scope !== void 0 ? { scope } : {} });
|
|
1606
|
+
}
|
|
1607
|
+
apply(action);
|
|
1608
|
+
if (action.type === "event" && !disposed) tell(action.event);
|
|
1609
|
+
};
|
|
1610
|
+
const tell = (e) => {
|
|
1611
|
+
try {
|
|
1612
|
+
options.onEvent?.(e);
|
|
1613
|
+
} catch {
|
|
1614
|
+
}
|
|
1615
|
+
};
|
|
1616
|
+
const event = (e) => ({
|
|
1617
|
+
type: "event",
|
|
1618
|
+
event: e,
|
|
1619
|
+
maxEvents,
|
|
1620
|
+
at: Date.now()
|
|
1621
|
+
});
|
|
1622
|
+
const queueShown = () => {
|
|
1623
|
+
for (const w of waiting) fold(event({ type: "message.queued", id: w.id, text: w.text }));
|
|
1624
|
+
};
|
|
1625
|
+
const endTurn = (body) => {
|
|
1626
|
+
const status = body.status;
|
|
1627
|
+
const text = typeof body.text === "string" ? body.text : "";
|
|
1628
|
+
if (status === "completed") {
|
|
1629
|
+
if (!turnFinal && text) fold(event({ type: "final", text }));
|
|
1630
|
+
} else if (status === "stopped") {
|
|
1631
|
+
fold(event({ type: "stopped" }));
|
|
1632
|
+
} else if (status === "failed") {
|
|
1633
|
+
const error = typeof body.error === "string" && body.error ? body.error : "the turn failed";
|
|
1634
|
+
fold(event({ type: "error", phase: "run", error }));
|
|
1635
|
+
}
|
|
1636
|
+
const result = status === "failed" ? void 0 : {
|
|
1637
|
+
runId: typeof body.runId === "string" ? body.runId : "",
|
|
1638
|
+
goal: state.goal ?? "",
|
|
1639
|
+
final: state.finalText || text,
|
|
1640
|
+
plan: state.plan ?? { thought: "", steps: [] },
|
|
1641
|
+
trace: [],
|
|
1642
|
+
steps: state.steps.length,
|
|
1643
|
+
applied: 0,
|
|
1644
|
+
stopped: status === "stopped",
|
|
1645
|
+
usage: body.usage ?? state.usage ?? emptyUsage2(),
|
|
1646
|
+
skills: []
|
|
1647
|
+
};
|
|
1648
|
+
for (const id of turnIds) {
|
|
1649
|
+
const done = runs.get(id);
|
|
1650
|
+
runs.delete(id);
|
|
1651
|
+
done?.(result);
|
|
1652
|
+
}
|
|
1653
|
+
turn = void 0;
|
|
1654
|
+
turnIds = [];
|
|
1655
|
+
turnFinal = false;
|
|
1656
|
+
answering.clear();
|
|
1657
|
+
apply({ type: "status", status: waiting.length > 0 ? "running" : "ready" });
|
|
1658
|
+
};
|
|
1659
|
+
const onFrame = (frame) => {
|
|
1660
|
+
let parsed;
|
|
1661
|
+
try {
|
|
1662
|
+
parsed = JSON.parse(frame.data);
|
|
1663
|
+
} catch {
|
|
1664
|
+
return;
|
|
1665
|
+
}
|
|
1666
|
+
const raw = parsed?.event;
|
|
1667
|
+
if (!raw || typeof raw !== "object" || typeof raw.type !== "string") return;
|
|
1668
|
+
const cursor2 = parseRemoteCursor(frame.id);
|
|
1669
|
+
const attempt = cursor2?.attempt ?? 0;
|
|
1670
|
+
const shown = remoteEventOf(raw);
|
|
1671
|
+
if (raw.type === "turn.started") {
|
|
1672
|
+
const ids = Array.isArray(raw.messageIds) ? raw.messageIds.filter((x) => typeof x === "string") : [];
|
|
1673
|
+
waiting = waiting.filter((w) => !ids.includes(w.id));
|
|
1674
|
+
turnIds = ids;
|
|
1675
|
+
turnFinal = false;
|
|
1676
|
+
apply({ type: "status", status: "running" });
|
|
1677
|
+
turn = { number: cursor2?.turn ?? 0, start: state, entries: [] };
|
|
1678
|
+
const goal = typeof raw.input === "string" ? raw.input : "";
|
|
1679
|
+
fold(event({ type: "run.start", goal }));
|
|
1680
|
+
queueShown();
|
|
1681
|
+
} else if (raw.type === "turn.ended") {
|
|
1682
|
+
endTurn(raw);
|
|
1683
|
+
} else if (raw.type === "tool.approval-resolved" && raw.parked === true) {
|
|
1684
|
+
} else {
|
|
1685
|
+
if (raw.type === "tool.approval-resolved" && typeof raw.id === "string") {
|
|
1686
|
+
answering.delete(raw.id);
|
|
1687
|
+
}
|
|
1688
|
+
if (raw.type === "final") turnFinal = true;
|
|
1689
|
+
fold(event(shown), attempt, remoteEventScope(raw));
|
|
1690
|
+
}
|
|
1691
|
+
if (raw.type === "turn.started" || raw.type === "turn.ended") tell(shown);
|
|
1692
|
+
};
|
|
1693
|
+
const headersFor = async (extra) => {
|
|
1694
|
+
const given = typeof options.headers === "function" ? await options.headers() : options.headers;
|
|
1695
|
+
const headers = new Headers(given ?? {});
|
|
1696
|
+
for (const [k, v] of Object.entries(extra)) headers.set(k, v);
|
|
1697
|
+
return headers;
|
|
1698
|
+
};
|
|
1699
|
+
const request = async (path, init, signal) => {
|
|
1700
|
+
const res = await doFetch(`${base}${path}`, {
|
|
1701
|
+
method: init.method,
|
|
1702
|
+
headers: await headersFor({
|
|
1703
|
+
...init.body !== void 0 ? { "content-type": "application/json" } : {},
|
|
1704
|
+
...init.headers
|
|
1705
|
+
}),
|
|
1706
|
+
...init.body !== void 0 ? { body: JSON.stringify(init.body) } : {},
|
|
1707
|
+
credentials: options.credentials ?? "same-origin",
|
|
1708
|
+
...signal ? { signal } : {}
|
|
1709
|
+
});
|
|
1710
|
+
if (!res.ok) {
|
|
1711
|
+
let code;
|
|
1712
|
+
let message = `the agent's server answered ${res.status}`;
|
|
1713
|
+
try {
|
|
1714
|
+
const body = await res.json();
|
|
1715
|
+
if (typeof body?.error?.code === "string") code = body.error.code;
|
|
1716
|
+
if (typeof body?.error?.message === "string") message = body.error.message;
|
|
1717
|
+
} catch {
|
|
1718
|
+
}
|
|
1719
|
+
throw new GatewayError(res.status, code, message);
|
|
1720
|
+
}
|
|
1721
|
+
return res;
|
|
1722
|
+
};
|
|
1723
|
+
const post = async (path, body) => {
|
|
1724
|
+
const res = await request(path, { method: "POST", body });
|
|
1725
|
+
try {
|
|
1726
|
+
return await res.json();
|
|
1727
|
+
} catch {
|
|
1728
|
+
return void 0;
|
|
1729
|
+
}
|
|
1730
|
+
};
|
|
1731
|
+
const sayError = (err) => fold(event({ type: "error", phase: "execute", error: errMessage(err) }));
|
|
1732
|
+
let streamAbort;
|
|
1733
|
+
let retryTimer;
|
|
1734
|
+
let cursor;
|
|
1735
|
+
let lastCursor;
|
|
1736
|
+
let wait = minWait;
|
|
1737
|
+
let serverRetry = 0;
|
|
1738
|
+
let streamGeneration = 0;
|
|
1739
|
+
const closeStream = () => {
|
|
1740
|
+
streamGeneration += 1;
|
|
1741
|
+
streamAbort?.abort();
|
|
1742
|
+
streamAbort = void 0;
|
|
1743
|
+
if (retryTimer !== void 0) clearTimeout(retryTimer);
|
|
1744
|
+
retryTimer = void 0;
|
|
1745
|
+
};
|
|
1746
|
+
const scheduleReconnect = (generation) => {
|
|
1747
|
+
if (disposed || generation !== streamGeneration || retryTimer !== void 0) return;
|
|
1748
|
+
const delay = Math.min(maxWait, Math.max(wait, serverRetry));
|
|
1749
|
+
wait = Math.min(maxWait, Math.max(minWait, wait * 2) || minWait);
|
|
1750
|
+
retryTimer = setTimeout(() => {
|
|
1751
|
+
retryTimer = void 0;
|
|
1752
|
+
if (generation === streamGeneration) void openStream(generation);
|
|
1753
|
+
}, delay);
|
|
1754
|
+
};
|
|
1755
|
+
const openStream = async (generation = streamGeneration) => {
|
|
1756
|
+
const sid = sessionId;
|
|
1757
|
+
if (disposed || !sid || generation !== streamGeneration || streamAbort) return;
|
|
1758
|
+
const ac = new AbortController();
|
|
1759
|
+
streamAbort = ac;
|
|
1760
|
+
const decoder = new TextDecoder();
|
|
1761
|
+
let cancelBody;
|
|
1762
|
+
try {
|
|
1763
|
+
const res = await request(
|
|
1764
|
+
`/sessions/${encodeURIComponent(sid)}/events`,
|
|
1765
|
+
{
|
|
1766
|
+
method: "GET",
|
|
1767
|
+
headers: { accept: "text/event-stream", ...cursor ? { "last-event-id": cursor } : {} }
|
|
1768
|
+
},
|
|
1769
|
+
ac.signal
|
|
1770
|
+
);
|
|
1771
|
+
const reader = res.body?.getReader();
|
|
1772
|
+
if (!reader) throw new Error("the event stream has no body");
|
|
1773
|
+
cancelBody = () => void reader.cancel().catch(() => {
|
|
1774
|
+
});
|
|
1775
|
+
if (ac.signal.aborted) cancelBody();
|
|
1776
|
+
else ac.signal.addEventListener("abort", cancelBody, { once: true });
|
|
1777
|
+
const parser = createSseParser(
|
|
1778
|
+
(frame) => {
|
|
1779
|
+
if (generation !== streamGeneration) return;
|
|
1780
|
+
const at = parseRemoteCursor(frame.id);
|
|
1781
|
+
if (at && lastCursor && compareCursors(at, lastCursor) <= 0) return;
|
|
1782
|
+
if (at) lastCursor = at;
|
|
1783
|
+
if (frame.id) cursor = frame.id;
|
|
1784
|
+
wait = minWait;
|
|
1785
|
+
onFrame(frame);
|
|
1786
|
+
},
|
|
1787
|
+
(ms) => {
|
|
1788
|
+
serverRetry = ms;
|
|
1789
|
+
}
|
|
1790
|
+
);
|
|
1791
|
+
for (; ; ) {
|
|
1792
|
+
const { done, value } = await reader.read();
|
|
1793
|
+
if (done || generation !== streamGeneration) break;
|
|
1794
|
+
parser.push(decoder.decode(value, { stream: true }));
|
|
1795
|
+
}
|
|
1796
|
+
} catch (err) {
|
|
1797
|
+
if (generation === streamGeneration && !disposed) {
|
|
1798
|
+
if (err instanceof GatewayError && err.status === 404) {
|
|
1799
|
+
forgetSession();
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1802
|
+
if (err instanceof GatewayError && (err.status === 400 || err.status === 401 || err.status === 403)) {
|
|
1803
|
+
sayError(err);
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
} finally {
|
|
1807
|
+
if (cancelBody) ac.signal.removeEventListener("abort", cancelBody);
|
|
1808
|
+
if (streamAbort === ac) streamAbort = void 0;
|
|
1809
|
+
}
|
|
1810
|
+
scheduleReconnect(generation);
|
|
1811
|
+
};
|
|
1812
|
+
const setSession = (next) => {
|
|
1813
|
+
if (next === sessionId) return;
|
|
1814
|
+
sessionId = next;
|
|
1815
|
+
snapshot = void 0;
|
|
1816
|
+
changed();
|
|
1817
|
+
try {
|
|
1818
|
+
options.onSessionId?.(next);
|
|
1819
|
+
} catch {
|
|
1820
|
+
}
|
|
1821
|
+
};
|
|
1822
|
+
const forgetSession = () => {
|
|
1823
|
+
closeStream();
|
|
1824
|
+
cursor = void 0;
|
|
1825
|
+
lastCursor = void 0;
|
|
1826
|
+
turn = void 0;
|
|
1827
|
+
turnIds = [];
|
|
1828
|
+
setSession(void 0);
|
|
1829
|
+
};
|
|
1830
|
+
const run = async (goal, runOptions) => {
|
|
1831
|
+
if (disposed || state.status === "running") return void 0;
|
|
1832
|
+
if (runOptions?.images?.length || runOptions?.files?.length) {
|
|
1833
|
+
sayError(new Error("this agent takes text only: attachments are not sent"));
|
|
1834
|
+
return void 0;
|
|
1835
|
+
}
|
|
1836
|
+
const text = goal.trim() ? goal : runOptions?.label ?? "";
|
|
1837
|
+
if (!text.trim()) return void 0;
|
|
1838
|
+
const id = newMessageId();
|
|
1839
|
+
const shown = runOptions?.label?.trim() ? runOptions.label : text;
|
|
1840
|
+
apply({ type: "status", status: "running" });
|
|
1841
|
+
apply({ type: "submitted", goal: shown });
|
|
1842
|
+
waiting.push({ id, text });
|
|
1843
|
+
const ended = new Promise((resolve) => runs.set(id, resolve));
|
|
1844
|
+
try {
|
|
1845
|
+
await sendMessage(id, text);
|
|
1846
|
+
} catch (err) {
|
|
1847
|
+
runs.delete(id);
|
|
1848
|
+
waiting = waiting.filter((w) => w.id !== id);
|
|
1849
|
+
fold(event({ type: "stopped" }));
|
|
1850
|
+
apply({ type: "status", status: "ready" });
|
|
1851
|
+
apply({ type: "undelivered", texts: [...state.undelivered, text] });
|
|
1852
|
+
sayError(err);
|
|
1853
|
+
return void 0;
|
|
1854
|
+
}
|
|
1855
|
+
return ended;
|
|
1856
|
+
};
|
|
1857
|
+
const sendMessage = async (id, text) => {
|
|
1858
|
+
const sid = sessionId;
|
|
1859
|
+
if (sid) {
|
|
1860
|
+
try {
|
|
1861
|
+
await post(`/sessions/${encodeURIComponent(sid)}/messages`, { text, id });
|
|
1862
|
+
return;
|
|
1863
|
+
} catch (err) {
|
|
1864
|
+
const gone = err instanceof GatewayError && (err.status === 404 || err.status === 409 && err.code === "session_ended");
|
|
1865
|
+
if (!gone || sessionId !== sid) throw err;
|
|
1866
|
+
forgetSession();
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
const made = await post("/sessions", { message: text, id });
|
|
1870
|
+
if (disposed) return;
|
|
1871
|
+
if (typeof made?.sessionId !== "string")
|
|
1872
|
+
throw new Error("the agent’s server started no session");
|
|
1873
|
+
closeStream();
|
|
1874
|
+
cursor = void 0;
|
|
1875
|
+
lastCursor = void 0;
|
|
1876
|
+
wait = minWait;
|
|
1877
|
+
setSession(made.sessionId);
|
|
1878
|
+
void openStream();
|
|
1879
|
+
};
|
|
1880
|
+
const send = (text) => {
|
|
1881
|
+
const clean = typeof text === "string" ? text.trim() : "";
|
|
1882
|
+
if (disposed || state.status !== "running" || !clean || !sessionId) return false;
|
|
1883
|
+
const id = newMessageId();
|
|
1884
|
+
waiting.push({ id, text: clean });
|
|
1885
|
+
fold(event({ type: "message.queued", id, text: clean }));
|
|
1886
|
+
void post(`/sessions/${encodeURIComponent(sessionId)}/messages`, { text: clean, id }).catch(
|
|
1887
|
+
(err) => {
|
|
1888
|
+
waiting = waiting.filter((w) => w.id !== id);
|
|
1889
|
+
fold({ type: "undelivered", texts: [...state.undelivered, clean], keepQueued: true });
|
|
1890
|
+
sayError(err);
|
|
1891
|
+
}
|
|
1892
|
+
);
|
|
1893
|
+
return true;
|
|
1894
|
+
};
|
|
1895
|
+
const stop = () => {
|
|
1896
|
+
if (disposed || !sessionId || state.status !== "running") return;
|
|
1897
|
+
const sid = sessionId;
|
|
1898
|
+
const left = waiting.map((w) => w.text);
|
|
1899
|
+
waiting = [];
|
|
1900
|
+
for (const [id, done] of runs) if (!turnIds.includes(id)) runs.delete(id), done(void 0);
|
|
1901
|
+
if (left.length > 0) fold({ type: "undelivered", texts: [...state.undelivered, ...left] });
|
|
1902
|
+
if (!turn) {
|
|
1903
|
+
fold(event({ type: "stopped" }));
|
|
1904
|
+
apply({ type: "status", status: "ready" });
|
|
1905
|
+
}
|
|
1906
|
+
void post(`/sessions/${encodeURIComponent(sid)}/stop`, {}).catch(sayError);
|
|
1907
|
+
};
|
|
1908
|
+
const answer = (id, body) => {
|
|
1909
|
+
const sid = sessionId;
|
|
1910
|
+
if (disposed || !sid || answering.has(id)) return;
|
|
1911
|
+
if (!state.approvals.some((a) => a.id === id && a.status === "pending")) return;
|
|
1912
|
+
answering.add(id);
|
|
1913
|
+
changed();
|
|
1914
|
+
void post(`/sessions/${encodeURIComponent(sid)}/answers/${encodeURIComponent(id)}`, body).catch(
|
|
1915
|
+
(err) => {
|
|
1916
|
+
if (err instanceof GatewayError && err.status === 409) return;
|
|
1917
|
+
answering.delete(id);
|
|
1918
|
+
changed();
|
|
1919
|
+
sayError(err);
|
|
1920
|
+
}
|
|
1921
|
+
);
|
|
1922
|
+
};
|
|
1923
|
+
const approve = (id, _opts) => {
|
|
1924
|
+
void _opts;
|
|
1925
|
+
answer(id, { approved: true });
|
|
1926
|
+
};
|
|
1927
|
+
const deny = (id, reason) => answer(id, { approved: false, ...reason ? { reason: reason.slice(0, 2e3) } : {} });
|
|
1928
|
+
const reset = () => {
|
|
1929
|
+
if (disposed) return;
|
|
1930
|
+
for (const [id, done] of runs) runs.delete(id), done(void 0);
|
|
1931
|
+
waiting = [];
|
|
1932
|
+
answering.clear();
|
|
1933
|
+
forgetSession();
|
|
1934
|
+
apply({ type: "reset" });
|
|
1935
|
+
apply({ type: "status", status: "ready" });
|
|
1936
|
+
};
|
|
1937
|
+
const loadChat = (chat) => {
|
|
1938
|
+
if (disposed) return;
|
|
1939
|
+
const next = chat.sessionId?.trim();
|
|
1940
|
+
if (!next || next === sessionId) return;
|
|
1941
|
+
reset();
|
|
1942
|
+
setSession(next);
|
|
1943
|
+
void openStream();
|
|
1944
|
+
};
|
|
1945
|
+
const dispose = () => {
|
|
1946
|
+
if (disposed) return;
|
|
1947
|
+
disposed = true;
|
|
1948
|
+
closeStream();
|
|
1949
|
+
for (const [id, done] of runs) runs.delete(id), done(void 0);
|
|
1950
|
+
listeners.clear();
|
|
1951
|
+
};
|
|
1952
|
+
if (sessionId) void openStream();
|
|
1953
|
+
const notAvailable = (what) => async () => {
|
|
1954
|
+
throw new Error(`${what} is not available for an agent on a server`);
|
|
1955
|
+
};
|
|
1956
|
+
return {
|
|
1957
|
+
getSnapshot: () => snapshot ??= snapshotOf(state, sessionId ?? "", answering),
|
|
1958
|
+
subscribe: (listener) => {
|
|
1959
|
+
if (disposed) return () => {
|
|
1960
|
+
};
|
|
1961
|
+
listeners.add(listener);
|
|
1962
|
+
return () => {
|
|
1963
|
+
listeners.delete(listener);
|
|
1964
|
+
};
|
|
1965
|
+
},
|
|
1966
|
+
// A server agent's configuration is the server's.
|
|
1967
|
+
update: () => {
|
|
1968
|
+
},
|
|
1969
|
+
dispose,
|
|
1970
|
+
get agent() {
|
|
1971
|
+
return void 0;
|
|
1972
|
+
},
|
|
1973
|
+
get disposed() {
|
|
1974
|
+
return disposed;
|
|
1975
|
+
},
|
|
1976
|
+
run,
|
|
1977
|
+
stop,
|
|
1978
|
+
stopRemote: () => {
|
|
1979
|
+
},
|
|
1980
|
+
send,
|
|
1981
|
+
sendRemote: () => false,
|
|
1982
|
+
stopSubagent: () => false,
|
|
1983
|
+
forgetSkill: async () => false,
|
|
1984
|
+
sendUndelivered: () => {
|
|
1985
|
+
const texts = state.undelivered;
|
|
1986
|
+
if (disposed || texts.length === 0 || state.status === "running") return;
|
|
1987
|
+
apply({ type: "undelivered", texts: [] });
|
|
1988
|
+
void run(texts.join("\n\n"));
|
|
1989
|
+
},
|
|
1990
|
+
discardUndelivered: () => apply({ type: "undelivered", texts: [] }),
|
|
1991
|
+
reset,
|
|
1992
|
+
// Opens the stream again at once (after the network came back, say).
|
|
1993
|
+
reload: () => {
|
|
1994
|
+
if (disposed || !sessionId) return;
|
|
1995
|
+
if (retryTimer !== void 0) clearTimeout(retryTimer);
|
|
1996
|
+
retryTimer = void 0;
|
|
1997
|
+
wait = minWait;
|
|
1998
|
+
void openStream();
|
|
1999
|
+
},
|
|
2000
|
+
approve,
|
|
2001
|
+
deny,
|
|
2002
|
+
answerQuestion: () => false,
|
|
2003
|
+
setApprovalMode: () => {
|
|
2004
|
+
},
|
|
2005
|
+
approveAppToolCall: notAvailable("An app’s tool call"),
|
|
2006
|
+
sampleForApp: notAvailable("An app’s sampling"),
|
|
2007
|
+
compact: async () => void 0,
|
|
2008
|
+
loadChat
|
|
2009
|
+
};
|
|
2010
|
+
};
|
|
2011
|
+
|
|
1429
2012
|
// src/controller.ts
|
|
1430
2013
|
var dbNameOf = (store) => {
|
|
1431
2014
|
const name = store?.dbName;
|
|
@@ -1433,7 +2016,7 @@ var dbNameOf = (store) => {
|
|
|
1433
2016
|
};
|
|
1434
2017
|
var modeOf = (config) => config.toolApproval?.mode ?? "autopilot";
|
|
1435
2018
|
var pageAppToolGrants = /* @__PURE__ */ new Set();
|
|
1436
|
-
var
|
|
2019
|
+
var snapshotOf2 = (state, extra) => {
|
|
1437
2020
|
const remoteRunning = state.remote?.running === true;
|
|
1438
2021
|
return Object.freeze({
|
|
1439
2022
|
...state,
|
|
@@ -1448,7 +2031,7 @@ var snapshotOf = (state, extra) => {
|
|
|
1448
2031
|
sessionId: extra.sessionId
|
|
1449
2032
|
});
|
|
1450
2033
|
};
|
|
1451
|
-
var createInitialAgentSnapshot = (config) =>
|
|
2034
|
+
var createInitialAgentSnapshot = (config) => snapshotOf2(createInitialAgentState(), {
|
|
1452
2035
|
sessionId: config.sessionId ?? "default",
|
|
1453
2036
|
pendingQuestions: [],
|
|
1454
2037
|
approvalMode: modeOf(config)
|
|
@@ -1536,7 +2119,7 @@ var createAgentController = (initialConfig, initialOptions = {}) => {
|
|
|
1536
2119
|
cancelled = true;
|
|
1537
2120
|
};
|
|
1538
2121
|
setBuildStatus({ status: "initializing" });
|
|
1539
|
-
(
|
|
2122
|
+
loadAgentCore().then((core) => core.createAgent(built)).then((next) => {
|
|
1540
2123
|
if (cancelled) return;
|
|
1541
2124
|
next.setToolApprovalMode(mode);
|
|
1542
2125
|
agent = next;
|
|
@@ -1557,11 +2140,22 @@ var createAgentController = (initialConfig, initialOptions = {}) => {
|
|
|
1557
2140
|
const syncWatch = () => {
|
|
1558
2141
|
const db = options.watch !== false && config.sessionLock !== "off" ? dbNameOf(config.memory) : void 0;
|
|
1559
2142
|
if (watched && watched.db === db && watched.sessionId === sessionId) return;
|
|
2143
|
+
const core = agentCoreIfLoaded();
|
|
2144
|
+
if (db && !core) {
|
|
2145
|
+
loadAgentCore().then(
|
|
2146
|
+
() => {
|
|
2147
|
+
if (!disposed) syncWatch();
|
|
2148
|
+
},
|
|
2149
|
+
() => {
|
|
2150
|
+
}
|
|
2151
|
+
);
|
|
2152
|
+
return;
|
|
2153
|
+
}
|
|
1560
2154
|
closeWatch();
|
|
1561
2155
|
watched = { db, sessionId };
|
|
1562
|
-
const
|
|
1563
|
-
if (!db || typeof
|
|
1564
|
-
watch =
|
|
2156
|
+
const watchSession = core?.watchSession;
|
|
2157
|
+
if (!db || typeof watchSession !== "function") return;
|
|
2158
|
+
watch = watchSession({
|
|
1565
2159
|
dbName: db,
|
|
1566
2160
|
sessionId,
|
|
1567
2161
|
onEvent: (event) => {
|
|
@@ -1595,7 +2189,7 @@ var createAgentController = (initialConfig, initialOptions = {}) => {
|
|
|
1595
2189
|
unfollowSkills?.();
|
|
1596
2190
|
skillsDb = { db };
|
|
1597
2191
|
unfollowSkills = followOtherTabs(
|
|
1598
|
-
|
|
2192
|
+
loadAgentCore,
|
|
1599
2193
|
db,
|
|
1600
2194
|
(change) => change.kind === "skills",
|
|
1601
2195
|
() => {
|
|
@@ -1608,10 +2202,11 @@ var createAgentController = (initialConfig, initialOptions = {}) => {
|
|
|
1608
2202
|
};
|
|
1609
2203
|
const run = async (goal, runOptions) => {
|
|
1610
2204
|
const current = agent;
|
|
1611
|
-
|
|
2205
|
+
const core = agentCoreIfLoaded();
|
|
2206
|
+
if (disposed || !current || abort || !core) return void 0;
|
|
1612
2207
|
const controller = new AbortController();
|
|
1613
2208
|
abort = controller;
|
|
1614
|
-
const runControl =
|
|
2209
|
+
const runControl = core.createRunControl();
|
|
1615
2210
|
control = runControl;
|
|
1616
2211
|
let left = [];
|
|
1617
2212
|
let stopped = false;
|
|
@@ -1845,7 +2440,7 @@ var createAgentController = (initialConfig, initialOptions = {}) => {
|
|
|
1845
2440
|
syncWatch();
|
|
1846
2441
|
syncSkills();
|
|
1847
2442
|
return {
|
|
1848
|
-
getSnapshot: () => snapshot ??=
|
|
2443
|
+
getSnapshot: () => snapshot ??= snapshotOf2(state, { sessionId, pendingQuestions, approvalMode }),
|
|
1849
2444
|
subscribe: (listener) => {
|
|
1850
2445
|
if (disposed) return () => {
|
|
1851
2446
|
};
|
|
@@ -1886,5 +2481,11 @@ var createAgentController = (initialConfig, initialOptions = {}) => {
|
|
|
1886
2481
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1887
2482
|
0 && (module.exports = {
|
|
1888
2483
|
createAgentController,
|
|
1889
|
-
createInitialAgentSnapshot
|
|
2484
|
+
createInitialAgentSnapshot,
|
|
2485
|
+
createInitialRemoteSnapshot,
|
|
2486
|
+
createRemoteAgentController,
|
|
2487
|
+
createSseParser,
|
|
2488
|
+
parseRemoteCursor,
|
|
2489
|
+
remoteEventOf,
|
|
2490
|
+
remoteEventScope
|
|
1890
2491
|
});
|