π€― 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(search): cap document content in Elasticsearch projections (#19993)
YuTengjingef863a5da62a
diff --git a/packages/database/src/repositories/ftsSearchDocument/builder.test.ts b/packages/database/src/repositories/ftsSearchDocument/builder.test.tsindex bed872504ae..13d4e71eae5 100644--- a/packages/database/src/repositories/ftsSearchDocument/builder.test.ts+++ b/packages/database/src/repositories/ftsSearchDocument/builder.test.ts@@ -22,7 +22,7 @@ import {} from '../../schemas';import type { LobeChatDatabase } from '../../type';import { FTS_SEARCH_DOCUMENT_FIXTURES } from './__tests__/fixtures';-import { FtsSearchDocumentBuilder } from './builder';+import { FTS_SEARCH_DOCUMENT_CONTENT_MAX_CHARS, FtsSearchDocumentBuilder } from './builder';import { FTS_SEARCH_DOCUMENT_ENTITIES } from './zodSchema';const userId = 'search-document-user';@@ -388,6 +388,28 @@ describe('FtsSearchDocumentBuilder', () => {).resolves.toEqual([]);});+ it('caps oversized document content by characters while keeping the full character count', async () => {+ const content = `${'ζ'.repeat(FTS_SEARCH_DOCUMENT_CONTENT_MAX_CHARS)}tail beyond the cap`;+ await db.insert(documents).values({+ content,+ fileType: 'text/plain',+ id: 'document-oversized',+ source: 'https://example.com/oversized.txt',+ sourceType: 'file',+ title: 'Oversized document',+ totalCharCount: content.length,+ totalLineCount: 1,+ userId,+ });++ const [document] = await builder.buildByIds('documents', ['document-oversized']);++ expect(document.source).toMatchObject({+ content: 'ζ'.repeat(FTS_SEARCH_DOCUMENT_CONTENT_MAX_CHARS),+ total_char_count: content.length,+ });+ });+it('rejects invalid batch limits before querying PostgreSQL', async () => {await expect(builder.buildBatch('agents', { limit: 0 })).rejects.toThrow('FTS search document batch limit must be a positive integer',diff --git a/packages/database/src/repositories/ftsSearchDocument/builder.ts b/packages/database/src/repositories/ftsSearchDocument/builder.tsindex 5326e10e0ec..ac86152b739 100644--- a/packages/database/src/repositories/ftsSearchDocument/builder.ts+++ b/packages/database/src/repositories/ftsSearchDocument/builder.ts@@ -53,6 +53,21 @@ export type FtsSearchDocumentRelationChange =| { fileIds: readonly string[]; relation: 'knowledgeBaseFiles' }| { memoryIds: readonly string[]; relation: 'userMemoryReferences' };+/**+ * Upper bound, in characters, of `documents.content` copied into a search document.+ *+ * Parsed files and generated documents can hold 100+ MiB of text (for example, a failed spreadsheet+ * parse that emitted mostly blank cells). A document that large exceeds the incremental sync bulk+ * limit, becomes a permanent dead letter, and blocks every later change; Elasticsearch Serverless+ * also rejects such requests with 429. Only the prefix is indexed, so matches beyond it are not+ * found. Results are hydrated from PostgreSQL, so callers never see the shortened text.+ *+ * One million characters matches Elasticsearch's default `index.highlight.max_analyzed_offset`.+ * Even at 6 JSON-escaped bytes per character this stays far below+ * `FTS_SEARCH_SYNC_BULK_MAX_BYTES`, so one document can no longer outgrow a bulk request.+ */+export const FTS_SEARCH_DOCUMENT_CONTENT_MAX_CHARS = 1_000_000;+const entityOrder = new Map(FTS_SEARCH_DOCUMENT_ENTITIES.map((entity, index) => [entity, index]));const normalizeIds = (ids: readonly string[]) => [...new Set(ids)].filter(Boolean).sort();@@ -915,7 +930,10 @@ export class FtsSearchDocumentBuilder {private async buildDocuments(selection: FtsSearchDocumentSelection) {const rows = await this.db.select({- content: documents.content,+ /** Truncate in SQL so oversized rows never cross the wire into application memory. */+ content: sql<+ string | null+ >`left(${documents.content}, ${FTS_SEARCH_DOCUMENT_CONTENT_MAX_CHARS})`,createdAt: documents.createdAt,description: documents.description,fileId: documents.fileId,