wave-agent-sdk 0.0.7 → 0.0.10

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.
Files changed (240) hide show
  1. package/dist/agent.d.ts +105 -24
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +438 -53
  4. package/dist/index.d.ts +4 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +4 -0
  7. package/dist/managers/aiManager.d.ts +18 -7
  8. package/dist/managers/aiManager.d.ts.map +1 -1
  9. package/dist/managers/aiManager.js +254 -142
  10. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  11. package/dist/managers/backgroundBashManager.js +11 -9
  12. package/dist/managers/hookManager.d.ts +6 -6
  13. package/dist/managers/hookManager.d.ts.map +1 -1
  14. package/dist/managers/hookManager.js +81 -39
  15. package/dist/managers/liveConfigManager.d.ts +95 -0
  16. package/dist/managers/liveConfigManager.d.ts.map +1 -0
  17. package/dist/managers/liveConfigManager.js +442 -0
  18. package/dist/managers/lspManager.d.ts +43 -0
  19. package/dist/managers/lspManager.d.ts.map +1 -0
  20. package/dist/managers/lspManager.js +326 -0
  21. package/dist/managers/messageManager.d.ts +41 -24
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +184 -73
  24. package/dist/managers/permissionManager.d.ts +66 -0
  25. package/dist/managers/permissionManager.d.ts.map +1 -0
  26. package/dist/managers/permissionManager.js +208 -0
  27. package/dist/managers/skillManager.d.ts +1 -0
  28. package/dist/managers/skillManager.d.ts.map +1 -1
  29. package/dist/managers/skillManager.js +2 -1
  30. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  31. package/dist/managers/slashCommandManager.js +4 -2
  32. package/dist/managers/subagentManager.d.ts +42 -6
  33. package/dist/managers/subagentManager.d.ts.map +1 -1
  34. package/dist/managers/subagentManager.js +213 -62
  35. package/dist/managers/toolManager.d.ts +38 -1
  36. package/dist/managers/toolManager.d.ts.map +1 -1
  37. package/dist/managers/toolManager.js +66 -2
  38. package/dist/services/aiService.d.ts +15 -5
  39. package/dist/services/aiService.d.ts.map +1 -1
  40. package/dist/services/aiService.js +446 -77
  41. package/dist/services/configurationService.d.ts +116 -0
  42. package/dist/services/configurationService.d.ts.map +1 -0
  43. package/dist/services/configurationService.js +585 -0
  44. package/dist/services/fileWatcher.d.ts +69 -0
  45. package/dist/services/fileWatcher.d.ts.map +1 -0
  46. package/dist/services/fileWatcher.js +212 -0
  47. package/dist/services/hook.d.ts +5 -40
  48. package/dist/services/hook.d.ts.map +1 -1
  49. package/dist/services/hook.js +47 -109
  50. package/dist/services/jsonlHandler.d.ts +71 -0
  51. package/dist/services/jsonlHandler.d.ts.map +1 -0
  52. package/dist/services/jsonlHandler.js +236 -0
  53. package/dist/services/memory.d.ts.map +1 -1
  54. package/dist/services/memory.js +33 -11
  55. package/dist/services/session.d.ts +116 -52
  56. package/dist/services/session.d.ts.map +1 -1
  57. package/dist/services/session.js +415 -143
  58. package/dist/tools/bashTool.d.ts.map +1 -1
  59. package/dist/tools/bashTool.js +77 -17
  60. package/dist/tools/deleteFileTool.d.ts.map +1 -1
  61. package/dist/tools/deleteFileTool.js +27 -1
  62. package/dist/tools/editTool.d.ts.map +1 -1
  63. package/dist/tools/editTool.js +33 -8
  64. package/dist/tools/lspTool.d.ts +6 -0
  65. package/dist/tools/lspTool.d.ts.map +1 -0
  66. package/dist/tools/lspTool.js +589 -0
  67. package/dist/tools/multiEditTool.d.ts.map +1 -1
  68. package/dist/tools/multiEditTool.js +30 -10
  69. package/dist/tools/readTool.d.ts.map +1 -1
  70. package/dist/tools/readTool.js +113 -3
  71. package/dist/tools/skillTool.js +2 -2
  72. package/dist/tools/todoWriteTool.d.ts.map +1 -1
  73. package/dist/tools/todoWriteTool.js +23 -0
  74. package/dist/tools/types.d.ts +11 -8
  75. package/dist/tools/types.d.ts.map +1 -1
  76. package/dist/tools/writeTool.d.ts.map +1 -1
  77. package/dist/tools/writeTool.js +30 -15
  78. package/dist/types/commands.d.ts +4 -1
  79. package/dist/types/commands.d.ts.map +1 -1
  80. package/dist/types/config.d.ts +4 -0
  81. package/dist/types/config.d.ts.map +1 -1
  82. package/dist/types/configuration.d.ts +69 -0
  83. package/dist/types/configuration.d.ts.map +1 -0
  84. package/dist/types/configuration.js +8 -0
  85. package/dist/types/core.d.ts +45 -0
  86. package/dist/types/core.d.ts.map +1 -1
  87. package/dist/types/environment.d.ts +83 -0
  88. package/dist/types/environment.d.ts.map +1 -0
  89. package/dist/types/environment.js +21 -0
  90. package/dist/types/fileSearch.d.ts +5 -0
  91. package/dist/types/fileSearch.d.ts.map +1 -0
  92. package/dist/types/fileSearch.js +1 -0
  93. package/dist/types/hooks.d.ts +18 -3
  94. package/dist/types/hooks.d.ts.map +1 -1
  95. package/dist/types/hooks.js +8 -8
  96. package/dist/types/index.d.ts +7 -0
  97. package/dist/types/index.d.ts.map +1 -1
  98. package/dist/types/index.js +7 -0
  99. package/dist/types/lsp.d.ts +90 -0
  100. package/dist/types/lsp.d.ts.map +1 -0
  101. package/dist/types/lsp.js +4 -0
  102. package/dist/types/messaging.d.ts +19 -12
  103. package/dist/types/messaging.d.ts.map +1 -1
  104. package/dist/types/permissions.d.ts +35 -0
  105. package/dist/types/permissions.d.ts.map +1 -0
  106. package/dist/types/permissions.js +12 -0
  107. package/dist/types/session.d.ts +15 -0
  108. package/dist/types/session.d.ts.map +1 -0
  109. package/dist/types/session.js +7 -0
  110. package/dist/types/skills.d.ts +1 -0
  111. package/dist/types/skills.d.ts.map +1 -1
  112. package/dist/types/tools.d.ts +35 -0
  113. package/dist/types/tools.d.ts.map +1 -0
  114. package/dist/types/tools.js +4 -0
  115. package/dist/utils/abortUtils.d.ts +34 -0
  116. package/dist/utils/abortUtils.d.ts.map +1 -0
  117. package/dist/utils/abortUtils.js +92 -0
  118. package/dist/utils/bashHistory.d.ts +4 -0
  119. package/dist/utils/bashHistory.d.ts.map +1 -1
  120. package/dist/utils/bashHistory.js +48 -30
  121. package/dist/utils/builtinSubagents.d.ts +7 -0
  122. package/dist/utils/builtinSubagents.d.ts.map +1 -0
  123. package/dist/utils/builtinSubagents.js +65 -0
  124. package/dist/utils/cacheControlUtils.d.ts +96 -0
  125. package/dist/utils/cacheControlUtils.d.ts.map +1 -0
  126. package/dist/utils/cacheControlUtils.js +324 -0
  127. package/dist/utils/commandPathResolver.d.ts +52 -0
  128. package/dist/utils/commandPathResolver.d.ts.map +1 -0
  129. package/dist/utils/commandPathResolver.js +145 -0
  130. package/dist/utils/configPaths.d.ts +85 -0
  131. package/dist/utils/configPaths.d.ts.map +1 -0
  132. package/dist/utils/configPaths.js +121 -0
  133. package/dist/utils/constants.d.ts +1 -13
  134. package/dist/utils/constants.d.ts.map +1 -1
  135. package/dist/utils/constants.js +2 -14
  136. package/dist/utils/convertMessagesForAPI.d.ts +2 -1
  137. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  138. package/dist/utils/convertMessagesForAPI.js +39 -18
  139. package/dist/utils/customCommands.d.ts.map +1 -1
  140. package/dist/utils/customCommands.js +66 -21
  141. package/dist/utils/fileSearch.d.ts +14 -0
  142. package/dist/utils/fileSearch.d.ts.map +1 -0
  143. package/dist/utils/fileSearch.js +88 -0
  144. package/dist/utils/fileUtils.d.ts +27 -0
  145. package/dist/utils/fileUtils.d.ts.map +1 -0
  146. package/dist/utils/fileUtils.js +145 -0
  147. package/dist/utils/globalLogger.d.ts +88 -0
  148. package/dist/utils/globalLogger.d.ts.map +1 -0
  149. package/dist/utils/globalLogger.js +120 -0
  150. package/dist/utils/largeOutputHandler.d.ts +15 -0
  151. package/dist/utils/largeOutputHandler.d.ts.map +1 -0
  152. package/dist/utils/largeOutputHandler.js +40 -0
  153. package/dist/utils/markdownParser.d.ts.map +1 -1
  154. package/dist/utils/markdownParser.js +1 -17
  155. package/dist/utils/mcpUtils.d.ts.map +1 -1
  156. package/dist/utils/mcpUtils.js +25 -3
  157. package/dist/utils/messageOperations.d.ts +20 -18
  158. package/dist/utils/messageOperations.d.ts.map +1 -1
  159. package/dist/utils/messageOperations.js +30 -38
  160. package/dist/utils/pathEncoder.d.ts +108 -0
  161. package/dist/utils/pathEncoder.d.ts.map +1 -0
  162. package/dist/utils/pathEncoder.js +279 -0
  163. package/dist/utils/subagentParser.d.ts +2 -2
  164. package/dist/utils/subagentParser.d.ts.map +1 -1
  165. package/dist/utils/subagentParser.js +12 -8
  166. package/dist/utils/tokenCalculation.d.ts +26 -0
  167. package/dist/utils/tokenCalculation.d.ts.map +1 -0
  168. package/dist/utils/tokenCalculation.js +36 -0
  169. package/dist/utils/tokenEstimator.d.ts +39 -0
  170. package/dist/utils/tokenEstimator.d.ts.map +1 -0
  171. package/dist/utils/tokenEstimator.js +55 -0
  172. package/package.json +6 -6
  173. package/src/agent.ts +586 -78
  174. package/src/index.ts +4 -0
  175. package/src/managers/aiManager.ts +341 -192
  176. package/src/managers/backgroundBashManager.ts +11 -9
  177. package/src/managers/hookManager.ts +102 -54
  178. package/src/managers/liveConfigManager.ts +634 -0
  179. package/src/managers/lspManager.ts +434 -0
  180. package/src/managers/messageManager.ts +258 -121
  181. package/src/managers/permissionManager.ts +276 -0
  182. package/src/managers/skillManager.ts +3 -1
  183. package/src/managers/slashCommandManager.ts +5 -3
  184. package/src/managers/subagentManager.ts +295 -76
  185. package/src/managers/toolManager.ts +95 -3
  186. package/src/services/aiService.ts +656 -84
  187. package/src/services/configurationService.ts +762 -0
  188. package/src/services/fileWatcher.ts +300 -0
  189. package/src/services/hook.ts +54 -144
  190. package/src/services/jsonlHandler.ts +303 -0
  191. package/src/services/memory.ts +34 -11
  192. package/src/services/session.ts +522 -173
  193. package/src/tools/bashTool.ts +94 -20
  194. package/src/tools/deleteFileTool.ts +38 -1
  195. package/src/tools/editTool.ts +44 -9
  196. package/src/tools/lspTool.ts +760 -0
  197. package/src/tools/multiEditTool.ts +41 -11
  198. package/src/tools/readTool.ts +127 -3
  199. package/src/tools/skillTool.ts +2 -2
  200. package/src/tools/todoWriteTool.ts +33 -1
  201. package/src/tools/types.ts +15 -9
  202. package/src/tools/writeTool.ts +43 -16
  203. package/src/types/commands.ts +6 -1
  204. package/src/types/config.ts +5 -0
  205. package/src/types/configuration.ts +73 -0
  206. package/src/types/core.ts +55 -0
  207. package/src/types/environment.ts +104 -0
  208. package/src/types/fileSearch.ts +4 -0
  209. package/src/types/hooks.ts +32 -16
  210. package/src/types/index.ts +7 -0
  211. package/src/types/lsp.ts +96 -0
  212. package/src/types/messaging.ts +21 -14
  213. package/src/types/permissions.ts +48 -0
  214. package/src/types/session.ts +20 -0
  215. package/src/types/skills.ts +1 -0
  216. package/src/types/tools.ts +38 -0
  217. package/src/utils/abortUtils.ts +118 -0
  218. package/src/utils/bashHistory.ts +55 -31
  219. package/src/utils/builtinSubagents.ts +71 -0
  220. package/src/utils/cacheControlUtils.ts +475 -0
  221. package/src/utils/commandPathResolver.ts +189 -0
  222. package/src/utils/configPaths.ts +163 -0
  223. package/src/utils/constants.ts +2 -17
  224. package/src/utils/convertMessagesForAPI.ts +44 -18
  225. package/src/utils/customCommands.ts +90 -22
  226. package/src/utils/fileSearch.ts +107 -0
  227. package/src/utils/fileUtils.ts +160 -0
  228. package/src/utils/globalLogger.ts +128 -0
  229. package/src/utils/largeOutputHandler.ts +55 -0
  230. package/src/utils/markdownParser.ts +1 -19
  231. package/src/utils/mcpUtils.ts +34 -3
  232. package/src/utils/messageOperations.ts +47 -53
  233. package/src/utils/pathEncoder.ts +394 -0
  234. package/src/utils/subagentParser.ts +13 -9
  235. package/src/utils/tokenCalculation.ts +43 -0
  236. package/src/utils/tokenEstimator.ts +68 -0
  237. package/dist/utils/configResolver.d.ts +0 -38
  238. package/dist/utils/configResolver.d.ts.map +0 -1
  239. package/dist/utils/configResolver.js +0 -106
  240. package/src/utils/configResolver.ts +0 -142
@@ -0,0 +1,303 @@
1
+ /**
2
+ * JSONL file operations service
3
+ * Handles reading and writing JSONL (JSON Lines) session files for improved performance
4
+ */
5
+
6
+ import { appendFile, readFile, writeFile, stat, mkdir } from "fs/promises";
7
+ import { dirname } from "path";
8
+ import { getLastLine } from "../utils/fileUtils.js";
9
+
10
+ import type { Message } from "../types/index.js";
11
+ import type { SessionMessage, SessionFilename } from "../types/session.js";
12
+
13
+ /**
14
+ * JSONL write options
15
+ */
16
+ export interface JsonlWriteOptions {
17
+ // Safety options
18
+ atomic?: boolean; // Default: true (write to temp file first)
19
+ }
20
+
21
+ /**
22
+ * JSONL handler class for message persistence operations
23
+ */
24
+ export class JsonlHandler {
25
+ private readonly defaultWriteOptions: Required<JsonlWriteOptions>;
26
+
27
+ constructor() {
28
+ this.defaultWriteOptions = {
29
+ atomic: true,
30
+ };
31
+ }
32
+
33
+ /**
34
+ * Create a new session file (simplified - no metadata header)
35
+ */
36
+ async createSession(filePath: string): Promise<void> {
37
+ // Ensure directory exists
38
+ await this.ensureDirectory(dirname(filePath));
39
+
40
+ // Create empty file (no metadata line needed)
41
+ await writeFile(filePath, "", "utf8");
42
+ }
43
+
44
+ /**
45
+ * Append a single message to JSONL file
46
+ */
47
+ async appendMessage(filePath: string, message: Message): Promise<void> {
48
+ return this.appendMessages(filePath, [message]);
49
+ }
50
+
51
+ /**
52
+ * Append multiple messages to JSONL file
53
+ */
54
+ async appendMessages(filePath: string, messages: Message[]): Promise<void> {
55
+ if (messages.length === 0) {
56
+ return;
57
+ }
58
+
59
+ // Convert to SessionMessage format with timestamps
60
+ const sessionMessages: SessionMessage[] = messages.map((message) => ({
61
+ ...message,
62
+ timestamp: new Date().toISOString(),
63
+ }));
64
+
65
+ return this.append(filePath, sessionMessages);
66
+ }
67
+
68
+ /**
69
+ * Append messages to JSONL file
70
+ */
71
+ async append(
72
+ filePath: string,
73
+ messages: SessionMessage[],
74
+ options?: JsonlWriteOptions,
75
+ ): Promise<void> {
76
+ if (messages.length === 0) {
77
+ return;
78
+ }
79
+
80
+ const opts = { ...this.defaultWriteOptions, ...options };
81
+
82
+ // Validate messages (always enabled for data integrity)
83
+ this.validateMessages(messages);
84
+
85
+ // Ensure directory exists
86
+ await this.ensureDirectory(dirname(filePath));
87
+
88
+ // Convert messages to JSONL lines (always compact JSON)
89
+ const lines = messages.map((message) => {
90
+ const { timestamp: existingTimestamp, ...messageWithoutTimestamp } =
91
+ message;
92
+ const messageWithTimestamp = {
93
+ timestamp: existingTimestamp || new Date().toISOString(),
94
+ ...messageWithoutTimestamp,
95
+ };
96
+
97
+ return JSON.stringify(messageWithTimestamp);
98
+ });
99
+
100
+ const content = lines.join("\n") + "\n";
101
+
102
+ if (opts.atomic) {
103
+ // Write to temp file first, then rename
104
+ const tempPath = `${filePath}.tmp`;
105
+ await writeFile(tempPath, content, "utf8");
106
+
107
+ // Atomic rename
108
+ const { rename } = await import("fs/promises");
109
+ await rename(tempPath, filePath);
110
+ } else {
111
+ // Direct append
112
+ await appendFile(filePath, content, "utf8");
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Read all messages from JSONL file (simplified - no metadata handling)
118
+ */
119
+ async read(filePath: string): Promise<SessionMessage[]> {
120
+ try {
121
+ const content = await readFile(filePath, "utf8");
122
+ const lines = content
123
+ .split(/\r?\n/)
124
+ .map((line: string) => line.trim())
125
+ .filter((line: string) => line.length > 0);
126
+
127
+ if (lines.length === 0) {
128
+ return [];
129
+ }
130
+
131
+ const allMessages: SessionMessage[] = [];
132
+
133
+ // Parse all messages (no metadata line to skip)
134
+ for (let i = 0; i < lines.length; i++) {
135
+ const line = lines[i];
136
+
137
+ try {
138
+ const message = JSON.parse(line) as SessionMessage;
139
+ if (message.timestamp) allMessages.push(message);
140
+ } catch (error) {
141
+ // Throw error for invalid JSON lines with line number
142
+ throw new Error(`Invalid JSON at line ${i + 1}: ${error}`);
143
+ }
144
+ }
145
+
146
+ return allMessages;
147
+ } catch (error) {
148
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
149
+ return [];
150
+ }
151
+ throw new Error(`Failed to read JSONL file "${filePath}": ${error}`);
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Get the last message from JSONL file using efficient file reading (simplified)
157
+ */
158
+ async getLastMessage(filePath: string): Promise<SessionMessage | null> {
159
+ try {
160
+ // First check if file exists
161
+ try {
162
+ await stat(filePath);
163
+ } catch (err) {
164
+ if ((err as NodeJS.ErrnoException).code === "ENOENT") {
165
+ return null;
166
+ }
167
+ throw err;
168
+ }
169
+
170
+ // Use our elegant utility to get the last line
171
+ const lastLine = await getLastLine(filePath);
172
+
173
+ if (!lastLine) {
174
+ return null;
175
+ }
176
+
177
+ try {
178
+ const parsed = JSON.parse(lastLine);
179
+ return parsed as SessionMessage;
180
+ } catch (error) {
181
+ throw new Error(`Invalid JSON in last line of "${filePath}": ${error}`);
182
+ }
183
+ } catch (error) {
184
+ throw new Error(
185
+ `Failed to get last message from "${filePath}": ${error}`,
186
+ );
187
+ }
188
+ }
189
+
190
+ /**
191
+ * Validate messages before writing
192
+ */
193
+ private validateMessages(messages: SessionMessage[]): void {
194
+ for (let i = 0; i < messages.length; i++) {
195
+ const message = messages[i];
196
+
197
+ if (!message.role) {
198
+ throw new Error(
199
+ `Message at index ${i} is missing required field: role`,
200
+ );
201
+ }
202
+
203
+ if (!message.blocks) {
204
+ throw new Error(
205
+ `Message at index ${i} is missing required field: blocks`,
206
+ );
207
+ }
208
+
209
+ if (!Array.isArray(message.blocks)) {
210
+ throw new Error(
211
+ `Message at index ${i} has invalid blocks field: must be an array`,
212
+ );
213
+ }
214
+ }
215
+ }
216
+
217
+ /**
218
+ * Ensure directory exists for the given file path
219
+ */
220
+ private async ensureDirectory(dirPath: string): Promise<void> {
221
+ try {
222
+ await mkdir(dirPath, { recursive: true });
223
+ } catch (error) {
224
+ const err = error as NodeJS.ErrnoException;
225
+ if (err.code !== "EEXIST") {
226
+ throw error;
227
+ }
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Parse session metadata from filename
233
+ * @param filePath - Path to the session file
234
+ * @returns Parsed session filename metadata
235
+ */
236
+ parseSessionFilename(filePath: string): SessionFilename {
237
+ // Extract filename from path
238
+ const filename = filePath.split("/").pop() || "";
239
+
240
+ // Check if it's a subagent session
241
+ const subagentMatch = filename.match(
242
+ /^subagent-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/,
243
+ );
244
+ if (subagentMatch) {
245
+ return {
246
+ sessionId: subagentMatch[1],
247
+ sessionType: "subagent",
248
+ };
249
+ }
250
+
251
+ // Check if it's a main session
252
+ const mainMatch = filename.match(
253
+ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/,
254
+ );
255
+ if (mainMatch) {
256
+ return {
257
+ sessionId: mainMatch[1],
258
+ sessionType: "main",
259
+ };
260
+ }
261
+
262
+ throw new Error(`Invalid session filename format: ${filename}`);
263
+ }
264
+
265
+ /**
266
+ * Validate filename format
267
+ * @param filename - Filename to validate
268
+ * @returns True if valid, false otherwise
269
+ */
270
+ isValidSessionFilename(filename: string): boolean {
271
+ // UUID validation patterns
272
+ const uuidPattern =
273
+ /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/;
274
+ const subagentPattern =
275
+ /^subagent-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/;
276
+
277
+ return uuidPattern.test(filename) || subagentPattern.test(filename);
278
+ }
279
+
280
+ /**
281
+ * Generate simple filename for sessions
282
+ * @param sessionId - UUID session identifier
283
+ * @param sessionType - Type of session ("main" or "subagent")
284
+ * @returns Generated filename
285
+ */
286
+ generateSessionFilename(
287
+ sessionId: string,
288
+ sessionType: "main" | "subagent",
289
+ ): string {
290
+ // Validate sessionId is a valid UUID
291
+ const uuidPattern =
292
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
293
+ if (!uuidPattern.test(sessionId)) {
294
+ throw new Error(`Invalid session ID format: ${sessionId}`);
295
+ }
296
+
297
+ if (sessionType === "subagent") {
298
+ return `subagent-${sessionId}.jsonl`;
299
+ } else {
300
+ return `${sessionId}.jsonl`;
301
+ }
302
+ }
303
+ }
@@ -1,6 +1,7 @@
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";
4
5
 
5
6
  // Project memory related methods
6
7
  export const isMemoryMessage = (message: string): boolean => {
@@ -28,6 +29,9 @@ export const addMemory = async (
28
29
  } catch (error) {
29
30
  // File does not exist, create new file
30
31
  if ((error as NodeJS.ErrnoException).code === "ENOENT") {
32
+ logger.info("Memory file does not exist, will create new one", {
33
+ memoryFilePath,
34
+ });
31
35
  existingContent =
32
36
  "# Memory\n\nThis is the AI assistant's memory file, recording important information and context.\n\n";
33
37
  } else {
@@ -41,9 +45,9 @@ export const addMemory = async (
41
45
  // Write file
42
46
  await fs.writeFile(memoryFilePath, updatedContent, "utf-8");
43
47
 
44
- // logger.debug(`Memory added to ${memoryFilePath}:`, message);
48
+ logger.debug(`Memory added to ${memoryFilePath}:`, message);
45
49
  } catch (error) {
46
- // logger.error("Failed to add memory:", error);
50
+ logger.error("Failed to add memory:", error);
47
51
  throw new Error(`Failed to add memory: ${(error as Error).message}`);
48
52
  }
49
53
  };
@@ -60,16 +64,19 @@ export const ensureUserMemoryFile = async (): Promise<void> => {
60
64
  } catch (error) {
61
65
  // File does not exist, create new file
62
66
  if ((error as NodeJS.ErrnoException).code === "ENOENT") {
67
+ logger.info("Creating new user memory file", {
68
+ userMemoryFile: USER_MEMORY_FILE,
69
+ });
63
70
  const initialContent =
64
71
  "# User Memory\n\nThis is the user-level memory file, recording important information and context across projects.\n\n";
65
72
  await fs.writeFile(USER_MEMORY_FILE, initialContent, "utf-8");
66
- // logger.debug(`Created user memory file: ${USER_MEMORY_FILE}`);
73
+ logger.debug(`Created user memory file: ${USER_MEMORY_FILE}`);
67
74
  } else {
68
75
  throw error;
69
76
  }
70
77
  }
71
78
  } catch (error) {
72
- // logger.error("Failed to ensure user memory file:", error);
79
+ logger.error("Failed to ensure user memory file:", error);
73
80
  throw new Error(
74
81
  `Failed to ensure user memory file: ${(error as Error).message}`,
75
82
  );
@@ -93,9 +100,9 @@ export const addUserMemory = async (message: string): Promise<void> => {
93
100
  // Write file
94
101
  await fs.writeFile(USER_MEMORY_FILE, updatedContent, "utf-8");
95
102
 
96
- // logger.debug(`User memory added to ${USER_MEMORY_FILE}:`, message);
103
+ logger.debug(`User memory added to ${USER_MEMORY_FILE}:`, message);
97
104
  } catch (error) {
98
- // logger.error("Failed to add user memory:", error);
105
+ logger.error("Failed to add user memory:", error);
99
106
  throw new Error(`Failed to add user memory: ${(error as Error).message}`);
100
107
  }
101
108
  };
@@ -103,22 +110,38 @@ export const addUserMemory = async (message: string): Promise<void> => {
103
110
  export const getUserMemoryContent = async (): Promise<string> => {
104
111
  try {
105
112
  await ensureUserMemoryFile();
106
- return await fs.readFile(USER_MEMORY_FILE, "utf-8");
107
- } catch {
108
- // logger.error("Failed to read user memory:", error);
113
+ const content = await fs.readFile(USER_MEMORY_FILE, "utf-8");
114
+ logger.debug("User memory content read successfully", {
115
+ userMemoryFile: USER_MEMORY_FILE,
116
+ contentLength: content.length,
117
+ });
118
+ return content;
119
+ } catch (error) {
120
+ logger.error("Failed to read user memory:", error);
109
121
  return "";
110
122
  }
111
123
  };
112
124
 
113
125
  // Read project memory file content
114
126
  export const readMemoryFile = async (workdir: string): Promise<string> => {
127
+ const memoryFilePath = path.join(workdir, "AGENTS.md");
128
+
129
+ // Direct file access
115
130
  try {
116
- const memoryFilePath = path.join(workdir, "AGENTS.md");
117
- return await fs.readFile(memoryFilePath, "utf-8");
131
+ const content = await fs.readFile(memoryFilePath, "utf-8");
132
+ logger.debug("Memory file read successfully via direct file access", {
133
+ memoryFilePath,
134
+ contentLength: content.length,
135
+ });
136
+ return content;
118
137
  } catch (error) {
119
138
  if ((error as NodeJS.ErrnoException).code === "ENOENT") {
139
+ logger.debug("Memory file does not exist, returning empty content", {
140
+ memoryFilePath,
141
+ });
120
142
  return "";
121
143
  }
144
+ logger.error("Failed to read memory file", { memoryFilePath, error });
122
145
  throw error;
123
146
  }
124
147
  };