@noldova/terminal 0.0.0-stage → 0.0.2

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.
Files changed (189) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -2
  3. package/menus.json +53 -0
  4. package/module.json +28 -0
  5. package/package.json +50 -4
  6. package/settings.json +83 -0
  7. package/src/api/index.d.ts +131 -0
  8. package/src/api/index.js +11 -0
  9. package/src/api/window.d.ts +467 -0
  10. package/src/api/window.js +8 -0
  11. package/src/enums/escape-state.js +15 -0
  12. package/src/enums/escape-stripper-state.js +15 -0
  13. package/src/enums/mark-kind.js +18 -0
  14. package/src/enums/shell-family.js +14 -0
  15. package/src/enums/system-requirement.js +11 -0
  16. package/src/exceptions/agent-terminal-restart.exception.js +16 -0
  17. package/src/exceptions/command-exited.exception.js +23 -0
  18. package/src/exceptions/command-failed.exception.js +13 -0
  19. package/src/exceptions/command-not-started.exception.js +17 -0
  20. package/src/exceptions/command-output-too-long.exception.js +18 -0
  21. package/src/exceptions/command-timed-out.exception.js +18 -0
  22. package/src/exceptions/command-variable-not-set.exception.js +18 -0
  23. package/src/exceptions/default-shell-not-found.exception.js +14 -0
  24. package/src/exceptions/environment-export-invalid.exception.js +14 -0
  25. package/src/exceptions/environment-key-unreadable.exception.js +17 -0
  26. package/src/exceptions/environment-unreadable.exception.js +13 -0
  27. package/src/exceptions/folder-not-found.exception.js +16 -0
  28. package/src/exceptions/host-ended.exception.js +14 -0
  29. package/src/exceptions/session-not-found.exception.js +18 -0
  30. package/src/exceptions/shell-kind-not-found.exception.js +16 -0
  31. package/src/exceptions/shell-not-found.exception.js +18 -0
  32. package/src/exceptions/shells-unreadable.exception.js +13 -0
  33. package/src/exceptions/stored-lines-damaged.exception.js +15 -0
  34. package/src/exceptions/stored-lines-unwritable.exception.js +15 -0
  35. package/src/exceptions/system-root-missing.exception.js +14 -0
  36. package/src/exceptions/terminal.exception.js +13 -0
  37. package/src/exceptions/unknown-method.exception.js +16 -0
  38. package/src/exceptions/unsupported-system.exception.js +18 -0
  39. package/src/exceptions/visual-studio-unreadable.exception.js +15 -0
  40. package/src/exceptions/wsl-distributions-unreadable.exception.js +15 -0
  41. package/src/interfaces/i-command-runner.js +8 -0
  42. package/src/interfaces/i-disposable.js +8 -0
  43. package/src/interfaces/i-environment-source.js +8 -0
  44. package/src/interfaces/i-file-system-reader.js +8 -0
  45. package/src/interfaces/i-file-system-writer.js +8 -0
  46. package/src/interfaces/i-flow-controller-listener.js +8 -0
  47. package/src/interfaces/i-folder-access.js +8 -0
  48. package/src/interfaces/i-line-process-listener.js +8 -0
  49. package/src/interfaces/i-line-process-starter.js +8 -0
  50. package/src/interfaces/i-line-process.js +8 -0
  51. package/src/interfaces/i-output-batcher-listener.js +8 -0
  52. package/src/interfaces/i-process-signaller.js +8 -0
  53. package/src/interfaces/i-pseudo-terminal-listener.js +8 -0
  54. package/src/interfaces/i-pty-exit-event.js +8 -0
  55. package/src/interfaces/i-pty-process.js +8 -0
  56. package/src/interfaces/i-pty-spawner.js +8 -0
  57. package/src/interfaces/i-terminal-mirror-listener.js +8 -0
  58. package/src/interfaces/i-terminal-session-listener.js +8 -0
  59. package/src/interfaces/i-waiting-options-request.js +8 -0
  60. package/src/models/command-listing.js +17 -0
  61. package/src/models/completion-candidate.js +19 -0
  62. package/src/models/environment-tools.js +23 -0
  63. package/src/models/environment-variable.js +15 -0
  64. package/src/models/folder-entry.js +15 -0
  65. package/src/models/hosted-terminal.js +116 -0
  66. package/src/models/mirror-line.js +27 -0
  67. package/src/models/mirror-snapshot.js +19 -0
  68. package/src/models/one-shot-launch.js +42 -0
  69. package/src/models/pane-progress.js +15 -0
  70. package/src/models/powershell-cmdlet.js +21 -0
  71. package/src/models/powershell-lookup.js +17 -0
  72. package/src/models/powershell-parameter.js +19 -0
  73. package/src/models/registry-value.js +15 -0
  74. package/src/models/request-client.js +23 -0
  75. package/src/models/saved-terminal.js +25 -0
  76. package/src/models/shell-launch.js +23 -0
  77. package/src/models/shell-line.js +21 -0
  78. package/src/models/shell-mark.js +19 -0
  79. package/src/models/shell-options-reply.js +17 -0
  80. package/src/models/shell.js +29 -0
  81. package/src/models/started-shell.js +21 -0
  82. package/src/models/system-shortfall.js +15 -0
  83. package/src/models/terminal-events.js +37 -0
  84. package/src/models/terminal-history-paths.js +15 -0
  85. package/src/models/terminal-prompt.js +28 -0
  86. package/src/models/tracked-command.js +26 -0
  87. package/src/models/typed-line.js +40 -0
  88. package/src/models/visual-studio-installation.js +22 -0
  89. package/src/models/waiting-run.js +25 -0
  90. package/src/resources.js +1725 -0
  91. package/src/runtime-part.js +84 -0
  92. package/src/services/agent-terminal.js +137 -0
  93. package/src/services/agent-terminals.js +55 -0
  94. package/src/services/bounded-reads.js +57 -0
  95. package/src/services/change-signal.js +33 -0
  96. package/src/services/command-history.js +20 -0
  97. package/src/services/command-runner.js +67 -0
  98. package/src/services/command-tracker.js +161 -0
  99. package/src/services/completion-source.js +316 -0
  100. package/src/services/context-pty-process.js +79 -0
  101. package/src/services/context-pty-spawner.js +26 -0
  102. package/src/services/deferred-pty-process.js +94 -0
  103. package/src/services/escape-sequence-tracker.js +126 -0
  104. package/src/services/escape-stripper.js +91 -0
  105. package/src/services/failure-text.js +15 -0
  106. package/src/services/file-system-writer.js +35 -0
  107. package/src/services/file-system.reader.js +54 -0
  108. package/src/services/flow-controller.js +102 -0
  109. package/src/services/folder-access.js +19 -0
  110. package/src/services/inherited-environment.js +20 -0
  111. package/src/services/line-process-starter.js +17 -0
  112. package/src/services/line-process.js +72 -0
  113. package/src/services/log-file.js +57 -0
  114. package/src/services/mac-environment.js +81 -0
  115. package/src/services/mac-folder-access.js +17 -0
  116. package/src/services/mark-scanner.js +94 -0
  117. package/src/services/one-shot-command.js +119 -0
  118. package/src/services/output-batcher.js +62 -0
  119. package/src/services/output-log.js +50 -0
  120. package/src/services/powershell-binding.js +61 -0
  121. package/src/services/process-signaller.js +19 -0
  122. package/src/services/pseudo-terminal.js +54 -0
  123. package/src/services/registry-export.js +108 -0
  124. package/src/services/resize-wakeup.js +39 -0
  125. package/src/services/saved-terminals.js +52 -0
  126. package/src/services/shared-reads.js +41 -0
  127. package/src/services/shell-command.js +90 -0
  128. package/src/services/shell-environment.js +20 -0
  129. package/src/services/shell-integration.js +207 -0
  130. package/src/services/shell-locator.js +177 -0
  131. package/src/services/shell-options-queue.js +34 -0
  132. package/src/services/shell-words.js +80 -0
  133. package/src/services/system-requirements.js +46 -0
  134. package/src/services/terminal-audience.js +83 -0
  135. package/src/services/terminal-history-folder.js +97 -0
  136. package/src/services/terminal-history-segment.js +109 -0
  137. package/src/services/terminal-history.js +130 -0
  138. package/src/services/terminal-host.js +931 -0
  139. package/src/services/terminal-line-reader.js +101 -0
  140. package/src/services/terminal-mirror.js +221 -0
  141. package/src/services/terminal-owner.service.js +27 -0
  142. package/src/services/terminal-session.js +398 -0
  143. package/src/services/visual-studio.reader.js +76 -0
  144. package/src/services/windows-environment.js +218 -0
  145. package/src/services/windows-folder-access.js +23 -0
  146. package/src/services/wsl-distribution.reader.js +49 -0
  147. package/src/services/xterm-headless-loader.js +14 -0
  148. package/src/tools/enums/command-status.js +13 -0
  149. package/src/tools/enums/run-method.js +11 -0
  150. package/src/tools/exceptions/abort-reason-unknown.exception.js +17 -0
  151. package/src/tools/exceptions/stale-terminal.exception.js +16 -0
  152. package/src/tools/interfaces/i-agent-command.js +8 -0
  153. package/src/tools/interfaces/i-agent-terminal.js +8 -0
  154. package/src/tools/interfaces/i-agent-terminals.js +8 -0
  155. package/src/tools/interfaces/i-output-source.js +8 -0
  156. package/src/tools/interfaces/i-terminal-prompt.js +8 -0
  157. package/src/tools/models/agent-terminal-access.js +25 -0
  158. package/src/tools/models/agent-terminal-opening.js +29 -0
  159. package/src/tools/models/command-line.js +28 -0
  160. package/src/tools/models/key-sequence.js +27 -0
  161. package/src/tools/models/output-chunk.js +18 -0
  162. package/src/tools/models/output-excerpt.js +31 -0
  163. package/src/tools/models/permission-ask.js +34 -0
  164. package/src/tools/models/read-cursor.js +26 -0
  165. package/src/tools/models/read-cursors.js +25 -0
  166. package/src/tools/models/terminal-open-input.js +34 -0
  167. package/src/tools/models/terminal-output-report.js +61 -0
  168. package/src/tools/models/terminal-read-input.js +39 -0
  169. package/src/tools/models/terminal-run-input.js +52 -0
  170. package/src/tools/models/terminal-target-input.js +34 -0
  171. package/src/tools/models/terminal-write-input.js +38 -0
  172. package/src/tools/models/tool-call-context.js +61 -0
  173. package/src/tools/services/abort-reason-reader.js +25 -0
  174. package/src/tools/services/permission-details.js +42 -0
  175. package/src/tools/services/permission-gate.js +35 -0
  176. package/src/tools/services/signal-race.js +24 -0
  177. package/src/tools/services/terminal-access-policy.js +57 -0
  178. package/src/tools/services/terminal-close-tool.js +57 -0
  179. package/src/tools/services/terminal-interrupt-tool.js +57 -0
  180. package/src/tools/services/terminal-list-tool.js +69 -0
  181. package/src/tools/services/terminal-open-tool.js +71 -0
  182. package/src/tools/services/terminal-read-tool.js +58 -0
  183. package/src/tools/services/terminal-run-tool.js +78 -0
  184. package/src/tools/services/terminal-text-escaper.js +40 -0
  185. package/src/tools/services/terminal-tool.js +47 -0
  186. package/src/tools/services/terminal-tools.js +60 -0
  187. package/src/tools/services/terminal-write-tool.js +75 -0
  188. package/src/tools/services/tool-failures.js +28 -0
  189. package/src/tools/services/tool-results.js +13 -0
@@ -0,0 +1,23 @@
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 { CommandVariableNotSetException } from "../exceptions/command-variable-not-set.exception.js";
10
+ import { Resources } from "../resources.js";
11
+ export class WindowsFolderAccess {
12
+ runner;
13
+ systemRoot;
14
+ constructor(runner, systemRoot) {
15
+ this.runner = runner;
16
+ this.systemRoot = systemRoot;
17
+ }
18
+ async restrictAsync(folder) {
19
+ if (this.systemRoot === undefined || this.systemRoot.length === 0)
20
+ throw new CommandVariableNotSetException(Resources.icaclsFile, Resources.systemRootVariable);
21
+ await this.runner.runAsync(path.win32.join(this.systemRoot, ...Resources.icaclsPath), [folder, ...Resources.privateFolderGrants], Resources.folderAccessMilliseconds);
22
+ }
23
+ }
@@ -0,0 +1,49 @@
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 { CommandFailedException } from "../exceptions/command-failed.exception.js";
10
+ import { WslDistributionsUnreadableException } from "../exceptions/wsl-distributions-unreadable.exception.js";
11
+ import { Resources } from "../resources.js";
12
+ export class WslDistributionReader {
13
+ runner;
14
+ powerShell;
15
+ constructor(runner, systemRoot) {
16
+ this.runner = runner;
17
+ this.powerShell = path.win32.join(systemRoot, ...Resources.windowsPowerShellPath);
18
+ }
19
+ async readAsync() {
20
+ let output;
21
+ try {
22
+ output = await this.runner.runAsync(this.powerShell, [...Resources.environmentReadArguments, WslDistributionReader.encode(Resources.wslDistributionScript)], Resources.shellListMilliseconds);
23
+ }
24
+ catch (error) {
25
+ if (error instanceof CommandFailedException)
26
+ throw new WslDistributionsUnreadableException(error);
27
+ throw error;
28
+ }
29
+ return WslDistributionReader.parse(output.replace(Resources.wslDistributionReaderByteOrderMark, ""));
30
+ }
31
+ static parse(output) {
32
+ const lines = output.split(Resources.wslDistributionReaderLineBreak).filter(t => t.length > 0);
33
+ if (lines.pop() !== Resources.wslEndLine)
34
+ throw new WslDistributionsUnreadableException();
35
+ const defaults = [];
36
+ const others = [];
37
+ for (const line of lines) {
38
+ const [name, flag, ...rest] = line.split(Resources.wslDistributionReaderFieldSeparator);
39
+ if (name === undefined || name.length === 0 || !Resources.base64Pattern.test(name) || rest.length > 0 ||
40
+ (flag !== Resources.wslDefaultFlag && flag !== Resources.wslOtherFlag))
41
+ throw new WslDistributionsUnreadableException();
42
+ (flag === Resources.wslDefaultFlag ? defaults : others).push(Buffer.from(name, Resources.base64Encoding).toString(Resources.utf8Encoding));
43
+ }
44
+ return [...defaults, ...others];
45
+ }
46
+ static encode(script) {
47
+ return Buffer.from(script, Resources.utf16Encoding).toString(Resources.base64Encoding);
48
+ }
49
+ }
@@ -0,0 +1,14 @@
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 { createRequire } from "node:module";
9
+ export class XtermHeadlessLoader {
10
+ static module = createRequire(import.meta.url)("@xterm/headless");
11
+ static createTerminal(options) {
12
+ return new XtermHeadlessLoader.module.Terminal(options);
13
+ }
14
+ }
@@ -0,0 +1,13 @@
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 const CommandStatus = {
9
+ Running: "Running",
10
+ AwaitingInput: "AwaitingInput",
11
+ Exited: "Exited",
12
+ Killed: "Killed"
13
+ };
@@ -0,0 +1,11 @@
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 const RunMethod = {
9
+ InShell: "InShell",
10
+ OneShot: "OneShot"
11
+ };
@@ -0,0 +1,17 @@
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 { inspect } from "node:util";
9
+ import { TerminalException } from "../../exceptions/terminal.exception.js";
10
+ import { Resources } from "../../resources.js";
11
+ export class AbortReasonUnknownException extends TerminalException {
12
+ reason;
13
+ constructor(reason) {
14
+ super(Resources.formatAbortReasonUnknown(inspect(reason)));
15
+ this.reason = reason;
16
+ }
17
+ }
@@ -0,0 +1,16 @@
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 { TerminalException } from "../../exceptions/terminal.exception.js";
9
+ import { Resources } from "../../resources.js";
10
+ export class StaleTerminalException extends TerminalException {
11
+ terminalId;
12
+ constructor(terminalId) {
13
+ super(Resources.formatTerminalStale(terminalId));
14
+ this.terminalId = terminalId;
15
+ }
16
+ }
@@ -0,0 +1,8 @@
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 {};
@@ -0,0 +1,8 @@
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 {};
@@ -0,0 +1,8 @@
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 {};
@@ -0,0 +1,8 @@
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 {};
@@ -0,0 +1,8 @@
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 {};
@@ -0,0 +1,25 @@
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 { ToolFailureCode } from "@noldova/desktop.runtime";
9
+ import { Resources } from "../../resources.js";
10
+ import { ToolFailures } from "../services/tool-failures.js";
11
+ export class AgentTerminalAccess {
12
+ terminal;
13
+ isShared;
14
+ constructor(terminal, isShared) {
15
+ this.terminal = terminal;
16
+ this.isShared = isShared;
17
+ }
18
+ get origin() {
19
+ return Resources.formatTerminalOrigin(this.terminal.id);
20
+ }
21
+ requireRunning() {
22
+ if (!this.terminal.isRunning)
23
+ throw ToolFailures.create(ToolFailureCode.Unavailable, Resources.endedCode, Resources.formatTerminalEnded(this.terminal.id));
24
+ }
25
+ }
@@ -0,0 +1,29 @@
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 { isAbsolute } from "node:path";
9
+ import { ArgumentException } from "@noldova/context.exceptions";
10
+ import { ArgumentValidator, Resources as ProtocolResources } from "@noldova/terminal.protocol";
11
+ import { Resources } from "../../resources.js";
12
+ export class AgentTerminalOpening {
13
+ ownerSessionId;
14
+ shellId;
15
+ cwd;
16
+ owner;
17
+ constructor(ownerSessionId, shellId, cwd, owner = null) {
18
+ ArgumentValidator.requireIdentifier(Resources.agentTerminalOpeningOwnerSessionIdField, ownerSessionId);
19
+ if (shellId !== null)
20
+ ArgumentValidator.requireIdentifier(Resources.agentTerminalOpeningShellIdField, shellId);
21
+ ArgumentValidator.requireNonBlank(Resources.cwdField, cwd, ProtocolResources.maximumPathLength);
22
+ if (!isAbsolute(cwd))
23
+ throw new ArgumentException(Resources.formatNotAbsolutePath(ProtocolResources.valueSubject), Resources.cwdField);
24
+ this.ownerSessionId = ownerSessionId;
25
+ this.shellId = shellId;
26
+ this.cwd = cwd;
27
+ this.owner = owner;
28
+ }
29
+ }
@@ -0,0 +1,28 @@
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 { ArgumentException } from "@noldova/context.exceptions";
9
+ import { ArgumentValidator, Resources as ProtocolResources } from "@noldova/terminal.protocol";
10
+ import { Resources } from "../../resources.js";
11
+ import { TerminalTextEscaper } from "../services/terminal-text-escaper.js";
12
+ export class CommandLine {
13
+ text;
14
+ constructor(text) {
15
+ ArgumentValidator.requireNonBlank(Resources.commandField, text, Resources.maximumCommandBytes);
16
+ if (Buffer.byteLength(text, Resources.utf8Encoding) > Resources.maximumCommandBytes)
17
+ throw new ArgumentException(Resources.formatTooManyBytes(ProtocolResources.valueSubject, Resources.maximumCommandBytes), Resources.commandField);
18
+ if (TerminalTextEscaper.hasControls(text))
19
+ throw new ArgumentException(Resources.commandHasControls, Resources.commandField);
20
+ this.text = text;
21
+ }
22
+ get display() {
23
+ return TerminalTextEscaper.escape(this.text);
24
+ }
25
+ get hasHiddenCharacters() {
26
+ return TerminalTextEscaper.hasHiddenCharacters(this.text);
27
+ }
28
+ }
@@ -0,0 +1,27 @@
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 { ArgumentException } from "@noldova/context.exceptions";
9
+ import { Resources as ProtocolResources } from "@noldova/terminal.protocol";
10
+ import { Resources } from "../../resources.js";
11
+ import { TerminalTextEscaper } from "../services/terminal-text-escaper.js";
12
+ export class KeySequence {
13
+ text;
14
+ constructor(text) {
15
+ if (text.length === 0)
16
+ throw new ArgumentException(Resources.noKeys, Resources.keysField);
17
+ if (Buffer.byteLength(text, Resources.utf8Encoding) > Resources.maximumKeysBytes)
18
+ throw new ArgumentException(Resources.formatTooManyBytes(ProtocolResources.valueSubject, Resources.maximumKeysBytes), Resources.keysField);
19
+ this.text = text;
20
+ }
21
+ get display() {
22
+ return TerminalTextEscaper.escape(this.text);
23
+ }
24
+ get hasHiddenCharacters() {
25
+ return TerminalTextEscaper.hasHiddenCharacters(this.text);
26
+ }
27
+ }
@@ -0,0 +1,18 @@
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 { ArgumentValidator, Resources as ProtocolResources } from "@noldova/terminal.protocol";
9
+ import { Resources } from "../../resources.js";
10
+ export class OutputChunk {
11
+ text;
12
+ endOffset;
13
+ constructor(text, endOffset) {
14
+ ArgumentValidator.requireIntegerInRange(Resources.outputChunkEndOffsetField, endOffset, 0, ProtocolResources.maximumOffset);
15
+ this.text = text;
16
+ this.endOffset = endOffset;
17
+ }
18
+ }
@@ -0,0 +1,31 @@
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 { ArgumentValidator } from "@noldova/terminal.protocol";
9
+ import { Resources } from "../../resources.js";
10
+ export class OutputExcerpt {
11
+ text;
12
+ omittedCharacters;
13
+ constructor(output, maximumCharacters) {
14
+ ArgumentValidator.requireIntegerInRange(Resources.outputExcerptMaximumCharactersField, maximumCharacters, 1, Resources.maximumToolOutputCharacters);
15
+ if (output.length <= maximumCharacters) {
16
+ this.text = output;
17
+ this.omittedCharacters = 0;
18
+ return;
19
+ }
20
+ const head = OutputExcerpt.trimHead(output.slice(0, Math.ceil(maximumCharacters / 2)));
21
+ const tail = OutputExcerpt.trimTail(output.slice(output.length - Math.floor(maximumCharacters / 2)));
22
+ this.omittedCharacters = output.length - head.length - tail.length;
23
+ this.text = head + Resources.formatOmittedCharacters(this.omittedCharacters) + tail;
24
+ }
25
+ static trimHead(head) {
26
+ return Resources.outputExcerptHighSurrogates.test(head) ? head.slice(0, -1) : head;
27
+ }
28
+ static trimTail(tail) {
29
+ return Resources.outputExcerptLowSurrogates.test(tail) ? tail.slice(1) : tail;
30
+ }
31
+ }
@@ -0,0 +1,34 @@
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 { createHash } from "node:crypto";
9
+ import { PermissionOption, ToolRisk } from "@noldova/desktop.runtime";
10
+ import { Resources } from "../../resources.js";
11
+ export class PermissionAsk {
12
+ static alwaysOptions = [PermissionOption.AllowOnce, PermissionOption.AllowAlways, PermissionOption.Deny];
13
+ static onceOptions = [PermissionOption.AllowOnce, PermissionOption.Deny];
14
+ toolName;
15
+ request;
16
+ constructor(toolName, action, title, risk, details, subject, options = PermissionAsk.alwaysOptions) {
17
+ this.toolName = toolName;
18
+ this.request = {
19
+ action: Resources.formatPermissionAskAction(action),
20
+ subject,
21
+ title,
22
+ details,
23
+ risk,
24
+ isAsking: risk !== ToolRisk.Low,
25
+ options
26
+ };
27
+ }
28
+ static listSubject(parts) {
29
+ return JSON.stringify(parts);
30
+ }
31
+ static hashSubject(parts) {
32
+ return `${Resources.subjectHashPrefix}${createHash(Resources.subjectHashAlgorithm).update(JSON.stringify(parts)).digest(Resources.hexEncoding)}`;
33
+ }
34
+ }
@@ -0,0 +1,26 @@
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 ReadCursor {
9
+ source;
10
+ command;
11
+ offset;
12
+ constructor(source, command, offset) {
13
+ this.source = source;
14
+ this.command = command;
15
+ this.offset = offset;
16
+ }
17
+ static forCommand(command, offset) {
18
+ return new ReadCursor(command, command, offset);
19
+ }
20
+ static forTerminal(terminal, offset) {
21
+ return new ReadCursor(terminal, null, offset);
22
+ }
23
+ advancedTo(offset) {
24
+ return new ReadCursor(this.source, this.command, offset);
25
+ }
26
+ }
@@ -0,0 +1,25 @@
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 ReadCursors {
9
+ bySession = new Map();
10
+ find(agentSessionId, terminalId) {
11
+ return this.bySession.get(agentSessionId)?.get(terminalId);
12
+ }
13
+ set(agentSessionId, terminalId, cursor) {
14
+ const cursors = this.bySession.get(agentSessionId) ?? new Map();
15
+ cursors.set(terminalId, cursor);
16
+ this.bySession.set(agentSessionId, cursors);
17
+ }
18
+ forgetTerminal(terminalId) {
19
+ for (const cursors of this.bySession.values())
20
+ cursors.delete(terminalId);
21
+ }
22
+ forgetSession(agentSessionId) {
23
+ this.bySession.delete(agentSessionId);
24
+ }
25
+ }
@@ -0,0 +1,34 @@
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 { ArgumentValidator, ProtocolReader, Resources as ProtocolResources } from "@noldova/terminal.protocol";
9
+ import { Resources } from "../../resources.js";
10
+ export class TerminalOpenInput {
11
+ static schema = {
12
+ type: Resources.jsonObjectType,
13
+ additionalProperties: false,
14
+ properties: {
15
+ [Resources.shellField]: { type: Resources.jsonStringType, minLength: 1, description: Resources.shellFieldDescription },
16
+ [Resources.explanationField]: { type: Resources.jsonStringType, maxLength: Resources.maximumExplanationLength, description: Resources.explanationFieldDescription }
17
+ }
18
+ };
19
+ shellId;
20
+ explanation;
21
+ constructor(shellId, explanation) {
22
+ if (shellId !== null)
23
+ ArgumentValidator.requireIdentifier(Resources.shellField, shellId);
24
+ if (explanation !== null)
25
+ ArgumentValidator.requireMaximumLength(Resources.explanationField, explanation, Resources.maximumExplanationLength);
26
+ this.shellId = shellId;
27
+ this.explanation = explanation;
28
+ }
29
+ static fromJson(value) {
30
+ const reader = ProtocolReader.fromValue(value);
31
+ reader.allowOnly([Resources.shellField, Resources.explanationField]);
32
+ return new TerminalOpenInput(reader.readOptionalString(Resources.shellField, ProtocolResources.maximumIdentifierLength) ?? null, reader.readOptionalString(Resources.explanationField, Resources.maximumExplanationLength) ?? null);
33
+ }
34
+ }
@@ -0,0 +1,61 @@
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
+ import { CommandStatus } from "../enums/command-status.js";
10
+ import { RunMethod } from "../enums/run-method.js";
11
+ import { ToolResults } from "../services/tool-results.js";
12
+ export class TerminalOutputReport {
13
+ static outputSchema = {
14
+ type: Resources.jsonObjectType,
15
+ additionalProperties: false,
16
+ required: [
17
+ Resources.terminalIdField, Resources.titleField, Resources.terminalOutputReportMethodField, Resources.terminalOutputReportStatusField,
18
+ Resources.terminalOutputReportExitCodeField, Resources.cwdField, Resources.terminalOutputReportOutputField,
19
+ Resources.terminalOutputReportOmittedCharactersField, Resources.terminalOutputReportLogFileField, Resources.terminalOutputReportIsAwaitingSecretField
20
+ ],
21
+ properties: {
22
+ [Resources.terminalIdField]: { type: Resources.jsonStringType },
23
+ [Resources.titleField]: { type: Resources.jsonStringType },
24
+ [Resources.terminalOutputReportMethodField]: { type: [Resources.jsonStringType, Resources.jsonNullType], enum: [...Object.values(RunMethod), null], description: Resources.methodFieldDescription },
25
+ [Resources.terminalOutputReportStatusField]: { type: [Resources.jsonStringType, Resources.jsonNullType], enum: [...Object.values(CommandStatus), null], description: Resources.statusFieldDescription },
26
+ [Resources.terminalOutputReportExitCodeField]: { type: [Resources.jsonIntegerType, Resources.jsonNullType] },
27
+ [Resources.cwdField]: { type: [Resources.jsonStringType, Resources.jsonNullType], description: Resources.cwdFieldDescription },
28
+ [Resources.terminalOutputReportOutputField]: { type: Resources.jsonStringType, description: Resources.outputFieldDescription },
29
+ [Resources.terminalOutputReportOmittedCharactersField]: { type: Resources.jsonIntegerType, minimum: 0 },
30
+ [Resources.terminalOutputReportLogFileField]: { type: [Resources.jsonStringType, Resources.jsonNullType], description: Resources.logFileFieldDescription },
31
+ [Resources.terminalOutputReportIsAwaitingSecretField]: { type: Resources.jsonBooleanType, description: Resources.awaitingSecretFieldDescription }
32
+ }
33
+ };
34
+ access;
35
+ command;
36
+ excerpt;
37
+ cwd;
38
+ constructor(access, command, excerpt, cwd) {
39
+ this.access = access;
40
+ this.command = command;
41
+ this.excerpt = excerpt;
42
+ this.cwd = cwd;
43
+ }
44
+ toResult(note = "") {
45
+ const status = this.command === null ? null : this.command.status;
46
+ const exitCode = this.command === null ? null : this.command.exitCode;
47
+ const isAwaitingSecret = this.access.terminal.isAwaitingSecret;
48
+ return ToolResults.create(this.excerpt.text + Resources.formatCommandSummary(status, exitCode, this.cwd, isAwaitingSecret) + note, {
49
+ [Resources.terminalIdField]: this.access.terminal.id,
50
+ [Resources.titleField]: this.access.terminal.title,
51
+ [Resources.terminalOutputReportMethodField]: this.command === null ? null : this.command.method,
52
+ [Resources.terminalOutputReportStatusField]: status,
53
+ [Resources.terminalOutputReportExitCodeField]: exitCode,
54
+ [Resources.cwdField]: this.cwd,
55
+ [Resources.terminalOutputReportOutputField]: this.excerpt.text,
56
+ [Resources.terminalOutputReportOmittedCharactersField]: this.excerpt.omittedCharacters,
57
+ [Resources.terminalOutputReportLogFileField]: this.command === null ? null : this.command.logFile,
58
+ [Resources.terminalOutputReportIsAwaitingSecretField]: isAwaitingSecret
59
+ }, this.access.origin);
60
+ }
61
+ }
@@ -0,0 +1,39 @@
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 { ArgumentValidator, ProtocolReader, Resources as ProtocolResources } from "@noldova/terminal.protocol";
9
+ import { Resources } from "../../resources.js";
10
+ export class TerminalReadInput {
11
+ static schema = {
12
+ type: Resources.jsonObjectType,
13
+ additionalProperties: false,
14
+ required: [Resources.terminalIdField],
15
+ properties: {
16
+ [Resources.terminalIdField]: { type: Resources.jsonStringType, minLength: 1, description: Resources.terminalIdFieldDescription },
17
+ [Resources.terminalReadInputWaitMsField]: { type: Resources.jsonIntegerType, minimum: 0, default: 0, description: Resources.waitFieldDescription },
18
+ [Resources.maxCharsField]: { type: Resources.jsonIntegerType, minimum: 1, default: Resources.maximumToolOutputCharacters, description: Resources.maxCharsFieldDescription }
19
+ }
20
+ };
21
+ terminalId;
22
+ waitMilliseconds;
23
+ maximumCharacters;
24
+ constructor(terminalId, waitMilliseconds, maximumCharacters) {
25
+ ArgumentValidator.requireIdentifier(Resources.terminalIdField, terminalId);
26
+ ArgumentValidator.requireIntegerInRange(Resources.terminalReadInputWaitMsField, waitMilliseconds, 0, Resources.maximumCallMilliseconds);
27
+ ArgumentValidator.requireIntegerInRange(Resources.maxCharsField, maximumCharacters, 1, Resources.maximumToolOutputCharacters);
28
+ this.terminalId = terminalId;
29
+ this.waitMilliseconds = waitMilliseconds;
30
+ this.maximumCharacters = maximumCharacters;
31
+ }
32
+ static fromJson(value) {
33
+ const reader = ProtocolReader.fromValue(value);
34
+ reader.allowOnly([Resources.terminalIdField, Resources.terminalReadInputWaitMsField, Resources.maxCharsField]);
35
+ const waitMilliseconds = reader.readOptionalInteger(Resources.terminalReadInputWaitMsField, 0, ProtocolResources.maximumOffset) ?? 0;
36
+ const maximumCharacters = reader.readOptionalInteger(Resources.maxCharsField, 1, ProtocolResources.maximumOffset) ?? Resources.maximumToolOutputCharacters;
37
+ return new TerminalReadInput(reader.readNonBlankString(Resources.terminalIdField, ProtocolResources.maximumIdentifierLength), Math.min(waitMilliseconds, Resources.maximumCallMilliseconds), Math.min(maximumCharacters, Resources.maximumToolOutputCharacters));
38
+ }
39
+ }
@@ -0,0 +1,52 @@
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 { ArgumentValidator, ProtocolReader, Resources as ProtocolResources } from "@noldova/terminal.protocol";
9
+ import { Resources } from "../../resources.js";
10
+ import { CommandLine } from "./command-line.js";
11
+ export class TerminalRunInput {
12
+ static schema = {
13
+ type: Resources.jsonObjectType,
14
+ additionalProperties: false,
15
+ required: [Resources.terminalIdField, Resources.commandField],
16
+ properties: {
17
+ [Resources.terminalIdField]: { type: Resources.jsonStringType, minLength: 1, description: Resources.terminalIdFieldDescription },
18
+ [Resources.commandField]: { type: Resources.jsonStringType, minLength: 1, description: Resources.commandFieldDescription },
19
+ [Resources.explanationField]: { type: Resources.jsonStringType, maxLength: Resources.maximumExplanationLength, description: Resources.explanationFieldDescription },
20
+ [Resources.terminalRunInputYieldMsField]: { type: Resources.jsonIntegerType, minimum: 0, default: Resources.defaultYieldMilliseconds, description: Resources.yieldFieldDescription },
21
+ [Resources.terminalRunInputTimeoutMsField]: { type: Resources.jsonIntegerType, minimum: 0, default: 0, description: Resources.timeoutFieldDescription },
22
+ [Resources.maxCharsField]: { type: Resources.jsonIntegerType, minimum: 1, default: Resources.maximumToolOutputCharacters, description: Resources.maxCharsFieldDescription }
23
+ }
24
+ };
25
+ terminalId;
26
+ command;
27
+ explanation;
28
+ yieldMilliseconds;
29
+ timeoutMilliseconds;
30
+ maximumCharacters;
31
+ constructor(terminalId, command, explanation, yieldMilliseconds, timeoutMilliseconds, maximumCharacters) {
32
+ ArgumentValidator.requireIdentifier(Resources.terminalIdField, terminalId);
33
+ if (explanation !== null)
34
+ ArgumentValidator.requireMaximumLength(Resources.explanationField, explanation, Resources.maximumExplanationLength);
35
+ ArgumentValidator.requireIntegerInRange(Resources.terminalRunInputYieldMsField, yieldMilliseconds, Resources.minimumYieldMilliseconds, Resources.maximumCallMilliseconds);
36
+ ArgumentValidator.requireIntegerInRange(Resources.terminalRunInputTimeoutMsField, timeoutMilliseconds, 0, ProtocolResources.maximumOffset);
37
+ ArgumentValidator.requireIntegerInRange(Resources.maxCharsField, maximumCharacters, 1, Resources.maximumToolOutputCharacters);
38
+ this.terminalId = terminalId;
39
+ this.command = command;
40
+ this.explanation = explanation;
41
+ this.yieldMilliseconds = yieldMilliseconds;
42
+ this.timeoutMilliseconds = timeoutMilliseconds;
43
+ this.maximumCharacters = maximumCharacters;
44
+ }
45
+ static fromJson(value) {
46
+ const reader = ProtocolReader.fromValue(value);
47
+ reader.allowOnly([Resources.terminalIdField, Resources.commandField, Resources.explanationField, Resources.terminalRunInputYieldMsField, Resources.terminalRunInputTimeoutMsField, Resources.maxCharsField]);
48
+ const yieldMilliseconds = reader.readOptionalInteger(Resources.terminalRunInputYieldMsField, 0, ProtocolResources.maximumOffset) ?? Resources.defaultYieldMilliseconds;
49
+ const maximumCharacters = reader.readOptionalInteger(Resources.maxCharsField, 1, ProtocolResources.maximumOffset) ?? Resources.maximumToolOutputCharacters;
50
+ return new TerminalRunInput(reader.readNonBlankString(Resources.terminalIdField, ProtocolResources.maximumIdentifierLength), new CommandLine(reader.readString(Resources.commandField, Resources.maximumCommandBytes)), reader.readOptionalString(Resources.explanationField, Resources.maximumExplanationLength) ?? null, Math.min(Math.max(yieldMilliseconds, Resources.minimumYieldMilliseconds), Resources.maximumCallMilliseconds), reader.readOptionalInteger(Resources.terminalRunInputTimeoutMsField, 0, ProtocolResources.maximumOffset) ?? 0, Math.min(maximumCharacters, Resources.maximumToolOutputCharacters));
51
+ }
52
+ }