Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797

Daniel5de241841d77
diff --git a/app/src/assets/scss/business/_agentChat.scss b/app/src/assets/scss/business/_agentChat.scss
index c3b12885eb..17e69838d8 100644
--- a/app/src/assets/scss/business/_agentChat.scss
+++ b/app/src/assets/scss/business/_agentChat.scss
@@ -340,8 +340,7 @@
border: 1px solid var(--b3-border-color);
border-radius: var(--b3-border-radius);
overflow: hidden;
- font-size: 13px;
- background: var(--b3-theme-background);
+ background: transparent;
}
.agent-chat__msg--thinking-done {
@@ -358,7 +357,7 @@
gap: 8px;
padding: 4px 8px;
cursor: pointer;
- background: var(--b3-theme-background);
+ background: transparent;
color: var(--b3-theme-on-surface-light);
user-select: none;
font-style: italic;
@@ -393,12 +392,11 @@
.agent-chat__thinking-body {
padding: 8px 12px;
- background: var(--b3-theme-surface);
+ background: transparent;
border-top: 1px solid var(--b3-border-color);
}
.agent-chat__thinking-reasoning-text {
- font-size: 13px;
color: var(--b3-theme-on-surface);
line-height: 1.5;
margin-bottom: 8px;
@@ -407,7 +405,6 @@
}
.agent-chat__thinking-chat {
- font-size: 13px;
color: var(--b3-theme-on-surface);
line-height: 1.5;
margin-bottom: 8px;
diff --git a/app/src/layout/dock/AgentComposer.ts b/app/src/layout/dock/AgentComposer.ts
index a49c92eb1d..18baa206ea 100644
--- a/app/src/layout/dock/AgentComposer.ts
+++ b/app/src/layout/dock/AgentComposer.ts
@@ -54,6 +54,10 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa
let historyIdx = -1;
let savedDraft = "";
+ let slashActive = false;
+ let slashRange: {from: number; to: number} | null = null;
+ let cachedSkills: BlockHit[] | null = null;
+
const updateHighlight = () => {
if (!suggestionMenu) { return; }
const items = suggestionMenu.querySelectorAll(".agent-mention-menu__item");
@@ -203,92 +207,48 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa
},
},
}),
- Mention.configure({
- HTMLAttributes: {class: "agent-mention-chip"},
- suggestion: {
- char: "/",
- items: async function ({query}): Promise<BlockHit[]> {
- try {
- const resp = await fetch("/api/ai/agent/lsSkills", {
- method: "POST",
- headers: {"Content-Type": "application/json"},
- });
- const data = await resp.json();
- const skills = (data && data.data) ? data.data : [];
- const q = query.toLowerCase();
- return skills.filter(function (s: Record<string, string>) {
- return !q || s.name.toLowerCase().includes(q) || s.description.toLowerCase().includes(q);
- }).map(function (s: Record<string, string>) {
- return {
- id: s.name,
- label: s.name,
- icon: "",
- hPath: s.description || "",
- };
- });
- } catch (e) {
- return [];
- }
- },
- command: function ({editor: ed, range, props}) {
- const item = props as BlockHit;
- ed.chain().focus().deleteRange(range).insertContent(item.label + " ").run();
- },
- render: function () {
- return {
- onStart: function (props) {
- suggestionCommand = function (item) { props.command(item); };
- openMenu(props.items as BlockHit[], suggestionCommand!, props.clientRect?.bind(props));
- },
- onUpdate: function (props) {
- suggestionCommand = function (item) { props.command(item); };
- openMenu(props.items as BlockHit[], suggestionCommand!, props.clientRect?.bind(props));
- },
- onExit: function () { closeMenu(); },
- onKeyDown: function (props) {
- if (!suggestionMenu) { return false; }
- if (props.event.key === "ArrowDown") {
- props.event.preventDefault();
- const items = suggestionMenu.querySelectorAll(".agent-mention-menu__item");
- if (items.length > 0) {
- selectedIndex = (selectedIndex + 1) % items.length;
- updateHighlight();
- }
- return true;
- }
- if (props.event.key === "ArrowUp") {
- props.event.preventDefault();
- const items = suggestionMenu.querySelectorAll(".agent-mention-menu__item");
- if (items.length > 0) {
- selectedIndex = (selectedIndex - 1 + items.length) % items.length;
- updateHighlight();
- }
- return true;
- }
- if (props.event.key === "Enter") {
- props.event.preventDefault();
- if (suggestionItems.length > 0 && suggestionCommand) {
- const idx = selectedIndex;
- if (idx >= 0 && idx < suggestionItems.length) {
- suggestionCommand(suggestionItems[idx]);
- }
- }
- return true;
- }
- if (props.event.key === "Escape") {
- closeMenu();
- return true;
- }
- return false;
- },
- };
- },
- },
- }),
],
editorProps: {
attributes: {class: "agent-composer__pm"},
handleKeyDown: function (_view, event) {
+ if (suggestionMenu && slashActive) {
+ if (event.key === "ArrowDown") {
+ event.preventDefault();
+ const items = suggestionMenu.querySelectorAll(".agent-mention-menu__item");
+ if (items.length > 0) {
+ selectedIndex = (selectedIndex + 1) % items.length;
+ updateHighlight();
+ }
+ return true;
+ }
+ if (event.key === "ArrowUp") {
+ event.preventDefault();
+ const items = suggestionMenu.querySelectorAll(".agent-mention-menu__item");
+ if (items.length > 0) {
+ selectedIndex = (selectedIndex - 1 + items.length) % items.length;
+ updateHighlight();
+ }
+ return true;
+ }
+ if (event.key === "Enter") {
+ event.preventDefault();
+ if (suggestionItems.length > 0 && suggestionCommand) {
+ const idx = selectedIndex;
+ if (idx >= 0 && idx < suggestionItems.length) {
+ suggestionCommand(suggestionItems[idx]);
+ }
+ }
+ return true;
+ }
+ if (event.key === "Escape") {
+ event.preventDefault();
+ closeMenu();
+ slashActive = false;
+ slashRange = null;
+ return true;
+ }
+ return false;
+ }
if (event.key === "Enter" && !event.shiftKey && !suggestionMenu) {
event.preventDefault();
onSend();
@@ -331,6 +291,62 @@ export function mountComposer(host: HTMLElement, onSend: () => void): ComposerHa
},
});
+ editor.on("update", function () {
+ if (suggestionMenu && !slashActive) { return; }
+ const {$from} = editor.state.selection;
+ const textBefore = $from.parent.textBetween(0, $from.parentOffset);
+ const match = textBefore.match(/(?:^|\s)\/(\S*)$/);
+ if (match) {
+ const query = match[1];
+ const slashPos = $from.pos - query.length - 1;
+ slashActive = true;
+ slashRange = {from: slashPos, to: $from.pos};
+
+ const filterAndOpen = function (skills: BlockHit[]) {
+ const q = query.toLowerCase();
+ const filtered = !q ? skills : skills.filter(function (s) {
+ return s.label.toLowerCase().includes(q) || s.hPath.toLowerCase().includes(q);
+ });
+ suggestionCommand = function (item: BlockHit) {
+ editor.chain().focus().deleteRange({from: slashRange!.from, to: slashRange!.to}).insertContent(item.label + " ").run();
+ closeMenu();
+ slashActive = false;
+ slashRange = null;
+ };
+ openMenu(filtered, suggestionCommand!);
+ cachedSkills = skills;
+ };
+
+ if (cachedSkills) {
+ filterAndOpen(cachedSkills);
+ } else {
+ fetch("/api/ai/agent/lsSkills", {
+ method: "POST",
+ headers: {"Content-Type": "application/json"},
+ }).then(function (r) { return r.json(); }).then(function (data) {
+ const rawSkills = (data && data.data) ? data.data : [];
+ const items: BlockHit[] = rawSkills.map(function (s: Record<string, string>) {
+ return {
+ id: s.name,
+ label: s.name,
+ icon: "",
+ hPath: s.description || "",
+ };
+ });
+ filterAndOpen(items);
+ }).catch(function () {
+ closeMenu();
+ slashActive = false;
+ slashRange = null;
+ });
+ }
+ } else if (slashActive) {
+ closeMenu();
+ slashActive = false;
+ slashRange = null;
+ }
+ });
+
return {
focus: function () { editor.commands.focus(); },
destroy: function () { closeMenu(); editor.destroy(); },