claude-brain 0.30.2 → 0.30.3
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/README.md +241 -191
- package/VERSION +1 -1
- package/assets/CLAUDE-unified.md +11 -11
- package/assets/CLAUDE.md +29 -29
- package/package.json +7 -3
- package/packs/backend/node.json +173 -173
- package/packs/core/javascript.json +176 -176
- package/packs/core/typescript.json +222 -222
- package/packs/frontend/react.json +254 -254
- package/packs/meta/testing.json +172 -172
- package/scripts/postinstall.mjs +531 -531
- package/src/automation/decision-detector.ts +452 -452
- package/src/automation/phase12-manager.ts +456 -456
- package/src/automation/proactive-recall.ts +373 -373
- package/src/automation/project-detector.ts +310 -310
- package/src/automation/repo-scanner.ts +210 -205
- package/src/cli/auto-setup.ts +75 -75
- package/src/cli/auto-start.ts +266 -266
- package/src/cli/bin.ts +264 -264
- package/src/cli/commands/autostart.ts +90 -90
- package/src/cli/commands/chroma.ts +578 -577
- package/src/cli/commands/export-training.ts +70 -70
- package/src/cli/commands/export.ts +130 -130
- package/src/cli/commands/git-hook.ts +183 -183
- package/src/cli/commands/hooks.ts +217 -217
- package/src/cli/commands/init.ts +123 -123
- package/src/cli/commands/install-mcp.ts +122 -111
- package/src/cli/commands/models.ts +979 -979
- package/src/cli/commands/pack.ts +200 -200
- package/src/cli/commands/refresh.ts +344 -339
- package/src/cli/commands/reindex.ts +120 -120
- package/src/cli/commands/serve.ts +466 -463
- package/src/cli/commands/start.ts +44 -44
- package/src/cli/commands/status.ts +220 -203
- package/src/cli/commands/uninstall-mcp.ts +45 -41
- package/src/cli/commands/update.ts +130 -124
- package/src/cli/migrate-chroma.ts +106 -106
- package/src/cli/ui/animations.ts +80 -80
- package/src/cli/ui/components.ts +82 -82
- package/src/cli/ui/index.ts +4 -4
- package/src/cli/ui/logo.ts +36 -36
- package/src/cli/ui/theme.ts +55 -55
- package/src/code-intelligence/indexer.ts +352 -352
- package/src/code-intelligence/linker.ts +178 -178
- package/src/code-intelligence/parser.ts +484 -484
- package/src/code-intelligence/query.ts +291 -291
- package/src/code-intelligence/schema.ts +83 -83
- package/src/code-intelligence/types.ts +95 -95
- package/src/config/defaults.ts +52 -52
- package/src/config/home.ts +56 -56
- package/src/config/index.ts +5 -5
- package/src/config/loader.ts +192 -192
- package/src/config/schema.ts +446 -415
- package/src/config/validator.ts +182 -182
- package/src/context/assembler.ts +407 -400
- package/src/context/index.ts +79 -79
- package/src/context/progress-tracker.ts +174 -174
- package/src/context/standards-manager.ts +287 -287
- package/src/context/validator.ts +58 -58
- package/src/diagnostics/index.ts +122 -121
- package/src/health/index.ts +233 -232
- package/src/hooks/brain-hook.ts +134 -131
- package/src/hooks/capture.ts +168 -168
- package/src/hooks/claude-code-mastery.md +112 -112
- package/src/hooks/context-hook.ts +260 -245
- package/src/hooks/deduplicator.ts +72 -72
- package/src/hooks/git-capture.ts +109 -109
- package/src/hooks/git-hook-installer.ts +211 -207
- package/src/hooks/index.ts +20 -20
- package/src/hooks/installer.ts +306 -288
- package/src/hooks/interceptor-hook.ts +204 -201
- package/src/hooks/passive-classifier.ts +397 -397
- package/src/hooks/queue.ts +160 -129
- package/src/hooks/session-tracker.ts +312 -312
- package/src/hooks/types.ts +52 -52
- package/src/index.ts +7 -7
- package/src/intelligence/cross-project/generalizer.ts +283 -283
- package/src/intelligence/cross-project/index.ts +7 -7
- package/src/intelligence/hf-downloader.ts +222 -222
- package/src/intelligence/hf-manifest.json +78 -78
- package/src/intelligence/index.ts +24 -24
- package/src/intelligence/inference-router.ts +762 -762
- package/src/intelligence/model-manager.ts +263 -245
- package/src/intelligence/optimization/index.ts +10 -10
- package/src/intelligence/optimization/precompute.ts +202 -202
- package/src/intelligence/optimization/semantic-cache.ts +213 -207
- package/src/intelligence/prediction/index.ts +7 -7
- package/src/intelligence/prediction/recommender.ts +276 -268
- package/src/intelligence/reasoning/chain-retrieval.ts +243 -247
- package/src/intelligence/reasoning/index.ts +7 -7
- package/src/intelligence/temporal/evolution.ts +193 -197
- package/src/intelligence/temporal/index.ts +16 -16
- package/src/intelligence/temporal/query-processor.ts +190 -190
- package/src/intelligence/temporal/timeline.ts +272 -259
- package/src/intelligence/temporal/trends.ts +263 -263
- package/src/intelligence/tokenizer.ts +118 -118
- package/src/knowledge/entity-extractor.ts +447 -443
- package/src/knowledge/graph/builder.ts +185 -185
- package/src/knowledge/graph/linker.ts +201 -201
- package/src/knowledge/graph/memory-graph.ts +359 -359
- package/src/knowledge/graph/schema.ts +99 -99
- package/src/knowledge/graph/search.ts +166 -166
- package/src/knowledge/relationship-extractor.ts +108 -108
- package/src/memory/chroma/client.ts +211 -192
- package/src/memory/chroma/collection-manager.ts +92 -92
- package/src/memory/chroma/config.ts +57 -57
- package/src/memory/chroma/embeddings.ts +177 -175
- package/src/memory/chroma/index.ts +82 -82
- package/src/memory/chroma/migration.ts +270 -270
- package/src/memory/chroma/schemas.ts +69 -69
- package/src/memory/chroma/search.ts +319 -315
- package/src/memory/chroma/store.ts +755 -747
- package/src/memory/compression.ts +121 -121
- package/src/memory/consolidation/archiver.ts +162 -165
- package/src/memory/consolidation/merger.ts +182 -186
- package/src/memory/consolidation/scorer.ts +136 -136
- package/src/memory/database.ts +9 -0
- package/src/memory/dual-write.ts +145 -0
- package/src/memory/embeddings.ts +226 -226
- package/src/memory/episodic/detector.ts +108 -108
- package/src/memory/episodic/manager.ts +347 -351
- package/src/memory/episodic/summarizer.ts +179 -179
- package/src/memory/episodic/types.ts +52 -52
- package/src/memory/fts5-search.ts +692 -633
- package/src/memory/index.ts +943 -1060
- package/src/memory/migrations/add-fts5.ts +118 -108
- package/src/memory/patterns.ts +438 -438
- package/src/memory/pruning.ts +60 -60
- package/src/memory/schema.ts +88 -88
- package/src/memory/store.ts +911 -787
- package/src/orchestrator/handlers/decision-handler.ts +204 -204
- package/src/packs/index.ts +9 -9
- package/src/packs/loader.ts +134 -134
- package/src/packs/manager.ts +204 -204
- package/src/packs/ranker.ts +78 -78
- package/src/packs/types.ts +81 -81
- package/src/phase12/index.ts +5 -5
- package/src/retrieval/bm25/index.ts +300 -297
- package/src/retrieval/bm25/tokenizer.ts +184 -184
- package/src/retrieval/feedback/adaptive.ts +221 -221
- package/src/retrieval/feedback/index.ts +16 -16
- package/src/retrieval/feedback/metrics.ts +221 -221
- package/src/retrieval/feedback/store.ts +283 -283
- package/src/retrieval/fusion/index.ts +194 -194
- package/src/retrieval/fusion/rrf.ts +165 -165
- package/src/retrieval/index.ts +12 -12
- package/src/retrieval/pipeline.ts +375 -375
- package/src/retrieval/query/expander.ts +203 -203
- package/src/retrieval/query/index.ts +27 -27
- package/src/retrieval/query/intent-classifier.ts +252 -252
- package/src/retrieval/query/temporal-parser.ts +295 -295
- package/src/retrieval/reranker/index.ts +189 -188
- package/src/retrieval/reranker/model.ts +99 -95
- package/src/retrieval/service.ts +125 -125
- package/src/retrieval/types.ts +162 -162
- package/src/routing/entity-extractor.ts +454 -454
- package/src/routing/handlers/exploration-handler.ts +369 -0
- package/src/routing/handlers/index.ts +19 -0
- package/src/routing/handlers/memory-handler.ts +273 -0
- package/src/routing/handlers/mutation-handler.ts +241 -0
- package/src/routing/handlers/recall-handler.ts +642 -0
- package/src/routing/handlers/shared.ts +515 -0
- package/src/routing/handlers/types.ts +48 -0
- package/src/routing/intent-classifier.ts +552 -552
- package/src/routing/response-filter.ts +399 -391
- package/src/routing/router.ts +245 -2193
- package/src/routing/search-engine.ts +521 -514
- package/src/routing/types.ts +104 -94
- package/src/scripts/health-check.ts +118 -118
- package/src/scripts/setup.ts +122 -122
- package/src/server/auto-updater.ts +283 -276
- package/src/server/handlers/call-tool.ts +159 -159
- package/src/server/handlers/list-tools.ts +35 -35
- package/src/server/handlers/tools/auto-remember.ts +165 -165
- package/src/server/handlers/tools/brain.ts +86 -86
- package/src/server/handlers/tools/create-project.ts +135 -135
- package/src/server/handlers/tools/get-code-standards.ts +123 -123
- package/src/server/handlers/tools/get-corrections.ts +152 -152
- package/src/server/handlers/tools/get-patterns.ts +156 -156
- package/src/server/handlers/tools/get-project-context.ts +75 -75
- package/src/server/handlers/tools/index.ts +30 -30
- package/src/server/handlers/tools/init-project.ts +756 -756
- package/src/server/handlers/tools/list-projects.ts +126 -126
- package/src/server/handlers/tools/recall-similar.ts +87 -87
- package/src/server/handlers/tools/recognize-pattern.ts +132 -132
- package/src/server/handlers/tools/record-correction.ts +131 -131
- package/src/server/handlers/tools/remember-decision.ts +168 -168
- package/src/server/handlers/tools/schemas.ts +179 -179
- package/src/server/handlers/tools/search-code.ts +122 -122
- package/src/server/handlers/tools/smart-context.ts +146 -146
- package/src/server/handlers/tools/update-progress.ts +131 -131
- package/src/server/http-api.ts +215 -1229
- package/src/server/mcp-proxy.ts +85 -84
- package/src/server/mcp-server.ts +285 -284
- package/src/server/middleware/auth.ts +39 -0
- package/src/server/middleware/error-handler.ts +37 -0
- package/src/server/middleware/rate-limit.ts +53 -0
- package/src/server/middleware/validate.ts +42 -0
- package/src/server/pid-manager.ts +137 -136
- package/src/server/providers/resources.ts +581 -581
- package/src/server/routes/code.ts +228 -0
- package/src/server/routes/context.ts +26 -0
- package/src/server/routes/health.ts +19 -0
- package/src/server/routes/helpers.ts +100 -0
- package/src/server/routes/hooks.ts +197 -0
- package/src/server/routes/mcp.ts +47 -0
- package/src/server/routes/memory.ts +397 -0
- package/src/server/routes/models.ts +96 -0
- package/src/server/routes/projects.ts +89 -0
- package/src/server/routes/types.ts +21 -0
- package/src/server/schemas/api-schemas.ts +202 -0
- package/src/server/services.ts +720 -720
- package/src/server/utils/memory-indicator.ts +84 -84
- package/src/server/utils/response-formatter.ts +129 -129
- package/src/server/web-viewer.ts +1145 -1115
- package/src/setup/index.ts +38 -38
- package/src/tools/registry.ts +115 -115
- package/src/tools/schemas.ts +666 -666
- package/src/tools/types.ts +412 -412
- package/src/training/data-store.ts +320 -298
- package/src/training/retrain-pipeline.ts +399 -394
- package/src/utils/error-handler.ts +136 -136
- package/src/utils/index.ts +58 -58
- package/src/utils/kill-port.ts +55 -53
- package/src/utils/phase12-helper.ts +56 -56
- package/src/utils/safe-path.ts +43 -0
- package/src/utils/timing.ts +47 -47
- package/src/utils/transaction.ts +63 -63
- package/src/vault/index.ts +4 -3
- package/src/vault/paths.ts +106 -106
- package/src/vault/query.ts +4 -1
- package/src/vault/reader.ts +44 -1
- package/src/vault/watcher.ts +24 -1
- package/src/vault/writer.ts +487 -413
- package/skills/persistent-memory/SKILL.md +0 -148
- package/skills/persistent-memory/references/tool-reference.md +0 -90
|
@@ -1,456 +1,456 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Phase 12 Manager
|
|
3
|
-
* Advanced Memory & Intelligent Automation
|
|
4
|
-
*
|
|
5
|
-
* Orchestrates all Phase 12 features:
|
|
6
|
-
* - Pattern Recognition
|
|
7
|
-
* - Learning System
|
|
8
|
-
* - Knowledge Extraction
|
|
9
|
-
* - Auto-Context Loading
|
|
10
|
-
* - Decision Detection
|
|
11
|
-
* - Proactive Recall
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import type { Logger } from 'pino'
|
|
15
|
-
import type { VaultManager } from '@/vault'
|
|
16
|
-
import type { MemoryManager } from '@/memory'
|
|
17
|
-
import type { ContextManager } from '@/context'
|
|
18
|
-
import { PatternRecognizer, type Pattern } from '@/memory/patterns'
|
|
19
|
-
import { LearningSystem, type LearningInsights } from '@/memory/learning'
|
|
20
|
-
import { KnowledgeExtractor, type ExtractedKnowledge } from '@/memory/knowledge-extractor'
|
|
21
|
-
import {
|
|
22
|
-
ProjectDetector,
|
|
23
|
-
AutoContextLoader,
|
|
24
|
-
DecisionDetector,
|
|
25
|
-
ProactiveRecallEngine,
|
|
26
|
-
type DetectedProject,
|
|
27
|
-
type DetectedDecision,
|
|
28
|
-
type RecallResult
|
|
29
|
-
} from '@/automation'
|
|
30
|
-
|
|
31
|
-
export interface Phase12Config {
|
|
32
|
-
/**
|
|
33
|
-
* Enable automatic context loading on startup
|
|
34
|
-
* @default true
|
|
35
|
-
*/
|
|
36
|
-
autoLoadContext: boolean
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Enable automatic decision detection and saving
|
|
40
|
-
* @default true
|
|
41
|
-
*/
|
|
42
|
-
autoDetectDecisions: boolean
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Enable proactive memory recall
|
|
46
|
-
* @default true
|
|
47
|
-
*/
|
|
48
|
-
enableProactiveRecall: boolean
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Minimum confidence for auto-saving decisions
|
|
52
|
-
* @default 0.7
|
|
53
|
-
*/
|
|
54
|
-
decisionConfidenceThreshold: number
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Enable pattern analysis on startup
|
|
58
|
-
* @default false
|
|
59
|
-
*/
|
|
60
|
-
analyzePatternsonStartup: boolean
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Enable knowledge extraction from messages
|
|
64
|
-
* @default true
|
|
65
|
-
*/
|
|
66
|
-
enableKnowledgeExtraction: boolean
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const DEFAULT_CONFIG: Phase12Config = {
|
|
70
|
-
autoLoadContext: true,
|
|
71
|
-
autoDetectDecisions: true,
|
|
72
|
-
enableProactiveRecall: true,
|
|
73
|
-
decisionConfidenceThreshold: 0.7,
|
|
74
|
-
analyzePatternsonStartup: false,
|
|
75
|
-
enableKnowledgeExtraction: true
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
export interface MessageProcessingResult {
|
|
79
|
-
recalledMemories?: RecallResult
|
|
80
|
-
detectedDecision?: DetectedDecision
|
|
81
|
-
extractedKnowledge?: ExtractedKnowledge[]
|
|
82
|
-
detectedProject?: DetectedProject
|
|
83
|
-
decisionSaved?: boolean
|
|
84
|
-
decisionId?: string
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export interface Phase12Stats {
|
|
88
|
-
patterns: {
|
|
89
|
-
total: number
|
|
90
|
-
byType: Record<string, number>
|
|
91
|
-
}
|
|
92
|
-
learning: LearningInsights
|
|
93
|
-
automation: {
|
|
94
|
-
contextLoaded: boolean
|
|
95
|
-
currentProject: string | null
|
|
96
|
-
recallStats: {
|
|
97
|
-
totalRecalls: number
|
|
98
|
-
successfulRecalls: number
|
|
99
|
-
averageRelevance: number
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export class Phase12Manager {
|
|
105
|
-
private logger: Logger
|
|
106
|
-
private config: Phase12Config
|
|
107
|
-
private initialized = false
|
|
108
|
-
|
|
109
|
-
// Advanced Memory
|
|
110
|
-
public readonly patterns: PatternRecognizer
|
|
111
|
-
public readonly learning: LearningSystem
|
|
112
|
-
public readonly knowledge: KnowledgeExtractor
|
|
113
|
-
|
|
114
|
-
// Intelligent Automation
|
|
115
|
-
public readonly projectDetector: ProjectDetector
|
|
116
|
-
public readonly autoContext: AutoContextLoader
|
|
117
|
-
public readonly decisionDetector: DecisionDetector
|
|
118
|
-
public readonly proactiveRecall: ProactiveRecallEngine
|
|
119
|
-
|
|
120
|
-
constructor(
|
|
121
|
-
logger: Logger,
|
|
122
|
-
vault: VaultManager,
|
|
123
|
-
memory: MemoryManager,
|
|
124
|
-
context: ContextManager,
|
|
125
|
-
config: Partial<Phase12Config> = {}
|
|
126
|
-
) {
|
|
127
|
-
this.logger = logger.child({ component: 'phase12-manager' })
|
|
128
|
-
this.config = { ...DEFAULT_CONFIG, ...config }
|
|
129
|
-
|
|
130
|
-
// Initialize advanced memory features
|
|
131
|
-
this.patterns = new PatternRecognizer(logger, memory)
|
|
132
|
-
this.learning = new LearningSystem(logger, memory)
|
|
133
|
-
this.knowledge = new KnowledgeExtractor(logger, memory)
|
|
134
|
-
|
|
135
|
-
// Initialize intelligent automation
|
|
136
|
-
this.projectDetector = new ProjectDetector(logger, vault)
|
|
137
|
-
this.autoContext = new AutoContextLoader(
|
|
138
|
-
logger,
|
|
139
|
-
context,
|
|
140
|
-
this.projectDetector
|
|
141
|
-
)
|
|
142
|
-
this.decisionDetector = new DecisionDetector(logger, memory)
|
|
143
|
-
this.proactiveRecall = new ProactiveRecallEngine(logger, memory)
|
|
144
|
-
|
|
145
|
-
this.logger.info(
|
|
146
|
-
{ config: this.config },
|
|
147
|
-
'Phase 12 Manager created'
|
|
148
|
-
)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Initialize all Phase 12 features
|
|
153
|
-
*/
|
|
154
|
-
async initialize(): Promise<void> {
|
|
155
|
-
if (this.initialized) {
|
|
156
|
-
this.logger.warn('Phase 12 already initialized')
|
|
157
|
-
return
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
this.logger.info('Initializing Phase 12 features...')
|
|
161
|
-
|
|
162
|
-
try {
|
|
163
|
-
// Start auto-context loader
|
|
164
|
-
if (this.config.autoLoadContext) {
|
|
165
|
-
await this.autoContext.initialize()
|
|
166
|
-
this.logger.info('Auto-context loader initialized')
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Run initial pattern analysis if enabled
|
|
170
|
-
if (this.config.analyzePatternsonStartup) {
|
|
171
|
-
const patterns = await this.patterns.analyzePatterns()
|
|
172
|
-
this.logger.info(
|
|
173
|
-
{ patternCount: patterns.length },
|
|
174
|
-
'Initial pattern analysis complete'
|
|
175
|
-
)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
this.initialized = true
|
|
179
|
-
this.logger.info('Phase 12 initialization complete')
|
|
180
|
-
} catch (error) {
|
|
181
|
-
this.logger.error({ error }, 'Phase 12 initialization failed')
|
|
182
|
-
throw error
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Check if Phase 12 is initialized
|
|
188
|
-
*/
|
|
189
|
-
isInitialized(): boolean {
|
|
190
|
-
return this.initialized
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Process incoming message with all Phase 12 features
|
|
195
|
-
*/
|
|
196
|
-
async processMessage(
|
|
197
|
-
message: string,
|
|
198
|
-
project?: string
|
|
199
|
-
): Promise<MessageProcessingResult> {
|
|
200
|
-
const results: MessageProcessingResult = {}
|
|
201
|
-
|
|
202
|
-
// Use detected project if not provided
|
|
203
|
-
const targetProject = project || this.autoContext.getCurrentProject() || undefined
|
|
204
|
-
|
|
205
|
-
// 1. Proactive recall
|
|
206
|
-
if (this.config.enableProactiveRecall) {
|
|
207
|
-
if (this.proactiveRecall.shouldRecall(message)) {
|
|
208
|
-
results.recalledMemories = await this.proactiveRecall.recall(
|
|
209
|
-
message,
|
|
210
|
-
targetProject
|
|
211
|
-
) || undefined
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// 2. Decision detection
|
|
216
|
-
if (this.config.autoDetectDecisions && targetProject) {
|
|
217
|
-
const decision = this.decisionDetector.detectDecision(message, targetProject)
|
|
218
|
-
|
|
219
|
-
if (decision && decision.confidence >= this.config.decisionConfidenceThreshold) {
|
|
220
|
-
results.detectedDecision = decision
|
|
221
|
-
|
|
222
|
-
// Auto-save if confidence is high enough
|
|
223
|
-
const decisionId = await this.decisionDetector.autoSaveDecision(
|
|
224
|
-
decision,
|
|
225
|
-
targetProject
|
|
226
|
-
)
|
|
227
|
-
|
|
228
|
-
if (decisionId) {
|
|
229
|
-
results.decisionSaved = true
|
|
230
|
-
results.decisionId = decisionId
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
// 3. Knowledge extraction
|
|
236
|
-
if (this.config.enableKnowledgeExtraction) {
|
|
237
|
-
const extracted = await this.knowledge.extractFromConversation(
|
|
238
|
-
message,
|
|
239
|
-
targetProject
|
|
240
|
-
)
|
|
241
|
-
|
|
242
|
-
if (extracted.knowledge.length > 0) {
|
|
243
|
-
results.extractedKnowledge = extracted.knowledge
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
// Log processing results
|
|
248
|
-
if (Object.keys(results).length > 0) {
|
|
249
|
-
this.logger.debug(
|
|
250
|
-
{
|
|
251
|
-
hasRecall: !!results.recalledMemories,
|
|
252
|
-
hasDecision: !!results.detectedDecision,
|
|
253
|
-
knowledgeCount: results.extractedKnowledge?.length || 0,
|
|
254
|
-
decisionSaved: results.decisionSaved
|
|
255
|
-
},
|
|
256
|
-
'Message processed with Phase 12 features'
|
|
257
|
-
)
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
return results
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Process outgoing response for decisions
|
|
265
|
-
* Call this with Claude's responses to detect and save decisions
|
|
266
|
-
*/
|
|
267
|
-
async processResponse(
|
|
268
|
-
response: string,
|
|
269
|
-
project?: string
|
|
270
|
-
): Promise<MessageProcessingResult> {
|
|
271
|
-
const results: MessageProcessingResult = {}
|
|
272
|
-
const targetProject = project || this.autoContext.getCurrentProject() || undefined
|
|
273
|
-
|
|
274
|
-
// Decision detection on responses (Claude's decisions)
|
|
275
|
-
if (this.config.autoDetectDecisions && targetProject) {
|
|
276
|
-
const decision = this.decisionDetector.detectDecision(response, targetProject)
|
|
277
|
-
|
|
278
|
-
if (decision && decision.confidence >= this.config.decisionConfidenceThreshold) {
|
|
279
|
-
results.detectedDecision = decision
|
|
280
|
-
|
|
281
|
-
const decisionId = await this.decisionDetector.autoSaveDecision(
|
|
282
|
-
decision,
|
|
283
|
-
targetProject
|
|
284
|
-
)
|
|
285
|
-
|
|
286
|
-
if (decisionId) {
|
|
287
|
-
results.decisionSaved = true
|
|
288
|
-
results.decisionId = decisionId
|
|
289
|
-
this.logger.info(
|
|
290
|
-
{ decisionId, project: targetProject },
|
|
291
|
-
'Decision auto-saved from response'
|
|
292
|
-
)
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
// Knowledge extraction from responses
|
|
298
|
-
if (this.config.enableKnowledgeExtraction) {
|
|
299
|
-
const extracted = await this.knowledge.extractFromConversation(
|
|
300
|
-
response,
|
|
301
|
-
targetProject
|
|
302
|
-
)
|
|
303
|
-
|
|
304
|
-
if (extracted.knowledge.length > 0) {
|
|
305
|
-
results.extractedKnowledge = extracted.knowledge
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return results
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Get proactively recalled context for a query
|
|
314
|
-
*/
|
|
315
|
-
async getRecalledContext(query: string, project?: string): Promise<string | null> {
|
|
316
|
-
const result = await this.proactiveRecall.recall(
|
|
317
|
-
query,
|
|
318
|
-
project || this.autoContext.getCurrentProject() || undefined
|
|
319
|
-
)
|
|
320
|
-
|
|
321
|
-
return result?.formattedContext || null
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
/**
|
|
325
|
-
* Get relevant patterns for a query
|
|
326
|
-
*/
|
|
327
|
-
async getRelevantPatterns(query: string): Promise<Pattern[]> {
|
|
328
|
-
return this.patterns.getRelevantPatterns(query)
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Record a correction for learning
|
|
333
|
-
*/
|
|
334
|
-
async recordCorrection(
|
|
335
|
-
original: string,
|
|
336
|
-
corrected: string,
|
|
337
|
-
reasoning: string,
|
|
338
|
-
project?: string
|
|
339
|
-
): Promise<string> {
|
|
340
|
-
const targetProject = project || this.autoContext.getCurrentProject() || 'global'
|
|
341
|
-
return this.learning.recordCorrection(original, corrected, reasoning, targetProject)
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Get comprehensive Phase 12 statistics
|
|
346
|
-
*/
|
|
347
|
-
getStats(): Phase12Stats {
|
|
348
|
-
const allPatterns = this.patterns.getAllPatterns()
|
|
349
|
-
const patternsByType: Record<string, number> = {}
|
|
350
|
-
|
|
351
|
-
for (const pattern of allPatterns) {
|
|
352
|
-
patternsByType[pattern.type] = (patternsByType[pattern.type] || 0) + 1
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
const recallStats = this.proactiveRecall.getStats()
|
|
356
|
-
|
|
357
|
-
return {
|
|
358
|
-
patterns: {
|
|
359
|
-
total: allPatterns.length,
|
|
360
|
-
byType: patternsByType
|
|
361
|
-
},
|
|
362
|
-
learning: this.learning.getLearningInsights(),
|
|
363
|
-
automation: {
|
|
364
|
-
contextLoaded: this.autoContext.getCurrentContext() !== undefined,
|
|
365
|
-
currentProject: this.autoContext.getCurrentProject(),
|
|
366
|
-
recallStats: {
|
|
367
|
-
totalRecalls: recallStats.totalRecalls,
|
|
368
|
-
successfulRecalls: recallStats.successfulRecalls,
|
|
369
|
-
averageRelevance: recallStats.averageRelevance
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* Get formatted status report
|
|
377
|
-
*/
|
|
378
|
-
getStatusReport(): string {
|
|
379
|
-
const stats = this.getStats()
|
|
380
|
-
const parts: string[] = []
|
|
381
|
-
|
|
382
|
-
parts.push('# Phase 12 Status Report\n')
|
|
383
|
-
|
|
384
|
-
// Current project
|
|
385
|
-
parts.push('## Current Context')
|
|
386
|
-
if (stats.automation.currentProject) {
|
|
387
|
-
parts.push(`- **Project:** ${stats.automation.currentProject}`)
|
|
388
|
-
parts.push(`- **Context Loaded:** Yes`)
|
|
389
|
-
} else {
|
|
390
|
-
parts.push('- **Project:** Not detected (use smart_context to load project context)')
|
|
391
|
-
parts.push('- **Context Loaded:** No')
|
|
392
|
-
}
|
|
393
|
-
parts.push('')
|
|
394
|
-
|
|
395
|
-
// Pattern analysis
|
|
396
|
-
parts.push('## Pattern Analysis')
|
|
397
|
-
parts.push(`- **Total Patterns:** ${stats.patterns.total}`)
|
|
398
|
-
if (stats.patterns.total === 0) {
|
|
399
|
-
parts.push(' *No patterns stored yet. Use `recognize_pattern` to document reusable solutions.*')
|
|
400
|
-
} else {
|
|
401
|
-
for (const [type, count] of Object.entries(stats.patterns.byType)) {
|
|
402
|
-
parts.push(` - ${type}: ${count}`)
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
parts.push('')
|
|
406
|
-
|
|
407
|
-
// Learning system
|
|
408
|
-
parts.push('## Learning System')
|
|
409
|
-
parts.push(`- **Total Corrections:** ${stats.learning.correctionCount}`)
|
|
410
|
-
if (stats.learning.correctionCount === 0) {
|
|
411
|
-
parts.push(' *No corrections recorded yet. Use `record_correction` to document lessons learned.*')
|
|
412
|
-
}
|
|
413
|
-
parts.push(`- **Categories Improved:** ${stats.learning.categoriesImproved.join(', ') || 'None'}`)
|
|
414
|
-
if (stats.learning.preferences.length > 0) {
|
|
415
|
-
parts.push('- **Top Preferences:**')
|
|
416
|
-
for (const pref of stats.learning.preferences.slice(0, 3)) {
|
|
417
|
-
parts.push(` - ${pref.category}: ${Math.round(pref.strength * 100)}% confidence`)
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
parts.push('')
|
|
421
|
-
|
|
422
|
-
// Proactive recall
|
|
423
|
-
parts.push('## Proactive Recall')
|
|
424
|
-
parts.push(`- **Total Recalls:** ${stats.automation.recallStats.totalRecalls}`)
|
|
425
|
-
parts.push(`- **Successful:** ${stats.automation.recallStats.successfulRecalls}`)
|
|
426
|
-
const successRate = stats.automation.recallStats.totalRecalls > 0
|
|
427
|
-
? Math.round((stats.automation.recallStats.successfulRecalls / stats.automation.recallStats.totalRecalls) * 100)
|
|
428
|
-
: 0
|
|
429
|
-
parts.push(`- **Success Rate:** ${successRate}%`)
|
|
430
|
-
parts.push(`- **Average Relevance:** ${Math.round(stats.automation.recallStats.averageRelevance * 100)}%`)
|
|
431
|
-
|
|
432
|
-
if (stats.automation.recallStats.totalRecalls === 0) {
|
|
433
|
-
parts.push('\n*Proactive recall activates automatically when you ask questions or request recommendations.*')
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
return parts.join('\n')
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* Cleanup and reset Phase 12
|
|
441
|
-
*/
|
|
442
|
-
cleanup(): void {
|
|
443
|
-
this.autoContext.clearAllContexts()
|
|
444
|
-
this.patterns.clearPatterns()
|
|
445
|
-
this.learning.clearLearning()
|
|
446
|
-
this.proactiveRecall.resetStats()
|
|
447
|
-
this.initialized = false
|
|
448
|
-
this.logger.info('Phase 12 cleaned up')
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
// Re-export types
|
|
453
|
-
export type { Pattern } from '@/memory/patterns'
|
|
454
|
-
export type { Correction, Preference, LearningInsights } from '@/memory/learning'
|
|
455
|
-
export type { ExtractedKnowledge, ExtractionResult } from '@/memory/knowledge-extractor'
|
|
456
|
-
export type { DetectedProject, DetectedDecision, RecallResult } from '@/automation'
|
|
1
|
+
/**
|
|
2
|
+
* Phase 12 Manager
|
|
3
|
+
* Advanced Memory & Intelligent Automation
|
|
4
|
+
*
|
|
5
|
+
* Orchestrates all Phase 12 features:
|
|
6
|
+
* - Pattern Recognition
|
|
7
|
+
* - Learning System
|
|
8
|
+
* - Knowledge Extraction
|
|
9
|
+
* - Auto-Context Loading
|
|
10
|
+
* - Decision Detection
|
|
11
|
+
* - Proactive Recall
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import type { Logger } from 'pino'
|
|
15
|
+
import type { VaultManager } from '@/vault'
|
|
16
|
+
import type { MemoryManager } from '@/memory'
|
|
17
|
+
import type { ContextManager } from '@/context'
|
|
18
|
+
import { PatternRecognizer, type Pattern } from '@/memory/patterns'
|
|
19
|
+
import { LearningSystem, type LearningInsights } from '@/memory/learning'
|
|
20
|
+
import { KnowledgeExtractor, type ExtractedKnowledge } from '@/memory/knowledge-extractor'
|
|
21
|
+
import {
|
|
22
|
+
ProjectDetector,
|
|
23
|
+
AutoContextLoader,
|
|
24
|
+
DecisionDetector,
|
|
25
|
+
ProactiveRecallEngine,
|
|
26
|
+
type DetectedProject,
|
|
27
|
+
type DetectedDecision,
|
|
28
|
+
type RecallResult
|
|
29
|
+
} from '@/automation'
|
|
30
|
+
|
|
31
|
+
export interface Phase12Config {
|
|
32
|
+
/**
|
|
33
|
+
* Enable automatic context loading on startup
|
|
34
|
+
* @default true
|
|
35
|
+
*/
|
|
36
|
+
autoLoadContext: boolean
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Enable automatic decision detection and saving
|
|
40
|
+
* @default true
|
|
41
|
+
*/
|
|
42
|
+
autoDetectDecisions: boolean
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Enable proactive memory recall
|
|
46
|
+
* @default true
|
|
47
|
+
*/
|
|
48
|
+
enableProactiveRecall: boolean
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Minimum confidence for auto-saving decisions
|
|
52
|
+
* @default 0.7
|
|
53
|
+
*/
|
|
54
|
+
decisionConfidenceThreshold: number
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Enable pattern analysis on startup
|
|
58
|
+
* @default false
|
|
59
|
+
*/
|
|
60
|
+
analyzePatternsonStartup: boolean
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Enable knowledge extraction from messages
|
|
64
|
+
* @default true
|
|
65
|
+
*/
|
|
66
|
+
enableKnowledgeExtraction: boolean
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const DEFAULT_CONFIG: Phase12Config = {
|
|
70
|
+
autoLoadContext: true,
|
|
71
|
+
autoDetectDecisions: true,
|
|
72
|
+
enableProactiveRecall: true,
|
|
73
|
+
decisionConfidenceThreshold: 0.7,
|
|
74
|
+
analyzePatternsonStartup: false,
|
|
75
|
+
enableKnowledgeExtraction: true
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface MessageProcessingResult {
|
|
79
|
+
recalledMemories?: RecallResult
|
|
80
|
+
detectedDecision?: DetectedDecision
|
|
81
|
+
extractedKnowledge?: ExtractedKnowledge[]
|
|
82
|
+
detectedProject?: DetectedProject
|
|
83
|
+
decisionSaved?: boolean
|
|
84
|
+
decisionId?: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface Phase12Stats {
|
|
88
|
+
patterns: {
|
|
89
|
+
total: number
|
|
90
|
+
byType: Record<string, number>
|
|
91
|
+
}
|
|
92
|
+
learning: LearningInsights
|
|
93
|
+
automation: {
|
|
94
|
+
contextLoaded: boolean
|
|
95
|
+
currentProject: string | null
|
|
96
|
+
recallStats: {
|
|
97
|
+
totalRecalls: number
|
|
98
|
+
successfulRecalls: number
|
|
99
|
+
averageRelevance: number
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export class Phase12Manager {
|
|
105
|
+
private logger: Logger
|
|
106
|
+
private config: Phase12Config
|
|
107
|
+
private initialized = false
|
|
108
|
+
|
|
109
|
+
// Advanced Memory
|
|
110
|
+
public readonly patterns: PatternRecognizer
|
|
111
|
+
public readonly learning: LearningSystem
|
|
112
|
+
public readonly knowledge: KnowledgeExtractor
|
|
113
|
+
|
|
114
|
+
// Intelligent Automation
|
|
115
|
+
public readonly projectDetector: ProjectDetector
|
|
116
|
+
public readonly autoContext: AutoContextLoader
|
|
117
|
+
public readonly decisionDetector: DecisionDetector
|
|
118
|
+
public readonly proactiveRecall: ProactiveRecallEngine
|
|
119
|
+
|
|
120
|
+
constructor(
|
|
121
|
+
logger: Logger,
|
|
122
|
+
vault: VaultManager,
|
|
123
|
+
memory: MemoryManager,
|
|
124
|
+
context: ContextManager,
|
|
125
|
+
config: Partial<Phase12Config> = {}
|
|
126
|
+
) {
|
|
127
|
+
this.logger = logger.child({ component: 'phase12-manager' })
|
|
128
|
+
this.config = { ...DEFAULT_CONFIG, ...config }
|
|
129
|
+
|
|
130
|
+
// Initialize advanced memory features
|
|
131
|
+
this.patterns = new PatternRecognizer(logger, memory)
|
|
132
|
+
this.learning = new LearningSystem(logger, memory)
|
|
133
|
+
this.knowledge = new KnowledgeExtractor(logger, memory)
|
|
134
|
+
|
|
135
|
+
// Initialize intelligent automation
|
|
136
|
+
this.projectDetector = new ProjectDetector(logger, vault)
|
|
137
|
+
this.autoContext = new AutoContextLoader(
|
|
138
|
+
logger,
|
|
139
|
+
context,
|
|
140
|
+
this.projectDetector
|
|
141
|
+
)
|
|
142
|
+
this.decisionDetector = new DecisionDetector(logger, memory)
|
|
143
|
+
this.proactiveRecall = new ProactiveRecallEngine(logger, memory)
|
|
144
|
+
|
|
145
|
+
this.logger.info(
|
|
146
|
+
{ config: this.config },
|
|
147
|
+
'Phase 12 Manager created'
|
|
148
|
+
)
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Initialize all Phase 12 features
|
|
153
|
+
*/
|
|
154
|
+
async initialize(): Promise<void> {
|
|
155
|
+
if (this.initialized) {
|
|
156
|
+
this.logger.warn('Phase 12 already initialized')
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.logger.info('Initializing Phase 12 features...')
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
// Start auto-context loader
|
|
164
|
+
if (this.config.autoLoadContext) {
|
|
165
|
+
await this.autoContext.initialize()
|
|
166
|
+
this.logger.info('Auto-context loader initialized')
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Run initial pattern analysis if enabled
|
|
170
|
+
if (this.config.analyzePatternsonStartup) {
|
|
171
|
+
const patterns = await this.patterns.analyzePatterns()
|
|
172
|
+
this.logger.info(
|
|
173
|
+
{ patternCount: patterns.length },
|
|
174
|
+
'Initial pattern analysis complete'
|
|
175
|
+
)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
this.initialized = true
|
|
179
|
+
this.logger.info('Phase 12 initialization complete')
|
|
180
|
+
} catch (error) {
|
|
181
|
+
this.logger.error({ error }, 'Phase 12 initialization failed')
|
|
182
|
+
throw error
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Check if Phase 12 is initialized
|
|
188
|
+
*/
|
|
189
|
+
isInitialized(): boolean {
|
|
190
|
+
return this.initialized
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Process incoming message with all Phase 12 features
|
|
195
|
+
*/
|
|
196
|
+
async processMessage(
|
|
197
|
+
message: string,
|
|
198
|
+
project?: string
|
|
199
|
+
): Promise<MessageProcessingResult> {
|
|
200
|
+
const results: MessageProcessingResult = {}
|
|
201
|
+
|
|
202
|
+
// Use detected project if not provided
|
|
203
|
+
const targetProject = project || this.autoContext.getCurrentProject() || undefined
|
|
204
|
+
|
|
205
|
+
// 1. Proactive recall
|
|
206
|
+
if (this.config.enableProactiveRecall) {
|
|
207
|
+
if (this.proactiveRecall.shouldRecall(message)) {
|
|
208
|
+
results.recalledMemories = await this.proactiveRecall.recall(
|
|
209
|
+
message,
|
|
210
|
+
targetProject
|
|
211
|
+
) || undefined
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 2. Decision detection
|
|
216
|
+
if (this.config.autoDetectDecisions && targetProject) {
|
|
217
|
+
const decision = this.decisionDetector.detectDecision(message, targetProject)
|
|
218
|
+
|
|
219
|
+
if (decision && decision.confidence >= this.config.decisionConfidenceThreshold) {
|
|
220
|
+
results.detectedDecision = decision
|
|
221
|
+
|
|
222
|
+
// Auto-save if confidence is high enough
|
|
223
|
+
const decisionId = await this.decisionDetector.autoSaveDecision(
|
|
224
|
+
decision,
|
|
225
|
+
targetProject
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
if (decisionId) {
|
|
229
|
+
results.decisionSaved = true
|
|
230
|
+
results.decisionId = decisionId
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 3. Knowledge extraction
|
|
236
|
+
if (this.config.enableKnowledgeExtraction) {
|
|
237
|
+
const extracted = await this.knowledge.extractFromConversation(
|
|
238
|
+
message,
|
|
239
|
+
targetProject
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
if (extracted.knowledge.length > 0) {
|
|
243
|
+
results.extractedKnowledge = extracted.knowledge
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// Log processing results
|
|
248
|
+
if (Object.keys(results).length > 0) {
|
|
249
|
+
this.logger.debug(
|
|
250
|
+
{
|
|
251
|
+
hasRecall: !!results.recalledMemories,
|
|
252
|
+
hasDecision: !!results.detectedDecision,
|
|
253
|
+
knowledgeCount: results.extractedKnowledge?.length || 0,
|
|
254
|
+
decisionSaved: results.decisionSaved
|
|
255
|
+
},
|
|
256
|
+
'Message processed with Phase 12 features'
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
return results
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Process outgoing response for decisions
|
|
265
|
+
* Call this with Claude's responses to detect and save decisions
|
|
266
|
+
*/
|
|
267
|
+
async processResponse(
|
|
268
|
+
response: string,
|
|
269
|
+
project?: string
|
|
270
|
+
): Promise<MessageProcessingResult> {
|
|
271
|
+
const results: MessageProcessingResult = {}
|
|
272
|
+
const targetProject = project || this.autoContext.getCurrentProject() || undefined
|
|
273
|
+
|
|
274
|
+
// Decision detection on responses (Claude's decisions)
|
|
275
|
+
if (this.config.autoDetectDecisions && targetProject) {
|
|
276
|
+
const decision = this.decisionDetector.detectDecision(response, targetProject)
|
|
277
|
+
|
|
278
|
+
if (decision && decision.confidence >= this.config.decisionConfidenceThreshold) {
|
|
279
|
+
results.detectedDecision = decision
|
|
280
|
+
|
|
281
|
+
const decisionId = await this.decisionDetector.autoSaveDecision(
|
|
282
|
+
decision,
|
|
283
|
+
targetProject
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
if (decisionId) {
|
|
287
|
+
results.decisionSaved = true
|
|
288
|
+
results.decisionId = decisionId
|
|
289
|
+
this.logger.info(
|
|
290
|
+
{ decisionId, project: targetProject },
|
|
291
|
+
'Decision auto-saved from response'
|
|
292
|
+
)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// Knowledge extraction from responses
|
|
298
|
+
if (this.config.enableKnowledgeExtraction) {
|
|
299
|
+
const extracted = await this.knowledge.extractFromConversation(
|
|
300
|
+
response,
|
|
301
|
+
targetProject
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
if (extracted.knowledge.length > 0) {
|
|
305
|
+
results.extractedKnowledge = extracted.knowledge
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return results
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Get proactively recalled context for a query
|
|
314
|
+
*/
|
|
315
|
+
async getRecalledContext(query: string, project?: string): Promise<string | null> {
|
|
316
|
+
const result = await this.proactiveRecall.recall(
|
|
317
|
+
query,
|
|
318
|
+
project || this.autoContext.getCurrentProject() || undefined
|
|
319
|
+
)
|
|
320
|
+
|
|
321
|
+
return result?.formattedContext || null
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Get relevant patterns for a query
|
|
326
|
+
*/
|
|
327
|
+
async getRelevantPatterns(query: string): Promise<Pattern[]> {
|
|
328
|
+
return this.patterns.getRelevantPatterns(query)
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Record a correction for learning
|
|
333
|
+
*/
|
|
334
|
+
async recordCorrection(
|
|
335
|
+
original: string,
|
|
336
|
+
corrected: string,
|
|
337
|
+
reasoning: string,
|
|
338
|
+
project?: string
|
|
339
|
+
): Promise<string> {
|
|
340
|
+
const targetProject = project || this.autoContext.getCurrentProject() || 'global'
|
|
341
|
+
return this.learning.recordCorrection(original, corrected, reasoning, targetProject)
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Get comprehensive Phase 12 statistics
|
|
346
|
+
*/
|
|
347
|
+
getStats(): Phase12Stats {
|
|
348
|
+
const allPatterns = this.patterns.getAllPatterns()
|
|
349
|
+
const patternsByType: Record<string, number> = {}
|
|
350
|
+
|
|
351
|
+
for (const pattern of allPatterns) {
|
|
352
|
+
patternsByType[pattern.type] = (patternsByType[pattern.type] || 0) + 1
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const recallStats = this.proactiveRecall.getStats()
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
patterns: {
|
|
359
|
+
total: allPatterns.length,
|
|
360
|
+
byType: patternsByType
|
|
361
|
+
},
|
|
362
|
+
learning: this.learning.getLearningInsights(),
|
|
363
|
+
automation: {
|
|
364
|
+
contextLoaded: this.autoContext.getCurrentContext() !== undefined,
|
|
365
|
+
currentProject: this.autoContext.getCurrentProject(),
|
|
366
|
+
recallStats: {
|
|
367
|
+
totalRecalls: recallStats.totalRecalls,
|
|
368
|
+
successfulRecalls: recallStats.successfulRecalls,
|
|
369
|
+
averageRelevance: recallStats.averageRelevance
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Get formatted status report
|
|
377
|
+
*/
|
|
378
|
+
getStatusReport(): string {
|
|
379
|
+
const stats = this.getStats()
|
|
380
|
+
const parts: string[] = []
|
|
381
|
+
|
|
382
|
+
parts.push('# Phase 12 Status Report\n')
|
|
383
|
+
|
|
384
|
+
// Current project
|
|
385
|
+
parts.push('## Current Context')
|
|
386
|
+
if (stats.automation.currentProject) {
|
|
387
|
+
parts.push(`- **Project:** ${stats.automation.currentProject}`)
|
|
388
|
+
parts.push(`- **Context Loaded:** Yes`)
|
|
389
|
+
} else {
|
|
390
|
+
parts.push('- **Project:** Not detected (use smart_context to load project context)')
|
|
391
|
+
parts.push('- **Context Loaded:** No')
|
|
392
|
+
}
|
|
393
|
+
parts.push('')
|
|
394
|
+
|
|
395
|
+
// Pattern analysis
|
|
396
|
+
parts.push('## Pattern Analysis')
|
|
397
|
+
parts.push(`- **Total Patterns:** ${stats.patterns.total}`)
|
|
398
|
+
if (stats.patterns.total === 0) {
|
|
399
|
+
parts.push(' *No patterns stored yet. Use `recognize_pattern` to document reusable solutions.*')
|
|
400
|
+
} else {
|
|
401
|
+
for (const [type, count] of Object.entries(stats.patterns.byType)) {
|
|
402
|
+
parts.push(` - ${type}: ${count}`)
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
parts.push('')
|
|
406
|
+
|
|
407
|
+
// Learning system
|
|
408
|
+
parts.push('## Learning System')
|
|
409
|
+
parts.push(`- **Total Corrections:** ${stats.learning.correctionCount}`)
|
|
410
|
+
if (stats.learning.correctionCount === 0) {
|
|
411
|
+
parts.push(' *No corrections recorded yet. Use `record_correction` to document lessons learned.*')
|
|
412
|
+
}
|
|
413
|
+
parts.push(`- **Categories Improved:** ${stats.learning.categoriesImproved.join(', ') || 'None'}`)
|
|
414
|
+
if (stats.learning.preferences.length > 0) {
|
|
415
|
+
parts.push('- **Top Preferences:**')
|
|
416
|
+
for (const pref of stats.learning.preferences.slice(0, 3)) {
|
|
417
|
+
parts.push(` - ${pref.category}: ${Math.round(pref.strength * 100)}% confidence`)
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
parts.push('')
|
|
421
|
+
|
|
422
|
+
// Proactive recall
|
|
423
|
+
parts.push('## Proactive Recall')
|
|
424
|
+
parts.push(`- **Total Recalls:** ${stats.automation.recallStats.totalRecalls}`)
|
|
425
|
+
parts.push(`- **Successful:** ${stats.automation.recallStats.successfulRecalls}`)
|
|
426
|
+
const successRate = stats.automation.recallStats.totalRecalls > 0
|
|
427
|
+
? Math.round((stats.automation.recallStats.successfulRecalls / stats.automation.recallStats.totalRecalls) * 100)
|
|
428
|
+
: 0
|
|
429
|
+
parts.push(`- **Success Rate:** ${successRate}%`)
|
|
430
|
+
parts.push(`- **Average Relevance:** ${Math.round(stats.automation.recallStats.averageRelevance * 100)}%`)
|
|
431
|
+
|
|
432
|
+
if (stats.automation.recallStats.totalRecalls === 0) {
|
|
433
|
+
parts.push('\n*Proactive recall activates automatically when you ask questions or request recommendations.*')
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return parts.join('\n')
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Cleanup and reset Phase 12
|
|
441
|
+
*/
|
|
442
|
+
cleanup(): void {
|
|
443
|
+
this.autoContext.clearAllContexts()
|
|
444
|
+
this.patterns.clearPatterns()
|
|
445
|
+
this.learning.clearLearning()
|
|
446
|
+
this.proactiveRecall.resetStats()
|
|
447
|
+
this.initialized = false
|
|
448
|
+
this.logger.info('Phase 12 cleaned up')
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Re-export types
|
|
453
|
+
export type { Pattern } from '@/memory/patterns'
|
|
454
|
+
export type { Correction, Preference, LearningInsights } from '@/memory/learning'
|
|
455
|
+
export type { ExtractedKnowledge, ExtractionResult } from '@/memory/knowledge-extractor'
|
|
456
|
+
export type { DetectedProject, DetectedDecision, RecallResult } from '@/automation'
|