Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/18826

Vanessacb7dd0e0912c
diff --git a/app/src/assets/scss/business/_ai_agent.scss b/app/src/assets/scss/business/_ai_agent.scss
index b9a106818c..37664225e2 100644
--- a/app/src/assets/scss/business/_ai_agent.scss
+++ b/app/src/assets/scss/business/_ai_agent.scss
@@ -1169,6 +1169,48 @@
overflow-y: auto;
}
+.agent-chat__composer-host,
+.agent-chat__msg--user {
+ span[data-type~="text"][custom-agent-skill="true"] {
+ margin: 0 4px;
+ display: inline;
+ position: relative;
+ box-sizing: border-box;
+ top: -1.5px;
+ padding: 3px 8px 3px 22px;
+ border: 1px solid var(--b3-theme-primary-lighter);
+ border-radius: var(--b3-border-radius-b);
+ background-color: var(--b3-theme-primary-lightest);
+ color: var(--b3-theme-primary);
+ font-family: var(--b3-font-family-code);
+ font-size: .8125rem;
+ font-weight: 500;
+ line-height: 16px;
+ white-space: nowrap;
+ vertical-align: baseline;
+
+ &::before {
+ content: "✦";
+ display: inline-flex;
+ align-items: center;
+ justify-content: center;
+ position: absolute;
+ top: 5px;
+ left: 6px;
+ width: 12px;
+ height: 12px;
+ font-family: var(--b3-font-family);
+ font-size: 12px;
+ line-height: 12px;
+ pointer-events: none;
+ }
+
+ &:hover {
+ border-color: var(--b3-theme-primary-light);
+ }
+ }
+}
+
.protyle-hint--agent-overlay {
font-size: var(--b3-font-size);
}
diff --git a/app/src/layout/dock/agent/AgentComposer.ts b/app/src/layout/dock/agent/AgentComposer.ts
index b318ce77b8..ab1fe2c9dd 100644
--- a/app/src/layout/dock/agent/AgentComposer.ts
+++ b/app/src/layout/dock/agent/AgentComposer.ts
@@ -36,6 +36,7 @@ interface ComposerHandle {
type OnChangeCallback = () => void;
const AGENT_HINT_OVERLAY_CLASS = "protyle-hint--agent-overlay";
+const AGENT_SKILL_SELECTOR = '[data-type~="text"][custom-agent-skill="true"]';
const skillHintRequestIDs = new WeakMap<IProtyle, number>();
interface ComposerOptions {
@@ -69,7 +70,7 @@ const hintAgentRef = (key: string, protyle: IProtyle, source: THintSource): IHin
return hintRef(key, protyle, source);
};
-// / 技能菜单:异步拉取 lsSkills,选中后把技能名作为纯文本插入(value 即技能名)。
+// / 技能菜单:异步拉取 lsSkills,选中后把技能名作为带持久标识的智能体行级元素插入。
// 返回 [] 占位,数据在 fetch 回调里通过 protyle.hint.genHTML 填充(与 hintRef 异步模式一致)。
const hintSkill = (key: string, protyle: IProtyle): IHintData[] => {
const requestID = (skillHintRequestIDs.get(protyle) || 0) + 1;
@@ -100,8 +101,10 @@ const hintSkill = (key: string, protyle: IProtyle): IHintData[] => {
.filter((s: Record<string, string>) => !q ||
(s.name || "").toLowerCase().includes(q) || (s.description || "").toLowerCase().includes(q))
.map((s: Record<string, string>) => ({
- value: s.name + " ",
- html: '<div class="b3-list-item__first"><span class="b3-list-item__text">' +
+ value: '<span data-type="text" custom-agent-skill="true">' +
+ escapeHtml(s.name) + "</span> ",
+ html: '<div class="b3-list-item__first"><svg class="b3-list-item__graphic">' +
+ '<use xlink:href="#iconSparkles"></use></svg><span class="b3-list-item__text">' +
escapeHtml(s.name) + "</span></div>" +
(s.description ? '<div class="b3-list-item__meta b3-list-item__showall">' + escapeHtml(s.description) + "</div>" : ""),
}));
@@ -230,6 +233,8 @@ export function mountComposer(host: HTMLElement, onSend: () => void, onChange?:
hintElement.classList.add(AGENT_HINT_OVERLAY_CLASS);
document.body.appendChild(hintElement);
wysiwyg.element.setAttribute("data-readonly", "false");
+ // 智能体输入区高度较小,工具栏子面板需以窗口而非编辑器作为垂直边界。
+ p.toolbar.subElement.setAttribute("data-position-boundary", "viewport");
const setEmptyContent = () => {
wysiwyg.element.innerHTML = "";
@@ -327,7 +332,11 @@ export function mountComposer(host: HTMLElement, onSend: () => void, onChange?:
}, true);
const getMarkdown = (): string => {
- return p.lute.BlockDOM2StdMd(wysiwyg.element.innerHTML).trim();
+ const clone = wysiwyg.element.cloneNode(true) as HTMLElement;
+ clone.querySelectorAll(AGENT_SKILL_SELECTOR).forEach((skillElement) => {
+ skillElement.replaceWith(document.createTextNode(skillElement.textContent || ""));
+ });
+ return p.lute.BlockDOM2StdMd(clone.innerHTML).trim();
};
const getBlockHTML = (): string => {
diff --git a/app/src/protyle/toolbar/index.ts b/app/src/protyle/toolbar/index.ts
index 7a0b17e14d..2ddf303c1e 100644
--- a/app/src/protyle/toolbar/index.ts
+++ b/app/src/protyle/toolbar/index.ts
@@ -176,8 +176,10 @@ export class Toolbar {
return;
}
const protyleRect = protyle.element.getBoundingClientRect();
- const topBoundary = protyleRect.top + 30;
- const bottomBoundary = Math.min(protyleRect.bottom, window.innerHeight);
+ const viewportBoundary = element.dataset.positionBoundary === "viewport";
+ const topBoundary = viewportBoundary ? 8 : protyleRect.top + 30;
+ const bottomBoundary = viewportBoundary ? window.innerHeight - 8 :
+ Math.min(protyleRect.bottom, window.innerHeight);
const rangeRects = this.range.getClientRects();
const rangeRect = (typeof this.rangePosition.rectIndex === "number" ?
rangeRects[this.rangePosition.rectIndex] : undefined) ||