@animalabs/membrane 0.5.55 → 0.5.63

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.
@@ -126,6 +126,8 @@ export interface ProviderRequest {
126
126
  presencePenalty?: number;
127
127
  /** Frequency penalty */
128
128
  frequencyPenalty?: number;
129
+ /** Repetition penalty (multiplicative, vLLM/HuggingFace style) */
130
+ repetitionPenalty?: number;
129
131
  /** Stop sequences */
130
132
  stopSequences?: string[];
131
133
  /** Tools in provider format */
@@ -140,6 +142,13 @@ export interface ProviderRequestOptions {
140
142
  idleTimeoutMs?: number;
141
143
  /** Called with the raw API request body right before fetch */
142
144
  onRequest?: (rawRequest: unknown) => void;
145
+ /**
146
+ * Wrap native thinking deltas in <thinking>...</thinking> tags on the
147
+ * onChunk stream. Used by the XML formatter path so its tag-based parser
148
+ * tracks thinking blocks; without this, native thinking content streams
149
+ * indistinguishably from visible text.
150
+ */
151
+ wrapThinkingTags?: boolean;
143
152
  }
144
153
  export interface ProviderResponse {
145
154
  /** Raw response content */
@@ -1 +1 @@
1
- {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/types/provider.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,MAAM,WAAW,cAAc;IAC7B,uEAAuE;IACvE,+BAA+B,CAAC,EAAE,OAAO,CAAC;IAE1C,+DAA+D;IAC/D,wBAAwB,CAAC,EAAE,OAAO,CAAC;IAEnC,iDAAiD;IACjD,sBAAsB,CAAC,EAAE,OAAO,CAAC;IAEjC,mEAAmE;IACnE,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAE/B,+DAA+D;IAC/D,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IAExB,8BAA8B;IAC9B,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAMD,MAAM,WAAW,iBAAiB;IAEhC,UAAU,EAAE,OAAO,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,UAAU,EAAE,OAAO,CAAC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B,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,gBAAgB;IAC/B,2BAA2B;IAC3B,OAAO,EAAE,OAAO,CAAC;IAEjB,qCAAqC;IACrC,UAAU,EAAE,MAAM,CAAC;IAEnB,oCAAoC;IACpC,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,+BAA+B;IAC/B,KAAK,EAAE;QACL,WAAW,EAAE,MAAM,CAAC;QACpB,YAAY,EAAE,MAAM,CAAC;QACrB,mBAAmB,CAAC,EAAE,MAAM,CAAC;QAC7B,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,CAAC;IAEF,8BAA8B;IAC9B,KAAK,EAAE,MAAM,CAAC;IAEd,oDAAoD;IACpD,UAAU,EAAE,OAAO,CAAC;IAEpB,iCAAiC;IACjC,GAAG,EAAE,OAAO,CAAC;CACd;AAED,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IACjC,cAAc,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,IAAI,CAAC;CAC1D"}
@@ -18,10 +18,20 @@ export interface GenerationConfig {
18
18
  presencePenalty?: number;
19
19
  /** Frequency penalty (provider-specific) */
20
20
  frequencyPenalty?: number;
21
+ /** Repetition penalty — multiplicative (vLLM/HuggingFace style, typically 1.0-1.2) */
22
+ repetitionPenalty?: number;
21
23
  /** Enable thinking/reasoning mode */
22
24
  thinking?: {
23
25
  enabled: boolean;
24
26
  budgetTokens?: number;
27
+ /** Thinking type for the API: 'enabled' (default, explicit budget) or 'adaptive' (model-managed) */
28
+ type?: 'enabled' | 'adaptive';
29
+ /**
30
+ * Controls how thinking content is returned: 'summarized' (readable summary)
31
+ * or 'omitted' (empty thinking field, signature only). Models like Fable 5 /
32
+ * Opus 4.7+ default to 'omitted' — set 'summarized' to receive thinking text.
33
+ */
34
+ display?: 'summarized' | 'omitted';
25
35
  };
26
36
  /** Image generation config (Gemini) */
27
37
  imageGeneration?: {
@@ -1 +1 @@
1
- {"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/types/request.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACtD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAMjD,MAAM,WAAW,gBAAgB;IAC/B,uBAAuB;IACvB,KAAK,EAAE,MAAM,CAAC;IAEd,iCAAiC;IACjC,SAAS,EAAE,MAAM,CAAC;IAElB,wBAAwB;IACxB,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB,6BAA6B;IAC7B,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,yCAAyC;IACzC,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,2CAA2C;IAC3C,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB,4CAA4C;IAC5C,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAE1B,qCAAqC;IACrC,QAAQ,CAAC,EAAE;QACT,OAAO,EAAE,OAAO,CAAC;QACjB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;IAEF,uCAAuC;IACvC,eAAe,CAAC,EAAE;QAChB,OAAO,EAAE,OAAO,CAAC;QACjB,UAAU,EAAE,CAAC,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC;QACjC,WAAW,CAAC,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,GAAG,KAAK,CAAC;QACtD,SAAS,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC;KAC1C,CAAC;CACH;AAMD,MAAM,MAAM,oBAAoB,GAC5B,MAAM,GACN,YAAY,GACZ,oBAAoB,CAAC;AAEzB,MAAM,WAAW,kBAAkB;IACjC,4BAA4B;IAC5B,SAAS,EAAE,MAAM,EAAE,CAAC;IAEpB,4CAA4C;IAC5C,QAAQ,CAAC,EAAE,oBAAoB,CAAC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AC;IAEvB;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAE5B;;;;OAIG;IACH,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAE9B;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,kDAAkD;IAClD,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC1C"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@animalabs/membrane",
3
- "version": "0.5.55",
3
+ "version": "0.5.63",
4
4
  "description": "LLM middleware - a selective boundary that transforms what passes through",
5
5
  "repository": {
6
6
  "type": "git",
@@ -385,10 +385,20 @@ export class NativeFormatter implements PrefillFormatter {
385
385
  is_error: block.isError,
386
386
  });
387
387
  } else if (block.type === 'thinking') {
388
+ // Round-trip thinking blocks verbatim, including the signature — the
389
+ // API validates it and (on display:'omitted' models) decrypts it to
390
+ // reconstruct the original reasoning. Signature-only blocks (empty
391
+ // thinking field) are valid and must be passed back unchanged.
388
392
  result.push({
389
393
  type: 'thinking',
390
394
  thinking: block.thinking,
395
+ ...((block as { signature?: string }).signature
396
+ ? { signature: (block as { signature?: string }).signature }
397
+ : {}),
391
398
  });
399
+ } else if (block.type === 'redacted_thinking') {
400
+ // Pass through verbatim (carries encrypted data field)
401
+ result.push({ ...(block as unknown as Record<string, unknown>) });
392
402
  } else if (block.type === 'document' || block.type === 'audio') {
393
403
  hasUnsupportedMedia = true;
394
404
  }
package/src/membrane.ts CHANGED
@@ -292,6 +292,12 @@ export class Membrane {
292
292
  // These can't be handled by the text-based XML parser, so we capture and append them
293
293
  const extraContentBlocks: ContentBlock[] = [];
294
294
 
295
+ // Native thinking blocks from the provider (with signatures). The parser
296
+ // derives signature-less thinking blocks from <thinking> text (via
297
+ // wrapThinkingTags); signatures from these are merged into those after
298
+ // parsing, and signature-only blocks are prepended.
299
+ const providerThinkingBlocks: ContentBlock[] = [];
300
+
295
301
  // Transform initial request using the formatter
296
302
  let { providerRequest, prefillResult } = this.transformRequest(request, formatter);
297
303
 
@@ -385,6 +391,10 @@ export class Membrane {
385
391
  {
386
392
  signal,
387
393
  normalizedRequest: request,
394
+ // The tag-based parser tracks thinking via <thinking> tags — ask the
395
+ // provider to wrap native thinking deltas so they don't stream as
396
+ // visible text (see ProviderRequestOptions.wrapThinkingTags)
397
+ wrapThinkingTags: true,
388
398
  onRequest: (req) => {
389
399
  rawRequest = req;
390
400
  onRequest?.(req);
@@ -410,6 +420,18 @@ export class Membrane {
410
420
  data: (block as any).data,
411
421
  mimeType: (block as any).mimeType,
412
422
  } as ContentBlock);
423
+ } else if (block.type === 'thinking') {
424
+ // Native thinking block from the provider — carries the signature
425
+ // (encrypted full reasoning). Captured so consumers can persist and
426
+ // round-trip it for reasoning continuity. Includes signature-only
427
+ // blocks (display:'omitted' returns an empty thinking field).
428
+ providerThinkingBlocks.push({
429
+ type: 'thinking',
430
+ thinking: (block as any).thinking ?? '',
431
+ ...((block as any).signature ? { signature: (block as any).signature } : {}),
432
+ } as ContentBlock);
433
+ } else if (block.type === 'redacted_thinking') {
434
+ providerThinkingBlocks.push({ ...(block as any) } as ContentBlock);
413
435
  }
414
436
  }
415
437
  }
@@ -700,6 +722,33 @@ export class Membrane {
700
722
  response.content.push(...extraContentBlocks);
701
723
  }
702
724
 
725
+ // Merge provider thinking signatures into parser-derived thinking blocks
726
+ // (matched in stream order), and prepend any leftover provider blocks —
727
+ // signature-only thinking (display:'omitted') never appears in the text
728
+ // stream, so the parser produces no block for it. redacted_thinking
729
+ // blocks are always prepended verbatim.
730
+ if (providerThinkingBlocks.length > 0) {
731
+ const parsedThinking = response.content.filter(
732
+ (b) => b.type === 'thinking'
733
+ ) as Array<{ type: 'thinking'; thinking: string; signature?: string }>;
734
+
735
+ const providerThinking = providerThinkingBlocks.filter((b) => b.type === 'thinking');
736
+ const redacted = providerThinkingBlocks.filter((b) => b.type === 'redacted_thinking');
737
+
738
+ const matched = Math.min(providerThinking.length, parsedThinking.length);
739
+ for (let i = 0; i < matched; i++) {
740
+ const sig = (providerThinking[i] as { signature?: string }).signature;
741
+ if (sig) {
742
+ parsedThinking[i]!.signature = sig;
743
+ }
744
+ }
745
+
746
+ const leftover = providerThinking.slice(matched);
747
+ if (leftover.length > 0 || redacted.length > 0) {
748
+ response.content.unshift(...leftover, ...redacted);
749
+ }
750
+ }
751
+
703
752
  return response;
704
753
  } catch (error) {
705
754
  // Check if this is an abort error
@@ -1005,6 +1054,19 @@ export class Membrane {
1005
1054
  content: block.content,
1006
1055
  is_error: block.isError,
1007
1056
  });
1057
+ } else if (block.type === 'thinking') {
1058
+ // Round-trip thinking blocks verbatim including the signature — the
1059
+ // API validates it and (on display:'omitted' models) decrypts it to
1060
+ // reconstruct prior reasoning. Empty thinking + signature is valid.
1061
+ content.push({
1062
+ type: 'thinking',
1063
+ thinking: (block as { thinking?: string }).thinking ?? '',
1064
+ ...((block as { signature?: string }).signature
1065
+ ? { signature: (block as { signature?: string }).signature }
1066
+ : {}),
1067
+ });
1068
+ } else if (block.type === 'redacted_thinking') {
1069
+ content.push({ ...(block as unknown as Record<string, unknown>) });
1008
1070
  } else if (block.type === 'image') {
1009
1071
  if (block.source.type === 'base64') {
1010
1072
  const imageBlock: Record<string, unknown> = {
@@ -1081,13 +1143,8 @@ export class Membrane {
1081
1143
  );
1082
1144
  }
1083
1145
 
1084
- // Build thinking config for native extended thinking
1085
- const thinking = request.config.thinking?.enabled
1086
- ? {
1087
- type: 'enabled' as const,
1088
- budget_tokens: request.config.thinking.budgetTokens ?? 5000,
1089
- }
1090
- : undefined;
1146
+ // Build thinking config for native extended thinking (budget clamped to max_tokens)
1147
+ const thinking = this.buildThinkingParam(request.config);
1091
1148
 
1092
1149
  // Anthropic requires temperature=1 when extended thinking is enabled
1093
1150
  const temperature = thinking ? 1 : request.config.temperature;
@@ -1172,8 +1229,10 @@ export class Membrane {
1172
1229
  * Used by transformRequest, buildContinuationRequest, and buildContinuationRequestWithImages.
1173
1230
  */
1174
1231
  private getBaseProviderParams(config: NormalizedRequest['config']) {
1232
+ // Build thinking config for native extended thinking
1233
+ const thinking = this.buildThinkingParam(config);
1175
1234
  // Anthropic requires temperature=1 when extended thinking is enabled
1176
- const temperature = config.thinking?.enabled ? 1 : config.temperature;
1235
+ const temperature = thinking ? 1 : config.temperature;
1177
1236
  return {
1178
1237
  model: config.model,
1179
1238
  maxTokens: config.maxTokens,
@@ -1182,9 +1241,41 @@ export class Membrane {
1182
1241
  topK: config.topK,
1183
1242
  presencePenalty: config.presencePenalty,
1184
1243
  frequencyPenalty: config.frequencyPenalty,
1244
+ repetitionPenalty: config.repetitionPenalty,
1245
+ thinking,
1185
1246
  };
1186
1247
  }
1187
1248
 
1249
+ /**
1250
+ * Build the provider thinking parameter from config.
1251
+ *
1252
+ * For type 'enabled', the API requires max_tokens > budget_tokens and a
1253
+ * minimum budget of 1024 — a misconfigured budget (e.g., default 10000 with
1254
+ * max_tokens 4096) is clamped to fit. If no valid budget fits (max_tokens
1255
+ * too small), thinking is omitted entirely rather than sending a request
1256
+ * the API will reject.
1257
+ */
1258
+ private buildThinkingParam(config: NormalizedRequest['config']):
1259
+ | { type: 'adaptive'; display?: 'summarized' | 'omitted' }
1260
+ | { type: 'enabled'; budget_tokens: number; display?: 'summarized' | 'omitted' }
1261
+ | undefined {
1262
+ if (!config.thinking?.enabled) return undefined;
1263
+
1264
+ const display = config.thinking.display;
1265
+ if ((config.thinking.type ?? 'enabled') === 'adaptive') {
1266
+ return { type: 'adaptive', ...(display ? { display } : {}) };
1267
+ }
1268
+
1269
+ const requested = config.thinking.budgetTokens ?? 5000;
1270
+ const maxTokens = typeof config.maxTokens === 'number' ? config.maxTokens : undefined;
1271
+ const budget = maxTokens !== undefined ? Math.min(requested, maxTokens - 1024) : requested;
1272
+ if (budget < 1024) {
1273
+ // Can't fit a valid thinking budget under max_tokens — skip thinking
1274
+ return undefined;
1275
+ }
1276
+ return { type: 'enabled', budget_tokens: budget, ...(display ? { display } : {}) };
1277
+ }
1278
+
1188
1279
  /**
1189
1280
  * Transform a normalized request into provider format using the formatter
1190
1281
  */
@@ -1232,6 +1323,15 @@ export class Membrane {
1232
1323
  },
1233
1324
  };
1234
1325
 
1326
+ // The API rejects extended thinking combined with an assistant prefill.
1327
+ // Prefill-style builds (XML formatter) use the thinking config for the
1328
+ // literal `<thinking>` text prefix instead of the API feature — drop the
1329
+ // API param when the built request actually ends in an assistant prefill.
1330
+ // Chat-style builds (no prefill) keep it.
1331
+ if (buildResult.assistantPrefill && providerRequest.thinking) {
1332
+ delete providerRequest.thinking;
1333
+ }
1334
+
1235
1335
  return { providerRequest, prefillResult: buildResult };
1236
1336
  }
1237
1337
 
@@ -1243,6 +1343,8 @@ export class Membrane {
1243
1343
  timeoutMs?: number;
1244
1344
  idleTimeoutMs?: number;
1245
1345
  onRequest?: (rawRequest: unknown) => void;
1346
+ /** See ProviderRequestOptions.wrapThinkingTags */
1347
+ wrapThinkingTags?: boolean;
1246
1348
  /**
1247
1349
  * The original NormalizedRequest, threaded through so the
1248
1350
  * `beforeRequest` hook can see both shapes (normalized + provider).
@@ -1292,6 +1394,9 @@ export class Membrane {
1292
1394
 
1293
1395
  return {
1294
1396
  ...this.getBaseProviderParams(originalRequest.config),
1397
+ // Continuations always end in an assistant prefill — the API rejects
1398
+ // extended thinking combined with prefill, so never send the param here
1399
+ thinking: undefined,
1295
1400
  messages,
1296
1401
  system: prefillResult.systemContent
1297
1402
  ? (Array.isArray(prefillResult.systemContent) && prefillResult.systemContent.length > 0
@@ -1362,6 +1467,9 @@ export class Membrane {
1362
1467
 
1363
1468
  return {
1364
1469
  ...this.getBaseProviderParams(originalRequest.config),
1470
+ // Continuations always end in an assistant prefill — the API rejects
1471
+ // extended thinking combined with prefill, so never send the param here
1472
+ thinking: undefined,
1365
1473
  messages,
1366
1474
  system: prefillResult.systemContent
1367
1475
  ? (Array.isArray(prefillResult.systemContent) && prefillResult.systemContent.length > 0
@@ -1595,6 +1703,11 @@ export class Membrane {
1595
1703
  return 'stop_sequence';
1596
1704
  case 'tool_use':
1597
1705
  return 'tool_use';
1706
+ case 'refusal':
1707
+ // Safety refusal (e.g., Fable 5 reasoning_extraction). Must survive
1708
+ // mapping — downstream consumers react to refusals (chapterx adds a
1709
+ // Discord reaction). Defaulting this to end_turn silently hid them.
1710
+ return 'refusal';
1598
1711
  default:
1599
1712
  return 'end_turn';
1600
1713
  }
@@ -2483,13 +2596,16 @@ export class Membrane {
2483
2596
  }
2484
2597
 
2485
2598
  // Native tool names must match ^[a-zA-Z0-9_-]{1,128}$.
2486
- // The framework uses module:tool namespacing, so we round-trip colons
2487
- // through an escape encoding for the API wire format.
2488
- // Lossless: escape underscores first (_u), then encode colons (_c).
2599
+ // Tool names use `--` namespacing, which is already API-valid; the only
2600
+ // character that ever needs escaping is a literal colon, encoded losslessly as
2601
+ // `__` and back. We deliberately do NOT escape underscores they are valid,
2602
+ // and escaping them (the previous `_u`/`_c` scheme) garbled every
2603
+ // underscore-containing tool name in the request the model actually sees
2604
+ // (`send_message` → `send_umessage`), polluting its reasoning for no benefit.
2489
2605
  function sanitizeToolName(name: string): string {
2490
- return name.replace(/_/g, '_u').replace(/:/g, '_c');
2606
+ return name.replace(/:/g, '__');
2491
2607
  }
2492
2608
 
2493
2609
  function unsanitizeToolName(name: string): string {
2494
- return name.replace(/_c/g, ':').replace(/_u/g, '_');
2610
+ return name.replace(/__/g, ':');
2495
2611
  }
@@ -122,12 +122,20 @@ export class AnthropicAdapter implements ProviderAdapter {
122
122
  let cacheReadTokens: number | undefined;
123
123
  let stopReason: string = 'end_turn';
124
124
  let stopSequence: string | undefined;
125
+ let stopDetails: unknown;
125
126
 
126
127
  // Content block tracking — finalized on content_block_stop
127
128
  const contentBlocks: Record<string, unknown>[] = [];
128
129
  let currentBlockIndex = -1;
129
130
  let currentBlockContent = '';
130
131
  let currentBlockInputJson = '';
132
+ // When wrapThinkingTags is set (XML formatter path), native thinking
133
+ // deltas are wrapped in <thinking>...</thinking> on the chunk stream so
134
+ // the tag-based parser tracks them as thinking instead of visible text.
135
+ // Tag opened lazily on the first delta — display:'omitted' models emit
136
+ // thinking blocks with no thinking_delta at all (signature only).
137
+ const wrapThinkingTags = options?.wrapThinkingTags === true;
138
+ let thinkingTagOpen = false;
131
139
 
132
140
  for await (const event of stream) {
133
141
  resetIdleTimer();
@@ -152,7 +160,21 @@ export class AnthropicAdapter implements ProviderAdapter {
152
160
  callbacks.onChunk(chunk);
153
161
  } else if (event.delta.type === 'thinking_delta') {
154
162
  currentBlockContent += event.delta.thinking;
163
+ if (wrapThinkingTags && !thinkingTagOpen) {
164
+ callbacks.onChunk('<thinking>');
165
+ thinkingTagOpen = true;
166
+ }
155
167
  callbacks.onChunk(event.delta.thinking);
168
+ } else if ((event.delta as { type: string }).type === 'signature_delta') {
169
+ // Accumulate the cryptographic signature that authenticates this
170
+ // thinking block. Without this, signatures never land on the
171
+ // streaming path and the next request — which carries the block
172
+ // back in history — fails Anthropic's signature validation.
173
+ const sig = (event.delta as { signature?: string }).signature;
174
+ const block = contentBlocks[currentBlockIndex];
175
+ if (block && block.type === 'thinking' && sig) {
176
+ block.signature = ((block.signature as string | undefined) ?? '') + sig;
177
+ }
156
178
  } else if ((event.delta as { type: string }).type === 'input_json_delta') {
157
179
  currentBlockInputJson += (event.delta as { partial_json: string }).partial_json;
158
180
  }
@@ -166,6 +188,10 @@ export class AnthropicAdapter implements ProviderAdapter {
166
188
  block.text = currentBlockContent;
167
189
  } else if (block.type === 'thinking') {
168
190
  block.thinking = currentBlockContent;
191
+ if (thinkingTagOpen) {
192
+ callbacks.onChunk('</thinking>\n');
193
+ thinkingTagOpen = false;
194
+ }
169
195
  } else if (block.type === 'tool_use' && currentBlockInputJson) {
170
196
  try { block.input = JSON.parse(currentBlockInputJson); } catch { /* partial JSON */ }
171
197
  }
@@ -176,9 +202,15 @@ export class AnthropicAdapter implements ProviderAdapter {
176
202
  // All content blocks are finalized by the time message_delta arrives.
177
203
  // Capture final metadata and exit — message_stop and the SSE connection
178
204
  // teardown after it add only variable latency with no useful data.
179
- const delta = event.delta as { stop_reason?: string; stop_sequence?: string };
205
+ const delta = event.delta as {
206
+ stop_reason?: string;
207
+ stop_sequence?: string;
208
+ stop_details?: unknown;
209
+ };
180
210
  stopReason = delta.stop_reason ?? 'end_turn';
181
211
  stopSequence = delta.stop_sequence ?? undefined;
212
+ // stop_details carries refusal metadata (e.g., category: 'reasoning_extraction')
213
+ stopDetails = delta.stop_details ?? undefined;
182
214
  const deltaUsage = event.usage as unknown as {
183
215
  output_tokens: number;
184
216
  cache_creation_input_tokens?: number | null;
@@ -219,6 +251,7 @@ export class AnthropicAdapter implements ProviderAdapter {
219
251
  content: contentBlocks,
220
252
  stop_reason: stopReason,
221
253
  stop_sequence: stopSequence ?? null,
254
+ stop_details: stopDetails ?? null,
222
255
  model,
223
256
  usage: {
224
257
  input_tokens: inputTokens,
@@ -249,7 +282,11 @@ export class AnthropicAdapter implements ProviderAdapter {
249
282
 
250
283
  private buildRequest(request: ProviderRequest): Anthropic.MessageCreateParams {
251
284
  // Strip provider-specific fields (e.g., sourceUrl for Gemini) from image blocks
252
- // before sending to Anthropic, which rejects extra inputs
285
+ // before sending to Anthropic, which rejects extra inputs.
286
+ // Also normalize nested tool_result content blocks: Membrane uses camelCase
287
+ // `mediaType`, Anthropic expects snake_case `media_type`. Without this,
288
+ // an image returned by a tool reaches the API as `{source: {mediaType: ...}}`
289
+ // and is silently rejected (the model sees the text label only).
253
290
  const sanitizedMessages = (request.messages as any[]).map((msg: any) => {
254
291
  if (!Array.isArray(msg.content)) return msg;
255
292
  return {
@@ -259,6 +296,12 @@ export class AnthropicAdapter implements ProviderAdapter {
259
296
  const { sourceUrl, ...rest } = block;
260
297
  return rest;
261
298
  }
299
+ if (block.type === 'tool_result' && Array.isArray(block.content)) {
300
+ return {
301
+ ...block,
302
+ content: toAnthropicToolResultContent(block.content as ContentBlock[]),
303
+ };
304
+ }
262
305
  return block;
263
306
  }),
264
307
  };
@@ -396,6 +439,41 @@ export class AnthropicAdapter implements ProviderAdapter {
396
439
  // Content Conversion Utilities
397
440
  // ============================================================================
398
441
 
442
+ /**
443
+ * Convert Membrane tool-result content blocks to Anthropic's tool_result.content
444
+ * mixed array (text + image). This is what carries an image returned by a tool
445
+ * (e.g. an MCP fetch_attachment result) all the way to the model. Other block
446
+ * types are not valid inside tool_result.content per the Anthropic API and are
447
+ * dropped.
448
+ */
449
+ function toAnthropicToolResultContent(
450
+ blocks: ContentBlock[],
451
+ ): Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam> {
452
+ const out: Array<Anthropic.TextBlockParam | Anthropic.ImageBlockParam> = [];
453
+ for (const block of blocks) {
454
+ if (block.type === 'text') {
455
+ out.push({ type: 'text', text: block.text });
456
+ } else if (block.type === 'image') {
457
+ if (block.source.type === 'base64') {
458
+ out.push({
459
+ type: 'image',
460
+ source: {
461
+ type: 'base64',
462
+ media_type: block.source.mediaType as 'image/jpeg' | 'image/png' | 'image/gif' | 'image/webp',
463
+ data: block.source.data,
464
+ },
465
+ });
466
+ } else if (block.source.type === 'url') {
467
+ out.push({
468
+ type: 'image',
469
+ source: { type: 'url', url: block.source.url },
470
+ });
471
+ }
472
+ }
473
+ }
474
+ return out;
475
+ }
476
+
399
477
  /**
400
478
  * Convert normalized content blocks to Anthropic format
401
479
  * Preserves cache_control for prompt caching
@@ -425,6 +503,11 @@ export function toAnthropicContent(blocks: ContentBlock[]): Anthropic.ContentBlo
425
503
  data: block.source.data,
426
504
  },
427
505
  });
506
+ } else if (block.source.type === 'url') {
507
+ result.push({
508
+ type: 'image',
509
+ source: { type: 'url', url: block.source.url },
510
+ });
428
511
  }
429
512
  break;
430
513
 
@@ -454,7 +537,7 @@ export function toAnthropicContent(blocks: ContentBlock[]): Anthropic.ContentBlo
454
537
  tool_use_id: block.toolUseId,
455
538
  content: typeof block.content === 'string'
456
539
  ? block.content
457
- : JSON.stringify(block.content),
540
+ : toAnthropicToolResultContent(block.content),
458
541
  is_error: block.isError,
459
542
  });
460
543
  break;
@@ -463,6 +546,7 @@ export function toAnthropicContent(blocks: ContentBlock[]): Anthropic.ContentBlo
463
546
  result.push({
464
547
  type: 'thinking',
465
548
  thinking: block.thinking,
549
+ ...(block.signature ? { signature: block.signature } : {}),
466
550
  } as any);
467
551
  break;
468
552
  }
@@ -301,6 +301,10 @@ export class OpenAICompatibleAdapter implements ProviderAdapter {
301
301
  params.frequency_penalty = request.frequencyPenalty;
302
302
  }
303
303
 
304
+ if (request.repetitionPenalty !== undefined) {
305
+ params.repetition_penalty = request.repetitionPenalty;
306
+ }
307
+
304
308
  // OpenAI-compatible APIs may limit stop sequences (OpenAI: 4) — truncate to be safe
305
309
  if (request.stopSequences && request.stopSequences.length > 0) {
306
310
  params.stop = request.stopSequences.slice(0, 4);
@@ -41,6 +41,7 @@ interface CompletionsRequest {
41
41
  top_p?: number;
42
42
  presence_penalty?: number;
43
43
  frequency_penalty?: number;
44
+ repetition_penalty?: number;
44
45
  stop?: string[];
45
46
  stream?: boolean;
46
47
  }
@@ -194,6 +195,19 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
194
195
  let accumulated = '';
195
196
  let finishReason = 'stop';
196
197
 
198
+ // Post-facto truncation of the adapter's own eotToken.
199
+ // The adapter serializes the prompt with this.eotToken and sends it as an
200
+ // API stop string, but some backends leak the stop string into streamed
201
+ // output. Since the bot-level formatter may use a different (or empty)
202
+ // turn-end token, downstream post-facto checks can't be relied on to
203
+ // catch it — the layer that introduced the token must truncate it.
204
+ // emittedLen tracks how much of `accumulated` has been emitted; a tail of
205
+ // eot.length-1 chars is held back in case the token is split across chunks.
206
+ const eot = this.eotToken;
207
+ let emittedLen = 0;
208
+ let eotFound = false;
209
+
210
+ streamLoop:
197
211
  while (true) {
198
212
  const { done, value } = await reader.read();
199
213
  if (done) break;
@@ -210,7 +224,28 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
210
224
 
211
225
  if (text) {
212
226
  accumulated += text;
213
- callbacks.onChunk(text);
227
+ if (eot) {
228
+ const idx = accumulated.indexOf(eot);
229
+ if (idx !== -1) {
230
+ // Truncate at the token, flush the un-emitted prefix, stop
231
+ accumulated = accumulated.slice(0, idx);
232
+ if (accumulated.length > emittedLen) {
233
+ callbacks.onChunk(accumulated.slice(emittedLen));
234
+ }
235
+ emittedLen = accumulated.length;
236
+ eotFound = true;
237
+ finishReason = 'stop';
238
+ break streamLoop;
239
+ }
240
+ // Emit all but a held-back tail that could be a partial token
241
+ const safeLen = Math.max(emittedLen, accumulated.length - (eot.length - 1));
242
+ if (safeLen > emittedLen) {
243
+ callbacks.onChunk(accumulated.slice(emittedLen, safeLen));
244
+ emittedLen = safeLen;
245
+ }
246
+ } else {
247
+ callbacks.onChunk(text);
248
+ }
214
249
  }
215
250
 
216
251
  if (parsed.choices?.[0]?.finish_reason) {
@@ -222,6 +257,14 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
222
257
  }
223
258
  }
224
259
 
260
+ // Flush any held-back tail if the token never completed
261
+ if (eot && !eotFound && accumulated.length > emittedLen) {
262
+ callbacks.onChunk(accumulated.slice(emittedLen));
263
+ }
264
+ if (eotFound) {
265
+ try { await reader.cancel(); } catch { /* stream already closed */ }
266
+ }
267
+
225
268
  return this.buildStreamedResponse(accumulated, finishReason, request.model, completionsRequest);
226
269
 
227
270
  } catch (error) {
@@ -383,6 +426,10 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
383
426
  params.frequency_penalty = request.frequencyPenalty;
384
427
  }
385
428
 
429
+ if (request.repetitionPenalty !== undefined) {
430
+ params.repetition_penalty = request.repetitionPenalty;
431
+ }
432
+
386
433
  if (stopSequences.length > 0) {
387
434
  params.stop = stopSequences;
388
435
  }
@@ -419,7 +466,16 @@ export class OpenAICompletionsAdapter implements ProviderAdapter {
419
466
 
420
467
  private parseResponse(response: CompletionsResponse, requestedModel: string, rawRequest: unknown): ProviderResponse {
421
468
  const choice = response.choices[0];
422
- const text = choice?.text ?? '';
469
+ let text = choice?.text ?? '';
470
+
471
+ // Post-facto truncation of the adapter's own eotToken — some backends
472
+ // leak the stop string into the output (see stream() for details)
473
+ if (this.eotToken) {
474
+ const idx = text.indexOf(this.eotToken);
475
+ if (idx !== -1) {
476
+ text = text.slice(0, idx);
477
+ }
478
+ }
423
479
 
424
480
  return {
425
481
  content: this.textToContent(text),
@@ -399,6 +399,10 @@ export class OpenAIAdapter implements ProviderAdapter {
399
399
  params.frequency_penalty = request.frequencyPenalty;
400
400
  }
401
401
 
402
+ if (request.repetitionPenalty !== undefined) {
403
+ params.repetition_penalty = request.repetitionPenalty;
404
+ }
405
+
402
406
  // Reasoning models (o1, o3, o4) don't support stop sequences
403
407
  // OpenAI limits stop sequences to 4 — truncate to fit
404
408
  if (request.stopSequences && request.stopSequences.length > 0 && !noStopSupport(model)) {