@stackmemoryai/stackmemory 0.5.52 → 0.5.53

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.
@@ -1,253 +0,0 @@
1
- /**
2
- * Context Bridge - Automatic synchronization between sessions and shared context
3
- *
4
- * This bridge automatically:
5
- * - Syncs important frames to shared context
6
- * - Loads relevant context on session start
7
- * - Maintains consistency across sessions
8
- */
9
- import { sharedContextLayer } from './shared-context-layer.js';
10
- import { sessionManager } from '../session/session-manager.js';
11
- import { logger } from '../monitoring/logger.js';
12
- export class ContextBridge {
13
- constructor() {
14
- this.frameManager = null;
15
- this.syncTimer = null;
16
- this.lastSyncTime = 0;
17
- this.options = {
18
- autoSync: true,
19
- syncInterval: 60000, // 1 minute
20
- minFrameScore: 0.5, // Include frames with score above 0.5
21
- importantTags: ['decision', 'error', 'milestone', 'learning'],
22
- };
23
- }
24
- static getInstance() {
25
- if (!ContextBridge.instance) {
26
- ContextBridge.instance = new ContextBridge();
27
- }
28
- return ContextBridge.instance;
29
- }
30
- /**
31
- * Initialize the bridge with a frame manager
32
- */
33
- async initialize(frameManager, options) {
34
- this.frameManager = frameManager;
35
- this.options = { ...this.options, ...options };
36
- // Load shared context on initialization
37
- await this.loadSharedContext();
38
- // Start auto-sync if enabled
39
- if (this.options.autoSync) {
40
- this.startAutoSync();
41
- }
42
- logger.info('Context bridge initialized', {
43
- autoSync: this.options.autoSync,
44
- syncInterval: this.options.syncInterval,
45
- });
46
- }
47
- /**
48
- * Load relevant shared context into current session
49
- */
50
- async loadSharedContext() {
51
- try {
52
- const session = sessionManager.getCurrentSession();
53
- if (!session)
54
- return;
55
- // Get context discovery
56
- const discovery = await sharedContextLayer.autoDiscoverContext();
57
- if (!discovery.hasSharedContext) {
58
- logger.info('No shared context available to load');
59
- return;
60
- }
61
- // Load recent patterns as metadata
62
- if (discovery.recentPatterns.length > 0) {
63
- logger.info('Loaded recent patterns from shared context', {
64
- patternCount: discovery.recentPatterns.length,
65
- });
66
- }
67
- // Load last decisions for reference
68
- if (discovery.lastDecisions.length > 0) {
69
- logger.info('Loaded recent decisions from shared context', {
70
- decisionCount: discovery.lastDecisions.length,
71
- });
72
- }
73
- // Store suggested frames in metadata for quick reference
74
- if (discovery.suggestedFrames.length > 0) {
75
- const metadata = {
76
- suggestedFrames: discovery.suggestedFrames,
77
- loadedAt: Date.now(),
78
- };
79
- // Store in frame manager's context
80
- if (this.frameManager) {
81
- await this.frameManager.addContext('shared-context-suggestions', metadata);
82
- }
83
- logger.info('Loaded suggested frames from shared context', {
84
- frameCount: discovery.suggestedFrames.length,
85
- });
86
- }
87
- }
88
- catch (error) {
89
- logger.error('Failed to load shared context', error);
90
- }
91
- }
92
- /**
93
- * Sync current session's important frames to shared context
94
- */
95
- async syncToSharedContext() {
96
- try {
97
- if (!this.frameManager)
98
- return;
99
- const session = sessionManager.getCurrentSession();
100
- if (!session)
101
- return;
102
- // Get all active frames (filter out any nulls from missing frames)
103
- const activeFrames = this.frameManager.getActiveFramePath().filter(Boolean);
104
- // Get recent closed frames (last 100)
105
- const recentFrames = await this.frameManager.getRecentFrames(100);
106
- // Combine and filter important frames
107
- const allFrames = [...activeFrames, ...recentFrames].filter(Boolean);
108
- const importantFrames = this.filterImportantFrames(allFrames);
109
- if (importantFrames.length === 0) {
110
- logger.debug('No important frames to sync');
111
- return;
112
- }
113
- // Add to shared context
114
- await sharedContextLayer.addToSharedContext(importantFrames, {
115
- minScore: this.options.minFrameScore,
116
- tags: this.options.importantTags,
117
- });
118
- this.lastSyncTime = Date.now();
119
- logger.info('Synced frames to shared context', {
120
- frameCount: importantFrames.length,
121
- sessionId: session.sessionId,
122
- });
123
- }
124
- catch (error) {
125
- logger.error('Failed to sync to shared context', error);
126
- }
127
- }
128
- /**
129
- * Query shared context for relevant frames
130
- */
131
- async querySharedFrames(query) {
132
- try {
133
- const results = await sharedContextLayer.querySharedContext({
134
- ...query,
135
- minScore: this.options.minFrameScore,
136
- });
137
- logger.info('Queried shared context', {
138
- query,
139
- resultCount: results.length,
140
- });
141
- return results;
142
- }
143
- catch (error) {
144
- logger.error('Failed to query shared context', error);
145
- return [];
146
- }
147
- }
148
- /**
149
- * Add a decision to shared context
150
- */
151
- async addDecision(decision, reasoning) {
152
- try {
153
- await sharedContextLayer.addDecision({
154
- decision,
155
- reasoning,
156
- outcome: 'pending',
157
- });
158
- logger.info('Added decision to shared context', { decision });
159
- }
160
- catch (error) {
161
- logger.error('Failed to add decision', error);
162
- }
163
- }
164
- /**
165
- * Start automatic synchronization
166
- */
167
- startAutoSync() {
168
- if (this.syncTimer) {
169
- clearInterval(this.syncTimer);
170
- }
171
- this.syncTimer = setInterval(() => {
172
- this.syncToSharedContext().catch((error) => {
173
- logger.error('Auto-sync failed', error);
174
- });
175
- }, this.options.syncInterval);
176
- // Also sync on important events
177
- this.setupEventListeners();
178
- }
179
- /**
180
- * Stop automatic synchronization
181
- */
182
- stopAutoSync() {
183
- if (this.syncTimer) {
184
- clearInterval(this.syncTimer);
185
- this.syncTimer = null;
186
- }
187
- }
188
- /**
189
- * Filter frames that are important enough to share
190
- */
191
- filterImportantFrames(frames) {
192
- return frames.filter((frame) => {
193
- // Check if frame has important tags
194
- const hasImportantTag = this.options.importantTags.some((tag) => frame.metadata?.tags?.includes(tag));
195
- // Check frame type importance
196
- const isImportantType = [
197
- 'task',
198
- 'milestone',
199
- 'error',
200
- 'resolution',
201
- 'decision',
202
- ].includes(frame.type);
203
- // Check metadata importance flag
204
- const markedImportant = frame.metadata?.importance === 'high';
205
- return hasImportantTag || isImportantType || markedImportant;
206
- });
207
- }
208
- /**
209
- * Setup event listeners for automatic syncing
210
- */
211
- setupEventListeners() {
212
- if (!this.frameManager)
213
- return;
214
- // Sync when a frame is closed
215
- const originalClose = this.frameManager.closeFrame.bind(this.frameManager);
216
- this.frameManager.closeFrame = async (frameId, metadata) => {
217
- const result = await originalClose(frameId, metadata);
218
- // Sync if it was an important frame
219
- const frame = await this.frameManager.getFrame(frameId);
220
- if (frame && this.filterImportantFrames([frame]).length > 0) {
221
- await this.syncToSharedContext();
222
- }
223
- return result;
224
- };
225
- // Sync when a milestone is reached
226
- const originalMilestone = this.frameManager.createFrame.bind(this.frameManager);
227
- this.frameManager.createFrame = async (params) => {
228
- const result = await originalMilestone(params);
229
- if (params.type === 'milestone') {
230
- await this.syncToSharedContext();
231
- }
232
- return result;
233
- };
234
- }
235
- /**
236
- * Get sync statistics
237
- */
238
- getSyncStats() {
239
- return {
240
- lastSyncTime: this.lastSyncTime,
241
- autoSyncEnabled: this.options.autoSync,
242
- syncInterval: this.options.syncInterval,
243
- };
244
- }
245
- /**
246
- * Manual trigger for immediate sync
247
- */
248
- async forceSyncNow() {
249
- logger.info('Force sync triggered');
250
- await this.syncToSharedContext();
251
- }
252
- }
253
- export const contextBridge = ContextBridge.getInstance();