agent-loft 0.2.96 → 0.2.98
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 -4
- package/package.json +1 -1
- package/src/channels/web/api.ts +7 -5
- package/src/channels/web/experts-api.ts +32 -5
- package/src/channels/web/skills-api.ts +42 -27
- package/src/channels/web/workspace-api.ts +14 -1
- package/src/core/builtin-agents.ts +2 -2
- package/src/core/experts.ts +18 -9
- package/src/core/git.ts +192 -2
- package/src/core/hub.ts +1 -1
- package/src/core/mcp.ts +121 -18
- 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/users.ts +15 -2
- package/src/mcp/tasks-server.ts +1 -1
- package/web/css/workspace.css +24 -0
- package/web/settings/experts.js +63 -10
- package/web/settings/skills.js +38 -13
- package/web/settings/users.js +29 -2
- package/web/views/workspace.js +38 -1
|
@@ -9,15 +9,19 @@ import { hub } from "./hub.js";
|
|
|
9
9
|
import { interactions } from "./interactions.js";
|
|
10
10
|
import { config } from "./config.js";
|
|
11
11
|
import { TASK_TOOL_TIMEOUT_SEC } from "./tasks-mcp.js";
|
|
12
|
+
import { builtinAgentDefaults } from "./builtin-agents.js";
|
|
13
|
+
import { expertTools, getExpert, isBuiltin } from "./experts.js";
|
|
14
|
+
import { getConversation } from "./store.js";
|
|
12
15
|
import {
|
|
13
16
|
activeSkills,
|
|
17
|
+
allowedHosts,
|
|
14
18
|
canSee,
|
|
15
19
|
cleanVars,
|
|
16
20
|
createSkill,
|
|
17
21
|
findSkillByName,
|
|
22
|
+
guardedNames,
|
|
18
23
|
hostAllowed,
|
|
19
24
|
isProtected,
|
|
20
|
-
protectedNames,
|
|
21
25
|
listFiles,
|
|
22
26
|
readSkillFile,
|
|
23
27
|
readSkillMd,
|
|
@@ -39,33 +43,91 @@ const METHODS = ["GET", "POST", "PUT", "PATCH", "DELETE", "HEAD"];
|
|
|
39
43
|
const RESPONSE_MAX = 256 * 1024;
|
|
40
44
|
const PLACEHOLDER = /\{\{\s*([A-Z][A-Z0-9_]{1,63})\s*\}\}/g;
|
|
41
45
|
|
|
42
|
-
/**
|
|
46
|
+
/** 响应里出现了密钥的值(接口回显、报错里带着):原文、URL 编码、base64 的都换掉,别让它回到模型手里 */
|
|
43
47
|
function redact(text: string, secrets: [string, string][]): string {
|
|
44
48
|
let out = text;
|
|
45
|
-
for (const [name, value] of secrets)
|
|
49
|
+
for (const [name, value] of secrets) {
|
|
50
|
+
const forms = new Set([value, encodeURIComponent(value), Buffer.from(value, "utf8").toString("base64"), Buffer.from(value, "utf8").toString("base64url")]);
|
|
51
|
+
for (const f of [...forms].sort((x, y) => y.length - x.length)) if (f.length >= 4) out = out.split(f).join(`[已隐藏 ${name}]`);
|
|
52
|
+
}
|
|
46
53
|
return out;
|
|
47
54
|
}
|
|
48
55
|
|
|
56
|
+
/** 局域网、本机的地址:受保护的密钥只有发往这些时才允许走明文 http */
|
|
57
|
+
function isPrivateHost(host: string): boolean {
|
|
58
|
+
const h = host.toLowerCase().replace(/^\[|\]$/g, "");
|
|
59
|
+
return (
|
|
60
|
+
h === "localhost" ||
|
|
61
|
+
h === "::1" ||
|
|
62
|
+
h.endsWith(".local") ||
|
|
63
|
+
h.endsWith(".lan") ||
|
|
64
|
+
/^127\./.test(h) ||
|
|
65
|
+
/^10\./.test(h) ||
|
|
66
|
+
/^192\.168\./.test(h) ||
|
|
67
|
+
/^172\.(1[6-9]|2\d|3[01])\./.test(h) ||
|
|
68
|
+
/^169\.254\./.test(h)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
49
72
|
/**
|
|
50
|
-
*
|
|
51
|
-
*
|
|
73
|
+
* 这条对话的会话挂了哪些技能(和 hub 开会话时的算法一样):自定义专家选中的(连同子智能体的)、
|
|
74
|
+
* 内置 Agent 默认加载的。undefined = 全部启用着的。
|
|
75
|
+
*/
|
|
76
|
+
export function skillScope(userId: string, convKey: string): Set<string> | undefined {
|
|
77
|
+
const conv = getConversation(convKey);
|
|
78
|
+
if (!conv) return new Set();
|
|
79
|
+
try {
|
|
80
|
+
if (conv.expert) {
|
|
81
|
+
const e = getExpert(conv.expert);
|
|
82
|
+
if (!e) return new Set();
|
|
83
|
+
const ids = new Set(expertTools(userId, e).skills);
|
|
84
|
+
for (const subId of e.subagents) {
|
|
85
|
+
const sub = getExpert(subId);
|
|
86
|
+
if (!sub || sub.owner !== e.owner) continue;
|
|
87
|
+
try {
|
|
88
|
+
for (const id of expertTools(userId, sub).skills) ids.add(id);
|
|
89
|
+
} catch {
|
|
90
|
+
// 子智能体取消共享了:它的技能不算
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return ids;
|
|
94
|
+
}
|
|
95
|
+
if (isBuiltin(conv.agent)) {
|
|
96
|
+
const picked = builtinAgentDefaults(userId)[conv.agent].skills;
|
|
97
|
+
return picked ? new Set(picked) : undefined;
|
|
98
|
+
}
|
|
99
|
+
} catch {
|
|
100
|
+
return new Set();
|
|
101
|
+
}
|
|
102
|
+
return undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
type Where = "header" | "query" | "url" | "body";
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 按 agent 给的 url / headers / body 发一个 HTTP 请求,里面的 {{NAME}} 换成用户填的变量值。只认这条对话
|
|
109
|
+
* 挂着的技能(scope)声明过的变量。受保护的密钥:
|
|
110
|
+
* - 只能发往用户对这些技能确认过、技能现在还声明着的地址(按换完之后的地址查:塞进主机名里也逃不掉);
|
|
111
|
+
* - 只能写在请求头和 url 的查询参数里 —— 写进 body 就能借允许的服务本身发出去(比如建一个公开的 gist);
|
|
112
|
+
* - 只走 https,局域网、本机的地址才允许 http。
|
|
52
113
|
* 不跟随跳转(跳到别处会把带着密钥的请求带过去),响应里出现的变量值一律抹掉。
|
|
53
114
|
*/
|
|
54
|
-
export async function skillRequest(userId: string, args: Record<string, unknown>): Promise<Reply> {
|
|
115
|
+
export async function skillRequest(userId: string, args: Record<string, unknown>, scope?: Set<string>): Promise<Reply> {
|
|
55
116
|
const prefs = userPrefs(userId);
|
|
56
|
-
const skills = activeSkills(userId);
|
|
57
|
-
const guarded =
|
|
117
|
+
const skills = activeSkills(userId).filter((sk) => !scope || scope.has(sk.id));
|
|
118
|
+
const guarded = guardedNames(userId);
|
|
58
119
|
const declared = new Set(skills.flatMap((sk) => sk.vars.map((v) => v.name)));
|
|
59
120
|
const method = String(args.method ?? "GET").toUpperCase();
|
|
60
121
|
if (!METHODS.includes(method)) return fail(`method 只能是 ${METHODS.join("、")}`);
|
|
61
122
|
const rawHeaders = args.headers && typeof args.headers === "object" ? (args.headers as Record<string, unknown>) : {};
|
|
62
123
|
const body = args.body === undefined || args.body === null ? undefined : String(args.body);
|
|
63
124
|
|
|
64
|
-
const used = new Set<
|
|
125
|
+
const used = new Map<string, Set<Where>>();
|
|
65
126
|
const missing = new Set<string>();
|
|
66
|
-
const fillIn = (text: string, encode: boolean) =>
|
|
67
|
-
text.replace(PLACEHOLDER, (m, name: string) => {
|
|
68
|
-
used.
|
|
127
|
+
const fillIn = (text: string, where: (offset: number) => Where, encode: boolean) =>
|
|
128
|
+
text.replace(PLACEHOLDER, (m, name: string, offset: number) => {
|
|
129
|
+
if (!used.has(name)) used.set(name, new Set());
|
|
130
|
+
used.get(name)!.add(where(offset));
|
|
69
131
|
const value = declared.has(name) ? prefs.vars[name] : undefined;
|
|
70
132
|
if (value === undefined) {
|
|
71
133
|
missing.add(name);
|
|
@@ -73,22 +135,29 @@ export async function skillRequest(userId: string, args: Record<string, unknown>
|
|
|
73
135
|
}
|
|
74
136
|
return encode ? encodeURIComponent(value) : value;
|
|
75
137
|
});
|
|
138
|
+
const rawUrl = String(args.url ?? "");
|
|
139
|
+
const q = rawUrl.indexOf("?");
|
|
140
|
+
const hash = rawUrl.indexOf("#");
|
|
141
|
+
const inQuery = (offset: number): Where => (q >= 0 && offset > q && (hash < 0 || offset < hash) ? "query" : "url");
|
|
76
142
|
let url: URL;
|
|
77
143
|
try {
|
|
78
|
-
url = new URL(fillIn(
|
|
144
|
+
url = new URL(fillIn(rawUrl, inQuery, true));
|
|
79
145
|
} catch {
|
|
80
146
|
return fail("url 不是合法的地址。");
|
|
81
147
|
}
|
|
82
148
|
const headers: Record<string, string> = {};
|
|
83
|
-
for (const [k, v] of Object.entries(rawHeaders)) headers[k] = fillIn(String(v), false);
|
|
84
|
-
const filledBody = body === undefined ? undefined : fillIn(body, false);
|
|
149
|
+
for (const [k, v] of Object.entries(rawHeaders)) headers[k] = fillIn(String(v), () => "header", false);
|
|
150
|
+
const filledBody = body === undefined ? undefined : fillIn(body, () => "body", false);
|
|
85
151
|
if (missing.size) {
|
|
86
|
-
return fail(
|
|
152
|
+
return fail(`这些变量用户还没配(或者这个会话挂着的技能都没声明过):${[...missing].join("、")}。请用户去网页侧栏「技能」里填好,下一条消息起可用。`);
|
|
87
153
|
}
|
|
88
154
|
if (url.protocol !== "https:" && url.protocol !== "http:") return fail("只支持 http 和 https。");
|
|
89
|
-
for (const name of used) {
|
|
155
|
+
for (const [name, where] of used) {
|
|
90
156
|
if (!guarded.has(name)) continue;
|
|
91
|
-
|
|
157
|
+
if (where.has("body") || where.has("url")) {
|
|
158
|
+
return fail(`${name} 是受保护的密钥,只能写在 headers 或 url 的查询参数(? 后面)里,不能写进 body 或 url 的主机、路径。没有发出去。`);
|
|
159
|
+
}
|
|
160
|
+
const allowed = allowedHosts(prefs, skills, name);
|
|
92
161
|
if (!hostAllowed(allowed, url)) {
|
|
93
162
|
return fail(
|
|
94
163
|
allowed.length
|
|
@@ -96,9 +165,10 @@ export async function skillRequest(userId: string, args: Record<string, unknown>
|
|
|
96
165
|
: `${name} 允许发往的地址用户还没确认(技能作者可能改过),请用户去网页侧栏「技能」里确认一下。`,
|
|
97
166
|
);
|
|
98
167
|
}
|
|
168
|
+
if (url.protocol === "http:" && !isPrivateHost(url.hostname)) return fail(`${name} 是受保护的密钥,发往公网地址要用 https,没有发出去。`);
|
|
99
169
|
}
|
|
100
170
|
|
|
101
|
-
const secrets: [string, string][] = Object.entries(prefs.vars).filter(([k]) => declared.has(k));
|
|
171
|
+
const secrets: [string, string][] = Object.entries(prefs.vars).filter(([k]) => declared.has(k) || guarded.has(k));
|
|
102
172
|
let res: Response;
|
|
103
173
|
try {
|
|
104
174
|
res = await fetch(url, {
|
|
@@ -181,15 +251,16 @@ function cardOf(fields: { name: string; description?: string; body?: string; fil
|
|
|
181
251
|
function describe(s: Skill, userId: string): string {
|
|
182
252
|
const vars = userPrefs(userId).vars;
|
|
183
253
|
const v = s.vars.length
|
|
184
|
-
? ` · 变量 ${s.vars.map((x) => `${x.name}${vars[x.name] !== undefined ? "(已配)" : x.required ? "(未配,必填)" : "(未配)"}${isProtected(x) ? `(受保护,只能用 skill_request 发往 ${x.
|
|
254
|
+
? ` · 变量 ${s.vars.map((x) => `${x.name}${vars[x.name] !== undefined ? "(已配)" : x.required ? "(未配,必填)" : "(未配)"}${isProtected(x) ? `(受保护,只能用 skill_request 发往 ${allowedHosts(userPrefs(userId), [s], x.name).join("、") || "用户还没确认的地址"})` : ""}`).join("、")}`
|
|
185
255
|
: "";
|
|
186
256
|
return `- ${s.name}:${s.description}${s.owner === userId ? "(自己建的)" : `(${getUser(s.owner)?.name ?? "别人"}建的)`}${v}`;
|
|
187
257
|
}
|
|
188
258
|
|
|
189
259
|
export async function skillToolCall(userId: string, convKey: string, tool: string, args: Record<string, unknown>): Promise<Reply> {
|
|
190
|
-
if (tool === "skill_request") return skillRequest(userId, args);
|
|
260
|
+
if (tool === "skill_request") return skillRequest(userId, args, skillScope(userId, convKey));
|
|
191
261
|
if (tool === "list_skills") {
|
|
192
|
-
const
|
|
262
|
+
const scope = skillScope(userId, convKey);
|
|
263
|
+
const list = activeSkills(userId).filter((s) => !scope || scope.has(s.id));
|
|
193
264
|
if (!list.length) return { text: "用户在 agent-loft 里还没有启用任何技能。" };
|
|
194
265
|
return { text: `用户启用着的技能(${list.length} 个):\n${list.map((s) => describe(s, userId)).join("\n")}` };
|
|
195
266
|
}
|
|
@@ -230,7 +301,7 @@ export async function skillToolCall(userId: string, convKey: string, tool: strin
|
|
|
230
301
|
}
|
|
231
302
|
const description = String(args.description ?? "").trim();
|
|
232
303
|
const body = String(args.body ?? "").trim();
|
|
233
|
-
if (findSkillByName(name)) return fail(
|
|
304
|
+
if (findSkillByName(name)) return fail(`技能名 ${name} 已被占用,换个名字;要改自己的用 update_skill。`);
|
|
234
305
|
const ok = await confirm(convKey, `新建技能「${name}」`, `${cardOf({ name, description, body, files: [...files.keys()], vars })}\n\n建好后只有你自己看得到,可以在网页侧栏「技能」里修改、分享给别人。`);
|
|
235
306
|
if (!ok) return fail("用户没有同意,技能没有建。");
|
|
236
307
|
try {
|
|
@@ -269,7 +340,7 @@ export async function skillToolCall(userId: string, convKey: string, tool: strin
|
|
|
269
340
|
...(changes.write ? { files: [...changes.write.keys()] } : {}),
|
|
270
341
|
...(changes.remove ? { removed: changes.remove } : {}),
|
|
271
342
|
...(vars ? { vars } : {}),
|
|
272
|
-
})}${s.visibility !== "private" ? "\n\n
|
|
343
|
+
})}${s.visibility !== "private" ? "\n\n这个技能分享给了别人:内容有变化时,装了它的人要在网页上确认新版之后才会加载。" : ""}`,
|
|
273
344
|
);
|
|
274
345
|
if (!ok) return fail("用户没有同意,技能没有改。");
|
|
275
346
|
try {
|
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/users.ts
CHANGED
|
@@ -32,6 +32,8 @@ export type User = {
|
|
|
32
32
|
remote?: boolean;
|
|
33
33
|
/** 成员能不能用工作区面板(浏览文件、看 Git 改动)。管理员一直能用,不看这个 */
|
|
34
34
|
files?: boolean;
|
|
35
|
+
/** 成员能不能在工作区面板里获取、拉取、推送(用的是服务器上的 Git 凭据)。管理员一直能用,不看这个 */
|
|
36
|
+
gitRemote?: boolean;
|
|
35
37
|
permissions?: ResourcePermissions;
|
|
36
38
|
};
|
|
37
39
|
|
|
@@ -185,6 +187,12 @@ export function canUseFiles(user: Pick<User, "role" | "disabled" | "files"> | un
|
|
|
185
187
|
return user.role === "admin" || user.files === true;
|
|
186
188
|
}
|
|
187
189
|
|
|
190
|
+
/** 能不能在工作区面板里连远程:用的是服务器主人的 Git 身份,所以比面板本身多一道开关。成员要两个都开 */
|
|
191
|
+
export function canUseGitRemote(user: Pick<User, "role" | "disabled" | "files" | "gitRemote"> | undefined): boolean {
|
|
192
|
+
if (!canUseFiles(user)) return false;
|
|
193
|
+
return user!.role === "admin" || user!.gitRemote === true;
|
|
194
|
+
}
|
|
195
|
+
|
|
188
196
|
export function canUseResource(user: Pick<User, "role" | "disabled" | "permissions"> | undefined, kind: ResourceKind, id: string): boolean {
|
|
189
197
|
if (!user || user.disabled) return false;
|
|
190
198
|
return user.role === "admin" || user.permissions?.[kind] == null || user.permissions[kind]!.includes(id);
|
|
@@ -241,7 +249,7 @@ function assertAdminLeft(list: User[]): void {
|
|
|
241
249
|
if (!list.some((u) => u.role === "admin" && !u.disabled)) throw new Error("至少要保留一个能登录的管理员");
|
|
242
250
|
}
|
|
243
251
|
|
|
244
|
-
export function createUser(params: { name: string; password: string; role?: Role; remote?: boolean; files?: boolean; permissions?: unknown }): PublicUser {
|
|
252
|
+
export function createUser(params: { name: string; password: string; role?: Role; remote?: boolean; files?: boolean; gitRemote?: boolean; permissions?: unknown }): PublicUser {
|
|
245
253
|
const list = load();
|
|
246
254
|
const name = checkName(params.name);
|
|
247
255
|
if (list.some((u) => u.name.toLowerCase() === name.toLowerCase())) throw new Error(`用户名「${name}」已经有人用了`);
|
|
@@ -254,6 +262,7 @@ export function createUser(params: { name: string; password: string; role?: Role
|
|
|
254
262
|
createdAt: Date.now(),
|
|
255
263
|
...(params.remote === true ? { remote: true } : {}),
|
|
256
264
|
...(params.files === true ? { files: true } : {}),
|
|
265
|
+
...(params.gitRemote === true ? { gitRemote: true } : {}),
|
|
257
266
|
...(params.permissions !== undefined ? { permissions: cleanPermissions(params.permissions) } : {}),
|
|
258
267
|
};
|
|
259
268
|
save([...list, user]);
|
|
@@ -262,7 +271,7 @@ export function createUser(params: { name: string; password: string; role?: Role
|
|
|
262
271
|
|
|
263
272
|
export function updateUser(
|
|
264
273
|
id: string,
|
|
265
|
-
patch: { name?: string; role?: Role; disabled?: boolean; password?: string; remote?: boolean; files?: boolean; permissions?: unknown },
|
|
274
|
+
patch: { name?: string; role?: Role; disabled?: boolean; password?: string; remote?: boolean; files?: boolean; gitRemote?: boolean; permissions?: unknown },
|
|
266
275
|
): PublicUser {
|
|
267
276
|
const list = load().map((u) => ({ ...u }));
|
|
268
277
|
const user = list.find((u) => u.id === id);
|
|
@@ -285,6 +294,10 @@ export function updateUser(
|
|
|
285
294
|
if (patch.files) user.files = true;
|
|
286
295
|
else delete user.files;
|
|
287
296
|
}
|
|
297
|
+
if (patch.gitRemote !== undefined) {
|
|
298
|
+
if (patch.gitRemote) user.gitRemote = true;
|
|
299
|
+
else delete user.gitRemote;
|
|
300
|
+
}
|
|
288
301
|
if (patch.permissions !== undefined) user.permissions = cleanPermissions(patch.permissions, user.permissions);
|
|
289
302
|
if (patch.password !== undefined) {
|
|
290
303
|
checkPassword(patch.password);
|