@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.
- 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,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 TerminalTargetInput {
|
|
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.explanationField]: { type: Resources.jsonStringType, maxLength: Resources.maximumExplanationLength, description: Resources.explanationFieldDescription }
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
terminalId;
|
|
21
|
+
explanation;
|
|
22
|
+
constructor(terminalId, explanation) {
|
|
23
|
+
ArgumentValidator.requireIdentifier(Resources.terminalIdField, terminalId);
|
|
24
|
+
if (explanation !== null)
|
|
25
|
+
ArgumentValidator.requireMaximumLength(Resources.explanationField, explanation, Resources.maximumExplanationLength);
|
|
26
|
+
this.terminalId = terminalId;
|
|
27
|
+
this.explanation = explanation;
|
|
28
|
+
}
|
|
29
|
+
static fromJson(value) {
|
|
30
|
+
const reader = ProtocolReader.fromValue(value);
|
|
31
|
+
reader.allowOnly([Resources.terminalIdField, Resources.explanationField]);
|
|
32
|
+
return new TerminalTargetInput(reader.readNonBlankString(Resources.terminalIdField, ProtocolResources.maximumIdentifierLength), reader.readOptionalString(Resources.explanationField, Resources.maximumExplanationLength) ?? null);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
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 { KeySequence } from "./key-sequence.js";
|
|
11
|
+
export class TerminalWriteInput {
|
|
12
|
+
static schema = {
|
|
13
|
+
type: Resources.jsonObjectType,
|
|
14
|
+
additionalProperties: false,
|
|
15
|
+
required: [Resources.terminalIdField, Resources.keysField],
|
|
16
|
+
properties: {
|
|
17
|
+
[Resources.terminalIdField]: { type: Resources.jsonStringType, minLength: 1, description: Resources.terminalIdFieldDescription },
|
|
18
|
+
[Resources.keysField]: { type: Resources.jsonStringType, minLength: 1, description: Resources.keysFieldDescription },
|
|
19
|
+
[Resources.explanationField]: { type: Resources.jsonStringType, maxLength: Resources.maximumExplanationLength, description: Resources.explanationFieldDescription }
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
terminalId;
|
|
23
|
+
keys;
|
|
24
|
+
explanation;
|
|
25
|
+
constructor(terminalId, keys, explanation) {
|
|
26
|
+
ArgumentValidator.requireIdentifier(Resources.terminalIdField, terminalId);
|
|
27
|
+
if (explanation !== null)
|
|
28
|
+
ArgumentValidator.requireMaximumLength(Resources.explanationField, explanation, Resources.maximumExplanationLength);
|
|
29
|
+
this.terminalId = terminalId;
|
|
30
|
+
this.keys = keys;
|
|
31
|
+
this.explanation = explanation;
|
|
32
|
+
}
|
|
33
|
+
static fromJson(value) {
|
|
34
|
+
const reader = ProtocolReader.fromValue(value);
|
|
35
|
+
reader.allowOnly([Resources.terminalIdField, Resources.keysField, Resources.explanationField]);
|
|
36
|
+
return new TerminalWriteInput(reader.readNonBlankString(Resources.terminalIdField, ProtocolResources.maximumIdentifierLength), new KeySequence(reader.readString(Resources.keysField, Resources.maximumKeysBytes)), reader.readOptionalString(Resources.explanationField, Resources.maximumExplanationLength) ?? null);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -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 { isAbsolute, relative, sep } from "node:path";
|
|
9
|
+
import { ArgumentException } from "@noldova/context.exceptions";
|
|
10
|
+
import { ArgumentValidator, ProtocolReader, Resources as ProtocolResources } from "@noldova/terminal.protocol";
|
|
11
|
+
import { Resources } from "../../resources.js";
|
|
12
|
+
export class ToolCallContext {
|
|
13
|
+
agentSessionId;
|
|
14
|
+
workingFolder;
|
|
15
|
+
boundTerminalIds;
|
|
16
|
+
owner;
|
|
17
|
+
call;
|
|
18
|
+
constructor(call) {
|
|
19
|
+
const reader = ProtocolReader.fromValue(call.bindings);
|
|
20
|
+
const folder = reader.readNonBlankString(Resources.folderBinding, ProtocolResources.maximumPathLength);
|
|
21
|
+
if (!isAbsolute(folder))
|
|
22
|
+
throw new ArgumentException(Resources.formatNotAbsolutePath(ProtocolResources.valueSubject), Resources.folderBinding);
|
|
23
|
+
const shared = new Map(Object.entries(call.bindings)).get(Resources.sharedBinding);
|
|
24
|
+
if (shared !== undefined && !Array.isArray(shared))
|
|
25
|
+
throw new ArgumentException(Resources.formatBindingNotList(ProtocolResources.valueSubject), Resources.sharedBinding);
|
|
26
|
+
const boundTerminalIds = [];
|
|
27
|
+
for (const terminalId of shared ?? []) {
|
|
28
|
+
if (typeof terminalId !== "string")
|
|
29
|
+
throw new ArgumentException(Resources.formatBindingNotList(ProtocolResources.valueSubject), Resources.sharedBinding);
|
|
30
|
+
ArgumentValidator.requireIdentifier(Resources.sharedBinding, terminalId);
|
|
31
|
+
boundTerminalIds.push(terminalId);
|
|
32
|
+
}
|
|
33
|
+
const owner = reader.readOptionalString(Resources.ownerBinding, ProtocolResources.maximumOwnerLength);
|
|
34
|
+
if (owner !== undefined)
|
|
35
|
+
ArgumentValidator.requireNonBlank(Resources.ownerBinding, owner, ProtocolResources.maximumOwnerLength);
|
|
36
|
+
this.call = call;
|
|
37
|
+
this.agentSessionId = call.session;
|
|
38
|
+
this.workingFolder = folder;
|
|
39
|
+
this.boundTerminalIds = boundTerminalIds;
|
|
40
|
+
this.owner = owner ?? null;
|
|
41
|
+
}
|
|
42
|
+
get signal() {
|
|
43
|
+
return this.call.signal;
|
|
44
|
+
}
|
|
45
|
+
get hasMarkedSending() {
|
|
46
|
+
return this.call.hasMarkedSending;
|
|
47
|
+
}
|
|
48
|
+
markSending() {
|
|
49
|
+
this.call.markSending();
|
|
50
|
+
}
|
|
51
|
+
requestPermissionAsync(request) {
|
|
52
|
+
return this.call.requestPermissionAsync(request);
|
|
53
|
+
}
|
|
54
|
+
isBound(terminalId) {
|
|
55
|
+
return this.boundTerminalIds.includes(terminalId);
|
|
56
|
+
}
|
|
57
|
+
isInsideWorkingFolder(folder) {
|
|
58
|
+
const path = relative(this.workingFolder, folder);
|
|
59
|
+
return path === "" || (path !== Resources.toolCallContextParentFolder && !path.startsWith(`${Resources.toolCallContextParentFolder}${sep}`) && !isAbsolute(path));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -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 { ToolAbortReason } from "@noldova/desktop.runtime";
|
|
9
|
+
import { AbortReasonUnknownException } from "../exceptions/abort-reason-unknown.exception.js";
|
|
10
|
+
export class AbortReasonReader {
|
|
11
|
+
static reasons = new Set(Object.values(ToolAbortReason));
|
|
12
|
+
static readReason(signal) {
|
|
13
|
+
const reason = signal.reason;
|
|
14
|
+
if (!AbortReasonReader.isReason(reason))
|
|
15
|
+
throw new AbortReasonUnknownException(reason);
|
|
16
|
+
return reason;
|
|
17
|
+
}
|
|
18
|
+
static interruptsCommand(signal) {
|
|
19
|
+
const reason = AbortReasonReader.readReason(signal);
|
|
20
|
+
return reason === ToolAbortReason.Stopped || reason === ToolAbortReason.Cancelled || reason === ToolAbortReason.SessionEnded;
|
|
21
|
+
}
|
|
22
|
+
static isReason(value) {
|
|
23
|
+
return AbortReasonReader.reasons.has(value);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { PermissionDetailKind } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../../resources.js";
|
|
10
|
+
import { TerminalTextEscaper } from "./terminal-text-escaper.js";
|
|
11
|
+
export class PermissionDetails {
|
|
12
|
+
static text(label, value) {
|
|
13
|
+
return { label, kind: PermissionDetailKind.Text, value };
|
|
14
|
+
}
|
|
15
|
+
static code(label, value) {
|
|
16
|
+
return { label, kind: PermissionDetailKind.Code, value };
|
|
17
|
+
}
|
|
18
|
+
static yesNo(label, value) {
|
|
19
|
+
return PermissionDetails.text(label, value ? Resources.yes : Resources.no);
|
|
20
|
+
}
|
|
21
|
+
static forTerminal(access) {
|
|
22
|
+
return [
|
|
23
|
+
PermissionDetails.text(Resources.terminalLabel, TerminalTextEscaper.escapeUnseen(access.terminal.title)),
|
|
24
|
+
PermissionDetails.code(Resources.terminalIdLabel, access.terminal.id),
|
|
25
|
+
PermissionDetails.yesNo(Resources.sharedLabel, access.isShared)
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
static forShell(access) {
|
|
29
|
+
return [PermissionDetails.text(Resources.shellLabel, access.terminal.shellId), PermissionDetails.code(Resources.shellPathLabel, access.terminal.shellPath)];
|
|
30
|
+
}
|
|
31
|
+
static forFolder(folder, isInsideWorkingFolder) {
|
|
32
|
+
return [
|
|
33
|
+
folder === null
|
|
34
|
+
? PermissionDetails.text(Resources.folderLabel, Resources.unknownFolder)
|
|
35
|
+
: PermissionDetails.code(Resources.folderLabel, TerminalTextEscaper.escapeUnseen(folder)),
|
|
36
|
+
PermissionDetails.yesNo(Resources.insideWorkingFolderLabel, isInsideWorkingFolder)
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
static forExplanation(explanation) {
|
|
40
|
+
return explanation === null ? [] : [{ label: Resources.explanationLabel, kind: PermissionDetailKind.AgentText, value: explanation }];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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 { PermissionDecision, ToolFailureCode } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../../resources.js";
|
|
10
|
+
import { ToolFailures } from "./tool-failures.js";
|
|
11
|
+
export class PermissionGate {
|
|
12
|
+
static failures = {
|
|
13
|
+
[PermissionDecision.AllowedOnce]: null,
|
|
14
|
+
[PermissionDecision.AllowedAlways]: null,
|
|
15
|
+
[PermissionDecision.Denied]: ToolFailureCode.Denied,
|
|
16
|
+
[PermissionDecision.DeniedAlways]: ToolFailureCode.Denied,
|
|
17
|
+
[PermissionDecision.Expired]: ToolFailureCode.Expired,
|
|
18
|
+
[PermissionDecision.Withdrawn]: ToolFailureCode.Withdrawn,
|
|
19
|
+
[PermissionDecision.TakenOver]: ToolFailureCode.Denied
|
|
20
|
+
};
|
|
21
|
+
async requireGrantAsync(ask, context) {
|
|
22
|
+
PermissionGate.requireOpenCall(ask, context);
|
|
23
|
+
if (Buffer.byteLength(JSON.stringify(ask.request.details), Resources.utf8Encoding) > Resources.maximumPermissionDetailBytes)
|
|
24
|
+
throw ToolFailures.create(ToolFailureCode.Refused, Resources.requestTooLargeCode, Resources.formatRequestTooLarge(ask.toolName));
|
|
25
|
+
const outcome = await context.requestPermissionAsync(ask.request);
|
|
26
|
+
const failure = PermissionGate.failures[outcome.decision];
|
|
27
|
+
if (failure !== null)
|
|
28
|
+
throw ToolFailures.create(failure, null, Resources.formatNotGranted(ask.toolName, outcome.decision));
|
|
29
|
+
PermissionGate.requireOpenCall(ask, context);
|
|
30
|
+
}
|
|
31
|
+
static requireOpenCall(ask, context) {
|
|
32
|
+
if (context.signal.aborted)
|
|
33
|
+
throw ToolFailures.forEndedCall(context.signal, Resources.formatCancelled(ask.toolName));
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
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 SignalRace {
|
|
10
|
+
static async raceAsync(task, signal, createError) {
|
|
11
|
+
const { promise: ended, reject } = Promise.withResolvers();
|
|
12
|
+
const onAbort = () => reject(createError());
|
|
13
|
+
if (signal.aborted)
|
|
14
|
+
onAbort();
|
|
15
|
+
else
|
|
16
|
+
signal.addEventListener(Resources.abortEvent, onAbort, { once: true });
|
|
17
|
+
try {
|
|
18
|
+
return await Promise.race([ended, task]);
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
signal.removeEventListener(Resources.abortEvent, onAbort);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -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
|
+
import { ToolFailureCode } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../../resources.js";
|
|
10
|
+
import { AgentTerminalAccess } from "../models/agent-terminal-access.js";
|
|
11
|
+
import { ToolFailures } from "./tool-failures.js";
|
|
12
|
+
export class TerminalAccessPolicy {
|
|
13
|
+
terminals;
|
|
14
|
+
constructor(terminals) {
|
|
15
|
+
this.terminals = terminals;
|
|
16
|
+
}
|
|
17
|
+
resolve(terminalId, context) {
|
|
18
|
+
const terminal = this.terminals.find(terminalId);
|
|
19
|
+
const access = terminal === undefined ? null
|
|
20
|
+
: TerminalAccessPolicy.accessTo(terminal, context, this.ownerOf(context));
|
|
21
|
+
if (access === null)
|
|
22
|
+
throw ToolFailures.create(ToolFailureCode.Refused, Resources.notFoundCode, Resources.formatTerminalNotFound(terminalId));
|
|
23
|
+
return access;
|
|
24
|
+
}
|
|
25
|
+
list(context) {
|
|
26
|
+
const accesses = [];
|
|
27
|
+
const sessionOwner = this.ownerOf(context);
|
|
28
|
+
for (const terminal of this.terminals.list()) {
|
|
29
|
+
const access = TerminalAccessPolicy.accessTo(terminal, context, sessionOwner);
|
|
30
|
+
if (access !== null)
|
|
31
|
+
accesses.push(access);
|
|
32
|
+
}
|
|
33
|
+
return accesses;
|
|
34
|
+
}
|
|
35
|
+
listClosingWith(agentSessionId) {
|
|
36
|
+
return this.terminals.list().filter(t => t.ownerSessionId === agentSessionId && t.owner === null);
|
|
37
|
+
}
|
|
38
|
+
requireStill(access, context) {
|
|
39
|
+
const terminal = this.terminals.find(access.terminal.id);
|
|
40
|
+
const now = terminal === undefined ? null
|
|
41
|
+
: TerminalAccessPolicy.accessTo(terminal, context, this.ownerOf(context));
|
|
42
|
+
if (now === null || now.isShared !== access.isShared)
|
|
43
|
+
throw ToolFailures.create(ToolFailureCode.Stale, Resources.staleCode, Resources.formatTerminalStale(access.terminal.id));
|
|
44
|
+
}
|
|
45
|
+
ownerOf(context) {
|
|
46
|
+
return this.terminals.findSessionOwner(context.agentSessionId) ?? context.owner;
|
|
47
|
+
}
|
|
48
|
+
static accessTo(terminal, context, sessionOwner) {
|
|
49
|
+
if (terminal.ownerSessionId === context.agentSessionId)
|
|
50
|
+
return new AgentTerminalAccess(terminal, false);
|
|
51
|
+
if (terminal.ownerSessionId !== null && sessionOwner !== null && terminal.owner === sessionOwner)
|
|
52
|
+
return new AgentTerminalAccess(terminal, false);
|
|
53
|
+
if (context.isBound(terminal.id))
|
|
54
|
+
return new AgentTerminalAccess(terminal, true);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -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
|
+
import { ToolRisk } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../../resources.js";
|
|
10
|
+
import { PermissionAsk } from "../models/permission-ask.js";
|
|
11
|
+
import { TerminalTargetInput } from "../models/terminal-target-input.js";
|
|
12
|
+
import { PermissionDetails } from "./permission-details.js";
|
|
13
|
+
import { SignalRace } from "./signal-race.js";
|
|
14
|
+
import { TerminalTool } from "./terminal-tool.js";
|
|
15
|
+
import { ToolFailures } from "./tool-failures.js";
|
|
16
|
+
import { ToolResults } from "./tool-results.js";
|
|
17
|
+
export class TerminalCloseTool extends TerminalTool {
|
|
18
|
+
accessPolicy;
|
|
19
|
+
gate;
|
|
20
|
+
cursors;
|
|
21
|
+
descriptor = {
|
|
22
|
+
name: Resources.closeToolName,
|
|
23
|
+
description: Resources.closeToolDescription,
|
|
24
|
+
inputSchema: TerminalTargetInput.schema,
|
|
25
|
+
outputSchema: {
|
|
26
|
+
type: Resources.jsonObjectType,
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
required: [Resources.terminalIdField],
|
|
29
|
+
properties: { [Resources.terminalIdField]: { type: Resources.jsonStringType } }
|
|
30
|
+
},
|
|
31
|
+
hints: { isReadOnly: false, isDestructive: true, isOpenWorld: false },
|
|
32
|
+
risk: { default: ToolRisk.Medium, maximum: ToolRisk.Medium },
|
|
33
|
+
isResumable: false,
|
|
34
|
+
marksSending: true,
|
|
35
|
+
deadline: { default: Resources.closeCallMilliseconds, maximum: Resources.maximumCallMilliseconds },
|
|
36
|
+
resource: Resources.terminalResourceKey,
|
|
37
|
+
nextTool: Resources.listToolName
|
|
38
|
+
};
|
|
39
|
+
constructor(accessPolicy, gate, cursors) {
|
|
40
|
+
super();
|
|
41
|
+
this.accessPolicy = accessPolicy;
|
|
42
|
+
this.gate = gate;
|
|
43
|
+
this.cursors = cursors;
|
|
44
|
+
}
|
|
45
|
+
parseInput(input) {
|
|
46
|
+
return TerminalTargetInput.fromJson(input);
|
|
47
|
+
}
|
|
48
|
+
async runAsync(input, context) {
|
|
49
|
+
const access = this.accessPolicy.resolve(input.terminalId, context);
|
|
50
|
+
await this.gate.requireGrantAsync(new PermissionAsk(Resources.closeToolName, Resources.closeAction, Resources.closeTitle, ToolRisk.Medium, [...PermissionDetails.forTerminal(access), ...PermissionDetails.forExplanation(input.explanation)], PermissionAsk.listSubject([access.terminal.id])), context);
|
|
51
|
+
this.accessPolicy.requireStill(access, context);
|
|
52
|
+
context.markSending();
|
|
53
|
+
await SignalRace.raceAsync(access.terminal.closeAsync(), context.signal, () => ToolFailures.forUncertainOutcome(Resources.formatCloseOutcomeUncertain(input.terminalId)));
|
|
54
|
+
this.cursors.forgetTerminal(input.terminalId);
|
|
55
|
+
return ToolResults.create(Resources.formatTerminalClosed(input.terminalId), { [Resources.terminalIdField]: input.terminalId }, null);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -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
|
+
import { ToolRisk } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../../resources.js";
|
|
10
|
+
import { PermissionAsk } from "../models/permission-ask.js";
|
|
11
|
+
import { TerminalTargetInput } from "../models/terminal-target-input.js";
|
|
12
|
+
import { PermissionDetails } from "./permission-details.js";
|
|
13
|
+
import { TerminalTool } from "./terminal-tool.js";
|
|
14
|
+
import { ToolResults } from "./tool-results.js";
|
|
15
|
+
export class TerminalInterruptTool extends TerminalTool {
|
|
16
|
+
accessPolicy;
|
|
17
|
+
gate;
|
|
18
|
+
descriptor = {
|
|
19
|
+
name: Resources.interruptToolName,
|
|
20
|
+
description: Resources.interruptToolDescription,
|
|
21
|
+
inputSchema: TerminalTargetInput.schema,
|
|
22
|
+
outputSchema: {
|
|
23
|
+
type: Resources.jsonObjectType,
|
|
24
|
+
additionalProperties: false,
|
|
25
|
+
required: [Resources.terminalIdField],
|
|
26
|
+
properties: { [Resources.terminalIdField]: { type: Resources.jsonStringType } }
|
|
27
|
+
},
|
|
28
|
+
hints: { isReadOnly: false, isDestructive: true, isOpenWorld: false },
|
|
29
|
+
risk: { default: ToolRisk.Medium, maximum: ToolRisk.Medium },
|
|
30
|
+
isResumable: false,
|
|
31
|
+
marksSending: true,
|
|
32
|
+
deadline: { default: Resources.shortCallMilliseconds, maximum: Resources.shortCallMilliseconds },
|
|
33
|
+
resource: Resources.terminalResourceKey,
|
|
34
|
+
nextTool: Resources.readToolName
|
|
35
|
+
};
|
|
36
|
+
constructor(accessPolicy, gate) {
|
|
37
|
+
super();
|
|
38
|
+
this.accessPolicy = accessPolicy;
|
|
39
|
+
this.gate = gate;
|
|
40
|
+
}
|
|
41
|
+
parseInput(input) {
|
|
42
|
+
return TerminalTargetInput.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
|
+
await this.gate.requireGrantAsync(new PermissionAsk(Resources.interruptToolName, Resources.interruptAction, Resources.interruptTitle, ToolRisk.Medium, [
|
|
49
|
+
...PermissionDetails.forTerminal(access),
|
|
50
|
+
...PermissionDetails.forFolder(prompt.cwd, prompt.cwd !== null && context.isInsideWorkingFolder(prompt.cwd)),
|
|
51
|
+
...PermissionDetails.forExplanation(input.explanation)
|
|
52
|
+
], PermissionAsk.listSubject([access.terminal.id])), context);
|
|
53
|
+
this.accessPolicy.requireStill(access, context);
|
|
54
|
+
TerminalTool.actOnGrantedState(input.terminalId, context, () => access.terminal.interrupt(prompt));
|
|
55
|
+
return ToolResults.create(Resources.formatInterruptSent(input.terminalId), { [Resources.terminalIdField]: input.terminalId }, null);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { ToolRisk } from "@noldova/desktop.runtime";
|
|
9
|
+
import { ProtocolReader } from "@noldova/terminal.protocol";
|
|
10
|
+
import { Resources } from "../../resources.js";
|
|
11
|
+
import { TerminalTool } from "./terminal-tool.js";
|
|
12
|
+
import { ToolResults } from "./tool-results.js";
|
|
13
|
+
export class TerminalListTool extends TerminalTool {
|
|
14
|
+
accessPolicy;
|
|
15
|
+
descriptor = {
|
|
16
|
+
name: Resources.listToolName,
|
|
17
|
+
description: Resources.listToolDescription,
|
|
18
|
+
inputSchema: { type: Resources.jsonObjectType, additionalProperties: false, properties: {} },
|
|
19
|
+
outputSchema: {
|
|
20
|
+
type: Resources.jsonObjectType,
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
required: [Resources.terminalListToolTerminalsField],
|
|
23
|
+
properties: {
|
|
24
|
+
[Resources.terminalListToolTerminalsField]: {
|
|
25
|
+
type: Resources.jsonArrayType,
|
|
26
|
+
items: {
|
|
27
|
+
type: Resources.jsonObjectType,
|
|
28
|
+
additionalProperties: false,
|
|
29
|
+
required: [Resources.terminalIdField, Resources.titleField, Resources.shellField, Resources.cwdField, Resources.terminalListToolIsSharedField,
|
|
30
|
+
Resources.terminalListToolIsRunningField],
|
|
31
|
+
properties: {
|
|
32
|
+
[Resources.terminalIdField]: { type: Resources.jsonStringType },
|
|
33
|
+
[Resources.titleField]: { type: Resources.jsonStringType },
|
|
34
|
+
[Resources.shellField]: { type: Resources.jsonStringType },
|
|
35
|
+
[Resources.cwdField]: { type: [Resources.jsonStringType, Resources.jsonNullType], description: Resources.cwdFieldDescription },
|
|
36
|
+
[Resources.terminalListToolIsSharedField]: { type: Resources.jsonBooleanType, description: Resources.sharedFieldDescription },
|
|
37
|
+
[Resources.terminalListToolIsRunningField]: { type: Resources.jsonBooleanType }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
hints: { isReadOnly: true, isDestructive: false, isOpenWorld: false },
|
|
44
|
+
risk: { default: ToolRisk.Low, maximum: ToolRisk.Low },
|
|
45
|
+
isResumable: true,
|
|
46
|
+
marksSending: false,
|
|
47
|
+
deadline: { default: Resources.shortCallMilliseconds, maximum: Resources.shortCallMilliseconds }
|
|
48
|
+
};
|
|
49
|
+
constructor(accessPolicy) {
|
|
50
|
+
super();
|
|
51
|
+
this.accessPolicy = accessPolicy;
|
|
52
|
+
}
|
|
53
|
+
parseInput(input) {
|
|
54
|
+
ProtocolReader.fromValue(input).allowOnly([]);
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
async runAsync(_input, context) {
|
|
58
|
+
const accesses = this.accessPolicy.list(context);
|
|
59
|
+
const terminals = accesses.map(t => ({
|
|
60
|
+
[Resources.terminalIdField]: t.terminal.id,
|
|
61
|
+
[Resources.titleField]: t.terminal.title,
|
|
62
|
+
[Resources.shellField]: t.terminal.shellId,
|
|
63
|
+
[Resources.cwdField]: t.terminal.readPrompt().cwd,
|
|
64
|
+
[Resources.terminalListToolIsSharedField]: t.isShared,
|
|
65
|
+
[Resources.terminalListToolIsRunningField]: t.terminal.isRunning
|
|
66
|
+
}));
|
|
67
|
+
return ToolResults.create(Resources.formatTerminalList(accesses.map(t => t.terminal.id)), { [Resources.terminalListToolTerminalsField]: terminals }, Resources.terminalListOrigin);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
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 { ToolRisk } from "@noldova/desktop.runtime";
|
|
9
|
+
import { Resources } from "../../resources.js";
|
|
10
|
+
import { AgentTerminalOpening } from "../models/agent-terminal-opening.js";
|
|
11
|
+
import { PermissionAsk } from "../models/permission-ask.js";
|
|
12
|
+
import { TerminalOpenInput } from "../models/terminal-open-input.js";
|
|
13
|
+
import { PermissionDetails } from "./permission-details.js";
|
|
14
|
+
import { SignalRace } from "./signal-race.js";
|
|
15
|
+
import { TerminalTool } from "./terminal-tool.js";
|
|
16
|
+
import { ToolFailures } from "./tool-failures.js";
|
|
17
|
+
import { ToolResults } from "./tool-results.js";
|
|
18
|
+
export class TerminalOpenTool extends TerminalTool {
|
|
19
|
+
terminals;
|
|
20
|
+
gate;
|
|
21
|
+
descriptor = {
|
|
22
|
+
name: Resources.openToolName,
|
|
23
|
+
description: Resources.openToolDescription,
|
|
24
|
+
inputSchema: TerminalOpenInput.schema,
|
|
25
|
+
outputSchema: {
|
|
26
|
+
type: Resources.jsonObjectType,
|
|
27
|
+
additionalProperties: false,
|
|
28
|
+
required: [Resources.terminalIdField, Resources.titleField, Resources.shellField, Resources.cwdField, Resources.terminalOpenToolHasShellIntegrationField],
|
|
29
|
+
properties: {
|
|
30
|
+
[Resources.terminalIdField]: { type: Resources.jsonStringType },
|
|
31
|
+
[Resources.titleField]: { type: Resources.jsonStringType },
|
|
32
|
+
[Resources.shellField]: { type: Resources.jsonStringType },
|
|
33
|
+
[Resources.cwdField]: { type: Resources.jsonStringType },
|
|
34
|
+
[Resources.terminalOpenToolHasShellIntegrationField]: { type: Resources.jsonBooleanType, description: Resources.integrationFieldDescription }
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
hints: { isReadOnly: false, isDestructive: false, isOpenWorld: false },
|
|
38
|
+
risk: { default: ToolRisk.Medium, maximum: ToolRisk.Medium },
|
|
39
|
+
isResumable: false,
|
|
40
|
+
marksSending: true,
|
|
41
|
+
deadline: { default: Resources.openCallMilliseconds, maximum: Resources.maximumCallMilliseconds },
|
|
42
|
+
nextTool: Resources.listToolName
|
|
43
|
+
};
|
|
44
|
+
constructor(terminals, gate) {
|
|
45
|
+
super();
|
|
46
|
+
this.terminals = terminals;
|
|
47
|
+
this.gate = gate;
|
|
48
|
+
}
|
|
49
|
+
parseInput(input) {
|
|
50
|
+
return TerminalOpenInput.fromJson(input);
|
|
51
|
+
}
|
|
52
|
+
async runAsync(input, context) {
|
|
53
|
+
await this.gate.requireGrantAsync(TerminalOpenTool.askFor(input, context), context);
|
|
54
|
+
context.markSending();
|
|
55
|
+
const terminal = await SignalRace.raceAsync(this.terminals.openAsync(new AgentTerminalOpening(context.agentSessionId, input.shellId, context.workingFolder, context.owner), context.signal), context.signal, () => ToolFailures.forUncertainOutcome(Resources.openOutcomeUncertainMessage));
|
|
56
|
+
return ToolResults.create(Resources.formatTerminalOpened(terminal.id, terminal.shellId, context.workingFolder), {
|
|
57
|
+
[Resources.terminalIdField]: terminal.id,
|
|
58
|
+
[Resources.titleField]: terminal.title,
|
|
59
|
+
[Resources.shellField]: terminal.shellId,
|
|
60
|
+
[Resources.cwdField]: context.workingFolder,
|
|
61
|
+
[Resources.terminalOpenToolHasShellIntegrationField]: terminal.hasShellIntegration
|
|
62
|
+
}, Resources.formatTerminalOrigin(terminal.id));
|
|
63
|
+
}
|
|
64
|
+
static askFor(input, context) {
|
|
65
|
+
return new PermissionAsk(Resources.openToolName, Resources.openAction, Resources.openTitle, ToolRisk.Medium, [
|
|
66
|
+
PermissionDetails.text(Resources.shellLabel, input.shellId ?? Resources.defaultShell),
|
|
67
|
+
...PermissionDetails.forFolder(context.workingFolder, true),
|
|
68
|
+
...PermissionDetails.forExplanation(input.explanation)
|
|
69
|
+
], PermissionAsk.listSubject([input.shellId]));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 { 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 { TerminalReadInput } from "../models/terminal-read-input.js";
|
|
15
|
+
import { PermissionDetails } from "./permission-details.js";
|
|
16
|
+
import { TerminalTool } from "./terminal-tool.js";
|
|
17
|
+
export class TerminalReadTool extends TerminalTool {
|
|
18
|
+
accessPolicy;
|
|
19
|
+
gate;
|
|
20
|
+
cursors;
|
|
21
|
+
descriptor = {
|
|
22
|
+
name: Resources.readToolName,
|
|
23
|
+
description: Resources.readToolDescription,
|
|
24
|
+
inputSchema: TerminalReadInput.schema,
|
|
25
|
+
outputSchema: TerminalOutputReport.outputSchema,
|
|
26
|
+
hints: { isReadOnly: true, isDestructive: false, isOpenWorld: false },
|
|
27
|
+
risk: { default: ToolRisk.Low, maximum: ToolRisk.Medium },
|
|
28
|
+
isResumable: true,
|
|
29
|
+
marksSending: false,
|
|
30
|
+
deadline: { default: Resources.shortCallMilliseconds, maximum: Resources.maximumCallMilliseconds }
|
|
31
|
+
};
|
|
32
|
+
constructor(accessPolicy, gate, cursors) {
|
|
33
|
+
super();
|
|
34
|
+
this.accessPolicy = accessPolicy;
|
|
35
|
+
this.gate = gate;
|
|
36
|
+
this.cursors = cursors;
|
|
37
|
+
}
|
|
38
|
+
parseInput(input) {
|
|
39
|
+
return TerminalReadInput.fromJson(input);
|
|
40
|
+
}
|
|
41
|
+
async runAsync(input, context) {
|
|
42
|
+
const access = this.accessPolicy.resolve(input.terminalId, context);
|
|
43
|
+
await this.gate.requireGrantAsync(TerminalReadTool.askFor(access), context);
|
|
44
|
+
this.accessPolicy.requireStill(access, context);
|
|
45
|
+
const cursor = this.cursors.find(context.agentSessionId, input.terminalId) ?? ReadCursor.forTerminal(access.terminal, 0);
|
|
46
|
+
await cursor.source.waitForOutputAsync(cursor.offset, input.waitMilliseconds, context.signal);
|
|
47
|
+
const chunk = cursor.source.readSince(cursor.offset);
|
|
48
|
+
const cwd = cursor.command === null ? access.terminal.readPrompt().cwd : cursor.command.cwd;
|
|
49
|
+
const report = new TerminalOutputReport(access, cursor.command, new OutputExcerpt(chunk.text, input.maximumCharacters), cwd);
|
|
50
|
+
if (context.signal.aborted)
|
|
51
|
+
throw TerminalTool.failEndedCall(Resources.readToolName, cursor.command, report, context.signal);
|
|
52
|
+
this.cursors.set(context.agentSessionId, input.terminalId, cursor.advancedTo(chunk.endOffset));
|
|
53
|
+
return report.toResult();
|
|
54
|
+
}
|
|
55
|
+
static askFor(access) {
|
|
56
|
+
return new PermissionAsk(Resources.readToolName, Resources.readAction, Resources.readTitle, access.isShared ? ToolRisk.Medium : ToolRisk.Low, PermissionDetails.forTerminal(access), PermissionAsk.listSubject([access.terminal.id]));
|
|
57
|
+
}
|
|
58
|
+
}
|