@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 CHANGED
@@ -5,42 +5,131 @@
5
5
  [![npm downloads - parser](https://img.shields.io/npm/dt/@ai-sdk-tool/parser)](https://www.npmjs.com/package/@ai-sdk-tool/parser)
6
6
  [![codecov](https://codecov.io/gh/minpeter/ai-sdk-tool-call-middleware/branch/main/graph/badge.svg)](https://codecov.io/gh/minpeter/ai-sdk-tool-call-middleware)
7
7
 
8
- Tooling for Vercel AI SDK: enable tool calling with models lacking native `tools`.
8
+ AI SDK middleware for parsing tool calls from models that do not natively support `tools`.
9
9
 
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.
10
+ ## Install
13
11
 
14
- ## Usage at a glance
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 { xmlToolMiddleware } from "@ai-sdk-tool/parser";
40
+ import { morphXmlToolMiddleware } from "@ai-sdk-tool/parser";
41
+ import { stepCountIs, streamText, wrapLanguageModel } from "ai";
42
+ import { z } from "zod";
20
43
 
21
- const client = createOpenAICompatible({
22
- /* baseURL, apiKey */
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: client("your-model-name"),
28
- middleware: xmlToolMiddleware,
52
+ model,
53
+ middleware: morphXmlToolMiddleware,
29
54
  }),
55
+ stopWhen: stepCountIs(4),
56
+ prompt: "What is the weather in Seoul?",
30
57
  tools: {
31
- /* your tools */
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
- // handle text and tool events
67
+ // text-delta / tool-input-start / tool-input-delta / tool-input-end / tool-call / tool-result
38
68
  }
39
69
  ```
40
70
 
41
- ## Tool-input delta semantics
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
- - `jsonProtocol`: `tool-input-delta` emits incremental JSON argument text.
44
- - `xmlProtocol` and `yamlProtocol`: `tool-input-delta` now also emits incremental JSON argument text (parsed-object prefixes), not raw XML/YAML fragments.
45
- - `jsonProtocol`, `xmlProtocol`, and `yamlProtocol`: malformed streaming tool payloads do not emit raw protocol markup to `text-delta` by default. Set `emitRawToolCallTextOnError: true` in parser options only if you explicitly want raw fallback text.
46
- - `tool-input-start.id`, `tool-input-end.id`, and `tool-call.toolCallId` are reconciled to the same ID for each tool call stream.
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
+ ```