-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblackboard.ts
More file actions
254 lines (220 loc) · 8.12 KB
/
Copy pathblackboard.ts
File metadata and controls
254 lines (220 loc) · 8.12 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { RuntimeLogger } from "./logger.js";
export interface AgentIntent {
agentName: string;
toolType: string;
intent: string;
timestamp: string;
expiresAt: string;
projectPath?: string;
}
export interface SystemLog {
timestamp: string;
message: string;
projectPath?: string;
}
export interface BlackboardData {
activeIntents: AgentIntent[];
logs: SystemLog[];
lastRefinement?: {
original: string;
refined: string;
timestamp: string;
gain: number;
};
}
export class AgenticBlackboard {
private static STORAGE_NAME = "blackboard.json";
private static DOT_REFINER = ".refiner";
private static writeQueue: Promise<void> = Promise.resolve();
private static listeners: Array<() => void> = [];
private static getGlobalDir(): string {
return process.env.PROMPT_REFINER_GLOBAL_DIR
|| (process.env.PROMPT_REFINER_PROJECT_DIR
? path.join(process.env.PROMPT_REFINER_PROJECT_DIR, ".global-refiner")
: path.join(os.homedir(), ".refiner"));
}
private static getGlobalLogPath(): string {
return path.join(this.getGlobalDir(), "global_history.json");
}
private static findProjectRoot(startPath: string): string {
try {
let current = path.resolve(startPath);
while (current !== path.parse(current).root) {
if (fs.existsSync(path.join(current, this.DOT_REFINER))) {
return current;
}
current = path.dirname(current);
}
return path.resolve(startPath);
} catch (error) {
RuntimeLogger.warn(`Failed to resolve project root for ${startPath}`, error);
return path.resolve(".");
}
}
private static getStoragePath(rootPath: string): string {
const effectiveRoot = rootPath === "."
? process.env.PROMPT_REFINER_PROJECT_DIR || rootPath
: rootPath;
const projectRoot = this.findProjectRoot(effectiveRoot);
return path.join(projectRoot, this.DOT_REFINER, this.STORAGE_NAME);
}
private static ensureStorage(rootPath: string) {
try {
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({ activeIntents: [], logs: [] }));
}
} catch (error) {
RuntimeLogger.error("Failed to ensure project blackboard storage", error);
}
}
private static ensureGlobalStorage() {
try {
const globalDir = this.getGlobalDir();
const globalLog = this.getGlobalLogPath();
if (!fs.existsSync(globalDir)) fs.mkdirSync(globalDir, { recursive: true });
if (!fs.existsSync(globalLog)) {
fs.writeFileSync(globalLog, JSON.stringify({ logs: [], projects: [] }));
}
} catch (error) {
RuntimeLogger.error("Failed to ensure global blackboard storage", error);
}
}
private static readJsonFile<T>(storagePath: string, fallback: T): T {
try {
if (!fs.existsSync(storagePath)) {
return fallback;
}
return JSON.parse(fs.readFileSync(storagePath, "utf-8")) as T;
} catch (error) {
RuntimeLogger.error(`Failed to read JSON file: ${storagePath}`, error);
return fallback;
}
}
private static atomicUpdate<T>(storagePath: string, fallback: T, updater: (data: T) => void): Promise<void> {
this.writeQueue = this.writeQueue.then(() => {
const temporaryPath = `${storagePath}.${process.pid}.${Date.now()}.tmp`;
try {
const data = this.readJsonFile<T>(storagePath, fallback);
updater(data);
fs.writeFileSync(temporaryPath, JSON.stringify(data, null, 2), { encoding: "utf8", mode: 0o600 });
fs.renameSync(temporaryPath, storagePath);
} catch (error) {
try {
fs.rmSync(temporaryPath, { force: true });
} catch {
// Preserve the original persistence failure.
}
RuntimeLogger.error(`Atomic update failed for ${storagePath}`, error);
}
});
return this.writeQueue;
}
private static notifyListeners() {
for (const listener of [...this.listeners]) {
try {
listener();
} catch (error) {
RuntimeLogger.error("Listener notification failed", error);
}
}
}
private static afterQueuedWrite(callback: () => void) {
void this.writeQueue.then(() => callback());
}
static async flushPendingWrites() {
await this.writeQueue;
}
static postLog(message: string, rootPath: string = ".") {
const logEntry: SystemLog = {
timestamp: new Date().toISOString(),
message,
projectPath: path.resolve(rootPath)
};
const storagePath = this.getStoragePath(rootPath);
this.ensureStorage(rootPath);
void this.atomicUpdate<BlackboardData>(storagePath, { activeIntents: [], logs: [] }, (data) => {
if (!data.logs) data.logs = [];
data.logs.unshift(logEntry);
if (data.logs.length > 200) data.logs.pop();
});
this.ensureGlobalStorage();
void this.atomicUpdate<{ logs: SystemLog[]; projects: string[] }>(
this.getGlobalLogPath(),
{ logs: [], projects: [] },
(globalData) => {
if (!globalData.logs) globalData.logs = [];
globalData.logs.unshift(logEntry);
if (globalData.logs.length > 500) globalData.logs.pop();
if (!globalData.projects) globalData.projects = [];
const absPath = path.resolve(rootPath);
if (!globalData.projects.includes(absPath)) globalData.projects.push(absPath);
}
);
this.afterQueuedWrite(() => this.notifyListeners());
}
static onUpdate(callback: () => void) {
this.listeners.push(callback);
return () => {
this.listeners = this.listeners.filter(listener => listener !== callback);
};
}
static getGlobalData() {
this.ensureGlobalStorage();
return this.readJsonFile(this.getGlobalLogPath(), { logs: [], projects: [] });
}
static async setLastRefinement(original: string, refined: string, rootPath: string = ".", gain: number = 0) {
const storagePath = this.getStoragePath(rootPath);
this.ensureStorage(rootPath);
await this.atomicUpdate<BlackboardData>(storagePath, { activeIntents: [], logs: [] }, (data) => {
data.lastRefinement = {
original,
refined,
timestamp: new Date().toISOString(),
gain
};
});
this.postLog(`Refinement Complete (Gain: ${gain}%)`, rootPath);
}
static getLastRefinement(rootPath: string = ".") {
const storagePath = this.getStoragePath(rootPath);
const data = this.readJsonFile<BlackboardData>(storagePath, { activeIntents: [], logs: [] });
return data.lastRefinement || null;
}
static getLogs(rootPath: string = "."): SystemLog[] {
const storagePath = this.getStoragePath(rootPath);
const data = this.readJsonFile<BlackboardData>(storagePath, { activeIntents: [], logs: [] });
return data.logs || [];
}
static postIntent(agentName: string, toolType: string, intent: string, rootPath: string = ".") {
const storagePath = this.getStoragePath(rootPath);
this.ensureStorage(rootPath);
const newIntent: AgentIntent = {
agentName,
toolType,
intent,
timestamp: new Date().toISOString(),
expiresAt: new Date(Date.now() + 1000 * 60 * 30).toISOString(),
projectPath: path.resolve(rootPath)
};
void this.atomicUpdate<BlackboardData>(storagePath, { activeIntents: [], logs: [] }, (data) => {
if (!data.activeIntents) data.activeIntents = [];
data.activeIntents = data.activeIntents.filter((i: AgentIntent) =>
i.agentName !== agentName && new Date(i.expiresAt) > new Date()
);
data.activeIntents.push(newIntent);
});
this.postLog(`Agent [${agentName}] intent: ${intent.substring(0, 50)}...`, rootPath);
return newIntent;
}
static getActiveIntents(rootPath: string = "."): AgentIntent[] {
const storagePath = this.getStoragePath(rootPath);
const data = this.readJsonFile<BlackboardData>(storagePath, { activeIntents: [], logs: [] });
return (data.activeIntents || []).filter((i: AgentIntent) => new Date(i.expiresAt) > new Date());
}
}