alineo-cli 0.2.1 → 0.4.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.
@@ -1,265 +0,0 @@
1
- import { c as serverConfigPath, i as configPath, l as serverDataDir, o as serverConfigContent, s as serverConfigDir, u as writeConfig } from "./index.mjs";
2
- import { existsSync } from "fs";
3
- import { mkdir } from "fs/promises";
4
- //#region src/docker.ts
5
- async function spawn(cmd) {
6
- let proc;
7
- try {
8
- proc = Bun.spawn(cmd, {
9
- stdout: "pipe",
10
- stderr: "pipe"
11
- });
12
- } catch {
13
- throw new Error(`'${cmd[0]}' not found — is Docker installed?`);
14
- }
15
- const [stdout, stderr, exitCode] = await Promise.all([
16
- new Response(proc.stdout).text(),
17
- new Response(proc.stderr).text(),
18
- proc.exited
19
- ]);
20
- return {
21
- ok: exitCode === 0,
22
- stdout,
23
- stderr
24
- };
25
- }
26
- async function checkDocker() {
27
- const { ok, stderr } = await spawn(["docker", "info"]);
28
- if (!ok) {
29
- if (stderr.includes("Cannot connect") || stderr.includes("daemon") || stderr.includes("socket")) throw new Error("Docker daemon is not running. Start Docker and try again.");
30
- throw new Error("Docker is not available. Install Docker and try again.");
31
- }
32
- }
33
- async function getContainerState(name) {
34
- const { ok, stdout } = await spawn([
35
- "docker",
36
- "inspect",
37
- name,
38
- "--format",
39
- "{{.State.Status}}"
40
- ]);
41
- if (!ok) return "missing";
42
- return stdout.trim() === "running" ? "running" : "stopped";
43
- }
44
- async function startContainer(name) {
45
- const { ok, stderr } = await spawn([
46
- "docker",
47
- "start",
48
- name
49
- ]);
50
- if (!ok) throw new Error(`Failed to start container '${name}': ${stderr.trim()}`);
51
- }
52
- async function runContainer(args, label = "container") {
53
- const { ok, stderr } = await spawn([
54
- "docker",
55
- "run",
56
- ...args
57
- ]);
58
- if (!ok) throw new Error(`Failed to start ${label}: ${stderr.trim()}`);
59
- }
60
- async function pullImage(image) {
61
- const { ok, stderr } = await spawn([
62
- "docker",
63
- "pull",
64
- image
65
- ]);
66
- if (!ok) throw new Error(`Failed to pull '${image}': ${stderr.trim()}`);
67
- }
68
- /** Default health predicate: OpenSandbox's `/health` returns `{ status: "healthy" }`. */
69
- const isOpenSandboxHealthy = (body) => body?.status === "healthy";
70
- async function pollHealth(url, timeoutMs = 6e4, isHealthy = isOpenSandboxHealthy) {
71
- const deadline = Date.now() + timeoutMs;
72
- while (Date.now() < deadline) {
73
- try {
74
- const res = await fetch(url);
75
- if (res.ok && isHealthy(await res.json())) return;
76
- } catch {}
77
- await new Promise((r) => setTimeout(r, 1e3));
78
- }
79
- throw new Error(`${url} did not become healthy within ${timeoutMs / 1e3}s`);
80
- }
81
- //#endregion
82
- //#region src/pi-model-keys.ts
83
- /**
84
- * Model API key env vars Pi's coding-agent runtime resolves providers from — mirrors the
85
- * `envMap` in earendil-works/pi's `packages/ai/src/env-api-keys.ts` (plus Anthropic's three,
86
- * handled separately there). An `AgentSpec`'s env map (`{ NVIDIA_API_KEY: "${NVIDIA_API_KEY}" }`,
87
- * see `packages/agent/src/schema.ts`) is resolved from `process.env` inside whatever process
88
- * calls `Alineo.load()`/`.spawn()` — for a swarm run through alineod, that's alineod's own
89
- * container. `alineo init` forwards whichever of these are set in the operator's shell into
90
- * that container so any Pi-supported model works, not just NVIDIA's free tier.
91
- *
92
- * Deliberately excludes ambient/file-based credential mechanisms (AWS IAM role chains,
93
- * `AWS_PROFILE`, Google Application Default Credentials) — those need a mounted file or
94
- * assumed-role wiring, not just a passed-through env var. Amazon Bedrock and Google Vertex
95
- * are not yet supported by `init`'s auto-start for that reason.
96
- *
97
- * Keep in sync with pi's `env-api-keys.ts` `envMap` as it adds providers.
98
- */
99
- const PI_MODEL_API_KEY_ENV_VARS = [
100
- "ANTHROPIC_API_KEY",
101
- "ANTHROPIC_OAUTH_TOKEN",
102
- "ANTHROPIC_AUTH_TOKEN",
103
- "OPENAI_API_KEY",
104
- "AZURE_OPENAI_API_KEY",
105
- "GEMINI_API_KEY",
106
- "NVIDIA_API_KEY",
107
- "DEEPSEEK_API_KEY",
108
- "MISTRAL_API_KEY",
109
- "GROQ_API_KEY",
110
- "CEREBRAS_API_KEY",
111
- "XAI_API_KEY",
112
- "OPENROUTER_API_KEY",
113
- "AI_GATEWAY_API_KEY",
114
- "CLOUDFLARE_API_KEY",
115
- "CLOUDFLARE_ACCOUNT_ID",
116
- "CLOUDFLARE_GATEWAY_ID",
117
- "RADIUS_API_KEY",
118
- "OPENCODE_API_KEY",
119
- "QWEN_TOKEN_PLAN_API_KEY",
120
- "QWEN_TOKEN_PLAN_CN_API_KEY",
121
- "MOONSHOT_API_KEY",
122
- "KIMI_API_KEY",
123
- "ZAI_API_KEY",
124
- "ZAI_CODING_CN_API_KEY",
125
- "MINIMAX_API_KEY",
126
- "MINIMAX_CN_API_KEY",
127
- "XIAOMI_API_KEY",
128
- "XIAOMI_TOKEN_PLAN_CN_API_KEY",
129
- "XIAOMI_TOKEN_PLAN_AMS_API_KEY",
130
- "XIAOMI_TOKEN_PLAN_SGP_API_KEY",
131
- "ANT_LING_API_KEY",
132
- "HF_TOKEN",
133
- "FIREWORKS_API_KEY",
134
- "TOGETHER_API_KEY",
135
- "BASETEN_API_KEY",
136
- "COPILOT_GITHUB_TOKEN"
137
- ];
138
- //#endregion
139
- //#region src/commands/init.ts
140
- const OPENSANDBOX_CONTAINER_NAME = "alineo-opensandbox";
141
- const SERVER_URL = "http://127.0.0.1:8080";
142
- const ALINEOD_CONTAINER_NAME = "alineo-alineod";
143
- const ALINEOD_IMAGE = "ghcr.io/drejt/alineod:latest";
144
- const ALINEOD_URL = "http://127.0.0.1:4600";
145
- const isAlineodHealthy = (body) => body?.ok === true;
146
- async function init() {
147
- console.log("Checking Docker...");
148
- await checkDocker();
149
- const state = await getContainerState(OPENSANDBOX_CONTAINER_NAME);
150
- if (state === "running") console.log(`OpenSandbox already running at ${SERVER_URL}`);
151
- else if (state === "stopped") {
152
- console.log("Restarting OpenSandbox container...");
153
- await startContainer(OPENSANDBOX_CONTAINER_NAME);
154
- console.log("Waiting for OpenSandbox to be ready...");
155
- await pollHealth(`${SERVER_URL}/health`);
156
- } else {
157
- await ensureServerConfig();
158
- await ensureServerDataDir();
159
- console.log("Starting OpenSandbox in Docker...");
160
- await runContainer([
161
- "-d",
162
- "--name",
163
- OPENSANDBOX_CONTAINER_NAME,
164
- "-p",
165
- "8080:8080",
166
- "-v",
167
- "/var/run/docker.sock:/var/run/docker.sock",
168
- "-v",
169
- `${serverConfigPath()}:/etc/opensandbox/config.toml:ro`,
170
- "-v",
171
- `${serverDataDir()}:/data`,
172
- "-e",
173
- "SANDBOX_CONFIG_PATH=/etc/opensandbox/config.toml",
174
- "-e",
175
- "OPENSANDBOX_INSECURE_SERVER=YES",
176
- "opensandbox/server:latest"
177
- ], "OpenSandbox container");
178
- console.log("Waiting for OpenSandbox to be ready...");
179
- await pollHealth(`${SERVER_URL}/health`);
180
- }
181
- await ensureAlineod();
182
- await ensureProjectConfig();
183
- console.log(`OpenSandbox running at ${SERVER_URL}, alineod running at ${ALINEOD_URL} — ready.`);
184
- }
185
- async function ensureAlineod() {
186
- const state = await getContainerState(ALINEOD_CONTAINER_NAME);
187
- if (state === "running") {
188
- console.log(`alineod already running at ${ALINEOD_URL}`);
189
- return;
190
- }
191
- if (state === "stopped") {
192
- console.log("Restarting alineod container...");
193
- await startContainer(ALINEOD_CONTAINER_NAME);
194
- } else {
195
- console.log(`Pulling ${ALINEOD_IMAGE}...`);
196
- await pullImage(ALINEOD_IMAGE);
197
- console.log("Starting alineod in Docker...");
198
- const foundModelKeys = PI_MODEL_API_KEY_ENV_VARS.filter((name) => process.env[name]);
199
- const modelKeyArgs = foundModelKeys.flatMap((name) => ["-e", `${name}=${process.env[name]}`]);
200
- if (foundModelKeys.length > 0) console.log(`Forwarding model key(s): ${foundModelKeys.join(", ")}`);
201
- else console.log("No known model API key found in the environment — alineod will start, but agent specs referencing a provider key will fail until one is set (see any of the env vars in packages/cli/src/pi-model-keys.ts).");
202
- await runContainer([
203
- "-d",
204
- "--name",
205
- ALINEOD_CONTAINER_NAME,
206
- "--network",
207
- "host",
208
- "-e",
209
- `ALINEO_SERVER_URL=${SERVER_URL}`,
210
- "-e",
211
- "ALINEO_USE_SERVER_PROXY=true",
212
- ...modelKeyArgs,
213
- "-v",
214
- "alineod-data:/data",
215
- ALINEOD_IMAGE
216
- ], "alineod container");
217
- }
218
- console.log("Waiting for alineod to be ready...");
219
- await pollHealth(`${ALINEOD_URL}/health`, 6e4, isAlineodHealthy);
220
- }
221
- async function ensureServerConfig() {
222
- const dir = serverConfigDir();
223
- if (!existsSync(dir)) await mkdir(dir, { recursive: true });
224
- const path = serverConfigPath();
225
- if (!existsSync(path)) await Bun.write(path, serverConfigContent());
226
- }
227
- /**
228
- * Host-side half of the `serverDataDir()` bind mount. `docker run -v` would auto-create
229
- * a missing host path anyway, but doing it explicitly here matches `ensureServerConfig()`'s
230
- * pattern and keeps directory creation in one place rather than relying on Docker's
231
- * legacy-`-v`-specific behavior (unlike `--mount`, which requires the source to pre-exist).
232
- */
233
- async function ensureServerDataDir() {
234
- const dir = serverDataDir();
235
- if (!existsSync(dir)) await mkdir(dir, { recursive: true });
236
- }
237
- async function ensureProjectConfig() {
238
- if (!existsSync(configPath())) {
239
- await writeConfig({
240
- serverUrl: SERVER_URL,
241
- useServerProxy: true,
242
- apiKey: "",
243
- adapterPath: "./.alineo/ledger.db",
244
- agentsDir: "./agents",
245
- defaults: { resources: {
246
- cpu: "1000m",
247
- memory: "1Gi"
248
- } }
249
- });
250
- console.log("Created alineo.config.json");
251
- }
252
- }
253
- const initCommand = {
254
- name: "init",
255
- group: "sdk",
256
- variants: [{
257
- usage: "alineo init",
258
- summary: "Start OpenSandbox and alineod locally via Docker"
259
- }],
260
- run: async () => {
261
- await init();
262
- }
263
- };
264
- //#endregion
265
- export { initCommand };