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.
- package/dist/agent.d.ts +14 -6
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +65 -88
- package/dist/constants/prompts.d.ts +18 -14
- package/dist/constants/prompts.d.ts.map +1 -1
- package/dist/constants/prompts.js +130 -54
- package/dist/constants/tools.d.ts +4 -1
- package/dist/constants/tools.d.ts.map +1 -1
- package/dist/constants/tools.js +4 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/managers/aiManager.d.ts +2 -5
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +59 -48
- package/dist/managers/backgroundTaskManager.d.ts.map +1 -1
- package/dist/managers/backgroundTaskManager.js +59 -53
- package/dist/managers/foregroundTaskManager.d.ts.map +1 -1
- package/dist/managers/foregroundTaskManager.js +3 -2
- package/dist/managers/mcpManager.d.ts.map +1 -1
- package/dist/managers/messageManager.d.ts +14 -10
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +102 -62
- package/dist/managers/permissionManager.d.ts.map +1 -1
- package/dist/managers/permissionManager.js +25 -15
- package/dist/managers/planManager.d.ts +1 -1
- package/dist/managers/planManager.d.ts.map +1 -1
- package/dist/managers/planManager.js +2 -2
- package/dist/managers/slashCommandManager.d.ts +3 -0
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +7 -2
- package/dist/managers/subagentManager.d.ts +4 -0
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +22 -14
- package/dist/managers/toolManager.d.ts +11 -0
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/managers/toolManager.js +20 -2
- package/dist/services/aiService.d.ts +0 -1
- package/dist/services/aiService.d.ts.map +1 -1
- package/dist/services/aiService.js +4 -140
- package/dist/services/memory.d.ts +0 -3
- package/dist/services/memory.d.ts.map +1 -1
- package/dist/services/memory.js +0 -59
- package/dist/services/session.d.ts +15 -1
- package/dist/services/session.d.ts.map +1 -1
- package/dist/services/session.js +57 -1
- package/dist/services/taskManager.d.ts +21 -0
- package/dist/services/taskManager.d.ts.map +1 -0
- package/dist/services/taskManager.js +158 -0
- package/dist/tools/askUserQuestion.d.ts.map +1 -1
- package/dist/tools/askUserQuestion.js +39 -25
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +7 -9
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +2 -1
- package/dist/tools/exitPlanMode.d.ts.map +1 -1
- package/dist/tools/exitPlanMode.js +25 -1
- package/dist/tools/globTool.d.ts.map +1 -1
- package/dist/tools/globTool.js +8 -2
- package/dist/tools/grepTool.d.ts.map +1 -1
- package/dist/tools/grepTool.js +17 -6
- package/dist/tools/lsTool.d.ts.map +1 -1
- package/dist/tools/lsTool.js +3 -1
- package/dist/tools/readTool.d.ts.map +1 -1
- package/dist/tools/readTool.js +16 -1
- package/dist/tools/taskManagementTools.d.ts +6 -0
- package/dist/tools/taskManagementTools.d.ts.map +1 -0
- package/dist/tools/taskManagementTools.js +453 -0
- package/dist/tools/taskOutputTool.d.ts.map +1 -1
- package/dist/tools/taskOutputTool.js +32 -8
- package/dist/tools/taskStopTool.d.ts.map +1 -1
- package/dist/tools/taskStopTool.js +7 -1
- package/dist/tools/taskTool.d.ts.map +1 -1
- package/dist/tools/taskTool.js +6 -1
- package/dist/tools/types.d.ts +9 -0
- package/dist/tools/types.d.ts.map +1 -1
- package/dist/tools/writeTool.d.ts.map +1 -1
- package/dist/tools/writeTool.js +9 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/messaging.d.ts +2 -8
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/processes.d.ts +11 -6
- package/dist/types/processes.d.ts.map +1 -1
- package/dist/types/tasks.d.ts +13 -0
- package/dist/types/tasks.d.ts.map +1 -0
- package/dist/types/tasks.js +1 -0
- package/dist/types/tools.d.ts +4 -1
- package/dist/types/tools.d.ts.map +1 -1
- package/dist/utils/builtinSubagents.d.ts.map +1 -1
- package/dist/utils/builtinSubagents.js +38 -1
- package/dist/utils/cacheControlUtils.d.ts.map +1 -1
- package/dist/utils/cacheControlUtils.js +18 -12
- package/dist/utils/constants.d.ts +0 -4
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +0 -4
- package/dist/utils/convertMessagesForAPI.js +2 -2
- package/dist/utils/messageOperations.d.ts +2 -35
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +4 -97
- package/dist/utils/nameGenerator.d.ts +1 -1
- package/dist/utils/nameGenerator.d.ts.map +1 -1
- package/dist/utils/nameGenerator.js +19 -3
- package/package.json +1 -1
- package/src/agent.ts +90 -101
- package/src/constants/prompts.ts +156 -65
- package/src/constants/tools.ts +4 -1
- package/src/index.ts +1 -0
- package/src/managers/aiManager.ts +79 -70
- package/src/managers/backgroundTaskManager.ts +53 -54
- package/src/managers/foregroundTaskManager.ts +3 -2
- package/src/managers/mcpManager.ts +6 -3
- package/src/managers/messageManager.ts +137 -73
- package/src/managers/permissionManager.ts +32 -21
- package/src/managers/planManager.ts +2 -2
- package/src/managers/slashCommandManager.ts +11 -2
- package/src/managers/subagentManager.ts +33 -14
- package/src/managers/toolManager.ts +32 -2
- package/src/services/aiService.ts +3 -145
- package/src/services/memory.ts +0 -72
- package/src/services/session.ts +73 -0
- package/src/services/taskManager.ts +188 -0
- package/src/tools/askUserQuestion.ts +51 -29
- package/src/tools/bashTool.ts +9 -15
- package/src/tools/editTool.ts +3 -1
- package/src/tools/exitPlanMode.ts +26 -2
- package/src/tools/globTool.ts +10 -2
- package/src/tools/grepTool.ts +17 -6
- package/src/tools/lsTool.ts +3 -1
- package/src/tools/readTool.ts +17 -1
- package/src/tools/taskManagementTools.ts +498 -0
- package/src/tools/taskOutputTool.ts +34 -12
- package/src/tools/taskStopTool.ts +7 -1
- package/src/tools/taskTool.ts +7 -1
- package/src/tools/types.ts +10 -0
- package/src/tools/writeTool.ts +9 -2
- package/src/types/index.ts +1 -0
- package/src/types/messaging.ts +1 -9
- package/src/types/processes.ts +13 -7
- package/src/types/tasks.ts +13 -0
- package/src/types/tools.ts +4 -1
- package/src/utils/builtinSubagents.ts +47 -1
- package/src/utils/cacheControlUtils.ts +26 -18
- package/src/utils/constants.ts +0 -5
- package/src/utils/convertMessagesForAPI.ts +2 -2
- package/src/utils/messageOperations.ts +5 -136
- package/src/utils/nameGenerator.ts +20 -3
- package/dist/tools/todoWriteTool.d.ts +0 -6
- package/dist/tools/todoWriteTool.d.ts.map +0 -1
- package/dist/tools/todoWriteTool.js +0 -220
- package/src/tools/todoWriteTool.ts +0 -257
package/src/constants/prompts.ts
CHANGED
|
@@ -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
|
-
|
|
11
|
-
|
|
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 ${
|
|
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
|
|
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
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
|
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:
|
|
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 (
|
|
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
|
-
|
|
111
|
-
|
|
212
|
+
|
|
213
|
+
for (const tool of tools) {
|
|
214
|
+
if (tool.prompt) {
|
|
215
|
+
prompt += tool.prompt();
|
|
216
|
+
}
|
|
112
217
|
}
|
|
113
218
|
|
|
114
|
-
if (
|
|
115
|
-
prompt +=
|
|
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
|
-
|
|
120
|
-
|
|
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
|
-
|
|
146
|
-
|
|
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 (
|
|
151
|
-
prompt +=
|
|
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;
|
package/src/constants/tools.ts
CHANGED
|
@@ -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
|
|
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";
|
|
@@ -1,11 +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";
|
|
10
|
+
import * as fsSync from "node:fs";
|
|
9
11
|
import * as fs from "node:fs/promises";
|
|
10
12
|
import type {
|
|
11
13
|
Logger,
|
|
@@ -21,10 +23,24 @@ import { ChatCompletionMessageFunctionToolCall } from "openai/resources.js";
|
|
|
21
23
|
import type { HookManager } from "./hookManager.js";
|
|
22
24
|
import type { ExtendedHookExecutionContext } from "../types/hooks.js";
|
|
23
25
|
import type { PermissionManager } from "./permissionManager.js";
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
}
|
|
28
44
|
|
|
29
45
|
export interface AIManagerCallbacks {
|
|
30
46
|
onCompressionStateChange?: (isCompressing: boolean) => void;
|
|
@@ -34,6 +50,7 @@ export interface AIManagerCallbacks {
|
|
|
34
50
|
export interface AIManagerOptions {
|
|
35
51
|
messageManager: MessageManager;
|
|
36
52
|
toolManager: ToolManager;
|
|
53
|
+
taskManager: import("../services/taskManager.js").TaskManager;
|
|
37
54
|
logger?: Logger;
|
|
38
55
|
backgroundTaskManager?: BackgroundTaskManager;
|
|
39
56
|
hookManager?: HookManager;
|
|
@@ -60,6 +77,7 @@ export class AIManager {
|
|
|
60
77
|
private logger?: Logger;
|
|
61
78
|
private toolManager: ToolManager;
|
|
62
79
|
private messageManager: MessageManager;
|
|
80
|
+
private taskManager: import("../services/taskManager.js").TaskManager;
|
|
63
81
|
private backgroundTaskManager?: BackgroundTaskManager;
|
|
64
82
|
private hookManager?: HookManager;
|
|
65
83
|
private reversionManager?: import("./reversionManager.js").ReversionManager;
|
|
@@ -79,6 +97,7 @@ export class AIManager {
|
|
|
79
97
|
constructor(options: AIManagerOptions) {
|
|
80
98
|
this.messageManager = options.messageManager;
|
|
81
99
|
this.toolManager = options.toolManager;
|
|
100
|
+
this.taskManager = options.taskManager;
|
|
82
101
|
this.backgroundTaskManager = options.backgroundTaskManager;
|
|
83
102
|
this.hookManager = options.hookManager;
|
|
84
103
|
this.reversionManager = options.reversionManager;
|
|
@@ -159,15 +178,6 @@ export class AIManager {
|
|
|
159
178
|
this.setIsLoading(false);
|
|
160
179
|
}
|
|
161
180
|
|
|
162
|
-
/**
|
|
163
|
-
* Abort the AI recursion loop immediately.
|
|
164
|
-
* This is used when a tool is backgrounded via Ctrl-B, even if no foreground task was active.
|
|
165
|
-
*/
|
|
166
|
-
public abortRecursion(): void {
|
|
167
|
-
this.logger?.info("Aborting AI recursion loop");
|
|
168
|
-
this.abortAIMessage();
|
|
169
|
-
}
|
|
170
|
-
|
|
171
181
|
// Helper method to generate compactParams
|
|
172
182
|
private generateCompactParams(
|
|
173
183
|
toolName: string,
|
|
@@ -180,6 +190,7 @@ export class AIManager {
|
|
|
180
190
|
if (toolPlugin?.formatCompactParams) {
|
|
181
191
|
const context: ToolContext = {
|
|
182
192
|
workdir: this.workdir,
|
|
193
|
+
taskManager: this.taskManager,
|
|
183
194
|
};
|
|
184
195
|
return toolPlugin.formatCompactParams(toolArgs, context);
|
|
185
196
|
}
|
|
@@ -213,9 +224,7 @@ export class AIManager {
|
|
|
213
224
|
);
|
|
214
225
|
|
|
215
226
|
// Check if messages need compression
|
|
216
|
-
const
|
|
217
|
-
this.messageManager.getMessages(),
|
|
218
|
-
);
|
|
227
|
+
const messagesToCompress = this.messageManager.getMessages();
|
|
219
228
|
|
|
220
229
|
// If there are messages to compress, perform compression
|
|
221
230
|
if (messagesToCompress.length > 0) {
|
|
@@ -248,7 +257,6 @@ export class AIManager {
|
|
|
248
257
|
|
|
249
258
|
// Execute message reconstruction and sessionId update after compression
|
|
250
259
|
this.messageManager.compressMessagesAndUpdateSession(
|
|
251
|
-
insertIndex,
|
|
252
260
|
compressionResult.content,
|
|
253
261
|
compressionUsage,
|
|
254
262
|
);
|
|
@@ -305,6 +313,9 @@ export class AIManager {
|
|
|
305
313
|
return;
|
|
306
314
|
}
|
|
307
315
|
|
|
316
|
+
// Save session in each recursion to ensure message persistence
|
|
317
|
+
await this.messageManager.saveSession();
|
|
318
|
+
|
|
308
319
|
// Only create new AbortControllers for the initial call (recursionDepth === 0)
|
|
309
320
|
// For recursive calls, reuse existing controllers to maintain abort signal
|
|
310
321
|
let abortController: AbortController;
|
|
@@ -319,14 +330,10 @@ export class AIManager {
|
|
|
319
330
|
this.toolAbortController = toolAbortController;
|
|
320
331
|
} else {
|
|
321
332
|
// Reuse existing controllers for recursive calls
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
toolAbortController = this.toolAbortController || new AbortController();
|
|
333
|
+
abortController = this.abortController!;
|
|
334
|
+
toolAbortController = this.toolAbortController!;
|
|
325
335
|
}
|
|
326
336
|
|
|
327
|
-
// Save session in each recursion to ensure message persistence
|
|
328
|
-
await this.messageManager.saveSession();
|
|
329
|
-
|
|
330
337
|
// Only set loading state for the initial call
|
|
331
338
|
if (recursionDepth === 0) {
|
|
332
339
|
this.setIsLoading(true);
|
|
@@ -354,17 +361,14 @@ export class AIManager {
|
|
|
354
361
|
this.getModelConfig().permissionMode,
|
|
355
362
|
);
|
|
356
363
|
const toolsConfig = this.getFilteredToolsConfig(tools);
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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));
|
|
361
368
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
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.`;
|
|
366
|
-
effectiveSystemPrompt = (effectiveSystemPrompt || "") + languagePrompt;
|
|
367
|
-
}
|
|
369
|
+
let planModeOptions:
|
|
370
|
+
| { planFilePath: string; planExists: boolean }
|
|
371
|
+
| undefined;
|
|
368
372
|
|
|
369
373
|
if (currentMode === "plan") {
|
|
370
374
|
const planFilePath = this.permissionManager?.getPlanFilePath();
|
|
@@ -376,10 +380,7 @@ export class AIManager {
|
|
|
376
380
|
} catch {
|
|
377
381
|
planExists = false;
|
|
378
382
|
}
|
|
379
|
-
|
|
380
|
-
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.`;
|
|
381
|
-
|
|
382
|
-
effectiveSystemPrompt = (effectiveSystemPrompt || "") + reminder;
|
|
383
|
+
planModeOptions = { planFilePath, planExists };
|
|
383
384
|
}
|
|
384
385
|
}
|
|
385
386
|
|
|
@@ -390,11 +391,23 @@ export class AIManager {
|
|
|
390
391
|
messages: recentMessages,
|
|
391
392
|
sessionId: this.messageManager.getSessionId(),
|
|
392
393
|
abortSignal: abortController.signal,
|
|
393
|
-
memory: combinedMemory, // Pass combined memory content
|
|
394
394
|
workdir: this.workdir, // Pass working directory
|
|
395
395
|
tools: toolsConfig, // Pass filtered tool configuration
|
|
396
396
|
model: model, // Use passed model
|
|
397
|
-
systemPrompt:
|
|
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
|
|
398
411
|
maxTokens: maxTokens, // Pass max tokens override
|
|
399
412
|
};
|
|
400
413
|
|
|
@@ -628,6 +641,8 @@ export class AIManager {
|
|
|
628
641
|
backgroundTaskManager: this.backgroundTaskManager,
|
|
629
642
|
workdir: this.workdir,
|
|
630
643
|
messageId: this.messageManager.getMessages().slice(-1)[0]?.id,
|
|
644
|
+
sessionId: this.messageManager.getSessionId(),
|
|
645
|
+
taskManager: this.taskManager,
|
|
631
646
|
};
|
|
632
647
|
|
|
633
648
|
// Execute tool
|
|
@@ -637,23 +652,6 @@ export class AIManager {
|
|
|
637
652
|
context,
|
|
638
653
|
);
|
|
639
654
|
|
|
640
|
-
// Check if the tool was backgrounded via Ctrl-B
|
|
641
|
-
// If it was backgrounded, we should abort the AI recursion
|
|
642
|
-
if (
|
|
643
|
-
toolResult.success &&
|
|
644
|
-
toolResult.content.includes(
|
|
645
|
-
"Command was manually backgrounded by user",
|
|
646
|
-
)
|
|
647
|
-
) {
|
|
648
|
-
this.logger?.info(
|
|
649
|
-
`Tool ${toolName} was backgrounded via Ctrl-B, aborting AI recursion`,
|
|
650
|
-
);
|
|
651
|
-
// Use abortAIMessage directly instead of abortRecursion to avoid double logging
|
|
652
|
-
// and ensure we don't trigger the "Request was aborted" error block
|
|
653
|
-
this.abortAIMessage();
|
|
654
|
-
return;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
655
|
// Update message state - tool execution completed
|
|
658
656
|
this.messageManager.updateToolBlock({
|
|
659
657
|
id: toolId,
|
|
@@ -666,6 +664,7 @@ export class AIManager {
|
|
|
666
664
|
stage: "end",
|
|
667
665
|
name: toolName,
|
|
668
666
|
shortResult: toolResult.shortResult,
|
|
667
|
+
isManuallyBackgrounded: toolResult.isManuallyBackgrounded,
|
|
669
668
|
});
|
|
670
669
|
|
|
671
670
|
// Execute PostToolUse hooks after successful tool completion
|
|
@@ -690,6 +689,7 @@ export class AIManager {
|
|
|
690
689
|
stage: "end",
|
|
691
690
|
name: toolName,
|
|
692
691
|
compactParams,
|
|
692
|
+
isManuallyBackgrounded: false,
|
|
693
693
|
});
|
|
694
694
|
}
|
|
695
695
|
},
|
|
@@ -721,7 +721,25 @@ export class AIManager {
|
|
|
721
721
|
const isCurrentlyAborted =
|
|
722
722
|
abortController.signal.aborted || toolAbortController.signal.aborted;
|
|
723
723
|
|
|
724
|
-
if
|
|
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) {
|
|
725
743
|
// Recursively call AI service, increment recursion depth, and pass same configuration
|
|
726
744
|
await this.sendAIMessage({
|
|
727
745
|
recursionDepth: recursionDepth + 1,
|
|
@@ -733,18 +751,9 @@ export class AIManager {
|
|
|
733
751
|
}
|
|
734
752
|
}
|
|
735
753
|
} catch (error) {
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
abortController.signal.aborted || toolAbortController.signal.aborted;
|
|
740
|
-
|
|
741
|
-
if (isCurrentlyAborted) {
|
|
742
|
-
this.logger?.info("AI message processing was aborted");
|
|
743
|
-
} else {
|
|
744
|
-
this.messageManager.addErrorBlock(
|
|
745
|
-
error instanceof Error ? error.message : "Unknown error occurred",
|
|
746
|
-
);
|
|
747
|
-
}
|
|
754
|
+
this.messageManager.addErrorBlock(
|
|
755
|
+
error instanceof Error ? error.message : "Unknown error occurred",
|
|
756
|
+
);
|
|
748
757
|
} finally {
|
|
749
758
|
// Only execute cleanup and hooks for the initial call
|
|
750
759
|
if (recursionDepth === 0) {
|