@ai-sdk-tool/parser 3.2.1 → 3.3.0
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/README.md +24 -116
- package/dist/{chunk-DCK5APVO.js → chunk-3QJVNEHE.js} +15 -12
- package/dist/chunk-3QJVNEHE.js.map +1 -0
- package/dist/chunk-DZB6Y354.js +240 -0
- package/dist/chunk-DZB6Y354.js.map +1 -0
- package/dist/chunk-IX4FJELL.js +671 -0
- package/dist/chunk-IX4FJELL.js.map +1 -0
- package/dist/chunk-TR2ARLIF.js +2216 -0
- package/dist/chunk-TR2ARLIF.js.map +1 -0
- package/dist/community.cjs +3293 -276
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +4 -1
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +3356 -254
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +11 -1
- package/dist/rjson.cjs +697 -0
- package/dist/rjson.cjs.map +1 -0
- package/dist/rjson.d.cts +108 -0
- package/dist/rjson.d.ts +108 -0
- package/dist/rjson.js +11 -0
- package/dist/rjson.js.map +1 -0
- package/dist/rxml.cjs +2471 -0
- package/dist/rxml.cjs.map +1 -0
- package/dist/rxml.d.cts +90 -0
- package/dist/rxml.d.ts +90 -0
- package/dist/rxml.js +10 -0
- package/dist/rxml.js.map +1 -0
- package/dist/schema-coerce.cjs +266 -0
- package/dist/schema-coerce.cjs.map +1 -0
- package/dist/schema-coerce.d.cts +5 -0
- package/dist/schema-coerce.d.ts +5 -0
- package/dist/schema-coerce.js +11 -0
- package/dist/schema-coerce.js.map +1 -0
- package/package.json +45 -14
- package/dist/chunk-DCK5APVO.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,131 +1,39 @@
|
|
|
1
|
-
|
|
1
|
+
<img width="3168" height="1344" alt="AI SDK Tool monorepo banner" src="https://github.com/user-attachments/assets/9a002988-e535-42ac-8baf-56ec8754410f" />
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/@ai-sdk-tool/parser)
|
|
3
|
+
----
|
|
4
|
+
[](https://www.npmjs.com/package/@ai-sdk-tool/parser)
|
|
5
|
+
[](https://www.npmjs.com/package/@ai-sdk-tool/parser)
|
|
7
6
|
[](https://codecov.io/gh/minpeter/ai-sdk-tool-call-middleware)
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
> Requires AI SDK v6. For AI SDK v5, use `@ai-sdk-tool/parser@3.x`. For AI SDK v4, pin `@ai-sdk-tool/parser@1.0.0`.
|
|
11
|
-
|
|
12
|
-
Middleware that enables tool calling with models that don’t natively support OpenAI‑style `tools`. Works with any provider (OpenRouter, vLLM, Ollama, etc.) via AI SDK middleware.
|
|
13
|
-
|
|
14
|
-
## Why This Exists
|
|
15
|
-
|
|
16
|
-
Many self‑hosted or third‑party model endpoints (vLLM, MLC‑LLM, Ollama, OpenRouter, etc.) don’t yet expose the OpenAI‑style `tools` parameter, forcing you to hack together tool parsing.
|
|
17
|
-
This project provides a flexible middleware that:
|
|
8
|
+
Tooling for Vercel AI SDK: enable tool calling with models lacking native `tools`.
|
|
18
9
|
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
10
|
+
- **@ai-sdk-tool/parser**: add tool-calling via middleware; works with any provider supported by AI SDK `wrapLanguageModel`.
|
|
11
|
+
- **@ai-sdk-tool/parser/rxml**: robust XML parser/streamer/builder for AI-generated XML.
|
|
12
|
+
- **@ai-sdk-tool/parser/rjson**: relaxed JSON parser with tolerant mode and JSON5-like syntax support.
|
|
22
13
|
|
|
23
|
-
##
|
|
14
|
+
## Usage at a glance
|
|
24
15
|
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Quickstart (streaming)
|
|
30
|
-
|
|
31
|
-
```typescript
|
|
16
|
+
```ts
|
|
17
|
+
import { wrapLanguageModel, streamText } from "ai";
|
|
32
18
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
33
|
-
import { wrapLanguageModel, stepCountIs, streamText } from "ai";
|
|
34
19
|
import { xmlToolMiddleware } from "@ai-sdk-tool/parser";
|
|
35
20
|
|
|
36
|
-
const
|
|
37
|
-
/*
|
|
21
|
+
const client = createOpenAICompatible({
|
|
22
|
+
/* baseURL, apiKey */
|
|
38
23
|
});
|
|
39
24
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
model:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
tools: {
|
|
50
|
-
get_location: {
|
|
51
|
-
/* ... */
|
|
52
|
-
},
|
|
53
|
-
get_weather: {
|
|
54
|
-
/* ... */
|
|
55
|
-
},
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
for await (const part of result.fullStream) {
|
|
60
|
-
// handle text/tool events
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
main().catch(console.error);
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## Quickstart (generate)
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
71
|
-
import { wrapLanguageModel, generateText } from "ai";
|
|
72
|
-
import { hermesToolMiddleware } from "@ai-sdk-tool/parser";
|
|
73
|
-
|
|
74
|
-
const openrouter = createOpenAICompatible({
|
|
75
|
-
/* ... */
|
|
25
|
+
const result = streamText({
|
|
26
|
+
model: wrapLanguageModel({
|
|
27
|
+
model: client("your-model-name"),
|
|
28
|
+
middleware: xmlToolMiddleware,
|
|
29
|
+
}),
|
|
30
|
+
tools: {
|
|
31
|
+
/* your tools */
|
|
32
|
+
},
|
|
33
|
+
prompt: "Find weather for Seoul today",
|
|
76
34
|
});
|
|
77
35
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
model: wrapLanguageModel({
|
|
81
|
-
model: openrouter("nousresearch/hermes-3-llama-3.1-70b"),
|
|
82
|
-
middleware: hermesToolMiddleware,
|
|
83
|
-
}),
|
|
84
|
-
prompt: "Find weather for Seoul today",
|
|
85
|
-
tools: {
|
|
86
|
-
get_weather: {
|
|
87
|
-
/* ... */
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
console.log(text);
|
|
36
|
+
for await (const part of result.fullStream) {
|
|
37
|
+
// handle text and tool events
|
|
93
38
|
}
|
|
94
|
-
|
|
95
|
-
main().catch(console.error);
|
|
96
39
|
```
|
|
97
|
-
|
|
98
|
-
## Prebuilt middlewares
|
|
99
|
-
|
|
100
|
-
- `hermesToolMiddleware` — JSON‑mix format wrapped in `<tool_call>` XML tags.
|
|
101
|
-
- `xmlToolMiddleware` — XML format (Morph‑XML protocol).
|
|
102
|
-
|
|
103
|
-
## Protocols
|
|
104
|
-
|
|
105
|
-
- `jsonProtocol` — JSON function calls in flexible text wrappers.
|
|
106
|
-
- `xmlProtocol` — XML element per call, robust to streaming.
|
|
107
|
-
|
|
108
|
-
## Tool choice support
|
|
109
|
-
|
|
110
|
-
- `toolChoice: { type: "required" }`: forces one tool call. Middleware sets a JSON response schema to validate calls.
|
|
111
|
-
- `toolChoice: { type: "tool", toolName }`: forces a specific tool. Provider‑defined tools are not supported; pass only custom function tools.
|
|
112
|
-
- `toolChoice: { type: "none" }` is not supported and will throw.
|
|
113
|
-
|
|
114
|
-
## Examples
|
|
115
|
-
|
|
116
|
-
See `examples/parser-core/src/*` for runnable demos (streaming/non‑streaming, tool choice).
|
|
117
|
-
|
|
118
|
-
## [dev] Contributor notes
|
|
119
|
-
|
|
120
|
-
- Debugging:
|
|
121
|
-
- Set `DEBUG_PARSER_MW=stream` to log raw/parsed chunks during runs.
|
|
122
|
-
- Set `DEBUG_PARSER_MW=parse` to log original matched text and parsed summary.
|
|
123
|
-
- Optional `DEBUG_PARSER_MW_STYLE=bg|inverse|underline|bold` to change highlight style.
|
|
124
|
-
- Provider options passthrough: `providerOptions.toolCallMiddleware` fields are merged into protocol options. Internal fields used:
|
|
125
|
-
- `originalTools`: internal propagation of custom tool schemas.
|
|
126
|
-
- `toolChoice`: internal fast‑path activation for required/specific tool modes.
|
|
127
|
-
- Transform details: `transformParams` injects a system message built from protocol `formatTools` and clears `tools` since many providers strip/ignore them.
|
|
128
|
-
|
|
129
|
-
## License
|
|
130
|
-
|
|
131
|
-
Licensed under Apache License 2.0. See the repository `LICENSE`. Include the `NOTICE` file in distributions.
|
|
@@ -1,8 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
parse as parse2,
|
|
3
|
+
stringify
|
|
4
|
+
} from "./chunk-TR2ARLIF.js";
|
|
5
|
+
import {
|
|
6
|
+
parse
|
|
7
|
+
} from "./chunk-IX4FJELL.js";
|
|
8
|
+
import {
|
|
9
|
+
coerceBySchema
|
|
10
|
+
} from "./chunk-DZB6Y354.js";
|
|
6
11
|
|
|
7
12
|
// src/core/utils/debug.ts
|
|
8
13
|
var LINE_SPLIT_REGEX = /\r?\n/;
|
|
@@ -191,7 +196,7 @@ function escapeRegExp(literal) {
|
|
|
191
196
|
function processToolCallJson(toolCallJson, fullMatch, processedElements, options) {
|
|
192
197
|
var _a, _b;
|
|
193
198
|
try {
|
|
194
|
-
const parsedToolCall =
|
|
199
|
+
const parsedToolCall = parse(toolCallJson);
|
|
195
200
|
processedElements.push({
|
|
196
201
|
type: "tool-call",
|
|
197
202
|
toolCallId: generateId(),
|
|
@@ -319,7 +324,7 @@ function emitToolCall(context) {
|
|
|
319
324
|
var _a, _b;
|
|
320
325
|
const { state, controller, toolCallStart, toolCallEnd, options } = context;
|
|
321
326
|
try {
|
|
322
|
-
const parsedToolCall =
|
|
327
|
+
const parsedToolCall = parse(state.currentToolCallJson);
|
|
323
328
|
closeTextBlock(state, controller);
|
|
324
329
|
controller.enqueue({
|
|
325
330
|
type: "tool-call",
|
|
@@ -512,7 +517,6 @@ function isTCMProtocolFactory(protocol) {
|
|
|
512
517
|
}
|
|
513
518
|
|
|
514
519
|
// src/core/protocols/xml-protocol.ts
|
|
515
|
-
import { parse, stringify } from "@ai-sdk-tool/rxml";
|
|
516
520
|
var NAME_CHAR_RE = /[A-Za-z0-9_:-]/;
|
|
517
521
|
var WHITESPACE_REGEX = /\s/;
|
|
518
522
|
function getToolSchema(tools, toolName) {
|
|
@@ -528,7 +532,7 @@ function processToolCall(params) {
|
|
|
528
532
|
onError: (_a = options == null ? void 0 : options.onError) != null ? _a : parseOptions == null ? void 0 : parseOptions.onError
|
|
529
533
|
};
|
|
530
534
|
try {
|
|
531
|
-
const parsed =
|
|
535
|
+
const parsed = parse2(toolCall.content, toolSchema, parseConfig);
|
|
532
536
|
processedElements.push({
|
|
533
537
|
type: "tool-call",
|
|
534
538
|
toolCallId: generateId(),
|
|
@@ -566,7 +570,7 @@ function handleStreamingToolCallEnd(params) {
|
|
|
566
570
|
};
|
|
567
571
|
flushText(ctrl);
|
|
568
572
|
try {
|
|
569
|
-
const parsedResult =
|
|
573
|
+
const parsedResult = parse2(toolContent, toolSchema, parseConfig);
|
|
570
574
|
ctrl.enqueue({
|
|
571
575
|
type: "tool-call",
|
|
572
576
|
toolCallId: generateId(),
|
|
@@ -1778,7 +1782,6 @@ function hasInputProperty(obj) {
|
|
|
1778
1782
|
|
|
1779
1783
|
// src/generate-handler.ts
|
|
1780
1784
|
import { generateId as generateId2 } from "@ai-sdk/provider-utils";
|
|
1781
|
-
import { coerceBySchema } from "@ai-sdk-tool/schema-coerce";
|
|
1782
1785
|
function parseToolChoiceJson(text, providerOptions) {
|
|
1783
1786
|
var _a;
|
|
1784
1787
|
try {
|
|
@@ -2874,4 +2877,4 @@ export {
|
|
|
2874
2877
|
xmlToolMiddleware,
|
|
2875
2878
|
yamlToolMiddleware
|
|
2876
2879
|
};
|
|
2877
|
-
//# sourceMappingURL=chunk-
|
|
2880
|
+
//# sourceMappingURL=chunk-3QJVNEHE.js.map
|