@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 CHANGED
@@ -1,131 +1,39 @@
1
- # AI SDK - tool call parser middleware
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
- ▲ Also available in the Vercel AI SDK official documentation: [Custom tool call parser](https://ai-sdk.dev/docs/ai-sdk-core/middleware#custom-tool-call-parser)
4
-
5
- [![npm](https://img.shields.io/npm/v/@ai-sdk-tool/parser)](https://www.npmjs.com/package/@ai-sdk-tool/parser)
6
- [![npm](https://img.shields.io/npm/dt/@ai-sdk-tool/parser)](https://www.npmjs.com/package/@ai-sdk-tool/parser)
3
+ ----
4
+ [![npm - parser](https://img.shields.io/npm/v/@ai-sdk-tool/parser)](https://www.npmjs.com/package/@ai-sdk-tool/parser)
5
+ [![npm downloads - parser](https://img.shields.io/npm/dt/@ai-sdk-tool/parser)](https://www.npmjs.com/package/@ai-sdk-tool/parser)
7
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)
8
7
 
9
- > [!NOTE]
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
- - Parses tool calls from streaming or batch responses
20
- - Prebuilt protocols: JSON‑mix (Gemma/Hermes‑style) and Morph‑XML
21
- - Full control over the tool call system prompt
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
- ## Installation
14
+ ## Usage at a glance
24
15
 
25
- ```bash
26
- pnpm add @ai-sdk-tool/parser
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 openrouter = createOpenAICompatible({
37
- /* ... */
21
+ const client = createOpenAICompatible({
22
+ /* baseURL, apiKey */
38
23
  });
39
24
 
40
- async function main() {
41
- const result = streamText({
42
- model: wrapLanguageModel({
43
- model: openrouter("your-model-name"),
44
- middleware: xmlToolMiddleware,
45
- }),
46
- system: "You are a helpful assistant.",
47
- prompt: "What is the weather in my city?",
48
- stopWhen: stepCountIs(4),
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
- async function main() {
79
- const { text } = await generateText({
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.