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
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
import { randomUUID } from 'crypto';
|
|
2
|
+
import { getDb } from '../lib/db';
|
|
3
|
+
import { resolveScope, ScopeValue } from '../lib/scope';
|
|
4
|
+
import type { ObservationRecord } from './observationStore';
|
|
5
|
+
import type { NoteRecord } from './noteStore';
|
|
6
|
+
import type { ActionRecord } from './actionStore';
|
|
7
|
+
|
|
8
|
+
export type SessionStatus = 'active' | 'paused' | 'completed' | 'abandoned';
|
|
9
|
+
|
|
10
|
+
const VALID_SESSION_STATUSES = new Set<SessionStatus>([
|
|
11
|
+
'active',
|
|
12
|
+
'paused',
|
|
13
|
+
'completed',
|
|
14
|
+
'abandoned',
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
export interface SessionRecord {
|
|
18
|
+
id: string;
|
|
19
|
+
scope: ScopeValue;
|
|
20
|
+
project_id: string | null;
|
|
21
|
+
agent_name: string | null;
|
|
22
|
+
cwd: string | null;
|
|
23
|
+
status: SessionStatus;
|
|
24
|
+
started_at: string;
|
|
25
|
+
ended_at: string | null;
|
|
26
|
+
summary: string | null;
|
|
27
|
+
files_touched_json: string | null;
|
|
28
|
+
handoff_summary_id: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface StartSessionInput {
|
|
32
|
+
id?: string;
|
|
33
|
+
scope?: ScopeValue;
|
|
34
|
+
project_id?: string | null;
|
|
35
|
+
projectId?: string | null;
|
|
36
|
+
agent_name?: string | null;
|
|
37
|
+
agentName?: string | null;
|
|
38
|
+
cwd?: string | null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface EndSessionInput {
|
|
42
|
+
status?: SessionStatus;
|
|
43
|
+
summary?: string | null;
|
|
44
|
+
files_touched?: string[];
|
|
45
|
+
filesTouched?: string[];
|
|
46
|
+
handoff_summary_id?: string | null;
|
|
47
|
+
handoffSummaryId?: string | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SessionDetails {
|
|
51
|
+
session: SessionRecord;
|
|
52
|
+
observations: ObservationRecord[];
|
|
53
|
+
notes: NoteRecord[];
|
|
54
|
+
actions: ActionRecord[];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface SessionReplayItem {
|
|
58
|
+
kind: 'observation' | 'note' | 'action';
|
|
59
|
+
timestamp: string;
|
|
60
|
+
id: string;
|
|
61
|
+
title: string;
|
|
62
|
+
detail: string | null;
|
|
63
|
+
meta?: Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface SessionReplay {
|
|
67
|
+
session: SessionRecord;
|
|
68
|
+
timeline: SessionReplayItem[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function makeSchemaError(message: string): Error {
|
|
72
|
+
const err = new Error(message);
|
|
73
|
+
(err as any).code = 'SCHEMA_INVALID';
|
|
74
|
+
return err;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function optionalString(value: unknown, field: string): string | null {
|
|
78
|
+
if (value === undefined || value === null) return null;
|
|
79
|
+
if (typeof value !== 'string') {
|
|
80
|
+
throw makeSchemaError(`${field} must be a string`);
|
|
81
|
+
}
|
|
82
|
+
return value.trim() || null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Start a new session or reactivate an existing session with the given id.
|
|
87
|
+
*/
|
|
88
|
+
export function startSession(input: StartSessionInput = {}): SessionRecord {
|
|
89
|
+
const rawProject = input.projectId !== undefined ? input.projectId : input.project_id;
|
|
90
|
+
const scoped = resolveScope(input.scope, rawProject);
|
|
91
|
+
if (!scoped.ok) {
|
|
92
|
+
throw makeSchemaError(scoped.error);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const db = getDb();
|
|
96
|
+
const customId = optionalString(input.id, 'id');
|
|
97
|
+
const id = customId || `sess_${randomUUID().replace(/-/g, '').slice(0, 16)}`;
|
|
98
|
+
const rawAgent = input.agentName !== undefined ? input.agentName : input.agent_name;
|
|
99
|
+
const agentName = optionalString(rawAgent, 'agent_name');
|
|
100
|
+
const cwd = optionalString(input.cwd, 'cwd');
|
|
101
|
+
|
|
102
|
+
const existing = db.prepare('SELECT * FROM sessions WHERE id = ?').get(id) as
|
|
103
|
+
| SessionRecord
|
|
104
|
+
| undefined;
|
|
105
|
+
|
|
106
|
+
if (existing) {
|
|
107
|
+
db.prepare(
|
|
108
|
+
`
|
|
109
|
+
UPDATE sessions
|
|
110
|
+
SET status = 'active',
|
|
111
|
+
ended_at = NULL,
|
|
112
|
+
agent_name = COALESCE(?, agent_name),
|
|
113
|
+
cwd = COALESCE(?, cwd)
|
|
114
|
+
WHERE id = ?
|
|
115
|
+
`
|
|
116
|
+
).run(agentName, cwd, id);
|
|
117
|
+
|
|
118
|
+
return getSession(id)!;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
db.prepare(
|
|
122
|
+
`
|
|
123
|
+
INSERT INTO sessions (id, scope, project_id, agent_name, cwd, status)
|
|
124
|
+
VALUES (?, ?, ?, ?, ?, 'active')
|
|
125
|
+
`
|
|
126
|
+
).run(id, scoped.scope, scoped.projectId, agentName, cwd);
|
|
127
|
+
|
|
128
|
+
return getSession(id)!;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Build a deterministic arithmetic summary from observations for a session.
|
|
133
|
+
* Never uses an LLM (Rule 1).
|
|
134
|
+
*/
|
|
135
|
+
function buildArithmeticSessionSummary(
|
|
136
|
+
db: ReturnType<typeof getDb>,
|
|
137
|
+
sessionId: string
|
|
138
|
+
): string {
|
|
139
|
+
const stats = db
|
|
140
|
+
.prepare(
|
|
141
|
+
`
|
|
142
|
+
SELECT
|
|
143
|
+
COUNT(*) AS total,
|
|
144
|
+
COALESCE(SUM(CASE WHEN failed = 1 THEN 1 ELSE 0 END), 0) AS errors
|
|
145
|
+
FROM observations
|
|
146
|
+
WHERE session_id = ?
|
|
147
|
+
`
|
|
148
|
+
)
|
|
149
|
+
.get(sessionId) as { total: number; errors: number };
|
|
150
|
+
|
|
151
|
+
if (stats.total === 0) {
|
|
152
|
+
return '0 observations recorded.';
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const toolRows = db
|
|
156
|
+
.prepare(
|
|
157
|
+
`
|
|
158
|
+
SELECT DISTINCT tool_name
|
|
159
|
+
FROM observations
|
|
160
|
+
WHERE session_id = ? AND tool_name IS NOT NULL AND tool_name != ''
|
|
161
|
+
ORDER BY tool_name ASC
|
|
162
|
+
`
|
|
163
|
+
)
|
|
164
|
+
.all(sessionId) as Array<{ tool_name: string }>;
|
|
165
|
+
|
|
166
|
+
const fileRows = db
|
|
167
|
+
.prepare(
|
|
168
|
+
`
|
|
169
|
+
SELECT DISTINCT file_path
|
|
170
|
+
FROM observations
|
|
171
|
+
WHERE session_id = ? AND file_path IS NOT NULL AND file_path != ''
|
|
172
|
+
ORDER BY file_path ASC
|
|
173
|
+
`
|
|
174
|
+
)
|
|
175
|
+
.all(sessionId) as Array<{ file_path: string }>;
|
|
176
|
+
|
|
177
|
+
const parts = [`${stats.total} observations (${stats.errors} errors)`];
|
|
178
|
+
if (toolRows.length > 0) {
|
|
179
|
+
parts.push(`Tools: ${toolRows.map((r) => r.tool_name).join(', ')}`);
|
|
180
|
+
}
|
|
181
|
+
if (fileRows.length > 0) {
|
|
182
|
+
parts.push(`Files: ${fileRows.map((r) => r.file_path).join(', ')}`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return `${parts.join('. ')}.`;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* End a session, setting ended_at, status, files_touched_json, and an
|
|
190
|
+
* arithmetic summary if none was provided.
|
|
191
|
+
*/
|
|
192
|
+
export function endSession(
|
|
193
|
+
id: string,
|
|
194
|
+
input: EndSessionInput = {}
|
|
195
|
+
): SessionRecord | undefined {
|
|
196
|
+
const db = getDb();
|
|
197
|
+
const existing = getSession(id);
|
|
198
|
+
if (!existing) return undefined;
|
|
199
|
+
|
|
200
|
+
const status: SessionStatus = input.status ?? 'completed';
|
|
201
|
+
if (!VALID_SESSION_STATUSES.has(status)) {
|
|
202
|
+
throw makeSchemaError(
|
|
203
|
+
`Invalid session status '${String(status)}'. Must be 'active', 'paused', 'completed', or 'abandoned'`
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const filesTouched = input.filesTouched ?? input.files_touched;
|
|
208
|
+
const filesTouchedJson = Array.isArray(filesTouched)
|
|
209
|
+
? JSON.stringify(filesTouched)
|
|
210
|
+
: existing.files_touched_json;
|
|
211
|
+
|
|
212
|
+
const providedSummary = optionalString(input.summary, 'summary');
|
|
213
|
+
const summary = providedSummary ?? buildArithmeticSessionSummary(db, id);
|
|
214
|
+
|
|
215
|
+
const rawHandoff =
|
|
216
|
+
input.handoffSummaryId !== undefined
|
|
217
|
+
? input.handoffSummaryId
|
|
218
|
+
: input.handoff_summary_id;
|
|
219
|
+
const handoffSummaryId =
|
|
220
|
+
optionalString(rawHandoff, 'handoff_summary_id') ?? existing.handoff_summary_id;
|
|
221
|
+
|
|
222
|
+
db.prepare(
|
|
223
|
+
`
|
|
224
|
+
UPDATE sessions
|
|
225
|
+
SET status = ?,
|
|
226
|
+
ended_at = datetime('now'),
|
|
227
|
+
summary = ?,
|
|
228
|
+
files_touched_json = ?,
|
|
229
|
+
handoff_summary_id = ?
|
|
230
|
+
WHERE id = ?
|
|
231
|
+
`
|
|
232
|
+
).run(status, summary, filesTouchedJson, handoffSummaryId, id);
|
|
233
|
+
|
|
234
|
+
return getSession(id)!;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function getSession(id: string): SessionRecord | undefined {
|
|
238
|
+
const db = getDb();
|
|
239
|
+
return db.prepare('SELECT * FROM sessions WHERE id = ?').get(id) as
|
|
240
|
+
| SessionRecord
|
|
241
|
+
| undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export function listSessions(filters?: {
|
|
245
|
+
projectId?: string;
|
|
246
|
+
status?: string;
|
|
247
|
+
agentName?: string;
|
|
248
|
+
limit?: number;
|
|
249
|
+
}): SessionRecord[] {
|
|
250
|
+
const db = getDb();
|
|
251
|
+
const conditions: string[] = [];
|
|
252
|
+
const params: unknown[] = [];
|
|
253
|
+
|
|
254
|
+
if (typeof filters?.projectId === 'string' && filters.projectId.trim()) {
|
|
255
|
+
conditions.push('project_id = ?');
|
|
256
|
+
params.push(filters.projectId.trim());
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (typeof filters?.status === 'string' && filters.status.trim()) {
|
|
260
|
+
conditions.push('status = ?');
|
|
261
|
+
params.push(filters.status.trim());
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (typeof filters?.agentName === 'string' && filters.agentName.trim()) {
|
|
265
|
+
conditions.push('agent_name = ?');
|
|
266
|
+
params.push(filters.agentName.trim());
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
270
|
+
const limit = filters?.limit ?? 50;
|
|
271
|
+
params.push(limit);
|
|
272
|
+
|
|
273
|
+
return db
|
|
274
|
+
.prepare(
|
|
275
|
+
`
|
|
276
|
+
SELECT * FROM sessions
|
|
277
|
+
${whereClause}
|
|
278
|
+
ORDER BY started_at DESC, rowid DESC
|
|
279
|
+
LIMIT ?
|
|
280
|
+
`
|
|
281
|
+
)
|
|
282
|
+
.all(...params) as SessionRecord[];
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export function getSessionDetails(id: string): SessionDetails | undefined {
|
|
286
|
+
const session = getSession(id);
|
|
287
|
+
if (!session) return undefined;
|
|
288
|
+
|
|
289
|
+
const db = getDb();
|
|
290
|
+
const observations = db
|
|
291
|
+
.prepare('SELECT * FROM observations WHERE session_id = ? ORDER BY created_at ASC')
|
|
292
|
+
.all(id) as ObservationRecord[];
|
|
293
|
+
|
|
294
|
+
const notes = db
|
|
295
|
+
.prepare('SELECT * FROM notes WHERE session_id = ? ORDER BY created_at ASC')
|
|
296
|
+
.all(id) as NoteRecord[];
|
|
297
|
+
|
|
298
|
+
const actions = db
|
|
299
|
+
.prepare('SELECT * FROM actions WHERE session_id = ? ORDER BY created_at ASC')
|
|
300
|
+
.all(id) as ActionRecord[];
|
|
301
|
+
|
|
302
|
+
return { session, observations, notes, actions };
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function toIsoTimestamp(ts: number | string): string {
|
|
306
|
+
if (typeof ts === 'number') {
|
|
307
|
+
return new Date(ts * 1000).toISOString();
|
|
308
|
+
}
|
|
309
|
+
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(ts)) {
|
|
310
|
+
return new Date(`${ts.replace(' ', 'T')}Z`).toISOString();
|
|
311
|
+
}
|
|
312
|
+
const parsed = new Date(ts);
|
|
313
|
+
return Number.isNaN(parsed.getTime()) ? ts : parsed.toISOString();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Build a chronological replay timeline of observations, notes, and actions
|
|
318
|
+
* associated with a session.
|
|
319
|
+
*/
|
|
320
|
+
export function getSessionReplay(id: string): SessionReplay | undefined {
|
|
321
|
+
const details = getSessionDetails(id);
|
|
322
|
+
if (!details) return undefined;
|
|
323
|
+
|
|
324
|
+
const timeline: SessionReplayItem[] = [
|
|
325
|
+
...details.observations.map((obs): SessionReplayItem => ({
|
|
326
|
+
kind: 'observation',
|
|
327
|
+
timestamp: toIsoTimestamp(obs.created_at),
|
|
328
|
+
id: obs.id,
|
|
329
|
+
title: obs.tool_name ? `${obs.kind}: ${obs.tool_name}` : obs.kind,
|
|
330
|
+
detail: obs.output ?? obs.input ?? null,
|
|
331
|
+
meta: {
|
|
332
|
+
obs_kind: obs.kind,
|
|
333
|
+
tool_name: obs.tool_name,
|
|
334
|
+
file_path: obs.file_path,
|
|
335
|
+
duration_ms: obs.duration_ms,
|
|
336
|
+
failed: obs.failed === 1,
|
|
337
|
+
},
|
|
338
|
+
})),
|
|
339
|
+
...details.notes.map((note): SessionReplayItem => ({
|
|
340
|
+
kind: 'note',
|
|
341
|
+
timestamp: toIsoTimestamp(note.created_at),
|
|
342
|
+
id: note.id,
|
|
343
|
+
title: note.title,
|
|
344
|
+
detail: note.content,
|
|
345
|
+
meta: {
|
|
346
|
+
type: note.type,
|
|
347
|
+
scope: note.scope,
|
|
348
|
+
version: note.version,
|
|
349
|
+
},
|
|
350
|
+
})),
|
|
351
|
+
...details.actions.map((action): SessionReplayItem => ({
|
|
352
|
+
kind: 'action',
|
|
353
|
+
timestamp: toIsoTimestamp(action.created_at),
|
|
354
|
+
id: action.id,
|
|
355
|
+
title: action.title,
|
|
356
|
+
detail: action.description,
|
|
357
|
+
meta: {
|
|
358
|
+
status: action.status,
|
|
359
|
+
priority: action.priority,
|
|
360
|
+
blocked_reason: action.blocked_reason,
|
|
361
|
+
},
|
|
362
|
+
})),
|
|
363
|
+
];
|
|
364
|
+
|
|
365
|
+
timeline.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
|
|
366
|
+
|
|
367
|
+
return {
|
|
368
|
+
session: details.session,
|
|
369
|
+
timeline,
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Return the most recent ended session with a non-empty summary for a project
|
|
375
|
+
* (or global if no projectId is given), or null if none exists.
|
|
376
|
+
*/
|
|
377
|
+
export function getLatestSessionSummary(projectId?: string): SessionRecord | null {
|
|
378
|
+
const db = getDb();
|
|
379
|
+
const cleanProject = typeof projectId === 'string' ? projectId.trim() || null : null;
|
|
380
|
+
|
|
381
|
+
if (cleanProject) {
|
|
382
|
+
const row = db
|
|
383
|
+
.prepare(
|
|
384
|
+
`
|
|
385
|
+
SELECT * FROM sessions
|
|
386
|
+
WHERE project_id = ? AND scope = 'project'
|
|
387
|
+
AND ended_at IS NOT NULL
|
|
388
|
+
AND summary IS NOT NULL AND summary != ''
|
|
389
|
+
ORDER BY ended_at DESC, rowid DESC
|
|
390
|
+
LIMIT 1
|
|
391
|
+
`
|
|
392
|
+
)
|
|
393
|
+
.get(cleanProject) as SessionRecord | undefined;
|
|
394
|
+
return row ?? null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const row = db
|
|
398
|
+
.prepare(
|
|
399
|
+
`
|
|
400
|
+
SELECT * FROM sessions
|
|
401
|
+
WHERE scope = 'global'
|
|
402
|
+
AND ended_at IS NOT NULL
|
|
403
|
+
AND summary IS NOT NULL AND summary != ''
|
|
404
|
+
ORDER BY ended_at DESC, rowid DESC
|
|
405
|
+
LIMIT 1
|
|
406
|
+
`
|
|
407
|
+
)
|
|
408
|
+
.get() as SessionRecord | undefined;
|
|
409
|
+
return row ?? null;
|
|
410
|
+
}
|