aem-ext-daemon 0.3.9 → 0.4.1
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/capabilities/shell.d.ts +13 -0
- package/dist/capabilities/shell.js +154 -0
- package/dist/dispatcher.js +9 -0
- package/package.json +1 -1
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* aio:run streams stdout/stderr back through the WebSocket.
|
|
5
5
|
* shell:exec runs a command and returns the full output.
|
|
6
|
+
* shell:spawn starts an interactive process (for commands that need user input).
|
|
7
|
+
* shell:stdin sends input to the active interactive process.
|
|
6
8
|
*/
|
|
7
9
|
import type { DaemonConnection } from "../connection.js";
|
|
8
10
|
/** Run a shell command synchronously and return stdout + stderr. */
|
|
@@ -15,3 +17,14 @@ export declare function checkAio(): string;
|
|
|
15
17
|
* then returns the exit code.
|
|
16
18
|
*/
|
|
17
19
|
export declare function runAio(args: string[], cwd: string, requestId: string, connection: DaemonConnection): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Start an interactive command. Collects output until the process goes
|
|
22
|
+
* quiet (waiting for user input) or exits, then returns what it has.
|
|
23
|
+
* Does NOT set CI=true or TERM=dumb so prompts render normally.
|
|
24
|
+
*/
|
|
25
|
+
export declare function spawnInteractive(command: string, cwd: string): Promise<string>;
|
|
26
|
+
/**
|
|
27
|
+
* Send input to the active interactive process. Waits for the process
|
|
28
|
+
* to produce more output (or go quiet again), then returns the new output.
|
|
29
|
+
*/
|
|
30
|
+
export declare function writeToProcess(input: string): Promise<string>;
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
*
|
|
4
4
|
* aio:run streams stdout/stderr back through the WebSocket.
|
|
5
5
|
* shell:exec runs a command and returns the full output.
|
|
6
|
+
* shell:spawn starts an interactive process (for commands that need user input).
|
|
7
|
+
* shell:stdin sends input to the active interactive process.
|
|
6
8
|
*/
|
|
7
9
|
import { execSync, spawn } from "node:child_process";
|
|
8
10
|
import fs from "node:fs";
|
|
@@ -143,3 +145,155 @@ export function runAio(args, cwd, requestId, connection) {
|
|
|
143
145
|
}, 300_000);
|
|
144
146
|
});
|
|
145
147
|
}
|
|
148
|
+
// ─── Interactive process support ────────────────────────────
|
|
149
|
+
// Allows commands that need user input (like `aio app create`)
|
|
150
|
+
// to run through the chat UI.
|
|
151
|
+
/** The single active interactive process (one per daemon). */
|
|
152
|
+
let activeProcess = null;
|
|
153
|
+
/** How long to wait for more output before assuming the process is waiting for input. */
|
|
154
|
+
const SILENCE_TIMEOUT = 3_000;
|
|
155
|
+
/** Strip ANSI escape codes from PTY output for clean text. */
|
|
156
|
+
function stripAnsi(text) {
|
|
157
|
+
// eslint-disable-next-line no-control-regex
|
|
158
|
+
return text.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, "")
|
|
159
|
+
.replace(/\r\n/g, "\n")
|
|
160
|
+
.replace(/\r/g, "\n");
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Start an interactive command. Collects output until the process goes
|
|
164
|
+
* quiet (waiting for user input) or exits, then returns what it has.
|
|
165
|
+
* Does NOT set CI=true or TERM=dumb so prompts render normally.
|
|
166
|
+
*/
|
|
167
|
+
export function spawnInteractive(command, cwd) {
|
|
168
|
+
// Kill any existing interactive process
|
|
169
|
+
if (activeProcess && !activeProcess.done) {
|
|
170
|
+
activeProcess.child.kill("SIGTERM");
|
|
171
|
+
}
|
|
172
|
+
activeProcess = null;
|
|
173
|
+
if (!fs.existsSync(cwd)) {
|
|
174
|
+
fs.mkdirSync(cwd, { recursive: true });
|
|
175
|
+
}
|
|
176
|
+
return new Promise((resolve) => {
|
|
177
|
+
// Use `script` to create a pseudo-TTY so inquirer-based CLIs
|
|
178
|
+
// (like aio app create) render their prompts properly.
|
|
179
|
+
// macOS: script -q /dev/null sh -c "<command>"
|
|
180
|
+
// Linux: script -qec "<command>" /dev/null
|
|
181
|
+
const isMac = os.platform() === "darwin";
|
|
182
|
+
const spawnCmd = isMac ? "script" : "script";
|
|
183
|
+
const spawnArgs = isMac
|
|
184
|
+
? ["-q", "/dev/null", "sh", "-c", command]
|
|
185
|
+
: ["-qec", command, "/dev/null"];
|
|
186
|
+
const child = spawn(spawnCmd, spawnArgs, {
|
|
187
|
+
cwd,
|
|
188
|
+
env: {
|
|
189
|
+
...getEnhancedEnv(),
|
|
190
|
+
// Do NOT set CI or TERM=dumb — we want interactive prompts
|
|
191
|
+
TERM: "xterm-256color",
|
|
192
|
+
COLUMNS: "120",
|
|
193
|
+
LINES: "40",
|
|
194
|
+
},
|
|
195
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
196
|
+
});
|
|
197
|
+
const proc = { child, buffer: "", done: false, exitCode: null };
|
|
198
|
+
activeProcess = proc;
|
|
199
|
+
let silenceTimer;
|
|
200
|
+
const flushAndReturn = () => {
|
|
201
|
+
const output = stripAnsi(proc.buffer);
|
|
202
|
+
proc.buffer = "";
|
|
203
|
+
resolve(output || "(no output)");
|
|
204
|
+
};
|
|
205
|
+
const resetTimer = () => {
|
|
206
|
+
clearTimeout(silenceTimer);
|
|
207
|
+
silenceTimer = setTimeout(() => {
|
|
208
|
+
if (!proc.done) {
|
|
209
|
+
// Process went quiet — probably waiting for input
|
|
210
|
+
flushAndReturn();
|
|
211
|
+
}
|
|
212
|
+
}, SILENCE_TIMEOUT);
|
|
213
|
+
};
|
|
214
|
+
child.stdout?.on("data", (chunk) => {
|
|
215
|
+
proc.buffer += chunk.toString();
|
|
216
|
+
resetTimer();
|
|
217
|
+
});
|
|
218
|
+
child.stderr?.on("data", (chunk) => {
|
|
219
|
+
proc.buffer += chunk.toString();
|
|
220
|
+
resetTimer();
|
|
221
|
+
});
|
|
222
|
+
child.on("close", (code) => {
|
|
223
|
+
proc.done = true;
|
|
224
|
+
proc.exitCode = code;
|
|
225
|
+
clearTimeout(silenceTimer);
|
|
226
|
+
proc.buffer += `\n[Process exited with code ${code ?? 1}]`;
|
|
227
|
+
flushAndReturn();
|
|
228
|
+
});
|
|
229
|
+
child.on("error", (err) => {
|
|
230
|
+
proc.done = true;
|
|
231
|
+
clearTimeout(silenceTimer);
|
|
232
|
+
proc.buffer += `\n[Error: ${err.message}]`;
|
|
233
|
+
flushAndReturn();
|
|
234
|
+
});
|
|
235
|
+
// Start the silence timer
|
|
236
|
+
resetTimer();
|
|
237
|
+
// Overall timeout: 5 minutes
|
|
238
|
+
setTimeout(() => {
|
|
239
|
+
if (!proc.done) {
|
|
240
|
+
child.kill("SIGTERM");
|
|
241
|
+
proc.done = true;
|
|
242
|
+
proc.buffer += "\n[Timed out after 5 minutes]";
|
|
243
|
+
flushAndReturn();
|
|
244
|
+
}
|
|
245
|
+
}, 300_000);
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Send input to the active interactive process. Waits for the process
|
|
250
|
+
* to produce more output (or go quiet again), then returns the new output.
|
|
251
|
+
*/
|
|
252
|
+
export function writeToProcess(input) {
|
|
253
|
+
if (!activeProcess || activeProcess.done) {
|
|
254
|
+
return Promise.resolve(activeProcess
|
|
255
|
+
? `[Process already exited with code ${activeProcess.exitCode}]`
|
|
256
|
+
: "[No interactive process running. Use interactive_bash to start one.]");
|
|
257
|
+
}
|
|
258
|
+
const proc = activeProcess;
|
|
259
|
+
return new Promise((resolve) => {
|
|
260
|
+
let silenceTimer;
|
|
261
|
+
let resolved = false;
|
|
262
|
+
const finish = (output) => {
|
|
263
|
+
if (resolved)
|
|
264
|
+
return;
|
|
265
|
+
resolved = true;
|
|
266
|
+
clearTimeout(silenceTimer);
|
|
267
|
+
proc.child.stdout?.off("data", onData);
|
|
268
|
+
proc.child.stderr?.off("data", onData);
|
|
269
|
+
proc.child.off("close", onClose);
|
|
270
|
+
resolve(output);
|
|
271
|
+
};
|
|
272
|
+
const resetTimer = () => {
|
|
273
|
+
clearTimeout(silenceTimer);
|
|
274
|
+
silenceTimer = setTimeout(() => {
|
|
275
|
+
if (!proc.done) {
|
|
276
|
+
const output = stripAnsi(proc.buffer);
|
|
277
|
+
proc.buffer = "";
|
|
278
|
+
finish(output || "(waiting for input...)");
|
|
279
|
+
}
|
|
280
|
+
}, SILENCE_TIMEOUT);
|
|
281
|
+
};
|
|
282
|
+
const onData = () => resetTimer();
|
|
283
|
+
const onClose = () => {
|
|
284
|
+
// Small delay to capture any final output
|
|
285
|
+
setTimeout(() => {
|
|
286
|
+
const output = stripAnsi(proc.buffer);
|
|
287
|
+
proc.buffer = "";
|
|
288
|
+
finish(output || "(no output)");
|
|
289
|
+
}, 200);
|
|
290
|
+
};
|
|
291
|
+
proc.child.stdout?.on("data", onData);
|
|
292
|
+
proc.child.stderr?.on("data", onData);
|
|
293
|
+
proc.child.once("close", onClose);
|
|
294
|
+
// Write the input
|
|
295
|
+
proc.child.stdin?.write(input + "\n");
|
|
296
|
+
// Start silence timer
|
|
297
|
+
resetTimer();
|
|
298
|
+
});
|
|
299
|
+
}
|
package/dist/dispatcher.js
CHANGED
|
@@ -113,6 +113,15 @@ async function dispatchInner(command, payload, connection) {
|
|
|
113
113
|
: getWorkspaceRoot();
|
|
114
114
|
return shellCap.exec(payload.command, cwd);
|
|
115
115
|
}
|
|
116
|
+
case "shell:spawn": {
|
|
117
|
+
const cwd = payload.cwd
|
|
118
|
+
? validatePath(payload.cwd)
|
|
119
|
+
: getWorkspaceRoot();
|
|
120
|
+
return shellCap.spawnInteractive(payload.command, cwd);
|
|
121
|
+
}
|
|
122
|
+
case "shell:stdin": {
|
|
123
|
+
return shellCap.writeToProcess(payload.input);
|
|
124
|
+
}
|
|
116
125
|
// ─── Skills ────────────────────────────────────────
|
|
117
126
|
case "skills:sync": {
|
|
118
127
|
const workspace = getWorkspaceRoot();
|