wave-agent-sdk 0.5.0 → 0.6.1

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 (152) hide show
  1. package/dist/agent.d.ts +14 -6
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +65 -88
  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 +130 -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 +14 -10
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +102 -62
  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/slashCommandManager.d.ts +3 -0
  30. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  31. package/dist/managers/slashCommandManager.js +7 -2
  32. package/dist/managers/subagentManager.d.ts +4 -0
  33. package/dist/managers/subagentManager.d.ts.map +1 -1
  34. package/dist/managers/subagentManager.js +22 -14
  35. package/dist/managers/toolManager.d.ts +11 -0
  36. package/dist/managers/toolManager.d.ts.map +1 -1
  37. package/dist/managers/toolManager.js +20 -2
  38. package/dist/services/aiService.d.ts +0 -1
  39. package/dist/services/aiService.d.ts.map +1 -1
  40. package/dist/services/aiService.js +4 -140
  41. package/dist/services/memory.d.ts +0 -3
  42. package/dist/services/memory.d.ts.map +1 -1
  43. package/dist/services/memory.js +0 -59
  44. package/dist/services/session.d.ts +15 -1
  45. package/dist/services/session.d.ts.map +1 -1
  46. package/dist/services/session.js +57 -1
  47. package/dist/services/taskManager.d.ts +21 -0
  48. package/dist/services/taskManager.d.ts.map +1 -0
  49. package/dist/services/taskManager.js +158 -0
  50. package/dist/tools/askUserQuestion.d.ts.map +1 -1
  51. package/dist/tools/askUserQuestion.js +39 -25
  52. package/dist/tools/bashTool.d.ts.map +1 -1
  53. package/dist/tools/bashTool.js +7 -9
  54. package/dist/tools/editTool.d.ts.map +1 -1
  55. package/dist/tools/editTool.js +2 -1
  56. package/dist/tools/exitPlanMode.d.ts.map +1 -1
  57. package/dist/tools/exitPlanMode.js +25 -1
  58. package/dist/tools/globTool.d.ts.map +1 -1
  59. package/dist/tools/globTool.js +8 -2
  60. package/dist/tools/grepTool.d.ts.map +1 -1
  61. package/dist/tools/grepTool.js +17 -6
  62. package/dist/tools/lsTool.d.ts.map +1 -1
  63. package/dist/tools/lsTool.js +3 -1
  64. package/dist/tools/readTool.d.ts.map +1 -1
  65. package/dist/tools/readTool.js +16 -1
  66. package/dist/tools/taskManagementTools.d.ts +6 -0
  67. package/dist/tools/taskManagementTools.d.ts.map +1 -0
  68. package/dist/tools/taskManagementTools.js +453 -0
  69. package/dist/tools/taskOutputTool.d.ts.map +1 -1
  70. package/dist/tools/taskOutputTool.js +32 -8
  71. package/dist/tools/taskStopTool.d.ts.map +1 -1
  72. package/dist/tools/taskStopTool.js +7 -1
  73. package/dist/tools/taskTool.d.ts.map +1 -1
  74. package/dist/tools/taskTool.js +6 -1
  75. package/dist/tools/types.d.ts +9 -0
  76. package/dist/tools/types.d.ts.map +1 -1
  77. package/dist/tools/writeTool.d.ts.map +1 -1
  78. package/dist/tools/writeTool.js +9 -1
  79. package/dist/types/index.d.ts +1 -0
  80. package/dist/types/index.d.ts.map +1 -1
  81. package/dist/types/index.js +1 -0
  82. package/dist/types/messaging.d.ts +2 -8
  83. package/dist/types/messaging.d.ts.map +1 -1
  84. package/dist/types/processes.d.ts +11 -6
  85. package/dist/types/processes.d.ts.map +1 -1
  86. package/dist/types/tasks.d.ts +13 -0
  87. package/dist/types/tasks.d.ts.map +1 -0
  88. package/dist/types/tasks.js +1 -0
  89. package/dist/types/tools.d.ts +4 -1
  90. package/dist/types/tools.d.ts.map +1 -1
  91. package/dist/utils/builtinSubagents.d.ts.map +1 -1
  92. package/dist/utils/builtinSubagents.js +38 -1
  93. package/dist/utils/cacheControlUtils.d.ts.map +1 -1
  94. package/dist/utils/cacheControlUtils.js +18 -12
  95. package/dist/utils/constants.d.ts +0 -4
  96. package/dist/utils/constants.d.ts.map +1 -1
  97. package/dist/utils/constants.js +0 -4
  98. package/dist/utils/convertMessagesForAPI.js +2 -2
  99. package/dist/utils/messageOperations.d.ts +2 -35
  100. package/dist/utils/messageOperations.d.ts.map +1 -1
  101. package/dist/utils/messageOperations.js +4 -97
  102. package/dist/utils/nameGenerator.d.ts +1 -1
  103. package/dist/utils/nameGenerator.d.ts.map +1 -1
  104. package/dist/utils/nameGenerator.js +19 -3
  105. package/package.json +1 -1
  106. package/src/agent.ts +90 -101
  107. package/src/constants/prompts.ts +156 -65
  108. package/src/constants/tools.ts +4 -1
  109. package/src/index.ts +1 -0
  110. package/src/managers/aiManager.ts +79 -70
  111. package/src/managers/backgroundTaskManager.ts +53 -54
  112. package/src/managers/foregroundTaskManager.ts +3 -2
  113. package/src/managers/mcpManager.ts +6 -3
  114. package/src/managers/messageManager.ts +137 -73
  115. package/src/managers/permissionManager.ts +32 -21
  116. package/src/managers/planManager.ts +2 -2
  117. package/src/managers/slashCommandManager.ts +11 -2
  118. package/src/managers/subagentManager.ts +33 -14
  119. package/src/managers/toolManager.ts +32 -2
  120. package/src/services/aiService.ts +3 -145
  121. package/src/services/memory.ts +0 -72
  122. package/src/services/session.ts +73 -0
  123. package/src/services/taskManager.ts +188 -0
  124. package/src/tools/askUserQuestion.ts +51 -29
  125. package/src/tools/bashTool.ts +9 -15
  126. package/src/tools/editTool.ts +3 -1
  127. package/src/tools/exitPlanMode.ts +26 -2
  128. package/src/tools/globTool.ts +10 -2
  129. package/src/tools/grepTool.ts +17 -6
  130. package/src/tools/lsTool.ts +3 -1
  131. package/src/tools/readTool.ts +17 -1
  132. package/src/tools/taskManagementTools.ts +498 -0
  133. package/src/tools/taskOutputTool.ts +34 -12
  134. package/src/tools/taskStopTool.ts +7 -1
  135. package/src/tools/taskTool.ts +7 -1
  136. package/src/tools/types.ts +10 -0
  137. package/src/tools/writeTool.ts +9 -2
  138. package/src/types/index.ts +1 -0
  139. package/src/types/messaging.ts +1 -9
  140. package/src/types/processes.ts +13 -7
  141. package/src/types/tasks.ts +13 -0
  142. package/src/types/tools.ts +4 -1
  143. package/src/utils/builtinSubagents.ts +47 -1
  144. package/src/utils/cacheControlUtils.ts +26 -18
  145. package/src/utils/constants.ts +0 -5
  146. package/src/utils/convertMessagesForAPI.ts +2 -2
  147. package/src/utils/messageOperations.ts +5 -136
  148. package/src/utils/nameGenerator.ts +20 -3
  149. package/dist/tools/todoWriteTool.d.ts +0 -6
  150. package/dist/tools/todoWriteTool.d.ts.map +0 -1
  151. package/dist/tools/todoWriteTool.js +0 -220
  152. package/src/tools/todoWriteTool.ts +0 -257
@@ -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,8 @@ import { logger } from "../utils/globalLogger.js";
28
28
 
29
29
  export interface SessionData {
30
30
  id: string;
31
+ rootSessionId?: string;
32
+ parentSessionId?: string;
31
33
  messages: Message[];
32
34
  metadata: {
33
35
  workdir: string;
@@ -38,6 +40,8 @@ export interface SessionData {
38
40
 
39
41
  export interface SessionMetadata {
40
42
  id: string;
43
+ rootSessionId?: string;
44
+ parentSessionId?: string;
41
45
  sessionType: "main" | "subagent";
42
46
  subagentType?: string;
43
47
  workdir: string;
@@ -101,6 +105,7 @@ async function updateSessionIndex(
101
105
  ...rest,
102
106
  lastActiveAt: metadata.lastActiveAt.toISOString(),
103
107
  firstMessage: metadata.firstMessage || index.sessions[id]?.firstMessage,
108
+ parentSessionId: metadata.parentSessionId,
104
109
  };
105
110
  index.lastUpdated = new Date().toISOString();
106
111
 
@@ -191,6 +196,8 @@ export async function appendMessages(
191
196
  newMessages: Message[],
192
197
  workdir: string,
193
198
  sessionType: "main" | "subagent" = "main",
199
+ rootSessionId?: string,
200
+ parentSessionId?: string,
194
201
  ): Promise<void> {
195
202
  // Do not save session files in test environment
196
203
  if (process.env.NODE_ENV === "test") {
@@ -252,6 +259,8 @@ export async function appendMessages(
252
259
 
253
260
  await updateSessionIndex(projectDir.encodedPath, {
254
261
  id: sessionId,
262
+ rootSessionId,
263
+ parentSessionId,
255
264
  sessionType,
256
265
  workdir,
257
266
  lastActiveAt: new Date(lastMessage.timestamp),
@@ -294,8 +303,28 @@ export async function loadSessionFromJsonl(
294
303
  // Extract metadata from messages
295
304
  const lastMessage = messages[messages.length - 1];
296
305
 
306
+ // Try to get rootSessionId and parentSessionId from index
307
+ let rootSessionId: string | undefined;
308
+ let parentSessionId: string | undefined;
309
+ try {
310
+ const encoder = new PathEncoder();
311
+ const projectDir = await encoder.getProjectDirectory(
312
+ workdir,
313
+ SESSION_DIR,
314
+ );
315
+ const indexPath = join(projectDir.encodedPath, SESSION_INDEX_FILENAME);
316
+ const indexContent = await fs.readFile(indexPath, "utf8");
317
+ const index = JSON.parse(indexContent) as SessionIndex;
318
+ rootSessionId = index.sessions[sessionId]?.rootSessionId;
319
+ parentSessionId = index.sessions[sessionId]?.parentSessionId;
320
+ } catch {
321
+ // Ignore index errors
322
+ }
323
+
297
324
  const sessionData: SessionData = {
298
325
  id: sessionId,
326
+ rootSessionId: rootSessionId || sessionId,
327
+ parentSessionId,
299
328
  messages: messages.map((msg) => {
300
329
  // Remove timestamp property for backward compatibility
301
330
  const { timestamp: _ignored, ...messageWithoutTimestamp } = msg;
@@ -841,3 +870,47 @@ export async function handleSessionRestoration(
841
870
  process.exit(1);
842
871
  }
843
872
  }
873
+
874
+ /**
875
+ * Load the full message thread by following parentSessionId links
876
+ * @param currentSessionId - The ID of the current session
877
+ * @param workdir - Working directory for the session
878
+ * @returns Promise that resolves to an array of all messages in the thread
879
+ */
880
+ export async function loadFullMessageThread(
881
+ currentSessionId: string,
882
+ workdir: string,
883
+ ): Promise<{ messages: Message[]; sessionIds: string[] }> {
884
+ const sessionIds: string[] = [];
885
+ let currentId: string | undefined = currentSessionId;
886
+ const allMessages: Message[] = [];
887
+
888
+ while (currentId) {
889
+ const sessionData = await loadSessionFromJsonl(currentId, workdir);
890
+ if (!sessionData) break;
891
+
892
+ sessionIds.unshift(currentId);
893
+ // Add messages from this session to the beginning of the list
894
+ // But skip the "compress" block if it's not the first session in our traversal (which is the latest)
895
+ // Actually, we should probably keep all messages and let the UI/logic handle it.
896
+ // But wait, if we are concatenating, the "compress" block in session N summarizes session N-1.
897
+ // So if we have session N-1 and session N, we should probably skip the compress block in session N.
898
+
899
+ const messages = sessionData.messages;
900
+ if (allMessages.length > 0) {
901
+ // If we already have messages (from "later" sessions),
902
+ // we are now adding messages from an "earlier" session.
903
+ // The later session's first message might be a "compress" block.
904
+ if (allMessages[0].blocks.some((b) => b.type === "compress")) {
905
+ // Remove the compress block from the later session's messages
906
+ // because we are now providing the actual messages it summarized.
907
+ allMessages.shift();
908
+ }
909
+ }
910
+
911
+ allMessages.unshift(...messages);
912
+ currentId = sessionData.parentSessionId;
913
+ }
914
+
915
+ return { messages: allMessages, sessionIds };
916
+ }