Data Flow
Request lifecycle and cross-package interactions for chat, files, billing, and background jobs.
This page documents the main runtime paths in ship-ai.
Read this with code
Use this page together with route handlers in apps/app/src/app/api/* and corresponding package modules to trace real execution paths.
1) Chat Request Flow
Route entrypoint
POST /api/chat in apps/app/src/app/api/chat/route.ts authenticates and delegates to buildChatStream(...).
Pipeline execution
Context assembly, memory, search/deep-search branching, and stream generation are coordinated inside chat builder modules.
Persistence and accounting
Post-finish handlers persist state and usage/billing telemetry for subsequent limits and analytics.
Execution Stages Inside Chat Builder
Core behavior in apps/app/src/lib/ai/chat/build-chat-stream.ts:
- preflight and guards
- derive chat operation (
chatordeep_search) - call
checkUsageLimits(...)from@ai/billing - load or create chat in DB (
@ai/db)
- derive chat operation (
- context assembly
- fetch prior messages
- optimize history for context window
- build memory context
- compose system instruction + selected context
- attachment and document processing
- routing and handler execution
- deep-search branch or standard branch
- streaming response
- post-finish persistence and accounting
2) Resumable Stream Flow
- Active stream id is persisted on chat record.
GET /api/chat/[id]/streamvalidates ownership and resumes viaresumable-stream.- Pub/sub backing uses Redis through
@ai/kv/client.
3) File Upload and Storage Flow
Typical upload URL path:
POST /api/files/upload-url- validate auth + mime + filename + size
- enforce upload and file-size limits via
@ai/billing - generate presigned URL using
@ai/storage - client uploads object to S3-compatible storage
4) Instruments and Jobs Flow
Transcription
POST /api/instruments/transcription- validate file ownership/type/size + billing limits
- create transcription DB record and mark processing
- enqueue BullMQ job via
@ai/jobs - worker runs transcription and writes results back to DB
5) Billing and Subscription Flow
Checkout path:
POST /api/billing/checkout- verify plan + existing subscription state
- create/reuse Stripe customer (
@ai/stripe) - create checkout session
Webhook path:
POST /api/billing/webhook- verify Stripe signature
- dedupe event (Redis key)
- apply subscription/payment state changes in DB
6) Feature and Service Gating
Feature behavior is environment-driven through @ai/features:
- product flags (
FEATURE_*) gate capabilities like search, deep search, memory, artifacts, and transcription - service flags (
SERVICE_*) represent deployed infra availability - development mode can bypass billing enforcement where configured
7) Data Ownership Summary
- Database: canonical state (
@ai/db) - Redis: cache/rate-limit/queue/stream coordination (
@ai/kv, BullMQ) - Object storage: uploaded file blobs (
@ai/storage) - Workers: asynchronous processing (
@ai/jobs) - Tracing/logging: observability across request and workflow boundaries (
@ai/tracing,@ai/logger)