@noldova/terminal 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +10 -2
- package/menus.json +53 -0
- package/module.json +28 -0
- package/package.json +50 -4
- package/settings.json +83 -0
- package/src/api/index.d.ts +131 -0
- package/src/api/index.js +11 -0
- package/src/api/window.d.ts +467 -0
- package/src/api/window.js +8 -0
- package/src/enums/escape-state.js +15 -0
- package/src/enums/escape-stripper-state.js +15 -0
- package/src/enums/mark-kind.js +18 -0
- package/src/enums/shell-family.js +14 -0
- package/src/enums/system-requirement.js +11 -0
- package/src/exceptions/agent-terminal-restart.exception.js +16 -0
- package/src/exceptions/command-exited.exception.js +23 -0
- package/src/exceptions/command-failed.exception.js +13 -0
- package/src/exceptions/command-not-started.exception.js +17 -0
- package/src/exceptions/command-output-too-long.exception.js +18 -0
- package/src/exceptions/command-timed-out.exception.js +18 -0
- package/src/exceptions/command-variable-not-set.exception.js +18 -0
- package/src/exceptions/default-shell-not-found.exception.js +14 -0
- package/src/exceptions/environment-export-invalid.exception.js +14 -0
- package/src/exceptions/environment-key-unreadable.exception.js +17 -0
- package/src/exceptions/environment-unreadable.exception.js +13 -0
- package/src/exceptions/folder-not-found.exception.js +16 -0
- package/src/exceptions/host-ended.exception.js +14 -0
- package/src/exceptions/session-not-found.exception.js +18 -0
- package/src/exceptions/shell-kind-not-found.exception.js +16 -0
- package/src/exceptions/shell-not-found.exception.js +18 -0
- package/src/exceptions/shells-unreadable.exception.js +13 -0
- package/src/exceptions/stored-lines-damaged.exception.js +15 -0
- package/src/exceptions/stored-lines-unwritable.exception.js +15 -0
- package/src/exceptions/system-root-missing.exception.js +14 -0
- package/src/exceptions/terminal.exception.js +13 -0
- package/src/exceptions/unknown-method.exception.js +16 -0
- package/src/exceptions/unsupported-system.exception.js +18 -0
- package/src/exceptions/visual-studio-unreadable.exception.js +15 -0
- package/src/exceptions/wsl-distributions-unreadable.exception.js +15 -0
- package/src/interfaces/i-command-runner.js +8 -0
- package/src/interfaces/i-disposable.js +8 -0
- package/src/interfaces/i-environment-source.js +8 -0
- package/src/interfaces/i-file-system-reader.js +8 -0
- package/src/interfaces/i-file-system-writer.js +8 -0
- package/src/interfaces/i-flow-controller-listener.js +8 -0
- package/src/interfaces/i-folder-access.js +8 -0
- package/src/interfaces/i-line-process-listener.js +8 -0
- package/src/interfaces/i-line-process-starter.js +8 -0
- package/src/interfaces/i-line-process.js +8 -0
- package/src/interfaces/i-output-batcher-listener.js +8 -0
- package/src/interfaces/i-process-signaller.js +8 -0
- package/src/interfaces/i-pseudo-terminal-listener.js +8 -0
- package/src/interfaces/i-pty-exit-event.js +8 -0
- package/src/interfaces/i-pty-process.js +8 -0
- package/src/interfaces/i-pty-spawner.js +8 -0
- package/src/interfaces/i-terminal-mirror-listener.js +8 -0
- package/src/interfaces/i-terminal-session-listener.js +8 -0
- package/src/interfaces/i-waiting-options-request.js +8 -0
- package/src/models/command-listing.js +17 -0
- package/src/models/completion-candidate.js +19 -0
- package/src/models/environment-tools.js +23 -0
- package/src/models/environment-variable.js +15 -0
- package/src/models/folder-entry.js +15 -0
- package/src/models/hosted-terminal.js +116 -0
- package/src/models/mirror-line.js +27 -0
- package/src/models/mirror-snapshot.js +19 -0
- package/src/models/one-shot-launch.js +42 -0
- package/src/models/pane-progress.js +15 -0
- package/src/models/powershell-cmdlet.js +21 -0
- package/src/models/powershell-lookup.js +17 -0
- package/src/models/powershell-parameter.js +19 -0
- package/src/models/registry-value.js +15 -0
- package/src/models/request-client.js +23 -0
- package/src/models/saved-terminal.js +25 -0
- package/src/models/shell-launch.js +23 -0
- package/src/models/shell-line.js +21 -0
- package/src/models/shell-mark.js +19 -0
- package/src/models/shell-options-reply.js +17 -0
- package/src/models/shell.js +29 -0
- package/src/models/started-shell.js +21 -0
- package/src/models/system-shortfall.js +15 -0
- package/src/models/terminal-events.js +37 -0
- package/src/models/terminal-history-paths.js +15 -0
- package/src/models/terminal-prompt.js +28 -0
- package/src/models/tracked-command.js +26 -0
- package/src/models/typed-line.js +40 -0
- package/src/models/visual-studio-installation.js +22 -0
- package/src/models/waiting-run.js +25 -0
- package/src/resources.js +1725 -0
- package/src/runtime-part.js +84 -0
- package/src/services/agent-terminal.js +137 -0
- package/src/services/agent-terminals.js +55 -0
- package/src/services/bounded-reads.js +57 -0
- package/src/services/change-signal.js +33 -0
- package/src/services/command-history.js +20 -0
- package/src/services/command-runner.js +67 -0
- package/src/services/command-tracker.js +161 -0
- package/src/services/completion-source.js +316 -0
- package/src/services/context-pty-process.js +79 -0
- package/src/services/context-pty-spawner.js +26 -0
- package/src/services/deferred-pty-process.js +94 -0
- package/src/services/escape-sequence-tracker.js +126 -0
- package/src/services/escape-stripper.js +91 -0
- package/src/services/failure-text.js +15 -0
- package/src/services/file-system-writer.js +35 -0
- package/src/services/file-system.reader.js +54 -0
- package/src/services/flow-controller.js +102 -0
- package/src/services/folder-access.js +19 -0
- package/src/services/inherited-environment.js +20 -0
- package/src/services/line-process-starter.js +17 -0
- package/src/services/line-process.js +72 -0
- package/src/services/log-file.js +57 -0
- package/src/services/mac-environment.js +81 -0
- package/src/services/mac-folder-access.js +17 -0
- package/src/services/mark-scanner.js +94 -0
- package/src/services/one-shot-command.js +119 -0
- package/src/services/output-batcher.js +62 -0
- package/src/services/output-log.js +50 -0
- package/src/services/powershell-binding.js +61 -0
- package/src/services/process-signaller.js +19 -0
- package/src/services/pseudo-terminal.js +54 -0
- package/src/services/registry-export.js +108 -0
- package/src/services/resize-wakeup.js +39 -0
- package/src/services/saved-terminals.js +52 -0
- package/src/services/shared-reads.js +41 -0
- package/src/services/shell-command.js +90 -0
- package/src/services/shell-environment.js +20 -0
- package/src/services/shell-integration.js +207 -0
- package/src/services/shell-locator.js +177 -0
- package/src/services/shell-options-queue.js +34 -0
- package/src/services/shell-words.js +80 -0
- package/src/services/system-requirements.js +46 -0
- package/src/services/terminal-audience.js +83 -0
- package/src/services/terminal-history-folder.js +97 -0
- package/src/services/terminal-history-segment.js +109 -0
- package/src/services/terminal-history.js +130 -0
- package/src/services/terminal-host.js +931 -0
- package/src/services/terminal-line-reader.js +101 -0
- package/src/services/terminal-mirror.js +221 -0
- package/src/services/terminal-owner.service.js +27 -0
- package/src/services/terminal-session.js +398 -0
- package/src/services/visual-studio.reader.js +76 -0
- package/src/services/windows-environment.js +218 -0
- package/src/services/windows-folder-access.js +23 -0
- package/src/services/wsl-distribution.reader.js +49 -0
- package/src/services/xterm-headless-loader.js +14 -0
- package/src/tools/enums/command-status.js +13 -0
- package/src/tools/enums/run-method.js +11 -0
- package/src/tools/exceptions/abort-reason-unknown.exception.js +17 -0
- package/src/tools/exceptions/stale-terminal.exception.js +16 -0
- package/src/tools/interfaces/i-agent-command.js +8 -0
- package/src/tools/interfaces/i-agent-terminal.js +8 -0
- package/src/tools/interfaces/i-agent-terminals.js +8 -0
- package/src/tools/interfaces/i-output-source.js +8 -0
- package/src/tools/interfaces/i-terminal-prompt.js +8 -0
- package/src/tools/models/agent-terminal-access.js +25 -0
- package/src/tools/models/agent-terminal-opening.js +29 -0
- package/src/tools/models/command-line.js +28 -0
- package/src/tools/models/key-sequence.js +27 -0
- package/src/tools/models/output-chunk.js +18 -0
- package/src/tools/models/output-excerpt.js +31 -0
- package/src/tools/models/permission-ask.js +34 -0
- package/src/tools/models/read-cursor.js +26 -0
- package/src/tools/models/read-cursors.js +25 -0
- package/src/tools/models/terminal-open-input.js +34 -0
- package/src/tools/models/terminal-output-report.js +61 -0
- package/src/tools/models/terminal-read-input.js +39 -0
- package/src/tools/models/terminal-run-input.js +52 -0
- package/src/tools/models/terminal-target-input.js +34 -0
- package/src/tools/models/terminal-write-input.js +38 -0
- package/src/tools/models/tool-call-context.js +61 -0
- package/src/tools/services/abort-reason-reader.js +25 -0
- package/src/tools/services/permission-details.js +42 -0
- package/src/tools/services/permission-gate.js +35 -0
- package/src/tools/services/signal-race.js +24 -0
- package/src/tools/services/terminal-access-policy.js +57 -0
- package/src/tools/services/terminal-close-tool.js +57 -0
- package/src/tools/services/terminal-interrupt-tool.js +57 -0
- package/src/tools/services/terminal-list-tool.js +69 -0
- package/src/tools/services/terminal-open-tool.js +71 -0
- package/src/tools/services/terminal-read-tool.js +58 -0
- package/src/tools/services/terminal-run-tool.js +78 -0
- package/src/tools/services/terminal-text-escaper.js +40 -0
- package/src/tools/services/terminal-tool.js +47 -0
- package/src/tools/services/terminal-tools.js +60 -0
- package/src/tools/services/terminal-write-tool.js +75 -0
- package/src/tools/services/tool-failures.js +28 -0
- package/src/tools/services/tool-results.js +13 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { homedir, release, tmpdir } from "node:os";
|
|
9
|
+
import { TerminalNames } from "@noldova/terminal.protocol";
|
|
10
|
+
import { UnsupportedSystemException } from "./exceptions/unsupported-system.exception.js";
|
|
11
|
+
import { EnvironmentTools } from "./models/environment-tools.js";
|
|
12
|
+
import { RequestClient } from "./models/request-client.js";
|
|
13
|
+
import { TerminalEvents } from "./models/terminal-events.js";
|
|
14
|
+
import { Resources } from "./resources.js";
|
|
15
|
+
import { AgentTerminals } from "./services/agent-terminals.js";
|
|
16
|
+
import { CommandRunner } from "./services/command-runner.js";
|
|
17
|
+
import { CompletionSource } from "./services/completion-source.js";
|
|
18
|
+
import { ContextPtySpawner } from "./services/context-pty-spawner.js";
|
|
19
|
+
import { FileSystemWriter } from "./services/file-system-writer.js";
|
|
20
|
+
import { FileSystemReader } from "./services/file-system.reader.js";
|
|
21
|
+
import { FolderAccess } from "./services/folder-access.js";
|
|
22
|
+
import { LineProcessStarter } from "./services/line-process-starter.js";
|
|
23
|
+
import { MacEnvironment } from "./services/mac-environment.js";
|
|
24
|
+
import { ProcessSignaller } from "./services/process-signaller.js";
|
|
25
|
+
import { SavedTerminals } from "./services/saved-terminals.js";
|
|
26
|
+
import { ShellEnvironment } from "./services/shell-environment.js";
|
|
27
|
+
import { ShellIntegration } from "./services/shell-integration.js";
|
|
28
|
+
import { ShellLocator } from "./services/shell-locator.js";
|
|
29
|
+
import { SystemRequirements } from "./services/system-requirements.js";
|
|
30
|
+
import { TerminalHistoryFolder } from "./services/terminal-history-folder.js";
|
|
31
|
+
import { TerminalHost } from "./services/terminal-host.js";
|
|
32
|
+
import { TerminalOwnerService } from "./services/terminal-owner.service.js";
|
|
33
|
+
import { TerminalTools } from "./tools/services/terminal-tools.js";
|
|
34
|
+
export class RuntimePart {
|
|
35
|
+
migrations = SavedTerminals.migrations;
|
|
36
|
+
host = null;
|
|
37
|
+
tools = null;
|
|
38
|
+
environment = null;
|
|
39
|
+
async activateAsync(context) {
|
|
40
|
+
const shortfall = SystemRequirements.findShortfall(process.platform, release(), process.report.getReport());
|
|
41
|
+
if (shortfall !== null) {
|
|
42
|
+
const error = new UnsupportedSystemException(shortfall.requirement, shortfall.found);
|
|
43
|
+
context.log.write(error.message);
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
const reader = new FileSystemReader();
|
|
47
|
+
const writer = new FileSystemWriter();
|
|
48
|
+
const runner = new CommandRunner(context);
|
|
49
|
+
const access = FolderAccess.create(process.platform, runner, process.env);
|
|
50
|
+
const environment = ShellEnvironment.create(process.platform, process.env, new EnvironmentTools(new LineProcessStarter(context), runner, reader, writer, access, tmpdir()), context.log, Intl.DateTimeFormat().resolvedOptions().locale, MacEnvironment.hasSystemLocaleAsync);
|
|
51
|
+
environment.start();
|
|
52
|
+
try {
|
|
53
|
+
const events = new TerminalEvents(context.declareEvent(TerminalNames.outputEvent), context.declareEvent(TerminalNames.exitedEvent), context.declareEvent(TerminalNames.restartedEvent), context.declareEvent(TerminalNames.titleChangedEvent), context.declareEvent(TerminalNames.cwdChangedEvent), context.declareEvent(TerminalNames.detachedEvent), context.declareEvent(TerminalNames.closedEvent), context.declareEvent(TerminalNames.openedEvent), context.declareEvent(TerminalNames.launchCommandEndedEvent), context.declareEvent(TerminalNames.runOpenedEvent), context.declareEvent(TerminalNames.runEndedEvent), context.declareEvent(TerminalNames.renamedEvent), context.declareEvent(TerminalNames.commandAddedEvent));
|
|
54
|
+
const host = new TerminalHost(new ContextPtySpawner(context), reader, new ShellIntegration(writer, tmpdir(), context.log, access, context.settings, process.platform), new TerminalHistoryFolder(writer, reader, new ProcessSignaller(), tmpdir(), process.pid, context.log, access), new ShellLocator(process.platform, process.arch, process.env, ShellLocator.readUserShell(), reader, runner, context.log), process.platform, release(), environment, homedir(), context.log, events, new SavedTerminals(context.database, context.log), new CompletionSource(reader, runner, process.platform, homedir(), context.log));
|
|
55
|
+
for (const name of TerminalNames.methods)
|
|
56
|
+
context.registerMethod(name, { handleAsync: t => host.handleAsync(name, t.payload, RequestClient.fromContext(t)) });
|
|
57
|
+
context.onClientClosed(t => host.closeConnectionAsync(t.connection, t.window));
|
|
58
|
+
context.publishService(Resources.ownersService, new TerminalOwnerService(host));
|
|
59
|
+
const tools = new TerminalTools(new AgentTerminals(host, writer, await context.getWorkFolderAsync(), context.log));
|
|
60
|
+
tools.publish(context.tools, context.log);
|
|
61
|
+
this.host = host;
|
|
62
|
+
this.tools = tools;
|
|
63
|
+
this.environment = environment;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
environment.end();
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async deactivateAsync() {
|
|
71
|
+
const host = this.host;
|
|
72
|
+
const environment = this.environment;
|
|
73
|
+
this.tools?.dispose();
|
|
74
|
+
this.tools = null;
|
|
75
|
+
this.host = null;
|
|
76
|
+
this.environment = null;
|
|
77
|
+
try {
|
|
78
|
+
await host?.endAllAsync();
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
environment?.end();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { TerminalPrompt } from "../models/terminal-prompt.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
import { CommandStatus } from "../tools/enums/command-status.js";
|
|
12
|
+
import { StaleTerminalException } from "../tools/exceptions/stale-terminal.exception.js";
|
|
13
|
+
import { LogFile } from "./log-file.js";
|
|
14
|
+
import { OneShotCommand } from "./one-shot-command.js";
|
|
15
|
+
import { ShellCommand } from "./shell-command.js";
|
|
16
|
+
export class AgentTerminal {
|
|
17
|
+
hosted;
|
|
18
|
+
host;
|
|
19
|
+
writer;
|
|
20
|
+
logFolder;
|
|
21
|
+
log;
|
|
22
|
+
logFile;
|
|
23
|
+
oneShot;
|
|
24
|
+
constructor(hosted, host, writer, logFolder, log) {
|
|
25
|
+
this.hosted = hosted;
|
|
26
|
+
this.host = host;
|
|
27
|
+
this.writer = writer;
|
|
28
|
+
this.logFolder = logFolder;
|
|
29
|
+
this.log = log;
|
|
30
|
+
this.logFile = undefined;
|
|
31
|
+
this.oneShot = undefined;
|
|
32
|
+
}
|
|
33
|
+
get id() {
|
|
34
|
+
return this.session.sessionId;
|
|
35
|
+
}
|
|
36
|
+
get title() {
|
|
37
|
+
return this.session.title;
|
|
38
|
+
}
|
|
39
|
+
get shellId() {
|
|
40
|
+
return this.hosted.shell.id;
|
|
41
|
+
}
|
|
42
|
+
get shellPath() {
|
|
43
|
+
return this.hosted.shell.file;
|
|
44
|
+
}
|
|
45
|
+
get startFolder() {
|
|
46
|
+
return this.hosted.folder;
|
|
47
|
+
}
|
|
48
|
+
get ownerSessionId() {
|
|
49
|
+
return this.hosted.ownerSessionId;
|
|
50
|
+
}
|
|
51
|
+
get owner() {
|
|
52
|
+
return this.hosted.owner;
|
|
53
|
+
}
|
|
54
|
+
get hasShellIntegration() {
|
|
55
|
+
return this.session.commands.hasMarks;
|
|
56
|
+
}
|
|
57
|
+
get isRunning() {
|
|
58
|
+
return !this.session.hasExited;
|
|
59
|
+
}
|
|
60
|
+
get isAwaitingSecret() {
|
|
61
|
+
if (this.oneShot?.isRunning === true)
|
|
62
|
+
return this.oneShot.status === CommandStatus.AwaitingInput;
|
|
63
|
+
return this.isRunning && !this.session.commands.isAtPrompt && this.session.text.isAwaitingSecret;
|
|
64
|
+
}
|
|
65
|
+
get hasPendingLine() {
|
|
66
|
+
return this.hosted.hasRecentInput || (this.hasShellIntegration && (this.session.readPromptLine() ?? "").length > 0);
|
|
67
|
+
}
|
|
68
|
+
get hasUnechoedInput() {
|
|
69
|
+
const received = this.hosted.inputReceivedLength;
|
|
70
|
+
if (received === null || !this.hosted.hasRecentInput)
|
|
71
|
+
return false;
|
|
72
|
+
return !this.hasShellIntegration || this.session.receivedLength <= received;
|
|
73
|
+
}
|
|
74
|
+
get session() {
|
|
75
|
+
return this.hosted.session;
|
|
76
|
+
}
|
|
77
|
+
readPrompt() {
|
|
78
|
+
if (!this.hasShellIntegration)
|
|
79
|
+
return new TerminalPrompt(this.isRunning && this.oneShot?.isRunning !== true && !this.hasUnechoedInput, null, null, this.isAwaitingSecret, this.hosted.inputCount);
|
|
80
|
+
const isIdle = this.isRunning && !this.hasUnechoedInput && this.session.readPromptLine() === "";
|
|
81
|
+
return new TerminalPrompt(isIdle, this.session.commands.cwd, this.session.commands.promptIdentity, this.isAwaitingSecret, this.hosted.inputCount);
|
|
82
|
+
}
|
|
83
|
+
readOneShotLine(command) {
|
|
84
|
+
return this.hasShellIntegration ? null : OneShotCommand.toLaunch(this.hosted.shell, command).commandLine;
|
|
85
|
+
}
|
|
86
|
+
run(command, expected, timeoutMilliseconds) {
|
|
87
|
+
this.requireExpected(expected);
|
|
88
|
+
const logFile = this.openLogFile();
|
|
89
|
+
if (this.hasShellIntegration)
|
|
90
|
+
return new ShellCommand(this.session, command, timeoutMilliseconds, logFile.path);
|
|
91
|
+
this.oneShot = new OneShotCommand(this.host, this.hosted.shell, this.hosted.folder, command, timeoutMilliseconds, logFile, this.log);
|
|
92
|
+
return this.oneShot;
|
|
93
|
+
}
|
|
94
|
+
write(keys, expected) {
|
|
95
|
+
this.requireExpected(expected);
|
|
96
|
+
this.send(keys);
|
|
97
|
+
}
|
|
98
|
+
interrupt(expected) {
|
|
99
|
+
this.requireExpected(expected);
|
|
100
|
+
this.send(Resources.interruptKey);
|
|
101
|
+
}
|
|
102
|
+
async closeAsync() {
|
|
103
|
+
await this.oneShot?.endAsync();
|
|
104
|
+
await this.host.closeTerminalAsync(this.id);
|
|
105
|
+
await this.logFile?.flushAsync();
|
|
106
|
+
}
|
|
107
|
+
readSince(offset) {
|
|
108
|
+
return this.session.text.readSince(offset);
|
|
109
|
+
}
|
|
110
|
+
waitForOutputAsync(offset, timeoutMilliseconds, signal) {
|
|
111
|
+
return this.session.changes.waitUntilAsync(() => this.session.hasExited || this.session.text.endOffset > offset, timeoutMilliseconds, signal);
|
|
112
|
+
}
|
|
113
|
+
async waitForIntegrationAsync(signal) {
|
|
114
|
+
const isReady = () => !this.session.hasIntegration || this.session.hasExited || this.session.commands.promptEndOffset !== null;
|
|
115
|
+
await this.session.waitForStartAsync(() => isReady() || this.session.commands.isLoaded, signal);
|
|
116
|
+
if (this.session.commands.isLoaded)
|
|
117
|
+
await this.session.changes.waitUntilAsync(isReady, Resources.shellStartMilliseconds, signal);
|
|
118
|
+
}
|
|
119
|
+
send(data) {
|
|
120
|
+
if (this.oneShot?.isRunning === true)
|
|
121
|
+
this.oneShot.write(data);
|
|
122
|
+
else
|
|
123
|
+
this.session.write(data);
|
|
124
|
+
}
|
|
125
|
+
openLogFile() {
|
|
126
|
+
if (this.logFile === undefined) {
|
|
127
|
+
const logFile = new LogFile(this.writer, path.join(this.logFolder, this.id + Resources.logFileExtension), this.log);
|
|
128
|
+
this.session.text.subscribe(t => logFile.append(t));
|
|
129
|
+
this.logFile = logFile;
|
|
130
|
+
}
|
|
131
|
+
return this.logFile;
|
|
132
|
+
}
|
|
133
|
+
requireExpected(expected) {
|
|
134
|
+
if (!this.readPrompt().equals(expected))
|
|
135
|
+
throw new StaleTerminalException(this.id);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources as ProtocolResources, TerminalSize } from "@noldova/terminal.protocol";
|
|
9
|
+
import { Resources } from "../resources.js";
|
|
10
|
+
import { AgentTerminal } from "./agent-terminal.js";
|
|
11
|
+
export class AgentTerminals {
|
|
12
|
+
host;
|
|
13
|
+
writer;
|
|
14
|
+
logFolder;
|
|
15
|
+
log;
|
|
16
|
+
terminals = new Map();
|
|
17
|
+
constructor(host, writer, logFolder, log) {
|
|
18
|
+
this.host = host;
|
|
19
|
+
this.writer = writer;
|
|
20
|
+
this.logFolder = logFolder;
|
|
21
|
+
this.log = log;
|
|
22
|
+
}
|
|
23
|
+
list() {
|
|
24
|
+
const hosted = this.host.terminals;
|
|
25
|
+
for (const id of this.terminals.keys()) {
|
|
26
|
+
if (!hosted.some(t => t.session.sessionId === id))
|
|
27
|
+
this.terminals.delete(id);
|
|
28
|
+
}
|
|
29
|
+
return hosted.map(t => this.wrap(t));
|
|
30
|
+
}
|
|
31
|
+
find(id) {
|
|
32
|
+
const hosted = this.host.findTerminal(id);
|
|
33
|
+
return hosted === undefined ? undefined : this.wrap(hosted);
|
|
34
|
+
}
|
|
35
|
+
async openAsync(opening, signal) {
|
|
36
|
+
const size = new TerminalSize(Resources.agentColumns, Resources.agentRows);
|
|
37
|
+
const terminal = this.wrap(await this.host.openTerminalAsync(opening.shellId, opening.cwd, size, opening.ownerSessionId, ProtocolResources.defaultStoredLimit, null, false, this.host.findAgentSessionOwner(opening.ownerSessionId) ?? opening.owner));
|
|
38
|
+
await terminal.waitForIntegrationAsync(signal);
|
|
39
|
+
return terminal;
|
|
40
|
+
}
|
|
41
|
+
findSessionOwner(agentSessionId) {
|
|
42
|
+
return this.host.findAgentSessionOwner(agentSessionId);
|
|
43
|
+
}
|
|
44
|
+
endSession(agentSessionId) {
|
|
45
|
+
this.host.unbindAgentSession(agentSessionId);
|
|
46
|
+
}
|
|
47
|
+
wrap(hosted) {
|
|
48
|
+
let terminal = this.terminals.get(hosted.session.sessionId);
|
|
49
|
+
if (terminal === undefined) {
|
|
50
|
+
terminal = new AgentTerminal(hosted, this.host, this.writer, this.logFolder, this.log);
|
|
51
|
+
this.terminals.set(hosted.session.sessionId, terminal);
|
|
52
|
+
}
|
|
53
|
+
return terminal;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
export class BoundedReads {
|
|
9
|
+
reader;
|
|
10
|
+
expiry = Promise.withResolvers();
|
|
11
|
+
timer;
|
|
12
|
+
pending = new Set();
|
|
13
|
+
hasExpired = false;
|
|
14
|
+
hasCutShort = false;
|
|
15
|
+
constructor(reader, milliseconds) {
|
|
16
|
+
this.reader = reader;
|
|
17
|
+
this.timer = setTimeout(() => {
|
|
18
|
+
this.hasExpired = true;
|
|
19
|
+
this.expiry.resolve();
|
|
20
|
+
}, milliseconds);
|
|
21
|
+
}
|
|
22
|
+
get wasCutShort() {
|
|
23
|
+
return this.hasCutShort;
|
|
24
|
+
}
|
|
25
|
+
listPendingReads() {
|
|
26
|
+
return [...this.pending];
|
|
27
|
+
}
|
|
28
|
+
end() {
|
|
29
|
+
clearTimeout(this.timer);
|
|
30
|
+
}
|
|
31
|
+
readEntriesAsync(path) {
|
|
32
|
+
return this.boundAsync(() => this.reader.readEntriesAsync(path), []);
|
|
33
|
+
}
|
|
34
|
+
readTextOrNullAsync(path) {
|
|
35
|
+
return this.boundAsync(() => this.reader.readTextOrNullAsync(path), null);
|
|
36
|
+
}
|
|
37
|
+
isProgramAsync(path) {
|
|
38
|
+
return this.boundAsync(() => this.reader.isProgramAsync(path), false);
|
|
39
|
+
}
|
|
40
|
+
readRealPathOrNullAsync(path) {
|
|
41
|
+
return this.boundAsync(() => this.reader.readRealPathOrNullAsync(path), null);
|
|
42
|
+
}
|
|
43
|
+
async boundAsync(read, late) {
|
|
44
|
+
if (this.hasExpired) {
|
|
45
|
+
this.hasCutShort = true;
|
|
46
|
+
return late;
|
|
47
|
+
}
|
|
48
|
+
const shared = read();
|
|
49
|
+
this.pending.add(shared);
|
|
50
|
+
const tracked = shared.finally(() => this.pending.delete(shared));
|
|
51
|
+
const answer = await Promise.race([tracked.then(t => ({ value: t })), this.expiry.promise.then(() => null)]);
|
|
52
|
+
if (answer !== null)
|
|
53
|
+
return answer.value;
|
|
54
|
+
this.hasCutShort = true;
|
|
55
|
+
return late;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources } from "../resources.js";
|
|
9
|
+
export class ChangeSignal {
|
|
10
|
+
waiters = [];
|
|
11
|
+
notify() {
|
|
12
|
+
for (const waiter of this.waiters.splice(0))
|
|
13
|
+
waiter();
|
|
14
|
+
}
|
|
15
|
+
async waitUntilAsync(condition, timeoutMilliseconds, signal) {
|
|
16
|
+
const deadline = timeoutMilliseconds === null ? null : Date.now() + timeoutMilliseconds;
|
|
17
|
+
while (!condition() && !signal.aborted && (deadline === null || Date.now() < deadline))
|
|
18
|
+
await this.waitAsync(deadline === null ? null : deadline - Date.now(), signal);
|
|
19
|
+
}
|
|
20
|
+
waitAsync(timeoutMilliseconds, signal) {
|
|
21
|
+
return new Promise(resolve => {
|
|
22
|
+
const done = () => {
|
|
23
|
+
clearTimeout(timer);
|
|
24
|
+
signal.removeEventListener(Resources.abortEvent, done);
|
|
25
|
+
this.waiters = this.waiters.filter(t => t !== done);
|
|
26
|
+
resolve();
|
|
27
|
+
};
|
|
28
|
+
const timer = timeoutMilliseconds === null ? undefined : setTimeout(done, timeoutMilliseconds);
|
|
29
|
+
signal.addEventListener(Resources.abortEvent, done);
|
|
30
|
+
this.waiters.push(done);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { Resources as ProtocolResources } from "@noldova/terminal.protocol";
|
|
9
|
+
export class CommandHistory {
|
|
10
|
+
commands = new Map();
|
|
11
|
+
add(shellId, command) {
|
|
12
|
+
this.commands.set(shellId, [command, ...this.list(shellId).filter(t => t !== command)].slice(0, ProtocolResources.maximumSuggestedCommands));
|
|
13
|
+
}
|
|
14
|
+
clear() {
|
|
15
|
+
this.commands.clear();
|
|
16
|
+
}
|
|
17
|
+
list(shellId) {
|
|
18
|
+
return this.commands.get(shellId) ?? [];
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { ProcessRequest, ProcessStartException } from "@noldova/desktop.runtime";
|
|
10
|
+
import { CommandExitedException } from "../exceptions/command-exited.exception.js";
|
|
11
|
+
import { CommandNotStartedException } from "../exceptions/command-not-started.exception.js";
|
|
12
|
+
import { CommandOutputTooLongException } from "../exceptions/command-output-too-long.exception.js";
|
|
13
|
+
import { CommandTimedOutException } from "../exceptions/command-timed-out.exception.js";
|
|
14
|
+
import { Resources } from "../resources.js";
|
|
15
|
+
export class CommandRunner {
|
|
16
|
+
context;
|
|
17
|
+
constructor(context) {
|
|
18
|
+
this.context = context;
|
|
19
|
+
}
|
|
20
|
+
async runAsync(file, args, timeoutMilliseconds, environment = {}) {
|
|
21
|
+
const name = path.basename(file);
|
|
22
|
+
const stopping = new AbortController();
|
|
23
|
+
const request = new ProcessRequest(file, [...args], path.dirname(file), environment, [], stopping.signal);
|
|
24
|
+
let isTooLong = false;
|
|
25
|
+
const timer = setTimeout(() => stopping.abort(), timeoutMilliseconds);
|
|
26
|
+
try {
|
|
27
|
+
let process;
|
|
28
|
+
try {
|
|
29
|
+
process = await this.context.startProcessAsync(request);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (stopping.signal.aborted && error === stopping.signal.reason)
|
|
33
|
+
throw new CommandTimedOutException(name, timeoutMilliseconds);
|
|
34
|
+
if (!(error instanceof ProcessStartException))
|
|
35
|
+
throw error;
|
|
36
|
+
throw new CommandNotStartedException(name, error);
|
|
37
|
+
}
|
|
38
|
+
let output = "";
|
|
39
|
+
process.output.setEncoding(Resources.utf8Encoding);
|
|
40
|
+
process.output.on(Resources.dataEvent, (text) => {
|
|
41
|
+
output += text;
|
|
42
|
+
if (output.length > Resources.maximumCommandOutputLength) {
|
|
43
|
+
isTooLong = true;
|
|
44
|
+
stopping.abort();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
let errors = "";
|
|
48
|
+
process.errors.setEncoding(Resources.utf8Encoding);
|
|
49
|
+
process.errors.on(Resources.dataEvent, (text) => {
|
|
50
|
+
errors = (errors + text).slice(-Resources.maximumCommandErrorLength);
|
|
51
|
+
});
|
|
52
|
+
const closed = new Promise(resolve => process.output.once(Resources.closeEvent, () => resolve()));
|
|
53
|
+
const errorsClosed = new Promise(resolve => process.errors.once(Resources.closeEvent, () => resolve()));
|
|
54
|
+
const [exit] = await Promise.all([process.exited.finally(() => clearTimeout(timer)), closed, errorsClosed]);
|
|
55
|
+
if (isTooLong)
|
|
56
|
+
throw new CommandOutputTooLongException(name, Resources.maximumCommandOutputLength);
|
|
57
|
+
if (stopping.signal.aborted)
|
|
58
|
+
throw new CommandTimedOutException(name, timeoutMilliseconds);
|
|
59
|
+
if (exit.code !== 0)
|
|
60
|
+
throw new CommandExitedException(name, exit, errors);
|
|
61
|
+
return output;
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
clearTimeout(timer);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright (c) Noldova.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
import { MarkKind } from "../enums/mark-kind.js";
|
|
9
|
+
import { TrackedCommand } from "../models/tracked-command.js";
|
|
10
|
+
import { Resources } from "../resources.js";
|
|
11
|
+
export class CommandTracker {
|
|
12
|
+
read;
|
|
13
|
+
promptOffset = null;
|
|
14
|
+
lineOffset = null;
|
|
15
|
+
promptText = null;
|
|
16
|
+
promptStart = null;
|
|
17
|
+
canRedraw = false;
|
|
18
|
+
redrawFrom = null;
|
|
19
|
+
current = null;
|
|
20
|
+
hasTypedAhead = false;
|
|
21
|
+
hasMarks = false;
|
|
22
|
+
isAtPrompt = false;
|
|
23
|
+
hasEnteredLine = false;
|
|
24
|
+
cwd = null;
|
|
25
|
+
path = null;
|
|
26
|
+
isPathReset = false;
|
|
27
|
+
temporaryFolder = null;
|
|
28
|
+
modulePath = null;
|
|
29
|
+
isModulePathReset = false;
|
|
30
|
+
names = null;
|
|
31
|
+
isNamesReset = false;
|
|
32
|
+
builtins = null;
|
|
33
|
+
hasOptionsBinding = false;
|
|
34
|
+
isLoaded = false;
|
|
35
|
+
lastCommand = null;
|
|
36
|
+
prompts = 0;
|
|
37
|
+
commandMarks = 0;
|
|
38
|
+
constructor(read) {
|
|
39
|
+
this.read = read;
|
|
40
|
+
}
|
|
41
|
+
get runningCommand() {
|
|
42
|
+
return this.current;
|
|
43
|
+
}
|
|
44
|
+
get promptEndOffset() {
|
|
45
|
+
return this.isAtPrompt ? this.lineOffset : null;
|
|
46
|
+
}
|
|
47
|
+
get promptIdentity() {
|
|
48
|
+
return this.isAtPrompt ? this.promptOffset : null;
|
|
49
|
+
}
|
|
50
|
+
get isWaitingAtPrompt() {
|
|
51
|
+
return this.isAtPrompt && !this.hasEnteredLine;
|
|
52
|
+
}
|
|
53
|
+
enterLine() {
|
|
54
|
+
if (this.isWaitingAtPrompt)
|
|
55
|
+
this.hasEnteredLine = true;
|
|
56
|
+
else
|
|
57
|
+
this.hasTypedAhead = true;
|
|
58
|
+
}
|
|
59
|
+
apply(mark) {
|
|
60
|
+
if (mark.kind === MarkKind.Loaded) {
|
|
61
|
+
this.isLoaded = true;
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.hasMarks = true;
|
|
65
|
+
switch (mark.kind) {
|
|
66
|
+
case MarkKind.PromptStart:
|
|
67
|
+
this.redrawFrom = this.canRedraw ? this.lineOffset : null;
|
|
68
|
+
this.canRedraw = false;
|
|
69
|
+
this.isAtPrompt = false;
|
|
70
|
+
this.promptStart = mark.offset;
|
|
71
|
+
this.prompts++;
|
|
72
|
+
break;
|
|
73
|
+
case MarkKind.PromptEnd:
|
|
74
|
+
this.endPrompt(mark.offset);
|
|
75
|
+
break;
|
|
76
|
+
case MarkKind.OutputStart:
|
|
77
|
+
this.stopRedraws();
|
|
78
|
+
this.isAtPrompt = false;
|
|
79
|
+
this.current = new TrackedCommand(mark.offset, true);
|
|
80
|
+
this.commandMarks++;
|
|
81
|
+
break;
|
|
82
|
+
case MarkKind.CommandEnd:
|
|
83
|
+
this.stopRedraws();
|
|
84
|
+
this.commandMarks++;
|
|
85
|
+
this.end(mark);
|
|
86
|
+
break;
|
|
87
|
+
case MarkKind.Property:
|
|
88
|
+
this.stopRedraws();
|
|
89
|
+
if (mark.value?.startsWith(Resources.cwdProperty) === true)
|
|
90
|
+
this.cwd = mark.value.slice(Resources.cwdProperty.length);
|
|
91
|
+
else if (mark.value?.startsWith(Resources.pathProperty) === true) {
|
|
92
|
+
this.path = mark.value.slice(Resources.pathProperty.length);
|
|
93
|
+
this.isPathReset = false;
|
|
94
|
+
}
|
|
95
|
+
else if (mark.value === Resources.pathReset) {
|
|
96
|
+
this.path = null;
|
|
97
|
+
this.isPathReset = true;
|
|
98
|
+
}
|
|
99
|
+
else if (mark.value?.startsWith(Resources.temporaryFolderProperty) === true)
|
|
100
|
+
this.temporaryFolder = mark.value.slice(Resources.temporaryFolderProperty.length);
|
|
101
|
+
else if (mark.value === Resources.temporaryFolderReset)
|
|
102
|
+
this.temporaryFolder = null;
|
|
103
|
+
else if (mark.value?.startsWith(Resources.modulePathProperty) === true) {
|
|
104
|
+
this.modulePath = mark.value.slice(Resources.modulePathProperty.length);
|
|
105
|
+
this.isModulePathReset = false;
|
|
106
|
+
}
|
|
107
|
+
else if (mark.value === Resources.modulePathReset) {
|
|
108
|
+
this.modulePath = null;
|
|
109
|
+
this.isModulePathReset = true;
|
|
110
|
+
}
|
|
111
|
+
else if (mark.value?.startsWith(Resources.namesProperty) === true) {
|
|
112
|
+
this.names = new Set(mark.value.slice(Resources.namesProperty.length).split(Resources.namesSeparator).filter(t => t !== ""));
|
|
113
|
+
this.isNamesReset = false;
|
|
114
|
+
}
|
|
115
|
+
else if (mark.value === Resources.namesReset) {
|
|
116
|
+
this.names = null;
|
|
117
|
+
this.isNamesReset = true;
|
|
118
|
+
}
|
|
119
|
+
else if (mark.value?.startsWith(Resources.builtinsProperty) === true)
|
|
120
|
+
this.builtins = new Set(mark.value.slice(Resources.builtinsProperty.length).split(Resources.namesSeparator).filter(t => t !== ""));
|
|
121
|
+
else if (mark.value === Resources.builtinsReset)
|
|
122
|
+
this.builtins = null;
|
|
123
|
+
else if (mark.value?.startsWith(Resources.optionsProperty) === true)
|
|
124
|
+
this.hasOptionsBinding = mark.value.slice(Resources.optionsProperty.length) === Resources.optionsBound;
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
endPrompt(offset) {
|
|
129
|
+
const from = this.canRedraw ? this.lineOffset : this.redrawFrom;
|
|
130
|
+
const drawn = this.canRedraw ? CommandTracker.lastLine(this.promptText) : this.promptText;
|
|
131
|
+
if (from === null || drawn === null || CommandTracker.drawing(this.read(from, offset)) !== drawn) {
|
|
132
|
+
this.promptOffset = offset;
|
|
133
|
+
this.promptText = this.promptStart === null ? null : CommandTracker.drawing(this.read(this.promptStart, offset));
|
|
134
|
+
}
|
|
135
|
+
this.lineOffset = offset;
|
|
136
|
+
this.promptStart = null;
|
|
137
|
+
this.redrawFrom = null;
|
|
138
|
+
this.canRedraw = true;
|
|
139
|
+
this.isAtPrompt = true;
|
|
140
|
+
this.hasEnteredLine = this.hasTypedAhead;
|
|
141
|
+
this.hasTypedAhead = false;
|
|
142
|
+
}
|
|
143
|
+
static drawing(text) {
|
|
144
|
+
return text.length > 1 && text.startsWith(Resources.lineFeed) ? text.slice(1) : text;
|
|
145
|
+
}
|
|
146
|
+
static lastLine(text) {
|
|
147
|
+
return text === null ? null : text.slice(text.lastIndexOf(Resources.lineFeed) + 1);
|
|
148
|
+
}
|
|
149
|
+
stopRedraws() {
|
|
150
|
+
this.canRedraw = false;
|
|
151
|
+
this.redrawFrom = null;
|
|
152
|
+
}
|
|
153
|
+
end(mark) {
|
|
154
|
+
const command = this.current ?? new TrackedCommand(this.lineOffset ?? mark.offset, false);
|
|
155
|
+
const exitCode = mark.value !== null && Resources.commandTrackerExitCode.test(mark.value) ? Number.parseInt(mark.value, Resources.decimalRadix) : null;
|
|
156
|
+
command.end(mark.offset, exitCode, this.cwd);
|
|
157
|
+
this.isAtPrompt = false;
|
|
158
|
+
this.current = null;
|
|
159
|
+
this.lastCommand = command;
|
|
160
|
+
}
|
|
161
|
+
}
|