@timmeck/brain 1.9.0 → 2.1.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 (253) hide show
  1. package/README.md +33 -0
  2. package/brain.log +3876 -0
  3. package/{src/cli/commands/dashboard.ts → dashboard.html} +694 -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 +2 -0
  8. package/dist/brain.js +15 -4
  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/dashboard.js +21 -2
  14. package/dist/cli/commands/dashboard.js.map +1 -1
  15. package/dist/cli/commands/peers.d.ts +2 -0
  16. package/dist/cli/commands/peers.js +38 -0
  17. package/dist/cli/commands/peers.js.map +1 -0
  18. package/dist/cli/commands/status.js +0 -1
  19. package/dist/cli/commands/status.js.map +1 -1
  20. package/dist/config.js +2 -29
  21. package/dist/config.js.map +1 -1
  22. package/dist/db/connection.d.ts +1 -2
  23. package/dist/db/connection.js +1 -18
  24. package/dist/db/connection.js.map +1 -1
  25. package/dist/index.js +3 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/ipc/__tests__/protocol.test.d.ts +1 -0
  28. package/dist/ipc/__tests__/protocol.test.js +117 -0
  29. package/dist/ipc/__tests__/protocol.test.js.map +1 -0
  30. package/dist/ipc/client.d.ts +1 -16
  31. package/dist/ipc/client.js +1 -100
  32. package/dist/ipc/client.js.map +1 -1
  33. package/dist/ipc/protocol.d.ts +1 -8
  34. package/dist/ipc/protocol.js +1 -28
  35. package/dist/ipc/protocol.js.map +1 -1
  36. package/dist/ipc/router.d.ts +2 -0
  37. package/dist/ipc/router.js +30 -0
  38. package/dist/ipc/router.js.map +1 -1
  39. package/dist/ipc/server.d.ts +1 -22
  40. package/dist/ipc/server.js +1 -163
  41. package/dist/ipc/server.js.map +1 -1
  42. package/dist/learning/confidence-scorer.d.ts +2 -5
  43. package/dist/learning/confidence-scorer.js +4 -19
  44. package/dist/learning/confidence-scorer.js.map +1 -1
  45. package/dist/learning/decay.js +2 -3
  46. package/dist/learning/decay.js.map +1 -1
  47. package/dist/learning/learning-engine.d.ts +2 -5
  48. package/dist/learning/learning-engine.js +3 -15
  49. package/dist/learning/learning-engine.js.map +1 -1
  50. package/dist/mcp/http-server.d.ts +1 -7
  51. package/dist/mcp/http-server.js +6 -117
  52. package/dist/mcp/http-server.js.map +1 -1
  53. package/dist/mcp/server.js +5 -61
  54. package/dist/mcp/server.js.map +1 -1
  55. package/dist/mcp/tools.js +36 -0
  56. package/dist/mcp/tools.js.map +1 -1
  57. package/dist/parsing/parsers/compiler.js +1 -1
  58. package/dist/parsing/parsers/compiler.js.map +1 -1
  59. package/dist/research/research-engine.d.ts +2 -6
  60. package/dist/research/research-engine.js +3 -23
  61. package/dist/research/research-engine.js.map +1 -1
  62. package/dist/services/synapse.service.d.ts +3 -3
  63. package/dist/signals/__tests__/fingerprint.test.d.ts +1 -0
  64. package/dist/signals/__tests__/fingerprint.test.js +118 -0
  65. package/dist/signals/__tests__/fingerprint.test.js.map +1 -0
  66. package/dist/synapses/activation.d.ts +3 -13
  67. package/dist/synapses/activation.js +2 -49
  68. package/dist/synapses/activation.js.map +1 -1
  69. package/dist/synapses/decay.d.ts +2 -11
  70. package/dist/synapses/decay.js +2 -26
  71. package/dist/synapses/decay.js.map +1 -1
  72. package/dist/synapses/hebbian.d.ts +2 -13
  73. package/dist/synapses/hebbian.js +2 -35
  74. package/dist/synapses/hebbian.js.map +1 -1
  75. package/dist/synapses/pathfinder.d.ts +2 -14
  76. package/dist/synapses/pathfinder.js +2 -49
  77. package/dist/synapses/pathfinder.js.map +1 -1
  78. package/dist/synapses/synapse-manager.d.ts +7 -23
  79. package/dist/synapses/synapse-manager.js +6 -63
  80. package/dist/synapses/synapse-manager.js.map +1 -1
  81. package/dist/types/ipc.types.d.ts +1 -11
  82. package/dist/utils/__tests__/hash.test.d.ts +1 -0
  83. package/dist/utils/__tests__/hash.test.js +32 -0
  84. package/dist/utils/__tests__/hash.test.js.map +1 -0
  85. package/dist/utils/__tests__/paths.test.d.ts +1 -0
  86. package/dist/utils/__tests__/paths.test.js +75 -0
  87. package/dist/utils/__tests__/paths.test.js.map +1 -0
  88. package/dist/utils/events.d.ts +4 -8
  89. package/dist/utils/events.js +2 -14
  90. package/dist/utils/events.js.map +1 -1
  91. package/dist/utils/hash.d.ts +1 -1
  92. package/dist/utils/hash.js +1 -4
  93. package/dist/utils/hash.js.map +1 -1
  94. package/dist/utils/logger.d.ts +3 -2
  95. package/dist/utils/logger.js +8 -35
  96. package/dist/utils/logger.js.map +1 -1
  97. package/dist/utils/paths.d.ts +2 -1
  98. package/dist/utils/paths.js +4 -13
  99. package/dist/utils/paths.js.map +1 -1
  100. package/eslint.config.js +14 -0
  101. package/package.json +56 -49
  102. package/BRAIN_PLAN.md +0 -3324
  103. package/reddit_post.md +0 -45
  104. package/src/api/server.ts +0 -395
  105. package/src/brain.ts +0 -313
  106. package/src/cli/colors.ts +0 -116
  107. package/src/cli/commands/config.ts +0 -169
  108. package/src/cli/commands/doctor.ts +0 -124
  109. package/src/cli/commands/explain.ts +0 -83
  110. package/src/cli/commands/export.ts +0 -31
  111. package/src/cli/commands/import.ts +0 -199
  112. package/src/cli/commands/insights.ts +0 -65
  113. package/src/cli/commands/learn.ts +0 -24
  114. package/src/cli/commands/modules.ts +0 -53
  115. package/src/cli/commands/network.ts +0 -67
  116. package/src/cli/commands/projects.ts +0 -42
  117. package/src/cli/commands/query.ts +0 -120
  118. package/src/cli/commands/start.ts +0 -105
  119. package/src/cli/commands/status.ts +0 -75
  120. package/src/cli/commands/stop.ts +0 -34
  121. package/src/cli/ipc-helper.ts +0 -22
  122. package/src/cli/update-check.ts +0 -63
  123. package/src/code/analyzer.ts +0 -117
  124. package/src/code/fingerprint.ts +0 -87
  125. package/src/code/matcher.ts +0 -129
  126. package/src/code/parsers/generic.ts +0 -29
  127. package/src/code/parsers/python.ts +0 -54
  128. package/src/code/parsers/typescript.ts +0 -65
  129. package/src/code/registry.ts +0 -60
  130. package/src/code/scorer.ts +0 -120
  131. package/src/config.ts +0 -135
  132. package/src/dashboard/server.ts +0 -142
  133. package/src/db/connection.ts +0 -22
  134. package/src/db/migrations/001_core_schema.ts +0 -120
  135. package/src/db/migrations/002_learning_schema.ts +0 -38
  136. package/src/db/migrations/003_code_schema.ts +0 -53
  137. package/src/db/migrations/004_synapses_schema.ts +0 -57
  138. package/src/db/migrations/005_fts_indexes.ts +0 -78
  139. package/src/db/migrations/006_synapses_phase3.ts +0 -17
  140. package/src/db/migrations/007_feedback.ts +0 -13
  141. package/src/db/migrations/008_git_integration.ts +0 -38
  142. package/src/db/migrations/009_embeddings.ts +0 -8
  143. package/src/db/migrations/index.ts +0 -70
  144. package/src/db/repositories/antipattern.repository.ts +0 -66
  145. package/src/db/repositories/code-module.repository.ts +0 -142
  146. package/src/db/repositories/error.repository.ts +0 -189
  147. package/src/db/repositories/insight.repository.ts +0 -99
  148. package/src/db/repositories/notification.repository.ts +0 -66
  149. package/src/db/repositories/project.repository.ts +0 -93
  150. package/src/db/repositories/rule.repository.ts +0 -108
  151. package/src/db/repositories/solution.repository.ts +0 -154
  152. package/src/db/repositories/synapse.repository.ts +0 -163
  153. package/src/db/repositories/terminal.repository.ts +0 -101
  154. package/src/embeddings/engine.ts +0 -238
  155. package/src/hooks/post-tool-use.ts +0 -92
  156. package/src/hooks/post-write.ts +0 -129
  157. package/src/index.ts +0 -63
  158. package/src/ipc/client.ts +0 -118
  159. package/src/ipc/protocol.ts +0 -35
  160. package/src/ipc/router.ts +0 -133
  161. package/src/ipc/server.ts +0 -176
  162. package/src/learning/confidence-scorer.ts +0 -80
  163. package/src/learning/decay.ts +0 -46
  164. package/src/learning/learning-engine.ts +0 -170
  165. package/src/learning/pattern-extractor.ts +0 -90
  166. package/src/learning/rule-generator.ts +0 -74
  167. package/src/main.rs:10:5 +0 -0
  168. package/src/matching/error-matcher.ts +0 -166
  169. package/src/matching/fingerprint.ts +0 -34
  170. package/src/matching/similarity.ts +0 -61
  171. package/src/matching/tfidf.ts +0 -74
  172. package/src/matching/tokenizer.ts +0 -41
  173. package/src/mcp/auto-detect.ts +0 -93
  174. package/src/mcp/http-server.ts +0 -140
  175. package/src/mcp/server.ts +0 -73
  176. package/src/mcp/tools.ts +0 -328
  177. package/src/parsing/error-parser.ts +0 -28
  178. package/src/parsing/parsers/compiler.ts +0 -93
  179. package/src/parsing/parsers/generic.ts +0 -28
  180. package/src/parsing/parsers/go.ts +0 -97
  181. package/src/parsing/parsers/node.ts +0 -69
  182. package/src/parsing/parsers/python.ts +0 -62
  183. package/src/parsing/parsers/rust.ts +0 -50
  184. package/src/parsing/parsers/shell.ts +0 -42
  185. package/src/parsing/types.ts +0 -47
  186. package/src/research/gap-analyzer.ts +0 -135
  187. package/src/research/insight-generator.ts +0 -123
  188. package/src/research/research-engine.ts +0 -116
  189. package/src/research/synergy-detector.ts +0 -126
  190. package/src/research/template-extractor.ts +0 -130
  191. package/src/research/trend-analyzer.ts +0 -127
  192. package/src/services/analytics.service.ts +0 -226
  193. package/src/services/code.service.ts +0 -271
  194. package/src/services/error.service.ts +0 -266
  195. package/src/services/git.service.ts +0 -132
  196. package/src/services/notification.service.ts +0 -41
  197. package/src/services/prevention.service.ts +0 -159
  198. package/src/services/research.service.ts +0 -98
  199. package/src/services/solution.service.ts +0 -174
  200. package/src/services/synapse.service.ts +0 -59
  201. package/src/services/terminal.service.ts +0 -81
  202. package/src/synapses/activation.ts +0 -80
  203. package/src/synapses/decay.ts +0 -38
  204. package/src/synapses/hebbian.ts +0 -69
  205. package/src/synapses/pathfinder.ts +0 -81
  206. package/src/synapses/synapse-manager.ts +0 -113
  207. package/src/types/code.types.ts +0 -52
  208. package/src/types/config.types.ts +0 -103
  209. package/src/types/error.types.ts +0 -67
  210. package/src/types/ipc.types.ts +0 -8
  211. package/src/types/mcp.types.ts +0 -53
  212. package/src/types/research.types.ts +0 -28
  213. package/src/types/solution.types.ts +0 -30
  214. package/src/types/synapse.types.ts +0 -50
  215. package/src/utils/events.ts +0 -45
  216. package/src/utils/hash.ts +0 -5
  217. package/src/utils/logger.ts +0 -48
  218. package/src/utils/paths.ts +0 -19
  219. package/tests/e2e/test_code_intelligence.py +0 -1015
  220. package/tests/e2e/test_error_memory.py +0 -451
  221. package/tests/e2e/test_full_integration.py +0 -534
  222. package/tests/fixtures/code-modules/modules.ts +0 -83
  223. package/tests/fixtures/errors/go.ts +0 -9
  224. package/tests/fixtures/errors/node.ts +0 -24
  225. package/tests/fixtures/errors/python.ts +0 -21
  226. package/tests/fixtures/errors/rust.ts +0 -25
  227. package/tests/fixtures/errors/shell.ts +0 -15
  228. package/tests/fixtures/solutions/solutions.ts +0 -27
  229. package/tests/helpers/setup-db.ts +0 -52
  230. package/tests/integration/code-flow.test.ts +0 -86
  231. package/tests/integration/error-flow.test.ts +0 -83
  232. package/tests/integration/ipc-flow.test.ts +0 -166
  233. package/tests/integration/learning-cycle.test.ts +0 -82
  234. package/tests/integration/synapse-flow.test.ts +0 -117
  235. package/tests/unit/code/analyzer.test.ts +0 -58
  236. package/tests/unit/code/fingerprint.test.ts +0 -51
  237. package/tests/unit/code/scorer.test.ts +0 -55
  238. package/tests/unit/learning/confidence-scorer.test.ts +0 -60
  239. package/tests/unit/learning/decay.test.ts +0 -45
  240. package/tests/unit/learning/pattern-extractor.test.ts +0 -50
  241. package/tests/unit/matching/error-matcher.test.ts +0 -69
  242. package/tests/unit/matching/fingerprint.test.ts +0 -47
  243. package/tests/unit/matching/similarity.test.ts +0 -65
  244. package/tests/unit/matching/tfidf.test.ts +0 -71
  245. package/tests/unit/matching/tokenizer.test.ts +0 -83
  246. package/tests/unit/parsing/parsers.test.ts +0 -113
  247. package/tests/unit/research/gap-analyzer.test.ts +0 -45
  248. package/tests/unit/research/trend-analyzer.test.ts +0 -45
  249. package/tests/unit/synapses/activation.test.ts +0 -80
  250. package/tests/unit/synapses/decay.test.ts +0 -27
  251. package/tests/unit/synapses/hebbian.test.ts +0 -96
  252. package/tests/unit/synapses/pathfinder.test.ts +0 -72
  253. 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
- }