@bastani/pi-ai 0.9.46-alpha.1 → 0.9.46

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.
@@ -1 +1 @@
1
- {"version":3,"file":"bedrock-converse-stream.d.ts","sourceRoot":"","sources":["../../src/api/bedrock-converse-stream.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAWX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,EACf,aAAa,EAGb,MAAM,aAAa,CAAC;AAiCrB,MAAM,MAAM,sBAAsB,GAAG,YAAY,GAAG,SAAS,CAAC;AAE9D,MAAM,WAAW,cAAe,SAAQ,aAAa;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAEtE,SAAS,CAAC,EAAE,aAAa,CAAC;IAE1B,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,sBAAsB,CAAC;IACzC;;;sGAGkG;IAClG,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC;;;;yGAIqG;IACrG,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAsBD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,yBAAyB,EAAE,cAAc,CAqP5E,CAAC;AAkLF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,yBAAyB,EAAE,mBAAmB,CAiDvF,CAAC"}
1
+ {"version":3,"file":"bedrock-converse-stream.d.ts","sourceRoot":"","sources":["../../src/api/bedrock-converse-stream.ts"],"names":[],"mappings":"AA+BA,OAAO,KAAK,EAWX,mBAAmB,EAEnB,cAAc,EACd,aAAa,EAEb,eAAe,EACf,aAAa,EAGb,MAAM,aAAa,CAAC;AAmCrB,MAAM,MAAM,sBAAsB,GAAG,YAAY,GAAG,SAAS,CAAC;AAE9D,MAAM,WAAW,cAAe,SAAQ,aAAa;IACpD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,UAAU,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IAEtE,SAAS,CAAC,EAAE,aAAa,CAAC;IAE1B,eAAe,CAAC,EAAE,eAAe,CAAC;IAElC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B;;;;;;;;;OASG;IACH,eAAe,CAAC,EAAE,sBAAsB,CAAC;IACzC;;;sGAGkG;IAClG,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACzC;;;;yGAIqG;IACrG,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB;AAsBD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,yBAAyB,EAAE,cAAc,CA+Q5E,CAAC;AAkLF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,yBAAyB,EAAE,mBAAmB,CAiDvF,CAAC"}
@@ -16,7 +16,7 @@ import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
16
16
  import { ToolCallArgumentsStream } from "../utils/streaming-json.js";
17
17
  import { getSystemMessageText } from "../utils/text.js";
18
18
  import { collapseSystemMessages, getCurrentTools, getInitialSystemMessage, withoutInitialSystemMessage, } from "../utils/transcript.js";
19
- import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
19
+ import { ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE, getJsonSchemaToolParameters, isStrictToolLimitErrorMessage, prefersJsonSchemaStrictSampling, resolveJsonSchemaStrictSampling, } from "./constrained-sampling.js";
20
20
  import { adjustMaxTokensForThinking, buildBaseOptions, clampMaxTokensToContext, clampReasoning, } from "./simple-options.js";
21
21
  import { splitErrorToolResultImages, transformMessages } from "./transform-messages.js";
22
22
  const EMPTY_TEXT_PLACEHOLDER = "<empty>";
@@ -149,29 +149,54 @@ export const stream = (model, context, options = {}) => {
149
149
  const inferenceMaxTokens = options.maxTokens ?? (isAnthropicClaudeModel(model) ? model.maxTokens : undefined);
150
150
  const initialSystemMessage = getInitialSystemMessage(normalizedContext.messages);
151
151
  const initialSystemPrompt = initialSystemMessage ? getSystemMessageText(initialSystemMessage) : undefined;
152
- let commandInput = {
153
- modelId: model.id,
154
- messages: convertMessages(normalizedContext, model, cacheRetention, options.env),
155
- system: buildSystemPrompt(initialSystemPrompt, model, cacheRetention, options.env),
156
- inferenceConfig: {
157
- ...(inferenceMaxTokens !== undefined && { maxTokens: inferenceMaxTokens }),
158
- // Claude Fable 5.1 rejects non-default `temperature`, `top_p`, and `top_k` on every
159
- // request. Generated metadata marks such models `supportsTemperature: false`; every
160
- // other model keeps sending the field exactly as before.
161
- // https://platform.claude.com/docs/en/build-with-claude/thinking
162
- ...(options.temperature !== undefined &&
163
- model.compat?.supportsTemperature !== false && { temperature: options.temperature }),
164
- },
165
- toolConfig: convertToolConfig(getCurrentTools(normalizedContext.messages), options.toolChoice, supportsStrictMode, model),
166
- additionalModelRequestFields: buildAdditionalModelRequestFields(model, options),
167
- ...(options.requestMetadata !== undefined && { requestMetadata: options.requestMetadata }),
168
- };
169
- const nextCommandInput = await options?.onPayload?.(commandInput, model);
170
- if (nextCommandInput !== undefined) {
171
- commandInput = nextCommandInput;
152
+ let disablePreferredStrictTools = false;
153
+ let response;
154
+ for (;;) {
155
+ const tools = convertToolConfig(getCurrentTools(normalizedContext.messages), options.toolChoice, supportsStrictMode, model, disablePreferredStrictTools);
156
+ let commandInput = {
157
+ modelId: model.id,
158
+ messages: convertMessages(normalizedContext, model, cacheRetention, options.env),
159
+ system: buildSystemPrompt(initialSystemPrompt, model, cacheRetention, options.env),
160
+ inferenceConfig: {
161
+ ...(inferenceMaxTokens !== undefined && { maxTokens: inferenceMaxTokens }),
162
+ // Claude Fable 5.1 rejects non-default `temperature`, `top_p`, and `top_k` on every
163
+ // request. Generated metadata marks such models `supportsTemperature: false`; every
164
+ // other model keeps sending the field exactly as before.
165
+ // https://platform.claude.com/docs/en/build-with-claude/thinking
166
+ ...(options.temperature !== undefined &&
167
+ model.compat?.supportsTemperature !== false && { temperature: options.temperature }),
168
+ },
169
+ toolConfig: tools.toolConfig,
170
+ additionalModelRequestFields: buildAdditionalModelRequestFields(model, options),
171
+ ...(options.requestMetadata !== undefined && { requestMetadata: options.requestMetadata }),
172
+ };
173
+ const nextCommandInput = await options?.onPayload?.(commandInput, model);
174
+ if (nextCommandInput !== undefined) {
175
+ commandInput = nextCommandInput;
176
+ }
177
+ try {
178
+ response = await client.send(new ConverseStreamCommand(commandInput), { abortSignal: options.signal });
179
+ break;
180
+ }
181
+ catch (error) {
182
+ // Bedrock checks the tool schemas before it starts the stream, so a retry never repeats streamed output.
183
+ if (options.signal?.aborted ||
184
+ disablePreferredStrictTools ||
185
+ !tools.hasPreferredStrictTools ||
186
+ !(error instanceof BedrockRuntimeServiceException) ||
187
+ error.name !== "ValidationException" ||
188
+ !isStrictToolLimitErrorMessage(error.message)) {
189
+ throw error;
190
+ }
191
+ disablePreferredStrictTools = true;
192
+ appendAssistantMessageDiagnostic(output, {
193
+ type: "bedrock_strict_tools_fallback",
194
+ timestamp: Date.now(),
195
+ error: { message: error.message },
196
+ details: { reason: "strict_tool_limits_exceeded" },
197
+ });
198
+ }
172
199
  }
173
- const command = new ConverseStreamCommand(commandInput);
174
- const response = await client.send(command, { abortSignal: options.signal });
175
200
  responseRequestId = normalizeDiagnosticValue(response.$metadata.requestId);
176
201
  if (!observedRawResponse && response.$metadata.httpStatusCode !== undefined) {
177
202
  const responseHeaders = {};
@@ -931,7 +956,7 @@ function convertMessages(context, model, cacheRetention, env) {
931
956
  }
932
957
  return result;
933
958
  }
934
- function convertToolConfig(tools, toolChoice, supportsStrictMode, model) {
959
+ function convertToolConfig(tools, toolChoice, supportsStrictMode, model, disablePreferredStrictTools) {
935
960
  // Validate the requested choice before any early return. A forced choice on a model that
936
961
  // rejects it is an error regardless of whether tools were supplied: returning `undefined` for
937
962
  // an empty or absent tool list would discard the caller's instruction silently, which is the
@@ -949,14 +974,18 @@ function convertToolConfig(tools, toolChoice, supportsStrictMode, model) {
949
974
  `Use toolChoice "auto" with strict tool use or structured outputs instead.`);
950
975
  }
951
976
  }
952
- if (!tools?.length)
953
- return undefined;
954
977
  // `none` is never a forced choice, so this return can never skip a rejection above.
955
- if (toolChoice === "none")
956
- return undefined;
978
+ if (!tools?.length || toolChoice === "none")
979
+ return { toolConfig: undefined, hasPreferredStrictTools: false };
957
980
  const strictProfile = isAnthropicClaudeModel(model) ? ANTHROPIC_STRICT_JSON_SCHEMA_PROFILE : undefined;
981
+ let hasPreferredStrictTools = false;
958
982
  const bedrockTools = tools.map((tool) => {
959
- const strict = resolveJsonSchemaStrictSampling(tool, supportsStrictMode, strictProfile);
983
+ const preferred = prefersJsonSchemaStrictSampling(tool);
984
+ const strict = disablePreferredStrictTools && preferred
985
+ ? undefined
986
+ : resolveJsonSchemaStrictSampling(tool, supportsStrictMode, strictProfile);
987
+ if (strict === true && preferred)
988
+ hasPreferredStrictTools = true;
960
989
  return {
961
990
  toolSpec: {
962
991
  name: tool.name,
@@ -979,7 +1008,7 @@ function convertToolConfig(tools, toolChoice, supportsStrictMode, model) {
979
1008
  bedrockToolChoice = { tool: { name: toolChoice.name } };
980
1009
  }
981
1010
  }
982
- return { tools: bedrockTools, toolChoice: bedrockToolChoice };
1011
+ return { toolConfig: { tools: bedrockTools, toolChoice: bedrockToolChoice }, hasPreferredStrictTools };
983
1012
  }
984
1013
  function mapStopReason(reason) {
985
1014
  switch (reason) {