wave-agent-sdk 0.5.0 → 0.6.0

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 (148) hide show
  1. package/dist/agent.d.ts +7 -2
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +58 -74
  4. package/dist/constants/prompts.d.ts +18 -14
  5. package/dist/constants/prompts.d.ts.map +1 -1
  6. package/dist/constants/prompts.js +134 -54
  7. package/dist/constants/tools.d.ts +4 -1
  8. package/dist/constants/tools.d.ts.map +1 -1
  9. package/dist/constants/tools.js +4 -1
  10. package/dist/index.d.ts +1 -0
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -0
  13. package/dist/managers/aiManager.d.ts +2 -5
  14. package/dist/managers/aiManager.d.ts.map +1 -1
  15. package/dist/managers/aiManager.js +59 -48
  16. package/dist/managers/backgroundTaskManager.d.ts.map +1 -1
  17. package/dist/managers/backgroundTaskManager.js +59 -53
  18. package/dist/managers/foregroundTaskManager.d.ts.map +1 -1
  19. package/dist/managers/foregroundTaskManager.js +3 -2
  20. package/dist/managers/mcpManager.d.ts.map +1 -1
  21. package/dist/managers/messageManager.d.ts +7 -3
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +28 -24
  24. package/dist/managers/permissionManager.d.ts.map +1 -1
  25. package/dist/managers/permissionManager.js +25 -15
  26. package/dist/managers/planManager.d.ts +1 -1
  27. package/dist/managers/planManager.d.ts.map +1 -1
  28. package/dist/managers/planManager.js +2 -2
  29. package/dist/managers/subagentManager.d.ts +4 -0
  30. package/dist/managers/subagentManager.d.ts.map +1 -1
  31. package/dist/managers/subagentManager.js +22 -14
  32. package/dist/managers/toolManager.d.ts +11 -0
  33. package/dist/managers/toolManager.d.ts.map +1 -1
  34. package/dist/managers/toolManager.js +20 -2
  35. package/dist/services/aiService.d.ts +0 -1
  36. package/dist/services/aiService.d.ts.map +1 -1
  37. package/dist/services/aiService.js +4 -140
  38. package/dist/services/memory.d.ts +0 -3
  39. package/dist/services/memory.d.ts.map +1 -1
  40. package/dist/services/memory.js +0 -59
  41. package/dist/services/session.d.ts +3 -1
  42. package/dist/services/session.d.ts.map +1 -1
  43. package/dist/services/session.js +16 -1
  44. package/dist/services/taskManager.d.ts +21 -0
  45. package/dist/services/taskManager.d.ts.map +1 -0
  46. package/dist/services/taskManager.js +158 -0
  47. package/dist/tools/askUserQuestion.d.ts.map +1 -1
  48. package/dist/tools/askUserQuestion.js +39 -25
  49. package/dist/tools/bashTool.d.ts.map +1 -1
  50. package/dist/tools/bashTool.js +7 -9
  51. package/dist/tools/editTool.d.ts.map +1 -1
  52. package/dist/tools/editTool.js +2 -1
  53. package/dist/tools/exitPlanMode.d.ts.map +1 -1
  54. package/dist/tools/exitPlanMode.js +25 -1
  55. package/dist/tools/globTool.d.ts.map +1 -1
  56. package/dist/tools/globTool.js +8 -2
  57. package/dist/tools/grepTool.d.ts.map +1 -1
  58. package/dist/tools/grepTool.js +17 -6
  59. package/dist/tools/lsTool.d.ts.map +1 -1
  60. package/dist/tools/lsTool.js +3 -1
  61. package/dist/tools/readTool.d.ts.map +1 -1
  62. package/dist/tools/readTool.js +16 -1
  63. package/dist/tools/taskManagementTools.d.ts +6 -0
  64. package/dist/tools/taskManagementTools.d.ts.map +1 -0
  65. package/dist/tools/taskManagementTools.js +453 -0
  66. package/dist/tools/taskOutputTool.d.ts.map +1 -1
  67. package/dist/tools/taskOutputTool.js +32 -8
  68. package/dist/tools/taskStopTool.d.ts.map +1 -1
  69. package/dist/tools/taskStopTool.js +7 -1
  70. package/dist/tools/taskTool.d.ts.map +1 -1
  71. package/dist/tools/taskTool.js +6 -1
  72. package/dist/tools/types.d.ts +9 -0
  73. package/dist/tools/types.d.ts.map +1 -1
  74. package/dist/tools/writeTool.d.ts.map +1 -1
  75. package/dist/tools/writeTool.js +9 -1
  76. package/dist/types/index.d.ts +1 -0
  77. package/dist/types/index.d.ts.map +1 -1
  78. package/dist/types/index.js +1 -0
  79. package/dist/types/messaging.d.ts +2 -8
  80. package/dist/types/messaging.d.ts.map +1 -1
  81. package/dist/types/processes.d.ts +11 -6
  82. package/dist/types/processes.d.ts.map +1 -1
  83. package/dist/types/tasks.d.ts +13 -0
  84. package/dist/types/tasks.d.ts.map +1 -0
  85. package/dist/types/tasks.js +1 -0
  86. package/dist/types/tools.d.ts +4 -1
  87. package/dist/types/tools.d.ts.map +1 -1
  88. package/dist/utils/builtinSubagents.d.ts.map +1 -1
  89. package/dist/utils/builtinSubagents.js +38 -1
  90. package/dist/utils/cacheControlUtils.d.ts.map +1 -1
  91. package/dist/utils/cacheControlUtils.js +18 -12
  92. package/dist/utils/constants.d.ts +0 -4
  93. package/dist/utils/constants.d.ts.map +1 -1
  94. package/dist/utils/constants.js +0 -4
  95. package/dist/utils/convertMessagesForAPI.js +2 -2
  96. package/dist/utils/messageOperations.d.ts +2 -30
  97. package/dist/utils/messageOperations.d.ts.map +1 -1
  98. package/dist/utils/messageOperations.js +4 -79
  99. package/dist/utils/nameGenerator.d.ts +1 -1
  100. package/dist/utils/nameGenerator.d.ts.map +1 -1
  101. package/dist/utils/nameGenerator.js +19 -3
  102. package/package.json +1 -1
  103. package/src/agent.ts +79 -84
  104. package/src/constants/prompts.ts +161 -65
  105. package/src/constants/tools.ts +4 -1
  106. package/src/index.ts +1 -0
  107. package/src/managers/aiManager.ts +79 -70
  108. package/src/managers/backgroundTaskManager.ts +53 -54
  109. package/src/managers/foregroundTaskManager.ts +3 -2
  110. package/src/managers/mcpManager.ts +6 -3
  111. package/src/managers/messageManager.ts +37 -26
  112. package/src/managers/permissionManager.ts +32 -21
  113. package/src/managers/planManager.ts +2 -2
  114. package/src/managers/subagentManager.ts +33 -14
  115. package/src/managers/toolManager.ts +32 -2
  116. package/src/services/aiService.ts +3 -145
  117. package/src/services/memory.ts +0 -72
  118. package/src/services/session.ts +21 -0
  119. package/src/services/taskManager.ts +188 -0
  120. package/src/tools/askUserQuestion.ts +51 -29
  121. package/src/tools/bashTool.ts +9 -15
  122. package/src/tools/editTool.ts +3 -1
  123. package/src/tools/exitPlanMode.ts +26 -2
  124. package/src/tools/globTool.ts +10 -2
  125. package/src/tools/grepTool.ts +17 -6
  126. package/src/tools/lsTool.ts +3 -1
  127. package/src/tools/readTool.ts +17 -1
  128. package/src/tools/taskManagementTools.ts +498 -0
  129. package/src/tools/taskOutputTool.ts +34 -12
  130. package/src/tools/taskStopTool.ts +7 -1
  131. package/src/tools/taskTool.ts +7 -1
  132. package/src/tools/types.ts +10 -0
  133. package/src/tools/writeTool.ts +9 -2
  134. package/src/types/index.ts +1 -0
  135. package/src/types/messaging.ts +1 -9
  136. package/src/types/processes.ts +13 -7
  137. package/src/types/tasks.ts +13 -0
  138. package/src/types/tools.ts +4 -1
  139. package/src/utils/builtinSubagents.ts +47 -1
  140. package/src/utils/cacheControlUtils.ts +26 -18
  141. package/src/utils/constants.ts +0 -5
  142. package/src/utils/convertMessagesForAPI.ts +2 -2
  143. package/src/utils/messageOperations.ts +5 -116
  144. package/src/utils/nameGenerator.ts +20 -3
  145. package/dist/tools/todoWriteTool.d.ts +0 -6
  146. package/dist/tools/todoWriteTool.d.ts.map +0 -1
  147. package/dist/tools/todoWriteTool.js +0 -220
  148. package/src/tools/todoWriteTool.ts +0 -257
@@ -323,40 +323,30 @@ export class PermissionManager {
323
323
  workdir,
324
324
  );
325
325
  if (!isInside) {
326
- this.logger?.warn(
327
- "File operation outside the Safe Zone in acceptEdits mode",
326
+ this.logger?.info(
327
+ "File operation outside the Safe Zone in acceptEdits mode, falling back to manual confirmation",
328
328
  {
329
329
  toolName: context.toolName,
330
330
  targetPath,
331
331
  resolvedPath,
332
332
  },
333
333
  );
334
- return {
335
- behavior: "deny",
336
- message: `Tool '${context.toolName}' attempted to modify a file outside the Safe Zone: ${targetPath}. Operations outside the Safe Zone always require manual confirmation.`,
337
- };
334
+ // Fall through to normal permission check flow to trigger confirmation prompt
335
+ } else {
336
+ this.logger?.debug(
337
+ "Permission automatically accepted for tool in acceptEdits mode",
338
+ {
339
+ toolName: context.toolName,
340
+ },
341
+ );
342
+ return { behavior: "allow" };
338
343
  }
339
344
  }
340
-
341
- this.logger?.debug(
342
- "Permission automatically accepted for tool in acceptEdits mode",
343
- {
344
- toolName: context.toolName,
345
- },
346
- );
347
- return { behavior: "allow" };
348
345
  }
349
346
  }
350
347
 
351
348
  // 1.3 If plan mode, allow Read-only tools and Edit/Write for plan file
352
349
  if (context.permissionMode === "plan") {
353
- if (context.toolName === BASH_TOOL_NAME) {
354
- return {
355
- behavior: "deny",
356
- message: "Bash commands are not allowed in plan mode.",
357
- };
358
- }
359
-
360
350
  const writeTools = [
361
351
  EDIT_TOOL_NAME,
362
352
  MULTI_EDIT_TOOL_NAME,
@@ -492,6 +482,27 @@ export class PermissionManager {
492
482
  suggestedPrefix,
493
483
  };
494
484
 
485
+ // Set hidePersistentOption for out-of-bounds file operations
486
+ const fileTools = [
487
+ EDIT_TOOL_NAME,
488
+ MULTI_EDIT_TOOL_NAME,
489
+ DELETE_FILE_TOOL_NAME,
490
+ WRITE_TOOL_NAME,
491
+ ];
492
+ if (fileTools.includes(toolName)) {
493
+ const targetPath = (toolInput?.file_path || toolInput?.target_file) as
494
+ | string
495
+ | undefined;
496
+ const workdir = toolInput?.workdir as string | undefined;
497
+
498
+ if (targetPath) {
499
+ const { isInside } = this.isInsideSafeZone(targetPath, workdir);
500
+ if (!isInside) {
501
+ context.hidePersistentOption = true;
502
+ }
503
+ }
504
+ }
505
+
495
506
  // Set hidePersistentOption for dangerous or out-of-bounds bash commands
496
507
  if (toolName === BASH_TOOL_NAME && toolInput?.command) {
497
508
  const command = String(toolInput.command);
@@ -17,7 +17,7 @@ export class PlanManager {
17
17
  /**
18
18
  * Ensures the plan directory exists and generates a new plan file path with a random name
19
19
  */
20
- public async getOrGeneratePlanFilePath(): Promise<{
20
+ public async getOrGeneratePlanFilePath(seed?: string): Promise<{
21
21
  path: string;
22
22
  name: string;
23
23
  }> {
@@ -30,7 +30,7 @@ export class PlanManager {
30
30
  );
31
31
  throw error;
32
32
  }
33
- const name = generateRandomName();
33
+ const name = generateRandomName(seed);
34
34
  const filePath = path.join(this.planDir, `${name}.md`);
35
35
  this.logger?.info(`Generated plan file path: ${filePath}`);
36
36
  return { path: filePath, name };
@@ -51,6 +51,11 @@ export interface SubagentManagerCallbacks {
51
51
  ) => void;
52
52
  /** Triggered when subagent messages change */
53
53
  onSubagentMessagesChange?: (subagentId: string, messages: Message[]) => void;
54
+ /** Triggered when subagent latest total tokens change */
55
+ onSubagentLatestTotalTokensChange?: (
56
+ subagentId: string,
57
+ tokens: number,
58
+ ) => void;
54
59
  }
55
60
 
56
61
  export interface SubagentInstance {
@@ -69,6 +74,7 @@ export interface SubagentManagerOptions {
69
74
  workdir: string;
70
75
  parentToolManager: ToolManager;
71
76
  parentMessageManager: MessageManager;
77
+ taskManager: import("../services/taskManager.js").TaskManager;
72
78
  callbacks?: SubagentManagerCallbacks; // Use SubagentManagerCallbacks instead of parentCallbacks
73
79
  logger?: Logger;
74
80
  getGatewayConfig: () => GatewayConfig;
@@ -88,6 +94,7 @@ export class SubagentManager {
88
94
  private workdir: string;
89
95
  private parentToolManager: ToolManager;
90
96
  private parentMessageManager: MessageManager;
97
+ private taskManager: import("../services/taskManager.js").TaskManager;
91
98
  private callbacks?: SubagentManagerCallbacks; // Use SubagentManagerCallbacks instead of parentCallbacks
92
99
  private logger?: Logger;
93
100
  private getGatewayConfig: () => GatewayConfig;
@@ -103,6 +110,7 @@ export class SubagentManager {
103
110
  this.workdir = options.workdir;
104
111
  this.parentToolManager = options.parentToolManager;
105
112
  this.parentMessageManager = options.parentMessageManager;
113
+ this.taskManager = options.taskManager;
106
114
  this.callbacks = options.callbacks; // Store SubagentManagerCallbacks
107
115
  this.logger = options.logger;
108
116
  this.getGatewayConfig = options.getGatewayConfig;
@@ -196,6 +204,7 @@ export class SubagentManager {
196
204
  const aiManager = new AIManager({
197
205
  messageManager,
198
206
  toolManager,
207
+ taskManager: this.taskManager,
199
208
  logger: this.logger,
200
209
  workdir: this.workdir,
201
210
  systemPrompt: configuration.systemPrompt,
@@ -294,6 +303,7 @@ export class SubagentManager {
294
303
  description: instance.configuration.description,
295
304
  stdout: "",
296
305
  stderr: "",
306
+ onStop: () => instance.aiManager.abortAIMessage(),
297
307
  });
298
308
 
299
309
  instance.backgroundTaskId = taskId;
@@ -359,6 +369,7 @@ export class SubagentManager {
359
369
  description: instance.configuration.description,
360
370
  stdout: "",
361
371
  stderr: "",
372
+ onStop: () => instance.aiManager.abortAIMessage(),
362
373
  });
363
374
 
364
375
  instance.backgroundTaskId = taskId;
@@ -378,24 +389,19 @@ export class SubagentManager {
378
389
  ): Promise<string> {
379
390
  // Set up consolidated abort handler to prevent listener accumulation
380
391
  let abortCleanup: (() => void) | undefined;
381
- if (abortSignal) {
392
+ // Only link to parent abort signal if NOT running in background
393
+ if (abortSignal && !instance.backgroundTaskId) {
382
394
  abortCleanup = addConsolidatedAbortListener(abortSignal, [
383
395
  () => {
384
396
  // Update status to aborted
385
- // Only update status if it's NOT a background task
386
- if (!instance.backgroundTaskId) {
387
- this.updateInstanceStatus(instance.subagentId, "aborted");
388
- this.parentMessageManager.updateSubagentBlock(instance.subagentId, {
389
- status: "aborted",
390
- });
391
- }
397
+ this.updateInstanceStatus(instance.subagentId, "aborted");
398
+ this.parentMessageManager.updateSubagentBlock(instance.subagentId, {
399
+ status: "aborted",
400
+ });
392
401
  },
393
402
  () => {
394
403
  // Abort the AI execution
395
- // Only abort if it's NOT a background task
396
- if (!instance.backgroundTaskId) {
397
- instance.aiManager.abortAIMessage();
398
- }
404
+ instance.aiManager.abortAIMessage();
399
405
  },
400
406
  ]);
401
407
  }
@@ -441,8 +447,6 @@ export class SubagentManager {
441
447
  });
442
448
 
443
449
  // If we have an abort signal, race against it using utilities to prevent listener accumulation
444
- // BUT: If this is a background task, we DON'T want to race against the abort signal
445
- // because the abort signal (Esc) should only stop the tool watching the task, not the task itself.
446
450
  if (abortSignal && !instance.backgroundTaskId) {
447
451
  await Promise.race([
448
452
  executeAI,
@@ -623,6 +627,7 @@ export class SubagentManager {
623
627
  const aiManager = new AIManager({
624
628
  messageManager,
625
629
  toolManager,
630
+ taskManager: this.taskManager,
626
631
  logger: this.logger,
627
632
  workdir: this.workdir,
628
633
  systemPrompt: configuration.systemPrompt,
@@ -734,6 +739,20 @@ export class SubagentManager {
734
739
  sessionId: newSessionId,
735
740
  });
736
741
  },
742
+
743
+ onLatestTotalTokensChange: (tokens: number) => {
744
+ // Forward latest total tokens to parent via SubagentManager callbacks
745
+ if (this.callbacks?.onSubagentLatestTotalTokensChange) {
746
+ this.callbacks.onSubagentLatestTotalTokensChange(subagentId, tokens);
747
+ }
748
+ },
749
+
750
+ onFileHistoryBlockAdded: (
751
+ snapshots: import("../types/reversion.js").FileSnapshot[],
752
+ ) => {
753
+ // Forward file history blocks to parent MessageManager
754
+ this.parentMessageManager.addFileHistoryBlock(snapshots);
755
+ },
737
756
  };
738
757
  }
739
758
  }
@@ -13,10 +13,15 @@ import { globTool } from "../tools/globTool.js";
13
13
  import { grepTool } from "../tools/grepTool.js";
14
14
  import { lsTool } from "../tools/lsTool.js";
15
15
  import { readTool } from "../tools/readTool.js";
16
- import { todoWriteTool } from "../tools/todoWriteTool.js";
17
16
  import { lspTool } from "../tools/lspTool.js";
18
17
  import { createTaskTool } from "../tools/taskTool.js";
19
18
  import { createSkillTool } from "../tools/skillTool.js";
19
+ import {
20
+ taskCreateTool,
21
+ taskGetTool,
22
+ taskUpdateTool,
23
+ taskListTool,
24
+ } from "../tools/taskManagementTools.js";
20
25
  import { McpManager } from "./mcpManager.js";
21
26
  import { PermissionManager } from "./permissionManager.js";
22
27
  import { ChatCompletionFunctionTool } from "openai/resources.js";
@@ -39,6 +44,8 @@ export interface ToolManagerOptions {
39
44
  permissionManager?: PermissionManager;
40
45
  /** Foreground task manager for backgrounding tasks */
41
46
  foregroundTaskManager?: import("../types/processes.js").IForegroundTaskManager;
47
+ /** Task manager for task management */
48
+ taskManager?: import("../services/taskManager.js").TaskManager;
42
49
  /** Reversion manager for file snapshots */
43
50
  reversionManager?: ReversionManager;
44
51
  /** Background task manager for background execution */
@@ -63,6 +70,7 @@ class ToolManager {
63
70
  private permissionManager?: PermissionManager;
64
71
  private foregroundTaskManager?: import("../types/processes.js").IForegroundTaskManager;
65
72
  private reversionManager?: ReversionManager;
73
+ private taskManager?: import("../services/taskManager.js").TaskManager;
66
74
  private backgroundTaskManager?: import("./backgroundTaskManager.js").BackgroundTaskManager;
67
75
  private permissionMode?: PermissionMode;
68
76
  private canUseToolCallback?: PermissionCallback;
@@ -72,6 +80,7 @@ class ToolManager {
72
80
  this.lspManager = options.lspManager;
73
81
  this.logger = options.logger;
74
82
  this.permissionManager = options.permissionManager;
83
+ this.taskManager = options.taskManager;
75
84
  this.foregroundTaskManager = options.foregroundTaskManager;
76
85
  this.reversionManager = options.reversionManager;
77
86
  this.backgroundTaskManager = options.backgroundTaskManager;
@@ -128,8 +137,11 @@ class ToolManager {
128
137
  grepTool,
129
138
  lsTool,
130
139
  readTool,
131
- todoWriteTool,
132
140
  lspTool,
141
+ taskCreateTool,
142
+ taskGetTool,
143
+ taskUpdateTool,
144
+ taskListTool,
133
145
  ];
134
146
 
135
147
  for (const tool of builtInTools) {
@@ -177,11 +189,13 @@ class ToolManager {
177
189
  permissionMode: effectivePermissionMode,
178
190
  canUseToolCallback: this.canUseToolCallback,
179
191
  permissionManager: this.permissionManager,
192
+ taskManager: this.taskManager!,
180
193
  reversionManager: this.reversionManager,
181
194
  backgroundTaskManager: this.backgroundTaskManager,
182
195
  foregroundTaskManager: this.foregroundTaskManager,
183
196
  mcpManager: this.mcpManager,
184
197
  lspManager: this.lspManager,
198
+ sessionId: context.sessionId,
185
199
  };
186
200
 
187
201
  this.logger?.debug("Executing tool with enhanced context", {
@@ -252,6 +266,13 @@ class ToolManager {
252
266
  return [...builtInToolsConfig, ...mcpToolsConfig];
253
267
  }
254
268
 
269
+ /**
270
+ * Get the list of registered tool plugins
271
+ */
272
+ public getTools(): ToolPlugin[] {
273
+ return Array.from(this.tools.values());
274
+ }
275
+
255
276
  /**
256
277
  * Get the current permission mode
257
278
  */
@@ -278,6 +299,15 @@ class ToolManager {
278
299
  public getPermissionManager(): PermissionManager | undefined {
279
300
  return this.permissionManager;
280
301
  }
302
+
303
+ /**
304
+ * Get the task manager
305
+ */
306
+ public getTaskManager():
307
+ | import("../services/taskManager.js").TaskManager
308
+ | undefined {
309
+ return this.taskManager;
310
+ }
281
311
  }
282
312
 
283
313
  // Export tool registry class and types
@@ -21,10 +21,7 @@ import * as os from "os";
21
21
  import * as fs from "fs";
22
22
  import * as path from "path";
23
23
 
24
- import {
25
- DEFAULT_SYSTEM_PROMPT,
26
- buildSystemPrompt,
27
- } from "../constants/prompts.js";
24
+ import { COMPRESS_MESSAGES_SYSTEM_PROMPT } from "../constants/prompts.js";
28
25
 
29
26
  /**
30
27
  * Interface for debug data saved during 400 errors
@@ -66,28 +63,6 @@ interface ErrorData {
66
63
  * Instead of parsing JSON, we use the raw chunk for efficient streaming
67
64
  */
68
65
 
69
- /**
70
- * Check if a directory is a git repository
71
- * @param dirPath Directory path to check
72
- * @returns "Yes" if it's a git repo, "No" otherwise
73
- */
74
- function isGitRepository(dirPath: string): string {
75
- try {
76
- // Check if .git directory exists in current directory or any parent directory
77
- let currentPath = path.resolve(dirPath);
78
- while (currentPath !== path.dirname(currentPath)) {
79
- const gitPath = path.join(currentPath, ".git");
80
- if (fs.existsSync(gitPath)) {
81
- return "Yes";
82
- }
83
- currentPath = path.dirname(currentPath);
84
- }
85
- return "No";
86
- } catch {
87
- return "No";
88
- }
89
- }
90
-
91
66
  /**
92
67
  * OpenAI model configuration type, based on OpenAI parameters but excluding messages
93
68
  */
@@ -144,7 +119,6 @@ export interface CallAgentOptions {
144
119
  messages: ChatCompletionMessageParam[];
145
120
  sessionId?: string;
146
121
  abortSignal?: AbortSignal;
147
- memory?: string; // Memory content parameter, content read from AGENTS.md
148
122
  workdir: string; // Current working directory
149
123
  tools?: ChatCompletionFunctionTool[]; // Tool configuration
150
124
  model?: string; // Custom model
@@ -187,7 +161,6 @@ export async function callAgent(
187
161
  modelConfig,
188
162
  messages,
189
163
  abortSignal,
190
- memory,
191
164
  workdir,
192
165
  tools,
193
166
  model,
@@ -216,28 +189,7 @@ export async function callAgent(
216
189
  });
217
190
 
218
191
  // Build system prompt content
219
- let systemContent = buildSystemPrompt(
220
- systemPrompt || DEFAULT_SYSTEM_PROMPT,
221
- tools || [],
222
- );
223
-
224
- // Always add environment information
225
- systemContent += `
226
-
227
- Here is useful information about the environment you are running in:
228
- <env>
229
- Working directory: ${workdir}
230
- Is directory a git repo: ${isGitRepository(workdir)}
231
- Platform: ${os.platform()}
232
- OS Version: ${os.type()} ${os.release()}
233
- Today's date: ${new Date().toISOString().split("T")[0]}
234
- </env>
235
- `;
236
-
237
- // If there is memory content, add it to the system prompt
238
- if (memory && memory.trim()) {
239
- systemContent += `\n## Memory Context\n\nThe following is important context and memory from previous interactions:\n\n${memory}`;
240
- }
192
+ const systemContent = systemPrompt || "";
241
193
 
242
194
  // Add system prompt
243
195
  const systemMessage: ChatCompletionMessageParam = {
@@ -792,101 +744,7 @@ export async function compressMessages(
792
744
  messages: [
793
745
  {
794
746
  role: "system",
795
- content: `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions.
796
- This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context.
797
-
798
- Before providing your final summary, wrap your analysis in <analysis> tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process:
799
-
800
- 1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify:
801
- - The user's explicit requests and intents
802
- - Your approach to addressing the user's requests
803
- - Key decisions, technical concepts and code patterns
804
- - Specific details like:
805
- - file names
806
- - full code snippets
807
- - function signatures
808
- - file edits
809
- - Errors that you ran into and how you fixed them
810
- - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
811
- 2. Double-check for technical accuracy and completeness, addressing each required element thoroughly.
812
-
813
- Your summary should include the following sections:
814
-
815
- 1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail
816
- 2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed.
817
- 3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important.
818
- 4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently.
819
- 5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
820
- 6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent.
821
- 6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
822
- 7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable.
823
- 8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first.
824
- If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation.
825
-
826
- Here's an example of how your output should be structured:
827
-
828
- <example>
829
- <analysis>
830
- [Your thought process, ensuring all points are covered thoroughly and accurately]
831
- </analysis>
832
-
833
- <summary>
834
- 1. Primary Request and Intent:
835
- [Detailed description]
836
-
837
- 2. Key Technical Concepts:
838
- - [Concept 1]
839
- - [Concept 2]
840
- - [...]
841
-
842
- 3. Files and Code Sections:
843
- - [File Name 1]
844
- - [Summary of why this file is important]
845
- - [Summary of the changes made to this file, if any]
846
- - [Important Code Snippet]
847
- - [File Name 2]
848
- - [Important Code Snippet]
849
- - [...]
850
-
851
- 4. Errors and fixes:
852
- - [Detailed description of error 1]:
853
- - [How you fixed the error]
854
- - [User feedback on the error if any]
855
- - [...]
856
-
857
- 5. Problem Solving:
858
- [Description of solved problems and ongoing troubleshooting]
859
-
860
- 6. All user messages:
861
- - [Detailed non tool use user message]
862
- - [...]
863
-
864
- 7. Pending Tasks:
865
- - [Task 1]
866
- - [Task 2]
867
- - [...]
868
-
869
- 8. Current Work:
870
- [Precise description of current work]
871
-
872
- 9. Optional Next Step:
873
- [Optional Next step to take]
874
-
875
- </summary>
876
- </example>
877
-
878
- Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
879
-
880
- There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
881
- <example>
882
- ## Compact Instructions
883
- When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
884
- </example>
885
-
886
- <example>
887
- # Summary instructions
888
- When you are using compact - please focus on test output and code changes. Include file reads verbatim.
889
- </example>`,
747
+ content: COMPRESS_MESSAGES_SYSTEM_PROMPT,
890
748
  },
891
749
  ...messages,
892
750
  {
@@ -4,54 +4,6 @@ import { USER_MEMORY_FILE, DATA_DIRECTORY } from "../utils/constants.js";
4
4
  import { logger } from "../utils/globalLogger.js";
5
5
 
6
6
  // Project memory related methods
7
- export const isMemoryMessage = (message: string): boolean => {
8
- return message.trim().startsWith("#");
9
- };
10
-
11
- export const addMemory = async (
12
- message: string,
13
- workdir: string,
14
- ): Promise<void> => {
15
- if (!isMemoryMessage(message)) {
16
- return;
17
- }
18
-
19
- try {
20
- const memoryFilePath = path.join(workdir, "AGENTS.md");
21
-
22
- // Format memory entry, starting with -, no timestamp
23
- const memoryEntry = `- ${message.substring(1).trim()}\n`;
24
-
25
- // Check if file exists
26
- let existingContent = "";
27
- try {
28
- existingContent = await fs.readFile(memoryFilePath, "utf-8");
29
- } catch (error) {
30
- // File does not exist, create new file
31
- if ((error as NodeJS.ErrnoException).code === "ENOENT") {
32
- logger.info("Memory file does not exist, will create new one", {
33
- memoryFilePath,
34
- });
35
- existingContent =
36
- "# Memory\n\nThis is the AI assistant's memory file, recording important information and context.\n\n";
37
- } else {
38
- throw error;
39
- }
40
- }
41
-
42
- // Append new memory entry to the end of the file
43
- const updatedContent = existingContent + memoryEntry;
44
-
45
- // Write file
46
- await fs.writeFile(memoryFilePath, updatedContent, "utf-8");
47
-
48
- logger.debug(`Memory added to ${memoryFilePath}:`, message);
49
- } catch (error) {
50
- logger.error("Failed to add memory:", error);
51
- throw new Error(`Failed to add memory: ${(error as Error).message}`);
52
- }
53
- };
54
-
55
7
  // User memory related methods
56
8
  export const ensureUserMemoryFile = async (): Promise<void> => {
57
9
  try {
@@ -83,30 +35,6 @@ export const ensureUserMemoryFile = async (): Promise<void> => {
83
35
  }
84
36
  };
85
37
 
86
- export const addUserMemory = async (message: string): Promise<void> => {
87
- try {
88
- // Ensure user memory file exists
89
- await ensureUserMemoryFile();
90
-
91
- // Format memory entry, starting with -
92
- const memoryEntry = `- ${message.substring(1).trim()}\n`;
93
-
94
- // Read existing content
95
- const existingContent = await fs.readFile(USER_MEMORY_FILE, "utf-8");
96
-
97
- // Append new memory entry to the end of the file
98
- const updatedContent = existingContent + memoryEntry;
99
-
100
- // Write file
101
- await fs.writeFile(USER_MEMORY_FILE, updatedContent, "utf-8");
102
-
103
- logger.debug(`User memory added to ${USER_MEMORY_FILE}:`, message);
104
- } catch (error) {
105
- logger.error("Failed to add user memory:", error);
106
- throw new Error(`Failed to add user memory: ${(error as Error).message}`);
107
- }
108
- };
109
-
110
38
  export const getUserMemoryContent = async (): Promise<string> => {
111
39
  try {
112
40
  await ensureUserMemoryFile();
@@ -28,6 +28,7 @@ import { logger } from "../utils/globalLogger.js";
28
28
 
29
29
  export interface SessionData {
30
30
  id: string;
31
+ rootSessionId?: string;
31
32
  messages: Message[];
32
33
  metadata: {
33
34
  workdir: string;
@@ -38,6 +39,7 @@ export interface SessionData {
38
39
 
39
40
  export interface SessionMetadata {
40
41
  id: string;
42
+ rootSessionId?: string;
41
43
  sessionType: "main" | "subagent";
42
44
  subagentType?: string;
43
45
  workdir: string;
@@ -191,6 +193,7 @@ export async function appendMessages(
191
193
  newMessages: Message[],
192
194
  workdir: string,
193
195
  sessionType: "main" | "subagent" = "main",
196
+ rootSessionId?: string,
194
197
  ): Promise<void> {
195
198
  // Do not save session files in test environment
196
199
  if (process.env.NODE_ENV === "test") {
@@ -252,6 +255,7 @@ export async function appendMessages(
252
255
 
253
256
  await updateSessionIndex(projectDir.encodedPath, {
254
257
  id: sessionId,
258
+ rootSessionId,
255
259
  sessionType,
256
260
  workdir,
257
261
  lastActiveAt: new Date(lastMessage.timestamp),
@@ -294,8 +298,25 @@ export async function loadSessionFromJsonl(
294
298
  // Extract metadata from messages
295
299
  const lastMessage = messages[messages.length - 1];
296
300
 
301
+ // Try to get rootSessionId from index
302
+ let rootSessionId: string | undefined;
303
+ try {
304
+ const encoder = new PathEncoder();
305
+ const projectDir = await encoder.getProjectDirectory(
306
+ workdir,
307
+ SESSION_DIR,
308
+ );
309
+ const indexPath = join(projectDir.encodedPath, SESSION_INDEX_FILENAME);
310
+ const indexContent = await fs.readFile(indexPath, "utf8");
311
+ const index = JSON.parse(indexContent) as SessionIndex;
312
+ rootSessionId = index.sessions[sessionId]?.rootSessionId;
313
+ } catch {
314
+ // Ignore index errors
315
+ }
316
+
297
317
  const sessionData: SessionData = {
298
318
  id: sessionId,
319
+ rootSessionId: rootSessionId || sessionId,
299
320
  messages: messages.map((msg) => {
300
321
  // Remove timestamp property for backward compatibility
301
322
  const { timestamp: _ignored, ...messageWithoutTimestamp } = msg;