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/dist/page.js CHANGED
@@ -3,11 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.modelStatus = modelStatus;
4
4
  exports.localToolApprovalBodyResponse = localToolApprovalBodyResponse;
5
5
  exports.localToolsStateText = localToolsStateText;
6
+ exports.remoteAgentLogEntryResponse = remoteAgentLogEntryResponse;
6
7
  exports.environmentStageText = environmentStageText;
7
8
  exports.environmentStatus = environmentStatus;
8
9
  exports.renderPage = renderPage;
9
10
  exports.renderInspectorPage = renderInspectorPage;
11
+ exports.renderRemoteAgentPage = renderRemoteAgentPage;
12
+ const node_crypto_1 = require("node:crypto");
10
13
  const version_1 = require("./version");
14
+ const html_1 = require("./html");
11
15
  const appearance_1 = require("./appearance");
12
16
  const installProgress_1 = require("./installProgress");
13
17
  const environmentProgress_1 = require("./environmentProgress");
@@ -19,7 +23,8 @@ const clientIcons_1 = require("./clientIcons");
19
23
  const environmentIcons_1 = require("./environmentIcons");
20
24
  const classroomApp_1 = require("./classroomApp");
21
25
  const promptCapture_1 = require("./promptCapture");
22
- const localCommands_1 = require("./localCommands");
26
+ const localToolApproval_1 = require("./localToolApproval");
27
+ const remoteAgentLogView_1 = require("./remoteAgentLogView");
23
28
  const inspectorView_1 = require("./inspectorView");
24
29
  const versionUpdateView_1 = require("./versionUpdateView");
25
30
  const powerMenuView_1 = require("./powerMenuView");
@@ -29,14 +34,6 @@ const VERSION = `v${version_1.VERSION}`;
29
34
  const COPY_ICON = `<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>`;
30
35
  const MOON = `<svg viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M21 14.5A8.5 8.5 0 1 1 9.5 3a7 7 0 0 0 11.5 11.5z"/></svg>`;
31
36
  const SUN = `<svg viewBox="0 0 24 24" aria-hidden="true"><circle cx="12" cy="12" r="4" fill="currentColor"/><path stroke="currentColor" stroke-width="2" stroke-linecap="round" d="M12 2.5v2M12 19.5v2M2.5 12h2M19.5 12h2M5 5l1.4 1.4M17.6 17.6 19 19M19 5l-1.4 1.4M6.4 17.6 5 19"/></svg>`;
32
- function escapeHtml(value) {
33
- return value
34
- .replaceAll("&", "&amp;")
35
- .replaceAll("<", "&lt;")
36
- .replaceAll(">", "&gt;")
37
- .replaceAll('"', "&quot;")
38
- .replaceAll("'", "&#39;");
39
- }
40
37
  function locked(view) {
41
38
  return view.commandRunning ? " disabled" : "";
42
39
  }
@@ -56,7 +53,7 @@ function restartHtml(view) {
56
53
  const joined = names.length === 1
57
54
  ? names[0]
58
55
  : `${names.slice(0, -1).join("、")} 與 ${names[names.length - 1]}`;
59
- parts.push(`<p>設定幫你更新好了,${escapeHtml(joined)} 還在裝睡。重新開啟 ${escapeHtml(joined)} 後生效,披風可不包叫醒服務。</p>`);
56
+ parts.push(`<p>設定幫你更新好了,${(0, html_1.escapeHtml)(joined)} 還在裝睡。重新開啟 ${(0, html_1.escapeHtml)(joined)} 後生效,披風可不包叫醒服務。</p>`);
60
57
  }
61
58
  if (clients.includes("vscode")) {
62
59
  parts.push(`<p>設定幫你更新好了,VS Code 還在裝睡。請完全退出 VS Code 再打開,不要只重載視窗。別敷衍牠,牠真的會繼續睡。</p>`);
@@ -69,10 +66,10 @@ function statusWord(view) {
69
66
  return view.mode === "classroom" ? "Classroom" : "Native";
70
67
  }
71
68
  function notices(view) {
72
- const notice = view.notice && view.notice !== view.connectionNotice ? `<p class="notice">${escapeHtml(view.notice)}</p>` : "";
69
+ const notice = view.notice && view.notice !== view.connectionNotice ? `<p class="notice">${(0, html_1.escapeHtml)(view.notice)}</p>` : "";
73
70
  const running = view.commandRunning ? `<p class="running">執行中</p>` : "";
74
71
  const klass = view.classLabel
75
- ? `<p class="class-label">${escapeHtml(view.classLabel)}</p>`
72
+ ? `<p class="class-label">${(0, html_1.escapeHtml)(view.classLabel)}</p>`
76
73
  : "";
77
74
  return `${klass}${notice}${running}`;
78
75
  }
@@ -80,9 +77,15 @@ function connectionFeedback(view) {
80
77
  const original = view.connectionError || view.connectionNotice || (view.detail !== "Classroom API Key 已設定。" ? view.detail : "");
81
78
  const message = original === classroomApp_1.COPIED_KEY_NOTICE
82
79
  ? "複製好了!API Key 不是群組伴手禮,別發給不信任的人喔。" : original;
83
- const feedback = `<div class="connection-feedback">${message ? `<div class="${view.connectionError ? "connection-error" : "connection-message"}" role="${view.connectionError ? "alert" : "status"}"><svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="9"/><path d="M12 7v6M12 15v2"/></svg><p>${escapeHtml(message)}</p></div>` : ""}</div>`;
80
+ const feedback = `<div class="connection-feedback">${message ? `<div class="${view.connectionError ? "connection-error" : "connection-message"}" role="${view.connectionError ? "alert" : "status"}"><svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8"><circle cx="12" cy="12" r="9"/><path d="M12 7v6M12 15v2"/></svg><p>${(0, html_1.escapeHtml)(message)}</p></div>` : ""}</div>`;
84
81
  return feedback;
85
82
  }
83
+ /** While VansPod waits for the browser: a link to the Authorization Page, for when the browser did not open. */
84
+ function authorizeLink(view) {
85
+ return view.connecting && view.authorizeUrl
86
+ ? `<p class="hint"><a href="${(0, html_1.escapeHtml)(view.authorizeUrl)}" target="_blank" rel="noopener">如果瀏覽器沒有打開,請點這裡</a></p>`
87
+ : "";
88
+ }
86
89
  function connectInner(view) {
87
90
  const feedback = connectionFeedback(view);
88
91
  if (view.canCopyKey) {
@@ -92,20 +95,26 @@ function connectInner(view) {
92
95
  </form>
93
96
  <form method="post" action="/clear">
94
97
  <button type="submit" class="quiet"${off}>清除連線</button>
95
- </form>${feedback}`;
98
+ </form>${authorizeLink(view)}${feedback}`;
96
99
  }
97
- return `<form class="stack" method="post" action="/redeem">
98
- <label>邀請碼 <input name="invite_code" autocomplete="off" value="${escapeHtml(view.connectionDraft?.inviteCode ?? "")}"${locked(view)}></label>
99
- <label>課堂暱稱 <input name="nickname" autocomplete="off" value="${escapeHtml(view.connectionDraft?.nickname ?? "")}"${locked(view)}></label>
100
+ if (view.connecting) {
101
+ return `<form class="stack" method="post" action="/connect-cancel">
102
+ <p class="connection-waiting" role="status">請在瀏覽器完成登入</p>
103
+ ${authorizeLink(view)}
104
+ ${feedback}
105
+ <button type="submit" class="quiet">取消</button>
106
+ </form>`;
107
+ }
108
+ return `<form class="stack" method="post" action="/connect">
100
109
  ${feedback}
101
110
  <button type="submit" class="go"${locked(view)}>連線</button>
102
111
  </form>`;
103
112
  }
104
113
  function folderInner(view) {
105
- const value = view.projectFolder ? ` value="${escapeHtml(view.projectFolder)}"` : "";
114
+ const value = view.projectFolder ? ` value="${(0, html_1.escapeHtml)(view.projectFolder)}"` : "";
106
115
  const off = locked(view);
107
116
  const note = view.installNotice
108
- ? `<p class="hint">${escapeHtml(view.installNotice)}</p>`
117
+ ? `<p class="hint">${(0, html_1.escapeHtml)(view.installNotice)}</p>`
109
118
  : "";
110
119
  return `<form class="stack" method="post" action="/project-folder">
111
120
  <label>專案資料夾 <input name="project_folder" autocomplete="off"${value}${off}></label>
@@ -117,7 +126,7 @@ function modelInner(view) {
117
126
  const off = locked(view);
118
127
  const models = view.allowedModels ?? (view.modelId ? [view.modelId] : []);
119
128
  const list = models.length
120
- ? `<ul class="allowed-model-list">${models.map(id => `<li>${escapeHtml(id)}</li>`).join("")}</ul>`
129
+ ? `<ul class="allowed-model-list">${models.map(id => `<li>${(0, html_1.escapeHtml)(id)}</li>`).join("")}</ul>`
121
130
  : `<p class="hint">${view.connected ? "這堂課尚無可用模型。" : "連線課堂後可查看。"}</p>`;
122
131
  return `
123
132
  ${restartHtml(view)}
@@ -139,19 +148,41 @@ function clientLight(view, client) {
139
148
  const lightClass = health === "green" ? " detected" : health === "red" || health === "yellow" ? ` health-${health}` : "";
140
149
  return { health, className: `client-light${lightClass}`, status };
141
150
  }
151
+ /**
152
+ * The connection state the page shows. The browser callback, 取消 and the 10-minute timeout change it
153
+ * outside any page action, so the status poll reloads the page when it changes.
154
+ */
155
+ function connectionKey(view) {
156
+ const state = view.connected ? "connected" : view.connecting ? "connecting" : "none";
157
+ // The messages go in as a hash, so the page does not show their text twice.
158
+ const messages = (0, node_crypto_1.createHash)("sha256").update(JSON.stringify([view.connectionError ?? "", view.connectionNotice ?? ""])).digest("hex").slice(0, 12);
159
+ return `${state}:${messages}`;
160
+ }
142
161
  function modelStatus(view) {
143
162
  return {
163
+ connection: connectionKey(view),
144
164
  lights: { codex: clientLight(view, "codex"), claude: clientLight(view, "claude"), vscode: clientLight(view, "vscode") },
145
165
  restart: restartHtml(view),
146
- ...(view.localTools ? { localTools: { on: view.localTools.on, text: localToolsStateText(view.localTools), approval: approvalStatus(view.localTools.approval), commands: commandsStatus(view.localTools.commands) } } : {}),
166
+ ...(view.localTools ? { localTools: { on: view.localTools.on, status: remoteStatus(view), approval: approvalStatus(view.localTools.approval, Boolean(view.localTools.autoApproval)), ...(view.localTools.log ? { log: (0, remoteAgentLogView_1.logListStatus)(view.localTools.log) } : {}) } } : {}),
147
167
  };
148
168
  }
149
- // The poll redraws the block only when the shown approval or the count changes; the seconds change every poll.
150
- function approvalStatus(approval) {
151
- return approval ? { key: approvalKey(approval), secondsLeft: approval.secondsLeft, html: approvalBlock(approval) } : null;
169
+ // The poll redraws the card only when the shown approval, the count or Auto Approval changes; the seconds and the bar change every poll.
170
+ function approvalStatus(approval, auto) {
171
+ return approval ? { key: approvalKey(approval, auto), secondsLeft: approval.secondsLeft, bar: approvalBarWidth(approval), html: approvalBlock(approval, auto) } : null;
172
+ }
173
+ function approvalBarWidth(approval) {
174
+ const total = localToolApproval_1.APPROVAL_TIMEOUT_MS / 1000;
175
+ return `${Math.round(Math.min(1, Math.max(0, approval.secondsLeft / total)) * 1000) / 10}%`;
176
+ }
177
+ function approvalKey(approval, auto) {
178
+ return `${approval.id}:${approval.waiting}${auto ? ":auto" : ""}`;
152
179
  }
153
- function approvalKey(approval) {
154
- return `${approval.id}:${approval.waiting}`;
180
+ const BOLT = `<svg class="auto-bolt" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M13 2 4 14h7l-1 8 9-12h-7z"/></svg>`;
181
+ /** The 自動允許 ▾ menu on the approval card. Each item opens the reminder dialog; only 確定打開 starts Auto Approval. */
182
+ function autoApprovalMenu() {
183
+ const items = Object.entries(classroomApp_1.AUTO_APPROVAL_DURATIONS)
184
+ .map(([duration, { label }]) => `<button type="button" role="menuitem" data-auto-approval="${duration}">${(0, html_1.escapeHtml)(label)}</button>`).join("");
185
+ return `<details class="auto-menu"><summary class="auto-menu-button">自動允許 ▾</summary><div class="auto-menu-list" role="menu">${items}</div></details>`;
155
186
  }
156
187
  const NO_APPROVAL = `<div data-local-approval hidden></div>`;
157
188
  /**
@@ -174,82 +205,98 @@ function localToolApprovalBodyResponse(app, id) {
174
205
  const body = app.localToolApprovalBody(id);
175
206
  return body === undefined ? { status: 404, body: { error: "這個確認已經結束。" } } : { status: 200, body: { id, body } };
176
207
  }
177
- // The Command Log on the page: the `bash` commands of this VansPod run (ADR 0013).
178
- const SHOWN_COMMANDS = 50;
179
- const SHOWN_COMMAND_CHARS = 300;
180
- function commandsKey(commands) {
181
- return commands.slice(0, SHOWN_COMMANDS).map(command => `${command.id}:${command.state}`).join(",");
182
- }
183
- // The poll redraws the list only when a command starts or ends.
184
- function commandsStatus(commands) {
185
- return commands ? { key: commandsKey(commands), html: commandsBlock(commands) } : null;
186
- }
187
- function commandStateText(command) {
188
- if (command.state === "running")
189
- return "執行中";
190
- if (command.state === "exited")
191
- return `結束代碼 ${command.exitCode ?? "不明"}`;
192
- if (command.state === "timed_out")
193
- return `超過 ${localCommands_1.BASH_TIMEOUT_MS / 1000} 秒,已停止`;
194
- if (command.state === "stopped")
195
- return "已停止";
196
- return "無法啟動";
197
- }
198
- function commandsBlock(commands) {
199
- if (!commands || commands.length === 0)
200
- return `<div class="local-commands" data-local-commands data-commands-key="" hidden></div>`;
201
- const items = commands.slice(0, SHOWN_COMMANDS).map(command => {
202
- const chars = Array.from(command.command);
203
- const text = chars.length > SHOWN_COMMAND_CHARS ? `${chars.slice(0, SHOWN_COMMAND_CHARS).join("")}…(共 ${chars.length} 個字元)` : command.command;
204
- const time = new Date(command.startedAt).toLocaleTimeString("zh-TW", { hour12: false });
205
- const stop = command.state === "running"
206
- ? `<form method="post" action="/local-command-stop"><input type="hidden" name="id" value="${escapeHtml(command.id)}"><button type="submit" class="local-command-stop">停止</button></form>`
207
- : "";
208
- return `<li><code>${escapeHtml(text)}</code><span class="hint">${escapeHtml(time)}・${escapeHtml(commandStateText(command))}</span>${stop}</li>`;
209
- }).join("");
210
- const older = commands.length > SHOWN_COMMANDS ? `<p class="hint">較早的 ${commands.length - SHOWN_COMMANDS} 個指令沒有顯示。</p>` : "";
211
- return `<div class="local-commands" data-local-commands data-commands-key="${escapeHtml(commandsKey(commands))}"><p class="hint">Agent Builder 這次執行過的指令(關掉 VansPod 就清除):</p>${older}<ol>${items}</ol></div>`;
212
- }
213
208
  // Local Tool Approval on the page (ADR 0013). No text field and no autofocus, so Enter cannot approve.
214
209
  function approvalBodyHtml(approval) {
215
210
  const { text, cut } = shownBody(approval.body);
216
- const more = cut ? `<button type="button" class="local-approval-full" data-approval-full="${escapeHtml(approval.id)}">顯示全部</button>` : "";
217
- return `<pre class="local-approval-body">${escapeHtml(text)}</pre>${more}`;
211
+ const more = cut ? `<button type="button" class="local-approval-full" data-approval-full="${(0, html_1.escapeHtml)(approval.id)}">顯示全部</button>` : "";
212
+ return `<pre class="local-approval-body">${(0, html_1.escapeHtml)(text)}</pre>${more}`;
218
213
  }
219
- function approvalBlock(approval) {
214
+ // During Auto Approval only exceptions are shown: the card says why, and has no 自動允許 ▾ (ADR 0014).
215
+ function approvalBlock(approval, auto) {
220
216
  if (!approval)
221
217
  return NO_APPROVAL;
222
- const waiting = approval.waiting > 0 ? `還有 ${approval.waiting} 個確認在排隊。` : "";
223
- return `<div class="local-approval" role="group" aria-label="Local Tool 確認" data-local-approval data-approval-key="${escapeHtml(approvalKey(approval))}">`
224
- + `<p class="local-approval-title">Agent Builder 要求:<strong>${escapeHtml(approval.title)}</strong></p>`
225
- + `<p class="hint">電腦:${escapeHtml(approval.device)} 資料夾:${escapeHtml(approval.folder)}</p>`
218
+ const waiting = approval.waiting > 0 ? `<span class="remote-chip local-approval-queue">還有 ${approval.waiting} 個在排隊</span>` : "";
219
+ const exception = auto && approval.exception;
220
+ return `<section class="tile local-approval${exception ? " is-exception" : ""}" role="group" aria-label="Local Tool 確認" data-local-approval data-approval-key="${(0, html_1.escapeHtml)(approvalKey(approval, auto))}">`
221
+ + (exception ? `<p class="local-approval-exception">${BOLT}自動允許期間仍要確認:<b>${(0, html_1.escapeHtml)(exception)}</b></p>` : "")
222
+ + `<div class="local-approval-head"><p class="local-approval-title">Agent 想要:<strong>${(0, html_1.escapeHtml)(approval.title)}</strong></p><code>${(0, html_1.escapeHtml)(approval.tool)}</code>${waiting}</div>`
223
+ + `<p class="hint">電腦:${(0, html_1.escapeHtml)(approval.device)} 資料夾:${(0, html_1.escapeHtml)(approval.folder)}</p>`
226
224
  + approvalBodyHtml(approval)
227
- + `<p class="hint"><span data-approval-seconds>${approval.secondsLeft}</span> 秒後自動拒絕。${waiting}</p>`
228
- + `<form class="local-approval-actions" method="post" action="/local-tool-approval"><input type="hidden" name="id" value="${escapeHtml(approval.id)}">`
229
- + `<button type="submit" name="decision" value="approve" class="local-approval-approve">允許一次</button>`
230
- + `<button type="submit" name="decision" value="deny" class="local-approval-deny">拒絕</button></form></div>`;
225
+ + `<div class="local-approval-timer"><span data-approval-bar style="width: ${approvalBarWidth(approval)}"></span></div>`
226
+ + `<form class="local-approval-actions" method="post" action="/local-tool-approval"><input type="hidden" name="id" value="${(0, html_1.escapeHtml)(approval.id)}">`
227
+ + `<span class="hint"><span data-approval-seconds>${approval.secondsLeft}</span> 秒後自動拒絕</span>`
228
+ + `<button type="submit" name="decision" value="deny" class="local-approval-deny">拒絕</button><button type="submit" name="decision" value="approve" class="local-approval-approve">允許一次</button>${auto ? "" : autoApprovalMenu()}</form></section>`;
231
229
  }
232
230
  /** One line about Local Tool Access for the student: the relay's message, or the connection state. */
233
231
  function localToolsStateText(local) {
234
232
  if (local.message)
235
233
  return local.message;
236
234
  if (local.state === "connected")
237
- return "已連上,Agent Builder 可以使用這台電腦。";
235
+ return "已連上,Agent 可以使用這台電腦。";
238
236
  if (local.state === "connecting")
239
237
  return "正在連線…";
240
238
  if (local.state === "retrying")
241
239
  return `連線中斷,${Math.max(1, Math.round((local.retryInMs ?? 1000) / 1000))} 秒後重新連線。`;
242
- return "已關閉,Agent Builder 不能使用這台電腦。";
240
+ return "已關閉,Agent 不能使用這台電腦。";
241
+ }
242
+ const LOCAL_TOOL_ACCESS = "允許 Agent 使用這台電腦";
243
+ // How to sign in on the Remote Agent's MCP Authorization Page: the same way as in VansPod (ADR 0013, ADR 0015).
244
+ function remoteSignInHint(local) {
245
+ if (local.loginMethod === "google")
246
+ return "在 Agent 的 MCP 授權頁,請選 Google,用同一個帳號登入。";
247
+ if (local.loginMethod === "nickname" && local.displayName)
248
+ return `在 Agent 的 MCP 授權頁,請選暱稱,輸入 <b>${(0, html_1.escapeHtml)(local.displayName)}</b>(大小寫要一樣)。`;
249
+ return "";
243
250
  }
244
- // 讓 Agent Builder 使用這台電腦 (Local Tool Access, ADR 0013). Only with a Classroom Connection.
245
- function localToolsBlock(view) {
251
+ // The status bar of the 遠端 Agent page: the connection, the names to match, and the
252
+ // 允許 Agent 使用這台電腦 switch (Local Tool Access, ADR 0013). `key` changes only when the bar changes.
253
+ function remoteStatus(view) {
246
254
  const local = view.localTools;
247
- if (!local || !view.connected)
248
- return "";
249
- const nickname = local.nickname
250
- ? `<p class="hint">在 Agent Builder 的授權頁選「暱稱」,輸入 <strong>${escapeHtml(local.nickname)}</strong>(大小寫要一樣)。這台電腦在 Agent Builder 叫做 <strong>${escapeHtml(local.device)}</strong>。</p>`
251
- : `<p class="hint">這台電腦在 Agent Builder 叫做 <strong>${escapeHtml(local.device)}</strong>。</p>`;
252
- return `<div class="local-tools" data-local-tools><form class="local-tools-row" method="post" action="/local-tools"><span>讓 Agent Builder 使用這台電腦</span><button type="submit" name="on" value="${local.on ? "0" : "1"}" role="switch" aria-checked="${local.on}" class="local-tools-switch${local.on ? " on" : ""}" data-local-tools-switch>${local.on ? "開" : "關"}</button></form><p class="hint" role="status" data-local-tools-state>${escapeHtml(localToolsStateText(local))}</p>${approvalBlock(local.approval)}${commandsBlock(local.commands)}${nickname}</div>`;
255
+ if (!local)
256
+ return { key: "", html: `<section class="tile remote-status" data-remote-status data-status-key=""><p class="hint">這個 VansPod 不能讓 Agent 使用這台電腦。</p></section>` };
257
+ const chip = (label, value) => `<span class="remote-chip">${label} <b>${(0, html_1.escapeHtml)(value)}</b></span>`;
258
+ const identity = local.displayName ? chip(local.loginMethod === "google" ? "Google" : "暱稱", local.displayName) : "";
259
+ const chips = chip("電腦", local.device) + identity + chip("資料夾", view.projectFolder ?? "還沒選");
260
+ const lit = local.on && local.state === "connected";
261
+ const guidance = !view.connected
262
+ ? `<p class="remote-setup">請先到「教室設定」按「連線」,才能讓 Agent 使用這台電腦。</p>`
263
+ : local.on ? ""
264
+ : `<p class="remote-setup">${remoteSignInHint(local)}這台電腦會顯示為 <b>${(0, html_1.escapeHtml)(local.device)}</b>。每次啟動 VansPod,這個開關都會關閉。</p>`;
265
+ const auto = local.autoApproval;
266
+ const main = `<div class="remote-status-main"><span class="remote-dot${lit ? " on" : ""}${auto ? " auto" : ""}"></span><div><strong role="status">${(0, html_1.escapeHtml)(localToolsStateText(local))}</strong><div class="remote-chips">${chips}</div></div></div>`;
267
+ const toggle = `<form class="remote-switch-row" method="post" action="/local-tools"><span>${LOCAL_TOOL_ACCESS}</span><button type="submit" name="on" value="${local.on ? "0" : "1"}" role="switch" aria-checked="${local.on}" aria-label="${LOCAL_TOOL_ACCESS}" class="remote-switch" data-local-tools-switch></button></form>`;
268
+ // The key leaves out the time left, which the page counts down by itself.
269
+ const autoLine = (time) => auto
270
+ ? `<div class="remote-auto-line">${BOLT}<b>自動允許中</b>${time}<span class="hint">Agent 的動作不會再問你。碰到機密檔案,或刪除專案資料夾以外的檔案,仍然會問。</span>`
271
+ + `<form method="post" action="/auto-approval-stop"><button type="submit" class="remote-auto-stop">停止自動允許</button></form></div>`
272
+ : "";
273
+ const time = !auto ? "" : auto.endsAt === undefined
274
+ ? `<span class="remote-auto-time">${(0, html_1.escapeHtml)(auto.label)}</span>`
275
+ : `<span class="remote-auto-time" data-auto-ends-at="${auto.endsAt}">剩 ${timeLeftText(auto.secondsLeft ?? 0)}</span>`;
276
+ const key = statusKey(`${main}${toggle}${autoLine(auto?.endsAt === undefined ? "" : String(auto.endsAt))}${guidance}`);
277
+ const body = `${main}${toggle}${autoLine(time)}${guidance}`;
278
+ return { key, html: `<section class="tile remote-status${auto ? " is-auto" : ""}" data-remote-status data-status-key="${(0, html_1.escapeHtml)(key)}">${body}</section>` };
279
+ }
280
+ /** The time left of Auto Approval, for example "9:41". */
281
+ function timeLeftText(seconds) {
282
+ const whole = Math.max(0, Math.ceil(seconds));
283
+ return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, "0")}`;
284
+ }
285
+ // A short key of the status bar, so that the poll redraws it only when it changes.
286
+ function statusKey(html) {
287
+ let hash = 0;
288
+ for (let i = 0; i < html.length; i++)
289
+ hash = (hash * 31 + html.charCodeAt(i)) | 0;
290
+ return `${html.length}:${(hash >>> 0).toString(36)}`;
291
+ }
292
+ /** The 遠端 Agent page body that the poll and the page actions redraw: status bar, approval card, Remote Agent Log. */
293
+ function remoteAgentBody(view) {
294
+ const log = view.localTools?.log;
295
+ return `${remoteStatus(view).html}${approvalBlock(view.localTools?.approval, Boolean(view.localTools?.autoApproval))}${log ? (0, remoteAgentLogView_1.remoteAgentLogBlock)(log) : ""}`;
296
+ }
297
+ /** The answer of GET /remote-agent-log-entry?id=...: the details of one Remote Agent Log entry, or 404. */
298
+ function remoteAgentLogEntryResponse(view, id) {
299
+ return (0, remoteAgentLogView_1.logEntryResponse)(view.localTools?.log, id);
253
300
  }
254
301
  function adapterForms(view) {
255
302
  const selected = view.adapters ?? { codex: "responses", claude: "responses" };
@@ -266,7 +313,7 @@ function adapterForms(view) {
266
313
  return `<div class="model-client"><div class="client-head"><span class="adapter-label"><span class="${className}" tabindex="0" title="${status}" aria-label="${status}" data-client-light="${client}"></span>${clientIcons_1.clientIcons[client]}<span>${clientLabel(client)}</span></span><form class="modes client-modes" method="post" action="/switch"><input type="hidden" name="client" value="${client}"><button type="submit" name="mode" value="native" aria-pressed="${mode === "native"}" class="${mode === "native" ? "on" : ""}"${locked(view)}>個人</button><button type="submit" name="mode" value="classroom" aria-pressed="${mode === "classroom"}" class="${mode === "classroom" ? "on" : ""}"${!available || !routeHealthy || view.commandRunning ? " disabled" : ""}>課堂</button></form></div><form class="adapter-row" method="post" action="/adapter"><input type="hidden" name="client" value="${client}"><span class="adapter-select"><select name="adapter" aria-label="${clientLabel(client)} API 協定"${adapterDisabled ? " disabled" : ""}>${options}</select><svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m7 10 5 5 5-5"/></svg></span></form><span class="client-flow" data-client-flow="${client}" aria-hidden="true"></span></div>`;
267
314
  }).join("");
268
315
  return forms +
269
- (view.adapterNotice ? `<p class="hint">${escapeHtml(view.adapterNotice)}</p>` : "");
316
+ (view.adapterNotice ? `<p class="hint">${(0, html_1.escapeHtml)(view.adapterNotice)}</p>` : "");
270
317
  }
271
318
  /** Environment Tool Readiness 與按下「確認安裝」後對該工具做的事。 */
272
319
  const READINESS_MARK = {
@@ -319,17 +366,17 @@ function environmentInner(view) {
319
366
  const tools = view.tools
320
367
  .map((tool) => {
321
368
  const checked = tool.selected ? " checked" : "";
322
- const title = tool.reason ? ` title="${escapeHtml(tool.reason)}"` : "";
369
+ const title = tool.reason ? ` title="${(0, html_1.escapeHtml)(tool.reason)}"` : "";
323
370
  // 安裝中的工具顯示階段;頁面輪詢 /environment-status 更新這段文字。
324
371
  const mark = view.environmentInstalling && tool.progress
325
- ? `<span class="tool-stage" data-environment-stage="${escapeHtml(tool.id)}">${escapeHtml(environmentStageText(tool.progress))}</span>`
372
+ ? `<span class="tool-stage" data-environment-stage="${(0, html_1.escapeHtml)(tool.id)}">${(0, html_1.escapeHtml)(environmentStageText(tool.progress))}</span>`
326
373
  : READINESS_MARK[tool.readiness];
327
- return `<label class="tool"><input type="checkbox" name="tool" value="${escapeHtml(tool.id)}"${checked}${off}> ${environmentIcons_1.environmentIcons[tool.id]}<span${title}>${escapeHtml(tool.label)}</span> ${mark}</label>`;
374
+ return `<label class="tool"><input type="checkbox" name="tool" value="${(0, html_1.escapeHtml)(tool.id)}"${checked}${off}> ${environmentIcons_1.environmentIcons[tool.id]}<span${title}>${(0, html_1.escapeHtml)(tool.label)}</span> ${mark}</label>`;
328
375
  })
329
376
  .join("");
330
377
  const polling = environmentPolling(view) ? " data-environment-progress" : "";
331
378
  const note = view.environmentNotice
332
- ? `<p class="hint">${escapeHtml(view.environmentNotice)}</p>`
379
+ ? `<p class="hint">${(0, html_1.escapeHtml)(view.environmentNotice)}</p>`
333
380
  : "";
334
381
  // 紀錄資料夾的路徑不放進頁面;伺服器開啟 ClassroomApp 記得的最近一次。
335
382
  const openLog = view.environmentLogFolder && !view.commandRunning
@@ -357,10 +404,10 @@ function catalogInner(view) {
357
404
  return `<p class="empty">這台電腦還沒有課程清單。</p>`;
358
405
  }
359
406
  const error = view.catalogError
360
- ? `<p class="error">${escapeHtml(view.catalogError)}</p>`
407
+ ? `<p class="error">${(0, html_1.escapeHtml)(view.catalogError)}</p>`
361
408
  : "";
362
409
  const note = view.catalog?.localNote
363
- ? `<p class="hint">${escapeHtml(view.catalog.localNote)}</p>`
410
+ ? `<p class="hint">${(0, html_1.escapeHtml)(view.catalog.localNote)}</p>`
364
411
  : "";
365
412
  return `${note}${error}`;
366
413
  }
@@ -370,7 +417,7 @@ function commandInner(view) {
370
417
  return `<p class="empty">沒有待確認的指令。</p>`;
371
418
  }
372
419
  const command = view.pendingCommand
373
- ? `<pre class="pending">${escapeHtml(view.pendingCommand)}</pre>`
420
+ ? `<pre class="pending">${(0, html_1.escapeHtml)(view.pendingCommand)}</pre>`
374
421
  : "";
375
422
  // 執行中保留「確認執行」的空間,讓「停止安裝」落在「取消」的位置,避免連點誤觸。
376
423
  const running = view.commandRunning;
@@ -382,17 +429,17 @@ function commandInner(view) {
382
429
  </div>`
383
430
  : "";
384
431
  const output = hasOutput
385
- ? `<h3>指令輸出</h3><pre>${escapeHtml(view.commandOutput ?? "")}</pre>`
432
+ ? `<h3>指令輸出</h3><pre>${(0, html_1.escapeHtml)(view.commandOutput ?? "")}</pre>`
386
433
  : "";
387
434
  return `${command}${choices}${output}`;
388
435
  }
389
436
  function snippetInner(snippet) {
390
437
  const hint = snippet.pasteHint
391
- ? `<p class="hint">${escapeHtml(snippet.pasteHint)}</p>`
438
+ ? `<p class="hint">${(0, html_1.escapeHtml)(snippet.pasteHint)}</p>`
392
439
  : "";
393
- return `<h3>${escapeHtml(snippet.title)}</h3>
440
+ return `<h3>${(0, html_1.escapeHtml)(snippet.title)}</h3>
394
441
  ${hint}
395
- <pre>${escapeHtml(snippet.body)}</pre>`;
442
+ <pre>${(0, html_1.escapeHtml)(snippet.body)}</pre>`;
396
443
  }
397
444
  function catalogTable(view) {
398
445
  const head = catalogInner(view);
@@ -400,13 +447,13 @@ function catalogTable(view) {
400
447
  .map((action) => {
401
448
  const off = view.commandRunning ? " disabled" : "";
402
449
  const description = action.description
403
- ? `<p class="hint">${escapeHtml(action.description)}</p>`
450
+ ? `<p class="hint">${(0, html_1.escapeHtml)(action.description)}</p>`
404
451
  : "";
405
452
  return `<tr>
406
- <td>${escapeHtml((0, courseCatalog_1.actionKindLabel)(action.kind))}</td>
407
- <td><strong>${escapeHtml(action.title)}</strong>${description}<code>${escapeHtml(action.command)}</code></td>
453
+ <td>${(0, html_1.escapeHtml)((0, courseCatalog_1.actionKindLabel)(action.kind))}</td>
454
+ <td><strong>${(0, html_1.escapeHtml)(action.title)}</strong>${description}<code>${(0, html_1.escapeHtml)(action.command)}</code></td>
408
455
  <td><form method="post" action="/prepare">
409
- <input type="hidden" name="action_id" value="${escapeHtml(action.id)}">
456
+ <input type="hidden" name="action_id" value="${(0, html_1.escapeHtml)(action.id)}">
410
457
  <button type="submit"${off}>查看完整指令</button>
411
458
  </form></td>
412
459
  </tr>`;
@@ -727,6 +774,12 @@ const SIDE_PAGES = [
727
774
  label: "Prompt 觀察室",
728
775
  icon: `<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><circle cx="11" cy="11" r="6.5"/><path d="m20 20-4.4-4.4M8 9.5h6M8 12.5h4"/></svg>`,
729
776
  },
777
+ {
778
+ id: "remote-agent",
779
+ href: "/remote-agent",
780
+ label: "遠端 Agent",
781
+ icon: `<svg viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="13" rx="2"/><path d="m7 9 2.5 2L7 13M12 13h4M8 21h8M12 17v4"/></svg>`,
782
+ },
730
783
  ];
731
784
  const BRAND = `<div class="brand"><div class="logo-badge"><img src="/brand-logo.png" alt=""></div><h1 class="brand-title">${PRODUCT}</h1><span class="ver">${VERSION}</span></div>`;
732
785
  /** 兩頁共用的側欄:行動版頂列、抽屜遮罩、選單與外觀切換。 */
@@ -803,7 +856,8 @@ function basePage(view) {
803
856
  <button type="button" data-appearance-toggle aria-label="切換外觀"></button>
804
857
  </header>
805
858
  <main>
806
- <p class="display">${escapeHtml(statusWord(view))}</p>
859
+ <div hidden data-connection-key="${(0, html_1.escapeHtml)(connectionKey(view))}"></div>
860
+ <p class="display">${(0, html_1.escapeHtml)(statusWord(view))}</p>
807
861
  ${notices(view)}
808
862
  <div class="modules">
809
863
  ${tile("連線", connectInner(view))}
@@ -823,10 +877,10 @@ function installActionCards(view) {
823
877
  .map((action, index) => {
824
878
  const number = index + 1;
825
879
  const description = action.description
826
- ? `<p class="hint">${escapeHtml(action.description)}</p>`
880
+ ? `<p class="hint">${(0, html_1.escapeHtml)(action.description)}</p>`
827
881
  : "";
828
882
  const installed = view.installedActionIds?.includes(action.id) ?? false;
829
- return `<article class="install-card${installed ? " installed" : ""}"><span class="install-number" aria-hidden="true">${number}</span><div class="install-main"><div class="install-line"><strong><span class="visually-hidden">安裝 ${number}:</span>${escapeHtml(action.title)}</strong><span data-kind="${escapeHtml(action.kind)}" class="kind-tag">${escapeHtml((0, courseCatalog_1.actionKindLabel)(action.kind))}</span></div>${description}</div><form method="post" action="/prepare"><input type="hidden" name="action_id" value="${escapeHtml(action.id)}"><button type="submit"${installed ? ' class="quiet"' : ""}${off}>${installed ? "重新安裝" : "安裝"}</button></form></article>`;
883
+ return `<article class="install-card${installed ? " installed" : ""}"><span class="install-number" aria-hidden="true">${number}</span><div class="install-main"><div class="install-line"><strong><span class="visually-hidden">安裝 ${number}:</span>${(0, html_1.escapeHtml)(action.title)}</strong><span data-kind="${(0, html_1.escapeHtml)(action.kind)}" class="kind-tag">${(0, html_1.escapeHtml)((0, courseCatalog_1.actionKindLabel)(action.kind))}</span></div>${description}</div><form method="post" action="/prepare"><input type="hidden" name="action_id" value="${(0, html_1.escapeHtml)(action.id)}"><button type="submit"${installed ? ' class="quiet"' : ""}${off}>${installed ? "重新安裝" : "安裝"}</button></form></article>`;
830
884
  })
831
885
  .join("");
832
886
  }
@@ -845,7 +899,7 @@ function presentCommand(body, view) {
845
899
  return { inline, dialog };
846
900
  }
847
901
  function projectFolderRow(view) {
848
- return `<div class="project-folder-row"><div class="project-folder-info"><span class="hint">專案資料夾</span><p class="folder-path"${view.projectFolder ? ` title="${escapeHtml(view.projectFolder)}"` : ""}>${escapeHtml(view.projectFolder || "尚未選擇安裝位置")}</p></div><form method="post" action="/pick-folder"><button type="submit" class="quiet"${locked(view)}>${view.projectFolder ? "變更" : "選擇專案資料夾"}</button></form>${view.folderError ? `<p class="error" role="alert">${escapeHtml(view.folderError)}</p>` : ""}${view.installNotice ? `<p class="hint" role="status">${escapeHtml(view.installNotice)}</p>` : ""}</div>`;
902
+ return `<div class="project-folder-row"><div class="project-folder-info"><span class="hint">專案資料夾</span><p class="folder-path"${view.projectFolder ? ` title="${(0, html_1.escapeHtml)(view.projectFolder)}"` : ""}>${(0, html_1.escapeHtml)(view.projectFolder || "尚未選擇安裝位置")}</p></div><form method="post" action="/pick-folder"><button type="submit" class="quiet"${locked(view)}>${view.projectFolder ? "變更" : "選擇專案資料夾"}</button></form>${view.folderError ? `<p class="error" role="alert">${(0, html_1.escapeHtml)(view.folderError)}</p>` : ""}${view.installNotice ? `<p class="hint" role="status">${(0, html_1.escapeHtml)(view.installNotice)}</p>` : ""}</div>`;
849
903
  }
850
904
  function splitCourseCards(html, view) {
851
905
  const commandMatch = html.match(/<section class="tile wide"><h2>指令<\/h2>([\s\S]*?)<\/section>/);
@@ -862,12 +916,12 @@ function splitCourseCards(html, view) {
862
916
  const copied = new Set(view.copiedSnippetIds ?? []);
863
917
  const snippetBody = (view.catalog?.snippets ?? []).map((snippet, index) => {
864
918
  const number = index + 1;
865
- const title = escapeHtml(snippet.title);
919
+ const title = (0, html_1.escapeHtml)(snippet.title);
866
920
  const done = copied.has(snippet.id);
867
921
  const hint = snippet.pasteHint
868
- ? `<span class="snippet-hint">${escapeHtml(snippet.pasteHint)}</span>`
922
+ ? `<span class="snippet-hint">${(0, html_1.escapeHtml)(snippet.pasteHint)}</span>`
869
923
  : "";
870
- return `<article class="snippet-card${done ? " copied" : ""}"><span class="snippet-number" aria-hidden="true">${number}</span><div class="snippet-main"><div class="snippet-line"><strong><span class="visually-hidden">片段 ${number}:</span>${title}</strong>${hint}</div><details class="snippet-peek"><summary>查看內容</summary><pre class="snippet-code">${escapeHtml(snippet.body)}</pre></details></div><button type="button"${done ? ' class="quiet"' : ""} data-copy-snippet="${escapeHtml(snippet.id)}" aria-label="${done ? "再複製一次" : "複製"}「${title}」">${COPY_ICON}<span>${done ? "✓ 已複製" : "複製"}</span></button><span class="visually-hidden" data-snippet-copy-status role="status" aria-live="polite"></span></article>`;
924
+ return `<article class="snippet-card${done ? " copied" : ""}"><span class="snippet-number" aria-hidden="true">${number}</span><div class="snippet-main"><div class="snippet-line"><strong><span class="visually-hidden">片段 ${number}:</span>${title}</strong>${hint}</div><details class="snippet-peek"><summary>查看內容</summary><pre class="snippet-code">${(0, html_1.escapeHtml)(snippet.body)}</pre></details></div><button type="button"${done ? ' class="quiet"' : ""} data-copy-snippet="${(0, html_1.escapeHtml)(snippet.id)}" aria-label="${done ? "再複製一次" : "複製"}「${title}」">${COPY_ICON}<span>${done ? "✓ 已複製" : "複製"}</span></button><span class="visually-hidden" data-snippet-copy-status role="status" aria-live="polite"></span></article>`;
871
925
  }).join("") || `<p class="empty">這堂課沒有片段。</p>`;
872
926
  const chevron = `<svg class="course-chevron" viewBox="0 0 24 24" aria-hidden="true" fill="none" stroke="currentColor" stroke-width="2"><path d="m9 6 6 6-6 6"/></svg>`;
873
927
  const install = `<section class="tile wide"><details class="course-section" data-course-section="install" open><summary class="tile-head"><h2>課程安裝</h2>${chevron}</summary><div class="course-content">${courseBody}${presented.inline}</div></details></section>${presented.dialog}`;
@@ -968,17 +1022,19 @@ function organizeSettings(html, view) {
968
1022
  .replace('<div class="mascot-traveler">', `${speech}<div class="mascot-traveler">`);
969
1023
  }
970
1024
  if (!actions) {
971
- body = body.replace('action="/redeem"', 'action="/redeem" id="settings-redeem"');
1025
+ body = body.replace(/action="\/(connect|connect-cancel)"/, '$& id="settings-connect"');
972
1026
  body = body.replace(/<button[\s\S]*?<\/button>/, (button) => {
973
- actions = button.replace('<button ', '<button form="settings-redeem" ');
1027
+ actions = button.replace('<button ', '<button form="settings-connect" ');
974
1028
  return "";
975
1029
  });
976
1030
  const feedback = body.match(/<div class="connection-feedback">[\s\S]*?<\/div>(?=\s*<\/form>)/)?.[0] ?? '<div class="connection-feedback"></div>';
977
1031
  body = body.replace(feedback, "");
978
- const quote = view.connectionError === "請先輸入邀請碼與課堂暱稱,再按「連線」。"
979
- ? "我披風都穿好了,你的邀請碼跟暱稱呢?"
980
- : "連線這關還沒過,披風倒是先上班了。照下面的提示再試一次!";
981
- const speech = `<div class="mascot-speech"><p class="mascot-quote">${escapeHtml(quote)}</p>${feedback}</div>`;
1032
+ const quote = view.connecting
1033
+ ? "我在這裡等你,去瀏覽器完成登入吧!"
1034
+ : !view.connectionError
1035
+ ? "我披風都穿好了,按「連線」去瀏覽器登入吧!"
1036
+ : "連線這關還沒過,披風倒是先上班了。照下面的提示再試一次!";
1037
+ const speech = `<div class="mascot-speech"><p class="mascot-quote">${(0, html_1.escapeHtml)(quote)}</p>${feedback}</div>`;
982
1038
  body += connectionMascot_1.connectionMascot.replace('class="connection-mascot"', 'class="connection-mascot mascot-waiting"').replace('<div class="mascot-traveler">', `${speech}<div class="mascot-traveler">`);
983
1039
  }
984
1040
  }
@@ -1048,17 +1104,17 @@ function renderPage(view) {
1048
1104
  if (view.connected && view.canCopyKey) {
1049
1105
  const off = view.commandRunning ? " disabled" : "";
1050
1106
  const course = view.courseTitle
1051
- ? `<p class="course-title">${escapeHtml(view.courseTitle)}</p>`
1107
+ ? `<p class="course-title">${(0, html_1.escapeHtml)(view.courseTitle)}</p>`
1052
1108
  : "";
1053
- const meta = [view.sessionTitle, view.nickname]
1109
+ const meta = [view.sessionTitle, view.displayName]
1054
1110
  .filter((value) => Boolean(value))
1055
- .map((value) => `<span>${escapeHtml(value)}</span>`)
1111
+ .map((value) => `<span>${(0, html_1.escapeHtml)(value)}</span>`)
1056
1112
  .join('<span class="connection-separator" aria-hidden="true">·</span>');
1057
1113
  const identity = course || meta
1058
1114
  ? `<div class="connection-identity">${course}${meta ? `<p class="connection-meta">${meta}</p>` : ""}</div>`
1059
1115
  : "";
1060
1116
  const mascot = `<div class="classroom-update-home"><div class="classroom-update">${connectionMascot_1.connectionMascot}${(0, classroomUpdate_1.classroomUpdateControl)(view, restartHtml(view))}</div></div>`;
1061
- const block = `${identity}<div class="key-row"><p>API Key 已設定</p><form method="post" action="/copy"><button type="submit" class="connection-copy" aria-label="複製 Classroom API Key"${off}>${COPY_ICON}<span>複製</span></button></form></div>${mascot}<form method="post" action="/clear"><button type="submit" class="connection-clear"${off}>清除連線</button></form>${localToolsBlock(view)}`;
1117
+ const block = `${identity}<div class="key-row"><p>API Key 已設定</p><form method="post" action="/copy"><button type="submit" class="connection-copy" aria-label="複製 Classroom API Key"${off}>${COPY_ICON}<span>複製</span></button></form></div>${mascot}<form method="post" action="/clear"><button type="submit" class="connection-clear"${off}>清除連線</button></form>`;
1062
1118
  html = html.replace(/<form method="post" action="\/copy">[\s\S]*?<\/form>\s*<form method="post" action="\/clear">[\s\S]*?<\/form>/, block);
1063
1119
  html = html.replace(/<p class="class-label">[\s\S]*?<\/p>/, "");
1064
1120
  html = html.replace(`<p class="detail">Classroom API Key 已設定。</p>`, "");
@@ -1090,21 +1146,6 @@ function renderPage(view) {
1090
1146
  .connection-meta { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 8px; color: var(--muted); font-size: 13px; line-height: 1.6; }
1091
1147
  .connection-meta > span { min-width: 0; overflow-wrap: anywhere; }
1092
1148
  .connection-separator { opacity: 0.5; }
1093
- .local-tools { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); }
1094
- .local-tools-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
1095
- .local-tools-switch { min-width: 3.5rem; background: var(--modes); color: var(--text); }
1096
- .local-tools-switch.on { background: var(--accent); color: var(--on-accent); }
1097
- .local-tools .hint { font-size: 13px; overflow-wrap: anywhere; }
1098
- .local-approval { display: flex; flex-direction: column; gap: 6px; padding: 10px; border: 2px solid var(--accent); border-radius: 8px; }
1099
- .local-approval-title { margin: 0; overflow-wrap: anywhere; }
1100
- .local-approval-body { margin: 0; max-height: 16rem; overflow: auto; padding: 8px; background: var(--modes); border-radius: 6px; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
1101
- .local-approval-full { align-self: flex-start; background: none; color: var(--accent); padding: 0; text-decoration: underline; }
1102
- .local-approval-actions { display: flex; gap: 8px; }
1103
- .local-commands ol { margin: 0; padding-left: 1.4rem; display: flex; flex-direction: column; gap: 4px; max-height: 14rem; overflow: auto; }
1104
- .local-commands li code { white-space: pre-wrap; overflow-wrap: anywhere; margin-right: 6px; }
1105
- .local-command-stop { margin-left: 6px; padding: 2px 10px; }
1106
- .local-commands form { display: inline; }
1107
- .local-approval-approve { background: var(--accent); color: var(--on-accent); }
1108
1149
  .tool em { color: var(--text); }
1109
1150
  .tool em.missing { color: var(--muted); }
1110
1151
  .tool em.needs-repair { color: light-dark(#9a4a08, #fbbf24); }
@@ -1222,7 +1263,7 @@ function renderPage(view) {
1222
1263
  main button.connection-clear:hover:not(:disabled) { color: light-dark(#b91c1c, #f87171); }
1223
1264
  ${classroomUpdate_1.classroomUpdateStyles}
1224
1265
  </style>
1225
- <script>${drawerScript()}${powerMenuView_1.powerMenuScript}${installProgress_1.installProgressScript}${environmentProgress_1.environmentProgressScript}${snippetCopyScript()}${courseSectionScript()}${connectionMascot_1.connectionMascotScript}${classroomUpdate_1.classroomUpdateScript}${settingsActions_1.settingsActionsScript}${modelStatus_1.modelStatusScript}${modelStatus_1.approvalFullScript}
1266
+ <script>${drawerScript()}${powerMenuView_1.powerMenuScript}${installProgress_1.installProgressScript}${environmentProgress_1.environmentProgressScript}${snippetCopyScript()}${courseSectionScript()}${connectionMascot_1.connectionMascotScript}${classroomUpdate_1.classroomUpdateScript}${settingsActions_1.settingsActionsScript}${modelStatus_1.modelStatusScript}
1226
1267
  </script>`;
1227
1268
  return html.replace("</body>", `${extra}</body>`);
1228
1269
  }
@@ -1298,3 +1339,152 @@ function renderInspectorPage(view) {
1298
1339
  </body>
1299
1340
  </html>`;
1300
1341
  }
1342
+ // The page actions of 遠端 Agent: post without a reload, then redraw the page body from the answer.
1343
+ // settingsBusy is the name that modelStatusScript reads to hold the status poll while an action runs.
1344
+ const remoteAgentActionsScript = `
1345
+ var settingsBusy = false;
1346
+ document.addEventListener("submit", async function (event) {
1347
+ var form = event.target;
1348
+ if (!form?.matches?.('form[action="/local-tools"], form[action="/local-tool-approval"], form[action="/remote-agent-log-stop"], form[action="/auto-approval-start"], form[action="/auto-approval-stop"]')) return;
1349
+ event.preventDefault();
1350
+ if (settingsBusy) return;
1351
+ settingsBusy = true;
1352
+ var app = document.getElementById("remote-app");
1353
+ app.querySelector(".remote-action-error")?.remove();
1354
+ var body = new URLSearchParams(new FormData(form));
1355
+ if (event.submitter?.name) body.set(event.submitter.name, event.submitter.value);
1356
+ var buttons = app.querySelectorAll("button");
1357
+ buttons.forEach(function (button) { button.disabled = true; });
1358
+ try {
1359
+ var response = await fetch(form.action, { method: "POST", headers: { "X-Remote-Agent": "1" }, body: body });
1360
+ if (!response.ok) throw new Error("remote agent action failed");
1361
+ var next = new DOMParser().parseFromString(await response.text(), "text/html").getElementById("remote-app");
1362
+ if (!next) throw new Error("remote agent page unavailable");
1363
+ keepRemoteAgentLogPlace(function () { app.replaceChildren(...next.childNodes); });
1364
+ form.closest("dialog")?.close();
1365
+ } catch (error) {
1366
+ buttons.forEach(function (button) { button.disabled = false; });
1367
+ var message = document.createElement("p");
1368
+ message.className = "error remote-action-error";
1369
+ message.setAttribute("role", "alert");
1370
+ message.textContent = "操作未完成,請再試一次。";
1371
+ app.prepend(message);
1372
+ } finally {
1373
+ settingsBusy = false;
1374
+ }
1375
+ });
1376
+ `;
1377
+ // Auto Approval on the page: 自動允許 ▾ opens the reminder dialog (not a browser dialog), and the
1378
+ // status bar counts the time left down by itself between polls.
1379
+ const autoApprovalScript = `
1380
+ document.addEventListener("click", function (event) {
1381
+ var item = event.target.closest && event.target.closest("[data-auto-approval]");
1382
+ var dialog = document.querySelector("[data-auto-dialog]");
1383
+ if (item && dialog) {
1384
+ dialog.querySelector("[data-auto-dialog-duration]").value = item.getAttribute("data-auto-approval");
1385
+ dialog.querySelector("[data-auto-dialog-label]").textContent = item.textContent;
1386
+ item.closest("details")?.removeAttribute("open");
1387
+ dialog.showModal();
1388
+ return;
1389
+ }
1390
+ if (event.target.closest && event.target.closest("[data-auto-dialog-cancel]")) dialog?.close();
1391
+ });
1392
+ setInterval(function () {
1393
+ document.querySelectorAll("[data-auto-ends-at]").forEach(function (time) {
1394
+ var seconds = Math.max(0, Math.ceil((Number(time.getAttribute("data-auto-ends-at")) - Date.now()) / 1000));
1395
+ time.textContent = "剩 " + Math.floor(seconds / 60) + ":" + String(seconds % 60).padStart(2, "0");
1396
+ });
1397
+ }, 1000);
1398
+ `;
1399
+ /** The reminder before Auto Approval starts (ADR 0014). It stays outside #remote-app, so the poll does not redraw it. */
1400
+ const AUTO_APPROVAL_DIALOG = `<dialog class="auto-dialog" data-auto-dialog aria-label="打開自動允許"><form method="post" action="/auto-approval-start"><input type="hidden" name="duration" value="10" data-auto-dialog-duration>`
1401
+ + `<h3>${BOLT}打開自動允許(<span data-auto-dialog-label>10 分鐘</span>)</h3>`
1402
+ + `<p>自動允許期間,Agent 可以不經過你,直接執行指令、寫入檔案和儲存圖片。</p>`
1403
+ + `<p class="auto-dialog-warn">如果你的 Agent 會讀網頁或電子郵件,那些內容可能藏著給 Agent 的指令。</p>`
1404
+ + `<p class="hint">下面這兩種情況仍然會問你:</p><ul class="hint"><li>碰到機密檔案,例如 <code>.env</code>、私密金鑰</li><li>刪除專案資料夾以外的檔案</li></ul>`
1405
+ + `<p class="hint">排隊中的確認也會一起允許。你隨時可以按「停止自動允許」。</p>`
1406
+ + `<div class="auto-dialog-actions"><button type="button" data-auto-dialog-cancel>取消</button><button type="submit" class="auto-dialog-ok">確定打開</button></div></form></dialog>`;
1407
+ const REMOTE_AGENT_STYLES = `
1408
+ main.remote-main { width: min(72rem, 100%); }
1409
+ main.remote-main, .auto-dialog { --auto: light-dark(#7c3aed, #a78bfa); --auto-err: light-dark(#c0362c, #ff7b72); }
1410
+ #remote-app { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
1411
+ .remote-main .tile { gap: 10px; padding: 16px 18px; min-width: 0; }
1412
+ .remote-main .hint { color: var(--muted); font-size: 13px; line-height: 1.6; }
1413
+ .remote-status { flex-direction: row; align-items: center; flex-wrap: wrap; gap: 10px 16px; }
1414
+ .remote-status-main { display: flex; align-items: center; gap: 10px; flex: 1 1 20rem; min-width: 0; }
1415
+ .remote-status-main strong { font-size: 14px; overflow-wrap: anywhere; }
1416
+ .remote-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--muted); flex-shrink: 0; }
1417
+ .remote-dot.on { background: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent); }
1418
+ .remote-chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 4px; }
1419
+ .remote-chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; background: var(--input); color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
1420
+ .remote-chip b { color: var(--text); font-weight: 600; }
1421
+ .remote-switch-row { display: flex; flex-direction: row; align-items: center; gap: 10px; font-size: 13px; white-space: nowrap; }
1422
+ /* 頁面的一般 button 樣式會改變開關大小,所以開關有自己的固定尺寸。 */
1423
+ main button.remote-switch { box-sizing: border-box; position: relative; width: 44px; height: 26px; min-width: 44px; min-height: 26px; max-height: 26px; padding: 0; line-height: 0; flex: 0 0 44px; border: 0; border-radius: 999px; background: var(--modes); }
1424
+ .remote-switch::after { content: ""; position: absolute; top: 50%; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transform: translateY(-50%); transition: left 0.15s; }
1425
+ main button.remote-switch[aria-checked="true"] { background: var(--accent); }
1426
+ .remote-switch[aria-checked="true"]::after { left: calc(100% - 23px); }
1427
+ .remote-setup { flex-basis: 100%; margin: 0; padding: 10px 12px; border-radius: 8px; background: var(--input); color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }
1428
+ .local-approval { border: 1px solid color-mix(in srgb, var(--accent) 60%, transparent); box-shadow: inset 4px 0 0 var(--accent); }
1429
+ .local-approval-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
1430
+ .local-approval-title { margin: 0; font-size: 15px; overflow-wrap: anywhere; }
1431
+ .local-approval-queue { margin-left: auto; }
1432
+ .local-approval-body { margin: 0; max-height: 16rem; overflow: auto; padding: 8px 10px; background: var(--input); border-radius: 6px; font-size: 13px; white-space: pre-wrap; overflow-wrap: anywhere; }
1433
+ main button.local-approval-full { align-self: flex-start; min-height: 0; background: none; color: var(--accent); padding: 0; text-decoration: underline; }
1434
+ .local-approval-timer { height: 4px; border-radius: 999px; background: var(--modes); overflow: hidden; }
1435
+ .local-approval-timer span { display: block; height: 100%; background: var(--accent); transition: width 1s linear; }
1436
+ .local-approval-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
1437
+ .local-approval-actions .hint { margin-right: auto; }
1438
+ main button.local-approval-deny { background: transparent; color: var(--text); border-color: var(--line); }
1439
+ .auto-bolt { width: 14px; height: 14px; flex-shrink: 0; }
1440
+ .remote-main .tile.remote-status.is-auto { border-color: color-mix(in srgb, var(--auto) 55%, transparent); box-shadow: inset 4px 0 0 var(--auto); }
1441
+ .remote-dot.auto { background: var(--auto); box-shadow: 0 0 0 4px color-mix(in srgb, var(--auto) 22%, transparent); }
1442
+ .remote-auto-line { flex-basis: 100%; display: flex; align-items: center; flex-wrap: wrap; gap: 6px 10px; padding: 10px 12px; border-radius: 8px; background: color-mix(in srgb, var(--auto) 12%, transparent); color: var(--auto); font-size: 13px; }
1443
+ .remote-auto-line .hint { flex: 1 1 18rem; }
1444
+ .remote-auto-time { font-family: Consolas, "Cascadia Mono", monospace; font-variant-numeric: tabular-nums; font-weight: 700; }
1445
+ main button.remote-auto-stop { min-height: 0; padding: 4px 12px; background: transparent; color: var(--auto); border: 1px solid color-mix(in srgb, var(--auto) 55%, transparent); }
1446
+ .remote-main .tile.local-approval.is-exception { border-color: color-mix(in srgb, var(--auto-err) 55%, transparent); box-shadow: inset 4px 0 0 var(--auto-err); }
1447
+ .local-approval-exception { display: flex; align-items: center; gap: 6px; margin: 0; font-size: 13px; color: var(--auto-err); }
1448
+ .auto-menu { position: relative; }
1449
+ .auto-menu-button { list-style: none; cursor: pointer; padding: 6px 14px; border-radius: 999px; border: 1px solid color-mix(in srgb, var(--auto) 55%, transparent); color: var(--auto); font-size: 13px; }
1450
+ .auto-menu-button::-webkit-details-marker { display: none; }
1451
+ .auto-menu-list { position: absolute; right: 0; bottom: calc(100% + 6px); z-index: 30; display: flex; flex-direction: column; min-width: 10rem; padding: 4px; border-radius: 10px; background: var(--tile, var(--bg)); border: 1px solid var(--line); box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); }
1452
+ main .auto-menu-list button { min-height: 0; text-align: left; padding: 8px 12px; border: 0; border-radius: 6px; background: transparent; color: var(--text); font-size: 13px; }
1453
+ main .auto-menu-list button:hover { background: var(--modes); }
1454
+ .auto-dialog { width: min(30rem, calc(100% - 32px)); padding: 20px 22px; border-radius: 14px; border: 1px solid var(--line); background: var(--tile, var(--bg)); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4); font-size: 14px; line-height: 1.6; }
1455
+ .auto-dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
1456
+ .auto-dialog form { display: flex; flex-direction: column; gap: 8px; }
1457
+ .auto-dialog h3 { display: flex; align-items: center; gap: 6px; margin: 0 0 4px; font-size: 16px; color: var(--auto); }
1458
+ .auto-dialog p, .auto-dialog ul { margin: 0; }
1459
+ .auto-dialog ul { padding-left: 1.2rem; }
1460
+ .auto-dialog .hint { color: var(--muted); font-size: 13px; }
1461
+ .auto-dialog-warn { color: var(--auto-err); }
1462
+ .auto-dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
1463
+ .auto-dialog-actions button { padding: 7px 16px; border-radius: 999px; border: 1px solid var(--line); background: transparent; color: var(--text); font: inherit; font-size: 13px; cursor: pointer; }
1464
+ .auto-dialog-actions .auto-dialog-ok { background: var(--auto); color: #fff; border-color: transparent; font-weight: 600; }
1465
+ ${remoteAgentLogView_1.REMOTE_AGENT_LOG_STYLES}`;
1466
+ /** 遠端 Agent:Local Tool Access 的開關、確認卡片和 Remote Agent Log,與其他頁共用頁殼、側欄、主題與法鬥超人頁首。 */
1467
+ function renderRemoteAgentPage(view) {
1468
+ return `${documentHead()}
1469
+ <body>
1470
+ ${sideMenu("remote-agent")}
1471
+ <main class="remote-main">
1472
+ <header class="page-head pi-head"><div class="pi-head-text"><h2>遠端 Agent</h2><p>看看 Agent 透過 MCP 在這台電腦做了哪些事。</p></div>${connectionMascot_1.connectionMascot}</header>
1473
+ <div id="remote-app">${remoteAgentBody(view)}</div>
1474
+ </main>
1475
+ ${AUTO_APPROVAL_DIALOG}
1476
+ <style>
1477
+ ${SHELL_LAYOUT_STYLES}
1478
+ ${SHELL_CONTROL_STYLES}
1479
+ .pi-head { display: flex; align-items: flex-end; flex-wrap: wrap; gap: 0 16px; }
1480
+ .pi-head-text { flex: 0 1 auto; padding-bottom: 12px; }
1481
+ .pi-head .connection-mascot { flex: 1 1 260px; height: 144px; }
1482
+ ${connectionMascot_1.connectionMascotStyles}
1483
+ ${headPatrol.styles}
1484
+ ${REMOTE_AGENT_STYLES}
1485
+ </style>
1486
+ ${headPatrol.sheet}
1487
+ <script>${drawerScript()}${powerMenuView_1.powerMenuScript}${connectionMascot_1.connectionMascotScript}${headPatrol.script}${remoteAgentActionsScript}${autoApprovalScript}${remoteAgentLogView_1.remoteAgentLogScript}${modelStatus_1.modelStatusScript}${modelStatus_1.approvalFullScript}</script>
1488
+ </body>
1489
+ </html>`;
1490
+ }