@timmeck/brain 1.9.0 → 2.0.0

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.
Files changed (214) hide show
  1. package/README.md +19 -0
  2. package/brain.log +1164 -0
  3. package/{src/cli/commands/dashboard.ts → dashboard.html} +688 -807
  4. package/dist/api/server.d.ts +4 -18
  5. package/dist/api/server.js +4 -173
  6. package/dist/api/server.js.map +1 -1
  7. package/dist/brain.d.ts +1 -0
  8. package/dist/brain.js +6 -1
  9. package/dist/brain.js.map +1 -1
  10. package/dist/cli/colors.d.ts +4 -25
  11. package/dist/cli/colors.js +3 -89
  12. package/dist/cli/colors.js.map +1 -1
  13. package/dist/cli/commands/peers.d.ts +2 -0
  14. package/dist/cli/commands/peers.js +38 -0
  15. package/dist/cli/commands/peers.js.map +1 -0
  16. package/dist/db/connection.d.ts +1 -2
  17. package/dist/db/connection.js +1 -18
  18. package/dist/db/connection.js.map +1 -1
  19. package/dist/index.js +3 -1
  20. package/dist/index.js.map +1 -1
  21. package/dist/ipc/__tests__/protocol.test.d.ts +1 -0
  22. package/dist/ipc/__tests__/protocol.test.js +117 -0
  23. package/dist/ipc/__tests__/protocol.test.js.map +1 -0
  24. package/dist/ipc/client.d.ts +1 -16
  25. package/dist/ipc/client.js +1 -100
  26. package/dist/ipc/client.js.map +1 -1
  27. package/dist/ipc/protocol.d.ts +1 -8
  28. package/dist/ipc/protocol.js +1 -28
  29. package/dist/ipc/protocol.js.map +1 -1
  30. package/dist/ipc/router.js +8 -0
  31. package/dist/ipc/router.js.map +1 -1
  32. package/dist/ipc/server.d.ts +1 -22
  33. package/dist/ipc/server.js +1 -163
  34. package/dist/ipc/server.js.map +1 -1
  35. package/dist/mcp/http-server.d.ts +1 -7
  36. package/dist/mcp/http-server.js +6 -117
  37. package/dist/mcp/http-server.js.map +1 -1
  38. package/dist/mcp/server.js +5 -61
  39. package/dist/mcp/server.js.map +1 -1
  40. package/dist/signals/__tests__/fingerprint.test.d.ts +1 -0
  41. package/dist/signals/__tests__/fingerprint.test.js +118 -0
  42. package/dist/signals/__tests__/fingerprint.test.js.map +1 -0
  43. package/dist/types/ipc.types.d.ts +1 -11
  44. package/dist/utils/__tests__/hash.test.d.ts +1 -0
  45. package/dist/utils/__tests__/hash.test.js +32 -0
  46. package/dist/utils/__tests__/hash.test.js.map +1 -0
  47. package/dist/utils/__tests__/paths.test.d.ts +1 -0
  48. package/dist/utils/__tests__/paths.test.js +75 -0
  49. package/dist/utils/__tests__/paths.test.js.map +1 -0
  50. package/dist/utils/events.d.ts +4 -8
  51. package/dist/utils/events.js +2 -14
  52. package/dist/utils/events.js.map +1 -1
  53. package/dist/utils/hash.d.ts +1 -1
  54. package/dist/utils/hash.js +1 -4
  55. package/dist/utils/hash.js.map +1 -1
  56. package/dist/utils/logger.d.ts +3 -2
  57. package/dist/utils/logger.js +8 -35
  58. package/dist/utils/logger.js.map +1 -1
  59. package/dist/utils/paths.d.ts +2 -1
  60. package/dist/utils/paths.js +4 -13
  61. package/dist/utils/paths.js.map +1 -1
  62. package/package.json +2 -1
  63. package/BRAIN_PLAN.md +0 -3324
  64. package/reddit_post.md +0 -45
  65. package/src/api/server.ts +0 -395
  66. package/src/brain.ts +0 -313
  67. package/src/cli/colors.ts +0 -116
  68. package/src/cli/commands/config.ts +0 -169
  69. package/src/cli/commands/doctor.ts +0 -124
  70. package/src/cli/commands/explain.ts +0 -83
  71. package/src/cli/commands/export.ts +0 -31
  72. package/src/cli/commands/import.ts +0 -199
  73. package/src/cli/commands/insights.ts +0 -65
  74. package/src/cli/commands/learn.ts +0 -24
  75. package/src/cli/commands/modules.ts +0 -53
  76. package/src/cli/commands/network.ts +0 -67
  77. package/src/cli/commands/projects.ts +0 -42
  78. package/src/cli/commands/query.ts +0 -120
  79. package/src/cli/commands/start.ts +0 -105
  80. package/src/cli/commands/status.ts +0 -75
  81. package/src/cli/commands/stop.ts +0 -34
  82. package/src/cli/ipc-helper.ts +0 -22
  83. package/src/cli/update-check.ts +0 -63
  84. package/src/code/analyzer.ts +0 -117
  85. package/src/code/fingerprint.ts +0 -87
  86. package/src/code/matcher.ts +0 -129
  87. package/src/code/parsers/generic.ts +0 -29
  88. package/src/code/parsers/python.ts +0 -54
  89. package/src/code/parsers/typescript.ts +0 -65
  90. package/src/code/registry.ts +0 -60
  91. package/src/code/scorer.ts +0 -120
  92. package/src/config.ts +0 -135
  93. package/src/dashboard/server.ts +0 -142
  94. package/src/db/connection.ts +0 -22
  95. package/src/db/migrations/001_core_schema.ts +0 -120
  96. package/src/db/migrations/002_learning_schema.ts +0 -38
  97. package/src/db/migrations/003_code_schema.ts +0 -53
  98. package/src/db/migrations/004_synapses_schema.ts +0 -57
  99. package/src/db/migrations/005_fts_indexes.ts +0 -78
  100. package/src/db/migrations/006_synapses_phase3.ts +0 -17
  101. package/src/db/migrations/007_feedback.ts +0 -13
  102. package/src/db/migrations/008_git_integration.ts +0 -38
  103. package/src/db/migrations/009_embeddings.ts +0 -8
  104. package/src/db/migrations/index.ts +0 -70
  105. package/src/db/repositories/antipattern.repository.ts +0 -66
  106. package/src/db/repositories/code-module.repository.ts +0 -142
  107. package/src/db/repositories/error.repository.ts +0 -189
  108. package/src/db/repositories/insight.repository.ts +0 -99
  109. package/src/db/repositories/notification.repository.ts +0 -66
  110. package/src/db/repositories/project.repository.ts +0 -93
  111. package/src/db/repositories/rule.repository.ts +0 -108
  112. package/src/db/repositories/solution.repository.ts +0 -154
  113. package/src/db/repositories/synapse.repository.ts +0 -163
  114. package/src/db/repositories/terminal.repository.ts +0 -101
  115. package/src/embeddings/engine.ts +0 -238
  116. package/src/hooks/post-tool-use.ts +0 -92
  117. package/src/hooks/post-write.ts +0 -129
  118. package/src/index.ts +0 -63
  119. package/src/ipc/client.ts +0 -118
  120. package/src/ipc/protocol.ts +0 -35
  121. package/src/ipc/router.ts +0 -133
  122. package/src/ipc/server.ts +0 -176
  123. package/src/learning/confidence-scorer.ts +0 -80
  124. package/src/learning/decay.ts +0 -46
  125. package/src/learning/learning-engine.ts +0 -170
  126. package/src/learning/pattern-extractor.ts +0 -90
  127. package/src/learning/rule-generator.ts +0 -74
  128. package/src/main.rs:10:5 +0 -0
  129. package/src/matching/error-matcher.ts +0 -166
  130. package/src/matching/fingerprint.ts +0 -34
  131. package/src/matching/similarity.ts +0 -61
  132. package/src/matching/tfidf.ts +0 -74
  133. package/src/matching/tokenizer.ts +0 -41
  134. package/src/mcp/auto-detect.ts +0 -93
  135. package/src/mcp/http-server.ts +0 -140
  136. package/src/mcp/server.ts +0 -73
  137. package/src/mcp/tools.ts +0 -328
  138. package/src/parsing/error-parser.ts +0 -28
  139. package/src/parsing/parsers/compiler.ts +0 -93
  140. package/src/parsing/parsers/generic.ts +0 -28
  141. package/src/parsing/parsers/go.ts +0 -97
  142. package/src/parsing/parsers/node.ts +0 -69
  143. package/src/parsing/parsers/python.ts +0 -62
  144. package/src/parsing/parsers/rust.ts +0 -50
  145. package/src/parsing/parsers/shell.ts +0 -42
  146. package/src/parsing/types.ts +0 -47
  147. package/src/research/gap-analyzer.ts +0 -135
  148. package/src/research/insight-generator.ts +0 -123
  149. package/src/research/research-engine.ts +0 -116
  150. package/src/research/synergy-detector.ts +0 -126
  151. package/src/research/template-extractor.ts +0 -130
  152. package/src/research/trend-analyzer.ts +0 -127
  153. package/src/services/analytics.service.ts +0 -226
  154. package/src/services/code.service.ts +0 -271
  155. package/src/services/error.service.ts +0 -266
  156. package/src/services/git.service.ts +0 -132
  157. package/src/services/notification.service.ts +0 -41
  158. package/src/services/prevention.service.ts +0 -159
  159. package/src/services/research.service.ts +0 -98
  160. package/src/services/solution.service.ts +0 -174
  161. package/src/services/synapse.service.ts +0 -59
  162. package/src/services/terminal.service.ts +0 -81
  163. package/src/synapses/activation.ts +0 -80
  164. package/src/synapses/decay.ts +0 -38
  165. package/src/synapses/hebbian.ts +0 -69
  166. package/src/synapses/pathfinder.ts +0 -81
  167. package/src/synapses/synapse-manager.ts +0 -113
  168. package/src/types/code.types.ts +0 -52
  169. package/src/types/config.types.ts +0 -103
  170. package/src/types/error.types.ts +0 -67
  171. package/src/types/ipc.types.ts +0 -8
  172. package/src/types/mcp.types.ts +0 -53
  173. package/src/types/research.types.ts +0 -28
  174. package/src/types/solution.types.ts +0 -30
  175. package/src/types/synapse.types.ts +0 -50
  176. package/src/utils/events.ts +0 -45
  177. package/src/utils/hash.ts +0 -5
  178. package/src/utils/logger.ts +0 -48
  179. package/src/utils/paths.ts +0 -19
  180. package/tests/e2e/test_code_intelligence.py +0 -1015
  181. package/tests/e2e/test_error_memory.py +0 -451
  182. package/tests/e2e/test_full_integration.py +0 -534
  183. package/tests/fixtures/code-modules/modules.ts +0 -83
  184. package/tests/fixtures/errors/go.ts +0 -9
  185. package/tests/fixtures/errors/node.ts +0 -24
  186. package/tests/fixtures/errors/python.ts +0 -21
  187. package/tests/fixtures/errors/rust.ts +0 -25
  188. package/tests/fixtures/errors/shell.ts +0 -15
  189. package/tests/fixtures/solutions/solutions.ts +0 -27
  190. package/tests/helpers/setup-db.ts +0 -52
  191. package/tests/integration/code-flow.test.ts +0 -86
  192. package/tests/integration/error-flow.test.ts +0 -83
  193. package/tests/integration/ipc-flow.test.ts +0 -166
  194. package/tests/integration/learning-cycle.test.ts +0 -82
  195. package/tests/integration/synapse-flow.test.ts +0 -117
  196. package/tests/unit/code/analyzer.test.ts +0 -58
  197. package/tests/unit/code/fingerprint.test.ts +0 -51
  198. package/tests/unit/code/scorer.test.ts +0 -55
  199. package/tests/unit/learning/confidence-scorer.test.ts +0 -60
  200. package/tests/unit/learning/decay.test.ts +0 -45
  201. package/tests/unit/learning/pattern-extractor.test.ts +0 -50
  202. package/tests/unit/matching/error-matcher.test.ts +0 -69
  203. package/tests/unit/matching/fingerprint.test.ts +0 -47
  204. package/tests/unit/matching/similarity.test.ts +0 -65
  205. package/tests/unit/matching/tfidf.test.ts +0 -71
  206. package/tests/unit/matching/tokenizer.test.ts +0 -83
  207. package/tests/unit/parsing/parsers.test.ts +0 -113
  208. package/tests/unit/research/gap-analyzer.test.ts +0 -45
  209. package/tests/unit/research/trend-analyzer.test.ts +0 -45
  210. package/tests/unit/synapses/activation.test.ts +0 -80
  211. package/tests/unit/synapses/decay.test.ts +0 -27
  212. package/tests/unit/synapses/hebbian.test.ts +0 -96
  213. package/tests/unit/synapses/pathfinder.test.ts +0 -72
  214. package/tsconfig.json +0 -18
@@ -1,108 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
-
4
- export interface RuleRecord {
5
- id: number;
6
- pattern: string;
7
- action: string;
8
- description: string | null;
9
- confidence: number;
10
- occurrences: number;
11
- active: number;
12
- project_id: number | null;
13
- created_at: string;
14
- updated_at: string;
15
- }
16
-
17
- type CreateRuleData = Omit<RuleRecord, 'id' | 'confidence' | 'occurrences' | 'active' | 'created_at' | 'updated_at'> & {
18
- confidence?: number;
19
- occurrences?: number;
20
- active?: number;
21
- };
22
- type UpdateRuleData = Partial<Omit<RuleRecord, 'id' | 'created_at'>>;
23
-
24
- export class RuleRepository {
25
- private stmts: Record<string, Statement>;
26
-
27
- constructor(private db: Database.Database) {
28
- this.stmts = {
29
- create: this.db.prepare(`
30
- INSERT INTO rules (pattern, action, description, confidence, occurrences, active, project_id)
31
- VALUES (@pattern, @action, @description, @confidence, @occurrences, @active, @project_id)
32
- `),
33
- getById: this.db.prepare(`
34
- SELECT * FROM rules WHERE id = ?
35
- `),
36
- update: this.db.prepare(`
37
- UPDATE rules
38
- SET pattern = COALESCE(@pattern, pattern),
39
- action = COALESCE(@action, action),
40
- description = COALESCE(@description, description),
41
- confidence = COALESCE(@confidence, confidence),
42
- occurrences = COALESCE(@occurrences, occurrences),
43
- active = COALESCE(@active, active),
44
- project_id = COALESCE(@project_id, project_id),
45
- updated_at = datetime('now')
46
- WHERE id = @id
47
- `),
48
- delete: this.db.prepare(`
49
- DELETE FROM rules WHERE id = ?
50
- `),
51
- findActiveAll: this.db.prepare(`
52
- SELECT * FROM rules WHERE active = 1 ORDER BY confidence DESC
53
- `),
54
- findActiveByProject: this.db.prepare(`
55
- SELECT * FROM rules WHERE active = 1 AND (project_id = ? OR project_id IS NULL) ORDER BY confidence DESC
56
- `),
57
- findByPattern: this.db.prepare(`
58
- SELECT * FROM rules WHERE pattern = ?
59
- `),
60
- };
61
- }
62
-
63
- create(data: CreateRuleData): number {
64
- const result = this.stmts.create.run({
65
- pattern: data.pattern,
66
- action: data.action,
67
- description: data.description ?? null,
68
- confidence: data.confidence ?? 0.5,
69
- occurrences: data.occurrences ?? 0,
70
- active: data.active ?? 1,
71
- project_id: data.project_id ?? null,
72
- });
73
- return result.lastInsertRowid as number;
74
- }
75
-
76
- getById(id: number): RuleRecord | undefined {
77
- return this.stmts.getById.get(id) as RuleRecord | undefined;
78
- }
79
-
80
- update(id: number, data: UpdateRuleData): void {
81
- this.stmts.update.run({
82
- id,
83
- pattern: data.pattern ?? null,
84
- action: data.action ?? null,
85
- description: data.description ?? null,
86
- confidence: data.confidence ?? null,
87
- occurrences: data.occurrences ?? null,
88
- active: data.active ?? null,
89
- project_id: data.project_id ?? null,
90
- updated_at: data.updated_at ?? null,
91
- });
92
- }
93
-
94
- delete(id: number): void {
95
- this.stmts.delete.run(id);
96
- }
97
-
98
- findActive(projectId?: number): RuleRecord[] {
99
- if (projectId !== undefined) {
100
- return this.stmts.findActiveByProject.all(projectId) as RuleRecord[];
101
- }
102
- return this.stmts.findActiveAll.all() as RuleRecord[];
103
- }
104
-
105
- findByPattern(pattern: string): RuleRecord[] {
106
- return this.stmts.findByPattern.all(pattern) as RuleRecord[];
107
- }
108
- }
@@ -1,154 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
- import type { SolutionRecord, ErrorSolution, SolutionAttempt } from '../../types/solution.types.js';
4
-
5
- type CreateSolutionData = Omit<SolutionRecord, 'id' | 'success_count' | 'fail_count' | 'created_at' | 'updated_at'>;
6
- type UpdateSolutionData = Partial<Omit<SolutionRecord, 'id' | 'created_at'>>;
7
-
8
- interface AttemptData {
9
- errorSolutionId: number;
10
- terminalId?: number | null;
11
- success: number;
12
- output?: string | null;
13
- durationMs?: number | null;
14
- }
15
-
16
- export class SolutionRepository {
17
- private stmts: Record<string, Statement>;
18
-
19
- constructor(private db: Database.Database) {
20
- this.stmts = {
21
- create: this.db.prepare(`
22
- INSERT INTO solutions (description, commands, code_change, source, confidence)
23
- VALUES (@description, @commands, @code_change, @source, @confidence)
24
- `),
25
- getById: this.db.prepare(`
26
- SELECT * FROM solutions WHERE id = ?
27
- `),
28
- update: this.db.prepare(`
29
- UPDATE solutions
30
- SET description = COALESCE(@description, description),
31
- commands = COALESCE(@commands, commands),
32
- code_change = COALESCE(@code_change, code_change),
33
- source = COALESCE(@source, source),
34
- confidence = COALESCE(@confidence, confidence),
35
- success_count = COALESCE(@success_count, success_count),
36
- fail_count = COALESCE(@fail_count, fail_count),
37
- updated_at = datetime('now')
38
- WHERE id = @id
39
- `),
40
- delete: this.db.prepare(`
41
- DELETE FROM solutions WHERE id = ?
42
- `),
43
- findForError: this.db.prepare(`
44
- SELECT s.* FROM solutions s
45
- JOIN error_solutions es ON s.id = es.solution_id
46
- WHERE es.error_id = ?
47
- ORDER BY s.confidence DESC
48
- `),
49
- linkToError: this.db.prepare(`
50
- INSERT OR IGNORE INTO error_solutions (error_id, solution_id)
51
- VALUES (@error_id, @solution_id)
52
- `),
53
- recordAttempt: this.db.prepare(`
54
- INSERT INTO solution_attempts (error_solution_id, terminal_id, success, output, duration_ms)
55
- VALUES (@error_solution_id, @terminal_id, @success, @output, @duration_ms)
56
- `),
57
- getAttempts: this.db.prepare(`
58
- SELECT * FROM solution_attempts WHERE error_solution_id = ? ORDER BY attempted_at DESC
59
- `),
60
- getAll: this.db.prepare(`
61
- SELECT * FROM solutions
62
- `),
63
- successRate: this.db.prepare(`
64
- SELECT
65
- COUNT(*) as total,
66
- SUM(CASE WHEN sa.success = 1 THEN 1 ELSE 0 END) as successes
67
- FROM solution_attempts sa
68
- JOIN error_solutions es ON sa.error_solution_id = es.id
69
- WHERE es.solution_id = ?
70
- `),
71
- updateSuccessCount: this.db.prepare(`
72
- UPDATE solutions
73
- SET success_count = success_count + 1,
74
- updated_at = datetime('now')
75
- WHERE id = ?
76
- `),
77
- updateFailCount: this.db.prepare(`
78
- UPDATE solutions
79
- SET fail_count = fail_count + 1,
80
- updated_at = datetime('now')
81
- WHERE id = ?
82
- `),
83
- };
84
- }
85
-
86
- create(data: CreateSolutionData): number {
87
- const result = this.stmts.create.run({
88
- description: data.description,
89
- commands: data.commands ?? null,
90
- code_change: data.code_change ?? null,
91
- source: data.source,
92
- confidence: data.confidence,
93
- });
94
- return result.lastInsertRowid as number;
95
- }
96
-
97
- getById(id: number): SolutionRecord | undefined {
98
- return this.stmts.getById.get(id) as SolutionRecord | undefined;
99
- }
100
-
101
- update(id: number, data: UpdateSolutionData): void {
102
- this.stmts.update.run({
103
- id,
104
- description: data.description ?? null,
105
- commands: data.commands ?? null,
106
- code_change: data.code_change ?? null,
107
- source: data.source ?? null,
108
- confidence: data.confidence ?? null,
109
- success_count: data.success_count ?? null,
110
- fail_count: data.fail_count ?? null,
111
- updated_at: data.updated_at ?? null,
112
- });
113
- }
114
-
115
- delete(id: number): void {
116
- this.stmts.delete.run(id);
117
- }
118
-
119
- findForError(errorId: number): SolutionRecord[] {
120
- return this.stmts.findForError.all(errorId) as SolutionRecord[];
121
- }
122
-
123
- linkToError(errorId: number, solutionId: number): void {
124
- this.stmts.linkToError.run({
125
- error_id: errorId,
126
- solution_id: solutionId,
127
- });
128
- }
129
-
130
- recordAttempt(data: AttemptData): number {
131
- const result = this.stmts.recordAttempt.run({
132
- error_solution_id: data.errorSolutionId,
133
- terminal_id: data.terminalId ?? null,
134
- success: data.success,
135
- output: data.output ?? null,
136
- duration_ms: data.durationMs ?? null,
137
- });
138
- return result.lastInsertRowid as number;
139
- }
140
-
141
- getAttempts(errorSolutionId: number): SolutionAttempt[] {
142
- return this.stmts.getAttempts.all(errorSolutionId) as SolutionAttempt[];
143
- }
144
-
145
- getAll(): SolutionRecord[] {
146
- return this.stmts.getAll.all() as SolutionRecord[];
147
- }
148
-
149
- successRate(solutionId: number): number {
150
- const row = this.stmts.successRate.get(solutionId) as { total: number; successes: number };
151
- if (row.total === 0) return 0;
152
- return row.successes / row.total;
153
- }
154
- }
@@ -1,163 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
- import type { SynapseRecord } from '../../types/synapse.types.js';
4
-
5
- type SynapseCreate = Omit<SynapseRecord, 'id' | 'activation_count' | 'last_activated_at' | 'created_at' | 'updated_at'>;
6
- type SynapseUpdate = Partial<Omit<SynapseRecord, 'id' | 'created_at'>>;
7
-
8
- export class SynapseRepository {
9
- private stmts: Record<string, Statement>;
10
-
11
- constructor(private db: Database.Database) {
12
- this.stmts = {
13
- create: db.prepare(`
14
- INSERT INTO synapses (source_type, source_id, target_type, target_id, synapse_type, weight, metadata)
15
- VALUES (@source_type, @source_id, @target_type, @target_id, @synapse_type, @weight, @metadata)
16
- `),
17
- getById: db.prepare('SELECT * FROM synapses WHERE id = ?'),
18
- delete: db.prepare('DELETE FROM synapses WHERE id = ?'),
19
- getOutgoing: db.prepare(
20
- 'SELECT * FROM synapses WHERE source_type = ? AND source_id = ? ORDER BY weight DESC'
21
- ),
22
- getIncoming: db.prepare(
23
- 'SELECT * FROM synapses WHERE target_type = ? AND target_id = ? ORDER BY weight DESC'
24
- ),
25
- findConnected: db.prepare(`
26
- SELECT * FROM synapses
27
- WHERE (source_type = ? AND source_id = ?) OR (target_type = ? AND target_id = ?)
28
- ORDER BY weight DESC
29
- `),
30
- findBySourceTarget: db.prepare(`
31
- SELECT * FROM synapses
32
- WHERE source_type = ? AND source_id = ? AND target_type = ? AND target_id = ? AND synapse_type = ?
33
- `),
34
- findInactiveSince: db.prepare(`
35
- SELECT * FROM synapses WHERE last_activated_at < ?
36
- `),
37
- countNodes: db.prepare(`
38
- SELECT COUNT(*) as count FROM (
39
- SELECT source_type, source_id FROM synapses
40
- UNION
41
- SELECT target_type, target_id FROM synapses
42
- )
43
- `),
44
- avgWeight: db.prepare('SELECT AVG(weight) as avg_weight FROM synapses'),
45
- countByType: db.prepare('SELECT synapse_type, COUNT(*) as count FROM synapses GROUP BY synapse_type'),
46
- totalCount: db.prepare('SELECT COUNT(*) as count FROM synapses'),
47
- topByWeight: db.prepare('SELECT * FROM synapses ORDER BY weight DESC LIMIT ?'),
48
- topDiverse: db.prepare(`
49
- SELECT * FROM (
50
- SELECT *, ROW_NUMBER() OVER (PARTITION BY synapse_type ORDER BY weight DESC) as rn
51
- FROM synapses
52
- ) WHERE rn <= ? ORDER BY weight DESC
53
- `),
54
- };
55
- }
56
-
57
- create(data: SynapseCreate): number {
58
- const result = this.stmts.create.run({
59
- source_type: data.source_type,
60
- source_id: data.source_id,
61
- target_type: data.target_type,
62
- target_id: data.target_id,
63
- synapse_type: data.synapse_type,
64
- weight: data.weight,
65
- metadata: data.metadata ?? null,
66
- });
67
- return result.lastInsertRowid as number;
68
- }
69
-
70
- getById(id: number): SynapseRecord | undefined {
71
- return this.stmts.getById.get(id) as SynapseRecord | undefined;
72
- }
73
-
74
- update(id: number, data: SynapseUpdate): boolean {
75
- const fields = Object.keys(data).filter(
76
- (key) => (data as Record<string, unknown>)[key] !== undefined
77
- );
78
- if (fields.length === 0) return false;
79
-
80
- const setClauses = fields.map((field) => `${field} = @${field}`).join(', ');
81
- const stmt = this.db.prepare(
82
- `UPDATE synapses SET ${setClauses}, updated_at = datetime('now') WHERE id = @id`
83
- );
84
- const result = stmt.run({ ...data, id });
85
- return result.changes > 0;
86
- }
87
-
88
- delete(id: number): boolean {
89
- const result = this.stmts.delete.run(id);
90
- return result.changes > 0;
91
- }
92
-
93
- getOutgoing(sourceType: string, sourceId: number): SynapseRecord[] {
94
- return this.stmts.getOutgoing.all(sourceType, sourceId) as SynapseRecord[];
95
- }
96
-
97
- getIncoming(targetType: string, targetId: number): SynapseRecord[] {
98
- return this.stmts.getIncoming.all(targetType, targetId) as SynapseRecord[];
99
- }
100
-
101
- findBySourceTarget(
102
- sourceType: string,
103
- sourceId: number,
104
- targetType: string,
105
- targetId: number,
106
- synapseType: string,
107
- ): SynapseRecord | undefined {
108
- return this.stmts.findBySourceTarget.get(
109
- sourceType, sourceId, targetType, targetId, synapseType
110
- ) as SynapseRecord | undefined;
111
- }
112
-
113
- findByWeight(minWeight: number, maxWeight?: number): SynapseRecord[] {
114
- if (maxWeight !== undefined) {
115
- const stmt = this.db.prepare(
116
- 'SELECT * FROM synapses WHERE weight >= ? AND weight <= ? ORDER BY weight DESC'
117
- );
118
- return stmt.all(minWeight, maxWeight) as SynapseRecord[];
119
- }
120
- const stmt = this.db.prepare('SELECT * FROM synapses WHERE weight >= ? ORDER BY weight DESC');
121
- return stmt.all(minWeight) as SynapseRecord[];
122
- }
123
-
124
- findConnected(nodeType: string, nodeId: number): SynapseRecord[] {
125
- return this.stmts.findConnected.all(nodeType, nodeId, nodeType, nodeId) as SynapseRecord[];
126
- }
127
-
128
- findInactiveSince(cutoff: string): SynapseRecord[] {
129
- return this.stmts.findInactiveSince.all(cutoff) as SynapseRecord[];
130
- }
131
-
132
- topByWeight(limit: number): SynapseRecord[] {
133
- return this.stmts.topByWeight.all(limit) as SynapseRecord[];
134
- }
135
-
136
- topDiverse(perType: number): SynapseRecord[] {
137
- return this.stmts.topDiverse.all(perType) as SynapseRecord[];
138
- }
139
-
140
- countNodes(): number {
141
- const row = this.stmts.countNodes.get() as { count: number };
142
- return row.count;
143
- }
144
-
145
- totalCount(): number {
146
- const row = this.stmts.totalCount.get() as { count: number };
147
- return row.count;
148
- }
149
-
150
- avgWeight(): number {
151
- const row = this.stmts.avgWeight.get() as { avg_weight: number | null };
152
- return row.avg_weight ?? 0;
153
- }
154
-
155
- countByType(): Record<string, number> {
156
- const rows = this.stmts.countByType.all() as Array<{ synapse_type: string; count: number }>;
157
- const result: Record<string, number> = {};
158
- for (const row of rows) {
159
- result[row.synapse_type] = row.count;
160
- }
161
- return result;
162
- }
163
- }
@@ -1,101 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
-
4
- export interface TerminalRecord {
5
- id: number;
6
- uuid: string;
7
- project_id: number | null;
8
- pid: number | null;
9
- shell: string | null;
10
- cwd: string | null;
11
- connected_at: string;
12
- last_seen: string;
13
- disconnected_at: string | null;
14
- }
15
-
16
- type CreateTerminalData = Omit<TerminalRecord, 'id' | 'connected_at' | 'last_seen' | 'disconnected_at'>;
17
- type UpdateTerminalData = Partial<Omit<TerminalRecord, 'id' | 'connected_at'>>;
18
-
19
- export class TerminalRepository {
20
- private stmts: Record<string, Statement>;
21
-
22
- constructor(private db: Database.Database) {
23
- this.stmts = {
24
- create: this.db.prepare(`
25
- INSERT INTO terminals (uuid, project_id, pid, shell, cwd)
26
- VALUES (@uuid, @project_id, @pid, @shell, @cwd)
27
- `),
28
- getById: this.db.prepare(`
29
- SELECT * FROM terminals WHERE id = ?
30
- `),
31
- update: this.db.prepare(`
32
- UPDATE terminals
33
- SET project_id = COALESCE(@project_id, project_id),
34
- pid = COALESCE(@pid, pid),
35
- shell = COALESCE(@shell, shell),
36
- cwd = COALESCE(@cwd, cwd),
37
- last_seen = COALESCE(@last_seen, last_seen),
38
- disconnected_at = COALESCE(@disconnected_at, disconnected_at)
39
- WHERE id = @id
40
- `),
41
- delete: this.db.prepare(`
42
- DELETE FROM terminals WHERE id = ?
43
- `),
44
- findByUuid: this.db.prepare(`
45
- SELECT * FROM terminals WHERE uuid = ?
46
- `),
47
- findConnected: this.db.prepare(`
48
- SELECT * FROM terminals WHERE disconnected_at IS NULL ORDER BY connected_at DESC
49
- `),
50
- cleanupStale: this.db.prepare(`
51
- UPDATE terminals
52
- SET disconnected_at = datetime('now')
53
- WHERE disconnected_at IS NULL AND last_seen < ?
54
- `),
55
- };
56
- }
57
-
58
- create(data: CreateTerminalData): number {
59
- const result = this.stmts.create.run({
60
- uuid: data.uuid,
61
- project_id: data.project_id ?? null,
62
- pid: data.pid ?? null,
63
- shell: data.shell ?? null,
64
- cwd: data.cwd ?? null,
65
- });
66
- return result.lastInsertRowid as number;
67
- }
68
-
69
- getById(id: number): TerminalRecord | undefined {
70
- return this.stmts.getById.get(id) as TerminalRecord | undefined;
71
- }
72
-
73
- update(id: number, data: UpdateTerminalData): void {
74
- this.stmts.update.run({
75
- id,
76
- project_id: data.project_id ?? null,
77
- pid: data.pid ?? null,
78
- shell: data.shell ?? null,
79
- cwd: data.cwd ?? null,
80
- last_seen: data.last_seen ?? null,
81
- disconnected_at: data.disconnected_at ?? null,
82
- });
83
- }
84
-
85
- delete(id: number): void {
86
- this.stmts.delete.run(id);
87
- }
88
-
89
- findByUuid(uuid: string): TerminalRecord | undefined {
90
- return this.stmts.findByUuid.get(uuid) as TerminalRecord | undefined;
91
- }
92
-
93
- findConnected(): TerminalRecord[] {
94
- return this.stmts.findConnected.all() as TerminalRecord[];
95
- }
96
-
97
- cleanupStale(olderThan: string): number {
98
- const result = this.stmts.cleanupStale.run(olderThan);
99
- return result.changes;
100
- }
101
- }