@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,585 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prefill mode transforms
|
|
3
|
+
*
|
|
4
|
+
* Converts normalized messages to participant-based conversation log format:
|
|
5
|
+
*
|
|
6
|
+
* Alice: Hello there!
|
|
7
|
+
*
|
|
8
|
+
* Bob: Hi Alice!
|
|
9
|
+
*
|
|
10
|
+
* Claude: [assistant continuation starts here...]
|
|
11
|
+
*
|
|
12
|
+
* Key features:
|
|
13
|
+
* - Cache control markers for Anthropic prompt caching
|
|
14
|
+
* - Image flushing (images cause conversation flush to user turn)
|
|
15
|
+
* - Tool injection into conversation
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type {
|
|
19
|
+
NormalizedMessage,
|
|
20
|
+
NormalizedRequest,
|
|
21
|
+
ContentBlock,
|
|
22
|
+
ToolDefinition,
|
|
23
|
+
CacheControl,
|
|
24
|
+
} from '../types/index.js';
|
|
25
|
+
import { isTextContent, isMediaContent } from '../types/index.js';
|
|
26
|
+
import { formatToolDefinitions, type ToolDefinitionForPrompt } from '../utils/tool-parser.js';
|
|
27
|
+
|
|
28
|
+
// ============================================================================
|
|
29
|
+
// Provider Content Block (with cache_control support)
|
|
30
|
+
// ============================================================================
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Content block in provider format (Anthropic-style)
|
|
34
|
+
* Can include cache_control for prompt caching
|
|
35
|
+
*/
|
|
36
|
+
export interface ProviderTextBlock {
|
|
37
|
+
type: 'text';
|
|
38
|
+
text: string;
|
|
39
|
+
cache_control?: CacheControl;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ProviderImageBlock {
|
|
43
|
+
type: 'image';
|
|
44
|
+
source: {
|
|
45
|
+
type: 'base64';
|
|
46
|
+
media_type: string;
|
|
47
|
+
data: string;
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type ProviderContentBlock = ProviderTextBlock | ProviderImageBlock;
|
|
52
|
+
|
|
53
|
+
// ============================================================================
|
|
54
|
+
// Provider Message (API-ready format)
|
|
55
|
+
// ============================================================================
|
|
56
|
+
|
|
57
|
+
export interface ProviderMessage {
|
|
58
|
+
role: 'user' | 'assistant';
|
|
59
|
+
content: string | ProviderContentBlock[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// ============================================================================
|
|
63
|
+
// Prefill Transform Result
|
|
64
|
+
// ============================================================================
|
|
65
|
+
|
|
66
|
+
export interface PrefillTransformResult {
|
|
67
|
+
/** System prompt content blocks (may have cache_control) */
|
|
68
|
+
systemContent: ProviderContentBlock[];
|
|
69
|
+
|
|
70
|
+
/** Messages in provider format (ready for API) */
|
|
71
|
+
messages: ProviderMessage[];
|
|
72
|
+
|
|
73
|
+
/** For legacy compatibility: system as string */
|
|
74
|
+
system: string;
|
|
75
|
+
|
|
76
|
+
/** For legacy compatibility: user content as string */
|
|
77
|
+
userContent: string;
|
|
78
|
+
|
|
79
|
+
/** For legacy compatibility: assistant prefill as string */
|
|
80
|
+
assistantPrefill: string;
|
|
81
|
+
|
|
82
|
+
/** Stop sequences to use */
|
|
83
|
+
stopSequences: string[];
|
|
84
|
+
|
|
85
|
+
/** Number of cache markers applied */
|
|
86
|
+
cacheMarkersApplied: number;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ============================================================================
|
|
90
|
+
// Transform Options
|
|
91
|
+
// ============================================================================
|
|
92
|
+
|
|
93
|
+
export interface PrefillTransformOptions {
|
|
94
|
+
/** Name of the assistant participant (default: 'Claude') */
|
|
95
|
+
assistantName?: string;
|
|
96
|
+
|
|
97
|
+
/** Maximum participants to include in stop sequences */
|
|
98
|
+
maxParticipantsForStop?: number;
|
|
99
|
+
|
|
100
|
+
/** Custom stop sequences to add */
|
|
101
|
+
additionalStopSequences?: string[];
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Where to inject tool definitions:
|
|
105
|
+
* - 'conversation': Inject into assistant content ~N messages from end (default)
|
|
106
|
+
* - 'system': Inject into system prompt
|
|
107
|
+
* - 'none': No injection (use getToolInstructions() for manual placement)
|
|
108
|
+
*/
|
|
109
|
+
toolInjectionMode?: 'conversation' | 'system' | 'none';
|
|
110
|
+
|
|
111
|
+
/** Position to inject tools when mode is 'conversation' (from end of messages) */
|
|
112
|
+
toolInjectionPosition?: number;
|
|
113
|
+
|
|
114
|
+
/** Enable prompt caching (default: true) */
|
|
115
|
+
promptCaching?: boolean;
|
|
116
|
+
|
|
117
|
+
/** Message delimiter for base models (e.g., '</s>') */
|
|
118
|
+
messageDelimiter?: string;
|
|
119
|
+
|
|
120
|
+
/** Context prefix for simulacrum seeding */
|
|
121
|
+
contextPrefix?: string;
|
|
122
|
+
|
|
123
|
+
/** Start assistant response with <thinking> tag */
|
|
124
|
+
prefillThinking?: boolean;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// ============================================================================
|
|
128
|
+
// Main Transform Function
|
|
129
|
+
// ============================================================================
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Transform normalized request to prefill format with cache control support
|
|
133
|
+
*/
|
|
134
|
+
export function transformToPrefill(
|
|
135
|
+
request: NormalizedRequest,
|
|
136
|
+
options: PrefillTransformOptions = {}
|
|
137
|
+
): PrefillTransformResult {
|
|
138
|
+
const {
|
|
139
|
+
assistantName = 'Claude',
|
|
140
|
+
maxParticipantsForStop = 10,
|
|
141
|
+
additionalStopSequences = [],
|
|
142
|
+
toolInjectionMode = 'conversation',
|
|
143
|
+
toolInjectionPosition = 10,
|
|
144
|
+
promptCaching = true,
|
|
145
|
+
messageDelimiter = '',
|
|
146
|
+
contextPrefix,
|
|
147
|
+
prefillThinking = false,
|
|
148
|
+
} = options;
|
|
149
|
+
|
|
150
|
+
const messages = request.messages;
|
|
151
|
+
const providerMessages: ProviderMessage[] = [];
|
|
152
|
+
|
|
153
|
+
// Track cache marker GLOBALLY across all flushes
|
|
154
|
+
// Everything BEFORE we see the marker gets cache_control
|
|
155
|
+
// Everything AFTER does NOT
|
|
156
|
+
let passedCacheMarker = false;
|
|
157
|
+
let cacheMarkersApplied = 0;
|
|
158
|
+
|
|
159
|
+
// Joiner between messages (if delimiter, no newlines needed)
|
|
160
|
+
const joiner = messageDelimiter ? '' : '\n';
|
|
161
|
+
|
|
162
|
+
// Track conversation lines for current section
|
|
163
|
+
let currentConversation: string[] = [];
|
|
164
|
+
let lastNonEmptyParticipant: string | null = null;
|
|
165
|
+
|
|
166
|
+
// Build system prompt
|
|
167
|
+
let systemText = request.system ?? '';
|
|
168
|
+
|
|
169
|
+
// Inject tool definitions into system prompt if mode is 'system'
|
|
170
|
+
if (toolInjectionMode === 'system' && request.tools && request.tools.length > 0) {
|
|
171
|
+
const toolsXml = formatToolsForPrefill(request.tools);
|
|
172
|
+
systemText = injectToolsIntoSystem(systemText, toolsXml);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// System prompt content (with cache_control if enabled)
|
|
176
|
+
const systemContent: ProviderContentBlock[] = [];
|
|
177
|
+
if (systemText) {
|
|
178
|
+
const systemBlock: ProviderTextBlock = { type: 'text', text: systemText };
|
|
179
|
+
if (promptCaching) {
|
|
180
|
+
systemBlock.cache_control = { type: 'ephemeral' };
|
|
181
|
+
cacheMarkersApplied++;
|
|
182
|
+
}
|
|
183
|
+
systemContent.push(systemBlock);
|
|
184
|
+
// Note: system content goes in systemContent, not providerMessages
|
|
185
|
+
// Anthropic's API requires system as a top-level parameter
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Add context prefix as first cached assistant message (for simulacrum seeding)
|
|
189
|
+
if (contextPrefix) {
|
|
190
|
+
const prefixBlock: ProviderTextBlock = { type: 'text', text: contextPrefix };
|
|
191
|
+
if (promptCaching) {
|
|
192
|
+
prefixBlock.cache_control = { type: 'ephemeral' };
|
|
193
|
+
cacheMarkersApplied++;
|
|
194
|
+
}
|
|
195
|
+
providerMessages.push({
|
|
196
|
+
role: 'assistant',
|
|
197
|
+
content: [prefixBlock],
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Process messages
|
|
202
|
+
for (let i = 0; i < messages.length; i++) {
|
|
203
|
+
const message = messages[i];
|
|
204
|
+
if (!message) continue;
|
|
205
|
+
|
|
206
|
+
const isLastMessage = i === messages.length - 1;
|
|
207
|
+
const isAssistant = message.participant === assistantName;
|
|
208
|
+
const hasCacheMarker = !!message.metadata?.cacheControl;
|
|
209
|
+
|
|
210
|
+
// Extract text and images
|
|
211
|
+
const { text, images } = formatContentForPrefill(message.content, message.participant);
|
|
212
|
+
const hasImages = images.length > 0;
|
|
213
|
+
const isEmpty = !text.trim() && !hasImages;
|
|
214
|
+
|
|
215
|
+
// Check for tool results
|
|
216
|
+
const hasToolResult = message.content.some(c => c.type === 'tool_result');
|
|
217
|
+
|
|
218
|
+
// If message has images, flush current conversation and add as user message
|
|
219
|
+
if (hasImages && !isEmpty) {
|
|
220
|
+
// Flush current assistant conversation
|
|
221
|
+
if (currentConversation.length > 0) {
|
|
222
|
+
const content = currentConversation.join(joiner);
|
|
223
|
+
providerMessages.push({
|
|
224
|
+
role: 'assistant',
|
|
225
|
+
content: content,
|
|
226
|
+
});
|
|
227
|
+
currentConversation = [];
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Add message with image as user turn
|
|
231
|
+
const userContent: ProviderContentBlock[] = [];
|
|
232
|
+
if (text) {
|
|
233
|
+
userContent.push({ type: 'text', text: `${message.participant}: ${text}` });
|
|
234
|
+
}
|
|
235
|
+
userContent.push(...images);
|
|
236
|
+
|
|
237
|
+
providerMessages.push({
|
|
238
|
+
role: 'user',
|
|
239
|
+
content: userContent,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
lastNonEmptyParticipant = message.participant;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Skip empty messages (except last)
|
|
247
|
+
if (isEmpty && !isLastMessage) {
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// Check if this message has the cache marker - switch to uncached mode AFTER this
|
|
252
|
+
if (hasCacheMarker && !passedCacheMarker) {
|
|
253
|
+
// Flush everything before this message WITH cache_control (if caching enabled)
|
|
254
|
+
if (currentConversation.length > 0) {
|
|
255
|
+
const content = currentConversation.join(joiner);
|
|
256
|
+
const contentBlock: ProviderTextBlock = { type: 'text', text: content };
|
|
257
|
+
if (promptCaching) {
|
|
258
|
+
contentBlock.cache_control = { type: 'ephemeral' };
|
|
259
|
+
cacheMarkersApplied++;
|
|
260
|
+
}
|
|
261
|
+
providerMessages.push({
|
|
262
|
+
role: 'assistant',
|
|
263
|
+
content: [contentBlock],
|
|
264
|
+
});
|
|
265
|
+
currentConversation = [];
|
|
266
|
+
}
|
|
267
|
+
passedCacheMarker = true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Check bot continuation logic
|
|
271
|
+
const isBotMessage = message.participant === assistantName;
|
|
272
|
+
const isContinuation = isBotMessage && lastNonEmptyParticipant === assistantName && !hasToolResult;
|
|
273
|
+
|
|
274
|
+
if (isContinuation && isLastMessage) {
|
|
275
|
+
// Bot continuation - don't add prefix, just complete from where we are
|
|
276
|
+
continue;
|
|
277
|
+
} else if (isLastMessage && isEmpty) {
|
|
278
|
+
// Completion target - optionally start with thinking tag
|
|
279
|
+
if (prefillThinking) {
|
|
280
|
+
currentConversation.push(`${message.participant}: <thinking>`);
|
|
281
|
+
} else {
|
|
282
|
+
currentConversation.push(`${message.participant}:`);
|
|
283
|
+
}
|
|
284
|
+
} else if (text) {
|
|
285
|
+
// Regular message - append delimiter if configured
|
|
286
|
+
currentConversation.push(`${message.participant}: ${text}${messageDelimiter}`);
|
|
287
|
+
if (!hasToolResult) {
|
|
288
|
+
lastNonEmptyParticipant = message.participant;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// If conversation doesn't end with assistant turn, append one
|
|
294
|
+
// This ensures the model knows to respond as the assistant
|
|
295
|
+
if (lastNonEmptyParticipant !== assistantName) {
|
|
296
|
+
if (prefillThinking) {
|
|
297
|
+
currentConversation.push(`${assistantName}: <thinking>`);
|
|
298
|
+
} else {
|
|
299
|
+
currentConversation.push(`${assistantName}:`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
// Flush any remaining conversation, inject tools if mode is 'conversation'
|
|
304
|
+
if (currentConversation.length > 0) {
|
|
305
|
+
const hasToolsForConversation =
|
|
306
|
+
toolInjectionMode === 'conversation' &&
|
|
307
|
+
request.tools &&
|
|
308
|
+
request.tools.length > 0;
|
|
309
|
+
|
|
310
|
+
if (hasToolsForConversation) {
|
|
311
|
+
// Inject tools into assistant content
|
|
312
|
+
const toolsText = formatToolsForInjection(request.tools!);
|
|
313
|
+
|
|
314
|
+
if (currentConversation.length > toolInjectionPosition) {
|
|
315
|
+
// Long conversation: insert tools ~N messages from the end
|
|
316
|
+
const splitPoint = currentConversation.length - toolInjectionPosition;
|
|
317
|
+
const beforeTools = currentConversation.slice(0, splitPoint);
|
|
318
|
+
const afterTools = currentConversation.slice(splitPoint);
|
|
319
|
+
|
|
320
|
+
const combined = [
|
|
321
|
+
...beforeTools,
|
|
322
|
+
toolsText,
|
|
323
|
+
...afterTools,
|
|
324
|
+
].join(joiner);
|
|
325
|
+
|
|
326
|
+
providerMessages.push({
|
|
327
|
+
role: 'assistant',
|
|
328
|
+
content: combined,
|
|
329
|
+
});
|
|
330
|
+
} else {
|
|
331
|
+
// Short conversation: inject tools at the end (right before completion point)
|
|
332
|
+
const combined = [...currentConversation, toolsText].join(joiner);
|
|
333
|
+
|
|
334
|
+
providerMessages.push({
|
|
335
|
+
role: 'assistant',
|
|
336
|
+
content: combined,
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
} else {
|
|
340
|
+
// No tool injection needed
|
|
341
|
+
providerMessages.push({
|
|
342
|
+
role: 'assistant',
|
|
343
|
+
content: currentConversation.join(joiner),
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Build stop sequences from participants
|
|
349
|
+
const stopSequences = buildStopSequences(
|
|
350
|
+
messages,
|
|
351
|
+
assistantName,
|
|
352
|
+
maxParticipantsForStop,
|
|
353
|
+
additionalStopSequences
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
// Build legacy string versions for backwards compatibility
|
|
357
|
+
const legacyStrings = buildLegacyStrings(providerMessages, systemText);
|
|
358
|
+
|
|
359
|
+
return {
|
|
360
|
+
systemContent,
|
|
361
|
+
messages: providerMessages,
|
|
362
|
+
system: systemText,
|
|
363
|
+
userContent: legacyStrings.userContent,
|
|
364
|
+
assistantPrefill: legacyStrings.assistantPrefill,
|
|
365
|
+
stopSequences,
|
|
366
|
+
cacheMarkersApplied,
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// ============================================================================
|
|
371
|
+
// Helper Functions
|
|
372
|
+
// ============================================================================
|
|
373
|
+
|
|
374
|
+
function formatContentForPrefill(
|
|
375
|
+
content: ContentBlock[],
|
|
376
|
+
participant: string
|
|
377
|
+
): { text: string; images: ProviderImageBlock[] } {
|
|
378
|
+
const parts: string[] = [];
|
|
379
|
+
const images: ProviderImageBlock[] = [];
|
|
380
|
+
|
|
381
|
+
for (const block of content) {
|
|
382
|
+
if (block.type === 'text') {
|
|
383
|
+
parts.push(block.text);
|
|
384
|
+
} else if (block.type === 'image') {
|
|
385
|
+
// Convert to provider format
|
|
386
|
+
if (block.source.type === 'base64') {
|
|
387
|
+
images.push({
|
|
388
|
+
type: 'image',
|
|
389
|
+
source: {
|
|
390
|
+
type: 'base64',
|
|
391
|
+
media_type: block.source.mediaType,
|
|
392
|
+
data: block.source.data,
|
|
393
|
+
},
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
} else if (block.type === 'tool_use') {
|
|
397
|
+
// Format as: Name>[toolname]: {json}
|
|
398
|
+
parts.push(`${participant}>[${block.name}]: ${JSON.stringify(block.input)}`);
|
|
399
|
+
} else if (block.type === 'tool_result') {
|
|
400
|
+
// Format as: Name<[tool_result]: result
|
|
401
|
+
const resultText = typeof block.content === 'string'
|
|
402
|
+
? block.content
|
|
403
|
+
: JSON.stringify(block.content);
|
|
404
|
+
parts.push(`${participant}<[tool_result]: ${resultText}`);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return { text: parts.join('\n'), images };
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
function formatToolsForPrefill(tools: ToolDefinition[]): string {
|
|
412
|
+
const toolsForPrompt: ToolDefinitionForPrompt[] = tools.map((tool) => ({
|
|
413
|
+
name: tool.name,
|
|
414
|
+
description: tool.description,
|
|
415
|
+
parameters: Object.fromEntries(
|
|
416
|
+
Object.entries(tool.inputSchema.properties).map(([name, schema]) => [
|
|
417
|
+
name,
|
|
418
|
+
{
|
|
419
|
+
type: schema.type,
|
|
420
|
+
description: schema.description,
|
|
421
|
+
required: tool.inputSchema.required?.includes(name),
|
|
422
|
+
enum: schema.enum,
|
|
423
|
+
},
|
|
424
|
+
])
|
|
425
|
+
),
|
|
426
|
+
}));
|
|
427
|
+
|
|
428
|
+
return formatToolDefinitions(toolsForPrompt);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
function injectToolsIntoSystem(system: string, toolsXml: string): string {
|
|
432
|
+
const toolsSection = `
|
|
433
|
+
<available_tools>
|
|
434
|
+
${toolsXml}
|
|
435
|
+
</available_tools>
|
|
436
|
+
|
|
437
|
+
When you want to use a tool, output:
|
|
438
|
+
<function_calls>
|
|
439
|
+
<invoke name="tool_name">
|
|
440
|
+
<parameter name="param_name">value</parameter>
|
|
441
|
+
</invoke>
|
|
442
|
+
</function_calls>
|
|
443
|
+
`;
|
|
444
|
+
|
|
445
|
+
return system + '\n\n' + toolsSection;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// Tool format constants (assembled to avoid triggering stop sequences)
|
|
449
|
+
const FUNCTIONS_OPEN = '<' + 'functions>';
|
|
450
|
+
const FUNCTIONS_CLOSE = '</' + 'functions>';
|
|
451
|
+
const FUNCTION_OPEN = '<' + 'function>';
|
|
452
|
+
const FUNCTION_CLOSE = '</' + 'function>';
|
|
453
|
+
const FUNC_CALLS_OPEN = '<' + 'function_calls>';
|
|
454
|
+
const FUNC_CALLS_CLOSE = '</' + 'function_calls>';
|
|
455
|
+
const INVOKE_OPEN = '<' + 'invoke name="';
|
|
456
|
+
const INVOKE_CLOSE = '</' + 'invoke>';
|
|
457
|
+
const PARAM_OPEN = '<' + 'parameter name="';
|
|
458
|
+
const PARAM_CLOSE = '</' + 'parameter>';
|
|
459
|
+
|
|
460
|
+
function formatToolsForInjection(tools: ToolDefinition[]): string {
|
|
461
|
+
// Use the same XML format as system mode for consistency
|
|
462
|
+
const toolsXml = formatToolsForPrefill(tools);
|
|
463
|
+
|
|
464
|
+
return `
|
|
465
|
+
<available_tools>
|
|
466
|
+
${toolsXml}
|
|
467
|
+
</available_tools>
|
|
468
|
+
|
|
469
|
+
When you want to use a tool, output:
|
|
470
|
+
${FUNC_CALLS_OPEN}
|
|
471
|
+
${INVOKE_OPEN}tool_name">
|
|
472
|
+
${PARAM_OPEN}param_name">value${PARAM_CLOSE}
|
|
473
|
+
${INVOKE_CLOSE}
|
|
474
|
+
${FUNC_CALLS_CLOSE}`;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
function buildStopSequences(
|
|
478
|
+
messages: NormalizedMessage[],
|
|
479
|
+
assistantName: string,
|
|
480
|
+
maxParticipants: number,
|
|
481
|
+
additionalSequences: string[]
|
|
482
|
+
): string[] {
|
|
483
|
+
// Collect unique participants (excluding assistant)
|
|
484
|
+
const participants = new Set<string>();
|
|
485
|
+
|
|
486
|
+
// Scan from end of messages
|
|
487
|
+
for (let i = messages.length - 1; i >= 0 && participants.size < maxParticipants; i--) {
|
|
488
|
+
const message = messages[i];
|
|
489
|
+
if (!message) continue;
|
|
490
|
+
const participant = message.participant;
|
|
491
|
+
if (participant !== assistantName) {
|
|
492
|
+
participants.add(participant);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
// Build stop sequences
|
|
497
|
+
const sequences: string[] = [];
|
|
498
|
+
|
|
499
|
+
// Participant-based stops
|
|
500
|
+
for (const participant of participants) {
|
|
501
|
+
sequences.push(`\n${participant}:`);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
// Tool-related stop
|
|
505
|
+
sequences.push('</function_calls>');
|
|
506
|
+
|
|
507
|
+
// Additional sequences
|
|
508
|
+
sequences.push(...additionalSequences);
|
|
509
|
+
|
|
510
|
+
return sequences;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function buildLegacyStrings(
|
|
514
|
+
messages: ProviderMessage[],
|
|
515
|
+
systemText: string
|
|
516
|
+
): { userContent: string; assistantPrefill: string } {
|
|
517
|
+
// Extract user content (first user message after system)
|
|
518
|
+
let userContent = '';
|
|
519
|
+
let assistantPrefill = '';
|
|
520
|
+
|
|
521
|
+
for (const msg of messages) {
|
|
522
|
+
if (msg.role === 'user') {
|
|
523
|
+
if (typeof msg.content === 'string') {
|
|
524
|
+
userContent += msg.content + '\n\n';
|
|
525
|
+
} else {
|
|
526
|
+
// Extract text from blocks
|
|
527
|
+
for (const block of msg.content) {
|
|
528
|
+
if (block.type === 'text') {
|
|
529
|
+
userContent += block.text + '\n\n';
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
} else if (msg.role === 'assistant') {
|
|
534
|
+
if (typeof msg.content === 'string') {
|
|
535
|
+
assistantPrefill += msg.content;
|
|
536
|
+
} else {
|
|
537
|
+
for (const block of msg.content) {
|
|
538
|
+
if (block.type === 'text') {
|
|
539
|
+
assistantPrefill += block.text;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
return {
|
|
547
|
+
userContent: userContent.trim(),
|
|
548
|
+
assistantPrefill: assistantPrefill,
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
// ============================================================================
|
|
553
|
+
// Prefill Continuation
|
|
554
|
+
// ============================================================================
|
|
555
|
+
|
|
556
|
+
/**
|
|
557
|
+
* Build a continuation request from accumulated output
|
|
558
|
+
*/
|
|
559
|
+
export function buildContinuationPrefill(
|
|
560
|
+
originalResult: PrefillTransformResult,
|
|
561
|
+
accumulated: string
|
|
562
|
+
): PrefillTransformResult {
|
|
563
|
+
// Update the last assistant message with accumulated content
|
|
564
|
+
const newMessages = [...originalResult.messages];
|
|
565
|
+
|
|
566
|
+
// Find the last assistant message or add one
|
|
567
|
+
const lastIdx = newMessages.length - 1;
|
|
568
|
+
if (lastIdx >= 0 && newMessages[lastIdx]?.role === 'assistant') {
|
|
569
|
+
newMessages[lastIdx] = {
|
|
570
|
+
role: 'assistant',
|
|
571
|
+
content: accumulated,
|
|
572
|
+
};
|
|
573
|
+
} else {
|
|
574
|
+
newMessages.push({
|
|
575
|
+
role: 'assistant',
|
|
576
|
+
content: accumulated,
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
return {
|
|
581
|
+
...originalResult,
|
|
582
|
+
messages: newMessages,
|
|
583
|
+
assistantPrefill: accumulated,
|
|
584
|
+
};
|
|
585
|
+
}
|