@0perator/operator 0.1.22 → 0.1.23
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/dist/cli.js +12 -1
- package/dist/tools/registry.js +24 -11
- package/dist/tools/searxng.js +196 -0
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -16,7 +16,7 @@ import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
|
|
|
16
16
|
import { join } from "node:path";
|
|
17
17
|
import { homedir } from "node:os";
|
|
18
18
|
import { createHash } from "node:crypto";
|
|
19
|
-
const VERSION = "0.1.
|
|
19
|
+
const VERSION = "0.1.23";
|
|
20
20
|
// operator/settings.json (override path via KODE_TSI_SETTINGS), env vars still win — see config/settings.ts.
|
|
21
21
|
const settings = loadSettings();
|
|
22
22
|
// Propagate the resolved sandbox mode to the tool layer (registry reads KODE_SANDBOX at exec time).
|
|
@@ -28,6 +28,17 @@ if (settings.search?.autopilotUrl)
|
|
|
28
28
|
process.env.KODE_AUTOPILOT_URL = settings.search.autopilotUrl;
|
|
29
29
|
if (settings.search?.searxngUrl)
|
|
30
30
|
process.env.KODE_SEARXNG_URL = settings.search.searxngUrl;
|
|
31
|
+
const sd = settings.search?.docker;
|
|
32
|
+
if (sd) {
|
|
33
|
+
if (sd.enabled === false)
|
|
34
|
+
process.env.KODE_SEARXNG_MANAGE = "0";
|
|
35
|
+
if (sd.port)
|
|
36
|
+
process.env.KODE_SEARXNG_PORT = String(sd.port);
|
|
37
|
+
if (sd.image)
|
|
38
|
+
process.env.KODE_SEARXNG_IMAGE = sd.image;
|
|
39
|
+
if (sd.idleSec !== undefined)
|
|
40
|
+
process.env.KODE_SEARXNG_IDLE_SEC = String(sd.idleSec);
|
|
41
|
+
}
|
|
31
42
|
// Computer control (opt-in, macOS embodiment). When enabled: register the control tools into the shared
|
|
32
43
|
// registry and compute permission rules. The FOREGROUND forces each computer_* tool to "ask" so every
|
|
33
44
|
// action prompts the owner via the approve() bridge in <Root>; BACKGROUND sub-agents get them DENIED —
|
package/dist/tools/registry.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
const nodeImport = (m) => import(m);
|
|
8
8
|
import { MemoryStore } from "../memory/index.js";
|
|
9
9
|
import { deniedWriteAsync, deniedReadAsync, wrapShell } from "./sandbox.js";
|
|
10
|
+
import { ensureSearxng, noteSearxngUsed } from "./searxng.js";
|
|
10
11
|
// Lazily built + cached on first use (node only). Same contract as promisify(execFile): resolves
|
|
11
12
|
// {stdout,stderr}, rejects with the error object (stdout/stderr attached) on non-zero exit.
|
|
12
13
|
let _pexec = null;
|
|
@@ -261,6 +262,27 @@ async function webSearch(args) {
|
|
|
261
262
|
const ctrl = new AbortController();
|
|
262
263
|
const timer = setTimeout(() => ctrl.abort(), Number(args.timeout_ms ?? 25_000));
|
|
263
264
|
try {
|
|
265
|
+
// Backend order: the reliable local SearXNG first (clean URLs, all engines), then the AXE autopilot
|
|
266
|
+
// SERP as fallback (real-browser scraping — higher fidelity on JS-heavy pages but engine-brittle).
|
|
267
|
+
// A backend that returns EMPTY (not just errors) falls through to the other, so one stale scraper
|
|
268
|
+
// never yields "(no results)" when the other could answer.
|
|
269
|
+
// SearXNG: an explicit KODE_SEARXNG_URL if reachable, else a container Operator stands up on demand
|
|
270
|
+
// and tears down when idle / at exit (ensureSearxng handles both; null = unavailable here).
|
|
271
|
+
const searx = await ensureSearxng();
|
|
272
|
+
if (searx) {
|
|
273
|
+
try {
|
|
274
|
+
const res = await fetch(`${searx.replace(/\/$/, "")}/search?q=${encodeURIComponent(q)}&format=json`, { signal: ctrl.signal, headers: { "User-Agent": "operator/kode-tsi" } });
|
|
275
|
+
if (res.ok) {
|
|
276
|
+
const j = await res.json().catch(() => ({}));
|
|
277
|
+
const rows = j.results ?? [];
|
|
278
|
+
if (rows.length) {
|
|
279
|
+
noteSearxngUsed();
|
|
280
|
+
return fmtResults(rows, n);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch { /* fall through to autopilot */ }
|
|
285
|
+
}
|
|
264
286
|
const ap = process.env.KODE_AUTOPILOT_URL;
|
|
265
287
|
if (ap) {
|
|
266
288
|
const res = await fetch(`${ap.replace(/\/$/, "")}/serp`, {
|
|
@@ -271,20 +293,11 @@ async function webSearch(args) {
|
|
|
271
293
|
if (res.ok) {
|
|
272
294
|
const j = await res.json().catch(() => null);
|
|
273
295
|
const rows = Array.isArray(j) ? j : (j?.results ?? j?.data);
|
|
274
|
-
if (Array.isArray(rows))
|
|
296
|
+
if (Array.isArray(rows) && rows.length)
|
|
275
297
|
return fmtResults(rows, n);
|
|
276
298
|
}
|
|
277
|
-
// autopilot configured but errored/odd-shaped — try SearXNG next rather than fail hard.
|
|
278
|
-
}
|
|
279
|
-
const searx = process.env.KODE_SEARXNG_URL;
|
|
280
|
-
if (searx) {
|
|
281
|
-
const res = await fetch(`${searx.replace(/\/$/, "")}/search?q=${encodeURIComponent(q)}&format=json`, { signal: ctrl.signal, headers: { "User-Agent": "operator/kode-tsi" } });
|
|
282
|
-
if (res.ok) {
|
|
283
|
-
const j = await res.json().catch(() => ({}));
|
|
284
|
-
return fmtResults(j.results ?? [], n);
|
|
285
|
-
}
|
|
286
299
|
}
|
|
287
|
-
return "error: web_search
|
|
300
|
+
return "error: web_search found no results — the local SearXNG (auto-managed via Docker) and the AXE autopilot SERP both returned nothing or are unavailable. Ensure Docker is running, or set KODE_AUTOPILOT_URL / KODE_SEARXNG_URL.";
|
|
288
301
|
}
|
|
289
302
|
catch (e) {
|
|
290
303
|
const msg = e instanceof Error ? e.message : String(e);
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
// Seamless local SearXNG backend for web_search — Operator stands up a Docker container on the first
|
|
2
|
+
// search and tears it down when the session ends or after an idle window, so there's nothing to babysit
|
|
3
|
+
// and no always-on RAM cost. CLI-only: every node:child_process / node:fs import is LAZY via an opaque
|
|
4
|
+
// specifier (same discipline as sandbox.ts) so the platform:neutral mobile JSCore bundle never resolves
|
|
5
|
+
// them — on mobile these functions simply no-op (Docker isn't there; ensureSearxng returns null).
|
|
6
|
+
//
|
|
7
|
+
// Lifecycle:
|
|
8
|
+
// • ensureSearxng() — called before web_search falls through to "no backend". Reuses a reachable URL;
|
|
9
|
+
// otherwise (auto-manage on + Docker present) starts/reuses a named container, waits for health,
|
|
10
|
+
// arms the idle timer + exit hooks, and returns its URL.
|
|
11
|
+
// • noteSearxngUsed() — resets the idle countdown after a successful search.
|
|
12
|
+
// • stopManagedSearxng() — stops the container if WE started it (best-effort).
|
|
13
|
+
//
|
|
14
|
+
// Config (env, projected from settings.search.docker by cli.tsx):
|
|
15
|
+
// KODE_SEARXNG_URL explicit backend; if reachable we use it and never manage a container
|
|
16
|
+
// KODE_SEARXNG_MANAGE=0 disable auto-manage entirely (default: on)
|
|
17
|
+
// KODE_SEARXNG_PORT host port (default 8888)
|
|
18
|
+
// KODE_SEARXNG_IMAGE image (default searxng/searxng:latest)
|
|
19
|
+
// KODE_SEARXNG_IDLE_SEC idle seconds before auto-stop (default 300; 0 = only stop on exit)
|
|
20
|
+
const nodeImport = (m) => import(/* @vite-ignore */ m);
|
|
21
|
+
const CONTAINER = "operator-searxng";
|
|
22
|
+
function port() {
|
|
23
|
+
const p = Number(process.env.KODE_SEARXNG_PORT);
|
|
24
|
+
return Number.isFinite(p) && p > 0 ? p : 8888;
|
|
25
|
+
}
|
|
26
|
+
function image() {
|
|
27
|
+
return process.env.KODE_SEARXNG_IMAGE || "searxng/searxng:latest";
|
|
28
|
+
}
|
|
29
|
+
function idleMs() {
|
|
30
|
+
const s = process.env.KODE_SEARXNG_IDLE_SEC;
|
|
31
|
+
const n = s === undefined ? 300 : Number(s);
|
|
32
|
+
return Number.isFinite(n) && n > 0 ? n * 1000 : 0;
|
|
33
|
+
}
|
|
34
|
+
function autoManage() {
|
|
35
|
+
return process.platform !== undefined && process.env.KODE_SEARXNG_MANAGE !== "0";
|
|
36
|
+
}
|
|
37
|
+
let managed = false; // true once WE start/adopt the container this process
|
|
38
|
+
let hooksArmed = false;
|
|
39
|
+
let idleTimer = null;
|
|
40
|
+
let spawnSyncFn = null; // preloaded for the sync exit hook (ESM has no require)
|
|
41
|
+
/** GET <url>/healthz with a short timeout; true iff it answers 200. */
|
|
42
|
+
async function reachable(base, ms = 2500) {
|
|
43
|
+
const ctrl = new AbortController();
|
|
44
|
+
const t = setTimeout(() => ctrl.abort(), ms);
|
|
45
|
+
try {
|
|
46
|
+
const r = await fetch(`${base.replace(/\/$/, "")}/healthz`, { signal: ctrl.signal });
|
|
47
|
+
return r.ok;
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
clearTimeout(t);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Run a docker command, resolving { code, stdout }. Never throws (missing docker -> code 127). */
|
|
57
|
+
async function docker(args, timeoutMs = 20_000) {
|
|
58
|
+
const { execFile } = await nodeImport("node:child_process");
|
|
59
|
+
return new Promise((resolve) => {
|
|
60
|
+
const child = execFile("docker", args, { timeout: timeoutMs }, (err, stdout) => {
|
|
61
|
+
resolve({ code: err ? (typeof err.code === "number" ? err.code : 1) : 0, out: (stdout || "").trim() });
|
|
62
|
+
});
|
|
63
|
+
child.on?.("error", () => resolve({ code: 127, out: "" }));
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async function dockerAvailable() {
|
|
67
|
+
return (await docker(["info", "--format", "{{.ServerVersion}}"], 8000)).code === 0;
|
|
68
|
+
}
|
|
69
|
+
/** Write ~/.axe/operator/searxng/settings.yml (json API on + a secret) if absent; return the dir. */
|
|
70
|
+
async function ensureSettingsDir() {
|
|
71
|
+
const fs = await nodeImport("node:fs/promises");
|
|
72
|
+
const home = process.env.HOME || "";
|
|
73
|
+
const dir = `${home}/.axe/operator/searxng`;
|
|
74
|
+
await fs.mkdir(dir, { recursive: true });
|
|
75
|
+
const file = `${dir}/settings.yml`;
|
|
76
|
+
try {
|
|
77
|
+
await fs.access(file);
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
const crypto = await nodeImport("node:crypto");
|
|
81
|
+
const secret = crypto.randomBytes(32).toString("hex");
|
|
82
|
+
// SearXNG ships with the JSON API OFF; web_search needs format=json, so enable it explicitly.
|
|
83
|
+
const yml = `use_default_settings: true\n` +
|
|
84
|
+
`server:\n secret_key: "${secret}"\n limiter: false\n bind_address: "0.0.0.0"\n` +
|
|
85
|
+
`search:\n safe_search: 0\n formats:\n - html\n - json\n`;
|
|
86
|
+
await fs.writeFile(file, yml, "utf8");
|
|
87
|
+
}
|
|
88
|
+
return dir;
|
|
89
|
+
}
|
|
90
|
+
async function armHooks() {
|
|
91
|
+
if (hooksArmed)
|
|
92
|
+
return;
|
|
93
|
+
hooksArmed = true;
|
|
94
|
+
// Preload spawnSync now (async) so the synchronous 'exit' handler can call it — ESM has no require().
|
|
95
|
+
try {
|
|
96
|
+
spawnSyncFn = (await nodeImport("node:child_process")).spawnSync;
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
spawnSyncFn = null;
|
|
100
|
+
}
|
|
101
|
+
// Clean session exit: stop synchronously (bounded) so we don't leak the container.
|
|
102
|
+
process.on("exit", () => {
|
|
103
|
+
if (!managed || !spawnSyncFn)
|
|
104
|
+
return;
|
|
105
|
+
try {
|
|
106
|
+
spawnSyncFn("docker", ["stop", "-t", "2", CONTAINER], { timeout: 6000, stdio: "ignore" });
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
/* best-effort */
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
// Ctrl-C / kill: stop then let the default handler end the process.
|
|
113
|
+
for (const sig of ["SIGINT", "SIGTERM"]) {
|
|
114
|
+
process.once(sig, () => {
|
|
115
|
+
void stopManagedSearxng().finally(() => process.exit(0));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function resetIdle() {
|
|
120
|
+
const ms = idleMs();
|
|
121
|
+
if (idleTimer)
|
|
122
|
+
clearTimeout(idleTimer);
|
|
123
|
+
idleTimer = null;
|
|
124
|
+
if (!ms || !managed)
|
|
125
|
+
return;
|
|
126
|
+
idleTimer = setTimeout(() => void stopManagedSearxng(), ms);
|
|
127
|
+
idleTimer.unref?.(); // never keep the process alive just to wait to stop the container
|
|
128
|
+
}
|
|
129
|
+
/** Ensure the managed container is up on `port()`; adopt a reachable one, start a stopped one, else run. */
|
|
130
|
+
async function bringUp() {
|
|
131
|
+
const p = port();
|
|
132
|
+
// already running?
|
|
133
|
+
const ps = await docker(["ps", "--filter", `name=^/${CONTAINER}$`, "--format", "{{.Names}}"]);
|
|
134
|
+
if (ps.out === CONTAINER)
|
|
135
|
+
return true;
|
|
136
|
+
// exists but stopped?
|
|
137
|
+
const all = await docker(["ps", "-a", "--filter", `name=^/${CONTAINER}$`, "--format", "{{.Names}}"]);
|
|
138
|
+
if (all.out === CONTAINER) {
|
|
139
|
+
return (await docker(["start", CONTAINER])).code === 0;
|
|
140
|
+
}
|
|
141
|
+
// fresh run
|
|
142
|
+
const dir = await ensureSettingsDir();
|
|
143
|
+
const run = await docker([
|
|
144
|
+
"run", "-d", "--name", CONTAINER, "--restart", "unless-stopped",
|
|
145
|
+
"-p", `${p}:8080`, "-v", `${dir}:/etc/searxng`, image(),
|
|
146
|
+
], 60_000);
|
|
147
|
+
return run.code === 0;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Return a reachable SearXNG base URL, standing up a managed container if needed. null means no backend
|
|
151
|
+
* (mobile, Docker absent, auto-manage off, or startup failed) — the caller then reports the usual error.
|
|
152
|
+
*/
|
|
153
|
+
export async function ensureSearxng() {
|
|
154
|
+
const explicit = process.env.KODE_SEARXNG_URL;
|
|
155
|
+
// A reachable explicit/managed URL wins with no Docker work.
|
|
156
|
+
if (explicit && (await reachable(explicit))) {
|
|
157
|
+
// If it's our managed container on the managed port, keep the idle timer honest.
|
|
158
|
+
if (managed)
|
|
159
|
+
resetIdle();
|
|
160
|
+
return explicit;
|
|
161
|
+
}
|
|
162
|
+
if (!autoManage())
|
|
163
|
+
return explicit ?? null;
|
|
164
|
+
if (!(await dockerAvailable()))
|
|
165
|
+
return explicit ?? null;
|
|
166
|
+
if (!(await bringUp()))
|
|
167
|
+
return explicit ?? null;
|
|
168
|
+
const base = `http://localhost:${port()}`;
|
|
169
|
+
// wait for readiness (SearXNG boots in ~2-4s; allow generous headroom on a cold image)
|
|
170
|
+
for (let i = 0; i < 40; i++) {
|
|
171
|
+
if (await reachable(base)) {
|
|
172
|
+
managed = true;
|
|
173
|
+
await armHooks();
|
|
174
|
+
resetIdle();
|
|
175
|
+
return base;
|
|
176
|
+
}
|
|
177
|
+
await new Promise((r) => setTimeout(r, 1000));
|
|
178
|
+
}
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
/** Reset the idle countdown after a successful search. */
|
|
182
|
+
export function noteSearxngUsed() {
|
|
183
|
+
if (managed)
|
|
184
|
+
resetIdle();
|
|
185
|
+
}
|
|
186
|
+
/** Stop the container if we started it. Best-effort; safe to call repeatedly. */
|
|
187
|
+
export async function stopManagedSearxng() {
|
|
188
|
+
if (!managed)
|
|
189
|
+
return;
|
|
190
|
+
managed = false;
|
|
191
|
+
if (idleTimer) {
|
|
192
|
+
clearTimeout(idleTimer);
|
|
193
|
+
idleTimer = null;
|
|
194
|
+
}
|
|
195
|
+
await docker(["stop", "-t", "2", CONTAINER], 10_000).catch(() => undefined);
|
|
196
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@0perator/operator",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.23",
|
|
4
4
|
"description": "Operator (KODE) — TypeScript + Ink terminal coding agent. Full-frame render, single-engine core shared across CLI/web/mobile; runs against a local Aurora model.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|