agent-loft 0.2.127 → 0.2.128
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 +3 -1
- package/bin/cli.js +40 -19
- package/package.json +1 -1
- package/src/agents/claude/session.ts +8 -5
- package/src/agents/codex/session.ts +5 -4
- package/src/agents/dsh/session.ts +3 -1
- package/src/agents/pi/session.ts +4 -3
- package/src/agents/workbuddy/session.ts +6 -3
- package/src/channels/web/api.ts +10 -1
- package/src/channels/web/memo-collab.ts +28 -0
- package/src/channels/web/memo-file.ts +30 -0
- package/src/channels/web/memo-storage.ts +15 -6
- package/src/channels/web/memos-api.ts +23 -21
- package/src/cli/service.ts +3 -1
- package/src/core/db.ts +12 -0
- package/src/core/expert-prompt.ts +2 -1
- package/src/core/hub.ts +4 -0
- package/src/core/mcp.ts +2 -2
- package/src/core/memo-index.ts +154 -0
- package/src/core/memo-sources.ts +33 -5
- package/src/core/memos-bridge.ts +63 -0
- package/src/core/tasks-bridge.ts +6 -0
- package/src/core/tasks-mcp.ts +7 -0
- package/src/core/types.ts +2 -0
- package/src/core/upgrade-health.ts +21 -0
- package/src/core/upgrade-launcher.ts +94 -0
- package/src/core/upgrade-releases.ts +103 -0
- package/src/core/upgrade.ts +54 -18
- package/src/main.ts +14 -0
- package/src/mcp/memo-tools.ts +26 -0
- package/src/mcp/tasks-server.ts +5 -4
- package/web/settings/users.js +41 -15
- package/web/views/memo-dialogs.js +48 -1
- package/web/views/memos.js +2 -2
package/src/core/upgrade.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { spawn, spawnSync } from "node:child_process";
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
|
+
import { readRelease, releaseStore, stageRelease } from "./upgrade-releases.js";
|
|
9
10
|
import { config, saveConfig } from "./config.js";
|
|
10
11
|
|
|
11
12
|
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
|
@@ -32,17 +33,23 @@ function npmEnv(): NodeJS.ProcessEnv {
|
|
|
32
33
|
function run(cmd: string, args: string[], timeoutMs: number): Promise<{ code: number; out: string }> {
|
|
33
34
|
return new Promise((resolve) => {
|
|
34
35
|
let out = "";
|
|
36
|
+
const npmCli = path.join(path.dirname(process.execPath), "node_modules/npm/bin/npm-cli.js");
|
|
37
|
+
if (process.platform === "win32" && cmd === npmBin() && fs.existsSync(npmCli)) {
|
|
38
|
+
args = [npmCli, ...args];
|
|
39
|
+
cmd = process.execPath;
|
|
40
|
+
}
|
|
35
41
|
const child = spawn(cmd, args, { env: npmEnv(), stdio: ["ignore", "pipe", "pipe"] });
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
child.
|
|
42
|
+
let timedOut = false;
|
|
43
|
+
const timer = setTimeout(() => { timedOut = true; child.kill("SIGKILL"); }, timeoutMs);
|
|
44
|
+
child.stdout.on("data", (d) => (out = (out + d).slice(-8000)));
|
|
45
|
+
child.stderr.on("data", (d) => (out = (out + d).slice(-8000)));
|
|
39
46
|
child.on("error", (err) => {
|
|
40
47
|
clearTimeout(timer);
|
|
41
48
|
resolve({ code: -1, out: err.message });
|
|
42
49
|
});
|
|
43
50
|
child.on("close", (code) => {
|
|
44
51
|
clearTimeout(timer);
|
|
45
|
-
resolve({ code: code ?? -1, out: out.trim() });
|
|
52
|
+
resolve({ code: code ?? -1, out: timedOut ? `操作超时(${timeoutMs / 1000} 秒)。${out.trim()}` : out.trim() });
|
|
46
53
|
});
|
|
47
54
|
});
|
|
48
55
|
}
|
|
@@ -68,7 +75,7 @@ function globalPrefix(): string | null {
|
|
|
68
75
|
return null;
|
|
69
76
|
}
|
|
70
77
|
};
|
|
71
|
-
const self = real(root);
|
|
78
|
+
const self = real(path.dirname(releaseStore(root)));
|
|
72
79
|
if (!self || path.basename(path.dirname(self)) !== "node_modules") return null;
|
|
73
80
|
if (process.platform === "win32") {
|
|
74
81
|
const prefix = path.dirname(path.dirname(self));
|
|
@@ -118,9 +125,22 @@ export type VersionInfo = {
|
|
|
118
125
|
/** 不能在线升级时说明原因和手动办法 */
|
|
119
126
|
reason?: string;
|
|
120
127
|
upgrading: boolean;
|
|
128
|
+
pendingVersion?: string;
|
|
129
|
+
upgradeError?: string;
|
|
121
130
|
};
|
|
122
131
|
|
|
123
132
|
let upgrading = false;
|
|
133
|
+
let installError: string | undefined;
|
|
134
|
+
|
|
135
|
+
// npm 安装可能比反向代理的请求时限更长,设置页用短请求读取进度。
|
|
136
|
+
export function upgradeStatus() {
|
|
137
|
+
return { version: RUNNING_VERSION, upgrading, pendingVersion: readRelease(releaseStore(root), "pending")?.version, error: installError };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function beginUpgrade(): void {
|
|
141
|
+
if (upgrading) return;
|
|
142
|
+
void upgrade().catch(err => console.error("[升级]", err.message));
|
|
143
|
+
}
|
|
124
144
|
|
|
125
145
|
export async function versionInfo(refresh = false): Promise<VersionInfo> {
|
|
126
146
|
const [kind, latest] = await Promise.all([installKind(), latestVersion(refresh)]);
|
|
@@ -131,7 +151,16 @@ export async function versionInfo(refresh = false): Promise<VersionInfo> {
|
|
|
131
151
|
: kind === "unknown"
|
|
132
152
|
? "不是用 npm install -g 装的,没法在线升级"
|
|
133
153
|
: undefined;
|
|
154
|
+
const store = releaseStore(root);
|
|
155
|
+
const pending = readRelease(store, "pending");
|
|
156
|
+
let upgradeError: string | undefined;
|
|
157
|
+
try {
|
|
158
|
+
upgradeError = fs.readFileSync(path.join(store, "last-error"), "utf8");
|
|
159
|
+
} catch (err) {
|
|
160
|
+
if ((err as NodeJS.ErrnoException).code !== "ENOENT") throw err;
|
|
161
|
+
}
|
|
134
162
|
return {
|
|
163
|
+
pendingVersion: pending?.version, upgradeError,
|
|
135
164
|
version: RUNNING_VERSION,
|
|
136
165
|
latest: latest.version,
|
|
137
166
|
...(latest.error ? { latestError: latest.error } : {}),
|
|
@@ -150,25 +179,32 @@ export async function versionInfo(refresh = false): Promise<VersionInfo> {
|
|
|
150
179
|
*/
|
|
151
180
|
export async function upgrade(): Promise<{ from: string; to: string; restarting: boolean; output: string }> {
|
|
152
181
|
if (upgrading) throw new Error("正在升级,稍等");
|
|
153
|
-
const info = await versionInfo(true);
|
|
154
|
-
if (!info.canUpgrade) throw new Error(info.reason ?? "没法在线升级");
|
|
155
|
-
if (!info.latest) throw new Error(`查不到最新版本:${info.latestError ?? ""}`);
|
|
156
|
-
if (!info.updateAvailable) throw new Error(`已经是最新版本 ${RUNNING_VERSION}`);
|
|
157
182
|
upgrading = true;
|
|
183
|
+
installError = undefined;
|
|
158
184
|
try {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (
|
|
164
|
-
|
|
165
|
-
|
|
185
|
+
const info = await versionInfo(true);
|
|
186
|
+
if (!info.canUpgrade) throw new Error(info.reason ?? "没法在线升级");
|
|
187
|
+
if (info.pendingVersion) throw new Error("已有更新等待重启验证");
|
|
188
|
+
if (!info.latest) throw new Error(`查不到最新版本:${info.latestError ?? ""}`);
|
|
189
|
+
if (!info.updateAvailable) throw new Error(`已经是最新版本 ${RUNNING_VERSION}`);
|
|
190
|
+
const version = info.latest;
|
|
191
|
+
const output = await stageRelease(root, version, async (prefix) => {
|
|
192
|
+
const r = await run(npmBin(), ["install", "-g", "--prefix", prefix, `${NAME}@${version}`, "--no-audit", "--no-fund"], 10 * 60_000);
|
|
193
|
+
if (r.code !== 0) throw new Error(`npm install 失败:${r.out || `退出码 ${r.code}`}`);
|
|
194
|
+
return r.out;
|
|
195
|
+
}, async (cli) => {
|
|
196
|
+
const r = await run(process.execPath, [cli, "--help"], 15_000);
|
|
197
|
+
if (r.code !== 0) throw new Error(`新版 CLI 无法启动:${r.out || r.code}`);
|
|
198
|
+
});
|
|
166
199
|
const restarting = !!info.service;
|
|
167
200
|
if (restarting) {
|
|
168
|
-
console.log(`[升级]
|
|
201
|
+
console.log(`[升级] 已准备 ${version},重启后验证`);
|
|
169
202
|
setTimeout(() => process.exit(0), 1500).unref();
|
|
170
203
|
}
|
|
171
|
-
return { from: RUNNING_VERSION, to:
|
|
204
|
+
return { from: RUNNING_VERSION, to: version, restarting, output: output.slice(-2000) };
|
|
205
|
+
} catch (err) {
|
|
206
|
+
installError = err instanceof Error ? err.message : String(err);
|
|
207
|
+
throw err;
|
|
172
208
|
} finally {
|
|
173
209
|
upgrading = false;
|
|
174
210
|
}
|
package/src/main.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { RUNNING_VERSION } from "./core/upgrade.js";
|
|
2
|
+
import { verifyUpgradeWeb } from "./core/upgrade-health.js";
|
|
3
|
+
import { startMemoIndexer, stopMemoIndexer } from "./core/memo-index.js";
|
|
1
4
|
// 入口:拉起 agent 驱动、飞书机器人和 Web 服务。
|
|
2
5
|
import { config, loadConfig, onConfigChange } from "./core/config.js";
|
|
3
6
|
import { hub } from "./core/hub.js";
|
|
@@ -102,6 +105,7 @@ async function main(): Promise<void> {
|
|
|
102
105
|
});
|
|
103
106
|
// 定时任务:agent 用的工具通道先开,再开始按点运行(停机期间错过的补跑)
|
|
104
107
|
startTasksBridge();
|
|
108
|
+
startMemoIndexer();
|
|
105
109
|
startTaskRunner();
|
|
106
110
|
// 先查一遍 agent 装没装、登没登录,网页打开时新对话里就只列出能用的
|
|
107
111
|
void agentHealth().catch(() => {});
|
|
@@ -123,6 +127,7 @@ async function main(): Promise<void> {
|
|
|
123
127
|
console.log(`[退出] 收到 ${signal},正在收尾…`);
|
|
124
128
|
const force = setTimeout(() => process.exit(0), 8000);
|
|
125
129
|
force.unref();
|
|
130
|
+
stopMemoIndexer();
|
|
126
131
|
stopScheduler();
|
|
127
132
|
stopFeishu();
|
|
128
133
|
stopWeixin();
|
|
@@ -138,6 +143,15 @@ async function main(): Promise<void> {
|
|
|
138
143
|
process.on("SIGINT", () => void shutdown("SIGINT"));
|
|
139
144
|
process.on("exit", (code) => console.log(`[退出] agent-loft 结束,退出码 ${code}`));
|
|
140
145
|
process.on("unhandledRejection", (err) => console.error("[未处理的异常]", err));
|
|
146
|
+
if (process.env.AGENT_LOFT_UPGRADE_TRIAL && process.send) {
|
|
147
|
+
const { host, port } = config().web;
|
|
148
|
+
try {
|
|
149
|
+
await verifyUpgradeWeb(host, port);
|
|
150
|
+
process.send({ type: "upgrade-health", version: RUNNING_VERSION });
|
|
151
|
+
} catch (err) {
|
|
152
|
+
process.send({ type: "upgrade-health", error: String(err) });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
141
155
|
}
|
|
142
156
|
|
|
143
157
|
void main();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const writeIntent = "只有用户表达了留存或更新笔记的意图时才写入备忘录。";
|
|
2
|
+
export const MEMO_WRITING_GUIDE = writeIntent +
|
|
3
|
+
"「这个留着下次用」「加到清单里」都算。普通聊天、问答或任务自然结束时,无需主动归档。" +
|
|
4
|
+
"用 agent_loft_memos 自行选择合适的已授权读写库,搜索相关笔记,先读取再更新。已有相同内容就跳过,没有合适的笔记就沿用目录和命名习惯新建 Markdown。" +
|
|
5
|
+
"根据对话拟标题、提炼正文,保存成功后告诉用户库名和文件路径。";
|
|
6
|
+
|
|
7
|
+
const fields = {
|
|
8
|
+
source: "笔记库 ID,从 list_memo_sources 获取", path: "库内相对文件路径", folder: "库内目录,默认根目录",
|
|
9
|
+
cursor: "上一页返回的分页标记", query: "文件名、路径或正文关键词", version: "read_memo 返回的最新版本",
|
|
10
|
+
text: "要新建、追加或替换成的文本", original: "要替换的唯一原文(精确匹配)",
|
|
11
|
+
};
|
|
12
|
+
type Field = keyof typeof fields;
|
|
13
|
+
function tool(name: string, description: string, required: Field[], optional: Field[] = [], write = false) {
|
|
14
|
+
const properties = Object.fromEntries([...required, ...optional].map(f => [f, { type: "string", description: fields[f] }]));
|
|
15
|
+
return { name, description: write ? writeIntent + description : description, inputSchema: { type: "object", properties, required, additionalProperties: false },
|
|
16
|
+
annotations: { readOnlyHint: !write, openWorldHint: true } };
|
|
17
|
+
}
|
|
18
|
+
export const MEMO_TOOLS = [
|
|
19
|
+
tool("list_memo_sources", "用户想留下笔记、清单或排障经验时,用此工具找到合适的读写库,再搜索、更新或新建笔记。库名、文件名和正文可以由你根据对话确定。", []),
|
|
20
|
+
tool("list_memo_files", "浏览笔记库中的一层目录。", ["source"], ["folder", "cursor"]),
|
|
21
|
+
tool("search_memos", "在授权笔记库的全文索引里搜索文件名和正文,返回路径和片段。success 为零表示首次索引未完成。", ["query"], ["source", "folder"]),
|
|
22
|
+
tool("read_memo", "从远端读取备忘录的最新正文和版本号。", ["source", "path"]),
|
|
23
|
+
tool("create_memo", "在已有目录中新建文本备忘录,同名文件会返回冲突。", ["source", "path", "text"], [], true),
|
|
24
|
+
tool("append_memo", "追加文本(自行包含所需换行)。先读取最新版本。", ["source", "path", "version", "text"], [], true),
|
|
25
|
+
tool("edit_memo", "精确替换一处唯一原文,先读取笔记。遇到版本冲突请重新读取并核对修改。", ["source", "path", "version", "original", "text"], [], true),
|
|
26
|
+
];
|
package/src/mcp/tasks-server.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MEMO_TOOLS } from "./memo-tools.js";
|
|
1
2
|
// 定时任务工具的 MCP 服务(stdio):给 agent 用的 list / create / update / delete。
|
|
2
3
|
// 环境变量 AGENT_LOFT_TOOLSET=skills 时换成技能工具(list / get / create / update_skill),走同一条通道。
|
|
3
4
|
//
|
|
@@ -227,8 +228,8 @@ const SKILL_TOOLS = [
|
|
|
227
228
|
},
|
|
228
229
|
];
|
|
229
230
|
|
|
230
|
-
const
|
|
231
|
-
const ACTIVE =
|
|
231
|
+
const toolset = process.env.AGENT_LOFT_TOOLSET;
|
|
232
|
+
const ACTIVE = toolset === "memos" ? MEMO_TOOLS : toolset === "skills" ? SKILL_TOOLS : TOOLS;
|
|
232
233
|
|
|
233
234
|
function text(t: string, isError = false) {
|
|
234
235
|
return { content: [{ type: "text", text: t }], ...(isError ? { isError: true } : {}) };
|
|
@@ -238,7 +239,7 @@ function text(t: string, isError = false) {
|
|
|
238
239
|
function forward(tool: string, args: Record<string, unknown>): Promise<{ text: string; isError?: boolean }> {
|
|
239
240
|
return new Promise((resolve) => {
|
|
240
241
|
if (!socketPath || !caller.user || !caller.conv) {
|
|
241
|
-
resolve({ text:
|
|
242
|
+
resolve({ text: `内置工具没有正确配置(缺少用户或对话)。`, isError: true });
|
|
242
243
|
return;
|
|
243
244
|
}
|
|
244
245
|
const sock = net.createConnection(socketPath);
|
|
@@ -274,7 +275,7 @@ async function handle(req: Request): Promise<unknown> {
|
|
|
274
275
|
return {
|
|
275
276
|
protocolVersion: typeof req.params?.protocolVersion === "string" ? req.params.protocolVersion : "2025-06-18",
|
|
276
277
|
capabilities: { tools: {} },
|
|
277
|
-
serverInfo: { name:
|
|
278
|
+
serverInfo: { name: `agent-loft-${toolset ?? "tasks"}`, version: "1.0.0" },
|
|
278
279
|
};
|
|
279
280
|
case "ping":
|
|
280
281
|
return {};
|
package/web/settings/users.js
CHANGED
|
@@ -348,28 +348,26 @@ export async function systemTab(cfg, page) {
|
|
|
348
348
|
const render = () => {
|
|
349
349
|
const recheck = h("button", { class: "btn small", disabled: checking }, checking ? "检查中…" : "检查更新");
|
|
350
350
|
recheck.onclick = () => void checkVersion(true);
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
const upgradeBtn = v?.updateAvailable && v.canUpgrade && !checking ? h("button", { class: "btn small primary" }, `升级到 ${v.latest}`) : null;
|
|
351
|
+
let status = null;
|
|
352
|
+
if (checking) status = h("span", { class: "status-tag" }, "正在检查更新…");
|
|
353
|
+
else if (v?.upgrading) status = h("span", { class: "status-tag" }, "正在下载并检查新版…");
|
|
354
|
+
else if (v?.pendingVersion) {
|
|
355
|
+
const state = v.version === v.pendingVersion ? "正在验证" : "等待重启验证";
|
|
356
|
+
status = h("span", { class: "status-tag" }, `${v.pendingVersion} ${state}`);
|
|
357
|
+
} else if (v?.updateAvailable) status = h("span", { class: "status-tag ok" }, `有新版本 ${v.latest}`);
|
|
358
|
+
else if (v?.latest) status = h("span", { class: "status-tag" }, "已是最新版本");
|
|
359
|
+
else if (v) status = h("span", { class: "status-tag bad", title: v.latestError ?? "" }, "查不到最新版本");
|
|
360
|
+
const upgradeBtn = v?.updateAvailable && v.canUpgrade && !v.upgrading && !v.pendingVersion && !checking ? h("button", { class: "btn small primary" }, `升级到 ${v.latest}`) : null;
|
|
361
361
|
if (upgradeBtn) {
|
|
362
362
|
upgradeBtn.onclick = async () => {
|
|
363
363
|
upgradeBtn.disabled = true;
|
|
364
364
|
upgradeBtn.textContent = "正在升级…";
|
|
365
365
|
try {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
void checkVersion(false);
|
|
366
|
+
await api("/api/server/upgrade?background=1", { method: "POST" });
|
|
367
|
+
await followUpgrade(v.latest);
|
|
369
368
|
} catch (err) {
|
|
370
369
|
toast(`升级失败:${err.message}`, 6000);
|
|
371
|
-
|
|
372
|
-
upgradeBtn.textContent = `升级到 ${v.latest}`;
|
|
370
|
+
void checkVersion(false);
|
|
373
371
|
}
|
|
374
372
|
};
|
|
375
373
|
}
|
|
@@ -378,6 +376,7 @@ export async function systemTab(cfg, page) {
|
|
|
378
376
|
h("h4", {}, "服务端"),
|
|
379
377
|
h("div", { class: "row" }, h("b", {}, `agent-loft ${v?.version ?? sys.version}`), status),
|
|
380
378
|
h("div", { class: "muted" }, `Node ${sys.node} · 已运行 ${uptime(sys.uptimeMs)} · ${sys.underSystemd ? "以系统服务运行" : "前台运行(重启需要在终端里操作)"}`),
|
|
379
|
+
v?.upgradeError && !v.pendingVersion ? h("div", { class: "muted" }, v.upgradeError) : null,
|
|
381
380
|
v && !checking && !v.latest && v.latestError ? h("div", { class: "muted" }, v.latestError) : null,
|
|
382
381
|
v?.updateAvailable && !v.canUpgrade && v.reason ? h("div", { class: "muted" }, v.reason) : null,
|
|
383
382
|
v?.updateAvailable && v.canUpgrade && !v.service ? h("div", { class: "muted" }, "agent-loft 是在前台手动运行的,升级后要自己重启一下。") : null,
|
|
@@ -404,6 +403,32 @@ export async function systemTab(cfg, page) {
|
|
|
404
403
|
checking = false;
|
|
405
404
|
if (sys) render();
|
|
406
405
|
};
|
|
406
|
+
async function followUpgrade(target) {
|
|
407
|
+
let disconnectedAt = 0;
|
|
408
|
+
while (sysBox.isConnected) {
|
|
409
|
+
let progress;
|
|
410
|
+
try {
|
|
411
|
+
progress = await api("/api/server/upgrade");
|
|
412
|
+
disconnectedAt = 0;
|
|
413
|
+
} catch (err) {
|
|
414
|
+
if (err.status && err.status < 500) throw err;
|
|
415
|
+
// 服务重启时反向代理可能短暂返回 502,继续等新进程报告结果。
|
|
416
|
+
disconnectedAt ||= Date.now();
|
|
417
|
+
if (Date.now() - disconnectedAt > 150_000) {
|
|
418
|
+
toast("暂时连不上服务,恢复连接后可在设置里查看升级结果", 6000);
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
if (progress?.error) throw new Error(progress.error);
|
|
423
|
+
if (progress && !progress.upgrading && (!progress.pendingVersion || !v?.service)) {
|
|
424
|
+
if (progress.pendingVersion) toast("新版已准备好,重启 agent-loft 后验证并启用", 6000);
|
|
425
|
+
else if (progress.version === target) toast(`已验证并启用 ${target}`, 6000);
|
|
426
|
+
await checkVersion(false);
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
430
|
+
}
|
|
431
|
+
}
|
|
407
432
|
const versionDone = checkVersion(false);
|
|
408
433
|
try {
|
|
409
434
|
sys = await api("/api/system");
|
|
@@ -413,6 +438,7 @@ export async function systemTab(cfg, page) {
|
|
|
413
438
|
}
|
|
414
439
|
render();
|
|
415
440
|
await versionDone;
|
|
441
|
+
if (v?.upgrading || (v?.pendingVersion && v.service)) void followUpgrade(v.pendingVersion || v.latest).catch(err => { toast(err.message); void checkVersion(false); });
|
|
416
442
|
}
|
|
417
443
|
|
|
418
444
|
/** 运行了多久:分钟 / 小时 / 天 */
|
|
@@ -2,6 +2,53 @@ import { h } from "../lib/dom.js";
|
|
|
2
2
|
import { modal } from "../lib/overlay.js";
|
|
3
3
|
import { api } from "../lib/api.js";
|
|
4
4
|
|
|
5
|
+
function memoAccessField(value = "off") {
|
|
6
|
+
const select = h("select", { class: "input", name: "access", "aria-label": "智能体访问" },
|
|
7
|
+
h("option", { value: "off" }, "关闭"), h("option", { value: "read" }, "只读"), h("option", { value: "write" }, "读写"));
|
|
8
|
+
select.value = value;
|
|
9
|
+
const hint = h("small", {}, "授权后,Agent Loft 会在服务器建立本库的全文索引。");
|
|
10
|
+
const update = () => { hint.hidden = select.value === "off"; };
|
|
11
|
+
select.addEventListener("change", update);
|
|
12
|
+
update();
|
|
13
|
+
return { select, field: h("label", {}, "智能体访问", select, hint) };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function memoAccessSettings(source) {
|
|
17
|
+
const { field, select } = memoAccessField(source.access);
|
|
18
|
+
const status = h("p", { role: "status" });
|
|
19
|
+
const refresh = h("button", { class: "btn", type: "button" }, "立即刷新索引");
|
|
20
|
+
const save = h("button", { class: "btn primary", type: "submit" }, "保存权限");
|
|
21
|
+
const form = h("form", { class: "modal-body memo-fields" }, field, status, refresh, save);
|
|
22
|
+
let timer, sequence = 0;
|
|
23
|
+
const url = action => "/api/memos/" + action + "?" + new URLSearchParams({ source: source.id });
|
|
24
|
+
async function load(method = "GET") {
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
const request = ++sequence;
|
|
27
|
+
refresh.hidden = source.access === "off" || !source.access;
|
|
28
|
+
if (refresh.hidden) { status.textContent = ""; return; }
|
|
29
|
+
try {
|
|
30
|
+
const index = await api(url("index"), { method });
|
|
31
|
+
if (!form.isConnected || sequence !== request) return;
|
|
32
|
+
const last = index.success ? "最近成功:" + new Date(index.success).toLocaleString() : "首次索引尚未完成";
|
|
33
|
+
status.textContent = [index.running ? "正在扫描,已检查 " + index.scanned + " 份文本" : "", index.error, last].filter(Boolean).join(" · ");
|
|
34
|
+
refresh.disabled = index.running;
|
|
35
|
+
if (index.running) timer = setTimeout(load, 1500);
|
|
36
|
+
} catch (err) { if (sequence === request) status.textContent = err.message; }
|
|
37
|
+
}
|
|
38
|
+
modal({ title: source.name + " · 智能体访问", body: form, beforeClose: () => { if (save.disabled) return false; clearTimeout(timer); ++sequence; } });
|
|
39
|
+
refresh.onclick = () => void load("POST");
|
|
40
|
+
form.onsubmit = async e => {
|
|
41
|
+
e.preventDefault();
|
|
42
|
+
save.disabled = true;
|
|
43
|
+
try {
|
|
44
|
+
Object.assign(source, await api(url("access"), { method: "PUT", body: { access: select.value } }));
|
|
45
|
+
await load();
|
|
46
|
+
} catch (err) { status.textContent = err.message; }
|
|
47
|
+
finally { save.disabled = false; }
|
|
48
|
+
};
|
|
49
|
+
void load();
|
|
50
|
+
}
|
|
51
|
+
|
|
5
52
|
export function connectSource(onConnect) {
|
|
6
53
|
const kind = h("select", { class: "input" }, h("option", { value: "webdav" }, "WebDAV"), h("option", { value: "s3" }, "S3 / 兼容存储"));
|
|
7
54
|
const input = (name, placeholder = "", type = "text") => h("input", { class: "input", name, placeholder, type, autocomplete: "off" });
|
|
@@ -25,7 +72,7 @@ export function connectSource(onConnect) {
|
|
|
25
72
|
name.required = true;
|
|
26
73
|
const error = h("div", { class: "memo-error", role: "alert" });
|
|
27
74
|
const submit = h("button", { class: "btn primary", type: "submit" }, "连接笔记库");
|
|
28
|
-
const form = h("form", { class: "modal-body memo-fields" }, label("名称", name), label("数据源", kind), label("服务地址", endpoint), s3, label(userLabel, username), label(secretLabel, secret), error, submit);
|
|
75
|
+
const form = h("form", { class: "modal-body memo-fields" }, label("名称", name), label("数据源", kind), label("服务地址", endpoint), s3, label(userLabel, username), label(secretLabel, secret), memoAccessField().field, error, submit);
|
|
29
76
|
const close = modal({ title: "连接笔记库", body: form, beforeClose: () => !submit.disabled });
|
|
30
77
|
form.onsubmit = async e => {
|
|
31
78
|
e.preventDefault();
|
package/web/views/memos.js
CHANGED
|
@@ -5,7 +5,7 @@ import { confirmDialog } from "../lib/overlay.js";
|
|
|
5
5
|
import { WS } from "../lib/ws.js";
|
|
6
6
|
import { S, setContentHash, ui } from "../state.js";
|
|
7
7
|
import { createEditor } from "./file-editor.js";
|
|
8
|
-
import { askMemoName, chooseMemoVersion, connectSource } from "./memo-dialogs.js";
|
|
8
|
+
import { askMemoName, chooseMemoVersion, connectSource, memoAccessSettings } from "./memo-dialogs.js";
|
|
9
9
|
import { closeSide, renderSidebar } from "./sidebar.js";
|
|
10
10
|
import { renderTopbar } from "./topbar.js";
|
|
11
11
|
|
|
@@ -478,7 +478,7 @@ function notebook() {
|
|
|
478
478
|
refreshTree();
|
|
479
479
|
});
|
|
480
480
|
}, true);
|
|
481
|
-
fill(side, h("div", { class: "memo-sources" }, choose, button("连接其他笔记库", addSource, "plus"), button("断开笔记库", () => run(disconnect), "unplug")),
|
|
481
|
+
fill(side, h("div", { class: "memo-sources" }, choose, button("智能体访问", () => memoAccessSettings(source), "gear"), button("连接其他笔记库", addSource, "plus"), button("断开笔记库", () => run(disconnect), "unplug")),
|
|
482
482
|
h("div", { class: "memo-tree-tools" }, button("新建笔记", () => run(() => create(false))), button("新建文件夹", () => run(() => create(true)), "folder"), button("刷新目录", refreshTree, "refresh"), h("button", { class: "btn memo-return", onclick: () => root.classList.remove("show-tree") }, "返回文件")), tree);
|
|
483
483
|
fill(root, side, pane);
|
|
484
484
|
async function init() {
|