-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
439 lines (395 loc) · 13.7 KB
/
Copy pathindex.js
File metadata and controls
439 lines (395 loc) · 13.7 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/usr/bin/env node
// Parse CLI arguments via yargs first — before loading config
import yargs from "yargs";
const parsed = yargs(process.argv.slice(2))
.option("cwd", {
alias: "c",
type: "string",
description: "Working directory to use",
})
.option("mode", {
alias: "m",
type: "string",
description: "CLI mode: 'chat' or 'interactive'",
})
.option("session", {
type: "string",
description: "Session ID to restore",
})
.option("sub-agent", {
type: "boolean",
default: false,
description: "Run as a sub-agent",
})
.positional("message", {
type: "string",
description: "Message to send",
}).argv;
// Load config first — before any other ./src imports — so config.cwd is set
// before process.chdir() potentially changes the working directory.
import { loadConfig } from "./src/config/loader.js";
const config = loadConfig(parsed["sub-agent"]);
// Change to the configured working directory before any other imports
if (parsed.cwd) {
process.chdir(parsed.cwd);
}
// Load config
import { fileURLToPath } from "node:url";
import { loadSession } from "./src/session/loader.js";
import React from "react";
const { setConfigValue } = await import("./src/config/loader.js");
const { createChatModel } = await import("./src/provider/openai.js");
const { createReactAgent, callReactAgent } = await import("./src/agent/react.js");
const { buildToolConfig } = await import("./src/tools/index.js");
const { logger } = await import("./src/logger.js");
const { default: pkg } = await import(new URL("package.json", import.meta.url).href, {
with: { type: "json" },
});
// Initialize subsystems
// Sync crontab from persisted job definitions (runs before any subsystem)
if (config.schedules.syncOnInit !== false) {
try {
const { Cron } = await import("./src/scheduler/cron.js");
const schedulesDir = config.memory?.schedulesDir || "memory/schedules/";
const result = await Cron.sync(schedulesDir);
if (result.error) {
logger.warn(`[scheduler] Crontab sync failed: ${result.error}`);
} else {
logger.info(
`[scheduler] Crontab sync complete: +${result.added} added, -${result.removed} removed, ~${result.updated} updated, =${result.skipped} skipped`,
);
}
// Ensure the daily reflection job exists in crontab and persisted (covers upgrading users
// who have no reflection-daily.json on disk). Cron.add() is idempotent.
const cwd = config.cwd;
const jobResult = Cron.add({
name: "reflection-daily",
cron: "0 2 * * *",
command: `cd ${cwd} && node index.js --chat "/reflection"`,
});
if (jobResult.added || !jobResult.error) {
try {
const { existsSync, mkdirSync, writeFileSync } = await import("node:fs");
const { join } = await import("node:path");
const schedulesDir = config.memory?.schedulesDir || "memory/schedules/";
const filePath = join(schedulesDir, "reflection-daily.json");
if (!existsSync(filePath)) {
mkdirSync(schedulesDir, { recursive: true });
const jobData = {
name: "reflection-daily",
cron: "0 2 * * *",
command: `cd ${cwd} && node index.js --chat "/reflection"`,
enabled: true,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
writeFileSync(filePath, JSON.stringify(jobData, null, 2));
}
} catch (err) {
logger.warn(`[scheduler] Failed to persist reflection-daily job file: ${err.message}`);
}
}
} catch (err) {
logger.warn(`[scheduler] Crontab sync error: ${err.message}`);
}
}
// Ensure sessions directory exists before any subsystem initialization
const { ensureSessionsDir } = await import("./src/session/index.js");
await ensureSessionsDir(config.cwd + "/" + "memory/sessions/");
// Initialize contextual onboarding if profile is missing (with graceful degradation)
let onboardingInstance = null;
try {
const { hasProfile, ATTRIBUTES } = await import("./src/memory/profile.js");
if (!hasProfile()) {
const { createOnboarding } = await import("./src/session/onboarding.js");
const { setupAutoSchedule } = await import("./src/scheduler/autoSchedule.js");
const autoSchedule = setupAutoSchedule();
onboardingInstance = createOnboarding(ATTRIBUTES, { onSave: autoSchedule });
}
} catch {
// Fail gracefully: continue without onboarding if profile detection fails
}
// Boot telemetry if enabled
let tracer = null;
let shutdownFn = null;
if (config.telemetry.enabled) {
const { initTelemetry, getTracer, shutdownTelemetry } =
await import("./src/telemetry/provider.js");
await initTelemetry(config.telemetry);
tracer = getTracer();
shutdownFn = shutdownTelemetry;
}
// Initialize skill registry
const { SkillRegistry, resolvePermissions, ensureSkillsDir } =
await import("./src/skills/index.js");
const registry = new SkillRegistry();
await ensureSkillsDir(config.cwd + "/" + "skills/");
registry.discover();
// Initialize memory system
const { writeMemoryFile, readMemoryFile, loadContext, loadMemories, formatMemoriesForPrompt } =
await import("./src/memory/index.js");
// Initialize workspace rules loader
const { loadAgents } = await import("./src/workspace/loadAgents.js");
// Initialize GC manager (if enabled)
let gcManager = null;
let gcTrace = null;
let maxGcPerHour = 4;
try {
const { initGC, gc: gcFn, isAvailable } = await import("./src/memory/gc.js");
const gcConfig = config.memory?.gc;
if (gcConfig?.enabled !== false) {
const idleTimeoutMs = gcConfig.idleTimeoutMs ?? 300000;
maxGcPerHour = gcConfig.maxGcPerHour ?? 4;
gcManager = initGC({
idleTimeoutMs,
maxGcPerHour,
onIdle(result) {
logger.info(
`[gc] idle GC ${result.triggered ? "triggered" : "skipped"} (${result.reason || "success"}, ${result.hourCalls} calls/hr)`,
);
},
});
gcTrace = () => gcFn(maxGcPerHour);
const avail = isAvailable();
logger.info(`[gc] V8 GC manager initialized ${avail ? "with" : "without"} --expose-gc`);
}
} catch {
logger.warn("[gc] Failed to initialize: graceful degradation");
}
// Initialize session
const { createSession, SessionStateManager, saveSession, handleShutdown, registerShutdownHandler } =
await import("./src/session/index.js");
const { flush: flushLogger } = await import("./src/logger.js");
// Initialize scheduler
const { ScheduleManager } = await import("./src/scheduler/index.js");
const scheduleManager = new ScheduleManager();
// Create or restore session
const providerName = Object.keys(config.providers)[0] || "openai";
const { sessionId, state: initialState } = createSession({
provider: providerName,
});
const sessionState = new SessionStateManager(initialState);
// Session-init: asynchronously clean up expired ephemeral memories (non-blocking)
try {
const { expireEphemeralMemories } = await import("./src/memory/expireEphemeral.js");
queueMicrotask(() =>
expireEphemeralMemories(config.cwd + "/" + config.memory.contextDir).catch(() => {}),
);
} catch {
// Graceful degradation: session starts even if cleanup import fails
}
// Load system prompt and append memory entries
const { loadSystemPrompt } = await import("./src/memory/prompts.js");
const { generateSkillCatalogPrompt } = await import("./src/tools/skills.js");
const systemPrompt = loadSystemPrompt(process.cwd(), config.subAgent);
const memoryEntriesDir = config.cwd + "/" + (config.memory?.entriesDir || "memory/context/");
// Build agent and tool config at startup (once)
const providerConfig = config.providers[providerName] || {};
// Create checkpointer before tools so compactContext can access it
const { createCheckpointer } = await import("./src/session/checkpointer.js");
const checkpointer = createCheckpointer(config.persistence);
const tools = await buildToolConfig({
permissions: config.sandbox.permissions || [],
allowedPaths: config.sandbox.paths,
maxReadSize: config.sandbox.maxReadSize || "1mb",
registry,
sessionsDir: config.cwd + "/" + "memory/sessions/",
safety: config.sandbox.safety,
timeout: config.sandbox.timeout,
memoryLimit: config.sandbox.memoryLimit,
contextDir: config.cwd + "/" + (config.memory?.contextDir || "memory/context/"),
ephemeralTtlDays: config.memory?.ephemeral?.ttlDays || 7,
ephemeralMaxEntries: config.memory?.ephemeral?.maxEntries || 10,
config,
checkpointer,
subAgent: config.subAgent,
});
const model = createChatModel(providerConfig);
const agent = createReactAgent(
model,
tools,
checkpointer,
config.agent?.recursionLimit ?? undefined,
config.agent?.nodeTimeout ?? 600000,
);
const sessionConfig = { configurable: { thread_id: sessionState.getThreadId() } };
async function callProvider(_name, _providerConfig, message, streamingCallback, signal) {
const isNewThread = sessionState.getConversation().length === 0;
const threadId = sessionState.getThreadId();
const memoryEntries = await loadMemories(memoryEntriesDir);
const memoryText = formatMemoriesForPrompt(memoryEntries);
const agentsText = await loadAgents();
const catalog = registry.getCatalog();
const skillCatalog = generateSkillCatalogPrompt(catalog);
const callPrompt = `${systemPrompt}${skillCatalog ? `\n\n${skillCatalog}` : ""}${memoryText ? `\n\n${memoryText}` : ""}${agentsText ? `\n\n${agentsText}` : ""}`;
const result = await callReactAgent(
agent,
message,
{ ...sessionConfig, configurable: { thread_id: threadId, isNewThread } },
callPrompt,
streamingCallback,
{
maxTokens: providerConfig.maxTokens,
checkpointer,
signal,
recursionLimit: config.agent?.recursionLimit,
turnHashWindow: config.agent?.turnHashWindow,
turnBufferMax: config.agent?.turnBufferMax,
},
);
return { provider: providerName, content: result.content, tokens: { input: 0, output: 0 } };
}
// Conversation handler
async function handleConversation(message, sessionId = "") {
// Restore existing session if requested
if (sessionId) {
const { conversation } = await loadSession(config.cwd + "/" + "memory/sessions/", 20);
if (conversation && conversation.length > 0) {
conversation.forEach((msg) => sessionState.addExchange(msg));
}
}
const response = await callProvider(null, null, message);
sessionState.addExchange({ role: "user", content: message });
sessionState.addExchange({ role: "assistant", content: response.content });
// Persist to memory
writeMemoryFile(
"memory/sessions/",
`Conversation ${new Date().toISOString()}`,
{
provider: response.provider,
sessionId,
},
JSON.stringify(sessionState.getConversation(), null, 2),
);
return response;
}
// LLM provider dispatch (for TUI and external callers)
async function dispatchProvider(message, _sessionState = null, streamingCallback, signal) {
return callProvider(null, null, message, streamingCallback, signal);
}
// Skill invocation through sandbox
async function invokeSkill(skillName, input = {}) {
const skill = registry.get(skillName);
if (!skill) {
throw new Error(`Unknown skill: ${skillName}`);
}
if (skill.disabled) {
throw new Error(`Skill "${skillName}" is disabled`);
}
const permissions = resolvePermissions(skill.metadata);
// Placeholder — actual sandbox execution
return {
skill: skillName,
input,
output: `[Skill ${skillName} executed with permissions: ${permissions.join(", ")}]`,
exitCode: 0,
};
}
// Shared shutdown logic — called on signals and in non-interactive mode
const runShutdown = async () => {
await saveSession(
config.cwd + "/" + "memory/sessions/",
sessionState.getConversation(),
sessionId,
);
if (gcManager) {
gcManager.stop();
}
if (shutdownFn) {
await shutdownFn();
}
};
registerShutdownHandler(runShutdown);
// CLI mode detection (if run directly as node.js/index.js)
const isMain = process.argv[1] === fileURLToPath(import.meta.url);
if (isMain) {
const isSubAgent = parsed["sub-agent"] === true;
const mode = isSubAgent ? "sub-agent" : parsed.mode === "interactive" ? "interactive" : "chat";
const chatSessionId = parsed.session || "";
let message = parsed.message;
if (!message && chatSessionId) {
message = "continue";
}
message = message || "Hello";
if (mode === "sub-agent") {
try {
const response = await handleConversation(message, chatSessionId);
const marker = "# SubAgent";
const output = `${marker}\n\n${response.content}`;
process.stdout.write(output);
} catch (err) {
const marker = "# SubAgent";
const errorOutput = `${marker}\n\n{"ok":false,"result":"","error":"${err.message}"}`;
process.stderr.write(errorOutput);
process.exit(1);
}
// Graceful shutdown in non-interactive mode
await runShutdown();
await flushLogger();
process.exit(0);
} else if (mode === "chat") {
try {
await handleConversation(message, chatSessionId);
process.stdout.write("\n");
} catch (_) {
process.exit(1);
}
// Graceful shutdown in non-interactive mode
await runShutdown();
await flushLogger();
process.exit(0);
} else {
const { render } = await import("ink");
const App = (await import("./src/tui/app.js")).default;
const appInfo = { name: config.tui.name, version: pkg.version };
render(
React.createElement(App, {
config,
registry,
sessionState,
dispatchProvider,
scheduleManager,
invokeSkill,
appInfo,
onboarding: onboardingInstance,
onSaveSession: async () =>
await saveSession(
config.cwd + "/" + "memory/sessions/",
sessionState.getConversation(),
sessionId,
),
gcManager: gcManager ? gcManager.onActivity.bind(gcManager) : null,
gcTrigger: gcTrace,
checkpointer,
}),
{
// Restore terminal with newline when app exits
onExit: async () => {
const shutdown = (await import("./src/session/index.js")).handleShutdown;
if (shutdown) await shutdown();
await flushLogger();
process.stdout.write("\n");
},
},
);
}
}
// Export for testing and TUI integration
export {
config,
sessionId,
sessionState,
registry,
tracer,
dispatchProvider,
handleConversation,
invokeSkill,
handleShutdown,
scheduleManager,
setConfigValue,
loadContext,
writeMemoryFile,
readMemoryFile,
loadMemories,
formatMemoriesForPrompt,
};