@animalabs/membrane 0.3.2 → 0.5.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/formatters/anthropic-xml.d.ts +63 -0
- package/dist/formatters/anthropic-xml.d.ts.map +1 -0
- package/dist/formatters/anthropic-xml.js +365 -0
- package/dist/formatters/anthropic-xml.js.map +1 -0
- package/dist/formatters/completions.d.ts +61 -0
- package/dist/formatters/completions.d.ts.map +1 -0
- package/dist/formatters/completions.js +224 -0
- package/dist/formatters/completions.js.map +1 -0
- package/dist/formatters/index.d.ts +8 -0
- package/dist/formatters/index.d.ts.map +1 -0
- package/dist/formatters/index.js +7 -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 -0
- package/dist/formatters/native.js +261 -0
- package/dist/formatters/native.js.map +1 -0
- package/dist/formatters/types.d.ts +152 -0
- package/dist/formatters/types.d.ts.map +1 -0
- package/dist/formatters/types.js +7 -0
- package/dist/formatters/types.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/membrane.d.ts +4 -0
- package/dist/membrane.d.ts.map +1 -1
- package/dist/membrane.js +40 -32
- package/dist/membrane.js.map +1 -1
- package/dist/providers/anthropic.d.ts.map +1 -1
- package/dist/providers/anthropic.js +3 -2
- package/dist/providers/anthropic.js.map +1 -1
- package/dist/transforms/index.d.ts +0 -1
- package/dist/transforms/index.d.ts.map +1 -1
- package/dist/transforms/index.js +2 -1
- package/dist/transforms/index.js.map +1 -1
- package/dist/types/config.d.ts +7 -0
- package/dist/types/config.d.ts.map +1 -1
- package/dist/types/config.js.map +1 -1
- package/dist/types/streaming.d.ts +6 -0
- package/dist/types/streaming.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/formatters/anthropic-xml.ts +490 -0
- package/src/formatters/completions.ts +343 -0
- package/src/formatters/index.ts +19 -0
- package/src/formatters/native.ts +340 -0
- package/src/formatters/types.ts +229 -0
- package/src/index.ts +3 -0
- package/src/membrane.ts +59 -45
- package/src/providers/anthropic.ts +3 -2
- package/src/transforms/index.ts +2 -10
- package/src/types/config.ts +9 -1
- package/src/types/streaming.ts +9 -0
- package/src/transforms/prefill.ts +0 -574
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic XML Formatter
|
|
3
|
+
*
|
|
4
|
+
* Prefill-based formatting for Anthropic models using XML tool syntax.
|
|
5
|
+
* This is the "classic" membrane format with:
|
|
6
|
+
* - Participant: content format
|
|
7
|
+
* - <function_calls>/<function_results> for tools
|
|
8
|
+
* - <thinking> blocks for extended thinking
|
|
9
|
+
*/
|
|
10
|
+
import type { NormalizedMessage, ContentBlock, ToolCall, ToolResult } from '../types/index.js';
|
|
11
|
+
import type { PrefillFormatter, StreamParser, BuildOptions, BuildResult, FormatterConfig } from './types.js';
|
|
12
|
+
export interface AnthropicXmlFormatterConfig extends FormatterConfig {
|
|
13
|
+
/**
|
|
14
|
+
* How to handle tool definitions:
|
|
15
|
+
* - 'xml': Inject into conversation as XML (prefill mode)
|
|
16
|
+
* - 'native': Pass to API as native tools
|
|
17
|
+
* Default: 'xml'
|
|
18
|
+
*/
|
|
19
|
+
toolMode?: 'xml' | 'native';
|
|
20
|
+
/**
|
|
21
|
+
* Where to inject tool definitions when toolMode is 'xml':
|
|
22
|
+
* - 'conversation': Inject into assistant content N messages from end
|
|
23
|
+
* - 'system': Inject into system prompt
|
|
24
|
+
* Default: 'conversation'
|
|
25
|
+
*/
|
|
26
|
+
toolInjectionMode?: 'conversation' | 'system';
|
|
27
|
+
/**
|
|
28
|
+
* Position to inject tools (from end of messages).
|
|
29
|
+
* Default: 10
|
|
30
|
+
*/
|
|
31
|
+
toolInjectionPosition?: number;
|
|
32
|
+
/**
|
|
33
|
+
* Message delimiter for base models (e.g., '</s>').
|
|
34
|
+
* Default: '' (none)
|
|
35
|
+
*/
|
|
36
|
+
messageDelimiter?: string;
|
|
37
|
+
/**
|
|
38
|
+
* Maximum participants to include in stop sequences.
|
|
39
|
+
* Default: 10
|
|
40
|
+
*/
|
|
41
|
+
maxParticipantsForStop?: number;
|
|
42
|
+
}
|
|
43
|
+
export declare class AnthropicXmlFormatter implements PrefillFormatter {
|
|
44
|
+
readonly name = "anthropic-xml";
|
|
45
|
+
readonly usesPrefill = true;
|
|
46
|
+
private config;
|
|
47
|
+
constructor(config?: AnthropicXmlFormatterConfig);
|
|
48
|
+
buildMessages(messages: NormalizedMessage[], options: BuildOptions): BuildResult;
|
|
49
|
+
formatToolResults(results: ToolResult[], options?: {
|
|
50
|
+
thinking?: boolean;
|
|
51
|
+
}): string;
|
|
52
|
+
createStreamParser(): StreamParser;
|
|
53
|
+
parseToolCalls(content: string): ToolCall[];
|
|
54
|
+
hasToolUse(content: string): boolean;
|
|
55
|
+
parseContentBlocks(content: string): ContentBlock[];
|
|
56
|
+
private extractContent;
|
|
57
|
+
private formatToolDefinitionsXml;
|
|
58
|
+
private formatToolsForInjection;
|
|
59
|
+
private injectToolsIntoSystem;
|
|
60
|
+
private buildStopSequences;
|
|
61
|
+
private convertToNativeTools;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=anthropic-xml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-xml.d.ts","sourceRoot":"","sources":["../../src/formatters/anthropic-xml.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,iBAAiB,EACjB,YAAY,EAEZ,QAAQ,EACR,UAAU,EACX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,eAAe,EAEhB,MAAM,YAAY,CAAC;AAcpB,MAAM,WAAW,2BAA4B,SAAQ,eAAe;IAClE;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;IAE5B;;;;;OAKG;IACH,iBAAiB,CAAC,EAAE,cAAc,GAAG,QAAQ,CAAC;IAE9C;;;OAGG;IACH,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAE/B;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;CACjC;AAMD,qBAAa,qBAAsB,YAAW,gBAAgB;IAC5D,QAAQ,CAAC,IAAI,mBAAmB;IAChC,QAAQ,CAAC,WAAW,QAAQ;IAE5B,OAAO,CAAC,MAAM,CAAwC;gBAE1C,MAAM,GAAE,2BAAgC;IAgBpD,aAAa,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,OAAO,EAAE,YAAY,GAAG,WAAW;IAwNhF,iBAAiB,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM;IAYlF,kBAAkB,IAAI,YAAY;IAIlC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,QAAQ,EAAE;IAK3C,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO;IAIpC,kBAAkB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,EAAE;IASnD,OAAO,CAAC,cAAc;IAqCtB,OAAO,CAAC,wBAAwB;IAoBhC,OAAO,CAAC,uBAAuB;IAwB/B,OAAO,CAAC,qBAAqB;IAgB7B,OAAO,CAAC,kBAAkB;IAmC1B,OAAO,CAAC,oBAAoB;CAO7B"}
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Anthropic XML Formatter
|
|
3
|
+
*
|
|
4
|
+
* Prefill-based formatting for Anthropic models using XML tool syntax.
|
|
5
|
+
* This is the "classic" membrane format with:
|
|
6
|
+
* - Participant: content format
|
|
7
|
+
* - <function_calls>/<function_results> for tools
|
|
8
|
+
* - <thinking> blocks for extended thinking
|
|
9
|
+
*/
|
|
10
|
+
import { parseToolCalls as parseToolCallsXml, formatToolResults as formatToolResultsXml, parseAccumulatedIntoBlocks, formatToolDefinitions, } from '../utils/tool-parser.js';
|
|
11
|
+
import { IncrementalXmlParser } from '../utils/stream-parser.js';
|
|
12
|
+
// ============================================================================
|
|
13
|
+
// Anthropic XML Formatter
|
|
14
|
+
// ============================================================================
|
|
15
|
+
export class AnthropicXmlFormatter {
|
|
16
|
+
name = 'anthropic-xml';
|
|
17
|
+
usesPrefill = true;
|
|
18
|
+
config;
|
|
19
|
+
constructor(config = {}) {
|
|
20
|
+
this.config = {
|
|
21
|
+
toolMode: config.toolMode ?? 'xml',
|
|
22
|
+
toolInjectionMode: config.toolInjectionMode ?? 'conversation',
|
|
23
|
+
toolInjectionPosition: config.toolInjectionPosition ?? 10,
|
|
24
|
+
messageDelimiter: config.messageDelimiter ?? '',
|
|
25
|
+
maxParticipantsForStop: config.maxParticipantsForStop ?? 10,
|
|
26
|
+
unsupportedMedia: config.unsupportedMedia ?? 'error',
|
|
27
|
+
warnOnStrip: config.warnOnStrip ?? true,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// ==========================================================================
|
|
31
|
+
// REQUEST BUILDING
|
|
32
|
+
// ==========================================================================
|
|
33
|
+
buildMessages(messages, options) {
|
|
34
|
+
const { assistantParticipant, tools, thinking, systemPrompt, promptCaching = false, contextPrefix, hasCacheMarker, } = options;
|
|
35
|
+
const providerMessages = [];
|
|
36
|
+
const joiner = this.config.messageDelimiter ? '' : '\n';
|
|
37
|
+
// Track conversation state
|
|
38
|
+
let currentConversation = [];
|
|
39
|
+
let lastNonEmptyParticipant = null;
|
|
40
|
+
// Track cache marker state - everything BEFORE we see the marker gets cache_control
|
|
41
|
+
let passedCacheMarker = false;
|
|
42
|
+
let cacheMarkersApplied = 0;
|
|
43
|
+
// Calculate tool injection point
|
|
44
|
+
const totalMessages = messages.length;
|
|
45
|
+
const toolInjectionIndex = Math.max(0, totalMessages - this.config.toolInjectionPosition);
|
|
46
|
+
let toolsInjected = false;
|
|
47
|
+
const hasToolsForConversation = this.config.toolMode === 'xml' &&
|
|
48
|
+
this.config.toolInjectionMode === 'conversation' &&
|
|
49
|
+
tools &&
|
|
50
|
+
tools.length > 0;
|
|
51
|
+
const toolsText = hasToolsForConversation ? this.formatToolsForInjection(tools) : '';
|
|
52
|
+
// Build system content
|
|
53
|
+
let systemText = typeof systemPrompt === 'string' ? systemPrompt : '';
|
|
54
|
+
if (Array.isArray(systemPrompt)) {
|
|
55
|
+
systemText = systemPrompt
|
|
56
|
+
.filter((b) => b.type === 'text')
|
|
57
|
+
.map(b => b.text)
|
|
58
|
+
.join('\n');
|
|
59
|
+
}
|
|
60
|
+
// Inject tools into system if configured
|
|
61
|
+
if (this.config.toolMode === 'xml' && this.config.toolInjectionMode === 'system' && tools?.length) {
|
|
62
|
+
const toolsXml = this.formatToolDefinitionsXml(tools);
|
|
63
|
+
systemText = this.injectToolsIntoSystem(systemText, toolsXml);
|
|
64
|
+
}
|
|
65
|
+
// Build system content with optional cache control
|
|
66
|
+
let systemContent;
|
|
67
|
+
if (systemText) {
|
|
68
|
+
const systemBlock = { type: 'text', text: systemText };
|
|
69
|
+
if (promptCaching) {
|
|
70
|
+
systemBlock.cache_control = { type: 'ephemeral' };
|
|
71
|
+
cacheMarkersApplied++;
|
|
72
|
+
}
|
|
73
|
+
systemContent = [systemBlock];
|
|
74
|
+
}
|
|
75
|
+
// Add context prefix as first cached assistant message (for simulacrum seeding)
|
|
76
|
+
if (contextPrefix) {
|
|
77
|
+
const prefixBlock = { type: 'text', text: contextPrefix };
|
|
78
|
+
if (promptCaching) {
|
|
79
|
+
prefixBlock.cache_control = { type: 'ephemeral' };
|
|
80
|
+
cacheMarkersApplied++;
|
|
81
|
+
}
|
|
82
|
+
providerMessages.push({
|
|
83
|
+
role: 'assistant',
|
|
84
|
+
content: [prefixBlock],
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
// Process messages
|
|
88
|
+
for (let i = 0; i < messages.length; i++) {
|
|
89
|
+
const message = messages[i];
|
|
90
|
+
if (!message)
|
|
91
|
+
continue;
|
|
92
|
+
const isLastMessage = i === messages.length - 1;
|
|
93
|
+
const isAssistant = message.participant === assistantParticipant;
|
|
94
|
+
// Extract content
|
|
95
|
+
const { text, images, hasUnsupportedMedia } = this.extractContent(message.content, message.participant);
|
|
96
|
+
const hasImages = images.length > 0;
|
|
97
|
+
const isEmpty = !text.trim() && !hasImages;
|
|
98
|
+
// Handle unsupported media
|
|
99
|
+
if (hasUnsupportedMedia) {
|
|
100
|
+
if (this.config.unsupportedMedia === 'error') {
|
|
101
|
+
throw new Error(`AnthropicXmlFormatter does not support media in message from ${message.participant}. Configure unsupportedMedia: 'strip' to ignore.`);
|
|
102
|
+
}
|
|
103
|
+
else if (this.config.warnOnStrip) {
|
|
104
|
+
console.warn(`[AnthropicXmlFormatter] Stripped unsupported media from message`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Check for tool results
|
|
108
|
+
const hasToolResult = message.content.some(c => c.type === 'tool_result');
|
|
109
|
+
// If message has images, flush and add as user turn
|
|
110
|
+
if (hasImages && !isEmpty) {
|
|
111
|
+
if (currentConversation.length > 0) {
|
|
112
|
+
providerMessages.push({
|
|
113
|
+
role: 'assistant',
|
|
114
|
+
content: currentConversation.join(joiner),
|
|
115
|
+
});
|
|
116
|
+
currentConversation = [];
|
|
117
|
+
}
|
|
118
|
+
const userContent = [];
|
|
119
|
+
if (text) {
|
|
120
|
+
userContent.push({ type: 'text', text: `${message.participant}: ${text}` });
|
|
121
|
+
}
|
|
122
|
+
userContent.push(...images);
|
|
123
|
+
providerMessages.push({ role: 'user', content: userContent });
|
|
124
|
+
lastNonEmptyParticipant = message.participant;
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
// Skip empty messages except last
|
|
128
|
+
if (isEmpty && !isLastMessage) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
// Check if this message has a cache marker - flush content BEFORE it with cache_control
|
|
132
|
+
if (hasCacheMarker && !passedCacheMarker && hasCacheMarker(message, i)) {
|
|
133
|
+
// Flush everything before this message WITH cache_control (if caching enabled)
|
|
134
|
+
if (currentConversation.length > 0) {
|
|
135
|
+
const content = currentConversation.join(joiner);
|
|
136
|
+
if (promptCaching) {
|
|
137
|
+
const contentBlock = { type: 'text', text: content };
|
|
138
|
+
contentBlock.cache_control = { type: 'ephemeral' };
|
|
139
|
+
cacheMarkersApplied++;
|
|
140
|
+
providerMessages.push({
|
|
141
|
+
role: 'assistant',
|
|
142
|
+
content: [contentBlock],
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
providerMessages.push({
|
|
147
|
+
role: 'assistant',
|
|
148
|
+
content: content,
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
currentConversation = [];
|
|
152
|
+
}
|
|
153
|
+
passedCacheMarker = true;
|
|
154
|
+
}
|
|
155
|
+
// Inject tools before this message if at injection point
|
|
156
|
+
const shouldInjectHere = toolInjectionIndex > 0 ? i >= toolInjectionIndex : i === 0;
|
|
157
|
+
if (hasToolsForConversation && !toolsInjected && shouldInjectHere) {
|
|
158
|
+
currentConversation.push(toolsText);
|
|
159
|
+
toolsInjected = true;
|
|
160
|
+
}
|
|
161
|
+
// Check bot continuation
|
|
162
|
+
const isBotMessage = message.participant === assistantParticipant;
|
|
163
|
+
const isContinuation = isBotMessage && lastNonEmptyParticipant === assistantParticipant && !hasToolResult;
|
|
164
|
+
if (isContinuation && isLastMessage) {
|
|
165
|
+
// Bot continuation - don't add prefix
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
else if (isLastMessage && isEmpty) {
|
|
169
|
+
// Completion target - prefix added below
|
|
170
|
+
}
|
|
171
|
+
else if (text) {
|
|
172
|
+
currentConversation.push(`${message.participant}: ${text}${this.config.messageDelimiter}`);
|
|
173
|
+
if (!hasToolResult) {
|
|
174
|
+
lastNonEmptyParticipant = message.participant;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Determine turn prefix
|
|
179
|
+
let turnPrefix;
|
|
180
|
+
if (thinking?.enabled) {
|
|
181
|
+
turnPrefix = `${assistantParticipant}: <thinking>`;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
turnPrefix = `${assistantParticipant}:`;
|
|
185
|
+
}
|
|
186
|
+
// Flush remaining conversation
|
|
187
|
+
if (hasToolsForConversation && !toolsInjected) {
|
|
188
|
+
currentConversation.push(toolsText);
|
|
189
|
+
}
|
|
190
|
+
if (currentConversation.length > 0) {
|
|
191
|
+
providerMessages.push({
|
|
192
|
+
role: 'assistant',
|
|
193
|
+
content: [...currentConversation, turnPrefix].join(joiner),
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
providerMessages.push({
|
|
198
|
+
role: 'assistant',
|
|
199
|
+
content: turnPrefix,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
// Build stop sequences
|
|
203
|
+
const stopSequences = this.buildStopSequences(messages, assistantParticipant, options);
|
|
204
|
+
// Native tools if configured
|
|
205
|
+
const nativeTools = this.config.toolMode === 'native' && tools?.length
|
|
206
|
+
? this.convertToNativeTools(tools)
|
|
207
|
+
: undefined;
|
|
208
|
+
return {
|
|
209
|
+
messages: providerMessages,
|
|
210
|
+
systemContent,
|
|
211
|
+
assistantPrefill: typeof providerMessages[providerMessages.length - 1]?.content === 'string'
|
|
212
|
+
? providerMessages[providerMessages.length - 1].content
|
|
213
|
+
: undefined,
|
|
214
|
+
stopSequences,
|
|
215
|
+
nativeTools,
|
|
216
|
+
cacheMarkersApplied,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
formatToolResults(results, options) {
|
|
220
|
+
let xml = formatToolResultsXml(results);
|
|
221
|
+
if (options?.thinking) {
|
|
222
|
+
xml += '\n<thinking>';
|
|
223
|
+
}
|
|
224
|
+
return xml;
|
|
225
|
+
}
|
|
226
|
+
// ==========================================================================
|
|
227
|
+
// RESPONSE PARSING
|
|
228
|
+
// ==========================================================================
|
|
229
|
+
createStreamParser() {
|
|
230
|
+
return new IncrementalXmlParser();
|
|
231
|
+
}
|
|
232
|
+
parseToolCalls(content) {
|
|
233
|
+
const result = parseToolCallsXml(content);
|
|
234
|
+
return result?.calls ?? [];
|
|
235
|
+
}
|
|
236
|
+
hasToolUse(content) {
|
|
237
|
+
return /<(antml:)?function_calls>/.test(content);
|
|
238
|
+
}
|
|
239
|
+
parseContentBlocks(content) {
|
|
240
|
+
const { blocks } = parseAccumulatedIntoBlocks(content);
|
|
241
|
+
return blocks;
|
|
242
|
+
}
|
|
243
|
+
// ==========================================================================
|
|
244
|
+
// PRIVATE HELPERS
|
|
245
|
+
// ==========================================================================
|
|
246
|
+
extractContent(content, participant) {
|
|
247
|
+
const parts = [];
|
|
248
|
+
const images = [];
|
|
249
|
+
let hasUnsupportedMedia = false;
|
|
250
|
+
for (const block of content) {
|
|
251
|
+
if (block.type === 'text') {
|
|
252
|
+
parts.push(block.text);
|
|
253
|
+
}
|
|
254
|
+
else if (block.type === 'image') {
|
|
255
|
+
if (block.source.type === 'base64') {
|
|
256
|
+
images.push({
|
|
257
|
+
type: 'image',
|
|
258
|
+
source: {
|
|
259
|
+
type: 'base64',
|
|
260
|
+
media_type: block.source.mediaType,
|
|
261
|
+
data: block.source.data,
|
|
262
|
+
},
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
else if (block.type === 'tool_use') {
|
|
267
|
+
parts.push(`${participant}>[${block.name}]: ${JSON.stringify(block.input)}`);
|
|
268
|
+
}
|
|
269
|
+
else if (block.type === 'tool_result') {
|
|
270
|
+
const resultText = typeof block.content === 'string'
|
|
271
|
+
? block.content
|
|
272
|
+
: JSON.stringify(block.content);
|
|
273
|
+
parts.push(`${participant}<[tool_result]: ${resultText}`);
|
|
274
|
+
}
|
|
275
|
+
else if (block.type === 'document' || block.type === 'audio') {
|
|
276
|
+
hasUnsupportedMedia = true;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return { text: parts.join('\n'), images, hasUnsupportedMedia };
|
|
280
|
+
}
|
|
281
|
+
formatToolDefinitionsXml(tools) {
|
|
282
|
+
const toolsForPrompt = tools.map((tool) => ({
|
|
283
|
+
name: tool.name,
|
|
284
|
+
description: tool.description,
|
|
285
|
+
parameters: Object.fromEntries(Object.entries(tool.inputSchema.properties).map(([name, schema]) => [
|
|
286
|
+
name,
|
|
287
|
+
{
|
|
288
|
+
type: schema.type,
|
|
289
|
+
description: schema.description,
|
|
290
|
+
required: tool.inputSchema.required?.includes(name),
|
|
291
|
+
enum: schema.enum,
|
|
292
|
+
},
|
|
293
|
+
])),
|
|
294
|
+
}));
|
|
295
|
+
return formatToolDefinitions(toolsForPrompt);
|
|
296
|
+
}
|
|
297
|
+
formatToolsForInjection(tools) {
|
|
298
|
+
const toolsXml = this.formatToolDefinitionsXml(tools);
|
|
299
|
+
// Assemble tags to avoid triggering stop sequences
|
|
300
|
+
const FUNC_CALLS_OPEN = '<' + 'function_calls>';
|
|
301
|
+
const FUNC_CALLS_CLOSE = '</' + 'function_calls>';
|
|
302
|
+
const INVOKE_OPEN = '<' + 'invoke name="';
|
|
303
|
+
const INVOKE_CLOSE = '</' + 'invoke>';
|
|
304
|
+
const PARAM_OPEN = '<' + 'parameter name="';
|
|
305
|
+
const PARAM_CLOSE = '</' + 'parameter>';
|
|
306
|
+
return `
|
|
307
|
+
<available_tools>
|
|
308
|
+
${toolsXml}
|
|
309
|
+
</available_tools>
|
|
310
|
+
|
|
311
|
+
When you want to use a tool, output:
|
|
312
|
+
${FUNC_CALLS_OPEN}
|
|
313
|
+
${INVOKE_OPEN}tool_name">
|
|
314
|
+
${PARAM_OPEN}param_name">value${PARAM_CLOSE}
|
|
315
|
+
${INVOKE_CLOSE}
|
|
316
|
+
${FUNC_CALLS_CLOSE}`;
|
|
317
|
+
}
|
|
318
|
+
injectToolsIntoSystem(system, toolsXml) {
|
|
319
|
+
const toolsSection = `
|
|
320
|
+
<available_tools>
|
|
321
|
+
${toolsXml}
|
|
322
|
+
</available_tools>
|
|
323
|
+
|
|
324
|
+
When you want to use a tool, output:
|
|
325
|
+
<function_calls>
|
|
326
|
+
<invoke name="tool_name">
|
|
327
|
+
<parameter name="param_name">value</parameter>
|
|
328
|
+
</invoke>
|
|
329
|
+
</function_calls>
|
|
330
|
+
`;
|
|
331
|
+
return system + '\n\n' + toolsSection;
|
|
332
|
+
}
|
|
333
|
+
buildStopSequences(messages, assistantName, options) {
|
|
334
|
+
const sequences = [];
|
|
335
|
+
// Use option's maxParticipantsForStop, falling back to config
|
|
336
|
+
const maxParticipants = options.maxParticipantsForStop ?? this.config.maxParticipantsForStop;
|
|
337
|
+
// Collect unique participants (excluding assistant)
|
|
338
|
+
const participants = new Set();
|
|
339
|
+
for (let i = messages.length - 1; i >= 0 && participants.size < maxParticipants; i--) {
|
|
340
|
+
const message = messages[i];
|
|
341
|
+
if (message && message.participant !== assistantName) {
|
|
342
|
+
participants.add(message.participant);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
// Participant-based stops
|
|
346
|
+
for (const participant of participants) {
|
|
347
|
+
sequences.push(`\n${participant}:`);
|
|
348
|
+
}
|
|
349
|
+
// Tool-related stop
|
|
350
|
+
sequences.push('</function_calls>');
|
|
351
|
+
// Add any additional stop sequences from options
|
|
352
|
+
if (options.additionalStopSequences?.length) {
|
|
353
|
+
sequences.push(...options.additionalStopSequences);
|
|
354
|
+
}
|
|
355
|
+
return sequences;
|
|
356
|
+
}
|
|
357
|
+
convertToNativeTools(tools) {
|
|
358
|
+
return tools.map(tool => ({
|
|
359
|
+
name: tool.name,
|
|
360
|
+
description: tool.description,
|
|
361
|
+
input_schema: tool.inputSchema,
|
|
362
|
+
}));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
//# sourceMappingURL=anthropic-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AAC,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,YAAY,CAAC,IAAI,GAAG,eAAe,EAAE,CAAC,EAAE,EAAE,CAAC;YACrF,MAAM,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;YAC5B,IAAI,OAAO,IAAI,OAAO,CAAC,WAAW,KAAK,aAAa,EAAE,CAAC;gBACrD,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;QAED,0BAA0B;QAC1B,KAAK,MAAM,WAAW,IAAI,YAAY,EAAE,CAAC;YACvC,SAAS,CAAC,IAAI,CAAC,KAAK,WAAW,GAAG,CAAC,CAAC;QACtC,CAAC;QAED,oBAAoB;QACpB,SAAS,CAAC,IAAI,CAAC,mBAAmB,CAAC,CAAC;QAEpC,iDAAiD;QACjD,IAAI,OAAO,CAAC,uBAAuB,EAAE,MAAM,EAAE,CAAC;YAC5C,SAAS,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;QACrD,CAAC;QAED,OAAO,SAAS,CAAC;IACnB,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,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Completions Formatter
|
|
3
|
+
*
|
|
4
|
+
* Formatter for base/completion models that use single-prompt input.
|
|
5
|
+
* Serializes conversations to "Participant: content<eot>" format.
|
|
6
|
+
*
|
|
7
|
+
* Key features:
|
|
8
|
+
* - Converts multi-turn conversations to single prompt string
|
|
9
|
+
* - Adds configurable end-of-turn tokens
|
|
10
|
+
* - Generates stop sequences from participant names
|
|
11
|
+
* - Strips images (not supported in completion models)
|
|
12
|
+
* - No XML block parsing (passthrough mode)
|
|
13
|
+
*/
|
|
14
|
+
import type { NormalizedMessage, ContentBlock, ToolCall, ToolResult } from '../types/index.js';
|
|
15
|
+
import type { PrefillFormatter, StreamParser, BuildOptions, BuildResult, FormatterConfig } from './types.js';
|
|
16
|
+
export interface CompletionsFormatterConfig extends FormatterConfig {
|
|
17
|
+
/**
|
|
18
|
+
* End-of-turn token to append after each message.
|
|
19
|
+
* Set to empty string to disable.
|
|
20
|
+
* Default: '<|eot|>'
|
|
21
|
+
*/
|
|
22
|
+
eotToken?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Format for participant name prefix.
|
|
25
|
+
* Use {name} as placeholder.
|
|
26
|
+
* Default: '{name}: '
|
|
27
|
+
*/
|
|
28
|
+
nameFormat?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Message separator between turns.
|
|
31
|
+
* Default: '\n\n'
|
|
32
|
+
*/
|
|
33
|
+
messageSeparator?: string;
|
|
34
|
+
/**
|
|
35
|
+
* Maximum participants to include in stop sequences.
|
|
36
|
+
* Default: 10
|
|
37
|
+
*/
|
|
38
|
+
maxParticipantsForStop?: number;
|
|
39
|
+
/**
|
|
40
|
+
* Whether to warn when images are stripped.
|
|
41
|
+
* Default: true
|
|
42
|
+
*/
|
|
43
|
+
warnOnImageStrip?: boolean;
|
|
44
|
+
}
|
|
45
|
+
export declare class CompletionsFormatter implements PrefillFormatter {
|
|
46
|
+
readonly name = "completions";
|
|
47
|
+
readonly usesPrefill = true;
|
|
48
|
+
private config;
|
|
49
|
+
constructor(config?: CompletionsFormatterConfig);
|
|
50
|
+
buildMessages(messages: NormalizedMessage[], options: BuildOptions): BuildResult;
|
|
51
|
+
formatToolResults(results: ToolResult[], options?: {
|
|
52
|
+
thinking?: boolean;
|
|
53
|
+
}): string;
|
|
54
|
+
createStreamParser(): StreamParser;
|
|
55
|
+
parseToolCalls(content: string): ToolCall[];
|
|
56
|
+
hasToolUse(content: string): boolean;
|
|
57
|
+
parseContentBlocks(content: string): ContentBlock[];
|
|
58
|
+
private extractTextContent;
|
|
59
|
+
private buildStopSequences;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=completions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"completions.d.ts","sourceRoot":"","sources":["../../src/formatters/completions.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,KAAK,EACV,iBAAiB,EACjB,YAAY,EAEZ,QAAQ,EACR,UAAU,EACX,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EACV,gBAAgB,EAChB,YAAY,EACZ,YAAY,EACZ,WAAW,EACX,eAAe,EAIhB,MAAM,YAAY,CAAC;AAMpB,MAAM,WAAW,0BAA2B,SAAQ,eAAe;IACjE;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAElB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAEhC;;;OAGG;IACH,gBAAgB,CAAC,EAAE,OAAO,CAAC;CAC5B;AA0ED,qBAAa,oBAAqB,YAAW,gBAAgB;IAC3D,QAAQ,CAAC,IAAI,iBAAiB;IAC9B,QAAQ,CAAC,WAAW,QAAQ;IAE5B,OAAO,CAAC,MAAM,CAGZ;gBAEU,MAAM,GAAE,0BAA+B;IAiBnD,aAAa,CAAC,QAAQ,EAAE,iBAAiB,EAAE,EAAE,OAAO,EAAE,YAAY,GAAG,WAAW;IA+EhF,iBAAiB,CAAC,OAAO,EAAE,UAAU,EAAE,EAAE,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,GAAG,MAAM;IAclF,kBAAkB,IAAI,YAAY;IAIl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