wave-agent-sdk 0.16.9 → 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 (85) hide show
  1. package/dist/agent.d.ts +5 -0
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +18 -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 +1 -0
  20. package/dist/services/configurationService.d.ts.map +1 -1
  21. package/dist/services/configurationService.js +48 -6
  22. package/dist/services/hook.d.ts +4 -0
  23. package/dist/services/hook.d.ts.map +1 -1
  24. package/dist/services/hook.js +10 -0
  25. package/dist/services/initializationService.d.ts.map +1 -1
  26. package/dist/services/initializationService.js +11 -0
  27. package/dist/services/interactionService.d.ts.map +1 -1
  28. package/dist/services/interactionService.js +0 -12
  29. package/dist/services/remoteSettingsService.d.ts +21 -0
  30. package/dist/services/remoteSettingsService.d.ts.map +1 -0
  31. package/dist/services/remoteSettingsService.js +280 -0
  32. package/dist/tools/bashTool.d.ts.map +1 -1
  33. package/dist/tools/bashTool.js +58 -32
  34. package/dist/tools/types.d.ts +4 -0
  35. package/dist/tools/types.d.ts.map +1 -1
  36. package/dist/types/agent.d.ts +7 -0
  37. package/dist/types/agent.d.ts.map +1 -1
  38. package/dist/types/auth.d.ts +12 -0
  39. package/dist/types/auth.d.ts.map +1 -1
  40. package/dist/types/configuration.d.ts +20 -0
  41. package/dist/types/configuration.d.ts.map +1 -1
  42. package/dist/types/hooks.d.ts +5 -1
  43. package/dist/types/hooks.d.ts.map +1 -1
  44. package/dist/types/hooks.js +1 -0
  45. package/dist/types/mcp.d.ts +1 -1
  46. package/dist/types/mcp.d.ts.map +1 -1
  47. package/dist/utils/containerSetup.d.ts.map +1 -1
  48. package/dist/utils/containerSetup.js +9 -8
  49. package/dist/utils/gitUtils.d.ts +18 -1
  50. package/dist/utils/gitUtils.d.ts.map +1 -1
  51. package/dist/utils/gitUtils.js +120 -49
  52. package/dist/utils/mcpUtils.d.ts.map +1 -1
  53. package/dist/utils/mcpUtils.js +6 -1
  54. package/dist/utils/openaiClient.d.ts.map +1 -1
  55. package/dist/utils/openaiClient.js +4 -2
  56. package/dist/utils/toolResultStorage.d.ts +46 -0
  57. package/dist/utils/toolResultStorage.d.ts.map +1 -0
  58. package/dist/utils/toolResultStorage.js +90 -0
  59. package/dist/utils/worktreeUtils.d.ts.map +1 -1
  60. package/dist/utils/worktreeUtils.js +58 -0
  61. package/package.json +3 -3
  62. package/src/agent.ts +20 -0
  63. package/src/constants/toolLimits.ts +3 -0
  64. package/src/managers/aiManager.ts +37 -0
  65. package/src/managers/hookManager.ts +42 -17
  66. package/src/managers/mcpManager.ts +10 -178
  67. package/src/services/authService.ts +243 -16
  68. package/src/services/configurationService.ts +58 -6
  69. package/src/services/hook.ts +15 -0
  70. package/src/services/initializationService.ts +13 -0
  71. package/src/services/interactionService.ts +0 -18
  72. package/src/services/remoteSettingsService.ts +315 -0
  73. package/src/tools/bashTool.ts +70 -38
  74. package/src/tools/types.ts +4 -0
  75. package/src/types/agent.ts +7 -0
  76. package/src/types/auth.ts +10 -0
  77. package/src/types/configuration.ts +23 -0
  78. package/src/types/hooks.ts +7 -1
  79. package/src/types/mcp.ts +1 -1
  80. package/src/utils/containerSetup.ts +8 -8
  81. package/src/utils/gitUtils.ts +123 -48
  82. package/src/utils/mcpUtils.ts +12 -1
  83. package/src/utils/openaiClient.ts +5 -2
  84. package/src/utils/toolResultStorage.ts +117 -0
  85. package/src/utils/worktreeUtils.ts +63 -0
@@ -0,0 +1,315 @@
1
+ import * as fs from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import * as path from "node:path";
4
+
5
+ import { authService, createAuthAwareFetch } from "./authService.js";
6
+ import type {
7
+ RemoteSettingsCache,
8
+ RemoteSettingsFetchResult,
9
+ RemoteSettingsResponse,
10
+ } from "../types/configuration.js";
11
+ import type { WaveConfiguration } from "../types/configuration.js";
12
+ import type { HookEvent, HookEventConfig } from "../types/hooks.js";
13
+ import { logger } from "../utils/globalLogger.js";
14
+
15
+ const CACHE_FILE = path.join(homedir(), ".wave", "remote-settings.json");
16
+ const POLLING_INTERVAL_MS = 60 * 60 * 1000; // 60 minutes
17
+ const FETCH_TIMEOUT_MS = 10_000;
18
+
19
+ let _cachedSettings: RemoteSettingsCache | null = null;
20
+ let _pollingTimer: ReturnType<typeof setInterval> | null = null;
21
+
22
+ function loadCacheFromDisk(): void {
23
+ try {
24
+ if (!fs.existsSync(CACHE_FILE)) {
25
+ return;
26
+ }
27
+ const raw = fs.readFileSync(CACHE_FILE, "utf-8");
28
+ const parsed: RemoteSettingsCache = JSON.parse(raw);
29
+ _cachedSettings = parsed;
30
+ logger.debug("remoteSettings: loaded cache from disk", {
31
+ checksum: parsed.checksum,
32
+ });
33
+ } catch (err) {
34
+ logger.debug("remoteSettings: failed to load cache from disk", { err });
35
+ }
36
+ }
37
+
38
+ function writeCacheToDisk(): void {
39
+ if (!_cachedSettings) {
40
+ return;
41
+ }
42
+ try {
43
+ const dir = path.dirname(CACHE_FILE);
44
+ if (!fs.existsSync(dir)) {
45
+ fs.mkdirSync(dir, { recursive: true });
46
+ }
47
+ fs.writeFileSync(CACHE_FILE, JSON.stringify(_cachedSettings, null, 2), {
48
+ mode: 0o600,
49
+ });
50
+ logger.debug("remoteSettings: wrote cache to disk", {
51
+ checksum: _cachedSettings.checksum,
52
+ });
53
+ } catch (err) {
54
+ logger.debug("remoteSettings: failed to write cache to disk", { err });
55
+ }
56
+ }
57
+
58
+ function removeCacheFromDisk(): void {
59
+ try {
60
+ if (fs.existsSync(CACHE_FILE)) {
61
+ fs.unlinkSync(CACHE_FILE);
62
+ }
63
+ } catch (err) {
64
+ logger.debug("remoteSettings: failed to remove cache file", { err });
65
+ }
66
+ }
67
+
68
+ async function fetchRemoteSettings(): Promise<RemoteSettingsFetchResult> {
69
+ if (!authService.isSSOAuthenticated()) {
70
+ logger.debug("remoteSettings: skipping fetch — not SSO authenticated");
71
+ return { success: false, error: "Not SSO authenticated" };
72
+ }
73
+
74
+ const token = authService.getSSOToken();
75
+ const serverUrl = authService.getServerUrl();
76
+ if (!token || !serverUrl) {
77
+ return { success: false, error: "Missing SSO token or server URL" };
78
+ }
79
+
80
+ const headers: Record<string, string> = {
81
+ Authorization: `Bearer ${token}`,
82
+ };
83
+ if (_cachedSettings?.checksum) {
84
+ headers["If-None-Match"] = _cachedSettings.checksum;
85
+ }
86
+
87
+ try {
88
+ const authFetch = createAuthAwareFetch(globalThis.fetch);
89
+ const response = await authFetch(`${serverUrl}/api/wave/settings`, {
90
+ method: "GET",
91
+ headers,
92
+ signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
93
+ });
94
+
95
+ if (response.status === 304) {
96
+ logger.debug("remoteSettings: 304 unchanged", {
97
+ checksum: _cachedSettings?.checksum,
98
+ });
99
+ return {
100
+ success: true,
101
+ settings: _cachedSettings!.settings,
102
+ checksum: _cachedSettings!.checksum,
103
+ };
104
+ }
105
+
106
+ if (response.status === 404) {
107
+ logger.debug("remoteSettings: 404 not configured — clearing stale cache");
108
+ _cachedSettings = null;
109
+ removeCacheFromDisk();
110
+ return { success: true, notConfigured: true, settings: null };
111
+ }
112
+
113
+ if (!response.ok) {
114
+ const body = await response.text().catch(() => "");
115
+ logger.debug("remoteSettings: fetch failed", {
116
+ status: response.status,
117
+ body: body.slice(0, 200),
118
+ });
119
+ return {
120
+ success: false,
121
+ error: `HTTP ${response.status}`,
122
+ settings: _cachedSettings?.settings ?? null,
123
+ };
124
+ }
125
+
126
+ const data = (await response.json()) as RemoteSettingsResponse;
127
+ _cachedSettings = {
128
+ uuid: data.uuid,
129
+ checksum: data.checksum,
130
+ settings: data.settings,
131
+ fetchedAt: new Date().toISOString(),
132
+ };
133
+ writeCacheToDisk();
134
+ logger.debug("remoteSettings: fetched new settings", {
135
+ checksum: data.checksum,
136
+ });
137
+ return {
138
+ success: true,
139
+ settings: data.settings,
140
+ checksum: data.checksum,
141
+ };
142
+ } catch (err) {
143
+ logger.debug("remoteSettings: network error, using cache", { err });
144
+ return {
145
+ success: false,
146
+ error: err instanceof Error ? err.message : String(err),
147
+ settings: _cachedSettings?.settings ?? null,
148
+ };
149
+ }
150
+ }
151
+
152
+ function startPolling(): void {
153
+ if (_pollingTimer) {
154
+ return;
155
+ }
156
+ _pollingTimer = setInterval(async () => {
157
+ try {
158
+ await fetchRemoteSettings();
159
+ } catch (err) {
160
+ logger.debug("remoteSettings: polling fetch error", { err });
161
+ }
162
+ }, POLLING_INTERVAL_MS);
163
+ _pollingTimer.unref();
164
+ }
165
+
166
+ export function initialize(): void {
167
+ loadCacheFromDisk();
168
+ // Fire-and-forget the initial fetch, then start background polling
169
+ fetchRemoteSettings()
170
+ .then(() => startPolling())
171
+ .catch((err) => {
172
+ logger.debug("remoteSettings: initial fetch failed", { err });
173
+ startPolling();
174
+ });
175
+ }
176
+
177
+ export function getRemoteSettingsSync(): WaveConfiguration | null {
178
+ return _cachedSettings?.settings ?? null;
179
+ }
180
+
181
+ export async function refresh(): Promise<RemoteSettingsFetchResult> {
182
+ // Clear in-memory so we force a fresh fetch
183
+ _cachedSettings = null;
184
+ removeCacheFromDisk();
185
+ return fetchRemoteSettings();
186
+ }
187
+
188
+ export function clear(): void {
189
+ _cachedSettings = null;
190
+ removeCacheFromDisk();
191
+ if (_pollingTimer) {
192
+ clearInterval(_pollingTimer);
193
+ _pollingTimer = null;
194
+ }
195
+ }
196
+
197
+ export function shutdown(): void {
198
+ if (_pollingTimer) {
199
+ clearInterval(_pollingTimer);
200
+ _pollingTimer = null;
201
+ }
202
+ }
203
+
204
+ function dedupe(arr: string[]): string[] {
205
+ return [...new Set(arr)];
206
+ }
207
+
208
+ function mergeHooks(
209
+ local: Partial<Record<HookEvent, HookEventConfig[]>> | undefined,
210
+ remote: Partial<Record<HookEvent, HookEventConfig[]>> | undefined,
211
+ ): Partial<Record<HookEvent, HookEventConfig[]>> | undefined {
212
+ if (!remote && !local) {
213
+ return undefined;
214
+ }
215
+ if (!remote) {
216
+ return local;
217
+ }
218
+ if (!local) {
219
+ return remote;
220
+ }
221
+ const merged: Partial<Record<HookEvent, HookEventConfig[]>> = { ...local };
222
+ for (const [event, remoteHooks] of Object.entries(remote)) {
223
+ const localHooks = merged[event as HookEvent] ?? [];
224
+ // Concatenate + dedupe by JSON serialization
225
+ const combined = [...localHooks, ...(remoteHooks ?? [])];
226
+ const seen = new Set<string>();
227
+ merged[event as HookEvent] = combined.filter((h) => {
228
+ const key = JSON.stringify(h);
229
+ if (seen.has(key)) {
230
+ return false;
231
+ }
232
+ seen.add(key);
233
+ return true;
234
+ });
235
+ }
236
+ return merged;
237
+ }
238
+
239
+ export function mergeRemoteSettings(
240
+ localMerged: WaveConfiguration,
241
+ remote: WaveConfiguration,
242
+ ): WaveConfiguration {
243
+ const result: WaveConfiguration = { ...localMerged };
244
+
245
+ // env: merge by key, remote wins per-key
246
+ if (remote.env || localMerged.env) {
247
+ result.env = { ...localMerged.env, ...remote.env };
248
+ }
249
+
250
+ // permissions
251
+ if (remote.permissions || localMerged.permissions) {
252
+ const lp = localMerged.permissions ?? {};
253
+ const rp = remote.permissions ?? {};
254
+ result.permissions = {
255
+ // allow: concatenate + dedupe
256
+ allow:
257
+ lp.allow || rp.allow
258
+ ? dedupe([...(lp.allow ?? []), ...(rp.allow ?? [])])
259
+ : undefined,
260
+ // deny: concatenate + dedupe
261
+ deny:
262
+ lp.deny || rp.deny
263
+ ? dedupe([...(lp.deny ?? []), ...(rp.deny ?? [])])
264
+ : undefined,
265
+ // permissionMode: remote wins (scalar)
266
+ permissionMode: rp.permissionMode ?? lp.permissionMode,
267
+ // additionalDirectories: concatenate + dedupe
268
+ additionalDirectories:
269
+ lp.additionalDirectories || rp.additionalDirectories
270
+ ? dedupe([
271
+ ...(lp.additionalDirectories ?? []),
272
+ ...(rp.additionalDirectories ?? []),
273
+ ])
274
+ : undefined,
275
+ };
276
+ // Clean up undefined keys
277
+ if (!result.permissions.allow) delete result.permissions.allow;
278
+ if (!result.permissions.deny) delete result.permissions.deny;
279
+ if (!result.permissions.permissionMode)
280
+ delete result.permissions.permissionMode;
281
+ if (!result.permissions.additionalDirectories)
282
+ delete result.permissions.additionalDirectories;
283
+ }
284
+
285
+ // hooks: concatenate per-event
286
+ result.hooks = mergeHooks(localMerged.hooks, remote.hooks);
287
+
288
+ // Scalar / last-write-wins fields: remote wins
289
+ if (remote.language !== undefined) result.language = remote.language;
290
+ if (remote.model !== undefined) result.model = remote.model;
291
+ if (remote.autoMemoryEnabled !== undefined)
292
+ result.autoMemoryEnabled = remote.autoMemoryEnabled;
293
+ if (remote.autoMemoryFrequency !== undefined)
294
+ result.autoMemoryFrequency = remote.autoMemoryFrequency;
295
+ if (remote.models !== undefined) result.models = remote.models;
296
+ if (remote.marketplaces !== undefined)
297
+ result.marketplaces = remote.marketplaces;
298
+ if (remote.enabledPlugins !== undefined)
299
+ result.enabledPlugins = remote.enabledPlugins;
300
+
301
+ return result;
302
+ }
303
+
304
+ /**
305
+ * Singleton object for consumers that prefer a namespace-style import.
306
+ * Usage: import { remoteSettingsService } from "./remoteSettingsService.js"
307
+ */
308
+ export const remoteSettingsService = {
309
+ initialize,
310
+ getRemoteSettingsSync,
311
+ refresh,
312
+ clear,
313
+ shutdown,
314
+ mergeRemoteSettings,
315
+ } as const;
@@ -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
+ }
@@ -44,6 +44,8 @@ export interface WaveConfiguration {
44
44
  autoMemoryEnabled?: boolean;
45
45
  /** Frequency of auto-memory extraction turns */
46
46
  autoMemoryFrequency?: number;
47
+ /** Persisted model selection (from /model command) */
48
+ model?: string;
47
49
  /** Model-specific configuration overrides */
48
50
  models?: Record<string, Partial<ModelConfig>>;
49
51
  /** Scoped marketplace declarations */
@@ -138,3 +140,24 @@ interface Logger {
138
140
  info: (...args: unknown[]) => void;
139
141
  debug: (...args: unknown[]) => void;
140
142
  }
143
+
144
+ export interface RemoteSettingsResponse {
145
+ uuid: string;
146
+ checksum: string;
147
+ settings: WaveConfiguration;
148
+ }
149
+
150
+ export interface RemoteSettingsCache {
151
+ uuid: string;
152
+ checksum: string;
153
+ settings: WaveConfiguration;
154
+ fetchedAt: string;
155
+ }
156
+
157
+ export interface RemoteSettingsFetchResult {
158
+ success: boolean;
159
+ settings?: WaveConfiguration | null;
160
+ checksum?: string;
161
+ error?: string;
162
+ notConfigured?: boolean;
163
+ }
@@ -22,6 +22,7 @@ export type HookEvent =
22
22
  | "PermissionRequest"
23
23
  | "WorktreeCreate"
24
24
  | "WorktreeRemove"
25
+ | "CwdChanged"
25
26
  | "SessionStart"
26
27
  | "SessionEnd";
27
28
 
@@ -113,6 +114,7 @@ export function isValidHookEvent(event: string): event is HookEvent {
113
114
  "PermissionRequest",
114
115
  "WorktreeCreate",
115
116
  "WorktreeRemove",
117
+ "CwdChanged",
116
118
  "SessionStart",
117
119
  "SessionEnd",
118
120
  ].includes(event);
@@ -171,7 +173,7 @@ export interface HookJsonInput {
171
173
  session_id: string; // Format: "wave_session_{uuid}_{shortId}"
172
174
  transcript_path: string; // Format: "~/.wave/sessions/session_{shortId}.json"
173
175
  cwd: string; // Absolute path to current working directory
174
- hook_event_name: HookEvent; // "PreToolUse" | "PostToolUse" | "UserPromptSubmit" | "Stop" | "SubagentStop" | "PermissionRequest" | "WorktreeCreate" | "SessionStart"
176
+ hook_event_name: HookEvent; // "PreToolUse" | "PostToolUse" | "UserPromptSubmit" | "Stop" | "SubagentStop" | "PermissionRequest" | "WorktreeCreate" | "CwdChanged" | "SessionStart"
175
177
 
176
178
  // Optional fields based on event type
177
179
  tool_name?: string; // Present for PreToolUse, PostToolUse, PermissionRequest
@@ -180,6 +182,8 @@ export interface HookJsonInput {
180
182
  user_prompt?: string; // Present for UserPromptSubmit only
181
183
  subagent_type?: string; // Present when hook is executed by a subagent
182
184
  name?: string; // Present for WorktreeCreate events
185
+ old_cwd?: string; // Present for CwdChanged events
186
+ new_cwd?: string; // Present for CwdChanged events
183
187
  source?: SessionStartSource; // Present for SessionStart events
184
188
  agent_type?: string; // Present for SessionStart events
185
189
  end_source?: SessionEndSource; // Present for SessionEnd events
@@ -196,6 +200,8 @@ export interface ExtendedHookExecutionContext extends HookExecutionContext {
196
200
  userPrompt?: string; // User prompt text (UserPromptSubmit only)
197
201
  subagentType?: string; // Subagent type when hook is executed by a subagent
198
202
  worktreeName?: string; // Worktree name (WorktreeCreate only)
203
+ oldCwd?: string; // Previous working directory (CwdChanged only)
204
+ newCwd?: string; // New working directory (CwdChanged only)
199
205
  source?: SessionStartSource; // Session start source (SessionStart only)
200
206
  agentType?: string; // Agent type identifier (SessionStart only)
201
207
  endSource?: SessionEndSource; // Session end source (SessionEnd only)
package/src/types/mcp.ts CHANGED
@@ -26,7 +26,7 @@ export interface McpTool {
26
26
  export interface McpServerStatus {
27
27
  name: string;
28
28
  config: McpServerConfig;
29
- /** Pre-resolution URL with template variables (e.g. ${WAVE_SSO_TOKEN}) preserved for safe display */
29
+ /** Pre-resolution URL with template variables preserved for safe display */
30
30
  originalUrl?: string;
31
31
  status:
32
32
  | "disconnected"
@@ -39,6 +39,7 @@ import type {
39
39
 
40
40
  import { logger } from "./globalLogger.js";
41
41
  import { authService } from "../services/authService.js";
42
+ import { remoteSettingsService } from "../services/remoteSettingsService.js";
42
43
 
43
44
  export interface AgentContainerSetupOptions {
44
45
  options: AgentOptions;
@@ -146,8 +147,6 @@ export function setupAgentContainer(
146
147
  });
147
148
  container.register("BackgroundTaskManager", backgroundTaskManager);
148
149
 
149
- const ssoToken = authService.getSSOToken();
150
- const serverUrl = options.serverUrl || process.env.WAVE_SERVER_URL;
151
150
  if (options.serverUrl) {
152
151
  authService.setServerUrl(options.serverUrl);
153
152
  }
@@ -156,17 +155,15 @@ export function setupAgentContainer(
156
155
  mcpServers: options.mcpServers as
157
156
  | Record<string, McpServerConfig>
158
157
  | undefined,
159
- serverUrl,
160
- ssoToken,
161
158
  });
162
159
  container.register("McpManager", mcpManager);
163
160
 
164
- // Wire up auth change callback to reconnect MCP servers after SSO login
161
+ // Wire up auth change callback to refresh/clear remote settings
165
162
  authService.onAuthChange((event) => {
166
163
  if (event === "login") {
167
- const newServerUrl = options.serverUrl || process.env.WAVE_SERVER_URL;
168
- const newToken = authService.getSSOToken();
169
- mcpManager.refreshCredentials(newServerUrl, newToken);
164
+ remoteSettingsService.refresh();
165
+ } else if (event === "logout") {
166
+ remoteSettingsService.clear();
170
167
  }
171
168
  });
172
169
 
@@ -192,6 +189,9 @@ export function setupAgentContainer(
192
189
  container.register("PlanManager", planManager);
193
190
 
194
191
  const hookManager = new HookManager(container, workdir);
192
+ if (options.hooks) {
193
+ hookManager.loadConfiguration(options.hooks);
194
+ }
195
195
  container.register("HookManager", hookManager);
196
196
 
197
197
  const skillManager = new SkillManager(container, {