@breadbars/spacez-cli 0.0.0-stage → 1.0.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/bin/spacez.mjs +398 -0
- package/package.json +14 -5
- package/README.md +0 -3
package/bin/spacez.mjs
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs/promises";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import readline from "node:readline/promises";
|
|
6
|
+
import { parseArgs } from "node:util";
|
|
7
|
+
|
|
8
|
+
const VERSION = "1.1.0";
|
|
9
|
+
const DEFAULT_URL = "https://preview-chat-17bcafa2-f255-4e81-815b-427a4612ada9.space-z.ai";
|
|
10
|
+
const DEFAULT_ID = "spacez";
|
|
11
|
+
const DEFAULT_NAME = "Space-Z";
|
|
12
|
+
const API_PREFIX = "/api/v1";
|
|
13
|
+
|
|
14
|
+
const c = {
|
|
15
|
+
reset: "\x1b[0m",
|
|
16
|
+
bold: "\x1b[1m",
|
|
17
|
+
dim: "\x1b[2m",
|
|
18
|
+
red: "\x1b[31m",
|
|
19
|
+
green: "\x1b[32m",
|
|
20
|
+
yellow: "\x1b[33m",
|
|
21
|
+
cyan: "\x1b[36m",
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const log = {
|
|
25
|
+
info: (msg) => console.log(`${c.cyan}ℹ${c.reset} ${msg}`),
|
|
26
|
+
ok: (msg) => console.log(`${c.green}✔${c.reset} ${msg}`),
|
|
27
|
+
warn: (msg) => console.log(`${c.yellow}⚠${c.reset} ${msg}`),
|
|
28
|
+
err: (msg) => console.error(`${c.red}✖${c.reset} ${msg}`),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function printHelp() {
|
|
32
|
+
console.log(`
|
|
33
|
+
${c.bold}spacez${c.reset} v${VERSION} — Multi-tool AI provider installer
|
|
34
|
+
|
|
35
|
+
${c.bold}USAGE${c.reset}
|
|
36
|
+
spacez <command> [options]
|
|
37
|
+
|
|
38
|
+
${c.bold}COMMANDS${c.reset}
|
|
39
|
+
install Install/update provider config in target tools (default)
|
|
40
|
+
remove Remove provider entries from target tools
|
|
41
|
+
list Show target config file paths and current status
|
|
42
|
+
|
|
43
|
+
${c.bold}OPTIONS${c.reset}
|
|
44
|
+
-u, --url <url> Provider base URL (default: ${DEFAULT_URL})
|
|
45
|
+
-k, --key <key> API key (falls back to SPACEZ_API_KEY or prompt)
|
|
46
|
+
-m, --model <id> Explicit model ID(s), comma-separated
|
|
47
|
+
-t, --targets <list> Comma-separated: opencode,continue,aider,env (default: all)
|
|
48
|
+
--id <id> Provider namespace ID (default: ${DEFAULT_ID})
|
|
49
|
+
--name <name> Display name (default: ${DEFAULT_NAME})
|
|
50
|
+
--no-discover Skip ${API_PREFIX}/models discovery
|
|
51
|
+
-y, --yes Non-interactive mode (do not prompt for missing key)
|
|
52
|
+
-v, --version Show version
|
|
53
|
+
-h, --help Show this help message
|
|
54
|
+
|
|
55
|
+
${c.bold}EXAMPLES${c.reset}
|
|
56
|
+
spacez install --key sk-1234
|
|
57
|
+
spacez install -t opencode,aider -m gpt-4o -y
|
|
58
|
+
spacez remove -t opencode,continue
|
|
59
|
+
`);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function normalizeBaseUrl(rawUrl) {
|
|
63
|
+
const clean = rawUrl.trim().replace(/\/+$/, "");
|
|
64
|
+
if (clean.endsWith(API_PREFIX)) return clean;
|
|
65
|
+
if (clean.endsWith("/v1")) return `${clean.slice(0, -3)}${API_PREFIX}`;
|
|
66
|
+
return `${clean}${API_PREFIX}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getPaths() {
|
|
70
|
+
const home = os.homedir();
|
|
71
|
+
return {
|
|
72
|
+
opencode:
|
|
73
|
+
process.env.OPENCODE_CONFIG ||
|
|
74
|
+
path.join(home, ".config", "opencode", "opencode.json"),
|
|
75
|
+
continue: path.join(home, ".continue", "config.json"),
|
|
76
|
+
aider: path.join(home, ".aider.conf.yml"),
|
|
77
|
+
env: path.resolve(process.cwd(), ".env"),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function promptIfNeeded(opts) {
|
|
82
|
+
if (opts.yes || opts.key || !process.stdin.isTTY) return opts.key;
|
|
83
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
84
|
+
try {
|
|
85
|
+
const answer = await rl.question(
|
|
86
|
+
`${c.cyan}?${c.reset} API Key ${c.dim}(press Enter to skip / use env var)${c.reset}: `
|
|
87
|
+
);
|
|
88
|
+
return answer.trim();
|
|
89
|
+
} finally {
|
|
90
|
+
rl.close();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function discoverModels(baseUrl, apiKey) {
|
|
95
|
+
if (!apiKey) return [];
|
|
96
|
+
const headers = {
|
|
97
|
+
Accept: "application/json",
|
|
98
|
+
Authorization: `Bearer ${apiKey}`,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
const res = await fetch(`${baseUrl}/models`, { headers, signal: AbortSignal.timeout(5000) });
|
|
103
|
+
if (!res.ok) return [];
|
|
104
|
+
const contentType = res.headers.get("content-type") || "";
|
|
105
|
+
if (!contentType.includes("json")) return [];
|
|
106
|
+
|
|
107
|
+
const data = await res.json();
|
|
108
|
+
if (Array.isArray(data?.data)) return data.data.map((m) => m.id).filter(Boolean);
|
|
109
|
+
if (Array.isArray(data?.models)) return data.models.map((m) => m.name || m.model).filter(Boolean);
|
|
110
|
+
} catch {
|
|
111
|
+
// Fall back to default model ID
|
|
112
|
+
}
|
|
113
|
+
return [];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async function readJsonSafe(filePath) {
|
|
117
|
+
try {
|
|
118
|
+
const raw = await fs.readFile(filePath, "utf8");
|
|
119
|
+
const stripped = raw.replace(/^\s*\/\/.*$/gm, "");
|
|
120
|
+
return JSON.parse(stripped);
|
|
121
|
+
} catch (err) {
|
|
122
|
+
if (err.code === "ENOENT") return {};
|
|
123
|
+
throw new Error(`Invalid JSON in ${filePath}: ${err.message}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async function writeJsonPretty(filePath, obj) {
|
|
128
|
+
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
129
|
+
await fs.writeFile(filePath, JSON.stringify(obj, null, 2) + "\n", "utf8");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const installers = {
|
|
133
|
+
async opencode({ id, name, baseUrl, modelIds }) {
|
|
134
|
+
const file = getPaths().opencode;
|
|
135
|
+
const cfg = await readJsonSafe(file);
|
|
136
|
+
cfg.$schema = cfg.$schema || "https://opencode.ai/config.json";
|
|
137
|
+
cfg.provider = cfg.provider || {};
|
|
138
|
+
|
|
139
|
+
const models = {};
|
|
140
|
+
for (const m of modelIds) {
|
|
141
|
+
models[m] = cfg.provider[id]?.models?.[m] || { name: m };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
cfg.provider[id] = {
|
|
145
|
+
...(cfg.provider[id] || {}),
|
|
146
|
+
npm: "@ai-sdk/openai-compatible",
|
|
147
|
+
name,
|
|
148
|
+
options: {
|
|
149
|
+
...(cfg.provider[id]?.options || {}),
|
|
150
|
+
baseURL: baseUrl,
|
|
151
|
+
apiKey: "{env:SPACEZ_API_KEY}",
|
|
152
|
+
},
|
|
153
|
+
models,
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
if (!cfg.model && modelIds[0]) cfg.model = `${id}/${modelIds[0]}`;
|
|
157
|
+
await writeJsonPretty(file, cfg);
|
|
158
|
+
return file;
|
|
159
|
+
},
|
|
160
|
+
|
|
161
|
+
async continue({ name, baseUrl, apiKey, modelIds }) {
|
|
162
|
+
const file = getPaths().continue;
|
|
163
|
+
const cfg = await readJsonSafe(file);
|
|
164
|
+
cfg.models = Array.isArray(cfg.models) ? cfg.models : [];
|
|
165
|
+
|
|
166
|
+
for (const m of modelIds) {
|
|
167
|
+
const title = `${name} (${m})`;
|
|
168
|
+
const entry = {
|
|
169
|
+
title,
|
|
170
|
+
provider: "openai",
|
|
171
|
+
model: m,
|
|
172
|
+
apiBase: baseUrl,
|
|
173
|
+
...(apiKey ? { apiKey } : {}),
|
|
174
|
+
};
|
|
175
|
+
const idx = cfg.models.findIndex((item) => item.title === title);
|
|
176
|
+
if (idx >= 0) cfg.models[idx] = entry;
|
|
177
|
+
else cfg.models.push(entry);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
await writeJsonPretty(file, cfg);
|
|
181
|
+
return file;
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
async aider({ baseUrl, apiKey, modelIds }) {
|
|
185
|
+
const file = getPaths().aider;
|
|
186
|
+
let raw = "";
|
|
187
|
+
try {
|
|
188
|
+
raw = await fs.readFile(file, "utf8");
|
|
189
|
+
} catch (err) {
|
|
190
|
+
if (err.code !== "ENOENT") throw err;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const blockStart = "# >>> spacez-cli >>>";
|
|
194
|
+
const blockEnd = "# <<< spacez-cli <<<";
|
|
195
|
+
const cleaned = raw.replace(new RegExp(`${blockStart}[\\s\\S]*?${blockEnd}\\n?`, "g"), "").trim();
|
|
196
|
+
|
|
197
|
+
const lines = [
|
|
198
|
+
blockStart,
|
|
199
|
+
`openai-api-base: ${baseUrl}`,
|
|
200
|
+
...(apiKey ? [`openai-api-key: ${apiKey}`] : []),
|
|
201
|
+
...(modelIds[0] ? [`model: openai/${modelIds[0]}`] : []),
|
|
202
|
+
blockEnd,
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
const out = (cleaned ? cleaned + "\n\n" : "") + lines.join("\n") + "\n";
|
|
206
|
+
await fs.writeFile(file, out, "utf8");
|
|
207
|
+
return file;
|
|
208
|
+
},
|
|
209
|
+
|
|
210
|
+
async env({ baseUrl, apiKey, modelIds }) {
|
|
211
|
+
const file = getPaths().env;
|
|
212
|
+
let existing = "";
|
|
213
|
+
try {
|
|
214
|
+
existing = await fs.readFile(file, "utf8");
|
|
215
|
+
} catch (err) {
|
|
216
|
+
if (err.code !== "ENOENT") throw err;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const updates = {
|
|
220
|
+
OPENAI_BASE_URL: baseUrl,
|
|
221
|
+
SPACEZ_BASE_URL: baseUrl,
|
|
222
|
+
SPACEZ_MODEL: modelIds[0] || "default",
|
|
223
|
+
...(apiKey ? { OPENAI_API_KEY: apiKey, SPACEZ_API_KEY: apiKey } : {}),
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const lines = existing ? existing.split(/\r?\n/) : [];
|
|
227
|
+
for (const [k, v] of Object.entries(updates)) {
|
|
228
|
+
const idx = lines.findIndex((l) => l.startsWith(`${k}=`));
|
|
229
|
+
if (idx >= 0) lines[idx] = `${k}=${v}`;
|
|
230
|
+
else lines.push(`${k}=${v}`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const out = lines.filter((l, i, a) => !(i === a.length - 1 && l === "")).join("\n") + "\n";
|
|
234
|
+
await fs.writeFile(file, out, "utf8");
|
|
235
|
+
return file;
|
|
236
|
+
},
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
const removers = {
|
|
240
|
+
async opencode({ id }) {
|
|
241
|
+
const file = getPaths().opencode;
|
|
242
|
+
const cfg = await readJsonSafe(file);
|
|
243
|
+
if (cfg.provider?.[id]) {
|
|
244
|
+
delete cfg.provider[id];
|
|
245
|
+
if (cfg.model?.startsWith(`${id}/`)) delete cfg.model;
|
|
246
|
+
await writeJsonPretty(file, cfg);
|
|
247
|
+
}
|
|
248
|
+
return file;
|
|
249
|
+
},
|
|
250
|
+
|
|
251
|
+
async continue({ name }) {
|
|
252
|
+
const file = getPaths().continue;
|
|
253
|
+
const cfg = await readJsonSafe(file);
|
|
254
|
+
if (Array.isArray(cfg.models)) {
|
|
255
|
+
cfg.models = cfg.models.filter((m) => !String(m.title || "").startsWith(`${name} (`));
|
|
256
|
+
await writeJsonPretty(file, cfg);
|
|
257
|
+
}
|
|
258
|
+
return file;
|
|
259
|
+
},
|
|
260
|
+
|
|
261
|
+
async aider() {
|
|
262
|
+
const file = getPaths().aider;
|
|
263
|
+
try {
|
|
264
|
+
const raw = await fs.readFile(file, "utf8");
|
|
265
|
+
const cleaned = raw.replace(/# >>> spacez-cli >>>[\s\S]*?# <<< spacez-cli <<<\n?/g, "").trim();
|
|
266
|
+
await fs.writeFile(file, cleaned ? cleaned + "\n" : "", "utf8");
|
|
267
|
+
} catch (err) {
|
|
268
|
+
if (err.code !== "ENOENT") throw err;
|
|
269
|
+
}
|
|
270
|
+
return file;
|
|
271
|
+
},
|
|
272
|
+
|
|
273
|
+
async env() {
|
|
274
|
+
const file = getPaths().env;
|
|
275
|
+
try {
|
|
276
|
+
const raw = await fs.readFile(file, "utf8");
|
|
277
|
+
const keys = new Set(["OPENAI_BASE_URL", "SPACEZ_BASE_URL", "SPACEZ_MODEL", "OPENAI_API_KEY", "SPACEZ_API_KEY"]);
|
|
278
|
+
const filtered = raw
|
|
279
|
+
.split(/\r?\n/)
|
|
280
|
+
.filter((l) => !keys.has(l.split("=")[0]));
|
|
281
|
+
await fs.writeFile(file, filtered.join("\n").trim() + "\n", "utf8");
|
|
282
|
+
} catch (err) {
|
|
283
|
+
if (err.code !== "ENOENT") throw err;
|
|
284
|
+
}
|
|
285
|
+
return file;
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
async function cmdInstall(opts) {
|
|
290
|
+
const baseUrl = normalizeBaseUrl(opts.url);
|
|
291
|
+
const apiKey = await promptIfNeeded(opts);
|
|
292
|
+
|
|
293
|
+
log.info(`Endpoint: ${c.bold}${baseUrl}${c.reset}`);
|
|
294
|
+
let discovered = [];
|
|
295
|
+
if (!opts["no-discover"] && !opts.model && apiKey) {
|
|
296
|
+
process.stdout.write(`${c.cyan}ℹ${c.reset} Probing ${API_PREFIX}/models... `);
|
|
297
|
+
discovered = await discoverModels(baseUrl, apiKey);
|
|
298
|
+
console.log(discovered.length ? `${c.green}found ${discovered.length}${c.reset}` : `${c.yellow}none exposed${c.reset}`);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const modelIds = opts.model
|
|
302
|
+
? opts.model.split(",").map((s) => s.trim()).filter(Boolean)
|
|
303
|
+
: discovered.length
|
|
304
|
+
? discovered
|
|
305
|
+
: ["default"];
|
|
306
|
+
|
|
307
|
+
if (!discovered.length && !opts.model) {
|
|
308
|
+
log.warn(`Using fallback model ID "${c.bold}default${c.reset}" (pass -m <id> to set a specific model).`);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
const targets = opts.targets.split(",").map((t) => t.trim().toLowerCase()).filter(Boolean);
|
|
312
|
+
for (const target of targets) {
|
|
313
|
+
if (!installers[target]) {
|
|
314
|
+
log.warn(`Unknown target "${target}" — skipped`);
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const writtenPath = await installers[target]({
|
|
318
|
+
id: opts.id,
|
|
319
|
+
name: opts.name,
|
|
320
|
+
baseUrl,
|
|
321
|
+
apiKey,
|
|
322
|
+
modelIds,
|
|
323
|
+
});
|
|
324
|
+
log.ok(`${target.padEnd(9)} -> ${c.dim}${writtenPath}${c.reset}`);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (!apiKey) {
|
|
328
|
+
log.warn(`No API key passed. Export ${c.bold}SPACEZ_API_KEY${c.reset} in your shell before running OpenCode.`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
async function cmdRemove(opts) {
|
|
333
|
+
const targets = opts.targets.split(",").map((t) => t.trim().toLowerCase()).filter(Boolean);
|
|
334
|
+
for (const target of targets) {
|
|
335
|
+
if (!removers[target]) {
|
|
336
|
+
log.warn(`Unknown target "${target}" — skipped`);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const p = await removers[target]({ id: opts.id, name: opts.name });
|
|
340
|
+
log.ok(`Removed from ${target} (${c.dim}${p}${c.reset})`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async function cmdList() {
|
|
345
|
+
const paths = getPaths();
|
|
346
|
+
console.log(`${c.bold}Target config locations:${c.reset}`);
|
|
347
|
+
for (const [tool, file] of Object.entries(paths)) {
|
|
348
|
+
let exists = false;
|
|
349
|
+
try {
|
|
350
|
+
await fs.access(file);
|
|
351
|
+
exists = true;
|
|
352
|
+
} catch {}
|
|
353
|
+
const status = exists ? `${c.green}exists${c.reset}` : `${c.dim}not created${c.reset}`;
|
|
354
|
+
console.log(` ${tool.padEnd(10)} ${status.padEnd(20)} ${file}`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
async function main() {
|
|
359
|
+
const { values, positionals } = parseArgs({
|
|
360
|
+
allowPositionals: true,
|
|
361
|
+
options: {
|
|
362
|
+
url: { type: "string", short: "u", default: DEFAULT_URL },
|
|
363
|
+
key: { type: "string", short: "k", default: process.env.SPACEZ_API_KEY || "" },
|
|
364
|
+
model: { type: "string", short: "m", default: "" },
|
|
365
|
+
targets: { type: "string", short: "t", default: "opencode,continue,aider,env" },
|
|
366
|
+
id: { type: "string", default: DEFAULT_ID },
|
|
367
|
+
name: { type: "string", default: DEFAULT_NAME },
|
|
368
|
+
"no-discover": { type: "boolean", default: false },
|
|
369
|
+
yes: { type: "boolean", short: "y", default: false },
|
|
370
|
+
version: { type: "boolean", short: "v", default: false },
|
|
371
|
+
help: { type: "boolean", short: "h", default: false },
|
|
372
|
+
},
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
if (values.version) {
|
|
376
|
+
console.log(VERSION);
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
if (values.help) {
|
|
380
|
+
printHelp();
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const command = (positionals[0] || "install").toLowerCase();
|
|
385
|
+
if (command === "install") await cmdInstall(values);
|
|
386
|
+
else if (command === "remove" || command === "uninstall") await cmdRemove(values);
|
|
387
|
+
else if (command === "list" || command === "status") await cmdList();
|
|
388
|
+
else {
|
|
389
|
+
log.err(`Unknown command: ${command}`);
|
|
390
|
+
printHelp();
|
|
391
|
+
process.exitCode = 1;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
main().catch((err) => {
|
|
396
|
+
log.err(err.message);
|
|
397
|
+
process.exit(1);
|
|
398
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@breadbars/spacez-cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
1
|
+
{
|
|
2
|
+
"name": "@breadbars/spacez-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Configure Space-Z custom API provider across AI coding tools",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"spacez": "./bin/spacez.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin/"
|
|
11
|
+
],
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18.0.0"
|
|
14
|
+
}
|
|
6
15
|
}
|
package/README.md
DELETED