Skip to content

Commit e346f77

Browse files
waleedlatif1claude
andcommitted
fix(knowledge): prevent documentId fallthrough and use byte-count limit
- Use if/else so filename lookup only runs when no documentId is provided, preventing stale IDs from silently replacing unrelated documents - Check utf8 byte length instead of character count for 1MB size limit, correctly handling multi-byte characters (CJK, emoji) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 1516b74 commit e346f77

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

apps/sim/app/api/knowledge/[id]/documents/upsert/route.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,7 @@ export async function POST(req: NextRequest, { params }: { params: Promise<{ id:
103103
if (existingDoc.length > 0) {
104104
existingDocumentId = existingDoc[0].id
105105
}
106-
}
107-
108-
if (!existingDocumentId) {
106+
} else {
109107
const docsByFilename = await db
110108
.select({ id: document.id })
111109
.from(document)

apps/sim/tools/knowledge/upsert_document.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ export const knowledgeUpsertDocumentTool: ToolConfig<
7777
if (!textContent || textContent.length < 1) {
7878
throw new Error('Document content cannot be empty')
7979
}
80-
if (textContent.length > 1000000) {
81-
throw new Error('Document content exceeds maximum size of 1MB')
82-
}
83-
8480
const utf8Bytes = new TextEncoder().encode(textContent)
8581
const contentBytes = utf8Bytes.length
82+
83+
if (contentBytes > 1_000_000) {
84+
throw new Error('Document content exceeds maximum size of 1MB')
85+
}
8686
let base64Content: string
8787
if (typeof Buffer !== 'undefined') {
8888
base64Content = Buffer.from(textContent, 'utf8').toString('base64')

0 commit comments

Comments
 (0)