agent-loft 0.2.76 → 0.2.78

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.
@@ -0,0 +1,30 @@
1
+ // 权限级别的显示文案和上限换算(级别的含义见服务端 core/permission.ts)
2
+ export const PERMISSION_ORDER = ["ask", "edit", "full"];
3
+
4
+ export const PERMISSIONS = {
5
+ ask: { label: "逐次询问", short: "询问", desc: "改文件、跑命令都先问你" },
6
+ edit: { label: "自动改文件", short: "自动改文件", desc: "工作目录内改文件不再询问,其余照常问" },
7
+ full: { label: "完全允许", short: "完全允许", desc: "不再询问,可以跑任意命令、改任意文件" },
8
+ };
9
+
10
+ /** 专家的权限 → 最高能选到哪一级 */
11
+ export function capOfAccess(access) {
12
+ return !access || access === "exec" ? "full" : access === "edit" ? "edit" : "ask";
13
+ }
14
+
15
+ export function minPermission(a, b) {
16
+ return PERMISSION_ORDER[Math.min(PERMISSION_ORDER.indexOf(a), PERMISSION_ORDER.indexOf(b))];
17
+ }
18
+
19
+ /** 不超过 max 的级别 */
20
+ export function permissionsUpTo(max) {
21
+ return PERMISSION_ORDER.slice(0, PERMISSION_ORDER.indexOf(max) + 1);
22
+ }
23
+
24
+ export const FULL_WARNING = {
25
+ title: "切换为「完全允许」?",
26
+ sub: "agent 的操作不再询问:可以跑任意命令、改写或删除这台机器上的任意文件。",
27
+ message: "只在你信任当前任务时使用。提问和标了「调用前询问」的 MCP 仍会询问。",
28
+ confirmText: "完全允许",
29
+ danger: true,
30
+ };
package/web/lib/ws.js CHANGED
@@ -5,7 +5,7 @@ import { showAuth } from "../views/auth.js";
5
5
  import { applyBootstrap, bootstrapQuery, renderSidebar, scheduleSidebar } from "../views/sidebar.js";
6
6
  import { scheduleQuota } from "../views/quota.js";
7
7
  import { renderTopbar } from "../views/topbar.js";
8
- import { applyEvent, openConversation } from "../views/messages.js";
8
+ import { applyEvent, openConversation, renderIdleNotice } from "../views/messages.js";
9
9
  import { upsertInteraction } from "../views/interaction.js";
10
10
  import { setBusy } from "../views/composer.js";
11
11
  import { SH, shownHere } from "./shell.js";
@@ -265,6 +265,7 @@ function onWsMessage(msg) {
265
265
  if (S.convs.get(msg.info.key)?.busy && !msg.info.busy) scheduleQuota();
266
266
  S.convs.set(msg.info.key, msg.info);
267
267
  if (msg.info.key === S.current) {
268
+ renderIdleNotice(msg.info);
268
269
  renderTopbar();
269
270
  setBusy(msg.info.busy);
270
271
  renderScheduled();
@@ -75,7 +75,7 @@ export function modelPicker(agent, model, effort, defaultLabel) {
75
75
  }
76
76
 
77
77
  /** agent-loft 内置命令名(和服务端 BUILTIN_COMMANDS 一致),自定义命令不能用 */
78
- const BUILTIN_NAMES = ["new", "status", "stop", "model", "effort", "sessions", "resume", "fork", "agent", "claude", "codex", "cd", "pwd", "autoedit", "web", "detach", "approve", "help"];
78
+ const BUILTIN_NAMES = ["new", "status", "stop", "model", "effort", "sessions", "resume", "fork", "agent", "claude", "codex", "cd", "pwd", "permission", "autoedit", "web", "detach", "approve", "help"];
79
79
 
80
80
  /**
81
81
  * 添加 / 编辑一条自定义命令。onSave 返回 true 才关窗;失败(比如服务端校验没过)留着让用户改。
@@ -81,6 +81,7 @@ export function renderComposer() {
81
81
  : null;
82
82
  ui.ctxBar = h("div", { class: "ctx-bar" });
83
83
  ui.modelSlot = h("span", { class: "model-slot" });
84
+ ui.permSlot = h("span", { class: "perm-slot" });
84
85
  ui.ctxRing = h("span", { class: "ctx-ring-slot" });
85
86
  ui.chips = h("div", { class: "chips" });
86
87
  ui.schedBar = h("div", { class: "sched-bar", hidden: true });
@@ -97,6 +98,7 @@ export function renderComposer() {
97
98
  { class: "composer-row" },
98
99
  h("button", { class: "icon-btn add-btn", title: "上传文件", onclick: () => ui.fileInput.click() }, icon("plus")),
99
100
  ui.fileInput,
101
+ ui.permSlot,
100
102
  h("span", { class: "spacer" }),
101
103
  ui.ctxRing,
102
104
  ui.modelSlot,
@@ -223,14 +225,17 @@ export async function ensureConversation() {
223
225
  }
224
226
  S.draftCwdPicked = false;
225
227
  // 新对话里选过模型 / 思考程度:建好会话后先切过去再发消息
226
- const pick = { ...draftPick() };
227
- for (const [action, value] of [["model", pick.model], ["effort", pick.effort]]) {
228
+ // 权限只对这一条新对话有效,不留给下一条(不放进按专家记住的 draftPick)
229
+ const pick = { ...draftPick(), permission: S.draftPermission };
230
+ S.draftPermission = null;
231
+ const FAILED = { model: "切换模型", effort: "切换思考程度", permission: "设置权限" };
232
+ for (const [action, value] of [["model", pick.model], ["effort", pick.effort], ["permission", pick.permission]]) {
228
233
  if (!value) continue;
229
234
  try {
230
235
  const res = await api(`/api/conversations/${encodeURIComponent(info.key)}/action`, { method: "POST", body: { action, value } });
231
236
  info = res.info;
232
237
  } catch (err) {
233
- toast(`${action === "model" ? "切换模型" : "切换思考程度"}失败:${err.message}`);
238
+ toast(`${FAILED[action]}失败:${err.message}`);
234
239
  }
235
240
  }
236
241
  S.convs.set(info.key, info);
@@ -8,6 +8,7 @@ import { closeMenu, confirmDialog, modal, placePopover, showPopover } from "../l
8
8
  import { newDraft, openConversation, renderEmpty } from "./messages.js";
9
9
  import { setDraftExpert } from "./experts.js";
10
10
  import { preloadCommands } from "./slash.js";
11
+ import { FULL_WARNING, PERMISSIONS, permissionsUpTo } from "../lib/permission.js";
11
12
 
12
13
  // ---------- 会话操作 ----------
13
14
 
@@ -144,6 +145,38 @@ export async function openModelPicker(anchor = ui.modelSlot) {
144
145
  render();
145
146
  }
146
147
 
148
+ /** 权限级别:已有对话直接切,新对话先记下,建好对话后带过去。max、current 由输入框上的按钮算好传进来 */
149
+ export function openPermissionPicker(anchor, { current, max }) {
150
+ if (ui.menu?.dataset.for === "permission") return closeMenu();
151
+ const pop = showPopover(anchor, { align: "left" });
152
+ pop.dataset.for = "permission";
153
+ pop.classList.add("perm-pop");
154
+ const choose = async (level) => {
155
+ closeMenu();
156
+ if (level === current) return;
157
+ if (level === "full" && !(await confirmDialog(FULL_WARNING))) return;
158
+ if (S.current) {
159
+ await doAction("permission", level);
160
+ } else {
161
+ S.draftPermission = level;
162
+ renderContext();
163
+ }
164
+ };
165
+ fill(
166
+ pop,
167
+ h("div", { class: "pop-head" }, "权限"),
168
+ permissionsUpTo(max).map((level) =>
169
+ h(
170
+ "button",
171
+ { class: `pop-item${level === current ? " on" : ""}`, onclick: () => void choose(level) },
172
+ h("span", { class: "grow" }, h("span", { class: "t" }, PERMISSIONS[level].label), h("span", { class: "s" }, PERMISSIONS[level].desc)),
173
+ level === current ? h("span", { class: "check" }, "✓") : null,
174
+ ),
175
+ ),
176
+ );
177
+ placePopover(pop, anchor);
178
+ }
179
+
147
180
  export async function openSessions(all) {
148
181
  const body = h("div", { class: "modal-body" }, h("span", { class: "spinner" }));
149
182
  const close = modal({ title: "历史会话", body });
@@ -18,6 +18,42 @@ import { preloadCommands } from "./slash.js";
18
18
 
19
19
  // ---------- 打开会话 ----------
20
20
 
21
+ let historyLoadedKey = null;
22
+
23
+ /** 闲置提示只属于当前页面,不写进 Agent 历史;其他页面续期或发消息时同步撤下。 */
24
+ export function renderIdleNotice(info = S.convs.get(S.current)) {
25
+ if (!info || info.key !== S.current || historyLoadedKey !== S.current) return;
26
+ const existing = ui.msgInner.querySelector(".idle-session-notice");
27
+ if (!info.idleExpired) {
28
+ existing?.remove();
29
+ return;
30
+ }
31
+ if (existing) return;
32
+ const button = h("button", {
33
+ type: "button",
34
+ class: "idle-session-continue",
35
+ onclick: async () => {
36
+ button.disabled = true;
37
+ try {
38
+ const next = await api(`/api/conversations/${encodeURIComponent(info.key)}/continue`, {
39
+ method: "POST", body: { agent: info.agent, sessionId: info.sessionId },
40
+ });
41
+ S.convs.set(info.key, next);
42
+ renderIdleNotice(next);
43
+ renderSidebar();
44
+ toast("已继续当前会话");
45
+ } catch (err) {
46
+ toast(err.message);
47
+ } finally {
48
+ button.disabled = false;
49
+ }
50
+ },
51
+ }, "继续当前会话");
52
+ ui.msgInner.append(h("div", { class: "notice idle-session-notice" },
53
+ "当前会话闲置太久,直接发送消息将开启新会话,也可以", button,
54
+ ));
55
+ }
56
+
21
57
  /** 从定时任务页、远程控制回到对话:把输入框放回来 */
22
58
  function leaveTasksView() {
23
59
  if (!S.view) return;
@@ -32,6 +68,7 @@ export function latestConversation() {
32
68
  }
33
69
 
34
70
  export function newDraft({ inheritLatest = false } = {}) {
71
+ historyLoadedKey = null;
35
72
  if (inheritLatest) {
36
73
  const latest = latestConversation();
37
74
  if (latest) {
@@ -120,6 +157,7 @@ document.addEventListener("aim:experts", () => {
120
157
  });
121
158
 
122
159
  export async function openConversation(key) {
160
+ historyLoadedKey = null;
123
161
  leaveTasksView();
124
162
  const token = ++S.loadToken;
125
163
  if (S.current && S.current !== key) WS.raw({ t: "unsub", key: S.current });
@@ -208,6 +246,8 @@ export async function openConversation(key) {
208
246
  );
209
247
  }
210
248
  setBusy(data.info.busy);
249
+ historyLoadedKey = key;
250
+ renderIdleNotice(data.info);
211
251
  renderTopbar();
212
252
  WS.subscribe(key, S.seq.get(key));
213
253
  renderScheduled();
@@ -751,6 +791,7 @@ export function applyEvent(env) {
751
791
  }
752
792
  case "user": {
753
793
  // 排队的消息:正在进行的那一轮照常在上面接着输出,这条等轮到它(queue 事件)再挂占位
794
+ ui.msgInner.querySelector(".idle-session-notice")?.remove();
754
795
  const queued = !!ev.queued;
755
796
  if (!queued && S.turnAi && !S.turnAi.done && S.turnAi.steps.length === 0 && !S.turnAi.committed) {
756
797
  // 上一个占位还空着(比如消息被当成了作答),不再多留一个
@@ -2,13 +2,14 @@ import { fill, h, icon, starsLoader, toast } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { clockTime } from "../lib/time.js";
4
4
  import { confirmDialog, modal, showMenu } from "../lib/overlay.js";
5
- import { S, ui } from "../state.js";
5
+ import { isAdmin, S, ui } from "../state.js";
6
6
  import { WS } from "../lib/ws.js";
7
7
  import { agentLabel, closeSide, renderSidebar } from "./sidebar.js";
8
8
  import { baseName, renderTopbar } from "./topbar.js";
9
9
  import { openConversation } from "./messages.js";
10
10
  import { changeCwd } from "./conv-actions.js";
11
11
  import { expertChoices, findExpert } from "./experts.js";
12
+ import { capOfAccess, minPermission, PERMISSIONS, permissionsUpTo } from "../lib/permission.js";
12
13
 
13
14
  // ---------- 定时任务页 ----------
14
15
  // 到点让 agent 按提示词干活,结果回到一条对话里;「有需要才提醒」时由 agent 判断这次要不要打扰你。
@@ -237,6 +238,8 @@ function taskCard(t) {
237
238
  h("span", {}, state),
238
239
  h("span", {}, t.notify === "smart" ? "有需要才提醒" : "每次都提醒"),
239
240
  h("span", {}, where),
241
+ t.target.kind === "dedicated" && t.permission ? h("span", { class: t.permission === "full" ? "task-perm-full" : "" }, PERMISSIONS[t.permission].label) : null,
242
+ t.target.kind === "conv" && t.convPermission === "full" ? h("span", { class: "task-perm-full" }, "跟随对话:完全允许") : null,
240
243
  ),
241
244
  h("div", { class: "task-prompt" }, t.prompt),
242
245
  last ? runLine(last) : h("div", { class: "task-run muted" }, "还没有运行过"),
@@ -394,13 +397,43 @@ export function openTaskForm(task, preset) {
394
397
  showCwd();
395
398
  },
396
399
  });
397
- const cwdField = field("工作目录", cwdBtn, "专家在这个目录里干活。需要授权时会推送给你,没人处理就按拒绝。没选就用专家默认的目录。");
398
- const dedicatedBox = h("div", { class: "task-dedicated" }, field("专家", expert, "到点由这个专家来做。要联网、读文件还是跑命令,看专家的权限(设置 → 专家)。"), cwdField);
400
+ const cwdField = field("工作目录", cwdBtn, "专家在这个目录里干活。没选就用专家默认的目录。");
401
+ // ---- 权限(只有专用对话有:发到已有对话的跟着那条对话)----
402
+ const permission = h("select", {});
403
+ const permissionHint = h("small", {});
404
+ const permissionField = h("div", { class: "field" }, h("span", {}, "权限"), permission, permissionHint);
405
+ let permissionValue = src.permission ?? "";
406
+ const syncPermission = () => {
407
+ const max = minPermission(isAdmin() ? "full" : "edit", capOfAccess(findExpert(expert.value)?.access));
408
+ const levels = permissionsUpTo(max);
409
+ // 已经存着的级别高过现在的上限(被降级了、换了专家):不留着,免得显示的和实际跑的不一样
410
+ if (permissionValue && !levels.includes(permissionValue)) permissionValue = "";
411
+ fill(
412
+ permission,
413
+ h("option", { value: "", selected: !permissionValue }, "默认(Claude 逐次询问,Codex 按沙箱设置)"),
414
+ levels.map((l) => h("option", { value: l, selected: l === permissionValue }, `${PERMISSIONS[l].label}:${PERMISSIONS[l].desc}`)),
415
+ );
416
+ permissionHint.textContent =
417
+ permission.value === "full" ? "⚠️ 到点无人值守地执行,不会询问任何操作。提示词要写清楚,只给信得过的任务用。" : "需要授权时会推送给你,没人处理就按拒绝。";
418
+ permissionHint.classList.toggle("task-perm-full", permission.value === "full");
419
+ };
420
+ permission.onchange = () => {
421
+ permissionValue = permission.value;
422
+ syncPermission();
423
+ };
424
+ const dedicatedBox = h("div", { class: "task-dedicated" }, field("专家", expert, "到点由这个专家来做。要联网、读文件还是跑命令,看专家的权限(设置 → 专家)。"), cwdField, permissionField);
425
+ const followField = convTarget
426
+ ? field("权限", h("div", { class: "task-static" }, `跟随这条对话(现在是「${task.convPermission ? PERMISSIONS[task.convPermission].label : "默认"}」)`), "要单独设权限,把结果改发到专用对话。")
427
+ : null;
399
428
  const syncVisibility = () => {
400
429
  const dedicated = !target || target.value === "dedicated";
401
430
  dedicatedBox.hidden = !dedicated;
431
+ if (followField) followField.hidden = dedicated;
402
432
  // 只能联网的专家不碰目录
403
- cwdField.hidden = findExpert(expert.value)?.access === "web";
433
+ const access = findExpert(expert.value)?.access;
434
+ cwdField.hidden = access === "web";
435
+ syncPermission();
436
+ permissionField.hidden = capOfAccess(access) === "ask";
404
437
  };
405
438
  expert.onchange = syncVisibility;
406
439
  if (target) target.onchange = syncVisibility;
@@ -415,6 +448,7 @@ export function openTaskForm(task, preset) {
415
448
  field("执行时间", freqBox, freqHint),
416
449
  field("提醒", notify),
417
450
  target ? field("结果发到", target) : field("结果发到", h("div", { class: "task-static" }, task?.convKey ? `专用对话「${task.convTitle || "对话"}」` : "专用对话(第一次运行时新建,之后都发到这条)")),
451
+ followField,
418
452
  dedicatedBox,
419
453
  );
420
454
  const save = h("button", { class: "btn primary" }, task ? "保存" : "创建");
@@ -439,13 +473,21 @@ export function openTaskForm(task, preset) {
439
473
  if (schedule.kind === "weekly" && !schedule.days.length) return toast("至少选择一个执行星期");
440
474
  const dedicated = !target || target.value === "dedicated";
441
475
  if (dedicated && !expert.value) return toast("选一个专家");
476
+ const level = dedicated && !permissionField.hidden ? permission.value : "";
477
+ if (
478
+ level === "full" &&
479
+ task?.permission !== "full" &&
480
+ !(await confirmDialog({ title: "让这个任务以「完全允许」运行?", sub: "到点无人值守地执行,不会询问任何操作:可以跑任意命令、改写或删除这台机器上的任意文件。", message: "只给信得过的任务用,并把提示词写清楚。", confirmText: "完全允许", danger: true }))
481
+ ) {
482
+ return;
483
+ }
442
484
  const payload = {
443
485
  title: title.value,
444
486
  prompt: prompt.value,
445
487
  schedule,
446
488
  notify: notify.value,
447
489
  target: dedicated ? { kind: "dedicated" } : { kind: "conv", key: task.target.key },
448
- ...(dedicated ? { expert: expert.value, cwd: findExpert(expert.value)?.access === "web" ? "" : cwd } : {}),
490
+ ...(dedicated ? { expert: expert.value, cwd: findExpert(expert.value)?.access === "web" ? "" : cwd, permission: level } : {}),
449
491
  };
450
492
  save.disabled = true;
451
493
  try {
@@ -1,13 +1,14 @@
1
1
  import { fill, h, icon, ICON, toast } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { relTime } from "../lib/time.js";
4
- import { desktop, desktopMenu, S, ui } from "../state.js";
4
+ import { desktop, desktopMenu, isAdmin, S, ui } from "../state.js";
5
5
  import { agentLabel, convTitle, openSide } from "./sidebar.js";
6
6
  import { convExpert, expertChoices, expertIcon, findExpert } from "./experts.js";
7
7
  import { closeMenu, modal, placePopover, showMenu, showPopover } from "../lib/overlay.js";
8
8
  import { openConversation } from "./messages.js";
9
9
  import { openSchedule } from "./schedule.js";
10
- import { changeCwd, deleteConv, doAction, forkConversation, openModelPicker, renameConv, switchAgent } from "./conv-actions.js";
10
+ import { changeCwd, deleteConv, doAction, forkConversation, openModelPicker, openPermissionPicker, renameConv, switchAgent } from "./conv-actions.js";
11
+ import { capOfAccess, minPermission, PERMISSION_ORDER, PERMISSIONS } from "../lib/permission.js";
11
12
 
12
13
  // ---------- 顶栏 ----------
13
14
 
@@ -419,6 +420,7 @@ export function renderContext() {
419
420
  effort = (c ? (c.runtimeEffort ?? c.effort) : (pick.effort ?? (draft?.effort || undefined))) ?? defaults.effort;
420
421
  }
421
422
  renderCtxRing();
423
+ renderPermission(c, expert);
422
424
  fill(
423
425
  ui.modelSlot,
424
426
  model
@@ -433,6 +435,31 @@ export function renderContext() {
433
435
  );
434
436
  }
435
437
 
438
+ /** 输入框里的权限按钮。只有一级可选(只能联网、只能读文件的专家)时不显示 */
439
+ function renderPermission(c, expert) {
440
+ if (!ui.permSlot) return;
441
+ const max = c ? c.permissionMax : minPermission(isAdmin() ? "full" : "edit", capOfAccess(expert?.access));
442
+ // 新对话选好后换了个权限低的专家:选的不再算数
443
+ if (!c && S.draftPermission && PERMISSION_ORDER.indexOf(S.draftPermission) > PERMISSION_ORDER.indexOf(max)) S.draftPermission = null;
444
+ const current = c ? c.permission : S.draftPermission;
445
+ if (max === "ask" && (!current || current === "ask")) return fill(ui.permSlot);
446
+ const locked = !!c?.taskId;
447
+ fill(
448
+ ui.permSlot,
449
+ h(
450
+ "button",
451
+ {
452
+ class: `perm-pill${current === "full" ? " danger" : ""}`,
453
+ title: locked ? "定时任务的专用对话:权限在任务设置里改" : `权限:${current ? PERMISSIONS[current].label : "默认"},点击切换`,
454
+ onclick: (e) => (locked ? toast("这是定时任务的专用对话,权限在任务设置里改") : openPermissionPicker(e.currentTarget, { current, max })),
455
+ },
456
+ icon("shield"),
457
+ h("span", { class: "perm-name" }, current ? PERMISSIONS[current].short : "默认权限"),
458
+ locked ? null : icon("down"),
459
+ ),
460
+ );
461
+ }
462
+
436
463
  /** 84321 → 84k,1234567 → 1.2M */
437
464
  function fmtTokens(n) {
438
465
  if (n >= 1e6) return `${(n / 1e6).toFixed(n >= 1e7 ? 0 : 1)}M`;