iubcli 5.0.3 → 5.0.6

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.
package/dist/index.js CHANGED
@@ -1419,7 +1419,6 @@ async function webSearch(query) {
1419
1419
  "Accept": "application/json"
1420
1420
  }
1421
1421
  });
1422
- clearTimeout(timeout);
1423
1422
  if (res.ok) {
1424
1423
  const data = await res.json();
1425
1424
  const results = [];
@@ -3584,19 +3583,28 @@ async function readInlinePrompt(options) {
3584
3583
  async function readOpenCodePrompt(options) {
3585
3584
  return readInlinePrompt(options);
3586
3585
  }
3587
- async function expandOpenCodeMentions(rawPrompt) {
3586
+ async function expandOpenCodeMentions(rawPrompt, signal) {
3588
3587
  const attachedFiles = [];
3589
3588
  const attachedImages = [];
3590
3589
  const contexts = [];
3590
+ if (signal?.aborted) {
3591
+ return { expandedPrompt: rawPrompt, attachedFiles, attachedImages };
3592
+ }
3591
3593
  const urlRegex = /(?:@)?(https?:\/\/[^\s"'<>]+)/g;
3592
3594
  const urlMatches = Array.from(rawPrompt.matchAll(urlRegex));
3593
3595
  const seenUrls = /* @__PURE__ */ new Set();
3594
3596
  for (const match of urlMatches) {
3597
+ if (signal?.aborted) break;
3595
3598
  const targetUrl = match[1].replace(/[.,;:!?)]+$/, "");
3596
3599
  if (!targetUrl || seenUrls.has(targetUrl)) continue;
3597
3600
  seenUrls.add(targetUrl);
3598
3601
  const controller = new AbortController();
3599
3602
  const timeout = setTimeout(() => controller.abort(), 6e3);
3603
+ const onAbort = () => controller.abort();
3604
+ if (signal) {
3605
+ if (signal.aborted) controller.abort();
3606
+ else signal.addEventListener("abort", onAbort, { once: true });
3607
+ }
3600
3608
  try {
3601
3609
  const res = await fetch(targetUrl, {
3602
3610
  signal: controller.signal,
@@ -3623,12 +3631,17 @@ async function expandOpenCodeMentions(rawPrompt) {
3623
3631
  } catch {
3624
3632
  } finally {
3625
3633
  clearTimeout(timeout);
3634
+ signal?.removeEventListener("abort", onAbort);
3626
3635
  }
3627
3636
  }
3637
+ if (signal?.aborted) {
3638
+ return { expandedPrompt: rawPrompt, attachedFiles, attachedImages };
3639
+ }
3628
3640
  const fileRegex = /(?:^|\s)@([a-zA-Z0-9_\-\.\/\\]+)/g;
3629
3641
  const fileMatches = Array.from(rawPrompt.matchAll(fileRegex));
3630
3642
  const seenPaths = /* @__PURE__ */ new Set();
3631
3643
  for (const match of fileMatches) {
3644
+ if (signal?.aborted) break;
3632
3645
  const rawPath = match[1];
3633
3646
  if (rawPath.startsWith("http://") || rawPath.startsWith("https://")) continue;
3634
3647
  const resolvedPath = path4.resolve(process.cwd(), rawPath);
@@ -3680,6 +3693,7 @@ async function expandOpenCodeMentions(rawPrompt) {
3680
3693
  const rawImageRegex = /(?:^|\s)["']?([a-zA-Z]:[\\/][^\r\n"']+\.(?:png|jpg|jpeg|webp|gif|bmp)|(?:\.{1,2}[\\/]|[a-zA-Z0-9_\-]+[\\/])[^\r\n"']+\.(?:png|jpg|jpeg|webp|gif|bmp))["']?(?=\s|$)/gi;
3681
3694
  const rawImageMatches = Array.from(rawPrompt.matchAll(rawImageRegex));
3682
3695
  for (const m of rawImageMatches) {
3696
+ if (signal?.aborted) break;
3683
3697
  const rawPath = m[1].replace(/["']/g, "").trim();
3684
3698
  const resolvedPath = path4.isAbsolute(rawPath) ? rawPath : path4.resolve(process.cwd(), rawPath);
3685
3699
  if (fs4.existsSync(resolvedPath) && !attachedImages.some((img) => img.path === resolvedPath)) {
@@ -6150,22 +6164,41 @@ function detectProjectContext(rootDir = process.cwd()) {
6150
6164
  // src/explorer.ts
6151
6165
  import { GoogleGenAI } from "@google/genai";
6152
6166
  import chalk7 from "chalk";
6153
- async function probeCandidate(candidate) {
6167
+ function probeAbortSignal(timeoutMs, signal) {
6168
+ const controller = new AbortController();
6169
+ const timer = setTimeout(() => controller.abort(), timeoutMs);
6170
+ const onAbort = () => controller.abort();
6171
+ if (signal) {
6172
+ if (signal.aborted) {
6173
+ controller.abort();
6174
+ } else {
6175
+ signal.addEventListener("abort", onAbort, { once: true });
6176
+ }
6177
+ }
6178
+ return {
6179
+ signal: controller.signal,
6180
+ cleanup: () => {
6181
+ clearTimeout(timer);
6182
+ if (signal) signal.removeEventListener("abort", onAbort);
6183
+ }
6184
+ };
6185
+ }
6186
+ async function probeCandidate(candidate, signal) {
6154
6187
  const timeoutMs = 5e3;
6188
+ if (signal?.aborted) return false;
6155
6189
  if (candidate.provider === "custom" && candidate.baseURL?.toLowerCase().includes("pollinations.ai")) {
6156
- const controller2 = new AbortController();
6157
- const timer = setTimeout(() => controller2.abort(), timeoutMs);
6190
+ const gate2 = probeAbortSignal(timeoutMs, signal);
6158
6191
  try {
6159
- const res = await fetch("https://text.pollinations.ai/models", { signal: controller2.signal });
6160
- clearTimeout(timer);
6192
+ const res = await fetch("https://text.pollinations.ai/models", { signal: gate2.signal });
6161
6193
  return res.ok;
6162
6194
  } catch {
6163
- clearTimeout(timer);
6164
6195
  return false;
6196
+ } finally {
6197
+ gate2.cleanup();
6165
6198
  }
6166
6199
  }
6167
6200
  if (candidate.provider === "gemini") {
6168
- let timer;
6201
+ const gate2 = probeAbortSignal(timeoutMs, signal);
6169
6202
  try {
6170
6203
  const ai = new GoogleGenAI({ apiKey: candidate.apiKey });
6171
6204
  const normalized = normalizeGeminiModel(candidate.id);
@@ -6176,23 +6209,24 @@ async function probeCandidate(candidate) {
6176
6209
  config: { maxOutputTokens: 10 }
6177
6210
  }),
6178
6211
  new Promise((_, reject) => {
6179
- timer = setTimeout(() => reject(new Error("PROBE_TIMEOUT")), timeoutMs);
6212
+ const fail = () => reject(new Error("PROBE_ABORTED"));
6213
+ if (gate2.signal.aborted) fail();
6214
+ else gate2.signal.addEventListener("abort", fail, { once: true });
6180
6215
  })
6181
6216
  ]);
6182
- if (timer) clearTimeout(timer);
6183
6217
  return Boolean(res && (res.text !== void 0 || res.candidates && res.candidates.length > 0));
6184
6218
  } catch {
6185
- if (timer) clearTimeout(timer);
6186
6219
  return false;
6220
+ } finally {
6221
+ gate2.cleanup();
6187
6222
  }
6188
6223
  }
6189
- const controller = new AbortController();
6190
- const timeout = setTimeout(() => controller.abort(), timeoutMs);
6224
+ const gate = probeAbortSignal(timeoutMs, signal);
6191
6225
  try {
6192
6226
  const targetUrl = candidate.baseURL ? candidate.baseURL.replace(/\/+$/, "") + "/chat/completions" : "https://openrouter.ai/api/v1/chat/completions";
6193
6227
  const res = await fetch(targetUrl, {
6194
6228
  method: "POST",
6195
- signal: controller.signal,
6229
+ signal: gate.signal,
6196
6230
  headers: {
6197
6231
  "Content-Type": "application/json",
6198
6232
  Authorization: `Bearer ${candidate.apiKey}`,
@@ -6205,7 +6239,6 @@ async function probeCandidate(candidate) {
6205
6239
  max_tokens: 10
6206
6240
  })
6207
6241
  });
6208
- clearTimeout(timeout);
6209
6242
  if (!res.ok) {
6210
6243
  return false;
6211
6244
  }
@@ -6221,11 +6254,15 @@ async function probeCandidate(candidate) {
6221
6254
  }
6222
6255
  return Boolean(data?.choices && data.choices.length > 0);
6223
6256
  } catch {
6224
- clearTimeout(timeout);
6225
6257
  return false;
6258
+ } finally {
6259
+ gate.cleanup();
6226
6260
  }
6227
6261
  }
6228
- async function exploreAndSelectFreeModel(config, excludedModels = /* @__PURE__ */ new Set(), silent = false, persist = true) {
6262
+ async function exploreAndSelectFreeModel(config, excludedModels = /* @__PURE__ */ new Set(), silent = false, persist = true, signal) {
6263
+ if (signal?.aborted) {
6264
+ return null;
6265
+ }
6229
6266
  if (!silent) {
6230
6267
  process.stdout.write(
6231
6268
  chalk7.cyan("\nExploring all available models across providers to find active free tokens...\n")
@@ -6278,8 +6315,10 @@ async function exploreAndSelectFreeModel(config, excludedModels = /* @__PURE__ *
6278
6315
  });
6279
6316
  }
6280
6317
  }
6318
+ if (signal?.aborted) return null;
6281
6319
  try {
6282
6320
  const liveFree = await fetchLiveOpenRouterFreeModels();
6321
+ if (signal?.aborted) return null;
6283
6322
  for (const m of liveFree) {
6284
6323
  pushCandidate({
6285
6324
  id: m.id,
@@ -6324,6 +6363,7 @@ async function exploreAndSelectFreeModel(config, excludedModels = /* @__PURE__ *
6324
6363
  });
6325
6364
  }
6326
6365
  }
6366
+ if (signal?.aborted) return null;
6327
6367
  try {
6328
6368
  const localOllama = await detectLocalOllamaModels(500);
6329
6369
  for (const m of localOllama) {
@@ -6354,13 +6394,15 @@ async function exploreAndSelectFreeModel(config, excludedModels = /* @__PURE__ *
6354
6394
  const chunkSize = 4;
6355
6395
  let testedCount = 0;
6356
6396
  for (let i = 0; i < candidatePool.length; i += chunkSize) {
6397
+ if (signal?.aborted) return null;
6357
6398
  const chunk = candidatePool.slice(i, i + chunkSize);
6358
6399
  testedCount += chunk.length;
6359
6400
  const probePromises = chunk.map(async (candidate) => {
6360
- const ok = await probeCandidate(candidate);
6401
+ const ok = await probeCandidate(candidate, signal);
6361
6402
  return ok ? candidate : null;
6362
6403
  });
6363
6404
  const results = await Promise.all(probePromises);
6405
+ if (signal?.aborted) return null;
6364
6406
  const winner = results.find((r) => r !== null);
6365
6407
  if (winner) {
6366
6408
  config.model = winner.id;
@@ -6959,7 +7001,14 @@ ${String(out).slice(0, 6e3)}`);
6959
7001
  if (nextOR) return nextOR;
6960
7002
  }
6961
7003
  const prevProviderBeforeExplore = this.config.provider;
6962
- const explored = await exploreAndSelectFreeModel(this.config, this.attemptedModelsInCascade, false, false);
7004
+ const explored = await exploreAndSelectFreeModel(
7005
+ this.config,
7006
+ this.attemptedModelsInCascade,
7007
+ false,
7008
+ false,
7009
+ signal
7010
+ );
7011
+ if (signal?.aborted) return null;
6963
7012
  if (explored) {
6964
7013
  this.attemptedModelsInCascade.add(explored.id);
6965
7014
  this.initClients();
@@ -6981,7 +7030,8 @@ ${String(out).slice(0, 6e3)}`);
6981
7030
  this.attemptedModelsInCascade.clear();
6982
7031
  this.deadProvidersInTurn.clear();
6983
7032
  const prevProviderBeforeRecovery = this.config.provider;
6984
- const recovered = await exploreAndSelectFreeModel(this.config, /* @__PURE__ */ new Set(), true, false);
7033
+ const recovered = await exploreAndSelectFreeModel(this.config, /* @__PURE__ */ new Set(), true, false, signal);
7034
+ if (signal?.aborted) return null;
6985
7035
  if (recovered) {
6986
7036
  this.initClients();
6987
7037
  this.syncHistoryForProviderChange(prevProviderBeforeRecovery, this.config.provider);
@@ -6989,6 +7039,7 @@ ${String(out).slice(0, 6e3)}`);
6989
7039
  return recovered.id;
6990
7040
  }
6991
7041
  }
7042
+ if (signal?.aborted) return null;
6992
7043
  if (this.config.baseURL !== POLLINATIONS_BASE_URL || this.config.model !== POLLINATIONS_DEFAULT_MODEL) {
6993
7044
  const prevProviderBeforeFallback = this.config.provider;
6994
7045
  renderCascadeNotice(currentModel, POLLINATIONS_DEFAULT_MODEL, "Switching to Zero-Key Built-in Free AI");
@@ -9449,9 +9500,6 @@ Unknown command "${cmdWord}". Type ${chalk9.cyan("/help")} to see all available
9449
9500
  if (isPriorityTask) {
9450
9501
  renderPriorityTaskBanner(taskPrompt, config.model, agent.getMode());
9451
9502
  }
9452
- const { expandedPrompt, attachedImages } = await expandOpenCodeMentions(taskPrompt);
9453
- const allImages = [...stagedImages, ...attachedImages];
9454
- stagedImages = [];
9455
9503
  const abortController = new AbortController();
9456
9504
  let hasAborted = false;
9457
9505
  const detachEscape = attachEscapeListener(() => {
@@ -9465,14 +9513,23 @@ Unknown command "${cmdWord}". Type ${chalk9.cyan("/help")} to see all available
9465
9513
  });
9466
9514
  });
9467
9515
  try {
9468
- const reply = await agent.chat(expandedPrompt, {
9469
- signal: abortController.signal,
9470
- images: allImages
9471
- });
9516
+ const { expandedPrompt, attachedImages } = await expandOpenCodeMentions(
9517
+ taskPrompt,
9518
+ abortController.signal
9519
+ );
9520
+ const allImages = [...stagedImages, ...attachedImages];
9521
+ stagedImages = [];
9472
9522
  if (abortController.signal.aborted || hasAborted) {
9473
9523
  } else {
9474
- console.log("\n" + chalk9.bold.hex("#00A3FF")("IUB CLI:"));
9475
- console.log(renderMarkdown(reply) + "\n");
9524
+ const reply = await agent.chat(expandedPrompt, {
9525
+ signal: abortController.signal,
9526
+ images: allImages
9527
+ });
9528
+ if (abortController.signal.aborted || hasAborted) {
9529
+ } else {
9530
+ console.log("\n" + chalk9.bold.hex("#00A3FF")("IUB CLI:"));
9531
+ console.log(renderMarkdown(reply) + "\n");
9532
+ }
9476
9533
  }
9477
9534
  } catch (err) {
9478
9535
  if (abortController.signal.aborted || hasAborted) {