Repository navigation
feat(discord): ingest inbound document attachments - #1330
clement-songis wants to merge 1 commit into
Conversation
[Medium impact] The change affects inbound messages handled by the Discord channel. Fix the two paths that skip supported documents before merging. FindingsSummaryAdds saved document attachments to Discord messages, with file labels, a 25 MiB limit, and sender-facing failure notices.
Diagram%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Approved Discord message] --> B[Resolve audio or image]
B --> C{Audio needs missing STT?}
C -->|Yes| D[Send voice setup hint and stop]
C -->|No| E[Select document attachments]
E --> F[Check size and download concurrently]
F --> G{Saved successfully?}
G -->|Yes| H[Add file metadata and label]
G -->|No| I[Send failure notice and add failure label]
H --> J[Dispatch caption, media, and documents]
I --> K{Any usable content?}
K -->|Yes| J
K -->|No| L[Stop after notice]
Reviews (1) · Last reviewed commit: "feat(discord): ingest inbound document a..." · Reviewed by Greptile |
| let documents = ingest_discord_documents( | ||
| &msg.attachments, | ||
| sink.as_ref(), | ||
| &reply_to, | ||
| &self.account_id, | ||
| &self.downloader, | ||
| ) | ||
| .await; |
There was a problem hiding this comment.
When a message contains both audio and a document, and speech-to-text is not configured, VoiceSttUnavailable returns before this new ingestion call. The bot sends only the voice setup hint and never saves or dispatches the document.
Run document ingestion independently of voice support, and continue with saved documents after sending the hint.
There was a problem hiding this comment.
Fixed in 6cd9524. The STT hint is still sent, then handling continues: saved documents are dispatched with a [Voice message - STT unavailable] marker, and the message only stops at the hint when no document was saved. The stop decision now lives in DocumentIngestion::nothing_to_dispatch, covered by voice_without_stt_still_delivers_saved_documents.
| pub(super) fn attachment_is_document(a: &Attachment) -> bool { | ||
| !attachment_is_audio(a) && !attachment_is_image(a) |
There was a problem hiding this comment.
WebM videos miss document ingestion
The new document filter excludes WebM videos. attachment_is_audio accepts every .webm filename, even when Discord reports video/webm, so the bot tries to transcribe these videos instead of saving them as documents. Without speech-to-text, the handler stops altogether. Either way, the agent never receives the saved video file.
Give an explicit video MIME type priority over the audio extension fallback, and add a video/webm test.
There was a problem hiding this comment.
Fixed in 6cd9524. In attachment_is_audio, a reported video/* or image/* type now wins over the extension, so a video/webm clip is saved as a document. A .webm with no reported type is still treated as a voice clip. Covered by webm_video_is_a_document_not_audio.
Discord only handled voice notes (STT) and images (multimodal). Any other attachment (PDF, text, Office files, archives, video...) was dropped: the agent saw the caption, or an empty body, and never got the file. Attachments that are neither audio nor a still image are now downloaded concurrently through the existing SSRF-checked CDN downloader, capped at 25 MB, saved with `save_channel_attachment`, and passed to the agent as `ChannelMessageMeta.documents`, like Telegram and WhatsApp. Each document gets a `[Document: name (mime)]` label after the caption, so a message can mix images and documents. Oversized files are rejected from Discord's reported size before any download. When a document is too large, fails to download or cannot be saved, the sender gets a message naming the file and the reason, and the agent's body says the file was not received. A message made only of unreadable documents gets that notice and is not dispatched. A voice note sent without speech-to-text configured still gets the setup hint, but the documents sent with it are saved and dispatched with a "[Voice message - STT unavailable]" marker. A reported video or image type wins over the `.webm` extension, so a `video/webm` clip is saved as a document instead of being transcribed. Stored filenames are prefixed with the attachment id and reduced to `[A-Za-z0-9._-]`; MIME types are normalized to a bare `type/subtype` with an extension fallback; display names drop control characters, brackets and backticks before reaching the prompt or the reply. The CDN download and attachment classification helpers move from handler/implementation.rs to handler/attachments.rs to keep the handler under the 1,500-line limit.
7970db3 to
6cd9524
Compare
Summary
Discord handled voice notes (STT) and images (multimodal), but every other attachment (PDF, text, Office files, archives, video…) was dropped: the agent saw the caption, or an empty body, and never got the file.
DiscordInboundMediaDownloader, capped at 25 MB, saved withChannelEventSink::save_channel_attachment, and passed to the agent throughChannelMessageMeta.documents, like Telegram and WhatsApp.[Document: name (mime)]label after the caption; a message can mix images and documents (the image still goes through the multimodal path).<attachment id>_<name reduced to [A-Za-z0-9._-]>, stem capped at 160 chars, extension kept. MIME types are normalized to a baretype/subtypewith an extension fallback. Display names drop control characters, brackets and backticks before reaching the prompt or the reply.handler/attachments.rs; ingestion lives inhandler/documents.rs(implementation.rsgoes from 1482 to 1350 lines).video/webmclips are saved as documents instead of being transcribed (Greptile review).docs/src/discord.md, capability column indocs/src/channels.md.Telegram's document helpers are not copied: Discord builds
ChannelDocumentFileinline. Text/PDF content is not inlined in the body; the agent opens the saved file with its tools. In sandboxed sessionsReadcannot extract PDF text today, tracked in #1328.flowchart TD M[Discord message from approved sender] --> R[resolve voice / image] R --> D[ingest documents: non-audio, non-image] D -->|size > 25 MB| F[failure] D -->|download error| F D -->|save_channel_attachment None| F D -->|saved| S[ChannelDocumentFile] F --> N[reply to sender: which file, why] S --> B[body = caption + document labels] F --> B B --> C{only failed docs, no text?} C -->|yes| X[stop after notice] C -->|no| G[dispatch_to_chat with meta.documents]Validation
Completed
cargo fmt --all -- --check(all checks below re-run on 6cd9524)cargo clippy -p moltis-discord --all-features --all-targets -- -D warningscargo test -p moltis-discord(149 passed)./scripts/check-file-size.shjust lint(macOS: workspace clippy plusmoltis-providersandmoltis-gatewaywithlocal-llm-metal,-D warnings)taplo fmt --check crates/discord/Cargo.tomlRemaining
./scripts/local-validate.sh <PR_NUMBER>Manual QA
Done on a live deployment:
inferred_kind=Documentwith the caption and a[Document: rapport-test.pdf (application/pdf)]label; the file is saved as<attachment id>_rapport-test.pdfin session media and the agent answered from its content..zipin one message: one dispatch with both labels; the agent read both files..mdwith no caption: dispatched with the document label only (previously an empty body).Still to check:
file: the file exceeds the 25 MB limit." and does not call the agent.image_and_document_in_one_message_are_both_handled, not yet checked live).