@ai-sdk-tool/parser 3.3.3 → 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.
package/dist/index.d.cts CHANGED
@@ -8,6 +8,12 @@ export { ParseOptions, parse, stringify, transform } from './rjson.cjs';
8
8
  */
9
9
  interface ParserOptions {
10
10
  onError?: (message: string, metadata?: Record<string, unknown>) => void;
11
+ /**
12
+ * When true, stream parsers may emit malformed raw tool-call text as
13
+ * `text-delta` fallback on parse failure. Defaults to false to avoid leaking
14
+ * protocol/internal markup to end users.
15
+ */
16
+ emitRawToolCallTextOnError?: boolean;
11
17
  }
12
18
  interface TCMProtocol {
13
19
  formatTools({ tools, toolSystemPromptTemplate, }: {
@@ -18,15 +24,11 @@ interface TCMProtocol {
18
24
  parseGeneratedText({ text, tools, options, }: {
19
25
  text: string;
20
26
  tools: LanguageModelV3FunctionTool[];
21
- options?: {
22
- onError?: (message: string, metadata?: Record<string, unknown>) => void;
23
- };
27
+ options?: ParserOptions;
24
28
  }): LanguageModelV3Content[];
25
29
  createStreamParser({ tools, options, }: {
26
30
  tools: LanguageModelV3FunctionTool[];
27
- options?: {
28
- onError?: (message: string, metadata?: Record<string, unknown>) => void;
29
- };
31
+ options?: ParserOptions;
30
32
  }): TransformStream<LanguageModelV3StreamPart, LanguageModelV3StreamPart>;
31
33
  extractToolCallSegments?: ({ text, tools, }: {
32
34
  text: string;
package/dist/index.d.ts CHANGED
@@ -8,6 +8,12 @@ export { ParseOptions, parse, stringify, transform } from './rjson.js';
8
8
  */
9
9
  interface ParserOptions {
10
10
  onError?: (message: string, metadata?: Record<string, unknown>) => void;
11
+ /**
12
+ * When true, stream parsers may emit malformed raw tool-call text as
13
+ * `text-delta` fallback on parse failure. Defaults to false to avoid leaking
14
+ * protocol/internal markup to end users.
15
+ */
16
+ emitRawToolCallTextOnError?: boolean;
11
17
  }
12
18
  interface TCMProtocol {
13
19
  formatTools({ tools, toolSystemPromptTemplate, }: {
@@ -18,15 +24,11 @@ interface TCMProtocol {
18
24
  parseGeneratedText({ text, tools, options, }: {
19
25
  text: string;
20
26
  tools: LanguageModelV3FunctionTool[];
21
- options?: {
22
- onError?: (message: string, metadata?: Record<string, unknown>) => void;
23
- };
27
+ options?: ParserOptions;
24
28
  }): LanguageModelV3Content[];
25
29
  createStreamParser({ tools, options, }: {
26
30
  tools: LanguageModelV3FunctionTool[];
27
- options?: {
28
- onError?: (message: string, metadata?: Record<string, unknown>) => void;
29
- };
31
+ options?: ParserOptions;
30
32
  }): TransformStream<LanguageModelV3StreamPart, LanguageModelV3StreamPart>;
31
33
  extractToolCallSegments?: ({ text, tools, }: {
32
34
  text: string;
package/dist/index.js CHANGED
@@ -27,16 +27,16 @@ import {
27
27
  xmlToolMiddleware,
28
28
  yamlProtocol,
29
29
  yamlToolMiddleware
30
- } from "./chunk-NAQSTPDQ.js";
30
+ } from "./chunk-DPGORNPB.js";
31
31
  import {
32
32
  escapeRegExp
33
- } from "./chunk-CXWS24JX.js";
33
+ } from "./chunk-76E6H46R.js";
34
34
  import {
35
35
  parse,
36
36
  stringify,
37
37
  transform
38
38
  } from "./chunk-IX4FJELL.js";
39
- import "./chunk-2KK5BDZF.js";
39
+ import "./chunk-DJB4DAZO.js";
40
40
  export {
41
41
  createDynamicIfThenElseSchema,
42
42
  createToolMiddleware,
package/dist/rxml.cjs CHANGED
@@ -586,13 +586,20 @@ function coerceStringWithoutSchema(value) {
586
586
  }
587
587
  function coerceStringToObject(s, unwrapped) {
588
588
  try {
589
- let normalized = s.replace(/'/g, '"');
590
- normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
591
- const obj = JSON.parse(normalized);
589
+ const obj = JSON.parse(s);
592
590
  if (obj && typeof obj === "object" && !Array.isArray(obj)) {
593
591
  return coerceObjectToObject(obj, unwrapped);
594
592
  }
595
593
  } catch (e) {
594
+ try {
595
+ let normalized = s.replace(/'/g, '"');
596
+ normalized = normalized.replace(EMPTY_OBJECT_REGEX, "{}");
597
+ const obj = JSON.parse(normalized);
598
+ if (obj && typeof obj === "object" && !Array.isArray(obj)) {
599
+ return coerceObjectToObject(obj, unwrapped);
600
+ }
601
+ } catch (e2) {
602
+ }
596
603
  }
597
604
  return null;
598
605
  }
@@ -600,8 +607,7 @@ function coerceStringToArray(s, unwrapped) {
600
607
  const prefixItems = Array.isArray(unwrapped.prefixItems) ? unwrapped.prefixItems : void 0;
601
608
  const itemsSchema = unwrapped.items;
602
609
  try {
603
- const normalized = s.replace(/'/g, '"');
604
- const arr = JSON.parse(normalized);
610
+ const arr = JSON.parse(s);
605
611
  if (Array.isArray(arr)) {
606
612
  if (prefixItems && arr.length === prefixItems.length) {
607
613
  return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
@@ -609,6 +615,17 @@ function coerceStringToArray(s, unwrapped) {
609
615
  return arr.map((v) => coerceBySchema(v, itemsSchema));
610
616
  }
611
617
  } catch (e) {
618
+ try {
619
+ const normalized = s.replace(/'/g, '"');
620
+ const arr = JSON.parse(normalized);
621
+ if (Array.isArray(arr)) {
622
+ if (prefixItems && arr.length === prefixItems.length) {
623
+ return arr.map((v, i) => coerceBySchema(v, prefixItems[i]));
624
+ }
625
+ return arr.map((v) => coerceBySchema(v, itemsSchema));
626
+ }
627
+ } catch (e2) {
628
+ }
612
629
  const csv = s.includes("\n") ? s.split(NEWLINE_SPLIT_REGEX) : s.split(COMMA_SPLIT_REGEX);
613
630
  const trimmed = csv.map((x) => x.trim()).filter((x) => x.length > 0);
614
631
  if (prefixItems && trimmed.length === prefixItems.length) {