Skip to content

Commit 0e16936

Browse files
waleedlatif1claude
andcommitted
fix(knowledge): fix btoa stack overflow and duplicate encoding in create_document
Same fixes as upsert_document: use loop-based String.fromCharCode instead of spread, consolidate duplicate TextEncoder calls, and check byte length instead of character length for 1MB limit. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6c6ba81 commit 0e16936

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

apps/sim/tools/knowledge/create_document.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,23 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
6666
if (!textContent || textContent.length < 1) {
6767
throw new Error('Document content cannot be empty')
6868
}
69-
if (textContent.length > 1000000) {
69+
const utf8Bytes = new TextEncoder().encode(textContent)
70+
const contentBytes = utf8Bytes.length
71+
72+
if (contentBytes > 1_000_000) {
7073
throw new Error('Document content exceeds maximum size of 1MB')
7174
}
7275

73-
const contentBytes = new TextEncoder().encode(textContent).length
74-
75-
const utf8Bytes = new TextEncoder().encode(textContent)
76-
const base64Content =
77-
typeof Buffer !== 'undefined'
78-
? Buffer.from(textContent, 'utf8').toString('base64')
79-
: btoa(String.fromCharCode(...utf8Bytes))
76+
let base64Content: string
77+
if (typeof Buffer !== 'undefined') {
78+
base64Content = Buffer.from(textContent, 'utf8').toString('base64')
79+
} else {
80+
let binary = ''
81+
for (let i = 0; i < utf8Bytes.length; i++) {
82+
binary += String.fromCharCode(utf8Bytes[i])
83+
}
84+
base64Content = btoa(binary)
85+
}
8086

8187
const { filename, mimeType } = inferDocumentFileInfo(documentName)
8288
const dataUri = `data:${mimeType};base64,${base64Content}`

0 commit comments

Comments
 (0)