wave-agent-sdk 0.16.10 → 0.16.12

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 (74) hide show
  1. package/dist/agent.d.ts +5 -0
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +15 -0
  4. package/dist/constants/toolLimits.d.ts +2 -0
  5. package/dist/constants/toolLimits.d.ts.map +1 -1
  6. package/dist/constants/toolLimits.js +2 -0
  7. package/dist/managers/aiManager.d.ts +5 -0
  8. package/dist/managers/aiManager.d.ts.map +1 -1
  9. package/dist/managers/aiManager.js +21 -0
  10. package/dist/managers/hookManager.d.ts +6 -3
  11. package/dist/managers/hookManager.d.ts.map +1 -1
  12. package/dist/managers/hookManager.js +36 -13
  13. package/dist/managers/mcpManager.d.ts +4 -28
  14. package/dist/managers/mcpManager.d.ts.map +1 -1
  15. package/dist/managers/mcpManager.js +10 -127
  16. package/dist/services/authService.d.ts +33 -1
  17. package/dist/services/authService.d.ts.map +1 -1
  18. package/dist/services/authService.js +212 -11
  19. package/dist/services/configurationService.d.ts.map +1 -1
  20. package/dist/services/configurationService.js +4 -1
  21. package/dist/services/hook.d.ts +4 -0
  22. package/dist/services/hook.d.ts.map +1 -1
  23. package/dist/services/hook.js +10 -0
  24. package/dist/services/interactionService.d.ts.map +1 -1
  25. package/dist/services/interactionService.js +0 -12
  26. package/dist/services/remoteSettingsService.d.ts.map +1 -1
  27. package/dist/services/remoteSettingsService.js +3 -2
  28. package/dist/tools/bashTool.d.ts.map +1 -1
  29. package/dist/tools/bashTool.js +58 -32
  30. package/dist/tools/types.d.ts +4 -0
  31. package/dist/tools/types.d.ts.map +1 -1
  32. package/dist/types/agent.d.ts +7 -0
  33. package/dist/types/agent.d.ts.map +1 -1
  34. package/dist/types/auth.d.ts +12 -0
  35. package/dist/types/auth.d.ts.map +1 -1
  36. package/dist/types/hooks.d.ts +5 -1
  37. package/dist/types/hooks.d.ts.map +1 -1
  38. package/dist/types/hooks.js +1 -0
  39. package/dist/types/mcp.d.ts +1 -1
  40. package/dist/types/mcp.d.ts.map +1 -1
  41. package/dist/utils/containerSetup.d.ts.map +1 -1
  42. package/dist/utils/containerSetup.js +3 -12
  43. package/dist/utils/gitUtils.d.ts +18 -1
  44. package/dist/utils/gitUtils.d.ts.map +1 -1
  45. package/dist/utils/gitUtils.js +120 -49
  46. package/dist/utils/mcpUtils.d.ts.map +1 -1
  47. package/dist/utils/mcpUtils.js +6 -1
  48. package/dist/utils/toolResultStorage.d.ts +46 -0
  49. package/dist/utils/toolResultStorage.d.ts.map +1 -0
  50. package/dist/utils/toolResultStorage.js +90 -0
  51. package/dist/utils/worktreeUtils.d.ts.map +1 -1
  52. package/dist/utils/worktreeUtils.js +58 -0
  53. package/package.json +3 -3
  54. package/src/agent.ts +17 -0
  55. package/src/constants/toolLimits.ts +3 -0
  56. package/src/managers/aiManager.ts +37 -0
  57. package/src/managers/hookManager.ts +42 -17
  58. package/src/managers/mcpManager.ts +10 -178
  59. package/src/services/authService.ts +243 -16
  60. package/src/services/configurationService.ts +6 -1
  61. package/src/services/hook.ts +15 -0
  62. package/src/services/interactionService.ts +0 -18
  63. package/src/services/remoteSettingsService.ts +3 -2
  64. package/src/tools/bashTool.ts +70 -38
  65. package/src/tools/types.ts +4 -0
  66. package/src/types/agent.ts +7 -0
  67. package/src/types/auth.ts +10 -0
  68. package/src/types/hooks.ts +7 -1
  69. package/src/types/mcp.ts +1 -1
  70. package/src/utils/containerSetup.ts +3 -13
  71. package/src/utils/gitUtils.ts +123 -48
  72. package/src/utils/mcpUtils.ts +12 -1
  73. package/src/utils/toolResultStorage.ts +117 -0
  74. package/src/utils/worktreeUtils.ts +63 -0
@@ -12,6 +12,7 @@ import {
12
12
  chmodSync,
13
13
  rmSync,
14
14
  mkdirSync,
15
+ statSync,
15
16
  } from "fs";
16
17
  import * as path from "path";
17
18
  import * as os from "os";
@@ -20,7 +21,8 @@ import { createServer, Server } from "http";
20
21
  import { URL } from "url";
21
22
  import { execFile } from "child_process";
22
23
  import { promisify } from "util";
23
- import type { AuthConfig, AuthUser } from "../types/auth.js";
24
+ import type { AuthConfig, AuthUser, TokenResponse } from "../types/auth.js";
25
+ import { logger } from "../utils/globalLogger.js";
24
26
 
25
27
  /** Persistent anonymous ID for telemetry fallback when SSO is not authenticated. */
26
28
  let _anonymousId: string | undefined;
@@ -32,6 +34,9 @@ export class AuthService {
32
34
  private _serverUrl: string | undefined;
33
35
  private onAuthChangeCallbacks: Array<(event: "login" | "logout") => void> =
34
36
  [];
37
+ private _refreshPromise: Promise<boolean> | null = null;
38
+ private _authFileMtime: number = 0;
39
+ private static readonly REFRESH_BUFFER_MS = 5 * 60 * 1000;
35
40
 
36
41
  static getInstance(): AuthService {
37
42
  if (!AuthService.instance) {
@@ -83,6 +88,12 @@ export class AuthService {
83
88
  }
84
89
  try {
85
90
  const content = readFileSync(authPath, "utf-8");
91
+ // Best-effort mtime tracking for multi-process detection
92
+ try {
93
+ this._authFileMtime = statSync(authPath).mtimeMs;
94
+ } catch {
95
+ // ignore stat errors
96
+ }
86
97
  return JSON.parse(content) as AuthConfig;
87
98
  } catch {
88
99
  return {};
@@ -97,11 +108,19 @@ export class AuthService {
97
108
  }
98
109
  writeFileSync(authPath, JSON.stringify(config, null, 2), "utf-8");
99
110
  chmodSync(authPath, 0o600);
111
+ // Update mtime after write
112
+ try {
113
+ this._authFileMtime = statSync(authPath).mtimeMs;
114
+ } catch {
115
+ // ignore stat errors
116
+ }
100
117
  }
101
118
 
102
119
  clearAuth(): void {
103
120
  const config = this.loadAuth();
104
121
  delete config.SSO_TOKEN;
122
+ delete config.SSO_REFRESH_TOKEN;
123
+ delete config.SSO_TOKEN_EXPIRES_AT;
105
124
  if (Object.keys(config).length === 0) {
106
125
  const authPath = this.getAuthPath();
107
126
  if (existsSync(authPath)) {
@@ -145,11 +164,22 @@ export class AuthService {
145
164
  });
146
165
 
147
166
  // Exchange authorization code for JWT (includes user info)
148
- const { token, user } = await this.exchangeCode(serverUrl, code);
167
+ const { token, refreshToken, expiresIn, user } = await this.exchangeCode(
168
+ serverUrl,
169
+ code,
170
+ );
149
171
 
150
172
  // Save the token and user info (preserve existing keys)
151
173
  const existing = this.loadAuth();
152
- this.saveAuth({ ...existing, SSO_TOKEN: token, user });
174
+ this.saveAuth({
175
+ ...existing,
176
+ SSO_TOKEN: token,
177
+ SSO_REFRESH_TOKEN: refreshToken,
178
+ SSO_TOKEN_EXPIRES_AT: expiresIn
179
+ ? Date.now() + expiresIn * 1000
180
+ : undefined,
181
+ user,
182
+ });
153
183
 
154
184
  this.notifyAuthChange("login");
155
185
 
@@ -158,32 +188,31 @@ export class AuthService {
158
188
 
159
189
  /**
160
190
  * Exchange a short-lived authorization code for a JWT token.
161
- * Returns both the token and user info.
191
+ * Returns token, optional refresh token, optional expiresIn, and user info.
162
192
  */
163
193
  private async exchangeCode(
164
194
  serverUrl: string,
165
195
  code: string,
166
- ): Promise<{ token: string; user: AuthUser }> {
167
- const exchangeUrl = `${serverUrl}/api/auth/exchange`;
196
+ ): Promise<TokenResponse> {
197
+ const exchangeUrl = `${serverUrl}/api/auth/token`;
198
+ logger.info(`[Auth] Exchanging authorization code at ${exchangeUrl}`);
168
199
  const response = await fetch(exchangeUrl, {
169
200
  method: "POST",
170
201
  headers: { "Content-Type": "application/json" },
171
- body: JSON.stringify({ code }),
202
+ body: JSON.stringify({ grant_type: "authorization_code", code }),
172
203
  });
173
204
 
174
205
  if (!response.ok) {
175
206
  const text = await response.text();
207
+ logger.info(
208
+ `[Auth] Authorization code exchange failed (${response.status}): ${text}`,
209
+ );
176
210
  throw new Error(`Token exchange failed (${response.status}): ${text}`);
177
211
  }
178
212
 
179
- const data = (await response.json()) as {
180
- token: string;
181
- user: { id: string; email?: string };
182
- };
183
- return {
184
- token: data.token,
185
- user: { id: data.user.id, email: data.user.email },
186
- };
213
+ const data = (await response.json()) as TokenResponse;
214
+ logger.info("[Auth] Authorization code exchanged successfully");
215
+ return data;
187
216
  }
188
217
 
189
218
  private startLocalAuthServer(
@@ -306,7 +335,149 @@ export class AuthService {
306
335
  }
307
336
 
308
337
  isSSOAuthenticated(): boolean {
309
- return this.getSSOToken() !== undefined;
338
+ const config = this.loadAuth();
339
+ if (!config.SSO_TOKEN) return false;
340
+ if (
341
+ config.SSO_TOKEN_EXPIRES_AT &&
342
+ Date.now() >= config.SSO_TOKEN_EXPIRES_AT
343
+ )
344
+ return false;
345
+ return true;
346
+ }
347
+
348
+ /**
349
+ * Check if the current token is expired or within the refresh buffer.
350
+ * Returns false if no expiry info (backward compat — treated as never-expiring).
351
+ */
352
+ isTokenExpired(): boolean {
353
+ const config = this.loadAuth();
354
+ if (!config.SSO_TOKEN_EXPIRES_AT) return false;
355
+ const expiresAt = config.SSO_TOKEN_EXPIRES_AT;
356
+ const bufferMs = AuthService.REFRESH_BUFFER_MS;
357
+ const now = Date.now();
358
+ const remaining = expiresAt - now;
359
+ const expired = now >= expiresAt - bufferMs;
360
+ if (expired) {
361
+ logger.info(
362
+ `[Auth] Token expired or within refresh buffer: remaining=${Math.round(remaining / 1000)}s, buffer=${bufferMs / 1000}s`,
363
+ );
364
+ }
365
+ return expired;
366
+ }
367
+
368
+ /**
369
+ * Check if the token needs refresh and refresh it if possible.
370
+ * Deduplicates concurrent refresh calls (401 dedup).
371
+ */
372
+ async checkAndRefreshTokenIfNeeded(): Promise<boolean> {
373
+ if (!this.isTokenExpired()) return true;
374
+ // Dedup: if a refresh is already in-flight, reuse the same promise
375
+ if (this._refreshPromise) {
376
+ logger.info(
377
+ "[Auth] Token refresh already in-flight, reusing existing promise",
378
+ );
379
+ return this._refreshPromise;
380
+ }
381
+ logger.info("[Auth] Starting token refresh");
382
+ this._refreshPromise = this.refreshToken();
383
+ try {
384
+ return await this._refreshPromise;
385
+ } finally {
386
+ this._refreshPromise = null;
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Refresh the access token using the stored refresh token.
392
+ * Returns true on success, false on failure.
393
+ */
394
+ private async refreshToken(): Promise<boolean> {
395
+ const config = this.loadAuth();
396
+ if (!config.SSO_REFRESH_TOKEN) {
397
+ logger.info("[Auth] No refresh token available, cannot refresh");
398
+ return false;
399
+ }
400
+
401
+ const serverUrl = this.getServerUrl();
402
+ try {
403
+ logger.info(`[Auth] Refreshing token via ${serverUrl}/api/auth/token`);
404
+ const response = await fetch(`${serverUrl}/api/auth/token`, {
405
+ method: "POST",
406
+ headers: { "Content-Type": "application/json" },
407
+ body: JSON.stringify({
408
+ grant_type: "refresh_token",
409
+ refresh_token: config.SSO_REFRESH_TOKEN,
410
+ }),
411
+ });
412
+
413
+ if (response.status === 400 || response.status === 401) {
414
+ // Refresh token revoked — clear auth
415
+ logger.info(
416
+ `[Auth] Refresh token rejected (${response.status}), clearing auth`,
417
+ );
418
+ this.clearAuth();
419
+ return false;
420
+ }
421
+
422
+ if (!response.ok) {
423
+ logger.info(
424
+ `[Auth] Token refresh failed with status ${response.status}`,
425
+ );
426
+ return false;
427
+ }
428
+
429
+ const data = (await response.json()) as TokenResponse;
430
+ const newExpiresAt = data.expiresIn
431
+ ? Date.now() + data.expiresIn * 1000
432
+ : undefined;
433
+ this.saveAuth({
434
+ ...config,
435
+ SSO_TOKEN: data.token,
436
+ SSO_REFRESH_TOKEN: data.refreshToken ?? config.SSO_REFRESH_TOKEN,
437
+ SSO_TOKEN_EXPIRES_AT: newExpiresAt,
438
+ user: data.user
439
+ ? { id: data.user.id, email: data.user.email }
440
+ : config.user,
441
+ });
442
+ logger.info(
443
+ `[Auth] Token refreshed successfully, new token expires at ${newExpiresAt ? new Date(newExpiresAt).toISOString() : "never"}`,
444
+ );
445
+ this.notifyAuthChange("login");
446
+ return true;
447
+ } catch (err) {
448
+ // Network error — don't clear auth (might be transient)
449
+ logger.info(`[Auth] Token refresh failed with network error: ${err}`);
450
+ return false;
451
+ }
452
+ }
453
+
454
+ /**
455
+ * Check if another process has refreshed the token on disk.
456
+ * Returns true if a fresh token was found and loaded.
457
+ */
458
+ /** @internal Check if another process has refreshed the token on disk */
459
+ tryReadRefreshedTokenFromDisk(): boolean {
460
+ try {
461
+ const authPath = this.getAuthPath();
462
+ if (!existsSync(authPath)) return false;
463
+ const stat = statSync(authPath);
464
+ if (stat.mtimeMs <= this._authFileMtime) return false;
465
+ // File was modified by another process — check if token is fresh
466
+ const config = this.loadAuth();
467
+ if (
468
+ config.SSO_TOKEN_EXPIRES_AT &&
469
+ Date.now() < config.SSO_TOKEN_EXPIRES_AT
470
+ ) {
471
+ logger.info(
472
+ `[Auth] Detected token refreshed by another process (auth.json mtime changed)`,
473
+ );
474
+ this._authFileMtime = stat.mtimeMs;
475
+ return true;
476
+ }
477
+ return false;
478
+ } catch {
479
+ return false;
480
+ }
310
481
  }
311
482
 
312
483
  getAuthUser(): AuthUser | undefined {
@@ -317,6 +488,62 @@ export class AuthService {
317
488
 
318
489
  export const authService = AuthService.getInstance();
319
490
 
491
+ /**
492
+ * Create a fetch wrapper that handles SSO token refresh transparently.
493
+ *
494
+ * 1. Proactive refresh: calls checkAndRefreshTokenIfNeeded() before each request
495
+ * 2. Updates Authorization header with fresh token
496
+ * 3. Reactive 401/403 recovery: tries disk refresh then force refresh, retries once
497
+ */
498
+ export function createAuthAwareFetch(innerFetch: typeof fetch): typeof fetch {
499
+ return async (input: RequestInfo | URL, init?: RequestInit) => {
500
+ // Proactive refresh
501
+ await authService.checkAndRefreshTokenIfNeeded();
502
+
503
+ // Update Authorization header with fresh token
504
+ const freshToken = authService.getSSOToken();
505
+ const headers = new Headers(init?.headers);
506
+ if (freshToken) {
507
+ headers.set("Authorization", `Bearer ${freshToken}`);
508
+ }
509
+ const modifiedInit = { ...init, headers };
510
+
511
+ const response = await innerFetch(input, modifiedInit);
512
+
513
+ // Reactive 401/403 recovery (single retry)
514
+ if (response.status === 401 || response.status === 403) {
515
+ logger.info(
516
+ `[Auth] Received ${response.status}, attempting token recovery`,
517
+ );
518
+ // Try disk refresh first (another process may have refreshed)
519
+ if (authService.tryReadRefreshedTokenFromDisk()) {
520
+ const retryToken = authService.getSSOToken();
521
+ const retryHeaders = new Headers(init?.headers);
522
+ if (retryToken) {
523
+ retryHeaders.set("Authorization", `Bearer ${retryToken}`);
524
+ }
525
+ logger.info("[Auth] Retrying request with disk-refreshed token");
526
+ return innerFetch(input, { ...init, headers: retryHeaders });
527
+ }
528
+
529
+ // Try force refresh
530
+ if (await authService.checkAndRefreshTokenIfNeeded()) {
531
+ const retryToken = authService.getSSOToken();
532
+ if (retryToken) {
533
+ const retryHeaders = new Headers(init?.headers);
534
+ retryHeaders.set("Authorization", `Bearer ${retryToken}`);
535
+ logger.info("[Auth] Retrying request with force-refreshed token");
536
+ return innerFetch(input, { ...init, headers: retryHeaders });
537
+ }
538
+ }
539
+
540
+ logger.info("[Auth] Token recovery failed, returning original response");
541
+ }
542
+
543
+ return response;
544
+ };
545
+ }
546
+
320
547
  /**
321
548
  * Get or create a persistent anonymous ID for telemetry.
322
549
  *
@@ -48,6 +48,7 @@ import {
48
48
  getRemoteSettingsSync,
49
49
  mergeRemoteSettings,
50
50
  } from "./remoteSettingsService.js";
51
+ import { createAuthAwareFetch } from "./authService.js";
51
52
 
52
53
  /**
53
54
  * Default ConfigurationService implementation
@@ -423,6 +424,10 @@ export class ConfigurationService {
423
424
  const ssoToken = this.readSSOToken();
424
425
  const serverUrl = this.options.serverUrl || process.env.WAVE_SERVER_URL;
425
426
  if (ssoToken && serverUrl) {
427
+ const baseFetch = fetch ?? this.options.fetch ?? globalThis.fetch;
428
+ const authAwareFetch = createAuthAwareFetch(
429
+ baseFetch as typeof globalThis.fetch,
430
+ ) as ClientOptions["fetch"];
426
431
  return {
427
432
  apiKey: ssoToken,
428
433
  baseURL: `${serverUrl}/api/v1`,
@@ -431,7 +436,7 @@ export class ConfigurationService {
431
436
  ? defaultHeaders
432
437
  : undefined,
433
438
  fetchOptions: fetchOptions ?? this.options.fetchOptions,
434
- fetch: fetch ?? this.options.fetch,
439
+ fetch: authAwareFetch,
435
440
  };
436
441
  }
437
442
 
@@ -277,6 +277,21 @@ export async function executeCommands(
277
277
  return results;
278
278
  }
279
279
 
280
+ /**
281
+ * Execute a CwdChanged hook
282
+ */
283
+ export async function executeCwdChangedHooks(
284
+ oldCwd: string,
285
+ newCwd: string,
286
+ context: ExtendedHookExecutionContext,
287
+ ): Promise<HookExecutionResult[]> {
288
+ // CwdChanged hooks are executed through HookManager.executeCwdChangedHooks()
289
+ void context;
290
+ void oldCwd;
291
+ void newCwd;
292
+ return [];
293
+ }
294
+
280
295
  /**
281
296
  * Validate command safety (basic checks)
282
297
  */
@@ -7,7 +7,6 @@ import type { ConfigurationService } from "./configurationService.js";
7
7
  import type { AIManager } from "../managers/aiManager.js";
8
8
  import type { SubagentManager } from "../managers/subagentManager.js";
9
9
  import type { TaskManager } from "./taskManager.js";
10
- import type { NotificationQueue } from "../managers/notificationQueue.js";
11
10
 
12
11
  export interface InteractionContext {
13
12
  messageManager: MessageManager;
@@ -62,23 +61,6 @@ export class InteractionService {
62
61
  // Don't add to history, let normal message processing logic below handle it
63
62
  }
64
63
 
65
- // Inject pending notifications from background tasks
66
- const notificationQueue = context.aiManager["container"].has(
67
- "NotificationQueue",
68
- )
69
- ? context.aiManager["container"].get<NotificationQueue>(
70
- "NotificationQueue",
71
- )
72
- : undefined;
73
- if (notificationQueue && notificationQueue.hasPending()) {
74
- const notifications = notificationQueue.dequeueAll();
75
- for (const notification of notifications) {
76
- messageManager.addUserMessage({
77
- content: notification,
78
- });
79
- }
80
- }
81
-
82
64
  // Handle normal AI message
83
65
  // Add user message first, will automatically sync to UI
84
66
  messageManager.addUserMessage({
@@ -2,7 +2,7 @@ import * as fs from "node:fs";
2
2
  import { homedir } from "node:os";
3
3
  import * as path from "node:path";
4
4
 
5
- import { authService } from "./authService.js";
5
+ import { authService, createAuthAwareFetch } from "./authService.js";
6
6
  import type {
7
7
  RemoteSettingsCache,
8
8
  RemoteSettingsFetchResult,
@@ -85,7 +85,8 @@ async function fetchRemoteSettings(): Promise<RemoteSettingsFetchResult> {
85
85
  }
86
86
 
87
87
  try {
88
- const response = await fetch(`${serverUrl}/api/wave/settings`, {
88
+ const authFetch = createAuthAwareFetch(globalThis.fetch);
89
+ const response = await authFetch(`${serverUrl}/api/wave/settings`, {
89
90
  method: "GET",
90
91
  headers,
91
92
  signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
@@ -4,6 +4,8 @@ import * as path from "path";
4
4
  import * as os from "os";
5
5
  import { logger } from "../utils/globalLogger.js";
6
6
  import { stripAnsiColors } from "../utils/stringUtils.js";
7
+ import { processToolResult } from "../utils/toolResultStorage.js";
8
+ import { BASH_MAX_OUTPUT_CHARS } from "../constants/toolLimits.js";
7
9
  import type { ToolPlugin, ToolResult, ToolContext } from "./types.js";
8
10
  import {
9
11
  BASH_TOOL_NAME,
@@ -14,36 +16,8 @@ import {
14
16
  WRITE_TOOL_NAME,
15
17
  } from "../constants/tools.js";
16
18
 
17
- const MAX_OUTPUT_LENGTH = 30000;
18
19
  const BASH_DEFAULT_TIMEOUT_MS = 120000;
19
20
 
20
- /**
21
- * Helper function to handle large output by truncation and persistence to a temporary file.
22
- */
23
- function processOutput(output: string): string {
24
- if (output.length <= MAX_OUTPUT_LENGTH) {
25
- return output;
26
- }
27
-
28
- try {
29
- const tempDir = os.tmpdir();
30
- const fileName = `bash_output_${Date.now()}_${Math.random().toString(36).substring(2, 11)}.txt`;
31
- const filePath = path.join(tempDir, fileName);
32
- fs.writeFileSync(filePath, output, "utf8");
33
-
34
- return (
35
- output.substring(0, MAX_OUTPUT_LENGTH) +
36
- `\n\n... (output truncated)\nFull output persisted to: ${filePath}`
37
- );
38
- } catch (error) {
39
- logger.error("Failed to persist large bash output:", error);
40
- return (
41
- output.substring(0, MAX_OUTPUT_LENGTH) +
42
- "\n\n... (output truncated, failed to persist full output)"
43
- );
44
- }
45
- }
46
-
47
21
  /**
48
22
  * Bash command execution tool - supports both foreground and background execution
49
23
  */
@@ -104,7 +78,7 @@ Usage notes:
104
78
  - The command argument is required.
105
79
  - You can specify an optional timeout in milliseconds (up to ${BASH_DEFAULT_TIMEOUT_MS}ms / ${BASH_DEFAULT_TIMEOUT_MS / 60000} minutes). If not specified, commands will timeout after ${BASH_DEFAULT_TIMEOUT_MS}ms (${BASH_DEFAULT_TIMEOUT_MS / 60000} minutes).
106
80
  - It is very helpful if you write a clear, concise description of what this command does in 5-10 words.
107
- - If the output exceeds ${MAX_OUTPUT_LENGTH} characters, output will be truncated and the full output will be persisted to a temporary file.
81
+ - If the output exceeds ${BASH_MAX_OUTPUT_CHARS.toLocaleString()} characters, output will be truncated and the full output will be persisted to a file you can read with the Read tool.
108
82
  - You can use the \`run_in_background\` parameter to run the command in the background, which allows you to continue working while the command runs. You can monitor the output using the ${READ_TOOL_NAME} tool as it becomes available. You do not need to use '&' at the end of the command when using this parameter.
109
83
  - Avoid using ${BASH_TOOL_NAME} with the \`find\`, \`sed\`, \`awk\`, or \`echo\` commands, unless explicitly instructed or when these commands are truly necessary for the task. Instead, always prefer using the dedicated tools for these commands:
110
84
  - File search: Use ${GLOB_TOOL_NAME} (NOT find or ls)
@@ -139,7 +113,10 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
139
113
  - Do not retry failing commands in a sleep loop — diagnose the root cause.
140
114
  - If waiting for a background task you started with \`run_in_background\`, you will be notified when it completes — do not poll.
141
115
  - 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.`,
116
+ - If you must sleep, keep the duration short (1-5 seconds) to avoid blocking the user.
117
+
118
+ # CWD management
119
+ The working directory persists between commands. 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.`,
143
120
  execute: async (
144
121
  args: Record<string, unknown>,
145
122
  context: ToolContext,
@@ -237,7 +214,14 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
237
214
 
238
215
  // Foreground execution (original behavior)
239
216
  return new Promise((resolve) => {
240
- const child: ChildProcess = spawn(command, {
217
+ // Create a temporary file to store the CWD
218
+ const tempCwdFile = path.join(
219
+ os.tmpdir(),
220
+ `wave_cwd_${Date.now()}_${Math.random().toString(36).substring(2, 11)}.tmp`,
221
+ );
222
+ const wrappedCommand = `${command} && pwd -P >| ${tempCwdFile}`;
223
+
224
+ const child: ChildProcess = spawn(wrappedCommand, {
241
225
  shell: true,
242
226
  stdio: "pipe",
243
227
  detached: true,
@@ -270,12 +254,12 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
270
254
  context.onShortResultUpdate(shortResult);
271
255
  }
272
256
 
273
- // Update full result
257
+ // Update full result (simple truncation for streaming — persistence happens at final result)
274
258
  if (context.onResultUpdate) {
275
259
  const content =
276
- combinedOutput.length <= MAX_OUTPUT_LENGTH
260
+ combinedOutput.length <= BASH_MAX_OUTPUT_CHARS
277
261
  ? combinedOutput
278
- : combinedOutput.substring(0, MAX_OUTPUT_LENGTH) +
262
+ : combinedOutput.substring(0, BASH_MAX_OUTPUT_CHARS) +
279
263
  "\n\n... (output truncated)";
280
264
  context.onResultUpdate(content);
281
265
  }
@@ -368,8 +352,10 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
368
352
  }
369
353
  }
370
354
 
371
- const processedOutput = processOutput(
355
+ const processedOutput = processToolResult(
372
356
  outputBuffer + (errorBuffer ? "\n" + errorBuffer : ""),
357
+ BASH_MAX_OUTPUT_CHARS,
358
+ "bash",
373
359
  );
374
360
  resolve({
375
361
  success: false,
@@ -422,14 +408,60 @@ Use the gh command via the Bash tool for GitHub-related tasks including working
422
408
  clearTimeout(timeoutHandle);
423
409
  }
424
410
 
411
+ // Read the new CWD from the temporary file
412
+ let newCwd: string | undefined;
413
+ try {
414
+ if (fs.existsSync(tempCwdFile)) {
415
+ newCwd = fs.readFileSync(tempCwdFile, "utf8").trim();
416
+ // Validate the path exists before calling the callback
417
+ fs.accessSync(newCwd, fs.constants.F_OK);
418
+ }
419
+ } catch (fileError) {
420
+ logger.warn(
421
+ `Could not read or validate new CWD from temp file ${tempCwdFile}:`,
422
+ fileError,
423
+ );
424
+ newCwd = undefined;
425
+ } finally {
426
+ // Ensure temp file is cleaned up even if reading fails
427
+ try {
428
+ if (fs.existsSync(tempCwdFile)) {
429
+ fs.unlinkSync(tempCwdFile);
430
+ }
431
+ } catch (fileError) {
432
+ logger.error("Failed to clean up temp CWD file:", fileError);
433
+ }
434
+ }
435
+
436
+ // If CWD changed, call the onCwdChange callback and add notification
437
+ let cwdResetMessage: string | undefined;
438
+ if (newCwd && newCwd !== context.workdir && context.onCwdChange) {
439
+ const isInSafeZone =
440
+ context.permissionManager?.isPathInSafeZone?.(newCwd) ?? true;
441
+
442
+ if (isInSafeZone) {
443
+ context.onCwdChange(newCwd);
444
+ } else if (context.originalWorkdir) {
445
+ context.onCwdChange(context.originalWorkdir);
446
+ cwdResetMessage = `Shell cwd was reset to ${context.originalWorkdir}`;
447
+ } else {
448
+ context.onCwdChange(newCwd);
449
+ }
450
+ }
451
+
425
452
  const exitCode = code ?? 0;
426
453
  const combinedOutput =
427
454
  outputBuffer + (errorBuffer ? "\n" + errorBuffer : "");
428
455
 
429
- // Handle large output by truncation and persistence if needed
456
+ // Prepend CWD reset message to output if present (like Claude Code's stderr approach)
430
457
  const finalOutput =
431
- combinedOutput || `Command executed with exit code: ${exitCode}`;
432
- const content = processOutput(finalOutput);
458
+ (cwdResetMessage ? cwdResetMessage + "\n" : "") +
459
+ (combinedOutput || `Command executed with exit code: ${exitCode}`);
460
+ const content = processToolResult(
461
+ finalOutput,
462
+ BASH_MAX_OUTPUT_CHARS,
463
+ "bash",
464
+ );
433
465
 
434
466
  const lines = combinedOutput.trim().split("\n");
435
467
  const shortResult =
@@ -107,4 +107,8 @@ export interface ToolContext {
107
107
  readFileState?: Map<string, { mtime: number; hash: string }>;
108
108
  /** Hook manager instance for executing hooks */
109
109
  hookManager?: import("../managers/hookManager.js").HookManager;
110
+ /** Callback to notify when the current working directory changes */
111
+ onCwdChange?: (newCwd: string) => void;
112
+ /** Original working directory (before any cd changes) for CWD reset */
113
+ originalWorkdir?: string;
110
114
  }
@@ -14,6 +14,7 @@ import type { MessageManagerCallbacks } from "../managers/messageManager.js";
14
14
  import type { BackgroundTaskManagerCallbacks } from "../managers/backgroundTaskManager.js";
15
15
  import type { McpManagerCallbacks } from "../managers/mcpManager.js";
16
16
  import type { SubagentManagerCallbacks } from "../managers/subagentManager.js";
17
+ import type { PartialHookConfiguration } from "./configuration.js";
17
18
  import type { ToolPlugin } from "../tools/types.js";
18
19
 
19
20
  /**
@@ -89,6 +90,11 @@ export interface AgentOptions {
89
90
  mcpServers?: Record<string, McpServerConfig>;
90
91
  /** Custom tools provided by the SDK user, registered alongside built-in tools */
91
92
  customTools?: ToolPlugin[];
93
+ /**
94
+ * Optional hook configuration to inject at creation time.
95
+ * File-based hooks (from config.json/.waverc.json) merge on top of these.
96
+ */
97
+ hooks?: PartialHookConfiguration;
92
98
  [key: string]: unknown;
93
99
  }
94
100
 
@@ -109,5 +115,6 @@ export interface AgentCallbacks
109
115
  onConfiguredModelsChange?: (models: string[]) => void;
110
116
  onLoadingChange?: (loading: boolean) => void;
111
117
  onCommandRunningChange?: (running: boolean) => void;
118
+ onWorkdirChange?: (newCwd: string) => void;
112
119
  onQueuedMessagesChange?: (messages: QueuedMessage[]) => void;
113
120
  }
package/src/types/auth.ts CHANGED
@@ -5,5 +5,15 @@ export interface AuthUser {
5
5
 
6
6
  export interface AuthConfig {
7
7
  SSO_TOKEN?: string;
8
+ SSO_REFRESH_TOKEN?: string;
9
+ SSO_TOKEN_EXPIRES_AT?: number; // Unix timestamp (ms) when SSO_TOKEN expires
8
10
  user?: AuthUser;
9
11
  }
12
+
13
+ /** Server response from POST /api/auth/token */
14
+ export interface TokenResponse {
15
+ token: string;
16
+ refreshToken?: string;
17
+ expiresIn?: number; // seconds until token expires
18
+ user: { id: string; email?: string };
19
+ }