agent-loft 0.2.48 → 0.2.49

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.
Files changed (52) hide show
  1. package/README.md +26 -12
  2. package/package.json +1 -1
  3. package/src/agents/claude/driver.ts +1 -1
  4. package/src/agents/claude/session.ts +140 -47
  5. package/src/agents/codex/driver.ts +1 -1
  6. package/src/agents/codex/session.ts +90 -58
  7. package/src/channels/feishu/bot.ts +1 -3
  8. package/src/channels/feishu/cards.ts +2 -2
  9. package/src/channels/web/api.ts +24 -17
  10. package/src/channels/web/experts-api.ts +185 -0
  11. package/src/channels/web/skills-api.ts +1 -7
  12. package/src/channels/web/tasks-api.ts +9 -5
  13. package/src/core/command-catalog.ts +4 -6
  14. package/src/core/commands.ts +6 -11
  15. package/src/core/expert-prompt.ts +119 -0
  16. package/src/core/experts.ts +416 -0
  17. package/src/core/hub.ts +134 -102
  18. package/src/core/mcp.ts +253 -0
  19. package/src/core/memory-curator.ts +8 -9
  20. package/src/core/memory.ts +11 -4
  21. package/src/core/personal.ts +5 -1
  22. package/src/core/skill-runtime.ts +11 -114
  23. package/src/core/store.ts +30 -11
  24. package/src/core/task-prompt.ts +1 -1
  25. package/src/core/task-runner.ts +24 -11
  26. package/src/core/tasks-bridge.ts +7 -7
  27. package/src/core/tasks.ts +49 -14
  28. package/src/core/types.ts +4 -7
  29. package/src/core/uploads.ts +7 -7
  30. package/src/mcp/tasks-server.ts +1 -2
  31. package/web/css/experts.css +103 -0
  32. package/web/css/messages.css +43 -0
  33. package/web/css/overlay.css +12 -0
  34. package/web/css/sidebar.css +3 -2
  35. package/web/css/skills.css +0 -11
  36. package/web/css/topbar.css +2 -2
  37. package/web/index.html +1 -0
  38. package/web/settings/experts.js +444 -0
  39. package/web/settings/index.js +3 -0
  40. package/web/settings/memory.js +21 -5
  41. package/web/settings/skills.js +3 -3
  42. package/web/state.js +5 -2
  43. package/web/views/composer.js +4 -16
  44. package/web/views/conv-actions.js +7 -9
  45. package/web/views/experts.js +73 -0
  46. package/web/views/layout.js +4 -3
  47. package/web/views/messages.js +44 -11
  48. package/web/views/sidebar.js +2 -2
  49. package/web/views/tasks.js +21 -36
  50. package/web/views/topbar.js +43 -74
  51. package/src/core/chat-mode.ts +0 -62
  52. package/web/views/mention.js +0 -202
@@ -6,6 +6,7 @@ import { agentLabel, convTitle, renderSidebar } from "./sidebar.js";
6
6
  import { agentCatalog, baseName, catalogDefaults, draftPick, modelName, renderContext, renderTopbar, saveDraftPick, visibleModels } from "./topbar.js";
7
7
  import { closeMenu, confirmDialog, modal, placePopover, showPopover } from "../lib/overlay.js";
8
8
  import { newDraft, openConversation, renderEmpty } from "./messages.js";
9
+ import { setDraftExpert } from "./experts.js";
9
10
 
10
11
  // ---------- 会话操作 ----------
11
12
 
@@ -42,15 +43,12 @@ export async function forkConversation(anchor, key = S.current) {
42
43
  }
43
44
  }
44
45
 
45
- export async function switchAgent(id) {
46
- if (!S.current) {
47
- S.draftAgent = id;
48
- localStorage.setItem("aim.agent", id);
49
- renderTopbar();
50
- renderEmpty();
51
- return;
52
- }
53
- await doAction("agent", id);
46
+ /** 新对话里选专家(进了对话就不能换了:要换专家新建对话) */
47
+ export function switchAgent(id) {
48
+ if (S.current) return;
49
+ setDraftExpert(id);
50
+ renderTopbar();
51
+ renderEmpty();
54
52
  }
55
53
 
56
54
  /** 模型 / 思考程度选择:已有会话直接切,新对话先记下,发第一条消息时带过去 */
@@ -0,0 +1,73 @@
1
+ import { agentIcon, h } from "../lib/dom.js";
2
+ import { api } from "../lib/api.js";
3
+ import { S } from "../state.js";
4
+ import { agentLabel } from "./sidebar.js";
5
+
6
+ // ---------- 专家:新对话、定时任务里选 ----------
7
+ // 内置专家就是能用的 agent(Claude Code / Codex),自定义专家在「设置 → 专家」里建。
8
+ // 对话从头到尾一个专家:只有新对话里能选,进了对话只显示。
9
+
10
+ /** 能用的 agent(启用了、装好登录了的;一个都没有时退回全部启用的) */
11
+ function usableEngines() {
12
+ const enabled = S.agents.filter((a) => a.enabled);
13
+ const usable = enabled.filter((a) => a.available !== false);
14
+ return (usable.length ? usable : enabled).map((a) => a.id);
15
+ }
16
+
17
+ /** 能选的专家:内置的在前,再是自己的(引擎能用的) */
18
+ export function expertChoices() {
19
+ const engines = usableEngines();
20
+ return [
21
+ ...engines.map((id) => ({ id, name: agentLabel(id), engine: id, builtin: true })),
22
+ ...S.experts.filter((e) => engines.includes(e.engine)),
23
+ ];
24
+ }
25
+
26
+ export function findExpert(id) {
27
+ return expertChoices().find((e) => e.id === id) ?? null;
28
+ }
29
+
30
+ /** 选的专家还在不在:不在(删了、引擎关了)就退回第一个能用的。引擎跟着专家 */
31
+ export function syncDraftExpert() {
32
+ const choices = expertChoices();
33
+ let e = choices.find((x) => x.id === S.draftExpert);
34
+ // 以前只记了 agent
35
+ if (!e && !S.draftExpert && S.draftAgent) e = choices.find((x) => x.id === S.draftAgent);
36
+ e ??= choices[0] ?? null;
37
+ S.draftExpert = e?.id ?? null;
38
+ S.draftAgent = e?.engine ?? null;
39
+ return e;
40
+ }
41
+
42
+ export function setDraftExpert(id) {
43
+ S.draftExpert = id;
44
+ localStorage.setItem("aim.expert", id);
45
+ const e = syncDraftExpert();
46
+ if (e) localStorage.setItem("aim.agent", e.engine);
47
+ // 换了专家:默认工作目录跟着专家走(没设就是工作区根目录)
48
+ S.draftCwd = e?.cwd || null;
49
+ }
50
+
51
+ /** 专家的图标:自定义专家有 emoji 就用 emoji,否则用引擎的图标 */
52
+ export function expertIcon(e, cls = "agent-icon") {
53
+ if (e?.icon) return h("span", { class: `${cls} expert-emoji` }, e.icon);
54
+ return agentIcon(e?.engine ?? e?.id, cls);
55
+ }
56
+
57
+ /** 对话的专家(服务端给的 c.expert)配上引擎,给 expertIcon 用 */
58
+ export function convExpert(c) {
59
+ const badge = c?.expert ?? { id: c?.agent, name: agentLabel(c?.agent ?? ""), builtin: true };
60
+ return { ...badge, engine: c?.agent };
61
+ }
62
+
63
+ /** 设置里改了专家:重新拉一遍 */
64
+ export async function reloadExperts() {
65
+ try {
66
+ const d = await api("/api/experts/brief");
67
+ S.experts = d.experts ?? [];
68
+ } catch {
69
+ // 拉不到就先用旧的
70
+ }
71
+ syncDraftExpert();
72
+ document.dispatchEvent(new CustomEvent("aim:experts"));
73
+ }
@@ -19,7 +19,8 @@ import {
19
19
  stopMarquee,
20
20
  } from "./sidebar.js";
21
21
  import { applyDesktopQuota, loadQuota, renderQuota } from "./quota.js";
22
- import { openBotConversations, openConvContextMenu, runDesktopMenu, usableAgents } from "./topbar.js";
22
+ import { openBotConversations, openConvContextMenu, runDesktopMenu } from "./topbar.js";
23
+ import { syncDraftExpert } from "./experts.js";
23
24
  import { confirmDialog, showMenu } from "../lib/overlay.js";
24
25
  import { applyShellInfo, handleAllowEmbed, HOME, mountSwitcher, refreshShellPush, routeToServer, shellBooted, switchServer } from "../lib/shell.js";
25
26
  import { bindScrollWatch, newDraft, openConversation, scrollToBottom } from "./messages.js";
@@ -141,8 +142,8 @@ export async function boot() {
141
142
  applyShellInfo(data.servers, srv || history.state?.loftServer || (location.hash.length > 1 ? HOME : undefined));
142
143
  applyBootstrap(data);
143
144
  S.pending = new Map(data.pending.map((i) => [i.id, i]));
144
- const enabled = usableAgents().map((a) => a.id);
145
- if (!S.draftAgent || !enabled.includes(S.draftAgent)) S.draftAgent = enabled[0] ?? null;
145
+ S.experts = data.experts ?? [];
146
+ syncDraftExpert();
146
147
  renderLayout();
147
148
  reportDesktop();
148
149
  WS.connect();
@@ -1,4 +1,4 @@
1
- import { agentIcon, fill, h, icon, starsLoader } from "../lib/dom.js";
1
+ import { fill, h, icon, starsLoader } from "../lib/dom.js";
2
2
  import { api } from "../lib/api.js";
3
3
  import { clockTime, liveDuration, serverNow, setServerTime, tidyFooter } from "../lib/time.js";
4
4
  import { copyText, patchMarkdown, renderMarkdown } from "../lib/markdown.js";
@@ -13,6 +13,7 @@ import { changeCwd, doAction, forkConversation } from "./conv-actions.js";
13
13
  import { scheduleNav } from "./msg-nav.js";
14
14
  import { linkReplyFiles } from "./file-preview.js";
15
15
  import { setTodos } from "./todos.js";
16
+ import { expertIcon, findExpert } from "./experts.js";
16
17
 
17
18
  // ---------- 打开会话 ----------
18
19
 
@@ -52,25 +53,57 @@ export function newDraft() {
52
53
  export function renderEmpty() {
53
54
  const enabled = usableAgents();
54
55
  const cwd = S.draftCwd ?? S.workspaceRoot ?? "";
56
+ const expert = findExpert(S.draftExpert);
57
+ // 自定义专家:有欢迎语就用它;只能联网的专家不碰目录,不问「在哪个目录里」
58
+ const custom = expert && !expert.builtin;
59
+ const dirTitle = h(
60
+ "h2",
61
+ {},
62
+ "你想让我们在 ",
63
+ h("button", { class: "hero-dir", title: `${cwd}\n点击切换工作目录`, onclick: () => changeCwd() }, baseName(cwd) || "工作区"),
64
+ " 中构建什么?",
65
+ );
66
+ const fillPrompt = (text) => {
67
+ if (!ui.input) return;
68
+ ui.input.value = text;
69
+ ui.input.dispatchEvent(new Event("input"));
70
+ ui.input.focus();
71
+ // 填空题(___):把光标放到空上
72
+ const blank = text.indexOf("___");
73
+ if (blank >= 0) ui.input.setSelectionRange(blank, blank + 3);
74
+ };
55
75
  fill(ui.msgInner,
56
76
  h(
57
77
  "div",
58
78
  { class: "empty hero" },
59
- agentIcon(S.draftAgent, "hero-logo"),
60
- enabled.length
61
- ? h(
62
- "h2",
63
- {},
64
- "你想让我们在 ",
65
- h("button", { class: "hero-dir", title: `${cwd}\n点击切换工作目录`, onclick: () => changeCwd() }, baseName(cwd) || "工作区"),
66
- " 中构建什么?",
67
- )
68
- : [h("h2", {}, "有什么可以帮你?"), h("div", {}, "还没有启用任何 agent,先去设置里启用。")],
79
+ expertIcon(expert ?? { id: S.draftAgent }, "hero-logo"),
80
+ !enabled.length
81
+ ? [h("h2", {}, "有什么可以帮你?"), h("div", {}, "还没有启用任何 agent,先去设置里启用。")]
82
+ : custom
83
+ ? [
84
+ expert.welcome ? h("h2", {}, expert.welcome) : expert.access === "web" ? h("h2", {}, `我是${expert.name},有什么可以帮你?`) : dirTitle,
85
+ expert.welcome && expert.access !== "web" ? h("div", { class: "hero-sub" }, "工作目录:", h("button", { class: "hero-dir", title: `${cwd}\n点击切换工作目录`, onclick: () => changeCwd() }, baseName(cwd) || "工作区")) : null,
86
+ expert.quickPrompts?.length
87
+ ? h(
88
+ "div",
89
+ { class: "hero-prompts" },
90
+ expert.quickPrompts.map((q) => h("button", { class: "hero-prompt", title: q.prompt, onclick: () => fillPrompt(q.prompt) }, q.title)),
91
+ )
92
+ : null,
93
+ ]
94
+ : dirTitle,
69
95
  ),
70
96
  );
71
97
  renderContext();
72
98
  }
73
99
 
100
+ // 设置里改了专家:新对话页上的专家、欢迎语跟着变
101
+ document.addEventListener("aim:experts", () => {
102
+ if (S.current || S.view) return;
103
+ renderTopbar();
104
+ renderEmpty();
105
+ });
106
+
74
107
  export async function openConversation(key) {
75
108
  leaveTasksView();
76
109
  const token = ++S.loadToken;
@@ -439,8 +439,8 @@ export function renderSidebar() {
439
439
  orphan ? h("span", { class: "brand-icon lead fork-mark", html: ICON.fork }) : null,
440
440
  // 定时任务的专用对话
441
441
  c.taskId ? h("span", { class: "brand-icon lead task-mark", title: "定时任务的对话", html: ICON.clock }) : null,
442
- // 聊天模式的对话
443
- c.mode === "chat" ? h("span", { class: "brand-icon lead chat-mark", title: "聊天模式", html: ICON.chat }) : null,
442
+ // 自定义专家的对话:前面挂专家的 emoji
443
+ c.expert?.icon && !c.expert.builtin ? h("span", { class: "brand-icon lead expert-mark", title: `专家:${c.expert.name}` }, c.expert.icon) : null,
444
444
  h("span", { class: "title" }, convTitle(c)),
445
445
  status([c.key]),
446
446
  );
@@ -5,10 +5,10 @@ import { confirmDialog, modal, showMenu } from "../lib/overlay.js";
5
5
  import { S, ui } from "../state.js";
6
6
  import { WS } from "../lib/ws.js";
7
7
  import { agentLabel, closeSide, renderSidebar } from "./sidebar.js";
8
- import { baseName, renderTopbar, usableAgents } from "./topbar.js";
8
+ import { baseName, renderTopbar } from "./topbar.js";
9
9
  import { openConversation } from "./messages.js";
10
10
  import { changeCwd } from "./conv-actions.js";
11
- import { attachMention } from "./mention.js";
11
+ import { expertChoices, findExpert } from "./experts.js";
12
12
 
13
13
  // ---------- 定时任务页 ----------
14
14
  // 到点让 agent 按提示词干活,结果回到一条对话里;「有需要才提醒」时由 agent 判断这次要不要打扰你。
@@ -41,21 +41,20 @@ const EXAMPLES = [
41
41
  prompt: "查一下 npm 上 agent-loft 的最新版本(https://www.npmjs.com/package/agent-loft),和当前服务端版本比较。有新版本就告诉我新版本号和主要更新内容。",
42
42
  schedule: { kind: "daily", time: "09:00" },
43
43
  notify: "smart",
44
- mode: "chat",
45
44
  },
46
45
  {
47
46
  title: "科技新闻早报",
48
47
  prompt: "搜一下过去 24 小时最重要的科技新闻,挑 5 条,每条用一两句话说明为什么值得关注,附上链接。",
49
48
  schedule: { kind: "daily", time: "08:30" },
50
49
  notify: "always",
51
- mode: "chat",
52
50
  },
53
51
  {
54
52
  title: "下班前看看改了什么",
55
53
  prompt: "用 git status 和 git log --since=midnight 看看这个目录今天改了什么,帮我列一个简短的日报草稿。今天没有任何改动的话就不用提醒我。",
56
54
  schedule: { kind: "weekdays", time: "18:00" },
57
55
  notify: "smart",
58
- mode: "work",
56
+ // 要跑 git 命令:用能执行命令的内置专家
57
+ expert: "claude",
59
58
  },
60
59
  ];
61
60
 
@@ -199,7 +198,7 @@ function taskCard(t) {
199
198
  const where =
200
199
  t.target.kind === "conv"
201
200
  ? `发到「${t.convTitle || "对话"}」`
202
- : `${agentLabel(t.agent)} · ${t.mode === "work" ? "工作模式" : "聊天模式"}${t.mode === "work" && t.cwd ? ` · ${baseName(t.cwd)}` : ""}`;
201
+ : `${findExpert(t.expert ?? t.agent)?.name ?? (t.expert ? "(已删除的专家)" : agentLabel(t.agent))}${t.cwd ? ` · ${baseName(t.cwd)}` : ""}`;
203
202
  const toggle = h("input", { type: "checkbox", checked: t.enabled, title: t.enabled ? "暂停" : "启用" });
204
203
  toggle.onchange = async () => {
205
204
  try {
@@ -282,18 +281,10 @@ const WEEK_NAME = ["日", "一", "二", "三", "四", "五", "六"];
282
281
  /** task:编辑这个;preset:新建时预填(例子) */
283
282
  export function openTaskForm(task, preset) {
284
283
  const src = task ?? preset ?? {};
285
- const agents = usableAgents();
284
+ const experts = expertChoices();
286
285
  const title = h("input", { value: src.title ?? "", maxlength: "60", placeholder: "比如:检查 agent-loft 更新" });
287
- const prompt = h("textarea", { rows: 5, maxlength: "4000", placeholder: "到点让 agent 做什么。写成完整的指令:那时你多半不在,它没法再问你。可以用 @ 指定技能。" });
286
+ const prompt = h("textarea", { rows: 5, maxlength: "4000", placeholder: "到点让 agent 做什么。写成完整的指令:那时你多半不在,它没法再问你。" });
288
287
  prompt.value = src.prompt ?? "";
289
- // @ 技能:和输入框一样,到点时服务端认提示词里的 @name
290
- const mentionMenu = h("div", { class: "slash-menu below", hidden: true });
291
- const mention = attachMention(prompt, mentionMenu);
292
- prompt.addEventListener("input", () => void mention.update());
293
- prompt.addEventListener("blur", () => setTimeout(mention.close, 150));
294
- prompt.addEventListener("keydown", (e) => {
295
- if (mention.keydown(e)) e.preventDefault();
296
- });
297
288
 
298
289
  // ---- 频率 ----
299
290
  const sch = { kind: "daily", time: "09:00", days: [1], day: 1, every: 2, minute: 0, expr: "0 9 * * *", at: Date.now() + 3600_000, ...(src.schedule ?? {}) };
@@ -369,16 +360,14 @@ export function openTaskForm(task, preset) {
369
360
  h("option", { value: "dedicated" }, "专用对话(单独开一条)"),
370
361
  )
371
362
  : null;
372
- const agent = h(
373
- "select",
374
- {},
375
- agents.map((a) => h("option", { value: a.id, selected: a.id === (src.agent ?? S.draftAgent) }, a.label)),
376
- );
377
- const mode = h(
363
+ const picked = src.expert ?? src.agent ?? S.draftExpert;
364
+ // 任务用的专家被删了:列表里没有它,留一项占位,免得一打开就悄悄换成别的
365
+ const missing = task && !experts.some((e) => e.id === picked);
366
+ const expert = h(
378
367
  "select",
379
368
  {},
380
- h("option", { value: "chat", selected: (src.mode ?? "chat") === "chat" }, "聊天模式:只能联网搜索,开销小,不需要授权"),
381
- h("option", { value: "work", selected: src.mode === "work" }, "工作模式:能执行命令、读写本机文件"),
369
+ missing ? h("option", { value: "", selected: true, disabled: true }, "(专家已删除,选一个)") : null,
370
+ experts.map((e) => h("option", { value: e.id, selected: !missing && e.id === picked }, `${e.icon ? `${e.icon} ` : ""}${e.name}`)),
382
371
  );
383
372
  let cwd = src.cwd || "";
384
373
  const cwdBtn = h("button", { type: "button", class: "btn task-cwd" });
@@ -395,20 +384,15 @@ export function openTaskForm(task, preset) {
395
384
  showCwd();
396
385
  },
397
386
  });
398
- const cwdField = field("工作目录", cwdBtn, "工作模式下 agent 在这个目录里干活。需要授权时会推送给你,没人处理就按拒绝。");
399
- const dedicatedBox = h(
400
- "div",
401
- { class: "task-dedicated" },
402
- agents.length > 1 ? field("Agent", agent) : null,
403
- field("模式", mode),
404
- cwdField,
405
- );
387
+ const cwdField = field("工作目录", cwdBtn, "专家在这个目录里干活。需要授权时会推送给你,没人处理就按拒绝。没选就用专家默认的目录。");
388
+ const dedicatedBox = h("div", { class: "task-dedicated" }, field("专家", expert, "到点由这个专家来做。要联网、读文件还是跑命令,看专家的权限(设置 → 专家)。"), cwdField);
406
389
  const syncVisibility = () => {
407
390
  const dedicated = !target || target.value === "dedicated";
408
391
  dedicatedBox.hidden = !dedicated;
409
- cwdField.hidden = mode.value !== "work";
392
+ // 只能联网的专家不碰目录
393
+ cwdField.hidden = findExpert(expert.value)?.access === "web";
410
394
  };
411
- mode.onchange = syncVisibility;
395
+ expert.onchange = syncVisibility;
412
396
  if (target) target.onchange = syncVisibility;
413
397
  syncVisibility();
414
398
 
@@ -416,7 +400,7 @@ export function openTaskForm(task, preset) {
416
400
  "div",
417
401
  { class: "modal-body task-form" },
418
402
  field("名称", title),
419
- field("要做的事", h("div", { class: "mention-wrap" }, prompt, mentionMenu), "需要对比的写清楚怎么判断,比如「和上次的结果比」「版本号比当前的新才算更新」。输入 @ 可以指定这次要用的技能。"),
403
+ field("要做的事", prompt, "需要对比的写清楚怎么判断,比如「和上次的结果比」「版本号比当前的新才算更新」。"),
420
404
  field("频率", h("div", { class: "task-freq-row" }, freq, freqBox), freqHint),
421
405
  field("提醒", notify),
422
406
  target ? field("结果发到", target) : field("结果发到", h("div", { class: "task-static" }, task?.convKey ? `专用对话「${task.convTitle || "对话"}」` : "专用对话(第一次运行时新建,之后都发到这条)")),
@@ -442,13 +426,14 @@ export function openTaskForm(task, preset) {
442
426
  : { kind: "cron", expr: sch.expr };
443
427
  if (schedule.kind === "once" && !(schedule.at > Date.now())) return toast("运行时间要在现在之后");
444
428
  const dedicated = !target || target.value === "dedicated";
429
+ if (dedicated && !expert.value) return toast("选一个专家");
445
430
  const payload = {
446
431
  title: title.value,
447
432
  prompt: prompt.value,
448
433
  schedule,
449
434
  notify: notify.value,
450
435
  target: dedicated ? { kind: "dedicated" } : { kind: "conv", key: task.target.key },
451
- ...(dedicated ? { agent: agent.value || agents[0]?.id, mode: mode.value, cwd: mode.value === "work" ? cwd : "" } : {}),
436
+ ...(dedicated ? { expert: expert.value, cwd: findExpert(expert.value)?.access === "web" ? "" : cwd } : {}),
452
437
  };
453
438
  save.disabled = true;
454
439
  try {
@@ -1,10 +1,11 @@
1
- import { agentIcon, fill, h, icon, ICON, toast } from "../lib/dom.js";
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 { closeMenu, confirmDialog, modal, placePopover, showMenu, showPopover } from "../lib/overlay.js";
7
- import { newDraft, openConversation } from "./messages.js";
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.mode === "chat" ? h("span", { class: "conv-mode-icon", title: "聊天模式", html: ICON.chat }) : null,
28
+ // 自定义专家的对话:标题前面挂上专家的图标
29
+ c.expert && !c.expert.builtin ? h("span", { class: "conv-expert-icon", title: `专家:${c.expert.name}` }, expertIcon(convExpert(c))) : null,
29
30
  h("span", { class: "conv-name", title: convTitle(c) }, convTitle(c)),
30
31
  ]
31
32
  : null,
@@ -38,15 +39,12 @@ export function renderTopbar() {
38
39
  }
39
40
 
40
41
  /**
41
- * 右上角「更多」里的项。对话页(含新对话)有聊天模式、定时发送;设置哪个页面都有,
42
+ * 右上角「更多」里的项。对话页(含新对话)有定时发送;设置哪个页面都有,
42
43
  * 桌面端自己的「更多」里已经有每台服务器的设置时不放
43
44
  */
44
45
  function topMenuItems() {
45
46
  const items = [];
46
47
  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
48
  items.push({ id: "schedule", label: "定时发送", icon: "clock", onclick: () => openSchedule() });
51
49
  }
52
50
  if (!desktopSettings) {
@@ -197,31 +195,38 @@ export function usableAgents() {
197
195
  return usable.length ? usable : enabled;
198
196
  }
199
197
 
200
- function pickAgentMenu(anchor) {
201
- const allowed = usableAgents().map((a) => a.id);
202
- if (allowed.length < 2) return;
203
- if (ui.menu?.dataset.for === "agent") return closeMenu();
198
+ /** 新对话里选专家:内置的在前,再是自己的;最下面去设置里管理 */
199
+ function pickExpertMenu(anchor) {
200
+ if (ui.menu?.dataset.for === "expert") return closeMenu();
204
201
  const pop = showPopover(anchor, { align: "left" });
205
- pop.dataset.for = "agent";
202
+ pop.dataset.for = "expert";
203
+ pop.classList.add("expert-pop");
204
+ const choices = expertChoices();
205
+ const item = (e) =>
206
+ h(
207
+ "button",
208
+ { class: `pop-item${e.id === S.draftExpert ? " on" : ""}`, title: e.description || "", onclick: () => (closeMenu(), switchAgent(e.id)) },
209
+ expertIcon(e),
210
+ h("span", { class: "grow" }, h("span", { class: "t" }, e.name), e.description ? h("span", { class: "s" }, e.description) : null),
211
+ e.id === S.draftExpert ? h("span", { class: "check" }, "✓") : null,
212
+ );
213
+ const builtin = choices.filter((e) => e.builtin);
214
+ const own = choices.filter((e) => !e.builtin);
206
215
  fill(
207
216
  pop,
208
- allowed.map((id) =>
209
- h(
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
- ),
217
+ builtin.map(item),
218
+ own.length ? [h("div", { class: "sep" }), h("div", { class: "pop-head" }, "我的专家"), own.map(item)] : null,
219
+ h("div", { class: "sep" }),
220
+ h("button", { class: "pop-item", onclick: () => (closeMenu(), openSettings("专家")) }, icon("gear"), h("span", { class: "grow" }, own.length ? "管理专家…" : "新建专家…")),
217
221
  );
218
222
  placePopover(pop, anchor);
219
223
  }
220
224
 
221
225
  /** 新对话里选的模型 / 思考程度,按 agent 分开记,创建会话时带过去 */
222
226
  S.draftPick = JSON.parse(localStorage.getItem("aim.draftPick") || "{}");
223
- export function draftPick(agent = S.draftAgent) {
224
- return (S.draftPick[agent] ??= {});
227
+ /** 新对话里选的模型 / 思考程度,按专家分开记(内置专家就是 claude / codex,和以前按 agent 记的对得上) */
228
+ export function draftPick(expert = S.draftExpert ?? S.draftAgent) {
229
+ return (S.draftPick[expert] ??= {});
225
230
  }
226
231
  export function saveDraftPick() {
227
232
  localStorage.setItem("aim.draftPick", JSON.stringify(S.draftPick));
@@ -289,21 +294,21 @@ export function renderContext() {
289
294
  if (!ui.ctxBar) return;
290
295
  const c = S.current ? S.convs.get(S.current) : null;
291
296
  const cwd = currentCwd();
292
- const multi = usableAgents().length > 1;
293
- // 会话里不能换 agent(两个 agent 的会话互不相通),只显示;新对话里可以选
294
- const agentId = c ? c.agent : S.draftAgent;
295
- const agentEl = c || !multi
296
- ? h("span", { class: "ctx-item static" }, agentIcon(agentId), h("span", { class: "label" }, agentLabel(agentId ?? "") || "无可用 agent"))
297
+ // 对话从头到尾一个专家:进了对话只显示;新对话里可以选
298
+ const expert = c ? convExpert(c) : findExpert(S.draftExpert);
299
+ const agentEl = c
300
+ ? h("span", { class: "ctx-item static", title: `专家:${expert.name}${expert.missing ? "(已删除)" : ""}` }, expertIcon(expert), h("span", { class: "label" }, expert.name))
297
301
  : h(
298
302
  "button",
299
- { class: "ctx-item", title: "选择 agent", onclick: (e) => pickAgentMenu(e.currentTarget) },
300
- agentIcon(agentId),
301
- h("span", { class: "label" }, agentLabel(agentId ?? "")),
303
+ { class: "ctx-item", title: "选择专家", onclick: (e) => pickExpertMenu(e.currentTarget) },
304
+ expert ? expertIcon(expert) : null,
305
+ h("span", { class: "label" }, expert?.name ?? "无可用 agent"),
302
306
  h("span", { class: "chev", html: ICON.down }),
303
307
  );
304
308
  fill(
305
309
  ui.ctxBar,
306
- h("button", { class: "ctx-item", title: `工作目录:${cwd}\n点击切换`, onclick: () => changeCwd() }, icon("folder"), h("span", { class: "label" }, baseName(cwd) || "选择目录")),
310
+ // 只能联网的专家不碰文件,不显示工作目录
311
+ expert?.access === "web" ? null : h("button", { class: "ctx-item", title: `工作目录:${cwd}\n点击切换`, onclick: () => changeCwd() }, icon("folder"), h("span", { class: "label" }, baseName(cwd) || "选择目录")),
307
312
  agentEl,
308
313
  );
309
314
  updateCtxVisibility();
@@ -316,9 +321,11 @@ export function renderContext() {
316
321
  ensureCatalog(modelAgent);
317
322
  const defaults = catalogDefaults.get(modelAgent) ?? {};
318
323
  const pick = c ? null : draftPick();
319
- const id = c ? (c.runtimeModel ?? c.model) : pick.model;
324
+ // 新对话:没选过就是专家配的默认模型
325
+ const draft = c ? null : findExpert(S.draftExpert);
326
+ const id = c ? (c.runtimeModel ?? c.model) : (pick.model ?? (draft?.model || undefined));
320
327
  model = modelName(modelAgent, id) ?? (catalogLoaded.has(modelAgent) ? "选择模型" : "…");
321
- effort = (c ? (c.runtimeEffort ?? c.effort) : pick.effort) ?? defaults.effort;
328
+ effort = (c ? (c.runtimeEffort ?? c.effort) : (pick.effort ?? (draft?.effort || undefined))) ?? defaults.effort;
322
329
  }
323
330
  renderCtxRing();
324
331
  fill(
@@ -335,44 +342,6 @@ export function renderContext() {
335
342
  );
336
343
  }
337
344
 
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
345
  /** 84321 → 84k,1234567 → 1.2M */
377
346
  function fmtTokens(n) {
378
347
  if (n >= 1e6) return `${(n / 1e6).toFixed(n >= 1e7 ? 0 : 1)}M`;
@@ -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
- }