@ai-sdk-tool/parser 4.0.1 → 4.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/README.md +109 -20
- package/dist/{chunk-DPGORNPB.js → chunk-722D5BGD.js} +3339 -826
- package/dist/chunk-722D5BGD.js.map +1 -0
- package/dist/{chunk-76E6H46R.js → chunk-D4YULTAO.js} +11 -8
- package/dist/chunk-D4YULTAO.js.map +1 -0
- package/dist/{chunk-DJB4DAZO.js → chunk-GMTE7BY5.js} +1 -1
- package/dist/{chunk-DJB4DAZO.js.map → chunk-GMTE7BY5.js.map} +1 -1
- package/dist/{chunk-IX4FJELL.js → chunk-QBZNMO5C.js} +1 -1
- package/dist/chunk-QBZNMO5C.js.map +1 -0
- package/dist/community.cjs +7868 -5243
- package/dist/community.cjs.map +1 -1
- package/dist/community.d.cts +7 -1
- package/dist/community.d.ts +7 -1
- package/dist/community.js +128 -8
- package/dist/community.js.map +1 -1
- package/dist/index.cjs +7199 -4683
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +40 -62
- package/dist/index.d.ts +40 -62
- package/dist/index.js +28 -14
- package/dist/rjson.cjs.map +1 -1
- package/dist/rjson.d.cts +15 -15
- package/dist/rjson.d.ts +15 -15
- package/dist/rjson.js +1 -1
- package/dist/rxml.cjs +6 -6
- package/dist/rxml.cjs.map +1 -1
- package/dist/rxml.d.cts +23 -23
- package/dist/rxml.d.ts +23 -23
- package/dist/rxml.js +2 -2
- package/dist/schema-coerce.cjs.map +1 -1
- package/dist/schema-coerce.js +1 -1
- package/package.json +7 -7
- package/dist/chunk-76E6H46R.js.map +0 -1
- package/dist/chunk-DPGORNPB.js.map +0 -1
- package/dist/chunk-IX4FJELL.js.map +0 -1
package/README.md
CHANGED
|
@@ -5,42 +5,131 @@
|
|
|
5
5
|
[](https://www.npmjs.com/package/@ai-sdk-tool/parser)
|
|
6
6
|
[](https://codecov.io/gh/minpeter/ai-sdk-tool-call-middleware)
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
AI SDK middleware for parsing tool calls from models that do not natively support `tools`.
|
|
9
9
|
|
|
10
|
-
|
|
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.
|
|
10
|
+
## Install
|
|
13
11
|
|
|
14
|
-
|
|
12
|
+
```bash
|
|
13
|
+
pnpm add @ai-sdk-tool/parser
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## AI SDK compatibility
|
|
17
|
+
|
|
18
|
+
Fact-checked from this repo `CHANGELOG.md` and npm release metadata (as of 2026-02-18).
|
|
19
|
+
|
|
20
|
+
| `@ai-sdk-tool/parser` major | AI SDK major | Maintenance status |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| `v1.x` | `v4.x` | Legacy (not actively maintained) |
|
|
23
|
+
| `v2.x` | `v5.x` | Legacy (not actively maintained) |
|
|
24
|
+
| `v3.x` | `v6.x` | Legacy (not actively maintained) |
|
|
25
|
+
| `v4.x` | `v6.x` | Active (current `latest` line) |
|
|
26
|
+
|
|
27
|
+
Note: there is no separate formal EOL announcement in releases/changelog for `v1`-`v3`; "legacy" here means non-current release lines.
|
|
28
|
+
|
|
29
|
+
## Package map
|
|
30
|
+
|
|
31
|
+
| Import | Purpose |
|
|
32
|
+
|---|---|
|
|
33
|
+
| `@ai-sdk-tool/parser` | Main middleware factory, preconfigured middleware, protocol exports |
|
|
34
|
+
| `@ai-sdk-tool/parser/community` | Community middleware (Sijawara, UI-TARS) |
|
|
35
|
+
|
|
36
|
+
## Quick start
|
|
15
37
|
|
|
16
38
|
```ts
|
|
17
|
-
import { wrapLanguageModel, streamText } from "ai";
|
|
18
39
|
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
19
|
-
import {
|
|
40
|
+
import { morphXmlToolMiddleware } from "@ai-sdk-tool/parser";
|
|
41
|
+
import { stepCountIs, streamText, wrapLanguageModel } from "ai";
|
|
42
|
+
import { z } from "zod";
|
|
20
43
|
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
|
|
44
|
+
const model = createOpenAICompatible({
|
|
45
|
+
name: "openrouter",
|
|
46
|
+
apiKey: process.env.OPENROUTER_API_KEY,
|
|
47
|
+
baseURL: "https://openrouter.ai/api/v1",
|
|
48
|
+
})("arcee-ai/trinity-large-preview:free");
|
|
24
49
|
|
|
25
50
|
const result = streamText({
|
|
26
51
|
model: wrapLanguageModel({
|
|
27
|
-
model
|
|
28
|
-
middleware:
|
|
52
|
+
model,
|
|
53
|
+
middleware: morphXmlToolMiddleware,
|
|
29
54
|
}),
|
|
55
|
+
stopWhen: stepCountIs(4),
|
|
56
|
+
prompt: "What is the weather in Seoul?",
|
|
30
57
|
tools: {
|
|
31
|
-
|
|
58
|
+
get_weather: {
|
|
59
|
+
description: "Get weather by city name",
|
|
60
|
+
inputSchema: z.object({ city: z.string() }),
|
|
61
|
+
execute: async ({ city }) => ({ city, condition: "sunny", celsius: 23 }),
|
|
62
|
+
},
|
|
32
63
|
},
|
|
33
|
-
prompt: "Find weather for Seoul today",
|
|
34
64
|
});
|
|
35
65
|
|
|
36
66
|
for await (const part of result.fullStream) {
|
|
37
|
-
//
|
|
67
|
+
// text-delta / tool-input-start / tool-input-delta / tool-input-end / tool-call / tool-result
|
|
38
68
|
}
|
|
39
69
|
```
|
|
40
70
|
|
|
41
|
-
##
|
|
71
|
+
## Choose middleware
|
|
72
|
+
|
|
73
|
+
Use the preconfigured middleware exports from `src/preconfigured-middleware.ts`:
|
|
74
|
+
|
|
75
|
+
| Middleware | Best for |
|
|
76
|
+
|---|---|
|
|
77
|
+
| `hermesToolMiddleware` | JSON-style tool payloads |
|
|
78
|
+
| `morphXmlToolMiddleware` | XML-style payloads with schema-aware coercion |
|
|
79
|
+
| `yamlXmlToolMiddleware` | XML tool tags + YAML bodies |
|
|
80
|
+
| `qwen3CoderToolMiddleware` | Qwen/UI-TARS style `<tool_call>` markup |
|
|
81
|
+
|
|
82
|
+
## Build custom middleware
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
import { createToolMiddleware, qwen3CoderProtocol } from "@ai-sdk-tool/parser";
|
|
86
|
+
|
|
87
|
+
export const myToolMiddleware = createToolMiddleware({
|
|
88
|
+
protocol: qwen3CoderProtocol,
|
|
89
|
+
toolSystemPromptTemplate: (tools) =>
|
|
90
|
+
`Use these tools and emit <tool_call> blocks only: ${JSON.stringify(tools)}`,
|
|
91
|
+
});
|
|
92
|
+
```
|
|
42
93
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
-
|
|
46
|
-
- `tool-input-start.id`, `tool-input-end.id`, and `tool-call.toolCallId` are reconciled to the same ID
|
|
94
|
+
## Streaming semantics
|
|
95
|
+
|
|
96
|
+
- Stream parsers emit `tool-input-start`, `tool-input-delta`, and `tool-input-end` when a tool input can be incrementally reconstructed.
|
|
97
|
+
- `tool-input-start.id`, `tool-input-end.id`, and final `tool-call.toolCallId` are reconciled to the same ID.
|
|
98
|
+
- `emitRawToolCallTextOnError` defaults to `false`; malformed tool-call markup is suppressed from `text-delta` unless explicitly enabled.
|
|
99
|
+
|
|
100
|
+
Configure parser error behavior through `providerOptions.toolCallMiddleware`:
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
const result = streamText({
|
|
104
|
+
// ...
|
|
105
|
+
providerOptions: {
|
|
106
|
+
toolCallMiddleware: {
|
|
107
|
+
onError: (message, metadata) => {
|
|
108
|
+
console.warn(message, metadata);
|
|
109
|
+
},
|
|
110
|
+
emitRawToolCallTextOnError: false,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Local development
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pnpm build
|
|
120
|
+
pnpm test
|
|
121
|
+
pnpm check:biome
|
|
122
|
+
pnpm check:types
|
|
123
|
+
pnpm check
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Examples in this repo
|
|
127
|
+
|
|
128
|
+
- Parser middleware examples: `examples/parser-core/README.md`
|
|
129
|
+
- RXML examples: `examples/rxml-core/README.md`
|
|
130
|
+
|
|
131
|
+
Run one example from repo root:
|
|
132
|
+
|
|
133
|
+
```bash
|
|
134
|
+
pnpm dlx tsx examples/parser-core/src/01-stream-tool-call.ts
|
|
135
|
+
```
|