@ai-sdk-tool/parser 3.2.0 → 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-PIUBQRFC.js → chunk-3QJVNEHE.js} +369 -59
- 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 +3547 -223
- package/dist/community.cjs.map +1 -1
- package/dist/community.js +4 -1
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +3584 -175
- 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 +46 -14
- package/dist/chunk-PIUBQRFC.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.
|