@gajae-code/ai 0.9.3 → 0.9.4

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/CHANGELOG.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.9.4] - 2026-07-09
6
+ ### Fixed
7
+
8
+ - Preserved Anthropic OAuth tool-call names and streamed arguments across interleaved tool-use blocks, preventing prefixed tool names and partial JSON deltas from being dropped or misattributed.
9
+
5
10
  ## [0.9.2] - 2026-07-09
6
11
  ### Added
7
12
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@gajae-code/ai",
4
- "version": "0.9.3",
4
+ "version": "0.9.4",
5
5
  "description": "Unified LLM API with automatic model discovery and provider configuration",
6
6
  "homepage": "https://gajae-code.com",
7
7
  "author": "Yeachan-Heo and Gajae Code Contributors",
@@ -40,7 +40,7 @@
40
40
  "dependencies": {
41
41
  "@anthropic-ai/sdk": "^0.94.0",
42
42
  "@bufbuild/protobuf": "^2.12.0",
43
- "@gajae-code/utils": "0.9.3",
43
+ "@gajae-code/utils": "0.9.4",
44
44
  "openai": "^6.36.0",
45
45
  "partial-json": "^0.1.7",
46
46
  "zod": "4.4.3"
package/src/models.ts CHANGED
@@ -1,6 +1,12 @@
1
- import { createRequire } from "node:module";
1
+ import { readFileSync } from "node:fs";
2
2
  import { isRetiredModelKey } from "./model-retirements";
3
3
  import { applyGeneratedModelPolicies, enrichModelThinking } from "./model-thinking";
4
+ // `with { type: "file" }` is embedded by `bun build --compile` and resolves to
5
+ // the bunfs path inside standalone binaries (and to the on-disk path in dev).
6
+ // A plain `createRequire` of a `.json` listed as an extra compile entrypoint is
7
+ // NOT emitted into the bunfs, and its cwd-fallback masks the failure whenever
8
+ // the process runs inside a repo checkout — see PR body for the minimal repro.
9
+ import modelsJsonPath from "./models.json" with { type: "file" };
4
10
  import type { Api, KnownProvider, Model, Usage } from "./types";
5
11
  import { isClaudeForcedToolChoiceIncapableModelId } from "./utils/tool-choice-capability";
6
12
 
@@ -14,16 +20,14 @@ import { isClaudeForcedToolChoiceIncapableModelId } from "./utils/tool-choice-ca
14
20
  */
15
21
  type BundledCatalog = typeof import("./models.json");
16
22
 
17
- const require = createRequire(import.meta.url);
18
- const COMPILED_MODELS_PATH = "./packages/ai/src/models.json";
19
23
  let bundledCatalog: BundledCatalog | undefined;
20
24
  let providerNames: KnownProvider[] | undefined;
21
25
  const providerModelRegistry: Map<string, Map<string, Model<Api>>> = new Map();
22
26
 
23
27
  function getBundledCatalog(): BundledCatalog {
24
- bundledCatalog ??= require(
25
- process.env.PI_COMPILED === "true" ? COMPILED_MODELS_PATH : "./models.json",
26
- ) as BundledCatalog;
28
+ // TS types a .json import as its contents; at runtime `with { type: "file" }`
29
+ // yields the file path (bunfs path in compiled binaries, disk path in dev).
30
+ bundledCatalog ??= JSON.parse(readFileSync(modelsJsonPath as unknown as string, "utf8")) as BundledCatalog;
27
31
  return bundledCatalog;
28
32
  }
29
33
 
@@ -586,15 +586,12 @@ const ANTHROPIC_BUILTIN_TOOL_NAMES = new Set(["web_search", "code_execution", "t
586
586
  export const applyClaudeToolPrefix = (name: string, prefixOverride: string = claudeToolPrefix) => {
587
587
  if (!prefixOverride) return name;
588
588
  if (ANTHROPIC_BUILTIN_TOOL_NAMES.has(name.toLowerCase())) return name;
589
- const prefix = prefixOverride.toLowerCase();
590
- if (name.toLowerCase().startsWith(prefix)) return name;
591
589
  return `${prefixOverride}${name}`;
592
590
  };
593
591
 
594
592
  export const stripClaudeToolPrefix = (name: string, prefixOverride: string = claudeToolPrefix) => {
595
593
  if (!prefixOverride) return name;
596
- const prefix = prefixOverride.toLowerCase();
597
- if (!name.toLowerCase().startsWith(prefix)) return name;
594
+ if (!name.startsWith(prefixOverride)) return name;
598
595
  return name.slice(prefixOverride.length);
599
596
  };
600
597
 
@@ -1325,6 +1322,25 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1325
1322
  | (ToolCall & { partialJson: string })
1326
1323
  ) & { index: number };
1327
1324
  const blocks = output.content as Block[];
1325
+ const blocksByAnthropicIndex = new Map<number, Block>();
1326
+ const getBlockByAnthropicIndex = (anthropicIndex: number) => {
1327
+ const block = blocksByAnthropicIndex.get(anthropicIndex);
1328
+ if (!block) return { block: undefined, contentIndex: -1 };
1329
+ return { block, contentIndex: blocks.indexOf(block) };
1330
+ };
1331
+ const trackBlockByAnthropicIndex = (anthropicIndex: number, block: Block) => {
1332
+ // A duplicate start for an active index is a provider-envelope violation;
1333
+ // finalize the orphaned block so no internal stream fields leak into output.
1334
+ const orphaned = blocksByAnthropicIndex.get(anthropicIndex);
1335
+ if (orphaned) {
1336
+ if (orphaned.type === "toolCall" && orphaned.partialJson.trim()) {
1337
+ orphaned.arguments = parseStreamingJson(orphaned.partialJson);
1338
+ }
1339
+ delete (orphaned as { index?: number }).index;
1340
+ delete (orphaned as { partialJson?: string }).partialJson;
1341
+ }
1342
+ blocksByAnthropicIndex.set(anthropicIndex, block);
1343
+ };
1328
1344
  const idleTimeoutMs = options?.streamIdleTimeoutMs ?? getStreamIdleTimeoutMs();
1329
1345
  const firstEventTimeoutMs = options?.streamFirstEventTimeoutMs ?? getStreamFirstEventTimeoutMs(idleTimeoutMs);
1330
1346
  stream.push({ type: "start", partial: output });
@@ -1334,6 +1350,8 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1334
1350
  let providerRetryAttempt = 0;
1335
1351
  let thinkingRepairAttempted = false;
1336
1352
  while (true) {
1353
+ // Retries reset output.content; drop stale block correlations from the aborted attempt.
1354
+ blocksByAnthropicIndex.clear();
1337
1355
  activeAbortTracker = createAbortSourceTracker(options?.signal);
1338
1356
  const firstEventTimeoutAbortError = new Error(
1339
1357
  "Anthropic stream timed out while waiting for the first event",
@@ -1403,6 +1421,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1403
1421
  index: event.index,
1404
1422
  };
1405
1423
  output.content.push(block);
1424
+ trackBlockByAnthropicIndex(event.index, block);
1406
1425
  stream.push({
1407
1426
  type: "text_start",
1408
1427
  contentIndex: output.content.length - 1,
@@ -1416,6 +1435,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1416
1435
  index: event.index,
1417
1436
  };
1418
1437
  output.content.push(block);
1438
+ trackBlockByAnthropicIndex(event.index, block);
1419
1439
  stream.push({
1420
1440
  type: "thinking_start",
1421
1441
  contentIndex: output.content.length - 1,
@@ -1428,6 +1448,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1428
1448
  index: event.index,
1429
1449
  };
1430
1450
  output.content.push(block);
1451
+ trackBlockByAnthropicIndex(event.index, block);
1431
1452
  } else if (event.content_block.type === "tool_use") {
1432
1453
  streamedReplayUnsafeContent = true;
1433
1454
  const block: Block = {
@@ -1441,6 +1462,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1441
1462
  index: event.index,
1442
1463
  };
1443
1464
  output.content.push(block);
1465
+ trackBlockByAnthropicIndex(event.index, block);
1444
1466
  stream.push({
1445
1467
  type: "toolcall_start",
1446
1468
  contentIndex: output.content.length - 1,
@@ -1449,8 +1471,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1449
1471
  }
1450
1472
  } else if (event.type === "content_block_delta") {
1451
1473
  if (event.delta.type === "text_delta") {
1452
- const index = blocks.findIndex(b => b.index === event.index);
1453
- const block = blocks[index];
1474
+ const { block, contentIndex: index } = getBlockByAnthropicIndex(event.index);
1454
1475
  if (block && block.type === "text") {
1455
1476
  block.text += event.delta.text;
1456
1477
  stream.push({
@@ -1461,8 +1482,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1461
1482
  });
1462
1483
  }
1463
1484
  } else if (event.delta.type === "thinking_delta") {
1464
- const index = blocks.findIndex(b => b.index === event.index);
1465
- const block = blocks[index];
1485
+ const { block, contentIndex: index } = getBlockByAnthropicIndex(event.index);
1466
1486
  if (block && block.type === "thinking") {
1467
1487
  block.thinking += event.delta.thinking;
1468
1488
  stream.push({
@@ -1473,8 +1493,7 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1473
1493
  });
1474
1494
  }
1475
1495
  } else if (event.delta.type === "input_json_delta") {
1476
- const index = blocks.findIndex(b => b.index === event.index);
1477
- const block = blocks[index];
1496
+ const { block, contentIndex: index } = getBlockByAnthropicIndex(event.index);
1478
1497
  if (block && block.type === "toolCall") {
1479
1498
  block.partialJson += event.delta.partial_json;
1480
1499
  block.arguments = parseStreamingJson(block.partialJson);
@@ -1486,17 +1505,16 @@ export const streamAnthropic: StreamFunction<"anthropic-messages"> = (
1486
1505
  });
1487
1506
  }
1488
1507
  } else if (event.delta.type === "signature_delta") {
1489
- const index = blocks.findIndex(b => b.index === event.index);
1490
- const block = blocks[index];
1508
+ const { block } = getBlockByAnthropicIndex(event.index);
1491
1509
  if (block && block.type === "thinking") {
1492
1510
  block.thinkingSignature = block.thinkingSignature || "";
1493
1511
  block.thinkingSignature += event.delta.signature;
1494
1512
  }
1495
1513
  }
1496
1514
  } else if (event.type === "content_block_stop") {
1497
- const index = blocks.findIndex(b => b.index === event.index);
1498
- const block = blocks[index];
1515
+ const { block, contentIndex: index } = getBlockByAnthropicIndex(event.index);
1499
1516
  if (block) {
1517
+ blocksByAnthropicIndex.delete(event.index);
1500
1518
  delete (block as { index?: number }).index;
1501
1519
  if (block.type === "text") {
1502
1520
  stream.push({