@lunaroute/cli 0.5.0 → 0.6.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.
Files changed (2) hide show
  1. package/dist/index.js +80 -37
  2. 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.5.0";
7
+ var version = "0.6.0";
8
8
 
9
9
  // src/login.ts
10
10
  import { createServer } from "http";
@@ -442,6 +442,17 @@ function whoami(profile) {
442
442
  console.log(` API: ${creds.api_url}`);
443
443
  }
444
444
 
445
+ // src/commands/key.ts
446
+ function key(profile) {
447
+ const creds = loadProfile(profile);
448
+ if (!creds?.routing_key) {
449
+ console.error('Not logged in. Run "lunaroute login" to get started.');
450
+ process.exitCode = 1;
451
+ return;
452
+ }
453
+ console.log(creds.routing_key);
454
+ }
455
+
445
456
  // src/commands/logout.ts
446
457
  function logout(profile) {
447
458
  if (!loadProfile(profile)) {
@@ -466,14 +477,14 @@ function isChatModel(m) {
466
477
  }
467
478
  return true;
468
479
  }
469
- async function fetchModels(routingUrl, key, opts) {
480
+ async function fetchModels(routingUrl, key2, opts) {
470
481
  const url = `${routingUrl}/v1/models`;
471
- const headers = key ? { Authorization: `Bearer ${key}` } : {};
482
+ const headers = key2 ? { Authorization: `Bearer ${key2}` } : {};
472
483
  const res = await fetch(url, { method: "GET", headers, signal: opts?.signal });
473
484
  if (!res.ok) {
474
485
  if (res.status === 401) {
475
486
  throw new KeyRejectedError(
476
- key ? `the routing service rejected that key (HTTP 401) \u2014 check that you copied the whole lr_ key` : `the routing service requires a key for /v1/models (HTTP 401) \u2014 run "lunaroute login" or pass --key`
487
+ key2 ? `the routing service rejected that key (HTTP 401) \u2014 check that you copied the whole lr_ key` : `the routing service requires a key for /v1/models (HTTP 401) \u2014 run "lunaroute login" or pass --key`
477
488
  );
478
489
  }
479
490
  throw new Error(`failed to fetch models: HTTP ${res.status} from ${url}`);
@@ -504,6 +515,13 @@ import {
504
515
  } from "fs";
505
516
  import { dirname } from "path";
506
517
  import { parseDocument } from "yaml";
518
+
519
+ // src/shell.ts
520
+ function shQuote(value) {
521
+ return `'${value.replace(/'/g, "'\\''")}'`;
522
+ }
523
+
524
+ // src/setup/apply.ts
507
525
  function readExistingJson(path) {
508
526
  if (!existsSync3(path)) return null;
509
527
  const raw = readFileSync3(path, "utf8").trim();
@@ -571,9 +589,23 @@ ${nextStr}`);
571
589
  const printed = plan.exports.filter((e) => !process.env[e.name]);
572
590
  if (printed.length > 0) {
573
591
  console.log("\n# Add these to your shell profile:");
592
+ let referencedKey = false;
574
593
  for (const e of printed) {
575
- const value = e.value === "__KEY__" ? opts.key : e.value;
576
- console.log(`export ${e.name}=${value}`);
594
+ if (e.value === "__KEY__") {
595
+ referencedKey = true;
596
+ const profileArg = opts.profile && opts.profile !== "default" ? ` -p ${shQuote(opts.profile)}` : "";
597
+ console.log(`export ${e.name}="$(lunaroute key${profileArg})"`);
598
+ } else {
599
+ console.log(`export ${e.name}=${e.value}`);
600
+ }
601
+ }
602
+ if (referencedKey) {
603
+ console.log(
604
+ "# The line above shells out to 'lunaroute key' \u2014 install the CLI first: npm install -g @lunaroute/cli"
605
+ );
606
+ console.log(
607
+ "# For non-shell config (systemd Environment=, docker .env), run 'lunaroute key' once and paste the value."
608
+ );
577
609
  }
578
610
  }
579
611
  for (const e of plan.exports) {
@@ -844,7 +876,7 @@ function providerPlan(ctx) {
844
876
  ]
845
877
  };
846
878
  }
847
- function buildKeyPlan(key) {
879
+ function buildKeyPlan(key2) {
848
880
  return {
849
881
  fileWrites: [
850
882
  {
@@ -853,7 +885,7 @@ function buildKeyPlan(key) {
853
885
  merge: (existing) => {
854
886
  const obj = existing ?? {};
855
887
  delete obj["lunaroute/"];
856
- return { ...obj, lunaroute: { type: "api", key } };
888
+ return { ...obj, lunaroute: { type: "api", key: key2 } };
857
889
  }
858
890
  }
859
891
  ],
@@ -969,7 +1001,7 @@ function buildStaleProviderCleanupPlan() {
969
1001
  };
970
1002
  }
971
1003
  var KEY_EXPIRES_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
972
- function buildKeyPlan2(key) {
1004
+ function buildKeyPlan2(key2) {
973
1005
  return {
974
1006
  fileWrites: [
975
1007
  {
@@ -979,7 +1011,7 @@ function buildKeyPlan2(key) {
979
1011
  const obj = existing ?? {};
980
1012
  return {
981
1013
  ...obj,
982
- lunaroute: { type: "oauth", access: key, refresh: "", expires: Date.now() + KEY_EXPIRES_MS }
1014
+ lunaroute: { type: "oauth", access: key2, refresh: "", expires: Date.now() + KEY_EXPIRES_MS }
983
1015
  };
984
1016
  }
985
1017
  }
@@ -1253,11 +1285,11 @@ function ensureProviderRow(doc) {
1253
1285
  seq.add(row);
1254
1286
  return row;
1255
1287
  }
1256
- function ensureMap(doc, parent, key) {
1257
- const existing = parent.get(key);
1288
+ function ensureMap(doc, parent, key2) {
1289
+ const existing = parent.get(key2);
1258
1290
  if (isMap(existing)) return existing;
1259
1291
  const created = doc.createNode({});
1260
- parent.set(key, created);
1292
+ parent.set(key2, created);
1261
1293
  return created;
1262
1294
  }
1263
1295
  function mergeProvider(doc, provider) {
@@ -1435,7 +1467,7 @@ async function runSetup(harness, opts, deps = {
1435
1467
  return 1;
1436
1468
  }
1437
1469
  try {
1438
- const summary = await applyPlan(plan, { print: opts.print, key: creds.routing_key });
1470
+ const summary = await applyPlan(plan, { print: opts.print, key: creds.routing_key, profile: opts.profile });
1439
1471
  if (!opts.print && summary.written.length > 0) {
1440
1472
  console.log(`
1441
1473
  Wrote: ${summary.written.join(", ")}`);
@@ -1498,7 +1530,8 @@ async function runPiSetup(opts, stored, routingUrl, deps) {
1498
1530
  return applyPiModels(
1499
1531
  creds2,
1500
1532
  routingUrl,
1501
- await deps.confirm("Merge LunaRoute provider into ~/.pi/agent/models.json (backup kept, other providers preserved)?", { yes: opts.yes })
1533
+ await deps.confirm("Merge LunaRoute provider into ~/.pi/agent/models.json (backup kept, other providers preserved)?", { yes: opts.yes }),
1534
+ opts.profile
1502
1535
  );
1503
1536
  }
1504
1537
  const code = await installPiPackages(deps);
@@ -1512,7 +1545,7 @@ async function runPiSetup(opts, stored, routingUrl, deps) {
1512
1545
  return 0;
1513
1546
  }
1514
1547
  const plan = combinePlans(buildKeyPlan2(creds2.routing_key), stalePiProviderExists() ? buildStaleProviderCleanupPlan() : null);
1515
- const wcode = await applyAndReport(plan, creds2.routing_key, true);
1548
+ const wcode = await applyAndReport(plan, creds2.routing_key, true, opts.profile);
1516
1549
  if (wcode !== 0) return wcode;
1517
1550
  console.log("\nDone. Start pi \u2014 it is signed in. Pick a model with /model.");
1518
1551
  return 0;
@@ -1535,7 +1568,8 @@ async function runPiSetup(opts, stored, routingUrl, deps) {
1535
1568
  return applyPiModels(
1536
1569
  creds2,
1537
1570
  routingUrl,
1538
- await deps.confirm("Merge LunaRoute provider into ~/.pi/agent/models.json (backup kept, other providers preserved)?", { yes: opts.yes })
1571
+ await deps.confirm("Merge LunaRoute provider into ~/.pi/agent/models.json (backup kept, other providers preserved)?", { yes: opts.yes }),
1572
+ opts.profile
1539
1573
  );
1540
1574
  }
1541
1575
  if (await deps.confirm(
@@ -1551,7 +1585,8 @@ async function runPiSetup(opts, stored, routingUrl, deps) {
1551
1585
  routingUrl,
1552
1586
  await deps.confirm("Merge LunaRoute provider into ~/.pi/agent/models.json (backup kept, other providers preserved)?", {
1553
1587
  yes: opts.yes
1554
- })
1588
+ }),
1589
+ opts.profile
1555
1590
  );
1556
1591
  } catch (err) {
1557
1592
  if (err instanceof NonInteractiveTerminalError) {
@@ -1569,7 +1604,7 @@ async function runHermesSetup(creds, routingUrl, opts, deps) {
1569
1604
  try {
1570
1605
  if (opts.print) {
1571
1606
  console.log("\n# would run:");
1572
- for (const args of commands) console.log(` hermes ${args.join(" ")}`);
1607
+ for (const args of commands) console.log(` hermes ${hermesCommandLine(args, creds.routing_key, opts.profile)}`);
1573
1608
  console.log(
1574
1609
  "\nThen start hermes and run /model custom:lunaroute:<model> to pick a LunaRoute model (models auto-discover from /v1/models)."
1575
1610
  );
@@ -1581,7 +1616,7 @@ async function runHermesSetup(creds, routingUrl, opts, deps) {
1581
1616
  return runHermesConfigSets(deps.spawn, routingUrl, creds.routing_key);
1582
1617
  }
1583
1618
  console.log("Skipped. Re-run with --yes, or configure manually:");
1584
- for (const args of commands) console.log(` hermes ${args.join(" ")}`);
1619
+ for (const args of commands) console.log(` hermes ${hermesCommandLine(args, creds.routing_key, opts.profile)}`);
1585
1620
  console.log("\nThen start hermes and run /model custom:lunaroute:<model> to pick a LunaRoute model.");
1586
1621
  return 0;
1587
1622
  } catch (err) {
@@ -1593,16 +1628,20 @@ async function runHermesSetup(creds, routingUrl, opts, deps) {
1593
1628
  return 1;
1594
1629
  }
1595
1630
  }
1596
- function hermesConfigSetArgs(routingUrl, key) {
1631
+ function hermesConfigSetArgs(routingUrl, key2) {
1597
1632
  return [
1598
1633
  ["config", "set", "providers.lunaroute.api", `${routingUrl}/v1`],
1599
1634
  ["config", "set", "providers.lunaroute.key_env", "LUNAROUTE_API_KEY"],
1600
1635
  ["config", "set", "providers.lunaroute.transport", "chat_completions"],
1601
- ["config", "set", "LUNAROUTE_API_KEY", key]
1636
+ ["config", "set", "LUNAROUTE_API_KEY", key2]
1602
1637
  ];
1603
1638
  }
1604
- async function runHermesConfigSets(spawn, routingUrl, key) {
1605
- const commands = hermesConfigSetArgs(routingUrl, key);
1639
+ function hermesCommandLine(args, key2, profile) {
1640
+ const profileArg = profile && profile !== "default" ? ` -p ${shQuote(profile)}` : "";
1641
+ return args.map((a) => a === key2 ? `"$(lunaroute key${profileArg})"` : a).join(" ");
1642
+ }
1643
+ async function runHermesConfigSets(spawn, routingUrl, key2) {
1644
+ const commands = hermesConfigSetArgs(routingUrl, key2);
1606
1645
  const setKeys = commands.map((c) => c[2]);
1607
1646
  for (let i = 0; i < commands.length; i++) {
1608
1647
  const code = await new Promise((resolve) => {
@@ -1643,7 +1682,7 @@ async function runOpencodeSetup(stored, settings, routingUrl, opts, deps) {
1643
1682
  ),
1644
1683
  opts.key ? buildKeyPlan("<your-api-key>") : null
1645
1684
  );
1646
- return applyAndReport(plan, stored?.routing_key ?? "", false);
1685
+ return applyAndReport(plan, stored?.routing_key ?? "", false, opts.profile);
1647
1686
  }
1648
1687
  if (opts.key) {
1649
1688
  const creds2 = await requireCreds(opts, deps, stored);
@@ -1657,14 +1696,15 @@ async function runOpencodeSetup(stored, settings, routingUrl, opts, deps) {
1657
1696
  routingUrl,
1658
1697
  await deps.confirm("Merge LunaRoute provider into opencode.json (backup kept, other providers preserved)?", {
1659
1698
  yes: opts.yes
1660
- })
1699
+ }),
1700
+ opts.profile
1661
1701
  );
1662
1702
  }
1663
1703
  const plan = buildPlan(
1664
1704
  { routingUrl, orgId: settings.org_id, models: [], keyEnvVar: "LUNAROUTE_API_KEY" },
1665
1705
  { extension: true }
1666
1706
  );
1667
- const code = await applyAndReport(plan, creds2.routing_key, true);
1707
+ const code = await applyAndReport(plan, creds2.routing_key, true, opts.profile);
1668
1708
  if (code !== 0) return code;
1669
1709
  const store = await deps.confirm(
1670
1710
  "Store the key in ~/.local/share/opencode/auth.json (replaces any existing LunaRoute login; other credentials preserved)?",
@@ -1674,7 +1714,7 @@ async function runOpencodeSetup(stored, settings, routingUrl, opts, deps) {
1674
1714
  console.log("\nSkipped. Inside opencode, run /connect to sign in.");
1675
1715
  return 0;
1676
1716
  }
1677
- const wcode = await applyAndReport(buildKeyPlan(creds2.routing_key), creds2.routing_key, true);
1717
+ const wcode = await applyAndReport(buildKeyPlan(creds2.routing_key), creds2.routing_key, true, opts.profile);
1678
1718
  if (wcode !== 0) return wcode;
1679
1719
  console.log("\nDone. Start opencode \u2014 it is signed in. Pick a model with /models.");
1680
1720
  return 0;
@@ -1687,7 +1727,7 @@ async function runOpencodeSetup(stored, settings, routingUrl, opts, deps) {
1687
1727
  { routingUrl, orgId: settings.org_id, models: [], keyEnvVar: "LUNAROUTE_API_KEY" },
1688
1728
  { extension: true }
1689
1729
  );
1690
- const code = await applyAndReport(plan, stored?.routing_key ?? "", true);
1730
+ const code = await applyAndReport(plan, stored?.routing_key ?? "", true, opts.profile);
1691
1731
  if (code !== 0) return code;
1692
1732
  if (await deps.confirm("Start opencode now? (if it's already open, quit and reopen it to load the extension)", {
1693
1733
  yes: opts.yes
@@ -1703,7 +1743,7 @@ async function runOpencodeSetup(stored, settings, routingUrl, opts, deps) {
1703
1743
  "Merge LunaRoute provider into opencode.json instead? (backup kept, other providers preserved)",
1704
1744
  { yes: opts.yes }
1705
1745
  );
1706
- return applyOpencodeProvider(creds, routingUrl, write);
1746
+ return applyOpencodeProvider(creds, routingUrl, write, opts.profile);
1707
1747
  } catch (err) {
1708
1748
  if (err instanceof NonInteractiveTerminalError) {
1709
1749
  console.error(
@@ -1715,7 +1755,7 @@ async function runOpencodeSetup(stored, settings, routingUrl, opts, deps) {
1715
1755
  return 1;
1716
1756
  }
1717
1757
  }
1718
- async function applyOpencodeProvider(creds, routingUrl, write) {
1758
+ async function applyOpencodeProvider(creds, routingUrl, write, profile = "default") {
1719
1759
  let models;
1720
1760
  try {
1721
1761
  models = await fetchModels(routingUrl, creds.routing_key);
@@ -1729,7 +1769,7 @@ async function applyOpencodeProvider(creds, routingUrl, write) {
1729
1769
  models,
1730
1770
  keyEnvVar: "LUNAROUTE_API_KEY"
1731
1771
  });
1732
- return applyAndReport(plan, creds.routing_key, write);
1772
+ return applyAndReport(plan, creds.routing_key, write, profile);
1733
1773
  }
1734
1774
  function combinePlans(a, b) {
1735
1775
  if (!b) return a;
@@ -1739,9 +1779,9 @@ function combinePlans(a, b) {
1739
1779
  notes: [...a.notes, ...b.notes]
1740
1780
  };
1741
1781
  }
1742
- async function applyAndReport(plan, key, write) {
1782
+ async function applyAndReport(plan, key2, write, profile = "default") {
1743
1783
  try {
1744
- const summary = await applyPlan(plan, { print: !write, key });
1784
+ const summary = await applyPlan(plan, { print: !write, key: key2, profile });
1745
1785
  if (write && summary.written.length > 0) {
1746
1786
  console.log(`
1747
1787
  Wrote: ${summary.written.join(", ")}`);
@@ -1802,7 +1842,7 @@ async function installPiExtension(deps, opts) {
1802
1842
  { yes: opts.yes }
1803
1843
  );
1804
1844
  if (remove) {
1805
- const ccode = await applyAndReport(buildStaleProviderCleanupPlan(), "", true);
1845
+ const ccode = await applyAndReport(buildStaleProviderCleanupPlan(), "", true, opts.profile);
1806
1846
  if (ccode !== 0) return ccode;
1807
1847
  }
1808
1848
  }
@@ -1827,7 +1867,7 @@ async function launchPi(spawn) {
1827
1867
  });
1828
1868
  });
1829
1869
  }
1830
- async function applyPiModels(creds, routingUrl, write) {
1870
+ async function applyPiModels(creds, routingUrl, write, profile = "default") {
1831
1871
  let models;
1832
1872
  try {
1833
1873
  models = await fetchModels(routingUrl, creds.routing_key);
@@ -1842,7 +1882,7 @@ async function applyPiModels(creds, routingUrl, write) {
1842
1882
  keyEnvVar: "LUNAROUTE_API_KEY"
1843
1883
  });
1844
1884
  try {
1845
- const summary = await applyPlan(plan, { print: !write, key: creds.routing_key });
1885
+ const summary = await applyPlan(plan, { print: !write, key: creds.routing_key, profile });
1846
1886
  if (write && summary.written.length > 0) {
1847
1887
  console.log(`
1848
1888
  Wrote: ${summary.written.join(", ")}`);
@@ -2516,6 +2556,9 @@ program.command("login").description("Authorize this device via the browser and
2516
2556
  program.command("whoami").description("Show the signed-in user and organization.").action(() => {
2517
2557
  whoami(program.opts().profile);
2518
2558
  });
2559
+ program.command("key").description('Print the stored routing key, for shell use (e.g. "$(lunaroute key)").').action(() => {
2560
+ key(program.opts().profile);
2561
+ });
2519
2562
  program.command("logout").description("Remove stored credentials for the active profile.").action(() => {
2520
2563
  logout(program.opts().profile);
2521
2564
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lunaroute/cli",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "LunaRoute CLI — configure coding harnesses and manage your LunaRoute account.",
5
5
  "repository": {
6
6
  "type": "git",