forked from highflame-ai/codeoid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
380 lines (352 loc) · 13.2 KB
/
Copy pathcli.ts
File metadata and controls
380 lines (352 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/usr/bin/env bun
/**
* Codeoid CLI — terminal interface + daemon launcher.
*
* Usage:
* codeoid start Start daemon (+ Telegram + Web UI)
* codeoid ls List active sessions
* codeoid new <name> <workdir> Create a new session
* codeoid attach <name|id> Attach to a session (streaming)
* codeoid send <name|id> <message> Send a one-shot message
* codeoid interrupt <name|id> Interrupt a running agent
* codeoid approve <name|id> [yes|no] Approve/deny pending permission
* codeoid destroy <name|id> Destroy a session
*/
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { program } from "commander";
import { DaemonServer } from "./daemon/server.js";
import { TerminalClient } from "./terminal/client.js";
import {
getConfigDir,
loadConfig,
loadDotEnv,
resolveZeroidUrl,
ZEROID_PRESETS,
} from "./config.js";
program
.name("codeoid")
.description("Identity-first remote control plane for AI coding agents")
.version("0.1.0");
// ── Daemon ────────────────────────────────────────────────────────────────────
program
.command("start")
.description("Start the Codeoid daemon with all frontends")
.option("-p, --port <port>", "Port to listen on", "7400")
.option("--host <host>", "Host to bind to", "127.0.0.1")
.option("--no-telegram", "Disable Telegram bot")
.option("--no-web", "Disable Web UI")
.action(async (opts) => {
// Load ~/.codeoid/.env before anything reads process.env — this is where
// env-only secrets (TELEGRAM_BOT_TOKEN, TELEGRAM_ALLOWED_USER_IDS) live so
// they survive daemon restarts regardless of which shell launched it.
const dotenvKeys = loadDotEnv();
if (dotenvKeys.length > 0) {
console.log(
`[codeoid] loaded ${dotenvKeys.length} var(s) from ~/.codeoid/.env: ${dotenvKeys.join(", ")}`,
);
}
const config = loadConfig();
const daemon = new DaemonServer({
port: Number.parseInt(opts.port, 10),
host: opts.host,
dbPath: config.dbPath,
transcriptDir: config.transcriptDir,
auth: config.auth,
oauth: config.oauth,
agentIdentity: config.agentIdentity,
memory: config.memory?.enabled
? {
dbPath: config.memory.dbPath,
model: config.memory.model,
modelCacheDir: config.memory.modelCacheDir,
}
: undefined,
// Forward the full config so session-level features (compress, etc.)
// get the parsed shape rather than re-reading env/file.
fullConfig: config,
});
// ── Register frontends ────────────────────────────────────────
// Web UI (always enabled unless --no-web) — the SolidJS app at /ui,
// single-origin so one HTTPS tunnel also serves as a Telegram Mini App.
if (opts.web !== false) {
const { WebUiFrontend } = await import("./frontends/web-ui/index.js");
daemon.use(new WebUiFrontend());
}
// Telegram (enabled when TELEGRAM_BOT_TOKEN is set)
if (opts.telegram !== false) {
const botToken = process.env.TELEGRAM_BOT_TOKEN;
const allowedIds = (process.env.TELEGRAM_ALLOWED_USER_IDS ?? "")
.split(",")
.map(Number)
.filter(Boolean);
if (botToken && allowedIds.length > 0) {
const { TelegramFrontend } = await import("./frontends/telegram/index.js");
daemon.use(new TelegramFrontend(botToken, allowedIds));
} else if (botToken) {
console.log("[codeoid] TELEGRAM_BOT_TOKEN set but TELEGRAM_ALLOWED_USER_IDS missing — skipping Telegram");
}
}
await daemon.start();
if (opts.web !== false) {
const url = `http://${opts.host === "0.0.0.0" ? "localhost" : opts.host}:${opts.port}/ui/`;
console.log(`[codeoid] web UI: ${url}`);
}
});
// ── Auth ──────────────────────────────────────────────────────────────────────
/** The full scope set codeoid asks ZeroID to mint for a session-driving key. */
const CODEOID_LOGIN_SCOPES = [
"session:create",
"session:list",
"session:attach",
"session:watch",
"session:send",
"session:interrupt",
"session:approve",
"session:destroy",
"fs:read",
"tools:read",
"tools:write",
"tools:execute",
"tools:agent",
].join(" ");
/** Read a secret from the TTY without echoing it (handles paste + backspace). */
function readSecret(promptText: string): Promise<string> {
process.stdout.write(promptText);
const stdin = process.stdin;
// Non-TTY (piped) input: read a single line plainly.
if (!stdin.isTTY) {
return new Promise((resolve) => {
let buf = "";
stdin.setEncoding("utf8");
stdin.on("data", (c: string) => {
buf += c;
});
stdin.on("end", () => resolve(buf.trim()));
});
}
return new Promise((resolve) => {
stdin.setRawMode(true);
stdin.resume();
let buf = "";
const onData = (chunk: Buffer): void => {
for (const ch of chunk.toString("utf8")) {
if (ch === "\r" || ch === "\n") {
stdin.setRawMode(false);
stdin.pause();
stdin.off("data", onData);
process.stdout.write("\n");
resolve(buf.trim());
return;
}
if (ch === "\u0003") process.exit(130); // Ctrl-C
else if (ch === "\u007f" || ch === "\b") buf = buf.slice(0, -1);
else buf += ch;
}
};
stdin.on("data", onData);
});
}
/** Merge-write the raw config.json (preserves existing keys, no defaults baked in). */
function writeConfigKeys(updates: Record<string, unknown>): string {
const dir = getConfigDir();
mkdirSync(dir, { recursive: true });
const path = join(dir, "config.json");
let current: Record<string, unknown> = {};
if (existsSync(path)) {
try {
current = JSON.parse(readFileSync(path, "utf8")) as Record<string, unknown>;
} catch {
// Corrupt file — refuse to clobber silently.
throw new Error(`Existing ${path} is not valid JSON; fix or remove it first.`);
}
}
const merged = { ...current, ...updates };
writeFileSync(path, `${JSON.stringify(merged, null, 2)}\n`, { mode: 0o600 });
return path;
}
program
.command("login [apiKey]")
.description(
"Authenticate with a ZeroID key (mint one in Studio's Code Agents screen). Stores it in ~/.codeoid/config.json.",
)
.option(
"--zeroid <preset-or-url>",
`ZeroID issuer: ${Object.keys(ZEROID_PRESETS).join(" | ")} | <url> (default: highflame SaaS)`,
)
.option("--no-verify", "Skip the token-exchange check and just save the key")
.action(
async (
apiKeyArg: string | undefined,
opts: { zeroid?: string; verify?: boolean },
) => {
// Resolve the issuer: explicit --zeroid wins, else whatever config resolves to.
const issuerInput = opts.zeroid;
const baseUrl = issuerInput
? resolveZeroidUrl(issuerInput)
: loadConfig().zeroidUrl;
const apiKey =
apiKeyArg ??
process.env.CODEOID_API_KEY ??
(await readSecret("Paste your ZeroID key (zid_sk_...): "));
if (!apiKey) {
console.error("No key provided.");
process.exit(1);
}
// Verify by exchanging the key for a token (unless --no-verify).
if (opts.verify !== false) {
process.stdout.write(`Verifying against ${baseUrl} ... `);
try {
const res = await fetch(`${baseUrl}/oauth2/token`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
grant_type: "api_key",
api_key: apiKey,
scope: CODEOID_LOGIN_SCOPES,
}),
});
if (!res.ok) {
console.error(`failed (HTTP ${res.status}).`);
console.error(` ${(await res.text()).slice(0, 300)}`);
process.exit(1);
}
const body = (await res.json()) as { access_token?: string };
if (!body.access_token) {
console.error("failed: no access_token in response.");
process.exit(1);
}
const claims = JSON.parse(
Buffer.from(body.access_token.split(".")[1] ?? "", "base64").toString("utf8"),
) as { sub?: string; scope?: string[]; scopes?: string[] };
console.log("ok.");
console.log(` subject: ${claims.sub ?? "(unknown)"}`);
const scopes = claims.scope ?? claims.scopes ?? [];
console.log(` scopes: ${scopes.length ? scopes.join(", ") : "(none)"}`);
} catch (err) {
console.error(`failed: ${err instanceof Error ? err.message : String(err)}`);
console.error(` (Is ${baseUrl} reachable? Override with --zeroid.)`);
process.exit(1);
}
}
// Persist. Store the issuer symbolically (preset name or URL as given) so
// the file stays readable; only touch zeroidUrl when --zeroid was passed.
const updates: Record<string, unknown> = { apiKey };
if (issuerInput) updates.zeroidUrl = issuerInput;
const path = writeConfigKeys(updates);
console.log(`Saved to ${path} (mode 600).`);
console.log("Run `codeoid start` to launch the daemon.");
},
);
// ── Session commands ──────────────────────────────────────────────────────────
program
.command("ls")
.description("List active sessions")
.action(async () => {
const config = loadConfig();
const client = new TerminalClient(config);
await client.connect();
await client.listSessions();
client.disconnect();
});
program
.command("new <name> [workdir]")
.description("Create a new agent session. With --worktree, auto-spawns a git worktree.")
.option(
"--worktree <branch>",
"Create a git worktree for <branch> and run the session there. Requires being inside a git repo (or pass --repo).",
)
.option(
"--repo <path>",
"Path to the source git repo (defaults to current directory when --worktree is set).",
)
.option(
"--worktree-dir <path>",
"Override the worktree directory (default: <repo>.wt-<branch>).",
)
.action(
async (
name: string,
workdir: string | undefined,
opts: { worktree?: string; repo?: string; worktreeDir?: string },
) => {
const config = loadConfig();
let resolvedWorkdir = workdir;
if (opts.worktree) {
const { createWorktree } = await import("./worktree.js");
resolvedWorkdir = await createWorktree({
branch: opts.worktree,
repo: opts.repo ?? process.cwd(),
workdir: opts.worktreeDir,
});
console.log(`[codeoid] worktree ready: ${resolvedWorkdir}`);
}
if (!resolvedWorkdir) {
console.error("workdir is required (pass as argument or use --worktree).");
process.exit(1);
}
const client = new TerminalClient(config);
await client.connect();
await client.createSession(name, resolvedWorkdir);
client.disconnect();
},
);
program
.command("attach <session>")
.description("Attach to a session (interactive streaming)")
.action(async (session: string) => {
const config = loadConfig();
const client = new TerminalClient(config);
await client.connect();
await client.attachSession(session);
});
program
.command("tui")
.description("Interactive multi-session cockpit (Ink-based TUI)")
.action(async () => {
const config = loadConfig();
const { startTui } = await import("./tui/index.js");
await startTui(config);
});
program
.command("send <session> <message...>")
.description("Send a message to a session")
.action(async (session: string, messageParts: string[]) => {
const config = loadConfig();
const client = new TerminalClient(config);
await client.connect();
await client.sendMessage(session, messageParts.join(" "));
client.disconnect();
});
program
.command("interrupt <session>")
.description("Interrupt a running agent")
.action(async (session: string) => {
const config = loadConfig();
const client = new TerminalClient(config);
await client.connect();
await client.interruptSession(session);
client.disconnect();
});
program
.command("approve <session>")
.description("Approve a pending permission request")
.option("--deny", "Deny instead of approve")
.action(async (session: string, opts: { deny?: boolean }) => {
const config = loadConfig();
const client = new TerminalClient(config);
await client.connect();
await client.approveSession(session, !opts.deny);
client.disconnect();
});
program
.command("destroy <session>")
.description("Destroy a session")
.action(async (session: string) => {
const config = loadConfig();
const client = new TerminalClient(config);
await client.connect();
await client.destroySession(session);
client.disconnect();
});
program.parse();