@animalabs/membrane 0.1.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/dist/context/index.d.ts +10 -0
- package/dist/context/index.d.ts.map +1 -0
- package/dist/context/index.js +9 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/process.d.ts +22 -0
- package/dist/context/process.d.ts.map +1 -0
- package/dist/context/process.js +369 -0
- package/dist/context/process.js.map +1 -0
- package/dist/context/types.d.ts +118 -0
- package/dist/context/types.d.ts.map +1 -0
- package/dist/context/types.js +60 -0
- package/dist/context/types.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/membrane.d.ts +96 -0
- package/dist/membrane.d.ts.map +1 -0
- package/dist/membrane.js +893 -0
- package/dist/membrane.js.map +1 -0
- package/dist/providers/anthropic.d.ts +36 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +265 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +8 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +74 -0
- package/dist/providers/openai-compatible.d.ts.map +1 -0
- package/dist/providers/openai-compatible.js +412 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai.d.ts +69 -0
- package/dist/providers/openai.d.ts.map +1 -0
- package/dist/providers/openai.js +455 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openrouter.d.ts +76 -0
- package/dist/providers/openrouter.d.ts.map +1 -0
- package/dist/providers/openrouter.js +492 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/transforms/chat.d.ts +52 -0
- package/dist/transforms/chat.d.ts.map +1 -0
- package/dist/transforms/chat.js +136 -0
- package/dist/transforms/chat.js.map +1 -0
- package/dist/transforms/index.d.ts +6 -0
- package/dist/transforms/index.d.ts.map +1 -0
- package/dist/transforms/index.js +6 -0
- package/dist/transforms/index.js.map +1 -0
- package/dist/transforms/prefill.d.ts +89 -0
- package/dist/transforms/prefill.d.ts.map +1 -0
- package/dist/transforms/prefill.js +401 -0
- package/dist/transforms/prefill.js.map +1 -0
- package/dist/types/config.d.ts +103 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/config.js +21 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/content.d.ts +81 -0
- package/dist/types/content.d.ts.map +1 -0
- package/dist/types/content.js +40 -0
- package/dist/types/content.js.map +1 -0
- package/dist/types/errors.d.ts +42 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +208 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +9 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/message.d.ts +46 -0
- package/dist/types/message.d.ts.map +1 -0
- package/dist/types/message.js +38 -0
- package/dist/types/message.js.map +1 -0
- package/dist/types/provider.d.ts +155 -0
- package/dist/types/provider.d.ts.map +1 -0
- package/dist/types/provider.js +5 -0
- package/dist/types/provider.js.map +1 -0
- package/dist/types/request.d.ts +78 -0
- package/dist/types/request.d.ts.map +1 -0
- package/dist/types/request.js +5 -0
- package/dist/types/request.js.map +1 -0
- package/dist/types/response.d.ts +131 -0
- package/dist/types/response.d.ts.map +1 -0
- package/dist/types/response.js +7 -0
- package/dist/types/response.js.map +1 -0
- package/dist/types/streaming.d.ts +164 -0
- package/dist/types/streaming.d.ts.map +1 -0
- package/dist/types/streaming.js +5 -0
- package/dist/types/streaming.js.map +1 -0
- package/dist/types/tools.d.ts +71 -0
- package/dist/types/tools.d.ts.map +1 -0
- package/dist/types/tools.js +5 -0
- package/dist/types/tools.js.map +1 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/stream-parser.d.ts +53 -0
- package/dist/utils/stream-parser.d.ts.map +1 -0
- package/dist/utils/stream-parser.js +359 -0
- package/dist/utils/stream-parser.js.map +1 -0
- package/dist/utils/tool-parser.d.ts +130 -0
- package/dist/utils/tool-parser.d.ts.map +1 -0
- package/dist/utils/tool-parser.js +571 -0
- package/dist/utils/tool-parser.js.map +1 -0
- package/package.json +37 -0
- package/src/context/index.ts +24 -0
- package/src/context/process.ts +520 -0
- package/src/context/types.ts +231 -0
- package/src/index.ts +23 -0
- package/src/membrane.ts +1174 -0
- package/src/providers/anthropic.ts +340 -0
- package/src/providers/index.ts +31 -0
- package/src/providers/openai-compatible.ts +570 -0
- package/src/providers/openai.ts +625 -0
- package/src/providers/openrouter.ts +662 -0
- package/src/transforms/chat.ts +212 -0
- package/src/transforms/index.ts +22 -0
- package/src/transforms/prefill.ts +585 -0
- package/src/types/config.ts +172 -0
- package/src/types/content.ts +181 -0
- package/src/types/errors.ts +277 -0
- package/src/types/index.ts +154 -0
- package/src/types/message.ts +89 -0
- package/src/types/provider.ts +249 -0
- package/src/types/request.ts +131 -0
- package/src/types/response.ts +223 -0
- package/src/types/streaming.ts +231 -0
- package/src/types/tools.ts +92 -0
- package/src/utils/index.ts +15 -0
- package/src/utils/stream-parser.ts +440 -0
- package/src/utils/tool-parser.ts +715 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-parser.d.ts","sourceRoot":"","sources":["../../src/utils/tool-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,eAAe,EAAE,YAAY,EAA0B,MAAM,mBAAmB,CAAC;AAsBrH;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CAyFnE;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,IA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AC,OAAO,EAAE,UAAU,EAAE,GAAG,OAAO,CASpE;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAgB;IAC/B,gFAAgF;IAChF,cAAc,EAAE,MAAM,CAAC;IAEvB,yDAAyD;IACzD,MAAM,EAAE,kBAAkB,EAAE,CAAC;IAE7B,uEAAuE;IACvE,aAAa,EAAE,MAAM,CAAC;IAEtB,oCAAoC;IACpC,SAAS,EAAE,OAAO,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,6BAA6B,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,gBAAgB,CAoFrF;AA+CD,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAOhD"}
|
|
@@ -0,0 +1,571 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool parsing utilities for XML-based tool calls
|
|
3
|
+
*
|
|
4
|
+
* Supports both plain and antml:-prefixed formats:
|
|
5
|
+
* <function_calls> or <function_calls>
|
|
6
|
+
* <invoke name="..."> or <invoke name="...">
|
|
7
|
+
* <parameter name="..."> or <parameter name="...">
|
|
8
|
+
*
|
|
9
|
+
* Also supports self-closing invoke tags:
|
|
10
|
+
* <invoke name="tool"/> or <invoke name="tool"/>
|
|
11
|
+
*/
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// Tool Call Parsing
|
|
14
|
+
// ============================================================================
|
|
15
|
+
// Regex patterns supporting both plain and antml: prefix
|
|
16
|
+
// Pattern matches: <function_calls> or <function_calls>
|
|
17
|
+
const FUNCTION_CALLS_REGEX = /<(antml:)?function_calls>([\s\S]*?)<\/(antml:)?function_calls>/g;
|
|
18
|
+
// Full invoke tags with content
|
|
19
|
+
const INVOKE_REGEX_FULL = /<(antml:)?invoke\s+name="([^"]+)">([\s\S]*?)<\/(antml:)?invoke>/g;
|
|
20
|
+
// Self-closing invoke tags (no parameters)
|
|
21
|
+
const INVOKE_REGEX_SELF_CLOSE = /<(antml:)?invoke\s+name="([^"]+)"\s*\/>/g;
|
|
22
|
+
// Parameter tags
|
|
23
|
+
const PARAMETER_REGEX = /<(antml:)?parameter\s+name="([^"]+)">([\s\S]*?)<\/(antml:)?parameter>/g;
|
|
24
|
+
// Check for function_results following a block
|
|
25
|
+
const FUNCTION_RESULTS_START = /<(antml:)?function_results>/;
|
|
26
|
+
/**
|
|
27
|
+
* Parse tool calls from text containing XML function_calls blocks
|
|
28
|
+
*
|
|
29
|
+
* Uses "last-unexecuted-block" logic: finds the last function_calls block
|
|
30
|
+
* that doesn't have function_results immediately following it.
|
|
31
|
+
*/
|
|
32
|
+
export function parseToolCalls(text) {
|
|
33
|
+
// Reset regex
|
|
34
|
+
FUNCTION_CALLS_REGEX.lastIndex = 0;
|
|
35
|
+
// Find all function_calls blocks and pick the last unexecuted one
|
|
36
|
+
let blockMatch = null;
|
|
37
|
+
let lastUnexecutedMatch = null;
|
|
38
|
+
while ((blockMatch = FUNCTION_CALLS_REGEX.exec(text)) !== null) {
|
|
39
|
+
// Check if this block already has results after it
|
|
40
|
+
const afterPos = blockMatch.index + blockMatch[0].length;
|
|
41
|
+
const textAfter = text.slice(afterPos, afterPos + 100); // Check next 100 chars
|
|
42
|
+
if (!FUNCTION_RESULTS_START.test(textAfter.trimStart())) {
|
|
43
|
+
// This block hasn't been executed yet - store it
|
|
44
|
+
// Need to capture all properties since exec returns are reused
|
|
45
|
+
lastUnexecutedMatch = {
|
|
46
|
+
...blockMatch,
|
|
47
|
+
index: blockMatch.index,
|
|
48
|
+
input: blockMatch.input,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!lastUnexecutedMatch) {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const fullMatch = lastUnexecutedMatch[0];
|
|
56
|
+
const innerContent = lastUnexecutedMatch[2] ?? ''; // Group 2 is content between tags
|
|
57
|
+
const matchIndex = lastUnexecutedMatch.index;
|
|
58
|
+
const beforeText = text.slice(0, matchIndex);
|
|
59
|
+
const afterText = text.slice(matchIndex + fullMatch.length);
|
|
60
|
+
const calls = [];
|
|
61
|
+
// Parse full invoke tags (with content and closing tag)
|
|
62
|
+
INVOKE_REGEX_FULL.lastIndex = 0;
|
|
63
|
+
let invokeMatch;
|
|
64
|
+
while ((invokeMatch = INVOKE_REGEX_FULL.exec(innerContent)) !== null) {
|
|
65
|
+
const toolName = invokeMatch[2] ?? ''; // Group 2 is the name
|
|
66
|
+
const invokeContent = invokeMatch[3] ?? ''; // Group 3 is the content
|
|
67
|
+
// Parse parameters
|
|
68
|
+
const input = {};
|
|
69
|
+
PARAMETER_REGEX.lastIndex = 0;
|
|
70
|
+
let paramMatch;
|
|
71
|
+
while ((paramMatch = PARAMETER_REGEX.exec(invokeContent)) !== null) {
|
|
72
|
+
const paramName = paramMatch[2] ?? ''; // Group 2 is the name
|
|
73
|
+
const paramValue = paramMatch[3] ?? ''; // Group 3 is the value
|
|
74
|
+
// Try to parse as JSON, fall back to string
|
|
75
|
+
try {
|
|
76
|
+
input[paramName] = JSON.parse(paramValue);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
input[paramName] = paramValue.trim();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
calls.push({
|
|
83
|
+
id: generateToolId(),
|
|
84
|
+
name: toolName,
|
|
85
|
+
input,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
// Parse self-closing invoke tags (no parameters)
|
|
89
|
+
INVOKE_REGEX_SELF_CLOSE.lastIndex = 0;
|
|
90
|
+
let selfCloseMatch;
|
|
91
|
+
while ((selfCloseMatch = INVOKE_REGEX_SELF_CLOSE.exec(innerContent)) !== null) {
|
|
92
|
+
const toolName = selfCloseMatch[2] ?? ''; // Group 2 is the name
|
|
93
|
+
calls.push({
|
|
94
|
+
id: generateToolId(),
|
|
95
|
+
name: toolName,
|
|
96
|
+
input: {}, // No parameters for self-closing tag
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
calls,
|
|
101
|
+
beforeText,
|
|
102
|
+
afterText,
|
|
103
|
+
fullMatch,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Check if text contains an unclosed function_calls block
|
|
108
|
+
* Used for false-positive stop sequence detection
|
|
109
|
+
* Supports both plain and antml: prefixed tags
|
|
110
|
+
*/
|
|
111
|
+
export function hasUnclosedToolBlock(text) {
|
|
112
|
+
// Use regex that matches both plain and antml: prefixed tags
|
|
113
|
+
const openPattern = /<(antml:)?function_calls>/g;
|
|
114
|
+
const closePattern = /<\/(antml:)?function_calls>/g;
|
|
115
|
+
const openCount = (text.match(openPattern) || []).length;
|
|
116
|
+
const closeCount = (text.match(closePattern) || []).length;
|
|
117
|
+
return openCount > closeCount;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Check if text ends with a partial/unclosed tool block
|
|
121
|
+
* Supports both plain and antml: prefixed tags
|
|
122
|
+
*/
|
|
123
|
+
export function endsWithPartialToolBlock(text) {
|
|
124
|
+
// Check for partial opening tag (plain or antml:)
|
|
125
|
+
if (/<(antml:)?function_calls[^>]*$/.test(text))
|
|
126
|
+
return true;
|
|
127
|
+
if (/<(antml:)?invoke[^>]*$/.test(text))
|
|
128
|
+
return true;
|
|
129
|
+
if (/<(antml:)?parameter[^>]*$/.test(text))
|
|
130
|
+
return true;
|
|
131
|
+
// Check for unclosed block
|
|
132
|
+
return hasUnclosedToolBlock(text);
|
|
133
|
+
}
|
|
134
|
+
// ============================================================================
|
|
135
|
+
// Tool Result Formatting
|
|
136
|
+
// ============================================================================
|
|
137
|
+
/**
|
|
138
|
+
* Format tool results as XML for injection.
|
|
139
|
+
* Handles both string content and structured content blocks (with images).
|
|
140
|
+
*/
|
|
141
|
+
export function formatToolResults(results) {
|
|
142
|
+
const parts = ['<function_results>'];
|
|
143
|
+
for (const result of results) {
|
|
144
|
+
const tagName = result.isError ? 'error' : 'result';
|
|
145
|
+
parts.push(`<${tagName} tool_use_id="${result.toolUseId}">`);
|
|
146
|
+
// Handle both string and array content
|
|
147
|
+
if (typeof result.content === 'string') {
|
|
148
|
+
parts.push(escapeXml(result.content));
|
|
149
|
+
}
|
|
150
|
+
else if (Array.isArray(result.content)) {
|
|
151
|
+
// Structured content blocks
|
|
152
|
+
for (const block of result.content) {
|
|
153
|
+
if (block.type === 'text') {
|
|
154
|
+
parts.push(escapeXml(block.text));
|
|
155
|
+
}
|
|
156
|
+
else if (block.type === 'image') {
|
|
157
|
+
// For XML mode, we can't embed images directly
|
|
158
|
+
// Add a note about the image for the model
|
|
159
|
+
const sizeKb = Math.round((block.source.data.length * 0.75) / 1024);
|
|
160
|
+
parts.push(`[Image: ${block.source.mediaType}, ~${sizeKb}KB]`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
parts.push(`</${tagName}>`);
|
|
165
|
+
}
|
|
166
|
+
parts.push('</function_results>');
|
|
167
|
+
return parts.join('\n');
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Format a single tool result
|
|
171
|
+
*/
|
|
172
|
+
export function formatToolResult(result) {
|
|
173
|
+
return formatToolResults([result]);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Format tool definitions as XML for system prompt
|
|
177
|
+
*/
|
|
178
|
+
export function formatToolDefinitions(tools) {
|
|
179
|
+
const parts = ['<tools>'];
|
|
180
|
+
for (const tool of tools) {
|
|
181
|
+
parts.push(`<tool name="${escapeXml(tool.name)}">`);
|
|
182
|
+
parts.push(`<description>${escapeXml(tool.description)}</description>`);
|
|
183
|
+
parts.push('<parameters>');
|
|
184
|
+
for (const [paramName, param] of Object.entries(tool.parameters)) {
|
|
185
|
+
const attrs = [`name="${escapeXml(paramName)}"`, `type="${param.type}"`];
|
|
186
|
+
if (param.required)
|
|
187
|
+
attrs.push('required="true"');
|
|
188
|
+
if (param.enum)
|
|
189
|
+
attrs.push(`enum="${param.enum.join(',')}"`);
|
|
190
|
+
parts.push(`<parameter ${attrs.join(' ')}>`);
|
|
191
|
+
if (param.description) {
|
|
192
|
+
parts.push(escapeXml(param.description));
|
|
193
|
+
}
|
|
194
|
+
parts.push('</parameter>');
|
|
195
|
+
}
|
|
196
|
+
parts.push('</parameters>');
|
|
197
|
+
parts.push('</tool>');
|
|
198
|
+
}
|
|
199
|
+
parts.push('</tools>');
|
|
200
|
+
return parts.join('\n');
|
|
201
|
+
}
|
|
202
|
+
// ============================================================================
|
|
203
|
+
// Accumulated Text to ContentBlock[] Parsing
|
|
204
|
+
// ============================================================================
|
|
205
|
+
// Regex for matching thinking blocks (both plain and antml: prefixed)
|
|
206
|
+
const THINKING_BLOCK_REGEX = /<(antml:)?thinking>([\s\S]*?)<\/(antml:)?thinking>/g;
|
|
207
|
+
// Regex for matching function_calls blocks with their content
|
|
208
|
+
const FUNCTION_BLOCK_WITH_CONTENT_REGEX = /<(antml:)?function_calls>([\s\S]*?)<\/(antml:)?function_calls>/g;
|
|
209
|
+
// Regex for matching function_results blocks with their content
|
|
210
|
+
const FUNCTION_RESULTS_BLOCK_REGEX = /<(antml:)?function_results>([\s\S]*?)<\/(antml:)?function_results>/g;
|
|
211
|
+
// Regex for individual result/error within function_results
|
|
212
|
+
const RESULT_REGEX = /<result\s+tool_use_id="([^"]+)">([\s\S]*?)<\/result>/g;
|
|
213
|
+
const ERROR_REGEX = /<error\s+tool_use_id="([^"]+)">([\s\S]*?)<\/error>/g;
|
|
214
|
+
/**
|
|
215
|
+
* Parse accumulated assistant text into structured ContentBlock[].
|
|
216
|
+
* Extracts thinking blocks, tool calls, tool results, and plain text.
|
|
217
|
+
*
|
|
218
|
+
* @param text - The accumulated assistant output text
|
|
219
|
+
* @returns Array of ContentBlock in order of appearance
|
|
220
|
+
*/
|
|
221
|
+
export function parseAccumulatedIntoBlocks(text) {
|
|
222
|
+
const blocks = [];
|
|
223
|
+
const toolCalls = [];
|
|
224
|
+
const toolResults = [];
|
|
225
|
+
const positions = [];
|
|
226
|
+
// Find all thinking blocks
|
|
227
|
+
THINKING_BLOCK_REGEX.lastIndex = 0;
|
|
228
|
+
let thinkingMatch;
|
|
229
|
+
while ((thinkingMatch = THINKING_BLOCK_REGEX.exec(text)) !== null) {
|
|
230
|
+
positions.push({
|
|
231
|
+
start: thinkingMatch.index,
|
|
232
|
+
end: thinkingMatch.index + thinkingMatch[0].length,
|
|
233
|
+
block: {
|
|
234
|
+
type: 'thinking',
|
|
235
|
+
thinking: thinkingMatch[2] ?? '',
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
// Find all function_calls blocks and parse their tool calls
|
|
240
|
+
FUNCTION_BLOCK_WITH_CONTENT_REGEX.lastIndex = 0;
|
|
241
|
+
let funcMatch;
|
|
242
|
+
while ((funcMatch = FUNCTION_BLOCK_WITH_CONTENT_REGEX.exec(text)) !== null) {
|
|
243
|
+
const innerContent = funcMatch[2] ?? '';
|
|
244
|
+
const blockToolCalls = [];
|
|
245
|
+
// Parse invoke tags in this block
|
|
246
|
+
INVOKE_REGEX_FULL.lastIndex = 0;
|
|
247
|
+
let invokeMatch;
|
|
248
|
+
while ((invokeMatch = INVOKE_REGEX_FULL.exec(innerContent)) !== null) {
|
|
249
|
+
const toolName = invokeMatch[2] ?? '';
|
|
250
|
+
const invokeContent = invokeMatch[3] ?? '';
|
|
251
|
+
const input = {};
|
|
252
|
+
// Parse parameters
|
|
253
|
+
PARAMETER_REGEX.lastIndex = 0;
|
|
254
|
+
let paramMatch;
|
|
255
|
+
while ((paramMatch = PARAMETER_REGEX.exec(invokeContent)) !== null) {
|
|
256
|
+
const paramName = paramMatch[2] ?? '';
|
|
257
|
+
const paramValue = paramMatch[3] ?? '';
|
|
258
|
+
try {
|
|
259
|
+
input[paramName] = JSON.parse(paramValue);
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
input[paramName] = paramValue.trim();
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const id = generateToolId();
|
|
266
|
+
const toolCall = { id, name: toolName, input };
|
|
267
|
+
toolCalls.push(toolCall);
|
|
268
|
+
blockToolCalls.push({
|
|
269
|
+
type: 'tool_use',
|
|
270
|
+
id,
|
|
271
|
+
name: toolName,
|
|
272
|
+
input,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
// Parse self-closing invoke tags
|
|
276
|
+
INVOKE_REGEX_SELF_CLOSE.lastIndex = 0;
|
|
277
|
+
let selfCloseMatch;
|
|
278
|
+
while ((selfCloseMatch = INVOKE_REGEX_SELF_CLOSE.exec(innerContent)) !== null) {
|
|
279
|
+
const toolName = selfCloseMatch[2] ?? '';
|
|
280
|
+
const id = generateToolId();
|
|
281
|
+
const toolCall = { id, name: toolName, input: {} };
|
|
282
|
+
toolCalls.push(toolCall);
|
|
283
|
+
blockToolCalls.push({
|
|
284
|
+
type: 'tool_use',
|
|
285
|
+
id,
|
|
286
|
+
name: toolName,
|
|
287
|
+
input: {},
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
if (blockToolCalls.length > 0) {
|
|
291
|
+
positions.push({
|
|
292
|
+
start: funcMatch.index,
|
|
293
|
+
end: funcMatch.index + funcMatch[0].length,
|
|
294
|
+
block: blockToolCalls,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
// Find all function_results blocks and parse their results
|
|
299
|
+
FUNCTION_RESULTS_BLOCK_REGEX.lastIndex = 0;
|
|
300
|
+
let resultsMatch;
|
|
301
|
+
while ((resultsMatch = FUNCTION_RESULTS_BLOCK_REGEX.exec(text)) !== null) {
|
|
302
|
+
const innerContent = resultsMatch[2] ?? '';
|
|
303
|
+
const blockResults = [];
|
|
304
|
+
// Parse result tags
|
|
305
|
+
RESULT_REGEX.lastIndex = 0;
|
|
306
|
+
let resultMatch;
|
|
307
|
+
while ((resultMatch = RESULT_REGEX.exec(innerContent)) !== null) {
|
|
308
|
+
const toolUseId = resultMatch[1] ?? '';
|
|
309
|
+
const content = unescapeXml(resultMatch[2] ?? '');
|
|
310
|
+
const result = { toolUseId, content, isError: false };
|
|
311
|
+
toolResults.push(result);
|
|
312
|
+
blockResults.push({
|
|
313
|
+
type: 'tool_result',
|
|
314
|
+
toolUseId,
|
|
315
|
+
content,
|
|
316
|
+
isError: false,
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
// Parse error tags
|
|
320
|
+
ERROR_REGEX.lastIndex = 0;
|
|
321
|
+
let errorMatch;
|
|
322
|
+
while ((errorMatch = ERROR_REGEX.exec(innerContent)) !== null) {
|
|
323
|
+
const toolUseId = errorMatch[1] ?? '';
|
|
324
|
+
const content = unescapeXml(errorMatch[2] ?? '');
|
|
325
|
+
const result = { toolUseId, content, isError: true };
|
|
326
|
+
toolResults.push(result);
|
|
327
|
+
blockResults.push({
|
|
328
|
+
type: 'tool_result',
|
|
329
|
+
toolUseId,
|
|
330
|
+
content,
|
|
331
|
+
isError: true,
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
if (blockResults.length > 0) {
|
|
335
|
+
positions.push({
|
|
336
|
+
start: resultsMatch.index,
|
|
337
|
+
end: resultsMatch.index + resultsMatch[0].length,
|
|
338
|
+
block: blockResults,
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
// Sort positions by start index
|
|
343
|
+
positions.sort((a, b) => a.start - b.start);
|
|
344
|
+
// Build final blocks array, inserting text blocks between special blocks
|
|
345
|
+
let lastEnd = 0;
|
|
346
|
+
for (const pos of positions) {
|
|
347
|
+
// Add text block for content before this special block
|
|
348
|
+
if (pos.start > lastEnd) {
|
|
349
|
+
const textContent = text.slice(lastEnd, pos.start).trim();
|
|
350
|
+
if (textContent) {
|
|
351
|
+
blocks.push({ type: 'text', text: textContent });
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// Add the special block(s)
|
|
355
|
+
if (Array.isArray(pos.block)) {
|
|
356
|
+
blocks.push(...pos.block);
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
blocks.push(pos.block);
|
|
360
|
+
}
|
|
361
|
+
lastEnd = pos.end;
|
|
362
|
+
}
|
|
363
|
+
// Add any remaining text after the last special block
|
|
364
|
+
if (lastEnd < text.length) {
|
|
365
|
+
const textContent = text.slice(lastEnd).trim();
|
|
366
|
+
if (textContent) {
|
|
367
|
+
blocks.push({ type: 'text', text: textContent });
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
// Handle case where there are no special blocks at all
|
|
371
|
+
if (positions.length === 0 && text.trim()) {
|
|
372
|
+
blocks.push({ type: 'text', text: text.trim() });
|
|
373
|
+
}
|
|
374
|
+
return { blocks, toolCalls, toolResults };
|
|
375
|
+
}
|
|
376
|
+
// Assembled to avoid triggering stop sequences in model output
|
|
377
|
+
const FUNC_CALLS_OPEN = '<' + 'function_calls>';
|
|
378
|
+
const FUNC_CALLS_CLOSE = '</' + 'function_calls>';
|
|
379
|
+
const INVOKE_OPEN = '<' + 'invoke name="';
|
|
380
|
+
const INVOKE_CLOSE = '</' + 'invoke>';
|
|
381
|
+
const PARAM_OPEN = '<' + 'parameter name="';
|
|
382
|
+
const PARAM_CLOSE = '</' + 'parameter>';
|
|
383
|
+
/**
|
|
384
|
+
* Get tool instructions string for manual placement.
|
|
385
|
+
* Use this when you want to control where tool instructions appear
|
|
386
|
+
* (e.g., injected into conversation rather than system prompt).
|
|
387
|
+
*
|
|
388
|
+
* @param tools - Tool definitions
|
|
389
|
+
* @returns Complete instruction string with definitions and usage example
|
|
390
|
+
*/
|
|
391
|
+
export function getToolInstructions(tools) {
|
|
392
|
+
// Format definitions
|
|
393
|
+
const definitions = tools.map((tool) => {
|
|
394
|
+
const toolDef = {
|
|
395
|
+
description: tool.description,
|
|
396
|
+
name: tool.name,
|
|
397
|
+
parameters: tool.inputSchema,
|
|
398
|
+
};
|
|
399
|
+
return `<function>${JSON.stringify(toolDef)}</function>`;
|
|
400
|
+
});
|
|
401
|
+
// Build instruction with example
|
|
402
|
+
return `<functions>
|
|
403
|
+
${definitions.join('\n')}
|
|
404
|
+
</functions>
|
|
405
|
+
|
|
406
|
+
When making function calls using tools that accept array or object parameters ensure those are structured using JSON. For example:
|
|
407
|
+
${FUNC_CALLS_OPEN}
|
|
408
|
+
${INVOKE_OPEN}example_tool">
|
|
409
|
+
${PARAM_OPEN}parameter">[{"key": "value"}]${PARAM_CLOSE}
|
|
410
|
+
${INVOKE_CLOSE}
|
|
411
|
+
${FUNC_CALLS_CLOSE}`;
|
|
412
|
+
}
|
|
413
|
+
/**
|
|
414
|
+
* Check if any tool result contains image content
|
|
415
|
+
*/
|
|
416
|
+
export function hasImageInToolResults(results) {
|
|
417
|
+
for (const result of results) {
|
|
418
|
+
if (Array.isArray(result.content)) {
|
|
419
|
+
if (result.content.some(block => block.type === 'image')) {
|
|
420
|
+
return true;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Format tool results for split-turn injection when images are present.
|
|
428
|
+
*
|
|
429
|
+
* This separates the XML into parts that go in the assistant turn (text)
|
|
430
|
+
* and the user turn (images), with continuation XML for the next assistant turn.
|
|
431
|
+
*
|
|
432
|
+
* Structure when images present:
|
|
433
|
+
* ```
|
|
434
|
+
* Assistant: <function_results>
|
|
435
|
+
* <result tool_use_id="...">
|
|
436
|
+
* text content here
|
|
437
|
+
* [END - mid XML]
|
|
438
|
+
*
|
|
439
|
+
* User: [image blocks]
|
|
440
|
+
*
|
|
441
|
+
* Assistant (prefill): </result>
|
|
442
|
+
* </function_results>
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
export function formatToolResultsForSplitTurn(results) {
|
|
446
|
+
const images = [];
|
|
447
|
+
let beforeImageXml = '<function_results>\n';
|
|
448
|
+
let afterImageXml = '';
|
|
449
|
+
let imageInsertionPoint = -1; // Index of result where we found images
|
|
450
|
+
for (let i = 0; i < results.length; i++) {
|
|
451
|
+
const result = results[i];
|
|
452
|
+
const tagName = result.isError ? 'error' : 'result';
|
|
453
|
+
// Check if this result has images
|
|
454
|
+
let resultHasImages = false;
|
|
455
|
+
let textParts = [];
|
|
456
|
+
let resultImages = [];
|
|
457
|
+
if (typeof result.content === 'string') {
|
|
458
|
+
textParts.push(escapeXml(result.content));
|
|
459
|
+
}
|
|
460
|
+
else if (Array.isArray(result.content)) {
|
|
461
|
+
for (const block of result.content) {
|
|
462
|
+
if (block.type === 'text') {
|
|
463
|
+
textParts.push(escapeXml(block.text));
|
|
464
|
+
}
|
|
465
|
+
else if (block.type === 'image') {
|
|
466
|
+
resultHasImages = true;
|
|
467
|
+
resultImages.push({
|
|
468
|
+
type: 'image',
|
|
469
|
+
source: {
|
|
470
|
+
type: 'base64',
|
|
471
|
+
media_type: block.source.mediaType,
|
|
472
|
+
data: block.source.data,
|
|
473
|
+
},
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (resultHasImages && imageInsertionPoint === -1) {
|
|
479
|
+
// First result with images - split here
|
|
480
|
+
imageInsertionPoint = i;
|
|
481
|
+
images.push(...resultImages);
|
|
482
|
+
// Add opening tag and text content (no closing tag yet)
|
|
483
|
+
beforeImageXml += `<${tagName} tool_use_id="${result.toolUseId}">\n`;
|
|
484
|
+
if (textParts.length > 0) {
|
|
485
|
+
beforeImageXml += textParts.join('\n');
|
|
486
|
+
}
|
|
487
|
+
// Note: Intentionally NOT adding closing tag - split happens here
|
|
488
|
+
// After image, we need to close this result and add remaining results
|
|
489
|
+
afterImageXml = `</${tagName}>\n`;
|
|
490
|
+
// Process remaining results into afterImageXml
|
|
491
|
+
for (let j = i + 1; j < results.length; j++) {
|
|
492
|
+
const remainingResult = results[j];
|
|
493
|
+
afterImageXml += formatSingleResultXml(remainingResult);
|
|
494
|
+
}
|
|
495
|
+
afterImageXml += '</function_results>';
|
|
496
|
+
// Stop processing - we've handled everything
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
else if (imageInsertionPoint === -1) {
|
|
500
|
+
// No images yet - add full result to beforeImageXml
|
|
501
|
+
beforeImageXml += `<${tagName} tool_use_id="${result.toolUseId}">\n`;
|
|
502
|
+
beforeImageXml += textParts.join('\n');
|
|
503
|
+
beforeImageXml += `\n</${tagName}>\n`;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// If no images were found, complete the XML normally
|
|
507
|
+
if (imageInsertionPoint === -1) {
|
|
508
|
+
beforeImageXml += '</function_results>';
|
|
509
|
+
return {
|
|
510
|
+
beforeImageXml,
|
|
511
|
+
images: [],
|
|
512
|
+
afterImageXml: '',
|
|
513
|
+
hasImages: false,
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
beforeImageXml,
|
|
518
|
+
images,
|
|
519
|
+
afterImageXml,
|
|
520
|
+
hasImages: true,
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Format a single tool result as complete XML
|
|
525
|
+
*/
|
|
526
|
+
function formatSingleResultXml(result) {
|
|
527
|
+
const tagName = result.isError ? 'error' : 'result';
|
|
528
|
+
let xml = `<${tagName} tool_use_id="${result.toolUseId}">\n`;
|
|
529
|
+
if (typeof result.content === 'string') {
|
|
530
|
+
xml += escapeXml(result.content);
|
|
531
|
+
}
|
|
532
|
+
else if (Array.isArray(result.content)) {
|
|
533
|
+
for (const block of result.content) {
|
|
534
|
+
if (block.type === 'text') {
|
|
535
|
+
xml += escapeXml(block.text);
|
|
536
|
+
}
|
|
537
|
+
else if (block.type === 'image') {
|
|
538
|
+
// For remaining results after split, images become text placeholders
|
|
539
|
+
const sizeKb = Math.round((block.source.data.length * 0.75) / 1024);
|
|
540
|
+
xml += `[Image: ${block.source.mediaType}, ~${sizeKb}KB]`;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
xml += `\n</${tagName}>\n`;
|
|
545
|
+
return xml;
|
|
546
|
+
}
|
|
547
|
+
// ============================================================================
|
|
548
|
+
// Utilities
|
|
549
|
+
// ============================================================================
|
|
550
|
+
let toolIdCounter = 0;
|
|
551
|
+
function generateToolId() {
|
|
552
|
+
toolIdCounter++;
|
|
553
|
+
return `tool_${Date.now()}_${toolIdCounter}`;
|
|
554
|
+
}
|
|
555
|
+
function escapeXml(text) {
|
|
556
|
+
return text
|
|
557
|
+
.replace(/&/g, '&')
|
|
558
|
+
.replace(/</g, '<')
|
|
559
|
+
.replace(/>/g, '>')
|
|
560
|
+
.replace(/"/g, '"')
|
|
561
|
+
.replace(/'/g, ''');
|
|
562
|
+
}
|
|
563
|
+
export function unescapeXml(text) {
|
|
564
|
+
return text
|
|
565
|
+
.replace(/'/g, "'")
|
|
566
|
+
.replace(/"/g, '"')
|
|
567
|
+
.replace(/>/g, '>')
|
|
568
|
+
.replace(/</g, '<')
|
|
569
|
+
.replace(/&/g, '&');
|
|
570
|
+
}
|
|
571
|
+
//# sourceMappingURL=tool-parser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tool-parser.js","sourceRoot":"","sources":["../../src/utils/tool-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAIH,+EAA+E;AAC/E,oBAAoB;AACpB,+EAA+E;AAE/E,yDAAyD;AACzD,wDAAwD;AACxD,MAAM,oBAAoB,GAAG,iEAAiE,CAAC;AAE/F,gCAAgC;AAChC,MAAM,iBAAiB,GAAG,kEAAkE,CAAC;AAE7F,2CAA2C;AAC3C,MAAM,uBAAuB,GAAG,0CAA0C,CAAC;AAE3E,iBAAiB;AACjB,MAAM,eAAe,GAAG,wEAAwE,CAAC;AAEjG,+CAA+C;AAC/C,MAAM,sBAAsB,GAAG,6BAA6B,CAAC;AAE7D;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,cAAc;IACd,oBAAoB,CAAC,SAAS,GAAG,CAAC,CAAC;IAEnC,kEAAkE;IAClE,IAAI,UAAU,GAA2B,IAAI,CA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AAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;AAC5B,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@animalabs/membrane",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "LLM middleware - a selective boundary that transforms what passes through",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsc",
|
|
16
|
+
"prepare": "npm run build",
|
|
17
|
+
"watch": "tsc --watch",
|
|
18
|
+
"clean": "rm -rf dist",
|
|
19
|
+
"typecheck": "tsc --noEmit"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@anthropic-ai/sdk": "^0.52.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.0.0",
|
|
26
|
+
"tsx": "^4.21.0",
|
|
27
|
+
"typescript": "^5.7.0"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"src"
|
|
32
|
+
],
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=20.0.0"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT"
|
|
37
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context management module
|
|
3
|
+
*
|
|
4
|
+
* Handles rolling context, cache marker placement, and state management
|
|
5
|
+
* for efficient prompt caching with LLMs.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export { processContext } from './process.js';
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
createInitialState,
|
|
12
|
+
defaultTokenEstimator,
|
|
13
|
+
DEFAULT_CONTEXT_CONFIG,
|
|
14
|
+
} from './types.js';
|
|
15
|
+
|
|
16
|
+
export type {
|
|
17
|
+
ContextInput,
|
|
18
|
+
ContextOutput,
|
|
19
|
+
ContextState,
|
|
20
|
+
ContextConfig,
|
|
21
|
+
ContextInfo,
|
|
22
|
+
ContextStreamOptions,
|
|
23
|
+
CacheMarker,
|
|
24
|
+
} from './types.js';
|