wave-agent-sdk 0.13.6 → 0.14.0

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 (54) hide show
  1. package/dist/agent.d.ts.map +1 -1
  2. package/dist/agent.js +4 -2
  3. package/dist/managers/aiManager.d.ts +3 -0
  4. package/dist/managers/aiManager.d.ts.map +1 -1
  5. package/dist/managers/aiManager.js +93 -8
  6. package/dist/managers/messageManager.d.ts +15 -0
  7. package/dist/managers/messageManager.d.ts.map +1 -1
  8. package/dist/managers/messageManager.js +52 -2
  9. package/dist/managers/permissionManager.d.ts +4 -0
  10. package/dist/managers/permissionManager.d.ts.map +1 -1
  11. package/dist/managers/permissionManager.js +6 -0
  12. package/dist/managers/subagentManager.d.ts.map +1 -1
  13. package/dist/managers/subagentManager.js +23 -17
  14. package/dist/prompts/index.d.ts +2 -1
  15. package/dist/prompts/index.d.ts.map +1 -1
  16. package/dist/prompts/index.js +50 -25
  17. package/dist/services/aiService.d.ts.map +1 -1
  18. package/dist/services/aiService.js +11 -1
  19. package/dist/tools/agentTool.d.ts.map +1 -1
  20. package/dist/tools/agentTool.js +14 -2
  21. package/dist/tools/bashTool.d.ts.map +1 -1
  22. package/dist/tools/bashTool.js +27 -5
  23. package/dist/tools/types.d.ts +1 -0
  24. package/dist/tools/types.d.ts.map +1 -1
  25. package/dist/tools/webFetchTool.d.ts.map +1 -1
  26. package/dist/tools/webFetchTool.js +202 -78
  27. package/dist/types/messaging.d.ts +1 -0
  28. package/dist/types/messaging.d.ts.map +1 -1
  29. package/dist/utils/convertMessagesForAPI.js +1 -1
  30. package/dist/utils/groupMessagesByApiRound.d.ts +24 -0
  31. package/dist/utils/groupMessagesByApiRound.d.ts.map +1 -0
  32. package/dist/utils/groupMessagesByApiRound.js +97 -0
  33. package/dist/utils/messageOperations.d.ts +1 -0
  34. package/dist/utils/messageOperations.d.ts.map +1 -1
  35. package/dist/utils/microcompact.d.ts +7 -0
  36. package/dist/utils/microcompact.d.ts.map +1 -0
  37. package/dist/utils/microcompact.js +78 -0
  38. package/package.json +2 -1
  39. package/src/agent.ts +4 -2
  40. package/src/managers/aiManager.ts +117 -15
  41. package/src/managers/messageManager.ts +64 -2
  42. package/src/managers/permissionManager.ts +7 -0
  43. package/src/managers/subagentManager.ts +28 -24
  44. package/src/prompts/index.ts +51 -25
  45. package/src/services/aiService.ts +14 -1
  46. package/src/tools/agentTool.ts +14 -2
  47. package/src/tools/bashTool.ts +27 -5
  48. package/src/tools/types.ts +1 -0
  49. package/src/tools/webFetchTool.ts +276 -86
  50. package/src/types/messaging.ts +1 -0
  51. package/src/utils/convertMessagesForAPI.ts +1 -1
  52. package/src/utils/groupMessagesByApiRound.ts +120 -0
  53. package/src/utils/messageOperations.ts +1 -0
  54. package/src/utils/microcompact.ts +101 -0
@@ -567,23 +567,25 @@ export class SubagentManager {
567
567
  instance.logStream?.end();
568
568
  const task = backgroundTaskManager.getTask(instance.backgroundTaskId);
569
569
  if (task) {
570
+ const wasAlreadyKilled = task.status === "killed";
570
571
  task.status = "completed";
571
572
  task.stdout = response || "Agent completed with no text response";
572
573
  task.endTime = Date.now();
573
574
  if (task.startTime) {
574
575
  task.runtime = task.endTime - task.startTime;
575
576
  }
576
- }
577
-
578
- // Enqueue completion notification
579
- const notificationQueue = this.container.has("NotificationQueue")
580
- ? this.container.get<NotificationQueue>("NotificationQueue")
581
- : undefined;
582
- if (notificationQueue) {
583
- const summary = `Agent task "${instance.description}" completed`;
584
- notificationQueue.enqueue(
585
- `<task-notification>\n<task-id>${instance.backgroundTaskId}</task-id>\n<task-type>agent</task-type>\n<status>completed</status>\n<summary>${summary}</summary>\n</task-notification>`,
586
- );
577
+ // Skip notification if task was already stopped (e.g. by main agent shutdown)
578
+ if (!wasAlreadyKilled) {
579
+ const notificationQueue = this.container.has("NotificationQueue")
580
+ ? this.container.get<NotificationQueue>("NotificationQueue")
581
+ : undefined;
582
+ if (notificationQueue) {
583
+ const summary = `Agent task "${instance.description}" completed`;
584
+ notificationQueue.enqueue(
585
+ `<task-notification>\n<task-id>${instance.backgroundTaskId}</task-id>\n<task-type>agent</task-type>\n<status>completed</status>\n<summary>${summary}</summary>\n</task-notification>`,
586
+ );
587
+ }
588
+ }
587
589
  }
588
590
  }
589
591
 
@@ -602,25 +604,27 @@ export class SubagentManager {
602
604
  instance.logStream?.end();
603
605
  const task = backgroundTaskManager.getTask(instance.backgroundTaskId);
604
606
  if (task) {
607
+ const wasAlreadyKilled = task.status === "killed";
605
608
  task.status = "failed";
606
609
  task.stderr = error instanceof Error ? error.message : String(error);
607
610
  task.endTime = Date.now();
608
611
  if (task.startTime) {
609
612
  task.runtime = task.endTime - task.startTime;
610
613
  }
611
- }
612
-
613
- // Enqueue error notification
614
- const notificationQueue = this.container.has("NotificationQueue")
615
- ? this.container.get<NotificationQueue>("NotificationQueue")
616
- : undefined;
617
- if (notificationQueue) {
618
- const errorMsg =
619
- error instanceof Error ? error.message : String(error);
620
- const summary = `Agent task "${instance.description}" failed: ${errorMsg}`;
621
- notificationQueue.enqueue(
622
- `<task-notification>\n<task-id>${instance.backgroundTaskId}</task-id>\n<task-type>agent</task-type>\n<status>failed</status>\n<summary>${summary}</summary>\n</task-notification>`,
623
- );
614
+ // Skip notification if task was already stopped (e.g. by main agent shutdown)
615
+ if (!wasAlreadyKilled) {
616
+ const notificationQueue = this.container.has("NotificationQueue")
617
+ ? this.container.get<NotificationQueue>("NotificationQueue")
618
+ : undefined;
619
+ if (notificationQueue) {
620
+ const errorMsg =
621
+ error instanceof Error ? error.message : String(error);
622
+ const summary = `Agent task "${instance.description}" failed: ${errorMsg}`;
623
+ notificationQueue.enqueue(
624
+ `<task-notification>\n<task-id>${instance.backgroundTaskId}</task-id>\n<task-type>agent</task-type>\n<status>failed</status>\n<summary>${summary}</summary>\n</task-notification>`,
625
+ );
626
+ }
627
+ }
624
628
  }
625
629
  }
626
630
  throw error;
@@ -179,28 +179,52 @@ NOTE: At any point in time through this workflow you should feel free to ask the
179
179
 
180
180
  export const DEFAULT_SYSTEM_PROMPT = BASE_SYSTEM_PROMPT;
181
181
 
182
- 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:
183
- 1. Task Overview
184
- The user's core request and success criteria
185
- Any clarifications or constraints they specified
186
- 2. Current State
187
- What has been completed so far
188
- Files created, modified, or analyzed (with paths if relevant)
189
- Key outputs or artifacts produced
190
- 3. Important Discoveries
191
- Technical constraints or requirements uncovered
192
- Decisions made and their rationale
193
- Errors encountered and how they were resolved
194
- What approaches were tried that didn't work (and why)
195
- 4. Next Steps
196
- Specific actions needed to complete the task
197
- Any blockers or open questions to resolve
198
- Priority order if multiple steps remain
199
- 5. Context to Preserve
200
- User preferences or style requirements
201
- Domain-specific details that aren't obvious
202
- Any promises made to the user
203
- 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
+ export const COMPRESS_MESSAGES_SYSTEM_PROMPT = `You are continuing work on a software engineering task. Write a detailed 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.
183
+
184
+ First, write your analysis in <analysis> tags as a thinking scratchpad:
185
+ - Chronologically review the conversation
186
+ - Identify user intents and goals
187
+ - Note files read/modified, approaches tried, decisions made
188
+ - Check for accuracy and completeness ensure nothing critical is missing
189
+
190
+ Then produce a structured summary in <summary> tags with these sections:
191
+
192
+ ## Primary Request and Intent
193
+ - The user's core request and success criteria
194
+ - Clarifications, constraints, or scope changes
195
+
196
+ ## Key Technical Concepts
197
+ - Frameworks, libraries, patterns, architectural decisions
198
+
199
+ ## Files and Code Sections
200
+ - Files read, modified, created (with full paths)
201
+ - Critical code snippets (function signatures, bug fixes, key logic)
202
+ - Focus on recent messages — include full code for important sections
203
+
204
+ ## Errors and Fixes
205
+ - Errors encountered, root causes, how they were resolved
206
+ - Approaches tried that didn't work and why
207
+
208
+ ## Problem Solving
209
+ - Approach evolution, trade-offs considered, decisions made
210
+
211
+ ## All User Messages
212
+ - Complete list of all user messages (non-tool content)
213
+ - Preserve exact wording where load-bearing
214
+
215
+ ## Pending Tasks
216
+ - Outstanding work, TODOs, unresolved questions
217
+
218
+ ## Current Work
219
+ - What was being worked on at the time of summarization
220
+ - Exact state of in-progress changes
221
+
222
+ ## Optional Next Step
223
+ - Immediate next action needed
224
+ - Include verbatim quotes from recent conversation if relevant
225
+
226
+ Be concise but complete — include information that prevents duplicate work or repeated mistakes.
227
+ Respond with text only. Do NOT call any tools.
204
228
  Wrap your summary in <summary></summary> tags.`;
205
229
 
206
230
  export const WEB_CONTENT_SYSTEM_PROMPT = `You are a helpful assistant that extracts information from web content. The content is provided in Markdown format.`;
@@ -214,6 +238,7 @@ export function buildSystemPrompt(
214
238
  tools: ToolPlugin[],
215
239
  options: {
216
240
  workdir?: string;
241
+ originalWorkdir?: string;
217
242
  memory?: string;
218
243
  language?: string;
219
244
  isSubagent?: boolean;
@@ -251,8 +276,9 @@ export function buildSystemPrompt(
251
276
  prompt += `\n\n${buildPlanModePrompt(options.planMode.planFilePath, options.planMode.planExists, options.isSubagent)}`;
252
277
  }
253
278
 
254
- if (options.workdir) {
255
- const isGitRepo = isGitRepository(options.workdir);
279
+ const workdirForPrompt = options.originalWorkdir || options.workdir;
280
+ if (workdirForPrompt) {
281
+ const isGitRepo = isGitRepository(workdirForPrompt);
256
282
  const platform = os.platform();
257
283
  const osVersion = `${os.type()} ${os.release()}`;
258
284
  const today = new Date().toISOString().split("T")[0];
@@ -267,7 +293,7 @@ export function buildSystemPrompt(
267
293
 
268
294
  Here is useful information about the environment you are running in:
269
295
  <env>
270
- Working directory: ${options.workdir}
296
+ Working directory: ${workdirForPrompt}
271
297
  Is directory a git repo: ${isGitRepo}
272
298
  Platform: ${platform}
273
299
  Shell: ${shellName}
@@ -784,6 +784,19 @@ export async function compressMessages(
784
784
  await acquireSlot(abortSignal);
785
785
  }
786
786
 
787
+ // Strip images from messages before compact API call to reduce token usage
788
+ const cleanedMessages = messages.map((msg) => {
789
+ // Handle user/assistant messages with array content
790
+ if (Array.isArray(msg.content)) {
791
+ const textParts = msg.content.filter(
792
+ (part) => part.type === "text",
793
+ ) as import("openai/resources.js").ChatCompletionContentPartText[];
794
+ const text = textParts.map((p) => p.text).join("\n");
795
+ return { ...msg, content: text || "(empty message)" };
796
+ }
797
+ return msg;
798
+ });
799
+
787
800
  // Create OpenAI client with injected configuration
788
801
  const openai = new OpenAIClient({
789
802
  apiKey: gatewayConfig.apiKey,
@@ -821,7 +834,7 @@ export async function compressMessages(
821
834
  role: "system",
822
835
  content: COMPRESS_MESSAGES_SYSTEM_PROMPT,
823
836
  },
824
- ...messages,
837
+ ...cleanedMessages,
825
838
  {
826
839
  role: "user",
827
840
  content: `Please create a detailed summary of the conversation so far.`,
@@ -71,7 +71,11 @@ When using the Agent tool, you must specify a subagent_type parameter to select
71
71
 
72
72
  - When doing file search, prefer to use the ${AGENT_TOOL_NAME} tool in order to reduce context usage.
73
73
  - You should proactively use the ${AGENT_TOOL_NAME} tool with specialized agents when the task at hand matches the agent's description.
74
- - 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 ${AGENT_TOOL_NAME} tool with subagent_type=${EXPLORE_SUBAGENT_TYPE} instead of running search commands directly.`;
74
+ - 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 ${AGENT_TOOL_NAME} tool with subagent_type=${EXPLORE_SUBAGENT_TYPE} instead of running search commands directly.
75
+ - You can optionally run agents in the background using the run_in_background parameter. When an agent runs in the background, you will be automatically notified when it completes — do NOT sleep, poll, or proactively check on its progress. Continue with other work or respond to the user instead.
76
+ - **Foreground vs background**: Use foreground (default) when you need the agent's results before you can proceed — e.g., research agents whose findings inform your next steps. Use background when you have genuinely independent work to do in parallel.
77
+ - **Don't peek.** The tool result includes an output file path — do not Read or tail it unless the user explicitly asks for a progress check. You get a completion notification; trust it. Reading the transcript mid-flight pulls the agent's tool noise into your context, which defeats the point of backgrounding.
78
+ - **Don't race.** After launching, you know nothing about what the agent found. Never fabricate or predict agent results in any format — not as prose, summary, or structured output. The notification arrives as a user-role message in a later turn; it is never something you write yourself. If the user asks a follow-up before the notification lands, tell them the agent is still running — give status, not a guess.`;
75
79
  },
76
80
 
77
81
  execute: async (
@@ -212,9 +216,17 @@ When using the Agent tool, you must specify a subagent_type parameter to select
212
216
  if (run_in_background) {
213
217
  const task = context.backgroundTaskManager?.getTask(result);
214
218
  const outputPath = task?.outputPath;
219
+ const backgroundMsg = [
220
+ `Agent started in background with ID: ${result}.`,
221
+ `The agent is working in the background. You will be notified automatically when it completes.`,
222
+ `Do not duplicate this agent's work — avoid working with the same files or topics it is using.`,
223
+ outputPath
224
+ ? `output_file: ${outputPath}`
225
+ : `Briefly tell the user what you launched and end your response.`,
226
+ ].join("\n");
215
227
  resolve({
216
228
  success: true,
217
- content: `Agent started in background with ID: ${result}.${outputPath ? ` Real-time output: ${outputPath}` : ""}`,
229
+ content: backgroundMsg,
218
230
  shortResult: `Agent started in background: ${result}`,
219
231
  });
220
232
  return;
@@ -139,7 +139,10 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
139
139
  - Do not retry failing commands in a sleep loop — diagnose the root cause.
140
140
  - If waiting for a background task you started with \`run_in_background\`, you will be notified when it completes — do not poll.
141
141
  - If you must poll an external process, use a check command (e.g. \`gh run view\`) rather than sleeping first.
142
- - If you must sleep, keep the duration short (1-5 seconds) to avoid blocking the user.`,
142
+ - If you must sleep, keep the duration short (1-5 seconds) to avoid blocking the user.
143
+
144
+ # CWD management
145
+ Try to maintain your current working directory throughout the session by using absolute paths and avoiding usage of \`cd\`. You may use \`cd\` if the User explicitly requests it. When you use \`cd\`, the shell working directory will be reset to the original working directory after the command completes.`,
143
146
  execute: async (
144
147
  args: Record<string, unknown>,
145
148
  context: ToolContext,
@@ -221,9 +224,16 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
221
224
  const { id: taskId } = backgroundTaskManager.startShell(command, timeout);
222
225
  const task = backgroundTaskManager.getTask(taskId);
223
226
  const outputPath = task?.outputPath;
227
+ const backgroundMsg = [
228
+ `Command started in background with ID: ${taskId}.`,
229
+ `You will be notified automatically when it completes.`,
230
+ outputPath
231
+ ? `output_file: ${outputPath}`
232
+ : `Use ${READ_TOOL_NAME} tool with task_id="${taskId}" to read the output.`,
233
+ ].join("\n");
224
234
  return {
225
235
  success: true,
226
- content: `Command started in background with ID: ${taskId}.${outputPath ? ` Real-time output: ${outputPath}` : ` Use ${READ_TOOL_NAME} tool with task_id="${taskId}" to monitor output.`}`,
236
+ content: backgroundMsg,
227
237
  shortResult: `Background process ${taskId} started`,
228
238
  };
229
239
  }
@@ -446,9 +456,20 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
446
456
  }
447
457
  }
448
458
 
449
- // If CWD changed, call the onCwdChange callback
459
+ // If CWD changed, call the onCwdChange callback and add notification
460
+ let cwdChangedNotification = "";
450
461
  if (newCwd && newCwd !== context.workdir && context.onCwdChange) {
451
- context.onCwdChange(newCwd);
462
+ const isInSafeZone =
463
+ context.permissionManager?.isPathInSafeZone?.(newCwd) ?? true;
464
+
465
+ if (isInSafeZone) {
466
+ context.onCwdChange(newCwd);
467
+ } else if (context.originalWorkdir) {
468
+ context.onCwdChange(context.originalWorkdir);
469
+ cwdChangedNotification = `Shell cwd was reset to ${context.originalWorkdir}\n`;
470
+ } else {
471
+ context.onCwdChange(newCwd);
472
+ }
452
473
  }
453
474
 
454
475
  const exitCode = code ?? 0;
@@ -457,7 +478,8 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
457
478
 
458
479
  // Handle large output by truncation and persistence if needed
459
480
  const finalOutput =
460
- combinedOutput || `Command executed with exit code: ${exitCode}`;
481
+ cwdChangedNotification +
482
+ (combinedOutput || `Command executed with exit code: ${exitCode}`);
461
483
  const content = processOutput(finalOutput);
462
484
 
463
485
  const lines = combinedOutput.trim().split("\n");
@@ -58,6 +58,7 @@ export interface ToolContext {
58
58
  abortSignal?: AbortSignal;
59
59
  backgroundTaskManager?: import("../managers/backgroundTaskManager.js").BackgroundTaskManager;
60
60
  workdir: string;
61
+ originalWorkdir?: string;
61
62
  /** Permission mode for this tool execution */
62
63
  permissionMode?: PermissionMode;
63
64
  /** Custom permission callback */