@animalabs/context-manager 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.
Files changed (83) hide show
  1. package/dist/src/blob-manager.d.ts +37 -0
  2. package/dist/src/blob-manager.d.ts.map +1 -0
  3. package/dist/src/blob-manager.js +128 -0
  4. package/dist/src/blob-manager.js.map +1 -0
  5. package/dist/src/context-log.d.ts +99 -0
  6. package/dist/src/context-log.d.ts.map +1 -0
  7. package/dist/src/context-log.js +277 -0
  8. package/dist/src/context-log.js.map +1 -0
  9. package/dist/src/context-manager.d.ts +245 -0
  10. package/dist/src/context-manager.d.ts.map +1 -0
  11. package/dist/src/context-manager.js +553 -0
  12. package/dist/src/context-manager.js.map +1 -0
  13. package/dist/src/index.d.ts +12 -0
  14. package/dist/src/index.d.ts.map +1 -0
  15. package/dist/src/index.js +12 -0
  16. package/dist/src/index.js.map +1 -0
  17. package/dist/src/message-store.d.ts +135 -0
  18. package/dist/src/message-store.d.ts.map +1 -0
  19. package/dist/src/message-store.js +372 -0
  20. package/dist/src/message-store.js.map +1 -0
  21. package/dist/src/strategies/autobiographical.d.ts +199 -0
  22. package/dist/src/strategies/autobiographical.d.ts.map +1 -0
  23. package/dist/src/strategies/autobiographical.js +1122 -0
  24. package/dist/src/strategies/autobiographical.js.map +1 -0
  25. package/dist/src/strategies/index.d.ts +3 -0
  26. package/dist/src/strategies/index.d.ts.map +1 -0
  27. package/dist/src/strategies/index.js +3 -0
  28. package/dist/src/strategies/index.js.map +1 -0
  29. package/dist/src/strategies/knowledge.d.ts +46 -0
  30. package/dist/src/strategies/knowledge.d.ts.map +1 -0
  31. package/dist/src/strategies/knowledge.js +270 -0
  32. package/dist/src/strategies/knowledge.js.map +1 -0
  33. package/dist/src/strategies/passthrough.d.ts +17 -0
  34. package/dist/src/strategies/passthrough.d.ts.map +1 -0
  35. package/dist/src/strategies/passthrough.js +69 -0
  36. package/dist/src/strategies/passthrough.js.map +1 -0
  37. package/dist/src/types/context.d.ts +108 -0
  38. package/dist/src/types/context.d.ts.map +1 -0
  39. package/dist/src/types/context.js +2 -0
  40. package/dist/src/types/context.js.map +1 -0
  41. package/dist/src/types/index.d.ts +5 -0
  42. package/dist/src/types/index.d.ts.map +1 -0
  43. package/dist/src/types/index.js +2 -0
  44. package/dist/src/types/index.js.map +1 -0
  45. package/dist/src/types/message.d.ts +129 -0
  46. package/dist/src/types/message.d.ts.map +1 -0
  47. package/dist/src/types/message.js +2 -0
  48. package/dist/src/types/message.js.map +1 -0
  49. package/dist/src/types/strategy.d.ts +233 -0
  50. package/dist/src/types/strategy.d.ts.map +1 -0
  51. package/dist/src/types/strategy.js +32 -0
  52. package/dist/src/types/strategy.js.map +1 -0
  53. package/dist/test/autobiographical.test.d.ts +2 -0
  54. package/dist/test/autobiographical.test.d.ts.map +1 -0
  55. package/dist/test/autobiographical.test.js +46 -0
  56. package/dist/test/autobiographical.test.js.map +1 -0
  57. package/dist/test/head-window-reset.test.d.ts +17 -0
  58. package/dist/test/head-window-reset.test.d.ts.map +1 -0
  59. package/dist/test/head-window-reset.test.js +342 -0
  60. package/dist/test/head-window-reset.test.js.map +1 -0
  61. package/dist/test/integration.test.d.ts +2 -0
  62. package/dist/test/integration.test.d.ts.map +1 -0
  63. package/dist/test/integration.test.js +1341 -0
  64. package/dist/test/integration.test.js.map +1 -0
  65. package/dist/test/knowledge.test.d.ts +2 -0
  66. package/dist/test/knowledge.test.d.ts.map +1 -0
  67. package/dist/test/knowledge.test.js +617 -0
  68. package/dist/test/knowledge.test.js.map +1 -0
  69. package/dist/tsconfig.tsbuildinfo +1 -0
  70. package/package.json +48 -0
  71. package/src/blob-manager.ts +155 -0
  72. package/src/context-log.ts +342 -0
  73. package/src/context-manager.ts +726 -0
  74. package/src/index.ts +50 -0
  75. package/src/message-store.ts +479 -0
  76. package/src/strategies/autobiographical.ts +1355 -0
  77. package/src/strategies/index.ts +2 -0
  78. package/src/strategies/knowledge.ts +336 -0
  79. package/src/strategies/passthrough.ts +98 -0
  80. package/src/types/context.ts +119 -0
  81. package/src/types/index.ts +42 -0
  82. package/src/types/message.ts +140 -0
  83. package/src/types/strategy.ts +282 -0
@@ -0,0 +1,245 @@
1
+ import { JsStore } from '@animalabs/chronicle';
2
+ import type { Membrane, ContentBlock } from '@animalabs/membrane';
3
+ import type { MessageId, Sequence, MessageMetadata, StoredMessage, TokenBudget, PendingWork, BranchInfo, ContextStrategy, MessageQuery, MessageQueryResult, ContextInjection, CompileResult } from './types/index.js';
4
+ /**
5
+ * Base configuration for ContextManager.
6
+ */
7
+ interface ContextManagerBaseConfig {
8
+ /** Initial strategy (default: PassthroughStrategy) */
9
+ strategy?: ContextStrategy;
10
+ /** Membrane instance for compression strategies */
11
+ membrane?: Membrane;
12
+ /** Token estimator function */
13
+ tokenEstimator?: (text: string) => number;
14
+ /**
15
+ * Namespace for multi-agent support.
16
+ * When set, the context log uses state ID `{namespace}/context`.
17
+ * Messages remain shared (no namespace) unless `isolate` is true.
18
+ */
19
+ namespace?: string;
20
+ /**
21
+ * When true, the namespace applies to messages as well as the context log,
22
+ * giving fully isolated state: `{namespace}/messages` + `{namespace}/context`.
23
+ * Use for subagents that should not share message state with the parent.
24
+ * Requires `namespace` to be set.
25
+ */
26
+ isolate?: boolean;
27
+ /**
28
+ * When true, log the compiled context to stderr for debugging.
29
+ */
30
+ debugLogContext?: boolean;
31
+ }
32
+ /**
33
+ * Configuration when ContextManager creates and owns the store.
34
+ */
35
+ interface ContextManagerPathConfig extends ContextManagerBaseConfig {
36
+ /** Path to Chronicle store */
37
+ path: string;
38
+ /** Blob cache size (default: 1000) */
39
+ blobCacheSize?: number;
40
+ store?: never;
41
+ }
42
+ /**
43
+ * Configuration when app provides an existing store.
44
+ * App retains ownership and is responsible for closing the store.
45
+ */
46
+ interface ContextManagerStoreConfig extends ContextManagerBaseConfig {
47
+ /** Existing Chronicle store (app-owned) */
48
+ store: JsStore;
49
+ path?: never;
50
+ blobCacheSize?: never;
51
+ }
52
+ /**
53
+ * Configuration for ContextManager.
54
+ */
55
+ export type ContextManagerConfig = ContextManagerPathConfig | ContextManagerStoreConfig;
56
+ /**
57
+ * Context Manager - the main interface for managing conversation context.
58
+ *
59
+ * Sits between the application/agent layer and Membrane, managing what goes
60
+ * into the context window. Uses Chronicle for persistent storage.
61
+ */
62
+ export declare class ContextManager {
63
+ private store;
64
+ private messageStore;
65
+ private contextLog;
66
+ private strategy;
67
+ private membrane?;
68
+ private initialized;
69
+ /** Whether we own the store (created it) vs app owns it (passed in) */
70
+ private ownsStore;
71
+ private debugLogContext;
72
+ private constructor();
73
+ /**
74
+ * Open or create a context manager.
75
+ *
76
+ * Can be called with either:
77
+ * - `{ path: string }` - Creates and owns a new store
78
+ * - `{ store: JsStore }` - Uses an existing app-owned store
79
+ *
80
+ * When using an app-owned store, the app is responsible for closing it.
81
+ * The app can register additional states on the store before passing it.
82
+ */
83
+ static open(config: ContextManagerConfig): Promise<ContextManager>;
84
+ /**
85
+ * Add a message to the store.
86
+ */
87
+ addMessage(participant: string, content: ContentBlock[], metadata?: MessageMetadata, causedBy?: MessageId[]): MessageId;
88
+ /**
89
+ * Edit a message in the store. Propagates to context log based on source relation.
90
+ */
91
+ editMessage(messageId: MessageId, content: ContentBlock[]): void;
92
+ /**
93
+ * Remove a message from the store. Propagates to context log.
94
+ */
95
+ removeMessage(messageId: MessageId): void;
96
+ /**
97
+ * Remove a range of messages from the store.
98
+ */
99
+ removeMessages(fromId: MessageId, toId: MessageId): void;
100
+ /**
101
+ * Get a message by ID.
102
+ */
103
+ getMessage(messageId: MessageId): StoredMessage | null;
104
+ /**
105
+ * Get a message as it was at a specific sequence (time travel).
106
+ */
107
+ getMessageAt(messageId: MessageId, atSequence: Sequence): StoredMessage | null;
108
+ /**
109
+ * Get all messages in the store.
110
+ */
111
+ getAllMessages(): StoredMessage[];
112
+ /**
113
+ * Query messages by filter criteria.
114
+ * Useful for finding messages from external sources, by participant, etc.
115
+ *
116
+ * @example
117
+ * // Find all messages from Discord
118
+ * const { messages } = manager.queryMessages({ source: 'discord' });
119
+ *
120
+ * @example
121
+ * // Find messages from a specific channel
122
+ * const { messages } = manager.queryMessages({
123
+ * source: 'discord',
124
+ * metadata: { 'external.channelId': '123456' }
125
+ * });
126
+ *
127
+ * @example
128
+ * // Find specific messages by external ID
129
+ * const { messages } = manager.queryMessages({
130
+ * source: 'discord',
131
+ * externalIds: ['msg1', 'msg2', 'msg3']
132
+ * });
133
+ */
134
+ queryMessages(filter: MessageQuery): MessageQueryResult;
135
+ /**
136
+ * Find a message by its external source and ID.
137
+ * Returns the internal message ID, or null if not found.
138
+ */
139
+ findMessageByExternalId(source: string, externalId: string): MessageId | null;
140
+ /**
141
+ * Create a branch from a specific message.
142
+ * The new branch will have state as of that message's sequence (time-travel branching).
143
+ */
144
+ branchAt(messageId: MessageId, name?: string): string;
145
+ /**
146
+ * Switch to a different branch.
147
+ */
148
+ switchBranch(branchId: string): void;
149
+ /**
150
+ * Get current branch.
151
+ */
152
+ currentBranch(): BranchInfo;
153
+ /**
154
+ * List all branches.
155
+ */
156
+ listBranches(): BranchInfo[];
157
+ /**
158
+ * Check if compile() will block waiting for background work.
159
+ */
160
+ isReady(): boolean;
161
+ /**
162
+ * Get info about pending background work.
163
+ */
164
+ getPendingWork(): PendingWork | null;
165
+ /**
166
+ * Compile context for Membrane.
167
+ *
168
+ * Accepts optional context injections (e.g., from MCPL servers) and merges
169
+ * them into the compiled output by position:
170
+ * - "system": returned separately in `systemInjections` (caller appends to system prompt)
171
+ * - "beforeUser": inserted before the last user message
172
+ * - "afterUser": inserted after the last user message
173
+ *
174
+ * May block if strategy has pending work.
175
+ */
176
+ compile(budget?: TokenBudget, injections?: ContextInjection[]): Promise<CompileResult>;
177
+ /**
178
+ * Log the compiled context to stderr for debugging.
179
+ * Uses stderr so it doesn't pollute the context log (which strategies read).
180
+ */
181
+ private logCompiledContext;
182
+ /**
183
+ * Set the context management strategy.
184
+ */
185
+ setStrategy(strategy: ContextStrategy): Promise<void>;
186
+ /**
187
+ * Get the current strategy.
188
+ */
189
+ getStrategy(): ContextStrategy;
190
+ /**
191
+ * Reset the head window to start from a new position.
192
+ * Old head window messages become compressible.
193
+ *
194
+ * If transitionText is provided, it's used as the transition summary.
195
+ * If omitted, an LLM call auto-generates a transition summary.
196
+ *
197
+ * Returns the transition summary text used.
198
+ */
199
+ resetHeadWindow(transitionText?: string): Promise<string>;
200
+ /**
201
+ * Trigger background maintenance work.
202
+ * Call this periodically to allow strategies to do compression, etc.
203
+ */
204
+ tick(): Promise<void>;
205
+ private initializeStrategy;
206
+ private createStrategyContext;
207
+ /**
208
+ * Handle message store events for edit propagation.
209
+ */
210
+ private handleMessageStoreEvent;
211
+ private handleMessageAdd;
212
+ private handleMessageEdit;
213
+ private handleMessageRemove;
214
+ /**
215
+ * Get the underlying Chronicle store.
216
+ * Useful for registering additional states or accessing store-level features.
217
+ */
218
+ getStore(): JsStore;
219
+ /**
220
+ * Sync to disk.
221
+ */
222
+ sync(): void;
223
+ /**
224
+ * Close the context manager.
225
+ *
226
+ * If the manager owns the store (created via path config), this closes the store.
227
+ * If the app owns the store (passed via store config), this is a no-op;
228
+ * the app is responsible for closing the store when done.
229
+ */
230
+ close(): void;
231
+ /**
232
+ * Check if the store has been closed.
233
+ */
234
+ isClosed(): boolean;
235
+ /**
236
+ * Get store stats.
237
+ */
238
+ stats(): {
239
+ messageCount: number;
240
+ contextEntryCount: number;
241
+ branches: number;
242
+ };
243
+ }
244
+ export {};
245
+ //# sourceMappingURL=context-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context-manager.d.ts","sourceRoot":"","sources":["../../src/context-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,KAAK,EAAE,QAAQ,EAAqB,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,KAAK,EACV,SAAS,EACT,QAAQ,EACR,eAAe,EACf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