Skip to content

feat(discord): ingest inbound document attachments - #1330

Open
clement-songis wants to merge 1 commit into
moltis-org:mainfrom
antre-ai:feat/discord-documents
Open

clement-songis wants to merge 1 commit into
moltis-org:mainfrom
antre-ai:feat/discord-documents

Conversation

@clement-songis

@clement-songis clement-songis commented Nov 26, 2026 •

Copy link
Copy Markdown

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.

  • Attachments that are neither audio nor a still image are downloaded concurrently through the existing SSRF-checked DiscordInboundMediaDownloader, capped at 25 MB, saved with ChannelEventSink::save_channel_attachment, and passed to the agent through ChannelMessageMeta.documents, like Telegram and WhatsApp.
  • Each document adds a [Document: name (mime)] label after the caption; a message can mix images and documents (the image still goes through the multimodal path).
  • Oversized files are rejected from Discord's reported size, before any download.
  • Too large / download failed / save failed: the sender gets a message naming the file and the reason, and the agent body says the file was not received. A message made only of unreadable documents gets the notice and is not dispatched.
  • Stored filenames: <attachment id>_<name reduced to [A-Za-z0-9._-]>, stem capped at 160 chars, extension kept. 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 to handler/attachments.rs; ingestion lives in handler/documents.rs (implementation.rs goes from 1482 to 1350 lines).
  • A voice note without speech-to-text no longer drops the documents sent with it, and video/webm clips are saved as documents instead of being transcribed (Greptile review).
  • Docs: new "Inbound Attachments" section in docs/src/discord.md, capability column in docs/src/channels.md.

Telegram's document helpers are not copied: Discord builds ChannelDocumentFile inline. Text/PDF content is not inlined in the body; the agent opens the saved file with its tools. In sandboxed sessions Read cannot 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]
Loading

Validation

Completed

  • cargo fmt --all -- --check (all checks below re-run on 6cd9524)
  • cargo clippy -p moltis-discord --all-features --all-targets -- -D warnings
  • cargo test -p moltis-discord (149 passed)
  • ./scripts/check-file-size.sh
  • just lint (macOS: workspace clippy plus moltis-providers and moltis-gateway with local-llm-metal, -D warnings)
  • taplo fmt --check crates/discord/Cargo.toml
  • Deployed on a NixOS host and tested over Discord DM (see Manual QA), before the Greptile fixes; those are covered by unit tests only

Remaining

  • ./scripts/local-validate.sh <PR_NUMBER>

Manual QA

Done on a live deployment:

  1. PDF with a caption in a DM: dispatched as inferred_kind=Document with the caption and a [Document: rapport-test.pdf (application/pdf)] label; the file is saved as <attachment id>_rapport-test.pdf in session media and the agent answered from its content.
  2. PDF and .zip in one message: one dispatch with both labels; the agent read both files.
  3. .md with no caption: dispatched with the document label only (previously an empty body).
  4. Voice note: still transcribed and answered as before.

Still to check:

  1. A file over 25 MB (Nitro account): the bot replies "I couldn't read file: the file exceeds the 25 MB limit." and does not call the agent.
  2. An image and a document in one message: the image reaches the model as a multimodal attachment, the document is saved and labelled (covered by image_and_document_in_one_message_are_both_handled, not yet checked live).

@greptile-apps

greptile-apps Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 3/5 · Fix the two paths that skip supported documents before merging.RetriggerView in GreptileConfidence Score: 3/5

[Medium impact] The change affects inbound messages handled by the Discord channel.

Fix the two paths that skip supported documents before merging.

Findings

  1. P1 Voice setup blocks documents ▶
  2. P1 WebM videos miss document ingestion ▶

Summary

Adds saved document attachments to Discord messages, with file labels, a 25 MiB limit, and sender-facing failure notices.

  • Discord messages can now include saved documents alongside captions and images.

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]
Loading

Reviews (1) · Last reviewed commit: "feat(discord): ingest inbound document a..." · Reviewed by Greptile

Comment on lines +838 to +845
let documents = ingest_discord_documents(
&msg.attachments,
sink.as_ref(),
&reply_to,
&self.account_id,
&self.downloader,
)
.await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Voice setup blocks documents

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.

@clement-songis clement-songis Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +60 to +61
pub(super) fn attachment_is_document(a: &Attachment) -> bool {
!attachment_is_audio(a) && !attachment_is_image(a)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

@clement-songis clement-songis Nov 26, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant