@quilltap/plugin-utils 1.4.1 → 1.5.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/dist/converters-BPYo8xdH.d.mts +245 -0
- package/dist/converters-BPYo8xdH.d.ts +245 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +58 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +58 -10
- package/dist/index.mjs.map +1 -1
- package/dist/providers/index.js +54 -8
- package/dist/providers/index.js.map +1 -1
- package/dist/providers/index.mjs +54 -8
- package/dist/providers/index.mjs.map +1 -1
- package/dist/tools/index.d.mts +76 -213
- package/dist/tools/index.d.ts +76 -213
- package/dist/tools/index.js +342 -4
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/index.mjs +323 -3
- package/dist/tools/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/tools/index.d.mts
CHANGED
|
@@ -1,246 +1,109 @@
|
|
|
1
|
-
import { ToolCallRequest
|
|
1
|
+
import { ToolCallRequest } from '@quilltap/plugin-types';
|
|
2
2
|
export { AnthropicToolDefinition, GoogleToolDefinition, OpenAIToolDefinition, ToolCall, ToolCallRequest, ToolFormatOptions, ToolResult, UniversalTool } from '@quilltap/plugin-types';
|
|
3
|
+
export { T as ToolCallFormat, a as ToolConvertTarget, b as applyDescriptionLimit, c as convertFromAnthropicFormat, d as convertFromGoogleFormat, e as convertOpenAIToAnthropicFormat, f as convertOpenAIToGoogleFormat, g as convertToAnthropicFormat, h as convertToGoogleFormat, i as convertToolTo, j as convertToolsTo, k as detectToolCallFormat, l as hasToolCalls, p as parseAnthropicToolCalls, m as parseGoogleToolCalls, n as parseOpenAIToolCalls, o as parseToolCalls } from '../converters-BPYo8xdH.mjs';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* Tool Call Parsers
|
|
6
|
+
* Text-Based Tool Call Parsers
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Utility functions for parsing spontaneous XML-style tool calls from LLM
|
|
9
|
+
* text responses. These are for models that emit tool-call-like markup in
|
|
10
|
+
* their output instead of using native function calling APIs.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Supported tool call response formats
|
|
16
|
-
*/
|
|
17
|
-
type ToolCallFormat = 'openai' | 'anthropic' | 'google' | 'auto';
|
|
18
|
-
/**
|
|
19
|
-
* Parse OpenAI format tool calls from LLM response
|
|
20
|
-
*
|
|
21
|
-
* Extracts tool calls from OpenAI/Grok API responses which return
|
|
22
|
-
* tool_calls in the message object.
|
|
23
|
-
*
|
|
24
|
-
* Expected response structures:
|
|
25
|
-
* - `response.tool_calls` (direct)
|
|
26
|
-
* - `response.choices[0].message.tool_calls` (nested)
|
|
12
|
+
* Provider plugins import and compose these utilities to implement their
|
|
13
|
+
* `hasTextToolMarkers`, `parseTextToolCalls`, and `stripTextToolMarkers` methods.
|
|
27
14
|
*
|
|
28
|
-
* @
|
|
29
|
-
* @returns Array of parsed tool call requests
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* const response = await openai.chat.completions.create({...});
|
|
34
|
-
* const toolCalls = parseOpenAIToolCalls(response);
|
|
35
|
-
* // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]
|
|
36
|
-
* ```
|
|
15
|
+
* @module @quilltap/plugin-utils/tools/text-parsers
|
|
37
16
|
*/
|
|
38
|
-
declare function parseOpenAIToolCalls(response: unknown): ToolCallRequest[];
|
|
39
|
-
/**
|
|
40
|
-
* Parse Anthropic format tool calls from LLM response
|
|
41
|
-
*
|
|
42
|
-
* Extracts tool calls from Anthropic API responses which return
|
|
43
|
-
* tool_use blocks in the content array.
|
|
44
|
-
*
|
|
45
|
-
* Expected response structure:
|
|
46
|
-
* - `response.content` array with `{ type: 'tool_use', name, input }`
|
|
47
|
-
*
|
|
48
|
-
* @param response - The raw response from provider API
|
|
49
|
-
* @returns Array of parsed tool call requests
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```typescript
|
|
53
|
-
* const response = await anthropic.messages.create({...});
|
|
54
|
-
* const toolCalls = parseAnthropicToolCalls(response);
|
|
55
|
-
* // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
declare function parseAnthropicToolCalls(response: unknown): ToolCallRequest[];
|
|
59
|
-
/**
|
|
60
|
-
* Parse Google Gemini format tool calls from LLM response
|
|
61
|
-
*
|
|
62
|
-
* Extracts tool calls from Google Gemini API responses which return
|
|
63
|
-
* functionCall objects in the parts array.
|
|
64
|
-
*
|
|
65
|
-
* Expected response structure:
|
|
66
|
-
* - `response.candidates[0].content.parts` array with `{ functionCall: { name, args } }`
|
|
67
|
-
*
|
|
68
|
-
* @param response - The raw response from provider API
|
|
69
|
-
* @returns Array of parsed tool call requests
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* const response = await gemini.generateContent({...});
|
|
74
|
-
* const toolCalls = parseGoogleToolCalls(response);
|
|
75
|
-
* // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
declare function parseGoogleToolCalls(response: unknown): ToolCallRequest[];
|
|
79
|
-
/**
|
|
80
|
-
* Detect the format of a tool call response
|
|
81
|
-
*
|
|
82
|
-
* Analyzes the response structure to determine which provider format it uses.
|
|
83
|
-
*
|
|
84
|
-
* @param response - The raw response from a provider API
|
|
85
|
-
* @returns The detected format, or null if unrecognized
|
|
86
|
-
*/
|
|
87
|
-
declare function detectToolCallFormat(response: unknown): ToolCallFormat | null;
|
|
88
|
-
/**
|
|
89
|
-
* Parse tool calls with auto-detection or explicit format
|
|
90
|
-
*
|
|
91
|
-
* A unified parser that can either auto-detect the response format
|
|
92
|
-
* or use a specified format. This is useful when you're not sure
|
|
93
|
-
* which provider's response you're handling.
|
|
94
|
-
*
|
|
95
|
-
* @param response - The raw response from a provider API
|
|
96
|
-
* @param format - The format to use: 'openai', 'anthropic', 'google', or 'auto'
|
|
97
|
-
* @returns Array of parsed tool call requests
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* // Auto-detect format
|
|
102
|
-
* const toolCalls = parseToolCalls(response, 'auto');
|
|
103
|
-
*
|
|
104
|
-
* // Or specify format explicitly
|
|
105
|
-
* const toolCalls = parseToolCalls(response, 'openai');
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
declare function parseToolCalls(response: unknown, format?: ToolCallFormat): ToolCallRequest[];
|
|
109
|
-
/**
|
|
110
|
-
* Check if a response contains tool calls
|
|
111
|
-
*
|
|
112
|
-
* Quick check to determine if a response has any tool calls
|
|
113
|
-
* without fully parsing them.
|
|
114
|
-
*
|
|
115
|
-
* @param response - The raw response from a provider API
|
|
116
|
-
* @returns True if the response contains tool calls
|
|
117
|
-
*/
|
|
118
|
-
declare function hasToolCalls(response: unknown): boolean;
|
|
119
17
|
|
|
120
18
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* Utilities for converting between different provider tool formats.
|
|
124
|
-
* The universal format (OpenAI-style) serves as the baseline for all conversions.
|
|
125
|
-
*
|
|
126
|
-
* @module @quilltap/plugin-utils/tools/converters
|
|
19
|
+
* Parsed text-based tool call from LLM output
|
|
127
20
|
*/
|
|
128
|
-
|
|
21
|
+
interface ParsedTextTool {
|
|
22
|
+
/** The tool name */
|
|
23
|
+
toolName: string;
|
|
24
|
+
/** Arguments extracted from the markup */
|
|
25
|
+
arguments: Record<string, unknown>;
|
|
26
|
+
/** The full matched markup text (for stripping) */
|
|
27
|
+
fullMatch: string;
|
|
28
|
+
/** Start index in the original text */
|
|
29
|
+
startIndex: number;
|
|
30
|
+
/** End index in the original text */
|
|
31
|
+
endIndex: number;
|
|
32
|
+
/** Which format was detected */
|
|
33
|
+
format: 'deepseek' | 'claude' | 'generic' | 'function_call' | 'tool_use';
|
|
34
|
+
}
|
|
129
35
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* Anthropic uses a tool_use format with:
|
|
133
|
-
* - name: string
|
|
134
|
-
* - description: string
|
|
135
|
-
* - input_schema: JSON schema object
|
|
136
|
-
*
|
|
137
|
-
* @param tool - Universal tool or OpenAI tool definition
|
|
138
|
-
* @returns Tool formatted for Anthropic's tool_use
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```typescript
|
|
142
|
-
* const anthropicTool = convertToAnthropicFormat(universalTool);
|
|
143
|
-
* // Returns: {
|
|
144
|
-
* // name: 'search_web',
|
|
145
|
-
* // description: 'Search the web',
|
|
146
|
-
* // input_schema: {
|
|
147
|
-
* // type: 'object',
|
|
148
|
-
* // properties: { query: { type: 'string' } },
|
|
149
|
-
* // required: ['query']
|
|
150
|
-
* // }
|
|
151
|
-
* // }
|
|
152
|
-
* ```
|
|
36
|
+
* Normalize a tool name from hallucinated markup to the canonical name.
|
|
37
|
+
* Returns the original name if no alias is found (passes through unknown tools).
|
|
153
38
|
*/
|
|
154
|
-
declare function
|
|
39
|
+
declare function normalizeToolName(name: string): string;
|
|
155
40
|
/**
|
|
156
|
-
* Convert
|
|
41
|
+
* Convert a ParsedTextTool to the standard ToolCallRequest format.
|
|
157
42
|
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* - description: string
|
|
161
|
-
* - parameters: JSON schema object
|
|
162
|
-
*
|
|
163
|
-
* @param tool - Universal tool or OpenAI tool definition
|
|
164
|
-
* @returns Tool formatted for Google's functionCall
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```typescript
|
|
168
|
-
* const googleTool = convertToGoogleFormat(universalTool);
|
|
169
|
-
* // Returns: {
|
|
170
|
-
* // name: 'search_web',
|
|
171
|
-
* // description: 'Search the web',
|
|
172
|
-
* // parameters: {
|
|
173
|
-
* // type: 'object',
|
|
174
|
-
* // properties: { query: { type: 'string' } },
|
|
175
|
-
* // required: ['query']
|
|
176
|
-
* // }
|
|
177
|
-
* // }
|
|
178
|
-
* ```
|
|
43
|
+
* For well-known tools, normalizes argument names (e.g., "search" → "query").
|
|
44
|
+
* For unknown tools, passes arguments through as-is.
|
|
179
45
|
*/
|
|
180
|
-
declare function
|
|
46
|
+
declare function convertToToolCallRequest(parsed: ParsedTextTool): ToolCallRequest;
|
|
181
47
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* @param tool - Anthropic format tool
|
|
185
|
-
* @returns Tool in universal OpenAI format
|
|
48
|
+
* Parse `<function_calls><invoke name="...">` format (DeepSeek and Claude-style)
|
|
186
49
|
*/
|
|
187
|
-
declare function
|
|
50
|
+
declare function parseFunctionCallsFormat(response: string): ParsedTextTool[];
|
|
188
51
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @param tool - Google format tool
|
|
192
|
-
* @returns Tool in universal OpenAI format
|
|
52
|
+
* Parse `<tool_call>` format (generic XML)
|
|
193
53
|
*/
|
|
194
|
-
declare function
|
|
54
|
+
declare function parseToolCallFormat(response: string): ParsedTextTool[];
|
|
195
55
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* Modifies a tool's description if it exceeds maxBytes, appending a warning
|
|
199
|
-
* that the description was truncated. This is useful for providers with
|
|
200
|
-
* strict token limits.
|
|
201
|
-
*
|
|
202
|
-
* @param tool - Tool object (any format) with a description property
|
|
203
|
-
* @param maxBytes - Maximum bytes allowed for description (including warning)
|
|
204
|
-
* @returns Modified tool with truncated description if needed
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const limitedTool = applyDescriptionLimit(tool, 500);
|
|
209
|
-
* // If description > 500 bytes, truncates and adds warning
|
|
210
|
-
* ```
|
|
56
|
+
* Parse `<function_call name="...">` format
|
|
211
57
|
*/
|
|
212
|
-
declare function
|
|
213
|
-
description: string;
|
|
214
|
-
}>(tool: T, maxBytes: number): T;
|
|
58
|
+
declare function parseFunctionCallFormat(response: string): ParsedTextTool[];
|
|
215
59
|
/**
|
|
216
|
-
*
|
|
60
|
+
* Parse `<tool_use>` format (Gemini and others)
|
|
61
|
+
*
|
|
62
|
+
* Handles multiple sub-formats:
|
|
63
|
+
* - Bare JSON: `<tool_use>{"name":"fn","input":{...}}</tool_use>`
|
|
64
|
+
* - XML children: `<tool_use><name>fn</name><arguments>...</arguments></tool_use>`
|
|
65
|
+
* - JSON in arguments: `<tool_use><name>fn</name><arguments>{"q":"v"}</arguments></tool_use>`
|
|
66
|
+
* - Attributed: `<tool_use name="fn"><arguments>...</arguments></tool_use>`
|
|
217
67
|
*/
|
|
218
|
-
|
|
68
|
+
declare function parseToolUseFormat(response: string): ParsedTextTool[];
|
|
219
69
|
/**
|
|
220
|
-
*
|
|
70
|
+
* Parse all known XML tool call formats from response text.
|
|
71
|
+
* Deduplicates by position and sorts by start index.
|
|
221
72
|
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @returns Tool in the target format
|
|
73
|
+
* Plugins can call this directly or compose individual format parsers
|
|
74
|
+
* for provider-specific behavior.
|
|
225
75
|
*/
|
|
226
|
-
declare function
|
|
76
|
+
declare function parseAllXMLFormats(response: string): ParsedTextTool[];
|
|
227
77
|
/**
|
|
228
|
-
* Convert
|
|
229
|
-
*
|
|
230
|
-
* @param tools - Array of universal tools or OpenAI tool definitions
|
|
231
|
-
* @param target - Target provider format
|
|
232
|
-
* @returns Array of tools in the target format
|
|
78
|
+
* Convert parsed text tools to standard ToolCallRequest array.
|
|
79
|
+
* Convenience wrapper for plugins implementing parseTextToolCalls.
|
|
233
80
|
*/
|
|
234
|
-
declare function
|
|
81
|
+
declare function parseAllXMLAsToolCalls(response: string): ToolCallRequest[];
|
|
235
82
|
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
83
|
+
* Check if text contains any of the known XML tool call patterns.
|
|
84
|
+
* Quick regex check before full parsing.
|
|
238
85
|
*/
|
|
239
|
-
declare
|
|
86
|
+
declare function hasAnyXMLToolMarkers(response: string): boolean;
|
|
87
|
+
/** Check for `<function_calls>` markers (DeepSeek/Claude-style) */
|
|
88
|
+
declare function hasFunctionCallsMarkers(response: string): boolean;
|
|
89
|
+
/** Check for `<tool_call>` markers (generic) */
|
|
90
|
+
declare function hasToolCallMarkers(response: string): boolean;
|
|
91
|
+
/** Check for `<function_call>` markers */
|
|
92
|
+
declare function hasFunctionCallMarkers(response: string): boolean;
|
|
93
|
+
/** Check for `<tool_use>` markers (Gemini-style) */
|
|
94
|
+
declare function hasToolUseMarkers(response: string): boolean;
|
|
240
95
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
96
|
+
* Strip all known XML tool call markers from text.
|
|
97
|
+
* Cleans up whitespace left behind.
|
|
243
98
|
*/
|
|
244
|
-
declare
|
|
99
|
+
declare function stripAllXMLToolMarkers(response: string): string;
|
|
100
|
+
/** Strip `<function_calls>` blocks */
|
|
101
|
+
declare function stripFunctionCallsMarkers(response: string): string;
|
|
102
|
+
/** Strip `<tool_call>` blocks */
|
|
103
|
+
declare function stripToolCallMarkers(response: string): string;
|
|
104
|
+
/** Strip `<function_call>` blocks */
|
|
105
|
+
declare function stripFunctionCallMarkers(response: string): string;
|
|
106
|
+
/** Strip `<tool_use>` blocks */
|
|
107
|
+
declare function stripToolUseMarkers(response: string): string;
|
|
245
108
|
|
|
246
|
-
export { type
|
|
109
|
+
export { type ParsedTextTool, convertToToolCallRequest as convertTextToolToRequest, hasAnyXMLToolMarkers, hasFunctionCallMarkers, hasFunctionCallsMarkers, hasToolCallMarkers, hasToolUseMarkers, normalizeToolName, parseAllXMLAsToolCalls, parseAllXMLFormats, parseFunctionCallFormat, parseFunctionCallsFormat, parseToolCallFormat, parseToolUseFormat, stripAllXMLToolMarkers, stripFunctionCallMarkers, stripFunctionCallsMarkers, stripToolCallMarkers, stripToolUseMarkers };
|
package/dist/tools/index.d.ts
CHANGED
|
@@ -1,246 +1,109 @@
|
|
|
1
|
-
import { ToolCallRequest
|
|
1
|
+
import { ToolCallRequest } from '@quilltap/plugin-types';
|
|
2
2
|
export { AnthropicToolDefinition, GoogleToolDefinition, OpenAIToolDefinition, ToolCall, ToolCallRequest, ToolFormatOptions, ToolResult, UniversalTool } from '@quilltap/plugin-types';
|
|
3
|
+
export { T as ToolCallFormat, a as ToolConvertTarget, b as applyDescriptionLimit, c as convertFromAnthropicFormat, d as convertFromGoogleFormat, e as convertOpenAIToAnthropicFormat, f as convertOpenAIToGoogleFormat, g as convertToAnthropicFormat, h as convertToGoogleFormat, i as convertToolTo, j as convertToolsTo, k as detectToolCallFormat, l as hasToolCalls, p as parseAnthropicToolCalls, m as parseGoogleToolCalls, n as parseOpenAIToolCalls, o as parseToolCalls } from '../converters-BPYo8xdH.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
|
-
* Tool Call Parsers
|
|
6
|
+
* Text-Based Tool Call Parsers
|
|
6
7
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* Utility functions for parsing spontaneous XML-style tool calls from LLM
|
|
9
|
+
* text responses. These are for models that emit tool-call-like markup in
|
|
10
|
+
* their output instead of using native function calling APIs.
|
|
10
11
|
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Supported tool call response formats
|
|
16
|
-
*/
|
|
17
|
-
type ToolCallFormat = 'openai' | 'anthropic' | 'google' | 'auto';
|
|
18
|
-
/**
|
|
19
|
-
* Parse OpenAI format tool calls from LLM response
|
|
20
|
-
*
|
|
21
|
-
* Extracts tool calls from OpenAI/Grok API responses which return
|
|
22
|
-
* tool_calls in the message object.
|
|
23
|
-
*
|
|
24
|
-
* Expected response structures:
|
|
25
|
-
* - `response.tool_calls` (direct)
|
|
26
|
-
* - `response.choices[0].message.tool_calls` (nested)
|
|
12
|
+
* Provider plugins import and compose these utilities to implement their
|
|
13
|
+
* `hasTextToolMarkers`, `parseTextToolCalls`, and `stripTextToolMarkers` methods.
|
|
27
14
|
*
|
|
28
|
-
* @
|
|
29
|
-
* @returns Array of parsed tool call requests
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```typescript
|
|
33
|
-
* const response = await openai.chat.completions.create({...});
|
|
34
|
-
* const toolCalls = parseOpenAIToolCalls(response);
|
|
35
|
-
* // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]
|
|
36
|
-
* ```
|
|
15
|
+
* @module @quilltap/plugin-utils/tools/text-parsers
|
|
37
16
|
*/
|
|
38
|
-
declare function parseOpenAIToolCalls(response: unknown): ToolCallRequest[];
|
|
39
|
-
/**
|
|
40
|
-
* Parse Anthropic format tool calls from LLM response
|
|
41
|
-
*
|
|
42
|
-
* Extracts tool calls from Anthropic API responses which return
|
|
43
|
-
* tool_use blocks in the content array.
|
|
44
|
-
*
|
|
45
|
-
* Expected response structure:
|
|
46
|
-
* - `response.content` array with `{ type: 'tool_use', name, input }`
|
|
47
|
-
*
|
|
48
|
-
* @param response - The raw response from provider API
|
|
49
|
-
* @returns Array of parsed tool call requests
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```typescript
|
|
53
|
-
* const response = await anthropic.messages.create({...});
|
|
54
|
-
* const toolCalls = parseAnthropicToolCalls(response);
|
|
55
|
-
* // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
declare function parseAnthropicToolCalls(response: unknown): ToolCallRequest[];
|
|
59
|
-
/**
|
|
60
|
-
* Parse Google Gemini format tool calls from LLM response
|
|
61
|
-
*
|
|
62
|
-
* Extracts tool calls from Google Gemini API responses which return
|
|
63
|
-
* functionCall objects in the parts array.
|
|
64
|
-
*
|
|
65
|
-
* Expected response structure:
|
|
66
|
-
* - `response.candidates[0].content.parts` array with `{ functionCall: { name, args } }`
|
|
67
|
-
*
|
|
68
|
-
* @param response - The raw response from provider API
|
|
69
|
-
* @returns Array of parsed tool call requests
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```typescript
|
|
73
|
-
* const response = await gemini.generateContent({...});
|
|
74
|
-
* const toolCalls = parseGoogleToolCalls(response);
|
|
75
|
-
* // Returns: [{ name: 'search_web', arguments: { query: 'hello' } }]
|
|
76
|
-
* ```
|
|
77
|
-
*/
|
|
78
|
-
declare function parseGoogleToolCalls(response: unknown): ToolCallRequest[];
|
|
79
|
-
/**
|
|
80
|
-
* Detect the format of a tool call response
|
|
81
|
-
*
|
|
82
|
-
* Analyzes the response structure to determine which provider format it uses.
|
|
83
|
-
*
|
|
84
|
-
* @param response - The raw response from a provider API
|
|
85
|
-
* @returns The detected format, or null if unrecognized
|
|
86
|
-
*/
|
|
87
|
-
declare function detectToolCallFormat(response: unknown): ToolCallFormat | null;
|
|
88
|
-
/**
|
|
89
|
-
* Parse tool calls with auto-detection or explicit format
|
|
90
|
-
*
|
|
91
|
-
* A unified parser that can either auto-detect the response format
|
|
92
|
-
* or use a specified format. This is useful when you're not sure
|
|
93
|
-
* which provider's response you're handling.
|
|
94
|
-
*
|
|
95
|
-
* @param response - The raw response from a provider API
|
|
96
|
-
* @param format - The format to use: 'openai', 'anthropic', 'google', or 'auto'
|
|
97
|
-
* @returns Array of parsed tool call requests
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```typescript
|
|
101
|
-
* // Auto-detect format
|
|
102
|
-
* const toolCalls = parseToolCalls(response, 'auto');
|
|
103
|
-
*
|
|
104
|
-
* // Or specify format explicitly
|
|
105
|
-
* const toolCalls = parseToolCalls(response, 'openai');
|
|
106
|
-
* ```
|
|
107
|
-
*/
|
|
108
|
-
declare function parseToolCalls(response: unknown, format?: ToolCallFormat): ToolCallRequest[];
|
|
109
|
-
/**
|
|
110
|
-
* Check if a response contains tool calls
|
|
111
|
-
*
|
|
112
|
-
* Quick check to determine if a response has any tool calls
|
|
113
|
-
* without fully parsing them.
|
|
114
|
-
*
|
|
115
|
-
* @param response - The raw response from a provider API
|
|
116
|
-
* @returns True if the response contains tool calls
|
|
117
|
-
*/
|
|
118
|
-
declare function hasToolCalls(response: unknown): boolean;
|
|
119
17
|
|
|
120
18
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* Utilities for converting between different provider tool formats.
|
|
124
|
-
* The universal format (OpenAI-style) serves as the baseline for all conversions.
|
|
125
|
-
*
|
|
126
|
-
* @module @quilltap/plugin-utils/tools/converters
|
|
19
|
+
* Parsed text-based tool call from LLM output
|
|
127
20
|
*/
|
|
128
|
-
|
|
21
|
+
interface ParsedTextTool {
|
|
22
|
+
/** The tool name */
|
|
23
|
+
toolName: string;
|
|
24
|
+
/** Arguments extracted from the markup */
|
|
25
|
+
arguments: Record<string, unknown>;
|
|
26
|
+
/** The full matched markup text (for stripping) */
|
|
27
|
+
fullMatch: string;
|
|
28
|
+
/** Start index in the original text */
|
|
29
|
+
startIndex: number;
|
|
30
|
+
/** End index in the original text */
|
|
31
|
+
endIndex: number;
|
|
32
|
+
/** Which format was detected */
|
|
33
|
+
format: 'deepseek' | 'claude' | 'generic' | 'function_call' | 'tool_use';
|
|
34
|
+
}
|
|
129
35
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* Anthropic uses a tool_use format with:
|
|
133
|
-
* - name: string
|
|
134
|
-
* - description: string
|
|
135
|
-
* - input_schema: JSON schema object
|
|
136
|
-
*
|
|
137
|
-
* @param tool - Universal tool or OpenAI tool definition
|
|
138
|
-
* @returns Tool formatted for Anthropic's tool_use
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```typescript
|
|
142
|
-
* const anthropicTool = convertToAnthropicFormat(universalTool);
|
|
143
|
-
* // Returns: {
|
|
144
|
-
* // name: 'search_web',
|
|
145
|
-
* // description: 'Search the web',
|
|
146
|
-
* // input_schema: {
|
|
147
|
-
* // type: 'object',
|
|
148
|
-
* // properties: { query: { type: 'string' } },
|
|
149
|
-
* // required: ['query']
|
|
150
|
-
* // }
|
|
151
|
-
* // }
|
|
152
|
-
* ```
|
|
36
|
+
* Normalize a tool name from hallucinated markup to the canonical name.
|
|
37
|
+
* Returns the original name if no alias is found (passes through unknown tools).
|
|
153
38
|
*/
|
|
154
|
-
declare function
|
|
39
|
+
declare function normalizeToolName(name: string): string;
|
|
155
40
|
/**
|
|
156
|
-
* Convert
|
|
41
|
+
* Convert a ParsedTextTool to the standard ToolCallRequest format.
|
|
157
42
|
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
160
|
-
* - description: string
|
|
161
|
-
* - parameters: JSON schema object
|
|
162
|
-
*
|
|
163
|
-
* @param tool - Universal tool or OpenAI tool definition
|
|
164
|
-
* @returns Tool formatted for Google's functionCall
|
|
165
|
-
*
|
|
166
|
-
* @example
|
|
167
|
-
* ```typescript
|
|
168
|
-
* const googleTool = convertToGoogleFormat(universalTool);
|
|
169
|
-
* // Returns: {
|
|
170
|
-
* // name: 'search_web',
|
|
171
|
-
* // description: 'Search the web',
|
|
172
|
-
* // parameters: {
|
|
173
|
-
* // type: 'object',
|
|
174
|
-
* // properties: { query: { type: 'string' } },
|
|
175
|
-
* // required: ['query']
|
|
176
|
-
* // }
|
|
177
|
-
* // }
|
|
178
|
-
* ```
|
|
43
|
+
* For well-known tools, normalizes argument names (e.g., "search" → "query").
|
|
44
|
+
* For unknown tools, passes arguments through as-is.
|
|
179
45
|
*/
|
|
180
|
-
declare function
|
|
46
|
+
declare function convertToToolCallRequest(parsed: ParsedTextTool): ToolCallRequest;
|
|
181
47
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* @param tool - Anthropic format tool
|
|
185
|
-
* @returns Tool in universal OpenAI format
|
|
48
|
+
* Parse `<function_calls><invoke name="...">` format (DeepSeek and Claude-style)
|
|
186
49
|
*/
|
|
187
|
-
declare function
|
|
50
|
+
declare function parseFunctionCallsFormat(response: string): ParsedTextTool[];
|
|
188
51
|
/**
|
|
189
|
-
*
|
|
190
|
-
*
|
|
191
|
-
* @param tool - Google format tool
|
|
192
|
-
* @returns Tool in universal OpenAI format
|
|
52
|
+
* Parse `<tool_call>` format (generic XML)
|
|
193
53
|
*/
|
|
194
|
-
declare function
|
|
54
|
+
declare function parseToolCallFormat(response: string): ParsedTextTool[];
|
|
195
55
|
/**
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* Modifies a tool's description if it exceeds maxBytes, appending a warning
|
|
199
|
-
* that the description was truncated. This is useful for providers with
|
|
200
|
-
* strict token limits.
|
|
201
|
-
*
|
|
202
|
-
* @param tool - Tool object (any format) with a description property
|
|
203
|
-
* @param maxBytes - Maximum bytes allowed for description (including warning)
|
|
204
|
-
* @returns Modified tool with truncated description if needed
|
|
205
|
-
*
|
|
206
|
-
* @example
|
|
207
|
-
* ```typescript
|
|
208
|
-
* const limitedTool = applyDescriptionLimit(tool, 500);
|
|
209
|
-
* // If description > 500 bytes, truncates and adds warning
|
|
210
|
-
* ```
|
|
56
|
+
* Parse `<function_call name="...">` format
|
|
211
57
|
*/
|
|
212
|
-
declare function
|
|
213
|
-
description: string;
|
|
214
|
-
}>(tool: T, maxBytes: number): T;
|
|
58
|
+
declare function parseFunctionCallFormat(response: string): ParsedTextTool[];
|
|
215
59
|
/**
|
|
216
|
-
*
|
|
60
|
+
* Parse `<tool_use>` format (Gemini and others)
|
|
61
|
+
*
|
|
62
|
+
* Handles multiple sub-formats:
|
|
63
|
+
* - Bare JSON: `<tool_use>{"name":"fn","input":{...}}</tool_use>`
|
|
64
|
+
* - XML children: `<tool_use><name>fn</name><arguments>...</arguments></tool_use>`
|
|
65
|
+
* - JSON in arguments: `<tool_use><name>fn</name><arguments>{"q":"v"}</arguments></tool_use>`
|
|
66
|
+
* - Attributed: `<tool_use name="fn"><arguments>...</arguments></tool_use>`
|
|
217
67
|
*/
|
|
218
|
-
|
|
68
|
+
declare function parseToolUseFormat(response: string): ParsedTextTool[];
|
|
219
69
|
/**
|
|
220
|
-
*
|
|
70
|
+
* Parse all known XML tool call formats from response text.
|
|
71
|
+
* Deduplicates by position and sorts by start index.
|
|
221
72
|
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* @returns Tool in the target format
|
|
73
|
+
* Plugins can call this directly or compose individual format parsers
|
|
74
|
+
* for provider-specific behavior.
|
|
225
75
|
*/
|
|
226
|
-
declare function
|
|
76
|
+
declare function parseAllXMLFormats(response: string): ParsedTextTool[];
|
|
227
77
|
/**
|
|
228
|
-
* Convert
|
|
229
|
-
*
|
|
230
|
-
* @param tools - Array of universal tools or OpenAI tool definitions
|
|
231
|
-
* @param target - Target provider format
|
|
232
|
-
* @returns Array of tools in the target format
|
|
78
|
+
* Convert parsed text tools to standard ToolCallRequest array.
|
|
79
|
+
* Convenience wrapper for plugins implementing parseTextToolCalls.
|
|
233
80
|
*/
|
|
234
|
-
declare function
|
|
81
|
+
declare function parseAllXMLAsToolCalls(response: string): ToolCallRequest[];
|
|
235
82
|
/**
|
|
236
|
-
*
|
|
237
|
-
*
|
|
83
|
+
* Check if text contains any of the known XML tool call patterns.
|
|
84
|
+
* Quick regex check before full parsing.
|
|
238
85
|
*/
|
|
239
|
-
declare
|
|
86
|
+
declare function hasAnyXMLToolMarkers(response: string): boolean;
|
|
87
|
+
/** Check for `<function_calls>` markers (DeepSeek/Claude-style) */
|
|
88
|
+
declare function hasFunctionCallsMarkers(response: string): boolean;
|
|
89
|
+
/** Check for `<tool_call>` markers (generic) */
|
|
90
|
+
declare function hasToolCallMarkers(response: string): boolean;
|
|
91
|
+
/** Check for `<function_call>` markers */
|
|
92
|
+
declare function hasFunctionCallMarkers(response: string): boolean;
|
|
93
|
+
/** Check for `<tool_use>` markers (Gemini-style) */
|
|
94
|
+
declare function hasToolUseMarkers(response: string): boolean;
|
|
240
95
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
96
|
+
* Strip all known XML tool call markers from text.
|
|
97
|
+
* Cleans up whitespace left behind.
|
|
243
98
|
*/
|
|
244
|
-
declare
|
|
99
|
+
declare function stripAllXMLToolMarkers(response: string): string;
|
|
100
|
+
/** Strip `<function_calls>` blocks */
|
|
101
|
+
declare function stripFunctionCallsMarkers(response: string): string;
|
|
102
|
+
/** Strip `<tool_call>` blocks */
|
|
103
|
+
declare function stripToolCallMarkers(response: string): string;
|
|
104
|
+
/** Strip `<function_call>` blocks */
|
|
105
|
+
declare function stripFunctionCallMarkers(response: string): string;
|
|
106
|
+
/** Strip `<tool_use>` blocks */
|
|
107
|
+
declare function stripToolUseMarkers(response: string): string;
|
|
245
108
|
|
|
246
|
-
export { type
|
|
109
|
+
export { type ParsedTextTool, convertToToolCallRequest as convertTextToolToRequest, hasAnyXMLToolMarkers, hasFunctionCallMarkers, hasFunctionCallsMarkers, hasToolCallMarkers, hasToolUseMarkers, normalizeToolName, parseAllXMLAsToolCalls, parseAllXMLFormats, parseFunctionCallFormat, parseFunctionCallsFormat, parseToolCallFormat, parseToolUseFormat, stripAllXMLToolMarkers, stripFunctionCallMarkers, stripFunctionCallsMarkers, stripToolCallMarkers, stripToolUseMarkers };
|