ai 6.0.312 → 6.0.314

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.mjs CHANGED
@@ -401,7 +401,7 @@ var ToolChoiceViolationError = class extends AISDKError15 {
401
401
  provider,
402
402
  modelId,
403
403
  content,
404
- message = toolChoice.type === "required" ? "Model response did not contain a tool call even though tool choice was required." : `Model response did not contain a call to the required tool '${toolChoice.toolName}'.`
404
+ message = toolChoice.type === "none" ? "Model response contained a tool call even though tool choice was none." : toolChoice.type === "required" ? "Model response did not contain a tool call even though tool choice was required." : `Model response did not contain a call to the required tool '${toolChoice.toolName}'.`
405
405
  }) {
406
406
  super({ name: name15, message });
407
407
  this[_a15] = true;
@@ -1225,7 +1225,7 @@ import {
1225
1225
  } from "@ai-sdk/provider-utils";
1226
1226
 
1227
1227
  // src/version.ts
1228
- var VERSION = true ? "6.0.312" : "0.0.0-test";
1228
+ var VERSION = true ? "6.0.314" : "0.0.0-test";
1229
1229
 
1230
1230
  // src/util/download/download.ts
1231
1231
  var download = async ({
@@ -2876,6 +2876,7 @@ function collectToolApprovals({
2876
2876
  }
2877
2877
  const approvedToolApprovals = [];
2878
2878
  const deniedToolApprovals = [];
2879
+ const collectedToolCallIds = /* @__PURE__ */ new Set();
2879
2880
  const approvalResponses = lastMessage.content.filter(
2880
2881
  (part) => part.type === "tool-approval-response"
2881
2882
  );
@@ -2896,6 +2897,10 @@ function collectToolApprovals({
2896
2897
  approvalId: approvalRequest.approvalId
2897
2898
  });
2898
2899
  }
2900
+ if (collectedToolCallIds.has(toolCall.toolCallId)) {
2901
+ continue;
2902
+ }
2903
+ collectedToolCallIds.add(toolCall.toolCallId);
2899
2904
  const approval = {
2900
2905
  approvalRequest,
2901
2906
  approvalResponse,
@@ -4960,16 +4965,18 @@ async function generateText({
4960
4965
  (toolCall) => parseToolCall({
4961
4966
  toolCall,
4962
4967
  tools: stepToolSet,
4963
- repairToolCall,
4968
+ // tool calls are rejected with tool choice none,
4969
+ // so user callbacks must not process them:
4970
+ repairToolCall: (stepToolChoice == null ? void 0 : stepToolChoice.type) === "none" ? void 0 : repairToolCall,
4964
4971
  system,
4965
4972
  messages: stepInputMessages
4966
4973
  })
4967
4974
  )
4968
4975
  );
4969
- const enforcedToolChoice = (stepToolChoice == null ? void 0 : stepToolChoice.type) === "required" || (stepToolChoice == null ? void 0 : stepToolChoice.type) === "tool" ? stepToolChoice : void 0;
4970
- if (enforcedToolChoice != null && !stepToolCalls.some(
4976
+ const enforcedToolChoice = (stepToolChoice == null ? void 0 : stepToolChoice.type) === "auto" ? void 0 : stepToolChoice;
4977
+ if (enforcedToolChoice != null && (enforcedToolChoice.type === "none" ? stepToolCalls.length > 0 : !stepToolCalls.some(
4971
4978
  (toolCall) => enforcedToolChoice.type === "required" || toolCall.toolName === enforcedToolChoice.toolName
4972
- )) {
4979
+ ))) {
4973
4980
  throw new ToolChoiceViolationError({
4974
4981
  toolChoice: enforcedToolChoice,
4975
4982
  finishReason: currentModelResponse.finishReason.unified,
@@ -6075,6 +6082,7 @@ function processUIMessageStream({
6075
6082
  const anyPart = part;
6076
6083
  if (part != null) {
6077
6084
  part.state = options.state;
6085
+ anyPart.type = `tool-${options.toolName}`;
6078
6086
  anyPart.input = anyOptions.input;
6079
6087
  anyPart.output = anyOptions.output;
6080
6088
  anyPart.errorText = anyOptions.errorText;
@@ -7029,6 +7037,7 @@ import {
7029
7037
  } from "@ai-sdk/provider-utils";
7030
7038
  function runToolsTransformation({
7031
7039
  tools,
7040
+ toolChoice,
7032
7041
  generatorStream,
7033
7042
  tracer,
7034
7043
  telemetry,
@@ -7118,10 +7127,19 @@ function runToolsTransformation({
7118
7127
  });
7119
7128
  }
7120
7129
  const activeToolCallToolNames = /* @__PURE__ */ new Map();
7130
+ const withholdToolParts = (toolChoice == null ? void 0 : toolChoice.type) === "none";
7131
+ const withheldToolContent = [];
7121
7132
  const forwardStream = new TransformStream({
7122
7133
  async transform(chunk, controller) {
7123
- var _a23, _b, _c, _d;
7134
+ var _a23, _b, _c, _d, _e, _f;
7124
7135
  const chunkType = chunk.type;
7136
+ if (withholdToolParts && (chunkType === "tool-input-start" || chunkType === "tool-input-delta" || chunkType === "tool-input-end" || chunkType === "tool-approval-request")) {
7137
+ return;
7138
+ }
7139
+ if (withholdToolParts && (chunkType === "tool-call" || chunkType === "tool-result")) {
7140
+ withheldToolContent.push(chunk);
7141
+ return;
7142
+ }
7125
7143
  switch (chunkType) {
7126
7144
  case "stream-start":
7127
7145
  case "text-start":
@@ -7179,13 +7197,26 @@ function runToolsTransformation({
7179
7197
  break;
7180
7198
  }
7181
7199
  case "finish": {
7200
+ const toolChoiceViolationError = withholdToolParts && withheldToolContent.some((part) => part.type === "tool-call") ? new ToolChoiceViolationError({
7201
+ toolChoice: { type: "none" },
7202
+ finishReason: chunk.finishReason.unified,
7203
+ provider: (_e = model == null ? void 0 : model.provider) != null ? _e : "unknown",
7204
+ modelId: (_f = model == null ? void 0 : model.modelId) != null ? _f : "unknown",
7205
+ content: withheldToolContent
7206
+ }) : void 0;
7182
7207
  finishChunk = {
7183
7208
  type: "finish",
7184
- finishReason: chunk.finishReason.unified,
7209
+ finishReason: toolChoiceViolationError == null ? chunk.finishReason.unified : "error",
7185
7210
  rawFinishReason: chunk.finishReason.raw,
7186
7211
  usage: asLanguageModelUsage(chunk.usage),
7187
7212
  providerMetadata: chunk.providerMetadata
7188
7213
  };
7214
+ if (toolChoiceViolationError != null) {
7215
+ controller.enqueue({
7216
+ type: "error",
7217
+ error: toolChoiceViolationError
7218
+ });
7219
+ }
7189
7220
  if (isToolExecutionAllowedFinishReason(chunk.finishReason.unified)) {
7190
7221
  for (const toolCall of toolCallsToExecute.splice(0)) {
7191
7222
  executeToolCallAfterFinish(toolCall);
@@ -8374,6 +8405,7 @@ var DefaultStreamTextResult = class {
8374
8405
  );
8375
8406
  const streamWithToolResults = runToolsTransformation({
8376
8407
  tools: stepToolSet,
8408
+ toolChoice: stepToolChoice,
8377
8409
  generatorStream: stream2,
8378
8410
  tracer,
8379
8411
  telemetry,
@@ -12805,18 +12837,53 @@ function pruneMessages({
12805
12837
  if (message.role !== "assistant" && message.role !== "tool" || typeof message.content === "string" || keepLastMessagesCount && messageIndex >= messages.length - keepLastMessagesCount) {
12806
12838
  return message;
12807
12839
  }
12808
- return {
12809
- ...message,
12810
- content: message.content.filter((part) => {
12811
- if (part.type !== "tool-call" && part.type !== "tool-result" && part.type !== "tool-approval-request" && part.type !== "tool-approval-response") {
12812
- return true;
12840
+ const keepParts = message.content.map((part) => {
12841
+ if (part.type !== "tool-call" && part.type !== "tool-result" && part.type !== "tool-approval-request" && part.type !== "tool-approval-response") {
12842
+ return true;
12843
+ }
12844
+ if ((part.type === "tool-call" || part.type === "tool-result") && keptToolCallIds.has(part.toolCallId) || (part.type === "tool-approval-request" || part.type === "tool-approval-response") && keptApprovalIds.has(part.approvalId)) {
12845
+ return true;
12846
+ }
12847
+ const partToolName = part.type === "tool-call" || part.type === "tool-result" ? part.toolName : approvalIdToToolName.get(part.approvalId);
12848
+ return toolCall.tools != null && partToolName != null && !toolCall.tools.includes(partToolName);
12849
+ });
12850
+ if (message.role === "assistant") {
12851
+ for (let reasoningIndex = 0; reasoningIndex < message.content.length; reasoningIndex++) {
12852
+ const part = message.content[reasoningIndex];
12853
+ if (part.type !== "reasoning") {
12854
+ continue;
12855
+ }
12856
+ let reasoningEndIndex = reasoningIndex + 1;
12857
+ while (reasoningEndIndex < message.content.length) {
12858
+ const nextPart = message.content[reasoningEndIndex];
12859
+ if (nextPart.type !== "reasoning") {
12860
+ break;
12861
+ }
12862
+ reasoningEndIndex++;
12813
12863
  }
12814
- if ((part.type === "tool-call" || part.type === "tool-result") && keptToolCallIds.has(part.toolCallId) || (part.type === "tool-approval-request" || part.type === "tool-approval-response") && keptApprovalIds.has(part.approvalId)) {
12815
- return true;
12864
+ let followingPartIndex = reasoningEndIndex;
12865
+ let hasFollowingPart = false;
12866
+ let hasKeptFollowingPart = false;
12867
+ while (followingPartIndex < message.content.length) {
12868
+ const followingPart = message.content[followingPartIndex];
12869
+ if (followingPart.type === "reasoning") {
12870
+ break;
12871
+ }
12872
+ hasFollowingPart = true;
12873
+ hasKeptFollowingPart || (hasKeptFollowingPart = keepParts[followingPartIndex]);
12874
+ followingPartIndex++;
12816
12875
  }
12817
- const partToolName = part.type === "tool-call" || part.type === "tool-result" ? part.toolName : approvalIdToToolName.get(part.approvalId);
12818
- return toolCall.tools != null && partToolName != null && !toolCall.tools.includes(partToolName);
12819
- })
12876
+ if (hasFollowingPart && !hasKeptFollowingPart) {
12877
+ for (let index = reasoningIndex; index < reasoningEndIndex; index++) {
12878
+ keepParts[index] = false;
12879
+ }
12880
+ }
12881
+ reasoningIndex = followingPartIndex - 1;
12882
+ }
12883
+ }
12884
+ return {
12885
+ ...message,
12886
+ content: message.content.filter((_, index) => keepParts[index])
12820
12887
  };
12821
12888
  });
12822
12889
  }