This document defines host-side AI integration for apps/host, including editor UI, API contracts, provider routing, and usage billing.
1. Scope
Target surfaces:
- Editor page:
apps/host/src/app/editor/[[...postsId]]/page.tsxapps/host/src/components/demo/EditorComponent.tsx
- Next server routes under:
apps/host/src/app/api/ai/*apps/host/src/app/api/storage/presign-put/route.ts
- Client API helper:
apps/host/src/utils/ghostApiClient.ts
2. Editor UI Contract
EditorComponent provides:
- AI icon button in the editor toolbar, placed next to Post Setting icon button.
- AI drawer on right side (
persistent), same mobile behavior as Post Setting drawer. - Mobile width is full-screen:
width: { xs: "100vw", sm: 420 }
- Chat panel includes:
- LLM provider pulldown
- model pulldown
- history toggle icon in header
- new chat icon near input area
- chat input box with send icon
Current Chatsection- optional
Chat Historysection inside same panel - copy icon on each message and history row
Behavior:
- Click AI icon: open/close AI drawer.
- Click history icon: toggle history list area.
- Click new chat icon: clear current message thread.
- Enter/Send: append user message and assistant placeholder.
3. API Contract Rules
All AI/storage routes require:
- Header
x-group-id - Body
group_idmatching header - Optional
x-request-id(server generates if omitted)
Provider selection fields:
provider:chatgpt | gemini | deepseek | glmmodel: optional provider model id
Server validates provider allowlist and resolves default model if missing.
4. Route Inventory
Implemented scaffold routes:
POST /api/ai/chatGET /api/ai/chat/historyGET /api/ai/usagePOST /api/ai/editorPOST /api/ai/ttsPOST /api/ai/sttPOST /api/ai/realtime/sessionPOST /api/storage/presign-put
Shared helpers and types:
apps/host/src/app/api/ai/_shared.tsapps/host/src/types/ai.ts
5. Group/User Billing Contract
Billing and observability policy:
- Billing unit:
group_id - Usage actor and audit:
user_id - Every request logs tokens/units + USD cost
Standard response usage shape:
usage: token/audio unitscost:{ currency: "USD", amount_usd: number }
6. Persistence Design
AI SDK does not persist chat history in production by default. Persist server-side via Ghost custom API + DB.
Recommended tables:
ai_conversationsai_messagesai_usage_ledger
Recommended keys:
- Private chat id:
g:{group_id}:u:{user_id}:c:{uuid} - Group shared chat id:
g:{group_id}:c:{uuid}
7. S3 and CloudFront
Voice and upload contract:
- Browser upload: presigned PUT URL from
/api/storage/presign-put - S3 key example:
groups/{group_id}/posts/{post_id}/voice/{lang}/{uuid}.{ext}
- Delivery URL:
- CloudFront URL derived from key and returned by server
8. Backend Integration TODO Boundary
Scaffold routes intentionally leave provider/DB integrations in TODO(...) blocks.
Implement in backend phase:
- Session and group permission checks
- Provider SDK/HTTP calls
- Token/cost computation
- Quota enforcement
- Ledger persistence
- Conversation/message persistence
9. Install and Environment
Install (from repo root):
yarn workspace @ghost-next/frontend add ai @ai-sdk/openai
yarn workspace @ghost-next/frontend add @ai-sdk/google @ai-sdk/anthropic
yarn workspace @ghost-next/frontend add @aws-sdk/client-s3 @aws-sdk/s3-request-presigner
Suggested env vars:
OPENAI_API_KEYGOOGLE_GENERATIVE_AI_API_KEYCLOUDFRONT_URLAWS_REGIONAWS_S3_BUCKETAWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAI_DEFAULT_MODELAI_GROUP_BUDGET_USD_MONTHLY
10. Validation
yarn workspace @ghost-next/frontend exec tsc --noEmit --pretty false
yarn workspace @ghost-next/frontend build
11. References (AI)
AI SDK core and Next.js integration:
- https://ai-sdk.dev/docs/introduction
- https://ai-sdk.dev/docs/getting-started/nextjs-app-router
- https://ai-sdk.dev/providers/ai-sdk-providers/openai
- https://ai-sdk.dev/docs/agents/overview
AI SDK chat UI and transport:
- https://ai-sdk.dev/docs/api-reference/use-chat
- https://ai-sdk.dev/docs/ai-sdk-ui/transport
- https://ai-sdk.dev/elements/components/conversation
- https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-message-persistence
- https://ai-sdk.dev/docs/ai-sdk-ui/chatbot-resume-streams
- https://openchat.ai-sdk.dev/
Audio and realtime:
- https://platform.openai.com/docs/guides/audio
- https://platform.openai.com/docs/guides/realtime/voice-design
- https://platform.openai.com/docs/guides/realtime-transcription
S3 signed upload: