@animalabs/membrane 0.5.50 → 0.5.52

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.
Files changed (123) hide show
  1. package/dist/context/index.d.ts +12 -0
  2. package/dist/context/index.js +11 -0
  3. package/dist/context/index.js.map +1 -0
  4. package/dist/context/process.d.ts +43 -0
  5. package/dist/context/process.js +381 -0
  6. package/dist/context/process.js.map +1 -0
  7. package/dist/context/types.d.ts +164 -0
  8. package/dist/context/types.js +61 -0
  9. package/dist/context/types.js.map +1 -0
  10. package/dist/formatters/anthropic-xml.d.ts +63 -0
  11. package/dist/formatters/anthropic-xml.js +417 -0
  12. package/dist/formatters/anthropic-xml.js.map +1 -0
  13. package/dist/formatters/completions.d.ts +68 -0
  14. package/dist/formatters/completions.js +261 -0
  15. package/dist/formatters/completions.js.map +1 -0
  16. package/dist/formatters/index.d.ts +8 -0
  17. package/dist/formatters/index.js +7 -0
  18. package/dist/formatters/index.js.map +1 -0
  19. package/dist/formatters/native.d.ts +35 -0
  20. package/dist/formatters/native.js +336 -0
  21. package/dist/formatters/native.js.map +1 -0
  22. package/dist/formatters/types.d.ts +167 -0
  23. package/dist/formatters/types.js +7 -0
  24. package/dist/formatters/types.js.map +1 -0
  25. package/dist/index.d.ts +13 -0
  26. package/dist/index.js +20 -0
  27. package/dist/index.js.map +1 -0
  28. package/dist/membrane.d.ts +155 -0
  29. package/dist/providers/anthropic.d.ts +36 -0
  30. package/dist/providers/bedrock.d.ts +43 -0
  31. package/dist/providers/gemini.d.ts +68 -0
  32. package/dist/providers/gemini.js +538 -0
  33. package/dist/providers/gemini.js.map +1 -0
  34. package/dist/providers/index.d.ts +13 -0
  35. package/dist/providers/index.js +13 -0
  36. package/dist/providers/index.js.map +1 -0
  37. package/dist/providers/mock.d.ts +90 -0
  38. package/dist/providers/mock.d.ts.map +1 -0
  39. package/dist/providers/mock.js +210 -0
  40. package/dist/providers/mock.js.map +1 -0
  41. package/dist/providers/openai-compatible.d.ts +82 -0
  42. package/dist/providers/openai-compatible.js +480 -0
  43. package/dist/providers/openai-compatible.js.map +1 -0
  44. package/dist/providers/openai-completions.d.ts +89 -0
  45. package/dist/providers/openai-completions.js +347 -0
  46. package/dist/providers/openai-completions.js.map +1 -0
  47. package/dist/providers/openai-responses.d.ts +77 -0
  48. package/dist/providers/openai-responses.js +333 -0
  49. package/dist/providers/openai-responses.js.map +1 -0
  50. package/dist/providers/openai.d.ts +77 -0
  51. package/dist/providers/openai.js +533 -0
  52. package/dist/providers/openai.js.map +1 -0
  53. package/dist/providers/openrouter.d.ts +82 -0
  54. package/dist/providers/openrouter.js +556 -0
  55. package/dist/providers/openrouter.js.map +1 -0
  56. package/dist/providers/utils.d.ts +44 -0
  57. package/dist/providers/utils.d.ts.map +1 -0
  58. package/dist/providers/utils.js +100 -0
  59. package/dist/providers/utils.js.map +1 -0
  60. package/dist/registry/default-pricing.d.ts +3 -0
  61. package/dist/registry/default-pricing.d.ts.map +1 -0
  62. package/dist/registry/default-pricing.js +75 -0
  63. package/dist/registry/default-pricing.js.map +1 -0
  64. package/dist/transforms/chat.d.ts +52 -0
  65. package/dist/transforms/chat.js +136 -0
  66. package/dist/transforms/chat.js.map +1 -0
  67. package/dist/transforms/index.d.ts +5 -0
  68. package/dist/transforms/index.js +7 -0
  69. package/dist/transforms/index.js.map +1 -0
  70. package/dist/types/config.d.ts +110 -0
  71. package/dist/types/config.js +21 -0
  72. package/dist/types/config.js.map +1 -0
  73. package/dist/types/content.d.ts +87 -0
  74. package/dist/types/content.d.ts.map +1 -0
  75. package/dist/types/content.js +40 -0
  76. package/dist/types/content.js.map +1 -0
  77. package/dist/types/errors.d.ts +50 -0
  78. package/dist/types/errors.d.ts.map +1 -0
  79. package/dist/types/errors.js +253 -0
  80. package/dist/types/errors.js.map +1 -0
  81. package/dist/types/index.d.ts +20 -0
  82. package/dist/types/index.js +10 -0
  83. package/dist/types/index.js.map +1 -0
  84. package/dist/types/message.d.ts +52 -0
  85. package/dist/types/message.d.ts.map +1 -0
  86. package/dist/types/message.js +38 -0
  87. package/dist/types/message.js.map +1 -0
  88. package/dist/types/provider.d.ts +169 -0
  89. package/dist/types/provider.d.ts.map +1 -0
  90. package/dist/types/provider.js +5 -0
  91. package/dist/types/provider.js.map +1 -0
  92. package/dist/types/request.d.ts +116 -0
  93. package/dist/types/request.d.ts.map +1 -0
  94. package/dist/types/request.js +5 -0
  95. package/dist/types/request.js.map +1 -0
  96. package/dist/types/response.d.ts +131 -0
  97. package/dist/types/response.d.ts.map +1 -0
  98. package/dist/types/response.js +7 -0
  99. package/dist/types/response.js.map +1 -0
  100. package/dist/types/streaming.d.ts +194 -0
  101. package/dist/types/streaming.js +5 -0
  102. package/dist/types/streaming.js.map +1 -0
  103. package/dist/types/tools.d.ts +71 -0
  104. package/dist/types/tools.d.ts.map +1 -0
  105. package/dist/types/tools.js +5 -0
  106. package/dist/types/tools.js.map +1 -0
  107. package/dist/utils/cost.d.ts +10 -0
  108. package/dist/utils/cost.d.ts.map +1 -0
  109. package/dist/utils/cost.js +19 -0
  110. package/dist/utils/cost.js.map +1 -0
  111. package/dist/utils/index.d.ts +7 -0
  112. package/dist/utils/index.js +6 -0
  113. package/dist/utils/index.js.map +1 -0
  114. package/dist/utils/stream-parser.d.ts +84 -0
  115. package/dist/utils/stream-parser.js +418 -0
  116. package/dist/utils/stream-parser.js.map +1 -0
  117. package/dist/utils/tool-parser.d.ts +134 -0
  118. package/dist/utils/tool-parser.js +600 -0
  119. package/dist/utils/tool-parser.js.map +1 -0
  120. package/dist/yielding-stream.d.ts +60 -0
  121. package/dist/yielding-stream.js +206 -0
  122. package/dist/yielding-stream.js.map +1 -0
  123. package/package.json +1 -1
@@ -0,0 +1,100 @@
1
+ /**
2
+ * Safely parse a JSON string, returning an empty object on failure.
3
+ * Used for tool call arguments which may be malformed from streaming.
4
+ */
5
+ export function safeParseJson(str) {
6
+ try {
7
+ return JSON.parse(str || '{}');
8
+ }
9
+ catch (e) {
10
+ console.warn('[membrane] Failed to parse tool arguments JSON:', e);
11
+ return {};
12
+ }
13
+ }
14
+ /**
15
+ * Create a combined AbortSignal that fires on either the caller's signal
16
+ * or a timeout (whichever comes first).
17
+ *
18
+ * The returned `cleanup` function MUST be called in a `finally` block to
19
+ * clear the timeout and remove the event listener, preventing leaks.
20
+ *
21
+ * Timeout aborts with `DOMException('Request timed out', 'AbortError')`
22
+ * so it classifies identically to user-initiated aborts.
23
+ */
24
+ export function createCombinedSignal(signal, timeoutMs) {
25
+ if (!signal && !timeoutMs)
26
+ return {};
27
+ if (signal && !timeoutMs)
28
+ return { signal };
29
+ const controller = new AbortController();
30
+ let timeoutId;
31
+ if (timeoutMs) {
32
+ timeoutId = setTimeout(() => controller.abort(new DOMException('Request timed out', 'AbortError')), timeoutMs);
33
+ }
34
+ const onAbort = () => controller.abort(signal.reason);
35
+ if (signal) {
36
+ if (signal.aborted) {
37
+ controller.abort(signal.reason);
38
+ }
39
+ else {
40
+ signal.addEventListener('abort', onAbort, { once: true });
41
+ }
42
+ }
43
+ return {
44
+ signal: controller.signal,
45
+ cleanup: () => {
46
+ if (timeoutId)
47
+ clearTimeout(timeoutId);
48
+ if (signal)
49
+ signal.removeEventListener('abort', onAbort);
50
+ },
51
+ };
52
+ }
53
+ /**
54
+ * SSE (Server-Sent Events) line parser that correctly handles events
55
+ * split across multiple TCP chunks.
56
+ *
57
+ * The naive approach of `chunk.split('\n').filter(l => l.startsWith('data: '))`
58
+ * silently drops events when an SSE line spans two chunks:
59
+ * Chunk 1: `data: {"choices":[{"delta":{"content":"don'` (no newline — incomplete)
60
+ * Chunk 2: `t do that"}}]}\n` (doesn't start with `data: `)
61
+ * Result: the entire event is lost, causing "skipped words" in output.
62
+ *
63
+ * This parser buffers partial lines and only yields complete `data: ...` lines.
64
+ */
65
+ export class SSELineParser {
66
+ buffer = '';
67
+ /**
68
+ * Feed a raw chunk from the stream reader and get back complete SSE data lines.
69
+ * Each returned string is the content after `data: ` (e.g. the JSON payload or `[DONE]`).
70
+ */
71
+ feed(chunk) {
72
+ this.buffer += chunk;
73
+ const results = [];
74
+ // Split on newlines, keeping the last (potentially incomplete) segment in the buffer
75
+ const lines = this.buffer.split('\n');
76
+ this.buffer = lines.pop() || '';
77
+ for (const line of lines) {
78
+ const trimmed = line.trim();
79
+ if (trimmed.startsWith('data: ')) {
80
+ results.push(trimmed.slice(6));
81
+ }
82
+ // Skip empty lines, comments (`:...`), and other SSE fields (event:, id:, retry:)
83
+ }
84
+ return results;
85
+ }
86
+ /**
87
+ * Flush any remaining buffered content (call when stream ends).
88
+ */
89
+ flush() {
90
+ if (!this.buffer.trim())
91
+ return [];
92
+ const trimmed = this.buffer.trim();
93
+ this.buffer = '';
94
+ if (trimmed.startsWith('data: ')) {
95
+ return [trimmed.slice(6)];
96
+ }
97
+ return [];
98
+ }
99
+ }
100
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/providers/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,GAAuB;IACnD,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,IAAI,CAAC,CAAC;IACjC,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,IAAI,CAAC,iDAAiD,EAAE,CAAC,CAAC,CAAC;QACnE,OAAO,EAAE,CAAC;IACZ,CAAC;AACH,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,oBAAoB,CAClC,MAAoB,EACpB,SAAkB;IAElB,IAAI,CAAC,MAAM,IAAI,CAAC,SAAS;QAAE,OAAO,EAAE,CAAC;IACrC,IAAI,MAAM,IAAI,CAAC,SAAS;QAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IAE5C,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,IAAI,SAAoD,CAAC;IAEzD,IAAI,SAAS,EA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IAAI,EAAE,CAAC;YAC5B,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;gBACjC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YACjC,CAAC;YACD,kFAAkF;QACpF,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE;YAAE,OAAO,EAAE,CAAC;QACnC,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;QACnC,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,OAAO,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC;YACjC,OAAO,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QAC5B,CAAC;QACD,OAAO,EAAE,CAAC;IACZ,CAAC;CACF"}
@@ -0,0 +1,3 @@
1
+ import type { ModelPricing } from '../types/provider.js';
2
+ export declare function getDefaultPricing(modelId: string): ModelPricing | undefined;
3
+ //# sourceMappingURL=default-pricing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-pricing.d.ts","sourceRoot":"","sources":["../../src/registry/default-pricing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAkEzD,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG,YAAY,GAAG,SAAS,CAU3E"}
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Built-in pricing table for known models.
3
+ * Prices in USD per million tokens. Last updated: 2025-07.
4
+ *
5
+ * Used as fallback when no ModelRegistry is configured.
6
+ * Registry pricing (if available) takes precedence.
7
+ */
8
+ const PRICING_TABLE = [
9
+ // Anthropic — Claude 4.6
10
+ {
11
+ prefix: 'claude-opus-4-6',
12
+ pricing: { inputPerMillion: 15, outputPerMillion: 75, cacheWritePerMillion: 18.75, cacheReadPerMillion: 1.50, currency: 'USD' },
13
+ },
14
+ {
15
+ prefix: 'claude-sonnet-4-6',
16
+ pricing: { inputPerMillion: 3, outputPerMillion: 15, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.30, currency: 'USD' },
17
+ },
18
+ // Anthropic — Claude 4.5
19
+ {
20
+ prefix: 'claude-haiku-4-5',
21
+ pricing: { inputPerMillion: 0.80, outputPerMillion: 4, cacheWritePerMillion: 1.00, cacheReadPerMillion: 0.08, currency: 'USD' },
22
+ },
23
+ // Anthropic — Claude 4
24
+ {
25
+ prefix: 'claude-opus-4',
26
+ pricing: { inputPerMillion: 15, outputPerMillion: 75, cacheWritePerMillion: 18.75, cacheReadPerMillion: 1.50, currency: 'USD' },
27
+ },
28
+ {
29
+ prefix: 'claude-sonnet-4',
30
+ pricing: { inputPerMillion: 3, outputPerMillion: 15, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.30, currency: 'USD' },
31
+ },
32
+ // Anthropic — Claude 3.5
33
+ {
34
+ prefix: 'claude-3-5-sonnet',
35
+ pricing: { inputPerMillion: 3, outputPerMillion: 15, cacheWritePerMillion: 3.75, cacheReadPerMillion: 0.30, currency: 'USD' },
36
+ },
37
+ {
38
+ prefix: 'claude-3-5-haiku',
39
+ pricing: { inputPerMillion: 0.80, outputPerMillion: 4, cacheWritePerMillion: 1.00, cacheReadPerMillion: 0.08, currency: 'USD' },
40
+ },
41
+ // OpenAI — GPT-4o
42
+ {
43
+ prefix: 'gpt-4o-2024',
44
+ pricing: { inputPerMillion: 2.50, outputPerMillion: 10, cacheReadPerMillion: 1.25, currency: 'USD' },
45
+ },
46
+ {
47
+ prefix: 'gpt-4o',
48
+ pricing: { inputPerMillion: 2.50, outputPerMillion: 10, cacheReadPerMillion: 1.25, currency: 'USD' },
49
+ },
50
+ {
51
+ prefix: 'gpt-4o-mini',
52
+ pricing: { inputPerMillion: 0.15, outputPerMillion: 0.60, cacheReadPerMillion: 0.075, currency: 'USD' },
53
+ },
54
+ // Google — Gemini 2.5
55
+ {
56
+ prefix: 'gemini-2.5-pro',
57
+ pricing: { inputPerMillion: 1.25, outputPerMillion: 10, currency: 'USD' },
58
+ },
59
+ {
60
+ prefix: 'gemini-2.5-flash',
61
+ pricing: { inputPerMillion: 0.15, outputPerMillion: 0.60, currency: 'USD' },
62
+ },
63
+ ];
64
+ export function getDefaultPricing(modelId) {
65
+ let best;
66
+ let bestLen = 0;
67
+ for (const entry of PRICING_TABLE) {
68
+ if (modelId.startsWith(entry.prefix) && entry.prefix.length > bestLen) {
69
+ best = entry.pricing;
70
+ bestLen = entry.prefix.length;
71
+ }
72
+ }
73
+ return best;
74
+ }
75
+ //# sourceMappingURL=default-pricing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"default-pricing.js","sourceRoot":"","sources":["../../src/registry/default-pricing.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,MAAM,aAAa,GAAqD;IACtE,yBAAyB;IACzB;QACE,MAAM,EAAE,iBAAiB;QACzB,OAAO,EAAE,EAAE,eAAe,EAAE,EAAE,EAAE,gBAAgB,EAAE,EAAE,EAAE,oBAAoB,EAAE,KAAK,EAAE,mBAAmB,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE;KAChI;IACD;QACE,MAAM,EAAE,mBAAmB;QAC3B,OAAO,EAAE,EAAE,eAAe,EAAE,CAAC,EAAE,gBAAgB,EAAE,EAAE,EAAE,oBAAoB,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE;KAC9H;IACD,yBAAyB;IACzB;QACE,MAAM,EAAE,kBAAkB;QAC1B,OAAO,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,gBAAgB,EAAE,CAAC,EAAE,oBAAoB,EAAE,IAAI,EAAE,mBAAmB,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE;KAChI;IACD,uBAAuB;I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AAE,QAAQ,EAAE,KAAK,EAAE;KACxG;IACD,sBAAsB;IACtB;QACE,MAAM,EAAE,gBAAgB;QACxB,OAAO,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,gBAAgB,EAAE,EAAE,EAAE,QAAQ,EAAE,KAAK,EAAE;KAC1E;IACD;QACE,MAAM,EAAE,kBAAkB;QAC1B,OAAO,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE;KAC5E;CACF,CAAC;AAEF,MAAM,UAAU,iBAAiB,CAAC,OAAe;IAC/C,IAAI,IAA8B,CAAC;IACnC,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE,CAAC;QAClC,IAAI,OAAO,CAAC,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,OAAO,EAAE,CAAC;YACtE,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC;YACrB,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC;QAChC,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Chat mode transforms
3
+ *
4
+ * Converts normalized messages to role-based alternating format
5
+ * for providers that don't support prefill mode.
6
+ */
7
+ import type { NormalizedRequest } from '../types/index.js';
8
+ export interface ChatMessage {
9
+ role: 'user' | 'assistant';
10
+ content: ChatContent[];
11
+ }
12
+ export type ChatContent = {
13
+ type: 'text';
14
+ text: string;
15
+ } | {
16
+ type: 'image';
17
+ source: {
18
+ type: 'base64';
19
+ data: string;
20
+ mediaType: string;
21
+ };
22
+ } | {
23
+ type: 'tool_use';
24
+ id: string;
25
+ name: string;
26
+ input: Record<string, unknown>;
27
+ } | {
28
+ type: 'tool_result';
29
+ tool_use_id: string;
30
+ content: string;
31
+ };
32
+ export interface ChatTransformResult {
33
+ /** System prompt */
34
+ system: string;
35
+ /** Messages in role-based format */
36
+ messages: ChatMessage[];
37
+ /** Stop sequences to use */
38
+ stopSequences: string[];
39
+ }
40
+ export interface ChatTransformOptions {
41
+ /** Name of the assistant participant (default: 'Claude') */
42
+ assistantName?: string;
43
+ /** Names to treat as 'user' role */
44
+ userParticipants?: string[];
45
+ /** How to handle multiple consecutive same-role messages */
46
+ mergeStrategy?: 'concatenate' | 'separate';
47
+ }
48
+ /**
49
+ * Transform normalized request to chat format
50
+ */
51
+ export declare function transformToChat(request: NormalizedRequest, options?: ChatTransformOptions): ChatTransformResult;
52
+ //# sourceMappingURL=chat.d.ts.map
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Chat mode transforms
3
+ *
4
+ * Converts normalized messages to role-based alternating format
5
+ * for providers that don't support prefill mode.
6
+ */
7
+ import { isTextContent, isToolUseContent, isToolResultContent } from '../types/index.js';
8
+ // ============================================================================
9
+ // Main Transform Function
10
+ // ============================================================================
11
+ /**
12
+ * Transform normalized request to chat format
13
+ */
14
+ export function transformToChat(request, options = {}) {
15
+ const { assistantName = 'Claude', userParticipants = [], mergeStrategy = 'concatenate', } = options;
16
+ const userSet = new Set(userParticipants);
17
+ const messages = [];
18
+ for (const message of request.messages) {
19
+ const role = determineRole(message.participant, assistantName, userSet);
20
+ const content = transformContent(message.content);
21
+ if (mergeStrategy === 'concatenate' && messages.length > 0) {
22
+ const lastMessage = messages[messages.length - 1];
23
+ if (lastMessage && lastMessage.role === role) {
24
+ // Merge with previous message
25
+ lastMessage.content.push(...content);
26
+ continue;
27
+ }
28
+ }
29
+ messages.push({ role, content });
30
+ }
31
+ // Ensure alternating roles (required by most providers)
32
+ const normalizedMessages = ensureAlternatingRoles(messages);
33
+ // Build stop sequences
34
+ const stopSequences = buildChatStopSequences(request);
35
+ return {
36
+ system: request.system ?? '',
37
+ messages: normalizedMessages,
38
+ stopSequences,
39
+ };
40
+ }
41
+ // ============================================================================
42
+ // Helper Functions
43
+ // ============================================================================
44
+ function determineRole(participant, assistantName, userSet) {
45
+ if (participant === assistantName) {
46
+ return 'assistant';
47
+ }
48
+ if (userSet.has(participant)) {
49
+ return 'user';
50
+ }
51
+ // Default: non-assistant is user
52
+ return 'user';
53
+ }
54
+ function transformContent(blocks) {
55
+ const result = [];
56
+ for (const block of blocks) {
57
+ if (isTextContent(block)) {
58
+ result.push({ type: 'text', text: block.text });
59
+ }
60
+ else if (block.type === 'image' && block.source.type === 'base64') {
61
+ result.push({
62
+ type: 'image',
63
+ source: {
64
+ type: 'base64',
65
+ data: block.source.data,
66
+ mediaType: block.source.mediaType,
67
+ },
68
+ });
69
+ }
70
+ else if (isToolUseContent(block)) {
71
+ result.push({
72
+ type: 'tool_use',
73
+ id: block.id,
74
+ name: block.name,
75
+ input: block.input,
76
+ });
77
+ }
78
+ else if (isToolResultContent(block)) {
79
+ const content = typeof block.content === 'string'
80
+ ? block.content
81
+ : JSON.stringify(block.content);
82
+ result.push({
83
+ type: 'tool_result',
84
+ tool_use_id: block.toolUseId,
85
+ content,
86
+ });
87
+ }
88
+ // Other block types are skipped or could be handled here
89
+ }
90
+ return result;
91
+ }
92
+ function ensureAlternatingRoles(messages) {
93
+ if (messages.length === 0)
94
+ return messages;
95
+ const result = [];
96
+ let lastRole = null;
97
+ for (const message of messages) {
98
+ if (lastRole === message.role) {
99
+ // Insert empty message of opposite role
100
+ const fillerRole = message.role === 'user' ? 'assistant' : 'user';
101
+ result.push({
102
+ role: fillerRole,
103
+ content: [{ type: 'text', text: '...' }],
104
+ });
105
+ }
106
+ result.push(message);
107
+ lastRole = message.role;
108
+ }
109
+ // Ensure starts with user
110
+ const first = result[0];
111
+ if (result.length > 0 && first && first.role === 'assistant') {
112
+ result.unshift({
113
+ role: 'user',
114
+ content: [{ type: 'text', text: '...' }],
115
+ });
116
+ }
117
+ return result;
118
+ }
119
+ function buildChatStopSequences(request) {
120
+ const sequences = [];
121
+ // Add tool-related stop if tools are defined
122
+ if (request.tools && request.tools.length > 0) {
123
+ sequences.push('</function_calls>');
124
+ }
125
+ // Add any explicit stop sequences from request
126
+ if (request.stopSequences) {
127
+ if (Array.isArray(request.stopSequences)) {
128
+ sequences.push(...request.stopSequences);
129
+ }
130
+ else {
131
+ sequences.push(...request.stopSequences.sequences);
132
+ }
133
+ }
134
+ return sequences;
135
+ }
136
+ //# sourceMappingURL=chat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.js","sourceRoot":"","sources":["../../src/transforms/chat.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,OAAO,EAAE,aAAa,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AA2CzF,+EAA+E;AAC/E,0BAA0B;AAC1B,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,eAAe,CAC7B,OAA0B,EAC1B,UAAgC,EAAE;IAElC,MAAM,EACJ,aAAa,GAAG,QAAQ,EACxB,gBAAgB,GAAG,EAAE,EACrB,aAAa,GAAG,aAAa,GAC9B,GAAG,OAAO,CAAC;IAEZ,MAAM,OAAO,GAAG,IAAI,GAAG,CAAC,gBAAgB,CAAC,CAAC;IAC1C,MAAM,QAAQ,GAAkB,EAAE,CAAC;IAEnC,KAAK,MAAM,OAAO,IAAI,OAAO,CAAC,QAAQ,EAAE,CAAC;QACvC,MAAM,IAAI,GAAG,aAAa,CAAC,OAAO,CAAC,WAAW,EAAE,aAAa,EAAE,OAAO,CAAC,CAAC;QACxE,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAElD,IAAI,aAAa,KAAK,aAAa,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC3D,MAAM,WAAW,GAAG,QAAQ,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;YAClD,IAAI,WAAW,IAAI,WAAW,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;gBAC7C,8BAA8B;gBAC9B,WA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AAC;AACnB,CAAC"}
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Transform exports
3
+ */
4
+ export { transformToChat, type ChatTransformResult, type ChatTransformOptions, type ChatMessage, type ChatContent, } from './chat.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Transform exports
3
+ */
4
+ // Note: transformToPrefill was removed in v0.4.0
5
+ // Use formatter.buildMessages() instead (see PrefillFormatter interface)
6
+ export { transformToChat, } from './chat.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/transforms/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,iDAAiD;AACjD,yEAAyE;AAEzE,OAAO,EACL,eAAe,GAKhB,MAAM,WAAW,CAAC"}
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Configuration types for membrane
3
+ */
4
+ import type { ModelRegistry } from './provider.js';
5
+ import type { ErrorInfo } from './errors.js';
6
+ import type { NormalizedRequest } from './request.js';
7
+ import type { NormalizedResponse } from './response.js';
8
+ import type { PrefillFormatter } from '../formatters/types.js';
9
+ export interface RetryConfig {
10
+ /** Maximum number of retry attempts (default: 3) */
11
+ maxRetries: number;
12
+ /** Initial retry delay in milliseconds (default: 1000) */
13
+ retryDelayMs: number;
14
+ /** Backoff multiplier (default: 2) */
15
+ backoffMultiplier: number;
16
+ /** Maximum retry delay (default: 30000) */
17
+ maxRetryDelayMs: number;
18
+ }
19
+ export interface MediaConfig {
20
+ images: {
21
+ /** Maximum input image size in bytes */
22
+ maxSizeBytes: number;
23
+ /** Maximum dimensions */
24
+ maxDimensions?: {
25
+ width: number;
26
+ height: number;
27
+ };
28
+ /** Auto-resize if exceeds limits */
29
+ autoResize: boolean;
30
+ /** JPEG quality for resizing (0-100) */
31
+ resizeQuality?: number;
32
+ /** Relocate images to user turns in prefill mode */
33
+ relocateInPrefillMode: boolean;
34
+ };
35
+ documents?: {
36
+ /** Maximum document size */
37
+ maxSizeBytes: number;
38
+ };
39
+ audio?: {
40
+ /** Maximum duration in seconds */
41
+ maxDurationSec: number;
42
+ };
43
+ video?: {
44
+ /** Maximum duration in seconds */
45
+ maxDurationSec: number;
46
+ };
47
+ }
48
+ export interface MembraneHooks {
49
+ /**
50
+ * Called before sending request to provider
51
+ * Can modify the raw request
52
+ */
53
+ beforeRequest?: (request: NormalizedRequest, rawRequest: unknown) => unknown | Promise<unknown>;
54
+ /**
55
+ * Called after receiving response from provider
56
+ * Can modify the response
57
+ */
58
+ afterResponse?: (response: NormalizedResponse, rawResponse: unknown) => NormalizedResponse | Promise<NormalizedResponse>;
59
+ /**
60
+ * Called on error, before retry decision
61
+ * Return 'retry' to retry, 'abort' to stop
62
+ */
63
+ onError?: (error: ErrorInfo, attempt: number) => 'retry' | 'abort' | Promise<'retry' | 'abort'>;
64
+ }
65
+ export interface MembraneLogger {
66
+ debug(message: string, data?: Record<string, unknown>): void;
67
+ info(message: string, data?: Record<string, unknown>): void;
68
+ warn(message: string, data?: Record<string, unknown>): void;
69
+ error(message: string, data?: Record<string, unknown>): void;
70
+ }
71
+ export interface MembraneConfig {
72
+ /** Model registry for capability lookup */
73
+ registry?: ModelRegistry;
74
+ /** Default model to use */
75
+ defaultModel?: string;
76
+ /**
77
+ * Participant name to recognize as assistant in prefill mode.
78
+ * Messages with this participant will be formatted as assistant turns.
79
+ * Default: 'Claude'
80
+ */
81
+ assistantParticipant?: string;
82
+ /**
83
+ * Maximum number of participants to include in auto-generated stop sequences.
84
+ * In prefill mode, membrane generates stop sequences like "\nUsername:" to prevent
85
+ * the model from speaking as other participants.
86
+ *
87
+ * Set to 0 to disable participant-based stop sequences (allows frags/quotes).
88
+ * Default: 10
89
+ */
90
+ maxParticipantsForStop?: number;
91
+ /**
92
+ * Prefill formatter for message serialization and response parsing.
93
+ * Controls how messages are formatted for the API and how responses are parsed.
94
+ * Default: AnthropicXmlFormatter
95
+ */
96
+ formatter?: PrefillFormatter;
97
+ /** Retry configuration */
98
+ retry?: Partial<RetryConfig>;
99
+ /** Media processing configuration */
100
+ media?: Partial<MediaConfig>;
101
+ /** Lifecycle hooks */
102
+ hooks?: MembraneHooks;
103
+ /** Logger instance */
104
+ logger?: MembraneLogger;
105
+ /** Enable debug logging */
106
+ debug?: boolean;
107
+ }
108
+ export declare const DEFAULT_RETRY_CONFIG: RetryConfig;
109
+ export declare const DEFAULT_MEDIA_CONFIG: MediaConfig;
110
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Configuration types for membrane
3
+ */
4
+ // ============================================================================
5
+ // Default Config
6
+ // ============================================================================
7
+ export const DEFAULT_RETRY_CONFIG = {
8
+ maxRetries: 0,
9
+ retryDelayMs: 1000,
10
+ backoffMultiplier: 2,
11
+ maxRetryDelayMs: 30000,
12
+ };
13
+ export const DEFAULT_MEDIA_CONFIG = {
14
+ images: {
15
+ maxSizeBytes: 5 * 1024 * 1024, // 5MB (Anthropic limit)
16
+ autoResize: true,
17
+ resizeQuality: 85,
18
+ relocateInPrefillMode: true,
19
+ },
20
+ };
21
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/types/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AA+JH,+EAA+E;AAC/E,iBAAiB;AACjB,+EAA+E;AAE/E,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C,UAAU,EAAE,CAAC;IACb,YAAY,EAAE,IAAI;IAClB,iBAAiB,EAAE,CAAC;IACpB,eAAe,EAAE,KAAK;CACvB,CAAC;AAEF,MAAM,CAAC,MAAM,oBAAoB,GAAgB;IAC/C,MAAM,EAAE;QACN,YAAY,EAAE,CAAC,GAAG,IAAI,GAAG,IAAI,EAAE,wBAAwB;QACvD,UAAU,EAAE,IAAI;QAChB,aAAa,EAAE,EAAE;QACjB,qBAAqB,EAAE,IAAI;KAC5B;CACF,CAAC"}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Content block types for normalized messages
3
+ */
4
+ export interface CacheControl {
5
+ type: 'ephemeral';
6
+ /** TTL for cache - '5m' (default) or '1h' for extended caching */
7
+ ttl?: '5m' | '1h';
8
+ }
9
+ export interface Base64Source {
10
+ type: 'base64';
11
+ data: string;
12
+ mediaType: string;
13
+ }
14
+ export interface UrlSource {
15
+ type: 'url';
16
+ url: string;
17
+ }
18
+ export type MediaSource = Base64Source | UrlSource;
19
+ export interface TextContent {
20
+ type: 'text';
21
+ text: string;
22
+ /** Cache control for Anthropic prompt caching */
23
+ cache_control?: CacheControl;
24
+ }
25
+ export interface ImageContent {
26
+ type: 'image';
27
+ source: MediaSource;
28
+ tokenEstimate?: number;
29
+ /** Original URL of the image (e.g., Discord CDN). Used by providers that
30
+ * can auto-fetch URLs from text (like Gemini 3.x) when inlineData is
31
+ * not viable (e.g., missing thought_signature on model-role images). */
32
+ sourceUrl?: string;
33
+ }
34
+ export interface DocumentContent {
35
+ type: 'document';
36
+ source: Base64Source;
37
+ filename?: string;
38
+ }
39
+ export interface AudioContent {
40
+ type: 'audio';
41
+ source: Base64Source;
42
+ duration?: number;
43
+ }
44
+ export interface VideoContent {
45
+ type: 'video';
46
+ source: Base64Source;
47
+ duration?: number;
48
+ }
49
+ export interface GeneratedImageContent {
50
+ type: 'generated_image';
51
+ data: string;
52
+ mimeType: string;
53
+ isPreview?: boolean;
54
+ }
55
+ export interface ToolUseContent {
56
+ type: 'tool_use';
57
+ id: string;
58
+ name: string;
59
+ input: Record<string, unknown>;
60
+ }
61
+ export interface ToolResultContent {
62
+ type: 'tool_result';
63
+ toolUseId: string;
64
+ content: string | ContentBlock[];
65
+ isError?: boolean;
66
+ }
67
+ export interface ThinkingContent {
68
+ type: 'thinking';
69
+ thinking: string;
70
+ signature?: string;
71
+ }
72
+ export interface RedactedThinkingContent {
73
+ type: 'redacted_thinking';
74
+ }
75
+ export type ContentBlock = TextContent | ImageContent | DocumentContent | AudioContent | VideoContent | GeneratedImageContent | ToolUseContent | ToolResultContent | ThinkingContent | RedactedThinkingContent;
76
+ export declare function isTextContent(block: ContentBlock): block is TextContent;
77
+ export declare function isImageContent(block: ContentBlock): block is ImageContent;
78
+ export declare function isDocumentContent(block: ContentBlock): block is DocumentContent;
79
+ export declare function isAudioContent(block: ContentBlock): block is AudioContent;
80
+ export declare function isVideoContent(block: ContentBlock): block is VideoContent;
81
+ export declare function isGeneratedImageContent(block: ContentBlock): block is GeneratedImageContent;
82
+ export declare function isToolUseContent(block: ContentBlock): block is ToolUseContent;
83
+ export declare function isToolResultContent(block: ContentBlock): block is ToolResultContent;
84
+ export declare function isThinkingContent(block: ContentBlock): block is ThinkingContent;
85
+ export declare function isRedactedThinkingContent(block: ContentBlock): block is RedactedThinkingContent;
86
+ export declare function isMediaContent(block: ContentBlock): block is ImageContent | DocumentContent | AudioContent | VideoContent;
87
+ //# sourceMappingURL=content.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"content.d.ts","sourceRoot":"","sources":["../../src/types/content.ts"],"names":[],"mappings":"AAAA;;GAEG;AAMH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,WAAW,CAAC;IAClB,kEAAkE;IAClE,GAAG,CAAC,EAAE,IAAI,GAAG,IAAI,CAAC;CACnB;AAMD,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,KAAK,CAAC;IACZ,GAAG,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,WAAW,GAAG,YAAY,GAAG,SAAS,CAAC;AAMnD,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,iDAAiD;IACjD,aAAa,CAAC,EAAE,YAAY,CAAC;CAC9B;AAMD,MAAM,WAAW,YAAY;IAC3B,IAAI,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