agent-loft 0.2.48 → 0.2.50
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 +32 -15
- package/package.json +1 -1
- package/src/agents/claude/driver.ts +1 -1
- package/src/agents/claude/session.ts +140 -47
- package/src/agents/codex/driver.ts +1 -1
- package/src/agents/codex/session.ts +90 -58
- package/src/channels/feishu/bot.ts +1 -3
- package/src/channels/feishu/cards.ts +2 -2
- package/src/channels/web/api.ts +49 -19
- package/src/channels/web/conv-pages.ts +37 -0
- package/src/channels/web/experts-api.ts +203 -0
- package/src/channels/web/skills-api.ts +1 -7
- package/src/channels/web/tasks-api.ts +9 -5
- package/src/core/command-catalog.ts +4 -6
- package/src/core/commands.ts +6 -11
- package/src/core/expert-prompt.ts +119 -0
- package/src/core/experts.ts +438 -0
- package/src/core/hub.ts +137 -102
- package/src/core/mcp.ts +253 -0
- package/src/core/memory-curator.ts +14 -19
- package/src/core/memory.ts +11 -4
- package/src/core/personal.ts +5 -1
- package/src/core/skill-runtime.ts +11 -114
- package/src/core/store.ts +30 -11
- package/src/core/task-prompt.ts +1 -1
- package/src/core/task-runner.ts +24 -11
- package/src/core/tasks-bridge.ts +7 -7
- package/src/core/tasks.ts +49 -14
- package/src/core/types.ts +4 -7
- package/src/core/uploads.ts +7 -7
- package/src/mcp/tasks-server.ts +1 -2
- package/web/css/experts.css +225 -0
- package/web/css/messages.css +43 -0
- package/web/css/overlay.css +12 -0
- package/web/css/search.css +214 -0
- package/web/css/sidebar.css +79 -56
- package/web/css/skills.css +0 -11
- package/web/css/topbar.css +135 -5
- package/web/index.html +2 -0
- package/web/lib/dom.js +6 -14
- package/web/settings/experts.js +622 -0
- package/web/settings/index.js +3 -0
- package/web/settings/memory.js +23 -7
- package/web/settings/skills.js +3 -3
- package/web/state.js +7 -2
- package/web/views/composer.js +31 -29
- package/web/views/conv-actions.js +8 -9
- package/web/views/conv-search.js +206 -0
- package/web/views/experts.js +74 -0
- package/web/views/layout.js +9 -4
- package/web/views/messages.js +44 -11
- package/web/views/quota.js +13 -10
- package/web/views/sidebar.js +71 -19
- package/web/views/tasks.js +21 -36
- package/web/views/topbar.js +148 -74
- package/src/core/chat-mode.ts +0 -62
- package/web/views/mention.js +0 -202
package/web/views/topbar.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
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
4
|
import { desktop, desktopMenu, desktopSettings, S, ui } from "../state.js";
|
|
5
5
|
import { agentLabel, convTitle, openSide } from "./sidebar.js";
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { convExpert, expertChoices, expertIcon, findExpert } from "./experts.js";
|
|
7
|
+
import { closeMenu, modal, placePopover, showMenu, showPopover } from "../lib/overlay.js";
|
|
8
|
+
import { openConversation } from "./messages.js";
|
|
8
9
|
import { openSettings } from "./layout.js";
|
|
9
10
|
import { openSchedule } from "./schedule.js";
|
|
10
11
|
import { changeCwd, deleteConv, doAction, forkConversation, openModelPicker, renameConv, switchAgent } from "./conv-actions.js";
|
|
@@ -24,8 +25,8 @@ export function renderTopbar() {
|
|
|
24
25
|
? h("span", { class: "conv-name" }, tasks ? "定时任务" : "远程控制")
|
|
25
26
|
: c
|
|
26
27
|
? [
|
|
27
|
-
//
|
|
28
|
-
c
|
|
28
|
+
// 标题前面是专家的头像:点一下或鼠标停上去,看专家的信息和工作目录
|
|
29
|
+
expertAvatarButton(c),
|
|
29
30
|
h("span", { class: "conv-name", title: convTitle(c) }, convTitle(c)),
|
|
30
31
|
]
|
|
31
32
|
: null,
|
|
@@ -37,16 +38,120 @@ export function renderTopbar() {
|
|
|
37
38
|
syncDesktopMenu();
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
// ---------- 顶栏的专家头像和信息卡 ----------
|
|
42
|
+
|
|
43
|
+
const ACCESS_LABEL = { web: "只能联网", read: "能读文件", edit: "能改文件", exec: "能跑命令" };
|
|
44
|
+
const ENGINE_LABEL = { claude: "Claude", codex: "Codex" };
|
|
45
|
+
/** 鼠标停多久才弹、离开多久才收(从头像挪到卡片上的那一下不能收) */
|
|
46
|
+
const HOVER_OPEN_MS = 250;
|
|
47
|
+
const HOVER_CLOSE_MS = 200;
|
|
48
|
+
const hoverCard = { timer: 0, pinned: false };
|
|
49
|
+
|
|
50
|
+
function expertAvatarButton(c) {
|
|
51
|
+
const btn = h(
|
|
52
|
+
"button",
|
|
53
|
+
{
|
|
54
|
+
class: "conv-expert-icon",
|
|
55
|
+
"aria-label": `专家:${c.expert?.name ?? agentLabel(c.agent)}`,
|
|
56
|
+
onclick: () => {
|
|
57
|
+
clearTimeout(hoverCard.timer);
|
|
58
|
+
// 已经是悬停弹出来的:点一下就钉住,不随鼠标离开收起
|
|
59
|
+
if (ui.menu?.dataset.for === "expert-info" && !hoverCard.pinned) {
|
|
60
|
+
hoverCard.pinned = true;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (ui.menu?.dataset.for === "expert-info") return closeMenu();
|
|
64
|
+
openExpertCard(btn, c.key, true);
|
|
65
|
+
},
|
|
66
|
+
onpointerenter: (e) => {
|
|
67
|
+
if (e.pointerType !== "mouse") return;
|
|
68
|
+
clearTimeout(hoverCard.timer);
|
|
69
|
+
if (ui.menu?.dataset.for === "expert-info") return;
|
|
70
|
+
hoverCard.timer = setTimeout(() => openExpertCard(btn, c.key, false), HOVER_OPEN_MS);
|
|
71
|
+
},
|
|
72
|
+
onpointerleave: (e) => {
|
|
73
|
+
if (e.pointerType === "mouse") scheduleCardClose();
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
expertIcon(convExpert(c)),
|
|
77
|
+
);
|
|
78
|
+
return btn;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function scheduleCardClose() {
|
|
82
|
+
clearTimeout(hoverCard.timer);
|
|
83
|
+
hoverCard.timer = setTimeout(() => {
|
|
84
|
+
if (!hoverCard.pinned && ui.menu?.dataset.for === "expert-info") closeMenu();
|
|
85
|
+
}, HOVER_CLOSE_MS);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** 专家信息卡:头像、名字、说明,引擎、模型、权限,当前工作目录(能读文件的才有) */
|
|
89
|
+
function openExpertCard(anchor, key, pinned) {
|
|
90
|
+
const c = S.convs.get(key);
|
|
91
|
+
if (!c || !anchor.isConnected) return;
|
|
92
|
+
const pop = showPopover(anchor, { place: "below", align: "left" });
|
|
93
|
+
pop.dataset.for = "expert-info";
|
|
94
|
+
pop.classList.add("expert-card");
|
|
95
|
+
hoverCard.pinned = pinned;
|
|
96
|
+
pop.onpointerenter = () => clearTimeout(hoverCard.timer);
|
|
97
|
+
pop.onpointerleave = (e) => {
|
|
98
|
+
if (e.pointerType === "mouse") scheduleCardClose();
|
|
99
|
+
};
|
|
100
|
+
const badge = convExpert(c);
|
|
101
|
+
// 自己的专家:说明、联网这些在 S.experts 里(别人的专家只有徽章上那点)
|
|
102
|
+
const mine = badge.builtin ? null : S.experts.find((e) => e.id === badge.id);
|
|
103
|
+
const access = badge.builtin ? null : (mine?.access ?? badge.access);
|
|
104
|
+
const abilities = badge.builtin
|
|
105
|
+
? "原装的样子:所有工具"
|
|
106
|
+
: [
|
|
107
|
+
mine?.webSearch !== false ? "联网搜索" : null,
|
|
108
|
+
mine?.webFetch !== false && c.agent === "claude" ? "读网页" : null,
|
|
109
|
+
access && access !== "web" ? ACCESS_LABEL[access]?.replace(/^能/, "") : null,
|
|
110
|
+
]
|
|
111
|
+
.filter(Boolean)
|
|
112
|
+
.join("、") || "只能聊天";
|
|
113
|
+
const model = modelName(c.agent, c.runtimeModel || c.model);
|
|
114
|
+
const effort = c.runtimeEffort || c.effort;
|
|
115
|
+
const row = (label, ...value) => h("div", { class: "ec-row" }, h("span", { class: "ec-label" }, label), h("span", { class: "ec-value" }, ...value));
|
|
116
|
+
fill(
|
|
117
|
+
pop,
|
|
118
|
+
h(
|
|
119
|
+
"div",
|
|
120
|
+
{ class: "ec-head" },
|
|
121
|
+
expertIcon(badge, "ec-avatar"),
|
|
122
|
+
h(
|
|
123
|
+
"div",
|
|
124
|
+
{ class: "ec-name" },
|
|
125
|
+
h("b", {}, badge.name),
|
|
126
|
+
h("span", { class: "ec-sub" }, badge.builtin ? "内置专家" : badge.missing ? "这个专家已经删除了" : `自定义专家 · ${ENGINE_LABEL[c.agent] ?? c.agent}`),
|
|
127
|
+
),
|
|
128
|
+
),
|
|
129
|
+
mine?.description ? h("div", { class: "ec-desc" }, mine.description) : null,
|
|
130
|
+
h(
|
|
131
|
+
"div",
|
|
132
|
+
{ class: "ec-rows" },
|
|
133
|
+
row("引擎", agentLabel(c.agent)),
|
|
134
|
+
row("模型", [model ?? "默认", effort ? ` · ${effort}` : ""].join("")),
|
|
135
|
+
row("能力", abilities),
|
|
136
|
+
access === "web"
|
|
137
|
+
? null
|
|
138
|
+
: row(
|
|
139
|
+
"工作目录",
|
|
140
|
+
h("span", { class: "ec-cwd", title: c.cwd }, c.cwd),
|
|
141
|
+
h("button", { class: "ec-link", onclick: () => (closeMenu(), changeCwd()) }, "切换"),
|
|
142
|
+
),
|
|
143
|
+
),
|
|
144
|
+
);
|
|
145
|
+
placePopover(pop, anchor);
|
|
146
|
+
}
|
|
147
|
+
|
|
40
148
|
/**
|
|
41
|
-
*
|
|
149
|
+
* 右上角「更多」里的项。对话页(含新对话)有定时发送;设置哪个页面都有,
|
|
42
150
|
* 桌面端自己的「更多」里已经有每台服务器的设置时不放
|
|
43
151
|
*/
|
|
44
152
|
function topMenuItems() {
|
|
45
153
|
const items = [];
|
|
46
154
|
if (!S.view) {
|
|
47
|
-
const c = S.current ? S.convs.get(S.current) : null;
|
|
48
|
-
const chat = (c ? (c.mode ?? "work") : S.draftMode) === "chat";
|
|
49
|
-
items.push({ id: "mode", label: "聊天模式", icon: "chat", checked: chat, onclick: () => void toggleMode() });
|
|
50
155
|
items.push({ id: "schedule", label: "定时发送", icon: "clock", onclick: () => openSchedule() });
|
|
51
156
|
}
|
|
52
157
|
if (!desktopSettings) {
|
|
@@ -197,31 +302,36 @@ export function usableAgents() {
|
|
|
197
302
|
return usable.length ? usable : enabled;
|
|
198
303
|
}
|
|
199
304
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
if (
|
|
203
|
-
if (ui.menu?.dataset.for === "agent") return closeMenu();
|
|
305
|
+
/** 新对话里选专家:内置的在前,再是自己的(新建、管理在「设置 → 专家」里) */
|
|
306
|
+
function pickExpertMenu(anchor) {
|
|
307
|
+
if (ui.menu?.dataset.for === "expert") return closeMenu();
|
|
204
308
|
const pop = showPopover(anchor, { align: "left" });
|
|
205
|
-
pop.dataset.for = "
|
|
309
|
+
pop.dataset.for = "expert";
|
|
310
|
+
pop.classList.add("expert-pop");
|
|
311
|
+
const choices = expertChoices();
|
|
312
|
+
const item = (e) =>
|
|
313
|
+
h(
|
|
314
|
+
"button",
|
|
315
|
+
{ class: `pop-item${e.id === S.draftExpert ? " on" : ""}`, title: e.description || "", onclick: () => (closeMenu(), switchAgent(e.id)) },
|
|
316
|
+
expertIcon(e),
|
|
317
|
+
h("span", { class: "grow" }, h("span", { class: "t" }, e.name), e.description ? h("span", { class: "s" }, e.description) : null),
|
|
318
|
+
e.id === S.draftExpert ? h("span", { class: "check" }, "✓") : null,
|
|
319
|
+
);
|
|
320
|
+
const builtin = choices.filter((e) => e.builtin);
|
|
321
|
+
const own = choices.filter((e) => !e.builtin);
|
|
206
322
|
fill(
|
|
207
323
|
pop,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
"button",
|
|
211
|
-
{ class: `pop-item${id === S.draftAgent ? " on" : ""}`, onclick: () => (closeMenu(), switchAgent(id)) },
|
|
212
|
-
agentIcon(id),
|
|
213
|
-
h("span", { class: "grow" }, agentLabel(id)),
|
|
214
|
-
id === S.draftAgent ? h("span", { class: "check" }, "✓") : null,
|
|
215
|
-
),
|
|
216
|
-
),
|
|
324
|
+
builtin.map(item),
|
|
325
|
+
own.length ? [h("div", { class: "sep" }), h("div", { class: "pop-head" }, "我的专家"), own.map(item)] : null,
|
|
217
326
|
);
|
|
218
327
|
placePopover(pop, anchor);
|
|
219
328
|
}
|
|
220
329
|
|
|
221
330
|
/** 新对话里选的模型 / 思考程度,按 agent 分开记,创建会话时带过去 */
|
|
222
331
|
S.draftPick = JSON.parse(localStorage.getItem("aim.draftPick") || "{}");
|
|
223
|
-
|
|
224
|
-
|
|
332
|
+
/** 新对话里选的模型 / 思考程度,按专家分开记(内置专家就是 claude / codex,和以前按 agent 记的对得上) */
|
|
333
|
+
export function draftPick(expert = S.draftExpert ?? S.draftAgent) {
|
|
334
|
+
return (S.draftPick[expert] ??= {});
|
|
225
335
|
}
|
|
226
336
|
export function saveDraftPick() {
|
|
227
337
|
localStorage.setItem("aim.draftPick", JSON.stringify(S.draftPick));
|
|
@@ -289,21 +399,21 @@ export function renderContext() {
|
|
|
289
399
|
if (!ui.ctxBar) return;
|
|
290
400
|
const c = S.current ? S.convs.get(S.current) : null;
|
|
291
401
|
const cwd = currentCwd();
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
? h("span", { class: "ctx-item static" }, agentIcon(agentId), h("span", { class: "label" }, agentLabel(agentId ?? "") || "无可用 agent"))
|
|
402
|
+
// 对话从头到尾一个专家:进了对话只显示;新对话里可以选
|
|
403
|
+
const expert = c ? convExpert(c) : findExpert(S.draftExpert);
|
|
404
|
+
const agentEl = c
|
|
405
|
+
? h("span", { class: "ctx-item static", title: `专家:${expert.name}${expert.missing ? "(已删除)" : ""}` }, expertIcon(expert), h("span", { class: "label" }, expert.name))
|
|
297
406
|
: h(
|
|
298
407
|
"button",
|
|
299
|
-
{ class: "ctx-item", title: "
|
|
300
|
-
|
|
301
|
-
h("span", { class: "label" },
|
|
408
|
+
{ class: "ctx-item", title: "选择专家", onclick: (e) => pickExpertMenu(e.currentTarget) },
|
|
409
|
+
expert ? expertIcon(expert) : null,
|
|
410
|
+
h("span", { class: "label" }, expert?.name ?? "无可用 agent"),
|
|
302
411
|
h("span", { class: "chev", html: ICON.down }),
|
|
303
412
|
);
|
|
304
413
|
fill(
|
|
305
414
|
ui.ctxBar,
|
|
306
|
-
|
|
415
|
+
// 只能联网的专家不碰文件,不显示工作目录
|
|
416
|
+
expert?.access === "web" ? null : h("button", { class: "ctx-item", title: `工作目录:${cwd}\n点击切换`, onclick: () => changeCwd() }, icon("folder"), h("span", { class: "label" }, baseName(cwd) || "选择目录")),
|
|
307
417
|
agentEl,
|
|
308
418
|
);
|
|
309
419
|
updateCtxVisibility();
|
|
@@ -316,9 +426,11 @@ export function renderContext() {
|
|
|
316
426
|
ensureCatalog(modelAgent);
|
|
317
427
|
const defaults = catalogDefaults.get(modelAgent) ?? {};
|
|
318
428
|
const pick = c ? null : draftPick();
|
|
319
|
-
|
|
429
|
+
// 新对话:没选过就是专家配的默认模型
|
|
430
|
+
const draft = c ? null : findExpert(S.draftExpert);
|
|
431
|
+
const id = c ? (c.runtimeModel ?? c.model) : (pick.model ?? (draft?.model || undefined));
|
|
320
432
|
model = modelName(modelAgent, id) ?? (catalogLoaded.has(modelAgent) ? "选择模型" : "…");
|
|
321
|
-
effort = (c ? (c.runtimeEffort ?? c.effort) : pick.effort) ?? defaults.effort;
|
|
433
|
+
effort = (c ? (c.runtimeEffort ?? c.effort) : (pick.effort ?? (draft?.effort || undefined))) ?? defaults.effort;
|
|
322
434
|
}
|
|
323
435
|
renderCtxRing();
|
|
324
436
|
fill(
|
|
@@ -335,44 +447,6 @@ export function renderContext() {
|
|
|
335
447
|
);
|
|
336
448
|
}
|
|
337
449
|
|
|
338
|
-
/**
|
|
339
|
-
* 「更多」里的聊天模式开关。聊天模式:精简的系统提示,只能联网搜索,不执行命令、不读写文件,每轮开销小很多;
|
|
340
|
-
* 工作模式:agent 原装的全部能力。会话里切换走服务端(同一条对话接着聊),新对话里只是记下选择
|
|
341
|
-
*/
|
|
342
|
-
async function toggleMode() {
|
|
343
|
-
const c = S.current ? S.convs.get(S.current) : null;
|
|
344
|
-
const next = (c ? (c.mode ?? "work") : S.draftMode) === "chat" ? "work" : "chat";
|
|
345
|
-
if (!c) {
|
|
346
|
-
S.draftMode = next;
|
|
347
|
-
localStorage.setItem("aim.mode", next);
|
|
348
|
-
renderTopbar();
|
|
349
|
-
toast(next === "chat" ? "新对话用聊天模式:更省、更快,只能联网搜索,不执行命令、不读写文件" : "新对话用工作模式");
|
|
350
|
-
return;
|
|
351
|
-
}
|
|
352
|
-
// 聊过的聊天对话切到工作模式:工作模式会在对话里留下一大段 Claude Code / Codex 的背景,
|
|
353
|
-
// 之后再切回聊天模式每轮还得带着它,就省不了了。先问一句,建议另开一条工作对话
|
|
354
|
-
if (next === "work" && S.items.length > 0) {
|
|
355
|
-
const choice = await confirmDialog({
|
|
356
|
-
title: "在这条对话里切到工作模式?",
|
|
357
|
-
message:
|
|
358
|
-
"切过去以后,这条对话里会留下工作模式的一大段背景(约 1.7 万 token),之后再切回聊天模式每轮也得带着,就省不了了。切换后的第一轮也会稍贵一些。\n\n要干活的话,建议新开一条工作对话(目录和 agent 跟这条一样),这条留着继续聊天。",
|
|
359
|
-
confirmText: "新开工作对话",
|
|
360
|
-
altText: "就在这里切",
|
|
361
|
-
});
|
|
362
|
-
if (choice === "alt") await doAction("mode", "work");
|
|
363
|
-
else if (choice === true) {
|
|
364
|
-
S.draftAgent = c.agent;
|
|
365
|
-
S.draftCwd = c.cwd;
|
|
366
|
-
S.draftMode = "work";
|
|
367
|
-
localStorage.setItem("aim.mode", "work");
|
|
368
|
-
newDraft();
|
|
369
|
-
toast("已新开工作对话,目录和 agent 跟原来那条一样");
|
|
370
|
-
}
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
await doAction("mode", next);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
450
|
/** 84321 → 84k,1234567 → 1.2M */
|
|
377
451
|
function fmtTokens(n) {
|
|
378
452
|
if (n >= 1e6) return `${(n / 1e6).toFixed(n >= 1e7 ? 0 : 1)}M`;
|
package/src/core/chat-mode.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// 聊天模式要告诉 agent 的话,以及怎么告诉。
|
|
2
|
-
//
|
|
3
|
-
// 两个 agent 的「换系统提示」能力不一样(都实测过):
|
|
4
|
-
// - Claude:系统提示每次拉起进程时整个给,换模式 = 带着新的系统提示 resume,干净利落。
|
|
5
|
-
// 聊天模式就用 chatSystemPrompt 换掉 Claude Code 原装的那一大段(约 1.6 万 token)。
|
|
6
|
-
// - Codex:thread 的基础指令一旦设了就钉在 thread 上(resume 时不传就沿用,传 null 也一样),
|
|
7
|
-
// 开发者指令只在 thread 开头写一次、resume 时给新的会被忽略。所以 Codex 不动它的指令,
|
|
8
|
-
// 模式和记忆用 contextBlock 附在「这个会话进程的第一条消息」后面 —— 它按时间顺序进历史,
|
|
9
|
-
// 模型以最新的为准。读历史、列会话时用 stripContext 把它剥掉。
|
|
10
|
-
//
|
|
11
|
-
// 日期写进去是因为模型不知道今天几号;它在一条会话里不变,不影响提示缓存。
|
|
12
|
-
|
|
13
|
-
function today(): string {
|
|
14
|
-
const d = new Date();
|
|
15
|
-
const pad = (n: number) => String(n).padStart(2, "0");
|
|
16
|
-
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
17
|
-
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}(${tz})`;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** search:这个 agent 里联网搜索 / 读网页的工具怎么称呼 */
|
|
21
|
-
function chatRules(search: string): string[] {
|
|
22
|
-
return [
|
|
23
|
-
"- 用用户使用的语言回答,自然、直接、简洁;需要时用 Markdown 排版(列表、表格、代码块)。",
|
|
24
|
-
`- 涉及最新信息、新闻、价格、版本、事实核查时,先用${search}查,回答里附上来源链接;不要凭记忆编造。`,
|
|
25
|
-
"- 用户发来的图片、PDF 和文本文件内容会直接附在消息里。",
|
|
26
|
-
"- 现在是「聊天模式」:你不能执行命令,也不能读写这台机器上的文件。用户要你动手干活(写代码、改文件、跑命令、操作服务器)时,告诉他发 /work 切到工作模式,同一条对话会接着聊。",
|
|
27
|
-
];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/** Claude 聊天模式的整段系统提示 */
|
|
31
|
-
export function chatSystemPrompt(search: string, memory?: string): string {
|
|
32
|
-
const base = ["你是用户的私人 AI 助手,他通过 agent-loft 的网页或飞书和你聊天。", "", ...chatRules(search), "", `今天是 ${today()}。`].join("\n");
|
|
33
|
-
return memory ? `${base}\n\n${memory}` : base;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const OPEN = "<agent-loft-context>";
|
|
37
|
-
const CLOSE = "</agent-loft-context>";
|
|
38
|
-
|
|
39
|
-
/** Codex:附在会话进程第一条消息后面的背景(模式 + 记忆 + 工作模式下的技能清单) */
|
|
40
|
-
export function contextBlock(mode: "chat" | "work", memory: string | undefined, search: string, skills: string[] = []): string {
|
|
41
|
-
const head =
|
|
42
|
-
mode === "chat"
|
|
43
|
-
? ["当前是「聊天模式」,你作为用户的私人 AI 助手和他聊天:", ...chatRules(search)]
|
|
44
|
-
: ["当前是「工作模式」:可以执行命令、读写文件,按平常的方式干活。之前如果说过「聊天模式下不能做」,那个限制已经解除。"];
|
|
45
|
-
return wrapContext([...head, ...(memory ? ["", memory] : []), ...(mode === "work" && skills.length ? ["", ...skills] : [])]);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** 包成 agent-loft 附上的背景:用户看不到(读历史、实时显示时都剥掉),agent 照着做 */
|
|
49
|
-
export function wrapContext(lines: string[]): string {
|
|
50
|
-
return [OPEN, "(这段是 agent-loft 自动附上的背景,不是用户写的。照着做,但不要在回复里提到它。)", "", ...lines, CLOSE].join("\n");
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** 剥掉 contextBlock(读历史、列会话标题时用)。一条消息里可能有好几段(定时任务的说明 + Codex 的模式说明) */
|
|
54
|
-
export function stripContext(text: string): string {
|
|
55
|
-
let out = text;
|
|
56
|
-
for (let start = out.indexOf(OPEN); start >= 0; start = out.indexOf(OPEN)) {
|
|
57
|
-
const end = out.indexOf(CLOSE, start);
|
|
58
|
-
const rest = end < 0 ? "" : out.slice(end + CLOSE.length);
|
|
59
|
-
out = `${out.slice(0, start)}${rest}`;
|
|
60
|
-
}
|
|
61
|
-
return out === text ? text : out.trim();
|
|
62
|
-
}
|
package/web/views/mention.js
DELETED
|
@@ -1,202 +0,0 @@
|
|
|
1
|
-
import { fill, h } from "../lib/dom.js";
|
|
2
|
-
import { api } from "../lib/api.js";
|
|
3
|
-
|
|
4
|
-
// ---------- @ 技能补全 ----------
|
|
5
|
-
//
|
|
6
|
-
// 光标前面是「@xxx」(@ 在开头或者前面是空白、标点)时弹出能用的技能,选中后补成「@name 」。
|
|
7
|
-
// 输入框和定时任务的「要做的事」共用:发出去之后由服务端认消息里的 @name(skill-runtime.ts),
|
|
8
|
-
// 前端只负责补全,不用另外传选了哪些。
|
|
9
|
-
// 列表缓存半分钟;技能有变化时服务端推 { t: "skills" },缓存作废。
|
|
10
|
-
|
|
11
|
-
let cache = null;
|
|
12
|
-
document.addEventListener("aim:skills", () => (cache = null));
|
|
13
|
-
|
|
14
|
-
async function loadSkills() {
|
|
15
|
-
if (cache && Date.now() - cache.at < 30_000) return cache.list;
|
|
16
|
-
const data = await api("/api/skills/mention");
|
|
17
|
-
cache = { at: Date.now(), list: data.skills };
|
|
18
|
-
return data.skills;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** 光标前的 @ 片段:返回 { start, query },不在 @ 里就是 null */
|
|
22
|
-
function mentionAt(input) {
|
|
23
|
-
const pos = input.selectionStart ?? input.value.length;
|
|
24
|
-
if (pos !== (input.selectionEnd ?? pos)) return null;
|
|
25
|
-
const m = /(^|[\s((,。,.!!??::;;"'“”‘’、])@([a-z0-9_-]*)$/i.exec(input.value.slice(0, pos));
|
|
26
|
-
if (!m) return null;
|
|
27
|
-
return { start: pos - m[2].length - 1, query: m[2].toLowerCase() };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function match(list, q) {
|
|
31
|
-
if (!q) return list;
|
|
32
|
-
const tiers = [[], [], []];
|
|
33
|
-
for (const s of list) {
|
|
34
|
-
if (s.name.startsWith(q)) tiers[0].push(s);
|
|
35
|
-
else if (s.name.includes(q)) tiers[1].push(s);
|
|
36
|
-
else if (s.description.toLowerCase().includes(q)) tiers[2].push(s);
|
|
37
|
-
}
|
|
38
|
-
return tiers.flat();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 给一个输入框挂上 @ 补全。menu 是放菜单的元素(调用方决定放哪、怎么定位,样式用 .slash-menu)。
|
|
43
|
-
* onEmpty:一个技能都没有时,菜单里给个入口(去设置里的技能页)。onShown:菜单关着时弹出来、有了内容那一下。返回 { update, keydown, close }:
|
|
44
|
-
* 输入框的 input / keydown / blur 里分别调用。
|
|
45
|
-
*/
|
|
46
|
-
export function attachMention(input, menu, { onEmpty, onPicked, onShown } = {}) {
|
|
47
|
-
const st = { open: false, shown: false, items: [], index: 0, token: 0, at: null };
|
|
48
|
-
|
|
49
|
-
const shown = () => {
|
|
50
|
-
if (st.shown) return;
|
|
51
|
-
st.shown = true;
|
|
52
|
-
onShown?.();
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
const close = () => {
|
|
56
|
-
st.open = false;
|
|
57
|
-
st.shown = false;
|
|
58
|
-
st.items = [];
|
|
59
|
-
st.index = 0;
|
|
60
|
-
st.token++;
|
|
61
|
-
menu.hidden = true;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
const highlight = () => {
|
|
65
|
-
menu.querySelectorAll(".slash-item").forEach((el, i) => el.classList.toggle("on", i === st.index));
|
|
66
|
-
menu.querySelector(".slash-item.on")?.scrollIntoView({ block: "nearest" });
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
const pick = (s) => {
|
|
70
|
-
const at = mentionAt(input);
|
|
71
|
-
if (!at) return close();
|
|
72
|
-
const before = input.value.slice(0, at.start);
|
|
73
|
-
const after = input.value.slice(input.selectionStart);
|
|
74
|
-
const insert = `@${s.name}${after.startsWith(" ") ? "" : " "}`;
|
|
75
|
-
input.value = `${before}${insert}${after}`;
|
|
76
|
-
const caret = before.length + insert.length;
|
|
77
|
-
input.setSelectionRange(caret, caret);
|
|
78
|
-
close();
|
|
79
|
-
input.dispatchEvent(new Event("input", { bubbles: true }));
|
|
80
|
-
input.focus();
|
|
81
|
-
onPicked?.(s);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const render = (items, note) => {
|
|
85
|
-
st.open = true;
|
|
86
|
-
menu.hidden = false;
|
|
87
|
-
if (!items) {
|
|
88
|
-
fill(menu, h("div", { class: "slash-empty" }, note ?? h("span", { class: "spinner" })));
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
fill(
|
|
92
|
-
menu,
|
|
93
|
-
items.map((s, i) =>
|
|
94
|
-
h(
|
|
95
|
-
"button",
|
|
96
|
-
{
|
|
97
|
-
class: `slash-item${i === st.index ? " on" : ""}`,
|
|
98
|
-
type: "button",
|
|
99
|
-
onmousedown: (e) => {
|
|
100
|
-
e.preventDefault();
|
|
101
|
-
pick(s);
|
|
102
|
-
},
|
|
103
|
-
onmousemove: () => {
|
|
104
|
-
if (st.index !== i) {
|
|
105
|
-
st.index = i;
|
|
106
|
-
highlight();
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
},
|
|
110
|
-
h(
|
|
111
|
-
"div",
|
|
112
|
-
{ class: "slash-head" },
|
|
113
|
-
h("span", { class: "slash-name" }, `@${s.name}`),
|
|
114
|
-
s.kind === "machine" ? h("span", { class: "badge" }, "本机") : null,
|
|
115
|
-
),
|
|
116
|
-
h("div", { class: "slash-desc" }, s.description || "(无说明)"),
|
|
117
|
-
),
|
|
118
|
-
),
|
|
119
|
-
);
|
|
120
|
-
highlight();
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const update = async () => {
|
|
124
|
-
const at = mentionAt(input);
|
|
125
|
-
if (!at) return close();
|
|
126
|
-
const token = ++st.token;
|
|
127
|
-
if (!st.open) render(null);
|
|
128
|
-
let list;
|
|
129
|
-
try {
|
|
130
|
-
list = await loadSkills();
|
|
131
|
-
} catch (err) {
|
|
132
|
-
if (token === st.token) render(null, `读取技能失败:${err.message}`);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
const now = mentionAt(input);
|
|
136
|
-
if (token !== st.token || !now) return;
|
|
137
|
-
if (list.length === 0) {
|
|
138
|
-
if (now.query || !onEmpty) return close();
|
|
139
|
-
st.items = [];
|
|
140
|
-
st.open = true;
|
|
141
|
-
menu.hidden = false;
|
|
142
|
-
fill(
|
|
143
|
-
menu,
|
|
144
|
-
h(
|
|
145
|
-
"button",
|
|
146
|
-
{
|
|
147
|
-
class: "slash-item on",
|
|
148
|
-
type: "button",
|
|
149
|
-
onmousedown: (e) => {
|
|
150
|
-
e.preventDefault();
|
|
151
|
-
close();
|
|
152
|
-
onEmpty();
|
|
153
|
-
},
|
|
154
|
-
},
|
|
155
|
-
h("div", { class: "slash-head" }, h("span", { class: "slash-name" }, "还没有技能")),
|
|
156
|
-
h("div", { class: "slash-desc" }, "去「设置 → 技能」新建、上传"),
|
|
157
|
-
),
|
|
158
|
-
);
|
|
159
|
-
shown();
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
const items = match(list, now.query);
|
|
163
|
-
if (items.length === 0) return close();
|
|
164
|
-
st.items = items;
|
|
165
|
-
st.index = Math.min(st.index, items.length - 1);
|
|
166
|
-
render(items);
|
|
167
|
-
shown();
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
/** 菜单开着时接管方向键 / Tab / 回车 / Esc;返回 true 表示已处理 */
|
|
171
|
-
const keydown = (e) => {
|
|
172
|
-
if (!st.open) return false;
|
|
173
|
-
if (e.isComposing) return false;
|
|
174
|
-
if (e.key === "Escape") {
|
|
175
|
-
close();
|
|
176
|
-
return true;
|
|
177
|
-
}
|
|
178
|
-
if (st.items.length === 0) return false;
|
|
179
|
-
switch (e.key) {
|
|
180
|
-
case "ArrowDown":
|
|
181
|
-
st.index = (st.index + 1) % st.items.length;
|
|
182
|
-
highlight();
|
|
183
|
-
return true;
|
|
184
|
-
case "ArrowUp":
|
|
185
|
-
st.index = (st.index - 1 + st.items.length) % st.items.length;
|
|
186
|
-
highlight();
|
|
187
|
-
return true;
|
|
188
|
-
case "Tab":
|
|
189
|
-
pick(st.items[st.index]);
|
|
190
|
-
return true;
|
|
191
|
-
case "Enter":
|
|
192
|
-
if (e.shiftKey) return false;
|
|
193
|
-
pick(st.items[st.index]);
|
|
194
|
-
return true;
|
|
195
|
-
}
|
|
196
|
-
return false;
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
// 光标移动(点击、方向键离开 @ 片段)也要跟着开关
|
|
200
|
-
input.addEventListener("click", () => void update());
|
|
201
|
-
return { update, keydown, close, get open() { return st.open; } };
|
|
202
|
-
}
|