@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,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context management module
|
|
3
|
+
*
|
|
4
|
+
* Handles rolling context, cache marker placement, and state management
|
|
5
|
+
* for efficient prompt caching with LLMs.
|
|
6
|
+
*/
|
|
7
|
+
export { processContext } from './process.js';
|
|
8
|
+
export { createInitialState, defaultTokenEstimator, DEFAULT_CONTEXT_CONFIG, } from './types.js';
|
|
9
|
+
export type { ContextInput, ContextOutput, ContextState, ContextConfig, ContextInfo, ContextStreamOptions, CacheMarker, } from './types.js';
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,YAAY,CAAC;AAEpB,YAAY,EACV,YAAY,EACZ,aAAa,EACb,YAAY,EACZ,aAAa,EACb,WAAW,EACX,oBAAoB,EACpB,WAAW,GACZ,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context management module
|
|
3
|
+
*
|
|
4
|
+
* Handles rolling context, cache marker placement, and state management
|
|
5
|
+
* for efficient prompt caching with LLMs.
|
|
6
|
+
*/
|
|
7
|
+
export { processContext } from './process.js';
|
|
8
|
+
export { createInitialState, defaultTokenEstimator, DEFAULT_CONTEXT_CONFIG, } from './types.js';
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/context/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EACL,kBAAkB,EAClB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context processing - main entry point
|
|
3
|
+
*/
|
|
4
|
+
import type { Membrane } from '../membrane.js';
|
|
5
|
+
import type { ContextInput, ContextState, ContextOutput, ContextStreamOptions } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Process context and stream LLM response.
|
|
8
|
+
*
|
|
9
|
+
* This function handles:
|
|
10
|
+
* - Rolling/truncation based on thresholds
|
|
11
|
+
* - Cache marker placement for prompt caching
|
|
12
|
+
* - Hard limit enforcement
|
|
13
|
+
* - State management
|
|
14
|
+
*
|
|
15
|
+
* @param membrane - Configured Membrane instance
|
|
16
|
+
* @param input - Messages, config, and context settings
|
|
17
|
+
* @param state - Previous state (null for first call)
|
|
18
|
+
* @param options - Stream options
|
|
19
|
+
* @returns Response, updated state, and context info
|
|
20
|
+
*/
|
|
21
|
+
export declare function processContext(membrane: Membrane, input: ContextInput, state: ContextState | null, options?: ContextStreamOptions): Promise<ContextOutput>;
|
|
22
|
+
//# sourceMappingURL=process.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.d.ts","sourceRoot":"","sources":["../../src/context/process.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE/C,OAAO,KAAK,EACV,YAAY,EACZ,YAAY,EACZ,aAAa,EAGb,oBAAoB,EAErB,MAAM,YAAY,CAAC;AAWpB;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,YAAY,EACnB,KAAK,EAAE,YAAY,GAAG,IAAI,EAC1B,OAAO,CAAC,EAAE,oBAAoB,GAC7B,OAAO,CAAC,aAAa,CAAC,CAmHxB"}
|
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context processing - main entry point
|
|
3
|
+
*/
|
|
4
|
+
import { createInitialState, defaultTokenEstimator, DEFAULT_CONTEXT_CONFIG, } from './types.js';
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// Main Entry Point
|
|
7
|
+
// ============================================================================
|
|
8
|
+
/**
|
|
9
|
+
* Process context and stream LLM response.
|
|
10
|
+
*
|
|
11
|
+
* This function handles:
|
|
12
|
+
* - Rolling/truncation based on thresholds
|
|
13
|
+
* - Cache marker placement for prompt caching
|
|
14
|
+
* - Hard limit enforcement
|
|
15
|
+
* - State management
|
|
16
|
+
*
|
|
17
|
+
* @param membrane - Configured Membrane instance
|
|
18
|
+
* @param input - Messages, config, and context settings
|
|
19
|
+
* @param state - Previous state (null for first call)
|
|
20
|
+
* @param options - Stream options
|
|
21
|
+
* @returns Response, updated state, and context info
|
|
22
|
+
*/
|
|
23
|
+
export async function processContext(membrane, input, state, options) {
|
|
24
|
+
// Merge config with defaults
|
|
25
|
+
const contextConfig = mergeConfig(input.context);
|
|
26
|
+
const tokenEstimator = contextConfig.tokenEstimator ?? defaultTokenEstimator;
|
|
27
|
+
// Initialize or continue state
|
|
28
|
+
let currentState = state ?? createInitialState();
|
|
29
|
+
// Detect discontinuity (new conversation or branch switch)
|
|
30
|
+
const isDiscontinuous = detectDiscontinuity(input.messages, currentState);
|
|
31
|
+
if (isDiscontinuous) {
|
|
32
|
+
currentState = createInitialState();
|
|
33
|
+
}
|
|
34
|
+
// Calculate tokens for all messages
|
|
35
|
+
const messageTokens = input.messages.map(m => ({
|
|
36
|
+
message: m,
|
|
37
|
+
tokens: tokenEstimator(m),
|
|
38
|
+
id: getMessageId(m),
|
|
39
|
+
}));
|
|
40
|
+
const totalTokens = messageTokens.reduce((sum, m) => sum + m.tokens, 0);
|
|
41
|
+
const totalCharacters = calculateCharacters(input.messages);
|
|
42
|
+
// Determine if we should roll
|
|
43
|
+
const rollDecision = shouldRoll(currentState, input.messages.length, totalTokens, totalCharacters, contextConfig);
|
|
44
|
+
// Apply rolling/truncation if needed
|
|
45
|
+
let keptMessages = input.messages;
|
|
46
|
+
let messagesDropped = 0;
|
|
47
|
+
let didRoll = false;
|
|
48
|
+
let hardLimitHit = false;
|
|
49
|
+
if (rollDecision.shouldRoll) {
|
|
50
|
+
const truncateResult = truncateMessages(messageTokens, rollDecision.targetTokens, rollDecision.targetMessages, contextConfig);
|
|
51
|
+
keptMessages = truncateResult.kept.map(m => m.message);
|
|
52
|
+
messagesDropped = truncateResult.dropped;
|
|
53
|
+
didRoll = true;
|
|
54
|
+
hardLimitHit = rollDecision.reason === 'hard_limit';
|
|
55
|
+
}
|
|
56
|
+
// Recalculate tokens after truncation
|
|
57
|
+
const keptTokens = keptMessages.map(m => ({
|
|
58
|
+
message: m,
|
|
59
|
+
tokens: tokenEstimator(m),
|
|
60
|
+
id: getMessageId(m),
|
|
61
|
+
}));
|
|
62
|
+
const keptTotalTokens = keptTokens.reduce((sum, m) => sum + m.tokens, 0);
|
|
63
|
+
// Place cache markers
|
|
64
|
+
const cacheMarkers = placeCacheMarkers(keptMessages, keptTokens, currentState, didRoll, contextConfig);
|
|
65
|
+
// Apply cache markers to messages
|
|
66
|
+
const messagesWithCache = applyCacheMarkers(keptMessages, cacheMarkers);
|
|
67
|
+
// Calculate cached/uncached tokens
|
|
68
|
+
const lastMarker = cacheMarkers[cacheMarkers.length - 1];
|
|
69
|
+
const cachedTokens = lastMarker?.tokenEstimate ?? 0;
|
|
70
|
+
const uncachedTokens = keptTotalTokens - cachedTokens;
|
|
71
|
+
// Build request
|
|
72
|
+
const request = {
|
|
73
|
+
messages: messagesWithCache,
|
|
74
|
+
system: input.system,
|
|
75
|
+
tools: input.tools,
|
|
76
|
+
config: input.config,
|
|
77
|
+
};
|
|
78
|
+
// Stream response
|
|
79
|
+
const response = await membrane.stream(request, {
|
|
80
|
+
onChunk: options?.onChunk,
|
|
81
|
+
signal: options?.signal,
|
|
82
|
+
});
|
|
83
|
+
// Update state
|
|
84
|
+
const newState = {
|
|
85
|
+
cacheMarkers,
|
|
86
|
+
windowMessageIds: keptMessages.map(m => getMessageId(m)),
|
|
87
|
+
messagesSinceRoll: didRoll ? 1 : currentState.messagesSinceRoll + 1,
|
|
88
|
+
tokensSinceRoll: didRoll ? keptTotalTokens : currentState.tokensSinceRoll + keptTotalTokens,
|
|
89
|
+
inGracePeriod: rollDecision.enteredGrace || (currentState.inGracePeriod && !didRoll),
|
|
90
|
+
lastRollTime: didRoll ? new Date().toISOString() : currentState.lastRollTime,
|
|
91
|
+
};
|
|
92
|
+
// Build info
|
|
93
|
+
const info = {
|
|
94
|
+
didRoll,
|
|
95
|
+
messagesDropped,
|
|
96
|
+
messagesKept: keptMessages.length,
|
|
97
|
+
cacheMarkers,
|
|
98
|
+
cachedTokens,
|
|
99
|
+
uncachedTokens,
|
|
100
|
+
totalTokens: keptTotalTokens,
|
|
101
|
+
hardLimitHit,
|
|
102
|
+
};
|
|
103
|
+
return { response, state: newState, info };
|
|
104
|
+
}
|
|
105
|
+
// ============================================================================
|
|
106
|
+
// Helper Functions
|
|
107
|
+
// ============================================================================
|
|
108
|
+
function mergeConfig(config) {
|
|
109
|
+
return {
|
|
110
|
+
rolling: {
|
|
111
|
+
...DEFAULT_CONTEXT_CONFIG.rolling,
|
|
112
|
+
...config.rolling,
|
|
113
|
+
},
|
|
114
|
+
limits: {
|
|
115
|
+
...DEFAULT_CONTEXT_CONFIG.limits,
|
|
116
|
+
...config.limits,
|
|
117
|
+
},
|
|
118
|
+
cache: {
|
|
119
|
+
...DEFAULT_CONTEXT_CONFIG.cache,
|
|
120
|
+
...config.cache,
|
|
121
|
+
},
|
|
122
|
+
tokenEstimator: config.tokenEstimator,
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function getMessageId(message) {
|
|
126
|
+
return message.metadata?.sourceId ?? `msg-${Math.random().toString(36).slice(2)}`;
|
|
127
|
+
}
|
|
128
|
+
function detectDiscontinuity(messages, state) {
|
|
129
|
+
if (state.windowMessageIds.length === 0) {
|
|
130
|
+
return false; // First call, not a discontinuity
|
|
131
|
+
}
|
|
132
|
+
const currentIds = new Set(messages.map(m => getMessageId(m)));
|
|
133
|
+
const overlap = state.windowMessageIds.filter(id => currentIds.has(id));
|
|
134
|
+
// If less than 50% overlap, consider it a new conversation
|
|
135
|
+
return overlap.length < state.windowMessageIds.length * 0.5;
|
|
136
|
+
}
|
|
137
|
+
function calculateCharacters(messages) {
|
|
138
|
+
let chars = 0;
|
|
139
|
+
for (const msg of messages) {
|
|
140
|
+
for (const block of msg.content) {
|
|
141
|
+
if (block.type === 'text') {
|
|
142
|
+
chars += block.text.length;
|
|
143
|
+
}
|
|
144
|
+
else if (block.type === 'tool_result') {
|
|
145
|
+
const content = typeof block.content === 'string'
|
|
146
|
+
? block.content
|
|
147
|
+
: JSON.stringify(block.content);
|
|
148
|
+
chars += content.length;
|
|
149
|
+
}
|
|
150
|
+
// Images not counted for character limits
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return chars;
|
|
154
|
+
}
|
|
155
|
+
function shouldRoll(state, messageCount, totalTokens, totalCharacters, config) {
|
|
156
|
+
const { rolling, limits } = config;
|
|
157
|
+
const unit = rolling.unit ?? 'messages';
|
|
158
|
+
const threshold = rolling.threshold;
|
|
159
|
+
const grace = rolling.grace ?? 0;
|
|
160
|
+
const maxThreshold = threshold + grace;
|
|
161
|
+
// Check hard limits first (always enforced)
|
|
162
|
+
if (limits?.maxCharacters && totalCharacters > limits.maxCharacters) {
|
|
163
|
+
return {
|
|
164
|
+
shouldRoll: true,
|
|
165
|
+
reason: 'hard_limit',
|
|
166
|
+
targetTokens: limits.maxTokens,
|
|
167
|
+
targetMessages: limits.maxMessages,
|
|
168
|
+
enteredGrace: false,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (limits?.maxTokens && totalTokens > limits.maxTokens) {
|
|
172
|
+
return {
|
|
173
|
+
shouldRoll: true,
|
|
174
|
+
reason: 'hard_limit',
|
|
175
|
+
targetTokens: limits.maxTokens,
|
|
176
|
+
targetMessages: limits.maxMessages,
|
|
177
|
+
enteredGrace: false,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
if (limits?.maxMessages && messageCount > limits.maxMessages) {
|
|
181
|
+
return {
|
|
182
|
+
shouldRoll: true,
|
|
183
|
+
reason: 'hard_limit',
|
|
184
|
+
targetTokens: limits.maxTokens,
|
|
185
|
+
targetMessages: limits.maxMessages,
|
|
186
|
+
enteredGrace: false,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
// Check rolling threshold
|
|
190
|
+
const current = unit === 'messages' ? state.messagesSinceRoll : state.tokensSinceRoll;
|
|
191
|
+
if (current >= maxThreshold) {
|
|
192
|
+
// Exceeded grace, must roll
|
|
193
|
+
return {
|
|
194
|
+
shouldRoll: true,
|
|
195
|
+
reason: 'grace_exceeded',
|
|
196
|
+
targetTokens: unit === 'tokens' ? threshold : undefined,
|
|
197
|
+
targetMessages: unit === 'messages' ? threshold : undefined,
|
|
198
|
+
enteredGrace: false,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (!state.inGracePeriod && current >= threshold) {
|
|
202
|
+
// Just entered grace period
|
|
203
|
+
return {
|
|
204
|
+
shouldRoll: false,
|
|
205
|
+
enteredGrace: true,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
shouldRoll: false,
|
|
210
|
+
enteredGrace: false,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
function truncateMessages(messages, targetTokens, targetMessages, config) {
|
|
214
|
+
// Truncate from the beginning, keeping most recent
|
|
215
|
+
if (targetMessages && messages.length > targetMessages) {
|
|
216
|
+
const startIdx = messages.length - targetMessages;
|
|
217
|
+
return {
|
|
218
|
+
kept: messages.slice(startIdx),
|
|
219
|
+
dropped: startIdx,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (targetTokens) {
|
|
223
|
+
let tokenSum = 0;
|
|
224
|
+
let startIdx = messages.length;
|
|
225
|
+
// Count from end backwards
|
|
226
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
227
|
+
tokenSum += messages[i].tokens;
|
|
228
|
+
if (tokenSum > targetTokens) {
|
|
229
|
+
startIdx = i + 1;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
startIdx = i;
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
kept: messages.slice(startIdx),
|
|
236
|
+
dropped: startIdx,
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
// Default: use buffer from config
|
|
240
|
+
const buffer = config?.rolling.buffer ?? 20;
|
|
241
|
+
const unit = config?.rolling.unit ?? 'messages';
|
|
242
|
+
if (unit === 'messages') {
|
|
243
|
+
const targetCount = Math.max(buffer * 2, messages.length - buffer);
|
|
244
|
+
if (messages.length > targetCount) {
|
|
245
|
+
const startIdx = messages.length - targetCount;
|
|
246
|
+
return {
|
|
247
|
+
kept: messages.slice(startIdx),
|
|
248
|
+
dropped: startIdx,
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return { kept: messages, dropped: 0 };
|
|
253
|
+
}
|
|
254
|
+
function placeCacheMarkers(messages, messageTokens, state, didRoll, config) {
|
|
255
|
+
const cacheConfig = config.cache ?? {};
|
|
256
|
+
if (cacheConfig.enabled === false) {
|
|
257
|
+
return [];
|
|
258
|
+
}
|
|
259
|
+
const numPoints = cacheConfig.points ?? 1;
|
|
260
|
+
const minTokens = cacheConfig.minTokens ?? 1024;
|
|
261
|
+
const preferUser = cacheConfig.preferUserMessages ?? true;
|
|
262
|
+
const totalTokens = messageTokens.reduce((sum, m) => sum + m.tokens, 0);
|
|
263
|
+
// Not enough tokens for caching
|
|
264
|
+
if (totalTokens < minTokens) {
|
|
265
|
+
return [];
|
|
266
|
+
}
|
|
267
|
+
// If we didn't roll, try to keep existing markers stable
|
|
268
|
+
if (!didRoll && state.cacheMarkers.length > 0) {
|
|
269
|
+
const currentIds = new Set(messages.map(m => getMessageId(m)));
|
|
270
|
+
const validMarkers = state.cacheMarkers.filter(m => currentIds.has(m.messageId));
|
|
271
|
+
if (validMarkers.length > 0) {
|
|
272
|
+
// Recalculate token estimates for valid markers
|
|
273
|
+
return validMarkers.map(marker => {
|
|
274
|
+
const idx = messages.findIndex(m => getMessageId(m) === marker.messageId);
|
|
275
|
+
const tokenEstimate = messageTokens
|
|
276
|
+
.slice(0, idx + 1)
|
|
277
|
+
.reduce((sum, m) => sum + m.tokens, 0);
|
|
278
|
+
return {
|
|
279
|
+
messageId: marker.messageId,
|
|
280
|
+
messageIndex: idx,
|
|
281
|
+
tokenEstimate,
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
// Place new markers using arithmetic positioning
|
|
287
|
+
const markers = [];
|
|
288
|
+
const buffer = config.rolling.buffer ?? 20;
|
|
289
|
+
// For single point: place at (length - buffer)
|
|
290
|
+
// For multiple points: distribute evenly in cacheable portion
|
|
291
|
+
const cacheableEnd = Math.max(0, messages.length - buffer);
|
|
292
|
+
if (cacheableEnd === 0) {
|
|
293
|
+
return []; // Nothing to cache
|
|
294
|
+
}
|
|
295
|
+
// Calculate step size for multiple cache points
|
|
296
|
+
const step = Math.floor(cacheableEnd / numPoints);
|
|
297
|
+
if (step === 0) {
|
|
298
|
+
return []; // Not enough messages for requested cache points
|
|
299
|
+
}
|
|
300
|
+
let runningTokens = 0;
|
|
301
|
+
let currentIdx = 0;
|
|
302
|
+
for (let point = 1; point <= numPoints; point++) {
|
|
303
|
+
const targetIdx = Math.min(point * step - 1, cacheableEnd - 1);
|
|
304
|
+
// Accumulate tokens up to target
|
|
305
|
+
while (currentIdx <= targetIdx && currentIdx < messageTokens.length) {
|
|
306
|
+
runningTokens += messageTokens[currentIdx].tokens;
|
|
307
|
+
currentIdx++;
|
|
308
|
+
}
|
|
309
|
+
let markerIdx = targetIdx;
|
|
310
|
+
let markerTokens = runningTokens;
|
|
311
|
+
// Adjust to user message if preferred
|
|
312
|
+
if (preferUser) {
|
|
313
|
+
const adjusted = findNearestUserMessage(messages, markerIdx, messageTokens);
|
|
314
|
+
if (adjusted) {
|
|
315
|
+
markerIdx = adjusted.index;
|
|
316
|
+
markerTokens = adjusted.tokens;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// Skip if below minimum
|
|
320
|
+
if (markerTokens < minTokens) {
|
|
321
|
+
continue;
|
|
322
|
+
}
|
|
323
|
+
// Skip if duplicate
|
|
324
|
+
if (markers.some(m => m.messageIndex === markerIdx)) {
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
markers.push({
|
|
328
|
+
messageId: getMessageId(messages[markerIdx]),
|
|
329
|
+
messageIndex: markerIdx,
|
|
330
|
+
tokenEstimate: markerTokens,
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
return markers;
|
|
334
|
+
}
|
|
335
|
+
function findNearestUserMessage(messages, startIdx, messageTokens) {
|
|
336
|
+
// Search backwards for a user message (non-assistant participant)
|
|
337
|
+
const maxSearch = 5;
|
|
338
|
+
let tokens = messageTokens.slice(0, startIdx + 1).reduce((sum, m) => sum + m.tokens, 0);
|
|
339
|
+
for (let i = startIdx; i >= Math.max(0, startIdx - maxSearch); i--) {
|
|
340
|
+
const msg = messages[i];
|
|
341
|
+
// Heuristic: if participant isn't a common assistant name, it's probably a user
|
|
342
|
+
const participant = msg.participant.toLowerCase();
|
|
343
|
+
const isUser = !['claude', 'assistant', 'bot', 'ai'].includes(participant);
|
|
344
|
+
if (isUser) {
|
|
345
|
+
return { index: i, tokens };
|
|
346
|
+
}
|
|
347
|
+
tokens -= messageTokens[i].tokens;
|
|
348
|
+
}
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
function applyCacheMarkers(messages, cacheMarkers) {
|
|
352
|
+
if (cacheMarkers.length === 0) {
|
|
353
|
+
return messages;
|
|
354
|
+
}
|
|
355
|
+
const markerIndices = new Set(cacheMarkers.map(m => m.messageIndex));
|
|
356
|
+
return messages.map((msg, idx) => {
|
|
357
|
+
if (markerIndices.has(idx)) {
|
|
358
|
+
return {
|
|
359
|
+
...msg,
|
|
360
|
+
metadata: {
|
|
361
|
+
...msg.metadata,
|
|
362
|
+
cacheControl: { type: 'ephemeral' },
|
|
363
|
+
},
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
return msg;
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
//# sourceMappingURL=process.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process.js","sourceRoot":"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AO,QAAQ,CAAC;IAClB,CAAC;IAED,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC;IAErE,OAAO,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC/B,IAAI,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAC3B,OAAO;gBACL,GAAG,GAAG;gBACN,QAAQ,EAAE;oBACR,GAAG,GAAG,CAAC,QAAQ;oBACf,YAAY,EAAE,EAAE,IAAI,EAAE,WAAoB,EAAE;iBAC7C;aACF,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context management types
|
|
3
|
+
*/
|
|
4
|
+
import type { NormalizedMessage, NormalizedResponse, AbortedResponse, GenerationConfig, ToolDefinition } from '../types/index.js';
|
|
5
|
+
export interface CacheMarker {
|
|
6
|
+
/** Message ID (from metadata.sourceId) */
|
|
7
|
+
messageId: string;
|
|
8
|
+
/** Index in the message array */
|
|
9
|
+
messageIndex: number;
|
|
10
|
+
/** Estimated tokens up to this point */
|
|
11
|
+
tokenEstimate: number;
|
|
12
|
+
}
|
|
13
|
+
export interface ContextConfig {
|
|
14
|
+
/** Rolling configuration */
|
|
15
|
+
rolling: {
|
|
16
|
+
/** Threshold before roll triggers */
|
|
17
|
+
threshold: number;
|
|
18
|
+
/** Buffer to leave uncached after roll */
|
|
19
|
+
buffer: number;
|
|
20
|
+
/** Grace period before forced roll (optional) */
|
|
21
|
+
grace?: number;
|
|
22
|
+
/** Unit for threshold/buffer/grace (default: 'messages') */
|
|
23
|
+
unit?: 'messages' | 'tokens';
|
|
24
|
+
};
|
|
25
|
+
/** Hard limits (always enforced) */
|
|
26
|
+
limits?: {
|
|
27
|
+
/** Maximum characters (default: 500000) */
|
|
28
|
+
maxCharacters?: number;
|
|
29
|
+
/** Maximum tokens */
|
|
30
|
+
maxTokens?: number;
|
|
31
|
+
/** Maximum messages */
|
|
32
|
+
maxMessages?: number;
|
|
33
|
+
};
|
|
34
|
+
/** Cache settings */
|
|
35
|
+
cache?: {
|
|
36
|
+
/** Enable caching (default: true) */
|
|
37
|
+
enabled?: boolean;
|
|
38
|
+
/** Number of cache points (default: 1, max: 4 for Anthropic) */
|
|
39
|
+
points?: 1 | 2 | 3 | 4;
|
|
40
|
+
/** Minimum tokens before caching (default: 1024) */
|
|
41
|
+
minTokens?: number;
|
|
42
|
+
/** Prefer user messages for cache markers (OpenRouter workaround) */
|
|
43
|
+
preferUserMessages?: boolean;
|
|
44
|
+
};
|
|
45
|
+
/** Custom token estimator (default: chars / 4) */
|
|
46
|
+
tokenEstimator?: (message: NormalizedMessage) => number;
|
|
47
|
+
}
|
|
48
|
+
export interface ContextState {
|
|
49
|
+
/** Current cache markers */
|
|
50
|
+
cacheMarkers: CacheMarker[];
|
|
51
|
+
/** Message IDs in current window (for continuity detection) */
|
|
52
|
+
windowMessageIds: string[];
|
|
53
|
+
/** Messages since last roll */
|
|
54
|
+
messagesSinceRoll: number;
|
|
55
|
+
/** Tokens since last roll */
|
|
56
|
+
tokensSinceRoll: number;
|
|
57
|
+
/** Whether we're in grace period */
|
|
58
|
+
inGracePeriod: boolean;
|
|
59
|
+
/** Last roll timestamp (ISO string) */
|
|
60
|
+
lastRollTime?: string;
|
|
61
|
+
}
|
|
62
|
+
export interface ContextInput {
|
|
63
|
+
/** Conversation messages */
|
|
64
|
+
messages: NormalizedMessage[];
|
|
65
|
+
/** System prompt */
|
|
66
|
+
system?: string;
|
|
67
|
+
/** Tool definitions */
|
|
68
|
+
tools?: ToolDefinition[];
|
|
69
|
+
/** Generation config (model, maxTokens, etc.) */
|
|
70
|
+
config: GenerationConfig;
|
|
71
|
+
/** Context management config */
|
|
72
|
+
context: ContextConfig;
|
|
73
|
+
}
|
|
74
|
+
export interface ContextInfo {
|
|
75
|
+
/** Whether a roll occurred */
|
|
76
|
+
didRoll: boolean;
|
|
77
|
+
/** Number of messages dropped in roll */
|
|
78
|
+
messagesDropped: number;
|
|
79
|
+
/** Number of messages kept */
|
|
80
|
+
messagesKept: number;
|
|
81
|
+
/** Current cache markers */
|
|
82
|
+
cacheMarkers: CacheMarker[];
|
|
83
|
+
/** Estimated cached tokens */
|
|
84
|
+
cachedTokens: number;
|
|
85
|
+
/** Estimated uncached tokens */
|
|
86
|
+
uncachedTokens: number;
|
|
87
|
+
/** Total estimated tokens */
|
|
88
|
+
totalTokens: number;
|
|
89
|
+
/** Whether hard limit was hit */
|
|
90
|
+
hardLimitHit: boolean;
|
|
91
|
+
}
|
|
92
|
+
export interface ContextOutput {
|
|
93
|
+
/** The LLM response (may be aborted) */
|
|
94
|
+
response: NormalizedResponse | AbortedResponse;
|
|
95
|
+
/** Updated state (save this for next call) */
|
|
96
|
+
state: ContextState;
|
|
97
|
+
/** Info about what happened */
|
|
98
|
+
info: ContextInfo;
|
|
99
|
+
}
|
|
100
|
+
export interface ContextStreamOptions {
|
|
101
|
+
/** Callback for streaming chunks */
|
|
102
|
+
onChunk?: (chunk: string) => void;
|
|
103
|
+
/** Abort signal */
|
|
104
|
+
signal?: AbortSignal;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create initial empty state
|
|
108
|
+
*/
|
|
109
|
+
export declare function createInitialState(): ContextState;
|
|
110
|
+
/**
|
|
111
|
+
* Default token estimator (chars / 4)
|
|
112
|
+
*/
|
|
113
|
+
export declare function defaultTokenEstimator(message: NormalizedMessage): number;
|
|
114
|
+
/**
|
|
115
|
+
* Default context config
|
|
116
|
+
*/
|
|
117
|
+
export declare const DEFAULT_CONTEXT_CONFIG: ContextConfig;
|
|
118
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","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AAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;IAElC,mBAAmB;IACnB,MAAM,CAAC,EAAE,WAAW,CAAC;CACtB;AAMD;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,YAAY,CAQjD;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,iBAAiB,GAAG,MAAM,CAgBxE;AAED;;GAEG;AACH,eAAO,MAAM,sBAAsB,EAAE,aAepC,CAAC"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Context management types
|
|
3
|
+
*/
|
|
4
|
+
// ============================================================================
|
|
5
|
+
// Helpers
|
|
6
|
+
// ============================================================================
|
|
7
|
+
/**
|
|
8
|
+
* Create initial empty state
|
|
9
|
+
*/
|
|
10
|
+
export function createInitialState() {
|
|
11
|
+
return {
|
|
12
|
+
cacheMarkers: [],
|
|
13
|
+
windowMessageIds: [],
|
|
14
|
+
messagesSinceRoll: 0,
|
|
15
|
+
tokensSinceRoll: 0,
|
|
16
|
+
inGracePeriod: false,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Default token estimator (chars / 4)
|
|
21
|
+
*/
|
|
22
|
+
export function defaultTokenEstimator(message) {
|
|
23
|
+
let chars = 0;
|
|
24
|
+
for (const block of message.content) {
|
|
25
|
+
if (block.type === 'text') {
|
|
26
|
+
chars += block.text.length;
|
|
27
|
+
}
|
|
28
|
+
else if (block.type === 'tool_result') {
|
|
29
|
+
const content = typeof block.content === 'string'
|
|
30
|
+
? block.content
|
|
31
|
+
: JSON.stringify(block.content);
|
|
32
|
+
chars += content.length;
|
|
33
|
+
}
|
|
34
|
+
else if (block.type === 'image') {
|
|
35
|
+
// Images: ~1500 tokens regardless of size (Anthropic)
|
|
36
|
+
chars += 6000; // 1500 * 4
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return Math.ceil(chars / 4);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Default context config
|
|
43
|
+
*/
|
|
44
|
+
export const DEFAULT_CONTEXT_CONFIG = {
|
|
45
|
+
rolling: {
|
|
46
|
+
threshold: 50,
|
|
47
|
+
buffer: 20,
|
|
48
|
+
unit: 'messages',
|
|
49
|
+
},
|
|
50
|
+
limits: {
|
|
51
|
+
maxCharacters: 500000,
|
|
52
|
+
},
|
|
53
|
+
cache: {
|
|
54
|
+
enabled: true,
|
|
55
|
+
points: 1,
|
|
56
|
+
minTokens: 1024,
|
|
57
|
+
preferUserMessages: true,
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/context/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AA4KH,+EAA+E;AAC/E,UAAU;AACV,+EAA+E;AAE/E;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO;QACL,YAAY,EAAE,EAAE;QAChB,gBAAgB,EAAE,EAAE;QACpB,iBAAiB,EAAE,CAAC;QACpB,eAAe,EAAE,CAAC;QAClB,aAAa,EAAE,KAAK;KACrB,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,OAA0B;IAC9D,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,OAAO,EAAE,CAAC;QACpC,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM,EAAE,CAAC;YAC1B,KAAK,IAAI,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC;QAC7B,CAAC;aAAM,IAAI,KAAK,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;YACxC,MAAM,OAAO,GAAG,OAAO,KAAK,CAAC,OAAO,KAAK,QAAQ;gBAC/C,CAAC,CAAC,KAAK,CAAC,OAAO;gBACf,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAClC,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;QAC1B,CAAC;aAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAAE,CAAC;YAClC,sDAAsD;YACtD,KAAK,IAAI,IAAI,CAAC,CAAC,WAAW;QAC5B,CAAC;IACH,CAAC;IACD,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC;AAC9B,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAkB;IACnD,OAAO,EAAE;QACP,SAAS,EAAE,EAAE;QACb,MAAM,EAAE,EAAE;QACV,IAAI,EAAE,UAAU;KACjB;IACD,MAAM,EAAE;QACN,aAAa,EAAE,MAAM;KACtB;IACD,KAAK,EAAE;QACL,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,CAAC;QACT,SAAS,EAAE,IAAI;QACf,kBAAkB,EAAE,IAAI;KACzB;CACF,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Membrane - LLM middleware
|
|
3
|
+
*
|
|
4
|
+
* A selective boundary that transforms what passes through.
|
|
5
|
+
*/
|
|
6
|
+
export { Membrane } from './membrane.js';
|
|
7
|
+
export * from './types/index.js';
|
|
8
|
+
export * from './transforms/index.js';
|
|
9
|
+
export * from './providers/index.js';
|
|
10
|
+
export * from './utils/index.js';
|
|
11
|
+
export * from './context/index.js';
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,cAAc,kBAAkB,CAAC;AAGjC,cAAc,uBAAuB,CAAC;AAGtC,cAAc,sBAAsB,CAAC;AAGrC,cAAc,kBAAkB,CAAC;AAGjC,cAAc,oBAAoB,CAAC"}
|