@prestyj/ai 5.3.0 → 5.3.1

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.cjs CHANGED
@@ -1091,6 +1091,12 @@ function parseToolArguments(argsJson) {
1091
1091
  // src/providers/anthropic.ts
1092
1092
  var NON_STREAMING_TIMEOUT_MS = 60 * 60 * 1e3;
1093
1093
  var anthropicClientCache = /* @__PURE__ */ new Map();
1094
+ function fineGrainedToolStreamingEnabled() {
1095
+ const raw = process.env.GG_FINE_GRAINED_TOOL_STREAMING ?? process.env.CLAUDE_CODE_ENABLE_FINE_GRAINED_TOOL_STREAMING;
1096
+ if (!raw) return false;
1097
+ const v = raw.trim().toLowerCase();
1098
+ return v === "1" || v === "true" || v === "yes" || v === "on";
1099
+ }
1094
1100
  function createClient(options) {
1095
1101
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
1096
1102
  const userAgent = isOAuth ? options.userAgent ?? "claude-cli/2.1.75 (external, cli)" : "";
@@ -1148,7 +1154,9 @@ async function prewarmAnthropicCache(options) {
1148
1154
  ] : system;
1149
1155
  const tools = options.tools?.length ? toAnthropicTools(options.tools, {
1150
1156
  cacheControl,
1151
- enableFineGrainedToolStreaming: true
1157
+ // Keep the serialized tool bytes identical to runStream so the
1158
+ // prewarmed prompt cache actually hits — both are gated by the flag.
1159
+ enableFineGrainedToolStreaming: fineGrainedToolStreamingEnabled()
1152
1160
  }) : void 0;
1153
1161
  await client.messages.create(
1154
1162
  {
@@ -1228,7 +1236,7 @@ async function* runStream(options) {
1228
1236
  options.tools.filter((t) => !reservedServerNames.has(t.name)),
1229
1237
  {
1230
1238
  ...supportsFirstPartyToolExtras && cacheControl ? { cacheControl } : {},
1231
- ...supportsFirstPartyToolExtras ? { enableFineGrainedToolStreaming: true } : {}
1239
+ ...supportsFirstPartyToolExtras && fineGrainedToolStreamingEnabled() ? { enableFineGrainedToolStreaming: true } : {}
1232
1240
  }
1233
1241
  ) : [];
1234
1242
  return {
@@ -1254,7 +1262,10 @@ async function* runStream(options) {
1254
1262
  ...isOAuth ? ["claude-code-20250219", "oauth-2025-04-20"] : [],
1255
1263
  ...options.compaction ? ["compact-2026-01-12"] : [],
1256
1264
  ...options.clearToolUses ? ["context-management-2025-06-27"] : [],
1257
- "fine-grained-tool-streaming-2025-05-14",
1265
+ // Eager tool-input streaming beta — opt-in only (see
1266
+ // fineGrainedToolStreamingEnabled). Off by default: the un-buffered stream
1267
+ // truncates large tool payloads into malformed JSON → phantom empty calls.
1268
+ ...fineGrainedToolStreamingEnabled() ? ["fine-grained-tool-streaming-2025-05-14"] : [],
1258
1269
  ...!hasAdaptiveThinking ? ["interleaved-thinking-2025-05-14"] : [],
1259
1270
  // The 1-h cache TTL (cacheRetention "long") is gated behind this beta. Without
1260
1271
  // it Anthropic silently ignores ttl:"1h" and falls back to the 5-min default,
@@ -1387,7 +1398,14 @@ async function* runStream(options) {
1387
1398
  try {
1388
1399
  const parsed = JSON.parse(accum.argsJson);
1389
1400
  args = isJsonObject(parsed) ? parsed : {};
1390
- } catch {
1401
+ } catch (parseErr) {
1402
+ const rawPartial = accum.argsJson;
1403
+ const snippet = rawPartial.length > 200 ? `${rawPartial.slice(0, 200)}\u2026` : rawPartial;
1404
+ throw new ProviderError(
1405
+ "anthropic",
1406
+ `Tool "${accum.toolName}" input JSON was truncated in the stream (${rawPartial.length} bytes): ${snippet}; ${parseErr.message}`,
1407
+ { cause: parseErr }
1408
+ );
1391
1409
  }
1392
1410
  }
1393
1411
  const tc = {
@@ -1608,6 +1626,7 @@ function readUnifiedRateLimit(headers) {
1608
1626
  return { rejected: status === "rejected", ...resetsAt ? { resetsAt } : {} };
1609
1627
  }
1610
1628
  function toError(err) {
1629
+ if (err instanceof ProviderError) return err;
1611
1630
  if (err instanceof import_sdk.default.APIError) {
1612
1631
  const errorBody = err.error;
1613
1632
  const nestedError = errorBody?.error;