ai-sdk-ollama 3.8.7 → 3.8.8

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
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 3.8.8
4
+
5
+ ### Patch Changes
6
+
7
+ - b4dde41: Return AI SDK `tool-calls` finish reasons when Ollama responses emit tool calls.
8
+
3
9
  ## 3.8.7
4
10
 
5
11
  ### Patch Changes
@@ -525,7 +525,13 @@ function convertToOllamaChatMessages(prompt) {
525
525
  }
526
526
 
527
527
  // src/utils/map-ollama-finish-reason.ts
528
- function mapOllamaFinishReason(reason) {
528
+ function mapOllamaFinishReason(reason, hasToolCalls = false) {
529
+ if (hasToolCalls) {
530
+ return {
531
+ unified: "tool-calls",
532
+ raw: reason || void 0
533
+ };
534
+ }
529
535
  if (!reason) {
530
536
  return {
531
537
  unified: "stop",
@@ -576,6 +582,28 @@ var OllamaError = class _OllamaError extends Error {
576
582
  // src/utils/object-generation-reliability.ts
577
583
  var import_provider_utils = require("@ai-sdk/provider-utils");
578
584
  var import_jsonrepair2 = require("jsonrepair");
585
+ var WHITESPACE_CHARS = /* @__PURE__ */ new Set([" ", " ", "\n"]);
586
+ var STRUCTURAL_CLOSE_CHARS = /* @__PURE__ */ new Set(["}", "]"]);
587
+ var KEY_START_CHARS = /* @__PURE__ */ new Set(['"', "\u201C", "_", "$"]);
588
+ var VALUE_START_CHARS = /* @__PURE__ */ new Set([
589
+ '"',
590
+ "\u201C",
591
+ "{",
592
+ "[",
593
+ "-",
594
+ "t",
595
+ "T",
596
+ "f",
597
+ "F",
598
+ "n",
599
+ "N"
600
+ ]);
601
+ var SMART_SINGLE_QUOTE_CHARS = /* @__PURE__ */ new Set([
602
+ "\u2018",
603
+ "\u2019",
604
+ "`",
605
+ "\xB4"
606
+ ]);
579
607
  function isZodSchema(schema) {
580
608
  return typeof schema === "object" && schema !== null && "parse" in schema && typeof schema.parse === "function";
581
609
  }
@@ -926,11 +954,14 @@ function replaceSmartQuotesOutsideStrings(text) {
926
954
  let j = i + 1;
927
955
  while (j < text.length) {
928
956
  const nextChar = text[j];
929
- if (nextChar === " " || nextChar === " " || nextChar === "\n") {
957
+ if (!nextChar) {
958
+ break;
959
+ }
960
+ if (WHITESPACE_CHARS.has(nextChar)) {
930
961
  j++;
931
962
  continue;
932
963
  }
933
- if (nextChar === "}" || nextChar === "]") {
964
+ if (STRUCTURAL_CLOSE_CHARS.has(nextChar)) {
934
965
  isClosing = true;
935
966
  break;
936
967
  }
@@ -941,18 +972,21 @@ function replaceSmartQuotesOutsideStrings(text) {
941
972
  if (!afterComma) {
942
973
  break;
943
974
  }
944
- if (afterComma === " " || afterComma === " " || afterComma === "\n") {
975
+ if (WHITESPACE_CHARS.has(afterComma)) {
945
976
  k++;
946
977
  continue;
947
978
  }
948
- if (afterComma === '"' || afterComma === "\u201C" || afterComma >= "a" && afterComma <= "z" || afterComma >= "A" && afterComma <= "Z" || afterComma === "_" || afterComma === "$") {
979
+ if (KEY_START_CHARS.has(afterComma) || afterComma >= "a" && afterComma <= "z" || afterComma >= "A" && afterComma <= "Z") {
949
980
  isClosing = true;
950
981
  break;
951
982
  } else if (afterComma >= "0" && afterComma <= "9") {
952
983
  let m = k + 1;
953
984
  while (m < text.length) {
954
985
  const afterDigit = text[m];
955
- if (afterDigit === " " || afterDigit === " " || afterDigit === "\n") {
986
+ if (!afterDigit) {
987
+ break;
988
+ }
989
+ if (WHITESPACE_CHARS.has(afterDigit)) {
956
990
  m++;
957
991
  continue;
958
992
  }
@@ -979,12 +1013,11 @@ function replaceSmartQuotesOutsideStrings(text) {
979
1013
  if (!afterColon) {
980
1014
  break;
981
1015
  }
982
- if (afterColon === " " || afterColon === " " || afterColon === "\n") {
1016
+ if (WHITESPACE_CHARS.has(afterColon)) {
983
1017
  k++;
984
1018
  continue;
985
1019
  }
986
- if (afterColon === '"' || afterColon === "\u201C" || // Opening smart quote
987
- afterColon === "{" || afterColon === "[" || afterColon >= "0" && afterColon <= "9" || afterColon === "-" || afterColon === "t" || afterColon === "T" || afterColon === "f" || afterColon === "F" || afterColon === "n" || afterColon === "N") {
1020
+ if (VALUE_START_CHARS.has(afterColon) || afterColon >= "0" && afterColon <= "9") {
988
1021
  isClosing = true;
989
1022
  }
990
1023
  break;
@@ -1003,7 +1036,7 @@ function replaceSmartQuotesOutsideStrings(text) {
1003
1036
  }
1004
1037
  }
1005
1038
  if (!inSingleQuote && !inDoubleQuote && !inSmartDoubleQuote) {
1006
- if (char === "\u2018" || char === "\u2019" || char === "`" || char === "\xB4") {
1039
+ if (char !== void 0 && SMART_SINGLE_QUOTE_CHARS.has(char)) {
1007
1040
  result += "'";
1008
1041
  i++;
1009
1042
  continue;
@@ -1862,7 +1895,10 @@ var OllamaChatLanguageModel = class {
1862
1895
  });
1863
1896
  }
1864
1897
  }
1865
- const finishReason = parsedToolCalls.length > 0 ? mapOllamaFinishReason("stop") : mapOllamaFinishReason(response.done_reason);
1898
+ const finishReason = mapOllamaFinishReason(
1899
+ response.done_reason,
1900
+ parsedToolCalls.length > 0
1901
+ );
1866
1902
  return {
1867
1903
  content,
1868
1904
  finishReason,
@@ -2455,7 +2491,10 @@ var OllamaChatLanguageModel = class {
2455
2491
  chunk.prompt_eval_count ?? void 0,
2456
2492
  chunk.eval_count ?? void 0
2457
2493
  );
2458
- finishReason = hasToolCalls ? mapOllamaFinishReason("stop") : mapOllamaFinishReason(chunk.done_reason);
2494
+ finishReason = mapOllamaFinishReason(
2495
+ chunk.done_reason,
2496
+ hasToolCalls
2497
+ );
2459
2498
  controller.enqueue({
2460
2499
  type: "finish",
2461
2500
  finishReason,