wave-agent-sdk 1.0.4 → 1.0.6
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 +23 -1
- package/dist/agent.js +34 -4
- package/dist/managers/aiManager.d.ts +36 -0
- package/dist/managers/aiManager.js +240 -8
- package/dist/managers/messageManager.d.ts +1 -3
- package/dist/managers/messageManager.js +19 -9
- package/dist/managers/slashCommandManager.js +28 -0
- package/dist/managers/subagentManager.d.ts +0 -2
- package/dist/services/session.js +2 -9
- package/dist/telemetry/instrumentation.d.ts +1 -1
- package/dist/telemetry/instrumentation.js +57 -28
- package/dist/tools/bashTool.js +16 -6
- package/dist/tools/grepTool.js +11 -3
- package/dist/types/index.d.ts +3 -0
- package/dist/types/messaging.d.ts +2 -0
- package/dist/utils/containerSetup.js +0 -3
- package/dist/utils/messageOperations.d.ts +10 -1
- package/dist/utils/messageOperations.js +19 -1
- package/dist/utils/notificationXml.js +5 -0
- package/dist/utils/ripgrep.d.ts +4 -4
- package/dist/utils/ripgrep.js +4 -27
- package/package.json +20 -8
- package/bin/rg +0 -79
- package/dist/managers/forkedAgentManager.d.ts +0 -50
- package/dist/managers/forkedAgentManager.js +0 -130
- package/scripts/install_ripgrep.js +0 -111
- package/scripts/postinstall.js +0 -38
- package/src/agent.ts +0 -1271
- package/src/constants/subagents.ts +0 -4
- package/src/constants/toolLimits.ts +0 -15
- package/src/constants/tools.ts +0 -24
- package/src/core/plugin.ts +0 -237
- package/src/core/session.ts +0 -9
- package/src/index.ts +0 -38
- package/src/managers/MemoryRuleManager.ts +0 -198
- package/src/managers/aiManager.ts +0 -2429
- package/src/managers/backgroundTaskManager.ts +0 -480
- package/src/managers/bangManager.ts +0 -119
- package/src/managers/cronManager.ts +0 -401
- package/src/managers/foregroundTaskManager.ts +0 -30
- package/src/managers/forkedAgentManager.ts +0 -216
- package/src/managers/hookManager.ts +0 -1089
- package/src/managers/liveConfigManager.ts +0 -445
- package/src/managers/lspManager.ts +0 -470
- package/src/managers/mcpManager.ts +0 -1010
- package/src/managers/messageManager.ts +0 -1180
- package/src/managers/messageQueue.ts +0 -157
- package/src/managers/permissionManager.ts +0 -1140
- package/src/managers/planManager.ts +0 -112
- package/src/managers/pluginManager.ts +0 -330
- package/src/managers/pluginScopeManager.ts +0 -124
- package/src/managers/reversionManager.ts +0 -178
- package/src/managers/skillManager.ts +0 -578
- package/src/managers/slashCommandManager.ts +0 -564
- package/src/managers/subagentManager.ts +0 -915
- package/src/managers/toolManager.ts +0 -489
- package/src/managers/workflowManager.ts +0 -493
- package/src/prompts/autoMemory.ts +0 -33
- package/src/prompts/autoMemoryExtraction.ts +0 -146
- package/src/prompts/index.ts +0 -506
- package/src/prompts/planModeReminders.ts +0 -126
- package/src/services/GitService.ts +0 -131
- package/src/services/MarketplaceService.ts +0 -1052
- package/src/services/MemoryRuleService.ts +0 -71
- package/src/services/aiService.ts +0 -919
- package/src/services/authService.ts +0 -565
- package/src/services/autoMemoryService.ts +0 -280
- package/src/services/configurationService.ts +0 -1520
- package/src/services/fileWatcher.ts +0 -319
- package/src/services/hook.ts +0 -348
- package/src/services/initializationService.ts +0 -357
- package/src/services/interactionService.ts +0 -229
- package/src/services/jsonlHandler.ts +0 -295
- package/src/services/memory.ts +0 -234
- package/src/services/pluginLoader.ts +0 -252
- package/src/services/remoteSettingsService.ts +0 -366
- package/src/services/reversionService.ts +0 -122
- package/src/services/session.ts +0 -922
- package/src/services/taskManager.ts +0 -321
- package/src/telemetry/events.ts +0 -65
- package/src/telemetry/instrumentation.ts +0 -499
- package/src/telemetry/sessionTracing.ts +0 -348
- package/src/tools/agentTool.ts +0 -322
- package/src/tools/askUserQuestion.ts +0 -160
- package/src/tools/bashTool.ts +0 -621
- package/src/tools/buildTool.ts +0 -61
- package/src/tools/cronCreateTool.ts +0 -161
- package/src/tools/cronDeleteTool.ts +0 -51
- package/src/tools/cronListTool.ts +0 -47
- package/src/tools/editTool.ts +0 -313
- package/src/tools/enterPlanMode.ts +0 -124
- package/src/tools/enterWorktreeTool.ts +0 -190
- package/src/tools/exitPlanMode.ts +0 -139
- package/src/tools/exitWorktreeTool.ts +0 -236
- package/src/tools/globTool.ts +0 -167
- package/src/tools/grepTool.ts +0 -399
- package/src/tools/lspTool.ts +0 -883
- package/src/tools/readTool.ts +0 -426
- package/src/tools/skillTool.ts +0 -248
- package/src/tools/taskManagementTools.ts +0 -574
- package/src/tools/taskStopTool.ts +0 -78
- package/src/tools/types.ts +0 -137
- package/src/tools/webFetchTool.ts +0 -373
- package/src/tools/workflowTool.ts +0 -205
- package/src/tools/writeTool.ts +0 -267
- package/src/types/agent.ts +0 -128
- package/src/types/auth.ts +0 -19
- package/src/types/commands.ts +0 -30
- package/src/types/config.ts +0 -43
- package/src/types/configuration.ts +0 -168
- package/src/types/core.ts +0 -105
- package/src/types/cron.ts +0 -13
- package/src/types/environment.ts +0 -106
- package/src/types/fileSearch.ts +0 -4
- package/src/types/history.ts +0 -7
- package/src/types/hooks.ts +0 -256
- package/src/types/index.ts +0 -41
- package/src/types/lsp.ts +0 -98
- package/src/types/marketplace.ts +0 -71
- package/src/types/mcp.ts +0 -43
- package/src/types/memoryRule.ts +0 -31
- package/src/types/messaging.ts +0 -114
- package/src/types/permissions.ts +0 -80
- package/src/types/plugins.ts +0 -39
- package/src/types/processes.ts +0 -99
- package/src/types/reversion.ts +0 -29
- package/src/types/session.ts +0 -12
- package/src/types/skills.ts +0 -108
- package/src/types/tasks.ts +0 -13
- package/src/types/telemetry.ts +0 -98
- package/src/types/tools.ts +0 -44
- package/src/types/workflow.ts +0 -6
- package/src/utils/abortUtils.ts +0 -118
- package/src/utils/atomicWrite.ts +0 -61
- package/src/utils/bashParser.ts +0 -899
- package/src/utils/cacheControlUtils.ts +0 -426
- package/src/utils/commandArgumentParser.ts +0 -161
- package/src/utils/commandPathResolver.ts +0 -88
- package/src/utils/configPaths.ts +0 -220
- package/src/utils/configValidator.ts +0 -100
- package/src/utils/constants.ts +0 -39
- package/src/utils/container.ts +0 -92
- package/src/utils/containerSetup.ts +0 -371
- package/src/utils/convertMessagesForAPI.ts +0 -326
- package/src/utils/cronTasks.ts +0 -128
- package/src/utils/cronTasksLock.ts +0 -182
- package/src/utils/cronToHuman.ts +0 -99
- package/src/utils/customCommands.ts +0 -95
- package/src/utils/editUtils.ts +0 -20
- package/src/utils/fileFormat.ts +0 -40
- package/src/utils/fileSearch.ts +0 -151
- package/src/utils/fileUtils.ts +0 -223
- package/src/utils/gitUtils.ts +0 -315
- package/src/utils/globalLogger.ts +0 -128
- package/src/utils/groupMessagesByApiRound.ts +0 -121
- package/src/utils/hookMatcher.ts +0 -176
- package/src/utils/markdownParser.ts +0 -317
- package/src/utils/mcpUtils.ts +0 -138
- package/src/utils/messageOperations.ts +0 -641
- package/src/utils/modelCapabilities.ts +0 -30
- package/src/utils/nameGenerator.ts +0 -99
- package/src/utils/notificationXml.ts +0 -57
- package/src/utils/openaiClient.ts +0 -251
- package/src/utils/parseCronExpression.ts +0 -78
- package/src/utils/path.ts +0 -72
- package/src/utils/pathEncoder.ts +0 -400
- package/src/utils/pathSafety.ts +0 -40
- package/src/utils/promptHistory.ts +0 -168
- package/src/utils/ripgrep.ts +0 -35
- package/src/utils/shellResolver.ts +0 -190
- package/src/utils/skillParser.ts +0 -246
- package/src/utils/stringUtils.ts +0 -156
- package/src/utils/subagentParser.ts +0 -279
- package/src/utils/taskReminder.ts +0 -96
- package/src/utils/tokenCalculation.ts +0 -43
- package/src/utils/tokenEstimate.ts +0 -34
- package/src/utils/toolResultStorage.ts +0 -117
- package/src/utils/worktreeSession.ts +0 -26
- package/src/utils/worktreeUtils.ts +0 -786
- package/src/workflow/budgetTracker.ts +0 -34
- package/src/workflow/concurrencyLimiter.ts +0 -47
- package/src/workflow/journal.ts +0 -95
- package/src/workflow/progressReporter.ts +0 -141
- package/src/workflow/runState.ts +0 -65
- package/src/workflow/scriptRuntime.ts +0 -274
- package/src/workflow/structuredOutput.ts +0 -123
- package/src/workflow/types.ts +0 -101
- package/src/workflow/workflowApis.ts +0 -410
- package/vendor/ripgrep/linux-aarch64/rg +0 -0
- package/vendor/ripgrep/linux-x86_64/rg +0 -0
- package/vendor/ripgrep/macos-aarch64/rg +0 -0
- package/vendor/ripgrep/macos-x86_64/rg +0 -0
- package/vendor/ripgrep/windows-aarch64/rg.exe +0 -0
- package/vendor/ripgrep/windows-x86_64/rg.exe +0 -0
|
@@ -1,480 +0,0 @@
|
|
|
1
|
-
import { spawn, type ChildProcess } from "child_process";
|
|
2
|
-
import * as os from "os";
|
|
3
|
-
import * as fs from "fs";
|
|
4
|
-
import * as path from "path";
|
|
5
|
-
import { BackgroundTask, BackgroundShell } from "../types/processes.js";
|
|
6
|
-
import { stripAnsiColors } from "../utils/stringUtils.js";
|
|
7
|
-
import { logger } from "../utils/globalLogger.js";
|
|
8
|
-
import { Container } from "../utils/container.js";
|
|
9
|
-
import { MessageQueue } from "./messageQueue.js";
|
|
10
|
-
import { resolveShellPath } from "../utils/shellResolver.js";
|
|
11
|
-
import type { ConfigurationService } from "../services/configurationService.js";
|
|
12
|
-
|
|
13
|
-
export interface BackgroundTaskManagerCallbacks {
|
|
14
|
-
onBackgroundTasksChange?: (tasks: BackgroundTask[]) => void;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface BackgroundTaskManagerOptions {
|
|
18
|
-
callbacks?: BackgroundTaskManagerCallbacks;
|
|
19
|
-
workdir: string;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export class BackgroundTaskManager {
|
|
23
|
-
private tasks = new Map<string, BackgroundTask>();
|
|
24
|
-
private nextId = 1;
|
|
25
|
-
private callbacks: BackgroundTaskManagerCallbacks;
|
|
26
|
-
private workdir: string;
|
|
27
|
-
|
|
28
|
-
constructor(
|
|
29
|
-
private container: Container,
|
|
30
|
-
options: BackgroundTaskManagerOptions,
|
|
31
|
-
) {
|
|
32
|
-
this.callbacks = options.callbacks || {};
|
|
33
|
-
this.workdir = options.workdir;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Merged env (OS env overlaid with this session's settings snapshot) for
|
|
38
|
-
* background-task subprocesses, so settings `env` vars reach them without
|
|
39
|
-
* polluting other sessions in one `wave --stdio` process.
|
|
40
|
-
*/
|
|
41
|
-
private get sessionEnv(): Record<string, string> {
|
|
42
|
-
return (
|
|
43
|
-
this.container
|
|
44
|
-
.get<ConfigurationService>("ConfigurationService")
|
|
45
|
-
?.getMergedEnv?.() ?? (process.env as Record<string, string>)
|
|
46
|
-
);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Fire the onBackgroundTasksChange callback so UI consumers refresh.
|
|
51
|
-
* Public so other managers (e.g. WorkflowManager) can trigger a refresh
|
|
52
|
-
* when their state changes without mutating a background task.
|
|
53
|
-
*/
|
|
54
|
-
public notifyTasksChange(): void {
|
|
55
|
-
this.callbacks.onBackgroundTasksChange?.(Array.from(this.tasks.values()));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public generateId(): string {
|
|
59
|
-
return `task_${process.pid}_${this.nextId++}`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public addTask(task: BackgroundTask): void {
|
|
63
|
-
this.tasks.set(task.id, task);
|
|
64
|
-
this.notifyTasksChange();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
public getTask(id: string): BackgroundTask | undefined {
|
|
68
|
-
return this.tasks.get(id);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public getAllTasks(): BackgroundTask[] {
|
|
72
|
-
return Array.from(this.tasks.values());
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public startShell(
|
|
76
|
-
command: string,
|
|
77
|
-
timeout?: number,
|
|
78
|
-
cwd?: string,
|
|
79
|
-
): { id: string; child: ChildProcess; detach: () => void } {
|
|
80
|
-
const id = this.generateId();
|
|
81
|
-
const startTime = Date.now();
|
|
82
|
-
|
|
83
|
-
const child = spawn(command, {
|
|
84
|
-
shell: resolveShellPath() ?? true,
|
|
85
|
-
stdio: "pipe",
|
|
86
|
-
detached: true,
|
|
87
|
-
cwd: cwd ?? this.workdir,
|
|
88
|
-
env: {
|
|
89
|
-
...process.env,
|
|
90
|
-
...this.sessionEnv,
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
// Create log file
|
|
95
|
-
const logPath = path.join(os.tmpdir(), `wave-task-${id}.log`);
|
|
96
|
-
const logStream = fs.createWriteStream(logPath, { flags: "w" });
|
|
97
|
-
|
|
98
|
-
const shell: BackgroundShell = {
|
|
99
|
-
id,
|
|
100
|
-
type: "shell",
|
|
101
|
-
process: child,
|
|
102
|
-
command,
|
|
103
|
-
startTime,
|
|
104
|
-
status: "running",
|
|
105
|
-
stdout: "",
|
|
106
|
-
stderr: "",
|
|
107
|
-
outputPath: logPath,
|
|
108
|
-
onStop: () => {
|
|
109
|
-
try {
|
|
110
|
-
logStream.end();
|
|
111
|
-
if (child.pid) {
|
|
112
|
-
process.kill(-child.pid, "SIGTERM");
|
|
113
|
-
const forceKillTimer = setTimeout(() => {
|
|
114
|
-
if (child.pid && !child.killed) {
|
|
115
|
-
try {
|
|
116
|
-
process.kill(-child.pid, "SIGKILL");
|
|
117
|
-
} catch (error: unknown) {
|
|
118
|
-
// ESRCH means the process already exited — not an error
|
|
119
|
-
if (
|
|
120
|
-
!(error instanceof Error) ||
|
|
121
|
-
(error as NodeJS.ErrnoException).code !== "ESRCH"
|
|
122
|
-
) {
|
|
123
|
-
logger.error("Failed to force kill process:", error);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}, 1000);
|
|
128
|
-
forceKillTimer.unref();
|
|
129
|
-
} else {
|
|
130
|
-
child.kill("SIGTERM");
|
|
131
|
-
}
|
|
132
|
-
} catch {
|
|
133
|
-
child.kill("SIGTERM");
|
|
134
|
-
}
|
|
135
|
-
},
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
this.tasks.set(id, shell);
|
|
139
|
-
this.notifyTasksChange();
|
|
140
|
-
|
|
141
|
-
// Set up timeout if specified
|
|
142
|
-
let timeoutHandle: NodeJS.Timeout | undefined;
|
|
143
|
-
if (timeout && timeout > 0) {
|
|
144
|
-
timeoutHandle = setTimeout(() => {
|
|
145
|
-
if (shell.status === "running") {
|
|
146
|
-
const timeoutMsg = "\n\nCommand timed out";
|
|
147
|
-
shell.stderr += timeoutMsg;
|
|
148
|
-
if (logStream.writable) {
|
|
149
|
-
logStream.write(timeoutMsg);
|
|
150
|
-
}
|
|
151
|
-
this.stopTask(id);
|
|
152
|
-
}
|
|
153
|
-
}, timeout);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
const onStdout = (data: Buffer | string) => {
|
|
157
|
-
const stripped = stripAnsiColors(data.toString());
|
|
158
|
-
shell.stdout += stripped;
|
|
159
|
-
if (logStream.writable) {
|
|
160
|
-
logStream.write(stripped);
|
|
161
|
-
}
|
|
162
|
-
this.notifyTasksChange();
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
const onStderr = (data: Buffer | string) => {
|
|
166
|
-
const stripped = stripAnsiColors(data.toString());
|
|
167
|
-
shell.stderr += stripped;
|
|
168
|
-
if (logStream.writable) {
|
|
169
|
-
logStream.write(stripped);
|
|
170
|
-
}
|
|
171
|
-
this.notifyTasksChange();
|
|
172
|
-
};
|
|
173
|
-
|
|
174
|
-
const onExit = (code: number | null) => {
|
|
175
|
-
if (timeoutHandle) {
|
|
176
|
-
clearTimeout(timeoutHandle);
|
|
177
|
-
}
|
|
178
|
-
if (logStream.writable) {
|
|
179
|
-
logStream.end();
|
|
180
|
-
}
|
|
181
|
-
const wasKilled = shell.status === "killed";
|
|
182
|
-
if (!wasKilled) {
|
|
183
|
-
shell.status = code === 0 ? "completed" : "failed";
|
|
184
|
-
}
|
|
185
|
-
shell.exitCode = code ?? 0;
|
|
186
|
-
shell.endTime = Date.now();
|
|
187
|
-
shell.runtime = shell.endTime - startTime;
|
|
188
|
-
this.notifyTasksChange();
|
|
189
|
-
|
|
190
|
-
// Skip notification if task was manually killed (user/agent-initiated stop)
|
|
191
|
-
if (!wasKilled) {
|
|
192
|
-
const messageQueue = this.container.has("MessageQueue")
|
|
193
|
-
? this.container.get<MessageQueue>("MessageQueue")
|
|
194
|
-
: undefined;
|
|
195
|
-
if (messageQueue) {
|
|
196
|
-
const statusStr = shell.status;
|
|
197
|
-
const summary = `Command "${command}" ${statusStr} with exit code ${code ?? 0}`;
|
|
198
|
-
messageQueue.enqueueNotification(
|
|
199
|
-
`<task-notification>\n<task-id>${id}</task-id>\n<task-type>shell</task-type>\n<output-file>${logPath}</output-file>\n<status>${statusStr}</status>\n<summary>${summary}</summary>\n</task-notification>`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
const onError = (error: Error) => {
|
|
206
|
-
if (timeoutHandle) {
|
|
207
|
-
clearTimeout(timeoutHandle);
|
|
208
|
-
}
|
|
209
|
-
const stripped = `\nProcess error: ${stripAnsiColors(error.message)}`;
|
|
210
|
-
shell.status = "failed";
|
|
211
|
-
shell.stderr += stripped;
|
|
212
|
-
if (logStream.writable) {
|
|
213
|
-
logStream.write(stripped);
|
|
214
|
-
logStream.end();
|
|
215
|
-
}
|
|
216
|
-
shell.exitCode = 1;
|
|
217
|
-
shell.endTime = Date.now();
|
|
218
|
-
shell.runtime = shell.endTime - startTime;
|
|
219
|
-
this.notifyTasksChange();
|
|
220
|
-
|
|
221
|
-
// Enqueue error notification
|
|
222
|
-
const messageQueue = this.container.has("MessageQueue")
|
|
223
|
-
? this.container.get<MessageQueue>("MessageQueue")
|
|
224
|
-
: undefined;
|
|
225
|
-
if (messageQueue) {
|
|
226
|
-
const summary = `Command "${command}" failed with error: ${stripAnsiColors(error.message)}`;
|
|
227
|
-
messageQueue.enqueueNotification(
|
|
228
|
-
`<task-notification>\n<task-id>${id}</task-id>\n<task-type>shell</task-type>\n<output-file>${logPath}</output-file>\n<status>failed</status>\n<summary>${summary}</summary>\n</task-notification>`,
|
|
229
|
-
);
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
child.stdout?.on("data", onStdout);
|
|
234
|
-
child.stderr?.on("data", onStderr);
|
|
235
|
-
child.on("exit", onExit);
|
|
236
|
-
child.on("error", onError);
|
|
237
|
-
|
|
238
|
-
const detach = () => {
|
|
239
|
-
child.stdout?.off("data", onStdout);
|
|
240
|
-
child.stderr?.off("data", onStderr);
|
|
241
|
-
child.off("exit", onExit);
|
|
242
|
-
child.off("error", onError);
|
|
243
|
-
if (timeoutHandle) {
|
|
244
|
-
clearTimeout(timeoutHandle);
|
|
245
|
-
}
|
|
246
|
-
logStream.end();
|
|
247
|
-
this.tasks.delete(id);
|
|
248
|
-
this.notifyTasksChange();
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
return { id, child, detach };
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
public adoptProcess(
|
|
255
|
-
child: ChildProcess,
|
|
256
|
-
command: string,
|
|
257
|
-
initialStdout: string = "",
|
|
258
|
-
initialStderr: string = "",
|
|
259
|
-
): string {
|
|
260
|
-
const id = this.generateId();
|
|
261
|
-
const startTime = Date.now();
|
|
262
|
-
|
|
263
|
-
// Create log file
|
|
264
|
-
const logPath = path.join(os.tmpdir(), `wave-task-${id}.log`);
|
|
265
|
-
const logStream = fs.createWriteStream(logPath, { flags: "w" });
|
|
266
|
-
|
|
267
|
-
// Write initial output to log file
|
|
268
|
-
if (initialStdout) {
|
|
269
|
-
logStream.write(stripAnsiColors(initialStdout));
|
|
270
|
-
}
|
|
271
|
-
if (initialStderr) {
|
|
272
|
-
logStream.write(stripAnsiColors(initialStderr));
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
const shell: BackgroundShell = {
|
|
276
|
-
id,
|
|
277
|
-
type: "shell",
|
|
278
|
-
process: child,
|
|
279
|
-
command,
|
|
280
|
-
startTime,
|
|
281
|
-
status: "running",
|
|
282
|
-
stdout: initialStdout,
|
|
283
|
-
stderr: initialStderr,
|
|
284
|
-
outputPath: logPath,
|
|
285
|
-
onStop: () => {
|
|
286
|
-
try {
|
|
287
|
-
logStream.end();
|
|
288
|
-
if (child.pid) {
|
|
289
|
-
process.kill(-child.pid, "SIGTERM");
|
|
290
|
-
const forceKillTimer = setTimeout(() => {
|
|
291
|
-
if (child.pid && !child.killed) {
|
|
292
|
-
try {
|
|
293
|
-
process.kill(-child.pid, "SIGKILL");
|
|
294
|
-
} catch (error: unknown) {
|
|
295
|
-
// ESRCH means the process already exited — not an error
|
|
296
|
-
if (
|
|
297
|
-
!(error instanceof Error) ||
|
|
298
|
-
(error as NodeJS.ErrnoException).code !== "ESRCH"
|
|
299
|
-
) {
|
|
300
|
-
logger.error("Failed to force kill process:", error);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
}, 1000);
|
|
305
|
-
forceKillTimer.unref();
|
|
306
|
-
} else {
|
|
307
|
-
child.kill("SIGTERM");
|
|
308
|
-
}
|
|
309
|
-
} catch {
|
|
310
|
-
child.kill("SIGTERM");
|
|
311
|
-
}
|
|
312
|
-
},
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
this.tasks.set(id, shell);
|
|
316
|
-
this.notifyTasksChange();
|
|
317
|
-
|
|
318
|
-
child.stdout?.on("data", (data) => {
|
|
319
|
-
const stripped = stripAnsiColors(data.toString());
|
|
320
|
-
shell.stdout += stripped;
|
|
321
|
-
if (logStream.writable) {
|
|
322
|
-
logStream.write(stripped);
|
|
323
|
-
}
|
|
324
|
-
this.notifyTasksChange();
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
child.stderr?.on("data", (data) => {
|
|
328
|
-
const stripped = stripAnsiColors(data.toString());
|
|
329
|
-
shell.stderr += stripped;
|
|
330
|
-
if (logStream.writable) {
|
|
331
|
-
logStream.write(stripped);
|
|
332
|
-
}
|
|
333
|
-
this.notifyTasksChange();
|
|
334
|
-
});
|
|
335
|
-
|
|
336
|
-
child.on("exit", (code) => {
|
|
337
|
-
if (logStream.writable) {
|
|
338
|
-
logStream.end();
|
|
339
|
-
}
|
|
340
|
-
const wasKilled = shell.status === "killed";
|
|
341
|
-
if (!wasKilled) {
|
|
342
|
-
shell.status = code === 0 ? "completed" : "failed";
|
|
343
|
-
}
|
|
344
|
-
shell.exitCode = code ?? 0;
|
|
345
|
-
shell.endTime = Date.now();
|
|
346
|
-
shell.runtime = shell.endTime - startTime;
|
|
347
|
-
this.notifyTasksChange();
|
|
348
|
-
|
|
349
|
-
// Skip notification if task was manually killed (user/agent-initiated stop)
|
|
350
|
-
if (!wasKilled) {
|
|
351
|
-
const messageQueue = this.container.has("MessageQueue")
|
|
352
|
-
? this.container.get<MessageQueue>("MessageQueue")
|
|
353
|
-
: undefined;
|
|
354
|
-
if (messageQueue) {
|
|
355
|
-
const statusStr = shell.status;
|
|
356
|
-
const summary = `Command "${command}" ${statusStr} with exit code ${code ?? 0}`;
|
|
357
|
-
messageQueue.enqueueNotification(
|
|
358
|
-
`<task-notification>\n<task-id>${id}</task-id>\n<task-type>shell</task-type>\n<output-file>${logPath}</output-file>\n<status>${statusStr}</status>\n<summary>${summary}</summary>\n</task-notification>`,
|
|
359
|
-
);
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
child.on("error", (error) => {
|
|
365
|
-
const stripped = `\nProcess error: ${stripAnsiColors(error.message)}`;
|
|
366
|
-
shell.status = "failed";
|
|
367
|
-
shell.stderr += stripped;
|
|
368
|
-
if (logStream.writable) {
|
|
369
|
-
logStream.write(stripped);
|
|
370
|
-
logStream.end();
|
|
371
|
-
}
|
|
372
|
-
shell.exitCode = 1;
|
|
373
|
-
shell.endTime = Date.now();
|
|
374
|
-
shell.runtime = shell.endTime - startTime;
|
|
375
|
-
this.notifyTasksChange();
|
|
376
|
-
|
|
377
|
-
// Enqueue error notification
|
|
378
|
-
const messageQueue = this.container.has("MessageQueue")
|
|
379
|
-
? this.container.get<MessageQueue>("MessageQueue")
|
|
380
|
-
: undefined;
|
|
381
|
-
if (messageQueue) {
|
|
382
|
-
const summary = `Command "${command}" failed with error: ${stripAnsiColors(error.message)}`;
|
|
383
|
-
messageQueue.enqueueNotification(
|
|
384
|
-
`<task-notification>\n<task-id>${id}</task-id>\n<task-type>shell</task-type>\n<output-file>${logPath}</output-file>\n<status>failed</status>\n<summary>${summary}</summary>\n</task-notification>`,
|
|
385
|
-
);
|
|
386
|
-
}
|
|
387
|
-
});
|
|
388
|
-
|
|
389
|
-
return id;
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
public getOutput(
|
|
393
|
-
id: string,
|
|
394
|
-
filter?: string,
|
|
395
|
-
): {
|
|
396
|
-
stdout: string;
|
|
397
|
-
stderr: string;
|
|
398
|
-
status: string;
|
|
399
|
-
outputPath?: string;
|
|
400
|
-
type: string;
|
|
401
|
-
exitCode?: number;
|
|
402
|
-
} | null {
|
|
403
|
-
const task = this.tasks.get(id);
|
|
404
|
-
if (!task) {
|
|
405
|
-
return null;
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
let stdout = task.stdout;
|
|
409
|
-
let stderr = task.stderr;
|
|
410
|
-
|
|
411
|
-
// Apply regex filter if provided
|
|
412
|
-
if (filter) {
|
|
413
|
-
try {
|
|
414
|
-
const regex = new RegExp(filter);
|
|
415
|
-
stdout = stdout
|
|
416
|
-
.split("\n")
|
|
417
|
-
.filter((line) => regex.test(line))
|
|
418
|
-
.join("\n");
|
|
419
|
-
stderr = stderr
|
|
420
|
-
.split("\n")
|
|
421
|
-
.filter((line) => regex.test(line))
|
|
422
|
-
.join("\n");
|
|
423
|
-
} catch (error) {
|
|
424
|
-
logger.warn(`Invalid filter regex: ${filter}`, error);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
return {
|
|
429
|
-
stdout,
|
|
430
|
-
stderr,
|
|
431
|
-
status: task.status,
|
|
432
|
-
outputPath: task.outputPath,
|
|
433
|
-
type: task.type,
|
|
434
|
-
exitCode: task.exitCode,
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
public stopTask(id: string): boolean {
|
|
439
|
-
const task = this.tasks.get(id);
|
|
440
|
-
if (!task || task.status !== "running") {
|
|
441
|
-
return false;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
if (task.onStop) {
|
|
445
|
-
try {
|
|
446
|
-
const result = task.onStop();
|
|
447
|
-
if (result instanceof Promise) {
|
|
448
|
-
result.catch((error) => {
|
|
449
|
-
logger.error("Error in background task onStop callback:", error);
|
|
450
|
-
});
|
|
451
|
-
}
|
|
452
|
-
} catch (error) {
|
|
453
|
-
logger.error("Error in background task onStop callback:", error);
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
// If it's a subagent task, we should also notify the subagent manager to cleanup
|
|
458
|
-
// However, to avoid circular dependency, we rely on the onStop callback
|
|
459
|
-
// which is already set to instance.aiManager.abortAIMessage()
|
|
460
|
-
// The subagentManager.cleanupInstance will be called by the tool or by status change.
|
|
461
|
-
|
|
462
|
-
task.status = "killed";
|
|
463
|
-
task.endTime = Date.now();
|
|
464
|
-
task.runtime = task.endTime - task.startTime;
|
|
465
|
-
this.notifyTasksChange();
|
|
466
|
-
|
|
467
|
-
return true;
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
public cleanup(): void {
|
|
471
|
-
// Kill all running tasks
|
|
472
|
-
for (const [id, task] of this.tasks) {
|
|
473
|
-
if (task.status === "running") {
|
|
474
|
-
this.stopTask(id);
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
this.tasks.clear();
|
|
478
|
-
this.notifyTasksChange();
|
|
479
|
-
}
|
|
480
|
-
}
|
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { spawn, type ChildProcess } from "child_process";
|
|
2
|
-
import type { MessageManager } from "./messageManager.js";
|
|
3
|
-
import { Container } from "../utils/container.js";
|
|
4
|
-
import { resolveShellPath } from "../utils/shellResolver.js";
|
|
5
|
-
import type { ConfigurationService } from "../services/configurationService.js";
|
|
6
|
-
|
|
7
|
-
export interface BangManagerOptions {
|
|
8
|
-
workdir: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface CommandExecutionResult {
|
|
12
|
-
exitCode: number;
|
|
13
|
-
output: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export class BangManager {
|
|
17
|
-
private workdir: string;
|
|
18
|
-
public isCommandRunning = false;
|
|
19
|
-
private currentProcess: ChildProcess | null = null;
|
|
20
|
-
onCommandRunningChange?: (running: boolean) => void;
|
|
21
|
-
|
|
22
|
-
constructor(
|
|
23
|
-
private container: Container,
|
|
24
|
-
options: BangManagerOptions,
|
|
25
|
-
) {
|
|
26
|
-
this.workdir = options.workdir;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
private get messageManager(): MessageManager {
|
|
30
|
-
return this.container.get<MessageManager>("MessageManager")!;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Merged env (OS env overlaid with this session's settings snapshot) for
|
|
35
|
-
* bang-command subprocesses, so settings `env` vars reach them without
|
|
36
|
-
* polluting other sessions in one `wave --stdio` process.
|
|
37
|
-
*/
|
|
38
|
-
private get sessionEnv(): Record<string, string> {
|
|
39
|
-
return (
|
|
40
|
-
this.container
|
|
41
|
-
.get<ConfigurationService>("ConfigurationService")
|
|
42
|
-
?.getMergedEnv?.() ?? (process.env as Record<string, string>)
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
private setCommandRunning(isRunning: boolean): void {
|
|
47
|
-
this.isCommandRunning = isRunning;
|
|
48
|
-
this.onCommandRunningChange?.(isRunning);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
public async executeCommand(command: string): Promise<number> {
|
|
52
|
-
if (this.isCommandRunning) {
|
|
53
|
-
throw new Error("Command already running");
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
this.setCommandRunning(true);
|
|
57
|
-
|
|
58
|
-
// Add bang placeholder
|
|
59
|
-
this.messageManager.addBangMessage(command);
|
|
60
|
-
|
|
61
|
-
return new Promise<number>((resolve) => {
|
|
62
|
-
const child = spawn(command, {
|
|
63
|
-
shell: resolveShellPath() ?? true,
|
|
64
|
-
stdio: "pipe",
|
|
65
|
-
cwd: this.workdir,
|
|
66
|
-
env: {
|
|
67
|
-
...process.env,
|
|
68
|
-
...this.sessionEnv,
|
|
69
|
-
},
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
this.currentProcess = child;
|
|
73
|
-
let outputBuffer = "";
|
|
74
|
-
|
|
75
|
-
const updateOutput = (newData: string) => {
|
|
76
|
-
outputBuffer += newData;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
child.stdout?.on("data", (data) => {
|
|
80
|
-
updateOutput(data.toString());
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
child.stderr?.on("data", (data) => {
|
|
84
|
-
updateOutput(data.toString());
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
child.on("exit", (code, signal) => {
|
|
88
|
-
const exitCode = code === null && signal ? 130 : (code ?? 0);
|
|
89
|
-
|
|
90
|
-
this.messageManager.completeBangMessage(
|
|
91
|
-
command,
|
|
92
|
-
exitCode,
|
|
93
|
-
outputBuffer,
|
|
94
|
-
);
|
|
95
|
-
|
|
96
|
-
this.setCommandRunning(false);
|
|
97
|
-
this.currentProcess = null;
|
|
98
|
-
resolve(exitCode);
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
child.on("error", (error) => {
|
|
102
|
-
updateOutput(`\nError: ${error.message}\n`);
|
|
103
|
-
this.messageManager.completeBangMessage(command, 1, outputBuffer);
|
|
104
|
-
|
|
105
|
-
this.setCommandRunning(false);
|
|
106
|
-
this.currentProcess = null;
|
|
107
|
-
resolve(1);
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
public abortCommand(): void {
|
|
113
|
-
if (this.currentProcess && this.isCommandRunning) {
|
|
114
|
-
this.currentProcess.kill("SIGKILL");
|
|
115
|
-
this.currentProcess = null;
|
|
116
|
-
this.isCommandRunning = false;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|