@timmeck/brain 1.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.
- package/BRAIN_PLAN.md +3324 -0
- package/LICENSE +21 -0
- package/README.md +188 -0
- package/dist/brain.d.ts +11 -0
- package/dist/brain.js +166 -0
- package/dist/brain.js.map +1 -0
- package/dist/cli/commands/dashboard.d.ts +2 -0
- package/dist/cli/commands/dashboard.js +457 -0
- package/dist/cli/commands/dashboard.js.map +1 -0
- package/dist/cli/commands/export.d.ts +2 -0
- package/dist/cli/commands/export.js +25 -0
- package/dist/cli/commands/export.js.map +1 -0
- package/dist/cli/commands/import.d.ts +2 -0
- package/dist/cli/commands/import.js +173 -0
- package/dist/cli/commands/import.js.map +1 -0
- package/dist/cli/commands/insights.d.ts +2 -0
- package/dist/cli/commands/insights.js +32 -0
- package/dist/cli/commands/insights.js.map +1 -0
- package/dist/cli/commands/modules.d.ts +2 -0
- package/dist/cli/commands/modules.js +29 -0
- package/dist/cli/commands/modules.js.map +1 -0
- package/dist/cli/commands/network.d.ts +2 -0
- package/dist/cli/commands/network.js +56 -0
- package/dist/cli/commands/network.js.map +1 -0
- package/dist/cli/commands/query.d.ts +2 -0
- package/dist/cli/commands/query.js +40 -0
- package/dist/cli/commands/query.js.map +1 -0
- package/dist/cli/commands/start.d.ts +2 -0
- package/dist/cli/commands/start.js +56 -0
- package/dist/cli/commands/start.js.map +1 -0
- package/dist/cli/commands/status.d.ts +2 -0
- package/dist/cli/commands/status.js +60 -0
- package/dist/cli/commands/status.js.map +1 -0
- package/dist/cli/commands/stop.d.ts +2 -0
- package/dist/cli/commands/stop.js +34 -0
- package/dist/cli/commands/stop.js.map +1 -0
- package/dist/cli/ipc-helper.d.ts +2 -0
- package/dist/cli/ipc-helper.js +25 -0
- package/dist/cli/ipc-helper.js.map +1 -0
- package/dist/code/analyzer.d.ts +12 -0
- package/dist/code/analyzer.js +58 -0
- package/dist/code/analyzer.js.map +1 -0
- package/dist/code/fingerprint.d.ts +2 -0
- package/dist/code/fingerprint.js +84 -0
- package/dist/code/fingerprint.js.map +1 -0
- package/dist/code/matcher.d.ts +9 -0
- package/dist/code/matcher.js +37 -0
- package/dist/code/matcher.js.map +1 -0
- package/dist/code/parsers/generic.d.ts +7 -0
- package/dist/code/parsers/generic.js +22 -0
- package/dist/code/parsers/generic.js.map +1 -0
- package/dist/code/parsers/python.d.ts +7 -0
- package/dist/code/parsers/python.js +45 -0
- package/dist/code/parsers/python.js.map +1 -0
- package/dist/code/parsers/typescript.d.ts +7 -0
- package/dist/code/parsers/typescript.js +58 -0
- package/dist/code/parsers/typescript.js.map +1 -0
- package/dist/code/registry.d.ts +22 -0
- package/dist/code/registry.js +31 -0
- package/dist/code/registry.js.map +1 -0
- package/dist/code/scorer.d.ts +15 -0
- package/dist/code/scorer.js +103 -0
- package/dist/code/scorer.js.map +1 -0
- package/dist/config.d.ts +2 -0
- package/dist/config.js +110 -0
- package/dist/config.js.map +1 -0
- package/dist/db/connection.d.ts +2 -0
- package/dist/db/connection.js +19 -0
- package/dist/db/connection.js.map +1 -0
- package/dist/db/migrations/001_core_schema.d.ts +2 -0
- package/dist/db/migrations/001_core_schema.js +119 -0
- package/dist/db/migrations/001_core_schema.js.map +1 -0
- package/dist/db/migrations/002_learning_schema.d.ts +2 -0
- package/dist/db/migrations/002_learning_schema.js +37 -0
- package/dist/db/migrations/002_learning_schema.js.map +1 -0
- package/dist/db/migrations/003_code_schema.d.ts +2 -0
- package/dist/db/migrations/003_code_schema.js +52 -0
- package/dist/db/migrations/003_code_schema.js.map +1 -0
- package/dist/db/migrations/004_synapses_schema.d.ts +2 -0
- package/dist/db/migrations/004_synapses_schema.js +56 -0
- package/dist/db/migrations/004_synapses_schema.js.map +1 -0
- package/dist/db/migrations/005_fts_indexes.d.ts +2 -0
- package/dist/db/migrations/005_fts_indexes.js +77 -0
- package/dist/db/migrations/005_fts_indexes.js.map +1 -0
- package/dist/db/migrations/006_synapses_phase3.d.ts +2 -0
- package/dist/db/migrations/006_synapses_phase3.js +14 -0
- package/dist/db/migrations/006_synapses_phase3.js.map +1 -0
- package/dist/db/migrations/index.d.ts +2 -0
- package/dist/db/migrations/index.js +49 -0
- package/dist/db/migrations/index.js.map +1 -0
- package/dist/db/repositories/antipattern.repository.d.ts +26 -0
- package/dist/db/repositories/antipattern.repository.js +44 -0
- package/dist/db/repositories/antipattern.repository.js.map +1 -0
- package/dist/db/repositories/code-module.repository.d.ts +19 -0
- package/dist/db/repositories/code-module.repository.js +64 -0
- package/dist/db/repositories/code-module.repository.js.map +1 -0
- package/dist/db/repositories/error.repository.d.ts +20 -0
- package/dist/db/repositories/error.repository.js +134 -0
- package/dist/db/repositories/error.repository.js.map +1 -0
- package/dist/db/repositories/insight.repository.d.ts +18 -0
- package/dist/db/repositories/insight.repository.js +57 -0
- package/dist/db/repositories/insight.repository.js.map +1 -0
- package/dist/db/repositories/notification.repository.d.ts +24 -0
- package/dist/db/repositories/notification.repository.js +40 -0
- package/dist/db/repositories/notification.repository.js.map +1 -0
- package/dist/db/repositories/project.repository.d.ts +25 -0
- package/dist/db/repositories/project.repository.js +72 -0
- package/dist/db/repositories/project.repository.js.map +1 -0
- package/dist/db/repositories/rule.repository.d.ts +31 -0
- package/dist/db/repositories/rule.repository.js +81 -0
- package/dist/db/repositories/rule.repository.js.map +1 -0
- package/dist/db/repositories/solution.repository.d.ts +27 -0
- package/dist/db/repositories/solution.repository.js +132 -0
- package/dist/db/repositories/solution.repository.js.map +1 -0
- package/dist/db/repositories/synapse.repository.d.ts +25 -0
- package/dist/db/repositories/synapse.repository.js +115 -0
- package/dist/db/repositories/synapse.repository.js.map +1 -0
- package/dist/db/repositories/terminal.repository.d.ts +27 -0
- package/dist/db/repositories/terminal.repository.js +78 -0
- package/dist/db/repositories/terminal.repository.js.map +1 -0
- package/dist/hooks/post-tool-use.d.ts +2 -0
- package/dist/hooks/post-tool-use.js +77 -0
- package/dist/hooks/post-tool-use.js.map +1 -0
- package/dist/hooks/post-write.d.ts +2 -0
- package/dist/hooks/post-write.js +102 -0
- package/dist/hooks/post-write.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/dist/ipc/client.d.ts +16 -0
- package/dist/ipc/client.js +101 -0
- package/dist/ipc/client.js.map +1 -0
- package/dist/ipc/protocol.d.ts +8 -0
- package/dist/ipc/protocol.js +29 -0
- package/dist/ipc/protocol.js.map +1 -0
- package/dist/ipc/router.d.ts +28 -0
- package/dist/ipc/router.js +70 -0
- package/dist/ipc/router.js.map +1 -0
- package/dist/ipc/server.d.ts +14 -0
- package/dist/ipc/server.js +98 -0
- package/dist/ipc/server.js.map +1 -0
- package/dist/learning/confidence-scorer.d.ts +13 -0
- package/dist/learning/confidence-scorer.js +35 -0
- package/dist/learning/confidence-scorer.js.map +1 -0
- package/dist/learning/decay.d.ts +13 -0
- package/dist/learning/decay.js +37 -0
- package/dist/learning/decay.js.map +1 -0
- package/dist/learning/learning-engine.d.ts +30 -0
- package/dist/learning/learning-engine.js +121 -0
- package/dist/learning/learning-engine.js.map +1 -0
- package/dist/learning/pattern-extractor.d.ts +16 -0
- package/dist/learning/pattern-extractor.js +61 -0
- package/dist/learning/pattern-extractor.js.map +1 -0
- package/dist/learning/rule-generator.d.ts +18 -0
- package/dist/learning/rule-generator.js +50 -0
- package/dist/learning/rule-generator.js.map +1 -0
- package/dist/matching/error-matcher.d.ts +13 -0
- package/dist/matching/error-matcher.js +84 -0
- package/dist/matching/error-matcher.js.map +1 -0
- package/dist/matching/fingerprint.d.ts +3 -0
- package/dist/matching/fingerprint.js +23 -0
- package/dist/matching/fingerprint.js.map +1 -0
- package/dist/matching/similarity.d.ts +3 -0
- package/dist/matching/similarity.js +53 -0
- package/dist/matching/similarity.js.map +1 -0
- package/dist/matching/tfidf.d.ts +15 -0
- package/dist/matching/tfidf.js +68 -0
- package/dist/matching/tfidf.js.map +1 -0
- package/dist/matching/tokenizer.d.ts +4 -0
- package/dist/matching/tokenizer.js +36 -0
- package/dist/matching/tokenizer.js.map +1 -0
- package/dist/mcp/auto-detect.d.ts +1 -0
- package/dist/mcp/auto-detect.js +81 -0
- package/dist/mcp/auto-detect.js.map +1 -0
- package/dist/mcp/server.d.ts +1 -0
- package/dist/mcp/server.js +68 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tools.d.ts +3 -0
- package/dist/mcp/tools.js +201 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/parsing/error-parser.d.ts +3 -0
- package/dist/parsing/error-parser.js +26 -0
- package/dist/parsing/error-parser.js.map +1 -0
- package/dist/parsing/parsers/compiler.d.ts +2 -0
- package/dist/parsing/parsers/compiler.js +83 -0
- package/dist/parsing/parsers/compiler.js.map +1 -0
- package/dist/parsing/parsers/generic.d.ts +2 -0
- package/dist/parsing/parsers/generic.js +23 -0
- package/dist/parsing/parsers/generic.js.map +1 -0
- package/dist/parsing/parsers/go.d.ts +2 -0
- package/dist/parsing/parsers/go.js +85 -0
- package/dist/parsing/parsers/go.js.map +1 -0
- package/dist/parsing/parsers/node.d.ts +2 -0
- package/dist/parsing/parsers/node.js +61 -0
- package/dist/parsing/parsers/node.js.map +1 -0
- package/dist/parsing/parsers/python.d.ts +2 -0
- package/dist/parsing/parsers/python.js +50 -0
- package/dist/parsing/parsers/python.js.map +1 -0
- package/dist/parsing/parsers/rust.d.ts +2 -0
- package/dist/parsing/parsers/rust.js +43 -0
- package/dist/parsing/parsers/rust.js.map +1 -0
- package/dist/parsing/parsers/shell.d.ts +2 -0
- package/dist/parsing/parsers/shell.js +36 -0
- package/dist/parsing/parsers/shell.js.map +1 -0
- package/dist/parsing/types.d.ts +28 -0
- package/dist/parsing/types.js +21 -0
- package/dist/parsing/types.js.map +1 -0
- package/dist/research/gap-analyzer.d.ts +23 -0
- package/dist/research/gap-analyzer.js +119 -0
- package/dist/research/gap-analyzer.js.map +1 -0
- package/dist/research/insight-generator.d.ts +23 -0
- package/dist/research/insight-generator.js +107 -0
- package/dist/research/insight-generator.js.map +1 -0
- package/dist/research/research-engine.d.ts +31 -0
- package/dist/research/research-engine.js +97 -0
- package/dist/research/research-engine.js.map +1 -0
- package/dist/research/synergy-detector.d.ts +24 -0
- package/dist/research/synergy-detector.js +109 -0
- package/dist/research/synergy-detector.js.map +1 -0
- package/dist/research/template-extractor.d.ts +18 -0
- package/dist/research/template-extractor.js +116 -0
- package/dist/research/template-extractor.js.map +1 -0
- package/dist/research/trend-analyzer.d.ts +20 -0
- package/dist/research/trend-analyzer.js +111 -0
- package/dist/research/trend-analyzer.js.map +1 -0
- package/dist/services/analytics.service.d.ts +52 -0
- package/dist/services/analytics.service.js +59 -0
- package/dist/services/analytics.service.js.map +1 -0
- package/dist/services/code.service.d.ts +39 -0
- package/dist/services/code.service.js +98 -0
- package/dist/services/code.service.js.map +1 -0
- package/dist/services/error.service.d.ts +35 -0
- package/dist/services/error.service.js +118 -0
- package/dist/services/error.service.js.map +1 -0
- package/dist/services/notification.service.d.ts +17 -0
- package/dist/services/notification.service.js +29 -0
- package/dist/services/notification.service.js.map +1 -0
- package/dist/services/prevention.service.d.ts +35 -0
- package/dist/services/prevention.service.js +82 -0
- package/dist/services/prevention.service.js.map +1 -0
- package/dist/services/research.service.d.ts +35 -0
- package/dist/services/research.service.js +60 -0
- package/dist/services/research.service.js.map +1 -0
- package/dist/services/solution.service.d.ts +30 -0
- package/dist/services/solution.service.js +73 -0
- package/dist/services/solution.service.js.map +1 -0
- package/dist/services/synapse.service.d.ts +30 -0
- package/dist/services/synapse.service.js +25 -0
- package/dist/services/synapse.service.js.map +1 -0
- package/dist/services/terminal.service.d.ts +20 -0
- package/dist/services/terminal.service.js +66 -0
- package/dist/services/terminal.service.js.map +1 -0
- package/dist/synapses/activation.d.ts +13 -0
- package/dist/synapses/activation.js +50 -0
- package/dist/synapses/activation.js.map +1 -0
- package/dist/synapses/decay.d.ts +11 -0
- package/dist/synapses/decay.js +27 -0
- package/dist/synapses/decay.js.map +1 -0
- package/dist/synapses/hebbian.d.ts +13 -0
- package/dist/synapses/hebbian.js +36 -0
- package/dist/synapses/hebbian.js.map +1 -0
- package/dist/synapses/pathfinder.d.ts +14 -0
- package/dist/synapses/pathfinder.js +50 -0
- package/dist/synapses/pathfinder.js.map +1 -0
- package/dist/synapses/synapse-manager.d.ts +30 -0
- package/dist/synapses/synapse-manager.js +72 -0
- package/dist/synapses/synapse-manager.js.map +1 -0
- package/dist/types/code.types.d.ts +47 -0
- package/dist/types/code.types.js +2 -0
- package/dist/types/code.types.js.map +1 -0
- package/dist/types/config.types.d.ts +70 -0
- package/dist/types/config.types.js +2 -0
- package/dist/types/config.types.js.map +1 -0
- package/dist/types/error.types.d.ts +60 -0
- package/dist/types/error.types.js +2 -0
- package/dist/types/error.types.js.map +1 -0
- package/dist/types/ipc.types.d.ts +11 -0
- package/dist/types/ipc.types.js +2 -0
- package/dist/types/ipc.types.js.map +1 -0
- package/dist/types/mcp.types.d.ts +46 -0
- package/dist/types/mcp.types.js +2 -0
- package/dist/types/mcp.types.js.map +1 -0
- package/dist/types/research.types.d.ts +25 -0
- package/dist/types/research.types.js +2 -0
- package/dist/types/research.types.js.map +1 -0
- package/dist/types/solution.types.d.ts +28 -0
- package/dist/types/solution.types.js +2 -0
- package/dist/types/solution.types.js.map +1 -0
- package/dist/types/synapse.types.d.ts +37 -0
- package/dist/types/synapse.types.js +2 -0
- package/dist/types/synapse.types.js.map +1 -0
- package/dist/utils/events.d.ts +59 -0
- package/dist/utils/events.js +23 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/hash.d.ts +1 -0
- package/dist/utils/hash.js +5 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/logger.d.ts +8 -0
- package/dist/utils/logger.js +39 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/paths.d.ts +3 -0
- package/dist/utils/paths.js +18 -0
- package/dist/utils/paths.js.map +1 -0
- package/package.json +43 -0
- package/src/brain.ts +220 -0
- package/src/cli/commands/dashboard.ts +495 -0
- package/src/cli/commands/export.ts +27 -0
- package/src/cli/commands/import.ts +190 -0
- package/src/cli/commands/insights.ts +33 -0
- package/src/cli/commands/modules.ts +30 -0
- package/src/cli/commands/network.ts +61 -0
- package/src/cli/commands/query.ts +43 -0
- package/src/cli/commands/start.ts +59 -0
- package/src/cli/commands/status.ts +69 -0
- package/src/cli/commands/stop.ts +33 -0
- package/src/cli/ipc-helper.ts +21 -0
- package/src/code/analyzer.ts +77 -0
- package/src/code/fingerprint.ts +87 -0
- package/src/code/matcher.ts +64 -0
- package/src/code/parsers/generic.ts +29 -0
- package/src/code/parsers/python.ts +54 -0
- package/src/code/parsers/typescript.ts +65 -0
- package/src/code/registry.ts +60 -0
- package/src/code/scorer.ts +108 -0
- package/src/config.ts +111 -0
- package/src/db/connection.ts +22 -0
- package/src/db/migrations/001_core_schema.ts +120 -0
- package/src/db/migrations/002_learning_schema.ts +38 -0
- package/src/db/migrations/003_code_schema.ts +53 -0
- package/src/db/migrations/004_synapses_schema.ts +57 -0
- package/src/db/migrations/005_fts_indexes.ts +78 -0
- package/src/db/migrations/006_synapses_phase3.ts +17 -0
- package/src/db/migrations/index.ts +64 -0
- package/src/db/repositories/antipattern.repository.ts +66 -0
- package/src/db/repositories/code-module.repository.ts +80 -0
- package/src/db/repositories/error.repository.ts +149 -0
- package/src/db/repositories/insight.repository.ts +78 -0
- package/src/db/repositories/notification.repository.ts +66 -0
- package/src/db/repositories/project.repository.ts +93 -0
- package/src/db/repositories/rule.repository.ts +108 -0
- package/src/db/repositories/solution.repository.ts +154 -0
- package/src/db/repositories/synapse.repository.ts +153 -0
- package/src/db/repositories/terminal.repository.ts +101 -0
- package/src/hooks/post-tool-use.ts +90 -0
- package/src/hooks/post-write.ts +117 -0
- package/src/index.ts +53 -0
- package/src/ipc/client.ts +118 -0
- package/src/ipc/protocol.ts +35 -0
- package/src/ipc/router.ts +106 -0
- package/src/ipc/server.ts +110 -0
- package/src/learning/confidence-scorer.ts +47 -0
- package/src/learning/decay.ts +46 -0
- package/src/learning/learning-engine.ts +162 -0
- package/src/learning/pattern-extractor.ts +90 -0
- package/src/learning/rule-generator.ts +74 -0
- package/src/main.rs:10:5 +0 -0
- package/src/matching/error-matcher.ts +115 -0
- package/src/matching/fingerprint.ts +29 -0
- package/src/matching/similarity.ts +61 -0
- package/src/matching/tfidf.ts +74 -0
- package/src/matching/tokenizer.ts +41 -0
- package/src/mcp/auto-detect.ts +93 -0
- package/src/mcp/server.ts +73 -0
- package/src/mcp/tools.ts +290 -0
- package/src/parsing/error-parser.ts +28 -0
- package/src/parsing/parsers/compiler.ts +93 -0
- package/src/parsing/parsers/generic.ts +28 -0
- package/src/parsing/parsers/go.ts +97 -0
- package/src/parsing/parsers/node.ts +69 -0
- package/src/parsing/parsers/python.ts +62 -0
- package/src/parsing/parsers/rust.ts +50 -0
- package/src/parsing/parsers/shell.ts +42 -0
- package/src/parsing/types.ts +47 -0
- package/src/research/gap-analyzer.ts +135 -0
- package/src/research/insight-generator.ts +123 -0
- package/src/research/research-engine.ts +116 -0
- package/src/research/synergy-detector.ts +126 -0
- package/src/research/template-extractor.ts +130 -0
- package/src/research/trend-analyzer.ts +127 -0
- package/src/services/analytics.service.ts +87 -0
- package/src/services/code.service.ts +140 -0
- package/src/services/error.service.ts +164 -0
- package/src/services/notification.service.ts +41 -0
- package/src/services/prevention.service.ts +119 -0
- package/src/services/research.service.ts +93 -0
- package/src/services/solution.service.ts +116 -0
- package/src/services/synapse.service.ts +59 -0
- package/src/services/terminal.service.ts +81 -0
- package/src/synapses/activation.ts +80 -0
- package/src/synapses/decay.ts +38 -0
- package/src/synapses/hebbian.ts +69 -0
- package/src/synapses/pathfinder.ts +81 -0
- package/src/synapses/synapse-manager.ts +109 -0
- package/src/types/code.types.ts +52 -0
- package/src/types/config.types.ts +79 -0
- package/src/types/error.types.ts +67 -0
- package/src/types/ipc.types.ts +8 -0
- package/src/types/mcp.types.ts +53 -0
- package/src/types/research.types.ts +28 -0
- package/src/types/solution.types.ts +30 -0
- package/src/types/synapse.types.ts +49 -0
- package/src/utils/events.ts +45 -0
- package/src/utils/hash.ts +5 -0
- package/src/utils/logger.ts +48 -0
- package/src/utils/paths.ts +19 -0
- package/tests/fixtures/code-modules/modules.ts +83 -0
- package/tests/fixtures/errors/go.ts +9 -0
- package/tests/fixtures/errors/node.ts +24 -0
- package/tests/fixtures/errors/python.ts +21 -0
- package/tests/fixtures/errors/rust.ts +25 -0
- package/tests/fixtures/errors/shell.ts +15 -0
- package/tests/fixtures/solutions/solutions.ts +27 -0
- package/tests/helpers/setup-db.ts +52 -0
- package/tests/integration/code-flow.test.ts +86 -0
- package/tests/integration/error-flow.test.ts +83 -0
- package/tests/integration/ipc-flow.test.ts +166 -0
- package/tests/integration/learning-cycle.test.ts +82 -0
- package/tests/integration/synapse-flow.test.ts +117 -0
- package/tests/unit/code/analyzer.test.ts +58 -0
- package/tests/unit/code/fingerprint.test.ts +51 -0
- package/tests/unit/code/scorer.test.ts +55 -0
- package/tests/unit/learning/confidence-scorer.test.ts +60 -0
- package/tests/unit/learning/decay.test.ts +45 -0
- package/tests/unit/learning/pattern-extractor.test.ts +50 -0
- package/tests/unit/matching/error-matcher.test.ts +69 -0
- package/tests/unit/matching/fingerprint.test.ts +47 -0
- package/tests/unit/matching/similarity.test.ts +65 -0
- package/tests/unit/matching/tfidf.test.ts +71 -0
- package/tests/unit/matching/tokenizer.test.ts +83 -0
- package/tests/unit/parsing/parsers.test.ts +113 -0
- package/tests/unit/research/gap-analyzer.test.ts +45 -0
- package/tests/unit/research/trend-analyzer.test.ts +45 -0
- package/tests/unit/synapses/activation.test.ts +80 -0
- package/tests/unit/synapses/decay.test.ts +27 -0
- package/tests/unit/synapses/hebbian.test.ts +96 -0
- package/tests/unit/synapses/pathfinder.test.ts +72 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import type { ResearchConfig } from '../types/config.types.js';
|
|
2
|
+
import type { CodeModuleRepository } from '../db/repositories/code-module.repository.js';
|
|
3
|
+
import type { ProjectRepository } from '../db/repositories/project.repository.js';
|
|
4
|
+
import type { InsightRepository } from '../db/repositories/insight.repository.js';
|
|
5
|
+
import { getLogger } from '../utils/logger.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Identifies code modules adapted across projects that can be generalized into templates.
|
|
9
|
+
*/
|
|
10
|
+
export class TemplateExtractor {
|
|
11
|
+
private logger = getLogger();
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
private codeModuleRepo: CodeModuleRepository,
|
|
15
|
+
private projectRepo: ProjectRepository,
|
|
16
|
+
private insightRepo: InsightRepository,
|
|
17
|
+
private config: ResearchConfig,
|
|
18
|
+
) {}
|
|
19
|
+
|
|
20
|
+
extract(): number {
|
|
21
|
+
let insightsCreated = 0;
|
|
22
|
+
|
|
23
|
+
insightsCreated += this.findReusableModules();
|
|
24
|
+
insightsCreated += this.findTemplatingCandidates();
|
|
25
|
+
|
|
26
|
+
this.logger.info(`Template extraction complete: ${insightsCreated} insights`);
|
|
27
|
+
return insightsCreated;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
private findReusableModules(): number {
|
|
31
|
+
const projects = this.projectRepo.getAll();
|
|
32
|
+
if (projects.length < 2) return 0;
|
|
33
|
+
|
|
34
|
+
// Collect all modules grouped by fingerprint
|
|
35
|
+
const byFingerprint = new Map<string, Array<{ projectId: number; projectName: string; moduleId: number; moduleName: string }>>();
|
|
36
|
+
|
|
37
|
+
for (const project of projects) {
|
|
38
|
+
const modules = this.codeModuleRepo.findByProject(project.id);
|
|
39
|
+
for (const mod of modules) {
|
|
40
|
+
const existing = byFingerprint.get(mod.fingerprint) ?? [];
|
|
41
|
+
existing.push({
|
|
42
|
+
projectId: project.id,
|
|
43
|
+
projectName: project.name,
|
|
44
|
+
moduleId: mod.id,
|
|
45
|
+
moduleName: mod.name,
|
|
46
|
+
});
|
|
47
|
+
byFingerprint.set(mod.fingerprint, existing);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let count = 0;
|
|
52
|
+
const expiresAt = new Date(Date.now() + this.config.insightExpiryDays * 86400000).toISOString();
|
|
53
|
+
|
|
54
|
+
for (const [fingerprint, usages] of byFingerprint) {
|
|
55
|
+
const uniqueProjects = new Set(usages.map(u => u.projectId));
|
|
56
|
+
if (uniqueProjects.size >= this.config.templateMinAdaptations) {
|
|
57
|
+
const moduleName = usages[0]!.moduleName;
|
|
58
|
+
this.insightRepo.create({
|
|
59
|
+
type: 'pattern',
|
|
60
|
+
title: `Shared module candidate: ${moduleName}`,
|
|
61
|
+
description: `Module "${moduleName}" appears identically in ${uniqueProjects.size} projects. Extract as a shared library.`,
|
|
62
|
+
evidence: JSON.stringify({
|
|
63
|
+
fingerprint,
|
|
64
|
+
projectCount: uniqueProjects.size,
|
|
65
|
+
usages: usages.map(u => ({ project: u.projectName, moduleId: u.moduleId })),
|
|
66
|
+
}),
|
|
67
|
+
priority: Math.min(80, 30 + uniqueProjects.size * 15),
|
|
68
|
+
project_id: null,
|
|
69
|
+
active: 1,
|
|
70
|
+
expires_at: expiresAt,
|
|
71
|
+
});
|
|
72
|
+
count++;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return count;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private findTemplatingCandidates(): number {
|
|
80
|
+
const projects = this.projectRepo.getAll();
|
|
81
|
+
if (projects.length < 2) return 0;
|
|
82
|
+
|
|
83
|
+
// Group modules by name across projects (same name, different fingerprint = adaptation)
|
|
84
|
+
const byName = new Map<string, Array<{ projectId: number; projectName: string; moduleId: number; reusability: number }>>();
|
|
85
|
+
|
|
86
|
+
for (const project of projects) {
|
|
87
|
+
const modules = this.codeModuleRepo.findByProject(project.id);
|
|
88
|
+
for (const mod of modules) {
|
|
89
|
+
const existing = byName.get(mod.name) ?? [];
|
|
90
|
+
existing.push({
|
|
91
|
+
projectId: project.id,
|
|
92
|
+
projectName: project.name,
|
|
93
|
+
moduleId: mod.id,
|
|
94
|
+
reusability: mod.reusability_score,
|
|
95
|
+
});
|
|
96
|
+
byName.set(mod.name, existing);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let count = 0;
|
|
101
|
+
const expiresAt = new Date(Date.now() + this.config.insightExpiryDays * 86400000).toISOString();
|
|
102
|
+
|
|
103
|
+
for (const [name, usages] of byName) {
|
|
104
|
+
const uniqueProjects = new Set(usages.map(u => u.projectId));
|
|
105
|
+
if (uniqueProjects.size >= this.config.templateMinAdaptations) {
|
|
106
|
+
const avgReusability = usages.reduce((sum, u) => sum + u.reusability, 0) / usages.length;
|
|
107
|
+
if (avgReusability >= 0.5) {
|
|
108
|
+
this.insightRepo.create({
|
|
109
|
+
type: 'suggestion',
|
|
110
|
+
title: `Template candidate: ${name}`,
|
|
111
|
+
description: `"${name}" exists in ${uniqueProjects.size} projects as adaptations (avg reusability ${Math.round(avgReusability * 100)}%). Generalize into parameterized template.`,
|
|
112
|
+
evidence: JSON.stringify({
|
|
113
|
+
name,
|
|
114
|
+
projectCount: uniqueProjects.size,
|
|
115
|
+
avgReusability,
|
|
116
|
+
usages: usages.map(u => ({ project: u.projectName, moduleId: u.moduleId })),
|
|
117
|
+
}),
|
|
118
|
+
priority: Math.min(75, 25 + uniqueProjects.size * 10 + Math.round(avgReusability * 20)),
|
|
119
|
+
project_id: null,
|
|
120
|
+
active: 1,
|
|
121
|
+
expires_at: expiresAt,
|
|
122
|
+
});
|
|
123
|
+
count++;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return count;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import type { ResearchConfig } from '../types/config.types.js';
|
|
2
|
+
import type { ErrorRepository } from '../db/repositories/error.repository.js';
|
|
3
|
+
import type { SolutionRepository } from '../db/repositories/solution.repository.js';
|
|
4
|
+
import type { ProjectRepository } from '../db/repositories/project.repository.js';
|
|
5
|
+
import type { InsightRepository } from '../db/repositories/insight.repository.js';
|
|
6
|
+
import { getLogger } from '../utils/logger.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Analyzes error frequency and solution success rate trends over time windows.
|
|
10
|
+
*/
|
|
11
|
+
export class TrendAnalyzer {
|
|
12
|
+
private logger = getLogger();
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
private errorRepo: ErrorRepository,
|
|
16
|
+
private solutionRepo: SolutionRepository,
|
|
17
|
+
private projectRepo: ProjectRepository,
|
|
18
|
+
private insightRepo: InsightRepository,
|
|
19
|
+
private config: ResearchConfig,
|
|
20
|
+
) {}
|
|
21
|
+
|
|
22
|
+
analyze(): number {
|
|
23
|
+
const projects = this.projectRepo.getAll();
|
|
24
|
+
let insightsCreated = 0;
|
|
25
|
+
|
|
26
|
+
for (const project of projects) {
|
|
27
|
+
insightsCreated += this.analyzeErrorTrend(project.id, project.name);
|
|
28
|
+
insightsCreated += this.analyzeSolutionTrend(project.id, project.name);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
this.logger.info(`Trend analysis complete: ${insightsCreated} insights`);
|
|
32
|
+
return insightsCreated;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
private analyzeErrorTrend(projectId: number, projectName: string): number {
|
|
36
|
+
const windowDays = this.config.trendWindowDays;
|
|
37
|
+
const now = Date.now();
|
|
38
|
+
const recentSince = new Date(now - windowDays * 86400000).toISOString();
|
|
39
|
+
const previousSince = new Date(now - windowDays * 2 * 86400000).toISOString();
|
|
40
|
+
|
|
41
|
+
const recentErrors = this.errorRepo.countSince(recentSince, projectId);
|
|
42
|
+
const allSinceDouble = this.errorRepo.countSince(previousSince, projectId);
|
|
43
|
+
const previousErrors = allSinceDouble - recentErrors;
|
|
44
|
+
|
|
45
|
+
if (previousErrors <= 0) return 0;
|
|
46
|
+
|
|
47
|
+
const changeRate = (recentErrors - previousErrors) / previousErrors;
|
|
48
|
+
const expiresAt = new Date(Date.now() + this.config.insightExpiryDays * 86400000).toISOString();
|
|
49
|
+
|
|
50
|
+
if (changeRate > 0.5) {
|
|
51
|
+
this.insightRepo.create({
|
|
52
|
+
type: 'warning',
|
|
53
|
+
title: `Error rate rising in ${projectName}`,
|
|
54
|
+
description: `${Math.round(changeRate * 100)}% more errors in the last ${windowDays} days vs previous window. Check recent changes: new dependencies, refactoring, or API changes.`,
|
|
55
|
+
evidence: JSON.stringify({ recentErrors, previousErrors, changeRate, windowDays }),
|
|
56
|
+
priority: Math.min(90, 50 + Math.round(changeRate * 30)),
|
|
57
|
+
project_id: projectId,
|
|
58
|
+
active: 1,
|
|
59
|
+
expires_at: expiresAt,
|
|
60
|
+
});
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (changeRate < -0.3) {
|
|
65
|
+
this.insightRepo.create({
|
|
66
|
+
type: 'pattern',
|
|
67
|
+
title: `Error rate declining in ${projectName}`,
|
|
68
|
+
description: `${Math.round(-changeRate * 100)}% fewer errors in the last ${windowDays} days. Whatever you're doing is working.`,
|
|
69
|
+
evidence: JSON.stringify({ recentErrors, previousErrors, changeRate, windowDays }),
|
|
70
|
+
priority: 20,
|
|
71
|
+
project_id: projectId,
|
|
72
|
+
active: 1,
|
|
73
|
+
expires_at: expiresAt,
|
|
74
|
+
});
|
|
75
|
+
return 1;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return 0;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
private analyzeSolutionTrend(projectId: number, projectName: string): number {
|
|
82
|
+
const errors = this.errorRepo.findByProject(projectId);
|
|
83
|
+
if (errors.length < this.config.minDataPoints) return 0;
|
|
84
|
+
|
|
85
|
+
let totalRate = 0;
|
|
86
|
+
let recentRate = 0;
|
|
87
|
+
let totalCount = 0;
|
|
88
|
+
let recentCount = 0;
|
|
89
|
+
const recentCutoff = new Date(Date.now() - this.config.trendWindowDays * 86400000).toISOString();
|
|
90
|
+
|
|
91
|
+
for (const error of errors) {
|
|
92
|
+
const solutions = this.solutionRepo.findForError(error.id);
|
|
93
|
+
for (const sol of solutions) {
|
|
94
|
+
const rate = this.solutionRepo.successRate(sol.id);
|
|
95
|
+
totalRate += rate;
|
|
96
|
+
totalCount++;
|
|
97
|
+
|
|
98
|
+
if (sol.updated_at >= recentCutoff) {
|
|
99
|
+
recentRate += rate;
|
|
100
|
+
recentCount++;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (totalCount === 0 || recentCount === 0) return 0;
|
|
106
|
+
|
|
107
|
+
const avgTotal = totalRate / totalCount;
|
|
108
|
+
const avgRecent = recentRate / recentCount;
|
|
109
|
+
|
|
110
|
+
if (avgRecent < avgTotal - 0.15 && totalCount >= this.config.minDataPoints) {
|
|
111
|
+
const expiresAt = new Date(Date.now() + this.config.insightExpiryDays * 86400000).toISOString();
|
|
112
|
+
this.insightRepo.create({
|
|
113
|
+
type: 'warning',
|
|
114
|
+
title: `Solution quality declining in ${projectName}`,
|
|
115
|
+
description: `Recent success rate ${Math.round(avgRecent * 100)}% vs overall ${Math.round(avgTotal * 100)}%. Solutions are becoming less effective.`,
|
|
116
|
+
evidence: JSON.stringify({ avgRecent, avgTotal, recentCount, totalCount }),
|
|
117
|
+
priority: 60,
|
|
118
|
+
project_id: projectId,
|
|
119
|
+
active: 1,
|
|
120
|
+
expires_at: expiresAt,
|
|
121
|
+
});
|
|
122
|
+
return 1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return 0;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ErrorRepository } from '../db/repositories/error.repository.js';
|
|
2
|
+
import type { SolutionRepository } from '../db/repositories/solution.repository.js';
|
|
3
|
+
import type { CodeModuleRepository } from '../db/repositories/code-module.repository.js';
|
|
4
|
+
import type { RuleRepository } from '../db/repositories/rule.repository.js';
|
|
5
|
+
import type { AntipatternRepository } from '../db/repositories/antipattern.repository.js';
|
|
6
|
+
import type { InsightRepository } from '../db/repositories/insight.repository.js';
|
|
7
|
+
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
8
|
+
import type { NetworkStats } from '../types/synapse.types.js';
|
|
9
|
+
|
|
10
|
+
export interface ProjectSummary {
|
|
11
|
+
errors: { total: number; unresolved: number; last7d: number };
|
|
12
|
+
solutions: { total: number };
|
|
13
|
+
rules: { active: number };
|
|
14
|
+
antipatterns: { total: number };
|
|
15
|
+
modules: { total: number };
|
|
16
|
+
insights: { active: number };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface NetworkOverview {
|
|
20
|
+
stats: NetworkStats;
|
|
21
|
+
strongestSynapses: Array<{
|
|
22
|
+
id: number;
|
|
23
|
+
source: string;
|
|
24
|
+
target: string;
|
|
25
|
+
type: string;
|
|
26
|
+
weight: number;
|
|
27
|
+
}>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class AnalyticsService {
|
|
31
|
+
constructor(
|
|
32
|
+
private errorRepo: ErrorRepository,
|
|
33
|
+
private solutionRepo: SolutionRepository,
|
|
34
|
+
private codeModuleRepo: CodeModuleRepository,
|
|
35
|
+
private ruleRepo: RuleRepository,
|
|
36
|
+
private antipatternRepo: AntipatternRepository,
|
|
37
|
+
private insightRepo: InsightRepository,
|
|
38
|
+
private synapseManager: SynapseManager,
|
|
39
|
+
) {}
|
|
40
|
+
|
|
41
|
+
getSummary(projectId?: number): ProjectSummary {
|
|
42
|
+
const sevenDaysAgo = new Date(Date.now() - 7 * 86400000).toISOString();
|
|
43
|
+
|
|
44
|
+
const unresolvedErrors = this.errorRepo.findUnresolved(projectId);
|
|
45
|
+
const allErrors = projectId ? this.errorRepo.findByProject(projectId) : [];
|
|
46
|
+
const last7dCount = this.errorRepo.countSince(sevenDaysAgo, projectId);
|
|
47
|
+
|
|
48
|
+
const rules = this.ruleRepo.findActive(projectId);
|
|
49
|
+
const antipatterns = projectId
|
|
50
|
+
? this.antipatternRepo.findByProject(projectId)
|
|
51
|
+
: this.antipatternRepo.findGlobal();
|
|
52
|
+
|
|
53
|
+
const moduleCount = projectId
|
|
54
|
+
? this.codeModuleRepo.findByProject(projectId).length
|
|
55
|
+
: this.codeModuleRepo.countAll();
|
|
56
|
+
const insights = this.insightRepo.findActive(projectId);
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
errors: {
|
|
60
|
+
total: allErrors.length,
|
|
61
|
+
unresolved: unresolvedErrors.length,
|
|
62
|
+
last7d: last7dCount,
|
|
63
|
+
},
|
|
64
|
+
solutions: { total: 0 }, // solutions are global, not per-project
|
|
65
|
+
rules: { active: rules.length },
|
|
66
|
+
antipatterns: { total: antipatterns.length },
|
|
67
|
+
modules: { total: moduleCount },
|
|
68
|
+
insights: { active: insights.length },
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
getNetworkOverview(limit: number = 10): NetworkOverview {
|
|
73
|
+
const stats = this.synapseManager.getNetworkStats();
|
|
74
|
+
const strongest = this.synapseManager.getStrongestSynapses(limit);
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
stats,
|
|
78
|
+
strongestSynapses: strongest.map(s => ({
|
|
79
|
+
id: s.id,
|
|
80
|
+
source: `${s.source_type}:${s.source_id}`,
|
|
81
|
+
target: `${s.target_type}:${s.target_id}`,
|
|
82
|
+
type: s.synapse_type,
|
|
83
|
+
weight: s.weight,
|
|
84
|
+
})),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import type { CodeModuleRecord } from '../types/code.types.js';
|
|
2
|
+
import type { CodeModuleRepository } from '../db/repositories/code-module.repository.js';
|
|
3
|
+
import type { ProjectRepository } from '../db/repositories/project.repository.js';
|
|
4
|
+
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
5
|
+
import { analyzeCode } from '../code/analyzer.js';
|
|
6
|
+
import { fingerprintCode } from '../code/fingerprint.js';
|
|
7
|
+
import { computeReusabilityScore } from '../code/scorer.js';
|
|
8
|
+
import { detectGranularity } from '../code/registry.js';
|
|
9
|
+
import { findExactMatches, findSemanticMatches } from '../code/matcher.js';
|
|
10
|
+
import { sha256 } from '../utils/hash.js';
|
|
11
|
+
import { getEventBus } from '../utils/events.js';
|
|
12
|
+
import { getLogger } from '../utils/logger.js';
|
|
13
|
+
|
|
14
|
+
export interface AnalyzeInput {
|
|
15
|
+
project: string;
|
|
16
|
+
name: string;
|
|
17
|
+
filePath: string;
|
|
18
|
+
language: string;
|
|
19
|
+
source: string;
|
|
20
|
+
description?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface FindReusableInput {
|
|
24
|
+
query?: string;
|
|
25
|
+
language?: string;
|
|
26
|
+
projectId?: number;
|
|
27
|
+
limit?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class CodeService {
|
|
31
|
+
private logger = getLogger();
|
|
32
|
+
private eventBus = getEventBus();
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
private codeModuleRepo: CodeModuleRepository,
|
|
36
|
+
private projectRepo: ProjectRepository,
|
|
37
|
+
private synapseManager: SynapseManager,
|
|
38
|
+
) {}
|
|
39
|
+
|
|
40
|
+
analyzeAndRegister(input: AnalyzeInput): { moduleId: number; isNew: boolean; reusabilityScore: number } {
|
|
41
|
+
// Ensure project exists
|
|
42
|
+
let project = this.projectRepo.findByName(input.project);
|
|
43
|
+
if (!project) {
|
|
44
|
+
const id = this.projectRepo.create({ name: input.project, path: null, language: input.language, framework: null });
|
|
45
|
+
project = this.projectRepo.getById(id)!;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Analyze the code
|
|
49
|
+
const analysis = analyzeCode(input.source, input.language);
|
|
50
|
+
const fingerprint = fingerprintCode(input.source, input.language);
|
|
51
|
+
const sourceHash = sha256(input.source);
|
|
52
|
+
|
|
53
|
+
// Check if module already exists (by fingerprint)
|
|
54
|
+
const existing = this.codeModuleRepo.findByFingerprint(fingerprint);
|
|
55
|
+
if (existing) {
|
|
56
|
+
this.codeModuleRepo.update(existing.id, {
|
|
57
|
+
source_hash: sourceHash,
|
|
58
|
+
updated_at: new Date().toISOString(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.synapseManager.strengthen(
|
|
62
|
+
{ type: 'code_module', id: existing.id },
|
|
63
|
+
{ type: 'project', id: project.id },
|
|
64
|
+
'uses_module',
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return { moduleId: existing.id, isNew: false, reusabilityScore: existing.reusability_score };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Compute reusability score
|
|
71
|
+
const reusabilityScore = computeReusabilityScore({
|
|
72
|
+
source: input.source,
|
|
73
|
+
filePath: input.filePath,
|
|
74
|
+
exports: analysis.exports,
|
|
75
|
+
internalDeps: analysis.internalDeps,
|
|
76
|
+
hasTypeAnnotations: analysis.hasTypeAnnotations,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const granularity = detectGranularity(input.source, input.language);
|
|
80
|
+
|
|
81
|
+
const moduleId = this.codeModuleRepo.create({
|
|
82
|
+
project_id: project.id,
|
|
83
|
+
name: input.name,
|
|
84
|
+
file_path: input.filePath,
|
|
85
|
+
language: input.language,
|
|
86
|
+
fingerprint,
|
|
87
|
+
description: input.description ?? null,
|
|
88
|
+
source_hash: sourceHash,
|
|
89
|
+
lines_of_code: analysis.linesOfCode,
|
|
90
|
+
complexity: null,
|
|
91
|
+
reusability_score: reusabilityScore,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Create synapse: module ↔ project
|
|
95
|
+
this.synapseManager.strengthen(
|
|
96
|
+
{ type: 'code_module', id: moduleId },
|
|
97
|
+
{ type: 'project', id: project.id },
|
|
98
|
+
'uses_module',
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
this.eventBus.emit('module:registered', { moduleId, projectId: project.id });
|
|
102
|
+
this.logger.info(`Code module registered (id=${moduleId}, name=${input.name}, granularity=${granularity}, score=${reusabilityScore.toFixed(2)})`);
|
|
103
|
+
|
|
104
|
+
return { moduleId, isNew: true, reusabilityScore };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
findReusable(input: FindReusableInput): CodeModuleRecord[] {
|
|
108
|
+
if (input.query) {
|
|
109
|
+
return this.codeModuleRepo.search(input.query);
|
|
110
|
+
}
|
|
111
|
+
if (input.language) {
|
|
112
|
+
return this.codeModuleRepo.findByLanguage(input.language, input.limit);
|
|
113
|
+
}
|
|
114
|
+
if (input.projectId) {
|
|
115
|
+
return this.codeModuleRepo.findByProject(input.projectId);
|
|
116
|
+
}
|
|
117
|
+
return [];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
checkSimilarity(source: string, language: string): Array<{ moduleId: number; score: number; matchType: string }> {
|
|
121
|
+
const fingerprint = fingerprintCode(source, language);
|
|
122
|
+
const allModules = this.codeModuleRepo.findByLanguage(language);
|
|
123
|
+
|
|
124
|
+
const exact = findExactMatches(fingerprint, allModules);
|
|
125
|
+
if (exact.length > 0) return exact;
|
|
126
|
+
|
|
127
|
+
return findSemanticMatches(source, allModules, 0.5);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
listModules(projectId?: number): CodeModuleRecord[] {
|
|
131
|
+
if (projectId) {
|
|
132
|
+
return this.codeModuleRepo.findByProject(projectId);
|
|
133
|
+
}
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
getById(id: number): CodeModuleRecord | undefined {
|
|
138
|
+
return this.codeModuleRepo.getById(id);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { ErrorRecord } from '../types/error.types.js';
|
|
2
|
+
import type { ErrorRepository } from '../db/repositories/error.repository.js';
|
|
3
|
+
import type { ProjectRepository } from '../db/repositories/project.repository.js';
|
|
4
|
+
import type { SynapseManager } from '../synapses/synapse-manager.js';
|
|
5
|
+
import { parseError } from '../parsing/error-parser.js';
|
|
6
|
+
import { generateFingerprint } from '../matching/fingerprint.js';
|
|
7
|
+
import { matchError, type MatchResult } from '../matching/error-matcher.js';
|
|
8
|
+
import { getEventBus } from '../utils/events.js';
|
|
9
|
+
import { getLogger } from '../utils/logger.js';
|
|
10
|
+
|
|
11
|
+
export interface ReportErrorInput {
|
|
12
|
+
project: string;
|
|
13
|
+
errorOutput: string;
|
|
14
|
+
filePath?: string;
|
|
15
|
+
terminalId?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface ErrorQueryInput {
|
|
19
|
+
projectId?: number;
|
|
20
|
+
resolved?: boolean;
|
|
21
|
+
search?: string;
|
|
22
|
+
limit?: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class ErrorService {
|
|
26
|
+
private logger = getLogger();
|
|
27
|
+
private eventBus = getEventBus();
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
private errorRepo: ErrorRepository,
|
|
31
|
+
private projectRepo: ProjectRepository,
|
|
32
|
+
private synapseManager: SynapseManager,
|
|
33
|
+
) {}
|
|
34
|
+
|
|
35
|
+
report(input: ReportErrorInput): { errorId: number; isNew: boolean; matches: MatchResult[] } {
|
|
36
|
+
// 1. Ensure project exists
|
|
37
|
+
let project = this.projectRepo.findByName(input.project);
|
|
38
|
+
if (!project) {
|
|
39
|
+
const id = this.projectRepo.create({ name: input.project, path: null, language: null, framework: null });
|
|
40
|
+
project = this.projectRepo.getById(id)!;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// 2. Parse the error
|
|
44
|
+
const parsed = parseError(input.errorOutput);
|
|
45
|
+
if (!parsed) {
|
|
46
|
+
this.logger.warn('Could not parse error output');
|
|
47
|
+
const errorId = this.errorRepo.create({
|
|
48
|
+
project_id: project.id,
|
|
49
|
+
terminal_id: input.terminalId ?? null,
|
|
50
|
+
fingerprint: '',
|
|
51
|
+
type: 'UnknownError',
|
|
52
|
+
message: input.errorOutput.split('\n')[0] ?? input.errorOutput,
|
|
53
|
+
raw_output: input.errorOutput,
|
|
54
|
+
context: null,
|
|
55
|
+
file_path: input.filePath ?? null,
|
|
56
|
+
line_number: null,
|
|
57
|
+
column_number: null,
|
|
58
|
+
});
|
|
59
|
+
return { errorId, isNew: true, matches: [] };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 3. Generate fingerprint
|
|
63
|
+
const fingerprint = generateFingerprint(parsed.errorType, parsed.message, parsed.frames);
|
|
64
|
+
|
|
65
|
+
// 4. Check for existing error with same fingerprint
|
|
66
|
+
const existing = this.errorRepo.findByFingerprint(fingerprint);
|
|
67
|
+
if (existing.length > 0) {
|
|
68
|
+
const err = existing[0]!;
|
|
69
|
+
this.errorRepo.incrementOccurrence(err.id);
|
|
70
|
+
this.logger.info(`Known error (id=${err.id}), occurrence incremented`);
|
|
71
|
+
|
|
72
|
+
// Strengthen synapse
|
|
73
|
+
this.synapseManager.strengthen(
|
|
74
|
+
{ type: 'error', id: err.id },
|
|
75
|
+
{ type: 'project', id: project.id },
|
|
76
|
+
'co_occurs',
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
return { errorId: err.id, isNew: false, matches: [] };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 5. Create new error record
|
|
83
|
+
const errorId = this.errorRepo.create({
|
|
84
|
+
project_id: project.id,
|
|
85
|
+
terminal_id: input.terminalId ?? null,
|
|
86
|
+
fingerprint,
|
|
87
|
+
type: parsed.errorType,
|
|
88
|
+
message: parsed.message,
|
|
89
|
+
raw_output: input.errorOutput,
|
|
90
|
+
context: null,
|
|
91
|
+
file_path: parsed.sourceFile ?? input.filePath ?? null,
|
|
92
|
+
line_number: parsed.sourceLine ?? null,
|
|
93
|
+
column_number: null,
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
// 6. Create synapse: error ↔ project
|
|
97
|
+
this.synapseManager.strengthen(
|
|
98
|
+
{ type: 'error', id: errorId },
|
|
99
|
+
{ type: 'project', id: project.id },
|
|
100
|
+
'co_occurs',
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// 7. Find similar errors
|
|
104
|
+
const candidates = this.errorRepo.findByProject(project.id)
|
|
105
|
+
.filter(e => e.id !== errorId);
|
|
106
|
+
const newError = this.errorRepo.getById(errorId)!;
|
|
107
|
+
const matches = matchError(newError, candidates);
|
|
108
|
+
|
|
109
|
+
// 8. Create similarity synapses for strong matches
|
|
110
|
+
for (const match of matches.filter(m => m.isStrong)) {
|
|
111
|
+
this.synapseManager.strengthen(
|
|
112
|
+
{ type: 'error', id: errorId },
|
|
113
|
+
{ type: 'error', id: match.errorId },
|
|
114
|
+
'similar_to',
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.eventBus.emit('error:reported', { errorId, projectId: project.id, fingerprint });
|
|
119
|
+
this.logger.info(`New error reported (id=${errorId}, type=${parsed.errorType})`);
|
|
120
|
+
|
|
121
|
+
return { errorId, isNew: true, matches };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
query(input: ErrorQueryInput): ErrorRecord[] {
|
|
125
|
+
if (input.search) {
|
|
126
|
+
return this.errorRepo.search(input.search);
|
|
127
|
+
}
|
|
128
|
+
if (input.resolved === false) {
|
|
129
|
+
return this.errorRepo.findUnresolved(input.projectId);
|
|
130
|
+
}
|
|
131
|
+
if (input.projectId) {
|
|
132
|
+
return this.errorRepo.findByProject(input.projectId);
|
|
133
|
+
}
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
matchSimilar(errorId: number): MatchResult[] {
|
|
138
|
+
const error = this.errorRepo.getById(errorId);
|
|
139
|
+
if (!error) return [];
|
|
140
|
+
|
|
141
|
+
const candidates = this.errorRepo.findByProject(error.project_id)
|
|
142
|
+
.filter(e => e.id !== errorId);
|
|
143
|
+
return matchError(error, candidates);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
resolve(errorId: number, solutionId?: number): void {
|
|
147
|
+
this.errorRepo.update(errorId, {
|
|
148
|
+
resolved: 1,
|
|
149
|
+
resolved_at: new Date().toISOString(),
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (solutionId) {
|
|
153
|
+
this.eventBus.emit('error:resolved', { errorId, solutionId });
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
getById(id: number): ErrorRecord | undefined {
|
|
158
|
+
return this.errorRepo.getById(id);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
countSince(since: string, projectId?: number): number {
|
|
162
|
+
return this.errorRepo.countSince(since, projectId);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { NotificationRepository, NotificationRecord } from '../db/repositories/notification.repository.js';
|
|
2
|
+
import { getLogger } from '../utils/logger.js';
|
|
3
|
+
|
|
4
|
+
export interface CreateNotificationInput {
|
|
5
|
+
type: string;
|
|
6
|
+
title: string;
|
|
7
|
+
message: string;
|
|
8
|
+
priority?: number;
|
|
9
|
+
projectId?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class NotificationService {
|
|
13
|
+
private logger = getLogger();
|
|
14
|
+
|
|
15
|
+
constructor(private notificationRepo: NotificationRepository) {}
|
|
16
|
+
|
|
17
|
+
create(input: CreateNotificationInput): number {
|
|
18
|
+
const id = this.notificationRepo.create({
|
|
19
|
+
type: input.type,
|
|
20
|
+
title: input.title,
|
|
21
|
+
message: input.message,
|
|
22
|
+
priority: input.priority ?? 0,
|
|
23
|
+
project_id: input.projectId ?? null,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
this.logger.info(`Notification created (id=${id}, type=${input.type})`);
|
|
27
|
+
return id;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
list(projectId?: number): NotificationRecord[] {
|
|
31
|
+
return this.notificationRepo.findUnacknowledged(projectId);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
acknowledge(id: number): void {
|
|
35
|
+
this.notificationRepo.acknowledge(id);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getById(id: number): NotificationRecord | undefined {
|
|
39
|
+
return this.notificationRepo.getById(id);
|
|
40
|
+
}
|
|
41
|
+
}
|