@lunaroute/cli 0.4.0 → 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/dist/index.js +190 -45
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { Command, InvalidArgumentError, Option } from "commander";
|
|
5
5
|
|
|
6
6
|
// package.json
|
|
7
|
-
var version = "0.
|
|
7
|
+
var version = "0.5.0";
|
|
8
8
|
|
|
9
9
|
// src/login.ts
|
|
10
10
|
import { createServer } from "http";
|
|
@@ -236,6 +236,34 @@ function resolveSettings(name) {
|
|
|
236
236
|
};
|
|
237
237
|
}
|
|
238
238
|
|
|
239
|
+
// src/pending.ts
|
|
240
|
+
import { chmodSync as chmodSync2, existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync as readFileSync2, rmSync, writeFileSync as writeFileSync2 } from "fs";
|
|
241
|
+
import { join as join2 } from "path";
|
|
242
|
+
function pendingPath(profile) {
|
|
243
|
+
const safe = profile.replace(/[^A-Za-z0-9_-]/g, "_");
|
|
244
|
+
return join2(configDir(), `pending-login-${safe}.json`);
|
|
245
|
+
}
|
|
246
|
+
function savePending(profile, p) {
|
|
247
|
+
mkdirSync2(configDir(), { recursive: true, mode: 448 });
|
|
248
|
+
const path = pendingPath(profile);
|
|
249
|
+
writeFileSync2(path, JSON.stringify(p, null, 2), { mode: 384 });
|
|
250
|
+
chmodSync2(path, 384);
|
|
251
|
+
}
|
|
252
|
+
function loadPending(profile) {
|
|
253
|
+
const path = pendingPath(profile);
|
|
254
|
+
if (!existsSync2(path)) return null;
|
|
255
|
+
try {
|
|
256
|
+
const parsed = JSON.parse(readFileSync2(path, "utf8"));
|
|
257
|
+
if (!parsed?.verifier || !parsed?.challenge || !parsed?.api_url) return null;
|
|
258
|
+
return parsed;
|
|
259
|
+
} catch {
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function clearPending(profile) {
|
|
264
|
+
rmSync(pendingPath(profile), { force: true });
|
|
265
|
+
}
|
|
266
|
+
|
|
239
267
|
// src/login.ts
|
|
240
268
|
var LOGIN_TIMEOUT_MS = 3 * 6e4;
|
|
241
269
|
async function waitWithTimeout(server, ms) {
|
|
@@ -324,14 +352,80 @@ async function runLogin(profile) {
|
|
|
324
352
|
\u2713 Logged in as ${result.user_email} (org ${result.org_id}).`);
|
|
325
353
|
console.log(` Routing key saved to profile "${profile}".`);
|
|
326
354
|
}
|
|
355
|
+
async function runLoginStart(profile) {
|
|
356
|
+
const settings = resolveSettings(profile);
|
|
357
|
+
const state = genState();
|
|
358
|
+
const verifier = genVerifier();
|
|
359
|
+
const challenge = challengeFor(verifier);
|
|
360
|
+
savePending(profile, {
|
|
361
|
+
verifier,
|
|
362
|
+
challenge,
|
|
363
|
+
state,
|
|
364
|
+
api_url: settings.api_url
|
|
365
|
+
});
|
|
366
|
+
const url = `${settings.front_url}/cli-auth?challenge=${challenge}&state=${state}&mode=code`;
|
|
367
|
+
console.log("Open this URL on any device to authorize:\n " + url + "\n");
|
|
368
|
+
console.log("Then paste the code it shows back here with:");
|
|
369
|
+
console.log(" lunaroute login --finish <code>");
|
|
370
|
+
}
|
|
371
|
+
async function runLoginFinish(profile, code) {
|
|
372
|
+
const pending = loadPending(profile);
|
|
373
|
+
if (!pending) {
|
|
374
|
+
throw new Error(
|
|
375
|
+
"no pending login for this profile \u2014 run `lunaroute login --start` first"
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
const settings = resolveSettings(profile);
|
|
379
|
+
let result;
|
|
380
|
+
try {
|
|
381
|
+
result = await exchangeCode(pending.api_url, {
|
|
382
|
+
code,
|
|
383
|
+
verifier: pending.verifier,
|
|
384
|
+
label: hostname()
|
|
385
|
+
});
|
|
386
|
+
} catch (err) {
|
|
387
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
388
|
+
if (/INVALID_CODE|PKCE_MISMATCH/.test(msg)) clearPending(profile);
|
|
389
|
+
throw err;
|
|
390
|
+
}
|
|
391
|
+
saveProfile(profile, {
|
|
392
|
+
api_url: pending.api_url,
|
|
393
|
+
routing_url: settings.routing_url,
|
|
394
|
+
front_url: settings.front_url,
|
|
395
|
+
org_id: result.org_id,
|
|
396
|
+
routing_key: result.full_key,
|
|
397
|
+
user_email: result.user_email
|
|
398
|
+
});
|
|
399
|
+
clearPending(profile);
|
|
400
|
+
console.log(`
|
|
401
|
+
\u2713 Logged in as ${result.user_email} (org ${result.org_id}).`);
|
|
402
|
+
console.log(` Routing key saved to profile "${profile}".`);
|
|
403
|
+
}
|
|
327
404
|
|
|
328
405
|
// src/commands/login.ts
|
|
406
|
+
function report(err) {
|
|
407
|
+
console.error(`Login failed: ${err.message}`);
|
|
408
|
+
process.exitCode = 1;
|
|
409
|
+
}
|
|
329
410
|
async function login(profile) {
|
|
330
411
|
try {
|
|
331
412
|
await runLogin(profile);
|
|
332
413
|
} catch (err) {
|
|
333
|
-
|
|
334
|
-
|
|
414
|
+
report(err);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
async function loginStart(profile) {
|
|
418
|
+
try {
|
|
419
|
+
await runLoginStart(profile);
|
|
420
|
+
} catch (err) {
|
|
421
|
+
report(err);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
async function loginFinish(profile, code) {
|
|
425
|
+
try {
|
|
426
|
+
await runLoginFinish(profile, code);
|
|
427
|
+
} catch (err) {
|
|
428
|
+
report(err);
|
|
335
429
|
}
|
|
336
430
|
}
|
|
337
431
|
|
|
@@ -401,18 +495,18 @@ async function fetchModels(routingUrl, key, opts) {
|
|
|
401
495
|
|
|
402
496
|
// src/setup/apply.ts
|
|
403
497
|
import {
|
|
404
|
-
mkdirSync as
|
|
405
|
-
readFileSync as
|
|
406
|
-
writeFileSync as
|
|
498
|
+
mkdirSync as mkdirSync3,
|
|
499
|
+
readFileSync as readFileSync3,
|
|
500
|
+
writeFileSync as writeFileSync3,
|
|
407
501
|
copyFileSync,
|
|
408
|
-
existsSync as
|
|
409
|
-
chmodSync as
|
|
502
|
+
existsSync as existsSync3,
|
|
503
|
+
chmodSync as chmodSync3
|
|
410
504
|
} from "fs";
|
|
411
505
|
import { dirname } from "path";
|
|
412
506
|
import { parseDocument } from "yaml";
|
|
413
507
|
function readExistingJson(path) {
|
|
414
|
-
if (!
|
|
415
|
-
const raw =
|
|
508
|
+
if (!existsSync3(path)) return null;
|
|
509
|
+
const raw = readFileSync3(path, "utf8").trim();
|
|
416
510
|
if (raw === "") return null;
|
|
417
511
|
try {
|
|
418
512
|
return JSON.parse(raw);
|
|
@@ -423,7 +517,7 @@ function readExistingJson(path) {
|
|
|
423
517
|
}
|
|
424
518
|
}
|
|
425
519
|
function resolveYamlContent(fw) {
|
|
426
|
-
const curStr =
|
|
520
|
+
const curStr = existsSync3(fw.path) ? readFileSync3(fw.path, "utf8") : null;
|
|
427
521
|
const doc = parseDocument(curStr ?? "", { logLevel: "silent" });
|
|
428
522
|
if (doc.errors.length > 0) {
|
|
429
523
|
const first = doc.errors[0];
|
|
@@ -439,7 +533,7 @@ function resolveYamlContent(fw) {
|
|
|
439
533
|
}
|
|
440
534
|
function resolveContent(fw) {
|
|
441
535
|
if (fw.kind === "text") {
|
|
442
|
-
const curStr2 =
|
|
536
|
+
const curStr2 = existsSync3(fw.path) ? readFileSync3(fw.path, "utf8") : null;
|
|
443
537
|
return { nextStr: fw.content, curStr: curStr2 };
|
|
444
538
|
}
|
|
445
539
|
if (fw.kind === "yaml") return resolveYamlContent(fw);
|
|
@@ -466,18 +560,26 @@ ${nextStr}`);
|
|
|
466
560
|
summary.backedUp.push(`${fw.path}.bak`);
|
|
467
561
|
}
|
|
468
562
|
const dir = dirname(fw.path);
|
|
469
|
-
const dirExisted =
|
|
470
|
-
|
|
471
|
-
if (!dirExisted)
|
|
472
|
-
|
|
473
|
-
|
|
563
|
+
const dirExisted = existsSync3(dir);
|
|
564
|
+
mkdirSync3(dir, { recursive: true });
|
|
565
|
+
if (!dirExisted) chmodSync3(dir, fw.dirMode ?? 448);
|
|
566
|
+
writeFileSync3(fw.path, nextStr, { mode: fw.fileMode ?? 384 });
|
|
567
|
+
chmodSync3(fw.path, fw.fileMode ?? 384);
|
|
474
568
|
summary.written.push(fw.path);
|
|
475
569
|
}
|
|
476
570
|
if (plan.exports.length > 0) {
|
|
477
|
-
|
|
571
|
+
const printed = plan.exports.filter((e) => !process.env[e.name]);
|
|
572
|
+
if (printed.length > 0) {
|
|
573
|
+
console.log("\n# Add these to your shell profile:");
|
|
574
|
+
for (const e of printed) {
|
|
575
|
+
const value = e.value === "__KEY__" ? opts.key : e.value;
|
|
576
|
+
console.log(`export ${e.name}=${value}`);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
478
579
|
for (const e of plan.exports) {
|
|
479
|
-
|
|
480
|
-
|
|
580
|
+
if (process.env[e.name]) {
|
|
581
|
+
console.log(`# ${e.name} is already set in your environment \u2014 not printing its value.`);
|
|
582
|
+
}
|
|
481
583
|
}
|
|
482
584
|
}
|
|
483
585
|
for (const note of plan.notes) console.log(note);
|
|
@@ -601,51 +703,51 @@ function describeSpawnError(command, installHint, err) {
|
|
|
601
703
|
// src/setup/paths.ts
|
|
602
704
|
import { execSync } from "child_process";
|
|
603
705
|
import { homedir as homedir2 } from "os";
|
|
604
|
-
import { join as
|
|
706
|
+
import { join as join3 } from "path";
|
|
605
707
|
function configHome() {
|
|
606
|
-
return process.env.XDG_CONFIG_HOME ||
|
|
708
|
+
return process.env.XDG_CONFIG_HOME || join3(homedir2(), ".config");
|
|
607
709
|
}
|
|
608
710
|
function opencodeConfigPath() {
|
|
609
|
-
return
|
|
711
|
+
return join3(configHome(), "opencode", "opencode.json");
|
|
610
712
|
}
|
|
611
713
|
function opencodeAuthPath() {
|
|
612
|
-
const dataHome = process.env.XDG_DATA_HOME ||
|
|
613
|
-
return
|
|
714
|
+
const dataHome = process.env.XDG_DATA_HOME || join3(homedir2(), ".local", "share");
|
|
715
|
+
return join3(dataHome, "opencode", "auth.json");
|
|
614
716
|
}
|
|
615
717
|
function openclawConfigPath() {
|
|
616
|
-
return
|
|
718
|
+
return join3(homedir2(), ".openclaw", "openclaw.json");
|
|
617
719
|
}
|
|
618
720
|
function codexProfilePath() {
|
|
619
|
-
const home = process.env.CODEX_HOME ||
|
|
620
|
-
return
|
|
721
|
+
const home = process.env.CODEX_HOME || join3(homedir2(), ".codex");
|
|
722
|
+
return join3(home, "lunaroute.config.toml");
|
|
621
723
|
}
|
|
622
724
|
function piModelsPath() {
|
|
623
|
-
return
|
|
725
|
+
return join3(homedir2(), ".pi", "agent", "models.json");
|
|
624
726
|
}
|
|
625
727
|
function piAuthPath() {
|
|
626
|
-
return
|
|
728
|
+
return join3(homedir2(), ".pi", "agent", "auth.json");
|
|
627
729
|
}
|
|
628
730
|
function dshHome() {
|
|
629
|
-
return process.env.DSH_HOME ||
|
|
731
|
+
return process.env.DSH_HOME || join3(homedir2(), ".dsh");
|
|
630
732
|
}
|
|
631
733
|
function dshProfilePatchPath(profile = "web") {
|
|
632
|
-
return
|
|
734
|
+
return join3(dshHome(), "profiles", profile, "cordis.patch.yml");
|
|
633
735
|
}
|
|
634
736
|
function dshHomePatchPath() {
|
|
635
|
-
return
|
|
737
|
+
return join3(dshHome(), "cordis.patch.yml");
|
|
636
738
|
}
|
|
637
739
|
var LUNAROUTE_SKILL_NAME = "lunaroute-memory";
|
|
638
740
|
function claudeUserConfigPath() {
|
|
639
|
-
return
|
|
741
|
+
return join3(homedir2(), ".claude.json");
|
|
640
742
|
}
|
|
641
743
|
function claudeUserSkillPath(name = LUNAROUTE_SKILL_NAME) {
|
|
642
|
-
return
|
|
744
|
+
return join3(homedir2(), ".claude", "skills", name, "SKILL.md");
|
|
643
745
|
}
|
|
644
746
|
function claudeProjectMcpPath(root) {
|
|
645
|
-
return
|
|
747
|
+
return join3(root, ".mcp.json");
|
|
646
748
|
}
|
|
647
749
|
function claudeProjectSkillPath(root, name = LUNAROUTE_SKILL_NAME) {
|
|
648
|
-
return
|
|
750
|
+
return join3(root, ".claude", "skills", name, "SKILL.md");
|
|
649
751
|
}
|
|
650
752
|
function gitRepoRoot(cwd = process.cwd()) {
|
|
651
753
|
try {
|
|
@@ -763,7 +865,7 @@ function buildKeyPlan(key) {
|
|
|
763
865
|
}
|
|
764
866
|
|
|
765
867
|
// src/setup/adapters/pi.ts
|
|
766
|
-
import { readFileSync as
|
|
868
|
+
import { readFileSync as readFileSync4 } from "fs";
|
|
767
869
|
function buildPiModelEntry(m) {
|
|
768
870
|
if (!m.context_window_tokens || !m.max_output_tokens) return null;
|
|
769
871
|
const reasoning = m.capabilities?.reasoning === true;
|
|
@@ -837,7 +939,7 @@ function buildPlan2(ctx) {
|
|
|
837
939
|
}
|
|
838
940
|
function stalePiProviderExists() {
|
|
839
941
|
try {
|
|
840
|
-
const obj = JSON.parse(
|
|
942
|
+
const obj = JSON.parse(readFileSync4(piModelsPath(), "utf8"));
|
|
841
943
|
return Boolean(obj.providers?.lunaroute);
|
|
842
944
|
} catch {
|
|
843
945
|
return false;
|
|
@@ -1074,13 +1176,30 @@ openclaw: restart the gateway, then \`openclaw models list\` and \`openclaw mode
|
|
|
1074
1176
|
}
|
|
1075
1177
|
|
|
1076
1178
|
// src/setup/adapters/dsh.ts
|
|
1077
|
-
import { existsSync as
|
|
1179
|
+
import { existsSync as existsSync4, readFileSync as readFileSync5 } from "fs";
|
|
1078
1180
|
import { isMap, isSeq, parseDocument as parseDocument2 } from "yaml";
|
|
1079
1181
|
var PI_AI_ROW_ID = "llm-pi-ai";
|
|
1080
1182
|
var ROUTE_KEY = "lunaroute";
|
|
1081
1183
|
function asRecord2(v) {
|
|
1082
1184
|
return typeof v === "object" && v !== null && !Array.isArray(v) ? v : {};
|
|
1083
1185
|
}
|
|
1186
|
+
var DSH_THINKING_FORMATS = /* @__PURE__ */ new Set([
|
|
1187
|
+
"openai",
|
|
1188
|
+
"deepseek",
|
|
1189
|
+
"openrouter",
|
|
1190
|
+
"together",
|
|
1191
|
+
"baseten",
|
|
1192
|
+
"zai",
|
|
1193
|
+
"qwen",
|
|
1194
|
+
"chat-template",
|
|
1195
|
+
"qwen-chat-template",
|
|
1196
|
+
"string-thinking",
|
|
1197
|
+
"ant-ling"
|
|
1198
|
+
]);
|
|
1199
|
+
function thinkingFormatOf(m) {
|
|
1200
|
+
const pi = asRecord2(m.client_compat?.pi);
|
|
1201
|
+
return typeof pi.thinkingFormat === "string" ? pi.thinkingFormat : void 0;
|
|
1202
|
+
}
|
|
1084
1203
|
function buildReasoning(m) {
|
|
1085
1204
|
if (m.capabilities?.reasoning !== true) return null;
|
|
1086
1205
|
const pi = asRecord2(m.client_compat?.pi);
|
|
@@ -1094,7 +1213,9 @@ function buildReasoning(m) {
|
|
|
1094
1213
|
}
|
|
1095
1214
|
if (Object.keys(efforts).length < 2) return null;
|
|
1096
1215
|
const compat = {};
|
|
1097
|
-
if (typeof pi.thinkingFormat === "string"
|
|
1216
|
+
if (typeof pi.thinkingFormat === "string" && DSH_THINKING_FORMATS.has(pi.thinkingFormat)) {
|
|
1217
|
+
compat.thinkingFormat = pi.thinkingFormat;
|
|
1218
|
+
}
|
|
1098
1219
|
if (typeof pi.maxTokensField === "string") compat.maxTokensField = pi.maxTokensField;
|
|
1099
1220
|
if (typeof pi.supportsReasoningEffort === "boolean") {
|
|
1100
1221
|
compat.supportsReasoningEffort = pi.supportsReasoningEffort;
|
|
@@ -1147,9 +1268,9 @@ function mergeProvider(doc, provider) {
|
|
|
1147
1268
|
}
|
|
1148
1269
|
function homePatchOverridesPiAi() {
|
|
1149
1270
|
const path = dshHomePatchPath();
|
|
1150
|
-
if (!
|
|
1271
|
+
if (!existsSync4(path)) return false;
|
|
1151
1272
|
try {
|
|
1152
|
-
const doc = parseDocument2(
|
|
1273
|
+
const doc = parseDocument2(readFileSync5(path, "utf8"), { logLevel: "silent" });
|
|
1153
1274
|
if (!isSeq(doc.contents)) return false;
|
|
1154
1275
|
return doc.contents.items.some(
|
|
1155
1276
|
(item) => isMap(item) && item.get("id") === PI_AI_ROW_ID
|
|
@@ -1176,6 +1297,16 @@ function buildPlan8(ctx) {
|
|
|
1176
1297
|
"The key rides `Authorization: Bearer lr_\u2026` via apiKeyEnv; LunaRoute authenticates it at the edge and never forwards it upstream.",
|
|
1177
1298
|
"Or paste the key in Settings \u2192 Models instead \u2014 dsh stores it in $DSH_HOME/.credentials.yaml."
|
|
1178
1299
|
];
|
|
1300
|
+
const unsupportedFormats = [
|
|
1301
|
+
...new Set(
|
|
1302
|
+
ctx.models.map(thinkingFormatOf).filter((f) => f !== void 0 && !DSH_THINKING_FORMATS.has(f))
|
|
1303
|
+
)
|
|
1304
|
+
];
|
|
1305
|
+
if (unsupportedFormats.length > 0) {
|
|
1306
|
+
notes.push(
|
|
1307
|
+
`dsh does not accept thinkingFormat value(s): ${unsupportedFormats.join(", ")} \u2014 omitted from the config. Fix client_compat.pi.thinkingFormat in the catalog.`
|
|
1308
|
+
);
|
|
1309
|
+
}
|
|
1179
1310
|
if (homePatchOverridesPiAi()) {
|
|
1180
1311
|
notes.push(
|
|
1181
1312
|
`WARNING: $DSH_HOME/cordis.patch.yml also has an \`llm-pi-ai\` row. It is a later config layer, so it REPLACES the row setup just wrote \u2014 remove it, or move the lunaroute provider there.`
|
|
@@ -1322,6 +1453,11 @@ var OPENCODE_LOGIN_HINT = "Or re-run and choose the extension \u2014 it logs in
|
|
|
1322
1453
|
var PI_LOGIN_HINT = "Or re-run and choose the extension \u2014 login happens via /login lunaroute inside pi.";
|
|
1323
1454
|
async function requireCreds(opts, deps, stored, hint) {
|
|
1324
1455
|
if (stored?.routing_key) return stored;
|
|
1456
|
+
const envKey = process.env.LUNAROUTE_API_KEY?.trim();
|
|
1457
|
+
if (envKey) {
|
|
1458
|
+
const settings = resolveSettings(opts.profile);
|
|
1459
|
+
return { ...settings, routing_key: envKey };
|
|
1460
|
+
}
|
|
1325
1461
|
const decline = [NOT_LOGGED_IN, hint].filter(Boolean).join(" ");
|
|
1326
1462
|
try {
|
|
1327
1463
|
const ok = await deps.confirm('Not logged in. Run "lunaroute login" now? (opens your browser)');
|
|
@@ -2365,8 +2501,17 @@ if (nodeMajor < MIN_NODE_MAJOR) {
|
|
|
2365
2501
|
}
|
|
2366
2502
|
var program = new Command();
|
|
2367
2503
|
program.name("lunaroute").description("LunaRoute CLI \u2014 configure coding harnesses and manage your account.").version(version).option("-p, --profile <name>", "credential profile to use", "default");
|
|
2368
|
-
program.command("login").description("Authorize this device via the browser and store a routing key.").action(async () => {
|
|
2369
|
-
|
|
2504
|
+
program.command("login").description("Authorize this device via the browser and store a routing key.").option("--start", "headless: print an authorization URL and exit (paste the code back with --finish)", false).option("--finish <code>", "headless: exchange the code shown by --start for a routing key").action(async (opts) => {
|
|
2505
|
+
const profile = program.opts().profile;
|
|
2506
|
+
if (opts.finish) {
|
|
2507
|
+
await loginFinish(profile, opts.finish);
|
|
2508
|
+
return;
|
|
2509
|
+
}
|
|
2510
|
+
if (opts.start) {
|
|
2511
|
+
await loginStart(profile);
|
|
2512
|
+
return;
|
|
2513
|
+
}
|
|
2514
|
+
await login(profile);
|
|
2370
2515
|
});
|
|
2371
2516
|
program.command("whoami").description("Show the signed-in user and organization.").action(() => {
|
|
2372
2517
|
whoami(program.opts().profile);
|