agent-loft 0.2.63 → 0.2.65
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 +5 -4
- package/package.json +1 -1
- package/src/agents/claude/session.ts +6 -6
- package/src/agents/codex/session.ts +9 -3
- package/src/channels/web/api.ts +10 -2
- package/src/channels/web/experts-api.ts +20 -0
- package/src/core/builtin-agents.ts +96 -0
- package/src/core/experts.ts +2 -2
- package/src/core/hub.ts +10 -6
- package/src/core/mcp.ts +1 -1
- package/src/core/personal.ts +2 -0
- package/src/core/skill-runtime.ts +6 -4
- package/src/core/types.ts +5 -1
- package/src/core/ui-prefs.ts +29 -2
- package/web/css/settings.css +5 -0
- package/web/css/sidebar.css +158 -7
- package/web/lib/overlay.js +7 -1
- package/web/settings/agents.js +55 -0
- package/web/settings/experts.js +2 -2
- package/web/settings/index.js +2 -2
- package/web/state.js +2 -0
- package/web/views/layout.js +2 -14
- package/web/views/sidebar-nav.js +299 -0
- package/web/views/sidebar.js +30 -15
- package/web/views/topbar.js +2 -10
package/web/css/sidebar.css
CHANGED
|
@@ -163,11 +163,12 @@ button.conv-group:hover {
|
|
|
163
163
|
|
|
164
164
|
.proj-row {
|
|
165
165
|
position: relative;
|
|
166
|
-
margin-
|
|
166
|
+
margin-bottom: 4px;
|
|
167
167
|
}
|
|
168
168
|
|
|
169
169
|
.conv-item.proj {
|
|
170
170
|
color: var(--fg-soft);
|
|
171
|
+
margin-bottom: 0;
|
|
171
172
|
}
|
|
172
173
|
|
|
173
174
|
.conv-item.proj:hover {
|
|
@@ -277,6 +278,7 @@ button.conv-group:hover {
|
|
|
277
278
|
border-radius: 8px;
|
|
278
279
|
font-size: 14px;
|
|
279
280
|
position: relative;
|
|
281
|
+
margin-bottom: 4px;
|
|
280
282
|
}
|
|
281
283
|
|
|
282
284
|
.conv-item:hover {
|
|
@@ -372,7 +374,8 @@ button.conv-group:hover {
|
|
|
372
374
|
margin-left: 0;
|
|
373
375
|
}
|
|
374
376
|
|
|
375
|
-
.conv-item .task-mark
|
|
377
|
+
.conv-item .task-mark,
|
|
378
|
+
.conv-item .feishu-mark {
|
|
376
379
|
flex-shrink: 0;
|
|
377
380
|
width: 14px;
|
|
378
381
|
height: 14px;
|
|
@@ -380,13 +383,16 @@ button.conv-group:hover {
|
|
|
380
383
|
color: var(--fg-faint);
|
|
381
384
|
}
|
|
382
385
|
|
|
383
|
-
.conv-item .task-mark svg
|
|
386
|
+
.conv-item .task-mark svg,
|
|
387
|
+
.conv-item .feishu-mark svg {
|
|
384
388
|
width: 14px;
|
|
385
389
|
height: 14px;
|
|
386
390
|
}
|
|
387
391
|
|
|
388
392
|
.conv-item > .dot ~ .task-mark,
|
|
389
|
-
.conv-item > .conv-spin ~ .task-mark
|
|
393
|
+
.conv-item > .conv-spin ~ .task-mark,
|
|
394
|
+
.conv-item > .dot ~ .feishu-mark,
|
|
395
|
+
.conv-item > .conv-spin ~ .feishu-mark {
|
|
390
396
|
margin-left: 0;
|
|
391
397
|
}
|
|
392
398
|
|
|
@@ -408,7 +414,8 @@ button.conv-group:hover {
|
|
|
408
414
|
display: inline-grid;
|
|
409
415
|
}
|
|
410
416
|
|
|
411
|
-
.conv-item:hover .task-mark
|
|
417
|
+
.conv-item:hover .task-mark,
|
|
418
|
+
.conv-item:hover .expert-mark ~ .feishu-mark {
|
|
412
419
|
display: none;
|
|
413
420
|
}
|
|
414
421
|
}
|
|
@@ -657,7 +664,151 @@ button.conv-group:hover {
|
|
|
657
664
|
color: var(--fg);
|
|
658
665
|
}
|
|
659
666
|
|
|
660
|
-
/*
|
|
661
|
-
.
|
|
667
|
+
/* 项目列表上方的菜单;「新建」固定,其余可配置显示和顺序。 */
|
|
668
|
+
.sidebar-nav {
|
|
669
|
+
padding: 4px 10px 8px;
|
|
670
|
+
flex-shrink: 0;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
.sidebar-nav .side-nav {
|
|
674
|
+
width: 100%;
|
|
675
|
+
margin: 0 0 4px;
|
|
676
|
+
text-align: left;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
.sidebar-nav .side-nav:last-child {
|
|
662
680
|
margin-bottom: 0;
|
|
663
681
|
}
|
|
682
|
+
|
|
683
|
+
.sidebar-nav .grow,
|
|
684
|
+
.sidebar-edit-row .grow {
|
|
685
|
+
flex: 1;
|
|
686
|
+
min-width: 0;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
.side-more[aria-expanded="true"] {
|
|
690
|
+
background: var(--bg-hover);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
.popover.side-more-menu {
|
|
694
|
+
min-width: 200px;
|
|
695
|
+
max-width: calc(100vw - 16px);
|
|
696
|
+
max-height: calc(100dvh - 16px);
|
|
697
|
+
overflow-y: auto;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
.side-more-menu .pop-item {
|
|
701
|
+
display: flex;
|
|
702
|
+
align-items: center;
|
|
703
|
+
gap: 10px;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
.side-more-menu .pop-item > span:first-child {
|
|
707
|
+
display: inline-grid;
|
|
708
|
+
flex-shrink: 0;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
.side-more-menu .pop-item svg {
|
|
712
|
+
width: 16px;
|
|
713
|
+
height: 16px;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
.side-more-menu .pop-item.active {
|
|
717
|
+
background: var(--bg-hover);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
.side-more-menu .live-dot {
|
|
721
|
+
display: none;
|
|
722
|
+
width: 7px;
|
|
723
|
+
height: 7px;
|
|
724
|
+
margin-left: auto;
|
|
725
|
+
border-radius: 50%;
|
|
726
|
+
background: var(--ok);
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
.side-more-menu .live .live-dot {
|
|
730
|
+
display: block;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
.modal.sidebar-editor {
|
|
734
|
+
max-width: 440px;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
.sidebar-edit-hint {
|
|
738
|
+
margin: 0;
|
|
739
|
+
color: var(--fg-soft);
|
|
740
|
+
font-size: 13px;
|
|
741
|
+
line-height: 1.6;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
.sidebar-edit-list {
|
|
745
|
+
display: flex;
|
|
746
|
+
flex-direction: column;
|
|
747
|
+
gap: 4px;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
.sidebar-edit-row {
|
|
751
|
+
display: flex;
|
|
752
|
+
align-items: center;
|
|
753
|
+
border-radius: 8px;
|
|
754
|
+
min-height: 44px;
|
|
755
|
+
padding: 0 4px 0 10px;
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
.sidebar-edit-row:hover,
|
|
759
|
+
.sidebar-edit-row.dragging {
|
|
760
|
+
background: var(--bg-hover);
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
.sidebar-edit-row.dragging {
|
|
764
|
+
opacity: 0.6;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
.sidebar-edit-row label {
|
|
768
|
+
display: flex;
|
|
769
|
+
align-items: center;
|
|
770
|
+
flex: 1;
|
|
771
|
+
gap: 12px;
|
|
772
|
+
min-height: 44px;
|
|
773
|
+
cursor: pointer;
|
|
774
|
+
font-size: 14px;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
.sidebar-edit-row input {
|
|
778
|
+
width: 17px;
|
|
779
|
+
height: 17px;
|
|
780
|
+
flex-shrink: 0;
|
|
781
|
+
margin: 0;
|
|
782
|
+
accent-color: var(--accent);
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
.sidebar-edit-row svg {
|
|
786
|
+
width: 17px;
|
|
787
|
+
height: 17px;
|
|
788
|
+
color: var(--fg-soft);
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
.sidebar-edit-grip {
|
|
792
|
+
display: grid;
|
|
793
|
+
place-items: center;
|
|
794
|
+
width: 36px;
|
|
795
|
+
height: 40px;
|
|
796
|
+
flex-shrink: 0;
|
|
797
|
+
border-radius: 6px;
|
|
798
|
+
font-size: 18px;
|
|
799
|
+
letter-spacing: -3px;
|
|
800
|
+
color: var(--fg-faint);
|
|
801
|
+
cursor: grab;
|
|
802
|
+
touch-action: none;
|
|
803
|
+
user-select: none;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
.sidebar-edit-grip:hover,
|
|
807
|
+
.sidebar-edit-grip:focus-visible {
|
|
808
|
+
color: var(--fg);
|
|
809
|
+
background: var(--bg-soft);
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
.sidebar-edit-grip:active {
|
|
813
|
+
cursor: grabbing;
|
|
814
|
+
}
|
package/web/lib/overlay.js
CHANGED
|
@@ -46,6 +46,7 @@ function onOutsideMenu(e) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
export function closeMenu() {
|
|
49
|
+
ui.menu?.onMenuClose?.();
|
|
49
50
|
ui.menu?.remove();
|
|
50
51
|
ui.menu = null;
|
|
51
52
|
document.removeEventListener("pointerdown", onOutsideMenu);
|
|
@@ -83,7 +84,12 @@ export function showPopover(anchor, { place = "above", align = "right" } = {}) {
|
|
|
83
84
|
pop.dataset.align = align;
|
|
84
85
|
document.body.append(pop);
|
|
85
86
|
ui.menu = pop;
|
|
86
|
-
setTimeout(() =>
|
|
87
|
+
setTimeout(() => {
|
|
88
|
+
if (ui.menu !== pop) return;
|
|
89
|
+
document.addEventListener("pointerdown", onOutsideMenu);
|
|
90
|
+
document.addEventListener("keydown", onMenuKey);
|
|
91
|
+
addEventListener("blur", closeMenu);
|
|
92
|
+
}, 0);
|
|
87
93
|
return pop;
|
|
88
94
|
}
|
|
89
95
|
|
package/web/settings/agents.js
CHANGED
|
@@ -156,7 +156,62 @@ function installCodexButton(after) {
|
|
|
156
156
|
return h("div", {}, btn);
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
+
/** 内置 Agent 的个人默认项;普通成员也能设置自己的,不需要服务器管理权限。 */
|
|
160
|
+
function builtinDefaultsSection() {
|
|
161
|
+
const section = h("div", { class: "section", "data-form": "" }, h("h4", {}, "内置 Agent 默认加载"), h("span", { class: "spinner" }));
|
|
162
|
+
const load = async () => {
|
|
163
|
+
let data;
|
|
164
|
+
try {
|
|
165
|
+
data = await api("/api/agents/defaults");
|
|
166
|
+
} catch (err) {
|
|
167
|
+
fill(section, h("h4", {}, "内置 Agent 默认加载"), h("div", { class: "error-text" }, err.message), h("button", { class: "btn small", onclick: load }, "重试"));
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
const choices = {};
|
|
171
|
+
const picker = (label, items, selected, inheritLabel) => {
|
|
172
|
+
const mode = h("select", { "aria-label": label },
|
|
173
|
+
...(inheritLabel ? [h("option", { value: "inherit", selected: selected === null }, inheritLabel)] : []),
|
|
174
|
+
h("option", { value: "pick", selected: selected !== null }, "指定加载(可全部取消)"));
|
|
175
|
+
const known = new Set(items.map((item) => item.id));
|
|
176
|
+
const missing = (selected ?? []).filter((id) => !known.has(id)).map((id) => ({ id, name: `${id}(暂不可用,可取消)` }));
|
|
177
|
+
const inputs = [...items, ...missing].map((item) => ({ id: item.id, input: h("input", { type: "checkbox", checked: (selected ?? []).includes(item.id) }), item }));
|
|
178
|
+
const list = h("div", { class: "check-list" }, inputs.length
|
|
179
|
+
? inputs.map(({ input, item }) => h("label", { class: "check-item", title: item.description ?? "" }, input, h("span", {}, item.name)))
|
|
180
|
+
: h("small", {}, "暂无可选项,请先在设置中添加"));
|
|
181
|
+
const sync = () => { list.hidden = mode.value !== "pick"; };
|
|
182
|
+
mode.addEventListener("change", sync);
|
|
183
|
+
sync();
|
|
184
|
+
return { el: h("div", { class: "field" }, h("span", {}, label), mode, list), value: () => mode.value === "inherit" ? null : inputs.filter((x) => x.input.checked).map((x) => x.id) };
|
|
185
|
+
};
|
|
186
|
+
const panels = ["claude", "codex"].map((engine) => {
|
|
187
|
+
const label = engine === "claude" ? "Claude Code" : "Codex";
|
|
188
|
+
const prefs = data.agents[engine];
|
|
189
|
+
const skills = picker(`${label} 技能`, data.skills, prefs.skills, "全部已启用的个人技能(含以后新增)");
|
|
190
|
+
const machine = (data.machineMcp?.[engine] ?? []).map((name) => ({ id: `${data.machinePrefix}${name}`, name: `${name}(本机)` }));
|
|
191
|
+
const mcp = picker(`${label} MCP`, [...data.mcp, ...machine], prefs.mcp, data.machineMcp ? "沿用机器上的 MCP 配置" : null);
|
|
192
|
+
const machineSkills = h("input", { type: "checkbox", checked: prefs.machineSkills });
|
|
193
|
+
choices[engine] = { skills, mcp, machineSkills };
|
|
194
|
+
return h("div", { class: "builtin-agent-defaults" }, h("h4", {}, label), skills.el,
|
|
195
|
+
data.machineMcp ? h("label", { class: "check-item" }, machineSkills, "同时加载机器上的技能") : null,
|
|
196
|
+
mcp.el);
|
|
197
|
+
});
|
|
198
|
+
fill(section, h("h4", {}, "内置 Agent 默认加载"),
|
|
199
|
+
h("p", { class: "skill-note" }, "仅影响你当前账号使用的内置 Agent;自定义专家继续使用各自的配置。保存后,下次发消息生效,正在执行的任务不受影响。"),
|
|
200
|
+
...panels,
|
|
201
|
+
h("small", {}, "技能和 MCP 可分别全部取消。记忆、定时任务、技能管理等系统工具仍按功能设置提供。"),
|
|
202
|
+
h("div", { class: "row" }, saveButton(async () => {
|
|
203
|
+
const agents = Object.fromEntries(Object.entries(choices).map(([engine, c]) => [engine, { skills: c.skills.value(), machineSkills: c.machineSkills.checked, mcp: c.mcp.value() }]));
|
|
204
|
+
await api("/api/agents/defaults", { method: "PUT", body: { agents } });
|
|
205
|
+
toast("已保存,下次发消息生效");
|
|
206
|
+
}, "保存默认加载设置")));
|
|
207
|
+
};
|
|
208
|
+
void load();
|
|
209
|
+
return section;
|
|
210
|
+
}
|
|
211
|
+
|
|
159
212
|
export function agentsTab(cfg, page) {
|
|
213
|
+
page.append(builtinDefaultsSection());
|
|
214
|
+
if (!cfg) return;
|
|
160
215
|
page.append(agentHealthSection());
|
|
161
216
|
const a = structuredClone(cfg.agents);
|
|
162
217
|
const claudeOn = h("input", { type: "checkbox", checked: a.claude.enabled });
|
package/web/settings/experts.js
CHANGED
|
@@ -48,7 +48,7 @@ function render(box, data, reload) {
|
|
|
48
48
|
h(
|
|
49
49
|
"p",
|
|
50
50
|
{ class: "skill-note" },
|
|
51
|
-
"专家是一份配置:用哪个引擎、什么提示词、能做什么(联网 / 读文件 / 改文件 / 跑命令)、带哪些技能和 MCP、能把活派给哪些子智能体。新建对话、定时任务时选一个专家,对话从头到尾都是它。内置的 Claude Code、Codex
|
|
51
|
+
"专家是一份配置:用哪个引擎、什么提示词、能做什么(联网 / 读文件 / 改文件 / 跑命令)、带哪些技能和 MCP、能把活派给哪些子智能体。新建对话、定时任务时选一个专家,对话从头到尾都是它。内置的 Claude Code、Codex 在「设置 → Agent」里选择默认技能和 MCP。",
|
|
52
52
|
),
|
|
53
53
|
h("div", { class: "skill-head-btns" }, add),
|
|
54
54
|
),
|
|
@@ -60,7 +60,7 @@ function render(box, data, reload) {
|
|
|
60
60
|
"div",
|
|
61
61
|
{ class: "task-card skill-card" },
|
|
62
62
|
h("div", { class: "task-top" }, h("span", { class: "task-title" }, agentIcon(b.engine, "expert-card-icon"), b.name), h("span", { class: "badge" }, "内置")),
|
|
63
|
-
h("div", { class: "task-prompt" }, `${b.name}
|
|
63
|
+
h("div", { class: "task-prompt" }, `${b.name} 原装的样子:原装提示词、所有工具;默认技能和 MCP 在「设置 → Agent」里配置。`),
|
|
64
64
|
),
|
|
65
65
|
),
|
|
66
66
|
data.experts.map((e) => expertCard(data, e, reload)),
|
package/web/settings/index.js
CHANGED
|
@@ -18,7 +18,7 @@ import { hasDirty, markDirty } from "./common.js";
|
|
|
18
18
|
|
|
19
19
|
// ---------- 设置 ----------
|
|
20
20
|
// 按「谁的、管什么」分组:我的(每个人自己的)、Agent、连接(飞书、远程控制)、管理。
|
|
21
|
-
//
|
|
21
|
+
// 普通成员可管理自己的资料和内置 Agent 默认项;管理员另有运行环境和服务器设置。
|
|
22
22
|
|
|
23
23
|
const LAST_TAB = "aim.settingsTab";
|
|
24
24
|
|
|
@@ -38,7 +38,7 @@ function groupsFor(cfg) {
|
|
|
38
38
|
["用量", (p) => usageTab(p)],
|
|
39
39
|
],
|
|
40
40
|
};
|
|
41
|
-
if (!cfg) return [mine];
|
|
41
|
+
if (!cfg) return [mine, { name: "Agent", tabs: [["Agent", (p) => agentsTab(null, p)]] }];
|
|
42
42
|
return [
|
|
43
43
|
mine,
|
|
44
44
|
{
|
package/web/state.js
CHANGED
package/web/views/layout.js
CHANGED
|
@@ -30,6 +30,7 @@ import { createNav } from "./msg-nav.js";
|
|
|
30
30
|
import { renameConv } from "./conv-actions.js";
|
|
31
31
|
import { clearCommandCache, preloadCommands } from "./slash.js";
|
|
32
32
|
import { releaseForeignPushSub } from "../lib/push.js";
|
|
33
|
+
import { createSidebarNav } from "./sidebar-nav.js";
|
|
33
34
|
|
|
34
35
|
// ---------- 主界面 ----------
|
|
35
36
|
|
|
@@ -276,20 +277,7 @@ function renderLayout() {
|
|
|
276
277
|
{ class: "side-top" },
|
|
277
278
|
h("button", { class: "btn new-chat", onclick: () => (newDraft(), closeSide()) }, icon("plus"), "新建"),
|
|
278
279
|
),
|
|
279
|
-
|
|
280
|
-
h("button", { class: "btn new-chat side-nav search-nav", onclick: () => openSearch() }, icon("search"), "搜索"),
|
|
281
|
-
// 「最近」上面的入口,像 ChatGPT 的 Tasks。当前在任务页时高亮(renderSidebar 里切换)
|
|
282
|
-
(ui.tasksNav = h("button", { class: "btn new-chat side-nav", onclick: () => openTasksView() }, icon("clock"), "定时任务")),
|
|
283
|
-
// 资料库:发过的附件、agent 生成的文件
|
|
284
|
-
(ui.libraryNav = h("button", { class: "btn new-chat side-nav", onclick: () => openLibraryView() }, icon("library"), "资料库")),
|
|
285
|
-
// 远程控制:这台机器的桌面和终端。后台连着时右边亮个小绿点(renderSidebar 里切换)
|
|
286
|
-
(ui.remoteNav = h(
|
|
287
|
-
"button",
|
|
288
|
-
{ class: "btn new-chat side-nav", hidden: !remoteTabs().length, onclick: () => openRemoteView() },
|
|
289
|
-
icon("monitor"),
|
|
290
|
-
"远程控制",
|
|
291
|
-
h("span", { class: "live-dot", title: "在后台连着" }),
|
|
292
|
-
)),
|
|
280
|
+
createSidebarNav({ search: openSearch, tasks: openTasksView, library: openLibraryView, remote: openRemoteView, settings: openSettings }),
|
|
293
281
|
ui.convList,
|
|
294
282
|
// 底部只有额度:没有额度可显示(关了查询、还没查到)时整块藏起来,连分隔线一起
|
|
295
283
|
(ui.sideBottom = h(
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { fill, h, icon, toast } from "../lib/dom.js";
|
|
2
|
+
import { api } from "../lib/api.js";
|
|
3
|
+
import { closeMenu, modal, showPopover } from "../lib/overlay.js";
|
|
4
|
+
import { remoteTabs, S, ui } from "../state.js";
|
|
5
|
+
|
|
6
|
+
const ITEMS = [
|
|
7
|
+
{ id: "search", label: "搜索", icon: "search" },
|
|
8
|
+
{ id: "tasks", label: "定时任务", icon: "clock" },
|
|
9
|
+
{ id: "library", label: "资料库", icon: "library" },
|
|
10
|
+
{ id: "remote", label: "远程控制", icon: "monitor" },
|
|
11
|
+
{ id: "settings", label: "设置", icon: "gear" },
|
|
12
|
+
];
|
|
13
|
+
let actions = {};
|
|
14
|
+
let navSignature = "";
|
|
15
|
+
let hoverTimer = 0;
|
|
16
|
+
let pinned = false;
|
|
17
|
+
|
|
18
|
+
function configuredItems() {
|
|
19
|
+
const seen = new Set();
|
|
20
|
+
const items = [];
|
|
21
|
+
for (const saved of S.sidebarNav ?? []) {
|
|
22
|
+
const item = ITEMS.find((it) => it.id === saved.id);
|
|
23
|
+
if (!item || seen.has(item.id)) continue;
|
|
24
|
+
seen.add(item.id);
|
|
25
|
+
items.push({ ...item, visible: saved.visible !== false });
|
|
26
|
+
}
|
|
27
|
+
return [...items, ...ITEMS.filter((it) => !seen.has(it.id)).map((it) => ({ ...it, visible: it.id !== "settings" }))];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const available = (item) => item.id !== "remote" || remoteTabs().length > 0;
|
|
31
|
+
const isActive = (item) => ["tasks", "library", "remote"].includes(item.id) && S.view === item.id;
|
|
32
|
+
const remoteLive = () => S.remoteLive.desktop || S.remoteLive.terminals > 0;
|
|
33
|
+
|
|
34
|
+
function navButton(item, inMenu = false) {
|
|
35
|
+
return h(
|
|
36
|
+
"button",
|
|
37
|
+
{
|
|
38
|
+
class: inMenu ? "pop-item side-nav-item" : `btn new-chat side-nav${item.id === "search" ? " search-nav" : ""}`,
|
|
39
|
+
"data-nav": item.id,
|
|
40
|
+
role: inMenu ? "menuitem" : null,
|
|
41
|
+
onclick: () => {
|
|
42
|
+
closeMenu();
|
|
43
|
+
actions[item.id]?.();
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
icon(item.icon),
|
|
47
|
+
h("span", { class: "grow" }, item.label),
|
|
48
|
+
item.id === "remote" ? h("span", { class: "live-dot", title: "在后台连着" }) : null,
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function createSidebarNav(handlers) {
|
|
53
|
+
actions = handlers;
|
|
54
|
+
navSignature = "";
|
|
55
|
+
ui.sidebarNav = h("nav", { class: "sidebar-nav", "aria-label": "侧边栏菜单" });
|
|
56
|
+
syncSidebarNav();
|
|
57
|
+
return ui.sidebarNav;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 会话重绘时只更新选中和连接状态,配置或权限变化时才重建入口。 */
|
|
61
|
+
export function syncSidebarNav() {
|
|
62
|
+
if (!ui.sidebarNav) return;
|
|
63
|
+
const items = configuredItems();
|
|
64
|
+
const signature = JSON.stringify([items, remoteTabs().length > 0]);
|
|
65
|
+
if (signature !== navSignature) {
|
|
66
|
+
if (ui.menu?.dataset.for === "sidebar-more") closeMenu();
|
|
67
|
+
clearTimeout(hoverTimer);
|
|
68
|
+
navSignature = signature;
|
|
69
|
+
const direct = items.filter((it) => it.visible && available(it)).map((it) => {
|
|
70
|
+
const button = navButton(it);
|
|
71
|
+
ui[`${it.id}Nav`] = button;
|
|
72
|
+
return button;
|
|
73
|
+
});
|
|
74
|
+
const more = h(
|
|
75
|
+
"button",
|
|
76
|
+
{
|
|
77
|
+
class: "btn new-chat side-nav side-more",
|
|
78
|
+
id: "sidebar-more",
|
|
79
|
+
"aria-haspopup": "menu",
|
|
80
|
+
"aria-expanded": "false",
|
|
81
|
+
onclick: () => {
|
|
82
|
+
clearTimeout(hoverTimer);
|
|
83
|
+
if (ui.menu?.anchor === more) {
|
|
84
|
+
if (!pinned) pinned = true;
|
|
85
|
+
else closeMenu();
|
|
86
|
+
} else openMore(more, true);
|
|
87
|
+
},
|
|
88
|
+
onpointerenter: (e) => {
|
|
89
|
+
if (e.pointerType !== "mouse") return;
|
|
90
|
+
clearTimeout(hoverTimer);
|
|
91
|
+
if (ui.menu?.anchor !== more) hoverTimer = setTimeout(() => openMore(more, false), 200);
|
|
92
|
+
},
|
|
93
|
+
onpointerleave: (e) => e.pointerType === "mouse" && scheduleClose(),
|
|
94
|
+
onkeydown: (e) => {
|
|
95
|
+
if (e.key !== "ArrowDown") return;
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
openMore(more, true);
|
|
98
|
+
ui.menu?.querySelector("button")?.focus();
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
icon("more"),
|
|
102
|
+
h("span", { class: "grow" }, "更多"),
|
|
103
|
+
h("span", { class: "live-dot", title: "远程控制在后台连着" }),
|
|
104
|
+
);
|
|
105
|
+
ui.sideMore = more;
|
|
106
|
+
fill(ui.sidebarNav, ...direct, more);
|
|
107
|
+
}
|
|
108
|
+
for (const container of [ui.sidebarNav, ui.menu?.dataset.for === "sidebar-more" ? ui.menu : null]) {
|
|
109
|
+
for (const button of container?.querySelectorAll("[data-nav]") ?? []) {
|
|
110
|
+
button.classList.toggle("active", isActive({ id: button.dataset.nav }));
|
|
111
|
+
button.classList.toggle("live", button.dataset.nav === "remote" && remoteLive());
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
const folded = items.filter((it) => !it.visible && available(it));
|
|
115
|
+
ui.sideMore.classList.toggle("active", folded.some(isActive));
|
|
116
|
+
ui.sideMore.classList.toggle("live", folded.some((it) => it.id === "remote") && remoteLive());
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function scheduleClose() {
|
|
120
|
+
clearTimeout(hoverTimer);
|
|
121
|
+
hoverTimer = setTimeout(() => {
|
|
122
|
+
if (!pinned && ui.menu?.dataset.for === "sidebar-more") closeMenu();
|
|
123
|
+
}, 200);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function openMore(anchor, pin) {
|
|
127
|
+
if (!anchor.isConnected) return;
|
|
128
|
+
clearTimeout(hoverTimer);
|
|
129
|
+
const pop = showPopover(anchor);
|
|
130
|
+
pop.classList.add("side-more-menu");
|
|
131
|
+
pop.dataset.for = "sidebar-more";
|
|
132
|
+
pop.setAttribute("role", "menu");
|
|
133
|
+
pop.setAttribute("aria-label", "更多菜单");
|
|
134
|
+
pinned = pin;
|
|
135
|
+
anchor.setAttribute("aria-expanded", "true");
|
|
136
|
+
pop.onMenuClose = () => {
|
|
137
|
+
clearTimeout(hoverTimer);
|
|
138
|
+
anchor.setAttribute("aria-expanded", "false");
|
|
139
|
+
};
|
|
140
|
+
pop.onpointerenter = () => clearTimeout(hoverTimer);
|
|
141
|
+
pop.onpointerleave = (e) => e.pointerType === "mouse" && scheduleClose();
|
|
142
|
+
pop.onkeydown = (e) => {
|
|
143
|
+
const buttons = [...pop.querySelectorAll("button")];
|
|
144
|
+
const i = buttons.indexOf(document.activeElement);
|
|
145
|
+
if (e.key === "Escape") {
|
|
146
|
+
e.preventDefault();
|
|
147
|
+
e.stopPropagation();
|
|
148
|
+
closeMenu();
|
|
149
|
+
anchor.focus();
|
|
150
|
+
} else if (["ArrowDown", "ArrowUp", "Home", "End"].includes(e.key)) {
|
|
151
|
+
e.preventDefault();
|
|
152
|
+
const next = e.key === "Home" ? 0 : e.key === "End" ? buttons.length - 1 : (i + (e.key === "ArrowDown" ? 1 : -1) + buttons.length) % buttons.length;
|
|
153
|
+
buttons[next]?.focus();
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
const folded = configuredItems().filter((it) => !it.visible && available(it));
|
|
157
|
+
fill(
|
|
158
|
+
pop,
|
|
159
|
+
...folded.map((it) => navButton(it, true)),
|
|
160
|
+
folded.length ? h("div", { class: "sep" }) : null,
|
|
161
|
+
h("button", { class: "pop-item", role: "menuitem", onclick: () => (closeMenu(), editSidebar()) }, icon("edit"), "编辑侧边栏"),
|
|
162
|
+
);
|
|
163
|
+
syncSidebarNav();
|
|
164
|
+
const r = anchor.getBoundingClientRect();
|
|
165
|
+
const width = pop.offsetWidth;
|
|
166
|
+
const side = innerWidth > 768 && r.right + width + 16 <= innerWidth;
|
|
167
|
+
pop.style.left = `${Math.max(8, Math.min(side ? r.right + 8 : r.left, innerWidth - width - 8))}px`;
|
|
168
|
+
pop.style.top = `${Math.max(8, Math.min(side ? r.top : r.bottom + 6, innerHeight - pop.offsetHeight - 8))}px`;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function editSidebar() {
|
|
172
|
+
const draft = configuredItems();
|
|
173
|
+
const list = h("div", { class: "sidebar-edit-list", role: "list", "aria-label": "菜单显示与顺序" });
|
|
174
|
+
const rows = new Map();
|
|
175
|
+
let saving = false;
|
|
176
|
+
const move = (id, to) => {
|
|
177
|
+
const from = draft.findIndex((it) => it.id === id);
|
|
178
|
+
if (from === to || to < 0 || to >= draft.length) return;
|
|
179
|
+
draft.splice(to, 0, ...draft.splice(from, 1));
|
|
180
|
+
for (const it of draft) list.append(rows.get(it.id));
|
|
181
|
+
};
|
|
182
|
+
for (const item of draft) {
|
|
183
|
+
const checkbox = h("input", {
|
|
184
|
+
type: "checkbox",
|
|
185
|
+
checked: item.visible,
|
|
186
|
+
onchange: () => (item.visible = checkbox.checked),
|
|
187
|
+
});
|
|
188
|
+
let drag = null;
|
|
189
|
+
const endDrag = () => {
|
|
190
|
+
drag = null;
|
|
191
|
+
row.classList.remove("dragging");
|
|
192
|
+
if (handle.hasPointerCapture(dragPointer)) handle.releasePointerCapture(dragPointer);
|
|
193
|
+
};
|
|
194
|
+
let dragPointer = 0;
|
|
195
|
+
const handle = h("button", {
|
|
196
|
+
class: "sidebar-edit-grip",
|
|
197
|
+
type: "button",
|
|
198
|
+
title: "拖动排序,也可用上下方向键调整",
|
|
199
|
+
"aria-label": `调整${item.label}顺序`,
|
|
200
|
+
onpointerdown: (e) => {
|
|
201
|
+
if (e.button !== 0 || saving) return;
|
|
202
|
+
e.preventDefault();
|
|
203
|
+
drag = { y: e.clientY };
|
|
204
|
+
dragPointer = e.pointerId;
|
|
205
|
+
handle.setPointerCapture(e.pointerId);
|
|
206
|
+
},
|
|
207
|
+
onpointermove: (e) => {
|
|
208
|
+
if (!drag || Math.abs(e.clientY - drag.y) < 4) return;
|
|
209
|
+
row.classList.add("dragging");
|
|
210
|
+
const to = draft.filter((it) => it.id !== item.id).filter((it) => {
|
|
211
|
+
const r = rows.get(it.id).getBoundingClientRect();
|
|
212
|
+
return e.clientY > r.top + r.height / 2;
|
|
213
|
+
}).length;
|
|
214
|
+
move(item.id, to);
|
|
215
|
+
handle.setPointerCapture(e.pointerId);
|
|
216
|
+
},
|
|
217
|
+
onpointerup: endDrag,
|
|
218
|
+
onpointercancel: endDrag,
|
|
219
|
+
onkeydown: (e) => {
|
|
220
|
+
if (saving || !["ArrowUp", "ArrowDown"].includes(e.key)) return;
|
|
221
|
+
e.preventDefault();
|
|
222
|
+
move(item.id, draft.indexOf(item) + (e.key === "ArrowUp" ? -1 : 1));
|
|
223
|
+
handle.focus();
|
|
224
|
+
},
|
|
225
|
+
}, "⋮⋮");
|
|
226
|
+
const row = h(
|
|
227
|
+
"div", { class: "sidebar-edit-row", role: "listitem", "data-nav": item.id },
|
|
228
|
+
h("label", {}, checkbox, icon(item.icon), h("span", { class: "grow" }, item.label)),
|
|
229
|
+
handle,
|
|
230
|
+
);
|
|
231
|
+
rows.set(item.id, row);
|
|
232
|
+
list.append(row);
|
|
233
|
+
}
|
|
234
|
+
const body = h("div", { class: "modal-body" }, h("p", { class: "sidebar-edit-hint" }, "勾选的菜单直接显示,未勾选的收进「更多」。拖动右侧手柄调整顺序。"), list);
|
|
235
|
+
const close = () => {
|
|
236
|
+
if (saving) return;
|
|
237
|
+
document.removeEventListener("keydown", onKey, true);
|
|
238
|
+
dismiss();
|
|
239
|
+
ui.sideMore?.focus();
|
|
240
|
+
};
|
|
241
|
+
const onKey = (e) => {
|
|
242
|
+
if (e.key === "Tab") {
|
|
243
|
+
const focusable = [...dialog.querySelectorAll("button:not(:disabled), input:not(:disabled)")];
|
|
244
|
+
const edge = e.shiftKey ? focusable[0] : focusable.at(-1);
|
|
245
|
+
if (document.activeElement === edge) {
|
|
246
|
+
e.preventDefault();
|
|
247
|
+
(e.shiftKey ? focusable.at(-1) : focusable[0])?.focus();
|
|
248
|
+
}
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
if (e.key !== "Escape") return;
|
|
252
|
+
e.preventDefault();
|
|
253
|
+
e.stopPropagation();
|
|
254
|
+
close();
|
|
255
|
+
};
|
|
256
|
+
const done = h("button", {
|
|
257
|
+
class: "btn primary",
|
|
258
|
+
onclick: async () => {
|
|
259
|
+
if (saving) return;
|
|
260
|
+
saving = true;
|
|
261
|
+
done.disabled = true;
|
|
262
|
+
done.textContent = "保存中…";
|
|
263
|
+
list.inert = true;
|
|
264
|
+
try {
|
|
265
|
+
const items = draft.map(({ id, visible }) => ({ id, visible }));
|
|
266
|
+
const result = await api("/api/ui/sidebar-nav", { method: "PUT", body: { items } });
|
|
267
|
+
S.sidebarNav = result.items;
|
|
268
|
+
syncSidebarNav();
|
|
269
|
+
saving = false;
|
|
270
|
+
close();
|
|
271
|
+
} catch (err) {
|
|
272
|
+
toast(`侧边栏保存失败:${err.message}`);
|
|
273
|
+
} finally {
|
|
274
|
+
saving = false;
|
|
275
|
+
done.disabled = false;
|
|
276
|
+
done.textContent = "完成";
|
|
277
|
+
list.inert = false;
|
|
278
|
+
}
|
|
279
|
+
},
|
|
280
|
+
}, "完成");
|
|
281
|
+
const dismiss = modal({
|
|
282
|
+
title: "编辑侧边栏",
|
|
283
|
+
className: "sidebar-editor",
|
|
284
|
+
body,
|
|
285
|
+
foot: h("div", { class: "modal-foot" }, h("button", { class: "btn ghost", onclick: close }, "取消"), done),
|
|
286
|
+
beforeClose: () => {
|
|
287
|
+
if (saving) return false;
|
|
288
|
+
close();
|
|
289
|
+
return false;
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
const dialog = body.closest(".modal");
|
|
293
|
+
dialog.setAttribute("role", "dialog");
|
|
294
|
+
dialog.setAttribute("aria-modal", "true");
|
|
295
|
+
dialog.setAttribute("aria-label", "编辑侧边栏");
|
|
296
|
+
dialog.querySelector(".modal-head .icon-btn").setAttribute("aria-label", "关闭");
|
|
297
|
+
document.addEventListener("keydown", onKey, true);
|
|
298
|
+
list.querySelector("input")?.focus();
|
|
299
|
+
}
|