-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal-brain.ts
More file actions
91 lines (80 loc) · 3.01 KB
/
Copy pathlocal-brain.ts
File metadata and controls
91 lines (80 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as fs from "fs";
import * as path from "path";
import { RuntimeLogger } from "../core/logger.js";
export interface LearnedPattern {
id: string;
category: string;
description: string;
learnedAt: string;
isProposed?: boolean;
}
export class LocalBrain {
private static STORAGE_NAME = "memory.json";
private static DOT_REFINER = ".refiner";
private static getStoragePath(rootPath: string): string {
return path.join(rootPath, this.DOT_REFINER, this.STORAGE_NAME);
}
private static ensureStorage(rootPath: string) {
const storagePath = this.getStoragePath(rootPath);
const dir = path.dirname(storagePath);
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true });
if (!fs.existsSync(storagePath)) {
fs.writeFileSync(storagePath, JSON.stringify({ patterns: [] }));
}
}
static getPatterns(rootPath: string = ".", includeProposed = false): LearnedPattern[] {
this.ensureStorage(rootPath);
try {
const storagePath = this.getStoragePath(rootPath);
const data = JSON.parse(fs.readFileSync(storagePath, "utf-8"));
const patterns = data.patterns || [];
if (!includeProposed) {
return patterns.filter((p: LearnedPattern) => !p.isProposed);
}
return patterns;
} catch (error) {
RuntimeLogger.error(`Failed to read local brain patterns for ${rootPath}`, error);
return [];
}
}
static savePattern(pattern: Omit<LearnedPattern, "learnedAt">, rootPath: string = ".") {
this.ensureStorage(rootPath);
const storagePath = this.getStoragePath(rootPath);
try {
const data = JSON.parse(fs.readFileSync(storagePath, "utf-8"));
const patterns: LearnedPattern[] = Array.isArray(data.patterns) ? data.patterns : [];
const newPattern: LearnedPattern = {
...pattern,
learnedAt: new Date().toISOString()
};
const existingIndex = patterns.findIndex((p: LearnedPattern) => p.id === pattern.id);
if (existingIndex >= 0) {
patterns[existingIndex] = newPattern;
} else {
patterns.push(newPattern);
}
data.patterns = patterns;
fs.writeFileSync(storagePath, JSON.stringify(data, null, 2));
return newPattern;
} catch (error) {
RuntimeLogger.error(`Failed to save local brain pattern ${pattern.id} for ${rootPath}`, error);
throw error;
}
}
static approvePattern(id: string, rootPath: string = ".") {
this.ensureStorage(rootPath);
const storagePath = this.getStoragePath(rootPath);
try {
const data = JSON.parse(fs.readFileSync(storagePath, "utf-8"));
const patterns: LearnedPattern[] = Array.isArray(data.patterns) ? data.patterns : [];
const pattern = patterns.find((p: LearnedPattern) => p.id === id);
if (pattern) {
pattern.isProposed = false;
fs.writeFileSync(storagePath, JSON.stringify(data, null, 2));
}
} catch (error) {
RuntimeLogger.error(`Failed to approve local brain pattern ${id} for ${rootPath}`, error);
throw error;
}
}
}