jen-ai 0.0.0 → 0.1.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/LICENSE +23 -0
- package/README.md +45 -1
- package/cli/api.js +144 -0
- package/cli/browser.js +37 -0
- package/cli/docker.js +172 -0
- package/cli/follow.js +156 -0
- package/cli/jen.js +13 -0
- package/cli/main.js +200 -0
- package/cli/release.js +2 -0
- package/cli/roots.js +61 -0
- package/cli/runtime.js +234 -0
- package/cli/settings.js +128 -0
- package/cli/uninstall.js +104 -0
- package/package.json +13 -4
package/cli/main.js
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// The jen command, run on the user's machine, outside any container. It parses
|
|
2
|
+
// its arguments and dispatches to the module for each command. It's a client
|
|
3
|
+
// of the runtime's API as the owner, and reaches the engine only through the
|
|
4
|
+
// user's docker command. It imports only types from src/runtime/ and
|
|
5
|
+
// src/agent/, so none of their code runs here. Every module it needs is
|
|
6
|
+
// imported here, at startup, so jen uninstall can remove the command's own
|
|
7
|
+
// files while it finishes.
|
|
8
|
+
import { emitKeypressEvents } from "node:readline";
|
|
9
|
+
import { createInterface } from "node:readline/promises";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
import { ApiError, Unanswered } from "./api.js";
|
|
12
|
+
import { createDocker, DockerError, EngineError, runProcess } from "./docker.js";
|
|
13
|
+
import { listRoots, newRoot, openRoot, query, stopRoot } from "./roots.js";
|
|
14
|
+
import { config, stopAll } from "./runtime.js";
|
|
15
|
+
import { MissingImage, namesFrom, Refusal } from "./settings.js";
|
|
16
|
+
import { uninstall } from "./uninstall.js";
|
|
17
|
+
export const USAGE = `jen runs your agents.
|
|
18
|
+
|
|
19
|
+
jen new <name> create a root agent named <name>
|
|
20
|
+
jen @<name> <query> send <query> to that root, show its progress and print its reply
|
|
21
|
+
jen @<name> open open that root's UI in the browser
|
|
22
|
+
jen list list the roots and whether each is running
|
|
23
|
+
jen stop @<name> stop that root and everything under it
|
|
24
|
+
jen stop all stop every agent and the runtime
|
|
25
|
+
jen config set the model, the base URL and the model key
|
|
26
|
+
jen uninstall delete everything of jen's, then the command itself
|
|
27
|
+
jen help show this
|
|
28
|
+
|
|
29
|
+
to remove jen, run jen uninstall: npm uninstall alone leaves every agent and setting in Docker.
|
|
30
|
+
|
|
31
|
+
in zsh, a ? or * in an unquoted query is a pattern, and zsh stops before jen runs
|
|
32
|
+
when no file matches it. To pass them as typed, add this to ~/.zshrc:
|
|
33
|
+
|
|
34
|
+
alias jen='noglob jen'`;
|
|
35
|
+
export function parse(args) {
|
|
36
|
+
const [first, ...rest] = args;
|
|
37
|
+
const only = (command) => (rest.length === 0 ? command : { kind: "usage" });
|
|
38
|
+
if (first === "help")
|
|
39
|
+
return only({ kind: "help" });
|
|
40
|
+
if (first === "list")
|
|
41
|
+
return only({ kind: "list" });
|
|
42
|
+
if (first === "config")
|
|
43
|
+
return only({ kind: "config" });
|
|
44
|
+
if (first === "uninstall")
|
|
45
|
+
return only({ kind: "uninstall" });
|
|
46
|
+
if (first === "new" && rest.length === 1)
|
|
47
|
+
return { kind: "new", name: rest[0] };
|
|
48
|
+
if (first === "stop" && rest.length === 1) {
|
|
49
|
+
if (rest[0] === "all")
|
|
50
|
+
return { kind: "stop-all" };
|
|
51
|
+
if (rest[0].startsWith("@") && rest[0].length > 1)
|
|
52
|
+
return { kind: "stop", name: rest[0].slice(1) };
|
|
53
|
+
}
|
|
54
|
+
if (first?.startsWith("@") && first.length > 1 && rest.length > 0) {
|
|
55
|
+
const name = first.slice(1);
|
|
56
|
+
if (rest.length === 1 && rest[0] === "open")
|
|
57
|
+
return { kind: "open", name };
|
|
58
|
+
return { kind: "query", name, text: rest.join(" ") };
|
|
59
|
+
}
|
|
60
|
+
return { kind: "usage" };
|
|
61
|
+
}
|
|
62
|
+
// Runs the command and answers its exit code.
|
|
63
|
+
export async function run(command, deps) {
|
|
64
|
+
if (command.kind === "help" || command.kind === "usage") {
|
|
65
|
+
(command.kind === "help" ? deps.stdout : deps.stderr)(`${USAGE}\n`);
|
|
66
|
+
return command.kind === "help" ? 0 : 2;
|
|
67
|
+
}
|
|
68
|
+
const ctx = {
|
|
69
|
+
docker: deps.docker,
|
|
70
|
+
names: namesFrom(deps.env),
|
|
71
|
+
terminal: deps.terminal,
|
|
72
|
+
say: (line) => deps.stderr(`${line}\n`),
|
|
73
|
+
print: (line) => deps.stdout(`${line}\n`),
|
|
74
|
+
fetch: deps.fetch,
|
|
75
|
+
sleep: deps.sleep,
|
|
76
|
+
now: deps.now,
|
|
77
|
+
};
|
|
78
|
+
const engine = { say: ctx.say, sleep: deps.sleep, now: deps.now };
|
|
79
|
+
try {
|
|
80
|
+
// Stopping starts no Docker: with the engine down, nothing of jen's runs.
|
|
81
|
+
if (command.kind === "stop" || command.kind === "stop-all") {
|
|
82
|
+
if (!(await deps.docker.ensureEngine({ ...engine, start: false }))) {
|
|
83
|
+
ctx.print("docker isn't running, so nothing of jen's is.");
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else
|
|
88
|
+
await deps.docker.ensureEngine({ ...engine, start: true });
|
|
89
|
+
switch (command.kind) {
|
|
90
|
+
case "new":
|
|
91
|
+
await newRoot(ctx, command.name);
|
|
92
|
+
return 0;
|
|
93
|
+
case "list":
|
|
94
|
+
await listRoots(ctx);
|
|
95
|
+
return 0;
|
|
96
|
+
case "stop":
|
|
97
|
+
await stopRoot(ctx, command.name);
|
|
98
|
+
return 0;
|
|
99
|
+
case "stop-all":
|
|
100
|
+
await stopAll(ctx);
|
|
101
|
+
return 0;
|
|
102
|
+
case "config":
|
|
103
|
+
await config(ctx);
|
|
104
|
+
return 0;
|
|
105
|
+
case "uninstall":
|
|
106
|
+
return await uninstall(ctx, { mainPath: deps.mainPath, npm: deps.npm });
|
|
107
|
+
case "open":
|
|
108
|
+
await openRoot(ctx, command.name, deps.open);
|
|
109
|
+
return 0;
|
|
110
|
+
case "query":
|
|
111
|
+
return await query(ctx, command.name, command.text, { interrupt: deps.interrupt, columns: deps.columns, write: deps.stdout });
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if ([Refusal, MissingImage, EngineError, ApiError, DockerError, Unanswered].some((type) => error instanceof type)) {
|
|
116
|
+
deps.stderr(`jen: ${error.message}\n`);
|
|
117
|
+
return 1;
|
|
118
|
+
}
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Asks on stderr, reading from stdin. A hidden answer is read with echo off.
|
|
123
|
+
// lead() runs before each question is written.
|
|
124
|
+
function terminal(lead) {
|
|
125
|
+
const ask = async (question) => {
|
|
126
|
+
lead();
|
|
127
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr, terminal: true });
|
|
128
|
+
try {
|
|
129
|
+
return await rl.question(question);
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
rl.close();
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const askHidden = (question) => new Promise((resolve) => {
|
|
136
|
+
lead();
|
|
137
|
+
process.stderr.write(question);
|
|
138
|
+
const stdin = process.stdin;
|
|
139
|
+
emitKeypressEvents(stdin);
|
|
140
|
+
stdin.setRawMode(true);
|
|
141
|
+
stdin.resume();
|
|
142
|
+
let answer = "";
|
|
143
|
+
const onKey = (text, key = {}) => {
|
|
144
|
+
if (key.ctrl && key.name === "c") {
|
|
145
|
+
stdin.setRawMode(false);
|
|
146
|
+
process.stderr.write("\n");
|
|
147
|
+
process.exit(130);
|
|
148
|
+
}
|
|
149
|
+
if (key.name === "return" || key.name === "enter") {
|
|
150
|
+
stdin.off("keypress", onKey);
|
|
151
|
+
stdin.setRawMode(false);
|
|
152
|
+
stdin.pause();
|
|
153
|
+
process.stderr.write("\n");
|
|
154
|
+
return resolve(answer);
|
|
155
|
+
}
|
|
156
|
+
if (key.name === "backspace")
|
|
157
|
+
answer = answer.slice(0, -1);
|
|
158
|
+
else if (text && !key.ctrl && text >= " ")
|
|
159
|
+
answer += text;
|
|
160
|
+
};
|
|
161
|
+
stdin.on("keypress", onKey);
|
|
162
|
+
});
|
|
163
|
+
return { interactive: process.stdin.isTTY === true, ask, askHidden };
|
|
164
|
+
}
|
|
165
|
+
// Runs the command in process.argv, as jen.ts does once Node's version passes.
|
|
166
|
+
export async function start() {
|
|
167
|
+
// In a terminal, a blank line sets jen's output apart from the command
|
|
168
|
+
// typed above it, written with the first output so the two arrive
|
|
169
|
+
// together, and another from the next prompt, however jen exits.
|
|
170
|
+
let led = !process.stderr.isTTY;
|
|
171
|
+
const lead = () => {
|
|
172
|
+
if (led)
|
|
173
|
+
return;
|
|
174
|
+
led = true;
|
|
175
|
+
process.stderr.write("\n");
|
|
176
|
+
process.on("exit", () => process.stderr.write("\n"));
|
|
177
|
+
};
|
|
178
|
+
const interrupt = new AbortController();
|
|
179
|
+
const command = parse(process.argv.slice(2));
|
|
180
|
+
// Only a followed query handles Ctrl-C, by leaving the root working.
|
|
181
|
+
if (command.kind === "query")
|
|
182
|
+
process.on("SIGINT", () => interrupt.abort());
|
|
183
|
+
const code = await run(command, {
|
|
184
|
+
docker: createDocker(),
|
|
185
|
+
env: process.env,
|
|
186
|
+
terminal: terminal(lead),
|
|
187
|
+
stdout: (text) => (lead(), process.stdout.write(text)),
|
|
188
|
+
stderr: (text) => (lead(), process.stderr.write(text)),
|
|
189
|
+
fetch: globalThis.fetch,
|
|
190
|
+
sleep: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
191
|
+
now: Date.now,
|
|
192
|
+
columns: process.stderr.columns || 80,
|
|
193
|
+
interrupt: interrupt.signal,
|
|
194
|
+
mainPath: fileURLToPath(import.meta.url),
|
|
195
|
+
npm: runProcess,
|
|
196
|
+
});
|
|
197
|
+
process.exitCode = code;
|
|
198
|
+
// An open stream or a keep-alive connection would hold the process.
|
|
199
|
+
process.stdout.write("", () => process.exit(code));
|
|
200
|
+
}
|
package/cli/release.js
ADDED
package/cli/roots.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// The commands that act on root agents by name: new, list, stop @<name>,
|
|
2
|
+
// @<name> open and @<name> <query>. Each starts the runtime first if it isn't
|
|
3
|
+
// running. A root is found by its name in the owner's list of agents.
|
|
4
|
+
import { openURL } from "./browser.js";
|
|
5
|
+
import { follow } from "./follow.js";
|
|
6
|
+
import { ensureRuntime } from "./runtime.js";
|
|
7
|
+
import { Refusal } from "./settings.js";
|
|
8
|
+
// The root named name, or a refusal suggesting jen new.
|
|
9
|
+
export async function findRoot(api, name) {
|
|
10
|
+
const root = (await api.list()).find((a) => a.parent === null && a.name === name);
|
|
11
|
+
if (!root)
|
|
12
|
+
throw new Refusal(`no root agent is named ${name}. Create it with: jen new ${name}`);
|
|
13
|
+
return root;
|
|
14
|
+
}
|
|
15
|
+
// A new root can delegate, work in its workspace and publish a UI.
|
|
16
|
+
export async function newRoot(ctx, name) {
|
|
17
|
+
const api = await ensureRuntime(ctx);
|
|
18
|
+
await api.spawn(name, { spawn: true, workspace: true });
|
|
19
|
+
ctx.print(`created @${name}. Talk to it with: jen @${name} <query>`);
|
|
20
|
+
}
|
|
21
|
+
// Named roots by name, then roots from before names by ID.
|
|
22
|
+
export async function listRoots(ctx) {
|
|
23
|
+
const api = await ensureRuntime(ctx);
|
|
24
|
+
const roots = (await api.list()).filter((a) => a.parent === null);
|
|
25
|
+
if (roots.length === 0)
|
|
26
|
+
return ctx.print("no root agents yet. Create one with: jen new <name>");
|
|
27
|
+
const named = roots.filter((r) => r.name !== null).sort((a, b) => a.name.localeCompare(b.name));
|
|
28
|
+
const unnamed = roots.filter((r) => r.name === null).sort((a, b) => a.id.localeCompare(b.id));
|
|
29
|
+
const width = Math.max(...roots.map((r) => (r.name === null ? r.id.length : r.name.length + 1)));
|
|
30
|
+
for (const r of named)
|
|
31
|
+
ctx.print(`${`@${r.name}`.padEnd(width)} ${r.state}`);
|
|
32
|
+
for (const r of unnamed)
|
|
33
|
+
ctx.print(`${r.id.padEnd(width)} ${r.state} (no name, so jen can't address it)`);
|
|
34
|
+
}
|
|
35
|
+
// Stops the root and everything under it, keeping their state, and leaves
|
|
36
|
+
// the runtime running.
|
|
37
|
+
export async function stopRoot(ctx, name) {
|
|
38
|
+
const api = await ensureRuntime(ctx);
|
|
39
|
+
const root = await findRoot(api, name);
|
|
40
|
+
await api.stop(root.id);
|
|
41
|
+
ctx.print(`stopped @${name} and everything under it. Its conversation and files are kept.`);
|
|
42
|
+
}
|
|
43
|
+
// Prints the URL of the root's page, with the owner token in its fragment,
|
|
44
|
+
// and opens it where the system can. The page starts nothing, so a stopped
|
|
45
|
+
// root is started first.
|
|
46
|
+
export async function openRoot(ctx, name, open = openURL) {
|
|
47
|
+
const api = await ensureRuntime(ctx);
|
|
48
|
+
const root = await findRoot(api, name);
|
|
49
|
+
if (root.state === "stopped")
|
|
50
|
+
await api.start(root.id);
|
|
51
|
+
const url = `${api.base}/agents/${root.id}/page#token=${api.token}`;
|
|
52
|
+
ctx.print(url);
|
|
53
|
+
if (!(await open(url)))
|
|
54
|
+
ctx.say("open that URL in a browser to see the UI.");
|
|
55
|
+
}
|
|
56
|
+
// Sends the query and follows the root's work until its reply.
|
|
57
|
+
export async function query(ctx, name, text, { interrupt, columns, write }) {
|
|
58
|
+
const api = await ensureRuntime(ctx);
|
|
59
|
+
const root = await findRoot(api, name);
|
|
60
|
+
return follow({ api, root, query: text, say: ctx.say, write, columns, interrupt, sleep: ctx.sleep, now: ctx.now });
|
|
61
|
+
}
|
package/cli/runtime.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
// Starting, checking, replacing and stopping the runtime's container. Every
|
|
2
|
+
// command but config, uninstall and help calls ensureRuntime() first, which
|
|
3
|
+
// starts the runtime when it isn't running and leaves it running after.
|
|
4
|
+
import { Api } from "./api.js";
|
|
5
|
+
import { DockerError } from "./docker.js";
|
|
6
|
+
import { alive, askSettings, IMAGE_KEYS, MissingImage, pulls, readSettings, Refusal, REGISTRY, registryRepository, runningRuntime, runtimeContainer, TARGETS, writeSettings, } from "./settings.js";
|
|
7
|
+
// How long the runtime may take to answer once started.
|
|
8
|
+
export const READY_MS = 15_000;
|
|
9
|
+
const POLL_MS = 200;
|
|
10
|
+
// An image's current ID, pulling an installed jen's own when it's missing,
|
|
11
|
+
// and failing for any other that's missing. pulled is set when it pulled.
|
|
12
|
+
async function image(ctx, key, pulled) {
|
|
13
|
+
const name = ctx.names[key];
|
|
14
|
+
const id = await ctx.docker.imageId(name);
|
|
15
|
+
if (id !== undefined)
|
|
16
|
+
return id;
|
|
17
|
+
if (!pulls(ctx.names, key))
|
|
18
|
+
throw new MissingImage(name, TARGETS[key]);
|
|
19
|
+
ctx.say(`pulling ${name}...`);
|
|
20
|
+
await ctx.docker.pull(name);
|
|
21
|
+
if (pulled)
|
|
22
|
+
pulled.any = true;
|
|
23
|
+
const fetched = await ctx.docker.imageId(name);
|
|
24
|
+
if (fetched === undefined)
|
|
25
|
+
throw new MissingImage(name, TARGETS[key]);
|
|
26
|
+
return fetched;
|
|
27
|
+
}
|
|
28
|
+
// Fails naming the runtime's image when it's missing and isn't pulled, since
|
|
29
|
+
// the settings are read and written with it.
|
|
30
|
+
export async function requireRuntimeImage(ctx) {
|
|
31
|
+
await image(ctx, "runtimeImage");
|
|
32
|
+
}
|
|
33
|
+
// A client of the running runtime, once it's up.
|
|
34
|
+
export async function ensureRuntime(ctx) {
|
|
35
|
+
const { docker, names } = ctx;
|
|
36
|
+
const pulled = { any: false };
|
|
37
|
+
const ids = {};
|
|
38
|
+
for (const key of IMAGE_KEYS)
|
|
39
|
+
ids[key] = await image(ctx, key, pulled);
|
|
40
|
+
const runtime = await docker.inspect(runtimeContainer(names.name));
|
|
41
|
+
const running = runtime !== undefined && alive(runtime.state);
|
|
42
|
+
const theirs = (await docker.containers([`jen.runtime=${names.name}`])).filter((c) => alive(c.state));
|
|
43
|
+
const stale = staleImages(ids, names, running ? runtime : undefined, theirs);
|
|
44
|
+
if (stale.length === 0) {
|
|
45
|
+
const api = running ? await connect(ctx) : await start(ctx);
|
|
46
|
+
if (pulled.any)
|
|
47
|
+
await removeOtherVersions(ctx);
|
|
48
|
+
return api;
|
|
49
|
+
}
|
|
50
|
+
// Agents started from an image that has since changed are stopped, through
|
|
51
|
+
// the runtime, since the tree is its to change. Each starts on the current
|
|
52
|
+
// image at its next message.
|
|
53
|
+
let api = running ? await connect(ctx) : await start(ctx);
|
|
54
|
+
await stopRoots(api);
|
|
55
|
+
if (running && runtime.image !== ids.runtimeImage) {
|
|
56
|
+
await docker.remove(runtimeContainer(names.name));
|
|
57
|
+
api = await start(ctx);
|
|
58
|
+
}
|
|
59
|
+
ctx.say(`${stale.join(" and ")} changed, so jen stopped every agent${running && runtime.image !== ids.runtimeImage ? " and restarted the runtime" : ""}. Each agent starts on the new image at its next message.`);
|
|
60
|
+
await removeOtherVersions(ctx);
|
|
61
|
+
return api;
|
|
62
|
+
}
|
|
63
|
+
// Once nothing runs on them, an installed jen removes the registry's images
|
|
64
|
+
// of its other versions, which an upgrade leaves behind. One a container
|
|
65
|
+
// still uses is passed over, as is any other failure: it's tidying, and the
|
|
66
|
+
// next upgrade tries again.
|
|
67
|
+
async function removeOtherVersions(ctx) {
|
|
68
|
+
const { names } = ctx;
|
|
69
|
+
if (names.version === undefined)
|
|
70
|
+
return;
|
|
71
|
+
const keep = IMAGE_KEYS.filter((key) => pulls(names, key));
|
|
72
|
+
if (keep.length === 0)
|
|
73
|
+
return;
|
|
74
|
+
try {
|
|
75
|
+
for (const listed of await ctx.docker.listImages(`${REGISTRY}/jen-*`)) {
|
|
76
|
+
const ours = keep.some((key) => listed.startsWith(`${registryRepository(key)}:`));
|
|
77
|
+
if (ours && !listed.endsWith(`:${names.version}`))
|
|
78
|
+
await ctx.docker.removeImage(listed).catch(() => { });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch { }
|
|
82
|
+
}
|
|
83
|
+
// The names of the images a running container of jen's isn't on any more.
|
|
84
|
+
function staleImages(ids, names, runtime, theirs) {
|
|
85
|
+
const stale = new Set();
|
|
86
|
+
if (runtime && runtime.image !== ids.runtimeImage)
|
|
87
|
+
stale.add(names.runtimeImage);
|
|
88
|
+
for (const c of theirs) {
|
|
89
|
+
if ("jen.agent" in c.labels && c.image !== ids.agentImage)
|
|
90
|
+
stale.add(names.agentImage);
|
|
91
|
+
if ("jen.workspace" in c.labels && c.image !== ids.workspaceImage)
|
|
92
|
+
stale.add(names.workspaceImage);
|
|
93
|
+
}
|
|
94
|
+
return [...stale];
|
|
95
|
+
}
|
|
96
|
+
// Stops every running root, which stops everything under it.
|
|
97
|
+
export async function stopRoots(api) {
|
|
98
|
+
const roots = (await api.list()).filter((a) => a.parent === null && a.state === "running");
|
|
99
|
+
for (const root of roots)
|
|
100
|
+
await api.stop(root.id);
|
|
101
|
+
return roots.length;
|
|
102
|
+
}
|
|
103
|
+
// A client of the runtime that's running, or being started by another jen,
|
|
104
|
+
// once it answers.
|
|
105
|
+
async function connect(ctx) {
|
|
106
|
+
const deadline = ctx.now() + READY_MS;
|
|
107
|
+
const container = runtimeContainer(ctx.names.name);
|
|
108
|
+
for (;;) {
|
|
109
|
+
const found = await runningRuntime(ctx.docker, ctx.names);
|
|
110
|
+
if (found) {
|
|
111
|
+
const api = new Api(`http://127.0.0.1:${found.port}`, found.token, ctx.fetch);
|
|
112
|
+
if (await answers(ctx, api))
|
|
113
|
+
return api;
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
const details = await ctx.docker.inspect(container);
|
|
117
|
+
if (details && (details.state === "exited" || details.state === "dead")) {
|
|
118
|
+
const logs = await ctx.docker.logs(container, 20);
|
|
119
|
+
throw new Refusal(`the runtime exited as it started${logs ? `:\n${logs}` : "."}`);
|
|
120
|
+
}
|
|
121
|
+
if (!details)
|
|
122
|
+
throw new Refusal("the runtime's container was removed as it started. Try again.");
|
|
123
|
+
}
|
|
124
|
+
if (ctx.now() >= deadline)
|
|
125
|
+
throw new Refusal(`the runtime didn't answer within ${READY_MS / 1000} seconds. Its output is in: docker logs ${container}`);
|
|
126
|
+
await ctx.sleep(POLL_MS);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// Whether the runtime answers at all: any HTTP answer, such as 401, will do.
|
|
130
|
+
async function answers(ctx, api) {
|
|
131
|
+
try {
|
|
132
|
+
await ctx.fetch(`${api.base}/agents`, { signal: AbortSignal.timeout(1_000) }).then((res) => res.body?.cancel());
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// The settings, asking for them from a terminal when there are none, or no
|
|
140
|
+
// base URL, from before it was required. The rest are kept, the token too.
|
|
141
|
+
async function settingsOrAsk(ctx) {
|
|
142
|
+
const settings = await readSettings(ctx.docker, ctx.names);
|
|
143
|
+
if (settings && settings.baseURL !== "")
|
|
144
|
+
return settings;
|
|
145
|
+
if (!ctx.terminal.interactive)
|
|
146
|
+
throw new Refusal(settings ? "jen's settings have no base URL. Run jen config to set it." : "jen has no settings yet. Run jen config to set the model, its base URL and its key.");
|
|
147
|
+
ctx.say(settings ? "jen needs the model's base URL before it can start." : "jen needs a model, its base URL and its key before it can start.");
|
|
148
|
+
const asked = await askSettings(ctx.terminal, settings);
|
|
149
|
+
// A blank line between the answers and what follows.
|
|
150
|
+
ctx.say("");
|
|
151
|
+
await writeSettings(ctx.docker, ctx.names, asked);
|
|
152
|
+
return asked;
|
|
153
|
+
}
|
|
154
|
+
// Starts the runtime's container, which isn't running, and waits for it.
|
|
155
|
+
export async function start(ctx) {
|
|
156
|
+
const { docker, names } = ctx;
|
|
157
|
+
const settings = await settingsOrAsk(ctx);
|
|
158
|
+
const container = runtimeContainer(names.name);
|
|
159
|
+
// Whatever is left of it, such as a container that exited.
|
|
160
|
+
const left = await docker.inspect(container);
|
|
161
|
+
if (left && !alive(left.state))
|
|
162
|
+
await docker.remove(container);
|
|
163
|
+
const run = (port) => docker.run([
|
|
164
|
+
"-d",
|
|
165
|
+
"--name",
|
|
166
|
+
container,
|
|
167
|
+
"--label",
|
|
168
|
+
`jen.runtime=${names.name}`,
|
|
169
|
+
"-v",
|
|
170
|
+
"/var/run/docker.sock:/var/run/docker.sock",
|
|
171
|
+
"-p",
|
|
172
|
+
port === undefined ? "127.0.0.1::8080" : `127.0.0.1:${port}:8080`,
|
|
173
|
+
...["JEN_OWNER_TOKEN", "JEN_MODEL", "JEN_API_KEY", "JEN_BASE_URL", "JEN_RUNTIME_NAME", "JEN_AGENT_IMAGE", "JEN_WORKSPACE_IMAGE"].flatMap((n) => ["-e", n]),
|
|
174
|
+
names.runtimeImage,
|
|
175
|
+
], {
|
|
176
|
+
env: { JEN_MODEL: settings.model, JEN_BASE_URL: settings.baseURL, JEN_RUNTIME_NAME: names.name, JEN_AGENT_IMAGE: names.agentImage, JEN_WORKSPACE_IMAGE: names.workspaceImage },
|
|
177
|
+
secrets: { JEN_OWNER_TOKEN: settings.ownerToken, JEN_API_KEY: settings.apiKey },
|
|
178
|
+
});
|
|
179
|
+
// A name already in use means another jen is starting the runtime.
|
|
180
|
+
const conflict = (stderr) => /Conflict\. The container name .* is already in use/.test(stderr);
|
|
181
|
+
let output = await run(settings.port);
|
|
182
|
+
if (output.code !== 0 && !conflict(output.stderr) && settings.port !== undefined) {
|
|
183
|
+
// The saved port is taken, most likely. Docker chooses another.
|
|
184
|
+
await docker.remove(container);
|
|
185
|
+
output = await run(undefined);
|
|
186
|
+
}
|
|
187
|
+
if (output.code !== 0 && !conflict(output.stderr)) {
|
|
188
|
+
await docker.remove(container);
|
|
189
|
+
throw new DockerError(["run"], output);
|
|
190
|
+
}
|
|
191
|
+
const api = await connect(ctx);
|
|
192
|
+
if (output.code === 0) {
|
|
193
|
+
// Keeping the port keeps a UI's URL the same across restarts.
|
|
194
|
+
const port = Number(new URL(api.base).port);
|
|
195
|
+
if (port !== settings.port)
|
|
196
|
+
await writeSettings(docker, names, { ...settings, port });
|
|
197
|
+
}
|
|
198
|
+
return api;
|
|
199
|
+
}
|
|
200
|
+
// jen config: asks for the settings and saves them, then replaces a running
|
|
201
|
+
// runtime with one that has them. Agents keep running through that, since the
|
|
202
|
+
// runtime sets the model on every call itself.
|
|
203
|
+
export async function config(ctx) {
|
|
204
|
+
if (!ctx.terminal.interactive)
|
|
205
|
+
throw new Refusal("jen config asks for the settings, so it needs a terminal.");
|
|
206
|
+
await requireRuntimeImage(ctx);
|
|
207
|
+
const current = await readSettings(ctx.docker, ctx.names);
|
|
208
|
+
const settings = await askSettings(ctx.terminal, current);
|
|
209
|
+
ctx.say("");
|
|
210
|
+
await writeSettings(ctx.docker, ctx.names, settings);
|
|
211
|
+
ctx.print("saved jen's settings.");
|
|
212
|
+
if (await runningRuntime(ctx.docker, ctx.names)) {
|
|
213
|
+
await ctx.docker.remove(runtimeContainer(ctx.names.name));
|
|
214
|
+
await start(ctx);
|
|
215
|
+
ctx.print("restarted the runtime with them. Running agents keep running.");
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// jen stop all: stops every agent through the runtime, starting it first when
|
|
219
|
+
// agents run without it, then removes the runtime. Every volume is kept.
|
|
220
|
+
export async function stopAll(ctx) {
|
|
221
|
+
const { docker, names } = ctx;
|
|
222
|
+
const runtime = await docker.inspect(runtimeContainer(names.name));
|
|
223
|
+
const agents = (await docker.containers([`jen.runtime=${names.name}`, "jen.agent"])).filter((c) => alive(c.state));
|
|
224
|
+
if (!(runtime && alive(runtime.state)) && agents.length === 0) {
|
|
225
|
+
if (runtime)
|
|
226
|
+
await docker.remove(runtimeContainer(names.name));
|
|
227
|
+
return ctx.print("nothing of jen's is running.");
|
|
228
|
+
}
|
|
229
|
+
const api = await ensureRuntime(ctx);
|
|
230
|
+
const stopped = await stopRoots(api);
|
|
231
|
+
await docker.remove(runtimeContainer(names.name));
|
|
232
|
+
const roots = stopped === 1 ? "1 root agent and everything under it" : `${stopped} root agents and everything under them`;
|
|
233
|
+
ctx.print(`stopped ${roots}, and the runtime. Their conversations and files are kept.`);
|
|
234
|
+
}
|
package/cli/settings.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
// jen's settings: the model, its base URL and key, the owner token jen makes
|
|
2
|
+
// up once, and the port the runtime was last published on. They're kept in a
|
|
3
|
+
// volume of the command's own, <name>-settings, as settings.json, so nothing
|
|
4
|
+
// of jen's lives outside Docker. The runtime never mounts it: it gets the
|
|
5
|
+
// settings in its environment when jen starts it. Reading and writing the
|
|
6
|
+
// volume takes a short-lived container of the runtime's image, so while the
|
|
7
|
+
// runtime runs, its token and port are read from its container instead.
|
|
8
|
+
import { randomBytes } from "node:crypto";
|
|
9
|
+
import { DockerError } from "./docker.js";
|
|
10
|
+
import { VERSION } from "./release.js";
|
|
11
|
+
// The Dockerfile target each image is built from.
|
|
12
|
+
export const TARGETS = { runtimeImage: "runtime", agentImage: "agent", workspaceImage: "workspace" };
|
|
13
|
+
export const IMAGE_KEYS = Object.keys(TARGETS);
|
|
14
|
+
// Where a release's images are published, as <REGISTRY>/jen-<target>:<version>.
|
|
15
|
+
export const REGISTRY = "ghcr.io/reveer-ai";
|
|
16
|
+
export const registryRepository = (key) => `${REGISTRY}/jen-${TARGETS[key]}`;
|
|
17
|
+
// An installed jen names its own release's images in the registry, and a
|
|
18
|
+
// checkout the images it builds. The environment overrides either.
|
|
19
|
+
export function namesFrom(env, version = VERSION) {
|
|
20
|
+
const given = { runtimeImage: env.JEN_RUNTIME_IMAGE, agentImage: env.JEN_AGENT_IMAGE, workspaceImage: env.JEN_WORKSPACE_IMAGE };
|
|
21
|
+
const image = (key) => given[key] || (version === undefined ? `jen-${TARGETS[key]}` : `${registryRepository(key)}:${version}`);
|
|
22
|
+
return {
|
|
23
|
+
name: env.JEN_RUNTIME_NAME || "jen",
|
|
24
|
+
runtimeImage: image("runtimeImage"),
|
|
25
|
+
agentImage: image("agentImage"),
|
|
26
|
+
workspaceImage: image("workspaceImage"),
|
|
27
|
+
...(version === undefined ? {} : { version }),
|
|
28
|
+
overridden: IMAGE_KEYS.filter((key) => given[key]),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// Whether jen pulls the image when it's missing: an installed jen's own.
|
|
32
|
+
export const pulls = (names, key) => names.version !== undefined && !names.overridden.includes(key);
|
|
33
|
+
export const settingsVolume = (name) => `${name}-settings`;
|
|
34
|
+
export const runtimeContainer = (name) => `${name}-runtime`;
|
|
35
|
+
// An image jen needs that isn't there and doesn't pull, named with the
|
|
36
|
+
// command that builds it.
|
|
37
|
+
export class MissingImage extends Error {
|
|
38
|
+
constructor(image, target) {
|
|
39
|
+
super(`the image ${image} is missing. Build it from jen's checkout with: docker build --target ${target} -t ${image} .`);
|
|
40
|
+
this.name = "MissingImage";
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// What the user can do about something, as opposed to a fault.
|
|
44
|
+
export class Refusal extends Error {
|
|
45
|
+
constructor(message) {
|
|
46
|
+
super(message);
|
|
47
|
+
this.name = "Refusal";
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// The settings in the volume, or undefined when there are none yet.
|
|
51
|
+
export async function readSettings(docker, names) {
|
|
52
|
+
const volume = settingsVolume(names.name);
|
|
53
|
+
// docker run would create a missing volume, without jen's labels.
|
|
54
|
+
if (!(await docker.volumeExists(volume)))
|
|
55
|
+
return undefined;
|
|
56
|
+
const args = ["--rm", "--network", "none", "-v", `${volume}:/settings:ro`, "--entrypoint", "cat", names.runtimeImage, "/settings/settings.json"];
|
|
57
|
+
const output = await docker.run(args);
|
|
58
|
+
if (output.code !== 0) {
|
|
59
|
+
if (/No such file/i.test(output.stderr))
|
|
60
|
+
return undefined;
|
|
61
|
+
throw new DockerError(["run"], output);
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
const parsed = JSON.parse(output.stdout);
|
|
65
|
+
// Settings saved before the base URL was required may have none.
|
|
66
|
+
const { model, apiKey, baseURL = "", ownerToken, port } = parsed ?? {};
|
|
67
|
+
if ([model, apiKey, baseURL, ownerToken].every((v) => typeof v === "string") && (port === undefined || Number.isSafeInteger(port))) {
|
|
68
|
+
return { model, apiKey, baseURL, ownerToken, ...(port === undefined ? {} : { port }) };
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch { }
|
|
72
|
+
// Settings that can't be read are asked for again, like missing ones.
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
// Replaces the settings whole: written to a temporary file renamed over the
|
|
76
|
+
// old one, so a write cut short leaves them as they were. The key is in the
|
|
77
|
+
// container's stdin, never its arguments.
|
|
78
|
+
export async function writeSettings(docker, names, settings) {
|
|
79
|
+
const volume = settingsVolume(names.name);
|
|
80
|
+
await docker.createVolume(volume, { "jen.runtime": names.name, "jen.settings": "" });
|
|
81
|
+
const script = "cat > /settings/.settings.json.tmp && mv /settings/.settings.json.tmp /settings/settings.json";
|
|
82
|
+
const args = ["--rm", "-i", "--network", "none", "-v", `${volume}:/settings`, "--entrypoint", "sh", names.runtimeImage, "-c", script];
|
|
83
|
+
const output = await docker.run(args, { input: JSON.stringify(settings), guard: [settings.apiKey, settings.ownerToken] });
|
|
84
|
+
if (output.code !== 0)
|
|
85
|
+
throw new DockerError(["run"], output);
|
|
86
|
+
}
|
|
87
|
+
// The running runtime's token and port, read from its container with no
|
|
88
|
+
// container started, or undefined when it isn't running or not yet published.
|
|
89
|
+
export async function runningRuntime(docker, names) {
|
|
90
|
+
const container = runtimeContainer(names.name);
|
|
91
|
+
const details = await docker.inspect(container);
|
|
92
|
+
if (!details || !alive(details.state))
|
|
93
|
+
return undefined;
|
|
94
|
+
const token = details.env.find((e) => e.startsWith("JEN_OWNER_TOKEN="))?.slice("JEN_OWNER_TOKEN=".length);
|
|
95
|
+
const port = await docker.port(container);
|
|
96
|
+
if (!token || port === undefined)
|
|
97
|
+
return undefined;
|
|
98
|
+
return { token, port };
|
|
99
|
+
}
|
|
100
|
+
export const alive = (state) => state === "running" || state === "restarting";
|
|
101
|
+
// The key as it's shown: its last four characters at most.
|
|
102
|
+
export const mask = (key) => (key.length > 8 ? `…${key.slice(-4)}` : "…");
|
|
103
|
+
// Asks for the model, the base URL and the key, in that order, showing each
|
|
104
|
+
// current value, and keeping it on an empty answer. All three are required,
|
|
105
|
+
// so with no current value they're asked again. The owner token is
|
|
106
|
+
// made up the first time, and kept.
|
|
107
|
+
export async function askSettings(terminal, current) {
|
|
108
|
+
const required = async (ask, kept) => {
|
|
109
|
+
for (;;) {
|
|
110
|
+
const answer = (await ask()).trim();
|
|
111
|
+
if (answer !== "")
|
|
112
|
+
return answer;
|
|
113
|
+
if (kept)
|
|
114
|
+
return kept;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const shown = (value) => (value ? ` [${value}]` : "");
|
|
118
|
+
const model = await required(() => terminal.ask(`model${shown(current?.model)}: `), current?.model);
|
|
119
|
+
const baseURL = await required(() => terminal.ask(`base URL${shown(current?.baseURL)}: `), current?.baseURL);
|
|
120
|
+
const apiKey = await required(() => terminal.askHidden(`model key${current?.apiKey ? ` [${mask(current.apiKey)}]` : ""}: `), current?.apiKey);
|
|
121
|
+
return {
|
|
122
|
+
model,
|
|
123
|
+
baseURL,
|
|
124
|
+
apiKey,
|
|
125
|
+
ownerToken: current?.ownerToken ?? randomBytes(32).toString("hex"),
|
|
126
|
+
...(current?.port === undefined ? {} : { port: current.port }),
|
|
127
|
+
};
|
|
128
|
+
}
|