wave-agent-sdk 0.0.7 → 0.0.8
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/dist/agent.d.ts +32 -20
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +202 -20
- package/dist/constants/events.d.ts +28 -0
- package/dist/constants/events.d.ts.map +1 -0
- package/dist/constants/events.js +27 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/managers/aiManager.d.ts +34 -1
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +243 -128
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +7 -6
- package/dist/managers/hookManager.d.ts +9 -4
- package/dist/managers/hookManager.d.ts.map +1 -1
- package/dist/managers/hookManager.js +62 -30
- package/dist/managers/liveConfigManager.d.ts +58 -0
- package/dist/managers/liveConfigManager.d.ts.map +1 -0
- package/dist/managers/liveConfigManager.js +160 -0
- package/dist/managers/messageManager.d.ts +38 -13
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +163 -30
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +4 -1
- package/dist/managers/subagentManager.d.ts +51 -0
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +189 -18
- package/dist/services/aiService.d.ts +13 -5
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +350 -74
- package/dist/services/configurationWatcher.d.ts +120 -0
- package/dist/services/configurationWatcher.d.ts.map +1 -0
- package/dist/services/configurationWatcher.js +439 -0
- package/dist/services/fileWatcher.d.ts +69 -0
- package/dist/services/fileWatcher.d.ts.map +1 -0
- package/dist/services/fileWatcher.js +213 -0
- package/dist/services/hook.d.ts +91 -9
- package/dist/services/hook.d.ts.map +1 -1
- package/dist/services/hook.js +393 -43
- package/dist/services/jsonlHandler.d.ts +62 -0
- package/dist/services/jsonlHandler.d.ts.map +1 -0
- package/dist/services/jsonlHandler.js +257 -0
- package/dist/services/memory.d.ts +9 -0
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +81 -12
- package/dist/services/memoryStore.d.ts +81 -0
- package/dist/services/memoryStore.d.ts.map +1 -0
- package/dist/services/memoryStore.js +200 -0
- package/dist/services/session.d.ts +64 -49
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +310 -132
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +5 -4
- package/dist/tools/deleteFileTool.d.ts.map +1 -1
- package/dist/tools/deleteFileTool.js +2 -1
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +3 -2
- package/dist/tools/multiEditTool.d.ts.map +1 -1
- package/dist/tools/multiEditTool.js +4 -3
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +2 -1
- package/dist/tools/writeTool.d.ts.map +1 -1
- package/dist/tools/writeTool.js +5 -6
- package/dist/types/commands.d.ts +4 -0
- package/dist/types/commands.d.ts.map +1 -1
- package/dist/types/core.d.ts +35 -0
- package/dist/types/core.d.ts.map +1 -1
- package/dist/types/environment.d.ts +42 -0
- package/dist/types/environment.d.ts.map +1 -0
- package/dist/types/environment.js +21 -0
- package/dist/types/hooks.d.ts +8 -2
- package/dist/types/hooks.d.ts.map +1 -1
- package/dist/types/hooks.js +8 -2
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -0
- package/dist/types/memoryStore.d.ts +82 -0
- package/dist/types/memoryStore.d.ts.map +1 -0
- package/dist/types/memoryStore.js +7 -0
- package/dist/types/messaging.d.ts +14 -2
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/session.d.ts +20 -0
- package/dist/types/session.d.ts.map +1 -0
- package/dist/types/session.js +7 -0
- package/dist/utils/bashHistory.d.ts.map +1 -1
- package/dist/utils/bashHistory.js +27 -26
- package/dist/utils/cacheControlUtils.d.ts +121 -0
- package/dist/utils/cacheControlUtils.d.ts.map +1 -0
- package/dist/utils/cacheControlUtils.js +367 -0
- package/dist/utils/commandPathResolver.d.ts +52 -0
- package/dist/utils/commandPathResolver.d.ts.map +1 -0
- package/dist/utils/commandPathResolver.js +145 -0
- package/dist/utils/configPaths.d.ts +85 -0
- package/dist/utils/configPaths.d.ts.map +1 -0
- package/dist/utils/configPaths.js +121 -0
- package/dist/utils/configResolver.d.ts +37 -10
- package/dist/utils/configResolver.d.ts.map +1 -1
- package/dist/utils/configResolver.js +127 -23
- package/dist/utils/constants.d.ts +1 -1
- package/dist/utils/constants.js +1 -1
- package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
- package/dist/utils/convertMessagesForAPI.js +7 -5
- package/dist/utils/customCommands.d.ts.map +1 -1
- package/dist/utils/customCommands.js +66 -21
- package/dist/utils/fileUtils.d.ts +15 -0
- package/dist/utils/fileUtils.d.ts.map +1 -0
- package/dist/utils/fileUtils.js +61 -0
- package/dist/utils/globalLogger.d.ts +102 -0
- package/dist/utils/globalLogger.d.ts.map +1 -0
- package/dist/utils/globalLogger.js +136 -0
- package/dist/utils/mcpUtils.d.ts.map +1 -1
- package/dist/utils/mcpUtils.js +25 -3
- package/dist/utils/messageOperations.d.ts +20 -8
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +25 -16
- package/dist/utils/pathEncoder.d.ts +104 -0
- package/dist/utils/pathEncoder.d.ts.map +1 -0
- package/dist/utils/pathEncoder.js +272 -0
- package/dist/utils/subagentParser.d.ts.map +1 -1
- package/dist/utils/subagentParser.js +2 -1
- package/dist/utils/tokenCalculation.d.ts +26 -0
- package/dist/utils/tokenCalculation.d.ts.map +1 -0
- package/dist/utils/tokenCalculation.js +36 -0
- package/package.json +6 -3
- package/src/agent.ts +298 -34
- package/src/constants/events.ts +38 -0
- package/src/index.ts +2 -0
- package/src/managers/aiManager.ts +323 -170
- package/src/managers/backgroundBashManager.ts +7 -6
- package/src/managers/hookManager.ts +83 -40
- package/src/managers/liveConfigManager.ts +248 -0
- package/src/managers/messageManager.ts +230 -63
- package/src/managers/slashCommandManager.ts +4 -1
- package/src/managers/subagentManager.ts +283 -21
- package/src/services/aiService.ts +474 -83
- package/src/services/configurationWatcher.ts +622 -0
- package/src/services/fileWatcher.ts +301 -0
- package/src/services/hook.ts +538 -47
- package/src/services/jsonlHandler.ts +319 -0
- package/src/services/memory.ts +92 -12
- package/src/services/memoryStore.ts +279 -0
- package/src/services/session.ts +381 -157
- package/src/tools/bashTool.ts +5 -4
- package/src/tools/deleteFileTool.ts +2 -1
- package/src/tools/editTool.ts +3 -2
- package/src/tools/multiEditTool.ts +4 -3
- package/src/tools/readTool.ts +2 -1
- package/src/tools/writeTool.ts +7 -6
- package/src/types/commands.ts +6 -0
- package/src/types/core.ts +44 -0
- package/src/types/environment.ts +60 -0
- package/src/types/hooks.ts +21 -8
- package/src/types/index.ts +2 -0
- package/src/types/memoryStore.ts +94 -0
- package/src/types/messaging.ts +14 -2
- package/src/types/session.ts +25 -0
- package/src/utils/bashHistory.ts +27 -27
- package/src/utils/cacheControlUtils.ts +540 -0
- package/src/utils/commandPathResolver.ts +189 -0
- package/src/utils/configPaths.ts +163 -0
- package/src/utils/configResolver.ts +182 -22
- package/src/utils/constants.ts +1 -1
- package/src/utils/convertMessagesForAPI.ts +7 -5
- package/src/utils/customCommands.ts +90 -22
- package/src/utils/fileUtils.ts +65 -0
- package/src/utils/globalLogger.ts +145 -0
- package/src/utils/mcpUtils.ts +34 -3
- package/src/utils/messageOperations.ts +42 -20
- package/src/utils/pathEncoder.ts +379 -0
- package/src/utils/subagentParser.ts +2 -1
- package/src/utils/tokenCalculation.ts +43 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSONL file operations service
|
|
3
|
+
* Handles reading and writing JSONL (JSON Lines) session files for improved performance
|
|
4
|
+
*/
|
|
5
|
+
import { appendFile, readFile, writeFile, stat, mkdir } from "fs/promises";
|
|
6
|
+
import { dirname } from "path";
|
|
7
|
+
import { getLastLine, readFirstLine } from "../utils/fileUtils.js";
|
|
8
|
+
/**
|
|
9
|
+
* JSONL handler class for message persistence operations
|
|
10
|
+
*/
|
|
11
|
+
export class JsonlHandler {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.defaultWriteOptions = {
|
|
14
|
+
atomic: true,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Create a new session file with metadata header
|
|
19
|
+
*/
|
|
20
|
+
async createSession(filePath, sessionId, workdir, sessionType = "main", parentSessionId, subagentType) {
|
|
21
|
+
const metadataLine = {
|
|
22
|
+
__meta__: true,
|
|
23
|
+
sessionId,
|
|
24
|
+
sessionType,
|
|
25
|
+
...(parentSessionId && { parentSessionId }),
|
|
26
|
+
...(subagentType && { subagentType }),
|
|
27
|
+
workdir,
|
|
28
|
+
startedAt: new Date().toISOString(),
|
|
29
|
+
};
|
|
30
|
+
// Ensure directory exists
|
|
31
|
+
await this.ensureDirectory(dirname(filePath));
|
|
32
|
+
// Write metadata line as first line
|
|
33
|
+
await writeFile(filePath, JSON.stringify(metadataLine) + "\n", "utf8");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Append a single message to JSONL file
|
|
37
|
+
*/
|
|
38
|
+
async appendMessage(filePath, message) {
|
|
39
|
+
return this.appendMessages(filePath, [message]);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Append multiple messages to JSONL file
|
|
43
|
+
*/
|
|
44
|
+
async appendMessages(filePath, messages) {
|
|
45
|
+
if (messages.length === 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// Convert to SessionMessage format with timestamps
|
|
49
|
+
const sessionMessages = messages.map((message) => ({
|
|
50
|
+
...message,
|
|
51
|
+
timestamp: new Date().toISOString(),
|
|
52
|
+
}));
|
|
53
|
+
return this.append(filePath, sessionMessages);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Append messages to JSONL file
|
|
57
|
+
*/
|
|
58
|
+
async append(filePath, messages, options) {
|
|
59
|
+
if (messages.length === 0) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const opts = { ...this.defaultWriteOptions, ...options };
|
|
63
|
+
// Validate messages (always enabled for data integrity)
|
|
64
|
+
this.validateMessages(messages);
|
|
65
|
+
// Ensure directory exists
|
|
66
|
+
await this.ensureDirectory(dirname(filePath));
|
|
67
|
+
// Convert messages to JSONL lines (always compact JSON)
|
|
68
|
+
const lines = messages.map((message) => {
|
|
69
|
+
const { timestamp: existingTimestamp, ...messageWithoutTimestamp } = message;
|
|
70
|
+
const messageWithTimestamp = {
|
|
71
|
+
timestamp: existingTimestamp || new Date().toISOString(),
|
|
72
|
+
...messageWithoutTimestamp,
|
|
73
|
+
};
|
|
74
|
+
return JSON.stringify(messageWithTimestamp);
|
|
75
|
+
});
|
|
76
|
+
const content = lines.join("\n") + "\n";
|
|
77
|
+
if (opts.atomic) {
|
|
78
|
+
// Write to temp file first, then rename
|
|
79
|
+
const tempPath = `${filePath}.tmp`;
|
|
80
|
+
await writeFile(tempPath, content, "utf8");
|
|
81
|
+
// Atomic rename
|
|
82
|
+
const { rename } = await import("fs/promises");
|
|
83
|
+
await rename(tempPath, filePath);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// Direct append
|
|
87
|
+
await appendFile(filePath, content, "utf8");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Read all messages from JSONL file
|
|
92
|
+
* Includes metadata handling for backward compatibility
|
|
93
|
+
*/
|
|
94
|
+
async read(filePath) {
|
|
95
|
+
try {
|
|
96
|
+
const content = await readFile(filePath, "utf8");
|
|
97
|
+
const lines = content
|
|
98
|
+
.split(/\r?\n/)
|
|
99
|
+
.map((line) => line.trim())
|
|
100
|
+
.filter((line) => line.length > 0);
|
|
101
|
+
if (lines.length === 0) {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
const allMessages = [];
|
|
105
|
+
// Skip metadata line if present (first line with __meta__: true)
|
|
106
|
+
let startIndex = 0;
|
|
107
|
+
if (lines.length > 0) {
|
|
108
|
+
try {
|
|
109
|
+
const firstLine = JSON.parse(lines[0]);
|
|
110
|
+
if (firstLine.__meta__ === true) {
|
|
111
|
+
startIndex = 1; // Skip metadata line
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
// If first line is not valid JSON, throw error with line number
|
|
116
|
+
if (lines[0].trim().length > 0) {
|
|
117
|
+
// Only throw if line is not empty
|
|
118
|
+
throw new Error(`Invalid JSON at line 1: ${error}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
// Parse all messages
|
|
123
|
+
for (let i = startIndex; i < lines.length; i++) {
|
|
124
|
+
const line = lines[i];
|
|
125
|
+
try {
|
|
126
|
+
const message = JSON.parse(line);
|
|
127
|
+
allMessages.push(message);
|
|
128
|
+
}
|
|
129
|
+
catch (error) {
|
|
130
|
+
// Throw error for invalid JSON lines with line number
|
|
131
|
+
throw new Error(`Invalid JSON at line ${i + 1}: ${error}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return allMessages;
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (error.code === "ENOENT") {
|
|
138
|
+
return [];
|
|
139
|
+
}
|
|
140
|
+
throw new Error(`Failed to read JSONL file "${filePath}": ${error}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Get the last message from JSONL file using efficient file reading
|
|
145
|
+
*/
|
|
146
|
+
async getLastMessage(filePath) {
|
|
147
|
+
try {
|
|
148
|
+
// First check if file exists
|
|
149
|
+
try {
|
|
150
|
+
await stat(filePath);
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
if (err.code === "ENOENT") {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
throw err;
|
|
157
|
+
}
|
|
158
|
+
// Use our elegant utility to get the last line
|
|
159
|
+
const lastLine = await getLastLine(filePath);
|
|
160
|
+
if (!lastLine) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
try {
|
|
164
|
+
const parsed = JSON.parse(lastLine);
|
|
165
|
+
// Skip metadata line
|
|
166
|
+
if (parsed.__meta__ === true) {
|
|
167
|
+
// If the last line is metadata, the file only contains metadata
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
// Found a valid message
|
|
171
|
+
return parsed;
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
throw new Error(`Invalid JSON in last line of "${filePath}": ${error}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
catch (error) {
|
|
178
|
+
throw new Error(`Failed to get last message from "${filePath}": ${error}`);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Read session metadata from first line (streaming - only reads first line)
|
|
183
|
+
*/
|
|
184
|
+
async readMetadata(filePath) {
|
|
185
|
+
try {
|
|
186
|
+
// First check if file exists
|
|
187
|
+
try {
|
|
188
|
+
await stat(filePath);
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
if (err.code === "ENOENT") {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
throw err;
|
|
195
|
+
}
|
|
196
|
+
// Read the first line efficiently
|
|
197
|
+
const firstLine = await readFirstLine(filePath);
|
|
198
|
+
if (!firstLine) {
|
|
199
|
+
return null; // Empty file or first line
|
|
200
|
+
}
|
|
201
|
+
try {
|
|
202
|
+
const parsed = JSON.parse(firstLine);
|
|
203
|
+
if (parsed.__meta__ === true) {
|
|
204
|
+
return parsed;
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
return null; // First line is not metadata
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return null; // Invalid JSON on first line
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
throw new Error(`Failed to read metadata from "${filePath}": ${error}`);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Check if a session file has metadata (first line check only)
|
|
220
|
+
* Very efficient - only reads first line
|
|
221
|
+
*/
|
|
222
|
+
async hasMetadata(filePath) {
|
|
223
|
+
const metadata = await this.readMetadata(filePath);
|
|
224
|
+
return metadata !== null;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Validate messages before writing
|
|
228
|
+
*/
|
|
229
|
+
validateMessages(messages) {
|
|
230
|
+
for (let i = 0; i < messages.length; i++) {
|
|
231
|
+
const message = messages[i];
|
|
232
|
+
if (!message.role) {
|
|
233
|
+
throw new Error(`Message at index ${i} is missing required field: role`);
|
|
234
|
+
}
|
|
235
|
+
if (!message.blocks) {
|
|
236
|
+
throw new Error(`Message at index ${i} is missing required field: blocks`);
|
|
237
|
+
}
|
|
238
|
+
if (!Array.isArray(message.blocks)) {
|
|
239
|
+
throw new Error(`Message at index ${i} has invalid blocks field: must be an array`);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Ensure directory exists for the given file path
|
|
245
|
+
*/
|
|
246
|
+
async ensureDirectory(dirPath) {
|
|
247
|
+
try {
|
|
248
|
+
await mkdir(dirPath, { recursive: true });
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
const err = error;
|
|
252
|
+
if (err.code !== "EEXIST") {
|
|
253
|
+
throw error;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
import { MemoryStoreService } from "./memoryStore.js";
|
|
2
|
+
/**
|
|
3
|
+
* Initialize global memory store
|
|
4
|
+
*/
|
|
5
|
+
export declare const initializeMemoryStore: (memoryStore: MemoryStoreService) => void;
|
|
6
|
+
/**
|
|
7
|
+
* Get current memory store instance
|
|
8
|
+
*/
|
|
9
|
+
export declare const getMemoryStore: () => MemoryStoreService | null;
|
|
1
10
|
export declare const isMemoryMessage: (message: string) => boolean;
|
|
2
11
|
export declare const addMemory: (message: string, workdir: string) => Promise<void>;
|
|
3
12
|
export declare const ensureUserMemoryFile: () => Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/services/memory.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"memory.d.ts","sourceRoot":"","sources":["../../src/services/memory.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAMtD;;GAEG;AACH,eAAO,MAAM,qBAAqB,GAChC,aAAa,kBAAkB,KAC9B,IAEF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,QAAO,kBAAkB,GAAG,IAEtD,CAAC;AAGF,eAAO,MAAM,eAAe,GAAI,SAAS,MAAM,KAAG,OAEjD,CAAC;AAEF,eAAO,MAAM,SAAS,GACpB,SAAS,MAAM,EACf,SAAS,MAAM,KACd,OAAO,CAAC,IAAI,CAqDd,CAAC;AAGF,eAAO,MAAM,oBAAoB,QAAa,OAAO,CAAC,IAAI,CA4BzD,CAAC;AAEF,eAAO,MAAM,aAAa,GAAU,SAAS,MAAM,KAAG,OAAO,CAAC,IAAI,CAsBjE,CAAC;AAEF,eAAO,MAAM,oBAAoB,QAAa,OAAO,CAAC,MAAM,CAa3D,CAAC;AAGF,eAAO,MAAM,cAAc,GAAU,SAAS,MAAM,KAAG,OAAO,CAAC,MAAM,CA4CpE,CAAC;AAGF,eAAO,MAAM,wBAAwB,GACnC,SAAS,MAAM,KACd,OAAO,CAAC,MAAM,CAoBhB,CAAC"}
|
package/dist/services/memory.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { promises as fs } from "fs";
|
|
2
2
|
import path from "path";
|
|
3
3
|
import { USER_MEMORY_FILE, DATA_DIRECTORY } from "../utils/constants.js";
|
|
4
|
+
import { logger } from "../utils/globalLogger.js";
|
|
5
|
+
// Global memory store instance for project memory files
|
|
6
|
+
let globalMemoryStore = null;
|
|
7
|
+
/**
|
|
8
|
+
* Initialize global memory store
|
|
9
|
+
*/
|
|
10
|
+
export const initializeMemoryStore = (memoryStore) => {
|
|
11
|
+
globalMemoryStore = memoryStore;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Get current memory store instance
|
|
15
|
+
*/
|
|
16
|
+
export const getMemoryStore = () => {
|
|
17
|
+
return globalMemoryStore;
|
|
18
|
+
};
|
|
4
19
|
// Project memory related methods
|
|
5
20
|
export const isMemoryMessage = (message) => {
|
|
6
21
|
return message.trim().startsWith("#");
|
|
@@ -21,6 +36,9 @@ export const addMemory = async (message, workdir) => {
|
|
|
21
36
|
catch (error) {
|
|
22
37
|
// File does not exist, create new file
|
|
23
38
|
if (error.code === "ENOENT") {
|
|
39
|
+
logger.info("Memory file does not exist, will create new one", {
|
|
40
|
+
memoryFilePath,
|
|
41
|
+
});
|
|
24
42
|
existingContent =
|
|
25
43
|
"# Memory\n\nThis is the AI assistant's memory file, recording important information and context.\n\n";
|
|
26
44
|
}
|
|
@@ -32,10 +50,22 @@ export const addMemory = async (message, workdir) => {
|
|
|
32
50
|
const updatedContent = existingContent + memoryEntry;
|
|
33
51
|
// Write file
|
|
34
52
|
await fs.writeFile(memoryFilePath, updatedContent, "utf-8");
|
|
35
|
-
//
|
|
53
|
+
// Update memory store if available
|
|
54
|
+
if (globalMemoryStore) {
|
|
55
|
+
try {
|
|
56
|
+
await globalMemoryStore.updateContent(memoryFilePath);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
logger.warn(`Failed to update memory store for ${memoryFilePath}:`, error);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
logger.debug("No global memory store available, skipping store update");
|
|
64
|
+
}
|
|
65
|
+
logger.debug(`Memory added to ${memoryFilePath}:`, message);
|
|
36
66
|
}
|
|
37
67
|
catch (error) {
|
|
38
|
-
|
|
68
|
+
logger.error("Failed to add memory:", error);
|
|
39
69
|
throw new Error(`Failed to add memory: ${error.message}`);
|
|
40
70
|
}
|
|
41
71
|
};
|
|
@@ -51,9 +81,12 @@ export const ensureUserMemoryFile = async () => {
|
|
|
51
81
|
catch (error) {
|
|
52
82
|
// File does not exist, create new file
|
|
53
83
|
if (error.code === "ENOENT") {
|
|
84
|
+
logger.info("Creating new user memory file", {
|
|
85
|
+
userMemoryFile: USER_MEMORY_FILE,
|
|
86
|
+
});
|
|
54
87
|
const initialContent = "# User Memory\n\nThis is the user-level memory file, recording important information and context across projects.\n\n";
|
|
55
88
|
await fs.writeFile(USER_MEMORY_FILE, initialContent, "utf-8");
|
|
56
|
-
|
|
89
|
+
logger.debug(`Created user memory file: ${USER_MEMORY_FILE}`);
|
|
57
90
|
}
|
|
58
91
|
else {
|
|
59
92
|
throw error;
|
|
@@ -61,7 +94,7 @@ export const ensureUserMemoryFile = async () => {
|
|
|
61
94
|
}
|
|
62
95
|
}
|
|
63
96
|
catch (error) {
|
|
64
|
-
|
|
97
|
+
logger.error("Failed to ensure user memory file:", error);
|
|
65
98
|
throw new Error(`Failed to ensure user memory file: ${error.message}`);
|
|
66
99
|
}
|
|
67
100
|
};
|
|
@@ -77,33 +110,69 @@ export const addUserMemory = async (message) => {
|
|
|
77
110
|
const updatedContent = existingContent + memoryEntry;
|
|
78
111
|
// Write file
|
|
79
112
|
await fs.writeFile(USER_MEMORY_FILE, updatedContent, "utf-8");
|
|
80
|
-
|
|
113
|
+
logger.debug(`User memory added to ${USER_MEMORY_FILE}:`, message);
|
|
81
114
|
}
|
|
82
115
|
catch (error) {
|
|
83
|
-
|
|
116
|
+
logger.error("Failed to add user memory:", error);
|
|
84
117
|
throw new Error(`Failed to add user memory: ${error.message}`);
|
|
85
118
|
}
|
|
86
119
|
};
|
|
87
120
|
export const getUserMemoryContent = async () => {
|
|
88
121
|
try {
|
|
89
122
|
await ensureUserMemoryFile();
|
|
90
|
-
|
|
123
|
+
const content = await fs.readFile(USER_MEMORY_FILE, "utf-8");
|
|
124
|
+
logger.debug("User memory content read successfully", {
|
|
125
|
+
userMemoryFile: USER_MEMORY_FILE,
|
|
126
|
+
contentLength: content.length,
|
|
127
|
+
});
|
|
128
|
+
return content;
|
|
91
129
|
}
|
|
92
|
-
catch {
|
|
93
|
-
|
|
130
|
+
catch (error) {
|
|
131
|
+
logger.error("Failed to read user memory:", error);
|
|
94
132
|
return "";
|
|
95
133
|
}
|
|
96
134
|
};
|
|
97
|
-
// Read project memory file content
|
|
135
|
+
// Read project memory file content with memory store optimization
|
|
98
136
|
export const readMemoryFile = async (workdir) => {
|
|
137
|
+
const memoryFilePath = path.join(workdir, "AGENTS.md");
|
|
138
|
+
// Use memory store if available for optimized access
|
|
139
|
+
if (globalMemoryStore) {
|
|
140
|
+
logger.debug("Using memory store for optimized access", { memoryFilePath });
|
|
141
|
+
try {
|
|
142
|
+
const content = await globalMemoryStore.getContent(memoryFilePath);
|
|
143
|
+
logger.debug("Memory content retrieved from store successfully", {
|
|
144
|
+
memoryFilePath,
|
|
145
|
+
contentLength: content.length,
|
|
146
|
+
});
|
|
147
|
+
return content;
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
// Fallback to direct file access on memory store error
|
|
151
|
+
logger.warn(`Memory store access failed for ${memoryFilePath}, falling back to file system:`, error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
logger.debug("No memory store available, using direct file access", {
|
|
156
|
+
memoryFilePath,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
// Fallback to direct file access (original behavior)
|
|
99
160
|
try {
|
|
100
|
-
const
|
|
101
|
-
|
|
161
|
+
const content = await fs.readFile(memoryFilePath, "utf-8");
|
|
162
|
+
logger.debug("Memory file read successfully via direct file access", {
|
|
163
|
+
memoryFilePath,
|
|
164
|
+
contentLength: content.length,
|
|
165
|
+
});
|
|
166
|
+
return content;
|
|
102
167
|
}
|
|
103
168
|
catch (error) {
|
|
104
169
|
if (error.code === "ENOENT") {
|
|
170
|
+
logger.debug("Memory file does not exist, returning empty content", {
|
|
171
|
+
memoryFilePath,
|
|
172
|
+
});
|
|
105
173
|
return "";
|
|
106
174
|
}
|
|
175
|
+
logger.error("Failed to read memory file", { memoryFilePath, error });
|
|
107
176
|
throw error;
|
|
108
177
|
}
|
|
109
178
|
};
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Memory Store Service
|
|
3
|
+
*
|
|
4
|
+
* Provides in-memory storage for file content with automatic updates.
|
|
5
|
+
* Optimizes performance by keeping frequently accessed files in memory.
|
|
6
|
+
*/
|
|
7
|
+
import { EventEmitter } from "events";
|
|
8
|
+
import type { Logger } from "../types/index.js";
|
|
9
|
+
export interface MemoryStoreEntry {
|
|
10
|
+
content: string;
|
|
11
|
+
path: string;
|
|
12
|
+
lastModified: number;
|
|
13
|
+
isLoaded: boolean;
|
|
14
|
+
}
|
|
15
|
+
export interface MemoryStoreStats {
|
|
16
|
+
contentSize: number;
|
|
17
|
+
lastUpdated: number;
|
|
18
|
+
updateCount: number;
|
|
19
|
+
isLoaded: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface MemoryUpdateEvent {
|
|
22
|
+
path: string;
|
|
23
|
+
reason: "file_change" | "initial_load" | "manual_reload";
|
|
24
|
+
timestamp: number;
|
|
25
|
+
previousSize?: number;
|
|
26
|
+
newSize: number;
|
|
27
|
+
}
|
|
28
|
+
export declare class MemoryStoreService extends EventEmitter {
|
|
29
|
+
private store;
|
|
30
|
+
private updateCounts;
|
|
31
|
+
private logger?;
|
|
32
|
+
constructor(logger?: Logger);
|
|
33
|
+
/**
|
|
34
|
+
* Get content from memory store (loads from file if not loaded)
|
|
35
|
+
* Maps to FR-006: Keep AGENTS.md content in memory to avoid repeated reads
|
|
36
|
+
*/
|
|
37
|
+
getContent(path: string): Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Update memory content from file
|
|
40
|
+
* Maps to FR-007: Update memory content when file changes
|
|
41
|
+
*/
|
|
42
|
+
updateContent(path: string): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Get memory store statistics
|
|
45
|
+
* For monitoring and debugging
|
|
46
|
+
*/
|
|
47
|
+
getStats(path?: string): MemoryStoreStats;
|
|
48
|
+
/**
|
|
49
|
+
* Check if content is loaded in memory
|
|
50
|
+
* For status checking
|
|
51
|
+
*/
|
|
52
|
+
isLoaded(path: string): boolean;
|
|
53
|
+
/**
|
|
54
|
+
* Manually reload content from file
|
|
55
|
+
* For force refresh scenarios
|
|
56
|
+
*/
|
|
57
|
+
reloadContent(path: string): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Remove content from memory store
|
|
60
|
+
* For cleanup when file is deleted
|
|
61
|
+
*/
|
|
62
|
+
removeContent(path: string): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Clear all content from memory store
|
|
65
|
+
* For cleanup and testing
|
|
66
|
+
*/
|
|
67
|
+
clear(): void;
|
|
68
|
+
/**
|
|
69
|
+
* Get all stored paths
|
|
70
|
+
* For monitoring and debugging
|
|
71
|
+
*/
|
|
72
|
+
getStoredPaths(): string[];
|
|
73
|
+
/**
|
|
74
|
+
* Check if file exists and is accessible
|
|
75
|
+
*/
|
|
76
|
+
fileExists(path: string): Promise<boolean>;
|
|
77
|
+
private loadFromFile;
|
|
78
|
+
private incrementUpdateCount;
|
|
79
|
+
private emitUpdateEvent;
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=memoryStore.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memoryStore.d.ts","sourceRoot":"","sources":["../../src/services/memoryStore.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,YAAY,EAAE,MAAM,CAAC;IACrB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,aAAa,GAAG,cAAc,GAAG,eAAe,CAAC;IACzD,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,kBAAmB,SAAQ,YAAY;IAClD,OAAO,CAAC,KAAK,CAA4C;IACzD,OAAO,CAAC,YAAY,CAAkC;IACtD,OAAO,CAAC,MAAM,CAAC,CAAS;gBAEZ,MAAM,CAAC,EAAE,MAAM;IAK3B;;;OAGG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAY/C;;;OAGG;IACG,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOhD;;;OAGG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,gBAAgB;IAkCzC;;;OAGG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAK/B;;;OAGG;IACG,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAOhD;;;OAGG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAWpC;;;OAGG;IACH,KAAK,IAAI,IAAI;IAYb;;;OAGG;IACH,cAAc,IAAI,MAAM,EAAE;IAI1B;;OAEG;IACG,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;YASlC,YAAY;IA2E1B,OAAO,CAAC,oBAAoB;IAK5B,OAAO,CAAC,eAAe;CAmBxB"}
|