@triflux/remote 10.31.0 → 10.33.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.
@@ -4,7 +4,8 @@ import { existsSync, readFileSync } from "node:fs";
4
4
  import { homedir } from "node:os";
5
5
  import { dirname, join } from "node:path";
6
6
  import { fileURLToPath } from "node:url";
7
- import { whichCommand, whichCommandAsync } from "@triflux/core/hub/platform.mjs";
7
+ import { resolveHubPortForContext } from "../../hub/hub-lifecycle.mjs";
8
+ import { whichCommand, whichCommandAsync } from "../../hub/platform.mjs";
8
9
 
9
10
  const HUB_DEFAULT_PORT = 27888;
10
11
  const DEFAULT_STATUS_URL = "http://127.0.0.1:27888/status";
@@ -37,13 +38,36 @@ function fetchHubStatus({
37
38
  };
38
39
  }
39
40
 
40
- export function resolveDefaultStatusUrl(env = process.env) {
41
- const envPort = Number.parseInt(String(env?.TFX_HUB_PORT ?? ""), 10);
42
- const port =
43
- Number.isFinite(envPort) && envPort > 0 ? envPort : HUB_DEFAULT_PORT;
41
+ export function resolveDefaultStatusUrl(env = process.env, cwd = process.cwd()) {
42
+ const port = resolveHubPortForContext({
43
+ env,
44
+ cwd,
45
+ defaultPort: HUB_DEFAULT_PORT,
46
+ });
44
47
  return `http://127.0.0.1:${port}/status`;
45
48
  }
46
49
 
50
+ function resolveStatusUrlForContext({
51
+ statusUrl,
52
+ env = process.env,
53
+ cwd = process.cwd(),
54
+ } = {}) {
55
+ try {
56
+ const url = new URL(String(statusUrl));
57
+ url.port = String(
58
+ resolveHubPortForContext({
59
+ port: url.port,
60
+ env,
61
+ cwd,
62
+ defaultPort: HUB_DEFAULT_PORT,
63
+ }),
64
+ );
65
+ return url.toString();
66
+ } catch {
67
+ return resolveDefaultStatusUrl(env, cwd);
68
+ }
69
+ }
70
+
47
71
  function normalizeCliName(name) {
48
72
  return String(name ?? "").trim() || null;
49
73
  }
@@ -212,8 +236,10 @@ export function detectCodexPlan(options = {}) {
212
236
  }
213
237
 
214
238
  export function checkHub({
239
+ env = process.env,
240
+ cwd = process.cwd(),
215
241
  pkgRoot = DEFAULT_PKG_ROOT,
216
- statusUrl = resolveDefaultStatusUrl(),
242
+ statusUrl = resolveDefaultStatusUrl(env, cwd),
217
243
  restart = true,
218
244
  requestTimeoutMs = 3000,
219
245
  pollAttempts = 8,
@@ -223,10 +249,11 @@ export function checkHub({
223
249
  existsSyncFn = existsSync,
224
250
  sleepSyncFn = sleepSync,
225
251
  } = {}) {
252
+ const guardedStatusUrl = resolveStatusUrlForContext({ statusUrl, env, cwd });
226
253
  try {
227
254
  return fetchHubStatus({
228
255
  execSyncFn,
229
- statusUrl,
256
+ statusUrl: guardedStatusUrl,
230
257
  timeout: requestTimeoutMs,
231
258
  });
232
259
  } catch {}
@@ -239,7 +266,7 @@ export function checkHub({
239
266
 
240
267
  try {
241
268
  const child = spawnFn(process.execPath, [serverPath], {
242
- env: { ...process.env, TFX_HUB_PORT: String(new URL(statusUrl).port) },
269
+ env: { ...env, TFX_HUB_PORT: String(new URL(guardedStatusUrl).port) },
243
270
  detached: true,
244
271
  stdio: "ignore",
245
272
  windowsHide: true,
@@ -254,7 +281,7 @@ export function checkHub({
254
281
  try {
255
282
  const status = fetchHubStatus({
256
283
  execSyncFn,
257
- statusUrl,
284
+ statusUrl: guardedStatusUrl,
258
285
  timeout: Math.min(requestTimeoutMs, 1000),
259
286
  });
260
287
  if (status.state === "healthy") {