ai 5.0.0-canary.14 → 5.0.0-canary.16

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
@@ -8,7 +8,11 @@ var __export = (target, all) => {
8
8
  import { createIdGenerator as createIdGenerator5, generateId as generateId2 } from "@ai-sdk/provider-utils";
9
9
 
10
10
  // core/util/index.ts
11
- import { generateId } from "@ai-sdk/provider-utils";
11
+ import {
12
+ generateId,
13
+ jsonSchema,
14
+ asSchema
15
+ } from "@ai-sdk/provider-utils";
12
16
 
13
17
  // core/util/process-chat-response.ts
14
18
  import { generateId as generateIdFunction } from "@ai-sdk/provider-utils";
@@ -25,6 +29,21 @@ function calculateLanguageModelUsage({
25
29
  };
26
30
  }
27
31
 
32
+ // core/ui/get-tool-invocations.ts
33
+ function getToolInvocations(message) {
34
+ return message.parts.filter(
35
+ (part) => part.type === "tool-invocation"
36
+ ).map((part) => part.toolInvocation);
37
+ }
38
+
39
+ // core/util/extract-max-tool-invocation-step.ts
40
+ function extractMaxToolInvocationStep(toolInvocations) {
41
+ return toolInvocations == null ? void 0 : toolInvocations.reduce((max, toolInvocation) => {
42
+ var _a17;
43
+ return Math.max(max, (_a17 = toolInvocation.step) != null ? _a17 : 0);
44
+ }, 0);
45
+ }
46
+
28
47
  // core/util/parse-partial-json.ts
29
48
  import { safeParseJSON } from "@ai-sdk/provider-utils";
30
49
 
@@ -747,13 +766,9 @@ async function processChatResponse({
747
766
  getCurrentDate = () => /* @__PURE__ */ new Date(),
748
767
  lastMessage
749
768
  }) {
750
- var _a17, _b;
769
+ var _a17;
751
770
  const replaceLastMessage = (lastMessage == null ? void 0 : lastMessage.role) === "assistant";
752
- let step = replaceLastMessage ? 1 + // find max step in existing tool invocations:
753
- ((_b = (_a17 = lastMessage.toolInvocations) == null ? void 0 : _a17.reduce((max, toolInvocation) => {
754
- var _a18;
755
- return Math.max(max, (_a18 = toolInvocation.step) != null ? _a18 : 0);
756
- }, 0)) != null ? _b : 0) : 0;
771
+ let step = replaceLastMessage ? 1 + ((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) : 0;
757
772
  const message = replaceLastMessage ? structuredClone(lastMessage) : {
758
773
  id: generateId3(),
759
774
  createdAt: getCurrentDate(),
@@ -823,19 +838,17 @@ async function processChatResponse({
823
838
  execUpdate();
824
839
  },
825
840
  onReasoningPart(value) {
826
- var _a18;
827
841
  if (currentReasoningPart == null) {
828
842
  currentReasoningPart = {
829
843
  type: "reasoning",
830
- reasoning: value.text,
844
+ text: value.text,
831
845
  providerMetadata: value.providerMetadata
832
846
  };
833
847
  message.parts.push(currentReasoningPart);
834
848
  } else {
835
- currentReasoningPart.reasoning += value.text;
849
+ currentReasoningPart.text += value.text;
836
850
  currentReasoningPart.providerMetadata = value.providerMetadata;
837
851
  }
838
- message.reasoning = ((_a18 = message.reasoning) != null ? _a18 : "") + value.text;
839
852
  execUpdate();
840
853
  },
841
854
  onReasoningPartFinish(value) {
@@ -859,24 +872,20 @@ async function processChatResponse({
859
872
  execUpdate();
860
873
  },
861
874
  onToolCallStreamingStartPart(value) {
862
- if (message.toolInvocations == null) {
863
- message.toolInvocations = [];
864
- }
875
+ const toolInvocations = getToolInvocations(message);
865
876
  partialToolCalls[value.toolCallId] = {
866
877
  text: "",
867
878
  step,
868
879
  toolName: value.toolName,
869
- index: message.toolInvocations.length
880
+ index: toolInvocations.length
870
881
  };
871
- const invocation = {
882
+ updateToolInvocationPart(value.toolCallId, {
872
883
  state: "partial-call",
873
884
  step,
874
885
  toolCallId: value.toolCallId,
875
886
  toolName: value.toolName,
876
887
  args: void 0
877
- };
878
- message.toolInvocations.push(invocation);
879
- updateToolInvocationPart(value.toolCallId, invocation);
888
+ });
880
889
  execUpdate();
881
890
  },
882
891
  async onToolCallDeltaPart(value) {
@@ -885,68 +894,53 @@ async function processChatResponse({
885
894
  const { value: partialArgs } = await parsePartialJson(
886
895
  partialToolCall.text
887
896
  );
888
- const invocation = {
897
+ updateToolInvocationPart(value.toolCallId, {
889
898
  state: "partial-call",
890
899
  step: partialToolCall.step,
891
900
  toolCallId: value.toolCallId,
892
901
  toolName: partialToolCall.toolName,
893
902
  args: partialArgs
894
- };
895
- message.toolInvocations[partialToolCall.index] = invocation;
896
- updateToolInvocationPart(value.toolCallId, invocation);
903
+ });
897
904
  execUpdate();
898
905
  },
899
906
  async onToolCallPart(value) {
900
- const invocation = {
907
+ updateToolInvocationPart(value.toolCallId, {
901
908
  state: "call",
902
909
  step,
903
910
  ...value
904
- };
905
- if (partialToolCalls[value.toolCallId] != null) {
906
- message.toolInvocations[partialToolCalls[value.toolCallId].index] = invocation;
907
- } else {
908
- if (message.toolInvocations == null) {
909
- message.toolInvocations = [];
910
- }
911
- message.toolInvocations.push(invocation);
912
- }
913
- updateToolInvocationPart(value.toolCallId, invocation);
911
+ });
914
912
  execUpdate();
915
913
  if (onToolCall) {
916
914
  const result = await onToolCall({ toolCall: value });
917
915
  if (result != null) {
918
- const invocation2 = {
916
+ updateToolInvocationPart(value.toolCallId, {
919
917
  state: "result",
920
918
  step,
921
919
  ...value,
922
920
  result
923
- };
924
- message.toolInvocations[message.toolInvocations.length - 1] = invocation2;
925
- updateToolInvocationPart(value.toolCallId, invocation2);
921
+ });
926
922
  execUpdate();
927
923
  }
928
924
  }
929
925
  },
930
926
  onToolResultPart(value) {
931
- const toolInvocations = message.toolInvocations;
927
+ const toolInvocations = getToolInvocations(message);
932
928
  if (toolInvocations == null) {
933
929
  throw new Error("tool_result must be preceded by a tool_call");
934
930
  }
935
931
  const toolInvocationIndex = toolInvocations.findIndex(
936
- (invocation2) => invocation2.toolCallId === value.toolCallId
932
+ (invocation) => invocation.toolCallId === value.toolCallId
937
933
  );
938
934
  if (toolInvocationIndex === -1) {
939
935
  throw new Error(
940
936
  "tool_result must be preceded by a tool_call with the same toolCallId"
941
937
  );
942
938
  }
943
- const invocation = {
939
+ updateToolInvocationPart(value.toolCallId, {
944
940
  ...toolInvocations[toolInvocationIndex],
945
941
  state: "result",
946
942
  ...value
947
- };
948
- toolInvocations[toolInvocationIndex] = invocation;
949
- updateToolInvocationPart(value.toolCallId, invocation);
943
+ });
950
944
  execUpdate();
951
945
  },
952
946
  onDataPart(value) {
@@ -1242,40 +1236,6 @@ function getTextFromDataUrl(dataUrl) {
1242
1236
  }
1243
1237
  }
1244
1238
 
1245
- // core/util/extract-max-tool-invocation-step.ts
1246
- function extractMaxToolInvocationStep(toolInvocations) {
1247
- return toolInvocations == null ? void 0 : toolInvocations.reduce((max, toolInvocation) => {
1248
- var _a17;
1249
- return Math.max(max, (_a17 = toolInvocation.step) != null ? _a17 : 0);
1250
- }, 0);
1251
- }
1252
-
1253
- // core/util/get-message-parts.ts
1254
- function getMessageParts(message) {
1255
- var _a17;
1256
- return (_a17 = message.parts) != null ? _a17 : [
1257
- ...message.toolInvocations ? message.toolInvocations.map((toolInvocation) => ({
1258
- type: "tool-invocation",
1259
- toolInvocation
1260
- })) : [],
1261
- ...message.reasoning ? [
1262
- {
1263
- type: "reasoning",
1264
- reasoning: message.reasoning
1265
- }
1266
- ] : [],
1267
- ...message.content ? [{ type: "text", text: message.content }] : []
1268
- ];
1269
- }
1270
-
1271
- // core/util/fill-message-parts.ts
1272
- function fillMessageParts(messages) {
1273
- return messages.map((message) => ({
1274
- ...message,
1275
- parts: getMessageParts(message)
1276
- }));
1277
- }
1278
-
1279
1239
  // core/util/is-deep-equal-data.ts
1280
1240
  function isDeepEqualData(obj1, obj2) {
1281
1241
  if (obj1 === obj2)
@@ -1316,7 +1276,7 @@ async function prepareAttachmentsForRequest(attachmentsFromOptions) {
1316
1276
  if (!attachmentsFromOptions) {
1317
1277
  return [];
1318
1278
  }
1319
- if (attachmentsFromOptions instanceof FileList) {
1279
+ if (globalThis.FileList && attachmentsFromOptions instanceof globalThis.FileList) {
1320
1280
  return Promise.all(
1321
1281
  Array.from(attachmentsFromOptions).map(async (attachment) => {
1322
1282
  const { name: name17, type } = attachment;
@@ -1343,52 +1303,25 @@ async function prepareAttachmentsForRequest(attachmentsFromOptions) {
1343
1303
  throw new Error("Invalid attachments type");
1344
1304
  }
1345
1305
 
1346
- // core/util/schema.ts
1347
- import { validatorSymbol } from "@ai-sdk/provider-utils";
1348
-
1349
- // core/util/zod-schema.ts
1350
- import zodToJsonSchema from "zod-to-json-schema";
1351
- function zodSchema(zodSchema2, options) {
1352
- var _a17;
1353
- const useReferences = (_a17 = options == null ? void 0 : options.useReferences) != null ? _a17 : false;
1354
- return jsonSchema(
1355
- zodToJsonSchema(zodSchema2, {
1356
- $refStrategy: useReferences ? "root" : "none",
1357
- target: "jsonSchema7"
1358
- // note: openai mode breaks various gemini conversions
1359
- }),
1360
- {
1361
- validate: (value) => {
1362
- const result = zodSchema2.safeParse(value);
1363
- return result.success ? { success: true, value: result.data } : { success: false, error: result.error };
1364
- }
1365
- }
1306
+ // core/util/update-tool-call-result.ts
1307
+ function updateToolCallResult({
1308
+ messages,
1309
+ toolCallId,
1310
+ toolResult: result
1311
+ }) {
1312
+ const lastMessage = messages[messages.length - 1];
1313
+ const invocationPart = lastMessage.parts.find(
1314
+ (part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
1366
1315
  );
1367
- }
1368
-
1369
- // core/util/schema.ts
1370
- var schemaSymbol = Symbol.for("vercel.ai.schema");
1371
- function jsonSchema(jsonSchema2, {
1372
- validate
1373
- } = {}) {
1374
- return {
1375
- [schemaSymbol]: true,
1376
- _type: void 0,
1377
- // should never be used directly
1378
- [validatorSymbol]: true,
1379
- jsonSchema: jsonSchema2,
1380
- validate
1316
+ if (invocationPart == null) {
1317
+ return;
1318
+ }
1319
+ invocationPart.toolInvocation = {
1320
+ ...invocationPart.toolInvocation,
1321
+ state: "result",
1322
+ result
1381
1323
  };
1382
1324
  }
1383
- function isSchema(value) {
1384
- return typeof value === "object" && value !== null && schemaSymbol in value && value[schemaSymbol] === true && "jsonSchema" in value && "validate" in value;
1385
- }
1386
- function asSchema(schema) {
1387
- return schema == null ? jsonSchema({
1388
- properties: {},
1389
- additionalProperties: false
1390
- }) : isSchema(schema) ? schema : zodSchema(schema);
1391
- }
1392
1325
 
1393
1326
  // core/util/should-resubmit-messages.ts
1394
1327
  function shouldResubmitMessages({
@@ -1403,9 +1336,9 @@ function shouldResubmitMessages({
1403
1336
  // check if the feature is enabled:
1404
1337
  maxSteps > 1 && // ensure there is a last message:
1405
1338
  lastMessage != null && // ensure we actually have new steps (to prevent infinite loops in case of errors):
1406
- (messages.length > originalMessageCount || extractMaxToolInvocationStep(lastMessage.toolInvocations) !== originalMaxToolInvocationStep) && // check that next step is possible:
1339
+ (messages.length > originalMessageCount || extractMaxToolInvocationStep(getToolInvocations(lastMessage)) !== originalMaxToolInvocationStep) && // check that next step is possible:
1407
1340
  isAssistantMessageWithCompletedToolCalls(lastMessage) && // limit the number of automatic steps:
1408
- ((_a17 = extractMaxToolInvocationStep(lastMessage.toolInvocations)) != null ? _a17 : 0) < maxSteps
1341
+ ((_a17 = extractMaxToolInvocationStep(getToolInvocations(lastMessage))) != null ? _a17 : 0) < maxSteps
1409
1342
  );
1410
1343
  }
1411
1344
  function isAssistantMessageWithCompletedToolCalls(message) {
@@ -1419,31 +1352,6 @@ function isAssistantMessageWithCompletedToolCalls(message) {
1419
1352
  return lastStepToolInvocations.length > 0 && lastStepToolInvocations.every((part) => "result" in part.toolInvocation);
1420
1353
  }
1421
1354
 
1422
- // core/util/update-tool-call-result.ts
1423
- function updateToolCallResult({
1424
- messages,
1425
- toolCallId,
1426
- toolResult: result
1427
- }) {
1428
- var _a17;
1429
- const lastMessage = messages[messages.length - 1];
1430
- const invocationPart = lastMessage.parts.find(
1431
- (part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === toolCallId
1432
- );
1433
- if (invocationPart == null) {
1434
- return;
1435
- }
1436
- const toolResult = {
1437
- ...invocationPart.toolInvocation,
1438
- state: "result",
1439
- result
1440
- };
1441
- invocationPart.toolInvocation = toolResult;
1442
- lastMessage.toolInvocations = (_a17 = lastMessage.toolInvocations) == null ? void 0 : _a17.map(
1443
- (toolInvocation) => toolInvocation.toolCallId === toolCallId ? toolResult : toolInvocation
1444
- );
1445
- }
1446
-
1447
1355
  // core/data-stream/create-data-stream.ts
1448
1356
  function createDataStream({
1449
1357
  execute,
@@ -3241,7 +3149,7 @@ function convertToCoreMessages(messages, options) {
3241
3149
  case "reasoning": {
3242
3150
  content2.push({
3243
3151
  type: "reasoning",
3244
- text: part.reasoning,
3152
+ text: part.text,
3245
3153
  providerOptions: part.providerMetadata
3246
3154
  });
3247
3155
  break;
@@ -3331,73 +3239,11 @@ function convertToCoreMessages(messages, options) {
3331
3239
  processBlock2();
3332
3240
  break;
3333
3241
  }
3334
- const toolInvocations = message.toolInvocations;
3335
- if (toolInvocations == null || toolInvocations.length === 0) {
3336
- coreMessages.push({ role: "assistant", content });
3337
- break;
3338
- }
3339
- const maxStep = toolInvocations.reduce((max, toolInvocation) => {
3340
- var _a18;
3341
- return Math.max(max, (_a18 = toolInvocation.step) != null ? _a18 : 0);
3342
- }, 0);
3343
- for (let i2 = 0; i2 <= maxStep; i2++) {
3344
- const stepInvocations = toolInvocations.filter(
3345
- (toolInvocation) => {
3346
- var _a18;
3347
- return ((_a18 = toolInvocation.step) != null ? _a18 : 0) === i2;
3348
- }
3349
- );
3350
- if (stepInvocations.length === 0) {
3351
- continue;
3352
- }
3353
- coreMessages.push({
3354
- role: "assistant",
3355
- content: [
3356
- ...isLastMessage && content && i2 === 0 ? [{ type: "text", text: content }] : [],
3357
- ...stepInvocations.map(
3358
- ({ toolCallId, toolName, args }) => ({
3359
- type: "tool-call",
3360
- toolCallId,
3361
- toolName,
3362
- args
3363
- })
3364
- )
3365
- ]
3366
- });
3367
- coreMessages.push({
3368
- role: "tool",
3369
- content: stepInvocations.map((toolInvocation) => {
3370
- if (!("result" in toolInvocation)) {
3371
- throw new MessageConversionError({
3372
- originalMessage: message,
3373
- message: "ToolInvocation must have a result: " + JSON.stringify(toolInvocation)
3374
- });
3375
- }
3376
- const { toolCallId, toolName, result } = toolInvocation;
3377
- const tool2 = tools[toolName];
3378
- return (tool2 == null ? void 0 : tool2.experimental_toToolResultContent) != null ? {
3379
- type: "tool-result",
3380
- toolCallId,
3381
- toolName,
3382
- result: tool2.experimental_toToolResultContent(result),
3383
- experimental_content: tool2.experimental_toToolResultContent(result)
3384
- } : {
3385
- type: "tool-result",
3386
- toolCallId,
3387
- toolName,
3388
- result
3389
- };
3390
- })
3391
- });
3392
- }
3393
3242
  if (content && !isLastMessage) {
3394
3243
  coreMessages.push({ role: "assistant", content });
3395
3244
  }
3396
3245
  break;
3397
3246
  }
3398
- case "data": {
3399
- break;
3400
- }
3401
3247
  default: {
3402
3248
  const _exhaustiveCheck = role;
3403
3249
  throw new MessageConversionError({
@@ -3887,10 +3733,36 @@ var enumOutputStrategy = (enumValues) => {
3887
3733
  })
3888
3734
  };
3889
3735
  },
3890
- validatePartialResult() {
3891
- throw new UnsupportedFunctionalityError({
3892
- functionality: "partial results in enum mode"
3893
- });
3736
+ async validatePartialResult({ value, textDelta }) {
3737
+ if (!isJSONObject(value) || typeof value.result !== "string") {
3738
+ return {
3739
+ success: false,
3740
+ error: new TypeValidationError({
3741
+ value,
3742
+ cause: 'value must be an object that contains a string in the "result" property.'
3743
+ })
3744
+ };
3745
+ }
3746
+ const result = value.result;
3747
+ const possibleEnumValues = enumValues.filter(
3748
+ (enumValue) => enumValue.startsWith(result)
3749
+ );
3750
+ if (value.result.length === 0 || possibleEnumValues.length === 0) {
3751
+ return {
3752
+ success: false,
3753
+ error: new TypeValidationError({
3754
+ value,
3755
+ cause: "value must be a string in the enum"
3756
+ })
3757
+ };
3758
+ }
3759
+ return {
3760
+ success: true,
3761
+ value: {
3762
+ partial: possibleEnumValues.length > 1 ? result : possibleEnumValues[0],
3763
+ textDelta
3764
+ }
3765
+ };
3894
3766
  },
3895
3767
  createElementStream() {
3896
3768
  throw new UnsupportedFunctionalityError({
@@ -4122,7 +3994,7 @@ async function generateObject(options) {
4122
3994
  });
4123
3995
  const promptMessages = await convertToLanguageModelPrompt({
4124
3996
  prompt: standardizedPrompt,
4125
- supportedUrls: await model.getSupportedUrls()
3997
+ supportedUrls: await model.supportedUrls
4126
3998
  });
4127
3999
  const generateResult = await retry(
4128
4000
  () => recordSpan({
@@ -4476,6 +4348,7 @@ function streamObject(options) {
4476
4348
  } = {},
4477
4349
  ...settings
4478
4350
  } = options;
4351
+ const enumValues = "enum" in options && options.enum ? options.enum : void 0;
4479
4352
  const {
4480
4353
  schema: inputSchema,
4481
4354
  schemaDescription,
@@ -4485,9 +4358,14 @@ function streamObject(options) {
4485
4358
  output,
4486
4359
  schema: inputSchema,
4487
4360
  schemaName,
4488
- schemaDescription
4361
+ schemaDescription,
4362
+ enumValues
4363
+ });
4364
+ const outputStrategy = getOutputStrategy({
4365
+ output,
4366
+ schema: inputSchema,
4367
+ enumValues
4489
4368
  });
4490
- const outputStrategy = getOutputStrategy({ output, schema: inputSchema });
4491
4369
  return new DefaultStreamObjectResult({
4492
4370
  model,
4493
4371
  telemetry,
@@ -4595,7 +4473,7 @@ var DefaultStreamObjectResult = class {
4595
4473
  ...prepareCallSettings(settings),
4596
4474
  prompt: await convertToLanguageModelPrompt({
4597
4475
  prompt: standardizedPrompt,
4598
- supportedUrls: await model.getSupportedUrls()
4476
+ supportedUrls: await model.supportedUrls
4599
4477
  }),
4600
4478
  providerOptions,
4601
4479
  abortSignal,
@@ -5389,7 +5267,7 @@ async function generateText({
5389
5267
  system: initialPrompt.system,
5390
5268
  messages: stepInputMessages
5391
5269
  },
5392
- supportedUrls: await model.getSupportedUrls()
5270
+ supportedUrls: await model.supportedUrls
5393
5271
  });
5394
5272
  const stepModel = (_a17 = prepareStepResult == null ? void 0 : prepareStepResult.model) != null ? _a17 : model;
5395
5273
  const { toolChoice: stepToolChoice, tools: stepTools } = prepareToolsAndToolChoice({
@@ -6725,7 +6603,7 @@ var DefaultStreamTextResult = class {
6725
6603
  system: initialPrompt.system,
6726
6604
  messages: stepInputMessages
6727
6605
  },
6728
- supportedUrls: await model.getSupportedUrls()
6606
+ supportedUrls: await model.supportedUrls
6729
6607
  });
6730
6608
  const toolsAndToolChoice = {
6731
6609
  ...prepareToolsAndToolChoice({ tools, toolChoice, activeTools })
@@ -7874,8 +7752,8 @@ var doWrap = ({
7874
7752
  provider: providerId != null ? providerId : model.provider,
7875
7753
  modelId: modelId != null ? modelId : model.modelId,
7876
7754
  // TODO middleware should be able to modify the supported urls
7877
- async getSupportedUrls() {
7878
- return model.getSupportedUrls();
7755
+ get supportedUrls() {
7756
+ return model.supportedUrls;
7879
7757
  },
7880
7758
  async doGenerate(params) {
7881
7759
  const transformedParams = await doTransform({ params, type: "generate" });
@@ -7915,7 +7793,7 @@ function appendResponseMessages({
7915
7793
  responseMessages,
7916
7794
  _internal: { currentDate = () => /* @__PURE__ */ new Date() } = {}
7917
7795
  }) {
7918
- var _a17, _b, _c, _d, _e;
7796
+ var _a17, _b, _c;
7919
7797
  const clonedMessages = structuredClone(messages);
7920
7798
  for (const message of responseMessages) {
7921
7799
  const role = message.role;
@@ -7923,7 +7801,7 @@ function appendResponseMessages({
7923
7801
  const isLastMessageAssistant = lastMessage.role === "assistant";
7924
7802
  switch (role) {
7925
7803
  case "assistant": {
7926
- let getToolInvocations2 = function(step) {
7804
+ let getToolInvocationsForStep2 = function(step) {
7927
7805
  return (typeof message.content === "string" ? [] : message.content.filter((part) => part.type === "tool-call")).map((call) => ({
7928
7806
  state: "call",
7929
7807
  step,
@@ -7932,7 +7810,7 @@ function appendResponseMessages({
7932
7810
  toolName: call.toolName
7933
7811
  }));
7934
7812
  };
7935
- var getToolInvocations = getToolInvocations2;
7813
+ var getToolInvocationsForStep = getToolInvocationsForStep2;
7936
7814
  const parts = [{ type: "step-start" }];
7937
7815
  let textContent = "";
7938
7816
  let reasoningTextContent = void 0;
@@ -7959,12 +7837,12 @@ function appendResponseMessages({
7959
7837
  if (reasoningPart == null) {
7960
7838
  reasoningPart = {
7961
7839
  type: "reasoning",
7962
- reasoning: ""
7840
+ text: ""
7963
7841
  };
7964
7842
  parts.push(reasoningPart);
7965
7843
  }
7966
7844
  reasoningTextContent = (reasoningTextContent != null ? reasoningTextContent : "") + part.text;
7967
- reasoningPart.reasoning += part.text;
7845
+ reasoningPart.text += part.text;
7968
7846
  reasoningPart.providerMetadata = part.providerOptions;
7969
7847
  break;
7970
7848
  }
@@ -7988,17 +7866,13 @@ function appendResponseMessages({
7988
7866
  }
7989
7867
  if (isLastMessageAssistant) {
7990
7868
  const maxStep = extractMaxToolInvocationStep(
7991
- lastMessage.toolInvocations
7869
+ getToolInvocations(lastMessage)
7870
+ // TODO remove once Message is removed
7992
7871
  );
7993
7872
  (_b = lastMessage.parts) != null ? _b : lastMessage.parts = [];
7994
7873
  lastMessage.content = textContent;
7995
- lastMessage.reasoning = reasoningTextContent;
7996
7874
  lastMessage.parts.push(...parts);
7997
- lastMessage.toolInvocations = [
7998
- ...(_c = lastMessage.toolInvocations) != null ? _c : [],
7999
- ...getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1)
8000
- ];
8001
- getToolInvocations2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
7875
+ getToolInvocationsForStep2(maxStep === void 0 ? 0 : maxStep + 1).map((call) => ({
8002
7876
  type: "tool-invocation",
8003
7877
  toolInvocation: call
8004
7878
  })).forEach((part) => {
@@ -8011,11 +7885,9 @@ function appendResponseMessages({
8011
7885
  createdAt: currentDate(),
8012
7886
  // generate a createdAt date for the message, will be overridden by the client
8013
7887
  content: textContent,
8014
- reasoning: reasoningTextContent,
8015
- toolInvocations: getToolInvocations2(0),
8016
7888
  parts: [
8017
7889
  ...parts,
8018
- ...getToolInvocations2(0).map((call) => ({
7890
+ ...getToolInvocationsForStep2(0).map((call) => ({
8019
7891
  type: "tool-invocation",
8020
7892
  toolInvocation: call
8021
7893
  }))
@@ -8025,17 +7897,17 @@ function appendResponseMessages({
8025
7897
  break;
8026
7898
  }
8027
7899
  case "tool": {
8028
- (_d = lastMessage.toolInvocations) != null ? _d : lastMessage.toolInvocations = [];
8029
7900
  if (lastMessage.role !== "assistant") {
8030
7901
  throw new Error(
8031
7902
  `Tool result must follow an assistant message: ${lastMessage.role}`
8032
7903
  );
8033
7904
  }
8034
- (_e = lastMessage.parts) != null ? _e : lastMessage.parts = [];
7905
+ (_c = lastMessage.parts) != null ? _c : lastMessage.parts = [];
8035
7906
  for (const contentPart of message.content) {
8036
- const toolCall = lastMessage.toolInvocations.find(
8037
- (call) => call.toolCallId === contentPart.toolCallId
8038
- );
7907
+ const toolCall = getToolInvocations(
7908
+ lastMessage
7909
+ // TODO remove once Message is removed
7910
+ ).find((call) => call.toolCallId === contentPart.toolCallId);
8039
7911
  const toolCallPart = lastMessage.parts.find(
8040
7912
  (part) => part.type === "tool-invocation" && part.toolInvocation.toolCallId === contentPart.toolCallId
8041
7913
  );
@@ -8588,6 +8460,24 @@ var MCPClient = class {
8588
8460
  await ((_a17 = this.transport) == null ? void 0 : _a17.close());
8589
8461
  this.onClose();
8590
8462
  }
8463
+ assertCapability(method) {
8464
+ switch (method) {
8465
+ case "initialize":
8466
+ break;
8467
+ case "tools/list":
8468
+ case "tools/call":
8469
+ if (!this.serverCapabilities.tools) {
8470
+ throw new MCPClientError({
8471
+ message: `Server does not support tools`
8472
+ });
8473
+ }
8474
+ break;
8475
+ default:
8476
+ throw new MCPClientError({
8477
+ message: `Unsupported method: ${method}`
8478
+ });
8479
+ }
8480
+ }
8591
8481
  async request({
8592
8482
  request,
8593
8483
  resultSchema,
@@ -8601,6 +8491,7 @@ var MCPClient = class {
8601
8491
  })
8602
8492
  );
8603
8493
  }
8494
+ this.assertCapability(request.method);
8604
8495
  const signal = options == null ? void 0 : options.signal;
8605
8496
  signal == null ? void 0 : signal.throwIfAborted();
8606
8497
  const messageId = this.requestMessageId++;
@@ -8629,7 +8520,7 @@ var MCPClient = class {
8629
8520
  resolve(result);
8630
8521
  } catch (error) {
8631
8522
  const parseError = new MCPClientError({
8632
- message: "Failed to parse server initialization result",
8523
+ message: "Failed to parse server response",
8633
8524
  cause: error
8634
8525
  });
8635
8526
  reject(parseError);
@@ -8645,11 +8536,6 @@ var MCPClient = class {
8645
8536
  params,
8646
8537
  options
8647
8538
  } = {}) {
8648
- if (!this.serverCapabilities.tools) {
8649
- throw new MCPClientError({
8650
- message: `Server does not support tools`
8651
- });
8652
- }
8653
8539
  try {
8654
8540
  return this.request({
8655
8541
  request: { method: "tools/list", params },
@@ -8665,11 +8551,6 @@ var MCPClient = class {
8665
8551
  args,
8666
8552
  options
8667
8553
  }) {
8668
- if (!this.serverCapabilities.tools) {
8669
- throw new MCPClientError({
8670
- message: `Server does not support tools`
8671
- });
8672
- }
8673
8554
  try {
8674
8555
  return this.request({
8675
8556
  request: { method: "tools/call", params: { name: name17, arguments: args } },
@@ -8939,13 +8820,12 @@ export {
8939
8820
  transcribe as experimental_transcribe,
8940
8821
  extractMaxToolInvocationStep,
8941
8822
  extractReasoningMiddleware,
8942
- fillMessageParts,
8943
8823
  formatDataStreamPart,
8944
8824
  generateId2 as generateId,
8945
8825
  generateObject,
8946
8826
  generateText,
8947
- getMessageParts,
8948
8827
  getTextFromDataUrl,
8828
+ getToolInvocations,
8949
8829
  isAssistantMessageWithCompletedToolCalls,
8950
8830
  isDeepEqualData,
8951
8831
  jsonSchema,
@@ -8963,7 +8843,6 @@ export {
8963
8843
  streamText,
8964
8844
  tool,
8965
8845
  updateToolCallResult,
8966
- wrapLanguageModel,
8967
- zodSchema
8846
+ wrapLanguageModel
8968
8847
  };
8969
8848
  //# sourceMappingURL=index.mjs.map