@dexto/core 1.5.4 → 1.5.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/DextoAgent.cjs +61 -155
- package/dist/agent/DextoAgent.d.ts +11 -54
- package/dist/agent/DextoAgent.d.ts.map +1 -1
- package/dist/agent/DextoAgent.js +61 -155
- package/dist/context/compaction/strategies/reactive-overflow.cjs +5 -3
- package/dist/context/compaction/strategies/reactive-overflow.d.ts +1 -0
- package/dist/context/compaction/strategies/reactive-overflow.d.ts.map +1 -1
- package/dist/context/compaction/strategies/reactive-overflow.js +5 -3
- package/dist/context/compaction/types.d.ts +13 -1
- package/dist/context/compaction/types.d.ts.map +1 -1
- package/dist/errors/types.cjs +0 -2
- package/dist/errors/types.d.ts +1 -5
- package/dist/errors/types.d.ts.map +1 -1
- package/dist/errors/types.js +0 -2
- package/dist/events/index.cjs +0 -2
- package/dist/events/index.d.ts +6 -25
- package/dist/events/index.d.ts.map +1 -1
- package/dist/events/index.js +0 -2
- package/dist/llm/executor/stream-processor.cjs +85 -27
- package/dist/llm/executor/stream-processor.d.ts +4 -0
- package/dist/llm/executor/stream-processor.d.ts.map +1 -1
- package/dist/llm/executor/stream-processor.js +85 -27
- package/dist/llm/executor/turn-executor.cjs +58 -130
- package/dist/llm/executor/turn-executor.d.ts +9 -43
- package/dist/llm/executor/turn-executor.d.ts.map +1 -1
- package/dist/llm/executor/turn-executor.js +58 -130
- package/dist/llm/executor/types.d.ts +0 -28
- package/dist/llm/executor/types.d.ts.map +1 -1
- package/dist/llm/services/vercel.cjs +2 -5
- package/dist/llm/services/vercel.d.ts +1 -6
- package/dist/llm/services/vercel.d.ts.map +1 -1
- package/dist/llm/services/vercel.js +2 -5
- package/dist/logger/logger.cjs +6 -7
- package/dist/logger/logger.d.ts +1 -0
- package/dist/logger/logger.d.ts.map +1 -1
- package/dist/logger/logger.js +6 -7
- package/dist/session/chat-session.cjs +19 -12
- package/dist/session/chat-session.d.ts +3 -6
- package/dist/session/chat-session.d.ts.map +1 -1
- package/dist/session/chat-session.js +19 -12
- package/dist/session/session-manager.cjs +0 -135
- package/dist/session/session-manager.d.ts +0 -43
- package/dist/session/session-manager.d.ts.map +1 -1
- package/dist/session/session-manager.js +0 -135
- package/dist/telemetry/telemetry.cjs +12 -5
- package/dist/telemetry/telemetry.d.ts.map +1 -1
- package/dist/telemetry/telemetry.js +12 -5
- package/dist/tools/schemas.cjs +2 -2
- package/dist/tools/schemas.js +2 -2
- package/package.json +15 -5
- package/dist/filesystem/error-codes.cjs +0 -53
- package/dist/filesystem/error-codes.d.ts +0 -31
- package/dist/filesystem/error-codes.d.ts.map +0 -1
- package/dist/filesystem/error-codes.js +0 -30
- package/dist/filesystem/errors.cjs +0 -303
- package/dist/filesystem/errors.d.ts +0 -109
- package/dist/filesystem/errors.d.ts.map +0 -1
- package/dist/filesystem/errors.js +0 -280
- package/dist/filesystem/filesystem-service.cjs +0 -534
- package/dist/filesystem/filesystem-service.d.ts +0 -97
- package/dist/filesystem/filesystem-service.d.ts.map +0 -1
- package/dist/filesystem/filesystem-service.js +0 -501
- package/dist/filesystem/index.cjs +0 -37
- package/dist/filesystem/index.d.ts +0 -11
- package/dist/filesystem/index.d.ts.map +0 -1
- package/dist/filesystem/index.js +0 -11
- package/dist/filesystem/path-validator.cjs +0 -250
- package/dist/filesystem/path-validator.d.ts +0 -103
- package/dist/filesystem/path-validator.d.ts.map +0 -1
- package/dist/filesystem/path-validator.js +0 -217
- package/dist/filesystem/types.cjs +0 -16
- package/dist/filesystem/types.d.ts +0 -175
- package/dist/filesystem/types.d.ts.map +0 -1
- package/dist/filesystem/types.js +0 -0
- package/dist/process/command-validator.cjs +0 -554
- package/dist/process/command-validator.d.ts +0 -49
- package/dist/process/command-validator.d.ts.map +0 -1
- package/dist/process/command-validator.js +0 -531
- package/dist/process/error-codes.cjs +0 -47
- package/dist/process/error-codes.d.ts +0 -25
- package/dist/process/error-codes.d.ts.map +0 -1
- package/dist/process/error-codes.js +0 -24
- package/dist/process/errors.cjs +0 -244
- package/dist/process/errors.d.ts +0 -87
- package/dist/process/errors.d.ts.map +0 -1
- package/dist/process/errors.js +0 -221
- package/dist/process/index.cjs +0 -37
- package/dist/process/index.d.ts +0 -11
- package/dist/process/index.d.ts.map +0 -1
- package/dist/process/index.js +0 -11
- package/dist/process/process-service.cjs +0 -497
- package/dist/process/process-service.d.ts +0 -69
- package/dist/process/process-service.d.ts.map +0 -1
- package/dist/process/process-service.js +0 -464
- package/dist/process/types.cjs +0 -16
- package/dist/process/types.d.ts +0 -107
- package/dist/process/types.d.ts.map +0 -1
- package/dist/process/types.js +0 -0
- package/dist/session/compaction-service.cjs +0 -139
- package/dist/session/compaction-service.d.ts +0 -81
- package/dist/session/compaction-service.d.ts.map +0 -1
- package/dist/session/compaction-service.js +0 -106
|
@@ -1,497 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var process_service_exports = {};
|
|
30
|
-
__export(process_service_exports, {
|
|
31
|
-
ProcessService: () => ProcessService
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(process_service_exports);
|
|
34
|
-
var import_node_child_process = require("node:child_process");
|
|
35
|
-
var crypto = __toESM(require("node:crypto"), 1);
|
|
36
|
-
var path = __toESM(require("node:path"), 1);
|
|
37
|
-
var import_command_validator = require("./command-validator.js");
|
|
38
|
-
var import_errors = require("./errors.js");
|
|
39
|
-
var import_types2 = require("../logger/v2/types.js");
|
|
40
|
-
const DEFAULT_TIMEOUT = 12e4;
|
|
41
|
-
const DEFAULT_MAX_TIMEOUT = 6e5;
|
|
42
|
-
const DEFAULT_MAX_CONCURRENT_PROCESSES = 5;
|
|
43
|
-
const DEFAULT_MAX_OUTPUT_BUFFER = 1024 * 1024;
|
|
44
|
-
class ProcessService {
|
|
45
|
-
config;
|
|
46
|
-
commandValidator;
|
|
47
|
-
initialized = false;
|
|
48
|
-
backgroundProcesses = /* @__PURE__ */ new Map();
|
|
49
|
-
logger;
|
|
50
|
-
constructor(config = {}, logger) {
|
|
51
|
-
this.config = {
|
|
52
|
-
securityLevel: config.securityLevel || "moderate",
|
|
53
|
-
maxTimeout: config.maxTimeout || DEFAULT_MAX_TIMEOUT,
|
|
54
|
-
maxConcurrentProcesses: config.maxConcurrentProcesses || DEFAULT_MAX_CONCURRENT_PROCESSES,
|
|
55
|
-
maxOutputBuffer: config.maxOutputBuffer || DEFAULT_MAX_OUTPUT_BUFFER,
|
|
56
|
-
allowedCommands: config.allowedCommands || [],
|
|
57
|
-
blockedCommands: config.blockedCommands || [],
|
|
58
|
-
environment: config.environment || {},
|
|
59
|
-
workingDirectory: config.workingDirectory
|
|
60
|
-
};
|
|
61
|
-
this.logger = logger.createChild(import_types2.DextoLogComponent.PROCESS);
|
|
62
|
-
this.commandValidator = new import_command_validator.CommandValidator(this.config, this.logger);
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Initialize the service
|
|
66
|
-
*/
|
|
67
|
-
async initialize() {
|
|
68
|
-
if (this.initialized) {
|
|
69
|
-
this.logger.debug("ProcessService already initialized");
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
this.backgroundProcesses.clear();
|
|
73
|
-
this.initialized = true;
|
|
74
|
-
this.logger.info("ProcessService initialized successfully");
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Execute a command
|
|
78
|
-
*/
|
|
79
|
-
async executeCommand(command, options = {}) {
|
|
80
|
-
if (!this.initialized) {
|
|
81
|
-
throw import_errors.ProcessError.notInitialized();
|
|
82
|
-
}
|
|
83
|
-
const validation = this.commandValidator.validateCommand(command);
|
|
84
|
-
if (!validation.isValid || !validation.normalizedCommand) {
|
|
85
|
-
throw import_errors.ProcessError.invalidCommand(command, validation.error || "Unknown error");
|
|
86
|
-
}
|
|
87
|
-
const normalizedCommand = validation.normalizedCommand;
|
|
88
|
-
const rawTimeout = options.timeout !== void 0 && Number.isFinite(options.timeout) ? options.timeout : DEFAULT_TIMEOUT;
|
|
89
|
-
const timeout = Math.max(1, Math.min(rawTimeout, this.config.maxTimeout));
|
|
90
|
-
const cwd = this.resolveSafeCwd(options.cwd);
|
|
91
|
-
const env = {};
|
|
92
|
-
for (const [key, value] of Object.entries({
|
|
93
|
-
...process.env,
|
|
94
|
-
...this.config.environment,
|
|
95
|
-
...options.env
|
|
96
|
-
})) {
|
|
97
|
-
if (value !== void 0) {
|
|
98
|
-
env[key] = value;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (options.runInBackground) {
|
|
102
|
-
return await this.executeInBackground(normalizedCommand, options);
|
|
103
|
-
}
|
|
104
|
-
return await this.executeForeground(normalizedCommand, {
|
|
105
|
-
cwd,
|
|
106
|
-
timeout,
|
|
107
|
-
env,
|
|
108
|
-
...options.description !== void 0 && { description: options.description },
|
|
109
|
-
...options.abortSignal !== void 0 && { abortSignal: options.abortSignal }
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
static SIGKILL_TIMEOUT_MS = 200;
|
|
113
|
-
/**
|
|
114
|
-
* Kill a process tree (process group on Unix, taskkill on Windows)
|
|
115
|
-
*/
|
|
116
|
-
async killProcessTree(pid, child) {
|
|
117
|
-
if (process.platform === "win32") {
|
|
118
|
-
await new Promise((resolve) => {
|
|
119
|
-
const killer = (0, import_node_child_process.spawn)("taskkill", ["/pid", String(pid), "/f", "/t"], {
|
|
120
|
-
stdio: "ignore"
|
|
121
|
-
});
|
|
122
|
-
killer.once("exit", () => resolve());
|
|
123
|
-
killer.once("error", () => resolve());
|
|
124
|
-
});
|
|
125
|
-
} else {
|
|
126
|
-
try {
|
|
127
|
-
process.kill(-pid, "SIGTERM");
|
|
128
|
-
await new Promise((res) => setTimeout(res, ProcessService.SIGKILL_TIMEOUT_MS));
|
|
129
|
-
if (child.exitCode === null) {
|
|
130
|
-
process.kill(-pid, "SIGKILL");
|
|
131
|
-
}
|
|
132
|
-
} catch {
|
|
133
|
-
child.kill("SIGTERM");
|
|
134
|
-
await new Promise((res) => setTimeout(res, ProcessService.SIGKILL_TIMEOUT_MS));
|
|
135
|
-
if (child.exitCode === null) {
|
|
136
|
-
child.kill("SIGKILL");
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Execute command in foreground with timeout and abort support
|
|
143
|
-
*/
|
|
144
|
-
executeForeground(command, options) {
|
|
145
|
-
return new Promise((resolve, reject) => {
|
|
146
|
-
const startTime = Date.now();
|
|
147
|
-
let stdout = "";
|
|
148
|
-
let stderr = "";
|
|
149
|
-
let killed = false;
|
|
150
|
-
let aborted = false;
|
|
151
|
-
let closed = false;
|
|
152
|
-
if (options.abortSignal?.aborted) {
|
|
153
|
-
this.logger.debug(`Command cancelled before execution: ${command}`);
|
|
154
|
-
resolve({
|
|
155
|
-
stdout: "",
|
|
156
|
-
stderr: "(Command was cancelled)",
|
|
157
|
-
exitCode: 130,
|
|
158
|
-
// Standard exit code for SIGINT
|
|
159
|
-
duration: 0
|
|
160
|
-
});
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
this.logger.debug(`Executing command: ${command}`);
|
|
164
|
-
const child = (0, import_node_child_process.spawn)(command, {
|
|
165
|
-
cwd: options.cwd,
|
|
166
|
-
env: options.env,
|
|
167
|
-
shell: true,
|
|
168
|
-
detached: process.platform !== "win32"
|
|
169
|
-
// Create process group on Unix
|
|
170
|
-
});
|
|
171
|
-
const timeoutHandle = setTimeout(() => {
|
|
172
|
-
killed = true;
|
|
173
|
-
if (child.pid) {
|
|
174
|
-
void this.killProcessTree(child.pid, child);
|
|
175
|
-
} else {
|
|
176
|
-
child.kill("SIGTERM");
|
|
177
|
-
}
|
|
178
|
-
}, options.timeout);
|
|
179
|
-
const abortHandler = () => {
|
|
180
|
-
if (closed) return;
|
|
181
|
-
aborted = true;
|
|
182
|
-
this.logger.debug(`Command cancelled by user: ${command}`);
|
|
183
|
-
clearTimeout(timeoutHandle);
|
|
184
|
-
if (child.pid) {
|
|
185
|
-
void this.killProcessTree(child.pid, child);
|
|
186
|
-
} else {
|
|
187
|
-
child.kill("SIGTERM");
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
options.abortSignal?.addEventListener("abort", abortHandler, { once: true });
|
|
191
|
-
child.stdout?.on("data", (data) => {
|
|
192
|
-
stdout += data.toString();
|
|
193
|
-
});
|
|
194
|
-
child.stderr?.on("data", (data) => {
|
|
195
|
-
stderr += data.toString();
|
|
196
|
-
});
|
|
197
|
-
child.on("close", (code, signal) => {
|
|
198
|
-
closed = true;
|
|
199
|
-
clearTimeout(timeoutHandle);
|
|
200
|
-
options.abortSignal?.removeEventListener("abort", abortHandler);
|
|
201
|
-
const duration = Date.now() - startTime;
|
|
202
|
-
if (aborted) {
|
|
203
|
-
stdout += "\n\n(Command was cancelled)";
|
|
204
|
-
this.logger.debug(`Command cancelled after ${duration}ms: ${command}`);
|
|
205
|
-
resolve({
|
|
206
|
-
stdout,
|
|
207
|
-
stderr,
|
|
208
|
-
exitCode: 130,
|
|
209
|
-
// Standard exit code for SIGINT
|
|
210
|
-
duration
|
|
211
|
-
});
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
if (killed) {
|
|
215
|
-
reject(import_errors.ProcessError.timeout(command, options.timeout));
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
let exitCode = typeof code === "number" ? code : 1;
|
|
219
|
-
if (code === null) {
|
|
220
|
-
stderr += `
|
|
221
|
-
Process terminated by signal ${signal ?? "UNKNOWN"}`;
|
|
222
|
-
}
|
|
223
|
-
this.logger.debug(
|
|
224
|
-
`Command completed with exit code ${exitCode} in ${duration}ms: ${command}`
|
|
225
|
-
);
|
|
226
|
-
resolve({
|
|
227
|
-
stdout,
|
|
228
|
-
stderr,
|
|
229
|
-
exitCode,
|
|
230
|
-
duration
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
child.on("error", (error) => {
|
|
234
|
-
clearTimeout(timeoutHandle);
|
|
235
|
-
options.abortSignal?.removeEventListener("abort", abortHandler);
|
|
236
|
-
if (error.code === "ENOENT") {
|
|
237
|
-
reject(import_errors.ProcessError.commandNotFound(command));
|
|
238
|
-
} else if (error.code === "EACCES") {
|
|
239
|
-
reject(import_errors.ProcessError.permissionDenied(command));
|
|
240
|
-
} else {
|
|
241
|
-
reject(import_errors.ProcessError.executionFailed(command, error.message));
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Execute command in background
|
|
248
|
-
*/
|
|
249
|
-
async executeInBackground(command, options) {
|
|
250
|
-
const runningCount = Array.from(this.backgroundProcesses.values()).filter(
|
|
251
|
-
(p) => p.status === "running"
|
|
252
|
-
).length;
|
|
253
|
-
if (runningCount >= this.config.maxConcurrentProcesses) {
|
|
254
|
-
throw import_errors.ProcessError.tooManyProcesses(runningCount, this.config.maxConcurrentProcesses);
|
|
255
|
-
}
|
|
256
|
-
const processId = crypto.randomBytes(4).toString("hex");
|
|
257
|
-
const cwd = this.resolveSafeCwd(options.cwd);
|
|
258
|
-
const env = {};
|
|
259
|
-
for (const [key, value] of Object.entries({
|
|
260
|
-
...process.env,
|
|
261
|
-
...this.config.environment,
|
|
262
|
-
...options.env
|
|
263
|
-
})) {
|
|
264
|
-
if (value !== void 0) {
|
|
265
|
-
env[key] = value;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
this.logger.debug(`Starting background process ${processId}: ${command}`);
|
|
269
|
-
const child = (0, import_node_child_process.spawn)(command, {
|
|
270
|
-
cwd,
|
|
271
|
-
env,
|
|
272
|
-
shell: true,
|
|
273
|
-
detached: false
|
|
274
|
-
});
|
|
275
|
-
const outputBuffer = {
|
|
276
|
-
stdout: [],
|
|
277
|
-
stderr: [],
|
|
278
|
-
complete: false,
|
|
279
|
-
lastRead: Date.now(),
|
|
280
|
-
bytesUsed: 0,
|
|
281
|
-
truncated: false
|
|
282
|
-
};
|
|
283
|
-
const bgProcess = {
|
|
284
|
-
processId,
|
|
285
|
-
command,
|
|
286
|
-
child,
|
|
287
|
-
startedAt: /* @__PURE__ */ new Date(),
|
|
288
|
-
status: "running",
|
|
289
|
-
outputBuffer,
|
|
290
|
-
description: options.description
|
|
291
|
-
};
|
|
292
|
-
this.backgroundProcesses.set(processId, bgProcess);
|
|
293
|
-
const bgTimeout = Math.max(
|
|
294
|
-
1,
|
|
295
|
-
Math.min(options.timeout || DEFAULT_TIMEOUT, this.config.maxTimeout)
|
|
296
|
-
);
|
|
297
|
-
const killTimer = setTimeout(() => {
|
|
298
|
-
if (bgProcess.status === "running") {
|
|
299
|
-
this.logger.warn(
|
|
300
|
-
`Background process ${processId} timed out after ${bgTimeout}ms, sending SIGTERM`
|
|
301
|
-
);
|
|
302
|
-
child.kill("SIGTERM");
|
|
303
|
-
setTimeout(() => {
|
|
304
|
-
if (bgProcess.status === "running") {
|
|
305
|
-
this.logger.warn(
|
|
306
|
-
`Background process ${processId} did not respond to SIGTERM, sending SIGKILL`
|
|
307
|
-
);
|
|
308
|
-
child.kill("SIGKILL");
|
|
309
|
-
}
|
|
310
|
-
}, 5e3);
|
|
311
|
-
}
|
|
312
|
-
}, bgTimeout);
|
|
313
|
-
child.stdout?.on("data", (data) => {
|
|
314
|
-
const chunk = data.toString();
|
|
315
|
-
const chunkBytes = Buffer.byteLength(chunk, "utf8");
|
|
316
|
-
if (outputBuffer.bytesUsed + chunkBytes <= this.config.maxOutputBuffer) {
|
|
317
|
-
outputBuffer.stdout.push(chunk);
|
|
318
|
-
outputBuffer.bytesUsed += chunkBytes;
|
|
319
|
-
} else {
|
|
320
|
-
if (!outputBuffer.truncated) {
|
|
321
|
-
outputBuffer.truncated = true;
|
|
322
|
-
this.logger.warn(`Output buffer full for process ${processId}`);
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
});
|
|
326
|
-
child.stderr?.on("data", (data) => {
|
|
327
|
-
const chunk = data.toString();
|
|
328
|
-
const chunkBytes = Buffer.byteLength(chunk, "utf8");
|
|
329
|
-
if (outputBuffer.bytesUsed + chunkBytes <= this.config.maxOutputBuffer) {
|
|
330
|
-
outputBuffer.stderr.push(chunk);
|
|
331
|
-
outputBuffer.bytesUsed += chunkBytes;
|
|
332
|
-
} else {
|
|
333
|
-
if (!outputBuffer.truncated) {
|
|
334
|
-
outputBuffer.truncated = true;
|
|
335
|
-
this.logger.warn(`Error buffer full for process ${processId}`);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
child.on("close", (code) => {
|
|
340
|
-
clearTimeout(killTimer);
|
|
341
|
-
bgProcess.status = code === 0 ? "completed" : "failed";
|
|
342
|
-
bgProcess.exitCode = code ?? void 0;
|
|
343
|
-
bgProcess.completedAt = /* @__PURE__ */ new Date();
|
|
344
|
-
bgProcess.outputBuffer.complete = true;
|
|
345
|
-
this.logger.debug(`Background process ${processId} completed with exit code ${code}`);
|
|
346
|
-
});
|
|
347
|
-
child.on("error", (error) => {
|
|
348
|
-
clearTimeout(killTimer);
|
|
349
|
-
bgProcess.status = "failed";
|
|
350
|
-
bgProcess.completedAt = /* @__PURE__ */ new Date();
|
|
351
|
-
bgProcess.outputBuffer.complete = true;
|
|
352
|
-
const chunk = `Error: ${error.message}`;
|
|
353
|
-
const chunkBytes = Buffer.byteLength(chunk, "utf8");
|
|
354
|
-
if (bgProcess.outputBuffer.bytesUsed + chunkBytes <= this.config.maxOutputBuffer) {
|
|
355
|
-
bgProcess.outputBuffer.stderr.push(chunk);
|
|
356
|
-
bgProcess.outputBuffer.bytesUsed += chunkBytes;
|
|
357
|
-
} else {
|
|
358
|
-
if (!bgProcess.outputBuffer.truncated) {
|
|
359
|
-
bgProcess.outputBuffer.truncated = true;
|
|
360
|
-
this.logger.warn(`Error buffer full for process ${processId}`);
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
this.logger.error(`Background process ${processId} failed: ${error.message}`);
|
|
364
|
-
});
|
|
365
|
-
return {
|
|
366
|
-
processId,
|
|
367
|
-
command,
|
|
368
|
-
pid: child.pid,
|
|
369
|
-
startedAt: bgProcess.startedAt,
|
|
370
|
-
description: options.description
|
|
371
|
-
};
|
|
372
|
-
}
|
|
373
|
-
/**
|
|
374
|
-
* Get output from a background process
|
|
375
|
-
*/
|
|
376
|
-
async getProcessOutput(processId) {
|
|
377
|
-
if (!this.initialized) {
|
|
378
|
-
throw import_errors.ProcessError.notInitialized();
|
|
379
|
-
}
|
|
380
|
-
const bgProcess = this.backgroundProcesses.get(processId);
|
|
381
|
-
if (!bgProcess) {
|
|
382
|
-
throw import_errors.ProcessError.processNotFound(processId);
|
|
383
|
-
}
|
|
384
|
-
const stdout = bgProcess.outputBuffer.stdout.join("");
|
|
385
|
-
const stderr = bgProcess.outputBuffer.stderr.join("");
|
|
386
|
-
bgProcess.outputBuffer.stdout = [];
|
|
387
|
-
bgProcess.outputBuffer.stderr = [];
|
|
388
|
-
bgProcess.outputBuffer.lastRead = Date.now();
|
|
389
|
-
bgProcess.outputBuffer.bytesUsed = 0;
|
|
390
|
-
return {
|
|
391
|
-
stdout,
|
|
392
|
-
stderr,
|
|
393
|
-
status: bgProcess.status,
|
|
394
|
-
exitCode: bgProcess.exitCode,
|
|
395
|
-
duration: bgProcess.completedAt ? bgProcess.completedAt.getTime() - bgProcess.startedAt.getTime() : void 0
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Kill a background process
|
|
400
|
-
*/
|
|
401
|
-
async killProcess(processId) {
|
|
402
|
-
if (!this.initialized) {
|
|
403
|
-
throw import_errors.ProcessError.notInitialized();
|
|
404
|
-
}
|
|
405
|
-
const bgProcess = this.backgroundProcesses.get(processId);
|
|
406
|
-
if (!bgProcess) {
|
|
407
|
-
throw import_errors.ProcessError.processNotFound(processId);
|
|
408
|
-
}
|
|
409
|
-
if (bgProcess.status !== "running") {
|
|
410
|
-
this.logger.debug(`Process ${processId} is not running (status: ${bgProcess.status})`);
|
|
411
|
-
return;
|
|
412
|
-
}
|
|
413
|
-
try {
|
|
414
|
-
bgProcess.child.kill("SIGTERM");
|
|
415
|
-
setTimeout(() => {
|
|
416
|
-
if (bgProcess.child.exitCode === null) {
|
|
417
|
-
bgProcess.child.kill("SIGKILL");
|
|
418
|
-
}
|
|
419
|
-
}, 5e3);
|
|
420
|
-
this.logger.debug(`Process ${processId} sent SIGTERM`);
|
|
421
|
-
} catch (error) {
|
|
422
|
-
throw import_errors.ProcessError.killFailed(
|
|
423
|
-
processId,
|
|
424
|
-
error instanceof Error ? error.message : String(error)
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* List all background processes
|
|
430
|
-
*/
|
|
431
|
-
async listProcesses() {
|
|
432
|
-
if (!this.initialized) {
|
|
433
|
-
throw import_errors.ProcessError.notInitialized();
|
|
434
|
-
}
|
|
435
|
-
return Array.from(this.backgroundProcesses.values()).map((bgProcess) => ({
|
|
436
|
-
processId: bgProcess.processId,
|
|
437
|
-
command: bgProcess.command,
|
|
438
|
-
pid: bgProcess.child.pid,
|
|
439
|
-
status: bgProcess.status,
|
|
440
|
-
startedAt: bgProcess.startedAt,
|
|
441
|
-
completedAt: bgProcess.completedAt,
|
|
442
|
-
exitCode: bgProcess.exitCode,
|
|
443
|
-
description: bgProcess.description
|
|
444
|
-
}));
|
|
445
|
-
}
|
|
446
|
-
/**
|
|
447
|
-
* Get buffer size in bytes
|
|
448
|
-
*/
|
|
449
|
-
getBufferSize(buffer) {
|
|
450
|
-
const stdoutSize = buffer.stdout.reduce((sum, line) => sum + line.length, 0);
|
|
451
|
-
const stderrSize = buffer.stderr.reduce((sum, line) => sum + line.length, 0);
|
|
452
|
-
return stdoutSize + stderrSize;
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Get service configuration
|
|
456
|
-
*/
|
|
457
|
-
getConfig() {
|
|
458
|
-
return { ...this.config };
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Resolve and confine cwd to the configured working directory
|
|
462
|
-
*/
|
|
463
|
-
resolveSafeCwd(cwd) {
|
|
464
|
-
const baseDir = this.config.workingDirectory || process.cwd();
|
|
465
|
-
if (!cwd) return baseDir;
|
|
466
|
-
const candidate = path.isAbsolute(cwd) ? path.resolve(cwd) : path.resolve(baseDir, cwd);
|
|
467
|
-
const rel = path.relative(baseDir, candidate);
|
|
468
|
-
const outside = rel.startsWith("..") || path.isAbsolute(rel);
|
|
469
|
-
if (outside) {
|
|
470
|
-
throw import_errors.ProcessError.invalidWorkingDirectory(
|
|
471
|
-
cwd,
|
|
472
|
-
`Working directory must be within ${baseDir}`
|
|
473
|
-
);
|
|
474
|
-
}
|
|
475
|
-
return candidate;
|
|
476
|
-
}
|
|
477
|
-
/**
|
|
478
|
-
* Cleanup completed processes
|
|
479
|
-
*/
|
|
480
|
-
async cleanup() {
|
|
481
|
-
const now = Date.now();
|
|
482
|
-
const CLEANUP_AGE = 36e5;
|
|
483
|
-
for (const [processId, bgProcess] of this.backgroundProcesses.entries()) {
|
|
484
|
-
if (bgProcess.status !== "running" && bgProcess.completedAt) {
|
|
485
|
-
const age = now - bgProcess.completedAt.getTime();
|
|
486
|
-
if (age > CLEANUP_AGE) {
|
|
487
|
-
this.backgroundProcesses.delete(processId);
|
|
488
|
-
this.logger.debug(`Cleaned up old process ${processId}`);
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
495
|
-
0 && (module.exports = {
|
|
496
|
-
ProcessService
|
|
497
|
-
});
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Process Service
|
|
3
|
-
*
|
|
4
|
-
* Secure command execution and process management for Dexto internal tools
|
|
5
|
-
*/
|
|
6
|
-
import { ProcessConfig, ExecuteOptions, ProcessResult, ProcessHandle, ProcessOutput, ProcessInfo } from './types.js';
|
|
7
|
-
import type { IDextoLogger } from '../logger/v2/types.js';
|
|
8
|
-
/**
|
|
9
|
-
* ProcessService - Handles command execution and process management
|
|
10
|
-
* TODO: Add tests for this class
|
|
11
|
-
*/
|
|
12
|
-
export declare class ProcessService {
|
|
13
|
-
private config;
|
|
14
|
-
private commandValidator;
|
|
15
|
-
private initialized;
|
|
16
|
-
private backgroundProcesses;
|
|
17
|
-
private logger;
|
|
18
|
-
constructor(config: Partial<ProcessConfig> | undefined, logger: IDextoLogger);
|
|
19
|
-
/**
|
|
20
|
-
* Initialize the service
|
|
21
|
-
*/
|
|
22
|
-
initialize(): Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* Execute a command
|
|
25
|
-
*/
|
|
26
|
-
executeCommand(command: string, options?: ExecuteOptions): Promise<ProcessResult | ProcessHandle>;
|
|
27
|
-
private static readonly SIGKILL_TIMEOUT_MS;
|
|
28
|
-
/**
|
|
29
|
-
* Kill a process tree (process group on Unix, taskkill on Windows)
|
|
30
|
-
*/
|
|
31
|
-
private killProcessTree;
|
|
32
|
-
/**
|
|
33
|
-
* Execute command in foreground with timeout and abort support
|
|
34
|
-
*/
|
|
35
|
-
private executeForeground;
|
|
36
|
-
/**
|
|
37
|
-
* Execute command in background
|
|
38
|
-
*/
|
|
39
|
-
private executeInBackground;
|
|
40
|
-
/**
|
|
41
|
-
* Get output from a background process
|
|
42
|
-
*/
|
|
43
|
-
getProcessOutput(processId: string): Promise<ProcessOutput>;
|
|
44
|
-
/**
|
|
45
|
-
* Kill a background process
|
|
46
|
-
*/
|
|
47
|
-
killProcess(processId: string): Promise<void>;
|
|
48
|
-
/**
|
|
49
|
-
* List all background processes
|
|
50
|
-
*/
|
|
51
|
-
listProcesses(): Promise<ProcessInfo[]>;
|
|
52
|
-
/**
|
|
53
|
-
* Get buffer size in bytes
|
|
54
|
-
*/
|
|
55
|
-
private getBufferSize;
|
|
56
|
-
/**
|
|
57
|
-
* Get service configuration
|
|
58
|
-
*/
|
|
59
|
-
getConfig(): Readonly<ProcessConfig>;
|
|
60
|
-
/**
|
|
61
|
-
* Resolve and confine cwd to the configured working directory
|
|
62
|
-
*/
|
|
63
|
-
private resolveSafeCwd;
|
|
64
|
-
/**
|
|
65
|
-
* Cleanup completed processes
|
|
66
|
-
*/
|
|
67
|
-
cleanup(): Promise<void>;
|
|
68
|
-
}
|
|
69
|
-
//# sourceMappingURL=process-service.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"process-service.d.ts","sourceRoot":"","sources":["../../src/process/process-service.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAKH,OAAO,EACH,aAAa,EACb,cAAc,EACd,aAAa,EACb,aAAa,EACb,aAAa,EACb,WAAW,EAEd,MAAM,YAAY,CAAC;AAGpB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAuB1D;;;GAGG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,gBAAgB,CAAmB;IAC3C,OAAO,CAAC,WAAW,CAAkB;IACrC,OAAO,CAAC,mBAAmB,CAA6C;IACxE,OAAO,CAAC,MAAM,CAAe;gBAEjB,MAAM,EAAE,OAAO,CAAC,aAAa,CAAC,YAAK,EAAE,MAAM,EAAE,YAAY;IAkBrE;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAajC;;OAEG;IACG,cAAc,CAChB,OAAO,EAAE,MAAM,EACf,OAAO,GAAE,cAAmB,GAC7B,OAAO,CAAC,aAAa,GAAG,aAAa,CAAC;IAuDzC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,kBAAkB,CAAO;IAEjD;;OAEG;YACW,eAAe;IA6B7B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAsIzB;;OAEG;YACW,mBAAmB;IAiKjC;;OAEG;IACG,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IA+BjE;;OAEG;IACG,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAmCnD;;OAEG;IACG,aAAa,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAiB7C;;OAEG;IACH,OAAO,CAAC,aAAa;IAMrB;;OAEG;IACH,SAAS,IAAI,QAAQ,CAAC,aAAa,CAAC;IAIpC;;OAEG;IACH,OAAO,CAAC,cAAc;IAetB;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAcjC"}
|