@0perator/operator 0.1.222 → 0.1.261

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.
Files changed (110) hide show
  1. package/README.md +6 -5
  2. package/dashboard/server.mjs +1 -1
  3. package/dist/acp/agent.js +320 -53
  4. package/dist/acp/auth.js +44 -0
  5. package/dist/acp/config-options.js +34 -0
  6. package/dist/acp/prompt.js +41 -0
  7. package/dist/acp/run.js +116 -10
  8. package/dist/acp/tool-map.js +48 -0
  9. package/dist/auth/byo.js +125 -0
  10. package/dist/auth/client.js +12 -6
  11. package/dist/auth/commands.js +71 -3
  12. package/dist/auth/identity.js +136 -0
  13. package/dist/auth/store.js +7 -5
  14. package/dist/autonomy/board.js +3 -2
  15. package/dist/autonomy/cron.js +332 -0
  16. package/dist/autonomy/delegate.js +168 -0
  17. package/dist/autonomy/dream.js +1 -1
  18. package/dist/autonomy/index.js +16 -2
  19. package/dist/autonomy/media-robot.js +137 -0
  20. package/dist/autonomy/profile.js +77 -0
  21. package/dist/autonomy/routines.js +5 -4
  22. package/dist/autonomy/session-factory.js +19 -10
  23. package/dist/background/index.js +8 -1
  24. package/dist/browser/cdp.js +266 -0
  25. package/dist/browser/session.js +286 -0
  26. package/dist/cli.js +207 -106
  27. package/dist/compose/client.js +56 -0
  28. package/dist/config/brand.js +3 -3
  29. package/dist/config/home.js +70 -0
  30. package/dist/config/managed.js +230 -0
  31. package/dist/config/persona.js +243 -21
  32. package/dist/config/scaffold.js +142 -0
  33. package/dist/config/settings.js +87 -20
  34. package/dist/config/skills-index.js +175 -0
  35. package/dist/consolidation/scheduler.js +10 -7
  36. package/dist/consolidation/sleep.js +17 -3
  37. package/dist/engine/agent.js +231 -21
  38. package/dist/engine/aurora.js +79 -14
  39. package/dist/engine/batch-scheduler.js +128 -0
  40. package/dist/engine/compaction.js +129 -2
  41. package/dist/engine/computer-event.js +139 -0
  42. package/dist/engine/continuation.js +55 -0
  43. package/dist/engine/contract.js +3 -1
  44. package/dist/engine/grandmaster.js +13 -3
  45. package/dist/engine/json-repair.js +105 -0
  46. package/dist/engine/messages.js +52 -5
  47. package/dist/engine/redact.js +25 -0
  48. package/dist/engine/rolling-summary.js +28 -11
  49. package/dist/engine/router-adapter.js +2 -0
  50. package/dist/engine/unrestricted.js +51 -0
  51. package/dist/engine/verify-gate.js +6 -0
  52. package/dist/enterprise/cli.js +137 -0
  53. package/dist/flywheel/processor.js +30 -2
  54. package/dist/flywheel/secret-gate.js +123 -0
  55. package/dist/index.browser.js +4 -0
  56. package/dist/index.js +4 -0
  57. package/dist/mcp/index.js +18 -24
  58. package/dist/memory/index.js +0 -0
  59. package/dist/memory/session-index.js +324 -0
  60. package/dist/memory/session-rewind.js +92 -0
  61. package/dist/memory/state-maintenance.js +441 -0
  62. package/dist/observability/index.js +3 -3
  63. package/dist/permissions/audit.js +4 -1
  64. package/dist/permissions/index.js +24 -12
  65. package/dist/permissions/policy.js +4 -3
  66. package/dist/permissions/tier.js +31 -14
  67. package/dist/remote/host.js +9 -2
  68. package/dist/router/models.js +62 -2
  69. package/dist/selfimprove/index.js +9 -7
  70. package/dist/session.js +129 -42
  71. package/dist/skills/catalogue.js +167 -0
  72. package/dist/telemetry/audit-verify.js +94 -0
  73. package/dist/telemetry/events.js +92 -7
  74. package/dist/telemetry/lifecycle.js +18 -0
  75. package/dist/tools/computer.js +1 -1
  76. package/dist/tools/deferred.js +117 -0
  77. package/dist/tools/executor.js +27 -23
  78. package/dist/tools/memory.js +117 -0
  79. package/dist/tools/mutating.js +6 -0
  80. package/dist/tools/registry.js +1022 -46
  81. package/dist/tools/sandbox.js +63 -17
  82. package/dist/tools/searxng.js +2 -2
  83. package/dist/tools/shell-session.js +36 -1
  84. package/dist/tools/tool-output-spill.js +4 -4
  85. package/dist/ui/ActivitySpinner.js +17 -6
  86. package/dist/ui/App.js +36 -14
  87. package/dist/ui/ArtifactPicker.js +109 -0
  88. package/dist/ui/Composer.js +39 -8
  89. package/dist/ui/Markdown.js +5 -3
  90. package/dist/ui/MouseProvider.js +99 -19
  91. package/dist/ui/QuestionPrompt.js +24 -3
  92. package/dist/ui/ThinkingSpinner.js +9 -5
  93. package/dist/ui/ToolStep.js +17 -12
  94. package/dist/ui/Transcript.js +28 -9
  95. package/dist/ui/capabilities.js +0 -9
  96. package/dist/ui/listhit.js +23 -0
  97. package/dist/ui/mouse.js +22 -0
  98. package/dist/ui/osc.js +7 -4
  99. package/dist/ui/progressbar.js +46 -0
  100. package/dist/ui/shimmer.js +43 -0
  101. package/dist/ui/theme.js +2 -0
  102. package/dist/ui/tick.js +48 -0
  103. package/dist/ui/toolicons.js +69 -0
  104. package/dist/ui/useInputHistory.js +3 -3
  105. package/dist/util/slash.js +1 -0
  106. package/dist/vault/cli.js +48 -0
  107. package/dist/vault/store.js +157 -0
  108. package/dist/vault/tool.js +50 -0
  109. package/package.json +3 -5
  110. package/CHANGELOG.md +0 -1629
@@ -0,0 +1,286 @@
1
+ // Persistent browser session + the two registry surfaces for HV3-02.
2
+ // ONE headless Chrome per Operator session (mirrors src/tools/shell-session.ts): pages, cookies and
3
+ // scroll state persist across tool calls; an idle browser decommissions on TTL; a dead Chrome
4
+ // respawns transparently. Tools are split READ vs ACT so the registry-derived permission policy
5
+ // (0.1.226) can let gated/plan mode READ pages freely while every page MUTATION stays gated.
6
+ import { CdpConnection, launchChrome, checkNavigationUrl, evalResultToText, fillScript, BROWSER_CONSTANTS, } from "./cdp.js";
7
+ async function nodeImport(id) {
8
+ const g = globalThis;
9
+ try {
10
+ const m = g.process?.getBuiltinModule?.(id);
11
+ if (m)
12
+ return m;
13
+ }
14
+ catch { /* dynamic */ }
15
+ return import(id);
16
+ }
17
+ class BrowserSession {
18
+ conn = null;
19
+ proc = null;
20
+ tabs = [];
21
+ active = -1;
22
+ lastUsed = 0;
23
+ async ensure() {
24
+ const now = Date.now();
25
+ const alive = this.conn && this.proc && this.proc.exitCode == null &&
26
+ now - this.lastUsed < BROWSER_CONSTANTS.IDLE_TTL_MS;
27
+ if (alive) {
28
+ this.lastUsed = now;
29
+ return this.conn;
30
+ }
31
+ await this.dispose(); // TTL expiry or dead child: rebuild clean (cookies live in the $KODE_HOME/browser profile, so a relaunch is cheap for state)
32
+ const { proc, wsUrl } = await launchChrome();
33
+ const conn = new CdpConnection(wsUrl);
34
+ await conn.connect();
35
+ this.proc = proc;
36
+ this.conn = conn;
37
+ this.tabs = [];
38
+ this.active = -1;
39
+ this.lastUsed = Date.now();
40
+ return conn;
41
+ }
42
+ /** Attach a fresh tab (target) and make it active. */
43
+ async newTab(conn, url) {
44
+ const created = await conn.send("Target.createTarget", { url: "about:blank" });
45
+ const attached = await conn.send("Target.attachToTarget", { targetId: created.targetId, flatten: true });
46
+ const tab = { targetId: created.targetId, sessionId: attached.sessionId, url: "about:blank" };
47
+ this.tabs.push(tab);
48
+ this.active = this.tabs.length - 1;
49
+ await conn.send("Page.enable", {}, tab.sessionId);
50
+ await conn.send("Runtime.enable", {}, tab.sessionId);
51
+ if (url !== "about:blank")
52
+ await this.navigate(conn, tab, url);
53
+ return tab;
54
+ }
55
+ tab() {
56
+ if (this.active < 0 || !this.tabs[this.active])
57
+ throw new Error("browser: no active tab — call open first");
58
+ return this.tabs[this.active];
59
+ }
60
+ async navigate(conn, tab, url) {
61
+ const blocked = checkNavigationUrl(url);
62
+ if (blocked)
63
+ throw new Error(blocked);
64
+ const loadFired = conn.once("Page.loadEventFired"); // subscribe BEFORE navigate so we never miss it
65
+ await conn.send("Page.navigate", { url }, tab.sessionId);
66
+ const settled = await Promise.race([
67
+ loadFired.then(() => "load"),
68
+ new Promise((res) => setTimeout(() => res("timeout"), BROWSER_CONSTANTS.NAV_TIMEOUT_MS)),
69
+ ]);
70
+ // load fired (or timed out) — give SPAs a beat to paint, then confirm readyState
71
+ await new Promise((r) => setTimeout(r, 300));
72
+ try {
73
+ const rs = await conn.send("Runtime.evaluate", { expression: "document.readyState", returnByValue: true }, tab.sessionId);
74
+ tab.url = (await conn.send("Runtime.evaluate", { expression: "location.href", returnByValue: true }, tab.sessionId))?.result?.value ?? url;
75
+ void settled;
76
+ void rs;
77
+ }
78
+ catch {
79
+ tab.url = url;
80
+ }
81
+ }
82
+ async evaluate(expr, awaitPromise = false) {
83
+ const conn = await this.ensure();
84
+ const t = this.tab();
85
+ const raw = await conn.send("Runtime.evaluate", { expression: expr, returnByValue: true, awaitPromise }, t.sessionId);
86
+ return evalResultToText(raw);
87
+ }
88
+ async run(action, a) {
89
+ const conn = await this.ensure();
90
+ switch (action) {
91
+ case "open": {
92
+ const url = String(a.url ?? "about:blank");
93
+ await this.newTab(conn, url);
94
+ return this.infoInner(conn);
95
+ }
96
+ case "navigate": {
97
+ await this.navigate(conn, this.tab(), String(a.url));
98
+ return this.infoInner(conn);
99
+ }
100
+ case "reload": {
101
+ await conn.send("Page.reload", { ignoreCache: !!a.cache_bust }, this.tab().sessionId);
102
+ await new Promise((r) => setTimeout(r, 500));
103
+ return this.infoInner(conn);
104
+ }
105
+ case "back":
106
+ case "forward": {
107
+ // history via JS (no CDP history domain): simplest reliable path
108
+ await this.evaluate(action === "back" ? "history.back(); 'back'" : "history.forward(); 'forward'");
109
+ await new Promise((r) => setTimeout(r, 400));
110
+ return this.infoInner(conn);
111
+ }
112
+ case "info": return this.infoInner(conn);
113
+ case "text": {
114
+ const max = Math.max(500, Math.min(Number(a.max_chars ?? 20000), 100_000));
115
+ const title = await this.evaluate("document.title");
116
+ const url = await this.evaluate("location.href");
117
+ const body = await this.evaluate("(() => { const b = document.body; const t = b ? b.innerText : ''; return t.replace(/\\n{3,}/g, '\\n\\n').slice(0, " + max + "); })()");
118
+ return `# ${title}\n${url}\n\n${body}`;
119
+ }
120
+ case "eval": {
121
+ const out = await this.evaluate(String(a.js ?? "1"), true);
122
+ return String(out);
123
+ }
124
+ case "click": {
125
+ const sel = String(a.selector);
126
+ const out = await this.evaluate(`(() => {
127
+ const el = document.querySelector(${JSON.stringify(sel)});
128
+ if (!el) throw new Error("element not found: " + ${JSON.stringify(sel)});
129
+ el.scrollIntoView({ block: "center" });
130
+ if (typeof el.click === "function") el.click(); else el.dispatchEvent(new MouseEvent("click", { bubbles: true }));
131
+ return "clicked " + (el.tagName + (el.id ? "#" + el.id : "")).toLowerCase();
132
+ })()`);
133
+ await new Promise((r) => setTimeout(r, Number(a.settle_ms ?? 250)));
134
+ return String(out);
135
+ }
136
+ case "fill": {
137
+ const out = await this.evaluate(fillScript(String(a.selector), String(a.text ?? "")));
138
+ return `filled ${a.selector}: ${out}`;
139
+ }
140
+ case "submit": {
141
+ return String(await this.evaluate(`(() => {
142
+ const el = document.querySelector(${JSON.stringify(String(a.selector))});
143
+ const f = el && (el.form || (el.tagName === "FORM" ? el : null));
144
+ if (!f) throw new Error("no form for selector: " + ${JSON.stringify(String(a.selector))});
145
+ if (typeof f.requestSubmit === "function") f.requestSubmit(); else f.submit();
146
+ return "submitted " + (f.action || "(inline)");
147
+ })()`));
148
+ }
149
+ case "tab_list": {
150
+ const list = await conn.send("Target.getTargets", {});
151
+ const pages = list.targetInfos.filter((x) => x.type === "page");
152
+ return pages.map((p, i) => `${i === this.active ? "*" : " "} ${i} ${p.title || "(untitled)"} — ${p.url.slice(0, 120)}`).join("\n") || "(no pages)";
153
+ }
154
+ case "tab_select": {
155
+ const i = Number(a.index);
156
+ if (!this.tabs[i])
157
+ throw new Error(`browser: no tab ${i} (have ${this.tabs.length})`);
158
+ this.active = i;
159
+ return this.infoInner(conn);
160
+ }
161
+ case "tab_close": {
162
+ const i = Number.isFinite(Number(a.index)) ? Number(a.index) : this.active;
163
+ const t = this.tabs[i];
164
+ if (!t)
165
+ throw new Error(`browser: no tab ${i}`);
166
+ await conn.send("Target.closeTarget", { targetId: t.targetId }).catch(() => undefined);
167
+ this.tabs.splice(i, 1);
168
+ this.active = Math.min(this.active, this.tabs.length - 1);
169
+ return `closed tab ${i}; ${this.tabs.length} left`;
170
+ }
171
+ case "screenshot": {
172
+ const shot = await conn.send("Page.captureScreenshot", { format: "png" }, this.tab().sessionId);
173
+ const bytes = Math.round(shot.data.length * 3 / 4); // base64 → raw size
174
+ const { mkdirSync, writeFileSync } = await nodeImport("node:fs");
175
+ const { operatorPath } = await nodeImport("../config/home.js");
176
+ let path;
177
+ try {
178
+ const dir = operatorPath("cache", "browser");
179
+ mkdirSync(dir, { recursive: true });
180
+ path = `${dir}/shot-${Date.now()}.png`;
181
+ }
182
+ catch {
183
+ const os = await nodeImport("node:os");
184
+ path = `${os.tmpdir()}/kode-shot-${Date.now()}.png`;
185
+ }
186
+ writeFileSync(path, shot.data, "base64");
187
+ return `screenshot ${path} (${Math.round(bytes / 1024)} KB) — pass to image_analyze if you must see pixels; prefer text/eval`;
188
+ }
189
+ case "close": {
190
+ await this.dispose();
191
+ return "browser closed";
192
+ }
193
+ default: throw new Error(`browser: unknown action '${action}'`);
194
+ }
195
+ }
196
+ async infoInner(conn) {
197
+ try {
198
+ const title = await conn.send("Runtime.evaluate", { expression: "document.title", returnByValue: true }, this.tab().sessionId);
199
+ const url = await conn.send("Runtime.evaluate", { expression: "location.href + ' | ' + document.readyState", returnByValue: true }, this.tab().sessionId);
200
+ return `${title?.result?.value || "(untitled)"} — ${url?.result?.value ?? "?"}`;
201
+ }
202
+ catch (e) {
203
+ return `info failed: ${e.message}`;
204
+ }
205
+ }
206
+ async dispose() {
207
+ try {
208
+ this.conn?.close();
209
+ }
210
+ catch { /* gone */ }
211
+ this.conn = null;
212
+ try {
213
+ this.proc?.kill?.("SIGTERM");
214
+ }
215
+ catch { /* gone */ }
216
+ this.proc = null;
217
+ this.tabs = [];
218
+ this.active = -1;
219
+ }
220
+ /** href of the active tab (origin-bound vault checks). Throws cleanly when no page. */
221
+ async href() {
222
+ const conn = await this.ensure();
223
+ const t = this.tab();
224
+ const r = await conn.send("Runtime.evaluate", { expression: "location.href", returnByValue: true }, t.sessionId);
225
+ const v = r?.result?.value;
226
+ if (!v)
227
+ throw new Error("no active page href");
228
+ return v;
229
+ }
230
+ /** Fill user+password fields atomically from the VAULT PATH — the secret value is a parameter to
231
+ * this function only; callers above (tool glue) must never echo it back. */
232
+ async fillPair(userSel, userVal, passSel, passVal) {
233
+ await this.evaluate(fillScript(userSel, userVal));
234
+ await this.evaluate(fillScript(passSel, passVal));
235
+ }
236
+ }
237
+ let singleton = null;
238
+ function session() {
239
+ if (!singleton)
240
+ singleton = new BrowserSession();
241
+ return singleton;
242
+ }
243
+ /** Async graceful teardown (TTL/idle paths). */
244
+ export async function disposeBrowser() {
245
+ if (singleton) {
246
+ await singleton.dispose();
247
+ singleton = null;
248
+ }
249
+ }
250
+ /** SYNC teardown for process-exit hooks — the same contract as disposeShellSession(): the SIGTERM
251
+ * is delivered synchronously inside dispose(), so the Chrome child is signaled before
252
+ * process.exit() can leave it orphaned. */
253
+ export function disposeBrowserNow() {
254
+ if (!singleton)
255
+ return;
256
+ void singleton.dispose().catch(() => undefined);
257
+ singleton = null;
258
+ }
259
+ export const BROWSER_READ_ACTIONS = ["info", "text", "tab_list"];
260
+ export const BROWSER_ACT_ACTIONS = ["open", "navigate", "reload", "back", "forward", "eval", "click", "fill", "submit", "tab_select", "tab_close", "screenshot", "close"];
261
+ /** The vault glue needs these two session primitives. */
262
+ export const browserSessionHooks = {
263
+ activeHref: () => session().href(),
264
+ fillPair: (u, uv, p, pv) => session().fillPair(u, uv, p, pv),
265
+ };
266
+ /** run() for the `browser` tool (ACT side, plus reads for convenience in ungated modes). */
267
+ export async function runBrowser(args) {
268
+ try {
269
+ return await session().run(String(args.action ?? "info"), args);
270
+ }
271
+ catch (e) {
272
+ const msg = e?.message ?? String(e);
273
+ if (/devtools|spawn|ws |Chrome|chrome/.test(msg))
274
+ return `browser: ${msg}`;
275
+ return `browser error: ${msg}`;
276
+ }
277
+ }
278
+ /** run() for the `browser_read` tool — read-only actions ONLY (the registry-derived permission
279
+ * policy lets safe tools flow in gated/plan/dream; eval/click/fill stay behind the gated one). */
280
+ export async function runBrowserRead(args) {
281
+ const action = String(args.action ?? "info");
282
+ if (!BROWSER_READ_ACTIONS.includes(action)) {
283
+ return `browser_read: '${action}' mutates or executes — use the gated 'browser' tool`;
284
+ }
285
+ return runBrowser({ ...args, action });
286
+ }