wave-agent-sdk 0.5.0 → 0.6.2

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 (169) hide show
  1. package/dist/agent.d.ts +14 -11
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +64 -151
  4. package/dist/constants/subagents.d.ts +5 -0
  5. package/dist/constants/subagents.d.ts.map +1 -0
  6. package/dist/constants/subagents.js +4 -0
  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 +43 -48
  16. package/dist/managers/backgroundTaskManager.d.ts.map +1 -1
  17. package/dist/managers/backgroundTaskManager.js +63 -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 +13 -27
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +94 -89
  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/reversionManager.d.ts.map +1 -1
  30. package/dist/managers/reversionManager.js +23 -2
  31. package/dist/managers/slashCommandManager.d.ts +3 -0
  32. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  33. package/dist/managers/slashCommandManager.js +8 -3
  34. package/dist/managers/subagentManager.d.ts +8 -14
  35. package/dist/managers/subagentManager.d.ts.map +1 -1
  36. package/dist/managers/subagentManager.js +46 -112
  37. package/dist/managers/toolManager.d.ts +11 -0
  38. package/dist/managers/toolManager.d.ts.map +1 -1
  39. package/dist/managers/toolManager.js +20 -2
  40. package/dist/{constants/prompts.d.ts → prompts/index.d.ts} +17 -15
  41. package/dist/prompts/index.d.ts.map +1 -0
  42. package/dist/prompts/index.js +309 -0
  43. package/dist/services/aiService.d.ts +0 -1
  44. package/dist/services/aiService.d.ts.map +1 -1
  45. package/dist/services/aiService.js +4 -140
  46. package/dist/services/memory.d.ts +0 -3
  47. package/dist/services/memory.d.ts.map +1 -1
  48. package/dist/services/memory.js +0 -59
  49. package/dist/services/session.d.ts +15 -1
  50. package/dist/services/session.d.ts.map +1 -1
  51. package/dist/services/session.js +57 -1
  52. package/dist/services/taskManager.d.ts +25 -0
  53. package/dist/services/taskManager.d.ts.map +1 -0
  54. package/dist/services/taskManager.js +164 -0
  55. package/dist/tools/askUserQuestion.d.ts.map +1 -1
  56. package/dist/tools/askUserQuestion.js +39 -25
  57. package/dist/tools/bashTool.d.ts.map +1 -1
  58. package/dist/tools/bashTool.js +13 -11
  59. package/dist/tools/editTool.d.ts.map +1 -1
  60. package/dist/tools/editTool.js +2 -1
  61. package/dist/tools/exitPlanMode.d.ts.map +1 -1
  62. package/dist/tools/exitPlanMode.js +26 -2
  63. package/dist/tools/globTool.d.ts.map +1 -1
  64. package/dist/tools/globTool.js +8 -2
  65. package/dist/tools/grepTool.d.ts.map +1 -1
  66. package/dist/tools/grepTool.js +17 -6
  67. package/dist/tools/lsTool.d.ts.map +1 -1
  68. package/dist/tools/lsTool.js +3 -1
  69. package/dist/tools/readTool.d.ts.map +1 -1
  70. package/dist/tools/readTool.js +16 -1
  71. package/dist/tools/taskManagementTools.d.ts +6 -0
  72. package/dist/tools/taskManagementTools.d.ts.map +1 -0
  73. package/dist/tools/taskManagementTools.js +461 -0
  74. package/dist/tools/taskOutputTool.d.ts.map +1 -1
  75. package/dist/tools/taskOutputTool.js +32 -8
  76. package/dist/tools/taskStopTool.d.ts.map +1 -1
  77. package/dist/tools/taskStopTool.js +7 -1
  78. package/dist/tools/taskTool.d.ts.map +1 -1
  79. package/dist/tools/taskTool.js +37 -2
  80. package/dist/tools/types.d.ts +11 -0
  81. package/dist/tools/types.d.ts.map +1 -1
  82. package/dist/tools/writeTool.d.ts.map +1 -1
  83. package/dist/tools/writeTool.js +9 -1
  84. package/dist/types/index.d.ts +1 -0
  85. package/dist/types/index.d.ts.map +1 -1
  86. package/dist/types/index.js +1 -0
  87. package/dist/types/messaging.d.ts +2 -18
  88. package/dist/types/messaging.d.ts.map +1 -1
  89. package/dist/types/processes.d.ts +16 -6
  90. package/dist/types/processes.d.ts.map +1 -1
  91. package/dist/types/tasks.d.ts +13 -0
  92. package/dist/types/tasks.d.ts.map +1 -0
  93. package/dist/types/tasks.js +1 -0
  94. package/dist/types/tools.d.ts +4 -1
  95. package/dist/types/tools.d.ts.map +1 -1
  96. package/dist/utils/builtinSubagents.d.ts.map +1 -1
  97. package/dist/utils/builtinSubagents.js +59 -44
  98. package/dist/utils/cacheControlUtils.d.ts.map +1 -1
  99. package/dist/utils/cacheControlUtils.js +18 -12
  100. package/dist/utils/constants.d.ts +0 -4
  101. package/dist/utils/constants.d.ts.map +1 -1
  102. package/dist/utils/constants.js +0 -4
  103. package/dist/utils/convertMessagesForAPI.js +2 -2
  104. package/dist/utils/editUtils.d.ts.map +1 -1
  105. package/dist/utils/editUtils.js +2 -2
  106. package/dist/utils/gitUtils.d.ts +7 -0
  107. package/dist/utils/gitUtils.d.ts.map +1 -0
  108. package/dist/utils/gitUtils.js +24 -0
  109. package/dist/utils/messageOperations.d.ts +3 -58
  110. package/dist/utils/messageOperations.d.ts.map +1 -1
  111. package/dist/utils/messageOperations.js +4 -146
  112. package/dist/utils/nameGenerator.d.ts +1 -1
  113. package/dist/utils/nameGenerator.d.ts.map +1 -1
  114. package/dist/utils/nameGenerator.js +19 -3
  115. package/package.json +1 -1
  116. package/src/agent.ts +86 -183
  117. package/src/constants/subagents.ts +4 -0
  118. package/src/constants/tools.ts +4 -1
  119. package/src/index.ts +1 -0
  120. package/src/managers/aiManager.ts +63 -70
  121. package/src/managers/backgroundTaskManager.ts +58 -54
  122. package/src/managers/foregroundTaskManager.ts +3 -2
  123. package/src/managers/mcpManager.ts +6 -3
  124. package/src/managers/messageManager.ts +126 -142
  125. package/src/managers/permissionManager.ts +32 -21
  126. package/src/managers/planManager.ts +2 -2
  127. package/src/managers/reversionManager.ts +26 -2
  128. package/src/managers/slashCommandManager.ts +12 -3
  129. package/src/managers/subagentManager.ts +60 -144
  130. package/src/managers/toolManager.ts +32 -2
  131. package/src/prompts/index.ts +366 -0
  132. package/src/services/aiService.ts +3 -145
  133. package/src/services/memory.ts +0 -72
  134. package/src/services/session.ts +73 -0
  135. package/src/services/taskManager.ts +195 -0
  136. package/src/tools/askUserQuestion.ts +51 -29
  137. package/src/tools/bashTool.ts +15 -17
  138. package/src/tools/editTool.ts +3 -1
  139. package/src/tools/exitPlanMode.ts +27 -3
  140. package/src/tools/globTool.ts +10 -2
  141. package/src/tools/grepTool.ts +17 -6
  142. package/src/tools/lsTool.ts +3 -1
  143. package/src/tools/readTool.ts +17 -1
  144. package/src/tools/taskManagementTools.ts +516 -0
  145. package/src/tools/taskOutputTool.ts +34 -12
  146. package/src/tools/taskStopTool.ts +7 -1
  147. package/src/tools/taskTool.ts +45 -1
  148. package/src/tools/types.ts +12 -0
  149. package/src/tools/writeTool.ts +9 -2
  150. package/src/types/index.ts +1 -0
  151. package/src/types/messaging.ts +1 -21
  152. package/src/types/processes.ts +18 -7
  153. package/src/types/tasks.ts +13 -0
  154. package/src/types/tools.ts +4 -1
  155. package/src/utils/builtinSubagents.ts +81 -45
  156. package/src/utils/cacheControlUtils.ts +26 -18
  157. package/src/utils/constants.ts +0 -5
  158. package/src/utils/convertMessagesForAPI.ts +2 -2
  159. package/src/utils/editUtils.ts +2 -6
  160. package/src/utils/gitUtils.ts +24 -0
  161. package/src/utils/messageOperations.ts +6 -229
  162. package/src/utils/nameGenerator.ts +20 -3
  163. package/dist/constants/prompts.d.ts.map +0 -1
  164. package/dist/constants/prompts.js +0 -118
  165. package/dist/tools/todoWriteTool.d.ts +0 -6
  166. package/dist/tools/todoWriteTool.d.ts.map +0 -1
  167. package/dist/tools/todoWriteTool.js +0 -220
  168. package/src/constants/prompts.ts +0 -155
  169. package/src/tools/todoWriteTool.ts +0 -257
@@ -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
+ }
@@ -0,0 +1,195 @@
1
+ import { promises as fs } from "fs";
2
+ import { join } from "path";
3
+ import { homedir } from "os";
4
+ import { EventEmitter } from "events";
5
+ import { Task } from "../types/tasks.js";
6
+ import { logger } from "../utils/globalLogger.js";
7
+
8
+ export class TaskManager extends EventEmitter {
9
+ private readonly baseDir: string;
10
+ private taskListId: string;
11
+
12
+ constructor(taskListId: string) {
13
+ super();
14
+ this.taskListId = taskListId;
15
+ this.baseDir = join(homedir(), ".wave", "tasks");
16
+ }
17
+
18
+ public getTaskListId(): string {
19
+ return this.taskListId;
20
+ }
21
+
22
+ public setTaskListId(taskListId: string): void {
23
+ this.taskListId = taskListId;
24
+ }
25
+
26
+ private getSessionDir(): string {
27
+ return join(this.baseDir, this.taskListId);
28
+ }
29
+
30
+ public getTaskPath(taskId: string): string {
31
+ return join(this.getSessionDir(), `${taskId}.json`);
32
+ }
33
+
34
+ private getLockPath(): string {
35
+ return join(this.getSessionDir(), `.lock`);
36
+ }
37
+
38
+ async ensureSessionDir(): Promise<void> {
39
+ await fs.mkdir(this.getSessionDir(), { recursive: true });
40
+ }
41
+
42
+ private async withLock<T>(operation: () => Promise<T>): Promise<T> {
43
+ const lockPath = this.getLockPath();
44
+ let lockHandle;
45
+ const maxRetries = 100;
46
+ const retryDelay = process.env.NODE_ENV === "test" ? 10 : 100;
47
+
48
+ await this.ensureSessionDir();
49
+
50
+ for (let i = 0; i < maxRetries; i++) {
51
+ try {
52
+ lockHandle = await fs.open(lockPath, "wx");
53
+ break;
54
+ } catch (error) {
55
+ if ((error as NodeJS.ErrnoException).code === "EEXIST") {
56
+ if (i === maxRetries - 1) {
57
+ throw new Error(
58
+ `Could not acquire lock for task list ${this.taskListId} after ${maxRetries} retries`,
59
+ );
60
+ }
61
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
62
+ continue;
63
+ }
64
+ throw error;
65
+ }
66
+ }
67
+
68
+ try {
69
+ return await operation();
70
+ } finally {
71
+ if (lockHandle) {
72
+ await lockHandle.close();
73
+ }
74
+ try {
75
+ await fs.unlink(lockPath);
76
+ } catch (error) {
77
+ logger.error(
78
+ `Failed to release lock for task list ${this.taskListId}:`,
79
+ error,
80
+ );
81
+ }
82
+ }
83
+ }
84
+
85
+ private validateTask(task: Task): void {
86
+ if (!task.id || typeof task.id !== "string")
87
+ throw new Error("Invalid task ID");
88
+ if (!task.subject || typeof task.subject !== "string")
89
+ throw new Error("Invalid task subject");
90
+ if (!task.description || typeof task.description !== "string")
91
+ throw new Error("Invalid task description");
92
+ if (
93
+ !task.status ||
94
+ !["pending", "in_progress", "completed", "deleted"].includes(task.status)
95
+ ) {
96
+ throw new Error(`Invalid task status: ${task.status}`);
97
+ }
98
+ }
99
+
100
+ async createTask(task: Omit<Task, "id">): Promise<string> {
101
+ return await this.withLock(async () => {
102
+ const taskId = await this.getNextTaskId();
103
+ const fullTask: Task = { ...task, id: taskId };
104
+ this.validateTask(fullTask);
105
+ const taskPath = this.getTaskPath(taskId);
106
+ const content = JSON.stringify(fullTask, null, 2);
107
+ await fs.writeFile(taskPath, content, "utf8");
108
+ this.emit("tasksChange", this.taskListId);
109
+ logger.info(`Task ${taskId} created in task list ${this.taskListId}`);
110
+ return taskId;
111
+ });
112
+ }
113
+
114
+ async getTask(taskId: string): Promise<Task | null> {
115
+ const taskPath = this.getTaskPath(taskId);
116
+ try {
117
+ const content = await fs.readFile(taskPath, "utf8");
118
+ try {
119
+ return JSON.parse(content.trim()) as Task;
120
+ } catch (parseError) {
121
+ logger.error(`Failed to parse task file ${taskPath}:`, parseError);
122
+ logger.error(`Corrupted content: ${content}`);
123
+ throw parseError;
124
+ }
125
+ } catch (error) {
126
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
127
+ return null;
128
+ }
129
+ throw error;
130
+ }
131
+ }
132
+
133
+ async updateTask(task: Task): Promise<void> {
134
+ this.validateTask(task);
135
+ await this.withLock(async () => {
136
+ const taskPath = this.getTaskPath(task.id);
137
+ const content = JSON.stringify(task, null, 2);
138
+ await fs.writeFile(taskPath, content, "utf8");
139
+ this.emit("tasksChange", this.taskListId);
140
+ logger.info(`Task ${task.id} updated in task list ${this.taskListId}`);
141
+ });
142
+ }
143
+
144
+ async listTasks(): Promise<Task[]> {
145
+ const sessionDir = this.getSessionDir();
146
+ try {
147
+ const files = await fs.readdir(sessionDir);
148
+ const taskFiles = files.filter((f) => f.endsWith(".json"));
149
+
150
+ const tasks = await Promise.all(
151
+ taskFiles.map(async (file) => {
152
+ const taskPath = join(sessionDir, file);
153
+ try {
154
+ const content = await fs.readFile(taskPath, "utf8");
155
+ return JSON.parse(content.trim()) as Task;
156
+ } catch (error) {
157
+ logger.error(
158
+ `Failed to read or parse task file ${taskPath}:`,
159
+ error,
160
+ );
161
+ return null;
162
+ }
163
+ }),
164
+ );
165
+
166
+ return tasks.filter((t): t is Task => t !== null);
167
+ } catch (error) {
168
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") {
169
+ return [];
170
+ }
171
+ throw error;
172
+ }
173
+ }
174
+
175
+ async getNextTaskId(): Promise<string> {
176
+ const tasks = await this.listTasks();
177
+ if (tasks.length === 0) {
178
+ return "1";
179
+ }
180
+ const ids = tasks.map((t) => parseInt(t.id, 10)).filter((id) => !isNaN(id));
181
+
182
+ if (ids.length === 0) {
183
+ return (tasks.length + 1).toString();
184
+ }
185
+
186
+ return (Math.max(...ids) + 1).toString();
187
+ }
188
+
189
+ /**
190
+ * Refreshes the task list by re-reading tasks from disk and emitting a change event.
191
+ */
192
+ public async refreshTasks(): Promise<void> {
193
+ this.emit("tasksChange", this.taskListId);
194
+ }
195
+ }
@@ -1,9 +1,6 @@
1
1
  import { ToolPlugin } from "./types.js";
2
2
  import { AskUserQuestionInput } from "../types/tools.js";
3
- import {
4
- ASK_USER_QUESTION_TOOL_NAME,
5
- EXIT_PLAN_MODE_TOOL_NAME,
6
- } from "../constants/tools.js";
3
+ import { ASK_USER_QUESTION_TOOL_NAME } from "../constants/tools.js";
7
4
 
8
5
  export const askUserQuestionTool: ToolPlugin = {
9
6
  name: ASK_USER_QUESTION_TOOL_NAME,
@@ -11,19 +8,8 @@ export const askUserQuestionTool: ToolPlugin = {
11
8
  type: "function",
12
9
  function: {
13
10
  name: ASK_USER_QUESTION_TOOL_NAME,
14
- description: `Asks the user multiple choice questions to gather information, clarify ambiguity, understand preferences, make decisions or offer them choices.
15
- Use this tool when you need to ask the user questions during execution. This allows you to:
16
- 1. Gather user preferences or requirements
17
- 2. Clarify ambiguous instructions
18
- 3. Get decisions on implementation choices as you work
19
- 4. Offer choices to the user about what direction to take.
20
-
21
- Usage notes:
22
- - Users will always be able to select "Other" to provide custom text input
23
- - Use multiSelect: true to allow multiple answers to be selected for a question
24
- - If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label
25
-
26
- Plan mode note: In plan mode, use this tool to clarify requirements or choose between approaches BEFORE finalizing your plan. Do NOT use this tool to ask "Is my plan ready?" or "Should I proceed?" - use ${EXIT_PLAN_MODE_TOOL_NAME} for plan approval.`,
11
+ description:
12
+ "Asks the user multiple choice questions to gather information, clarify ambiguity, understand preferences, make decisions or offer them choices.",
27
13
  parameters: {
28
14
  type: "object",
29
15
  properties: {
@@ -31,37 +17,38 @@ Plan mode note: In plan mode, use this tool to clarify requirements or choose be
31
17
  type: "array",
32
18
  minItems: 1,
33
19
  maxItems: 4,
20
+ description: "Questions to ask the user (1-4 questions)",
34
21
  items: {
35
22
  type: "object",
36
23
  properties: {
37
24
  question: {
38
25
  type: "string",
39
- description: "The complete question to ask the user.",
26
+ description:
27
+ 'The complete question to ask the user. Should be clear, specific, and end with a question mark. Example: "Which library should we use for date formatting?" If multiSelect is true, phrase it accordingly, e.g. "Which features do you want to enable?"',
40
28
  },
41
29
  header: {
42
30
  type: "string",
43
31
  maxLength: 12,
44
- description:
45
- "Very short label displayed as a chip/tag (max 12 chars).",
32
+ description: `Very short label displayed as a chip/tag (max 12 chars). Examples: "Auth method", "Library", "Approach".`,
46
33
  },
47
34
  options: {
48
35
  type: "array",
49
36
  minItems: 2,
50
37
  maxItems: 4,
38
+ description:
39
+ "The available choices for this question. Must have 2-4 options. Each option should be a distinct, mutually exclusive choice (unless multiSelect is enabled). There should be no 'Other' option, that will be provided automatically.",
51
40
  items: {
52
41
  type: "object",
53
42
  properties: {
54
43
  label: {
55
44
  type: "string",
56
- description: "The display text for this option.",
45
+ description:
46
+ "The display text for this option that the user will see and select. Should be concise (1-5 words) and clearly describe the choice.",
57
47
  },
58
48
  description: {
59
49
  type: "string",
60
- description: "Explanation of what this option means.",
61
- },
62
- isRecommended: {
63
- type: "boolean",
64
- description: "Whether this option is recommended.",
50
+ description:
51
+ "Explanation of what this option means or what will happen if chosen. Useful for providing context about trade-offs or implications.",
65
52
  },
66
53
  },
67
54
  required: ["label"],
@@ -70,19 +57,54 @@ Plan mode note: In plan mode, use this tool to clarify requirements or choose be
70
57
  multiSelect: {
71
58
  type: "boolean",
72
59
  default: false,
73
- description: "Allow multiple answers to be selected.",
60
+ description:
61
+ "Set to true to allow the user to select multiple options instead of just one. Use when choices are not mutually exclusive.",
74
62
  },
75
63
  },
76
64
  required: ["question", "header", "options"],
77
65
  },
78
66
  },
67
+ answers: {
68
+ type: "object",
69
+ additionalProperties: { type: "string" },
70
+ description: "User answers collected by the permission component",
71
+ },
72
+ metadata: {
73
+ type: "object",
74
+ properties: {
75
+ source: {
76
+ type: "string",
77
+ description:
78
+ 'Optional identifier for the source of this question (e.g., "remember" for /remember command). Used for analytics tracking.',
79
+ },
80
+ },
81
+ description: "Optional metadata for the question",
82
+ },
79
83
  },
80
84
  required: ["questions"],
81
85
  },
82
86
  },
83
87
  },
88
+ prompt:
89
+ () => `Use this tool when you need to ask the user questions during execution. This allows you to:
90
+ 1. Gather user preferences or requirements
91
+ 2. Clarify ambiguous instructions
92
+ 3. Get decisions on implementation choices as you work
93
+ 4. Offer choices to the user about what direction to take.
94
+
95
+ Usage notes:
96
+ - Users will always be able to select "Other" to provide custom text input
97
+ - Use multiSelect: true to allow multiple answers to be selected for a question
98
+ - If you recommend a specific option, make that the first option in the list and add "(Recommended)" at the end of the label
99
+
100
+ Plan mode note: In plan mode, use this tool to clarify requirements or choose between approaches BEFORE finalizing your plan. Do NOT use this tool to ask "Is my plan ready?" or "Should I proceed?" - use ExitPlanMode for plan approval.
101
+ `,
84
102
  execute: async (args, context) => {
85
- const { questions } = args as unknown as AskUserQuestionInput;
103
+ const {
104
+ questions,
105
+ answers: existingAnswers,
106
+ metadata,
107
+ } = args as unknown as AskUserQuestionInput;
86
108
 
87
109
  if (!context.permissionManager) {
88
110
  throw new Error(
@@ -94,7 +116,7 @@ Plan mode note: In plan mode, use this tool to clarify requirements or choose be
94
116
  ASK_USER_QUESTION_TOOL_NAME,
95
117
  context.permissionMode || "default",
96
118
  context.canUseToolCallback,
97
- { questions },
119
+ { questions, answers: existingAnswers, metadata },
98
120
  );
99
121
  permissionContext.hidePersistentOption = true; // Always hide persistent option for questions
100
122
 
@@ -95,6 +95,9 @@ Usage notes:
95
95
  },
96
96
  },
97
97
  },
98
+ prompt: () => `
99
+ - Reserve bash tools exclusively for actual system commands and terminal operations that require shell execution. NEVER use bash echo or other command-line tools to communicate thoughts, explanations, or instructions to the user. Output all communication directly in your response text instead.
100
+ - When making multiple bash tool calls, you MUST send a single message with multiple tools calls to run the calls in parallel. For example, if you need to run "git status" and "git diff", send a single message with two tool calls in parallel.`,
98
101
  execute: async (
99
102
  args: Record<string, unknown>,
100
103
  context: ToolContext,
@@ -218,8 +221,9 @@ Usage notes:
218
221
  );
219
222
  resolve({
220
223
  success: true,
221
- content: `Command moved to background with ID: ${taskId}. Use TaskOutput tool with task_id="${taskId}" to monitor output.`,
224
+ content: `Command moved to background with ID: ${taskId}.`,
222
225
  shortResult: `Process ${taskId} backgrounded`,
226
+ isManuallyBackgrounded: true,
223
227
  });
224
228
  } else {
225
229
  handleAbort(
@@ -291,34 +295,28 @@ Usage notes:
291
295
  // Handle abort signal from context
292
296
  if (context?.abortSignal) {
293
297
  if (context.abortSignal.aborted) {
294
- if (!isBackgrounded) {
295
- handleAbort();
296
- }
298
+ handleAbort();
297
299
  return;
298
300
  }
299
301
  // Use { once: true } to prevent listener accumulation on signal reuse
300
- context.abortSignal.addEventListener(
301
- "abort",
302
- () => {
303
- if (!isBackgrounded) {
304
- handleAbort();
305
- }
306
- },
307
- {
308
- once: true,
309
- },
310
- );
302
+ context.abortSignal.addEventListener("abort", () => handleAbort(), {
303
+ once: true,
304
+ });
311
305
  }
312
306
 
313
307
  child.stdout?.on("data", (data) => {
314
308
  if (!isAborted && !isBackgrounded) {
315
- outputBuffer += stripAnsiColors(data.toString());
309
+ const chunk = stripAnsiColors(data.toString());
310
+ outputBuffer += chunk;
311
+ context.onShortResultUpdate?.(chunk.trim().split("\n").pop() || "");
316
312
  }
317
313
  });
318
314
 
319
315
  child.stderr?.on("data", (data) => {
320
316
  if (!isAborted && !isBackgrounded) {
321
- errorBuffer += stripAnsiColors(data.toString());
317
+ const chunk = stripAnsiColors(data.toString());
318
+ errorBuffer += chunk;
319
+ context.onShortResultUpdate?.(chunk.trim().split("\n").pop() || "");
322
320
  }
323
321
  });
324
322
 
@@ -22,11 +22,13 @@ function formatCompactParams(
22
22
  export const editTool: ToolPlugin = {
23
23
  name: EDIT_TOOL_NAME,
24
24
  formatCompactParams,
25
+ prompt: () =>
26
+ `Performs exact string replacements in files. \n\nUsage:\n- You must use your \`${READ_TOOL_NAME}\` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file. \n- When editing text from read_file tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.\n- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.\n- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.\n- The edit will FAIL if \`old_string\` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use \`replace_all\` to change every instance of \`old_string\`. \n- Use \`replace_all\` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.`,
25
27
  config: {
26
28
  type: "function",
27
29
  function: {
28
30
  name: EDIT_TOOL_NAME,
29
- description: `Performs exact string replacements in files. \n\nUsage:\n- You must use your \`${READ_TOOL_NAME}\` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file. \n- When editing text from read_file tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.\n- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.\n- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.\n- The edit will FAIL if \`old_string\` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use \`replace_all\` to change every instance of \`old_string\`. \n- Use \`replace_all\` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.`,
31
+ description: "A tool for editing files",
30
32
  parameters: {
31
33
  type: "object",
32
34
  properties: {
@@ -12,8 +12,7 @@ export const exitPlanModeTool: ToolPlugin = {
12
12
  type: "function",
13
13
  function: {
14
14
  name: EXIT_PLAN_MODE_TOOL_NAME,
15
- description:
16
- "Use this tool when you are in plan mode and have finished writing your plan to the plan file and are ready for user approval. This tool will read the plan from the file specified in the system message and present it to the user for confirmation. You should have already written your plan to that file before calling this tool.",
15
+ description: "Prompts the user to exit plan mode and start coding",
17
16
  parameters: {
18
17
  type: "object",
19
18
  properties: {},
@@ -22,6 +21,31 @@ export const exitPlanModeTool: ToolPlugin = {
22
21
  },
23
22
  },
24
23
  },
24
+ prompt:
25
+ () => `Use this tool when you are in plan mode and have finished writing your plan to the plan file and are ready for user approval.
26
+
27
+ ## How This Tool Works
28
+ - You should have already written your plan to the plan file specified in the plan mode system message
29
+ - This tool does NOT take the plan content as a parameter - it will read the plan from the file you wrote
30
+ - This tool simply signals that you're done planning and ready for the user to review and approve
31
+ - The user will see the contents of your plan file when they review it
32
+
33
+ ## When to Use This Tool
34
+ IMPORTANT: Only use this tool when the task requires planning the implementation steps of a task that requires writing code. For research tasks where you're gathering information, searching files, reading files or in general trying to understand the codebase - do NOT use this tool.
35
+
36
+ ## Before Using This Tool
37
+ Ensure your plan is complete and unambiguous:
38
+ - If you have unresolved questions about requirements or approach, use AskUserQuestion first (in earlier phases)
39
+ - Once your plan is finalized, use THIS tool to request approval
40
+
41
+ **Important:** Do NOT use AskUserQuestion to ask "Is this plan okay?" or "Should I proceed?" - that's exactly what THIS tool does. ExitPlanMode inherently requests user approval of your plan.
42
+
43
+ ## Examples
44
+
45
+ 1. Initial task: "Search for and understand the implementation of vim mode in the codebase" - Do not use the exit plan mode tool because you are not planning the implementation steps of a task.
46
+ 2. Initial task: "Help me implement yank mode for vim" - Use the exit plan mode tool after you have finished planning the implementation steps of the task.
47
+ 3. Initial task: "Add a new feature to handle user authentication" - If unsure about auth method (OAuth, JWT, etc.), use AskUserQuestion first, then use exit plan mode tool after clarifying the approach.
48
+ `,
25
49
  execute: async (
26
50
  _args: Record<string, unknown>,
27
51
  context: ToolContext,
@@ -69,7 +93,7 @@ export const exitPlanModeTool: ToolPlugin = {
69
93
  if (permissionResult.behavior === "deny") {
70
94
  return {
71
95
  success: false,
72
- content: permissionResult.message || "Plan rejected by user",
96
+ content: `User feedback: ${permissionResult.message || "Plan rejected by user"}. Please update your proposal accordingly.`,
73
97
  error: permissionResult.message ? undefined : "Plan rejected by user",
74
98
  };
75
99
  }
@@ -3,7 +3,7 @@ import { stat } from "fs/promises";
3
3
  import type { ToolPlugin, ToolResult, ToolContext } from "./types.js";
4
4
  import { resolvePath, getDisplayPath } from "../utils/path.js";
5
5
  import { getGlobIgnorePatterns } from "../utils/fileFilter.js";
6
- import { GLOB_TOOL_NAME, TASK_TOOL_NAME } from "../constants/tools.js";
6
+ import { GLOB_TOOL_NAME } from "../constants/tools.js";
7
7
 
8
8
  /**
9
9
  * Glob Tool Plugin - Fast file pattern matching
@@ -14,7 +14,8 @@ export const globTool: ToolPlugin = {
14
14
  type: "function",
15
15
  function: {
16
16
  name: GLOB_TOOL_NAME,
17
- description: `- Fast file pattern matching tool that works with any codebase size\n- Supports glob patterns like "**/*.js" or "src/**/*.ts"\n- Returns matching file paths sorted by modification time\n- Use this tool when you need to find files by name patterns\n- When you are doing an open ended search that may require multiple rounds of globbing and grepping, use the ${TASK_TOOL_NAME} tool instead\n- You have the capability to call multiple tools in a single response. It is always better to speculatively perform multiple searches as a batch that are potentially useful.`,
17
+ description:
18
+ "Fast file pattern matching tool that works with any codebase size",
18
19
  parameters: {
19
20
  type: "object",
20
21
  properties: {
@@ -32,6 +33,13 @@ export const globTool: ToolPlugin = {
32
33
  },
33
34
  },
34
35
  },
36
+ prompt:
37
+ () => `- Fast file pattern matching tool that works with any codebase size
38
+ - Supports glob patterns like "**/*.js" or "src/**/*.ts"
39
+ - Returns matching file paths sorted by modification time
40
+ - Use this tool when you need to find files by name patterns
41
+ - When you are doing an open ended search that may require multiple rounds of globbing and grepping, use the Agent tool instead
42
+ - You can call multiple tools in a single response. It is always better to speculatively perform multiple searches in parallel if they are potentially useful.`,
35
43
  execute: async (
36
44
  args: Record<string, unknown>,
37
45
  context: ToolContext,