@moxxy/plugin-terminal 0.42.0 → 0.42.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/pty.d.ts +19 -1
- package/dist/pty.d.ts.map +1 -1
- package/dist/pty.js +52 -7
- package/dist/pty.js.map +1 -1
- package/dist/shell-hooks.d.ts +37 -0
- package/dist/shell-hooks.d.ts.map +1 -0
- package/dist/shell-hooks.js +113 -0
- package/dist/shell-hooks.js.map +1 -0
- package/dist/terminal.d.ts +9 -8
- package/dist/terminal.d.ts.map +1 -1
- package/dist/terminal.js +64 -16
- package/dist/terminal.js.map +1 -1
- package/package.json +4 -4
- package/src/pty.ts +60 -6
- package/src/shell-hooks.test.ts +65 -0
- package/src/shell-hooks.ts +139 -0
- package/src/terminal.shell.test.ts +76 -0
- package/src/terminal.test.ts +158 -0
- package/src/terminal.ts +67 -17
package/src/terminal.test.ts
CHANGED
|
@@ -286,6 +286,164 @@ describe('runCommand sentinel detection (tail-scan, single RegExp compile)', ()
|
|
|
286
286
|
});
|
|
287
287
|
});
|
|
288
288
|
|
|
289
|
+
/** A fake terminal whose shell reports its commands and each return to its prompt. */
|
|
290
|
+
function promptingTerminal(): ReturnType<typeof fakeTerminal> & {
|
|
291
|
+
/** The shell starts a command it was typed. */
|
|
292
|
+
startCommand(): void;
|
|
293
|
+
/** The shell is back at its prompt. */
|
|
294
|
+
showPrompt(exitCode: number): void;
|
|
295
|
+
} {
|
|
296
|
+
const listeners = new Set<(exitCode: number, commands: number) => void>();
|
|
297
|
+
let seen = false;
|
|
298
|
+
let commands = 0;
|
|
299
|
+
return Object.assign(fakeTerminal(), {
|
|
300
|
+
prompt: {
|
|
301
|
+
get seen() {
|
|
302
|
+
return seen;
|
|
303
|
+
},
|
|
304
|
+
get commands() {
|
|
305
|
+
return commands;
|
|
306
|
+
},
|
|
307
|
+
onPrompt: (cb: (exitCode: number, commands: number) => void) => {
|
|
308
|
+
listeners.add(cb);
|
|
309
|
+
return () => listeners.delete(cb);
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
startCommand: () => {
|
|
313
|
+
commands += 1;
|
|
314
|
+
},
|
|
315
|
+
showPrompt: (exitCode: number) => {
|
|
316
|
+
seen = true;
|
|
317
|
+
for (const cb of [...listeners]) cb(exitCode, commands);
|
|
318
|
+
},
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
describe('runCommand in a shell that reports its prompt', () => {
|
|
323
|
+
it('types the command alone and finishes when the prompt is back', async () => {
|
|
324
|
+
const term = promptingTerminal();
|
|
325
|
+
term.showPrompt(0);
|
|
326
|
+
const p = runCommand(term, 'npm test', '__MOXXY_DONE_hook_0__', 5_000);
|
|
327
|
+
|
|
328
|
+
expect(term.writes).toEqual(['npm test\n']);
|
|
329
|
+
term.startCommand();
|
|
330
|
+
term.feed('npm test\n2 failed\n');
|
|
331
|
+
term.showPrompt(1);
|
|
332
|
+
|
|
333
|
+
const res = await p;
|
|
334
|
+
expect(res).toEqual({ output: '2 failed', exitCode: 1, timedOut: false });
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('is not ended by a prompt drawn again before its command started', async () => {
|
|
338
|
+
const term = promptingTerminal();
|
|
339
|
+
term.showPrompt(0);
|
|
340
|
+
let done = false;
|
|
341
|
+
const p = runCommand(term, 'npm test', '__MOXXY_DONE_hook_4__', 5_000).then((res) => {
|
|
342
|
+
done = true;
|
|
343
|
+
return res;
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
// The user pressed Enter on an empty line while the command was on its way.
|
|
347
|
+
term.showPrompt(0);
|
|
348
|
+
await Promise.resolve();
|
|
349
|
+
expect(done).toBe(false);
|
|
350
|
+
|
|
351
|
+
term.startCommand();
|
|
352
|
+
term.showPrompt(2);
|
|
353
|
+
expect((await p).exitCode).toBe(2);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('waits out a command the user is running before counting its own', async () => {
|
|
357
|
+
const term = promptingTerminal();
|
|
358
|
+
term.showPrompt(0);
|
|
359
|
+
term.startCommand(); // typed in the pane by the user, still running
|
|
360
|
+
let done = false;
|
|
361
|
+
const p = runCommand(term, 'ls', '__MOXXY_DONE_hook_5__', 5_000).then((res) => {
|
|
362
|
+
done = true;
|
|
363
|
+
return res;
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
term.showPrompt(9); // the user's command ends
|
|
367
|
+
await Promise.resolve();
|
|
368
|
+
expect(done).toBe(false);
|
|
369
|
+
|
|
370
|
+
term.startCommand();
|
|
371
|
+
term.showPrompt(0);
|
|
372
|
+
expect((await p).exitCode).toBe(0);
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
it('ends a blank command at the next prompt, since the shell starts nothing for it', async () => {
|
|
376
|
+
const term = promptingTerminal();
|
|
377
|
+
term.showPrompt(0);
|
|
378
|
+
const p = runCommand(term, ' ', '__MOXXY_DONE_hook_6__', 5_000);
|
|
379
|
+
|
|
380
|
+
term.showPrompt(0);
|
|
381
|
+
expect((await p).timedOut).toBe(false);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
it('waits for a shell that is still starting before typing', async () => {
|
|
385
|
+
const term = promptingTerminal();
|
|
386
|
+
const p = runCommand(term, 'ls', '__MOXXY_DONE_hook_1__', 5_000);
|
|
387
|
+
|
|
388
|
+
expect(term.writes).toEqual([]);
|
|
389
|
+
term.showPrompt(0);
|
|
390
|
+
await waitFor(() => term.writes.length > 0);
|
|
391
|
+
expect(term.writes).toEqual(['ls\n']);
|
|
392
|
+
term.startCommand();
|
|
393
|
+
term.feed('a.txt\n');
|
|
394
|
+
term.showPrompt(0);
|
|
395
|
+
|
|
396
|
+
expect((await p).output).toBe('a.txt');
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('types its own end marker when the shell never reports a prompt', async () => {
|
|
400
|
+
vi.useFakeTimers();
|
|
401
|
+
try {
|
|
402
|
+
const term = promptingTerminal();
|
|
403
|
+
const marker = '__MOXXY_DONE_hook_2__';
|
|
404
|
+
const p = runCommand(term, 'ls', marker, 60_000);
|
|
405
|
+
|
|
406
|
+
await vi.advanceTimersByTimeAsync(10_000);
|
|
407
|
+
expect(term.writes.join('')).toContain(`"${marker}" "$?"`);
|
|
408
|
+
term.feed(`a.txt\n${marker} 0\n`);
|
|
409
|
+
|
|
410
|
+
expect((await p).exitCode).toBe(0);
|
|
411
|
+
} finally {
|
|
412
|
+
vi.useRealTimers();
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('does not wait again for a shell that showed no prompt the first time', async () => {
|
|
417
|
+
vi.useFakeTimers();
|
|
418
|
+
try {
|
|
419
|
+
const term = promptingTerminal();
|
|
420
|
+
const first = runCommand(term, 'ls', '__MOXXY_DONE_hook_7__', 60_000);
|
|
421
|
+
await vi.advanceTimersByTimeAsync(10_000);
|
|
422
|
+
term.feed('__MOXXY_DONE_hook_7__ 0\n');
|
|
423
|
+
await first;
|
|
424
|
+
|
|
425
|
+
const second = runCommand(term, 'pwd', '__MOXXY_DONE_hook_8__', 60_000);
|
|
426
|
+
await vi.advanceTimersByTimeAsync(0);
|
|
427
|
+
expect(term.writes.join('')).toContain('"__MOXXY_DONE_hook_8__" "$?"');
|
|
428
|
+
term.feed('__MOXXY_DONE_hook_8__ 0\n');
|
|
429
|
+
await second;
|
|
430
|
+
} finally {
|
|
431
|
+
vi.useRealTimers();
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it.skipIf(process.platform === 'win32')('runs a command that ends in a backslash from a file, so the shell is not left waiting', async () => {
|
|
436
|
+
const term = promptingTerminal();
|
|
437
|
+
term.showPrompt(0);
|
|
438
|
+
const p = runCommand(term, 'echo hi \\', '__MOXXY_DONE_hook_3__', 5_000);
|
|
439
|
+
|
|
440
|
+
expect(term.writes[0]).toMatch(/^\. '[^']+'\n$/);
|
|
441
|
+
term.startCommand();
|
|
442
|
+
term.showPrompt(0);
|
|
443
|
+
await p;
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
|
|
289
447
|
describe.skipIf(process.platform === 'win32')('runCommand with a long or multi-line command', () => {
|
|
290
448
|
it('types one short line that runs the command from a file, and removes the file when done', async () => {
|
|
291
449
|
const term = fakeTerminal();
|
package/src/terminal.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { randomBytes } from 'node:crypto';
|
|
|
2
2
|
import { rmSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { defineSurface, defineTool, z, type SurfaceInstance } from '@moxxy/sdk';
|
|
6
|
-
import { createTerminalProcess, type TerminalProcess } from './pty.js';
|
|
5
|
+
import { defineSurface, defineTool, waitFor, z, type SurfaceInstance } from '@moxxy/sdk';
|
|
6
|
+
import { createTerminalProcess, type PromptSignal, type TerminalProcess } from './pty.js';
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Shared terminal processes, keyed by cwd. The surface and the `terminal` tool
|
|
@@ -191,11 +191,11 @@ const terminalInputSchema = z.object({
|
|
|
191
191
|
});
|
|
192
192
|
|
|
193
193
|
/**
|
|
194
|
-
* The `terminal` tool runs a command in the SHARED terminal the user sees
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
*
|
|
198
|
-
* can take over at any time.
|
|
194
|
+
* The `terminal` tool runs a command in the SHARED terminal the user sees and
|
|
195
|
+
* reads its output until the shell is back at its prompt. zsh and bash report
|
|
196
|
+
* that themselves (shell-hooks.ts); any other shell is typed a unique sentinel
|
|
197
|
+
* that echoes the exit code. The command and its output stay visible to the
|
|
198
|
+
* user, who can take over at any time.
|
|
199
199
|
*/
|
|
200
200
|
export function buildTerminalTool() {
|
|
201
201
|
return defineTool({
|
|
@@ -274,9 +274,35 @@ const MAX_ACC = 1_000_000;
|
|
|
274
274
|
const commandTails = new WeakMap<TerminalProcess, Promise<void>>();
|
|
275
275
|
|
|
276
276
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
|
|
277
|
+
* How long a starting shell gets to show its first prompt before its commands
|
|
278
|
+
* are typed with a sentinel instead (its own start files replaced the hook).
|
|
279
|
+
*/
|
|
280
|
+
const PROMPT_REPORT_DEADLINE_MS = 5_000;
|
|
281
|
+
|
|
282
|
+
/** Shells that showed no prompt before the deadline: their later commands do not wait for one again. */
|
|
283
|
+
const silent = new WeakSet<TerminalProcess>();
|
|
284
|
+
|
|
285
|
+
/** Resolves once the shell has shown a prompt; false when it reports none in time. */
|
|
286
|
+
async function reportsPrompt(
|
|
287
|
+
proc: TerminalProcess,
|
|
288
|
+
prompt: PromptSignal,
|
|
289
|
+
signal: AbortSignal | undefined,
|
|
290
|
+
): Promise<boolean> {
|
|
291
|
+
if (silent.has(proc)) return false;
|
|
292
|
+
const outcome = await waitFor(() => (prompt.seen ? true : undefined), {
|
|
293
|
+
wakeOn: [(wake) => prompt.onPrompt(wake)],
|
|
294
|
+
timeoutMs: PROMPT_REPORT_DEADLINE_MS,
|
|
295
|
+
signal,
|
|
296
|
+
});
|
|
297
|
+
if (outcome.status === 'timeout') silent.add(proc);
|
|
298
|
+
return outcome.status === 'ready';
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Write `command` to a shared shell and collect output until the shell reports
|
|
303
|
+
* its prompt, or, in a shell that reports none, until the sentinel `printf`
|
|
304
|
+
* typed after the command prints. Returns the captured output (best-effort
|
|
305
|
+
* stripped of the echoed command) + the exit code.
|
|
280
306
|
*
|
|
281
307
|
* Serialized per shared process: a command writes only once the previous one on
|
|
282
308
|
* the same shell finishes, so concurrent callers never interleave their writes
|
|
@@ -329,6 +355,7 @@ function runCommandSerialized(
|
|
|
329
355
|
settled = true;
|
|
330
356
|
unsubData();
|
|
331
357
|
unsubExit();
|
|
358
|
+
unsubPrompt();
|
|
332
359
|
clearTimeout(timer);
|
|
333
360
|
if (onAbort) signal?.removeEventListener('abort', onAbort);
|
|
334
361
|
typed?.remove();
|
|
@@ -424,6 +451,7 @@ function runCommandSerialized(
|
|
|
424
451
|
// immediately with the shell's exit code rather than hanging to the full
|
|
425
452
|
// timeout (up to 600s) on a known-dead process.
|
|
426
453
|
const unsubExit = proc.onExit((code) => finish(code, false));
|
|
454
|
+
let unsubPrompt = (): void => {};
|
|
427
455
|
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
428
456
|
// Two writes to the interactive shell: the command, then the sentinel whose
|
|
429
457
|
// $? reflects the command's exit. A leading newline before the printf
|
|
@@ -434,7 +462,7 @@ function runCommandSerialized(
|
|
|
434
462
|
// `preexec` setting the window title) print right before it, and the
|
|
435
463
|
// sentinel is only recognized at the start of a line. On a non-PTY pipe the
|
|
436
464
|
// shell still runs both sequentially.
|
|
437
|
-
const writeAndArm = (): void => {
|
|
465
|
+
const writeAndArm = (hooked: boolean): void => {
|
|
438
466
|
if (settled) return; // shell died (or we were torn down) before our turn
|
|
439
467
|
// The shell is ALREADY dead at write time (it exited before our turn, or
|
|
440
468
|
// in the TOCTOU window between getSharedTerminal returning it and now).
|
|
@@ -448,14 +476,34 @@ function runCommandSerialized(
|
|
|
448
476
|
}
|
|
449
477
|
timer = setTimeout(() => finish(null, true), timeoutMs);
|
|
450
478
|
typed = typeable(command, marker);
|
|
479
|
+
if (hooked && proc.prompt) {
|
|
480
|
+
// Nothing follows the command on the terminal's input, where the
|
|
481
|
+
// program it starts could read it: the shell says when it is done. A
|
|
482
|
+
// prompt drawn again for anything else (an empty Enter, a Ctrl-C, the
|
|
483
|
+
// end of a command the user typed) started no command since, so it is
|
|
484
|
+
// not this one's; a blank line starts none, and any prompt ends it.
|
|
485
|
+
const before = proc.prompt.commands;
|
|
486
|
+
const blank = command.trim() === '';
|
|
487
|
+
unsubPrompt = proc.prompt.onPrompt((exitCode, commands) => {
|
|
488
|
+
if (blank || commands > before) finish(exitCode, false);
|
|
489
|
+
});
|
|
490
|
+
proc.write(`${typed.line}\n`);
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
451
493
|
proc.write(`${typed.line}\n`);
|
|
452
494
|
proc.write(`\n${sentinelPrintf(marker)}\n`);
|
|
453
495
|
};
|
|
454
|
-
//
|
|
455
|
-
//
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
496
|
+
// A shell with no prompt report, or one that has shown its prompt already,
|
|
497
|
+
// is decided on the spot, which keeps the single-command path synchronous.
|
|
498
|
+
const start = (): void => {
|
|
499
|
+
if (!proc.prompt || proc.prompt.seen) writeAndArm(proc.prompt !== undefined);
|
|
500
|
+
else void reportsPrompt(proc, proc.prompt, signal).then(writeAndArm);
|
|
501
|
+
};
|
|
502
|
+
// Idle shell → write now. Busy shell → wait our turn; the timeout clock only
|
|
503
|
+
// starts when we write, so a command queued behind a slow predecessor
|
|
504
|
+
// doesn't burn its budget waiting.
|
|
505
|
+
if (startWrites) void startWrites.then(start);
|
|
506
|
+
else start();
|
|
459
507
|
});
|
|
460
508
|
}
|
|
461
509
|
|
|
@@ -475,7 +523,9 @@ interface TypedCommand {
|
|
|
475
523
|
}
|
|
476
524
|
|
|
477
525
|
function typeable(command: string, marker: string): TypedCommand {
|
|
478
|
-
|
|
526
|
+
// A trailing backslash joins the next typed line to the command; in a file it ends with it.
|
|
527
|
+
const oneShortLine = !command.includes('\n') && command.length <= TYPED_LIMIT && !command.endsWith('\\');
|
|
528
|
+
if (process.platform === 'win32' || oneShortLine) {
|
|
479
529
|
return { line: command, remove: () => {} };
|
|
480
530
|
}
|
|
481
531
|
const file = join(tmpdir(), `moxxy-command-${marker}.sh`);
|