Skip to content

🀯 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.

πŸ› fix(agent-documents): accept backing IDs when reading (#17763)

YuTengjing4b968a634792
diff --git a/apps/server/src/services/agentDocuments/index.test.ts b/apps/server/src/services/agentDocuments/index.test.ts
index d2f33ad81e6..2fb52014b4e 100644
--- a/apps/server/src/services/agentDocuments/index.test.ts
+++ b/apps/server/src/services/agentDocuments/index.test.ts
@@ -94,6 +94,7 @@ describe('AgentDocumentsService', () => {
findById: vi.fn(),
findByAgent: vi.fn(),
findContextByAgent: vi.fn(),
+ findByDocumentId: vi.fn(),
findByDocumentIds: vi.fn(),
findByFilename: vi.fn(),
findByParentAndFilename: vi.fn(),
@@ -568,26 +569,48 @@ describe('AgentDocumentsService', () => {
describe('getDocumentSnapshotById', () => {
it('should fall back to markdown content when editor data is empty', async () => {
+ const agentDocumentId = '11111111-1111-4111-8111-111111111111';
mockModel.findById.mockResolvedValue({
agentId: 'agent-1',
content: 'fallback content',
editorData: { root: { children: [] } },
- id: 'agent-doc-1',
+ id: agentDocumentId,
title: 'Doc',
});
const service = new AgentDocumentsService(db, userId);
- const result = await service.getDocumentSnapshotById('agent-doc-1', 'agent-1');
+ const result = await service.getDocumentSnapshotById(agentDocumentId, 'agent-1');
expect(result).toEqual({
agentId: 'agent-1',
content: 'fallback content',
editorData: { root: { children: [] } },
- id: 'agent-doc-1',
+ id: agentDocumentId,
litexml: '<p id="node-1">content</p>',
title: 'Doc',
});
});
+
+ it('should resolve a backing document id without querying the UUID binding column', async () => {
+ const agentDocumentId = '11111111-1111-4111-8111-111111111111';
+ mockModel.findByDocumentId.mockResolvedValue({
+ agentId: 'agent-1',
+ content: 'backing content',
+ documentId: 'docs_backing-doc-1',
+ id: agentDocumentId,
+ title: 'Doc',
+ });
+
+ const service = new AgentDocumentsService(db, userId);
+ const result = await service.getDocumentSnapshotById('docs_backing-doc-1', 'agent-1');
+
+ expect(mockModel.findByDocumentId).toHaveBeenCalledWith('agent-1', 'docs_backing-doc-1');
+ expect(mockModel.findById).not.toHaveBeenCalled();
+ expect(result).toMatchObject({
+ documentId: 'docs_backing-doc-1',
+ id: agentDocumentId,
+ });
+ });
});
describe('upsertDocumentByFilename', () => {
diff --git a/apps/server/src/services/agentDocuments/index.ts b/apps/server/src/services/agentDocuments/index.ts
index 45d52df2ea1..cc8ee54610c 100644
--- a/apps/server/src/services/agentDocuments/index.ts
+++ b/apps/server/src/services/agentDocuments/index.ts
@@ -25,6 +25,7 @@ import {
extractMarkdownH1Title,
} from '@/database/models/agentDocuments';
import { TopicDocumentModel } from '@/database/models/topicDocument';
+import { isUuid } from '@/database/utils/uuid';
import { AgentDocumentVfsError } from '../agentDocumentVfs/errors';
import { isManagedSkillDocument } from '../agentDocumentVfs/mounts/skills/providers/providerSkillsAgentDocumentUtils';
@@ -407,7 +408,10 @@ export class AgentDocumentsService {
}
async getDocumentById(id: string, expectedAgentId?: string) {
- return this.getDocumentByIdInAgent(id, expectedAgentId);
+ const doc = await this.findReadableDocumentById(id, expectedAgentId);
+ if (!doc) return undefined;
+
+ return this.projectDocumentContent(doc);
}
/**
@@ -421,10 +425,8 @@ export class AgentDocumentsService {
}
async getDocumentSnapshotById(id: string, expectedAgentId?: string) {
- const doc = await this.agentDocumentModel.findById(id);
-
+ const doc = await this.findReadableDocumentById(id, expectedAgentId);
if (!doc) return undefined;
- if (expectedAgentId && doc.agentId !== expectedAgentId) return undefined;
return this.attachLiteXML(doc);
}
@@ -436,6 +438,25 @@ export class AgentDocumentsService {
return this.attachLiteXML(doc);
}
+ /**
+ * Resolve either the agent-document binding id exposed as `id` or the backing
+ * `documents.id` exposed as `documentId`. Both identifiers appear in document
+ * discovery results, and older callers may pass the latter back to read APIs.
+ * Branch before querying the UUID column so a backing id cannot trigger a
+ * PostgreSQL 22P02 error.
+ */
+ private async findReadableDocumentById(id: string, expectedAgentId?: string) {
+ const doc = isUuid(id)
+ ? await this.agentDocumentModel.findById(id)
+ : expectedAgentId
+ ? await this.agentDocumentModel.findByDocumentId(expectedAgentId, id)
+ : undefined;
+ if (!doc) return undefined;
+ if (expectedAgentId && doc.agentId !== expectedAgentId) return undefined;
+
+ return doc;
+ }
+
private async getDocumentByIdInAgent(documentId: string, expectedAgentId?: string) {
const doc = await this.agentDocumentModel.findById(documentId);
diff --git a/packages/builtin-tool-agent-documents/src/manifest.ts b/packages/builtin-tool-agent-documents/src/manifest.ts
index a40e7a2efc7..3d8ed48cd40 100644
--- a/packages/builtin-tool-agent-documents/src/manifest.ts
+++ b/packages/builtin-tool-agent-documents/src/manifest.ts
@@ -3,6 +3,9 @@ import type { BuiltinToolManifest } from '@lobechat/types';
import { systemPrompt } from './systemRole';
import { AgentDocumentsApiName, AgentDocumentsIdentifier } from './types';
+const AGENT_DOCUMENT_ID_DESCRIPTION =
+ 'Target agent document ID. Use the "id" field returned by listDocuments, not "documentId".';
+
export const AgentDocumentsManifest: BuiltinToolManifest = {
api: [
{
@@ -55,7 +58,7 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {
type: 'string',
},
id: {
- description: 'Target document ID.',
+ description: AGENT_DOCUMENT_ID_DESCRIPTION,
type: 'string',
},
},
@@ -74,7 +77,7 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {
type: 'string',
},
id: {
- description: 'Target document ID.',
+ description: AGENT_DOCUMENT_ID_DESCRIPTION,
type: 'string',
},
},
@@ -93,7 +96,7 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {
parameters: {
properties: {
id: {
- description: 'Target document ID.',
+ description: AGENT_DOCUMENT_ID_DESCRIPTION,
type: 'string',
},
operations: {
@@ -159,7 +162,7 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {
parameters: {
properties: {
id: {
- description: 'Target document ID.',
+ description: AGENT_DOCUMENT_ID_DESCRIPTION,
type: 'string',
},
},
@@ -178,7 +181,7 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {
parameters: {
properties: {
id: {
- description: 'Target document ID.',
+ description: AGENT_DOCUMENT_ID_DESCRIPTION,
type: 'string',
},
newTitle: {
@@ -253,7 +256,7 @@ export const AgentDocumentsManifest: BuiltinToolManifest = {
parameters: {
properties: {
id: {
- description: 'Target document ID.',
+ description: AGENT_DOCUMENT_ID_DESCRIPTION,
type: 'string',
},
rule: {
diff --git a/packages/builtin-tool-agent-documents/src/systemRole.test.ts b/packages/builtin-tool-agent-documents/src/systemRole.test.ts
new file mode 100644
index 00000000000..34427373e33
--- /dev/null
+++ b/packages/builtin-tool-agent-documents/src/systemRole.test.ts
@@ -0,0 +1,12 @@
+import { describe, expect, it } from 'vitest';
+
+import { systemPrompt } from './systemRole';
+
+describe('systemPrompt', () => {
+ it('should distinguish agent document ids from folder navigation ids', () => {
+ expect(systemPrompt).toContain("For read and mutation calls, pass the result's `id`.");
+ expect(systemPrompt).toContain(
+ "For folder navigation, pass the folder's `documentId` (or the ID shown in a collapsed folder row) as `listDocuments.parentId`.",
+ );
+ });
+});
diff --git a/packages/builtin-tool-agent-documents/src/systemRole.ts b/packages/builtin-tool-agent-documents/src/systemRole.ts
index 210122783f6..1e0bbcaf5f3 100644
--- a/packages/builtin-tool-agent-documents/src/systemRole.ts
+++ b/packages/builtin-tool-agent-documents/src/systemRole.ts
@@ -25,8 +25,8 @@ export const systemPrompt = `You have access to an Agent Documents tool for crea
- Set hintIsSkill=true only when creating a document that contains reusable procedural knowledge, workflow instructions, tool usage guidance, or durable agent behavior. Leave ordinary notes unhinted.
- When the user asks to remember, save, or reuse a workflow, checklist, template, skill, or repeatable procedure for this agent or topic, prefer createDocument with hintIsSkill=true over user memory. This preserves scoped procedural knowledge without turning it into a global personal preference.
- Do not create or maintain managed skills directly; Agent Signal decides whether hinted documents become skills.
-- **listDocuments**: list agent documents. Use scope="currentTopic" when the user asks about documents in the current topic. The default agent_documents_index hides web-crawled documents; pass sourceType="web" here to enumerate them, or sourceType="all" to see everything. The index also collapses folders into a single πŸ“ row β€” pass parentId=<folder id from that row> to expand a folder and see its documents. Use this to resolve a title to a document ID before reading.
-- **readDocument**: retrieve current content by document ID. This is the only way to read an agent document β€” there is no read-by-filename variant. Prefer format="xml" when you may edit content, because XML includes stable node IDs. If the response contains empty content, the document is genuinely empty; do not retry with a different format or filename.
+- **listDocuments**: list agent documents. Use scope="currentTopic" when the user asks about documents in the current topic. The default agent_documents_index hides web-crawled documents; pass sourceType="web" here to enumerate them, or sourceType="all" to see everything. The index also collapses folders into a single πŸ“ row β€” pass parentId=<folder id from that row> to expand a folder and see its documents. Use this to resolve a title to a document ID before reading. For read and mutation calls, pass the result's \`id\`. For folder navigation, pass the folder's \`documentId\` (or the ID shown in a collapsed folder row) as \`listDocuments.parentId\`.
+- **readDocument**: retrieve current content by agent document ID. Pass the \`id\` field returned by listDocuments, not \`documentId\`. This is the only way to read an agent document β€” there is no read-by-filename variant. Prefer format="xml" when you may edit content, because XML includes stable node IDs. If the response contains empty content, the document is genuinely empty; do not retry with a different format or filename.
- **modifyNodes**: preferred content-edit API. Use LiteXML insert/modify/remove operations after reading XML. For modify operations, include the existing node ID in the LiteXML.
- **replaceDocumentContent**: overwrite the full content of an existing document only when replacing most or all content.
- **removeDocument**: permanently remove a document by ID.