feat: add email service with multi-adapter support (#146)

* feat: add email service with multi-adapter support

Implement a full-stack email service with global provider management,
per-bot bindings with granular read/write permissions, outbox audit
storage, and MCP tool integration for direct mailbox access.

Backend:
- Email providers: CRUD with dynamic config schema (generic SMTP/IMAP, Mailgun)
- Generic adapter: go-mail (SMTP) + go-imap/v2 (IMAP IDLE real-time push via
  UnilateralDataHandler + UID-based tracking + periodic check fallback)
- Mailgun adapter: mailgun-go/v5 with dual inbound mode (webhook + poll)
- Bot email bindings: per-bot provider binding with independent r/w permissions
- Outbox: outbound email audit log with status tracking
- Trigger: inbound emails push notification to bot_inbox (from/subject only,
  LLM reads full content on demand via MCP tools)
- MailboxReader interface: on-demand IMAP queries for listing/reading emails
- MCP tools: email_accounts, email_send, email_list (paginated mailbox),
  email_read (by UID) — all with multi-binding and provider_id selection
- Webhook: /email/mailgun/webhook/:config_id (JWT-skipped, signature-verified)
- DB migration: 0019_add_email (email_providers, bot_email_bindings, email_outbox)

Frontend:
- Email Providers page: /email-providers with MasterDetailSidebarLayout
- Dynamic config form rendered from ordered provider meta schema with i18n keys
- Bot detail: Email tab with bindings management + outbox audit table
- Sidebar navigation entry
- Full i18n support (en + zh)
- Auto-generated SDK from Swagger

Closes #17

* feat(email): trigger bot conversation immediately on inbound email

Instead of only storing an inbox item and waiting for the next chat,
the email trigger now proactively invokes the conversation resolver
so the bot processes new emails right away — aligned with the
schedule/heartbeat trigger pattern.

* fix: lint

---------

Co-authored-by: Acbox <acbox0328@gmail.com>
This commit is contained in:
BBQ
2026-02-28 21:03:59 +08:00
committed by GitHub
parent 7d392a1143
commit cc5f00355f
47 changed files with 7719 additions and 55 deletions
+39
View File
@@ -65,6 +65,19 @@ type BotChannelRoute struct {
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}
type BotEmailBinding struct {
ID pgtype.UUID `json:"id"`
BotID pgtype.UUID `json:"bot_id"`
EmailProviderID pgtype.UUID `json:"email_provider_id"`
EmailAddress string `json:"email_address"`
CanRead bool `json:"can_read"`
CanWrite bool `json:"can_write"`
CanDelete bool `json:"can_delete"`
Config []byte `json:"config"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}
type BotHeartbeatLog struct {
ID pgtype.UUID `json:"id"`
BotID pgtype.UUID `json:"bot_id"`
@@ -187,6 +200,32 @@ type ContainerVersion struct {
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type EmailOutbox struct {
ID pgtype.UUID `json:"id"`
ProviderID pgtype.UUID `json:"provider_id"`
BotID pgtype.UUID `json:"bot_id"`
MessageID string `json:"message_id"`
FromAddress string `json:"from_address"`
ToAddresses []byte `json:"to_addresses"`
Subject string `json:"subject"`
BodyText string `json:"body_text"`
BodyHtml string `json:"body_html"`
Attachments []byte `json:"attachments"`
Status string `json:"status"`
Error string `json:"error"`
SentAt pgtype.Timestamptz `json:"sent_at"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
type EmailProvider struct {
ID pgtype.UUID `json:"id"`
Name string `json:"name"`
Provider string `json:"provider"`
Config []byte `json:"config"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}
type LifecycleEvent struct {
ID string `json:"id"`
ContainerID string `json:"container_id"`