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