Milestone 1: core services + Mox ingest + wired v0 API

- internal/mail: MailBackend interface (Send/Start/Capabilities) + NullBackend.
- internal/core: inbox/message/thread services over pgx; default-pod bootstrap.
- IngestRaw: real mox/message parse → body extraction (text/html walk) →
  quote-stripping (extracted_text) → reference-based threading → store, all in
  one tx; emits message.received event; populates FTS tsvector.
- core implements mail.InboundSink (Deliver) so any backend feeds the same path.
- internal/api: v0 routes wired live (was 501) — create/list/get inbox, ingest,
  list/get messages, list/get thread(+messages), send/reply (503 via NullBackend).
- cmd/openmail serve: builds core + NullBackend, ensures default pod.

Verified e2e vs Postgres 16: inbox create; ingest original+reply → same thread
via In-Reply-To/References; extracted_text strips quoted history; thread detail
ordered; FTS matches 'invoice'; events written; send → honest 503. vet clean.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
karti-ai
2026-06-21 12:54:25 -07:00
parent abce753ba8
commit 319a2fa689
8 changed files with 906 additions and 27 deletions
+244
View File
@@ -0,0 +1,244 @@
package api
import (
"encoding/json"
"errors"
"io"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/karti-ai/openmail/internal/core"
"github.com/karti-ai/openmail/internal/mail"
)
const maxIngestBytes = 30 << 20 // 30 MiB raw message cap
func (s *Server) createInbox(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
var body struct {
Address string `json:"address"`
DisplayName *string `json:"display_name"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.Address == "" {
writeErr(w, http.StatusBadRequest, "invalid_request", "address is required")
return
}
ib, err := s.core.CreateInbox(r.Context(), s.podID, body.Address, body.DisplayName)
if err != nil {
writeErr(w, http.StatusInternalServerError, "create_failed", err.Error())
return
}
writeJSON(w, http.StatusCreated, ib)
}
func (s *Server) listInboxes(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
list, err := s.core.ListInboxes(r.Context(), s.podID)
if err != nil {
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"inboxes": list})
}
func (s *Server) getInbox(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
ib, err := s.core.GetInbox(r.Context(), chi.URLParam(r, "id"))
if handleErr(w, err) {
return
}
writeJSON(w, http.StatusOK, ib)
}
// ingest accepts a raw RFC 5322 message body and stores it in the inbox — the
// milestone-1 seed path, and the same path a MailBackend uses for real inbound.
func (s *Server) ingest(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
inboxID := chi.URLParam(r, "id")
if _, err := s.core.GetInbox(r.Context(), inboxID); handleErr(w, err) {
return
}
raw, err := io.ReadAll(io.LimitReader(r.Body, maxIngestBytes))
if err != nil || len(raw) == 0 {
writeErr(w, http.StatusBadRequest, "invalid_request", "raw message body required")
return
}
msg, err := s.core.IngestRaw(r.Context(), inboxID, raw)
if err != nil {
writeErr(w, http.StatusInternalServerError, "ingest_failed", err.Error())
return
}
writeJSON(w, http.StatusCreated, msg)
}
func (s *Server) listMessages(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
list, err := s.core.ListMessages(r.Context(), chi.URLParam(r, "id"), limit)
if err != nil {
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"messages": list})
}
func (s *Server) getMessage(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
m, err := s.core.GetMessage(r.Context(), chi.URLParam(r, "msgID"))
if handleErr(w, err) {
return
}
writeJSON(w, http.StatusOK, m)
}
func (s *Server) listThreads(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
list, err := s.core.ListThreads(r.Context(), chi.URLParam(r, "id"))
if err != nil {
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"threads": list})
}
func (s *Server) getThread(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
threadID := chi.URLParam(r, "threadID")
t, err := s.core.GetThread(r.Context(), threadID)
if handleErr(w, err) {
return
}
msgs, err := s.core.GetThreadMessages(r.Context(), threadID)
if err != nil {
writeErr(w, http.StatusInternalServerError, "list_failed", err.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"thread": t, "messages": msgs})
}
func (s *Server) sendMessage(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
inboxID := chi.URLParam(r, "id")
ib, err := s.core.GetInbox(r.Context(), inboxID)
if handleErr(w, err) {
return
}
var body struct {
To []string `json:"to"`
Cc []string `json:"cc"`
Bcc []string `json:"bcc"`
Subject string `json:"subject"`
Text string `json:"text"`
HTML string `json:"html"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || len(body.To) == 0 {
writeErr(w, http.StatusBadRequest, "invalid_request", "at least one 'to' recipient is required")
return
}
s.dispatch(w, r, &mail.OutgoingMessage{
InboxID: inboxID, From: ib.Address,
To: body.To, Cc: body.Cc, Bcc: body.Bcc,
Subject: body.Subject, Text: body.Text, HTML: body.HTML,
})
}
func (s *Server) replyMessage(w http.ResponseWriter, r *http.Request) {
if !s.requireCore(w) {
return
}
inboxID := chi.URLParam(r, "id")
ib, err := s.core.GetInbox(r.Context(), inboxID)
if handleErr(w, err) {
return
}
orig, err := s.core.GetMessage(r.Context(), chi.URLParam(r, "msgID"))
if handleErr(w, err) {
return
}
var body struct {
Text string `json:"text"`
HTML string `json:"html"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
writeErr(w, http.StatusBadRequest, "invalid_request", "invalid body")
return
}
out := &mail.OutgoingMessage{
InboxID: inboxID, From: ib.Address,
Subject: replySubject(orig.Subject),
Text: body.Text, HTML: body.HTML,
References: append(append([]string{}, orig.References...), strPtr(orig.MessageIDHdr)...),
}
if orig.MessageIDHdr != nil {
out.InReplyTo = *orig.MessageIDHdr
}
if orig.FromAddr != nil {
out.To = []string{*orig.FromAddr}
}
s.dispatch(w, r, out)
}
// dispatch hands an outgoing message to the active backend, translating the
// NullBackend's ErrNotSupported into a clear 503 (no send path configured yet).
func (s *Server) dispatch(w http.ResponseWriter, r *http.Request, out *mail.OutgoingMessage) {
res, err := s.backend.Send(r.Context(), out)
if errors.Is(err, mail.ErrNotSupported) {
writeErr(w, http.StatusServiceUnavailable, "send_unavailable",
"no send-capable mail backend configured (relay/imap_smtp/embedded land in later milestones)")
return
}
if err != nil {
writeErr(w, http.StatusBadGateway, "send_failed", err.Error())
return
}
writeJSON(w, http.StatusAccepted, res)
}
func handleErr(w http.ResponseWriter, err error) bool {
switch {
case err == nil:
return false
case errors.Is(err, core.ErrNotFound):
writeErr(w, http.StatusNotFound, "not_found", "resource not found")
default:
writeErr(w, http.StatusInternalServerError, "internal_error", err.Error())
}
return true
}
func replySubject(s *string) string {
if s == nil || *s == "" {
return "Re:"
}
if len(*s) >= 3 && (*s)[:3] == "Re:" {
return *s
}
return "Re: " + *s
}
func strPtr(s *string) []string {
if s == nil || *s == "" {
return nil
}
return []string{*s}
}
+44 -26
View File
@@ -1,6 +1,5 @@
// Package api is OpenMail's agent-facing HTTP surface: the AgentMail-shaped v0
// REST API (see ARCHITECTURE.md §4). v0 routes are stubbed pending the core
// services; health and auth are real.
// REST API (see ARCHITECTURE.md §4).
package api
import (
@@ -11,16 +10,19 @@ import (
"github.com/go-chi/chi/v5/middleware"
"github.com/karti-ai/openmail/internal/config"
"github.com/karti-ai/openmail/internal/store"
"github.com/karti-ai/openmail/internal/core"
"github.com/karti-ai/openmail/internal/mail"
)
type Server struct {
cfg config.Config
store *store.Store
cfg config.Config
core *core.Service // nil when DATABASE_URL is unset
backend mail.MailBackend // never nil (NullBackend by default)
podID string // default pod, resolved at startup
}
func New(cfg config.Config, st *store.Store) *Server {
return &Server{cfg: cfg, store: st}
func New(cfg config.Config, svc *core.Service, backend mail.MailBackend, podID string) *Server {
return &Server{cfg: cfg, core: svc, backend: backend, podID: podID}
}
func (s *Server) Router() http.Handler {
@@ -29,23 +31,22 @@ func (s *Server) Router() http.Handler {
r.Use(middleware.RealIP)
r.Use(middleware.Recoverer)
// Unauthenticated liveness/readiness.
r.Get("/healthz", s.handleHealth)
// Authenticated v0 surface.
r.Group(func(r chi.Router) {
r.Use(s.bearerAuth)
r.Route("/v0/inboxes", func(r chi.Router) {
r.Post("/", notImplemented) // create inbox
r.Get("/", notImplemented) // list inboxes
r.Get("/{id}", notImplemented) // get inbox
r.Post("/{id}/messages/send", notImplemented)
r.Get("/{id}/messages", notImplemented)
r.Get("/{id}/messages/{msgID}", notImplemented)
r.Post("/{id}/messages/{msgID}/reply", notImplemented)
r.Get("/{id}/threads", notImplemented)
r.Get("/{id}/threads/{threadID}", notImplemented)
r.Post("/", s.createInbox)
r.Get("/", s.listInboxes)
r.Get("/{id}", s.getInbox)
r.Post("/{id}/ingest", s.ingest) // seed/inbound path (milestone 1)
r.Post("/{id}/messages/send", s.sendMessage)
r.Get("/{id}/messages", s.listMessages)
r.Get("/{id}/messages/{msgID}", s.getMessage)
r.Post("/{id}/messages/{msgID}/reply", s.replyMessage)
r.Get("/{id}/threads", s.listThreads)
r.Get("/{id}/threads/{threadID}", s.getThread)
})
})
@@ -53,9 +54,9 @@ func (s *Server) Router() http.Handler {
}
func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
status := map[string]any{"status": "ok"}
if s.store != nil {
if err := s.store.Pool.Ping(r.Context()); err != nil {
status := map[string]any{"status": "ok", "backend": backendName(s.backend)}
if s.core != nil {
if err := s.core.Ping(r.Context()); err != nil {
status["status"] = "degraded"
status["db"] = err.Error()
writeJSON(w, http.StatusServiceUnavailable, status)
@@ -68,11 +69,24 @@ func (s *Server) handleHealth(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, status)
}
func notImplemented(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusNotImplemented, map[string]string{
"error": "not_implemented",
"message": "endpoint scaffolded; core service pending (milestone 1)",
})
func backendName(b mail.MailBackend) string {
if _, ok := b.(mail.NullBackend); ok {
return "null"
}
return "configured"
}
// requireCore guards handlers that need the database. Returns false (and writes
// a 503) when the store is unconfigured.
func (s *Server) requireCore(w http.ResponseWriter) bool {
if s.core == nil {
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
"error": "db_unconfigured",
"message": "DATABASE_URL not set",
})
return false
}
return true
}
func writeJSON(w http.ResponseWriter, code int, v any) {
@@ -80,3 +94,7 @@ func writeJSON(w http.ResponseWriter, code int, v any) {
w.WriteHeader(code)
_ = json.NewEncoder(w).Encode(v)
}
func writeErr(w http.ResponseWriter, code int, errCode, msg string) {
writeJSON(w, code, map[string]string{"error": errCode, "message": msg})
}