vanspod 0.4.13 → 0.4.16
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 +4 -4
- package/dist/classroomApp.js +124 -1
- package/dist/codexWebSearch.js +1 -1
- package/dist/commandRunner.js +1 -0
- package/dist/deviceConnection.js +177 -0
- package/dist/html.js +12 -0
- package/dist/lineChanges.js +75 -0
- package/dist/localCommands.js +184 -0
- package/dist/localToolApproval.js +181 -0
- package/dist/localTools.js +232 -0
- package/dist/modelStatus.js +38 -1
- package/dist/page.js +321 -43
- package/dist/projectPath.js +86 -0
- package/dist/remoteAgentLog.js +126 -0
- package/dist/remoteAgentLogView.js +421 -0
- package/dist/saveImage.js +137 -0
- package/dist/start.js +103 -1
- package/dist/trayChannel.js +122 -0
- package/dist/version.js +1 -1
- package/dist/windowsEnvironmentSystem.js +1 -0
- package/package.json +1 -1
- package/scripts/tray.ps1 +398 -4
package/README.md
CHANGED
|
@@ -38,7 +38,7 @@ npm run dist
|
|
|
38
38
|
npm pack --pack-destination dist
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
exe 產物為 `dist/VansPod.exe`,本機套件為 `dist/vanspod-0.4.
|
|
41
|
+
exe 產物為 `dist/VansPod.exe`,本機套件為 `dist/vanspod-0.4.16.tgz`;可在 Windows 用 `npm install -g <套件路徑>` 安裝測試。圖片、favicon、系統匣與移除腳本均包含在發佈產物中。更新 logo 後執行 `powershell.exe -NoProfile -File scripts/build-icon.ps1` 重建多尺寸 Windows icon。
|
|
42
42
|
|
|
43
43
|
exe 發佈管道為本專案 GitHub Releases,npm 為公開 registry 的 [`vanspod`](https://www.npmjs.com/package/vanspod) 套件。更新時不要執行完整移除,否則會清除課堂資料。
|
|
44
44
|
|
|
@@ -46,12 +46,12 @@ exe 發佈管道為本專案 GitHub Releases,npm 為公開 registry 的 [`vans
|
|
|
46
46
|
|
|
47
47
|
一般 push 不發佈;推送 `v*` tag 會在 Windows runner 安裝 Node.js 24、檢查型別、執行測試、打包並驗證 exe/npm 生命週期,全部通過後才建立 GitHub Release,最後發佈 npm 套件。學生從 Release 下載 `VansPod.exe`,同頁提供 `SHA256SUMS.txt`。Release 另附 brotli 壓縮的 `VansPod.exe.br`(約三分之一大小),程式內更新只下載這份,解壓後再比對 `VansPod.exe` 的 SHA-256;SHA256SUMS 沒列出它的舊版 Release 則下載完整 exe。
|
|
48
48
|
|
|
49
|
-
發版前同步 `package.json`、`package-lock.json` 與 `src/version.ts` 的版本;tag 必須等於 `v` 加版本號,否則流程失敗。以 `0.4.
|
|
49
|
+
發版前同步 `package.json`、`package-lock.json` 與 `src/version.ts` 的版本;tag 必須等於 `v` 加版本號,否則流程失敗。以 `0.4.16` 為例:
|
|
50
50
|
|
|
51
51
|
```powershell
|
|
52
52
|
git push origin master
|
|
53
|
-
git tag -a v0.4.
|
|
54
|
-
git push origin v0.4.
|
|
53
|
+
git tag -a v0.4.16 -m "VansPod 0.4.16"
|
|
54
|
+
git push origin v0.4.16
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
新版本使用新的版本號與 tag。相同 tag 的 Actions 重跑會更新該 Release 的產物。
|
package/dist/classroomApp.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ClassroomApp = exports.COPIED_KEY_NOTICE = exports.ENVIRONMENT_LABEL = exports.ENVIRONMENT_TOOLS = void 0;
|
|
3
|
+
exports.ClassroomApp = exports.COPIED_KEY_NOTICE = exports.AUTO_APPROVAL_NEEDS_ACCESS = exports.AUTO_APPROVAL_DURATIONS = exports.ENVIRONMENT_LABEL = exports.ENVIRONMENT_TOOLS = void 0;
|
|
4
4
|
const secureStorage_1 = require("./secureStorage");
|
|
5
5
|
const storageErrors_1 = require("./storageErrors");
|
|
6
6
|
const adapterSettings_1 = require("./adapterSettings");
|
|
@@ -9,6 +9,7 @@ const node_util_1 = require("node:util");
|
|
|
9
9
|
const courseCatalog_1 = require("./courseCatalog");
|
|
10
10
|
const modelProxy_1 = require("./modelProxy");
|
|
11
11
|
const classroomKeyRejection_1 = require("./classroomKeyRejection");
|
|
12
|
+
const localToolApproval_1 = require("./localToolApproval");
|
|
12
13
|
const CLIENT_ROUTE_OF = { codex: "codex", claude: "claudeCode", vscode: "vsCodeChat" };
|
|
13
14
|
// 新工具加在最後,不改既有 id。
|
|
14
15
|
exports.ENVIRONMENT_TOOLS = ["uv", "git", "node", "pwsh", "vscode"];
|
|
@@ -19,6 +20,13 @@ exports.ENVIRONMENT_LABEL = {
|
|
|
19
20
|
pwsh: "PowerShell 7",
|
|
20
21
|
vscode: "VS Code",
|
|
21
22
|
};
|
|
23
|
+
exports.AUTO_APPROVAL_DURATIONS = {
|
|
24
|
+
"10": { label: "10 分鐘", ms: 10 * 60_000 },
|
|
25
|
+
"30": { label: "30 分鐘", ms: 30 * 60_000 },
|
|
26
|
+
until_off: { label: "直到關閉開關" },
|
|
27
|
+
};
|
|
28
|
+
const LOCAL_TOOLS_NEED_CONNECTION = "請先兌換課堂連線,才能讓 Agent 使用這台電腦。";
|
|
29
|
+
exports.AUTO_APPROVAL_NEEDS_ACCESS = "請先打開「允許 Agent 使用這台電腦」,才能自動允許。";
|
|
22
30
|
const LOCAL_LIST_NOTE = "這是本機清單。";
|
|
23
31
|
exports.COPIED_KEY_NOTICE = "已複製 Classroom API Key。請勿分享給不信任的人。";
|
|
24
32
|
const KEY_REJECTED_NOTICE = {
|
|
@@ -100,8 +108,16 @@ class ClassroomApp {
|
|
|
100
108
|
routeRestartNoted = false;
|
|
101
109
|
vsCodeOrigin;
|
|
102
110
|
sending = new Map();
|
|
111
|
+
localToolsNotice;
|
|
112
|
+
autoApprovalLabel;
|
|
103
113
|
constructor(deps) {
|
|
104
114
|
this.deps = deps;
|
|
115
|
+
// Every way that Local Tool Access turns off (the switch, or the relay by itself) ends Auto Approval
|
|
116
|
+
// and denies the waiting approvals (ADR 0014).
|
|
117
|
+
const { device, approvals } = deps;
|
|
118
|
+
if (device && approvals)
|
|
119
|
+
device.subscribe(status => { if (!status.on)
|
|
120
|
+
approvals.denyAll(); });
|
|
105
121
|
}
|
|
106
122
|
serialConnectionOperation(operation) {
|
|
107
123
|
const next = this.connectionOperations.then(operation, operation);
|
|
@@ -134,6 +150,41 @@ class ClassroomApp {
|
|
|
134
150
|
};
|
|
135
151
|
if (this.connectionError)
|
|
136
152
|
view.connectionError = this.connectionError;
|
|
153
|
+
if (this.deps.device) {
|
|
154
|
+
const status = this.deps.device.status();
|
|
155
|
+
const local = { on: status.on, state: status.state, device: status.device };
|
|
156
|
+
if (this.nickname)
|
|
157
|
+
local.nickname = this.nickname;
|
|
158
|
+
// While it is off, the newest notice (from the student's last action) comes before the relay's reason.
|
|
159
|
+
const message = status.on ? status.message : this.localToolsNotice ?? status.message;
|
|
160
|
+
if (message)
|
|
161
|
+
local.message = message;
|
|
162
|
+
if (status.retryInMs !== undefined)
|
|
163
|
+
local.retryInMs = status.retryInMs;
|
|
164
|
+
const shown = this.deps.approvals?.shown();
|
|
165
|
+
if (shown) {
|
|
166
|
+
local.approval = {
|
|
167
|
+
id: shown.id,
|
|
168
|
+
tool: shown.request.tool,
|
|
169
|
+
title: shown.request.title,
|
|
170
|
+
body: shown.request.body,
|
|
171
|
+
folder: shown.request.folder,
|
|
172
|
+
device: status.device,
|
|
173
|
+
secondsLeft: Math.max(0, Math.ceil((shown.expiresAt - Date.now()) / 1000)),
|
|
174
|
+
waiting: shown.waiting,
|
|
175
|
+
...(shown.exception ? { exception: localToolApproval_1.AUTO_APPROVAL_EXCEPTION_LABELS[shown.exception] } : {}),
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
const auto = this.deps.approvals?.autoApproval();
|
|
179
|
+
if (auto) {
|
|
180
|
+
local.autoApproval = auto.endsAt === undefined
|
|
181
|
+
? { label: this.autoApprovalLabel ?? exports.AUTO_APPROVAL_DURATIONS.until_off.label }
|
|
182
|
+
: { label: this.autoApprovalLabel ?? "", endsAt: auto.endsAt, secondsLeft: Math.max(0, Math.ceil((auto.endsAt - Date.now()) / 1000)) };
|
|
183
|
+
}
|
|
184
|
+
if (this.deps.remoteAgentLog)
|
|
185
|
+
local.log = { entries: this.deps.remoteAgentLog.list(), dropped: this.deps.remoteAgentLog.dropped() };
|
|
186
|
+
view.localTools = local;
|
|
187
|
+
}
|
|
137
188
|
if (this.notice && this.notice === this.connectionNotice)
|
|
138
189
|
view.connectionNotice = this.connectionNotice;
|
|
139
190
|
if (this.connectionDraft && !this.connected)
|
|
@@ -791,6 +842,8 @@ class ClassroomApp {
|
|
|
791
842
|
this.notice = this.connectionNotice = "無法安全保存或清除原有課堂連線,換堂未完成。請稍後再試。";
|
|
792
843
|
return;
|
|
793
844
|
}
|
|
845
|
+
// A new Classroom Connection never inherits Local Tool Access from the old one.
|
|
846
|
+
await this.turnLocalToolAccessOff();
|
|
794
847
|
const desiredMode = this.mode;
|
|
795
848
|
const desiredClientModes = { ...this.clientModes };
|
|
796
849
|
const desiredVsCodeMode = this.clientRoutes.vsCodeChat.mode;
|
|
@@ -1692,6 +1745,74 @@ class ClassroomApp {
|
|
|
1692
1745
|
return false;
|
|
1693
1746
|
}
|
|
1694
1747
|
}
|
|
1748
|
+
/** Turns Local Tool Access (允許 Agent 使用這台電腦) on or off. It needs a Classroom Connection. */
|
|
1749
|
+
async setLocalToolAccess(on) {
|
|
1750
|
+
return this.serialConnectionOperation(async () => {
|
|
1751
|
+
const device = this.deps.device;
|
|
1752
|
+
if (!device)
|
|
1753
|
+
return;
|
|
1754
|
+
this.localToolsNotice = undefined;
|
|
1755
|
+
if (!on) {
|
|
1756
|
+
await this.turnLocalToolAccessOff();
|
|
1757
|
+
return;
|
|
1758
|
+
}
|
|
1759
|
+
let apiKey;
|
|
1760
|
+
try {
|
|
1761
|
+
apiKey = this.connected ? (await this.deps.storage.getConnection())?.apiKey : undefined;
|
|
1762
|
+
}
|
|
1763
|
+
catch {
|
|
1764
|
+
apiKey = undefined;
|
|
1765
|
+
}
|
|
1766
|
+
if (!apiKey) {
|
|
1767
|
+
this.localToolsNotice = LOCAL_TOOLS_NEED_CONNECTION;
|
|
1768
|
+
return;
|
|
1769
|
+
}
|
|
1770
|
+
device.start(apiKey);
|
|
1771
|
+
});
|
|
1772
|
+
}
|
|
1773
|
+
/** Decides the shown Local Tool Approval from the page. False when it is no longer shown: the first decision wins. */
|
|
1774
|
+
decideLocalToolApproval(id, decision) {
|
|
1775
|
+
return this.deps.approvals?.decide(id, decision) ?? false;
|
|
1776
|
+
}
|
|
1777
|
+
/**
|
|
1778
|
+
* Starts Auto Approval (ADR 0014) from the page or the Approval Popup. The waiting approvals that are not
|
|
1779
|
+
* Auto Approval Exceptions are approved now. False when Local Tool Access is off: the page then tells the student.
|
|
1780
|
+
*/
|
|
1781
|
+
startAutoApproval(duration) {
|
|
1782
|
+
const approvals = this.deps.approvals;
|
|
1783
|
+
if (!approvals || !Object.hasOwn(exports.AUTO_APPROVAL_DURATIONS, duration))
|
|
1784
|
+
return false;
|
|
1785
|
+
if (!this.deps.device?.status().on) {
|
|
1786
|
+
this.localToolsNotice = exports.AUTO_APPROVAL_NEEDS_ACCESS;
|
|
1787
|
+
return false;
|
|
1788
|
+
}
|
|
1789
|
+
const { label, ms } = exports.AUTO_APPROVAL_DURATIONS[duration];
|
|
1790
|
+
this.autoApprovalLabel = label;
|
|
1791
|
+
approvals.startAutoApproval(ms);
|
|
1792
|
+
return true;
|
|
1793
|
+
}
|
|
1794
|
+
/** Stops Auto Approval from the page or the tray menu. */
|
|
1795
|
+
stopAutoApproval() {
|
|
1796
|
+
this.deps.approvals?.stopAutoApproval();
|
|
1797
|
+
}
|
|
1798
|
+
/** The full body of the shown Local Tool Approval, or undefined when `id` is not the shown one. The page cuts a long body. */
|
|
1799
|
+
localToolApprovalBody(id) {
|
|
1800
|
+
const shown = this.deps.approvals?.shown();
|
|
1801
|
+
return shown && shown.id === id ? shown.request.body : undefined;
|
|
1802
|
+
}
|
|
1803
|
+
/** Stops a running `bash` command from the page. False when it is not running. */
|
|
1804
|
+
stopRemoteAgentCommand(id) {
|
|
1805
|
+
return this.deps.remoteAgentLog?.stop(id) ?? false;
|
|
1806
|
+
}
|
|
1807
|
+
// Denies every waiting approval, lets the denials reach the relay, then disconnects.
|
|
1808
|
+
async turnLocalToolAccessOff() {
|
|
1809
|
+
const approvals = this.deps.approvals;
|
|
1810
|
+
const waiting = approvals?.shown() !== undefined;
|
|
1811
|
+
approvals?.denyAll();
|
|
1812
|
+
if (waiting)
|
|
1813
|
+
await new Promise(resolve => setImmediate(resolve));
|
|
1814
|
+
this.deps.device?.stop();
|
|
1815
|
+
}
|
|
1695
1816
|
async clearConnection() {
|
|
1696
1817
|
return this.serialConnectionOperation(() => this.clearConnectionNow());
|
|
1697
1818
|
}
|
|
@@ -1703,6 +1824,8 @@ class ClassroomApp {
|
|
|
1703
1824
|
this.notice = this.connectionNotice = "無法清除課堂連線。已保留原本連線,重新啟動後仍可能恢復。請稍後再試。";
|
|
1704
1825
|
return;
|
|
1705
1826
|
}
|
|
1827
|
+
await this.turnLocalToolAccessOff();
|
|
1828
|
+
this.localToolsNotice = undefined;
|
|
1706
1829
|
this.applyConnectionIdentity(undefined);
|
|
1707
1830
|
this.connectionError = undefined;
|
|
1708
1831
|
this.connectionDraft = undefined;
|
package/dist/codexWebSearch.js
CHANGED
|
@@ -16,7 +16,7 @@ function unsupported(commands) {
|
|
|
16
16
|
return `課堂模式沒有這些網路指令。${commands.length ? `These web commands are not supported in this classroom session and were not run: ${commands.join(", ")}.` : "This request named no web command that this classroom session runs."} Searching is done with search_query (searching by query text); its results already include a text snippet of each page. Do not retry the unsupported commands. Do not use shell commands (curl, wget, scripts) to get that information some other way. If that is not enough, tell the user this classroom session cannot do it.`;
|
|
17
17
|
}
|
|
18
18
|
// open, click and find read a page Codex already knows the address of. That is not Web
|
|
19
|
-
// Search (
|
|
19
|
+
// Search (GLOSSARY.md): Router is not called, so whether the Class Session allows Web
|
|
20
20
|
// Search is never asked, and the Coding Agent fetches the page itself.
|
|
21
21
|
const READ_COMMANDS = ["open", "click", "find"];
|
|
22
22
|
// What is written back to the model; a longer ref_id is still answered, just not quoted.
|
package/dist/commandRunner.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.collectOutput = collectOutput;
|
|
4
|
+
exports.killTree = killTree;
|
|
4
5
|
exports.shellLaunch = shellLaunch;
|
|
5
6
|
exports.runCommand = runCommand;
|
|
6
7
|
const node_child_process_1 = require("node:child_process");
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Device connection: while Local Tool Access is on, VansPod keeps one WebSocket to the
|
|
3
|
+
// vans-mcp-server relay and answers the Local Tool calls that come through it.
|
|
4
|
+
// Wire protocol: vans-mcp-server docs/device-relay-protocol.md. Design: docs/adr/0013.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.deviceName = deviceName;
|
|
7
|
+
exports.createDeviceConnection = createDeviceConnection;
|
|
8
|
+
const MAX_DEVICE_NAME = 64;
|
|
9
|
+
const REFUSED = 4401;
|
|
10
|
+
const BAD_HELLO = 4400;
|
|
11
|
+
const REPLACED = 4409;
|
|
12
|
+
/** The computer name as the relay accepts it: trimmed, at most 64 characters, never empty. */
|
|
13
|
+
function deviceName(hostname) {
|
|
14
|
+
return hostname.trim().slice(0, MAX_DEVICE_NAME).trim() || "VansPod";
|
|
15
|
+
}
|
|
16
|
+
function createDeviceConnection(options) {
|
|
17
|
+
const retry = options.retry ?? { firstMs: 1_000, maxMs: 30_000 };
|
|
18
|
+
const device = deviceName(options.device);
|
|
19
|
+
let current = { on: false, state: "off", device };
|
|
20
|
+
let apiKey;
|
|
21
|
+
let socket;
|
|
22
|
+
let timer;
|
|
23
|
+
let nextDelay = retry.firstMs;
|
|
24
|
+
// Each attempt has its own number; events from an older socket are ignored.
|
|
25
|
+
let attempt = 0;
|
|
26
|
+
const calls = new Map();
|
|
27
|
+
const listeners = new Set();
|
|
28
|
+
const set = (next) => {
|
|
29
|
+
current = { ...next, device };
|
|
30
|
+
options.onChange?.({ ...current });
|
|
31
|
+
for (const listener of [...listeners]) {
|
|
32
|
+
try {
|
|
33
|
+
listener({ ...current });
|
|
34
|
+
}
|
|
35
|
+
catch { /* one listener must not stop the others */ }
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const abortCalls = () => {
|
|
39
|
+
for (const control of calls.values())
|
|
40
|
+
control.abort();
|
|
41
|
+
calls.clear();
|
|
42
|
+
};
|
|
43
|
+
const close = () => {
|
|
44
|
+
attempt += 1;
|
|
45
|
+
if (timer)
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
timer = undefined;
|
|
48
|
+
abortCalls();
|
|
49
|
+
const open = socket;
|
|
50
|
+
socket = undefined;
|
|
51
|
+
if (open && open.readyState !== WebSocket.CLOSED && open.readyState !== WebSocket.CLOSING)
|
|
52
|
+
open.close(1000);
|
|
53
|
+
};
|
|
54
|
+
const off = (message) => {
|
|
55
|
+
close();
|
|
56
|
+
apiKey = undefined;
|
|
57
|
+
set(message ? { on: false, state: "off", message } : { on: false, state: "off" });
|
|
58
|
+
};
|
|
59
|
+
const scheduleRetry = (message) => {
|
|
60
|
+
close();
|
|
61
|
+
const wait = nextDelay;
|
|
62
|
+
nextDelay = Math.min(nextDelay * 2, retry.maxMs);
|
|
63
|
+
set({ on: true, state: "retrying", retryInMs: wait, ...(message ? { message } : {}) });
|
|
64
|
+
timer = setTimeout(connect, wait);
|
|
65
|
+
};
|
|
66
|
+
const reply = (open, frame) => {
|
|
67
|
+
if (open === socket && open.readyState === WebSocket.OPEN)
|
|
68
|
+
open.send(JSON.stringify(frame));
|
|
69
|
+
};
|
|
70
|
+
const run = async (open, id, tool, args) => {
|
|
71
|
+
const control = new AbortController();
|
|
72
|
+
calls.set(id, control);
|
|
73
|
+
let answer;
|
|
74
|
+
try {
|
|
75
|
+
answer = await options.handle(tool, args, control.signal);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
answer = { ok: false, error: "failed", message: `本機工具失敗:${error instanceof Error ? error.message : "原因不明"}。` };
|
|
79
|
+
}
|
|
80
|
+
if (calls.get(id) !== control || control.signal.aborted)
|
|
81
|
+
return;
|
|
82
|
+
calls.delete(id);
|
|
83
|
+
reply(open, { type: "result", id, ...answer });
|
|
84
|
+
};
|
|
85
|
+
const connect = () => {
|
|
86
|
+
timer = undefined;
|
|
87
|
+
if (!apiKey)
|
|
88
|
+
return;
|
|
89
|
+
const mine = ++attempt;
|
|
90
|
+
const key = apiKey;
|
|
91
|
+
set({ on: true, state: "connecting" });
|
|
92
|
+
let open;
|
|
93
|
+
try {
|
|
94
|
+
open = new WebSocket(options.url);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
scheduleRetry();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
socket = open;
|
|
101
|
+
let refusal;
|
|
102
|
+
open.addEventListener("open", () => {
|
|
103
|
+
if (mine !== attempt)
|
|
104
|
+
return;
|
|
105
|
+
open.send(JSON.stringify({ type: "hello", api_key: key, device }));
|
|
106
|
+
});
|
|
107
|
+
open.addEventListener("message", event => {
|
|
108
|
+
if (mine !== attempt || typeof event.data !== "string")
|
|
109
|
+
return;
|
|
110
|
+
let frame;
|
|
111
|
+
try {
|
|
112
|
+
frame = JSON.parse(event.data);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (!frame || typeof frame !== "object")
|
|
118
|
+
return;
|
|
119
|
+
if (frame.type === "welcome") {
|
|
120
|
+
nextDelay = retry.firstMs;
|
|
121
|
+
set({ on: true, state: "connected" });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (frame.type === "refused") {
|
|
125
|
+
refusal = typeof frame.message === "string" ? frame.message : undefined;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (frame.type === "cancel" && typeof frame.id === "string") {
|
|
129
|
+
calls.get(frame.id)?.abort();
|
|
130
|
+
calls.delete(frame.id);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
if (frame.type === "call" && typeof frame.id === "string") {
|
|
134
|
+
const id = frame.id;
|
|
135
|
+
if (typeof frame.tool !== "string") {
|
|
136
|
+
reply(open, { type: "result", id, ok: false, error: "invalid_arguments", message: "工具名稱不正確。" });
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const args = frame.arguments && typeof frame.arguments === "object" && !Array.isArray(frame.arguments) ? frame.arguments : {};
|
|
140
|
+
void run(open, id, frame.tool, args);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
open.addEventListener("close", event => {
|
|
144
|
+
if (mine !== attempt)
|
|
145
|
+
return;
|
|
146
|
+
if (event.code === REFUSED) {
|
|
147
|
+
off(refusal ?? "課堂連線被拒絕,已關閉「允許 Agent 使用這台電腦」。");
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
if (event.code === BAD_HELLO) {
|
|
151
|
+
off("VansPod 無法連上遠端 Agent 的轉送站(連線資料不正確),請更新 VansPod。");
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (event.code === REPLACED) {
|
|
155
|
+
off("這台電腦有另一個較新的連線,這個連線已停止。");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
scheduleRetry(refusal);
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
start(key) {
|
|
163
|
+
if (apiKey === key && current.on)
|
|
164
|
+
return;
|
|
165
|
+
close();
|
|
166
|
+
apiKey = key;
|
|
167
|
+
nextDelay = retry.firstMs;
|
|
168
|
+
connect();
|
|
169
|
+
},
|
|
170
|
+
stop() { off(); },
|
|
171
|
+
status() { return { ...current }; },
|
|
172
|
+
subscribe(listener) {
|
|
173
|
+
listeners.add(listener);
|
|
174
|
+
return () => { listeners.delete(listener); };
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
package/dist/html.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.escapeHtml = escapeHtml;
|
|
4
|
+
/** Escapes text for HTML text and attribute values. */
|
|
5
|
+
function escapeHtml(value) {
|
|
6
|
+
return value
|
|
7
|
+
.replaceAll("&", "&")
|
|
8
|
+
.replaceAll("<", "<")
|
|
9
|
+
.replaceAll(">", ">")
|
|
10
|
+
.replaceAll('"', """)
|
|
11
|
+
.replaceAll("'", "'");
|
|
12
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The changed lines between two texts, for a Local Tool Approval of `write_file` on an existing file.
|
|
3
|
+
// Each group starts with "@@ 原本第 N 行 @@". A removed line starts with "-" and an added line with "+".
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.lineChanges = lineChanges;
|
|
6
|
+
const MAX_TABLE = 4_000_000;
|
|
7
|
+
function lines(text) {
|
|
8
|
+
const all = text.replace(/\r\n/g, "\n").split("\n");
|
|
9
|
+
if (all.length > 1 && all[all.length - 1] === "")
|
|
10
|
+
all.pop();
|
|
11
|
+
return all;
|
|
12
|
+
}
|
|
13
|
+
/** The steps from `a` to `b` by the longest common subsequence. A table too large becomes one replace. */
|
|
14
|
+
function steps(a, b) {
|
|
15
|
+
if (a.length * b.length > MAX_TABLE) {
|
|
16
|
+
return [...a.map(text => ({ kind: "removed", text })), ...b.map(text => ({ kind: "added", text }))];
|
|
17
|
+
}
|
|
18
|
+
const width = b.length + 1;
|
|
19
|
+
const table = new Uint32Array((a.length + 1) * width);
|
|
20
|
+
for (let i = a.length - 1; i >= 0; i--) {
|
|
21
|
+
for (let j = b.length - 1; j >= 0; j--) {
|
|
22
|
+
table[i * width + j] = a[i] === b[j] ? table[(i + 1) * width + j + 1] + 1 : Math.max(table[(i + 1) * width + j], table[i * width + j + 1]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const out = [];
|
|
26
|
+
let i = 0, j = 0;
|
|
27
|
+
while (i < a.length && j < b.length) {
|
|
28
|
+
if (a[i] === b[j]) {
|
|
29
|
+
out.push({ kind: "same", text: a[i] });
|
|
30
|
+
i++;
|
|
31
|
+
j++;
|
|
32
|
+
}
|
|
33
|
+
else if (table[(i + 1) * width + j] >= table[i * width + j + 1])
|
|
34
|
+
out.push({ kind: "removed", text: a[i++] });
|
|
35
|
+
else
|
|
36
|
+
out.push({ kind: "added", text: b[j++] });
|
|
37
|
+
}
|
|
38
|
+
while (i < a.length)
|
|
39
|
+
out.push({ kind: "removed", text: a[i++] });
|
|
40
|
+
while (j < b.length)
|
|
41
|
+
out.push({ kind: "added", text: b[j++] });
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
function lineChanges(before, after) {
|
|
45
|
+
const a = lines(before), b = lines(after);
|
|
46
|
+
let start = 0;
|
|
47
|
+
while (start < a.length && start < b.length && a[start] === b[start])
|
|
48
|
+
start++;
|
|
49
|
+
let endA = a.length, endB = b.length;
|
|
50
|
+
while (endA > start && endB > start && a[endA - 1] === b[endB - 1]) {
|
|
51
|
+
endA--;
|
|
52
|
+
endB--;
|
|
53
|
+
}
|
|
54
|
+
const middle = steps(a.slice(start, endA), b.slice(start, endB));
|
|
55
|
+
const out = [];
|
|
56
|
+
let oldLine = start + 1;
|
|
57
|
+
let inGroup = false;
|
|
58
|
+
for (const step of middle) {
|
|
59
|
+
if (step.kind === "same") {
|
|
60
|
+
inGroup = false;
|
|
61
|
+
oldLine++;
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (!inGroup) {
|
|
65
|
+
out.push(`@@ 原本第 ${oldLine} 行 @@`);
|
|
66
|
+
inGroup = true;
|
|
67
|
+
}
|
|
68
|
+
out.push(`${step.kind === "removed" ? "-" : "+"}${step.text}`);
|
|
69
|
+
if (step.kind === "removed")
|
|
70
|
+
oldLine++;
|
|
71
|
+
}
|
|
72
|
+
if (out.length === 0)
|
|
73
|
+
return before === after ? "(內容沒有改變)" : "(只改了換行符號)";
|
|
74
|
+
return out.join("\n");
|
|
75
|
+
}
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MAX_OUTPUT_CHARS = exports.BASH_TIMEOUT_MS = void 0;
|
|
7
|
+
exports.runBashCommand = runBashCommand;
|
|
8
|
+
exports.bashTool = bashTool;
|
|
9
|
+
// The `bash` Local Tool (ADR 0013): one command in Git Bash, from the Project Folder, after a
|
|
10
|
+
// Local Tool Approval. The Remote Agent Log keeps each call and can stop a running command.
|
|
11
|
+
const node_child_process_1 = require("node:child_process");
|
|
12
|
+
const node_fs_1 = require("node:fs");
|
|
13
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
14
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
15
|
+
const commandRunner_1 = require("./commandRunner");
|
|
16
|
+
const localToolApproval_1 = require("./localToolApproval");
|
|
17
|
+
const systemPath_1 = require("./systemPath");
|
|
18
|
+
exports.BASH_TIMEOUT_MS = 60_000;
|
|
19
|
+
/** About this many characters of standard output and standard error together reach the agent. */
|
|
20
|
+
exports.MAX_OUTPUT_CHARS = 30_000;
|
|
21
|
+
// A UTF-8 character has at most 4 bytes. Keep this many bytes from the start and from the end.
|
|
22
|
+
const KEEP_BYTES = exports.MAX_OUTPUT_CHARS * 4;
|
|
23
|
+
function collector() {
|
|
24
|
+
return { head: [], headBytes: 0, tail: [], tailBytes: 0, total: 0 };
|
|
25
|
+
}
|
|
26
|
+
function collect(into, chunk) {
|
|
27
|
+
into.total += chunk.length;
|
|
28
|
+
if (into.headBytes < KEEP_BYTES) {
|
|
29
|
+
const part = chunk.subarray(0, KEEP_BYTES - into.headBytes);
|
|
30
|
+
into.head.push(part);
|
|
31
|
+
into.headBytes += part.length;
|
|
32
|
+
chunk = chunk.subarray(part.length);
|
|
33
|
+
}
|
|
34
|
+
if (chunk.length === 0)
|
|
35
|
+
return;
|
|
36
|
+
into.tail.push(chunk);
|
|
37
|
+
into.tailBytes += chunk.length;
|
|
38
|
+
while (into.tail.length > 1 && into.tailBytes - into.tail[0].length >= KEEP_BYTES)
|
|
39
|
+
into.tailBytes -= into.tail.shift().length;
|
|
40
|
+
}
|
|
41
|
+
/** The text of a stream, and its length in characters when nothing was dropped in the middle. */
|
|
42
|
+
function streamText(from) {
|
|
43
|
+
const head = Array.from(new TextDecoder("utf-8").decode(Buffer.concat(from.head)));
|
|
44
|
+
if (from.tailBytes === 0)
|
|
45
|
+
return head;
|
|
46
|
+
let tail = Buffer.concat(from.tail);
|
|
47
|
+
if (tail.length > KEEP_BYTES)
|
|
48
|
+
tail = tail.subarray(tail.length - KEEP_BYTES);
|
|
49
|
+
// Bytes between the head and the tail were not kept; mark the gap with an empty slot.
|
|
50
|
+
const gap = from.total > from.headBytes + tail.length;
|
|
51
|
+
return [...head, ...(gap ? [""] : []), ...Array.from(new TextDecoder("utf-8").decode(tail))];
|
|
52
|
+
}
|
|
53
|
+
/** Keeps the start and the end, which usually holds the error, and marks the cut. */
|
|
54
|
+
function cut(chars, keep) {
|
|
55
|
+
const missing = chars.includes("");
|
|
56
|
+
if (!missing && chars.length <= keep)
|
|
57
|
+
return { text: chars.join(""), cut: false };
|
|
58
|
+
const front = Math.ceil(keep / 2);
|
|
59
|
+
const back = keep - front;
|
|
60
|
+
const omitted = missing ? "很多" : String(chars.length - keep);
|
|
61
|
+
return { text: `${chars.slice(0, front).join("")}\n…(輸出太長,中間省略了 ${omitted} 個字元)…\n${chars.slice(chars.length - back).join("")}`, cut: true };
|
|
62
|
+
}
|
|
63
|
+
/** Shares MAX_OUTPUT_CHARS between the two streams; a short stream keeps all of its text. */
|
|
64
|
+
function shareOutput(stdout, stderr) {
|
|
65
|
+
const errorKeep = Math.min(stderr.length, Math.max(exports.MAX_OUTPUT_CHARS - stdout.length, exports.MAX_OUTPUT_CHARS / 3));
|
|
66
|
+
const out = cut(stdout, exports.MAX_OUTPUT_CHARS - errorKeep);
|
|
67
|
+
const err = cut(stderr, errorKeep);
|
|
68
|
+
return { stdout: out.text, stderr: err.text, truncated: out.cut || err.cut };
|
|
69
|
+
}
|
|
70
|
+
// Bash writes its own pid first. Without job control, every process that the command starts
|
|
71
|
+
// stays in that process group, so `kill -- -pid` ends them all, also after a parent has exited.
|
|
72
|
+
const RUN_SCRIPT = `echo $$ > "$VANSPOD_BASH_PID_FILE"; unset VANSPOD_BASH_PID_FILE; eval "$VANSPOD_BASH_COMMAND"`;
|
|
73
|
+
/**
|
|
74
|
+
* Ends the command and everything it started. Git Bash forks do not keep the Windows parent link,
|
|
75
|
+
* so the Windows process tree alone can miss a running program. Kill the Bash process group first.
|
|
76
|
+
*/
|
|
77
|
+
function stopProcesses(bash, child, pidFile) {
|
|
78
|
+
let pid = "";
|
|
79
|
+
try {
|
|
80
|
+
pid = (0, node_fs_1.readFileSync)(pidFile, "utf8").trim();
|
|
81
|
+
}
|
|
82
|
+
catch { /* bash has not written it yet */ }
|
|
83
|
+
if (!/^[0-9]+$/.test(pid)) {
|
|
84
|
+
(0, commandRunner_1.killTree)(child);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
(0, node_child_process_1.spawn)(bash, ["-c", `kill -9 -- -${pid}`], { windowsHide: true, stdio: "ignore" })
|
|
88
|
+
.on("error", () => (0, commandRunner_1.killTree)(child))
|
|
89
|
+
.on("exit", () => (0, commandRunner_1.killTree)(child));
|
|
90
|
+
}
|
|
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 };
|
|
168
|
+
}
|
|
169
|
+
const NOT_RUN = "沒有執行";
|
|
170
|
+
/** The `bash` Local Tool. The log entry of the call runs the command, so the page can stop it. */
|
|
171
|
+
async function bashTool(projectFolder, args, signal, context) {
|
|
172
|
+
if (typeof args.command !== "string" || args.command.trim() === "") {
|
|
173
|
+
return { ok: false, error: "invalid_arguments", message: "command 必須是要在 Git Bash 執行的指令。" };
|
|
174
|
+
}
|
|
175
|
+
const command = args.command;
|
|
176
|
+
const bash = await context.gitBash();
|
|
177
|
+
if (!bash) {
|
|
178
|
+
return { ok: false, error: "failed", message: `這台電腦找不到 Git Bash,${NOT_RUN}。請學生在 VansPod 的「環境安裝」安裝 Git。` };
|
|
179
|
+
}
|
|
180
|
+
const refusal = await (0, localToolApproval_1.askApproval)(context.approver, { tool: "bash", title: "在 Git Bash 執行指令", body: command, target: command, folder: projectFolder }, signal, NOT_RUN);
|
|
181
|
+
if (refusal)
|
|
182
|
+
return refusal;
|
|
183
|
+
return context.call.runBash(bash, command, projectFolder, signal);
|
|
184
|
+
}
|