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
package/dist/app.js
ADDED
|
@@ -0,0 +1,1013 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.app = void 0;
|
|
7
|
+
exports.createApp = createApp;
|
|
8
|
+
const express_1 = __importDefault(require("express"));
|
|
9
|
+
const crypto_1 = __importDefault(require("crypto"));
|
|
10
|
+
const db_1 = require("./lib/db");
|
|
11
|
+
const logger_1 = require("./lib/logger");
|
|
12
|
+
const tracing_1 = require("./middleware/tracing");
|
|
13
|
+
const rateLimiter_1 = require("./middleware/rateLimiter");
|
|
14
|
+
const auth_1 = require("./middleware/auth");
|
|
15
|
+
const observationStore_1 = require("./memory/observationStore");
|
|
16
|
+
const trajectoryStore_1 = require("./memory/trajectoryStore");
|
|
17
|
+
const noteStore_1 = require("./memory/noteStore");
|
|
18
|
+
const rollupSweeper_1 = require("./memory/rollupSweeper");
|
|
19
|
+
const failurePatternStore_1 = require("./memory/failurePatternStore");
|
|
20
|
+
const deadLetterStore_1 = require("./memory/deadLetterStore");
|
|
21
|
+
const embedQueueStore_1 = require("./memory/embedQueueStore");
|
|
22
|
+
const sessionStore_1 = require("./memory/sessionStore");
|
|
23
|
+
const graphStore_1 = require("./memory/graphStore");
|
|
24
|
+
const projectRename_1 = require("./memory/projectRename");
|
|
25
|
+
const actionStore_1 = require("./memory/actionStore");
|
|
26
|
+
const storeMeta_1 = require("./learning/storeMeta");
|
|
27
|
+
const search_1 = require("./learning/search");
|
|
28
|
+
const embedWorker_1 = require("./learning/embedWorker");
|
|
29
|
+
const candidates_1 = require("./consolidation/candidates");
|
|
30
|
+
const gate_1 = require("./consolidation/gate");
|
|
31
|
+
const fixtures_1 = require("./consolidation/fixtures");
|
|
32
|
+
const runner_1 = require("./consolidation/runner");
|
|
33
|
+
const config_1 = require("./ai/config");
|
|
34
|
+
const circuitBreaker_1 = require("./ai/circuitBreaker");
|
|
35
|
+
const memories_1 = require("./routes/memories");
|
|
36
|
+
const learning_1 = require("./routes/learning");
|
|
37
|
+
const dashboard_1 = require("./routes/dashboard");
|
|
38
|
+
const mcp_1 = require("./routes/mcp");
|
|
39
|
+
function parseBoundedLimit(raw, defaultLimit = 50, maxLimit = 200) {
|
|
40
|
+
if (raw === undefined || raw === null || raw === '')
|
|
41
|
+
return defaultLimit;
|
|
42
|
+
const parsed = parseInt(String(raw), 10);
|
|
43
|
+
if (!Number.isFinite(parsed) || parsed <= 0)
|
|
44
|
+
return defaultLimit;
|
|
45
|
+
return Math.min(parsed, maxLimit);
|
|
46
|
+
}
|
|
47
|
+
function createApp() {
|
|
48
|
+
const app = (0, express_1.default)();
|
|
49
|
+
// Basic middleware
|
|
50
|
+
app.use(express_1.default.json({ limit: '10mb', strict: false }));
|
|
51
|
+
// Global security headers
|
|
52
|
+
app.use((_req, res, next) => {
|
|
53
|
+
res.setHeader('X-Content-Type-Options', 'nosniff');
|
|
54
|
+
res.setHeader('X-Frame-Options', 'DENY');
|
|
55
|
+
res.setHeader('Permissions-Policy', 'camera=(), microphone=(), geolocation=()');
|
|
56
|
+
next();
|
|
57
|
+
});
|
|
58
|
+
// Middleware pipeline order: tracing -> auth -> rate limiter -> routes
|
|
59
|
+
app.use(tracing_1.requestTracing);
|
|
60
|
+
app.use(auth_1.apiKeyAuth);
|
|
61
|
+
app.use((0, rateLimiter_1.createRateLimiter)());
|
|
62
|
+
// -------------------------------------------------------------------------
|
|
63
|
+
// Health (unauthenticated, rate-limit exempt)
|
|
64
|
+
// -------------------------------------------------------------------------
|
|
65
|
+
app.get('/health', async (_req, res) => {
|
|
66
|
+
let dbStatus = 'ok';
|
|
67
|
+
try {
|
|
68
|
+
(0, db_1.getDb)().prepare('SELECT 1').get();
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
dbStatus = 'unavailable';
|
|
72
|
+
}
|
|
73
|
+
const qdrantStatus = await (0, embedWorker_1.probeQdrant)();
|
|
74
|
+
let verification = {
|
|
75
|
+
match: false,
|
|
76
|
+
recipe: {
|
|
77
|
+
embed_tier: 'none',
|
|
78
|
+
embed_model: 'none',
|
|
79
|
+
embed_model_id: 'none',
|
|
80
|
+
embed_dim: 0,
|
|
81
|
+
},
|
|
82
|
+
mismatchReason: null,
|
|
83
|
+
};
|
|
84
|
+
let pendingEmbedCount = 0;
|
|
85
|
+
if (dbStatus === 'ok') {
|
|
86
|
+
try {
|
|
87
|
+
verification = (0, storeMeta_1.verifyOrCreateStoreMeta)();
|
|
88
|
+
pendingEmbedCount = (0, embedQueueStore_1.getPendingEmbedCount)('live');
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
dbStatus = 'unavailable';
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const status = dbStatus === 'ok' && verification.match ? 'ok' : 'degraded';
|
|
95
|
+
res.json({
|
|
96
|
+
status,
|
|
97
|
+
db: dbStatus,
|
|
98
|
+
qdrant: qdrantStatus,
|
|
99
|
+
embed_queue_pending: pendingEmbedCount,
|
|
100
|
+
store_meta: {
|
|
101
|
+
match: Boolean(verification.match),
|
|
102
|
+
embed_tier: verification.recipe?.embed_tier ?? 'none',
|
|
103
|
+
embed_model: verification.recipe?.embed_model ?? 'none',
|
|
104
|
+
embed_model_id: verification.recipe?.embed_model_id ?? 'none',
|
|
105
|
+
embed_dim: verification.recipe?.embed_dim ?? 0,
|
|
106
|
+
mismatch_reason: verification.mismatchReason || null,
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
// -------------------------------------------------------------------------
|
|
111
|
+
// Capture Hot-Paths (rate-limit exempt)
|
|
112
|
+
// -------------------------------------------------------------------------
|
|
113
|
+
app.post('/capture/observations', (req, res) => {
|
|
114
|
+
if (!req.body || typeof req.body !== 'object') {
|
|
115
|
+
res.status(400).json({
|
|
116
|
+
error: {
|
|
117
|
+
code: 'BAD_REQUEST',
|
|
118
|
+
message: 'Request body must be a valid JSON object or array',
|
|
119
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
120
|
+
},
|
|
121
|
+
});
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const raw = req.body;
|
|
125
|
+
const items = Array.isArray(raw) ? raw : (raw.observations ?? [raw]);
|
|
126
|
+
const result = (0, observationStore_1.captureObservations)(items);
|
|
127
|
+
res.status(201).json(result);
|
|
128
|
+
});
|
|
129
|
+
app.post('/capture/trajectories', (req, res) => {
|
|
130
|
+
if (!req.body || typeof req.body !== 'object') {
|
|
131
|
+
res.status(400).json({
|
|
132
|
+
error: {
|
|
133
|
+
code: 'BAD_REQUEST',
|
|
134
|
+
message: 'Request body must be a valid JSON object or array',
|
|
135
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const raw = req.body;
|
|
141
|
+
const items = Array.isArray(raw) ? raw : (raw.trajectories ?? [raw]);
|
|
142
|
+
const result = (0, trajectoryStore_1.captureTrajectories)(items);
|
|
143
|
+
res.status(201).json(result);
|
|
144
|
+
});
|
|
145
|
+
// -------------------------------------------------------------------------
|
|
146
|
+
// Retrieval & Search
|
|
147
|
+
// -------------------------------------------------------------------------
|
|
148
|
+
app.post('/search', async (req, res, next) => {
|
|
149
|
+
try {
|
|
150
|
+
if (!req.body || typeof req.body.q !== 'string') {
|
|
151
|
+
res.status(400).json({
|
|
152
|
+
error: {
|
|
153
|
+
code: 'INVALID_QUERY',
|
|
154
|
+
message: "Missing or invalid query parameter 'q'",
|
|
155
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const response = await (0, search_1.hybridSearch)(req.body);
|
|
161
|
+
res.json(response);
|
|
162
|
+
}
|
|
163
|
+
catch (err) {
|
|
164
|
+
next(err);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
// -------------------------------------------------------------------------
|
|
168
|
+
// Notes
|
|
169
|
+
// -------------------------------------------------------------------------
|
|
170
|
+
app.post('/notes', (req, res, next) => {
|
|
171
|
+
try {
|
|
172
|
+
const created = (0, noteStore_1.createNote)({
|
|
173
|
+
...req.body,
|
|
174
|
+
projectId: req.body.projectId ?? req.body.project_id,
|
|
175
|
+
session_id: req.body.session_id ?? req.body.sessionId,
|
|
176
|
+
});
|
|
177
|
+
res.status(201).json(created);
|
|
178
|
+
}
|
|
179
|
+
catch (err) {
|
|
180
|
+
next(err);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
app.get('/notes/:id', (req, res) => {
|
|
184
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
185
|
+
const note = (0, noteStore_1.getNote)(id);
|
|
186
|
+
if (!note) {
|
|
187
|
+
res.status(404).json({
|
|
188
|
+
error: {
|
|
189
|
+
code: 'NOT_FOUND',
|
|
190
|
+
message: `Note '${id}' not found`,
|
|
191
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
192
|
+
},
|
|
193
|
+
});
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
res.json(note);
|
|
197
|
+
});
|
|
198
|
+
app.patch('/notes/:id/scope', (req, res, next) => {
|
|
199
|
+
try {
|
|
200
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
201
|
+
const updated = (0, noteStore_1.updateNoteScope)(id, req.body.scope, req.body.projectId ?? req.body.project_id);
|
|
202
|
+
res.json(updated);
|
|
203
|
+
}
|
|
204
|
+
catch (err) {
|
|
205
|
+
next(err);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
app.get('/notes', (req, res) => {
|
|
209
|
+
const scope = req.query.scope;
|
|
210
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
211
|
+
const includeGlobal = req.query.include_global !== 'false';
|
|
212
|
+
const concept = req.query.concept;
|
|
213
|
+
const filePath = (req.query.file_path || req.query.filePath);
|
|
214
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : undefined;
|
|
215
|
+
const offset = req.query.offset ? parseInt(String(req.query.offset), 10) : undefined;
|
|
216
|
+
let type;
|
|
217
|
+
if (req.query.type) {
|
|
218
|
+
type = Array.isArray(req.query.type)
|
|
219
|
+
? req.query.type
|
|
220
|
+
: [String(req.query.type)];
|
|
221
|
+
}
|
|
222
|
+
const notes = (0, noteStore_1.listNotes)({
|
|
223
|
+
scope,
|
|
224
|
+
projectId,
|
|
225
|
+
includeGlobal,
|
|
226
|
+
type,
|
|
227
|
+
concept,
|
|
228
|
+
filePath,
|
|
229
|
+
limit,
|
|
230
|
+
offset,
|
|
231
|
+
});
|
|
232
|
+
res.json(notes);
|
|
233
|
+
});
|
|
234
|
+
// Files -> Notes associations (support URL-encoded and wildcard paths)
|
|
235
|
+
app.get(/^\/files\/(.+)\/notes$/, (req, res) => {
|
|
236
|
+
const rawPath = Array.isArray(req.params[0]) ? req.params[0][0] : req.params[0];
|
|
237
|
+
const decodedPath = decodeURIComponent(String(rawPath));
|
|
238
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
239
|
+
const notes = (0, noteStore_1.getNotesByFile)(decodedPath, projectId);
|
|
240
|
+
res.json(notes);
|
|
241
|
+
});
|
|
242
|
+
app.get('/files/:path/notes', (req, res) => {
|
|
243
|
+
const rawPath = Array.isArray(req.params.path) ? req.params.path[0] : req.params.path;
|
|
244
|
+
const decodedPath = decodeURIComponent(String(rawPath));
|
|
245
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
246
|
+
const notes = (0, noteStore_1.getNotesByFile)(decodedPath, projectId);
|
|
247
|
+
res.json(notes);
|
|
248
|
+
});
|
|
249
|
+
// -------------------------------------------------------------------------
|
|
250
|
+
// Trajectories
|
|
251
|
+
// -------------------------------------------------------------------------
|
|
252
|
+
app.get('/trajectories/:id', (req, res) => {
|
|
253
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
254
|
+
const traj = (0, trajectoryStore_1.getTrajectory)(id);
|
|
255
|
+
if (!traj) {
|
|
256
|
+
res.status(404).json({
|
|
257
|
+
error: {
|
|
258
|
+
code: 'NOT_FOUND',
|
|
259
|
+
message: `Trajectory '${id}' not found`,
|
|
260
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
261
|
+
},
|
|
262
|
+
});
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
res.json(traj);
|
|
266
|
+
});
|
|
267
|
+
app.get('/trajectories', (req, res) => {
|
|
268
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
269
|
+
const scope = req.query.scope;
|
|
270
|
+
const taskType = (req.query.task_type || req.query.taskType);
|
|
271
|
+
// 'unknown' is a third answer here, not a missing one: a swept rollup has
|
|
272
|
+
// no outcome, and folding it into either branch would invent one.
|
|
273
|
+
const rawSuccess = req.query.success;
|
|
274
|
+
const successUnknown = rawSuccess === 'unknown' || rawSuccess === 'null';
|
|
275
|
+
const success = rawSuccess !== undefined && !successUnknown
|
|
276
|
+
? rawSuccess === 'true' || rawSuccess === '1'
|
|
277
|
+
: undefined;
|
|
278
|
+
const rollupSourceParam = (req.query.rollup_source || req.query.rollupSource);
|
|
279
|
+
const rollupSource = rollupSourceParam === 'signalled' || rollupSourceParam === 'swept'
|
|
280
|
+
? rollupSourceParam
|
|
281
|
+
: undefined;
|
|
282
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : undefined;
|
|
283
|
+
const offset = req.query.offset ? parseInt(String(req.query.offset), 10) : undefined;
|
|
284
|
+
const list = (0, trajectoryStore_1.listTrajectories)({
|
|
285
|
+
projectId,
|
|
286
|
+
scope,
|
|
287
|
+
taskType,
|
|
288
|
+
success,
|
|
289
|
+
successUnknown,
|
|
290
|
+
rollupSource,
|
|
291
|
+
limit,
|
|
292
|
+
offset,
|
|
293
|
+
});
|
|
294
|
+
res.json(list);
|
|
295
|
+
});
|
|
296
|
+
// -------------------------------------------------------------------------
|
|
297
|
+
// Observations
|
|
298
|
+
// -------------------------------------------------------------------------
|
|
299
|
+
app.get('/observations/:id', (req, res) => {
|
|
300
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
301
|
+
const obs = (0, observationStore_1.getObservation)(id);
|
|
302
|
+
if (!obs) {
|
|
303
|
+
res.status(404).json({
|
|
304
|
+
error: {
|
|
305
|
+
code: 'NOT_FOUND',
|
|
306
|
+
message: `Observation '${id}' not found`,
|
|
307
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
res.json(obs);
|
|
313
|
+
});
|
|
314
|
+
app.get('/observations', (req, res) => {
|
|
315
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
316
|
+
const scope = req.query.scope;
|
|
317
|
+
const sessionId = (req.query.session_id || req.query.sessionId);
|
|
318
|
+
const failed = req.query.failed !== undefined ? parseInt(String(req.query.failed), 10) : undefined;
|
|
319
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : undefined;
|
|
320
|
+
const offset = req.query.offset ? parseInt(String(req.query.offset), 10) : undefined;
|
|
321
|
+
const list = (0, observationStore_1.listObservations)({
|
|
322
|
+
projectId,
|
|
323
|
+
scope,
|
|
324
|
+
sessionId,
|
|
325
|
+
failed,
|
|
326
|
+
limit,
|
|
327
|
+
offset,
|
|
328
|
+
});
|
|
329
|
+
res.json(list);
|
|
330
|
+
});
|
|
331
|
+
// -------------------------------------------------------------------------
|
|
332
|
+
// Summaries
|
|
333
|
+
// -------------------------------------------------------------------------
|
|
334
|
+
app.get('/summaries/:id', (req, res) => {
|
|
335
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
336
|
+
const db = (0, db_1.getDb)();
|
|
337
|
+
const summary = db
|
|
338
|
+
.prepare('SELECT * FROM summaries WHERE id = ?')
|
|
339
|
+
.get(id);
|
|
340
|
+
if (!summary) {
|
|
341
|
+
res.status(404).json({
|
|
342
|
+
error: {
|
|
343
|
+
code: 'NOT_FOUND',
|
|
344
|
+
message: `Summary '${id}' not found`,
|
|
345
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
const claims = db
|
|
351
|
+
.prepare('SELECT id, summary_id, text, ordinal FROM summary_claims WHERE summary_id = ? ORDER BY ordinal ASC')
|
|
352
|
+
.all(summary.id);
|
|
353
|
+
const sources = db
|
|
354
|
+
.prepare('SELECT summary_id, claim_id, record_id, grain FROM summary_sources WHERE summary_id = ?')
|
|
355
|
+
.all(summary.id);
|
|
356
|
+
res.json({
|
|
357
|
+
...summary,
|
|
358
|
+
claims,
|
|
359
|
+
sources,
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
app.get('/summaries', (req, res) => {
|
|
363
|
+
const db = (0, db_1.getDb)();
|
|
364
|
+
const conditions = [];
|
|
365
|
+
const params = [];
|
|
366
|
+
if (req.query.project_id || req.query.projectId) {
|
|
367
|
+
conditions.push('project_id = ?');
|
|
368
|
+
params.push((req.query.project_id || req.query.projectId));
|
|
369
|
+
}
|
|
370
|
+
if (req.query.scope) {
|
|
371
|
+
conditions.push('scope = ?');
|
|
372
|
+
params.push(req.query.scope);
|
|
373
|
+
}
|
|
374
|
+
if (req.query.kind) {
|
|
375
|
+
conditions.push('kind = ?');
|
|
376
|
+
params.push(req.query.kind);
|
|
377
|
+
}
|
|
378
|
+
if (req.query.only_latest !== 'false') {
|
|
379
|
+
conditions.push('is_latest = 1');
|
|
380
|
+
}
|
|
381
|
+
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
382
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : 50;
|
|
383
|
+
const offset = req.query.offset ? parseInt(String(req.query.offset), 10) : 0;
|
|
384
|
+
params.push(limit, offset);
|
|
385
|
+
const summaries = db
|
|
386
|
+
.prepare(`SELECT * FROM summaries ${whereClause} ORDER BY created_at DESC LIMIT ? OFFSET ?`)
|
|
387
|
+
.all(...params);
|
|
388
|
+
if (summaries.length === 0) {
|
|
389
|
+
res.json([]);
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const summaryIds = summaries.map((s) => s.id);
|
|
393
|
+
const placeholders = summaryIds.map(() => '?').join(',');
|
|
394
|
+
const allClaims = db
|
|
395
|
+
.prepare(`SELECT id, summary_id, text, ordinal FROM summary_claims WHERE summary_id IN (${placeholders}) ORDER BY ordinal ASC`)
|
|
396
|
+
.all(...summaryIds);
|
|
397
|
+
const allSources = db
|
|
398
|
+
.prepare(`SELECT summary_id, claim_id, record_id, grain FROM summary_sources WHERE summary_id IN (${placeholders})`)
|
|
399
|
+
.all(...summaryIds);
|
|
400
|
+
const claimsBySummary = new Map();
|
|
401
|
+
for (const c of allClaims) {
|
|
402
|
+
const list = claimsBySummary.get(c.summary_id) || [];
|
|
403
|
+
list.push(c);
|
|
404
|
+
claimsBySummary.set(c.summary_id, list);
|
|
405
|
+
}
|
|
406
|
+
const sourcesBySummary = new Map();
|
|
407
|
+
for (const s of allSources) {
|
|
408
|
+
const list = sourcesBySummary.get(s.summary_id) || [];
|
|
409
|
+
list.push(s);
|
|
410
|
+
sourcesBySummary.set(s.summary_id, list);
|
|
411
|
+
}
|
|
412
|
+
const results = summaries.map((s) => ({
|
|
413
|
+
...s,
|
|
414
|
+
claims: claimsBySummary.get(s.id) || [],
|
|
415
|
+
sources: sourcesBySummary.get(s.id) || [],
|
|
416
|
+
}));
|
|
417
|
+
res.json(results);
|
|
418
|
+
});
|
|
419
|
+
// -------------------------------------------------------------------------
|
|
420
|
+
// Sessions
|
|
421
|
+
// -------------------------------------------------------------------------
|
|
422
|
+
app.post('/sessions/start', (req, res, next) => {
|
|
423
|
+
try {
|
|
424
|
+
const session = (0, sessionStore_1.startSession)(req.body ?? {});
|
|
425
|
+
res.status(201).json({ session });
|
|
426
|
+
}
|
|
427
|
+
catch (err) {
|
|
428
|
+
next(err);
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
app.post('/sessions/:id/end', (req, res, next) => {
|
|
432
|
+
try {
|
|
433
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
434
|
+
const session = (0, sessionStore_1.endSession)(id, req.body ?? {});
|
|
435
|
+
if (!session) {
|
|
436
|
+
res.status(404).json({
|
|
437
|
+
error: {
|
|
438
|
+
code: 'NOT_FOUND',
|
|
439
|
+
message: `Session '${id}' not found`,
|
|
440
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
res.json({ session });
|
|
446
|
+
}
|
|
447
|
+
catch (err) {
|
|
448
|
+
next(err);
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
app.get('/sessions', (req, res) => {
|
|
452
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
453
|
+
const status = req.query.status;
|
|
454
|
+
const agentName = (req.query.agent_name || req.query.agentName);
|
|
455
|
+
const limit = parseBoundedLimit(req.query.limit, 50, 200);
|
|
456
|
+
const sessions = (0, sessionStore_1.listSessions)({ projectId, status, agentName, limit });
|
|
457
|
+
res.json({ sessions });
|
|
458
|
+
});
|
|
459
|
+
app.get('/sessions/:id', (req, res) => {
|
|
460
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
461
|
+
const details = (0, sessionStore_1.getSessionDetails)(id);
|
|
462
|
+
if (!details) {
|
|
463
|
+
res.status(404).json({
|
|
464
|
+
error: {
|
|
465
|
+
code: 'NOT_FOUND',
|
|
466
|
+
message: `Session '${id}' not found`,
|
|
467
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
468
|
+
},
|
|
469
|
+
});
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
res.json(details);
|
|
473
|
+
});
|
|
474
|
+
app.get('/sessions/:id/replay', (req, res) => {
|
|
475
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
476
|
+
const replay = (0, sessionStore_1.getSessionReplay)(id);
|
|
477
|
+
if (!replay) {
|
|
478
|
+
res.status(404).json({
|
|
479
|
+
error: {
|
|
480
|
+
code: 'NOT_FOUND',
|
|
481
|
+
message: `Session '${id}' not found`,
|
|
482
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
483
|
+
},
|
|
484
|
+
});
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
res.json(replay);
|
|
488
|
+
});
|
|
489
|
+
// -------------------------------------------------------------------------
|
|
490
|
+
// Knowledge Graph
|
|
491
|
+
// -------------------------------------------------------------------------
|
|
492
|
+
app.get('/graph', (req, res) => {
|
|
493
|
+
const graph = (0, graphStore_1.getKnowledgeGraph)((0, graphStore_1.parseGraphQuery)(req.query));
|
|
494
|
+
res.json(graph);
|
|
495
|
+
});
|
|
496
|
+
// -------------------------------------------------------------------------
|
|
497
|
+
// Actions
|
|
498
|
+
// -------------------------------------------------------------------------
|
|
499
|
+
app.post('/actions', (req, res, next) => {
|
|
500
|
+
try {
|
|
501
|
+
const action = (0, actionStore_1.createAction)(req.body ?? {});
|
|
502
|
+
res.status(201).json({ action });
|
|
503
|
+
}
|
|
504
|
+
catch (err) {
|
|
505
|
+
next(err);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
app.get('/actions', (req, res) => {
|
|
509
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
510
|
+
const includeGlobal = req.query.include_global !== 'false';
|
|
511
|
+
const sessionId = (req.query.session_id || req.query.sessionId);
|
|
512
|
+
const limit = parseBoundedLimit(req.query.limit, 50, 200);
|
|
513
|
+
let status;
|
|
514
|
+
if (req.query.status) {
|
|
515
|
+
status = Array.isArray(req.query.status)
|
|
516
|
+
? req.query.status
|
|
517
|
+
: String(req.query.status);
|
|
518
|
+
}
|
|
519
|
+
const actions = (0, actionStore_1.listActions)({
|
|
520
|
+
projectId,
|
|
521
|
+
includeGlobal,
|
|
522
|
+
status,
|
|
523
|
+
sessionId,
|
|
524
|
+
limit,
|
|
525
|
+
});
|
|
526
|
+
res.json({ actions });
|
|
527
|
+
});
|
|
528
|
+
app.patch('/actions/:id', (req, res, next) => {
|
|
529
|
+
try {
|
|
530
|
+
const id = Array.isArray(req.params.id) ? req.params.id[0] : req.params.id;
|
|
531
|
+
const action = (0, actionStore_1.updateAction)(id, req.body ?? {});
|
|
532
|
+
if (!action) {
|
|
533
|
+
res.status(404).json({
|
|
534
|
+
error: {
|
|
535
|
+
code: 'NOT_FOUND',
|
|
536
|
+
message: `Action '${id}' not found`,
|
|
537
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
538
|
+
},
|
|
539
|
+
});
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
res.json({ action });
|
|
543
|
+
}
|
|
544
|
+
catch (err) {
|
|
545
|
+
next(err);
|
|
546
|
+
}
|
|
547
|
+
});
|
|
548
|
+
// -------------------------------------------------------------------------
|
|
549
|
+
// Push Injection
|
|
550
|
+
// -------------------------------------------------------------------------
|
|
551
|
+
app.get('/inject', (req, res) => {
|
|
552
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
553
|
+
const taskType = (req.query.task_type || req.query.taskType);
|
|
554
|
+
const limit = parseBoundedLimit(req.query.limit, 5, 200);
|
|
555
|
+
const failure_patterns = (0, failurePatternStore_1.getFailurePatterns)({
|
|
556
|
+
projectId,
|
|
557
|
+
taskType,
|
|
558
|
+
includeGlobal: true,
|
|
559
|
+
limit,
|
|
560
|
+
});
|
|
561
|
+
const prior_outcomes = (0, trajectoryStore_1.listTrajectories)({
|
|
562
|
+
projectId,
|
|
563
|
+
taskType,
|
|
564
|
+
limit,
|
|
565
|
+
});
|
|
566
|
+
const ready_actions = (0, actionStore_1.getReadyActions)({
|
|
567
|
+
projectId,
|
|
568
|
+
limit,
|
|
569
|
+
});
|
|
570
|
+
const latest_session_summary = (0, sessionStore_1.getLatestSessionSummary)(projectId);
|
|
571
|
+
const lessons = (0, noteStore_1.listNotes)({
|
|
572
|
+
projectId,
|
|
573
|
+
includeGlobal: true,
|
|
574
|
+
type: ['lesson', 'bug', 'pattern'],
|
|
575
|
+
limit,
|
|
576
|
+
});
|
|
577
|
+
const notes = (0, noteStore_1.listNotes)({
|
|
578
|
+
projectId,
|
|
579
|
+
includeGlobal: true,
|
|
580
|
+
type: ['architecture', 'workflow', 'preference', 'fact'],
|
|
581
|
+
limit,
|
|
582
|
+
});
|
|
583
|
+
const formatted_warnings = (0, failurePatternStore_1.formatCriticalWarnings)(failure_patterns);
|
|
584
|
+
const consolidation_warning = (0, candidates_1.formatConsolidationWarning)((0, candidates_1.getConsolidationStatus)(projectId), projectId);
|
|
585
|
+
res.json({
|
|
586
|
+
failure_patterns,
|
|
587
|
+
prior_outcomes,
|
|
588
|
+
ready_actions,
|
|
589
|
+
latest_session_summary,
|
|
590
|
+
lessons,
|
|
591
|
+
notes,
|
|
592
|
+
formatted_warnings,
|
|
593
|
+
consolidation_warning,
|
|
594
|
+
});
|
|
595
|
+
});
|
|
596
|
+
// -------------------------------------------------------------------------
|
|
597
|
+
// Consolidation
|
|
598
|
+
// -------------------------------------------------------------------------
|
|
599
|
+
app.get('/consolidate/candidates', (req, res) => {
|
|
600
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
601
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : undefined;
|
|
602
|
+
const leaseDurationSec = req.query.lease_duration_sec
|
|
603
|
+
? parseInt(String(req.query.lease_duration_sec), 10)
|
|
604
|
+
: undefined;
|
|
605
|
+
const result = (0, candidates_1.getConsolidationCandidates)({
|
|
606
|
+
projectId,
|
|
607
|
+
limit,
|
|
608
|
+
leaseDurationSec,
|
|
609
|
+
});
|
|
610
|
+
res.json(result);
|
|
611
|
+
});
|
|
612
|
+
app.post('/consolidate/summaries', (req, res) => {
|
|
613
|
+
if (!req.body || typeof req.body !== 'object' || Array.isArray(req.body)) {
|
|
614
|
+
res.status(400).json({
|
|
615
|
+
error: {
|
|
616
|
+
code: 'BAD_REQUEST',
|
|
617
|
+
message: 'Request body must be a valid JSON object',
|
|
618
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
619
|
+
},
|
|
620
|
+
});
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
const result = (0, gate_1.verifyAndStoreSummary)(req.body);
|
|
624
|
+
if (!result.ok) {
|
|
625
|
+
res.status(result.status).json({
|
|
626
|
+
error: {
|
|
627
|
+
code: result.reason,
|
|
628
|
+
message: result.message,
|
|
629
|
+
details: result.details,
|
|
630
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
631
|
+
},
|
|
632
|
+
});
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
res.status(result.status).json(result);
|
|
636
|
+
});
|
|
637
|
+
// Runs the configured server-side producer (`remote` or `vendor`). With the
|
|
638
|
+
// default tier `agent` it does nothing but say so: the host agent pulls
|
|
639
|
+
// candidates above and posts summaries back, holding no key at all.
|
|
640
|
+
app.post('/consolidate/run', async (req, res, next) => {
|
|
641
|
+
try {
|
|
642
|
+
const body = req.body ?? {};
|
|
643
|
+
const report = await (0, runner_1.runConsolidation)({
|
|
644
|
+
projectId: body.project_id ?? body.projectId,
|
|
645
|
+
limit: body.limit ? parseInt(String(body.limit), 10) : undefined,
|
|
646
|
+
leaseDurationSec: body.lease_duration_sec
|
|
647
|
+
? parseInt(String(body.lease_duration_sec), 10)
|
|
648
|
+
: undefined,
|
|
649
|
+
});
|
|
650
|
+
res.status(report.status === 'misconfigured' ? 400 : 200).json(report);
|
|
651
|
+
}
|
|
652
|
+
catch (err) {
|
|
653
|
+
next(err);
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
// -------------------------------------------------------------------------
|
|
657
|
+
// Operations & Maintenance
|
|
658
|
+
// -------------------------------------------------------------------------
|
|
659
|
+
app.post('/maintenance/cleanup', (_req, res) => {
|
|
660
|
+
const result = (0, trajectoryStore_1.runCleanup)();
|
|
661
|
+
res.json(result);
|
|
662
|
+
});
|
|
663
|
+
/**
|
|
664
|
+
* Roll up sessions nobody closed. Arithmetic only, idempotent, and the
|
|
665
|
+
* safety net behind the explicit `observation_ids` path — see
|
|
666
|
+
* src/memory/rollupSweeper.ts.
|
|
667
|
+
*/
|
|
668
|
+
app.post('/maintenance/rollup-sweep', (req, res) => {
|
|
669
|
+
const body = (req.body ?? {});
|
|
670
|
+
const rawIdle = body.idle_minutes ?? body.idleMinutes ?? req.query.idle_minutes;
|
|
671
|
+
const rawLimit = body.limit ?? req.query.limit;
|
|
672
|
+
const idleMinutes = rawIdle !== undefined ? Number(rawIdle) : undefined;
|
|
673
|
+
if (idleMinutes !== undefined && (!Number.isFinite(idleMinutes) || idleMinutes < 0)) {
|
|
674
|
+
res.status(400).json({
|
|
675
|
+
error: {
|
|
676
|
+
code: 'SCHEMA_INVALID',
|
|
677
|
+
message: 'idle_minutes must be a non-negative number',
|
|
678
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
679
|
+
},
|
|
680
|
+
});
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
const limit = rawLimit !== undefined ? Number(rawLimit) : undefined;
|
|
684
|
+
if (limit !== undefined && (!Number.isFinite(limit) || limit < 1)) {
|
|
685
|
+
res.status(400).json({
|
|
686
|
+
error: {
|
|
687
|
+
code: 'SCHEMA_INVALID',
|
|
688
|
+
message: 'limit must be a positive number',
|
|
689
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
690
|
+
},
|
|
691
|
+
});
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const result = (0, rollupSweeper_1.sweepIdleSessions)({ idleMinutes, limit });
|
|
695
|
+
res.json(result);
|
|
696
|
+
});
|
|
697
|
+
app.post('/maintenance/reembed', async (_req, res, next) => {
|
|
698
|
+
try {
|
|
699
|
+
const currentStatus = (0, embedWorker_1.getReembedStatus)();
|
|
700
|
+
if (currentStatus.status === 'running') {
|
|
701
|
+
res.status(409).json({
|
|
702
|
+
error: {
|
|
703
|
+
code: 'REEMBED_IN_PROGRESS',
|
|
704
|
+
message: 'A re-embedding process is already running',
|
|
705
|
+
status: currentStatus,
|
|
706
|
+
trace_id: (0, tracing_1.getCorrelationId)(_req),
|
|
707
|
+
},
|
|
708
|
+
});
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
const result = await (0, embedWorker_1.startReembed)();
|
|
712
|
+
res.json(result);
|
|
713
|
+
}
|
|
714
|
+
catch (err) {
|
|
715
|
+
next(err);
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
app.get('/maintenance/reembed', (_req, res) => {
|
|
719
|
+
res.json((0, embedWorker_1.getReembedStatus)());
|
|
720
|
+
});
|
|
721
|
+
app.post('/maintenance/rename-project', async (req, res, next) => {
|
|
722
|
+
try {
|
|
723
|
+
const body = req.body && typeof req.body === 'object' ? req.body : {};
|
|
724
|
+
const fromId = body.from_project_id ?? body.fromProjectId;
|
|
725
|
+
const toId = body.to_project_id ?? body.toProjectId;
|
|
726
|
+
if (typeof fromId !== 'string' || typeof toId !== 'string') {
|
|
727
|
+
res.status(400).json({
|
|
728
|
+
error: {
|
|
729
|
+
code: 'SCHEMA_INVALID',
|
|
730
|
+
message: 'from_project_id and to_project_id strings are required',
|
|
731
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
732
|
+
},
|
|
733
|
+
});
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
const result = await (0, projectRename_1.renameProject)(fromId, toId);
|
|
737
|
+
res.json(result);
|
|
738
|
+
}
|
|
739
|
+
catch (err) {
|
|
740
|
+
if (err instanceof Error && (err.message.startsWith('Invalid ') || err.message.includes('must be different'))) {
|
|
741
|
+
res.status(400).json({
|
|
742
|
+
error: {
|
|
743
|
+
code: 'SCHEMA_INVALID',
|
|
744
|
+
message: err.message,
|
|
745
|
+
trace_id: (0, tracing_1.getCorrelationId)(req),
|
|
746
|
+
},
|
|
747
|
+
});
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
next(err);
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
// -------------------------------------------------------------------------
|
|
754
|
+
// In-Service API Key Management (Single-Writer Compliant)
|
|
755
|
+
// -------------------------------------------------------------------------
|
|
756
|
+
app.get('/maintenance/api-keys', (_req, res) => {
|
|
757
|
+
const traceId = (0, tracing_1.getCorrelationId)(_req);
|
|
758
|
+
try {
|
|
759
|
+
const db = (0, db_1.getDb)();
|
|
760
|
+
const keys = db
|
|
761
|
+
.prepare(`SELECT id, name, created_at, last_used_at, expires_at
|
|
762
|
+
FROM api_keys
|
|
763
|
+
WHERE revoked_at IS NULL AND (expires_at IS NULL OR expires_at > datetime('now'))
|
|
764
|
+
ORDER BY created_at DESC`)
|
|
765
|
+
.all();
|
|
766
|
+
res.json(keys);
|
|
767
|
+
}
|
|
768
|
+
catch (err) {
|
|
769
|
+
logger_1.logger.error({ error: err, traceId }, 'failed to list api keys');
|
|
770
|
+
res.status(500).json({
|
|
771
|
+
error: {
|
|
772
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
773
|
+
message: 'Failed to list API keys',
|
|
774
|
+
trace_id: traceId,
|
|
775
|
+
},
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
app.post('/maintenance/api-keys', (req, res) => {
|
|
780
|
+
const traceId = (0, tracing_1.getCorrelationId)(req);
|
|
781
|
+
const body = req.body && typeof req.body === 'object' ? req.body : {};
|
|
782
|
+
let name = typeof body.name === 'string' ? body.name.trim() : 'agent-key';
|
|
783
|
+
if (!name)
|
|
784
|
+
name = 'agent-key';
|
|
785
|
+
if (name.length > 100) {
|
|
786
|
+
res.status(400).json({
|
|
787
|
+
error: {
|
|
788
|
+
code: 'BAD_REQUEST',
|
|
789
|
+
message: 'name must be at most 100 characters',
|
|
790
|
+
trace_id: traceId,
|
|
791
|
+
},
|
|
792
|
+
});
|
|
793
|
+
return;
|
|
794
|
+
}
|
|
795
|
+
let expiresIn = null;
|
|
796
|
+
const rawExpires = body.expires_in_seconds ?? body.expiresInSeconds ?? body.expires_in;
|
|
797
|
+
if (rawExpires !== undefined && rawExpires !== null) {
|
|
798
|
+
const val = Number(rawExpires);
|
|
799
|
+
if (!Number.isFinite(val) || val <= 0) {
|
|
800
|
+
res.status(400).json({
|
|
801
|
+
error: {
|
|
802
|
+
code: 'BAD_REQUEST',
|
|
803
|
+
message: 'expires_in_seconds must be a positive number',
|
|
804
|
+
trace_id: traceId,
|
|
805
|
+
},
|
|
806
|
+
});
|
|
807
|
+
return;
|
|
808
|
+
}
|
|
809
|
+
expiresIn = val;
|
|
810
|
+
}
|
|
811
|
+
try {
|
|
812
|
+
const id = crypto_1.default.randomUUID();
|
|
813
|
+
const rawKey = crypto_1.default.randomBytes(32).toString('hex');
|
|
814
|
+
const hashedKey = (0, auth_1.hashApiKey)(rawKey);
|
|
815
|
+
const expiresAt = expiresIn ? new Date(Date.now() + expiresIn * 1000).toISOString() : null;
|
|
816
|
+
const createdAt = new Date().toISOString();
|
|
817
|
+
const db = (0, db_1.getDb)();
|
|
818
|
+
db.prepare(`
|
|
819
|
+
INSERT INTO api_keys (id, name, key, expires_at, created_at)
|
|
820
|
+
VALUES (?, ?, ?, ?, ?)
|
|
821
|
+
`).run(id, name, hashedKey, expiresAt, createdAt);
|
|
822
|
+
logger_1.logger.info({ keyId: id, name }, 'api key created via maintenance endpoint');
|
|
823
|
+
if (req.headers['x-requested-with']) {
|
|
824
|
+
(0, auth_1.setDashboardSessionCookie)(res, id, name, req.secure);
|
|
825
|
+
}
|
|
826
|
+
res.status(201).json({
|
|
827
|
+
id,
|
|
828
|
+
name,
|
|
829
|
+
key: rawKey,
|
|
830
|
+
created_at: createdAt,
|
|
831
|
+
expires_at: expiresAt,
|
|
832
|
+
});
|
|
833
|
+
}
|
|
834
|
+
catch (err) {
|
|
835
|
+
logger_1.logger.error({ error: err, traceId }, 'failed to create api key');
|
|
836
|
+
res.status(500).json({
|
|
837
|
+
error: {
|
|
838
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
839
|
+
message: 'Failed to create API key',
|
|
840
|
+
trace_id: traceId,
|
|
841
|
+
},
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
app.delete('/maintenance/api-keys/:id', (req, res) => {
|
|
846
|
+
const traceId = (0, tracing_1.getCorrelationId)(req);
|
|
847
|
+
const keyId = req.params.id;
|
|
848
|
+
if (!keyId) {
|
|
849
|
+
res.status(400).json({
|
|
850
|
+
error: {
|
|
851
|
+
code: 'BAD_REQUEST',
|
|
852
|
+
message: 'API key ID is required',
|
|
853
|
+
trace_id: traceId,
|
|
854
|
+
},
|
|
855
|
+
});
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
try {
|
|
859
|
+
const db = (0, db_1.getDb)();
|
|
860
|
+
const result = db.prepare(`DELETE FROM api_keys WHERE id = ?`).run(keyId);
|
|
861
|
+
if (result.changes === 0) {
|
|
862
|
+
res.status(404).json({
|
|
863
|
+
error: {
|
|
864
|
+
code: 'NOT_FOUND',
|
|
865
|
+
message: `API key '${keyId}' not found`,
|
|
866
|
+
trace_id: traceId,
|
|
867
|
+
},
|
|
868
|
+
});
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
logger_1.logger.info({ keyId }, 'api key deleted via maintenance endpoint');
|
|
872
|
+
res.json({ success: true, id: keyId });
|
|
873
|
+
}
|
|
874
|
+
catch (err) {
|
|
875
|
+
logger_1.logger.error({ error: err, traceId }, 'failed to delete api key');
|
|
876
|
+
res.status(500).json({
|
|
877
|
+
error: {
|
|
878
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
879
|
+
message: 'Failed to delete API key',
|
|
880
|
+
trace_id: traceId,
|
|
881
|
+
},
|
|
882
|
+
});
|
|
883
|
+
}
|
|
884
|
+
});
|
|
885
|
+
app.get('/diagnostics/ai', (_req, res) => {
|
|
886
|
+
const diagnostics = (0, fixtures_1.runAiConformanceDiagnostics)();
|
|
887
|
+
res.json(diagnostics);
|
|
888
|
+
});
|
|
889
|
+
// Resolved generative configuration. Never includes an API key or any part
|
|
890
|
+
// of one — it reports only which env var a tier needs.
|
|
891
|
+
app.get('/diagnostics/ai-config', (_req, res) => {
|
|
892
|
+
res.json({
|
|
893
|
+
...(0, config_1.describeAiConfig)(),
|
|
894
|
+
circuit_breakers: (0, circuitBreaker_1.getBreakerStatus)(),
|
|
895
|
+
});
|
|
896
|
+
});
|
|
897
|
+
app.get('/failure-patterns', (req, res) => {
|
|
898
|
+
const projectId = (req.query.project_id || req.query.projectId);
|
|
899
|
+
const taskType = (req.query.task_type || req.query.taskType);
|
|
900
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : undefined;
|
|
901
|
+
const patterns = (0, failurePatternStore_1.getFailurePatterns)({
|
|
902
|
+
projectId,
|
|
903
|
+
taskType,
|
|
904
|
+
includeGlobal: true,
|
|
905
|
+
limit,
|
|
906
|
+
});
|
|
907
|
+
res.json(patterns);
|
|
908
|
+
});
|
|
909
|
+
app.get('/dead-letters', (req, res) => {
|
|
910
|
+
const source = req.query.source;
|
|
911
|
+
const reason = req.query.reason;
|
|
912
|
+
const producer = req.query.producer;
|
|
913
|
+
const limit = req.query.limit ? parseInt(String(req.query.limit), 10) : undefined;
|
|
914
|
+
const offset = req.query.offset ? parseInt(String(req.query.offset), 10) : undefined;
|
|
915
|
+
const deadLetters = (0, deadLetterStore_1.listDeadLetters)({
|
|
916
|
+
source,
|
|
917
|
+
reason,
|
|
918
|
+
producer,
|
|
919
|
+
limit,
|
|
920
|
+
offset,
|
|
921
|
+
});
|
|
922
|
+
res.json(deadLetters);
|
|
923
|
+
});
|
|
924
|
+
// -------------------------------------------------------------------------
|
|
925
|
+
// Dashboard & Operator Console
|
|
926
|
+
// -------------------------------------------------------------------------
|
|
927
|
+
app.get('/', (_req, res) => {
|
|
928
|
+
res.redirect(302, '/dashboard');
|
|
929
|
+
});
|
|
930
|
+
app.use('/dashboard', dashboard_1.dashboardRouter);
|
|
931
|
+
// -------------------------------------------------------------------------
|
|
932
|
+
// Legacy mounted routes
|
|
933
|
+
// -------------------------------------------------------------------------
|
|
934
|
+
app.use('/memories', memories_1.memoriesRouter);
|
|
935
|
+
app.use('/learning', learning_1.learningRouter);
|
|
936
|
+
app.use('/mcp', mcp_1.mcpRouter);
|
|
937
|
+
// -------------------------------------------------------------------------
|
|
938
|
+
// Standard Error Handler
|
|
939
|
+
// -------------------------------------------------------------------------
|
|
940
|
+
app.use((err, req, res, _next) => {
|
|
941
|
+
const traceId = (0, tracing_1.getCorrelationId)(req);
|
|
942
|
+
logger_1.logger.error({
|
|
943
|
+
path: req.path,
|
|
944
|
+
method: req.method,
|
|
945
|
+
error: err instanceof Error ? err.message : String(err),
|
|
946
|
+
stack: err instanceof Error ? err.stack : undefined,
|
|
947
|
+
traceId,
|
|
948
|
+
}, 'unhandled error during request processing');
|
|
949
|
+
if ((0, db_1.isDatabaseUnavailableError)(err)) {
|
|
950
|
+
res.status(503).json({
|
|
951
|
+
error: {
|
|
952
|
+
code: 'DATABASE_UNAVAILABLE',
|
|
953
|
+
message: err.message,
|
|
954
|
+
trace_id: traceId,
|
|
955
|
+
},
|
|
956
|
+
});
|
|
957
|
+
return;
|
|
958
|
+
}
|
|
959
|
+
if (err.code === 'SCOPE_REQUIRED') {
|
|
960
|
+
res.status(400).json({
|
|
961
|
+
error: {
|
|
962
|
+
code: 'SCOPE_REQUIRED',
|
|
963
|
+
message: err.message,
|
|
964
|
+
trace_id: traceId,
|
|
965
|
+
},
|
|
966
|
+
});
|
|
967
|
+
return;
|
|
968
|
+
}
|
|
969
|
+
if (err.status === 400 ||
|
|
970
|
+
err.statusCode === 400 ||
|
|
971
|
+
err instanceof SyntaxError ||
|
|
972
|
+
err.type === 'entity.parse.failed') {
|
|
973
|
+
res.status(400).json({
|
|
974
|
+
error: {
|
|
975
|
+
code: 'BAD_REQUEST',
|
|
976
|
+
message: err.message,
|
|
977
|
+
trace_id: traceId,
|
|
978
|
+
},
|
|
979
|
+
});
|
|
980
|
+
return;
|
|
981
|
+
}
|
|
982
|
+
if (err.code === 'SCHEMA_INVALID' || err.code === 'PARENT_NOT_FOUND') {
|
|
983
|
+
res.status(400).json({
|
|
984
|
+
error: {
|
|
985
|
+
code: err.code,
|
|
986
|
+
message: err.message,
|
|
987
|
+
trace_id: traceId,
|
|
988
|
+
},
|
|
989
|
+
});
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
if (err.code === 'NOT_FOUND') {
|
|
993
|
+
res.status(404).json({
|
|
994
|
+
error: {
|
|
995
|
+
code: 'NOT_FOUND',
|
|
996
|
+
message: err.message,
|
|
997
|
+
trace_id: traceId,
|
|
998
|
+
},
|
|
999
|
+
});
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
res.status(500).json({
|
|
1003
|
+
error: {
|
|
1004
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
1005
|
+
message: err instanceof Error ? err.message : 'Internal Server Error',
|
|
1006
|
+
trace_id: traceId,
|
|
1007
|
+
},
|
|
1008
|
+
});
|
|
1009
|
+
});
|
|
1010
|
+
return app;
|
|
1011
|
+
}
|
|
1012
|
+
exports.app = createApp();
|
|
1013
|
+
//# sourceMappingURL=app.js.map
|