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.
- package/dist/agent.d.ts +42 -11
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +114 -115
- 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 +6 -3
- package/dist/constants/tools.d.ts.map +1 -1
- package/dist/constants/tools.js +6 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/managers/MemoryRuleManager.js +1 -1
- package/dist/managers/aiManager.d.ts +5 -3
- package/dist/managers/aiManager.d.ts.map +1 -1
- package/dist/managers/aiManager.js +57 -20
- package/dist/managers/backgroundBashManager.d.ts.map +1 -1
- package/dist/managers/backgroundBashManager.js +1 -0
- package/dist/managers/backgroundTaskManager.d.ts +35 -0
- package/dist/managers/backgroundTaskManager.d.ts.map +1 -0
- package/dist/managers/backgroundTaskManager.js +255 -0
- package/dist/managers/foregroundTaskManager.d.ts +9 -0
- package/dist/managers/foregroundTaskManager.d.ts.map +1 -0
- package/dist/managers/foregroundTaskManager.js +21 -0
- package/dist/managers/liveConfigManager.d.ts +1 -1
- package/dist/managers/lspManager.d.ts.map +1 -1
- package/dist/managers/lspManager.js +3 -1
- package/dist/managers/mcpManager.d.ts.map +1 -1
- package/dist/managers/messageManager.d.ts +26 -12
- package/dist/managers/messageManager.d.ts.map +1 -1
- package/dist/managers/messageManager.js +138 -64
- package/dist/managers/permissionManager.d.ts.map +1 -1
- package/dist/managers/permissionManager.js +26 -22
- 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/pluginManager.d.ts.map +1 -1
- package/dist/managers/pluginManager.js +3 -2
- package/dist/managers/slashCommandManager.d.ts +6 -0
- package/dist/managers/slashCommandManager.d.ts.map +1 -1
- package/dist/managers/slashCommandManager.js +8 -2
- package/dist/managers/subagentManager.d.ts +15 -2
- package/dist/managers/subagentManager.d.ts.map +1 -1
- package/dist/managers/subagentManager.js +153 -39
- package/dist/managers/toolManager.d.ts +18 -1
- package/dist/managers/toolManager.d.ts.map +1 -1
- package/dist/managers/toolManager.js +29 -5
- package/dist/services/GitService.d.ts.map +1 -1
- package/dist/services/GitService.js +6 -2
- package/dist/services/MarketplaceService.d.ts +2 -2
- package/dist/services/MarketplaceService.d.ts.map +1 -1
- package/dist/services/MarketplaceService.js +18 -11
- package/dist/services/MemoryRuleService.d.ts +1 -1
- package/dist/services/MemoryRuleService.d.ts.map +1 -1
- package/dist/services/MemoryRuleService.js +13 -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 +1 -60
- 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 +0 -8
- package/dist/tools/bashTool.d.ts.map +1 -1
- package/dist/tools/bashTool.js +48 -172
- package/dist/tools/editTool.d.ts.map +1 -1
- package/dist/tools/editTool.js +8 -6
- 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/multiEditTool.d.ts.map +1 -1
- package/dist/tools/multiEditTool.js +7 -6
- 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 +3 -0
- package/dist/tools/taskOutputTool.d.ts.map +1 -0
- package/dist/tools/taskOutputTool.js +173 -0
- package/dist/tools/taskStopTool.d.ts +3 -0
- package/dist/tools/taskStopTool.d.ts.map +1 -0
- package/dist/tools/taskStopTool.js +71 -0
- package/dist/tools/taskTool.d.ts.map +1 -1
- package/dist/tools/taskTool.js +110 -63
- package/dist/tools/types.d.ts +12 -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/marketplace.d.ts +1 -0
- package/dist/types/marketplace.d.ts.map +1 -1
- package/dist/types/messaging.d.ts +3 -8
- package/dist/types/messaging.d.ts.map +1 -1
- package/dist/types/processes.d.ts +29 -4
- 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/editUtils.d.ts +2 -11
- package/dist/utils/editUtils.d.ts.map +1 -1
- package/dist/utils/editUtils.js +52 -79
- package/dist/utils/messageOperations.d.ts +5 -36
- package/dist/utils/messageOperations.d.ts.map +1 -1
- package/dist/utils/messageOperations.js +9 -98
- 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 +5 -5
- package/src/agent.ts +157 -134
- package/src/constants/prompts.ts +156 -65
- package/src/constants/tools.ts +6 -3
- package/src/index.ts +1 -0
- package/src/managers/MemoryRuleManager.ts +1 -1
- package/src/managers/aiManager.ts +77 -35
- package/src/managers/backgroundBashManager.ts +1 -0
- package/src/managers/backgroundTaskManager.ts +305 -0
- package/src/managers/foregroundTaskManager.ts +27 -0
- package/src/managers/lspManager.ts +3 -1
- package/src/managers/mcpManager.ts +6 -3
- package/src/managers/messageManager.ts +185 -75
- package/src/managers/permissionManager.ts +33 -28
- package/src/managers/planManager.ts +2 -2
- package/src/managers/pluginManager.ts +4 -3
- package/src/managers/slashCommandManager.ts +15 -2
- package/src/managers/subagentManager.ts +194 -35
- package/src/managers/toolManager.ts +48 -6
- package/src/services/GitService.ts +6 -2
- package/src/services/MarketplaceService.ts +30 -12
- package/src/services/MemoryRuleService.ts +18 -6
- package/src/services/aiService.ts +3 -145
- package/src/services/memory.ts +1 -73
- 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 +63 -196
- package/src/tools/editTool.ts +9 -18
- 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/multiEditTool.ts +7 -18
- package/src/tools/readTool.ts +17 -1
- package/src/tools/taskManagementTools.ts +498 -0
- package/src/tools/taskOutputTool.ts +196 -0
- package/src/tools/taskStopTool.ts +78 -0
- package/src/tools/taskTool.ts +136 -74
- package/src/tools/types.ts +13 -0
- package/src/tools/writeTool.ts +9 -2
- package/src/types/index.ts +1 -0
- package/src/types/marketplace.ts +1 -0
- package/src/types/messaging.ts +2 -9
- package/src/types/processes.ts +39 -4
- 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/editUtils.ts +65 -103
- package/src/utils/messageOperations.ts +12 -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
|
@@ -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
|
|
4
|
-
export const
|
|
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
|
|
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
|
|
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 {
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
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
|
|
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.
|
|
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
|
|
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
|
|
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
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
-
|
|
356
|
-
|
|
357
|
-
|
|
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:
|
|
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
|
-
|
|
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
|
|
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,
|