Skip to content
Prev Previous commit
Next Next commit
fix(knowledge): fix btoa stack overflow and duplicate encoding in cre…
…ate_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>
  • Loading branch information
waleedlatif1 and claude committed Mar 18, 2026
commit 0e169360378c945c714c49706623daf03249af1b
22 changes: 14 additions & 8 deletions apps/sim/tools/knowledge/create_document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,23 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
if (!textContent || textContent.length < 1) {
throw new Error('Document content cannot be empty')
}
if (textContent.length > 1000000) {
const utf8Bytes = new TextEncoder().encode(textContent)
const contentBytes = utf8Bytes.length

if (contentBytes > 1_000_000) {
throw new Error('Document content exceeds maximum size of 1MB')
}

const contentBytes = new TextEncoder().encode(textContent).length

const utf8Bytes = new TextEncoder().encode(textContent)
const base64Content =
typeof Buffer !== 'undefined'
? Buffer.from(textContent, 'utf8').toString('base64')
: btoa(String.fromCharCode(...utf8Bytes))
let base64Content: string
if (typeof Buffer !== 'undefined') {
base64Content = Buffer.from(textContent, 'utf8').toString('base64')
} else {
let binary = ''
for (let i = 0; i < utf8Bytes.length; i++) {
binary += String.fromCharCode(utf8Bytes[i])
}
base64Content = btoa(binary)
}

const { filename, mimeType } = inferDocumentFileInfo(documentName)
const dataUri = `data:${mimeType};base64,${base64Content}`
Expand Down
Loading