@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,66 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
-
4
- export interface AntipatternRecord {
5
- id: number;
6
- pattern: string;
7
- description: string;
8
- severity: string;
9
- suggestion: string | null;
10
- occurrences: number;
11
- project_id: number | null;
12
- global: number;
13
- created_at: string;
14
- }
15
-
16
- type AntipatternCreate = Omit<AntipatternRecord, 'id' | 'created_at'>;
17
- type AntipatternUpdate = Partial<Omit<AntipatternRecord, 'id' | 'created_at'>>;
18
-
19
- export class AntipatternRepository {
20
- private stmts: Record<string, Statement>;
21
-
22
- constructor(private db: Database.Database) {
23
- this.stmts = {
24
- create: db.prepare(`
25
- INSERT INTO antipatterns (pattern, description, severity, suggestion, occurrences, project_id, global)
26
- VALUES (@pattern, @description, @severity, @suggestion, @occurrences, @project_id, @global)
27
- `),
28
- getById: db.prepare('SELECT * FROM antipatterns WHERE id = ?'),
29
- delete: db.prepare('DELETE FROM antipatterns WHERE id = ?'),
30
- findByProject: db.prepare('SELECT * FROM antipatterns WHERE project_id = ? ORDER BY occurrences DESC'),
31
- findGlobal: db.prepare('SELECT * FROM antipatterns WHERE global = 1 ORDER BY occurrences DESC'),
32
- };
33
- }
34
-
35
- create(data: AntipatternCreate): number {
36
- const result = this.stmts.create.run(data);
37
- return result.lastInsertRowid as number;
38
- }
39
-
40
- getById(id: number): AntipatternRecord | undefined {
41
- return this.stmts.getById.get(id) as AntipatternRecord | undefined;
42
- }
43
-
44
- update(id: number, data: AntipatternUpdate): boolean {
45
- const fields = Object.keys(data).filter((key) => (data as Record<string, unknown>)[key] !== undefined);
46
- if (fields.length === 0) return false;
47
-
48
- const setClauses = fields.map((field) => `${field} = @${field}`).join(', ');
49
- const stmt = this.db.prepare(`UPDATE antipatterns SET ${setClauses} WHERE id = @id`);
50
- const result = stmt.run({ ...data, id });
51
- return result.changes > 0;
52
- }
53
-
54
- delete(id: number): boolean {
55
- const result = this.stmts.delete.run(id);
56
- return result.changes > 0;
57
- }
58
-
59
- findByProject(projectId: number): AntipatternRecord[] {
60
- return this.stmts.findByProject.all(projectId) as AntipatternRecord[];
61
- }
62
-
63
- findGlobal(): AntipatternRecord[] {
64
- return this.stmts.findGlobal.all() as AntipatternRecord[];
65
- }
66
- }
@@ -1,142 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
- import type { CodeModuleRecord } from '../../types/code.types.js';
4
-
5
- type CodeModuleCreate = Omit<CodeModuleRecord, 'id' | 'created_at' | 'updated_at'>;
6
- type CodeModuleUpdate = Partial<Omit<CodeModuleRecord, 'id' | 'created_at'>>;
7
-
8
- export class CodeModuleRepository {
9
- private stmts: Record<string, Statement>;
10
-
11
- constructor(private db: Database.Database) {
12
- this.stmts = {
13
- create: db.prepare(`
14
- INSERT INTO code_modules (project_id, name, file_path, language, fingerprint, description, source_hash, lines_of_code, complexity, reusability_score)
15
- VALUES (@project_id, @name, @file_path, @language, @fingerprint, @description, @source_hash, @lines_of_code, @complexity, @reusability_score)
16
- `),
17
- getById: db.prepare('SELECT * FROM code_modules WHERE id = ?'),
18
- delete: db.prepare('DELETE FROM code_modules WHERE id = ?'),
19
- findByFingerprint: db.prepare('SELECT * FROM code_modules WHERE fingerprint = ?'),
20
- findByProject: db.prepare('SELECT * FROM code_modules WHERE project_id = ? ORDER BY name ASC'),
21
- findAll: db.prepare('SELECT * FROM code_modules ORDER BY name ASC'),
22
- countAll: db.prepare('SELECT COUNT(*) as count FROM code_modules'),
23
- search: db.prepare(`
24
- SELECT cm.* FROM code_modules cm
25
- JOIN code_modules_fts fts ON cm.id = fts.rowid
26
- WHERE code_modules_fts MATCH ?
27
- ORDER BY rank
28
- `),
29
- upsertSimilarity: db.prepare(`
30
- INSERT INTO module_similarities (module_a_id, module_b_id, similarity_score)
31
- VALUES (@module_a_id, @module_b_id, @similarity_score)
32
- ON CONFLICT(module_a_id, module_b_id)
33
- DO UPDATE SET similarity_score = @similarity_score, computed_at = datetime('now')
34
- `),
35
- findSimilarModules: db.prepare(`
36
- SELECT ms.*, cm.name, cm.file_path, cm.language, cm.reusability_score
37
- FROM module_similarities ms
38
- JOIN code_modules cm ON (
39
- CASE WHEN ms.module_a_id = ? THEN ms.module_b_id ELSE ms.module_a_id END
40
- ) = cm.id
41
- WHERE ms.module_a_id = ? OR ms.module_b_id = ?
42
- ORDER BY ms.similarity_score DESC
43
- LIMIT ?
44
- `),
45
- findHighSimilarityPairs: db.prepare(`
46
- SELECT ms.*,
47
- a.name as a_name, a.file_path as a_path,
48
- b.name as b_name, b.file_path as b_path
49
- FROM module_similarities ms
50
- JOIN code_modules a ON ms.module_a_id = a.id
51
- JOIN code_modules b ON ms.module_b_id = b.id
52
- WHERE ms.similarity_score >= ?
53
- ORDER BY ms.similarity_score DESC
54
- LIMIT ?
55
- `),
56
- };
57
- }
58
-
59
- create(data: CodeModuleCreate): number {
60
- const result = this.stmts.create.run(data);
61
- return result.lastInsertRowid as number;
62
- }
63
-
64
- getById(id: number): CodeModuleRecord | undefined {
65
- return this.stmts.getById.get(id) as CodeModuleRecord | undefined;
66
- }
67
-
68
- update(id: number, data: CodeModuleUpdate): boolean {
69
- const fields = Object.keys(data).filter((key) => (data as Record<string, unknown>)[key] !== undefined);
70
- if (fields.length === 0) return false;
71
-
72
- const setClauses = fields.map((field) => `${field} = @${field}`).join(', ');
73
- const stmt = this.db.prepare(
74
- `UPDATE code_modules SET ${setClauses}, updated_at = datetime('now') WHERE id = @id`
75
- );
76
- const result = stmt.run({ ...data, id });
77
- return result.changes > 0;
78
- }
79
-
80
- delete(id: number): boolean {
81
- const result = this.stmts.delete.run(id);
82
- return result.changes > 0;
83
- }
84
-
85
- findByFingerprint(fingerprint: string): CodeModuleRecord | undefined {
86
- return this.stmts.findByFingerprint.get(fingerprint) as CodeModuleRecord | undefined;
87
- }
88
-
89
- findByLanguage(language: string, limit?: number): CodeModuleRecord[] {
90
- const sql = limit
91
- ? 'SELECT * FROM code_modules WHERE language = ? ORDER BY name ASC LIMIT ?'
92
- : 'SELECT * FROM code_modules WHERE language = ? ORDER BY name ASC';
93
- const stmt = this.db.prepare(sql);
94
- return (limit ? stmt.all(language, limit) : stmt.all(language)) as CodeModuleRecord[];
95
- }
96
-
97
- findByProject(projectId: number): CodeModuleRecord[] {
98
- return this.stmts.findByProject.all(projectId) as CodeModuleRecord[];
99
- }
100
-
101
- search(query: string): CodeModuleRecord[] {
102
- return this.stmts.search.all(query) as CodeModuleRecord[];
103
- }
104
-
105
- findAll(limit?: number): CodeModuleRecord[] {
106
- if (limit) {
107
- const stmt = this.db.prepare('SELECT * FROM code_modules ORDER BY name ASC LIMIT ?');
108
- return stmt.all(limit) as CodeModuleRecord[];
109
- }
110
- return this.stmts.findAll.all() as CodeModuleRecord[];
111
- }
112
-
113
- countAll(): number {
114
- return (this.stmts.countAll.get() as { count: number }).count;
115
- }
116
-
117
- upsertSimilarity(moduleAId: number, moduleBId: number, score: number): void {
118
- // Always store with smaller id first for consistency
119
- const [a, b] = moduleAId < moduleBId ? [moduleAId, moduleBId] : [moduleBId, moduleAId];
120
- this.stmts.upsertSimilarity.run({
121
- module_a_id: a,
122
- module_b_id: b,
123
- similarity_score: score,
124
- });
125
- }
126
-
127
- findSimilarModules(moduleId: number, limit: number = 10): Array<{ module_id: number; similarity_score: number; name: string; file_path: string }> {
128
- return this.stmts.findSimilarModules.all(moduleId, moduleId, moduleId, limit) as Array<{
129
- module_id: number; similarity_score: number; name: string; file_path: string;
130
- }>;
131
- }
132
-
133
- findHighSimilarityPairs(minScore: number = 0.75, limit: number = 50): Array<{
134
- module_a_id: number; module_b_id: number; similarity_score: number;
135
- a_name: string; a_path: string; b_name: string; b_path: string;
136
- }> {
137
- return this.stmts.findHighSimilarityPairs.all(minScore, limit) as Array<{
138
- module_a_id: number; module_b_id: number; similarity_score: number;
139
- a_name: string; a_path: string; b_name: string; b_path: string;
140
- }>;
141
- }
142
- }
@@ -1,189 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
- import type { ErrorRecord } from '../../types/error.types.js';
4
-
5
- type CreateErrorData = Omit<ErrorRecord, 'id' | 'occurrence_count' | 'first_seen' | 'last_seen' | 'resolved' | 'resolved_at'>;
6
- type UpdateErrorData = Partial<Omit<ErrorRecord, 'id' | 'first_seen'>>;
7
-
8
- export class ErrorRepository {
9
- private stmts: Record<string, Statement>;
10
-
11
- constructor(private db: Database.Database) {
12
- this.stmts = {
13
- create: this.db.prepare(`
14
- INSERT INTO errors (project_id, terminal_id, fingerprint, type, message, raw_output, context, file_path, line_number, column_number)
15
- VALUES (@project_id, @terminal_id, @fingerprint, @type, @message, @raw_output, @context, @file_path, @line_number, @column_number)
16
- `),
17
- createChain: this.db.prepare(`
18
- INSERT OR IGNORE INTO error_chains (parent_error_id, child_error_id, relationship)
19
- VALUES (@parent_error_id, @child_error_id, @relationship)
20
- `),
21
- findChainChildren: this.db.prepare(
22
- 'SELECT e.* FROM errors e JOIN error_chains ec ON e.id = ec.child_error_id WHERE ec.parent_error_id = ?'
23
- ),
24
- findChainParents: this.db.prepare(
25
- 'SELECT e.* FROM errors e JOIN error_chains ec ON e.id = ec.parent_error_id WHERE ec.child_error_id = ?'
26
- ),
27
- findRecentByProject: this.db.prepare(
28
- 'SELECT * FROM errors WHERE project_id = ? AND first_seen >= ? ORDER BY first_seen DESC LIMIT ?'
29
- ),
30
- findAllPaginated: this.db.prepare(
31
- 'SELECT * FROM errors ORDER BY last_seen DESC LIMIT ? OFFSET ?'
32
- ),
33
- getById: this.db.prepare(`
34
- SELECT * FROM errors WHERE id = ?
35
- `),
36
- update: this.db.prepare(`
37
- UPDATE errors
38
- SET project_id = COALESCE(@project_id, project_id),
39
- terminal_id = COALESCE(@terminal_id, terminal_id),
40
- fingerprint = COALESCE(@fingerprint, fingerprint),
41
- type = COALESCE(@type, type),
42
- message = COALESCE(@message, message),
43
- raw_output = COALESCE(@raw_output, raw_output),
44
- context = COALESCE(@context, context),
45
- file_path = COALESCE(@file_path, file_path),
46
- line_number = COALESCE(@line_number, line_number),
47
- column_number = COALESCE(@column_number, column_number),
48
- occurrence_count = COALESCE(@occurrence_count, occurrence_count),
49
- last_seen = COALESCE(@last_seen, last_seen),
50
- resolved = COALESCE(@resolved, resolved),
51
- resolved_at = COALESCE(@resolved_at, resolved_at)
52
- WHERE id = @id
53
- `),
54
- delete: this.db.prepare(`
55
- DELETE FROM errors WHERE id = ?
56
- `),
57
- findByFingerprint: this.db.prepare(`
58
- SELECT * FROM errors WHERE fingerprint = ? ORDER BY last_seen DESC
59
- `),
60
- findByProject: this.db.prepare(`
61
- SELECT * FROM errors WHERE project_id = ? ORDER BY last_seen DESC
62
- `),
63
- findUnresolvedAll: this.db.prepare(`
64
- SELECT * FROM errors WHERE resolved = 0 ORDER BY last_seen DESC
65
- `),
66
- findUnresolvedByProject: this.db.prepare(`
67
- SELECT * FROM errors WHERE resolved = 0 AND project_id = ? ORDER BY last_seen DESC
68
- `),
69
- countSinceAll: this.db.prepare(`
70
- SELECT COUNT(*) as count FROM errors WHERE first_seen >= ?
71
- `),
72
- countSinceByProject: this.db.prepare(`
73
- SELECT COUNT(*) as count FROM errors WHERE first_seen >= ? AND project_id = ?
74
- `),
75
- search: this.db.prepare(`
76
- SELECT errors.* FROM errors
77
- JOIN errors_fts ON errors.id = errors_fts.rowid
78
- WHERE errors_fts MATCH ?
79
- ORDER BY rank
80
- `),
81
- incrementOccurrence: this.db.prepare(`
82
- UPDATE errors
83
- SET occurrence_count = occurrence_count + 1,
84
- last_seen = datetime('now')
85
- WHERE id = ?
86
- `),
87
- };
88
- }
89
-
90
- create(data: CreateErrorData): number {
91
- const result = this.stmts.create.run({
92
- project_id: data.project_id,
93
- terminal_id: data.terminal_id ?? null,
94
- fingerprint: data.fingerprint,
95
- type: data.type,
96
- message: data.message,
97
- raw_output: data.raw_output,
98
- context: data.context ?? null,
99
- file_path: data.file_path ?? null,
100
- line_number: data.line_number ?? null,
101
- column_number: data.column_number ?? null,
102
- });
103
- return result.lastInsertRowid as number;
104
- }
105
-
106
- getById(id: number): ErrorRecord | undefined {
107
- return this.stmts.getById.get(id) as ErrorRecord | undefined;
108
- }
109
-
110
- update(id: number, data: UpdateErrorData): void {
111
- this.stmts.update.run({
112
- id,
113
- project_id: data.project_id ?? null,
114
- terminal_id: data.terminal_id ?? null,
115
- fingerprint: data.fingerprint ?? null,
116
- type: data.type ?? null,
117
- message: data.message ?? null,
118
- raw_output: data.raw_output ?? null,
119
- context: data.context ?? null,
120
- file_path: data.file_path ?? null,
121
- line_number: data.line_number ?? null,
122
- column_number: data.column_number ?? null,
123
- occurrence_count: data.occurrence_count ?? null,
124
- last_seen: data.last_seen ?? null,
125
- resolved: data.resolved ?? null,
126
- resolved_at: data.resolved_at ?? null,
127
- });
128
- }
129
-
130
- delete(id: number): void {
131
- this.stmts.delete.run(id);
132
- }
133
-
134
- findByFingerprint(fingerprint: string): ErrorRecord[] {
135
- return this.stmts.findByFingerprint.all(fingerprint) as ErrorRecord[];
136
- }
137
-
138
- findByProject(projectId: number): ErrorRecord[] {
139
- return this.stmts.findByProject.all(projectId) as ErrorRecord[];
140
- }
141
-
142
- findUnresolved(projectId?: number): ErrorRecord[] {
143
- if (projectId !== undefined) {
144
- return this.stmts.findUnresolvedByProject.all(projectId) as ErrorRecord[];
145
- }
146
- return this.stmts.findUnresolvedAll.all() as ErrorRecord[];
147
- }
148
-
149
- countSince(since: string, projectId?: number): number {
150
- if (projectId !== undefined) {
151
- const row = this.stmts.countSinceByProject.get(since, projectId) as { count: number };
152
- return row.count;
153
- }
154
- const row = this.stmts.countSinceAll.get(since) as { count: number };
155
- return row.count;
156
- }
157
-
158
- search(query: string): ErrorRecord[] {
159
- return this.stmts.search.all(query) as ErrorRecord[];
160
- }
161
-
162
- incrementOccurrence(id: number): void {
163
- this.stmts.incrementOccurrence.run(id);
164
- }
165
-
166
- createChain(parentErrorId: number, childErrorId: number, relationship: string = 'caused_by_fix'): void {
167
- this.stmts.createChain.run({
168
- parent_error_id: parentErrorId,
169
- child_error_id: childErrorId,
170
- relationship,
171
- });
172
- }
173
-
174
- findChainChildren(errorId: number): ErrorRecord[] {
175
- return this.stmts.findChainChildren.all(errorId) as ErrorRecord[];
176
- }
177
-
178
- findChainParents(errorId: number): ErrorRecord[] {
179
- return this.stmts.findChainParents.all(errorId) as ErrorRecord[];
180
- }
181
-
182
- findRecentByProject(projectId: number, since: string, limit: number = 10): ErrorRecord[] {
183
- return this.stmts.findRecentByProject.all(projectId, since, limit) as ErrorRecord[];
184
- }
185
-
186
- findAll(limit: number = 100, offset: number = 0): ErrorRecord[] {
187
- return this.stmts.findAllPaginated.all(limit, offset) as ErrorRecord[];
188
- }
189
- }
@@ -1,99 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
- import type { InsightRecord } from '../../types/research.types.js';
4
-
5
- type InsightCreate = Omit<InsightRecord, 'id' | 'created_at'>;
6
- type InsightUpdate = Partial<Omit<InsightRecord, 'id' | 'created_at'>>;
7
-
8
- export class InsightRepository {
9
- private stmts: Record<string, Statement>;
10
-
11
- constructor(private db: Database.Database) {
12
- this.stmts = {
13
- create: db.prepare(`
14
- INSERT INTO insights (type, title, description, evidence, priority, project_id, active, expires_at)
15
- VALUES (@type, @title, @description, @evidence, @priority, @project_id, @active, @expires_at)
16
- `),
17
- getById: db.prepare('SELECT * FROM insights WHERE id = ?'),
18
- delete: db.prepare('DELETE FROM insights WHERE id = ?'),
19
- findActiveAll: db.prepare(
20
- 'SELECT * FROM insights WHERE active = 1 ORDER BY priority DESC, created_at DESC'
21
- ),
22
- findActiveByProject: db.prepare(
23
- 'SELECT * FROM insights WHERE active = 1 AND project_id = ? ORDER BY priority DESC, created_at DESC'
24
- ),
25
- findByType: db.prepare('SELECT * FROM insights WHERE type = ? ORDER BY priority DESC'),
26
- findByPriority: db.prepare(
27
- 'SELECT * FROM insights WHERE priority >= ? ORDER BY priority DESC, created_at DESC'
28
- ),
29
- expire: db.prepare(
30
- `UPDATE insights SET active = 0 WHERE active = 1 AND expires_at IS NOT NULL AND expires_at < datetime('now')`
31
- ),
32
- };
33
- }
34
-
35
- create(data: InsightCreate): number {
36
- const result = this.stmts.create.run(data);
37
- return result.lastInsertRowid as number;
38
- }
39
-
40
- getById(id: number): InsightRecord | undefined {
41
- return this.stmts.getById.get(id) as InsightRecord | undefined;
42
- }
43
-
44
- update(id: number, data: InsightUpdate): boolean {
45
- const fields = Object.keys(data).filter((key) => (data as Record<string, unknown>)[key] !== undefined);
46
- if (fields.length === 0) return false;
47
-
48
- const setClauses = fields.map((field) => `${field} = @${field}`).join(', ');
49
- const stmt = this.db.prepare(`UPDATE insights SET ${setClauses} WHERE id = @id`);
50
- const result = stmt.run({ ...data, id });
51
- return result.changes > 0;
52
- }
53
-
54
- delete(id: number): boolean {
55
- const result = this.stmts.delete.run(id);
56
- return result.changes > 0;
57
- }
58
-
59
- findActive(projectId?: number): InsightRecord[] {
60
- if (projectId !== undefined) {
61
- return this.stmts.findActiveByProject.all(projectId) as InsightRecord[];
62
- }
63
- return this.stmts.findActiveAll.all() as InsightRecord[];
64
- }
65
-
66
- findByType(type: string): InsightRecord[] {
67
- return this.stmts.findByType.all(type) as InsightRecord[];
68
- }
69
-
70
- findByPriority(minPriority: number): InsightRecord[] {
71
- return this.stmts.findByPriority.all(minPriority) as InsightRecord[];
72
- }
73
-
74
- expire(): number {
75
- const result = this.stmts.expire.run();
76
- return result.changes;
77
- }
78
-
79
- rate(id: number, rating: number, comment?: string): boolean {
80
- const stmt = this.db.prepare(
81
- `UPDATE insights SET rating = ?, rating_comment = ?, rated_at = datetime('now') WHERE id = ?`
82
- );
83
- const result = stmt.run(rating, comment ?? null, id);
84
- return result.changes > 0;
85
- }
86
-
87
- findRated(minRating?: number): InsightRecord[] {
88
- if (minRating !== undefined) {
89
- const stmt = this.db.prepare(
90
- 'SELECT * FROM insights WHERE rating IS NOT NULL AND rating >= ? ORDER BY rating DESC'
91
- );
92
- return stmt.all(minRating) as InsightRecord[];
93
- }
94
- const stmt = this.db.prepare(
95
- 'SELECT * FROM insights WHERE rating IS NOT NULL ORDER BY rating DESC'
96
- );
97
- return stmt.all() as InsightRecord[];
98
- }
99
- }
@@ -1,66 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
-
4
- export interface NotificationRecord {
5
- id: number;
6
- type: string;
7
- title: string;
8
- message: string;
9
- priority: number;
10
- project_id: number | null;
11
- acknowledged: number;
12
- acknowledged_at: string | null;
13
- created_at: string;
14
- }
15
-
16
- type NotificationCreate = Omit<NotificationRecord, 'id' | 'acknowledged' | 'acknowledged_at' | 'created_at'>;
17
-
18
- export class NotificationRepository {
19
- private stmts: Record<string, Statement>;
20
-
21
- constructor(private db: Database.Database) {
22
- this.stmts = {
23
- create: db.prepare(`
24
- INSERT INTO notifications (type, title, message, priority, project_id)
25
- VALUES (@type, @title, @message, @priority, @project_id)
26
- `),
27
- getById: db.prepare('SELECT * FROM notifications WHERE id = ?'),
28
- delete: db.prepare('DELETE FROM notifications WHERE id = ?'),
29
- findUnacknowledgedAll: db.prepare(
30
- 'SELECT * FROM notifications WHERE acknowledged = 0 ORDER BY priority DESC, created_at DESC'
31
- ),
32
- findUnacknowledgedByProject: db.prepare(
33
- 'SELECT * FROM notifications WHERE acknowledged = 0 AND project_id = ? ORDER BY priority DESC, created_at DESC'
34
- ),
35
- acknowledge: db.prepare(
36
- `UPDATE notifications SET acknowledged = 1, acknowledged_at = datetime('now') WHERE id = ?`
37
- ),
38
- };
39
- }
40
-
41
- create(data: NotificationCreate): number {
42
- const result = this.stmts.create.run(data);
43
- return result.lastInsertRowid as number;
44
- }
45
-
46
- getById(id: number): NotificationRecord | undefined {
47
- return this.stmts.getById.get(id) as NotificationRecord | undefined;
48
- }
49
-
50
- delete(id: number): boolean {
51
- const result = this.stmts.delete.run(id);
52
- return result.changes > 0;
53
- }
54
-
55
- findUnacknowledged(projectId?: number): NotificationRecord[] {
56
- if (projectId !== undefined) {
57
- return this.stmts.findUnacknowledgedByProject.all(projectId) as NotificationRecord[];
58
- }
59
- return this.stmts.findUnacknowledgedAll.all() as NotificationRecord[];
60
- }
61
-
62
- acknowledge(id: number): boolean {
63
- const result = this.stmts.acknowledge.run(id);
64
- return result.changes > 0;
65
- }
66
- }
@@ -1,93 +0,0 @@
1
- import type Database from 'better-sqlite3';
2
- import type { Statement } from 'better-sqlite3';
3
-
4
- export interface ProjectRecord {
5
- id: number;
6
- name: string;
7
- path: string | null;
8
- language: string | null;
9
- framework: string | null;
10
- created_at: string;
11
- updated_at: string;
12
- }
13
-
14
- type CreateProjectData = Omit<ProjectRecord, 'id' | 'created_at' | 'updated_at'>;
15
- type UpdateProjectData = Partial<Omit<ProjectRecord, 'id' | 'created_at'>>;
16
-
17
- export class ProjectRepository {
18
- private stmts: Record<string, Statement>;
19
-
20
- constructor(private db: Database.Database) {
21
- this.stmts = {
22
- create: this.db.prepare(`
23
- INSERT INTO projects (name, path, language, framework)
24
- VALUES (@name, @path, @language, @framework)
25
- `),
26
- getById: this.db.prepare(`
27
- SELECT * FROM projects WHERE id = ?
28
- `),
29
- update: this.db.prepare(`
30
- UPDATE projects
31
- SET name = COALESCE(@name, name),
32
- path = COALESCE(@path, path),
33
- language = COALESCE(@language, language),
34
- framework = COALESCE(@framework, framework),
35
- updated_at = datetime('now')
36
- WHERE id = @id
37
- `),
38
- delete: this.db.prepare(`
39
- DELETE FROM projects WHERE id = ?
40
- `),
41
- findByName: this.db.prepare(`
42
- SELECT * FROM projects WHERE name = ?
43
- `),
44
- findByPath: this.db.prepare(`
45
- SELECT * FROM projects WHERE path = ?
46
- `),
47
- getAll: this.db.prepare(`
48
- SELECT * FROM projects ORDER BY name
49
- `),
50
- };
51
- }
52
-
53
- create(data: CreateProjectData): number {
54
- const result = this.stmts.create.run({
55
- name: data.name,
56
- path: data.path ?? null,
57
- language: data.language ?? null,
58
- framework: data.framework ?? null,
59
- });
60
- return result.lastInsertRowid as number;
61
- }
62
-
63
- getById(id: number): ProjectRecord | undefined {
64
- return this.stmts.getById.get(id) as ProjectRecord | undefined;
65
- }
66
-
67
- update(id: number, data: UpdateProjectData): void {
68
- this.stmts.update.run({
69
- id,
70
- name: data.name ?? null,
71
- path: data.path ?? null,
72
- language: data.language ?? null,
73
- framework: data.framework ?? null,
74
- updated_at: data.updated_at ?? null,
75
- });
76
- }
77
-
78
- delete(id: number): void {
79
- this.stmts.delete.run(id);
80
- }
81
-
82
- findByName(name: string): ProjectRecord | undefined {
83
- return this.stmts.findByName.get(name) as ProjectRecord | undefined;
84
- }
85
-
86
- findByPath(path: string): ProjectRecord | undefined {
87
- return this.stmts.findByPath.get(path) as ProjectRecord | undefined;
88
- }
89
-
90
- getAll(): ProjectRecord[] {
91
- return this.stmts.getAll.all() as ProjectRecord[];
92
- }
93
- }