Skip to content

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

:bug: Trim boundary whitespace from agent user messages

Daniel1ff61d7f3ec8
diff --git a/app/src/layout/dock/agent/AgentComposer.ts b/app/src/layout/dock/agent/AgentComposer.ts
index 35c7f25996..36fea221d7 100644
--- a/app/src/layout/dock/agent/AgentComposer.ts
+++ b/app/src/layout/dock/agent/AgentComposer.ts
@@ -354,7 +354,50 @@ export function mountComposer(host: HTMLElement, onSend: () => void, onChange?:
};
const getBlockHTML = (): string => {
- return fragment.getBlockHTML(resetEmbedBlocks);
+ return fragment.getBlockHTML((element) => {
+ resetEmbedBlocks(element);
+ // 仅清理消息边界的段落空白,保留内部空行和非文本内容。
+ const trimBoundary = (fromStart: boolean) => {
+ const trimNode = (node: Node): boolean => {
+ if (node.nodeType === Node.TEXT_NODE) {
+ const pattern = fromStart ? `^[\\s${Constants.ZWSP}]+` : `[\\s${Constants.ZWSP}]+$`;
+ node.textContent = (node.textContent || "").replace(new RegExp(pattern), "");
+ return !node.textContent;
+ }
+ if (!(node instanceof HTMLElement)) {
+ return false;
+ }
+ if (node.tagName === "BR") {
+ node.remove();
+ return true;
+ }
+ const types = (node.getAttribute("data-type") || "").split(" ").filter(Boolean);
+ if (node.tagName !== "SPAN" || types.some(type =>
+ !["strong", "em", "s", "u", "mark", "sup", "sub", "text"].includes(type))) {
+ return false;
+ }
+ return trimChildren(node);
+ };
+ const trimChildren = (parent: HTMLElement): boolean => {
+ const nodes = Array.from(parent.childNodes);
+ if (!fromStart) {
+ nodes.reverse();
+ }
+ return nodes.every(trimNode);
+ };
+ let block = fromStart ? element.firstElementChild : element.lastElementChild;
+ while (block?.getAttribute("data-type") === "NodeParagraph") {
+ const content = block.querySelector<HTMLElement>('[contenteditable="true"]');
+ if (!content || !trimChildren(content)) {
+ break;
+ }
+ block.remove();
+ block = fromStart ? element.firstElementChild : element.lastElementChild;
+ }
+ };
+ trimBoundary(true);
+ trimBoundary(false);
+ });
};
return {