fix: inject text-file content into chat completions messages (#9896)

Non-image/non-audio file attachments (txt, md, csv, json) were being
  stored in the 'files' metadata field but never added to the message
  content array sent to /v1/chat/completions. Images and audio correctly
  received content blocks; files did not.

  Fix: push a text content block into messageContent when textContent is
  present, matching the pattern used for image_url and audio_url.

  Also fixes Home.jsx addFiles which never called file.text() at all,
  meaning files attached on the home screen had empty textContent even
  before reaching useChat.js.

  Note: PDF files use file.text() which returns raw bytes rather than
  parsed text. Proper PDF support would require PDF.js or server-side
  extraction and is not part of this fix.

Signed-off-by: Daniel Liljeberg <damien_@hotmail.com>
This commit is contained in:
Daniel Liljeberg
2026-05-20 01:00:32 +02:00
committed by GitHub
parent 2009544b44
commit fc3980dadd
2 changed files with 14 additions and 4 deletions

View File

@@ -218,9 +218,15 @@ export function useChat(initialModel = '') {
})
userFiles.push({ name: file.name, type: 'audio' })
} else {
// Text/PDF files - append to content
userFiles.push({ name: file.name, type: 'file', content: file.textContent || '' })
}
// Text/PDF files - append to content
if (file.textContent) {
messageContent.push({
type: 'text',
text: `\n\n--- File: ${file.name} ---\n${file.textContent}\n--- End of ${file.name} ---`,
})
}
userFiles.push({ name: file.name, type: 'file', content: file.textContent || '' })
}
}
} else {
messageContent = content

View File

@@ -161,7 +161,11 @@ export default function Home() {
const newFiles = []
for (const file of fileList) {
const base64 = await fileToBase64(file)
newFiles.push({ name: file.name, type: file.type, base64 })
const entry = { name: file.name, type: file.type, base64 }
if (!file.type.startsWith('image/') && !file.type.startsWith('audio/')) {
entry.textContent = await file.text().catch(() => '')
}
newFiles.push(entry)
}
setter(prev => [...prev, ...newFiles])
}, [])