nexrall-code 0.5.75 → 0.5.77

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 (3) hide show
  1. package/dist/index.js +173 -34
  2. package/nex-watch.js +117 -0
  3. package/package.json +6 -4
package/dist/index.js CHANGED
@@ -14029,7 +14029,7 @@ ${diff3}${xfile}${sec}` };
14029
14029
  }
14030
14030
  return null;
14031
14031
  }
14032
- async function fetchUrl(input, _workDir2, _redirectCount = 0) {
14032
+ async function fetchUrl(input, _workDir2, _redirectCount = 0, abortSignal) {
14033
14033
  const url = typeof input.url === "string" ? input.url : "";
14034
14034
  if (!url)
14035
14035
  return { error: "Missing required parameter: url" };
@@ -14073,8 +14073,20 @@ ${diff3}${xfile}${sec}` };
14073
14073
  if (settled)
14074
14074
  return;
14075
14075
  settled = true;
14076
+ if (abortPoll !== void 0)
14077
+ clearInterval(abortPoll);
14076
14078
  resolve3(result);
14077
14079
  };
14080
+ let abortPoll;
14081
+ if (abortSignal) {
14082
+ abortPoll = setInterval(() => {
14083
+ if (abortSignal.aborted && !settled) {
14084
+ selfAborted = true;
14085
+ req.destroy();
14086
+ finish({ error: "Request stopped by user", interrupted: true });
14087
+ }
14088
+ }, 100);
14089
+ }
14078
14090
  const req = transport.get(url, {
14079
14091
  timeout: DEFAULT_TIMEOUT_MS,
14080
14092
  lookup: guardedLookup,
@@ -14096,7 +14108,7 @@ ${diff3}${xfile}${sec}` };
14096
14108
  finish({ error: `Invalid redirect location: ${res.headers.location}` });
14097
14109
  return;
14098
14110
  }
14099
- fetchUrl({ url: nextUrl }, void 0, _redirectCount + 1).then(finish);
14111
+ fetchUrl({ url: nextUrl }, void 0, _redirectCount + 1, abortSignal).then(finish);
14100
14112
  return;
14101
14113
  }
14102
14114
  const contentType = res.headers["content-type"] ?? "";
@@ -14434,45 +14446,83 @@ ${lines.join("\n")}`;
14434
14446
  }
14435
14447
  }
14436
14448
  var VALID_ASPECT_RATIOS = /* @__PURE__ */ new Set(["1:1", "9:16", "16:9", "3:4", "4:3", "3:2", "2:3", "5:4", "4:5", "21:9", "auto"]);
14437
- function _httpsPost(url, body, headers, timeoutMs = 3e4) {
14449
+ function _httpsPost(url, body, headers, timeoutMs = 3e4, abortSignal) {
14438
14450
  return new Promise((resolve3, reject) => {
14439
14451
  const u = new URL(url);
14440
14452
  const chunks = [];
14453
+ let settled = false;
14454
+ let abortPoll;
14455
+ const done = (fn) => {
14456
+ if (settled)
14457
+ return;
14458
+ settled = true;
14459
+ if (abortPoll)
14460
+ clearInterval(abortPoll);
14461
+ fn();
14462
+ };
14441
14463
  const req = https3.request({ hostname: u.hostname, path: u.pathname + u.search, method: "POST", headers: { ...headers, "Content-Length": Buffer.byteLength(body) }, timeout: timeoutMs }, (res) => {
14442
14464
  res.on("data", (c) => chunks.push(c));
14443
- res.on("end", () => resolve3({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf-8") }));
14465
+ res.on("end", () => done(() => resolve3({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf-8") })));
14444
14466
  });
14445
14467
  req.on("timeout", () => {
14446
14468
  req.destroy();
14447
- reject(new Error(`Request timed out after ${timeoutMs}ms`));
14469
+ done(() => reject(new Error(`Request timed out after ${timeoutMs}ms`)));
14448
14470
  });
14449
- req.on("error", reject);
14471
+ req.on("error", (err) => done(() => reject(err)));
14472
+ if (abortSignal) {
14473
+ abortPoll = setInterval(() => {
14474
+ if (abortSignal.aborted && !settled) {
14475
+ req.destroy();
14476
+ done(() => reject(Object.assign(new Error("Stopped by user"), { name: "AbortError" })));
14477
+ }
14478
+ }, 100);
14479
+ }
14450
14480
  req.write(body);
14451
14481
  req.end();
14452
14482
  });
14453
14483
  }
14454
- function _httpsGet(url, headers, timeoutMs = 2e4) {
14484
+ function _httpsGet(url, headers, timeoutMs = 2e4, abortSignal) {
14455
14485
  return new Promise((resolve3, reject) => {
14456
14486
  const u = new URL(url);
14457
14487
  const transport = u.protocol === "https:" ? https3 : http3;
14458
14488
  const chunks = [];
14489
+ let settled = false;
14490
+ let abortPoll;
14491
+ const done = (fn) => {
14492
+ if (settled)
14493
+ return;
14494
+ settled = true;
14495
+ if (abortPoll)
14496
+ clearInterval(abortPoll);
14497
+ fn();
14498
+ };
14459
14499
  const req = transport.get({ hostname: u.hostname, path: u.pathname + u.search, headers, timeout: timeoutMs }, (res) => {
14460
14500
  if ((res.statusCode === 301 || res.statusCode === 302 || res.statusCode === 307 || res.statusCode === 308) && res.headers.location) {
14461
14501
  req.destroy();
14462
- _httpsGet(new URL(res.headers.location, url).href, headers, timeoutMs).then(resolve3, reject);
14502
+ done(() => {
14503
+ _httpsGet(new URL(res.headers.location, url).href, headers, timeoutMs, abortSignal).then(resolve3, reject);
14504
+ });
14463
14505
  return;
14464
14506
  }
14465
14507
  res.on("data", (c) => chunks.push(c));
14466
- res.on("end", () => resolve3({ status: res.statusCode ?? 0, body: Buffer.concat(chunks), contentType: res.headers["content-type"] ?? "" }));
14508
+ res.on("end", () => done(() => resolve3({ status: res.statusCode ?? 0, body: Buffer.concat(chunks), contentType: res.headers["content-type"] ?? "" })));
14467
14509
  });
14468
14510
  req.on("timeout", () => {
14469
14511
  req.destroy();
14470
- reject(new Error(`Download timed out after ${timeoutMs}ms`));
14512
+ done(() => reject(new Error(`Download timed out after ${timeoutMs}ms`)));
14471
14513
  });
14472
- req.on("error", reject);
14514
+ req.on("error", (err) => done(() => reject(err)));
14515
+ if (abortSignal) {
14516
+ abortPoll = setInterval(() => {
14517
+ if (abortSignal.aborted && !settled) {
14518
+ req.destroy();
14519
+ done(() => reject(Object.assign(new Error("Stopped by user"), { name: "AbortError" })));
14520
+ }
14521
+ }, 100);
14522
+ }
14473
14523
  });
14474
14524
  }
14475
- async function generateImage(input, workDir) {
14525
+ async function generateImage(input, workDir, abortSignal) {
14476
14526
  const prompt2 = typeof input.prompt === "string" ? input.prompt.trim() : "";
14477
14527
  const relPath = typeof input.path === "string" ? input.path.trim() : "";
14478
14528
  const aspectRatio = typeof input.aspect_ratio === "string" && VALID_ASPECT_RATIOS.has(input.aspect_ratio) ? input.aspect_ratio : "1:1";
@@ -14487,7 +14537,7 @@ ${lines.join("\n")}`;
14487
14537
  let cost;
14488
14538
  let balance;
14489
14539
  try {
14490
- const r2 = await _httpsPost(`${client_1.API_BASE}/api/code/assets/generate`, JSON.stringify({ prompt: prompt2, aspect_ratio: aspectRatio }), { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, 17e4);
14540
+ const r2 = await _httpsPost(`${client_1.API_BASE}/api/code/assets/generate`, JSON.stringify({ prompt: prompt2, aspect_ratio: aspectRatio }), { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, 17e4, abortSignal);
14491
14541
  let json = {};
14492
14542
  try {
14493
14543
  json = JSON.parse(r2.body);
@@ -14501,6 +14551,8 @@ ${lines.join("\n")}`;
14501
14551
  if (!imageUrl)
14502
14552
  return { error: "Image generation succeeded but no URL was returned." };
14503
14553
  } catch (err) {
14554
+ if (err.name === "AbortError")
14555
+ return { error: "generate_image stopped by user", interrupted: true };
14504
14556
  return { error: `generate_image failed: ${err.message}` };
14505
14557
  }
14506
14558
  const destPath = resolvePath(relPath, workDir);
@@ -14509,7 +14561,7 @@ ${lines.join("\n")}`;
14509
14561
  } catch (_) {
14510
14562
  }
14511
14563
  try {
14512
- const dl = await _httpsGet(imageUrl, { "User-Agent": FETCH_USER_AGENT }, 6e4);
14564
+ const dl = await _httpsGet(imageUrl, { "User-Agent": FETCH_USER_AGENT }, 6e4, abortSignal);
14513
14565
  if (dl.status !== 200)
14514
14566
  return { error: `Failed to download generated image (HTTP ${dl.status}).` };
14515
14567
  fs9.writeFileSync(destPath, dl.body);
@@ -14517,10 +14569,12 @@ ${lines.join("\n")}`;
14517
14569
  const costNote = cost != null ? ` ($${cost.toFixed(2)} charged` + (balance != null ? `, balance $${balance.toFixed(2)})` : ")") : "";
14518
14570
  return { output: `Generated and saved image to ${destPath} (${kb} KB)${costNote}.` };
14519
14571
  } catch (err) {
14572
+ if (err.name === "AbortError")
14573
+ return { error: "generate_image stopped by user (image was generated and billed, but the download was interrupted)", interrupted: true };
14520
14574
  return { error: `Failed to save image to ${destPath}: ${err.message}` };
14521
14575
  }
14522
14576
  }
14523
- async function stockPhoto(input, workDir) {
14577
+ async function stockPhoto(input, workDir, abortSignal) {
14524
14578
  const query = typeof input.query === "string" ? input.query.trim() : "";
14525
14579
  const relPath = typeof input.path === "string" ? input.path.trim() : "";
14526
14580
  const count = typeof input.count === "number" ? Math.min(Math.max(Math.floor(input.count), 1), 10) : 3;
@@ -14537,7 +14591,7 @@ ${lines.join("\n")}`;
14537
14591
  u.searchParams.set("count", String(count));
14538
14592
  if (orientation)
14539
14593
  u.searchParams.set("orientation", orientation);
14540
- const r2 = await _httpsGet(u.toString(), { Authorization: `Bearer ${token}` });
14594
+ const r2 = await _httpsGet(u.toString(), { Authorization: `Bearer ${token}` }, 2e4, abortSignal);
14541
14595
  let json = {};
14542
14596
  try {
14543
14597
  json = JSON.parse(r2.body.toString("utf-8"));
@@ -14549,6 +14603,8 @@ ${lines.join("\n")}`;
14549
14603
  if (!photos.length)
14550
14604
  return { output: `No stock photos found for "${query}".` };
14551
14605
  } catch (err) {
14606
+ if (err.name === "AbortError")
14607
+ return { error: "stock_photo stopped by user", interrupted: true };
14552
14608
  return { error: `stock_photo search failed: ${err.message}` };
14553
14609
  }
14554
14610
  if (relPath) {
@@ -14561,7 +14617,7 @@ ${lines.join("\n")}`;
14561
14617
  } catch (_) {
14562
14618
  }
14563
14619
  try {
14564
- const dl = await _httpsGet(top.url, { "User-Agent": FETCH_USER_AGENT });
14620
+ const dl = await _httpsGet(top.url, { "User-Agent": FETCH_USER_AGENT }, 2e4, abortSignal);
14565
14621
  if (dl.status !== 200)
14566
14622
  return { error: `Failed to download photo (HTTP ${dl.status}).` };
14567
14623
  fs9.writeFileSync(destPath, dl.body);
@@ -14569,6 +14625,8 @@ ${lines.join("\n")}`;
14569
14625
  const credit = top.credit ? ` \u2014 Photo by ${top.credit} on Unsplash` : "";
14570
14626
  return { output: `Downloaded "${top.alt || query}" to ${destPath} (${kb} KB).${credit}` };
14571
14627
  } catch (err) {
14628
+ if (err.name === "AbortError")
14629
+ return { error: "stock_photo download stopped by user", interrupted: true };
14572
14630
  return { error: `Failed to save photo to ${destPath}: ${err.message}` };
14573
14631
  }
14574
14632
  }
@@ -14576,7 +14634,7 @@ ${lines.join("\n")}`;
14576
14634
  return { output: `Found ${photos.length} photo(s) for "${query}":
14577
14635
  ${lines.join("\n")}` };
14578
14636
  }
14579
- async function webSearch(input) {
14637
+ async function webSearch(input, _workDir2, abortSignal) {
14580
14638
  const query = typeof input.query === "string" ? input.query.trim() : "";
14581
14639
  if (!query)
14582
14640
  return { error: "Missing required parameter: query" };
@@ -14584,7 +14642,7 @@ ${lines.join("\n")}` };
14584
14642
  if (!token)
14585
14643
  return { error: "Not authenticated. Run `nexrall-code login` first." };
14586
14644
  try {
14587
- const r2 = await _httpsPost(`${client_1.API_BASE}/api/code/tools/web_search`, JSON.stringify({ query }), { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, 3e4);
14645
+ const r2 = await _httpsPost(`${client_1.API_BASE}/api/code/tools/web_search`, JSON.stringify({ query }), { "Content-Type": "application/json", Authorization: `Bearer ${token}` }, 3e4, abortSignal);
14588
14646
  let json = {};
14589
14647
  try {
14590
14648
  json = JSON.parse(r2.body);
@@ -14597,6 +14655,8 @@ ${lines.join("\n")}` };
14597
14655
  return { error: "web_search succeeded but returned no results text." };
14598
14656
  return { output: text };
14599
14657
  } catch (err) {
14658
+ if (err.name === "AbortError")
14659
+ return { error: "web_search stopped by user", interrupted: true };
14600
14660
  return { error: `web_search failed: ${err.message}` };
14601
14661
  }
14602
14662
  }
@@ -14731,8 +14791,6 @@ ${expanded}` };
14731
14791
  "delete_file",
14732
14792
  "notebook_read",
14733
14793
  "notebook_edit",
14734
- "generate_image",
14735
- "stock_photo",
14736
14794
  "get_symbols",
14737
14795
  "get_workspace_symbols",
14738
14796
  "go_to_definition",
@@ -14752,6 +14810,14 @@ ${expanded}` };
14752
14810
  return await todoWrite(input, agentScope);
14753
14811
  if (name === "todo_read")
14754
14812
  return await todoRead(input, agentScope);
14813
+ if (name === "fetch_url")
14814
+ return await fetchUrl(input, workDir, 0, abortSignal);
14815
+ if (name === "web_search")
14816
+ return await webSearch(input, workDir, abortSignal);
14817
+ if (name === "generate_image")
14818
+ return await generateImage(input, workDir, abortSignal);
14819
+ if (name === "stock_photo")
14820
+ return await stockPhoto(input, workDir, abortSignal);
14755
14821
  if (WORKDIR_TOOLS.has(name)) {
14756
14822
  return await TOOL_MAP[name](input, workDir);
14757
14823
  }
@@ -17791,7 +17857,7 @@ ${options.nexrallMd}` : "") : options.nexrallMd;
17791
17857
  if (external !== null && external !== void 0) {
17792
17858
  result = external;
17793
17859
  } else if (options.mcpManager?.isMcpTool(name)) {
17794
- const mcpOutput = await options.mcpManager.callTool(name, input);
17860
+ const mcpOutput = await options.mcpManager.callTool(name, input, options.abortSignal);
17795
17861
  result = { output: mcpOutput ?? "" };
17796
17862
  } else {
17797
17863
  options.checkpointManager?.recordBeforeMutation(name, input);
@@ -18555,8 +18621,24 @@ var require_manager = __commonJS({
18555
18621
  }
18556
18622
  return tools;
18557
18623
  }
18558
- /** Execute an MCP tool call. Returns null if this tool is not an MCP tool. */
18559
- async callTool(prefixedName, input) {
18624
+ /**
18625
+ * Execute an MCP tool call. Returns null if this tool is not an MCP tool.
18626
+ *
18627
+ * `abortSignal` is optional and best-effort: the underlying transports (stdio
18628
+ * JSON-RPC / HTTP / SSE) have no cancellation message in this client, so an
18629
+ * abort here does NOT stop the server from finishing its work — it only stops
18630
+ * THIS CALL from making the caller wait for it. Before this was added, Ctrl+C
18631
+ * during an MCP tool call was silently swallowed: loop.ts set
18632
+ * `abortSignal.aborted = true` but nothing downstream of `options.mcpManager
18633
+ * .callTool(name, input)` ever looked at it, so the user's only way out was
18634
+ * to wait out the full 60s MCP_TOOL_TIMEOUT_MS (or however long the server
18635
+ * actually took). Racing the same abortSignal the 60s timeout already races
18636
+ * against makes Ctrl+C return control immediately instead of up to a minute
18637
+ * later — matching how the `bash` tool already behaves (its own 200ms abort
18638
+ * poll in executor.ts), just without the ability to also kill a remote
18639
+ * server's in-flight work the way `bash` can kill a local child process.
18640
+ */
18641
+ async callTool(prefixedName, input, abortSignal) {
18560
18642
  const serverName = this._toolMap.get(prefixedName);
18561
18643
  if (!serverName)
18562
18644
  return null;
@@ -18566,7 +18648,21 @@ var require_manager = __commonJS({
18566
18648
  const toolName = prefixedName.slice(serverName.length + 2);
18567
18649
  const MCP_TOOL_TIMEOUT_MS = 6e4;
18568
18650
  const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error(`MCP tool "${prefixedName}" timed out after ${MCP_TOOL_TIMEOUT_MS / 1e3}s`)), MCP_TOOL_TIMEOUT_MS));
18569
- return Promise.race([client.callTool(toolName, input), timeoutPromise]);
18651
+ const racers = [client.callTool(toolName, input), timeoutPromise];
18652
+ if (abortSignal) {
18653
+ let pollId;
18654
+ const abortPromise = new Promise((_, reject) => {
18655
+ pollId = setInterval(() => {
18656
+ if (abortSignal.aborted) {
18657
+ clearInterval(pollId);
18658
+ reject(Object.assign(new Error("Aborted"), { name: "AbortError" }));
18659
+ }
18660
+ }, 100);
18661
+ });
18662
+ racers.push(abortPromise);
18663
+ return Promise.race(racers).finally(() => clearInterval(pollId));
18664
+ }
18665
+ return Promise.race(racers);
18570
18666
  }
18571
18667
  /** True if this tool name belongs to an MCP server. */
18572
18668
  isMcpTool(name) {
@@ -64886,6 +64982,12 @@ var App2 = ({ onReady }) => {
64886
64982
  }
64887
64983
  const newlineIdx = char.search(/[\r\n]/);
64888
64984
  const hasNewline = key.return || newlineIdx !== -1;
64985
+ if (hasNewline && key.shift) {
64986
+ const pos = cursorRef.current;
64987
+ setLine((s2) => s2.slice(0, pos) + "\n" + s2.slice(pos));
64988
+ setCursorPos(pos + 1);
64989
+ return;
64990
+ }
64889
64991
  if (hasNewline) {
64890
64992
  const before = newlineIdx === -1 ? char : char.slice(0, newlineIdx);
64891
64993
  const after = newlineIdx === -1 ? "" : char.slice(newlineIdx + 1).replace(/^[\r\n]/, "");
@@ -64940,7 +65042,36 @@ function startInkTerminal() {
64940
65042
  }
64941
65043
  }
64942
65044
  ),
64943
- { exitOnCtrlC: false }
65045
+ {
65046
+ exitOnCtrlC: false,
65047
+ // Opt into the Kitty keyboard protocol so `key.shift` is actually
65048
+ // populated for Enter (see the Shift+Enter handling in useInput
65049
+ // above) instead of always being false/undefined.
65050
+ //
65051
+ // MUST be 'enabled', not 'auto'. 'auto' has Ink send a CSI `?u` query
65052
+ // to the terminal and then listen on `stdin`'s 'data' event for the
65053
+ // reply (see ink/build/ink.js confirmKittySupport). That listener
65054
+ // races Ink's own input pipeline, which reads stdin via 'readable' +
65055
+ // `.read()` (paused mode) for its normal keystroke handling — Node
65056
+ // streams do not support mixing 'data' (flowing) and 'readable'
65057
+ // (paused) consumers safely, and in practice the 'readable' consumer
65058
+ // wins the terminal's query response. The reply itself (`\x1b[?1u`)
65059
+ // then gets fed through Ink's normal key parser, which doesn't
65060
+ // recognise it as any special key and inserts it as literal text —
65061
+ // this is exactly the "[?1u" that appears in the input box right
65062
+ // after Ink mounts (e.g. right after confirming the trust prompt).
65063
+ //
65064
+ // 'enabled' skips the query/response dance entirely: Ink just writes
65065
+ // the enable sequence once (`\x1b[>1u`) and never listens for a
65066
+ // reply, so there is nothing to leak into the input box. Terminals
65067
+ // that don't support the protocol silently ignore the unrecognised
65068
+ // escape sequence (the same way they ignore bracketed-paste mode),
65069
+ // so Shift+Enter still safely falls back to the '\' continuation
65070
+ // there — this trades "verify support first" for "assume support,
65071
+ // no-op if absent", which is safe here because the only visible
65072
+ // effect of enabling on an unsupporting terminal is nothing at all.
65073
+ kittyKeyboard: { mode: "enabled", flags: ["disambiguateEscapeCodes"] }
65074
+ }
64944
65075
  );
64945
65076
  });
64946
65077
  }
@@ -65221,23 +65352,29 @@ var Spinner = class {
65221
65352
  * No-op when the spinner isn't active: progress events can arrive a tick after
65222
65353
  * something else (a tool row, the first text delta) legitimately stopped it, and
65223
65354
  * resurrecting the spinner there would fight the printed output for the status line.
65355
+ *
65356
+ * Deliberately does NOT call render() immediately. onThinkingDelta/onThinkingProgress
65357
+ * can fire many times per second while the model streams (one SSE delta each), and
65358
+ * each of those used to trigger its own Ink re-render (setLive -> React state update ->
65359
+ * full-frame clear/redraw) layered on TOP of the interval's own 100ms redraw already
65360
+ * running in start(). Two independent, unsynchronized render sources hitting the same
65361
+ * frame is exactly what produced the visible jitter users reported ("dòng working...
65362
+ * giật giật liên tục") — every fast delta briefly repainted the status line out of
65363
+ * step with the spinner's own tick. Just updating the field here and letting the
65364
+ * existing setInterval pick it up on its next 100ms tick caps the redraw rate at a
65365
+ * steady 10fps with zero perceptible added latency (worst case: one stale frame for
65366
+ * <100ms), and removes the race entirely.
65224
65367
  */
65225
65368
  setDetail(detail) {
65226
65369
  if (this.interval === null)
65227
65370
  return;
65228
- if (this.detail === detail)
65229
- return;
65230
65371
  this.detail = detail;
65231
- this.render();
65232
65372
  }
65233
- /** Swap the label without resetting the elapsed timer (phase change within one turn). */
65373
+ /** Swap the label without resetting the elapsed timer (phase change within one turn). Same reasoning as setDetail() above: no immediate render, the interval tick picks it up. */
65234
65374
  setText(text) {
65235
65375
  if (this.interval === null)
65236
65376
  return;
65237
- if (this.baseText === text)
65238
- return;
65239
65377
  this.baseText = text;
65240
- this.render();
65241
65378
  }
65242
65379
  render() {
65243
65380
  getInkTerminal()?.setLive(composeStatusLine({
@@ -65695,6 +65832,8 @@ Set ${TRUST_ENV_VAR}=1 to confirm non-interactively, or run \`nex\` interactivel
65695
65832
  process.on("exit", () => {
65696
65833
  stopInkTerminal();
65697
65834
  });
65835
+ if (process.stdin.isTTY)
65836
+ process.stdin.setRawMode(true);
65698
65837
  await startInkTerminal();
65699
65838
  startRowCount();
65700
65839
  }
@@ -65847,7 +65986,7 @@ ${text}` : "");
65847
65986
  } else {
65848
65987
  console.error(source_default.red("\nError: ") + String(err.message));
65849
65988
  if (salvaged) {
65850
- console.error(source_default.dim(" Progress was saved \u2014 run `nex --continue` to resume this session."));
65989
+ console.error(source_default.dim(" Progress was saved \u2014 run `nex --resume` (or `nex -r`) to resume this session."));
65851
65990
  }
65852
65991
  }
65853
65992
  process.exit(1);
package/nex-watch.js ADDED
@@ -0,0 +1,117 @@
1
+ #!/usr/bin/env node
2
+ // nex-watch — auto-relaunch `nex` if the OS kills it with SIGKILL.
3
+ //
4
+ // Usage:
5
+ // nex-watch [any nex flags/prompt...]
6
+ // nex-watch # interactive session, auto-resumes on kill
7
+ // nex-watch -y "long unattended task"
8
+ //
9
+ // Why this exists (not something `nex` itself can do):
10
+ // SIGKILL (signal 9 — what a shell reports as "killed") is the one POSIX
11
+ // signal a process can NEVER intercept, handle, or clean up after — the
12
+ // kernel tears it down before a single line of the process's own code can
13
+ // run. On macOS this is the Jetsam low-memory killer; on Linux it's the
14
+ // OOM killer. Both pick a victim purely from system-wide memory pressure at
15
+ // that instant — a machine with plenty of total RAM is not immune if
16
+ // something else (browser, IDE, other builds) is using nearly all of it
17
+ // right now. No amount of code inside `nex` can catch or prevent this.
18
+ //
19
+ // What this script actually does:
20
+ // `nex` already persists its conversation to ~/.nexrall/cli-sessions on
21
+ // every turn boundary (≤3s debounce), independent of this script. That
22
+ // means a SIGKILL loses at most a few seconds of progress, not the whole
23
+ // session — but resuming still requires a human to notice the crash and
24
+ // type `nex --resume`. This script automates exactly that: it spawns nex
25
+ // as a child process and watches HOW it exits. Node's child_process
26
+ // reports the terminating signal directly (no exit-code-to-signal
27
+ // guesswork needed) — if nex died from SIGKILL or SIGTERM specifically,
28
+ // this relaunches it with `--resume last` so the run continues from the
29
+ // last saved turn instead of silently stopping. Any OTHER exit (success,
30
+ // a real error, Ctrl+C which nex already handles gracefully) stops the
31
+ // watch loop — this is a safety net for the un-catchable kill case, not a
32
+ // generic "keep retrying forever" wrapper.
33
+ //
34
+ // A capped retry count (not infinite) matters: if the machine is so starved
35
+ // of memory that every relaunch gets killed again within seconds, retrying
36
+ // forever would just contribute to the same memory pressure that caused the
37
+ // first kill. Capping means it gives up loudly instead of thrashing.
38
+ // Cross-platform note: on Windows there is no SIGKILL/SIGTERM in the POSIX
39
+ // sense — a killed process there typically just reports a non-zero exit
40
+ // code with signal:null. This tool's relaunch-on-signal logic is therefore
41
+ // a no-op (and harmless) on Windows; the memory-pressure-kill scenario it
42
+ // guards against is a macOS/Linux OOM-killer phenomenon in the first place.
43
+
44
+ import { spawnSync } from 'child_process';
45
+
46
+ const MAX_RETRIES = Number(process.env.NEX_WATCH_MAX_RETRIES) > 0
47
+ ? Math.floor(Number(process.env.NEX_WATCH_MAX_RETRIES))
48
+ : 5;
49
+ const NEX_BIN = process.env.NEX_WATCH_BIN || 'nex';
50
+
51
+ const isTTY = process.stderr.isTTY === true;
52
+ const c = isTTY
53
+ ? { dim: '\x1b[2m', green: '\x1b[32m', yellow: '\x1b[33m', red: '\x1b[31m', reset: '\x1b[0m' }
54
+ : { dim: '', green: '', yellow: '', red: '', reset: '' };
55
+
56
+ const info = (msg) => process.stderr.write(`${c.dim}[nex-watch]${c.reset} ${msg}\n`);
57
+ const warn = (msg) => process.stderr.write(`${c.yellow}[nex-watch]${c.reset} ${msg}\n`);
58
+ const ok = (msg) => process.stderr.write(`${c.green}[nex-watch]${c.reset} ${msg}\n`);
59
+ const fatal = (msg) => { process.stderr.write(`${c.red}[nex-watch]${c.reset} ${msg}\n`); process.exit(1); };
60
+
61
+ // Kill-type signals worth auto-relaunching on. SIGKILL is the un-catchable
62
+ // OOM/Jetsam case this tool exists for; SIGTERM is included because some
63
+ // low-memory managers (and `docker stop`) send it first before escalating —
64
+ // treating it the same way costs nothing extra since nex has no in-flight
65
+ // state to lose beyond what's already persisted.
66
+ const KILL_SIGNALS = new Set(['SIGKILL', 'SIGTERM']);
67
+
68
+ const userArgs = process.argv.slice(2);
69
+
70
+ let attempt = 0;
71
+ for (;;) {
72
+ const args = attempt === 0 ? userArgs : ['--resume', 'last', ...userArgs];
73
+ if (attempt === 0) {
74
+ info(`starting: ${NEX_BIN} ${args.join(' ')}`.trim());
75
+ } else {
76
+ info(`relaunch #${attempt}: ${NEX_BIN} ${args.join(' ')}`.trim());
77
+ }
78
+
79
+ const result = spawnSync(NEX_BIN, args, { stdio: 'inherit' });
80
+
81
+ if (result.error) {
82
+ // e.g. ENOENT — nex isn't on PATH. Not a kill scenario; fail immediately
83
+ // with a clear message rather than retrying something that can't work.
84
+ fatal(`failed to launch '${NEX_BIN}': ${result.error.message}. Install it first: npm install -g nexrall-code`);
85
+ }
86
+
87
+ if (result.signal && KILL_SIGNALS.has(result.signal)) {
88
+ attempt += 1;
89
+ if (attempt > MAX_RETRIES) {
90
+ fatal(
91
+ `nex was killed (${result.signal}) ${MAX_RETRIES} times in a row — giving up. ` +
92
+ `This usually means the machine is critically low on memory; close other applications ` +
93
+ `before retrying. Progress is saved — run 'nex --resume last' manually once memory is free.`
94
+ );
95
+ }
96
+ warn(
97
+ `nex was killed (${result.signal}) — this is the OS's low-memory killer, not a nex bug. ` +
98
+ `Progress up to the last saved turn is safe. Retrying (${attempt}/${MAX_RETRIES}) in 2s...`
99
+ );
100
+ // Synchronous sleep via spawnSync on a no-op — avoids pulling in an async
101
+ // main() just for one delay, keeping this script a simple top-level loop.
102
+ spawnSync(process.platform === 'win32' ? 'timeout' : 'sleep', [process.platform === 'win32' ? '/t 2' : '2'], { stdio: 'ignore', shell: process.platform === 'win32' });
103
+ continue;
104
+ }
105
+
106
+ // Any other outcome — normal exit, a real error, or a signal nex already
107
+ // handles gracefully (e.g. SIGINT from Ctrl+C) — stop here and mirror the
108
+ // child's result so scripts calling nex-watch see the same status nex itself
109
+ // would have produced.
110
+ if (result.status === 0) ok('nex exited normally.');
111
+ if (result.signal) {
112
+ // A non-kill signal (rare) — exit with the conventional 128+n code.
113
+ const SIGNUMS = { SIGINT: 2, SIGHUP: 1, SIGQUIT: 3 };
114
+ process.exit(128 + (SIGNUMS[result.signal] || 0));
115
+ }
116
+ process.exit(result.status ?? 1);
117
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nexrall-code",
3
- "version": "0.5.75",
3
+ "version": "0.5.77",
4
4
  "description": "Nexrall Code — AI coding assistant for your terminal (headless agent for scripts, CI and automation)",
5
5
  "keywords": [
6
6
  "ai",
@@ -20,13 +20,15 @@
20
20
  "license": "MIT",
21
21
  "type": "module",
22
22
  "bin": {
23
- "nex": "dist/index.js"
23
+ "nex": "dist/index.js",
24
+ "nex-watch": "nex-watch.js"
24
25
  },
25
26
  "main": "dist/index.js",
26
27
  "files": [
27
28
  "dist",
28
29
  "install.sh",
29
- "install.ps1"
30
+ "install.ps1",
31
+ "nex-watch.js"
30
32
  ],
31
33
  "engines": {
32
34
  "node": ">=18"
@@ -39,7 +41,7 @@
39
41
  "release": "node build.js && node scripts/upload-release.cjs"
40
42
  },
41
43
  "dependencies": {
42
- "@nexrall/code-core": "^1.4.44",
44
+ "@nexrall/code-core": "^1.4.45",
43
45
  "chalk": "^5.3.0",
44
46
  "commander": "^12.0.0",
45
47
  "diff": "^5.2.0",