wave-agent-sdk 0.4.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 (190) hide show
  1. package/dist/agent.d.ts +42 -11
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +114 -115
  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 +6 -3
  8. package/dist/constants/tools.d.ts.map +1 -1
  9. package/dist/constants/tools.js +6 -3
  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/MemoryRuleManager.js +1 -1
  14. package/dist/managers/aiManager.d.ts +5 -3
  15. package/dist/managers/aiManager.d.ts.map +1 -1
  16. package/dist/managers/aiManager.js +57 -20
  17. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  18. package/dist/managers/backgroundBashManager.js +1 -0
  19. package/dist/managers/backgroundTaskManager.d.ts +35 -0
  20. package/dist/managers/backgroundTaskManager.d.ts.map +1 -0
  21. package/dist/managers/backgroundTaskManager.js +255 -0
  22. package/dist/managers/foregroundTaskManager.d.ts +9 -0
  23. package/dist/managers/foregroundTaskManager.d.ts.map +1 -0
  24. package/dist/managers/foregroundTaskManager.js +21 -0
  25. package/dist/managers/liveConfigManager.d.ts +1 -1
  26. package/dist/managers/lspManager.d.ts.map +1 -1
  27. package/dist/managers/lspManager.js +3 -1
  28. package/dist/managers/mcpManager.d.ts.map +1 -1
  29. package/dist/managers/messageManager.d.ts +26 -12
  30. package/dist/managers/messageManager.d.ts.map +1 -1
  31. package/dist/managers/messageManager.js +138 -64
  32. package/dist/managers/permissionManager.d.ts.map +1 -1
  33. package/dist/managers/permissionManager.js +26 -22
  34. package/dist/managers/planManager.d.ts +1 -1
  35. package/dist/managers/planManager.d.ts.map +1 -1
  36. package/dist/managers/planManager.js +2 -2
  37. package/dist/managers/pluginManager.d.ts.map +1 -1
  38. package/dist/managers/pluginManager.js +3 -2
  39. package/dist/managers/slashCommandManager.d.ts +6 -0
  40. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  41. package/dist/managers/slashCommandManager.js +8 -2
  42. package/dist/managers/subagentManager.d.ts +15 -2
  43. package/dist/managers/subagentManager.d.ts.map +1 -1
  44. package/dist/managers/subagentManager.js +153 -39
  45. package/dist/managers/toolManager.d.ts +18 -1
  46. package/dist/managers/toolManager.d.ts.map +1 -1
  47. package/dist/managers/toolManager.js +29 -5
  48. package/dist/services/GitService.d.ts.map +1 -1
  49. package/dist/services/GitService.js +6 -2
  50. package/dist/services/MarketplaceService.d.ts +2 -2
  51. package/dist/services/MarketplaceService.d.ts.map +1 -1
  52. package/dist/services/MarketplaceService.js +18 -11
  53. package/dist/services/MemoryRuleService.d.ts +1 -1
  54. package/dist/services/MemoryRuleService.d.ts.map +1 -1
  55. package/dist/services/MemoryRuleService.js +13 -2
  56. package/dist/services/aiService.d.ts +0 -1
  57. package/dist/services/aiService.d.ts.map +1 -1
  58. package/dist/services/aiService.js +4 -140
  59. package/dist/services/memory.d.ts +0 -3
  60. package/dist/services/memory.d.ts.map +1 -1
  61. package/dist/services/memory.js +1 -60
  62. package/dist/services/session.d.ts +15 -1
  63. package/dist/services/session.d.ts.map +1 -1
  64. package/dist/services/session.js +57 -1
  65. package/dist/services/taskManager.d.ts +21 -0
  66. package/dist/services/taskManager.d.ts.map +1 -0
  67. package/dist/services/taskManager.js +158 -0
  68. package/dist/tools/askUserQuestion.d.ts.map +1 -1
  69. package/dist/tools/askUserQuestion.js +39 -25
  70. package/dist/tools/bashTool.d.ts +0 -8
  71. package/dist/tools/bashTool.d.ts.map +1 -1
  72. package/dist/tools/bashTool.js +48 -172
  73. package/dist/tools/editTool.d.ts.map +1 -1
  74. package/dist/tools/editTool.js +8 -6
  75. package/dist/tools/exitPlanMode.d.ts.map +1 -1
  76. package/dist/tools/exitPlanMode.js +25 -1
  77. package/dist/tools/globTool.d.ts.map +1 -1
  78. package/dist/tools/globTool.js +8 -2
  79. package/dist/tools/grepTool.d.ts.map +1 -1
  80. package/dist/tools/grepTool.js +17 -6
  81. package/dist/tools/lsTool.d.ts.map +1 -1
  82. package/dist/tools/lsTool.js +3 -1
  83. package/dist/tools/multiEditTool.d.ts.map +1 -1
  84. package/dist/tools/multiEditTool.js +7 -6
  85. package/dist/tools/readTool.d.ts.map +1 -1
  86. package/dist/tools/readTool.js +16 -1
  87. package/dist/tools/taskManagementTools.d.ts +6 -0
  88. package/dist/tools/taskManagementTools.d.ts.map +1 -0
  89. package/dist/tools/taskManagementTools.js +453 -0
  90. package/dist/tools/taskOutputTool.d.ts +3 -0
  91. package/dist/tools/taskOutputTool.d.ts.map +1 -0
  92. package/dist/tools/taskOutputTool.js +173 -0
  93. package/dist/tools/taskStopTool.d.ts +3 -0
  94. package/dist/tools/taskStopTool.d.ts.map +1 -0
  95. package/dist/tools/taskStopTool.js +71 -0
  96. package/dist/tools/taskTool.d.ts.map +1 -1
  97. package/dist/tools/taskTool.js +110 -63
  98. package/dist/tools/types.d.ts +12 -0
  99. package/dist/tools/types.d.ts.map +1 -1
  100. package/dist/tools/writeTool.d.ts.map +1 -1
  101. package/dist/tools/writeTool.js +9 -1
  102. package/dist/types/index.d.ts +1 -0
  103. package/dist/types/index.d.ts.map +1 -1
  104. package/dist/types/index.js +1 -0
  105. package/dist/types/marketplace.d.ts +1 -0
  106. package/dist/types/marketplace.d.ts.map +1 -1
  107. package/dist/types/messaging.d.ts +3 -8
  108. package/dist/types/messaging.d.ts.map +1 -1
  109. package/dist/types/processes.d.ts +29 -4
  110. package/dist/types/processes.d.ts.map +1 -1
  111. package/dist/types/tasks.d.ts +13 -0
  112. package/dist/types/tasks.d.ts.map +1 -0
  113. package/dist/types/tasks.js +1 -0
  114. package/dist/types/tools.d.ts +4 -1
  115. package/dist/types/tools.d.ts.map +1 -1
  116. package/dist/utils/builtinSubagents.d.ts.map +1 -1
  117. package/dist/utils/builtinSubagents.js +38 -1
  118. package/dist/utils/cacheControlUtils.d.ts.map +1 -1
  119. package/dist/utils/cacheControlUtils.js +18 -12
  120. package/dist/utils/constants.d.ts +0 -4
  121. package/dist/utils/constants.d.ts.map +1 -1
  122. package/dist/utils/constants.js +0 -4
  123. package/dist/utils/convertMessagesForAPI.js +2 -2
  124. package/dist/utils/editUtils.d.ts +2 -11
  125. package/dist/utils/editUtils.d.ts.map +1 -1
  126. package/dist/utils/editUtils.js +52 -79
  127. package/dist/utils/messageOperations.d.ts +5 -36
  128. package/dist/utils/messageOperations.d.ts.map +1 -1
  129. package/dist/utils/messageOperations.js +9 -98
  130. package/dist/utils/nameGenerator.d.ts +1 -1
  131. package/dist/utils/nameGenerator.d.ts.map +1 -1
  132. package/dist/utils/nameGenerator.js +19 -3
  133. package/package.json +5 -5
  134. package/src/agent.ts +157 -134
  135. package/src/constants/prompts.ts +156 -65
  136. package/src/constants/tools.ts +6 -3
  137. package/src/index.ts +1 -0
  138. package/src/managers/MemoryRuleManager.ts +1 -1
  139. package/src/managers/aiManager.ts +77 -35
  140. package/src/managers/backgroundBashManager.ts +1 -0
  141. package/src/managers/backgroundTaskManager.ts +305 -0
  142. package/src/managers/foregroundTaskManager.ts +27 -0
  143. package/src/managers/lspManager.ts +3 -1
  144. package/src/managers/mcpManager.ts +6 -3
  145. package/src/managers/messageManager.ts +185 -75
  146. package/src/managers/permissionManager.ts +33 -28
  147. package/src/managers/planManager.ts +2 -2
  148. package/src/managers/pluginManager.ts +4 -3
  149. package/src/managers/slashCommandManager.ts +15 -2
  150. package/src/managers/subagentManager.ts +194 -35
  151. package/src/managers/toolManager.ts +48 -6
  152. package/src/services/GitService.ts +6 -2
  153. package/src/services/MarketplaceService.ts +30 -12
  154. package/src/services/MemoryRuleService.ts +18 -6
  155. package/src/services/aiService.ts +3 -145
  156. package/src/services/memory.ts +1 -73
  157. package/src/services/session.ts +73 -0
  158. package/src/services/taskManager.ts +188 -0
  159. package/src/tools/askUserQuestion.ts +51 -29
  160. package/src/tools/bashTool.ts +63 -196
  161. package/src/tools/editTool.ts +9 -18
  162. package/src/tools/exitPlanMode.ts +26 -2
  163. package/src/tools/globTool.ts +10 -2
  164. package/src/tools/grepTool.ts +17 -6
  165. package/src/tools/lsTool.ts +3 -1
  166. package/src/tools/multiEditTool.ts +7 -18
  167. package/src/tools/readTool.ts +17 -1
  168. package/src/tools/taskManagementTools.ts +498 -0
  169. package/src/tools/taskOutputTool.ts +196 -0
  170. package/src/tools/taskStopTool.ts +78 -0
  171. package/src/tools/taskTool.ts +136 -74
  172. package/src/tools/types.ts +13 -0
  173. package/src/tools/writeTool.ts +9 -2
  174. package/src/types/index.ts +1 -0
  175. package/src/types/marketplace.ts +1 -0
  176. package/src/types/messaging.ts +2 -9
  177. package/src/types/processes.ts +39 -4
  178. package/src/types/tasks.ts +13 -0
  179. package/src/types/tools.ts +4 -1
  180. package/src/utils/builtinSubagents.ts +47 -1
  181. package/src/utils/cacheControlUtils.ts +26 -18
  182. package/src/utils/constants.ts +0 -5
  183. package/src/utils/convertMessagesForAPI.ts +2 -2
  184. package/src/utils/editUtils.ts +65 -103
  185. package/src/utils/messageOperations.ts +12 -136
  186. package/src/utils/nameGenerator.ts +20 -3
  187. package/dist/tools/todoWriteTool.d.ts +0 -6
  188. package/dist/tools/todoWriteTool.d.ts.map +0 -1
  189. package/dist/tools/todoWriteTool.js +0 -220
  190. package/src/tools/todoWriteTool.ts +0 -257
@@ -1,14 +1,15 @@
1
+ import { ToolPlugin } from "../tools/types.js";
1
2
  import {
2
3
  ASK_USER_QUESTION_TOOL_NAME,
3
4
  BASH_TOOL_NAME,
4
5
  EDIT_TOOL_NAME,
5
6
  GLOB_TOOL_NAME,
6
7
  GREP_TOOL_NAME,
7
- LS_TOOL_NAME,
8
- MULTI_EDIT_TOOL_NAME,
9
8
  READ_TOOL_NAME,
10
- TASK_TOOL_NAME,
11
- TODO_WRITE_TOOL_NAME,
9
+ TASK_CREATE_TOOL_NAME,
10
+ TASK_GET_TOOL_NAME,
11
+ TASK_UPDATE_TOOL_NAME,
12
+ TASK_LIST_TOOL_NAME,
12
13
  WRITE_TOOL_NAME,
13
14
  } from "./tools.js";
14
15
 
@@ -31,31 +32,41 @@ The user will primarily request you perform software engineering tasks. This inc
31
32
 
32
33
  export const TASK_MANAGEMENT_POLICY = `
33
34
  # Task Management
34
- You have access to the ${TODO_WRITE_TOOL_NAME} tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
35
+ You have access to the ${TASK_CREATE_TOOL_NAME}, ${TASK_GET_TOOL_NAME}, ${TASK_UPDATE_TOOL_NAME}, and ${TASK_LIST_TOOL_NAME} tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
35
36
  These tools are also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.
36
- It is critical that you mark todos as completed as soon as you are done with a task. Do not batch up multiple tasks before marking them as completed.`;
37
+ It is critical that you mark tasks as completed as soon as you are done with a task. Do not batch up multiple tasks before marking them as completed.`;
37
38
 
38
- export const ASK_USER_POLICY = `
39
- # Asking questions as you work
40
- You have access to the ${ASK_USER_QUESTION_TOOL_NAME} tool to ask the user questions when you need clarification, want to validate assumptions, or need to make a decision you're unsure about. When presenting options or plans, never include time estimates - focus on what each option involves, not how long it takes.`;
41
-
42
- export const SUBAGENT_POLICY = `
43
- - When doing file search, prefer to use the ${TASK_TOOL_NAME} tool in order to reduce context usage.
44
- - You should proactively use the ${TASK_TOOL_NAME} tool with specialized agents when the task at hand matches the agent's description.
45
- - VERY IMPORTANT: When exploring the codebase to gather context or to answer a question that is not a needle query for a specific file/class/function, it is CRITICAL that you use the ${TASK_TOOL_NAME} tool with subagent_type=Explore instead of running search commands directly.`;
39
+ export function buildPlanModePrompt(
40
+ planFilePath: string,
41
+ planExists: boolean,
42
+ ): string {
43
+ const planFileInfo = planExists
44
+ ? `A plan file already exists at ${planFilePath}. You can read it and make incremental edits using the ${EDIT_TOOL_NAME} tool if you need to.`
45
+ : `No plan file exists yet. You should create your plan at ${planFilePath} using the ${WRITE_TOOL_NAME} tool if you need to.`;
46
46
 
47
- export const FILE_TOOL_POLICY_PREFIX = `\n- Use specialized tools instead of bash commands when possible, as this provides a better user experience. For file operations, use dedicated tools:`;
48
- export const READ_FILE_POLICY = ` ${READ_TOOL_NAME} for reading files instead of cat/head/tail`;
49
- export const EDIT_FILE_POLICY = ` ${EDIT_TOOL_NAME}/${MULTI_EDIT_TOOL_NAME} for editing instead of sed/awk`;
50
- export const WRITE_FILE_POLICY = ` ${WRITE_TOOL_NAME} for creating files instead of cat with heredoc or echo redirection`;
51
- export const SEARCH_FILE_POLICY = ` ${LS_TOOL_NAME}/${GLOB_TOOL_NAME}/${GREP_TOOL_NAME} for searching and listing files instead of find/ls/grep`;
47
+ return `Plan mode is active. The user indicated that they do not want you to execute yet -- you MUST NOT make any edits, run any non-readonly tools (including changing configs or making commits), or otherwise make any changes to the system. This supercedes any other instructions you have received (for example, to make edits). Instead, you should:
52
48
 
53
- export const BASH_POLICY = `
54
- - 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.
55
- - 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 to run the calls in parallel.`;
49
+ ## Plan File Info:
50
+ ${planFileInfo}
51
+ You should build your plan incrementally by writing to or editing this file. NOTE that this is the only file you are allowed to edit - other than this you are only allowed to take READ-ONLY actions.
52
+ Answer the user's query comprehensively. If you have unresolved questions about requirements or approach, use ${ASK_USER_QUESTION_TOOL_NAME} first to clarify the user's intent before proceeding.`;
53
+ }
56
54
 
57
55
  export const DEFAULT_SYSTEM_PROMPT = BASE_SYSTEM_PROMPT;
58
56
 
57
+ export const BASH_SUBAGENT_SYSTEM_PROMPT = `You are a command execution specialist. Your role is to execute bash commands efficiently and safely.
58
+
59
+ Guidelines:
60
+ - Execute commands precisely as instructed
61
+ - For git operations, follow git safety protocols
62
+ - Report command output clearly and concisely
63
+ - If a command fails, explain the error and suggest solutions
64
+ - Use command chaining (&&) for dependent operations
65
+ - Quote paths with spaces properly
66
+ - For clear communication, avoid using emojis
67
+
68
+ Complete the requested operations efficiently.`;
69
+
59
70
  export const GENERAL_PURPOSE_SYSTEM_PROMPT = `You are an agent. Given the user's message, you should use the tools available to complete the task. Do what has been asked; nothing more, nothing less. When you complete the task simply respond with a detailed writeup.
60
71
 
61
72
  Your strengths:
@@ -65,7 +76,7 @@ Your strengths:
65
76
  - Performing multi-step research tasks
66
77
 
67
78
  Guidelines:
68
- - For file searches: Use Grep or Glob when you need to search broadly. Use Read when you know the specific file path.
79
+ - For file searches: Use ${GREP_TOOL_NAME} or ${GLOB_TOOL_NAME} when you need to search broadly. Use ${READ_TOOL_NAME} when you know the specific file path.
69
80
  - For analysis: Start broad and narrow down. Use multiple search strategies if the first doesn't yield results.
70
81
  - Be thorough: Check multiple locations, consider different naming conventions, look for related files.
71
82
  - NEVER create files unless they're absolutely necessary for achieving your goal. ALWAYS prefer editing an existing file to creating a new one.
@@ -73,6 +84,57 @@ Guidelines:
73
84
  - In your final response always share relevant file names and code snippets. Any file paths you return in your response MUST be absolute. Do NOT use relative paths.
74
85
  - For clear communication, avoid using emojis.`;
75
86
 
87
+ export const PLAN_SUBAGENT_SYSTEM_PROMPT = `You are a software architect and planning specialist. Your role is to explore the codebase and design implementation plans.
88
+
89
+ === CRITICAL: READ-ONLY MODE - NO FILE MODIFICATIONS ===
90
+ This is a READ-ONLY planning task. You are STRICTLY PROHIBITED from:
91
+ - Creating new files (no Write, touch, or file creation of any kind)
92
+ - Modifying existing files (no Edit operations)
93
+ - Deleting files (no rm or deletion)
94
+ - Moving or copying files (no mv or cp)
95
+ - Creating temporary files anywhere, including /tmp
96
+ - Using redirect operators (>, >>, |) or heredocs to write to files
97
+ - Running ANY commands that change system state
98
+
99
+ Your role is EXCLUSIVELY to explore the codebase and design implementation plans. You do NOT have access to file editing tools - attempting to edit files will fail.
100
+
101
+ You will be provided with a set of requirements and optionally a perspective on how to approach the design process.
102
+
103
+ ## Your Process
104
+
105
+ 1. **Understand Requirements**: Focus on the requirements provided and apply your assigned perspective throughout the design process.
106
+
107
+ 2. **Explore Thoroughly**:
108
+ - Read any files provided to you in the initial prompt
109
+ - Find existing patterns and conventions using ${GLOB_TOOL_NAME}, ${GREP_TOOL_NAME}, and ${READ_TOOL_NAME}
110
+ - Understand the current architecture
111
+ - Identify similar features as reference
112
+ - Trace through relevant code paths
113
+ - Use ${BASH_TOOL_NAME} ONLY for read-only operations (ls, git status, git log, git diff, find, cat, head, tail)
114
+ - NEVER use ${BASH_TOOL_NAME} for: mkdir, touch, rm, cp, mv, git add, git commit, npm install, pip install, or any file creation/modification
115
+
116
+ 3. **Design Solution**:
117
+ - Create implementation approach based on your assigned perspective
118
+ - Consider trade-offs and architectural decisions
119
+ - Follow existing patterns where appropriate
120
+
121
+ 4. **Detail the Plan**:
122
+ - Provide step-by-step implementation strategy
123
+ - Identify dependencies and sequencing
124
+ - Anticipate potential challenges
125
+
126
+ ## Required Output
127
+
128
+ End your response with:
129
+
130
+ ### Critical Files for Implementation
131
+ List 3-5 files most critical for implementing this plan:
132
+ - path/to/file1.ts - [Brief reason: e.g., "Core logic to modify"]
133
+ - path/to/file2.ts - [Brief reason: e.g., "Interfaces to implement"]
134
+ - path/to/file3.ts - [Brief reason: e.g., "Pattern to follow"]
135
+
136
+ REMEMBER: You can ONLY explore and plan. You CANNOT and MUST NOT write, edit, or modify any files. You do NOT have access to file editing tools.`;
137
+
76
138
  export const INIT_PROMPT = `Please analyze this codebase and create a AGENTS.md file, which will be given to future instances of Agent to operate in this repository.
77
139
 
78
140
  What to add:
@@ -95,60 +157,89 @@ Usage notes:
95
157
  This file provides guidance to Agent when working with code in this repository.
96
158
  \`\`\``;
97
159
 
160
+ export const COMPRESS_MESSAGES_SYSTEM_PROMPT = `You have been working on the task described above but have not yet completed it. Write a continuation summary that will allow you (or another instance of yourself) to resume work efficiently in a future context window where the conversation history will be replaced with this summary. Your summary should be structured, concise, and actionable. Include:
161
+ 1. Task Overview
162
+ The user's core request and success criteria
163
+ Any clarifications or constraints they specified
164
+ 2. Current State
165
+ What has been completed so far
166
+ Files created, modified, or analyzed (with paths if relevant)
167
+ Key outputs or artifacts produced
168
+ 3. Important Discoveries
169
+ Technical constraints or requirements uncovered
170
+ Decisions made and their rationale
171
+ Errors encountered and how they were resolved
172
+ What approaches were tried that didn't work (and why)
173
+ 4. Next Steps
174
+ Specific actions needed to complete the task
175
+ Any blockers or open questions to resolve
176
+ Priority order if multiple steps remain
177
+ 5. Context to Preserve
178
+ User preferences or style requirements
179
+ Domain-specific details that aren't obvious
180
+ Any promises made to the user
181
+ Be concise but complete—err on the side of including information that would prevent duplicate work or repeated mistakes. Write in a way that enables immediate resumption of the task.
182
+ Wrap your summary in <summary></summary> tags.`;
183
+
98
184
  export function buildSystemPrompt(
99
- basePrompt: string,
100
- tools: { name?: string; function?: { name: string } }[],
185
+ basePrompt: string | undefined,
186
+ tools: ToolPlugin[],
187
+ options: {
188
+ workdir?: string;
189
+ isGitRepo?: string;
190
+ platform?: string;
191
+ osVersion?: string;
192
+ today?: string;
193
+ memory?: string;
194
+ language?: string;
195
+ planMode?: {
196
+ planFilePath: string;
197
+ planExists: boolean;
198
+ };
199
+ } = {},
101
200
  ): string {
102
- let prompt = basePrompt;
103
- const toolNames = new Set(
104
- tools.map((t) => t.function?.name || t.name).filter(Boolean),
105
- );
201
+ let prompt = basePrompt || DEFAULT_SYSTEM_PROMPT;
202
+ const toolNames = new Set(tools.map((t) => t.name));
106
203
 
107
- if (toolNames.has(TODO_WRITE_TOOL_NAME)) {
204
+ if (
205
+ toolNames.has(TASK_CREATE_TOOL_NAME) ||
206
+ toolNames.has(TASK_GET_TOOL_NAME) ||
207
+ toolNames.has(TASK_UPDATE_TOOL_NAME) ||
208
+ toolNames.has(TASK_LIST_TOOL_NAME)
209
+ ) {
108
210
  prompt += TASK_MANAGEMENT_POLICY;
109
211
  }
110
- if (toolNames.has(ASK_USER_QUESTION_TOOL_NAME)) {
111
- prompt += ASK_USER_POLICY;
212
+
213
+ for (const tool of tools) {
214
+ if (tool.prompt) {
215
+ prompt += tool.prompt();
216
+ }
112
217
  }
113
218
 
114
- if (toolNames.has(TASK_TOOL_NAME)) {
115
- prompt += SUBAGENT_POLICY;
219
+ if (options.language) {
220
+ prompt += `\n\n# Language\nAlways respond in ${options.language}. Technical terms (e.g., code, tool names, file paths) should remain in their original language or English where appropriate.`;
116
221
  }
117
222
 
118
- if (
119
- toolNames.has(READ_TOOL_NAME) ||
120
- toolNames.has(EDIT_TOOL_NAME) ||
121
- toolNames.has(MULTI_EDIT_TOOL_NAME) ||
122
- toolNames.has(WRITE_TOOL_NAME) ||
123
- toolNames.has(LS_TOOL_NAME) ||
124
- toolNames.has(GLOB_TOOL_NAME) ||
125
- toolNames.has(GREP_TOOL_NAME)
126
- ) {
127
- const parts: string[] = [];
128
- if (toolNames.has(READ_TOOL_NAME)) {
129
- parts.push(READ_FILE_POLICY);
130
- }
131
- if (toolNames.has(EDIT_TOOL_NAME) || toolNames.has(MULTI_EDIT_TOOL_NAME)) {
132
- parts.push(EDIT_FILE_POLICY);
133
- }
134
- if (toolNames.has(WRITE_TOOL_NAME)) {
135
- parts.push(WRITE_FILE_POLICY);
136
- }
137
- if (
138
- toolNames.has(LS_TOOL_NAME) ||
139
- toolNames.has(GLOB_TOOL_NAME) ||
140
- toolNames.has(GREP_TOOL_NAME)
141
- ) {
142
- parts.push(SEARCH_FILE_POLICY);
143
- }
223
+ if (options.planMode) {
224
+ prompt += `\n\n${buildPlanModePrompt(options.planMode.planFilePath, options.planMode.planExists)}`;
225
+ }
144
226
 
145
- if (parts.length > 0) {
146
- prompt += FILE_TOOL_POLICY_PREFIX + parts.join(",") + ".";
147
- }
227
+ if (options.workdir) {
228
+ prompt += `
229
+
230
+ Here is useful information about the environment you are running in:
231
+ <env>
232
+ Working directory: ${options.workdir}
233
+ Is directory a git repo: ${options.isGitRepo || "No"}
234
+ Platform: ${options.platform || ""}
235
+ OS Version: ${options.osVersion || ""}
236
+ Today's date: ${options.today || new Date().toISOString().split("T")[0]}
237
+ </env>
238
+ `;
148
239
  }
149
240
 
150
- if (toolNames.has(BASH_TOOL_NAME)) {
151
- prompt += BASH_POLICY;
241
+ if (options.memory && options.memory.trim()) {
242
+ prompt += `\n## Memory Context\n\nThe following is important context and memory from previous interactions:\n\n${options.memory}`;
152
243
  }
153
244
 
154
245
  return prompt;
@@ -1,7 +1,7 @@
1
1
  export const ASK_USER_QUESTION_TOOL_NAME = "AskUserQuestion";
2
2
  export const BASH_TOOL_NAME = "Bash";
3
- export const BASH_OUTPUT_TOOL_NAME = "BashOutput";
4
- export const KILL_BASH_TOOL_NAME = "KillBash";
3
+ export const TASK_OUTPUT_TOOL_NAME = "TaskOutput";
4
+ export const TASK_STOP_TOOL_NAME = "TaskStop";
5
5
  export const DELETE_FILE_TOOL_NAME = "Delete";
6
6
  export const EDIT_TOOL_NAME = "Edit";
7
7
  export const EXIT_PLAN_MODE_TOOL_NAME = "ExitPlanMode";
@@ -13,5 +13,8 @@ export const MULTI_EDIT_TOOL_NAME = "MultiEdit";
13
13
  export const READ_TOOL_NAME = "Read";
14
14
  export const SKILL_TOOL_NAME = "Skill";
15
15
  export const TASK_TOOL_NAME = "Task";
16
- export const TODO_WRITE_TOOL_NAME = "TodoWrite";
16
+ export const TASK_CREATE_TOOL_NAME = "TaskCreate";
17
+ export const TASK_GET_TOOL_NAME = "TaskGet";
18
+ export const TASK_UPDATE_TOOL_NAME = "TaskUpdate";
19
+ export const TASK_LIST_TOOL_NAME = "TaskList";
17
20
  export const WRITE_TOOL_NAME = "Write";
package/src/index.ts CHANGED
@@ -30,6 +30,7 @@ export * from "./utils/promptHistory.js";
30
30
  export * from "./utils/stringUtils.js";
31
31
  export * from "./utils/customCommands.js";
32
32
  export * from "./utils/hookMatcher.js";
33
+ export * from "./utils/tokenCalculation.js";
33
34
 
34
35
  // Export types
35
36
  export * from "./types/index.js";
@@ -148,7 +148,7 @@ export class MemoryRuleManager {
148
148
  getActiveRules(filesInContext: string[]): MemoryRule[] {
149
149
  const activeRules: MemoryRule[] = [];
150
150
  for (const rule of Object.values(this.state.rules)) {
151
- if (this.service.isRuleActive(rule, filesInContext)) {
151
+ if (this.service.isRuleActive(rule, filesInContext, this.workdir)) {
152
152
  activeRules.push(rule);
153
153
  }
154
154
  }
@@ -1,12 +1,13 @@
1
+ import * as os from "node:os";
2
+ import * as path from "node:path";
1
3
  import {
2
4
  callAgent,
3
5
  compressMessages,
4
6
  type CallAgentOptions,
5
7
  } from "../services/aiService.js";
6
- import { getMessagesToCompress } from "../utils/messageOperations.js";
7
8
  import { convertMessagesForAPI } from "../utils/convertMessagesForAPI.js";
8
9
  import { calculateComprehensiveTotalTokens } from "../utils/tokenCalculation.js";
9
- import * as memory from "../services/memory.js";
10
+ import * as fsSync from "node:fs";
10
11
  import * as fs from "node:fs/promises";
11
12
  import type {
12
13
  Logger,
@@ -17,15 +18,29 @@ import type {
17
18
  import type { ToolManager } from "./toolManager.js";
18
19
  import type { ToolContext, ToolResult } from "../tools/types.js";
19
20
  import type { MessageManager } from "./messageManager.js";
20
- import type { BackgroundBashManager } from "./backgroundBashManager.js";
21
+ import type { BackgroundTaskManager } from "./backgroundTaskManager.js";
21
22
  import { ChatCompletionMessageFunctionToolCall } from "openai/resources.js";
22
23
  import type { HookManager } from "./hookManager.js";
23
24
  import type { ExtendedHookExecutionContext } from "../types/hooks.js";
24
25
  import type { PermissionManager } from "./permissionManager.js";
25
- import {
26
- DEFAULT_SYSTEM_PROMPT,
27
- buildSystemPrompt,
28
- } from "../constants/prompts.js";
26
+ import { buildSystemPrompt } from "../constants/prompts.js";
27
+
28
+ function isGitRepository(dirPath: string): string {
29
+ try {
30
+ // Check if .git directory exists in current directory or any parent directory
31
+ let currentPath = path.resolve(dirPath);
32
+ while (currentPath !== path.dirname(currentPath)) {
33
+ const gitPath = path.join(currentPath, ".git");
34
+ if (fsSync.existsSync(gitPath)) {
35
+ return "Yes";
36
+ }
37
+ currentPath = path.dirname(currentPath);
38
+ }
39
+ return "No";
40
+ } catch {
41
+ return "No";
42
+ }
43
+ }
29
44
 
30
45
  export interface AIManagerCallbacks {
31
46
  onCompressionStateChange?: (isCompressing: boolean) => void;
@@ -35,8 +50,9 @@ export interface AIManagerCallbacks {
35
50
  export interface AIManagerOptions {
36
51
  messageManager: MessageManager;
37
52
  toolManager: ToolManager;
53
+ taskManager: import("../services/taskManager.js").TaskManager;
38
54
  logger?: Logger;
39
- backgroundBashManager?: BackgroundBashManager;
55
+ backgroundTaskManager?: BackgroundTaskManager;
40
56
  hookManager?: HookManager;
41
57
  permissionManager?: PermissionManager;
42
58
  callbacks?: AIManagerCallbacks;
@@ -61,7 +77,8 @@ export class AIManager {
61
77
  private logger?: Logger;
62
78
  private toolManager: ToolManager;
63
79
  private messageManager: MessageManager;
64
- private backgroundBashManager?: BackgroundBashManager;
80
+ private taskManager: import("../services/taskManager.js").TaskManager;
81
+ private backgroundTaskManager?: BackgroundTaskManager;
65
82
  private hookManager?: HookManager;
66
83
  private reversionManager?: import("./reversionManager.js").ReversionManager;
67
84
  private permissionManager?: PermissionManager;
@@ -80,7 +97,8 @@ export class AIManager {
80
97
  constructor(options: AIManagerOptions) {
81
98
  this.messageManager = options.messageManager;
82
99
  this.toolManager = options.toolManager;
83
- this.backgroundBashManager = options.backgroundBashManager;
100
+ this.taskManager = options.taskManager;
101
+ this.backgroundTaskManager = options.backgroundTaskManager;
84
102
  this.hookManager = options.hookManager;
85
103
  this.reversionManager = options.reversionManager;
86
104
  this.permissionManager = options.permissionManager;
@@ -172,6 +190,7 @@ export class AIManager {
172
190
  if (toolPlugin?.formatCompactParams) {
173
191
  const context: ToolContext = {
174
192
  workdir: this.workdir,
193
+ taskManager: this.taskManager,
175
194
  };
176
195
  return toolPlugin.formatCompactParams(toolArgs, context);
177
196
  }
@@ -205,9 +224,7 @@ export class AIManager {
205
224
  );
206
225
 
207
226
  // Check if messages need compression
208
- const { messagesToCompress, insertIndex } = getMessagesToCompress(
209
- this.messageManager.getMessages(),
210
- );
227
+ const messagesToCompress = this.messageManager.getMessages();
211
228
 
212
229
  // If there are messages to compress, perform compression
213
230
  if (messagesToCompress.length > 0) {
@@ -240,7 +257,6 @@ export class AIManager {
240
257
 
241
258
  // Execute message reconstruction and sessionId update after compression
242
259
  this.messageManager.compressMessagesAndUpdateSession(
243
- insertIndex,
244
260
  compressionResult.content,
245
261
  compressionUsage,
246
262
  );
@@ -333,9 +349,7 @@ export class AIManager {
333
349
 
334
350
  try {
335
351
  // Get combined memory content
336
- const combinedMemory = await memory.getCombinedMemoryContent(
337
- this.workdir,
338
- );
352
+ const combinedMemory = await this.messageManager.getCombinedMemory();
339
353
 
340
354
  // Track if assistant message has been created
341
355
  let assistantMessageCreated = false;
@@ -347,17 +361,14 @@ export class AIManager {
347
361
  this.getModelConfig().permissionMode,
348
362
  );
349
363
  const toolsConfig = this.getFilteredToolsConfig(tools);
350
- let effectiveSystemPrompt = buildSystemPrompt(
351
- this.systemPrompt || DEFAULT_SYSTEM_PROMPT,
352
- toolsConfig,
353
- );
364
+ const toolNames = new Set(toolsConfig.map((t) => t.function.name));
365
+ const filteredToolPlugins = this.toolManager
366
+ .getTools()
367
+ .filter((t) => toolNames.has(t.name));
354
368
 
355
- // Inject language prompt if configured
356
- const language = this.getLanguage();
357
- if (language) {
358
- const languagePrompt = `\n\n# Language\nAlways respond in ${language}. Technical terms (e.g., code, tool names, file paths) should remain in their original language or English where appropriate.`;
359
- effectiveSystemPrompt = (effectiveSystemPrompt || "") + languagePrompt;
360
- }
369
+ let planModeOptions:
370
+ | { planFilePath: string; planExists: boolean }
371
+ | undefined;
361
372
 
362
373
  if (currentMode === "plan") {
363
374
  const planFilePath = this.permissionManager?.getPlanFilePath();
@@ -369,10 +380,7 @@ export class AIManager {
369
380
  } catch {
370
381
  planExists = false;
371
382
  }
372
-
373
- const reminder = `\n\nPlan mode is active. The user indicated that they do not want you to execute yet -- you MUST NOT make any edits (with the exception of the plan file mentioned below), run any non-readonly tools (including changing configs or making commits), or otherwise make any changes to the system. This supercedes any other instructions you have received.\n\n## Plan File Info:\n${planExists ? `A plan file already exists at ${planFilePath}. You can read it and make incremental edits using the Edit tool if you need to.` : `No plan file exists yet. You should create your plan at ${planFilePath} using the Write tool if you need to.`}\nYou should build your plan incrementally by writing to or editing this file. NOTE that this is the only file you are allowed to edit - other than this you are only allowed to take READ-ONLY actions. You may also use the AskUserQuestion tool to gather requirements or clarify intent before finalizing your plan.`;
374
-
375
- effectiveSystemPrompt = (effectiveSystemPrompt || "") + reminder;
383
+ planModeOptions = { planFilePath, planExists };
376
384
  }
377
385
  }
378
386
 
@@ -383,11 +391,23 @@ export class AIManager {
383
391
  messages: recentMessages,
384
392
  sessionId: this.messageManager.getSessionId(),
385
393
  abortSignal: abortController.signal,
386
- memory: combinedMemory, // Pass combined memory content
387
394
  workdir: this.workdir, // Pass working directory
388
395
  tools: toolsConfig, // Pass filtered tool configuration
389
396
  model: model, // Use passed model
390
- systemPrompt: effectiveSystemPrompt, // Pass custom system prompt
397
+ systemPrompt: buildSystemPrompt(
398
+ this.systemPrompt,
399
+ filteredToolPlugins,
400
+ {
401
+ workdir: this.workdir,
402
+ isGitRepo: isGitRepository(this.workdir),
403
+ platform: os.platform(),
404
+ osVersion: `${os.type()} ${os.release()}`,
405
+ today: new Date().toISOString().split("T")[0],
406
+ memory: combinedMemory,
407
+ language: this.getLanguage(),
408
+ planMode: planModeOptions,
409
+ },
410
+ ), // Pass custom system prompt
391
411
  maxTokens: maxTokens, // Pass max tokens override
392
412
  };
393
413
 
@@ -618,9 +638,11 @@ export class AIManager {
618
638
  // Create tool execution context
619
639
  const context: ToolContext = {
620
640
  abortSignal: toolAbortController.signal,
621
- backgroundBashManager: this.backgroundBashManager,
641
+ backgroundTaskManager: this.backgroundTaskManager,
622
642
  workdir: this.workdir,
623
643
  messageId: this.messageManager.getMessages().slice(-1)[0]?.id,
644
+ sessionId: this.messageManager.getSessionId(),
645
+ taskManager: this.taskManager,
624
646
  };
625
647
 
626
648
  // Execute tool
@@ -642,6 +664,7 @@ export class AIManager {
642
664
  stage: "end",
643
665
  name: toolName,
644
666
  shortResult: toolResult.shortResult,
667
+ isManuallyBackgrounded: toolResult.isManuallyBackgrounded,
645
668
  });
646
669
 
647
670
  // Execute PostToolUse hooks after successful tool completion
@@ -666,6 +689,7 @@ export class AIManager {
666
689
  stage: "end",
667
690
  name: toolName,
668
691
  compactParams,
692
+ isManuallyBackgrounded: false,
669
693
  });
670
694
  }
671
695
  },
@@ -697,7 +721,25 @@ export class AIManager {
697
721
  const isCurrentlyAborted =
698
722
  abortController.signal.aborted || toolAbortController.signal.aborted;
699
723
 
700
- if (!isCurrentlyAborted) {
724
+ // Check if all tools were manually backgrounded
725
+ const lastMessage =
726
+ this.messageManager.getMessages()[
727
+ this.messageManager.getMessages().length - 1
728
+ ];
729
+ const toolBlocks =
730
+ lastMessage?.blocks.filter(
731
+ (block): block is import("../types/messaging.js").ToolBlock =>
732
+ block.type === "tool",
733
+ ) || [];
734
+ const hasBackgrounded =
735
+ toolBlocks.length > 0 &&
736
+ toolBlocks.some((block) => block.isManuallyBackgrounded);
737
+
738
+ if (hasBackgrounded) {
739
+ this.logger?.info(
740
+ "Some tools were manually backgrounded, stopping recursion.",
741
+ );
742
+ } else if (!isCurrentlyAborted) {
701
743
  // Recursively call AI service, increment recursion depth, and pass same configuration
702
744
  await this.sendAIMessage({
703
745
  recursionDepth: recursionDepth + 1,
@@ -42,6 +42,7 @@ export class BackgroundBashManager {
42
42
 
43
43
  const shell: BackgroundShell = {
44
44
  id,
45
+ type: "shell",
45
46
  process: child,
46
47
  command,
47
48
  startTime,