vanspod 0.4.15 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/classroomApp.js +185 -56
- package/dist/classroomKeyRejection.js +1 -1
- package/dist/codexWebSearch.js +1 -1
- package/dist/desktop.js +13 -3
- package/dist/deviceConnection.js +13 -2
- package/dist/html.js +12 -0
- package/dist/legacyMigration.js +10 -44
- package/dist/localCommands.js +82 -119
- package/dist/localToolApproval.js +81 -4
- package/dist/localTools.js +31 -8
- package/dist/modelStatus.js +14 -12
- package/dist/page.js +322 -132
- package/dist/platformStorage.js +1 -1
- package/dist/remoteAgentLog.js +126 -0
- package/dist/remoteAgentLogView.js +421 -0
- package/dist/saveImage.js +26 -0
- package/dist/secureStorage.js +17 -5
- package/dist/settingsActions.js +2 -10
- package/dist/start.js +86 -49
- package/dist/storageErrors.js +4 -5
- package/dist/trayChannel.js +25 -6
- package/dist/version.js +1 -1
- package/package.json +2 -2
- package/scripts/tray.ps1 +141 -11
package/dist/localCommands.js
CHANGED
|
@@ -4,11 +4,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.MAX_OUTPUT_CHARS = exports.BASH_TIMEOUT_MS = void 0;
|
|
7
|
-
exports.
|
|
7
|
+
exports.runBashCommand = runBashCommand;
|
|
8
8
|
exports.bashTool = bashTool;
|
|
9
9
|
// The `bash` Local Tool (ADR 0013): one command in Git Bash, from the Project Folder, after a
|
|
10
|
-
// Local Tool Approval.
|
|
11
|
-
// like Prompt Capture. The page lists it and can stop a running command.
|
|
10
|
+
// Local Tool Approval. The Remote Agent Log keeps each call and can stop a running command.
|
|
12
11
|
const node_child_process_1 = require("node:child_process");
|
|
13
12
|
const node_fs_1 = require("node:fs");
|
|
14
13
|
const node_os_1 = __importDefault(require("node:os"));
|
|
@@ -21,8 +20,6 @@ exports.BASH_TIMEOUT_MS = 60_000;
|
|
|
21
20
|
exports.MAX_OUTPUT_CHARS = 30_000;
|
|
22
21
|
// A UTF-8 character has at most 4 bytes. Keep this many bytes from the start and from the end.
|
|
23
22
|
const KEEP_BYTES = exports.MAX_OUTPUT_CHARS * 4;
|
|
24
|
-
/** The Command Log keeps the newest commands only. */
|
|
25
|
-
const MAX_RECORDS = 200;
|
|
26
23
|
function collector() {
|
|
27
24
|
return { head: [], headBytes: 0, tail: [], tailBytes: 0, total: 0 };
|
|
28
25
|
}
|
|
@@ -91,120 +88,86 @@ function stopProcesses(bash, child, pidFile) {
|
|
|
91
88
|
.on("error", () => (0, commandRunner_1.killTree)(child))
|
|
92
89
|
.on("exit", () => (0, commandRunner_1.killTree)(child));
|
|
93
90
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
record.endedAt = Date.now();
|
|
172
|
-
if (ended.error) {
|
|
173
|
-
record.state = "failed";
|
|
174
|
-
notify();
|
|
175
|
-
return { ok: false, error: "failed", message: `VansPod 無法啟動 Git Bash:${ended.error}。` };
|
|
176
|
-
}
|
|
177
|
-
record.state = ended.why === "timed_out" ? "timed_out" : ended.why ? "stopped" : "exited";
|
|
178
|
-
if (!ended.why && ended.code !== undefined)
|
|
179
|
-
record.exitCode = ended.code;
|
|
180
|
-
notify();
|
|
181
|
-
const output = shareOutput(streamText(stdout), streamText(stderr));
|
|
182
|
-
const result = { exit_code: ended.why ? null : ended.code ?? null, stdout: output.stdout, stderr: output.stderr };
|
|
183
|
-
const notes = [];
|
|
184
|
-
if (ended.why === "timed_out") {
|
|
185
|
-
result.timed_out = true;
|
|
186
|
-
notes.push(`指令執行超過 ${Math.round(timeoutMs / 1000)} 秒,VansPod 已停止它。下面是停止前的輸出。`);
|
|
187
|
-
}
|
|
188
|
-
if (ended.why === "stopped") {
|
|
189
|
-
result.stopped = true;
|
|
190
|
-
notes.push("學生在 VansPod 頁面停止了這個指令。下面是停止前的輸出。");
|
|
191
|
-
}
|
|
192
|
-
if (ended.why === "cancelled") {
|
|
193
|
-
result.stopped = true;
|
|
194
|
-
notes.push("這個呼叫已被取消,VansPod 已停止這個指令。");
|
|
195
|
-
}
|
|
196
|
-
if (output.truncated) {
|
|
197
|
-
result.truncated = true;
|
|
198
|
-
notes.push(`輸出太長,只回傳開頭和結尾,共約 ${exports.MAX_OUTPUT_CHARS.toLocaleString("en-US")} 個字元。`);
|
|
199
|
-
}
|
|
200
|
-
if (notes.length > 0)
|
|
201
|
-
result.note = notes.join(" ");
|
|
202
|
-
return { ok: true, result };
|
|
203
|
-
},
|
|
204
|
-
};
|
|
91
|
+
let nextRun = 0;
|
|
92
|
+
/**
|
|
93
|
+
* Runs one approved command and answers what the Agent receives. `stoppable` gets the function
|
|
94
|
+
* that stops the command as soon as it runs; `ended` hears how it ended, before the answer.
|
|
95
|
+
*/
|
|
96
|
+
async function runBashCommand(bash, command, folder, signal, options) {
|
|
97
|
+
const timeoutMs = options.timeoutMs;
|
|
98
|
+
const env = await (0, systemPath_1.envWithRegistryPath)();
|
|
99
|
+
// From here to the spawn nothing waits, so a listed running command can always be stopped.
|
|
100
|
+
const stdout = collector();
|
|
101
|
+
const stderr = collector();
|
|
102
|
+
const ended = await new Promise(resolve => {
|
|
103
|
+
let why;
|
|
104
|
+
let settled = false;
|
|
105
|
+
// The command and the pid file go in the environment, so no Windows argument quoting or globbing changes them.
|
|
106
|
+
const pidFile = node_path_1.default.join(node_os_1.default.tmpdir(), `vanspod-bash-${process.pid}-${++nextRun}.pid`);
|
|
107
|
+
const child = (0, node_child_process_1.spawn)(bash, ["-c", RUN_SCRIPT], {
|
|
108
|
+
cwd: folder,
|
|
109
|
+
env: { ...env, VANSPOD_BASH_COMMAND: command, VANSPOD_BASH_PID_FILE: pidFile },
|
|
110
|
+
windowsHide: true,
|
|
111
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
112
|
+
});
|
|
113
|
+
const finish = (result) => {
|
|
114
|
+
if (settled)
|
|
115
|
+
return;
|
|
116
|
+
settled = true;
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
signal.removeEventListener("abort", onCancel);
|
|
119
|
+
(0, node_fs_1.rmSync)(pidFile, { force: true });
|
|
120
|
+
resolve({ ...result, ...(why ? { why } : {}) });
|
|
121
|
+
};
|
|
122
|
+
const end = (reason) => {
|
|
123
|
+
if (why || settled)
|
|
124
|
+
return;
|
|
125
|
+
why = reason;
|
|
126
|
+
stopProcesses(bash, child, pidFile);
|
|
127
|
+
// A grandchild can hold the pipes open; do not wait for it forever.
|
|
128
|
+
setTimeout(() => finish({}), 3_000).unref();
|
|
129
|
+
};
|
|
130
|
+
const timer = setTimeout(() => end("timed_out"), timeoutMs);
|
|
131
|
+
const onCancel = () => end("cancelled");
|
|
132
|
+
signal.addEventListener("abort", onCancel, { once: true });
|
|
133
|
+
if (signal.aborted)
|
|
134
|
+
onCancel();
|
|
135
|
+
child.stdout.on("data", (chunk) => collect(stdout, chunk));
|
|
136
|
+
child.stderr.on("data", (chunk) => collect(stderr, chunk));
|
|
137
|
+
child.on("error", error => finish({ error: error.message }));
|
|
138
|
+
child.on("close", code => finish(code === null ? {} : { code }));
|
|
139
|
+
options.stoppable(() => end("stopped"));
|
|
140
|
+
});
|
|
141
|
+
if (ended.error) {
|
|
142
|
+
options.ended({});
|
|
143
|
+
return { ok: false, error: "failed", message: `VansPod 無法啟動 Git Bash:${ended.error}。` };
|
|
144
|
+
}
|
|
145
|
+
options.ended(!ended.why && ended.code !== undefined ? { exitCode: ended.code } : {});
|
|
146
|
+
const output = shareOutput(streamText(stdout), streamText(stderr));
|
|
147
|
+
const result = { exit_code: ended.why ? null : ended.code ?? null, stdout: output.stdout, stderr: output.stderr };
|
|
148
|
+
const notes = [];
|
|
149
|
+
if (ended.why === "timed_out") {
|
|
150
|
+
result.timed_out = true;
|
|
151
|
+
notes.push(`指令執行超過 ${Math.round(timeoutMs / 1000)} 秒,VansPod 已停止它。下面是停止前的輸出。`);
|
|
152
|
+
}
|
|
153
|
+
if (ended.why === "stopped") {
|
|
154
|
+
result.stopped = true;
|
|
155
|
+
notes.push("學生在 VansPod 頁面停止了這個指令。下面是停止前的輸出。");
|
|
156
|
+
}
|
|
157
|
+
if (ended.why === "cancelled") {
|
|
158
|
+
result.stopped = true;
|
|
159
|
+
notes.push("這個呼叫已被取消,VansPod 已停止這個指令。");
|
|
160
|
+
}
|
|
161
|
+
if (output.truncated) {
|
|
162
|
+
result.truncated = true;
|
|
163
|
+
notes.push(`輸出太長,只回傳開頭和結尾,共約 ${exports.MAX_OUTPUT_CHARS.toLocaleString("en-US")} 個字元。`);
|
|
164
|
+
}
|
|
165
|
+
if (notes.length > 0)
|
|
166
|
+
result.note = notes.join(" ");
|
|
167
|
+
return { ok: true, result };
|
|
205
168
|
}
|
|
206
169
|
const NOT_RUN = "沒有執行";
|
|
207
|
-
/** The `bash` Local Tool.
|
|
170
|
+
/** The `bash` Local Tool. The log entry of the call runs the command, so the page can stop it. */
|
|
208
171
|
async function bashTool(projectFolder, args, signal, context) {
|
|
209
172
|
if (typeof args.command !== "string" || args.command.trim() === "") {
|
|
210
173
|
return { ok: false, error: "invalid_arguments", message: "command 必須是要在 Git Bash 執行的指令。" };
|
|
@@ -214,8 +177,8 @@ async function bashTool(projectFolder, args, signal, context) {
|
|
|
214
177
|
if (!bash) {
|
|
215
178
|
return { ok: false, error: "failed", message: `這台電腦找不到 Git Bash,${NOT_RUN}。請學生在 VansPod 的「環境安裝」安裝 Git。` };
|
|
216
179
|
}
|
|
217
|
-
const refusal = await (0, localToolApproval_1.askApproval)(context.approver, { tool: "bash", title: "在 Git Bash 執行指令", body: command, folder: projectFolder }, signal, NOT_RUN);
|
|
180
|
+
const refusal = await (0, localToolApproval_1.askApproval)(context.approver, { tool: "bash", title: "在 Git Bash 執行指令", body: command, target: command, folder: projectFolder }, signal, NOT_RUN);
|
|
218
181
|
if (refusal)
|
|
219
182
|
return refusal;
|
|
220
|
-
return context.
|
|
183
|
+
return context.call.runBash(bash, command, projectFolder, signal);
|
|
221
184
|
}
|
|
@@ -1,26 +1,69 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.APPROVAL_TIMEOUT_MS = void 0;
|
|
3
|
+
exports.APPROVAL_TIMEOUT_MS = exports.AUTO_APPROVAL_EXCEPTION_LABELS = void 0;
|
|
4
4
|
exports.askApproval = askApproval;
|
|
5
5
|
exports.approvalRefusal = approvalRefusal;
|
|
6
|
+
exports.autoApprovalException = autoApprovalException;
|
|
6
7
|
exports.createApprovalQueue = createApprovalQueue;
|
|
8
|
+
const projectPath_1 = require("./projectPath");
|
|
7
9
|
/**
|
|
8
10
|
* Asks `approver` and turns anything but approval into the reply for the agent.
|
|
9
11
|
* `notDone` says what did not happen, for example "沒有寫入檔案".
|
|
10
12
|
*/
|
|
11
13
|
async function askApproval(approver, request, signal, notDone) {
|
|
12
14
|
const outcome = await approver.request(request, signal);
|
|
13
|
-
return outcome === "approved" ? undefined : approvalRefusal(outcome, notDone);
|
|
15
|
+
return outcome === "approved" || outcome === "auto" ? undefined : approvalRefusal(outcome, notDone);
|
|
14
16
|
}
|
|
15
17
|
function approvalRefusal(outcome, notDone) {
|
|
16
18
|
if (outcome === "timeout")
|
|
17
19
|
return { ok: false, error: "approval_timeout", message: `學生在 ${exports.APPROVAL_TIMEOUT_MS / 60_000} 分鐘內沒有決定,${notDone}。` };
|
|
18
20
|
if (outcome === "access_off")
|
|
19
|
-
return { ok: false, error: "denied", message:
|
|
21
|
+
return { ok: false, error: "denied", message: `學生關閉了「允許 Agent 使用這台電腦」,${notDone}。` };
|
|
20
22
|
if (outcome === "cancelled")
|
|
21
23
|
return { ok: false, error: "denied", message: `這個呼叫已被取消,${notDone}。` };
|
|
22
24
|
return { ok: false, error: "denied", message: `學生拒絕了,${notDone}。` };
|
|
23
25
|
}
|
|
26
|
+
exports.AUTO_APPROVAL_EXCEPTION_LABELS = {
|
|
27
|
+
secret_file: "碰到機密檔案",
|
|
28
|
+
delete_outside: "刪除專案資料夾以外的檔案",
|
|
29
|
+
};
|
|
30
|
+
// The words of a command, split at spaces, quotes and shell punctuation.
|
|
31
|
+
const commandWords = (command) => command.split(/[\s'"`=<>|;&()]+/).filter(Boolean);
|
|
32
|
+
// A target outside the Project Folder: absolute, the home folder, the parent folder, or a drive letter.
|
|
33
|
+
const OUTSIDE_TARGET = /^(\/|~|\$HOME\b|\$\{HOME\}|\.\.|[A-Za-z]:)/;
|
|
34
|
+
// The parts of a command that run on their own: split at `;`, `&&`, `||`, `|`, `&`, line ends, `( )`, `$(` and backquotes.
|
|
35
|
+
const COMMAND_BREAKS = /&&|\|\||\$\(|[;|&\n()`]/;
|
|
36
|
+
// `rm` as a command word, also with a path such as `/bin/rm`.
|
|
37
|
+
const isRm = (word) => word === "rm" || word.endsWith("/rm");
|
|
38
|
+
// An `rm` anywhere in the command, also inside `bash -c "..."`, `$( )` or after `xargs`, with -r/-f and a target outside.
|
|
39
|
+
function deletesOutside(command) {
|
|
40
|
+
for (const part of command.split(COMMAND_BREAKS)) {
|
|
41
|
+
const words = part.replace(/["']/g, " ").trim().split(/\s+/);
|
|
42
|
+
const at = words.findIndex(isRm);
|
|
43
|
+
if (at < 0)
|
|
44
|
+
continue;
|
|
45
|
+
const rest = words.slice(at + 1);
|
|
46
|
+
const forced = rest.some(word => word === "--recursive" || word === "--force" || /^-[^-]*[rRf]/.test(word));
|
|
47
|
+
if (forced && rest.some(word => !word.startsWith("-") && OUTSIDE_TARGET.test(word)))
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The Auto Approval Exception of a request, or undefined. The rules match text, so they catch only clear cases (ADR 0014).
|
|
54
|
+
* The Secret File check is the one that `read_file` uses: the file name, and for `write_file` also a private key in the text.
|
|
55
|
+
*/
|
|
56
|
+
function autoApprovalException(request) {
|
|
57
|
+
if (request.tool === "write_file") {
|
|
58
|
+
const secret = (0, projectPath_1.isSecretFileName)(request.target) || (request.contents ?? []).some(projectPath_1.hasPrivateKey);
|
|
59
|
+
return secret ? "secret_file" : undefined;
|
|
60
|
+
}
|
|
61
|
+
if (request.tool !== "bash")
|
|
62
|
+
return undefined;
|
|
63
|
+
if (commandWords(request.target).some(projectPath_1.isSecretFileName))
|
|
64
|
+
return "secret_file";
|
|
65
|
+
return deletesOutside(request.target) ? "delete_outside" : undefined;
|
|
66
|
+
}
|
|
24
67
|
/** The default time limit, from when an approval is shown (ADR 0013). */
|
|
25
68
|
exports.APPROVAL_TIMEOUT_MS = 120_000;
|
|
26
69
|
function createApprovalQueue(options = {}) {
|
|
@@ -29,6 +72,8 @@ function createApprovalQueue(options = {}) {
|
|
|
29
72
|
const listeners = new Set();
|
|
30
73
|
let shown;
|
|
31
74
|
let timer;
|
|
75
|
+
let auto;
|
|
76
|
+
let autoTimer;
|
|
32
77
|
let nextId = 0;
|
|
33
78
|
const snapshot = () => shown && { ...shown, waiting: entries.length - 1 };
|
|
34
79
|
const notify = () => {
|
|
@@ -45,7 +90,7 @@ function createApprovalQueue(options = {}) {
|
|
|
45
90
|
if (!shown && entries.length > 0) {
|
|
46
91
|
const first = entries[0];
|
|
47
92
|
const shownAt = Date.now();
|
|
48
|
-
shown = { id: first.id, request: first.request, shownAt, expiresAt: shownAt + timeoutMs, waiting: 0 };
|
|
93
|
+
shown = { id: first.id, request: first.request, ...(first.exception ? { exception: first.exception } : {}), shownAt, expiresAt: shownAt + timeoutMs, waiting: 0 };
|
|
49
94
|
timer = setTimeout(() => end(first.id, "timeout"), timeoutMs);
|
|
50
95
|
changed = true;
|
|
51
96
|
}
|
|
@@ -69,10 +114,23 @@ function createApprovalQueue(options = {}) {
|
|
|
69
114
|
showNext(wasShown);
|
|
70
115
|
return true;
|
|
71
116
|
};
|
|
117
|
+
/** Ends Auto Approval. True when it was on. */
|
|
118
|
+
const endAuto = () => {
|
|
119
|
+
if (!auto)
|
|
120
|
+
return false;
|
|
121
|
+
auto = undefined;
|
|
122
|
+
if (autoTimer)
|
|
123
|
+
clearTimeout(autoTimer);
|
|
124
|
+
autoTimer = undefined;
|
|
125
|
+
return true;
|
|
126
|
+
};
|
|
72
127
|
return {
|
|
73
128
|
request(request, signal) {
|
|
74
129
|
if (signal?.aborted)
|
|
75
130
|
return Promise.resolve("cancelled");
|
|
131
|
+
const exception = autoApprovalException(request);
|
|
132
|
+
if (auto && !exception)
|
|
133
|
+
return Promise.resolve("auto");
|
|
76
134
|
return new Promise(resolve => {
|
|
77
135
|
const id = `approval-${++nextId}`;
|
|
78
136
|
const onAbort = () => end(id, "cancelled");
|
|
@@ -80,6 +138,7 @@ function createApprovalQueue(options = {}) {
|
|
|
80
138
|
entries.push({
|
|
81
139
|
id,
|
|
82
140
|
request,
|
|
141
|
+
...(exception ? { exception } : {}),
|
|
83
142
|
settle(outcome) { signal?.removeEventListener("abort", onAbort); resolve(outcome); },
|
|
84
143
|
});
|
|
85
144
|
// A new waiting approval changes the count that the shown one carries.
|
|
@@ -93,9 +152,27 @@ function createApprovalQueue(options = {}) {
|
|
|
93
152
|
return end(id, decision === "approve" ? "approved" : "denied");
|
|
94
153
|
},
|
|
95
154
|
denyAll() {
|
|
155
|
+
const changed = endAuto();
|
|
96
156
|
for (const entry of [...entries].reverse())
|
|
97
157
|
end(entry.id, "access_off");
|
|
158
|
+
if (changed)
|
|
159
|
+
notify();
|
|
160
|
+
},
|
|
161
|
+
startAutoApproval(durationMs) {
|
|
162
|
+
endAuto();
|
|
163
|
+
auto = durationMs === undefined ? {} : { endsAt: Date.now() + durationMs };
|
|
164
|
+
if (durationMs !== undefined)
|
|
165
|
+
autoTimer = setTimeout(() => { if (endAuto())
|
|
166
|
+
notify(); }, durationMs);
|
|
167
|
+
for (const entry of entries.filter(entry => !entry.exception))
|
|
168
|
+
end(entry.id, "auto");
|
|
169
|
+
notify();
|
|
170
|
+
},
|
|
171
|
+
stopAutoApproval() {
|
|
172
|
+
if (endAuto())
|
|
173
|
+
notify();
|
|
98
174
|
},
|
|
175
|
+
autoApproval: () => auto && { ...auto },
|
|
99
176
|
subscribe(listener) {
|
|
100
177
|
listeners.add(listener);
|
|
101
178
|
return () => { listeners.delete(listener); };
|
package/dist/localTools.js
CHANGED
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.localSystem = exports.MAX_READ_CHARS = exports.NO_PROJECT_FOLDER_NOTE = void 0;
|
|
7
7
|
exports.createLocalTools = createLocalTools;
|
|
8
8
|
exports.findGitBash = findGitBash;
|
|
9
|
-
// The Local Tools that a Device answers for Agent
|
|
9
|
+
// The Local Tools that a Device answers for a Remote Agent (ADR 0013).
|
|
10
10
|
// A tool that is not built yet answers `unknown_tool`.
|
|
11
11
|
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
12
12
|
const node_os_1 = __importDefault(require("node:os"));
|
|
@@ -15,6 +15,7 @@ const lineChanges_1 = require("./lineChanges");
|
|
|
15
15
|
const localCommands_1 = require("./localCommands");
|
|
16
16
|
const localToolApproval_1 = require("./localToolApproval");
|
|
17
17
|
const projectPath_1 = require("./projectPath");
|
|
18
|
+
const remoteAgentLog_1 = require("./remoteAgentLog");
|
|
18
19
|
const saveImage_1 = require("./saveImage");
|
|
19
20
|
const systemPath_1 = require("./systemPath");
|
|
20
21
|
const windowsEnvironmentSystem_1 = require("./windowsEnvironmentSystem");
|
|
@@ -116,17 +117,24 @@ async function writeFile(projectFolder, args, signal, context) {
|
|
|
116
117
|
return target;
|
|
117
118
|
let title = `寫入新檔案 ${target.relative}`;
|
|
118
119
|
let body = content;
|
|
120
|
+
let before;
|
|
119
121
|
if (target.exists) {
|
|
120
122
|
if ((await promises_1.default.stat(target.absolute)).isDirectory()) {
|
|
121
123
|
return { ok: false, error: "invalid_arguments", message: `「${target.relative}」是資料夾,不能寫成檔案。` };
|
|
122
124
|
}
|
|
123
125
|
const old = await promises_1.default.readFile(target.absolute);
|
|
124
126
|
title = `修改檔案 ${target.relative}`;
|
|
125
|
-
|
|
127
|
+
before = old.includes(0) ? undefined : old.toString("utf8");
|
|
128
|
+
body = before === undefined
|
|
126
129
|
? `(原本不是文字檔,整個檔案會換成下面的內容)\n${content}`
|
|
127
|
-
: (0, lineChanges_1.lineChanges)(
|
|
130
|
+
: (0, lineChanges_1.lineChanges)(before, content);
|
|
131
|
+
context.call.wrote(content, before, false);
|
|
128
132
|
}
|
|
129
|
-
|
|
133
|
+
else {
|
|
134
|
+
context.call.wrote(content, undefined, true);
|
|
135
|
+
}
|
|
136
|
+
const contents = before === undefined ? [content] : [content, before];
|
|
137
|
+
const refusal = await (0, localToolApproval_1.askApproval)(context.approver, { tool: "write_file", title, body, target: target.relative, folder: projectFolder, contents }, signal, NOT_WRITTEN);
|
|
130
138
|
if (refusal)
|
|
131
139
|
return refusal;
|
|
132
140
|
// Resolve again: the folder may have changed while the student decided.
|
|
@@ -150,9 +158,22 @@ const PROJECT_TOOLS = {
|
|
|
150
158
|
bash: localCommands_1.bashTool,
|
|
151
159
|
save_image: saveImage_1.saveImage,
|
|
152
160
|
};
|
|
161
|
+
/** The path or the command of a call, for the Remote Agent Log. */
|
|
162
|
+
function callTarget(tool, args) {
|
|
163
|
+
const text = (value) => typeof value === "string" ? value : "";
|
|
164
|
+
if (tool === "bash")
|
|
165
|
+
return text(args.command);
|
|
166
|
+
if (tool === "list_files")
|
|
167
|
+
return text(args.path ?? ".");
|
|
168
|
+
if (tool === "save_image")
|
|
169
|
+
return `images/${text(args.file_name)}`;
|
|
170
|
+
if (tool === "local_status")
|
|
171
|
+
return "";
|
|
172
|
+
return text(args.path);
|
|
173
|
+
}
|
|
153
174
|
function createLocalTools(options) {
|
|
154
|
-
const
|
|
155
|
-
return
|
|
175
|
+
const log = options.log ?? (0, remoteAgentLog_1.createRemoteAgentLog)();
|
|
176
|
+
return (tool, args, signal) => log.record(tool, callTarget(tool, args), async (call) => {
|
|
156
177
|
if (tool === "local_status")
|
|
157
178
|
return localStatus(options);
|
|
158
179
|
const projectTool = Object.hasOwn(PROJECT_TOOLS, tool) ? PROJECT_TOOLS[tool] : undefined;
|
|
@@ -160,10 +181,12 @@ function createLocalTools(options) {
|
|
|
160
181
|
const folder = options.projectFolder();
|
|
161
182
|
if (!folder)
|
|
162
183
|
return { ok: false, error: "no_project_folder", message: exports.NO_PROJECT_FOLDER_NOTE };
|
|
163
|
-
|
|
184
|
+
// The entry keeps how each Local Tool Approval ended.
|
|
185
|
+
const approver = { request: (request, abort) => call.ask(options.approver, request, abort) };
|
|
186
|
+
return projectTool(folder, args, signal, { approver, call, gitBash: () => options.system.gitBash() });
|
|
164
187
|
}
|
|
165
188
|
return { ok: false, error: "unknown_tool", message: `這台電腦的 VansPod 不認識工具「${tool}」,請更新 VansPod。` };
|
|
166
|
-
};
|
|
189
|
+
});
|
|
167
190
|
}
|
|
168
191
|
function osName() {
|
|
169
192
|
const release = node_os_1.default.release();
|
package/dist/modelStatus.js
CHANGED
|
@@ -15,19 +15,19 @@ exports.modelStatusScript = `
|
|
|
15
15
|
setTimeout(refreshModelStatus, 1000);
|
|
16
16
|
}
|
|
17
17
|
function applyModelStatus(status) {
|
|
18
|
+
// The connection changed outside this page (browser callback, 取消, timeout): draw the new state.
|
|
19
|
+
var shownConnection = document.querySelector("[data-connection-key]");
|
|
20
|
+
if (shownConnection && typeof status.connection === "string" && shownConnection.getAttribute("data-connection-key") !== status.connection) {
|
|
21
|
+
location.reload();
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
18
24
|
if (status.localTools) {
|
|
19
|
-
var
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
localSwitch.textContent = status.localTools.on ? "開" : "關";
|
|
26
|
-
localSwitch.classList.toggle("on", status.localTools.on);
|
|
27
|
-
}
|
|
28
|
-
var commandList = document.querySelector("[data-local-commands]");
|
|
29
|
-
var commands = status.localTools.commands;
|
|
30
|
-
if (commandList && commands && (commandList.getAttribute("data-commands-key") || "") !== commands.key) commandList.outerHTML = commands.html;
|
|
25
|
+
var statusBar = document.querySelector("[data-remote-status]");
|
|
26
|
+
var remoteStatus = status.localTools.status;
|
|
27
|
+
if (statusBar && remoteStatus && (statusBar.getAttribute("data-status-key") || "") !== remoteStatus.key) statusBar.outerHTML = remoteStatus.html;
|
|
28
|
+
var logList = document.querySelector("[data-log-list]");
|
|
29
|
+
var log = status.localTools.log;
|
|
30
|
+
if (logList && log && (logList.getAttribute("data-log-key") || "") !== log.key && typeof replaceRemoteAgentLog === "function") replaceRemoteAgentLog(log);
|
|
31
31
|
var approvalBlock = document.querySelector("[data-local-approval]");
|
|
32
32
|
if (approvalBlock) {
|
|
33
33
|
var approval = status.localTools.approval;
|
|
@@ -36,6 +36,8 @@ exports.modelStatusScript = `
|
|
|
36
36
|
} else if (approval) {
|
|
37
37
|
var seconds = approvalBlock.querySelector("[data-approval-seconds]");
|
|
38
38
|
if (seconds) seconds.textContent = String(approval.secondsLeft);
|
|
39
|
+
var bar = approvalBlock.querySelector("[data-approval-bar]");
|
|
40
|
+
if (bar) bar.style.width = approval.bar;
|
|
39
41
|
}
|
|
40
42
|
}
|
|
41
43
|
}
|