mira-cli-ts 0.23.0 → 0.25.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/cli.js +102 -7
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1,11 +1,78 @@
1
1
  #!/usr/bin/env bun
2
2
  // @bun
3
+ var __esm = (fn, res, err) => () => {
4
+ if (fn)
5
+ try {
6
+ res = fn(fn = 0);
7
+ } catch (e) {
8
+ err = [e];
9
+ }
10
+ if (err)
11
+ throw err[0];
12
+ return res;
13
+ };
14
+ var __require = import.meta.require;
15
+
16
+ // ../shared/src/utils/paths.ts
17
+ function resolveBunBinary() {
18
+ try {
19
+ const exec = process.execPath ?? "";
20
+ if (exec.toLowerCase().includes("bun"))
21
+ return exec;
22
+ } catch {}
23
+ try {
24
+ const which = Bun?.which;
25
+ if (which) {
26
+ const found = which("bun");
27
+ if (found)
28
+ return found;
29
+ }
30
+ } catch {}
31
+ if (typeof process !== "undefined" && process.platform === "win32") {
32
+ return "bun.exe";
33
+ }
34
+ return "bun";
35
+ }
36
+ var init_paths = () => {};
3
37
 
4
38
  // src/cli.ts
5
39
  var VERSION = "0.1.0";
6
40
  var DEFAULT_API = process.env.MIRA_API_URL ?? process.env.MIRA_APIURL ?? "http://127.0.0.1:4096";
41
+ var cachedMiraPort;
42
+ function getMiraPortCached() {
43
+ if (cachedMiraPort !== undefined)
44
+ return cachedMiraPort;
45
+ try {
46
+ const { readFileSync, existsSync } = __require("fs");
47
+ const cands = [".mira/port", "../.mira/port", "../../.mira/port"];
48
+ try {
49
+ const u = new URL(import.meta.url).pathname;
50
+ const dir = u.slice(0, u.lastIndexOf("/"));
51
+ cands.push(`${dir}/../../.mira/port`, `${dir}/../../../.mira/port`);
52
+ } catch {}
53
+ for (const p of cands) {
54
+ try {
55
+ if (existsSync(p)) {
56
+ const n = Number(readFileSync(p, "utf-8").trim());
57
+ if (Number.isFinite(n) && n > 0 && n <= 65535) {
58
+ cachedMiraPort = n;
59
+ return n;
60
+ }
61
+ }
62
+ } catch {}
63
+ }
64
+ } catch {}
65
+ cachedMiraPort = null;
66
+ return null;
67
+ }
7
68
  function apiUrl() {
8
- return (process.env.MIRA_API_URL ?? DEFAULT_API).replace(/\/$/, "");
69
+ const envUrl = process.env.MIRA_API_URL ?? process.env.MIRA_APIURL;
70
+ if (envUrl)
71
+ return envUrl.replace(/\/$/, "");
72
+ const port = getMiraPortCached();
73
+ if (port)
74
+ return `http://127.0.0.1:${port}`;
75
+ return DEFAULT_API.replace(/\/$/, "");
9
76
  }
10
77
  function token() {
11
78
  return process.env.MIRA_TOKEN ?? "";
@@ -15,11 +82,38 @@ function authHeaders() {
15
82
  return t ? { Authorization: `Bearer ${t}` } : {};
16
83
  }
17
84
  async function apiFetch(path, init) {
18
- const res = await fetch(`${apiUrl()}${path}`, {
19
- ...init,
20
- headers: { "Content-Type": "application/json", ...authHeaders(), ...init?.headers }
21
- });
22
- return res;
85
+ const primary = apiUrl();
86
+ const headers = { "Content-Type": "application/json", ...authHeaders(), ...init?.headers };
87
+ const usingDefault = !process.env.MIRA_API_URL && !process.env.MIRA_APIURL;
88
+ const candidates = [primary];
89
+ if (usingDefault) {
90
+ const filePort = getMiraPortCached();
91
+ if (filePort) {
92
+ const u = `http://127.0.0.1:${filePort}`;
93
+ if (!candidates.includes(u))
94
+ candidates.push(u);
95
+ }
96
+ for (let p = 4096;p <= 4106; p++) {
97
+ const u = `http://127.0.0.1:${p}`;
98
+ if (!candidates.includes(u))
99
+ candidates.push(u);
100
+ }
101
+ }
102
+ let lastErr = null;
103
+ for (const base of candidates) {
104
+ try {
105
+ const res = await fetch(`${base}${path}`, { ...init, headers });
106
+ return res;
107
+ } catch (e) {
108
+ const msg = String(e?.message ?? e);
109
+ const isConn = e instanceof TypeError || msg.includes("ECONNREFUSED") || msg.includes("Failed to fetch") || msg.includes("Connection refused") || msg.includes("fetch failed") || msg.includes("ECONNRESET");
110
+ lastErr = e;
111
+ if (!isConn)
112
+ throw e;
113
+ continue;
114
+ }
115
+ }
116
+ throw lastErr ?? new Error("fetch failed");
23
117
  }
24
118
  function printHelp() {
25
119
  const help = `
@@ -106,7 +200,8 @@ async function cmdServe(opts) {
106
200
  process.env.PORT = port;
107
201
  process.env.HOST = host;
108
202
  const serverDir = new URL("../../server", import.meta.url).pathname;
109
- const proc = Bun.spawn(["bun", "run", "src/index.ts"], {
203
+ await Promise.resolve().then(() => init_paths());
204
+ const proc = Bun.spawn([resolveBunBinary(), "run", "src/index.ts"], {
110
205
  cwd: serverDir,
111
206
  env: { ...process.env, PORT: port, HOST: host },
112
207
  stdout: "inherit",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mira-cli-ts",
3
- "version": "0.23.0",
3
+ "version": "0.25.0",
4
4
  "description": "Mira CLI — thin wrapper around @mira/server (serve + session/agent/complete)",
5
5
  "type": "module",
6
6
  "bin": {