@ai-sdk-tool/parser 2.1.0 → 2.1.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
@@ -1,13 +1,78 @@
1
1
  import * as _ai_sdk_provider from '@ai-sdk/provider';
2
2
  import { LanguageModelV2FunctionTool, LanguageModelV2ToolCall, LanguageModelV2ToolResultPart, LanguageModelV2Content, LanguageModelV2StreamPart, LanguageModelV2Middleware } from '@ai-sdk/provider';
3
3
 
4
+ /**
5
+ * ToolCallProtocol
6
+ *
7
+ * A pluggable strategy that defines how tools are surfaced to a model, how
8
+ * tool calls are rendered into provider-facing text, and how model output is
9
+ * parsed back into the AI SDK v2 content/stream primitives.
10
+ *
11
+ * Implementations can choose any wire format (e.g. XML, JSON-with-tags, etc.)
12
+ * as long as they respect the contract below:
13
+ * - Static formatting helpers (`formatTools`, `formatToolCall`, `formatToolResponse`)
14
+ * are used to construct strings that the model will read.
15
+ * - Parsing helpers (`parseGeneratedText`, `createStreamParser`) must convert
16
+ * model output back into structured `LanguageModelV2Content` parts, emitting
17
+ * `text` for regular content and `tool-call` for detected tool invocations.
18
+ */
4
19
  interface ToolCallProtocol {
20
+ /**
21
+ * Produces a provider-facing string that describes all available tools.
22
+ *
23
+ * Typical usage is to serialize each tool's `name`, `description`, and
24
+ * JSON schema and inject that text into a system prompt using the supplied
25
+ * `toolSystemPromptTemplate`.
26
+ *
27
+ * Implementations should be resilient to empty inputs.
28
+ *
29
+ * @param tools List of tools that can be invoked by the model.
30
+ * @param toolSystemPromptTemplate Function that receives the serialized
31
+ * tools and returns the final prompt text.
32
+ * @returns A string to be embedded into the model's system prompt.
33
+ */
5
34
  formatTools({ tools, toolSystemPromptTemplate, }: {
6
35
  tools: LanguageModelV2FunctionTool[];
7
36
  toolSystemPromptTemplate: (tools: string) => string;
8
37
  }): string;
38
+ /**
39
+ * Renders a single tool invocation into provider-facing text.
40
+ *
41
+ * Implementations may accept `toolCall.input` as a JSON string or as an
42
+ * object (some runtimes normalize prior to calling). The result should be a
43
+ * string that the model can understand and that the paired parser can later
44
+ * recognize and recover as a `tool-call`.
45
+ *
46
+ * @param toolCall The tool call to format for the model.
47
+ * @returns A textual representation of the tool call (e.g., an XML element).
48
+ */
9
49
  formatToolCall(toolCall: LanguageModelV2ToolCall): string;
50
+ /**
51
+ * Formats a tool result payload so the model can consume it as plain text.
52
+ *
53
+ * This is commonly used to echo tool outputs back to the model in a format
54
+ * symmetrical to `formatToolCall`.
55
+ *
56
+ * @param toolResult The result part produced after executing a tool.
57
+ * @returns Textual representation of the tool result for the model.
58
+ */
10
59
  formatToolResponse(toolResult: LanguageModelV2ToolResultPart): string;
60
+ /**
61
+ * Parses a fully generated text (non-streaming) response from the model and
62
+ * converts it into a sequence of `LanguageModelV2Content` parts.
63
+ *
64
+ * Implementations should:
65
+ * - Detect tool-call segments addressed to known `tools` and emit
66
+ * `{ type: "tool-call", toolName, input }` parts.
67
+ * - Emit `{ type: "text", text }` parts for any non-tool segments.
68
+ * - Call `options.onError` and fall back to emitting the original text if a
69
+ * segment cannot be parsed into a valid tool call.
70
+ *
71
+ * @param text The model output to parse.
72
+ * @param tools The list of tools that may be invoked.
73
+ * @param options Optional error callback for non-fatal parsing issues.
74
+ * @returns A list of structured content parts derived from the text.
75
+ */
11
76
  parseGeneratedText({ text, tools, options, }: {
12
77
  text: string;
13
78
  tools: LanguageModelV2FunctionTool[];
@@ -15,6 +80,22 @@ interface ToolCallProtocol {
15
80
  onError?: (message: string, metadata?: Record<string, unknown>) => void;
16
81
  };
17
82
  }): LanguageModelV2Content[];
83
+ /**
84
+ * Creates a TransformStream that converts streaming model deltas
85
+ * (`LanguageModelV2StreamPart`) into a normalized sequence of stream parts,
86
+ * including `text-start`/`text-delta`/`text-end` and `tool-call` events.
87
+ *
88
+ * The stream parser should:
89
+ * - Buffer text until a complete tool-call segment can be recognized, then
90
+ * emit a `tool-call` part and properly close/open text segments around it.
91
+ * - Be robust to partial/incomplete fragments commonly seen in streaming.
92
+ * - Invoke `options.onError` and pass through the original text when a
93
+ * segment cannot be parsed into a valid tool call.
94
+ *
95
+ * @param tools The list of tools that may be invoked by the model.
96
+ * @param options Optional error callback for non-fatal streaming issues.
97
+ * @returns A TransformStream that accepts and emits `LanguageModelV2StreamPart`s.
98
+ */
18
99
  createStreamParser({ tools, options, }: {
19
100
  tools: LanguageModelV2FunctionTool[];
20
101
  options?: {
@@ -38,8 +119,16 @@ declare const jsonMixProtocol: ({ toolCallStart, toolCallEnd, toolResponseStart,
38
119
 
39
120
  declare const xmlProtocol: () => ToolCallProtocol;
40
121
 
122
+ declare function unwrapJsonSchema(schema: unknown): unknown;
123
+ declare function getSchemaType(schema: unknown): string | undefined;
124
+ declare function coerceBySchema(value: unknown, schema?: unknown): unknown;
125
+ declare function fixToolCallWithSchema(part: LanguageModelV2Content, tools: Array<{
126
+ name?: string;
127
+ inputSchema?: unknown;
128
+ }>): LanguageModelV2Content;
129
+
41
130
  declare const gemmaToolMiddleware: _ai_sdk_provider.LanguageModelV2Middleware;
42
131
  declare const hermesToolMiddleware: _ai_sdk_provider.LanguageModelV2Middleware;
43
132
  declare const xmlToolMiddleware: _ai_sdk_provider.LanguageModelV2Middleware;
44
133
 
45
- export { createToolMiddleware, gemmaToolMiddleware, hermesToolMiddleware, jsonMixProtocol, xmlProtocol, xmlToolMiddleware };
134
+ export { coerceBySchema, createToolMiddleware, fixToolCallWithSchema, gemmaToolMiddleware, getSchemaType, hermesToolMiddleware, jsonMixProtocol, unwrapJsonSchema, xmlProtocol, xmlToolMiddleware };
package/dist/index.d.ts CHANGED
@@ -1,13 +1,78 @@
1
1
  import * as _ai_sdk_provider from '@ai-sdk/provider';
2
2
  import { LanguageModelV2FunctionTool, LanguageModelV2ToolCall, LanguageModelV2ToolResultPart, LanguageModelV2Content, LanguageModelV2StreamPart, LanguageModelV2Middleware } from '@ai-sdk/provider';
3
3
 
4
+ /**
5
+ * ToolCallProtocol
6
+ *
7
+ * A pluggable strategy that defines how tools are surfaced to a model, how
8
+ * tool calls are rendered into provider-facing text, and how model output is
9
+ * parsed back into the AI SDK v2 content/stream primitives.
10
+ *
11
+ * Implementations can choose any wire format (e.g. XML, JSON-with-tags, etc.)
12
+ * as long as they respect the contract below:
13
+ * - Static formatting helpers (`formatTools`, `formatToolCall`, `formatToolResponse`)
14
+ * are used to construct strings that the model will read.
15
+ * - Parsing helpers (`parseGeneratedText`, `createStreamParser`) must convert
16
+ * model output back into structured `LanguageModelV2Content` parts, emitting
17
+ * `text` for regular content and `tool-call` for detected tool invocations.
18
+ */
4
19
  interface ToolCallProtocol {
20
+ /**
21
+ * Produces a provider-facing string that describes all available tools.
22
+ *
23
+ * Typical usage is to serialize each tool's `name`, `description`, and
24
+ * JSON schema and inject that text into a system prompt using the supplied
25
+ * `toolSystemPromptTemplate`.
26
+ *
27
+ * Implementations should be resilient to empty inputs.
28
+ *
29
+ * @param tools List of tools that can be invoked by the model.
30
+ * @param toolSystemPromptTemplate Function that receives the serialized
31
+ * tools and returns the final prompt text.
32
+ * @returns A string to be embedded into the model's system prompt.
33
+ */
5
34
  formatTools({ tools, toolSystemPromptTemplate, }: {
6
35
  tools: LanguageModelV2FunctionTool[];
7
36
  toolSystemPromptTemplate: (tools: string) => string;
8
37
  }): string;
38
+ /**
39
+ * Renders a single tool invocation into provider-facing text.
40
+ *
41
+ * Implementations may accept `toolCall.input` as a JSON string or as an
42
+ * object (some runtimes normalize prior to calling). The result should be a
43
+ * string that the model can understand and that the paired parser can later
44
+ * recognize and recover as a `tool-call`.
45
+ *
46
+ * @param toolCall The tool call to format for the model.
47
+ * @returns A textual representation of the tool call (e.g., an XML element).
48
+ */
9
49
  formatToolCall(toolCall: LanguageModelV2ToolCall): string;
50
+ /**
51
+ * Formats a tool result payload so the model can consume it as plain text.
52
+ *
53
+ * This is commonly used to echo tool outputs back to the model in a format
54
+ * symmetrical to `formatToolCall`.
55
+ *
56
+ * @param toolResult The result part produced after executing a tool.
57
+ * @returns Textual representation of the tool result for the model.
58
+ */
10
59
  formatToolResponse(toolResult: LanguageModelV2ToolResultPart): string;
60
+ /**
61
+ * Parses a fully generated text (non-streaming) response from the model and
62
+ * converts it into a sequence of `LanguageModelV2Content` parts.
63
+ *
64
+ * Implementations should:
65
+ * - Detect tool-call segments addressed to known `tools` and emit
66
+ * `{ type: "tool-call", toolName, input }` parts.
67
+ * - Emit `{ type: "text", text }` parts for any non-tool segments.
68
+ * - Call `options.onError` and fall back to emitting the original text if a
69
+ * segment cannot be parsed into a valid tool call.
70
+ *
71
+ * @param text The model output to parse.
72
+ * @param tools The list of tools that may be invoked.
73
+ * @param options Optional error callback for non-fatal parsing issues.
74
+ * @returns A list of structured content parts derived from the text.
75
+ */
11
76
  parseGeneratedText({ text, tools, options, }: {
12
77
  text: string;
13
78
  tools: LanguageModelV2FunctionTool[];
@@ -15,6 +80,22 @@ interface ToolCallProtocol {
15
80
  onError?: (message: string, metadata?: Record<string, unknown>) => void;
16
81
  };
17
82
  }): LanguageModelV2Content[];
83
+ /**
84
+ * Creates a TransformStream that converts streaming model deltas
85
+ * (`LanguageModelV2StreamPart`) into a normalized sequence of stream parts,
86
+ * including `text-start`/`text-delta`/`text-end` and `tool-call` events.
87
+ *
88
+ * The stream parser should:
89
+ * - Buffer text until a complete tool-call segment can be recognized, then
90
+ * emit a `tool-call` part and properly close/open text segments around it.
91
+ * - Be robust to partial/incomplete fragments commonly seen in streaming.
92
+ * - Invoke `options.onError` and pass through the original text when a
93
+ * segment cannot be parsed into a valid tool call.
94
+ *
95
+ * @param tools The list of tools that may be invoked by the model.
96
+ * @param options Optional error callback for non-fatal streaming issues.
97
+ * @returns A TransformStream that accepts and emits `LanguageModelV2StreamPart`s.
98
+ */
18
99
  createStreamParser({ tools, options, }: {
19
100
  tools: LanguageModelV2FunctionTool[];
20
101
  options?: {
@@ -38,8 +119,16 @@ declare const jsonMixProtocol: ({ toolCallStart, toolCallEnd, toolResponseStart,
38
119
 
39
120
  declare const xmlProtocol: () => ToolCallProtocol;
40
121
 
122
+ declare function unwrapJsonSchema(schema: unknown): unknown;
123
+ declare function getSchemaType(schema: unknown): string | undefined;
124
+ declare function coerceBySchema(value: unknown, schema?: unknown): unknown;
125
+ declare function fixToolCallWithSchema(part: LanguageModelV2Content, tools: Array<{
126
+ name?: string;
127
+ inputSchema?: unknown;
128
+ }>): LanguageModelV2Content;
129
+
41
130
  declare const gemmaToolMiddleware: _ai_sdk_provider.LanguageModelV2Middleware;
42
131
  declare const hermesToolMiddleware: _ai_sdk_provider.LanguageModelV2Middleware;
43
132
  declare const xmlToolMiddleware: _ai_sdk_provider.LanguageModelV2Middleware;
44
133
 
45
- export { createToolMiddleware, gemmaToolMiddleware, hermesToolMiddleware, jsonMixProtocol, xmlProtocol, xmlToolMiddleware };
134
+ export { coerceBySchema, createToolMiddleware, fixToolCallWithSchema, gemmaToolMiddleware, getSchemaType, hermesToolMiddleware, jsonMixProtocol, unwrapJsonSchema, xmlProtocol, xmlToolMiddleware };