@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,78 @@
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, ToolRisk } from "@noldova/desktop.runtime";
9
+ import { Resources } from "../../resources.js";
10
+ import { OutputExcerpt } from "../models/output-excerpt.js";
11
+ import { PermissionAsk } from "../models/permission-ask.js";
12
+ import { ReadCursor } from "../models/read-cursor.js";
13
+ import { TerminalOutputReport } from "../models/terminal-output-report.js";
14
+ import { TerminalRunInput } from "../models/terminal-run-input.js";
15
+ import { PermissionDetails } from "./permission-details.js";
16
+ import { TerminalTextEscaper } from "./terminal-text-escaper.js";
17
+ import { TerminalTool } from "./terminal-tool.js";
18
+ import { ToolFailures } from "./tool-failures.js";
19
+ export class TerminalRunTool extends TerminalTool {
20
+ accessPolicy;
21
+ gate;
22
+ cursors;
23
+ descriptor = {
24
+ name: Resources.runToolName,
25
+ description: Resources.runToolDescription,
26
+ inputSchema: TerminalRunInput.schema,
27
+ outputSchema: TerminalOutputReport.outputSchema,
28
+ hints: { isReadOnly: false, isDestructive: true, isOpenWorld: true },
29
+ risk: { default: ToolRisk.High, maximum: ToolRisk.High },
30
+ isResumable: true,
31
+ marksSending: true,
32
+ deadline: { default: Resources.defaultYieldMilliseconds, maximum: Resources.maximumCallMilliseconds },
33
+ resource: Resources.terminalResourceKey
34
+ };
35
+ constructor(accessPolicy, gate, cursors) {
36
+ super();
37
+ this.accessPolicy = accessPolicy;
38
+ this.gate = gate;
39
+ this.cursors = cursors;
40
+ }
41
+ parseInput(input) {
42
+ return TerminalRunInput.fromJson(input);
43
+ }
44
+ async runAsync(input, context) {
45
+ const access = this.accessPolicy.resolve(input.terminalId, context);
46
+ access.requireRunning();
47
+ const prompt = access.terminal.readPrompt();
48
+ if (!prompt.isIdle)
49
+ throw ToolFailures.create(ToolFailureCode.Unavailable, Resources.notIdleCode, Resources.formatTerminalNotIdle(input.terminalId));
50
+ await this.gate.requireGrantAsync(TerminalRunTool.askFor(input, access, prompt, context), context);
51
+ this.accessPolicy.requireStill(access, context);
52
+ const command = TerminalTool.actOnGrantedState(input.terminalId, context, () => access.terminal.run(input.command.text, prompt, input.timeoutMilliseconds));
53
+ await command.waitForEndAsync(input.yieldMilliseconds, context.signal);
54
+ const chunk = command.readSince(0);
55
+ this.cursors.set(context.agentSessionId, input.terminalId, ReadCursor.forCommand(command, context.signal.aborted ? 0 : chunk.endOffset));
56
+ const report = new TerminalOutputReport(access, command, new OutputExcerpt(chunk.text, input.maximumCharacters), command.cwd);
57
+ if (context.signal.aborted)
58
+ throw TerminalTool.failEndedCall(Resources.runToolName, command, report, context.signal);
59
+ return report.toResult();
60
+ }
61
+ static readOneShotLine(terminal, command) {
62
+ const line = terminal.readOneShotLine(command.text);
63
+ return line === null ? null : TerminalTextEscaper.escapeHidden(line);
64
+ }
65
+ static askFor(input, access, prompt, context) {
66
+ const oneShotLine = TerminalRunTool.readOneShotLine(access.terminal, input.command);
67
+ const folder = oneShotLine === null ? prompt.cwd : access.terminal.startFolder;
68
+ return new PermissionAsk(Resources.runToolName, Resources.runAction, Resources.runTitle, ToolRisk.High, [
69
+ ...PermissionDetails.forTerminal(access),
70
+ ...PermissionDetails.forShell(access),
71
+ ...PermissionDetails.forFolder(folder, folder !== null && context.isInsideWorkingFolder(folder)),
72
+ PermissionDetails.code(Resources.commandLabel, input.command.display),
73
+ PermissionDetails.yesNo(Resources.hiddenCharactersLabel, input.command.hasHiddenCharacters),
74
+ oneShotLine === null ? PermissionDetails.text(Resources.methodLabel, Resources.inShellMethod) : PermissionDetails.code(Resources.oneShotLineLabel, oneShotLine),
75
+ ...PermissionDetails.forExplanation(input.explanation)
76
+ ], PermissionAsk.hashSubject([access.terminal.id, folder, input.command.text]));
77
+ }
78
+ }
@@ -0,0 +1,40 @@
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 TerminalTextEscaper {
10
+ static hasControls(text) {
11
+ return Resources.terminalTextEscaperControls.test(text);
12
+ }
13
+ static hasHiddenCharacters(text) {
14
+ return Resources.terminalTextEscaperHiddenCharacters.test(text);
15
+ }
16
+ static escape(text) {
17
+ let escaped = "";
18
+ for (const character of text)
19
+ escaped += TerminalTextEscaper.escapeCharacter(character);
20
+ return escaped;
21
+ }
22
+ static escapeHidden(text) {
23
+ return text.replaceAll(new RegExp(Resources.terminalTextEscaperHiddenCharacters.source, Resources.terminalTextEscaperSearchFlags), t => TerminalTextEscaper.formatCodePoint(t));
24
+ }
25
+ static escapeUnseen(text) {
26
+ return text.replaceAll(new RegExp(Resources.formatTerminalTextEscaperAlternation(Resources.terminalTextEscaperControls.source, Resources.terminalTextEscaperHiddenCharacters.source), Resources.terminalTextEscaperSearchFlags), t => TerminalTextEscaper.formatCodePoint(t));
27
+ }
28
+ static escapeCharacter(character) {
29
+ const name = Resources.terminalTextEscaperNamed.get(character);
30
+ if (name !== undefined)
31
+ return name;
32
+ if (TerminalTextEscaper.hasControls(character) || TerminalTextEscaper.hasHiddenCharacters(character))
33
+ return TerminalTextEscaper.formatCodePoint(character);
34
+ return character;
35
+ }
36
+ static formatCodePoint(character) {
37
+ return Resources.formatTerminalTextEscaperCodePoint(Number(character.codePointAt(0)).toString(Resources.hexadecimalRadix).toUpperCase()
38
+ .padStart(Resources.terminalTextEscaperCodePointDigits, Resources.terminalTextEscaperCodePointPad));
39
+ }
40
+ }
@@ -0,0 +1,47 @@
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 { JsonException } from "@noldova/context.json";
10
+ import { ToolFailureCode } from "@noldova/desktop.runtime";
11
+ import { Resources } from "../../resources.js";
12
+ import { StaleTerminalException } from "../exceptions/stale-terminal.exception.js";
13
+ import { ToolCallContext } from "../models/tool-call-context.js";
14
+ import { AbortReasonReader } from "./abort-reason-reader.js";
15
+ import { ToolFailures } from "./tool-failures.js";
16
+ export class TerminalTool {
17
+ async handleAsync(call) {
18
+ const input = TerminalTool.refuseInvalid(Resources.invalidInputCode, () => this.parseInput(call.arguments));
19
+ return await this.runAsync(input, TerminalTool.refuseInvalid(Resources.invalidBindingCode, () => new ToolCallContext(call)));
20
+ }
21
+ static actOnGrantedState(terminalId, context, action) {
22
+ context.markSending();
23
+ try {
24
+ return action();
25
+ }
26
+ catch (error) {
27
+ if (error instanceof StaleTerminalException)
28
+ throw ToolFailures.create(ToolFailureCode.Stale, Resources.staleCode, Resources.formatTerminalStale(terminalId));
29
+ throw error;
30
+ }
31
+ }
32
+ static failEndedCall(action, command, report, signal) {
33
+ if (command !== null && AbortReasonReader.interruptsCommand(signal) && command.interrupt())
34
+ return ToolFailures.forEndedCall(signal, Resources.formatInterruptedWithOutput(action), report.toResult(Resources.interruptedNote));
35
+ return ToolFailures.forEndedCall(signal, Resources.formatEndedWithOutput(action), report.toResult());
36
+ }
37
+ static refuseInvalid(moduleCode, read) {
38
+ try {
39
+ return read();
40
+ }
41
+ catch (error) {
42
+ if (error instanceof JsonException || error instanceof ArgumentException)
43
+ throw ToolFailures.create(ToolFailureCode.Refused, moduleCode, error.message);
44
+ throw error;
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,60 @@
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 { ReadCursors } from "../models/read-cursors.js";
10
+ import { PermissionGate } from "./permission-gate.js";
11
+ import { TerminalAccessPolicy } from "./terminal-access-policy.js";
12
+ import { TerminalCloseTool } from "./terminal-close-tool.js";
13
+ import { TerminalInterruptTool } from "./terminal-interrupt-tool.js";
14
+ import { TerminalListTool } from "./terminal-list-tool.js";
15
+ import { TerminalOpenTool } from "./terminal-open-tool.js";
16
+ import { TerminalReadTool } from "./terminal-read-tool.js";
17
+ import { TerminalRunTool } from "./terminal-run-tool.js";
18
+ import { TerminalWriteTool } from "./terminal-write-tool.js";
19
+ export class TerminalTools {
20
+ accessPolicy;
21
+ terminals;
22
+ cursors = new ReadCursors();
23
+ registrations = [];
24
+ tools;
25
+ constructor(terminals) {
26
+ const accessPolicy = new TerminalAccessPolicy(terminals);
27
+ const gate = new PermissionGate();
28
+ this.accessPolicy = accessPolicy;
29
+ this.terminals = terminals;
30
+ this.tools = [
31
+ new TerminalListTool(accessPolicy),
32
+ new TerminalReadTool(accessPolicy, gate, this.cursors),
33
+ new TerminalOpenTool(terminals, gate),
34
+ new TerminalRunTool(accessPolicy, gate, this.cursors),
35
+ new TerminalWriteTool(accessPolicy, gate),
36
+ new TerminalInterruptTool(accessPolicy, gate),
37
+ new TerminalCloseTool(accessPolicy, gate, this.cursors)
38
+ ];
39
+ }
40
+ get toolNames() {
41
+ return this.tools.map(t => t.descriptor.name);
42
+ }
43
+ publish(tools, log) {
44
+ for (const tool of this.tools)
45
+ this.registrations.push(tools.register(tool.descriptor, tool));
46
+ this.registrations.push(tools.onSessionEnded(t => void this.endSessionAsync(t).catch((error) => log.write(Resources.formatSessionEndFailed(t, error instanceof Error ? error.message : String(error))))));
47
+ }
48
+ dispose() {
49
+ for (const registration of this.registrations.splice(0))
50
+ registration[Symbol.dispose]();
51
+ }
52
+ async endSessionAsync(agentSessionId) {
53
+ const owned = this.accessPolicy.listClosingWith(agentSessionId);
54
+ this.cursors.forgetSession(agentSessionId);
55
+ this.terminals.endSession(agentSessionId);
56
+ await Promise.all(owned.map(t => t.closeAsync()));
57
+ for (const terminal of owned)
58
+ this.cursors.forgetTerminal(terminal.id);
59
+ }
60
+ }
@@ -0,0 +1,75 @@
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, ToolRisk } from "@noldova/desktop.runtime";
9
+ import { Resources } from "../../resources.js";
10
+ import { PermissionAsk } from "../models/permission-ask.js";
11
+ import { TerminalWriteInput } from "../models/terminal-write-input.js";
12
+ import { PermissionDetails } from "./permission-details.js";
13
+ import { TerminalTool } from "./terminal-tool.js";
14
+ import { ToolFailures } from "./tool-failures.js";
15
+ import { ToolResults } from "./tool-results.js";
16
+ export class TerminalWriteTool extends TerminalTool {
17
+ accessPolicy;
18
+ gate;
19
+ descriptor = {
20
+ name: Resources.writeToolName,
21
+ description: Resources.writeToolDescription,
22
+ inputSchema: TerminalWriteInput.schema,
23
+ outputSchema: {
24
+ type: Resources.jsonObjectType,
25
+ additionalProperties: false,
26
+ required: [Resources.terminalIdField, Resources.titleField],
27
+ properties: { [Resources.terminalIdField]: { type: Resources.jsonStringType }, [Resources.titleField]: { type: Resources.jsonStringType } }
28
+ },
29
+ hints: { isReadOnly: false, isDestructive: true, isOpenWorld: true },
30
+ risk: { default: ToolRisk.High, maximum: ToolRisk.High },
31
+ isResumable: false,
32
+ marksSending: true,
33
+ deadline: { default: Resources.shortCallMilliseconds, maximum: Resources.shortCallMilliseconds },
34
+ resource: Resources.terminalResourceKey,
35
+ nextTool: Resources.readToolName
36
+ };
37
+ constructor(accessPolicy, gate) {
38
+ super();
39
+ this.accessPolicy = accessPolicy;
40
+ this.gate = gate;
41
+ }
42
+ parseInput(input) {
43
+ return TerminalWriteInput.fromJson(input);
44
+ }
45
+ async runAsync(input, context) {
46
+ const access = this.accessPolicy.resolve(input.terminalId, context);
47
+ access.requireRunning();
48
+ TerminalWriteTool.refuseSecret(access);
49
+ TerminalWriteTool.refusePendingLine(access);
50
+ const prompt = access.terminal.readPrompt();
51
+ await this.gate.requireGrantAsync(TerminalWriteTool.askFor(input, access, prompt, context), context);
52
+ this.accessPolicy.requireStill(access, context);
53
+ TerminalTool.actOnGrantedState(input.terminalId, context, () => access.terminal.write(input.keys.text, prompt));
54
+ return ToolResults.create(Resources.formatKeysSent(input.terminalId), { [Resources.terminalIdField]: input.terminalId, [Resources.titleField]: access.terminal.title }, access.origin);
55
+ }
56
+ static refuseSecret(access) {
57
+ if (access.terminal.isAwaitingSecret)
58
+ throw ToolFailures.create(ToolFailureCode.Refused, Resources.awaitingSecretCode, Resources.formatAwaitingSecret(access.terminal.id));
59
+ }
60
+ static refusePendingLine(access) {
61
+ if (access.terminal.hasPendingLine)
62
+ throw ToolFailures.create(ToolFailureCode.Unavailable, Resources.lineInUseCode, Resources.formatLineInUse(access.terminal.id));
63
+ }
64
+ static askFor(input, access, prompt, context) {
65
+ return new PermissionAsk(Resources.writeToolName, Resources.writeAction, Resources.writeTitle, ToolRisk.High, [
66
+ ...PermissionDetails.forTerminal(access),
67
+ ...PermissionDetails.forShell(access),
68
+ ...PermissionDetails.forFolder(prompt.cwd, prompt.cwd !== null && context.isInsideWorkingFolder(prompt.cwd)),
69
+ PermissionDetails.yesNo(Resources.atPromptLabel, prompt.isIdle),
70
+ PermissionDetails.code(Resources.keysLabel, input.keys.display),
71
+ PermissionDetails.yesNo(Resources.hiddenCharactersLabel, input.keys.hasHiddenCharacters),
72
+ ...PermissionDetails.forExplanation(input.explanation)
73
+ ], PermissionAsk.hashSubject([access.terminal.id, prompt.cwd, input.keys.text]), PermissionAsk.onceOptions);
74
+ }
75
+ }
@@ -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 { ToolAbortReason, ToolFailure, ToolFailureCode, ToolFailureException } from "@noldova/desktop.runtime";
9
+ import { Resources } from "../../resources.js";
10
+ import { AbortReasonReader } from "./abort-reason-reader.js";
11
+ export class ToolFailures {
12
+ static endedCodes = {
13
+ [ToolAbortReason.Stopped]: ToolFailureCode.Stopped,
14
+ [ToolAbortReason.Cancelled]: ToolFailureCode.Cancelled,
15
+ [ToolAbortReason.SessionEnded]: ToolFailureCode.Cancelled,
16
+ [ToolAbortReason.SwitchedOff]: ToolFailureCode.Unavailable,
17
+ [ToolAbortReason.DeadlineReached]: ToolFailureCode.TimedOut
18
+ };
19
+ static create(code, moduleCode, message, output = null) {
20
+ return new ToolFailureException(new ToolFailure(code, message, moduleCode, output?.content ?? []));
21
+ }
22
+ static forEndedCall(signal, message, output = null) {
23
+ return ToolFailures.create(ToolFailures.endedCodes[AbortReasonReader.readReason(signal)], null, message, output);
24
+ }
25
+ static forUncertainOutcome(message) {
26
+ return ToolFailures.create(ToolFailureCode.OutcomeUncertain, Resources.uncertainCode, message);
27
+ }
28
+ }
@@ -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
+ import { ToolContent, ToolResult } from "@noldova/desktop.runtime";
9
+ export class ToolResults {
10
+ static create(text, structured, origin) {
11
+ return new ToolResult([ToolContent.fromText(text, origin)], structured);
12
+ }
13
+ }