@ai-sdk-tool/parser 4.0.0 → 4.0.1

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.
@@ -836,9 +836,19 @@ function getPotentialStartIndex(text, searchedText) {
836
836
  if (directIndex !== -1) {
837
837
  return directIndex;
838
838
  }
839
- for (let i = text.length - 1; i >= 0; i -= 1) {
840
- const suffix = text.substring(i);
841
- if (searchedText.startsWith(suffix)) {
839
+ const textLength = text.length;
840
+ const searchedTextLength = searchedText.length;
841
+ const startAt = Math.max(0, textLength - searchedTextLength + 1);
842
+ for (let i = startAt; i < textLength; i++) {
843
+ let match = true;
844
+ const currentSuffixLength = textLength - i;
845
+ for (let j = 0; j < currentSuffixLength; j++) {
846
+ if (text[i + j] !== searchedText[j]) {
847
+ match = false;
848
+ break;
849
+ }
850
+ }
851
+ if (match) {
842
852
  return i;
843
853
  }
844
854
  }
@@ -2123,13 +2133,20 @@ function coerceStringWithoutSchema(value) {
2123
2133
  }
2124
2134
  function coerceStringToObject(s, unwrapped) {
2125
2135
  try {
2126
- let normalized = s.replace(/'/g, '"');
2127
- normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
2128
- const obj = JSON.parse(normalized);
2136
+ const obj = JSON.parse(s);
2129
2137
  if (obj && typeof obj === "object" && !Array.isArray(obj)) {
2130
2138
  return coerceObjectToObject(obj, unwrapped);
2131
2139
  }
2132
2140
  } catch (e) {
2141
+ try {
2142
+ let normalized = s.replace(/'/g, '"');
2143
+ normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
2144
+ const obj = JSON.parse(normalized);
2145
+ if (obj && typeof obj === "object" && !Array.isArray(obj)) {
2146
+ return coerceObjectToObject(obj, unwrapped);
2147
+ }
2148
+ } catch (e2) {
2149
+ }
2133
2150
  }
2134
2151
  return null;
2135
2152
  }
@@ -2137,8 +2154,7 @@ function coerceStringToArray(s, unwrapped) {
2137
2154
  const prefixItems = Array.isArray(unwrapped.prefixItems) ? unwrapped.prefixItems : void 0;
2138
2155
  const itemsSchema = unwrapped.items;
2139
2156
  try {
2140
- const normalized = s.replace(/'/g, '"');
2141
- const arr = JSON.parse(normalized);
2157
+ const arr = JSON.parse(s);
2142
2158
  if (Array.isArray(arr)) {
2143
2159
  if (prefixItems && arr.length === prefixItems.length) {
2144
2160
  return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
@@ -2146,6 +2162,17 @@ function coerceStringToArray(s, unwrapped) {
2146
2162
  return arr.map((v) => coerceBySchema(v, itemsSchema));
2147
2163
  }
2148
2164
  } catch (e) {
2165
+ try {
2166
+ const normalized = s.replace(/'/g, '"');
2167
+ const arr = JSON.parse(normalized);
2168
+ if (Array.isArray(arr)) {
2169
+ if (prefixItems && arr.length === prefixItems.length) {
2170
+ return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
2171
+ }
2172
+ return arr.map((v) => coerceBySchema(v, itemsSchema));
2173
+ }
2174
+ } catch (e2) {
2175
+ }
2149
2176
  const csv = s.includes("\n") ? s.split(NEWLINE_SPLIT_REGEX) : s.split(COMMA_SPLIT_REGEX);
2150
2177
  const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
2151
2178
  if (prefixItems && trimmed.length === prefixItems.length) {
@@ -6125,9 +6152,9 @@ function findEarliestTagPosition(openIdx, selfIdx) {
6125
6152
  function collectToolCallsForName(text, toolName) {
6126
6153
  const toolCalls = [];
6127
6154
  let searchIndex = 0;
6155
+ const startTag = `<${toolName}>`;
6128
6156
  const selfTagRegex = getSelfClosingTagPattern2(toolName);
6129
6157
  while (searchIndex < text.length) {
6130
- const startTag = `<${toolName}>`;
6131
6158
  const openIdx = text.indexOf(startTag, searchIndex);
6132
6159
  selfTagRegex.lastIndex = searchIndex;
6133
6160
  const selfMatch = selfTagRegex.exec(text);