@animalabs/membrane 0.5.51 → 0.5.53
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 +12 -0
- package/dist/context/index.js +11 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/process.d.ts +43 -0
- package/dist/context/process.js +381 -0
- package/dist/context/process.js.map +1 -0
- package/dist/context/types.d.ts +164 -0
- package/dist/context/types.js +61 -0
- package/dist/context/types.js.map +1 -0
- package/dist/formatters/anthropic-xml.d.ts +63 -0
- package/dist/formatters/anthropic-xml.js +417 -0
- package/dist/formatters/anthropic-xml.js.map +1 -0
- package/dist/formatters/completions.d.ts +68 -0
- package/dist/formatters/completions.js +261 -0
- package/dist/formatters/completions.js.map +1 -0
- package/dist/formatters/index.d.ts +10 -0
- package/dist/formatters/index.d.ts.map +1 -1
- package/dist/formatters/index.js +8 -0
- package/dist/formatters/index.js.map +1 -0
- package/dist/formatters/native.d.ts +35 -0
- package/dist/formatters/native.d.ts.map +1 -1
- package/dist/formatters/native.js +346 -0
- package/dist/formatters/native.js.map +1 -0
- package/dist/formatters/normalize-tool-pairs.d.ts +75 -0
- package/dist/formatters/normalize-tool-pairs.d.ts.map +1 -0
- package/dist/formatters/normalize-tool-pairs.js +498 -0
- package/dist/formatters/normalize-tool-pairs.js.map +1 -0
- package/dist/formatters/types.d.ts +222 -0
- package/dist/formatters/types.d.ts.map +1 -1
- package/dist/formatters/types.js +7 -0
- package/dist/formatters/types.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +20 -0
- package/dist/index.js.map +1 -0
- package/dist/membrane.d.ts +155 -0
- package/dist/providers/anthropic.d.ts +36 -0
- package/dist/providers/bedrock.d.ts +43 -0
- package/dist/providers/gemini.d.ts +68 -0
- package/dist/providers/gemini.js +538 -0
- package/dist/providers/gemini.js.map +1 -0
- package/dist/providers/index.d.ts +13 -0
- package/dist/providers/index.js +13 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/mock.d.ts +90 -0
- package/dist/providers/mock.d.ts.map +1 -0
- package/dist/providers/mock.js +210 -0
- package/dist/providers/mock.js.map +1 -0
- package/dist/providers/openai-compatible.d.ts +82 -0
- package/dist/providers/openai-compatible.js +480 -0
- package/dist/providers/openai-compatible.js.map +1 -0
- package/dist/providers/openai-completions.d.ts +89 -0
- package/dist/providers/openai-completions.js +347 -0
- package/dist/providers/openai-completions.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +77 -0
- package/dist/providers/openai-responses.js +333 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/providers/openai.d.ts +77 -0
- package/dist/providers/openai.js +533 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/openrouter.d.ts +82 -0
- package/dist/providers/openrouter.js +556 -0
- package/dist/providers/openrouter.js.map +1 -0
- package/dist/providers/utils.d.ts +44 -0
- package/dist/providers/utils.d.ts.map +1 -0
- package/dist/providers/utils.js +100 -0
- package/dist/providers/utils.js.map +1 -0
- package/dist/registry/default-pricing.d.ts +3 -0
- package/dist/registry/default-pricing.d.ts.map +1 -0
- package/dist/registry/default-pricing.js +75 -0
- package/dist/registry/default-pricing.js.map +1 -0
- package/dist/transforms/chat.d.ts +52 -0
- package/dist/transforms/chat.js +136 -0
- package/dist/transforms/chat.js.map +1 -0
- package/dist/transforms/index.d.ts +5 -0
- package/dist/transforms/index.js +7 -0
- package/dist/transforms/index.js.map +1 -0
- package/dist/types/config.d.ts +110 -0
- package/dist/types/config.js +21 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/content.d.ts +87 -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 +50 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +253 -0
- package/dist/types/errors.js.map +1 -0
- package/dist/types/index.d.ts +20 -0
- package/dist/types/index.js +10 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/message.d.ts +52 -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 +169 -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 +116 -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 +194 -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/cost.d.ts +10 -0
- package/dist/utils/cost.d.ts.map +1 -0
- package/dist/utils/cost.js +19 -0
- package/dist/utils/cost.js.map +1 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +6 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/stream-parser.d.ts +84 -0
- package/dist/utils/stream-parser.js +418 -0
- package/dist/utils/stream-parser.js.map +1 -0
- package/dist/utils/tool-parser.d.ts +134 -0
- package/dist/utils/tool-parser.js +600 -0
- package/dist/utils/tool-parser.js.map +1 -0
- package/dist/yielding-stream.d.ts +60 -0
- package/dist/yielding-stream.js +206 -0
- package/dist/yielding-stream.js.map +1 -0
- package/package.json +1 -1
- package/src/formatters/index.ts +9 -0
- package/src/formatters/native.ts +12 -1
- package/src/formatters/normalize-tool-pairs.ts +622 -0
- package/src/formatters/types.ts +39 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Native Formatter
|
|
3
|
+
*
|
|
4
|
+
* Pass-through formatter that converts messages to standard user/assistant
|
|
5
|
+
* format without prefill. Uses native API tool calling.
|
|
6
|
+
*
|
|
7
|
+
* Supports two participant modes:
|
|
8
|
+
* - 'simple': Strict two-party (Human/Assistant), no names in content
|
|
9
|
+
* - 'multiuser': Multiple participants, names prefixed to content
|
|
10
|
+
*/
|
|
11
|
+
import { normalizeToolPairs } from './normalize-tool-pairs.js';
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// Pass-through Stream Parser
|
|
14
|
+
// ============================================================================
|
|
15
|
+
/**
|
|
16
|
+
* Simple pass-through parser that doesn't do XML tracking.
|
|
17
|
+
* Just accumulates content and emits text chunks.
|
|
18
|
+
*/
|
|
19
|
+
class PassthroughParser {
|
|
20
|
+
accumulated = '';
|
|
21
|
+
blockIndex = 0;
|
|
22
|
+
blockStarted = false;
|
|
23
|
+
processChunk(chunk) {
|
|
24
|
+
this.accumulated += chunk;
|
|
25
|
+
const meta = {
|
|
26
|
+
type: 'text',
|
|
27
|
+
visible: true,
|
|
28
|
+
blockIndex: this.blockIndex,
|
|
29
|
+
};
|
|
30
|
+
const emissions = [];
|
|
31
|
+
const blockEvents = [];
|
|
32
|
+
// Emit block_start on first chunk
|
|
33
|
+
if (!this.blockStarted) {
|
|
34
|
+
const startEvent = {
|
|
35
|
+
event: 'block_start',
|
|
36
|
+
index: this.blockIndex,
|
|
37
|
+
block: { type: 'text' },
|
|
38
|
+
};
|
|
39
|
+
emissions.push({ kind: 'blockEvent', event: startEvent });
|
|
40
|
+
blockEvents.push(startEvent);
|
|
41
|
+
this.blockStarted = true;
|
|
42
|
+
}
|
|
43
|
+
emissions.push({ kind: 'content', text: chunk, meta });
|
|
44
|
+
return { emissions, content: [{ text: chunk, meta }], blockEvents };
|
|
45
|
+
}
|
|
46
|
+
flush() {
|
|
47
|
+
const emissions = [];
|
|
48
|
+
const blockEvents = [];
|
|
49
|
+
if (this.blockStarted) {
|
|
50
|
+
const completeEvent = {
|
|
51
|
+
event: 'block_complete',
|
|
52
|
+
index: this.blockIndex,
|
|
53
|
+
block: { type: 'text', content: this.accumulated },
|
|
54
|
+
};
|
|
55
|
+
emissions.push({ kind: 'blockEvent', event: completeEvent });
|
|
56
|
+
blockEvents.push(completeEvent);
|
|
57
|
+
this.blockStarted = false;
|
|
58
|
+
}
|
|
59
|
+
return { emissions, content: [], blockEvents };
|
|
60
|
+
}
|
|
61
|
+
getAccumulated() {
|
|
62
|
+
return this.accumulated;
|
|
63
|
+
}
|
|
64
|
+
reset() {
|
|
65
|
+
this.accumulated = '';
|
|
66
|
+
this.blockIndex = 0;
|
|
67
|
+
this.blockStarted = false;
|
|
68
|
+
}
|
|
69
|
+
push(content) {
|
|
70
|
+
this.accumulated += content;
|
|
71
|
+
}
|
|
72
|
+
getCurrentBlockType() {
|
|
73
|
+
return 'text';
|
|
74
|
+
}
|
|
75
|
+
getBlockIndex() {
|
|
76
|
+
return this.blockIndex;
|
|
77
|
+
}
|
|
78
|
+
incrementBlockIndex() {
|
|
79
|
+
this.blockIndex++;
|
|
80
|
+
}
|
|
81
|
+
isInsideBlock() {
|
|
82
|
+
// Pass-through mode never has nested blocks
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
getDepths() {
|
|
86
|
+
return { functionCalls: 0, functionResults: 0, thinking: 0 };
|
|
87
|
+
}
|
|
88
|
+
resetForNewIteration() {
|
|
89
|
+
// No special reset needed for pass-through mode
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// ============================================================================
|
|
93
|
+
// Native Formatter
|
|
94
|
+
// ============================================================================
|
|
95
|
+
export class NativeFormatter {
|
|
96
|
+
name = 'native';
|
|
97
|
+
usesPrefill = false;
|
|
98
|
+
config;
|
|
99
|
+
constructor(config = {}) {
|
|
100
|
+
this.config = {
|
|
101
|
+
nameFormat: config.nameFormat ?? '{name}: ',
|
|
102
|
+
unsupportedMedia: config.unsupportedMedia ?? 'error',
|
|
103
|
+
warnOnStrip: config.warnOnStrip ?? true,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
// ==========================================================================
|
|
107
|
+
// REQUEST BUILDING
|
|
108
|
+
// ==========================================================================
|
|
109
|
+
buildMessages(messages, options) {
|
|
110
|
+
const { participantMode, assistantParticipant, humanParticipant, tools, systemPrompt, promptCaching = false, cacheTtl, hasCacheMarker, contextPrefix, additionalStopSequences, } = options;
|
|
111
|
+
// Build cache_control object if prompt caching is enabled
|
|
112
|
+
const cacheControl = promptCaching
|
|
113
|
+
? { type: 'ephemeral', ...(cacheTtl ? { ttl: cacheTtl } : {}) }
|
|
114
|
+
: undefined;
|
|
115
|
+
const providerMessages = [];
|
|
116
|
+
// Add context prefix as first assistant message (for simulacrum seeding)
|
|
117
|
+
if (contextPrefix) {
|
|
118
|
+
const prefixBlock = { type: 'text', text: contextPrefix };
|
|
119
|
+
if (promptCaching && cacheControl) {
|
|
120
|
+
prefixBlock.cache_control = cacheControl;
|
|
121
|
+
}
|
|
122
|
+
providerMessages.push({
|
|
123
|
+
role: 'assistant',
|
|
124
|
+
content: [prefixBlock],
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
// Validate simple mode participants
|
|
128
|
+
if (participantMode === 'simple' && !humanParticipant) {
|
|
129
|
+
throw new Error('NativeFormatter in simple mode requires humanParticipant option');
|
|
130
|
+
}
|
|
131
|
+
for (let i = 0; i < messages.length; i++) {
|
|
132
|
+
const message = messages[i];
|
|
133
|
+
if (!message)
|
|
134
|
+
continue;
|
|
135
|
+
// Determine role
|
|
136
|
+
const isAssistant = message.participant === assistantParticipant;
|
|
137
|
+
// Validate participant in simple mode
|
|
138
|
+
if (participantMode === 'simple') {
|
|
139
|
+
if (!isAssistant && message.participant !== humanParticipant) {
|
|
140
|
+
throw new Error(`NativeFormatter in simple mode only supports "${humanParticipant}" and "${assistantParticipant}". ` +
|
|
141
|
+
`Got: "${message.participant}". Use participantMode: 'multiuser' for multiple participants.`);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const role = isAssistant ? 'assistant' : 'user';
|
|
145
|
+
// Convert content
|
|
146
|
+
const content = this.convertContent(message.content, message.participant, {
|
|
147
|
+
includeNames: participantMode === 'multiuser' && !isAssistant,
|
|
148
|
+
});
|
|
149
|
+
if (content.length === 0) {
|
|
150
|
+
continue; // Skip empty messages
|
|
151
|
+
}
|
|
152
|
+
// hasCacheMarker: cache boundary is BEFORE this message — tag previous message's last block
|
|
153
|
+
if (hasCacheMarker && hasCacheMarker(message, i) && cacheControl && providerMessages.length > 0) {
|
|
154
|
+
const prevMsg = providerMessages[providerMessages.length - 1];
|
|
155
|
+
const prevContent = Array.isArray(prevMsg.content) ? prevMsg.content : [];
|
|
156
|
+
if (prevContent.length > 0) {
|
|
157
|
+
prevContent[prevContent.length - 1].cache_control = cacheControl;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
providerMessages.push({ role, content });
|
|
161
|
+
// cacheBreakpoint: cache up to and INCLUDING this message — tag last block
|
|
162
|
+
if (message.cacheBreakpoint && cacheControl && content.length > 0) {
|
|
163
|
+
content[content.length - 1].cache_control = cacheControl;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// Tool-pair normalizer: wire-boundary safety net for Anthropic's
|
|
167
|
+
// structural rules on tool cycles. See `normalize-tool-pairs.ts`
|
|
168
|
+
// for the full rationale. Runs BEFORE mergeConsecutiveRoles so the
|
|
169
|
+
// merge sees role-correct envelopes.
|
|
170
|
+
const normalized = normalizeToolPairs(providerMessages, {
|
|
171
|
+
pendingToolCallIds: options.pendingToolCallIds,
|
|
172
|
+
onEvent: options.onNormalize,
|
|
173
|
+
});
|
|
174
|
+
// Merge consecutive same-role messages (API requires alternating)
|
|
175
|
+
const mergedMessages = this.mergeConsecutiveRoles(normalized.messages);
|
|
176
|
+
// Build system content with optional cache control
|
|
177
|
+
let systemContent;
|
|
178
|
+
if (typeof systemPrompt === 'string') {
|
|
179
|
+
if (cacheControl) {
|
|
180
|
+
// Must use array format for cache_control support
|
|
181
|
+
systemContent = [{ type: 'text', text: systemPrompt, cache_control: cacheControl }];
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
systemContent = systemPrompt;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
else if (Array.isArray(systemPrompt)) {
|
|
188
|
+
if (cacheControl && systemPrompt.length > 0) {
|
|
189
|
+
// Add cache_control to the last system block
|
|
190
|
+
systemContent = systemPrompt.map((block, idx) => {
|
|
191
|
+
if (idx === systemPrompt.length - 1) {
|
|
192
|
+
return { ...block, cache_control: cacheControl };
|
|
193
|
+
}
|
|
194
|
+
return block;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
systemContent = systemPrompt;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Native tools
|
|
202
|
+
const nativeTools = tools?.length ? this.convertToNativeTools(tools) : undefined;
|
|
203
|
+
return {
|
|
204
|
+
messages: mergedMessages,
|
|
205
|
+
systemContent,
|
|
206
|
+
stopSequences: additionalStopSequences ?? [],
|
|
207
|
+
nativeTools,
|
|
208
|
+
ready: normalized.ready,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
formatToolResults(results) {
|
|
212
|
+
// Native mode uses API tool_result blocks, not string formatting
|
|
213
|
+
// This method is mainly for prefill modes
|
|
214
|
+
return JSON.stringify(results.map(r => ({
|
|
215
|
+
tool_use_id: r.toolUseId,
|
|
216
|
+
content: r.content,
|
|
217
|
+
is_error: r.isError,
|
|
218
|
+
})));
|
|
219
|
+
}
|
|
220
|
+
// ==========================================================================
|
|
221
|
+
// RESPONSE PARSING
|
|
222
|
+
// ==========================================================================
|
|
223
|
+
createStreamParser() {
|
|
224
|
+
return new PassthroughParser();
|
|
225
|
+
}
|
|
226
|
+
parseToolCalls(content) {
|
|
227
|
+
// Native mode gets tool calls from API response, not from content parsing
|
|
228
|
+
// Return empty - tool calls come through the native API response
|
|
229
|
+
return [];
|
|
230
|
+
}
|
|
231
|
+
hasToolUse(content) {
|
|
232
|
+
// Native mode determines tool use from API stop_reason, not content
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
parseContentBlocks(content) {
|
|
236
|
+
// Native mode - content is plain text
|
|
237
|
+
if (!content.trim()) {
|
|
238
|
+
return [];
|
|
239
|
+
}
|
|
240
|
+
return [{ type: 'text', text: content }];
|
|
241
|
+
}
|
|
242
|
+
// ==========================================================================
|
|
243
|
+
// PRIVATE HELPERS
|
|
244
|
+
// ==========================================================================
|
|
245
|
+
convertContent(content, participant, options) {
|
|
246
|
+
const result = [];
|
|
247
|
+
let hasUnsupportedMedia = false;
|
|
248
|
+
for (const block of content) {
|
|
249
|
+
if (block.type === 'text') {
|
|
250
|
+
let text = block.text;
|
|
251
|
+
if (options.includeNames) {
|
|
252
|
+
const prefix = this.config.nameFormat.replace('{name}', participant);
|
|
253
|
+
text = prefix + text;
|
|
254
|
+
}
|
|
255
|
+
const textBlock = { type: 'text', text };
|
|
256
|
+
if (block.cache_control) {
|
|
257
|
+
textBlock.cache_control = block.cache_control;
|
|
258
|
+
}
|
|
259
|
+
result.push(textBlock);
|
|
260
|
+
}
|
|
261
|
+
else if (block.type === 'image') {
|
|
262
|
+
if (block.source.type === 'base64') {
|
|
263
|
+
const imageBlock = {
|
|
264
|
+
type: 'image',
|
|
265
|
+
source: {
|
|
266
|
+
type: 'base64',
|
|
267
|
+
media_type: block.source.mediaType,
|
|
268
|
+
data: block.source.data,
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
// Preserve sourceUrl for providers that use URL-as-text (Gemini 3.x)
|
|
272
|
+
if (block.sourceUrl) {
|
|
273
|
+
imageBlock.sourceUrl = block.sourceUrl;
|
|
274
|
+
}
|
|
275
|
+
result.push(imageBlock);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
else if (block.type === 'tool_use') {
|
|
279
|
+
result.push({
|
|
280
|
+
type: 'tool_use',
|
|
281
|
+
id: block.id,
|
|
282
|
+
name: block.name,
|
|
283
|
+
input: block.input,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
else if (block.type === 'tool_result') {
|
|
287
|
+
result.push({
|
|
288
|
+
type: 'tool_result',
|
|
289
|
+
tool_use_id: block.toolUseId,
|
|
290
|
+
content: block.content,
|
|
291
|
+
is_error: block.isError,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
else if (block.type === 'thinking') {
|
|
295
|
+
result.push({
|
|
296
|
+
type: 'thinking',
|
|
297
|
+
thinking: block.thinking,
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
else if (block.type === 'document' || block.type === 'audio') {
|
|
301
|
+
hasUnsupportedMedia = true;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (hasUnsupportedMedia) {
|
|
305
|
+
if (this.config.unsupportedMedia === 'error') {
|
|
306
|
+
throw new Error(`NativeFormatter: unsupported media type in content. Configure unsupportedMedia: 'strip' to ignore.`);
|
|
307
|
+
}
|
|
308
|
+
else if (this.config.warnOnStrip) {
|
|
309
|
+
console.warn(`[NativeFormatter] Stripped unsupported media from message`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
mergeConsecutiveRoles(messages) {
|
|
315
|
+
if (messages.length === 0)
|
|
316
|
+
return [];
|
|
317
|
+
const merged = [];
|
|
318
|
+
let current = messages[0];
|
|
319
|
+
for (let i = 1; i < messages.length; i++) {
|
|
320
|
+
const next = messages[i];
|
|
321
|
+
if (next.role === current.role) {
|
|
322
|
+
// Merge content arrays
|
|
323
|
+
const currentContent = Array.isArray(current.content) ? current.content : [current.content];
|
|
324
|
+
const nextContent = Array.isArray(next.content) ? next.content : [next.content];
|
|
325
|
+
current = {
|
|
326
|
+
role: current.role,
|
|
327
|
+
content: [...currentContent, ...nextContent],
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
else {
|
|
331
|
+
merged.push(current);
|
|
332
|
+
current = next;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
merged.push(current);
|
|
336
|
+
return merged;
|
|
337
|
+
}
|
|
338
|
+
convertToNativeTools(tools) {
|
|
339
|
+
return tools.map(tool => ({
|
|
340
|
+
name: tool.name,
|
|
341
|
+
description: tool.description,
|
|
342
|
+
input_schema: tool.inputSchema,
|
|
343
|
+
}));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
//# sourceMappingURL=native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.js","sourceRoot":"","sources":["../../src/formatters/native.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAqBH,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAc/D,+EAA+E;AAC/E,6BAA6B;AAC7B,+EAA+E;AAE/E;;;GAGG;AACH,MAAM,iBAAiB;IACb,WAAW,GAAG,EAAE,CAAC;IACjB,UAAU,GAAG,CAAC,CAAC;IACf,YAAY,GAAG,KAAK,CAAC;IAE7B,YAAY,CAAC,KAAa;QACxB,IAAI,CAAC,WAAW,IAAI,KAAK,CAAC;QAC1B,MAAM,IAAI,GAAG;YACX,IAAI,EAAE,MAAe;YACrB,OAAO,EAAE,IAAI;YACb,UAAU,EAAE,IAAI,CAAC,UAAU;SAC5B,CAAC;QAEF,MAAM,SAAS,GAAqB,EAAE,CAAC;QACvC,MAAM,WAAW,GAAiB,EAAE,CAAC;QAErC,kCAAkC;QAClC,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACvB,MAAM,UAAU,GAAe;gBAC7B,KAAK,EAAE,aAAa;gBACpB,KAAK,EAAE,IAAI,CAAC,UAAU;gBACtB,KAAK,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE;aACxB,CAAC;YACF,SAAS,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,UAAU,EAAE,CAAC,CAAC;YAC1D,WAAW,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC7B,IAAI,CAAC,YAAY,GAAG,IAAI,CAAC;QAC3B,CAAC;QAED,SAA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,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;gBAC5F,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAChF,OAAO,GAAG;oBACR,IAAI,EAAE,OAAO,CAAC,IAAI;oBAClB,OAAO,EAAE,CAAC,GAAG,cAAc,EAAE,GAAG,WAAW,CAAC;iBAC7C,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBACrB,OAAO,GAAG,IAAI,CAAC;YACjB,CAAC;QACH,CAAC;QAED,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACrB,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,oBAAoB,CAAC,KAAuB;QAClD,OAAO,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YACxB,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,WAAW,EAAE,IAAI,CAAC,WAAW;YAC7B,YAAY,EAAE,IAAI,CAAC,WAAW;SAC/B,CAAC,CAAC,CAAC;IACN,CAAC;CACF"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool-Pair Normalizer
|
|
3
|
+
*
|
|
4
|
+
* Anthropic's API enforces structural rules on tool cycles that any of
|
|
5
|
+
* Membrane's upstreams can accidentally violate:
|
|
6
|
+
*
|
|
7
|
+
* - `tool_use` blocks must live in assistant-role messages.
|
|
8
|
+
* - `tool_result` blocks must live in user-role messages.
|
|
9
|
+
* - Every `tool_use` must be matched by its `tool_result` in the very
|
|
10
|
+
* next user-role message.
|
|
11
|
+
* - `thinking` blocks must live in assistant turns.
|
|
12
|
+
*
|
|
13
|
+
* When these are violated, the API returns 400 (e.g. `tool_use blocks can
|
|
14
|
+
* only be in assistant messages`). This module is the wire-boundary safety
|
|
15
|
+
* net: every formatter funnels through `normalizeToolPairs` before its
|
|
16
|
+
* output is shipped, so producer-side bugs cannot leak the same 400 family
|
|
17
|
+
* (compression-bug 5/6/7/8/9, agent-framework #37, 2026-05-22 miner stall).
|
|
18
|
+
*
|
|
19
|
+
* Algorithm overview (six phases): reclassify blocks by required role,
|
|
20
|
+
* reflow into role-correct envelopes, hoist matching tool_results across
|
|
21
|
+
* the assistant→user boundary, evict interlopers wedged between use and
|
|
22
|
+
* result, synthesize `[pending]` results for trailing orphans (or signal
|
|
23
|
+
* not-ready when the id is in the caller-supplied pending set), validate.
|
|
24
|
+
*/
|
|
25
|
+
import type { ProviderMessage as LooseProviderMessage } from './types.js';
|
|
26
|
+
import type { NormalizeEvent } from './types.js';
|
|
27
|
+
/**
|
|
28
|
+
* Block shape used internally and exposed for callers that want to
|
|
29
|
+
* build inputs without the full Anthropic SDK types. The required
|
|
30
|
+
* `type` discriminator names the kind of block; any block whose `type`
|
|
31
|
+
* matches a strict-role entry in `requiredRoleOf` is re-roled to its
|
|
32
|
+
* required role during normalization. Unrecognized `tool_*` or
|
|
33
|
+
* `thinking*` types fall through as `inherit` — see the one-shot
|
|
34
|
+
* warning below.
|
|
35
|
+
*/
|
|
36
|
+
export type ProviderBlock = Record<string, unknown> & {
|
|
37
|
+
type: string;
|
|
38
|
+
};
|
|
39
|
+
export interface NormalizeOptions {
|
|
40
|
+
/** See `BuildOptions.pendingToolCallIds`. */
|
|
41
|
+
pendingToolCallIds?: ReadonlySet<string>;
|
|
42
|
+
/** See `BuildOptions.onNormalize`. */
|
|
43
|
+
onEvent?: (event: NormalizeEvent) => void;
|
|
44
|
+
}
|
|
45
|
+
export interface NormalizeResult {
|
|
46
|
+
/**
|
|
47
|
+
* Normalized messages, structurally compatible with the loose
|
|
48
|
+
* `ProviderMessage` from `./types.js`. Block contents are
|
|
49
|
+
* `ProviderBlock[]` at runtime; the loose type is preserved at the
|
|
50
|
+
* public boundary so callers wired against `./types.js` don't need
|
|
51
|
+
* to cast.
|
|
52
|
+
*/
|
|
53
|
+
messages: LooseProviderMessage[];
|
|
54
|
+
/**
|
|
55
|
+
* `false` iff a trailing unmatched tool_use's id was in
|
|
56
|
+
* `pendingToolCallIds`. Caller should wait for the in-flight result
|
|
57
|
+
* to land and retry instead of shipping the request.
|
|
58
|
+
*/
|
|
59
|
+
ready: boolean;
|
|
60
|
+
}
|
|
61
|
+
export declare class MembraneNormalizerError extends Error {
|
|
62
|
+
readonly input: ReadonlyArray<LooseProviderMessage>;
|
|
63
|
+
readonly output: ReadonlyArray<LooseProviderMessage>;
|
|
64
|
+
constructor(message: string, input: ReadonlyArray<LooseProviderMessage>, output: ReadonlyArray<LooseProviderMessage>);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Normalize a sequence of provider messages so the output is API-valid
|
|
68
|
+
* with respect to Anthropic's tool-cycle structural rules.
|
|
69
|
+
*
|
|
70
|
+
* This function does NOT merge consecutive same-role envelopes — that
|
|
71
|
+
* remains the caller's responsibility (NativeFormatter.mergeConsecutiveRoles)
|
|
72
|
+
* so existing cache-control / breakpoint logic continues to work.
|
|
73
|
+
*/
|
|
74
|
+
export declare function normalizeToolPairs(input: ReadonlyArray<LooseProviderMessage>, options?: NormalizeOptions): NormalizeResult;
|
|
75
|
+
//# sourceMappingURL=normalize-tool-pairs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalize-tool-pairs.d.ts","sourceRoot":"","sources":["../../src/formatters/normalize-tool-pairs.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AAEH,OAAO,KAAK,EAAE,eAAe,IAAI,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAC1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAMjD;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvE,MAAM,WAAW,gBAAgB;IAC/B,6CAA6C;IAC7C,kBAAkB,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACzC,sCAAsC;IACtC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;CAC3C;AAED,MAAM,WAAW,eAAe;IAC9B;;;;;;OAMG;IACH,QAAQ,EAAE,oBAAoB,EAAE,CAAC;IACjC;;;;OAIG;IACH,KAAK,EAAE,OAAO,CAAC;CAChB;AAED,qBAAa,uBAAwB,SAAQ,KAAK;aAG9B,KAAK,EAAE,aAAa,CAAC,oBAAoB,CAAC;aAC1C,MAAM,EAAE,aAAa,CAAC,oBAAoB,CAAC;gBAF3D,OAAO,EAAE,MAAM,EACC,KAAK,EAAE,aAAa,CAAC,oBAAoB,CAAC,EAC1C,MAAM,EAAE,aAAa,CAAC,oBAAoB,CAAC;CAK9D;AAMD;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,KAAK,EAAE,aAAa,CAAC,oBAAoB,CAAC,EAC1C,OAAO,GAAE,gBAAqB,GAC7B,eAAe,CA+DjB"}
|