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
|
@@ -0,0 +1,137 @@
|
|
|
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.readProjectImage = readProjectImage;
|
|
7
|
+
exports.saveImage = saveImage;
|
|
8
|
+
// `save_image`: saves a generated image under `images/` in the Project Folder (ADR 0013).
|
|
9
|
+
// The Device-side contract is "save_image" in vans-mcp-server docs/device-relay-protocol.md.
|
|
10
|
+
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
11
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
12
|
+
const localToolApproval_1 = require("./localToolApproval");
|
|
13
|
+
const projectPath_1 = require("./projectPath");
|
|
14
|
+
const IMAGE_FOLDER = "images";
|
|
15
|
+
const NOT_SAVED = "沒有儲存圖片";
|
|
16
|
+
const EXTENSIONS = {
|
|
17
|
+
"image/png": [".png"],
|
|
18
|
+
"image/jpeg": [".jpg", ".jpeg"],
|
|
19
|
+
"image/webp": [".webp"],
|
|
20
|
+
"image/gif": [".gif"],
|
|
21
|
+
};
|
|
22
|
+
const BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
23
|
+
// Characters that Windows does not allow in a file name, and control characters.
|
|
24
|
+
const BAD_NAME_CHARS = /[<>:"|?*\u0000-\u001f]/g;
|
|
25
|
+
function invalid(message) {
|
|
26
|
+
return { ok: false, error: "invalid_arguments", message };
|
|
27
|
+
}
|
|
28
|
+
/** Only the base name, safe for Windows, with an extension that matches `mimeType`. */
|
|
29
|
+
function imageFileName(suggested, mimeType) {
|
|
30
|
+
const extensions = EXTENSIONS[mimeType];
|
|
31
|
+
const base = typeof suggested === "string" ? suggested.split(/[\\/]/).pop() : "";
|
|
32
|
+
let name = base.replace(BAD_NAME_CHARS, "").replace(/^[.\s]+|[.\s]+$/g, "");
|
|
33
|
+
if (!name)
|
|
34
|
+
name = "image";
|
|
35
|
+
if (!extensions.includes(node_path_1.default.extname(name).toLowerCase()))
|
|
36
|
+
name += extensions[0];
|
|
37
|
+
return name;
|
|
38
|
+
}
|
|
39
|
+
/** Width x height from the image header, or undefined when the header is not known. */
|
|
40
|
+
function dimensions(bytes, mimeType) {
|
|
41
|
+
if (mimeType === "image/png" && bytes.length >= 24)
|
|
42
|
+
return [bytes.readUInt32BE(16), bytes.readUInt32BE(20)];
|
|
43
|
+
if (mimeType === "image/gif" && bytes.length >= 10)
|
|
44
|
+
return [bytes.readUInt16LE(6), bytes.readUInt16LE(8)];
|
|
45
|
+
if (mimeType === "image/jpeg") {
|
|
46
|
+
// Walk the segments to the first start-of-frame marker (SOF0-SOF15, without DHT, JPG, DAC).
|
|
47
|
+
let at = 2;
|
|
48
|
+
while (at + 9 <= bytes.length && bytes[at] === 0xff) {
|
|
49
|
+
const marker = bytes[at + 1];
|
|
50
|
+
if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) {
|
|
51
|
+
return [bytes.readUInt16BE(at + 7), bytes.readUInt16BE(at + 5)];
|
|
52
|
+
}
|
|
53
|
+
at += 2 + bytes.readUInt16BE(at + 2);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
function approvalBody(bytes, mimeType) {
|
|
59
|
+
const size = dimensions(bytes, mimeType);
|
|
60
|
+
const lines = [`類型:${mimeType}`, `大小:${bytes.length.toLocaleString("en-US")} 位元組`];
|
|
61
|
+
if (size)
|
|
62
|
+
lines.splice(1, 0, `尺寸:${size[0]} × ${size[1]} 像素`);
|
|
63
|
+
return lines.join("\n");
|
|
64
|
+
}
|
|
65
|
+
/** The name in `images/` with "-2", "-3", ... before the extension when the name is taken. */
|
|
66
|
+
function candidate(name, count) {
|
|
67
|
+
if (count === 1)
|
|
68
|
+
return name;
|
|
69
|
+
const extension = node_path_1.default.extname(name);
|
|
70
|
+
return `${name.slice(0, name.length - extension.length)}-${count}${extension}`;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A saved image for the 遠端 Agent page, read from the disk. Only an image file under `images/`
|
|
74
|
+
* in the Project Folder; anything else, or a file that is gone, is undefined.
|
|
75
|
+
*/
|
|
76
|
+
async function readProjectImage(projectFolder, requested) {
|
|
77
|
+
if (!projectFolder)
|
|
78
|
+
return undefined;
|
|
79
|
+
const target = await (0, projectPath_1.resolveProjectPath)(projectFolder, requested);
|
|
80
|
+
if (!target.ok || !target.relative.startsWith(`${IMAGE_FOLDER}/`))
|
|
81
|
+
return undefined;
|
|
82
|
+
const extension = node_path_1.default.extname(target.relative).toLowerCase();
|
|
83
|
+
const mimeType = Object.keys(EXTENSIONS).find(type => EXTENSIONS[type].includes(extension));
|
|
84
|
+
if (!mimeType)
|
|
85
|
+
return undefined;
|
|
86
|
+
try {
|
|
87
|
+
if (!(await promises_1.default.stat(target.absolute)).isFile())
|
|
88
|
+
return undefined;
|
|
89
|
+
return { bytes: await promises_1.default.readFile(target.absolute), mimeType };
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return undefined;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async function saveImage(projectFolder, args, signal, context) {
|
|
96
|
+
const mimeType = args.mime_type;
|
|
97
|
+
if (typeof mimeType !== "string" || !Object.hasOwn(EXTENSIONS, mimeType)) {
|
|
98
|
+
return invalid("mime_type 必須是 image/png、image/jpeg、image/webp 或 image/gif。");
|
|
99
|
+
}
|
|
100
|
+
const data = typeof args.data === "string" ? args.data.replace(/\s/g, "") : "";
|
|
101
|
+
if (!data || data.length % 4 === 1 || !BASE64.test(data))
|
|
102
|
+
return invalid("data 必須是圖片內容的 base64 文字。");
|
|
103
|
+
const bytes = Buffer.from(data, "base64");
|
|
104
|
+
if (bytes.length === 0)
|
|
105
|
+
return invalid("data 必須是圖片內容的 base64 文字。");
|
|
106
|
+
const name = imageFileName(args.file_name, mimeType);
|
|
107
|
+
const refusal = await (0, localToolApproval_1.askApproval)(context.approver, {
|
|
108
|
+
tool: "save_image",
|
|
109
|
+
title: `儲存圖片 ${IMAGE_FOLDER}/${name}`,
|
|
110
|
+
target: `${IMAGE_FOLDER}/${name}`,
|
|
111
|
+
body: approvalBody(bytes, mimeType),
|
|
112
|
+
folder: projectFolder,
|
|
113
|
+
}, signal, NOT_SAVED);
|
|
114
|
+
if (refusal)
|
|
115
|
+
return refusal;
|
|
116
|
+
for (let count = 1; count <= 1000; count++) {
|
|
117
|
+
// Resolve each time: `images` may be a link out of the Project Folder, or may change while the student decided.
|
|
118
|
+
const target = await (0, projectPath_1.resolveProjectPath)(projectFolder, `${IMAGE_FOLDER}/${candidate(name, count)}`, { allowMissing: true });
|
|
119
|
+
if (!target.ok)
|
|
120
|
+
return target;
|
|
121
|
+
if (target.exists)
|
|
122
|
+
continue;
|
|
123
|
+
try {
|
|
124
|
+
await promises_1.default.mkdir(node_path_1.default.dirname(target.absolute), { recursive: true });
|
|
125
|
+
// "wx" fails when the file exists, so a file made in the meantime is never overwritten.
|
|
126
|
+
await promises_1.default.writeFile(target.absolute, bytes, { flag: "wx" });
|
|
127
|
+
context.call.target(target.relative);
|
|
128
|
+
return { ok: true, result: { path: target.relative } };
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (error.code === "EEXIST")
|
|
132
|
+
continue;
|
|
133
|
+
return { ok: false, error: "failed", message: `無法儲存「${target.relative}」:${error instanceof Error ? error.message : "原因不明"}。` };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return { ok: false, error: "failed", message: `${IMAGE_FOLDER}/ 裡同名的圖片太多,${NOT_SAVED}。` };
|
|
137
|
+
}
|
package/dist/start.js
CHANGED
|
@@ -7,6 +7,11 @@ exports.main = main;
|
|
|
7
7
|
const platformStorage_1 = require("./platformStorage");
|
|
8
8
|
const clientDetection_1 = require("./clientDetection");
|
|
9
9
|
const runtimeOptions_1 = require("./runtimeOptions");
|
|
10
|
+
const deviceConnection_1 = require("./deviceConnection");
|
|
11
|
+
const localTools_1 = require("./localTools");
|
|
12
|
+
const remoteAgentLog_1 = require("./remoteAgentLog");
|
|
13
|
+
const localToolApproval_1 = require("./localToolApproval");
|
|
14
|
+
const trayChannel_1 = require("./trayChannel");
|
|
10
15
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
11
16
|
const node_child_process_1 = require("node:child_process");
|
|
12
17
|
const promises_1 = __importDefault(require("node:fs/promises"));
|
|
@@ -14,6 +19,7 @@ const node_os_1 = __importDefault(require("node:os"));
|
|
|
14
19
|
const node_path_1 = __importDefault(require("node:path"));
|
|
15
20
|
const classroomApp_1 = require("./classroomApp");
|
|
16
21
|
const page_1 = require("./page");
|
|
22
|
+
const saveImage_1 = require("./saveImage");
|
|
17
23
|
const promptCapture_1 = require("./promptCapture");
|
|
18
24
|
const clipboard_1 = require("./clipboard");
|
|
19
25
|
const folderPicker_1 = require("./folderPicker");
|
|
@@ -244,6 +250,7 @@ async function openTray(url) {
|
|
|
244
250
|
await promises_1.default.mkdir(directory, { recursive: true });
|
|
245
251
|
const script = await (0, desktop_1.materializeAsset)(directory, "tray.ps1");
|
|
246
252
|
const icon = await (0, desktop_1.materializeAsset)(directory, "brand-icon.ico");
|
|
253
|
+
const mascot = await (0, desktop_1.materializeAsset)(directory, "fadou-superhero.png");
|
|
247
254
|
return (0, node_child_process_1.spawn)("powershell.exe", [
|
|
248
255
|
"-STA",
|
|
249
256
|
"-NoProfile",
|
|
@@ -254,7 +261,10 @@ async function openTray(url) {
|
|
|
254
261
|
"-Url", url,
|
|
255
262
|
"-IconPath", icon,
|
|
256
263
|
"-StateFile", node_path_1.default.join((0, instance_1.managementDirectory)(), "runtime", "instance.json"),
|
|
257
|
-
|
|
264
|
+
"-MascotPath", mascot,
|
|
265
|
+
],
|
|
266
|
+
// stdin and stdout are the tray channel (src/trayChannel.ts).
|
|
267
|
+
{ stdio: ["pipe", "pipe", "ignore"], windowsHide: true });
|
|
258
268
|
}
|
|
259
269
|
let proxyAccepting = false;
|
|
260
270
|
function createProxy() {
|
|
@@ -400,6 +410,12 @@ async function main(instance) {
|
|
|
400
410
|
},
|
|
401
411
|
handle: (accepted, req, res) => dispatch(accepted, req, res),
|
|
402
412
|
});
|
|
413
|
+
// One Local Tool Approval queue for this Device. The page and the Approval Popup (tray) decide here.
|
|
414
|
+
const approvals = (0, localToolApproval_1.createApprovalQueue)();
|
|
415
|
+
// The Remote Agent Log of this VansPod run. Only memory: a restart starts an empty log.
|
|
416
|
+
const remoteAgentLog = (0, remoteAgentLog_1.createRemoteAgentLog)();
|
|
417
|
+
let trayChannel;
|
|
418
|
+
approvals.subscribe(() => trayChannel?.update());
|
|
403
419
|
app = new classroomApp_1.ClassroomApp({
|
|
404
420
|
detectClient: clientDetection_1.detectClient,
|
|
405
421
|
adapterSettings: (0, adapterSettings_1.createAdapterSettingsStorage)(settingsFile),
|
|
@@ -436,6 +452,14 @@ async function main(instance) {
|
|
|
436
452
|
},
|
|
437
453
|
proxy: createProxy(),
|
|
438
454
|
send: sendUpstream,
|
|
455
|
+
device: (0, deviceConnection_1.createDeviceConnection)({
|
|
456
|
+
url: process.env.CLASSROOM_DEVICE_RELAY_URL ?? "wss://mcp.vanscoding.com/device",
|
|
457
|
+
device: node_os_1.default.hostname(),
|
|
458
|
+
handle: (0, localTools_1.createLocalTools)({ projectFolder: () => app.view().projectFolder, system: localTools_1.localSystem, approver: approvals, log: remoteAgentLog }),
|
|
459
|
+
onChange: () => trayChannel?.update(),
|
|
460
|
+
}),
|
|
461
|
+
approvals,
|
|
462
|
+
remoteAgentLog,
|
|
439
463
|
});
|
|
440
464
|
dispatch = async (accepted, req, res) => {
|
|
441
465
|
if (!/^(127\.0\.0\.1|localhost):\d+$/.test(req.headers.host ?? "")) {
|
|
@@ -454,6 +478,19 @@ async function main(instance) {
|
|
|
454
478
|
}
|
|
455
479
|
redirect(res);
|
|
456
480
|
};
|
|
481
|
+
const sendRemoteAgentPage = () => {
|
|
482
|
+
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
|
|
483
|
+
const html = (0, page_1.renderRemoteAgentPage)(app.view());
|
|
484
|
+
res.end(instance ? (0, lifecyclePage_1.lifecyclePage)(html, instance.token) : html);
|
|
485
|
+
};
|
|
486
|
+
// The Local Tool Access actions live on the 遠端 Agent page; its script posts them with X-Remote-Agent.
|
|
487
|
+
const goRemoteAgent = () => {
|
|
488
|
+
if (req.headers["x-remote-agent"] === "1") {
|
|
489
|
+
sendRemoteAgentPage();
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
redirect(res, "/remote-agent");
|
|
493
|
+
};
|
|
457
494
|
try {
|
|
458
495
|
if (instance && pathname.startsWith("/_vanspod/")) {
|
|
459
496
|
if (req.headers.authorization !== `Bearer ${instance.token}` || (req.headers.origin && req.headers.origin !== instance.origin)) {
|
|
@@ -538,6 +575,30 @@ async function main(instance) {
|
|
|
538
575
|
res.end(JSON.stringify((0, page_1.environmentStatus)(app.view())));
|
|
539
576
|
return;
|
|
540
577
|
}
|
|
578
|
+
if (req.method === "GET" && pathname === "/local-tool-approval-body") {
|
|
579
|
+
const answer = (0, page_1.localToolApprovalBodyResponse)(app, parsed.searchParams.get("id") ?? "");
|
|
580
|
+
res.writeHead(answer.status, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
|
|
581
|
+
res.end(JSON.stringify(answer.body));
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
if (req.method === "GET" && pathname === "/remote-agent-log-entry") {
|
|
585
|
+
const answer = (0, page_1.remoteAgentLogEntryResponse)(app.view(), parsed.searchParams.get("id") ?? "");
|
|
586
|
+
res.writeHead(answer.status, { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" });
|
|
587
|
+
res.end(JSON.stringify(answer.body));
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
// A saved image of the Remote Agent Log, read from the disk: only from images/ in the Project Folder.
|
|
591
|
+
if (req.method === "GET" && pathname === "/remote-agent-image") {
|
|
592
|
+
const image = await (0, saveImage_1.readProjectImage)(app.view().projectFolder, parsed.searchParams.get("path") ?? "");
|
|
593
|
+
if (!image) {
|
|
594
|
+
res.writeHead(404, { "Cache-Control": "no-store" });
|
|
595
|
+
res.end();
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
res.writeHead(200, { "Content-Type": image.mimeType, "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" });
|
|
599
|
+
res.end(image.bytes);
|
|
600
|
+
return;
|
|
601
|
+
}
|
|
541
602
|
if (req.method === "GET" && pathname === "/model-status") {
|
|
542
603
|
res.writeHead(200, {
|
|
543
604
|
"Content-Type": "application/json; charset=utf-8",
|
|
@@ -560,6 +621,10 @@ async function main(instance) {
|
|
|
560
621
|
res.end(JSON.stringify({ recording: promptCapture.recording, ...promptCapture.list() }));
|
|
561
622
|
return;
|
|
562
623
|
}
|
|
624
|
+
if (req.method === "GET" && pathname === "/remote-agent") {
|
|
625
|
+
sendRemoteAgentPage();
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
563
628
|
if (req.method === "GET" && pathname === "/prompt-inspector") {
|
|
564
629
|
const html = (0, page_1.renderInspectorPage)({ recording: promptCapture.recording, ...promptCapture.list() });
|
|
565
630
|
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" });
|
|
@@ -703,6 +768,39 @@ async function main(instance) {
|
|
|
703
768
|
res.end(JSON.stringify({ ok }));
|
|
704
769
|
return;
|
|
705
770
|
}
|
|
771
|
+
if (req.method === "POST" && pathname === "/local-tools") {
|
|
772
|
+
const params = new URLSearchParams(await readBody(req));
|
|
773
|
+
await app.setLocalToolAccess(params.get("on") === "1");
|
|
774
|
+
goRemoteAgent();
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
if (req.method === "POST" && pathname === "/local-tool-approval") {
|
|
778
|
+
const params = new URLSearchParams(await readBody(req));
|
|
779
|
+
const decision = params.get("decision");
|
|
780
|
+
if (decision === "approve" || decision === "deny")
|
|
781
|
+
app.decideLocalToolApproval(params.get("id") ?? "", decision);
|
|
782
|
+
goRemoteAgent();
|
|
783
|
+
return;
|
|
784
|
+
}
|
|
785
|
+
if (req.method === "POST" && pathname === "/auto-approval-start") {
|
|
786
|
+
const params = new URLSearchParams(await readBody(req));
|
|
787
|
+
const duration = params.get("duration");
|
|
788
|
+
if (duration && Object.hasOwn(classroomApp_1.AUTO_APPROVAL_DURATIONS, duration))
|
|
789
|
+
app.startAutoApproval(duration);
|
|
790
|
+
goRemoteAgent();
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
if (req.method === "POST" && pathname === "/auto-approval-stop") {
|
|
794
|
+
app.stopAutoApproval();
|
|
795
|
+
goRemoteAgent();
|
|
796
|
+
return;
|
|
797
|
+
}
|
|
798
|
+
if (req.method === "POST" && pathname === "/remote-agent-log-stop") {
|
|
799
|
+
const params = new URLSearchParams(await readBody(req));
|
|
800
|
+
app.stopRemoteAgentCommand(params.get("id") ?? "");
|
|
801
|
+
goRemoteAgent();
|
|
802
|
+
return;
|
|
803
|
+
}
|
|
706
804
|
if (req.method === "POST" && pathname === "/clear") {
|
|
707
805
|
const params = new URLSearchParams(await readBody(req));
|
|
708
806
|
await app.clearConnection();
|
|
@@ -867,6 +965,10 @@ async function main(instance) {
|
|
|
867
965
|
process.stderr.write(`系統匣圖示無法顯示:${trayError}\n`);
|
|
868
966
|
return undefined;
|
|
869
967
|
});
|
|
968
|
+
if (tray?.stdin && tray.stdout) {
|
|
969
|
+
const channel = trayChannel = (0, trayChannel_1.createTrayChannel)({ input: tray.stdout, output: tray.stdin, app });
|
|
970
|
+
tray.once("exit", () => channel.close());
|
|
971
|
+
}
|
|
870
972
|
}
|
|
871
973
|
if (runtimeOptions.desktop && !process.env.VANSPOD_WORKER_TOKEN)
|
|
872
974
|
(0, desktop_1.openBrowser)(home);
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TRAY_BODY_CHARS = exports.AUTO_APPROVAL_NEEDS_ACCESS = void 0;
|
|
4
|
+
exports.createTrayChannel = createTrayChannel;
|
|
5
|
+
const classroomApp_1 = require("./classroomApp");
|
|
6
|
+
Object.defineProperty(exports, "AUTO_APPROVAL_NEEDS_ACCESS", { enumerable: true, get: function () { return classroomApp_1.AUTO_APPROVAL_NEEDS_ACCESS; } });
|
|
7
|
+
/** The popup shows this much of the body. On the page, 「顯示全部」 shows all of it. */
|
|
8
|
+
exports.TRAY_BODY_CHARS = 2_000;
|
|
9
|
+
function trayText(value, max) {
|
|
10
|
+
return value.length <= max ? value : `${value.slice(0, max)}\n…(內容太長,完整內容請看 VansPod 頁面)`;
|
|
11
|
+
}
|
|
12
|
+
/** JSON with every character outside printable ASCII escaped. */
|
|
13
|
+
function asciiJson(value) {
|
|
14
|
+
return JSON.stringify(value).replace(/[^\x20-\x7e]/g, char => `\\u${char.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
15
|
+
}
|
|
16
|
+
function createTrayChannel(options) {
|
|
17
|
+
const { input, output, app } = options;
|
|
18
|
+
let closed = false;
|
|
19
|
+
let lastState;
|
|
20
|
+
let buffer = "";
|
|
21
|
+
const send = (message) => {
|
|
22
|
+
if (closed || output.destroyed || output.writableEnded)
|
|
23
|
+
return;
|
|
24
|
+
try {
|
|
25
|
+
output.write(`${asciiJson(message)}\n`);
|
|
26
|
+
}
|
|
27
|
+
catch { /* the tray has gone */ }
|
|
28
|
+
};
|
|
29
|
+
const state = (local) => {
|
|
30
|
+
const shown = local?.approval;
|
|
31
|
+
const auto = local?.autoApproval;
|
|
32
|
+
return {
|
|
33
|
+
type: "state",
|
|
34
|
+
access: local?.on ?? false,
|
|
35
|
+
autoApproval: auto ? { label: auto.label, ...(auto.endsAt === undefined ? {} : { endsAt: auto.endsAt, secondsLeft: auto.secondsLeft }) } : null,
|
|
36
|
+
approval: shown ? {
|
|
37
|
+
id: shown.id,
|
|
38
|
+
tool: shown.tool,
|
|
39
|
+
title: trayText(shown.title, 200),
|
|
40
|
+
body: trayText(shown.body, exports.TRAY_BODY_CHARS),
|
|
41
|
+
folder: shown.folder,
|
|
42
|
+
device: shown.device,
|
|
43
|
+
secondsLeft: shown.secondsLeft,
|
|
44
|
+
waiting: shown.waiting,
|
|
45
|
+
...(shown.exception ? { exception: shown.exception } : {}),
|
|
46
|
+
} : null,
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const update = () => {
|
|
50
|
+
if (closed)
|
|
51
|
+
return;
|
|
52
|
+
const local = app.view().localTools;
|
|
53
|
+
const current = state(local);
|
|
54
|
+
// secondsLeft alone does not make a new state: the tray counts down itself.
|
|
55
|
+
// A new end time of Auto Approval does.
|
|
56
|
+
const key = JSON.stringify({
|
|
57
|
+
...current,
|
|
58
|
+
autoApproval: current.autoApproval && { ...current.autoApproval, secondsLeft: 0 },
|
|
59
|
+
approval: current.approval && { ...current.approval, secondsLeft: 0 },
|
|
60
|
+
});
|
|
61
|
+
if (key === lastState)
|
|
62
|
+
return;
|
|
63
|
+
lastState = key;
|
|
64
|
+
send(current);
|
|
65
|
+
};
|
|
66
|
+
const receive = async (line) => {
|
|
67
|
+
let message;
|
|
68
|
+
try {
|
|
69
|
+
message = JSON.parse(line.replace(/^/, "").trim());
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
if (!message || typeof message !== "object")
|
|
75
|
+
return;
|
|
76
|
+
const { type, id, decision, on, duration } = message;
|
|
77
|
+
if (type === "decide" && typeof id === "string" && (decision === "approve" || decision === "deny")) {
|
|
78
|
+
app.decideLocalToolApproval(id, decision);
|
|
79
|
+
update();
|
|
80
|
+
}
|
|
81
|
+
else if (type === "access" && typeof on === "boolean") {
|
|
82
|
+
try {
|
|
83
|
+
await app.setLocalToolAccess(on);
|
|
84
|
+
}
|
|
85
|
+
catch { /* the state below tells the tray */ }
|
|
86
|
+
if (closed)
|
|
87
|
+
return;
|
|
88
|
+
const local = app.view().localTools;
|
|
89
|
+
if (on && !local?.on && local?.message)
|
|
90
|
+
send({ type: "refused", message: local.message });
|
|
91
|
+
update();
|
|
92
|
+
}
|
|
93
|
+
else if (type === "auto_approval_start" && typeof duration === "string" && Object.hasOwn(classroomApp_1.AUTO_APPROVAL_DURATIONS, duration)) {
|
|
94
|
+
if (!app.startAutoApproval(duration))
|
|
95
|
+
send({ type: "refused", message: classroomApp_1.AUTO_APPROVAL_NEEDS_ACCESS });
|
|
96
|
+
update();
|
|
97
|
+
}
|
|
98
|
+
else if (type === "auto_approval_stop") {
|
|
99
|
+
app.stopAutoApproval();
|
|
100
|
+
update();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
const onData = (chunk) => {
|
|
104
|
+
if (closed)
|
|
105
|
+
return;
|
|
106
|
+
buffer += chunk.toString();
|
|
107
|
+
const lines = buffer.split("\n");
|
|
108
|
+
buffer = lines.pop() ?? "";
|
|
109
|
+
for (const line of lines)
|
|
110
|
+
void receive(line);
|
|
111
|
+
};
|
|
112
|
+
input.on("data", onData);
|
|
113
|
+
output.on("error", () => { });
|
|
114
|
+
update();
|
|
115
|
+
return {
|
|
116
|
+
update,
|
|
117
|
+
close() {
|
|
118
|
+
closed = true;
|
|
119
|
+
input.off("data", onData);
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.windowsEnvironmentSystem = void 0;
|
|
7
|
+
exports.resolveCommand = resolveCommand;
|
|
7
8
|
exports.createWindowsEnvironmentSystem = createWindowsEnvironmentSystem;
|
|
8
9
|
const node_child_process_1 = require("node:child_process");
|
|
9
10
|
const promises_1 = __importDefault(require("node:fs/promises"));
|