@bastani/pi-ai 0.9.35-alpha.2 → 0.9.35-alpha.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
@@ -4,6 +4,24 @@ This package is a Bastani fork of `@earendil-works/pi-ai`. Upstream history at t
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [0.9.35-alpha.4] - 2026-10-13
8
+
9
+ ### Added
10
+
11
+ - Added `personalizeOpenRouterCatalog()` to restrict OpenRouter chat catalogs to the models a key may use, with its context limits, output limits and prices from `/models/user`. Image and classifier models are preserved.
12
+ - Added `RefreshModelsContext.baseUrl` so catalog refreshes can use the registered provider's configured endpoint.
13
+
14
+ ### Fixed
15
+
16
+ - Personalized OpenRouter catalogs now treat negative placeholder prices, such as those returned for `openrouter/auto`, as zero instead of negative token costs.
17
+
18
+ ## [0.9.35-alpha.3] - 2026-10-12
19
+
20
+ ### Fixed
21
+
22
+ - Fixed Anthropic requests rejected by strict-tool capacity limits by retrying once with ordinary schemas for tools marked `strict: "prefer"`. Required strict tools remain unchanged.
23
+ - Fixed "The upstream provider returned an error" not being retried as a transient provider error ([#10842](https://github.com/earendil-works/pi/issues/10842)).
24
+
7
25
  ## [0.9.35-alpha.1] - 2026-10-12
8
26
 
9
27
  ### Changed
@@ -1 +1 @@
1
- {"version":3,"file":"anthropic-messages.d.ts","sourceRoot":"","sources":["../../src/api/anthropic-messages.ts"],"names":[],"mappings":"AAAA,OAAO,SAAiC,MAAM,mBAAmB,CAAC;AAiBlE,OAAO,KAAK,EASX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAOb,MAAM,aAAa,CAAC;AAoKrB,MAAM,MAAM,eAAe,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,CAAC;AAE1E,MAAM,MAAM,wBAAwB,GAAG,YAAY,GAAG,SAAS,CAAC;AAmPhE,MAAM,WAAW,gBAAiB,SAAQ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;;;;;;;;;OAWG;IACH,eAAe,CAAC,EAAE,wBAAwB,CAAC;IAC3C;;;;;OAKG;IACH,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACtE;;;;OAIG;IACH,MAAM,CAAC,EAAE,SAAS,CAAC;CACnB;AAgSD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,oBAAoB,EAAE,gBAAgB,CAyZzE,CAAC;AA4BF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,oBAAoB,EAAE,mBAAmB,CAgDlF,CAAC"}
@@ -609,22 +609,51 @@ export const stream = (model, context, options) => {
609
609
  client = created.client;
610
610
  isOAuth = created.isOAuthToken;
611
611
  }
612
- let params = buildParams(model, normalizedContext, isOAuth, options);
613
- const nextParams = await options?.onPayload?.(params, model);
614
- if (nextParams !== undefined) {
615
- params = nextParams;
616
- }
617
- assertPayloadPreservesFastRoute(model, params);
618
612
  const requestOptions = {
619
613
  ...(streamDeadline.signal ? { signal: streamDeadline.signal } : {}),
620
614
  ...(options?.timeoutMs !== undefined ? { timeout: options.timeoutMs } : {}),
621
615
  maxRetries: 0,
622
616
  };
623
- const response = await retryProviderRequest(() => client.beta.messages.create({ ...params, stream: true }, requestOptions).asResponse(), {
624
- maxRetries: options?.maxRetries,
625
- maxRetryDelayMs: options?.maxRetryDelayMs,
626
- signal: streamDeadline.signal,
627
- });
617
+ let disablePreferredStrictTools = false;
618
+ let response;
619
+ for (;;) {
620
+ streamDeadline.signal?.throwIfAborted();
621
+ const built = buildParams(model, normalizedContext, isOAuth, options, disablePreferredStrictTools);
622
+ let params = built.params;
623
+ const nextParams = await options?.onPayload?.(params, model);
624
+ if (nextParams !== undefined)
625
+ params = nextParams;
626
+ assertPayloadPreservesFastRoute(model, params);
627
+ try {
628
+ response = await retryProviderRequest(() => client.beta.messages.create({ ...params, stream: true }, requestOptions).asResponse(), {
629
+ maxRetries: options?.maxRetries,
630
+ maxRetryDelayMs: options?.maxRetryDelayMs,
631
+ signal: streamDeadline.signal,
632
+ });
633
+ break;
634
+ }
635
+ catch (error) {
636
+ if (streamDeadline.signal?.aborted ||
637
+ disablePreferredStrictTools ||
638
+ !built.hasPreferredStrictTools ||
639
+ !(error instanceof Anthropic.APIError) ||
640
+ error.status !== 400 ||
641
+ error.type !== "invalid_request_error" ||
642
+ ![
643
+ "The compiled grammar is too large",
644
+ "Too many strict tools (",
645
+ "Schemas contains too many parameters with union types (",
646
+ ].some((message) => error.message.includes(message)))
647
+ throw error;
648
+ disablePreferredStrictTools = true;
649
+ appendAssistantMessageDiagnostic(output, {
650
+ type: "anthropic_strict_tools_fallback",
651
+ timestamp: Date.now(),
652
+ error: { message: error.message },
653
+ details: { reason: "strict_tool_limits_exceeded" },
654
+ });
655
+ }
656
+ }
628
657
  await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
629
658
  stream.push({ type: "start", partial: output });
630
659
  const blocks = output.content;
@@ -1094,18 +1123,28 @@ function getNativeToolChangeBetas(model, context, isOAuth, options) {
1094
1123
  features.push(FAST_MODE_BETA);
1095
1124
  return [...new Set(features)];
1096
1125
  }
1097
- function buildParams(model, context, isOAuthToken, options) {
1126
+ function buildParams(model, context, isOAuthToken, options, disablePreferredStrictTools = false) {
1098
1127
  const { cacheControl } = getCacheControl(model, options?.cacheRetention, options?.env);
1099
1128
  const compat = getAnthropicCompat(model);
1100
1129
  const initialSystemMessage = getInitialSystemMessage(context.messages);
1101
1130
  const initialSystemText = initialSystemMessage ? getSystemMessageText(initialSystemMessage) : "";
1102
1131
  const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
1103
1132
  const conversationMessages = initialSystemMessage ? transformedMessages.slice(1) : transformedMessages;
1133
+ let hasPreferredStrictTools = false;
1134
+ const convertToolDefinitions = (tools, toolCacheControl) => {
1135
+ if (isOAuthToken)
1136
+ tools = [...new Map(tools.map((tool) => [toClaudeCodeName(tool.name), tool])).values()];
1137
+ const definitions = convertTools(tools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl, disablePreferredStrictTools);
1138
+ if (definitions.some((definition, index) => {
1139
+ const config = tools[index].constrainedSampling;
1140
+ return definition.strict === true && config && config.type === "json_schema" && config.strict === "prefer";
1141
+ }))
1142
+ hasPreferredStrictTools = true;
1143
+ return definitions;
1144
+ };
1104
1145
  const initialTools = initialSystemMessage?.toolsAdded ?? [];
1105
1146
  const nativeToolChanges = compat.supportsMidConvoSystemMessages && compat.supportsMidConvoToolChanges && initialTools.length > 0;
1106
- const converted = convertMessages(conversationMessages, isOAuthToken, cacheControl, compat.allowEmptySignature, supportsMidConvoEffort(model) ? model.provider : undefined, nativeToolChanges
1107
- ? (tools) => convertTools(tools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools)
1108
- : undefined);
1147
+ const converted = convertMessages(conversationMessages, isOAuthToken, cacheControl, compat.allowEmptySignature, supportsMidConvoEffort(model) ? model.provider : undefined, nativeToolChanges ? convertToolDefinitions : undefined);
1109
1148
  const activeEffort = options?.effort ?? "high";
1110
1149
  const params = {
1111
1150
  model: upstreamModelId(model),
@@ -1156,15 +1195,12 @@ function buildParams(model, context, isOAuthToken, options) {
1156
1195
  }
1157
1196
  const toolCacheControl = compat.supportsCacheControlOnTools ? cacheControl : undefined;
1158
1197
  if (nativeToolChanges) {
1159
- params.tools = [
1160
- ...convertTools(initialTools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl),
1161
- DEFERRED_TOOL_PLACEHOLDER,
1162
- ];
1198
+ params.tools = [...convertToolDefinitions(initialTools, toolCacheControl), DEFERRED_TOOL_PLACEHOLDER];
1163
1199
  }
1164
1200
  else {
1165
1201
  const tools = getCurrentTools(context.messages);
1166
1202
  if (tools.length > 0) {
1167
- params.tools = convertTools(tools, isOAuthToken, compat.supportsEagerToolInputStreaming, compat.supportsStrictTools, toolCacheControl);
1203
+ params.tools = convertToolDefinitions(tools, toolCacheControl);
1168
1204
  }
1169
1205
  }
1170
1206
  // Managed effort models always use adaptive thinking. Their per-turn markers
@@ -1265,7 +1301,7 @@ function buildParams(model, context, isOAuthToken, options) {
1265
1301
  if (allowedFallbackModels && allowedFallbackModels.length > 0) {
1266
1302
  params.fallbacks = allowedFallbackModels.map((fallback) => ({ model: fallback.model }));
1267
1303
  }
1268
- return params;
1304
+ return { params, hasPreferredStrictTools };
1269
1305
  }
1270
1306
  // Normalize tool call IDs to match Anthropic's required pattern and length
1271
1307
  function normalizeToolCallId(id) {
@@ -1559,15 +1595,14 @@ function projectObjectUnionForAnthropic(schema) {
1559
1595
  required: (branches[0]?.required ?? []).filter((key) => branches.every((branch) => (branch.required ?? []).includes(key))),
1560
1596
  };
1561
1597
  }
1562
- function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supportsStrictTools, cacheControl) {
1598
+ function convertTools(tools, isOAuthToken, supportsEagerToolInputStreaming, supportsStrictTools, cacheControl, disablePreferredStrictTools = false) {
1563
1599
  if (!tools)
1564
1600
  return [];
1565
- // OAuth canonicalization can make distinct transcript names collide. Keep the latest
1566
- // declaration, as the pre-transcript tool serializer did, without sending duplicate names.
1567
- if (isOAuthToken)
1568
- tools = [...new Map(tools.map((tool) => [toClaudeCodeName(tool.name), tool])).values()];
1569
1601
  return tools.map((tool, index) => {
1570
- const strict = resolveJsonSchemaStrictSampling(tool, supportsStrictTools, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
1602
+ const config = tool.constrainedSampling;
1603
+ const strict = disablePreferredStrictTools && config && config.type === "json_schema" && config.strict === "prefer"
1604
+ ? undefined
1605
+ : resolveJsonSchemaStrictSampling(tool, supportsStrictTools, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
1571
1606
  const parameters = getJsonSchemaToolParameters(tool, strict, ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE);
1572
1607
  const schema = parameters;
1573
1608
  // Anthropic rejects top-level combinators, so project object-union fields for advertising only.