mira-cli-ts 0.24.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 +67 -6
  2. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -11,6 +11,7 @@ var __esm = (fn, res, err) => () => {
11
11
  throw err[0];
12
12
  return res;
13
13
  };
14
+ var __require = import.meta.require;
14
15
 
15
16
  // ../shared/src/utils/paths.ts
16
17
  function resolveBunBinary() {
@@ -37,8 +38,41 @@ var init_paths = () => {};
37
38
  // src/cli.ts
38
39
  var VERSION = "0.1.0";
39
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
+ }
40
68
  function apiUrl() {
41
- 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(/\/$/, "");
42
76
  }
43
77
  function token() {
44
78
  return process.env.MIRA_TOKEN ?? "";
@@ -48,11 +82,38 @@ function authHeaders() {
48
82
  return t ? { Authorization: `Bearer ${t}` } : {};
49
83
  }
50
84
  async function apiFetch(path, init) {
51
- const res = await fetch(`${apiUrl()}${path}`, {
52
- ...init,
53
- headers: { "Content-Type": "application/json", ...authHeaders(), ...init?.headers }
54
- });
55
- 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");
56
117
  }
57
118
  function printHelp() {
58
119
  const help = `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mira-cli-ts",
3
- "version": "0.24.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": {