neuralbrain 0.0.0-stage → 0.0.1
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/.dockerignore +11 -0
- package/.env.example +245 -0
- package/Dockerfile +65 -0
- package/LICENSE +44 -0
- package/README.md +312 -2
- package/dist/ai/chat.d.ts +45 -0
- package/dist/ai/chat.d.ts.map +1 -0
- package/dist/ai/chat.js +153 -0
- package/dist/ai/chat.js.map +1 -0
- package/dist/ai/circuitBreaker.d.ts +35 -0
- package/dist/ai/circuitBreaker.d.ts.map +1 -0
- package/dist/ai/circuitBreaker.js +95 -0
- package/dist/ai/circuitBreaker.js.map +1 -0
- package/dist/ai/config.d.ts +88 -0
- package/dist/ai/config.d.ts.map +1 -0
- package/dist/ai/config.js +255 -0
- package/dist/ai/config.js.map +1 -0
- package/dist/ai/vendors.d.ts +58 -0
- package/dist/ai/vendors.d.ts.map +1 -0
- package/dist/ai/vendors.js +223 -0
- package/dist/ai/vendors.js.map +1 -0
- package/dist/app.d.ts +4 -0
- package/dist/app.d.ts.map +1 -0
- package/dist/app.js +1013 -0
- package/dist/app.js.map +1 -0
- package/dist/cli.d.ts +133 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +912 -0
- package/dist/cli.js.map +1 -0
- package/dist/consolidation/candidates.d.ts +34 -0
- package/dist/consolidation/candidates.d.ts.map +1 -0
- package/dist/consolidation/candidates.js +150 -0
- package/dist/consolidation/candidates.js.map +1 -0
- package/dist/consolidation/fixtures.d.ts +25 -0
- package/dist/consolidation/fixtures.d.ts.map +1 -0
- package/dist/consolidation/fixtures.js +400 -0
- package/dist/consolidation/fixtures.js.map +1 -0
- package/dist/consolidation/gate.d.ts +39 -0
- package/dist/consolidation/gate.d.ts.map +1 -0
- package/dist/consolidation/gate.js +286 -0
- package/dist/consolidation/gate.js.map +1 -0
- package/dist/consolidation/runner.d.ts +63 -0
- package/dist/consolidation/runner.d.ts.map +1 -0
- package/dist/consolidation/runner.js +171 -0
- package/dist/consolidation/runner.js.map +1 -0
- package/dist/dashboard/clientScript.d.ts +2 -0
- package/dist/dashboard/clientScript.d.ts.map +1 -0
- package/dist/dashboard/clientScript.js +4212 -0
- package/dist/dashboard/clientScript.js.map +1 -0
- package/dist/dashboard/html.d.ts +2 -0
- package/dist/dashboard/html.d.ts.map +1 -0
- package/dist/dashboard/html.js +554 -0
- package/dist/dashboard/html.js.map +1 -0
- package/dist/dashboard/styles.d.ts +2 -0
- package/dist/dashboard/styles.d.ts.map +1 -0
- package/dist/dashboard/styles.js +1283 -0
- package/dist/dashboard/styles.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +124 -0
- package/dist/index.js.map +1 -0
- package/dist/learning/embedWorker.d.ts +58 -0
- package/dist/learning/embedWorker.d.ts.map +1 -0
- package/dist/learning/embedWorker.js +552 -0
- package/dist/learning/embedWorker.js.map +1 -0
- package/dist/learning/embedding.d.ts +52 -0
- package/dist/learning/embedding.d.ts.map +1 -0
- package/dist/learning/embedding.js +511 -0
- package/dist/learning/embedding.js.map +1 -0
- package/dist/learning/engine.d.ts +20 -0
- package/dist/learning/engine.d.ts.map +1 -0
- package/dist/learning/engine.js +69 -0
- package/dist/learning/engine.js.map +1 -0
- package/dist/learning/search.d.ts +4 -0
- package/dist/learning/search.d.ts.map +1 -0
- package/dist/learning/search.js +509 -0
- package/dist/learning/search.js.map +1 -0
- package/dist/learning/storeMeta.d.ts +11 -0
- package/dist/learning/storeMeta.d.ts.map +1 -0
- package/dist/learning/storeMeta.js +100 -0
- package/dist/learning/storeMeta.js.map +1 -0
- package/dist/learning/types.d.ts +101 -0
- package/dist/learning/types.d.ts.map +1 -0
- package/dist/learning/types.js +3 -0
- package/dist/learning/types.js.map +1 -0
- package/dist/lib/brainHome.d.ts +28 -0
- package/dist/lib/brainHome.d.ts.map +1 -0
- package/dist/lib/brainHome.js +104 -0
- package/dist/lib/brainHome.js.map +1 -0
- package/dist/lib/db.d.ts +10 -0
- package/dist/lib/db.d.ts.map +1 -0
- package/dist/lib/db.js +589 -0
- package/dist/lib/db.js.map +1 -0
- package/dist/lib/logger.d.ts +3 -0
- package/dist/lib/logger.d.ts.map +1 -0
- package/dist/lib/logger.js +11 -0
- package/dist/lib/logger.js.map +1 -0
- package/dist/lib/projectSlug.d.ts +61 -0
- package/dist/lib/projectSlug.d.ts.map +1 -0
- package/dist/lib/projectSlug.js +273 -0
- package/dist/lib/projectSlug.js.map +1 -0
- package/dist/lib/redactSecrets.d.ts +7 -0
- package/dist/lib/redactSecrets.d.ts.map +1 -0
- package/dist/lib/redactSecrets.js +90 -0
- package/dist/lib/redactSecrets.js.map +1 -0
- package/dist/lib/scope.d.ts +54 -0
- package/dist/lib/scope.d.ts.map +1 -0
- package/dist/lib/scope.js +103 -0
- package/dist/lib/scope.js.map +1 -0
- package/dist/lib/spool.d.ts +23 -0
- package/dist/lib/spool.d.ts.map +1 -0
- package/dist/lib/spool.js +317 -0
- package/dist/lib/spool.js.map +1 -0
- package/dist/lib/sqlite.d.ts +50 -0
- package/dist/lib/sqlite.d.ts.map +1 -0
- package/dist/lib/sqlite.js +130 -0
- package/dist/lib/sqlite.js.map +1 -0
- package/dist/lib/tls.d.ts +34 -0
- package/dist/lib/tls.d.ts.map +1 -0
- package/dist/lib/tls.js +212 -0
- package/dist/lib/tls.js.map +1 -0
- package/dist/mcp/server.d.ts +231 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +615 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/memory/actionStore.d.ts +75 -0
- package/dist/memory/actionStore.d.ts.map +1 -0
- package/dist/memory/actionStore.js +239 -0
- package/dist/memory/actionStore.js.map +1 -0
- package/dist/memory/deadLetterStore.d.ts +30 -0
- package/dist/memory/deadLetterStore.d.ts.map +1 -0
- package/dist/memory/deadLetterStore.js +91 -0
- package/dist/memory/deadLetterStore.js.map +1 -0
- package/dist/memory/embedQueueStore.d.ts +32 -0
- package/dist/memory/embedQueueStore.d.ts.map +1 -0
- package/dist/memory/embedQueueStore.js +143 -0
- package/dist/memory/embedQueueStore.js.map +1 -0
- package/dist/memory/failurePatternStore.d.ts +37 -0
- package/dist/memory/failurePatternStore.d.ts.map +1 -0
- package/dist/memory/failurePatternStore.js +179 -0
- package/dist/memory/failurePatternStore.js.map +1 -0
- package/dist/memory/graphStore.d.ts +38 -0
- package/dist/memory/graphStore.d.ts.map +1 -0
- package/dist/memory/graphStore.js +229 -0
- package/dist/memory/graphStore.js.map +1 -0
- package/dist/memory/noteStore.d.ts +58 -0
- package/dist/memory/noteStore.d.ts.map +1 -0
- package/dist/memory/noteStore.js +280 -0
- package/dist/memory/noteStore.js.map +1 -0
- package/dist/memory/observationStore.d.ts +40 -0
- package/dist/memory/observationStore.d.ts.map +1 -0
- package/dist/memory/observationStore.js +213 -0
- package/dist/memory/observationStore.js.map +1 -0
- package/dist/memory/projectRename.d.ts +14 -0
- package/dist/memory/projectRename.d.ts.map +1 -0
- package/dist/memory/projectRename.js +105 -0
- package/dist/memory/projectRename.js.map +1 -0
- package/dist/memory/rollupSweeper.d.ts +36 -0
- package/dist/memory/rollupSweeper.d.ts.map +1 -0
- package/dist/memory/rollupSweeper.js +235 -0
- package/dist/memory/rollupSweeper.js.map +1 -0
- package/dist/memory/sessionStore.d.ts +81 -0
- package/dist/memory/sessionStore.d.ts.map +1 -0
- package/dist/memory/sessionStore.js +285 -0
- package/dist/memory/sessionStore.js.map +1 -0
- package/dist/memory/trajectoryStore.d.ts +86 -0
- package/dist/memory/trajectoryStore.d.ts.map +1 -0
- package/dist/memory/trajectoryStore.js +528 -0
- package/dist/memory/trajectoryStore.js.map +1 -0
- package/dist/middleware/auth.d.ts +30 -0
- package/dist/middleware/auth.d.ts.map +1 -0
- package/dist/middleware/auth.js +387 -0
- package/dist/middleware/auth.js.map +1 -0
- package/dist/middleware/rateLimiter.d.ts +13 -0
- package/dist/middleware/rateLimiter.d.ts.map +1 -0
- package/dist/middleware/rateLimiter.js +153 -0
- package/dist/middleware/rateLimiter.js.map +1 -0
- package/dist/middleware/tracing.d.ts +15 -0
- package/dist/middleware/tracing.d.ts.map +1 -0
- package/dist/middleware/tracing.js +53 -0
- package/dist/middleware/tracing.js.map +1 -0
- package/dist/routes/dashboard.d.ts +2 -0
- package/dist/routes/dashboard.d.ts.map +1 -0
- package/dist/routes/dashboard.js +443 -0
- package/dist/routes/dashboard.js.map +1 -0
- package/dist/routes/learning.d.ts +2 -0
- package/dist/routes/learning.d.ts.map +1 -0
- package/dist/routes/learning.js +46 -0
- package/dist/routes/learning.js.map +1 -0
- package/dist/routes/mcp.d.ts +8 -0
- package/dist/routes/mcp.d.ts.map +1 -0
- package/dist/routes/mcp.js +316 -0
- package/dist/routes/mcp.js.map +1 -0
- package/dist/routes/memories.d.ts +2 -0
- package/dist/routes/memories.d.ts.map +1 -0
- package/dist/routes/memories.js +33 -0
- package/dist/routes/memories.js.map +1 -0
- package/dist/scripts/createApiKey.d.ts +2 -0
- package/dist/scripts/createApiKey.d.ts.map +1 -0
- package/dist/scripts/createApiKey.js +78 -0
- package/dist/scripts/createApiKey.js.map +1 -0
- package/dist/scripts/doctor.d.ts +24 -0
- package/dist/scripts/doctor.d.ts.map +1 -0
- package/dist/scripts/doctor.js +332 -0
- package/dist/scripts/doctor.js.map +1 -0
- package/dist/scripts/installAgent.d.ts +80 -0
- package/dist/scripts/installAgent.d.ts.map +1 -0
- package/dist/scripts/installAgent.js +1046 -0
- package/dist/scripts/installAgent.js.map +1 -0
- package/dist/scripts/migrateAgentMemory.d.ts +16 -0
- package/dist/scripts/migrateAgentMemory.d.ts.map +1 -0
- package/dist/scripts/migrateAgentMemory.js +209 -0
- package/dist/scripts/migrateAgentMemory.js.map +1 -0
- package/dist/scripts/smokeTest.d.ts +16 -0
- package/dist/scripts/smokeTest.d.ts.map +1 -0
- package/dist/scripts/smokeTest.js +150 -0
- package/dist/scripts/smokeTest.js.map +1 -0
- package/docker-compose.qdrant-port.yml +7 -0
- package/docker-compose.yml +107 -0
- package/npm-shrinkwrap.json +7635 -0
- package/package.json +106 -4
- package/plugin/hooks/postToolUse.js +513 -0
- package/plugin/hooks/sessionStart.js +416 -0
- package/plugin/hooks/sessionStop.js +346 -0
- package/plugin/hooks.json +30 -0
- package/plugin/mcp_config.json +13 -0
- package/plugin/plugin.json +15 -0
- package/plugin/rules/AGENTS.md +66 -0
- package/plugin/skills/brain-memory/REFERENCE.md +64 -0
- package/plugin/skills/brain-memory/SKILL.md +68 -0
- package/plugin/skills/brain-memory/TROUBLESHOOTING.md +24 -0
- package/plugin/skills/digest/SKILL.md +54 -0
- package/plugin/skills/digest/TROUBLESHOOTING.md +24 -0
- package/plugin/skills/handoff/SKILL.md +59 -0
- package/plugin/skills/handoff/TROUBLESHOOTING.md +24 -0
- package/plugin/skills/save/SKILL.md +76 -0
- package/plugin/skills/save/TROUBLESHOOTING.md +24 -0
- package/plugin/skills/search/SKILL.md +60 -0
- package/plugin/skills/search/TROUBLESHOOTING.md +24 -0
- package/src/ai/chat.ts +214 -0
- package/src/ai/circuitBreaker.ts +120 -0
- package/src/ai/config.ts +311 -0
- package/src/ai/vendors.ts +292 -0
- package/src/app.ts +1150 -0
- package/src/cli.ts +1080 -0
- package/src/consolidation/candidates.ts +219 -0
- package/src/consolidation/fixtures.ts +461 -0
- package/src/consolidation/gate.ts +408 -0
- package/src/consolidation/runner.ts +220 -0
- package/src/dashboard/clientScript.ts +4208 -0
- package/src/dashboard/html.ts +550 -0
- package/src/dashboard/styles.ts +1279 -0
- package/src/index.ts +139 -0
- package/src/learning/embedWorker.ts +651 -0
- package/src/learning/embedding.ts +522 -0
- package/src/learning/engine.ts +86 -0
- package/src/learning/search.ts +568 -0
- package/src/learning/storeMeta.ts +147 -0
- package/src/learning/types.ts +111 -0
- package/src/lib/brainHome.ts +101 -0
- package/src/lib/db.ts +627 -0
- package/src/lib/logger.ts +7 -0
- package/src/lib/projectSlug.ts +286 -0
- package/src/lib/redactSecrets.ts +87 -0
- package/src/lib/scope.ts +121 -0
- package/src/lib/spool.ts +372 -0
- package/src/lib/sqlite.ts +158 -0
- package/src/lib/tls.ts +252 -0
- package/src/mcp/server.ts +793 -0
- package/src/memory/actionStore.ts +378 -0
- package/src/memory/deadLetterStore.ts +141 -0
- package/src/memory/embedQueueStore.ts +176 -0
- package/src/memory/failurePatternStore.ts +221 -0
- package/src/memory/graphStore.ts +352 -0
- package/src/memory/noteStore.ts +403 -0
- package/src/memory/observationStore.ts +303 -0
- package/src/memory/projectRename.ts +147 -0
- package/src/memory/rollupSweeper.ts +309 -0
- package/src/memory/sessionStore.ts +410 -0
- package/src/memory/trajectoryStore.ts +775 -0
- package/src/middleware/auth.ts +450 -0
- package/src/middleware/rateLimiter.ts +186 -0
- package/src/middleware/tracing.ts +70 -0
- package/src/routes/dashboard.ts +496 -0
- package/src/routes/learning.ts +51 -0
- package/src/routes/mcp.ts +349 -0
- package/src/routes/memories.ts +33 -0
- package/src/scripts/createApiKey.ts +109 -0
- package/src/scripts/doctor.ts +359 -0
- package/src/scripts/installAgent.ts +1278 -0
- package/src/scripts/migrateAgentMemory.ts +263 -0
- package/src/scripts/smokeTest.ts +189 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
function getBrainHomeDir() {
|
|
8
|
+
const envHome = process.env.BRAIN_HOME && process.env.BRAIN_HOME.trim();
|
|
9
|
+
if (envHome) {
|
|
10
|
+
if (envHome === '~') return os.homedir();
|
|
11
|
+
if (envHome.startsWith('~/') || envHome.startsWith('~\\')) {
|
|
12
|
+
return path.join(os.homedir(), envHome.slice(2));
|
|
13
|
+
}
|
|
14
|
+
return path.resolve(envHome);
|
|
15
|
+
}
|
|
16
|
+
return path.join(os.homedir(), '.neuralbrain');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function readDotEnvValue(key, envCandidates) {
|
|
20
|
+
const brainHome = getBrainHomeDir();
|
|
21
|
+
const candidates = envCandidates || [
|
|
22
|
+
path.join(brainHome, '.env'),
|
|
23
|
+
path.join(__dirname, '../../.env'),
|
|
24
|
+
];
|
|
25
|
+
for (const envFilePath of candidates) {
|
|
26
|
+
try {
|
|
27
|
+
if (fs.existsSync(envFilePath)) {
|
|
28
|
+
const content = fs.readFileSync(envFilePath, 'utf-8');
|
|
29
|
+
const lines = content.split('\n');
|
|
30
|
+
for (const line of lines) {
|
|
31
|
+
const trimmed = line.trim();
|
|
32
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
33
|
+
const eqIdx = trimmed.indexOf('=');
|
|
34
|
+
if (eqIdx !== -1) {
|
|
35
|
+
const varName = trimmed.slice(0, eqIdx).trim();
|
|
36
|
+
if (varName === key) {
|
|
37
|
+
let val = trimmed.slice(eqIdx + 1).trim();
|
|
38
|
+
if (
|
|
39
|
+
(val.startsWith('"') && val.endsWith('"')) ||
|
|
40
|
+
(val.startsWith("'") && val.endsWith("'"))
|
|
41
|
+
) {
|
|
42
|
+
val = val.slice(1, -1);
|
|
43
|
+
}
|
|
44
|
+
if (val) return val;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
} catch {
|
|
50
|
+
// Ignore read errors
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function readApiKeyFile(keyPath) {
|
|
57
|
+
try {
|
|
58
|
+
if (fs.existsSync(keyPath)) {
|
|
59
|
+
const content = fs.readFileSync(keyPath, 'utf-8').trim();
|
|
60
|
+
if (content) return content;
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
// Ignore read errors
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function resolveApiKey(options) {
|
|
69
|
+
if (options && options.apiKey && typeof options.apiKey === 'string' && options.apiKey.trim()) {
|
|
70
|
+
return options.apiKey.trim();
|
|
71
|
+
}
|
|
72
|
+
if (process.env.BRAIN_API_KEY && process.env.BRAIN_API_KEY.trim()) {
|
|
73
|
+
return process.env.BRAIN_API_KEY.trim();
|
|
74
|
+
}
|
|
75
|
+
const brainHome = getBrainHomeDir();
|
|
76
|
+
const homeEnvKey = readDotEnvValue('BRAIN_API_KEY', [path.join(brainHome, '.env')]);
|
|
77
|
+
if (homeEnvKey && homeEnvKey.trim()) {
|
|
78
|
+
return homeEnvKey.trim();
|
|
79
|
+
}
|
|
80
|
+
const homeKeyFile = readApiKeyFile(path.join(brainHome, 'data', 'api-key.txt'));
|
|
81
|
+
if (homeKeyFile) {
|
|
82
|
+
return homeKeyFile;
|
|
83
|
+
}
|
|
84
|
+
const repoEnvKey = readDotEnvValue('BRAIN_API_KEY', [path.join(__dirname, '../../.env')]);
|
|
85
|
+
if (repoEnvKey && repoEnvKey.trim()) {
|
|
86
|
+
return repoEnvKey.trim();
|
|
87
|
+
}
|
|
88
|
+
return readApiKeyFile(path.join(__dirname, '../../data/api-key.txt'));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function resolveApiUrl(options) {
|
|
92
|
+
if (options && options.apiUrl && typeof options.apiUrl === 'string' && options.apiUrl.trim()) {
|
|
93
|
+
return options.apiUrl.trim().replace(/\/$/, '');
|
|
94
|
+
}
|
|
95
|
+
if (process.env.BRAIN_API_URL && process.env.BRAIN_API_URL.trim()) {
|
|
96
|
+
return process.env.BRAIN_API_URL.trim().replace(/\/$/, '');
|
|
97
|
+
}
|
|
98
|
+
const brainHome = getBrainHomeDir();
|
|
99
|
+
const homeEnvCandidates = [path.join(brainHome, '.env')];
|
|
100
|
+
const homeEnvUrl = readDotEnvValue('BRAIN_API_URL', homeEnvCandidates);
|
|
101
|
+
if (homeEnvUrl && homeEnvUrl.trim()) {
|
|
102
|
+
return homeEnvUrl.trim().replace(/\/$/, '');
|
|
103
|
+
}
|
|
104
|
+
const envOrHomePort =
|
|
105
|
+
(process.env.PORT && process.env.PORT.trim()) ||
|
|
106
|
+
readDotEnvValue('PORT', homeEnvCandidates);
|
|
107
|
+
const envOrHomeHttps =
|
|
108
|
+
(process.env.BRAIN_HTTPS && process.env.BRAIN_HTTPS.trim()) ||
|
|
109
|
+
readDotEnvValue('BRAIN_HTTPS', homeEnvCandidates);
|
|
110
|
+
if (envOrHomePort || envOrHomeHttps) {
|
|
111
|
+
const port = envOrHomePort || '3737';
|
|
112
|
+
const useHttps = envOrHomeHttps === '1' || envOrHomeHttps === 'true';
|
|
113
|
+
return `${useHttps ? 'https' : 'http'}://127.0.0.1:${port}`;
|
|
114
|
+
}
|
|
115
|
+
const repoEnvCandidates = [path.join(__dirname, '../../.env')];
|
|
116
|
+
const repoEnvUrl = readDotEnvValue('BRAIN_API_URL', repoEnvCandidates);
|
|
117
|
+
if (repoEnvUrl && repoEnvUrl.trim()) {
|
|
118
|
+
return repoEnvUrl.trim().replace(/\/$/, '');
|
|
119
|
+
}
|
|
120
|
+
const port = readDotEnvValue('PORT', repoEnvCandidates) || '3737';
|
|
121
|
+
const httpsFlag = readDotEnvValue('BRAIN_HTTPS', repoEnvCandidates);
|
|
122
|
+
const useHttps = httpsFlag === '1' || httpsFlag === 'true';
|
|
123
|
+
return `${useHttps ? 'https' : 'http'}://127.0.0.1:${port}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function resolveProjectId(cwd) {
|
|
127
|
+
try {
|
|
128
|
+
let slugModule;
|
|
129
|
+
try {
|
|
130
|
+
slugModule = require(path.join(__dirname, 'lib/projectSlug'));
|
|
131
|
+
} catch {
|
|
132
|
+
try {
|
|
133
|
+
slugModule = require(path.join(__dirname, '../../dist/lib/projectSlug'));
|
|
134
|
+
} catch {
|
|
135
|
+
// Fall back
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (slugModule && typeof slugModule.resolveCanonicalProjectId === 'function') {
|
|
139
|
+
const resolved = slugModule.resolveCanonicalProjectId({ cwd, fallbackToDirname: true });
|
|
140
|
+
if (resolved) return resolved;
|
|
141
|
+
}
|
|
142
|
+
} catch {
|
|
143
|
+
// Fall back to in-line resolution
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
let current = path.resolve(cwd || process.cwd());
|
|
148
|
+
for (;;) {
|
|
149
|
+
for (const name of ['.brainrc', '.brain.json']) {
|
|
150
|
+
const fp = path.join(current, name);
|
|
151
|
+
if (fs.existsSync(fp)) {
|
|
152
|
+
const raw = fs.readFileSync(fp, 'utf-8');
|
|
153
|
+
try {
|
|
154
|
+
const parsed = JSON.parse(raw);
|
|
155
|
+
const val = parsed.project_id || parsed.projectId || parsed.project;
|
|
156
|
+
if (val && typeof val === 'string') return val.trim().toLowerCase();
|
|
157
|
+
} catch {
|
|
158
|
+
if (raw.trim()) return raw.trim().toLowerCase();
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const parent = path.dirname(current);
|
|
163
|
+
if (parent === current) break;
|
|
164
|
+
current = parent;
|
|
165
|
+
}
|
|
166
|
+
const base = path.basename(path.resolve(cwd || process.cwd()));
|
|
167
|
+
return base.toLowerCase().replace(/[^a-z0-9._-]+/g, '-').replace(/^-+|-+$/g, '') || null;
|
|
168
|
+
} catch {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The project for this event, fixed per session: the first event of a session
|
|
175
|
+
* resolves it from the project root and pins it, so later events stay in the
|
|
176
|
+
* same project even after the agent changes into a subfolder (see
|
|
177
|
+
* resolveSessionProjectId in src/lib/projectSlug.ts).
|
|
178
|
+
*/
|
|
179
|
+
function resolveSessionProject(sessionId, cwd, options = {}) {
|
|
180
|
+
try {
|
|
181
|
+
let slugModule;
|
|
182
|
+
try {
|
|
183
|
+
slugModule = require(path.join(__dirname, 'lib/projectSlug'));
|
|
184
|
+
} catch {
|
|
185
|
+
try {
|
|
186
|
+
slugModule = require(path.join(__dirname, '../../dist/lib/projectSlug'));
|
|
187
|
+
} catch {
|
|
188
|
+
// Fall back
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (slugModule && typeof slugModule.resolveSessionProjectId === 'function') {
|
|
192
|
+
return slugModule.resolveSessionProjectId({ sessionId, cwd, pinDir: options.pinDir });
|
|
193
|
+
}
|
|
194
|
+
} catch {
|
|
195
|
+
// Fall back to per-event resolution
|
|
196
|
+
}
|
|
197
|
+
return resolveProjectId(cwd);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function readStdin() {
|
|
201
|
+
return new Promise((resolve) => {
|
|
202
|
+
let data = '';
|
|
203
|
+
process.stdin.setEncoding('utf-8');
|
|
204
|
+
process.stdin.on('data', (chunk) => {
|
|
205
|
+
data += chunk;
|
|
206
|
+
});
|
|
207
|
+
process.stdin.on('end', () => {
|
|
208
|
+
if (!data.trim()) {
|
|
209
|
+
resolve({});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
resolve(JSON.parse(data));
|
|
214
|
+
} catch {
|
|
215
|
+
resolve({});
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
process.stdin.on('error', () => {
|
|
219
|
+
resolve({});
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function runSessionStart(inputObj = {}, options = {}) {
|
|
225
|
+
try {
|
|
226
|
+
const cwd =
|
|
227
|
+
options.cwd ||
|
|
228
|
+
inputObj.cwd ||
|
|
229
|
+
(Array.isArray(inputObj.workspacePaths) && inputObj.workspacePaths[0]) ||
|
|
230
|
+
process.cwd();
|
|
231
|
+
|
|
232
|
+
const sessionId =
|
|
233
|
+
inputObj?.conversationId ||
|
|
234
|
+
inputObj?.session_id ||
|
|
235
|
+
inputObj?.sessionId ||
|
|
236
|
+
undefined;
|
|
237
|
+
const projectId = options.projectId || resolveSessionProject(sessionId, cwd, options);
|
|
238
|
+
const scope = projectId ? 'project' : 'unknown';
|
|
239
|
+
const agentName =
|
|
240
|
+
inputObj?.agent_name ||
|
|
241
|
+
inputObj?.agent ||
|
|
242
|
+
process.env.BRAIN_AGENT_NAME ||
|
|
243
|
+
(inputObj?.transcript_path && String(inputObj.transcript_path).includes('.claude')
|
|
244
|
+
? 'claude'
|
|
245
|
+
: inputObj?.conversationId
|
|
246
|
+
? 'antigravity'
|
|
247
|
+
: 'unknown');
|
|
248
|
+
|
|
249
|
+
const apiUrl = resolveApiUrl(options);
|
|
250
|
+
const apiKey = resolveApiKey(options);
|
|
251
|
+
const fetchFn = options.fetchFn || globalThis.fetch;
|
|
252
|
+
|
|
253
|
+
const headers = { Accept: 'application/json' };
|
|
254
|
+
if (apiKey) {
|
|
255
|
+
headers['Authorization'] = `Bearer ${apiKey}`;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// Non-blocking session registration
|
|
259
|
+
try {
|
|
260
|
+
const postHeaders = { 'Content-Type': 'application/json' };
|
|
261
|
+
if (apiKey) {
|
|
262
|
+
postHeaders['Authorization'] = `Bearer ${apiKey}`;
|
|
263
|
+
}
|
|
264
|
+
await fetchFn(`${apiUrl}/sessions/start`, {
|
|
265
|
+
method: 'POST',
|
|
266
|
+
headers: postHeaders,
|
|
267
|
+
body: JSON.stringify({
|
|
268
|
+
id: sessionId,
|
|
269
|
+
project_id: projectId || undefined,
|
|
270
|
+
scope,
|
|
271
|
+
agent_name: agentName,
|
|
272
|
+
cwd,
|
|
273
|
+
}),
|
|
274
|
+
signal: AbortSignal.timeout(1500),
|
|
275
|
+
});
|
|
276
|
+
} catch {
|
|
277
|
+
// Non-blocking if offline
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const query = new URLSearchParams();
|
|
281
|
+
if (projectId) {
|
|
282
|
+
query.set('project_id', projectId);
|
|
283
|
+
}
|
|
284
|
+
query.set('limit', '5');
|
|
285
|
+
|
|
286
|
+
const res = await fetchFn(`${apiUrl}/inject?${query.toString()}`, {
|
|
287
|
+
method: 'GET',
|
|
288
|
+
headers,
|
|
289
|
+
signal: AbortSignal.timeout(2500),
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
if (!res.ok) {
|
|
293
|
+
return {};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const data = await res.json().catch(() => ({}));
|
|
297
|
+
|
|
298
|
+
let readyActionsText = '';
|
|
299
|
+
if (Array.isArray(data.ready_actions) && data.ready_actions.length > 0) {
|
|
300
|
+
const lines = data.ready_actions.map((a) => {
|
|
301
|
+
const prio =
|
|
302
|
+
a.priority !== undefined && a.priority !== null && String(a.priority).trim()
|
|
303
|
+
? `[${String(a.priority).toUpperCase()}] `
|
|
304
|
+
: '';
|
|
305
|
+
const status = a.status ? ` (${a.status})` : '';
|
|
306
|
+
const desc = a.description ? `: ${a.description}` : '';
|
|
307
|
+
return `- ${prio}${a.title}${status}${desc}`;
|
|
308
|
+
});
|
|
309
|
+
readyActionsText = `READY NEXT ACTIONS:\n${lines.join('\n')}`;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const warnings =
|
|
313
|
+
data.formatted_warnings ||
|
|
314
|
+
data.prompt_injection ||
|
|
315
|
+
data.critical_warnings ||
|
|
316
|
+
'';
|
|
317
|
+
|
|
318
|
+
let lessonsText = '';
|
|
319
|
+
if (Array.isArray(data.lessons) && data.lessons.length > 0) {
|
|
320
|
+
const lines = data.lessons.map((l) => {
|
|
321
|
+
if (typeof l === 'string') return `- ${l}`;
|
|
322
|
+
const label = l.title ? `${l.title}: ` : '';
|
|
323
|
+
const body = l.content || l.text || l.description || '';
|
|
324
|
+
return `- ${label}${body}`.trim();
|
|
325
|
+
});
|
|
326
|
+
lessonsText = `LESSONS:\n${lines.join('\n')}`;
|
|
327
|
+
} else if (typeof data.lessons === 'string' && data.lessons.trim()) {
|
|
328
|
+
lessonsText = `LESSONS:\n${data.lessons.trim()}`;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
let lastSessionText = '';
|
|
332
|
+
if (data.latest_session_summary) {
|
|
333
|
+
if (typeof data.latest_session_summary === 'string' && data.latest_session_summary.trim()) {
|
|
334
|
+
lastSessionText = `LAST SESSION SUMMARY:\n${data.latest_session_summary.trim()}`;
|
|
335
|
+
} else if (typeof data.latest_session_summary === 'object') {
|
|
336
|
+
const s = data.latest_session_summary;
|
|
337
|
+
const summaryBody =
|
|
338
|
+
s.summary ||
|
|
339
|
+
s.description ||
|
|
340
|
+
s.content ||
|
|
341
|
+
(Array.isArray(s.claims) ? s.claims.map((c) => `- ${c.text}`).join('\n') : '') ||
|
|
342
|
+
'';
|
|
343
|
+
if (summaryBody && String(summaryBody).trim()) {
|
|
344
|
+
lastSessionText = `LAST SESSION SUMMARY:\n${String(summaryBody).trim()}`;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
let notesText = '';
|
|
350
|
+
if (Array.isArray(data.notes) && data.notes.length > 0) {
|
|
351
|
+
const lines = data.notes.map(
|
|
352
|
+
(n) => `- [${n.type || 'note'}] ${n.title}: ${n.content}`
|
|
353
|
+
);
|
|
354
|
+
notesText = `CURATED MEMORIES:\n${lines.join('\n')}`;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const consolidationWarning =
|
|
358
|
+
typeof data.consolidation_warning === 'string' && data.consolidation_warning.trim()
|
|
359
|
+
? data.consolidation_warning.trim()
|
|
360
|
+
: '';
|
|
361
|
+
|
|
362
|
+
const parts = [
|
|
363
|
+
readyActionsText.trim(),
|
|
364
|
+
String(warnings).trim(),
|
|
365
|
+
consolidationWarning,
|
|
366
|
+
lessonsText.trim(),
|
|
367
|
+
lastSessionText.trim(),
|
|
368
|
+
notesText.trim(),
|
|
369
|
+
].filter(Boolean);
|
|
370
|
+
if (parts.length === 0) {
|
|
371
|
+
return {};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const systemMessage = parts.join('\n\n');
|
|
375
|
+
|
|
376
|
+
return {
|
|
377
|
+
systemMessage,
|
|
378
|
+
hookSpecificOutput: {
|
|
379
|
+
hookEventName: 'SessionStart',
|
|
380
|
+
additionalContext: systemMessage,
|
|
381
|
+
systemMessage,
|
|
382
|
+
},
|
|
383
|
+
injectSteps: [
|
|
384
|
+
{
|
|
385
|
+
systemMessage: {
|
|
386
|
+
systemMessage,
|
|
387
|
+
},
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
};
|
|
391
|
+
} catch {
|
|
392
|
+
return {};
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (require.main === module) {
|
|
397
|
+
(async () => {
|
|
398
|
+
try {
|
|
399
|
+
const input = await readStdin();
|
|
400
|
+
const output = await runSessionStart(input);
|
|
401
|
+
console.log(JSON.stringify(output || {}));
|
|
402
|
+
} catch {
|
|
403
|
+
console.log(JSON.stringify({}));
|
|
404
|
+
}
|
|
405
|
+
process.exit(0);
|
|
406
|
+
})();
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
module.exports = {
|
|
410
|
+
runSessionStart,
|
|
411
|
+
readDotEnvValue,
|
|
412
|
+
resolveApiKey,
|
|
413
|
+
resolveApiUrl,
|
|
414
|
+
resolveProjectId,
|
|
415
|
+
resolveSessionProject,
|
|
416
|
+
};
|