-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
401 lines (336 loc) · 12.9 KB
/
Copy pathindex.js
File metadata and controls
401 lines (336 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import fs from "fs/promises";
import path from "path";
import { loadConfig, getConfig, LOG_FILE, CONTEXT_SESSION_DIR } from './src/config.js';
import { createDebugLogger } from './src/utils/debug.js';
import { saveContext } from './src/modules/saveContext.js';
import { initializeIntelligenceLearning } from './src/modules/intelligence.js';
import { getSessionGuidance } from './src/modules/sessionGuidance.js';
import { initializeGlobalIntelligence } from './src/utils/globalIntelligence.js';
import { getRelevantContexts, formatForInjection } from './src/modules/contextInjector.js';
import { handleInjectCommand } from './src/modules/injectHandler.js';
import { syncToRemote, getSyncStatus, initializeRemoteSync } from './src/modules/remoteSync.js';
import { setDestroyed, init as initLifecycle } from './src/handlers/lifecycle.js';
import {
getCurrentSessionId,
setCurrentSessionId,
getHasInjectedContext,
setHasInjectedContext,
getLastSession,
setLastSession,
handleSessionCreated,
handleSessionUpdated,
handleSessionEnd,
handleSessionIdle,
handleSessionCompacted
} from './src/handlers/sessionHandlers.js';
import {
handleMessageUpdatedOrCreated,
handleMessagePartDelta,
handleMessagePartUpdated
} from './src/handlers/messageHandlers.js';
import { handleCommandExecuteBefore } from './src/handlers/commandHandlers.js';
const logger = createDebugLogger('context-plugin');
async function loadPreviousContexts(directory, limit = 5) {
try {
const ctxDir = path.join(directory, CONTEXT_SESSION_DIR);
try {
await fs.access(ctxDir);
} catch {
return [];
}
const skipNames = new Set([
'daily-summary.md',
'day-summary.md',
'week-summary.md',
'monthly-summary.md',
'annual-summary.md',
'intelligence-learning.md'
]);
const contexts = [];
async function scan(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = path.join(dir, entry.name);
if (entry.isDirectory()) {
if (entry.name === 'cache' || entry.name === 'reports') continue;
await scan(fullPath);
continue;
}
if (!entry.name.endsWith('.md')) continue;
if (skipNames.has(entry.name)) continue;
if (!/^(exit|compact)-/.test(entry.name)) continue;
const stat = await fs.stat(fullPath);
contexts.push({
file: path.relative(ctxDir, fullPath),
filepath: fullPath,
mtimeMs: stat.mtimeMs
});
}
}
await scan(ctxDir);
const recentContexts = contexts
.sort((a, b) => b.mtimeMs - a.mtimeMs)
.slice(0, limit);
const loaded = await Promise.all(
recentContexts.map(async (ctx) => {
const content = await fs.readFile(ctx.filepath, 'utf-8');
return { file: ctx.file, content };
})
);
logger(`[context-plugin] Loaded ${loaded.length} previous contexts`);
return loaded;
} catch (error) {
logger(`[context-plugin] Error loading contexts: ${error.message}`);
return [];
}
}
function buildContextInjection(contexts) {
if (contexts.length === 0) return '';
let injection = `\n\n---\n## Previous Session Contexts\n\n`;
injection += `*The following contexts from previous sessions are available for reference:*\n\n`;
contexts.forEach(ctx => {
injection += `### From: ${ctx.file}\n`;
injection += `${ctx.content}\n\n`;
injection += `---\n\n`;
});
return injection;
}
/**
* Auto-inject relevant contexts at session start
* Called from OpenCode plugin lifecycle hook
*/
export async function autoInjectContexts(session) {
const config = getConfig();
if (!config.injection?.enabled || !config.injection?.autoInject) {
return null;
}
try {
const scoredContexts = await getRelevantContexts(session, {
maxContexts: config.injection.maxContexts,
maxTokens: config.injection.maxTokens,
baseDir: session.directory
});
if (scoredContexts.length === 0) {
return null;
}
const injected = formatForInjection(scoredContexts);
logger(`[context-plugin] Auto-injected ${scoredContexts.length} contexts`);
return injected;
} catch (error) {
logger(`[context-plugin] Auto-inject failed: ${error.message}`);
return null;
}
}
/**
* Hook registration for OpenCode plugin API
* This registers the plugin with OpenCode's lifecycle hooks
* @param {Object} opencodeApi - OpenCode plugin API
* @param {Object} client - OpenCode client instance for LLM access
*/
export async function registerPluginHooks(opencodeApi, client = null) {
// Session start - auto inject if enabled
opencodeApi.onSessionStart(async (session) => {
await loadConfig(session.directory);
if (client) {
logger(`[context-plugin] Client available for session start: ${!!client.sessions}`);
}
const { readIntelligenceLearning } = await import('./src/agents/readIntelligenceLearning.js');
const intelligence = await readIntelligenceLearning(session.directory, { summary: true });
if (intelligence && typeof intelligence === 'string') {
opencodeApi.addToPrompt(`\n\n## Intelligence Learning\n\n${intelligence}\n`);
}
const injected = await autoInjectContexts(session);
if (injected) {
opencodeApi.addToPrompt(injected);
}
});
// Session end - save context with client for LLM analysis
opencodeApi.onSessionEnd(async (session) => {
await loadConfig(session.directory);
const { saveContext } = await import('./src/modules/saveContext.js');
await saveContext(session.directory, session, 'exit', client);
});
}
// V2 Plugin class that opencode will instantiate
class ContextPlugin {
constructor(input) {
this.directory = input?.directory;
this.client = input?.client;
this._initPromise = null;
this._config = null;
this._intelligenceInitialized = false;
this._globalIntelligenceInitialized = false;
this._remoteSyncInitialized = false;
this._isDestroyed = false;
initLifecycle();
logger(`[context-plugin] ContextPlugin instantiated for: ${this.directory}`);
}
async _ensureInitialized() {
if (!this.directory) return;
if (this._initPromise) return this._initPromise;
this._initPromise = (async () => {
try {
const [config] = await Promise.all([
loadConfig(this.directory),
this._initIntelligence(),
this._initGlobalIntelligence(),
this._initRemoteSync()
]);
this._config = config;
logger(`[context-plugin] Initialization complete: debug=${config.debug}, debounceMs=${config.debounceMs}`);
} catch (err) {
logger(`[context-plugin] Initialization failed: ${err.message}`);
}
})();
return this._initPromise;
}
async _initIntelligence() {
if (this._intelligenceInitialized) return;
this._intelligenceInitialized = true;
return initializeIntelligenceLearning(this.directory).catch(err => {
logger(`[context-plugin] Intelligence learning init failed: ${err.message}`);
});
}
async _initGlobalIntelligence() {
if (this._globalIntelligenceInitialized) return;
this._globalIntelligenceInitialized = true;
return initializeGlobalIntelligence().catch(err => {
logger(`[context-plugin] Global intelligence init failed: ${err.message}`);
});
}
async _initRemoteSync() {
if (this._remoteSyncInitialized) return;
this._remoteSyncInitialized = true;
return initializeRemoteSync().catch(err => {
logger(`[context-plugin] Remote sync init failed: ${err.message}`);
});
}
getConfig() {
return this._config || getConfig();
}
isDestroyed() {
return this._isDestroyed;
}
async destroy() {
if (this._isDestroyed) {
return;
}
this._isDestroyed = true;
setDestroyed(true);
try {
const { resetSessionState } = await import('./src/handlers/sessionHandlers.js');
await resetSessionState();
logger(`[context-plugin] Session state reset on destroy`);
} catch (err) {
logger(`[context-plugin] Error resetting session state on destroy: ${err.message}`);
}
this._initPromise = null;
this._config = null;
this._intelligenceInitialized = false;
this._globalIntelligenceInitialized = false;
this._remoteSyncInitialized = false;
logger(`[context-plugin] ContextPlugin destroyed`);
}
async event(eventInput) {
if (this._isDestroyed) {
logger(`[context-plugin] Ignoring event - plugin destroyed`);
return;
}
await this._ensureInitialized();
logger(`[context-plugin] RAW EVENT received: ${JSON.stringify(eventInput)}`);
const event = eventInput?.event || eventInput;
const eventType = event?.type;
logger(`[context-plugin] Parsed eventType: ${eventType}`);
if (!eventType) return;
const eventHandlers = {
'session.created': () => handleSessionCreated(event, this.directory),
'session.updated': () => handleSessionUpdated(event),
'session.end': () => this.getConfig() && handleSessionEnd(this.directory, this.client, this.getConfig()),
'session.compacted': () => handleSessionCompacted(this.directory, this.client),
'experimental.compaction.autocontinue': () => handleSessionCompacted(this.directory, this.client),
'session.idle': () => {
const sessionId = event?.properties?.sessionID || event?.sessionId || getCurrentSessionId();
if (sessionId) handleSessionIdle(this.directory, this.client, sessionId);
},
'session.deleted': () => {
const sessionId = event?.properties?.sessionID || event?.sessionId || getCurrentSessionId();
if (sessionId) handleSessionIdle(this.directory, this.client, sessionId);
},
'message.updated': () => handleMessageUpdatedOrCreated(event),
'message.created': () => handleMessageUpdatedOrCreated(event),
'message.part.delta': () => handleMessagePartDelta(event),
'message.part.updated': () => handleMessagePartUpdated(event),
'command.execute.before': () => handleCommandExecuteBefore(event),
};
const handler = eventHandlers[eventType];
if (handler) {
await handler();
}
}
async "experimental.chat.messages.transform"(transformInput) {
if (this._isDestroyed) {
return transformInput?.messages || transformInput;
}
await this._ensureInitialized();
const messages = transformInput?.messages || transformInput;
if (!messages || messages.length === 0) {
return messages;
}
const lastMsg = messages[messages.length - 1];
if (lastMsg?.role === 'user' && lastMsg?.content?.includes('/inject')) {
logger('[context-plugin] /inject command detected');
await handleInjectCommand(lastMsg, this.directory);
return messages;
}
const isFirstMessage = messages.length === 1 && !getHasInjectedContext();
if (isFirstMessage) {
logger('[context-plugin] First message detected - injecting context');
const contexts = await loadPreviousContexts(this.directory, 5);
if (contexts.length > 0) {
const injection = buildContextInjection(contexts);
const firstMsg = messages[0];
if (firstMsg.content) {
const ctxNames = contexts.map(c => path.basename(c.file).replace(/-[0-9]{4}-[0-9]{2}-[0-9]{2}T.*/, '')).join(', ');
const notification = `\n\n> 📌 **Context Plugin**: Injected ${contexts.length} prior session contexts: ${ctxNames}\n`;
firstMsg.content = notification + firstMsg.content + injection;
await setHasInjectedContext(true);
logger(`[context-plugin] Injected ${contexts.length} contexts into first message`);
}
}
}
return messages;
}
}
// Main plugin entry point
export { saveContext } from './src/modules/saveContext.js';
export { getRelevantContexts, formatForInjection, injectContextPrompt } from './src/modules/contextInjector.js';
// Search functionality exports
export { buildSearchIndex, searchSessions, updateSearchIndex } from './src/modules/searchIndexer.js';
export { executeSearch, parseSearchQuery } from './src/modules/searchQuery.js';
// Report generation exports
export { generateWeeklyReport, generateMonthlyReport, generateActivityReport, saveReport } from './src/modules/reportGenerator.js';
// Remote sync exports
export { configureRemoteSync, syncToRemote, getSyncStatus, syncGlobalIntelligence, initializeRemoteSync } from './src/modules/remoteSync.js';
// Agent system exports
export { showHelp } from './src/agents/ocpHelp.js';
export {
// Generate agents
generateTodaySummary,
generateWeeklySummary,
generateMonthlySummary,
generateAnnualSummary,
updateIntelligenceLearning,
// Read agents
readTodaySummary,
readWeeklySummary,
readMonthlySummary,
readAnnualSummary,
readIntelligenceLearning,
// Constants
REPORT_PATHS
} from './src/agents/index.js';
// V2 Export format - { id, server } - server must be instantiable with `new`
export default {
id: "@devwellington/opencode-context-plugin",
server: (input) => new ContextPlugin(input)
};