🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
🐛 fix(agent-documents): refuse raw JSON bodies that Markdown would rewrite (#20411)
Arvin Xu4f81d1629f5b
diff --git a/apps/server/src/services/agentDocuments/headlessEditor.test.ts b/apps/server/src/services/agentDocuments/headlessEditor.test.tsindex 55776a14fa8..68b64265005 100644--- a/apps/server/src/services/agentDocuments/headlessEditor.test.ts+++ b/apps/server/src/services/agentDocuments/headlessEditor.test.ts@@ -5,6 +5,7 @@ import { isValidEditorData } from '@/libs/editor/isValidEditorData';import {applyLiteXMLOperations,+ createAgentMarkdownSnapshot,createMarkdownEditorSnapshot,exportEditorDataSnapshot,} from './headlessEditor';@@ -254,4 +255,48 @@ describe('agent document headless editor', () => {expect(result.content).toContain('<model>');});});++ describe('raw JSON content', () => {+ // Shape of a real createDocument body: a JSON lorebook whose strings hold+ // escaped quotes, `\n` and a `__` placeholder.+ const json = JSON.stringify(+ {+ entries: [+ {+ content: '维护"谁知道什么"的全表。\n压缩办法:压成"背景事件:__(影响已消化)"一行。',+ key: ['曝光', '知道'],+ },+ { content: '其余在校人员:__(不知情)', key: [] },+ ],+ },+ null,+ 2,+ );++ it('rejects raw JSON with a way to store it verbatim instead of rewriting it', async () => {+ // Markdown alone drops the escapes and pairs the placeholders into bold.+ const markdown = await createMarkdownEditorSnapshot(json);+ expect(() => JSON.parse(markdown.content)).toThrow();++ await expect(createAgentMarkdownSnapshot(json)).rejects.toThrow(+ /raw JSON.*Nothing was saved\. Wrap the JSON in a fenced code block/,+ );+ });++ it('stores the same JSON byte-exact inside a json code fence', async () => {+ const fenced = `\`\`\`json\n${json}\n\`\`\``;+ const snapshot = await createAgentMarkdownSnapshot(fenced);+ const body = snapshot.content.match(/```json\n([\S\s]*?)\n```/)?.[1];++ expect(body).toBe(json);+ expect((await createAgentMarkdownSnapshot(snapshot.content)).content).toBe(snapshot.content);+ });++ it('keeps accepting Markdown that only starts like JSON', async () => {+ await expect(+ createAgentMarkdownSnapshot('[link](https://example.com) text'),+ ).resolves.toBeDefined();+ await expect(createAgentMarkdownSnapshot('{draft} notes')).resolves.toBeDefined();+ });+ });});diff --git a/apps/server/src/services/agentDocuments/headlessEditor.ts b/apps/server/src/services/agentDocuments/headlessEditor.tsindex 121935ab357..d3c1d2357b7 100644--- a/apps/server/src/services/agentDocuments/headlessEditor.ts+++ b/apps/server/src/services/agentDocuments/headlessEditor.ts@@ -216,6 +216,18 @@ export const createMarkdownEditorSnapshot = async (const LITEXML_DOCUMENT_PATTERN = /^\s*(?:<\?xml[\s?]|<root[\s>])/;+const isJsonValueDocument = (content: string): boolean => {+ const trimmed = content.trim();+ if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) return false;++ try {+ JSON.parse(trimmed);+ return true;+ } catch {+ return false;+ }+};+/*** Markdown snapshot for content written by an agent. Rejects input that would* silently become an empty document — most often LiteXML sent to a Markdown@@ -230,6 +242,15 @@ export const createAgentMarkdownSnapshot = async ();}+ // Markdown consumes backslash escapes (`\"` → `"`) and reads `__…__` / `*…*`+ // as formatting, so a raw JSON body is silently rewritten into text that no+ // longer parses. Fenced code is stored verbatim.+ if (isJsonValueDocument(content)) {+ throw new Error(+ 'Document content is raw JSON, but this API stores Markdown: Markdown parsing would drop backslash escapes and turn `__` or `*` into formatting, so the stored JSON would no longer parse. Nothing was saved. Wrap the JSON in a fenced code block (```json … ```); code blocks are stored exactly as written.',+ );+ }+const snapshot = await createMarkdownEditorSnapshot(content);if (content.trim().length > 0 && snapshot.content.trim().length === 0) {diff --git a/packages/builtin-tool-agent-documents/src/manifest.ts b/packages/builtin-tool-agent-documents/src/manifest.tsindex 12ad3933c0a..e27006c55e9 100644--- a/packages/builtin-tool-agent-documents/src/manifest.ts+++ b/packages/builtin-tool-agent-documents/src/manifest.ts@@ -20,7 +20,8 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {parameters: {properties: {content: {- description: 'Document content in markdown or plain text.',+ description:+ 'Document content in Markdown. Put JSON, code or any text that must stay byte-exact inside a fenced code block (```json … ```); outside code, Markdown syntax such as `\\`, `__` or `*` is interpreted.',type: 'string',},hintIsSkill: {@@ -93,7 +94,8 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {parameters: {properties: {content: {- description: 'New full document content.',+ description:+ 'New full document content in Markdown. Put JSON, code or any text that must stay byte-exact inside a fenced code block (```json … ```).',type: 'string',},id: {