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