agent-loft 0.2.97 → 0.2.99
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 +6 -6
- package/package.json +1 -1
- package/src/agents/claude/session.ts +57 -5
- package/src/agents/codex/app-server.ts +2 -1
- package/src/agents/codex/protocol.ts +2 -1
- package/src/agents/codex/session.ts +65 -1
- package/src/channels/feishu/cards.ts +9 -4
- package/src/channels/web/api.ts +8 -0
- package/src/channels/web/experts-api.ts +32 -5
- package/src/channels/web/skills-api.ts +42 -27
- package/src/channels/weixin/bot.ts +2 -1
- package/src/channels/weixin/format.ts +5 -5
- package/src/core/builtin-agents.ts +49 -8
- package/src/core/command-catalog.ts +2 -1
- package/src/core/commands.ts +11 -0
- package/src/core/experts.ts +18 -9
- package/src/core/hub.ts +62 -12
- package/src/core/interactions.ts +1 -1
- package/src/core/mcp.ts +121 -18
- package/src/core/permission.ts +20 -2
- package/src/core/skill-runtime.ts +5 -4
- package/src/core/skills-bridge.ts +95 -24
- package/src/core/skills.ts +184 -37
- package/src/core/store.ts +5 -0
- package/src/core/types.ts +14 -0
- package/src/mcp/tasks-server.ts +1 -1
- package/web/css/composer.css +15 -0
- package/web/lib/permission.js +23 -4
- package/web/settings/agents.js +15 -5
- package/web/settings/commands.js +1 -1
- package/web/settings/experts.js +63 -10
- package/web/settings/skills.js +38 -13
- package/web/state.js +4 -0
- package/web/views/composer.js +4 -3
- package/web/views/conv-actions.js +24 -4
- package/web/views/interaction.js +9 -5
- package/web/views/sidebar.js +1 -0
- package/web/views/tasks.js +2 -2
- package/web/views/topbar.js +15 -10
package/src/core/skills.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// skill 是同一种格式,只是由 agent-loft 按人管理:
|
|
3
3
|
// - 谁都能建(网页上写、上传技能包,或者让 agent 在对话里生成),建的人是作者;
|
|
4
4
|
// - 作者决定谁看得到:只有自己 / 指定的几个人 / 所有人,看得到的人可在网页「共享技能」中安装;
|
|
5
|
-
// -
|
|
5
|
+
// - 安装的是作者那一份本身,不是副本;但作者改了内容(rev 加一),装了的人要确认过新版才会加载;
|
|
6
6
|
// - 技能可以声明要用的环境变量(API 地址、密钥之类),值由每个用户自己填,只注入到他自己的会话里。
|
|
7
7
|
//
|
|
8
8
|
// 存储(data/skills/):
|
|
@@ -18,6 +18,7 @@ import fs from "node:fs";
|
|
|
18
18
|
import path from "node:path";
|
|
19
19
|
import { EventEmitter } from "node:events";
|
|
20
20
|
import { dataDir, readJson, writeJsonAtomic } from "../home.js";
|
|
21
|
+
import { getUser } from "./users.js";
|
|
21
22
|
|
|
22
23
|
export type SkillVisibility = "private" | "users" | "all";
|
|
23
24
|
|
|
@@ -47,6 +48,8 @@ export type Skill = {
|
|
|
47
48
|
sharedWith: string[];
|
|
48
49
|
vars: SkillVar[];
|
|
49
50
|
source: "manual" | "upload" | "agent";
|
|
51
|
+
/** 内容版本:SKILL.md、附带文件、变量声明真的变了才加一。装了的人要确认过这一版才会加载 */
|
|
52
|
+
rev?: number;
|
|
50
53
|
createdAt: number;
|
|
51
54
|
updatedAt: number;
|
|
52
55
|
};
|
|
@@ -59,10 +62,18 @@ export type UserSkillPrefs = {
|
|
|
59
62
|
/** 变量的值:按变量名存,几个技能用同一个变量名(比如 GITHUB_TOKEN)就共用一个值 */
|
|
60
63
|
vars: Record<string, string>;
|
|
61
64
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
65
|
+
* 受保护的密钥,用户确认过可以发往哪些地址:按「变量名 → 技能 id → 地址」分开记,
|
|
66
|
+
* 同意 A 技能发往 a.com 不等于同意 B 技能发往 b.com。实际放行时还要和技能现在声明的取交集,
|
|
67
|
+
* 作者删掉的地址、卸掉的技能自然失效;作者新加的地址要用户再确认一次。
|
|
64
68
|
*/
|
|
65
|
-
approved: Record<string, string[]
|
|
69
|
+
approved: Record<string, Record<string, string[]>>;
|
|
70
|
+
/**
|
|
71
|
+
* 按受保护密钥填的值:一旦受保护就一直受保护(声明它受保护的技能停用、卸载、改掉了也一样),
|
|
72
|
+
* 免得别的技能声明同名的普通变量就能拿到明文。用户重新填值或清掉时才解除。
|
|
73
|
+
*/
|
|
74
|
+
guarded: string[];
|
|
75
|
+
/** 装了的技能,用户确认过的内容版本(见 Skill.rev)。作者改了内容,确认前不加载 */
|
|
76
|
+
accepted: Record<string, number>;
|
|
66
77
|
};
|
|
67
78
|
|
|
68
79
|
export const SKILL_NAME = /^[a-z0-9][a-z0-9-]{0,47}$/;
|
|
@@ -129,11 +140,33 @@ export function userPrefs(userId: string): UserSkillPrefs {
|
|
|
129
140
|
if (raw.vars && typeof raw.vars === "object") {
|
|
130
141
|
for (const [k, v] of Object.entries(raw.vars)) if (VAR_NAME.test(k) && typeof v === "string") vars[k] = v;
|
|
131
142
|
}
|
|
132
|
-
const
|
|
143
|
+
const installed = strings(raw.installed);
|
|
144
|
+
const approved: Record<string, Record<string, string[]>> = {};
|
|
145
|
+
const guarded = new Set(strings(raw.guarded));
|
|
133
146
|
if (raw.approved && typeof raw.approved === "object") {
|
|
134
|
-
for (const [k, v] of Object.entries(raw.approved
|
|
147
|
+
for (const [k, v] of Object.entries(raw.approved as Record<string, unknown>)) {
|
|
148
|
+
if (!VAR_NAME.test(k)) continue;
|
|
149
|
+
const bySkill: Record<string, string[]> = {};
|
|
150
|
+
if (Array.isArray(v)) {
|
|
151
|
+
// 旧格式:一个变量一份地址(当时是几个技能声明的合起来)。拆给现在声明了它的每个技能,只留各自声明过的
|
|
152
|
+
for (const s of load()) {
|
|
153
|
+
if (s.owner !== userId && !installed.includes(s.id)) continue;
|
|
154
|
+
const declared = declaredHosts([s], k);
|
|
155
|
+
const keep = strings(v).filter((h) => declared.includes(h));
|
|
156
|
+
if (keep.length) bySkill[s.id] = keep;
|
|
157
|
+
}
|
|
158
|
+
} else if (v && typeof v === "object") {
|
|
159
|
+
for (const [id, hosts] of Object.entries(v as Record<string, unknown>)) bySkill[id] = strings(hosts);
|
|
160
|
+
}
|
|
161
|
+
approved[k] = bySkill;
|
|
162
|
+
if (vars[k] !== undefined) guarded.add(k);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const accepted: Record<string, number> = {};
|
|
166
|
+
if (raw.accepted && typeof raw.accepted === "object") {
|
|
167
|
+
for (const [k, v] of Object.entries(raw.accepted)) if (typeof v === "number") accepted[k] = v;
|
|
135
168
|
}
|
|
136
|
-
return { installed
|
|
169
|
+
return { installed, disabled: strings(raw.disabled), vars, approved, guarded: [...guarded].filter((k) => vars[k] !== undefined), accepted };
|
|
137
170
|
}
|
|
138
171
|
|
|
139
172
|
function savePrefs(userId: string, prefs: UserSkillPrefs): void {
|
|
@@ -163,18 +196,35 @@ export function canSee(viewer: Pick<Viewer, "id">, skill: Skill): boolean {
|
|
|
163
196
|
return skill.owner === viewer.id || skill.visibility === "all" || (skill.visibility === "users" && skill.sharedWith.includes(viewer.id));
|
|
164
197
|
}
|
|
165
198
|
|
|
199
|
+
/**
|
|
200
|
+
* 能在网页上查看:看得到的,加上管理员看得到所有分享出来的(包括没指名给他的),好审查、下架。
|
|
201
|
+
* 只是能看能下架,安装、使用还是按 canSee。
|
|
202
|
+
*/
|
|
203
|
+
export function canView(viewer: Viewer, skill: Skill): boolean {
|
|
204
|
+
return canSee(viewer, skill) || (viewer.role === "admin" && skill.visibility !== "private");
|
|
205
|
+
}
|
|
206
|
+
|
|
166
207
|
/** 能改、能删:只有作者。管理员能下架(删)别人公开或分享出来的,但不能改内容 */
|
|
167
208
|
export function canDelete(viewer: Viewer, skill: Skill): boolean {
|
|
168
209
|
return skill.owner === viewer.id || (viewer.role === "admin" && skill.visibility !== "private");
|
|
169
210
|
}
|
|
170
211
|
|
|
171
|
-
|
|
212
|
+
export function skillRev(skill: Skill): number {
|
|
213
|
+
return skill.rev ?? 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** 装了的别人的技能,作者改过内容、这个人还没确认新的这一版 */
|
|
217
|
+
export function updatePending(userId: string, skill: Skill, prefs: UserSkillPrefs = userPrefs(userId)): boolean {
|
|
218
|
+
return skill.owner !== userId && prefs.installed.includes(skill.id) && (prefs.accepted[skill.id] ?? 0) < skillRev(skill);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/** 这个人此刻能用的技能:自己的和装了的,看得到、没停用、没有待确认的更新。按名字排 */
|
|
172
222
|
export function activeSkills(userId: string): Skill[] {
|
|
173
223
|
const prefs = userPrefs(userId);
|
|
174
224
|
const installed = new Set(prefs.installed);
|
|
175
225
|
const disabled = new Set(prefs.disabled);
|
|
176
226
|
return load()
|
|
177
|
-
.filter((s) => (s.owner === userId || (installed.has(s.id) && canSee({ id: userId }, s))) && !disabled.has(s.id))
|
|
227
|
+
.filter((s) => (s.owner === userId || (installed.has(s.id) && canSee({ id: userId }, s) && !updatePending(userId, s, prefs))) && !disabled.has(s.id))
|
|
178
228
|
.sort((a, b) => a.name.localeCompare(b.name));
|
|
179
229
|
}
|
|
180
230
|
|
|
@@ -192,8 +242,10 @@ function installersOf(id: string): string[] {
|
|
|
192
242
|
.filter((u) => userPrefs(u).installed.includes(id));
|
|
193
243
|
}
|
|
194
244
|
|
|
245
|
+
/** 装着、而且还看得到的人数(撤回分享后留在设置里的不算) */
|
|
195
246
|
export function installCount(id: string): number {
|
|
196
|
-
|
|
247
|
+
const skill = getSkill(id);
|
|
248
|
+
return skill ? installersOf(id).filter((u) => canSee({ id: u }, skill)).length : 0;
|
|
197
249
|
}
|
|
198
250
|
|
|
199
251
|
function affected(skill: Skill): string[] {
|
|
@@ -322,7 +374,8 @@ export function hostAllowed(allowed: string[], url: URL): boolean {
|
|
|
322
374
|
|
|
323
375
|
function cleanVisibility(raw: unknown, sharedWith: unknown, owner: string): { visibility: SkillVisibility; sharedWith: string[] } {
|
|
324
376
|
const visibility = (["private", "users", "all"] as const).find((v) => v === raw) ?? "private";
|
|
325
|
-
|
|
377
|
+
// 只认还在的账号:乱写的 id、删掉的人不留在名单里
|
|
378
|
+
const list = Array.isArray(sharedWith) ? [...new Set(sharedWith.filter((u): u is string => typeof u === "string" && u !== owner && !!getUser(u)))] : [];
|
|
326
379
|
if (visibility === "users" && list.length === 0) throw new Error("选「指定用户」时至少勾一个人");
|
|
327
380
|
return { visibility, sharedWith: visibility === "users" ? list : [] };
|
|
328
381
|
}
|
|
@@ -480,7 +533,7 @@ export function createSkill(
|
|
|
480
533
|
): Skill {
|
|
481
534
|
if (load().filter((s) => s.owner === owner).length >= SKILL_LIMITS.perUser) throw new Error(`每人最多建 ${SKILL_LIMITS.perUser} 个技能`);
|
|
482
535
|
const name = cleanName(input.name ?? upload?.md.name);
|
|
483
|
-
if (findSkillByName(name)) throw new Error(
|
|
536
|
+
if (findSkillByName(name)) throw new Error(`技能名 ${name} 已被占用,换个名字`);
|
|
484
537
|
const description = cleanDescription(input.description ?? upload?.md.description);
|
|
485
538
|
const body = String(input.body ?? upload?.md.body ?? "").trim();
|
|
486
539
|
if (!body) throw new Error("技能的正文(告诉 agent 怎么做)不能为空");
|
|
@@ -531,6 +584,7 @@ export function updateSkill(
|
|
|
531
584
|
const vars = input.vars !== undefined ? cleanVars(input.vars) : upload?.vars;
|
|
532
585
|
const visibility = input.visibility !== undefined ? cleanVisibility(input.visibility, input.sharedWith ?? skill.sharedWith, skill.owner) : undefined;
|
|
533
586
|
const contentChanged = input.description !== undefined || input.body !== undefined || !!upload || !!opts.files;
|
|
587
|
+
let revise = !!vars && JSON.stringify(vars) !== JSON.stringify(skill.vars);
|
|
534
588
|
if (contentChanged) {
|
|
535
589
|
let files = upload ? upload.files : readFiles(id);
|
|
536
590
|
if (opts.files) {
|
|
@@ -547,30 +601,53 @@ export function updateSkill(
|
|
|
547
601
|
}
|
|
548
602
|
const md = composeSkillMd(skill.name, description, body, upload ? upload.md.extra : current.extra);
|
|
549
603
|
checkSizes(md, files);
|
|
550
|
-
|
|
604
|
+
const old = readFiles(id);
|
|
605
|
+
const same = md === readSkillMd(id) && old.size === files.size && [...files].every(([p, d]) => old.get(p)?.equals(d));
|
|
606
|
+
if (!same) {
|
|
607
|
+
replaceFiles(id, md, files);
|
|
608
|
+
revise = true;
|
|
609
|
+
}
|
|
551
610
|
}
|
|
552
611
|
skill.description = description;
|
|
553
612
|
if (vars) skill.vars = vars;
|
|
613
|
+
// 内容真的变了:装了的人确认新版之前不加载(作者自己不受影响)
|
|
614
|
+
if (revise) skill.rev = skillRev(skill) + 1;
|
|
554
615
|
if (visibility) Object.assign(skill, visibility);
|
|
555
616
|
skill.updatedAt = Date.now();
|
|
556
617
|
saveIndex();
|
|
618
|
+
markGuarded(actor, skill);
|
|
557
619
|
// 改之前装着的人也要通知:可能刚被取消了分享
|
|
558
620
|
skillEvents.emit("changed", [...new Set([...before, ...affected(skill)])]);
|
|
559
621
|
return skill;
|
|
560
622
|
}
|
|
561
623
|
|
|
624
|
+
/** 这个技能声明成受保护、而且用户填了值的变量记成「一直受保护」(技能之后停用、卸掉、改掉也不解除) */
|
|
625
|
+
function markGuarded(userId: string, skill: Skill): void {
|
|
626
|
+
const prefs = userPrefs(userId);
|
|
627
|
+
const add = skill.vars.filter((v) => isProtected(v) && prefs.vars[v.name] !== undefined && !prefs.guarded.includes(v.name)).map((v) => v.name);
|
|
628
|
+
if (add.length) savePrefs(userId, { ...prefs, guarded: [...prefs.guarded, ...add] });
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
/** 设置里和这个技能有关的都去掉:装没装、停没停用、确认过的版本、确认过的地址 */
|
|
632
|
+
function forgetSkill(prefs: UserSkillPrefs, id: string): UserSkillPrefs {
|
|
633
|
+
const approved: UserSkillPrefs["approved"] = {};
|
|
634
|
+
for (const [k, bySkill] of Object.entries(prefs.approved)) {
|
|
635
|
+
const { [id]: _gone, ...rest } = bySkill;
|
|
636
|
+
approved[k] = rest;
|
|
637
|
+
}
|
|
638
|
+
const { [id]: _a, ...accepted } = prefs.accepted;
|
|
639
|
+
return { ...prefs, installed: prefs.installed.filter((x) => x !== id), disabled: prefs.disabled.filter((x) => x !== id), approved, accepted };
|
|
640
|
+
}
|
|
641
|
+
|
|
562
642
|
export function deleteSkill(viewer: Viewer, id: string): void {
|
|
563
643
|
const skill = getSkill(id);
|
|
564
|
-
if (!skill || !
|
|
644
|
+
if (!skill || !canView(viewer, skill) || !canDelete(viewer, skill)) throw new Error("这个技能不存在,或者你不能删它");
|
|
565
645
|
const users = affected(skill);
|
|
646
|
+
for (const u of users) markGuarded(u, skill);
|
|
566
647
|
cache = load().filter((s) => s.id !== id);
|
|
567
648
|
saveIndex();
|
|
568
649
|
fs.rmSync(skillDir(id), { recursive: true, force: true });
|
|
569
|
-
for (const u of users)
|
|
570
|
-
const prefs = userPrefs(u);
|
|
571
|
-
if (!prefs.installed.includes(id) && !prefs.disabled.includes(id)) continue;
|
|
572
|
-
savePrefs(u, { ...prefs, installed: prefs.installed.filter((x) => x !== id), disabled: prefs.disabled.filter((x) => x !== id) });
|
|
573
|
-
}
|
|
650
|
+
for (const u of users) savePrefs(u, forgetSkill(userPrefs(u), id));
|
|
574
651
|
skillEvents.emit("changed", users);
|
|
575
652
|
}
|
|
576
653
|
|
|
@@ -581,11 +658,26 @@ export function installSkill(userId: string, id: string, on: boolean): void {
|
|
|
581
658
|
const prefs = userPrefs(userId);
|
|
582
659
|
const has = prefs.installed.includes(id);
|
|
583
660
|
if (has === on) return;
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
installed:
|
|
587
|
-
|
|
588
|
-
}
|
|
661
|
+
if (on) {
|
|
662
|
+
// 安装 = 确认了现在这一版
|
|
663
|
+
savePrefs(userId, { ...prefs, installed: [...prefs.installed, id], disabled: prefs.disabled.filter((x) => x !== id), accepted: { ...prefs.accepted, [id]: skillRev(skill) } });
|
|
664
|
+
markGuarded(userId, skill);
|
|
665
|
+
} else {
|
|
666
|
+
markGuarded(userId, skill);
|
|
667
|
+
savePrefs(userId, forgetSkill(userPrefs(userId), id));
|
|
668
|
+
}
|
|
669
|
+
skillEvents.emit("changed", [userId]);
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** 装了的技能作者改过内容:用户看过之后确认用新的这一版 */
|
|
673
|
+
export function acceptSkillUpdate(userId: string, id: string): void {
|
|
674
|
+
const skill = getSkill(id);
|
|
675
|
+
if (!skill || !canSee({ id: userId }, skill)) throw new Error("这个技能不存在");
|
|
676
|
+
const prefs = userPrefs(userId);
|
|
677
|
+
if (!prefs.installed.includes(id)) throw new Error("还没安装这个技能");
|
|
678
|
+
if (!updatePending(userId, skill, prefs)) return;
|
|
679
|
+
savePrefs(userId, { ...prefs, accepted: { ...prefs.accepted, [id]: skillRev(skill) } });
|
|
680
|
+
markGuarded(userId, skill);
|
|
589
681
|
skillEvents.emit("changed", [userId]);
|
|
590
682
|
}
|
|
591
683
|
|
|
@@ -597,8 +689,8 @@ export function setSkillEnabled(userId: string, id: string, on: boolean): void {
|
|
|
597
689
|
skillEvents.emit("changed", [userId]);
|
|
598
690
|
}
|
|
599
691
|
|
|
600
|
-
/**
|
|
601
|
-
function ownedOrInstalled(userId: string): Skill[] {
|
|
692
|
+
/** 这个人自己的和装了的技能(含停用的、待确认更新的):变量受不受保护、确认地址都按这些算 */
|
|
693
|
+
export function ownedOrInstalled(userId: string): Skill[] {
|
|
602
694
|
const installed = new Set(userPrefs(userId).installed);
|
|
603
695
|
return load().filter((s) => s.owner === userId || (installed.has(s.id) && canSee({ id: userId }, s)));
|
|
604
696
|
}
|
|
@@ -610,35 +702,71 @@ export function declaredHosts(skills: Skill[], name: string): string[] {
|
|
|
610
702
|
return [...out];
|
|
611
703
|
}
|
|
612
704
|
|
|
705
|
+
/** 实际放行的地址:每个技能用户确认过的,和它现在声明的取交集,再合起来 */
|
|
706
|
+
export function allowedHosts(prefs: UserSkillPrefs, skills: Skill[], name: string): string[] {
|
|
707
|
+
const out = new Set<string>();
|
|
708
|
+
for (const s of skills) {
|
|
709
|
+
const declared = declaredHosts([s], name);
|
|
710
|
+
for (const h of prefs.approved[name]?.[s.id] ?? []) if (declared.includes(h)) out.add(h);
|
|
711
|
+
}
|
|
712
|
+
return [...out];
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
/** 这个技能对这个变量声明了、用户还没确认的地址(值没填就不算) */
|
|
716
|
+
export function pendingHosts(prefs: UserSkillPrefs, skill: Skill, name: string): string[] {
|
|
717
|
+
if (prefs.vars[name] === undefined) return [];
|
|
718
|
+
const ok = prefs.approved[name]?.[skill.id] ?? [];
|
|
719
|
+
return declaredHosts([skill], name).filter((h) => !ok.includes(h));
|
|
720
|
+
}
|
|
721
|
+
|
|
613
722
|
/**
|
|
614
|
-
* 改变量的值。null / 空字符串 =
|
|
615
|
-
*
|
|
723
|
+
* 改变量的值。null / 空字符串 = 删掉。skillId:在哪个技能的变量页里填的。
|
|
724
|
+
* 填了受保护密钥的值,就等于同意「这个技能」(和自己建的技能)现在声明的地址,别人技能声明的地址要在
|
|
725
|
+
* 各自的变量页里另外确认;换了新值,以前对别的技能的同意作废。
|
|
726
|
+
* approve 里的变量名:值不变,只把这个技能的地址确认成它现在声明的(作者改过地址之后)。
|
|
616
727
|
*/
|
|
617
|
-
export function setSkillVars(userId: string, patch: Record<string, unknown>, approve: string[] = []): void {
|
|
728
|
+
export function setSkillVars(userId: string, patch: Record<string, unknown>, approve: string[] = [], skillId?: string): void {
|
|
618
729
|
const prefs = userPrefs(userId);
|
|
619
730
|
const vars = { ...prefs.vars };
|
|
620
731
|
const approved = { ...prefs.approved };
|
|
732
|
+
const guarded = new Set(prefs.guarded);
|
|
621
733
|
const skills = ownedOrInstalled(userId);
|
|
734
|
+
const from = skillId ? skills.find((s) => s.id === skillId) : undefined;
|
|
735
|
+
if (skillId && !from) throw new Error("这个技能不存在,或者你还没装它");
|
|
736
|
+
const protectors = protectedNames(skills);
|
|
737
|
+
const grant = (k: string, bySkill: Record<string, string[]>) => {
|
|
738
|
+
for (const s of skills) {
|
|
739
|
+
if (s.owner !== userId && s !== from) continue;
|
|
740
|
+
const hosts = declaredHosts([s], k);
|
|
741
|
+
if (hosts.length) bySkill[s.id] = hosts;
|
|
742
|
+
}
|
|
743
|
+
return bySkill;
|
|
744
|
+
};
|
|
622
745
|
for (const [k, v] of Object.entries(patch)) {
|
|
623
746
|
if (!VAR_NAME.test(k) || RESERVED_VAR.test(k)) throw new Error(`变量名 ${k} 不合规`);
|
|
624
747
|
if (v === null || v === undefined || v === "") {
|
|
625
748
|
delete vars[k];
|
|
626
749
|
delete approved[k];
|
|
750
|
+
guarded.delete(k);
|
|
627
751
|
continue;
|
|
628
752
|
}
|
|
629
753
|
const s = String(v);
|
|
630
754
|
if (s.length > SKILL_LIMITS.varValue) throw new Error(`${k} 的值太长(最多 ${SKILL_LIMITS.varValue} 个字符)`);
|
|
631
755
|
vars[k] = s;
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
756
|
+
if (protectors.has(k)) {
|
|
757
|
+
guarded.add(k);
|
|
758
|
+
approved[k] = grant(k, {});
|
|
759
|
+
} else {
|
|
760
|
+
// 没有任何技能把它当受保护的密钥:用户这次是按普通变量填的
|
|
761
|
+
guarded.delete(k);
|
|
762
|
+
delete approved[k];
|
|
763
|
+
}
|
|
635
764
|
}
|
|
636
765
|
for (const k of approve) {
|
|
637
766
|
if (vars[k] === undefined) continue;
|
|
638
|
-
|
|
639
|
-
if (hosts.length) approved[k] = hosts;
|
|
767
|
+
approved[k] = grant(k, { ...(approved[k] ?? {}) });
|
|
640
768
|
}
|
|
641
|
-
savePrefs(userId, { ...prefs, vars, approved });
|
|
769
|
+
savePrefs(userId, { ...prefs, vars, approved, guarded: [...guarded] });
|
|
642
770
|
skillEvents.emit("changed", [userId]);
|
|
643
771
|
}
|
|
644
772
|
|
|
@@ -647,16 +775,24 @@ export function protectedNames(skills: Skill[]): Set<string> {
|
|
|
647
775
|
return new Set(skills.flatMap((s) => s.vars.filter(isProtected).map((v) => v.name)));
|
|
648
776
|
}
|
|
649
777
|
|
|
778
|
+
/**
|
|
779
|
+
* 这个人不进环境变量的变量名:他自己的和装了的技能(不管启没启用、这次会话挂没挂)里有一个声明成受保护,
|
|
780
|
+
* 或者当初是按受保护密钥填的值。只看这次会话挂上的技能就会被同名的普通变量钻空子。
|
|
781
|
+
*/
|
|
782
|
+
export function guardedNames(userId: string): Set<string> {
|
|
783
|
+
return new Set([...protectedNames(ownedOrInstalled(userId)), ...userPrefs(userId).guarded]);
|
|
784
|
+
}
|
|
785
|
+
|
|
650
786
|
/** 这个人的会话要注入的环境变量:只给他启用着的技能声明过的、他填了值的;受保护的密钥不放 */
|
|
651
787
|
export function skillEnv(userId: string, skills: Skill[] = activeSkills(userId)): Record<string, string> {
|
|
652
788
|
const vars = userPrefs(userId).vars;
|
|
653
|
-
const guarded =
|
|
789
|
+
const guarded = guardedNames(userId);
|
|
654
790
|
const out: Record<string, string> = {};
|
|
655
791
|
for (const s of skills) for (const v of s.vars) if (vars[v.name] !== undefined && !guarded.has(v.name)) out[v.name] = vars[v.name]!;
|
|
656
792
|
return out;
|
|
657
793
|
}
|
|
658
794
|
|
|
659
|
-
/**
|
|
795
|
+
/** 删用户:他建的技能(装了的人也就没了)、他的设置一起删,别人分享名单里的他也去掉 */
|
|
660
796
|
export function purgeUserSkills(userId: string): void {
|
|
661
797
|
for (const s of load().filter((x) => x.owner === userId)) {
|
|
662
798
|
try {
|
|
@@ -665,6 +801,17 @@ export function purgeUserSkills(userId: string): void {
|
|
|
665
801
|
// 已经没了
|
|
666
802
|
}
|
|
667
803
|
}
|
|
804
|
+
const touched: string[] = [];
|
|
805
|
+
for (const s of load()) {
|
|
806
|
+
if (!s.sharedWith.includes(userId)) continue;
|
|
807
|
+
s.sharedWith = s.sharedWith.filter((u) => u !== userId);
|
|
808
|
+
if (s.visibility === "users" && !s.sharedWith.length) s.visibility = "private";
|
|
809
|
+
touched.push(s.owner);
|
|
810
|
+
}
|
|
811
|
+
if (touched.length) {
|
|
812
|
+
saveIndex();
|
|
813
|
+
skillEvents.emit("changed", [...new Set(touched)]);
|
|
814
|
+
}
|
|
668
815
|
fs.rmSync(prefsPath(userId), { force: true });
|
|
669
816
|
fs.rmSync(path.join(pluginsRoot, checkId(userId, "用户")), { recursive: true, force: true });
|
|
670
817
|
}
|
package/src/core/store.ts
CHANGED
|
@@ -64,6 +64,11 @@ export type Conversation = {
|
|
|
64
64
|
* 「自动改文件」跨 /new 保留,「完全允许」开新会话时清掉。实际生效的还要封顶,见 hub.permissionMax。
|
|
65
65
|
*/
|
|
66
66
|
permission?: PermissionLevel;
|
|
67
|
+
/**
|
|
68
|
+
* 计划模式:agent 先出方案,用户批准了才按 permission 动手。和权限级别叠加,属于对话;
|
|
69
|
+
* 方案批准后自动关掉(见 hub 的 onPlanMode)
|
|
70
|
+
*/
|
|
71
|
+
plan?: boolean;
|
|
67
72
|
/** 以前 /autoedit 记的,只读不写(Claude 下等于 permission: "edit");选过级别就清掉 */
|
|
68
73
|
autoEdit?: boolean;
|
|
69
74
|
/** 从哪条对话分叉出来的(对话 key),列表里据此显示关联 */
|
package/src/core/types.ts
CHANGED
|
@@ -60,6 +60,14 @@ export type InteractionSpec =
|
|
|
60
60
|
alwaysGlobal?: boolean;
|
|
61
61
|
/** 给了就显示「本会话全部允许」按钮。只有 agent 能真正兑现时才给 */
|
|
62
62
|
allowSession?: boolean;
|
|
63
|
+
/** 「允许一次」「拒绝」按钮换个说法(批准计划:开始执行 / 继续改方案) */
|
|
64
|
+
allowLabel?: string;
|
|
65
|
+
denyLabel?: string;
|
|
66
|
+
/**
|
|
67
|
+
* 发起方不等这张卡(Codex 出完方案那一轮已经结束了):用户另外发了消息就撤掉,
|
|
68
|
+
* 和不阻塞的提问一样(见 interactions.dismissDetached)
|
|
69
|
+
*/
|
|
70
|
+
detached?: boolean;
|
|
63
71
|
}
|
|
64
72
|
| {
|
|
65
73
|
kind: "question";
|
|
@@ -108,6 +116,8 @@ export type AgentEvents = {
|
|
|
108
116
|
onTextDelta: (delta: string) => void;
|
|
109
117
|
/** 一段完整的回复正文 */
|
|
110
118
|
onText: (text: string) => void;
|
|
119
|
+
/** agent 自己进出了计划模式(用户批准了方案、Claude 自己进了计划模式):核心据此更新对话上的开关 */
|
|
120
|
+
onPlanMode?: (on: boolean) => void;
|
|
111
121
|
/** 用户回答了不阻塞的提问(detached):把答案当下一条用户消息发出 */
|
|
112
122
|
onFollowUp?: (text: string) => void;
|
|
113
123
|
onResult: (result: TurnResult) => void;
|
|
@@ -223,6 +233,8 @@ export type SessionOptions = {
|
|
|
223
233
|
uploadsDir: string;
|
|
224
234
|
/** 权限级别(见 core/permission.ts),已按主人和专家封顶。不给 = 老行为:Claude 逐次询问,Codex 按设置里的沙箱 */
|
|
225
235
|
permission?: PermissionLevel | undefined;
|
|
236
|
+
/** 计划模式:先出方案,用户批准了再按 permission 动手(见 store.ts 的 Conversation.plan) */
|
|
237
|
+
plan?: boolean;
|
|
226
238
|
/** 不给「本会话全部允许」按钮:有主人的对话,主人不是管理员 */
|
|
227
239
|
noSessionGrant?: boolean;
|
|
228
240
|
/**
|
|
@@ -285,6 +297,8 @@ export interface AgentSession {
|
|
|
285
297
|
dispose(): Promise<void>;
|
|
286
298
|
/** 运行中切换权限级别。不追溯已经弹出的授权 */
|
|
287
299
|
setPermission?(level: PermissionLevel | undefined): Promise<void>;
|
|
300
|
+
/** 开关计划模式。Claude 立即生效,Codex 下一轮生效 */
|
|
301
|
+
setPlan?(on: boolean): Promise<void>;
|
|
288
302
|
}
|
|
289
303
|
|
|
290
304
|
export type Diagnosis = {
|
package/src/mcp/tasks-server.ts
CHANGED
|
@@ -189,7 +189,7 @@ const SKILL_TOOLS = [
|
|
|
189
189
|
name: "skill_request",
|
|
190
190
|
description:
|
|
191
191
|
"替你发一个 HTTP 请求,url、headers、body 里的 {{变量名}} 由 agent-loft 换成用户在技能里填的值 —— 你看不到密钥本身。" +
|
|
192
|
-
"技能里的受保护密钥(list_skills
|
|
192
|
+
"技能里的受保护密钥(list_skills 里标了「受保护」的)只能这样用:只能写在 headers 或 url 的查询参数(? 后面)里,不能写进 body 和 url 的主机、路径;只发往用户确认过的地址,公网地址要用 https。普通变量写在哪都行。" +
|
|
193
193
|
"不跟随跳转;响应里出现的变量值会被抹掉;最多返回 256KB 文本。",
|
|
194
194
|
inputSchema: {
|
|
195
195
|
type: "object",
|
package/web/css/composer.css
CHANGED
|
@@ -520,6 +520,21 @@ textarea.sending {
|
|
|
520
520
|
min-width: 260px;
|
|
521
521
|
}
|
|
522
522
|
|
|
523
|
+
/* 计划模式那一行:开关只显示状态,点的是整行 */
|
|
524
|
+
.perm-pop .perm-plan .switch {
|
|
525
|
+
flex-shrink: 0;
|
|
526
|
+
pointer-events: none;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
.perm-plan-tag {
|
|
530
|
+
padding: 0 6px;
|
|
531
|
+
border-radius: 999px;
|
|
532
|
+
font-size: 11px;
|
|
533
|
+
line-height: 18px;
|
|
534
|
+
background: color-mix(in srgb, var(--accent) 12%, transparent);
|
|
535
|
+
color: var(--accent);
|
|
536
|
+
}
|
|
537
|
+
|
|
523
538
|
@media (max-width: 480px) {
|
|
524
539
|
/* 窄屏只留盾牌,把地方让给模型按钮;完全允许仍然保留文字 */
|
|
525
540
|
.perm-pill:not(.danger) .perm-name {
|
package/web/lib/permission.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
// 权限级别的显示文案和上限换算(级别的含义见服务端 core/permission.ts)
|
|
2
|
-
export const PERMISSION_ORDER = ["ask", "edit", "full"];
|
|
2
|
+
export const PERMISSION_ORDER = ["ask", "edit", "auto", "full"];
|
|
3
3
|
|
|
4
4
|
export const PERMISSIONS = {
|
|
5
5
|
ask: { label: "逐次询问", short: "询问", desc: "改文件、跑命令都先问你" },
|
|
6
6
|
edit: { label: "自动改文件", short: "自动改文件", desc: "工作目录内改文件不再询问,其余照常问" },
|
|
7
|
+
auto: { label: "自动判断", short: "自动判断", desc: "Claude 逐个判断安不安全,安全的直接做,拿不准再问你" },
|
|
7
8
|
full: { label: "完全允许", short: "完全允许", desc: "不再询问,可以跑任意命令、改任意文件" },
|
|
8
9
|
};
|
|
9
10
|
|
|
11
|
+
/** 能设成默认的级别(完全允许只对一次任务说,不能默认) */
|
|
12
|
+
export const DEFAULT_LEVELS = ["ask", "edit", "auto"];
|
|
13
|
+
|
|
14
|
+
export const PLAN_INFO = { label: "计划模式", desc: "先只读、出方案,你批准了再按上面的权限动手" };
|
|
15
|
+
|
|
10
16
|
/** 专家的权限 → 最高能选到哪一级 */
|
|
11
17
|
export function capOfAccess(access) {
|
|
12
18
|
return !access || access === "exec" ? "full" : access === "edit" ? "edit" : "ask";
|
|
@@ -16,9 +22,22 @@ export function minPermission(a, b) {
|
|
|
16
22
|
return PERMISSION_ORDER[Math.min(PERMISSION_ORDER.indexOf(a), PERMISSION_ORDER.indexOf(b))];
|
|
17
23
|
}
|
|
18
24
|
|
|
19
|
-
/**
|
|
20
|
-
export function
|
|
21
|
-
return
|
|
25
|
+
/** 自动判断只有 Claude 有 */
|
|
26
|
+
export function engineSupports(agent, level) {
|
|
27
|
+
return level !== "auto" || agent === "claude";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** 不超过 max、这个引擎认的级别。agent 不给就不按引擎筛 */
|
|
31
|
+
export function permissionsUpTo(max, agent) {
|
|
32
|
+
return PERMISSION_ORDER.slice(0, PERMISSION_ORDER.indexOf(max) + 1).filter((l) => !agent || engineSupports(agent, l));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** 新对话没选级别时按哪一级:设置里定的默认,封顶后引擎不认就降到自动改文件(和服务端 hub.effectivePermission 一致) */
|
|
36
|
+
export function defaultPermissionFor(agent, max, defaults) {
|
|
37
|
+
const wanted = defaults?.[agent];
|
|
38
|
+
if (!wanted || !PERMISSIONS[wanted]) return null;
|
|
39
|
+
const capped = minPermission(wanted, max);
|
|
40
|
+
return engineSupports(agent, capped) ? capped : "edit";
|
|
22
41
|
}
|
|
23
42
|
|
|
24
43
|
export const FULL_WARNING = {
|
package/web/settings/agents.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { fill, h, icon, toast } from "../lib/dom.js";
|
|
2
2
|
import { api } from "../lib/api.js";
|
|
3
3
|
import { copyText } from "../lib/markdown.js";
|
|
4
|
-
import { desktopQuota, S } from "../state.js";
|
|
5
|
-
import { defaultEntryName, usableAgents } from "../views/topbar.js";
|
|
4
|
+
import { desktopQuota, isAdmin, S } from "../state.js";
|
|
5
|
+
import { defaultEntryName, renderContext, usableAgents } from "../views/topbar.js";
|
|
6
|
+
import { DEFAULT_LEVELS, engineSupports, PERMISSIONS } from "../lib/permission.js";
|
|
6
7
|
import { renderEmpty } from "../views/messages.js";
|
|
7
8
|
import { field, saveButton, saveConfig } from "./common.js";
|
|
8
9
|
import { modelPicker } from "./commands.js";
|
|
@@ -193,16 +194,25 @@ function builtinDefaultsSections(targets) {
|
|
|
193
194
|
const machine = (data.machineMcp?.[engine] ?? []).map((name) => ({ id: `${data.machinePrefix}${name}`, name: `${name}(本机)` }));
|
|
194
195
|
const mcp = picker(`${label} MCP`, [...data.mcp, ...machine], prefs.mcp, data.machineMcp ? "沿用机器上的 MCP 配置" : null);
|
|
195
196
|
const machineSkills = h("input", { type: "checkbox", checked: prefs.machineSkills });
|
|
196
|
-
|
|
197
|
+
// 成员最高到自动改文件;自动判断只有 Claude 有
|
|
198
|
+
const levels = DEFAULT_LEVELS.filter((l) => engineSupports(engine, l) && (l !== "auto" || isAdmin()));
|
|
199
|
+
const permission = h("select", { "aria-label": `${label} 默认权限` },
|
|
200
|
+
h("option", { value: "", selected: !prefs.permission }, engine === "claude" ? "跟随 Claude Code(逐次询问)" : "跟随沙箱设置"),
|
|
201
|
+
levels.map((l) => h("option", { value: l, selected: l === prefs.permission }, `${PERMISSIONS[l].label}:${PERMISSIONS[l].desc}`)));
|
|
202
|
+
fill(section, h("h4", {}, "默认权限"),
|
|
203
|
+
field("新对话的权限", permission, "没单独选过权限的对话都按这一级(输入框里的盾牌按钮可以随时改)。自定义专家也按它,但不会超过专家自己的权限;定时任务按任务里选的。「完全允许」不能设成默认。"),
|
|
204
|
+
h("h4", {}, "默认加载"),
|
|
197
205
|
h("p", { class: "skill-note" }, "仅影响你当前账号使用的这个内置 Agent,自定义专家使用各自的配置。保存后,下次发消息生效。"),
|
|
198
206
|
skills.el,
|
|
199
207
|
data.machineMcp ? h("label", { class: "check-item" }, machineSkills, "同时加载机器上的技能") : null,
|
|
200
208
|
mcp.el,
|
|
201
209
|
h("small", {}, "技能和 MCP 可分别全部取消。记忆、定时任务、技能管理等系统工具仍按功能设置提供。"),
|
|
202
210
|
h("div", { class: "row" }, saveButton(async () => {
|
|
203
|
-
await api("/api/agents/defaults", { method: "PUT", body: { agents: { [engine]: { skills: skills.value(), machineSkills: machineSkills.checked, mcp: mcp.value() } } } });
|
|
211
|
+
const res = await api("/api/agents/defaults", { method: "PUT", body: { agents: { [engine]: { permission: permission.value || null, skills: skills.value(), machineSkills: machineSkills.checked, mcp: mcp.value() } } } });
|
|
212
|
+
S.defaultPermission = { ...S.defaultPermission, [engine]: res.agents?.[engine]?.permission ?? null };
|
|
213
|
+
renderContext();
|
|
204
214
|
toast("已保存,下次发消息生效");
|
|
205
|
-
}, `保存 ${label}
|
|
215
|
+
}, `保存 ${label} 默认设置`)));
|
|
206
216
|
}
|
|
207
217
|
if (!Object.keys(data.agents).length) {
|
|
208
218
|
const section = Object.values(targets)[0];
|
package/web/settings/commands.js
CHANGED
|
@@ -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", "permission", "autoedit", "web", "detach", "approve", "help"];
|
|
78
|
+
const BUILTIN_NAMES = ["new", "status", "stop", "model", "effort", "sessions", "resume", "fork", "agent", "claude", "codex", "cd", "pwd", "permission", "planmode", "autoedit", "web", "detach", "approve", "help"];
|
|
79
79
|
|
|
80
80
|
/**
|
|
81
81
|
* 添加 / 编辑一条自定义命令。onSave 返回 true 才关窗;失败(比如服务端校验没过)留着让用户改。
|