@oalo-ai/cli 0.4.5 → 0.5.0
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 +20 -0
- package/man/oalo.1 +120 -5
- package/package.json +1 -1
- package/src/args.js +9 -1
- package/src/channel.js +1 -1
- package/src/commands/data.js +9 -1
- package/src/commands/deploy.js +6 -2
- package/src/commands/graphs.js +28 -8
- package/src/commands/machines.js +165 -0
- package/src/commands/serve.js +562 -0
- package/src/commands/switches.js +10 -0
- package/src/commands/tokens.js +14 -6
- package/src/commands/typegen.js +2 -1
- package/src/coord-release.json +9 -9
- package/src/events.js +9 -4
- package/src/graphfiles.js +44 -1
- package/src/help.js +66 -4
- package/src/index.js +8 -0
- package/src/serve/command.js +142 -0
- package/src/serve/config.js +215 -0
- package/src/serve/folder.js +368 -0
- package/src/serve/install.js +107 -0
- package/src/serve/model.js +363 -0
- package/src/serve/node.js +292 -0
|
@@ -0,0 +1,562 @@
|
|
|
1
|
+
import { existsSync, statSync, mkdirSync, writeFileSync, realpathSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { resolve as resolvePath, dirname, join } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { CLI_VERSION } from "../server.js";
|
|
6
|
+
import { dim } from "../events.js";
|
|
7
|
+
import {
|
|
8
|
+
pickMachine,
|
|
9
|
+
readMachine,
|
|
10
|
+
writeMachine,
|
|
11
|
+
handlerNameError,
|
|
12
|
+
parseInputSpec,
|
|
13
|
+
declaration,
|
|
14
|
+
clampTimeout,
|
|
15
|
+
size,
|
|
16
|
+
DEFAULT_TIMEOUT_MS,
|
|
17
|
+
MAX_TIMEOUT_MS,
|
|
18
|
+
FOLDER_SCHEMA,
|
|
19
|
+
MODEL_SERVERS,
|
|
20
|
+
MAX_MODELS as MAX_MODELS_DECLARED,
|
|
21
|
+
} from "../serve/config.js";
|
|
22
|
+
import { ModelRequests, discoverModels, isLocalHost, trimUrl } from "../serve/model.js";
|
|
23
|
+
import { walk, EXTENSIONS } from "../serve/folder.js";
|
|
24
|
+
import { Handlers, capOutput, serveNode, requestLine } from "../serve/node.js";
|
|
25
|
+
import { unitFor } from "../serve/install.js";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* `oalo serve`: answer the workspace's bots from this computer. It connects
|
|
29
|
+
* out to the box (nothing listens here), declares the machine's handlers, and
|
|
30
|
+
* runs each request the box sends. Only the handler's answer goes back; the
|
|
31
|
+
* folder or script it reads stays here.
|
|
32
|
+
*
|
|
33
|
+
* oalo serve [--machine <name>] run, in the foreground
|
|
34
|
+
* oalo serve add folder <name> <path> --describe "..."
|
|
35
|
+
* oalo serve add command <name> --describe "..." [--input 'sku:string,qty:integer?'] [--timeout <s>] -- <program> [args...]
|
|
36
|
+
* oalo serve add model <name> [--base-url <url>] [--server ollama|vllm|openai] [--models a,b] [--key-env VAR]
|
|
37
|
+
* oalo serve remove <name> | list | test <name> '<json>'
|
|
38
|
+
* oalo serve install [--dry-run] a launchd or systemd unit; prints how to load it
|
|
39
|
+
*
|
|
40
|
+
* Handlers live in the machine's config, ~/.oalo/serve/<machine>.json, never
|
|
41
|
+
* in a project. The command line never prints the machine's key.
|
|
42
|
+
*/
|
|
43
|
+
export async function serve(flags, rest, opts = {}) {
|
|
44
|
+
const sub = rest[1];
|
|
45
|
+
if (sub && !["add", "remove", "list", "test", "install"].includes(sub)) return fail(`unknown serve command: ${sub}\n\n${USAGE}`, 2);
|
|
46
|
+
|
|
47
|
+
const picked = pickMachine(flags.machine);
|
|
48
|
+
if (picked.error) return fail(picked.error, picked.code);
|
|
49
|
+
let cfg;
|
|
50
|
+
try {
|
|
51
|
+
cfg = readMachine(picked.name);
|
|
52
|
+
} catch (e) {
|
|
53
|
+
return fail(e.message, 1);
|
|
54
|
+
}
|
|
55
|
+
if (!cfg) return fail(`this computer has no machine called ${picked.name}. Set it up with: oalo machines add ${picked.name}`, 2);
|
|
56
|
+
|
|
57
|
+
switch (sub) {
|
|
58
|
+
case "add":
|
|
59
|
+
return rest[2] === "model" ? await addModel(cfg, flags, rest, opts) : add(cfg, flags, rest);
|
|
60
|
+
case "remove":
|
|
61
|
+
return remove(cfg, rest[2]);
|
|
62
|
+
case "list":
|
|
63
|
+
return list(cfg, flags);
|
|
64
|
+
case "test":
|
|
65
|
+
return await test(cfg, rest[2], rest[3], flags, opts);
|
|
66
|
+
case "install":
|
|
67
|
+
return install(cfg, flags, opts);
|
|
68
|
+
default:
|
|
69
|
+
return await run(cfg, opts);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const USAGE = [
|
|
74
|
+
"usage: oalo serve [--machine <name>]",
|
|
75
|
+
" oalo serve add folder <name> <path> --describe \"<what's in it>\"",
|
|
76
|
+
" oalo serve add command <name> --describe \"<what it answers>\" [--input 'sku:string,qty:integer?'] [--timeout <seconds>] -- <program> [args...]",
|
|
77
|
+
" oalo serve add model <name> [--base-url http://127.0.0.1:11434/v1] [--server ollama|vllm|openai] [--models id1,id2] [--key-env VAR] [--context N] [--output N]",
|
|
78
|
+
" oalo serve remove <name>",
|
|
79
|
+
" oalo serve list [--json]",
|
|
80
|
+
" oalo serve test <name> '<json input>'",
|
|
81
|
+
" oalo serve install [--dry-run]",
|
|
82
|
+
" (each takes --machine <name> when this computer has more than one machine)",
|
|
83
|
+
].join("\n");
|
|
84
|
+
|
|
85
|
+
const home = (p) => (p === "~" ? homedir() : p.startsWith("~/") ? join(homedir(), p.slice(2)) : p);
|
|
86
|
+
|
|
87
|
+
function describeOf(flags) {
|
|
88
|
+
if (flags.describe === undefined || flags.describe === true || !String(flags.describe).trim())
|
|
89
|
+
return { error: "--describe \"...\" is needed: it's what the bot reads when deciding to use this. Say what's in it, like \"Store policies, shipping and returns\"." };
|
|
90
|
+
const d = String(flags.describe).trim();
|
|
91
|
+
if (d.length > 1000) return { error: `--describe is ${d.length} characters; keep it under 1,000.` };
|
|
92
|
+
return { description: d };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function add(cfg, flags, rest) {
|
|
96
|
+
const kind = rest[2];
|
|
97
|
+
const name = rest[3];
|
|
98
|
+
if (kind !== "folder" && kind !== "command") return fail(`oalo serve add takes folder, command or model.\n\n${USAGE}`, 2);
|
|
99
|
+
const bad = handlerNameError(name);
|
|
100
|
+
if (bad) return fail(bad, 2);
|
|
101
|
+
if (cfg.handlers.some((h) => h.name === name)) return fail(`${cfg.name} already has a handler called ${name}. Remove it first: oalo serve remove ${name}`, 1);
|
|
102
|
+
const d = describeOf(flags);
|
|
103
|
+
if (d.error) return fail(d.error, 2);
|
|
104
|
+
|
|
105
|
+
let handler;
|
|
106
|
+
if (kind === "folder") {
|
|
107
|
+
if (flags["--"]?.length) return fail("a folder handler takes no command after --.", 2);
|
|
108
|
+
if (!rest[4]) return fail(`which folder? oalo serve add folder ${name} <path> --describe "..."`, 2);
|
|
109
|
+
if (rest.length > 5) return fail(`one folder per handler (got ${rest.slice(4).join(" ")}). Put the path in quotes if it has spaces.`, 2);
|
|
110
|
+
const path = resolvePath(home(rest[4]));
|
|
111
|
+
if (!existsSync(path) || !statSync(path).isDirectory()) return fail(`${path} isn't a folder on this computer.`, 1);
|
|
112
|
+
let count = 0;
|
|
113
|
+
try {
|
|
114
|
+
count = walk(path).files.length;
|
|
115
|
+
} catch (e) {
|
|
116
|
+
return fail(`can't read ${path}: ${e.code || e.message}`, 1);
|
|
117
|
+
}
|
|
118
|
+
handler = { name, kind, description: d.description, timeout_ms: DEFAULT_TIMEOUT_MS, input_schema: FOLDER_SCHEMA, folder: { path } };
|
|
119
|
+
cfg.handlers.push(handler);
|
|
120
|
+
const file = writeMachine(cfg);
|
|
121
|
+
console.log(`added ${name} to ${cfg.name}: searches ${path} (${count} file${count === 1 ? "" : "s"} it can read).`);
|
|
122
|
+
if (!count) console.log(dim(`It reads ${[...EXTENSIONS].join(" ")} files and skips hidden ones, node_modules and files over 2 MB. None found yet.`));
|
|
123
|
+
console.log(dim(`Saved in ${file}.`));
|
|
124
|
+
console.log(`Try it: oalo serve test ${name} '{"query": "return policy"}'`);
|
|
125
|
+
return restartHint();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const argv = flags["--"];
|
|
129
|
+
if (!argv?.length) {
|
|
130
|
+
const stray = rest.slice(4);
|
|
131
|
+
return fail(
|
|
132
|
+
stray.length
|
|
133
|
+
? `put the program after --, so its own flags aren't read as oalo's: oalo serve add command ${name} --describe "..." -- ${stray.join(" ")}`
|
|
134
|
+
: `which program? Put it after --: oalo serve add command ${name} --describe "..." -- python3 ~/oalo/${name}.py`,
|
|
135
|
+
2,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
if (rest.length > 4) return fail(`unexpected ${rest.slice(4).join(" ")}. The program and its arguments go after --.`, 2);
|
|
139
|
+
if (flags.input === true) return fail("--input needs fields, like --input 'sku:string,qty:integer?'", 2);
|
|
140
|
+
const spec = parseInputSpec(flags.input);
|
|
141
|
+
if (spec.error) return fail(spec.error, 2);
|
|
142
|
+
let timeout = DEFAULT_TIMEOUT_MS;
|
|
143
|
+
if (flags.timeout !== undefined) {
|
|
144
|
+
const s = Number(flags.timeout);
|
|
145
|
+
if (!Number.isInteger(s) || s < 1 || s * 1000 > MAX_TIMEOUT_MS) return fail(`--timeout is in seconds, 1 to ${MAX_TIMEOUT_MS / 1000}.`, 2);
|
|
146
|
+
timeout = s * 1000;
|
|
147
|
+
}
|
|
148
|
+
// The folder it was added from, so a relative path in the command still
|
|
149
|
+
// works when the service runs it from somewhere else.
|
|
150
|
+
handler = { name, kind, description: d.description, timeout_ms: timeout, input_schema: spec.schema, command: { argv: argv.map(String), cwd: process.cwd() } };
|
|
151
|
+
cfg.handlers.push(handler);
|
|
152
|
+
const file = writeMachine(cfg);
|
|
153
|
+
console.log(`added ${name} to ${cfg.name}: runs ${argv.join(" ")} (no shell), up to ${timeout / 1000}s.`);
|
|
154
|
+
console.log(dim(`It gets {"input": ..., "context": ...} on stdin and prints one JSON object. Saved in ${file}.`));
|
|
155
|
+
const example = Object.fromEntries(Object.entries(spec.schema.properties).map(([k, p]) => [k, p.type === "string" ? "..." : p.type === "boolean" ? true : 1]));
|
|
156
|
+
console.log(`Try it: oalo serve test ${name} '${JSON.stringify(example)}'`);
|
|
157
|
+
return restartHint();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const DEFAULT_MODEL_URL = "http://127.0.0.1:11434/v1";
|
|
161
|
+
const SERVER_NAMES = { ollama: "Ollama", vllm: "vLLM", openai: "an OpenAI-compatible server" };
|
|
162
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]{0,127}$/;
|
|
163
|
+
const num = (n) => Number(n).toLocaleString("en-US");
|
|
164
|
+
|
|
165
|
+
function positiveInt(flags, name) {
|
|
166
|
+
if (flags[name] === undefined) return { value: null };
|
|
167
|
+
const n = Number(flags[name]);
|
|
168
|
+
if (!Number.isInteger(n) || n < 1 || n > 10_000_000) return { error: `--${name} is a number of tokens, like --${name} ${name === "context" ? 32768 : 4096}` };
|
|
169
|
+
return { value: n };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* `oalo serve add model <name>`: read the model server's models and declare
|
|
174
|
+
* them. Running it again with the same name updates the handler, keeping its
|
|
175
|
+
* address, server kind, key env and description unless new ones are given.
|
|
176
|
+
*/
|
|
177
|
+
async function addModel(cfg, flags, rest, opts) {
|
|
178
|
+
const name = rest[3];
|
|
179
|
+
const bad = handlerNameError(name);
|
|
180
|
+
if (bad) return fail(bad, 2);
|
|
181
|
+
if (rest.length > 4) return fail(`unexpected ${rest.slice(4).join(" ")}. Give the model server's address with --base-url <url>.`, 2);
|
|
182
|
+
const existing = cfg.handlers.find((h) => h.name === name);
|
|
183
|
+
if (existing && existing.kind !== "model") return fail(`${cfg.name} already has a ${existing.kind} handler called ${name}. Pick another name, or remove it first: oalo serve remove ${name}`, 1);
|
|
184
|
+
for (const f of ["base-url", "server", "models", "key-env", "context", "output", "describe"]) {
|
|
185
|
+
if (flags[f] === true) return fail(`--${f} needs a value`, 2);
|
|
186
|
+
}
|
|
187
|
+
const env = opts.env || process.env;
|
|
188
|
+
|
|
189
|
+
const raw = String(flags["base-url"] ?? existing?.base_url ?? DEFAULT_MODEL_URL);
|
|
190
|
+
let url;
|
|
191
|
+
try {
|
|
192
|
+
url = new URL(raw);
|
|
193
|
+
} catch {
|
|
194
|
+
return fail(`--base-url ${raw} isn't a URL. Like: --base-url http://127.0.0.1:11434/v1`, 2);
|
|
195
|
+
}
|
|
196
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") return fail(`--base-url must be http or https, not ${url.protocol.replace(/:$/, "")}`, 2);
|
|
197
|
+
if (url.username || url.password) return fail("--base-url can't hold a user or password. Put the key in an env var and name it with --key-env.", 2);
|
|
198
|
+
if (url.search || url.hash) return fail("--base-url can't have a ? query or # part. It's the API root, like http://127.0.0.1:11434/v1", 2);
|
|
199
|
+
const baseUrl = trimUrl(url.href);
|
|
200
|
+
|
|
201
|
+
// --server is also the workspace URL flag elsewhere, so a URL here is a mix-up.
|
|
202
|
+
let server = flags.server === undefined ? null : String(flags.server);
|
|
203
|
+
if (server !== null && !MODEL_SERVERS.includes(server))
|
|
204
|
+
return fail(`--server is the kind of model server: ${MODEL_SERVERS.join(", ")}.${/[:/.]/.test(server) ? " The address goes in --base-url." : ""}`, 2);
|
|
205
|
+
if (!server && existing && trimUrl(existing.base_url) === baseUrl) server = existing.server;
|
|
206
|
+
if (!server && url.port === "11434") server = "ollama";
|
|
207
|
+
|
|
208
|
+
const keyEnv = flags["key-env"] === undefined ? existing?.key_env ?? null : String(flags["key-env"]) || null;
|
|
209
|
+
if (keyEnv && !ENV_NAME.test(keyEnv)) return fail(`--key-env takes the NAME of an environment variable (like OPENAI_API_KEY), not the key itself.`, 2);
|
|
210
|
+
const key = keyEnv ? env[keyEnv] : null;
|
|
211
|
+
|
|
212
|
+
let only = null;
|
|
213
|
+
if (flags.models !== undefined) {
|
|
214
|
+
only = [...new Set(String(flags.models).split(",").map((s) => s.trim()).filter(Boolean))];
|
|
215
|
+
if (!only.length) return fail("--models takes the model ids, comma-separated: --models qwen3:14b,llama3.2:3b", 2);
|
|
216
|
+
if (only.length > MAX_MODELS_DECLARED) return fail(`a handler declares up to ${MAX_MODELS_DECLARED} models; --models names ${only.length}.`, 2);
|
|
217
|
+
}
|
|
218
|
+
const context = positiveInt(flags, "context");
|
|
219
|
+
if (context.error) return fail(context.error, 2);
|
|
220
|
+
const output = positiveInt(flags, "output");
|
|
221
|
+
if (output.error) return fail(output.error, 2);
|
|
222
|
+
if (context.value && output.value && output.value > context.value) return fail("--output can't be more than --context.", 2);
|
|
223
|
+
|
|
224
|
+
let description = existing?.description;
|
|
225
|
+
if (flags.describe !== undefined) {
|
|
226
|
+
const d = describeOf(flags);
|
|
227
|
+
if (d.error) return fail(d.error, 2);
|
|
228
|
+
description = d.description;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (!isLocalHost(url.hostname))
|
|
232
|
+
console.error(`warning: ${url.hostname} isn't this computer or a private network address. This machine will send its bots' conversations there.`);
|
|
233
|
+
if (keyEnv && !key) console.error(`warning: ${keyEnv} isn't set in this shell. oalo serve sends it as the model server's key, so set it where oalo serve runs.`);
|
|
234
|
+
|
|
235
|
+
console.error(dim(`reading the models at ${baseUrl}/models ...`));
|
|
236
|
+
let found = await discoverModels({ baseUrl, server, only, key, fetchImpl: opts.fetchImpl });
|
|
237
|
+
if (found.error) {
|
|
238
|
+
if (!(found.unreachable && only && context.value)) {
|
|
239
|
+
const hint = found.unreachable ? ` Start the model server and run this again (or give --models and --context to add it while it's off).` : "";
|
|
240
|
+
return fail(found.error + hint, 1);
|
|
241
|
+
}
|
|
242
|
+
console.error(`warning: ${found.error}. Adding ${only.join(", ")} as given.`);
|
|
243
|
+
found = { models: only.map((id) => ({ id, context: null, vision: false })), server: server || "openai", notes: [], skipped: [] };
|
|
244
|
+
}
|
|
245
|
+
server = found.server || server || "openai";
|
|
246
|
+
|
|
247
|
+
const models = [];
|
|
248
|
+
const unknown = [];
|
|
249
|
+
for (const m of found.models) {
|
|
250
|
+
const ctx = context.value ?? m.context;
|
|
251
|
+
if (!ctx) {
|
|
252
|
+
unknown.push(m.id);
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
const out = output.value ?? Math.min(8192, Math.floor(ctx / 4));
|
|
256
|
+
if (out > ctx) return fail(`--output ${out} is more than ${m.id}'s context of ${ctx}.`, 2);
|
|
257
|
+
models.push({ id: m.id, context: ctx, output: out, vision: m.vision === true, ollamaMax: m.ollamaMax && !context.value });
|
|
258
|
+
}
|
|
259
|
+
if (unknown.length) return fail(`${baseUrl} doesn't say how long a context ${unknown.join(", ")} take${unknown.length === 1 ? "s" : ""}. Give it with --context <tokens>.`, 1);
|
|
260
|
+
if (!models.length) return fail(`${baseUrl} has no chat models to declare${found.skipped.length ? ` (skipped ${found.skipped.join(", ")}: not chat models)` : ""}.`, 1);
|
|
261
|
+
|
|
262
|
+
const handler = {
|
|
263
|
+
name,
|
|
264
|
+
kind: "model",
|
|
265
|
+
description: description || `${server === "ollama" ? "Ollama" : server === "vllm" ? "vLLM" : "Model"} models on ${cfg.name}`,
|
|
266
|
+
base_url: baseUrl,
|
|
267
|
+
server,
|
|
268
|
+
...(keyEnv ? { key_env: keyEnv } : {}),
|
|
269
|
+
models: models.map(({ id, context: c, output: o, vision }) => ({ id, context: c, output: o, vision })),
|
|
270
|
+
};
|
|
271
|
+
if (existing) cfg.handlers[cfg.handlers.indexOf(existing)] = handler;
|
|
272
|
+
else cfg.handlers.push(handler);
|
|
273
|
+
const file = writeMachine(cfg);
|
|
274
|
+
|
|
275
|
+
console.log(`${existing ? "updated" : "added"} ${name} on ${cfg.name}: ${SERVER_NAMES[server]} at ${baseUrl}, ${models.length} model${models.length === 1 ? "" : "s"}.`);
|
|
276
|
+
const w = Math.max(...models.map((m) => m.id.length));
|
|
277
|
+
for (const m of models) console.log(` ${m.id.padEnd(w)} context ${num(m.context)}, output ${num(m.output)}${m.vision ? ", vision" : ""}`);
|
|
278
|
+
if (found.skipped.length) console.log(dim(`skipped ${found.skipped.join(", ")}: not ${found.skipped.length === 1 ? "a chat model" : "chat models"}.`));
|
|
279
|
+
for (const n of found.notes) console.error(`warning: ${n}`);
|
|
280
|
+
const atMax = models.filter((m) => m.ollamaMax).map((m) => m.id);
|
|
281
|
+
if (atMax.length) {
|
|
282
|
+
console.log(
|
|
283
|
+
`note: for ${atMax.join(", ")} that context is the model's maximum. Ollama serves a smaller window unless OLLAMA_CONTEXT_LENGTH is set for the Ollama server or the model's num_ctx is raised, and quietly drops what doesn't fit. The box's model check finds the real window; or declare it now with --context <tokens>.`,
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
if (keyEnv) console.log(dim(`sends the key in $${keyEnv} to the model server (the key itself isn't saved).`));
|
|
287
|
+
console.log(dim(`Saved in ${file}. Once oalo serve is running, the workspace checks each new model (a few minutes each); \`oalo machines\` shows the results and the id to set a bot to.`));
|
|
288
|
+
console.log(`Try it: oalo serve test ${name} '{"model": "${models[0].id}", "message": "Say hi in five words"}'`);
|
|
289
|
+
return restartHint();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function restartHint() {
|
|
293
|
+
console.log(dim("If oalo serve is already running, restart it so the box sees the change."));
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function remove(cfg, name) {
|
|
297
|
+
if (!name) return fail("usage: oalo serve remove <name> (names from `oalo serve list`)", 2);
|
|
298
|
+
const before = cfg.handlers.length;
|
|
299
|
+
cfg.handlers = cfg.handlers.filter((h) => h.name !== name);
|
|
300
|
+
if (cfg.handlers.length === before) return fail(`${cfg.name} has no handler called ${name}. See them with: oalo serve list`, 1);
|
|
301
|
+
writeMachine(cfg);
|
|
302
|
+
console.log(`removed ${name} from ${cfg.name}.`);
|
|
303
|
+
restartHint();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function list(cfg, flags) {
|
|
307
|
+
if (flags.json) {
|
|
308
|
+
return void console.log(JSON.stringify({ machine: cfg.name, url: cfg.url, has_key: !!cfg.token, handlers: cfg.handlers }, null, 2));
|
|
309
|
+
}
|
|
310
|
+
console.log(`${cfg.name} ${dim(`${cfg.url}${cfg.token ? "" : ", no key (revoked): run oalo machines add " + cfg.name}`)}`);
|
|
311
|
+
if (!cfg.handlers.length) {
|
|
312
|
+
console.log(dim("no handlers yet. Add one: oalo serve add folder <name> <path> --describe \"...\""));
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const w = Math.max(...cfg.handlers.map((h) => h.name.length));
|
|
316
|
+
for (const h of cfg.handlers) {
|
|
317
|
+
console.log(` ${h.name.padEnd(w)} ${h.kind.padEnd(7)} ${h.description || ""}`);
|
|
318
|
+
const what =
|
|
319
|
+
h.kind === "model"
|
|
320
|
+
? `forwards to ${h.base_url} (${h.server})${h.key_env ? `, key from $${h.key_env}` : ""}: ${(h.models || []).map((m) => `${m.id} (${num(m.context)}/${num(m.output)}${m.vision ? ", vision" : ""})`).join(", ")}`
|
|
321
|
+
: h.kind === "folder"
|
|
322
|
+
? `searches ${h.folder?.path}`
|
|
323
|
+
: `runs ${(h.command?.argv || []).join(" ")}${fields(h.input_schema)}, up to ${clampTimeout(h.timeout_ms) / 1000}s`;
|
|
324
|
+
console.log(` ${" ".repeat(w)} ${dim(what)}`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function fields(schema) {
|
|
329
|
+
const props = Object.entries(schema?.properties || {});
|
|
330
|
+
if (!props.length) return "";
|
|
331
|
+
const req = new Set(schema.required || []);
|
|
332
|
+
return `, input ${props.map(([k, p]) => `${k}:${p.type}${req.has(k) ? "" : "?"}`).join(",")}`;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function test(cfg, name, json, flags, opts) {
|
|
336
|
+
if (!name) return fail("usage: oalo serve test <name> '<json input>'", 2);
|
|
337
|
+
const h = cfg.handlers.find((x) => x.name === name);
|
|
338
|
+
if (!h) return fail(`${cfg.name} has no handler called ${name}. See them with: oalo serve list`, 1);
|
|
339
|
+
if (h.kind === "model") return await testModel(h, json, opts);
|
|
340
|
+
let input = {};
|
|
341
|
+
if (json !== undefined) {
|
|
342
|
+
try {
|
|
343
|
+
input = JSON.parse(json);
|
|
344
|
+
} catch {
|
|
345
|
+
return fail(`that input isn't JSON. Put it in single quotes: oalo serve test ${name} '{"query": "return policy"}'`, 2);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const handlers = new Handlers([h], { env: opts.env || process.env });
|
|
349
|
+
const started = Date.now();
|
|
350
|
+
const context = { surface: "test", bot: null, conversation: null, member: null };
|
|
351
|
+
const r = capOutput(await handlers.run(name, input, context));
|
|
352
|
+
const ms = Date.now() - started;
|
|
353
|
+
if (!r.ok) {
|
|
354
|
+
console.error(`error: ${r.error}`);
|
|
355
|
+
console.error(dim(`${ms} ms. This is what the bot would see.`));
|
|
356
|
+
process.exitCode = 1;
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
console.log(JSON.stringify(r.output, null, 2));
|
|
360
|
+
console.error(dim(`ok in ${ms} ms, ${size(Buffer.byteLength(JSON.stringify(r.output)))} (max 64 KB)`));
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* One streaming chat call through the same code path a box request takes
|
|
365
|
+
* (thinking off), printing the reply as it comes and the timings.
|
|
366
|
+
*/
|
|
367
|
+
async function testModel(h, json, opts) {
|
|
368
|
+
let input = {};
|
|
369
|
+
if (json !== undefined) {
|
|
370
|
+
try {
|
|
371
|
+
input = JSON.parse(json);
|
|
372
|
+
} catch {
|
|
373
|
+
return fail(`that input isn't JSON. Put it in single quotes: oalo serve test ${h.name} '{"message": "Say hi in five words"}'`, 2);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
if (!input || typeof input !== "object" || Array.isArray(input)) return fail(`the input is a JSON object: '{"model": "...", "message": "..."}'`, 2);
|
|
377
|
+
const ids = (h.models || []).map((m) => m.id);
|
|
378
|
+
const model = input.model ?? ids[0];
|
|
379
|
+
const m = (h.models || []).find((x) => x.id === model);
|
|
380
|
+
if (!m) return fail(ids.length ? `${h.name} doesn't serve ${model}. It has: ${ids.join(", ")}` : `${h.name} has no models. Run oalo serve add model ${h.name} again.`, 1);
|
|
381
|
+
const message = input.message ?? "Say hi in five words";
|
|
382
|
+
if (typeof message !== "string" || !message.trim()) return fail("message is the text to send, a string.", 2);
|
|
383
|
+
|
|
384
|
+
const write = opts.write || ((s) => process.stdout.write(s));
|
|
385
|
+
let text = "";
|
|
386
|
+
let end = null;
|
|
387
|
+
let entry = null;
|
|
388
|
+
const requests = new ModelRequests({
|
|
389
|
+
handlers: [h],
|
|
390
|
+
env: opts.env || process.env,
|
|
391
|
+
fetchImpl: opts.fetchImpl,
|
|
392
|
+
push: (event, p) => {
|
|
393
|
+
if (event === "model_end") end = p;
|
|
394
|
+
if (event !== "model_chunk") return;
|
|
395
|
+
try {
|
|
396
|
+
const d = JSON.parse(p.data)?.choices?.[0]?.delta;
|
|
397
|
+
if (typeof d?.content === "string" && d.content) {
|
|
398
|
+
write(d.content);
|
|
399
|
+
text += d.content;
|
|
400
|
+
}
|
|
401
|
+
} catch {
|
|
402
|
+
/* not a chunk with text */
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
log: (e) => (entry = e),
|
|
406
|
+
});
|
|
407
|
+
await requests.handle({
|
|
408
|
+
request_id: "mr_test",
|
|
409
|
+
handler: h.name,
|
|
410
|
+
model,
|
|
411
|
+
stream: true,
|
|
412
|
+
thinking: "off",
|
|
413
|
+
body: { model, messages: [{ role: "user", content: message }], max_tokens: m.output || 1024 },
|
|
414
|
+
});
|
|
415
|
+
if (text) write("\n");
|
|
416
|
+
if (end?.error) {
|
|
417
|
+
console.error(`error: ${end.error}`);
|
|
418
|
+
console.error(dim(`${entry?.ms ?? "?"} ms. This is what the box would see.`));
|
|
419
|
+
process.exitCode = 1;
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (!text) console.error("warning: the model sent no text. A bot on it would send an empty reply.");
|
|
423
|
+
const parts = [`ok in ${entry.ms} ms`];
|
|
424
|
+
if (entry.firstMs !== null && entry.firstMs !== undefined) parts.push(`first token ${entry.firstMs} ms`);
|
|
425
|
+
const u = end?.usage;
|
|
426
|
+
if (u && Number.isFinite(u.prompt_tokens)) {
|
|
427
|
+
parts.push(`tokens in ${u.prompt_tokens}, out ${u.completion_tokens}`);
|
|
428
|
+
const genMs = entry.ms - (entry.firstMs ?? 0);
|
|
429
|
+
if (u.completion_tokens > 1 && genMs > 0) parts.push(`${(u.completion_tokens / (genMs / 1000)).toFixed(1)} tokens/s`);
|
|
430
|
+
} else parts.push("the server sent no token counts");
|
|
431
|
+
console.error(dim(`${parts.join(", ")} (${model}, thinking off)`));
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** node and this CLI's entry file, by absolute path, for a service unit. */
|
|
435
|
+
function program() {
|
|
436
|
+
const entry = fileURLToPath(new URL("../index.js", import.meta.url));
|
|
437
|
+
let real = entry;
|
|
438
|
+
try {
|
|
439
|
+
real = realpathSync(entry);
|
|
440
|
+
} catch {
|
|
441
|
+
/* keep it as is */
|
|
442
|
+
}
|
|
443
|
+
return [process.execPath, real];
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function install(cfg, flags, opts) {
|
|
447
|
+
const unit = unitFor(cfg.name, { platform: opts.platform, home: opts.home, program: opts.program || program(), env: opts.env || process.env });
|
|
448
|
+
if (unit.error) return fail(unit.error, 1);
|
|
449
|
+
if (!cfg.token) return fail(`${cfg.name} has no key (it was revoked). Set it up again first: oalo machines add ${cfg.name}`, 1);
|
|
450
|
+
if (!cfg.handlers.length) console.error(dim(`warning: ${cfg.name} has no handlers yet, so the service will have nothing to offer. Add one with oalo serve add.`));
|
|
451
|
+
const existed = existsSync(unit.path);
|
|
452
|
+
const command = existed ? unit.reload : unit.load;
|
|
453
|
+
|
|
454
|
+
if (flags["dry-run"]) {
|
|
455
|
+
console.log(dim(`would write ${unit.path}:`));
|
|
456
|
+
console.log(unit.content);
|
|
457
|
+
console.log(dim("then you'd run:"));
|
|
458
|
+
console.log(` ${command}`);
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
mkdirSync(dirname(unit.path), { recursive: true });
|
|
463
|
+
writeFileSync(unit.path, unit.content, { mode: 0o644 });
|
|
464
|
+
console.log(`wrote ${unit.path}`);
|
|
465
|
+
console.log(`It runs oalo serve --machine ${cfg.name} at login and restarts it if it stops. To start it now, run:`);
|
|
466
|
+
console.log("");
|
|
467
|
+
console.log(` ${command}`);
|
|
468
|
+
console.log("");
|
|
469
|
+
if (unit.log) console.log(dim(`Its output goes to ${unit.log}.`));
|
|
470
|
+
if (unit.logs) console.log(dim(`See its output with: ${unit.logs}`));
|
|
471
|
+
if (unit.note) console.log(dim(unit.note));
|
|
472
|
+
console.log(dim("A laptop that sleeps stops answering until it wakes. If you change Node versions, run oalo serve install again."));
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
async function run(cfg, opts) {
|
|
476
|
+
if (!cfg.token) return fail(`${cfg.name} has no key (it was revoked). Set it up again with: oalo machines add ${cfg.name}`, 1);
|
|
477
|
+
if (!cfg.url || !cfg.node_id) return fail(`${cfg.name}'s config is missing its workspace. Set it up again with: oalo machines add ${cfg.name}`, 1);
|
|
478
|
+
if (!cfg.handlers.length) return fail(`${cfg.name} has no handlers yet, so there's nothing to serve. Add one first:\n oalo serve add folder <name> <path> --describe "..."\n oalo serve add command <name> --describe "..." -- <program> [args...]\n oalo serve add model <name> [--base-url http://127.0.0.1:11434/v1]`, 2);
|
|
479
|
+
const runEnv = opts.env || process.env;
|
|
480
|
+
for (const h of cfg.handlers) {
|
|
481
|
+
if (h.kind === "model" && h.key_env && !runEnv[h.key_env])
|
|
482
|
+
console.error(`warning: ${h.name} sends the key in $${h.key_env}, which isn't set here. Its model calls will fail until it is.`);
|
|
483
|
+
if (h.kind === "folder" && !(existsSync(h.folder?.path || "") && statSync(h.folder.path).isDirectory()))
|
|
484
|
+
console.error(`warning: ${h.name}'s folder ${h.folder?.path} isn't there. Its requests will fail until it is.`);
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const out = opts.out || ((s) => console.log(s));
|
|
488
|
+
const err = opts.err || ((s) => console.error(s));
|
|
489
|
+
let connected = false;
|
|
490
|
+
let warnedRetry = false;
|
|
491
|
+
|
|
492
|
+
return await new Promise((resolve) => {
|
|
493
|
+
let node;
|
|
494
|
+
const finish = (code) => {
|
|
495
|
+
process.off("SIGINT", onSignal);
|
|
496
|
+
process.off("SIGTERM", onSignal);
|
|
497
|
+
node?.stop();
|
|
498
|
+
process.exitCode = code;
|
|
499
|
+
resolve();
|
|
500
|
+
};
|
|
501
|
+
const onSignal = () => {
|
|
502
|
+
out(dim("stopped."));
|
|
503
|
+
finish(0);
|
|
504
|
+
};
|
|
505
|
+
process.on("SIGINT", onSignal);
|
|
506
|
+
process.on("SIGTERM", onSignal);
|
|
507
|
+
|
|
508
|
+
node = (opts.serveNode || serveNode)(cfg, {
|
|
509
|
+
version: CLI_VERSION,
|
|
510
|
+
on: {
|
|
511
|
+
joined(reply) {
|
|
512
|
+
warnedRetry = false;
|
|
513
|
+
const all = cfg.handlers.map(declaration);
|
|
514
|
+
const declared = all.filter((d) => d.kind !== "model");
|
|
515
|
+
const modelDecls = all.filter((d) => d.kind === "model");
|
|
516
|
+
const offered = Array.isArray(reply.tools) ? new Set(reply.tools) : new Set(declared.map((d) => d.name));
|
|
517
|
+
const name = reply.node?.name || cfg.name;
|
|
518
|
+
if (!connected) {
|
|
519
|
+
connected = true;
|
|
520
|
+
out(`connected to ${cfg.url} as ${name}`);
|
|
521
|
+
const shown = declared.filter((d) => offered.has(d.name));
|
|
522
|
+
if (shown.length) {
|
|
523
|
+
out("tools your bots can use:");
|
|
524
|
+
const w = Math.max(...shown.map((d) => d.name.length));
|
|
525
|
+
for (const d of shown) out(` ${d.name.padEnd(w)} ${d.kind.padEnd(7)} ${dim(d.description)}`);
|
|
526
|
+
out(dim(`give one to a bot: oalo bots tools <bot> --add ${shown[0].name}`));
|
|
527
|
+
}
|
|
528
|
+
// Model handlers serve a bot's model, not a tool.
|
|
529
|
+
if (modelDecls.length) out(`models: ${modelDecls.map((d) => `${d.name} (${d.models.map((m) => m.id).join(", ")})`).join("; ")}`);
|
|
530
|
+
} else {
|
|
531
|
+
out(`connected again to ${cfg.url}`);
|
|
532
|
+
}
|
|
533
|
+
for (const s of reply.skipped || []) err(`warning: ${s.name} wasn't offered: ${s.reason || "no reason given"}`);
|
|
534
|
+
out(dim("waiting for requests (Ctrl-C to stop)"));
|
|
535
|
+
},
|
|
536
|
+
dropped() {
|
|
537
|
+
err("connection lost. Reconnecting...");
|
|
538
|
+
},
|
|
539
|
+
retrying() {
|
|
540
|
+
if (warnedRetry) return;
|
|
541
|
+
warnedRetry = true;
|
|
542
|
+
err(`can't reach ${cfg.url} right now. Still trying (check the internet connection).`);
|
|
543
|
+
},
|
|
544
|
+
refused(message) {
|
|
545
|
+
err(message);
|
|
546
|
+
},
|
|
547
|
+
fatal(message) {
|
|
548
|
+
err(message);
|
|
549
|
+
finish(1);
|
|
550
|
+
},
|
|
551
|
+
request(e) {
|
|
552
|
+
out(requestLine(e));
|
|
553
|
+
},
|
|
554
|
+
},
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function fail(message, code) {
|
|
560
|
+
console.error(message);
|
|
561
|
+
process.exitCode = code;
|
|
562
|
+
}
|
package/src/commands/switches.js
CHANGED
|
@@ -24,6 +24,16 @@ export async function loops(flags, rest, opts = {}) {
|
|
|
24
24
|
return fail("usage: oalo loops list, oalo loops test|on|off|delete <name>, or oalo loops runs [<name>] [--id <run>]", 2, flags);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
// `emit` and `run loop` take --payload; `loops test` silently ran with an
|
|
28
|
+
// empty input when given it (2026-11-18). Say what it takes instead.
|
|
29
|
+
if (sub === "test" && flags.payload !== undefined) {
|
|
30
|
+
return fail(
|
|
31
|
+
`loops test takes --input, an event envelope: --input '{"type": "<trigger event>", "payload": {...}}'. --payload is for oalo emit and oalo run loop.`,
|
|
32
|
+
2,
|
|
33
|
+
flags,
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
27
37
|
let input;
|
|
28
38
|
if (sub === "test" && flags.input !== undefined) {
|
|
29
39
|
try {
|
package/src/commands/tokens.js
CHANGED
|
@@ -34,11 +34,7 @@ export async function tokens(flags, rest) {
|
|
|
34
34
|
const list = r.tokens || [];
|
|
35
35
|
if (flags.json) return void console.log(JSON.stringify(list, null, 2));
|
|
36
36
|
if (!list.length) return void console.log(`no sync tokens in ${url}`);
|
|
37
|
-
for (const t of list)
|
|
38
|
-
const when = t.last_used_at ? `last used ${t.last_used_at.slice(0, 10)}` : "never used";
|
|
39
|
-
const exp = t.expires_at ? `, expires ${t.expires_at.slice(0, 10)}` : "";
|
|
40
|
-
console.log(`${t.id} ${t.name} ${(t.scopes || []).join(" ")} ${dim(when + exp)}`);
|
|
41
|
-
}
|
|
37
|
+
for (const t of list) console.log(tokenLine(t));
|
|
42
38
|
return;
|
|
43
39
|
}
|
|
44
40
|
|
|
@@ -63,7 +59,7 @@ export async function tokens(flags, rest) {
|
|
|
63
59
|
console.log(r.token);
|
|
64
60
|
// What the server granted, not what was asked for.
|
|
65
61
|
const granted = (r.scopes || scopes).join(" ");
|
|
66
|
-
const expiry = expires_at ? `expires ${expires_at.slice(0,
|
|
62
|
+
const expiry = expires_at ? `expires ${expires_at.slice(0, 16).replace("T", " ")} UTC (${days} day${days === 1 ? "" : "s"}; --days 0 for no expiry)` : "never expires";
|
|
67
63
|
console.error(dim(`${flags.name}: ${granted}, ${expiry}. ` +
|
|
68
64
|
"Shown once: put it in the app server's secrets now. Never ship it inside a mobile or web app."));
|
|
69
65
|
return;
|
|
@@ -91,6 +87,18 @@ export async function tokens(flags, rest) {
|
|
|
91
87
|
}
|
|
92
88
|
}
|
|
93
89
|
|
|
90
|
+
/**
|
|
91
|
+
* One `oalo tokens list` line. An expired token stays listed until it is
|
|
92
|
+
* revoked, so it says so instead of looking live (demo sweep 2026-11-18).
|
|
93
|
+
*/
|
|
94
|
+
export function tokenLine(t, now = Date.now()) {
|
|
95
|
+
const when = t.last_used_at ? `last used ${t.last_used_at.slice(0, 10)}` : "never used";
|
|
96
|
+
const expired = t.expires_at && Date.parse(t.expires_at) <= now;
|
|
97
|
+
const exp = !t.expires_at ? "" : expired ? `, expired ${t.expires_at.slice(0, 16).replace("T", " ")} UTC` : `, expires ${t.expires_at.slice(0, 16).replace("T", " ")} UTC`;
|
|
98
|
+
const line = `${t.id} ${t.name} ${(t.scopes || []).join(" ")} ${dim(when + exp)}`;
|
|
99
|
+
return expired ? `${line} EXPIRED: calls with it get 401. Revoke it: oalo tokens revoke ${t.id}` : line;
|
|
100
|
+
}
|
|
101
|
+
|
|
94
102
|
/** The workspace's data model names, or null when they can't be read (no data token, an older Base). */
|
|
95
103
|
async function modelNames(url, cfg) {
|
|
96
104
|
const token = (cfg.serverUrl === url && cfg.token) || readToken({ serverUrl: url, appId: cfg.appId }, "sync");
|
package/src/commands/typegen.js
CHANGED
|
@@ -348,7 +348,8 @@ function render(manifest, cfg, workflows = []) {
|
|
|
348
348
|
` product_id: string | null;`,
|
|
349
349
|
` seqnum: number;`,
|
|
350
350
|
` row_hash: string;`,
|
|
351
|
-
`
|
|
351
|
+
` /** ISO 8601, as the REST API sends it. */`,
|
|
352
|
+
` deleted_at: string | null;`,
|
|
352
353
|
` last_modified_ms: number;`,
|
|
353
354
|
`}`,
|
|
354
355
|
``,
|
package/src/coord-release.json
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
2
|
+
"version": "0.5.0",
|
|
3
3
|
"targets": {
|
|
4
4
|
"darwin-arm64": {
|
|
5
5
|
"file": "oalo-coord-darwin-arm64.gz",
|
|
6
|
-
"sha256": "
|
|
7
|
-
"bytes":
|
|
6
|
+
"sha256": "cf4abef37936e89ced06bf613fbcbdf00953c1e5cf5566c4b7668391f8227a91",
|
|
7
|
+
"bytes": 24857692
|
|
8
8
|
},
|
|
9
9
|
"darwin-x64": {
|
|
10
10
|
"file": "oalo-coord-darwin-x64.gz",
|
|
11
|
-
"sha256": "
|
|
12
|
-
"bytes":
|
|
11
|
+
"sha256": "0307cb99b7a2bfdc3b7f8008da665acef727f1f503dafe41dfb4232ca918ec80",
|
|
12
|
+
"bytes": 26996485
|
|
13
13
|
},
|
|
14
14
|
"linux-x64": {
|
|
15
15
|
"file": "oalo-coord-linux-x64.gz",
|
|
16
|
-
"sha256": "
|
|
17
|
-
"bytes":
|
|
16
|
+
"sha256": "9c3547c9c283b5f6bb342f595adc54fb1735d4e236f65ba515075a086ef2eaf8",
|
|
17
|
+
"bytes": 36888776
|
|
18
18
|
},
|
|
19
19
|
"linux-arm64": {
|
|
20
20
|
"file": "oalo-coord-linux-arm64.gz",
|
|
21
|
-
"sha256": "
|
|
22
|
-
"bytes":
|
|
21
|
+
"sha256": "46b339f47d8a292f017173bd2e2f07395d770d9cd09e832ac13004636ec81c06",
|
|
22
|
+
"bytes": 37129629
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
}
|