PRD: 2.1 - Foundation & Simple AI Features
Date: October 23, 2025
Status: Code Complete - Manual Integration Required
All AI infrastructure, services, components, and documentation have been created. The features are code-complete but require:
- OpenAI API Key configuration
- Cloud Functions deployment
- Manual integration into chat screen (see integration guide below)
| File | Status | Purpose |
|---|---|---|
functions/src/utils/openai.ts |
✅ Complete | OpenAI client initialization with model config |
functions/src/utils/rateLimiter.ts |
✅ Complete | Per-user rate limiting (10 req/min) |
functions/src/utils/costMonitoring.ts |
✅ Complete | Token usage tracking and cost calculation |
functions/src/utils/errorHandling.ts |
✅ Complete | Standardized error responses |
functions/src/ai/translateMessage.ts |
✅ Complete | Translation Cloud Function |
functions/src/ai/explainContext.ts |
✅ Complete | Cultural context Cloud Function |
functions/src/ai/defineSlang.ts |
✅ Complete | Slang definition Cloud Function |
functions/src/index.ts |
✅ Updated | Exports all AI functions |
| File | Status | Purpose |
|---|---|---|
src/services/ai/translationService.ts |
✅ Complete | Translation with caching |
src/services/ai/contextService.ts |
✅ Complete | Context explanations with caching |
src/services/ai/definitionService.ts |
✅ Complete | Slang definitions with caching |
| File | Status | Purpose |
|---|---|---|
src/components/chat/MessageActions.tsx |
✅ Complete | Long-press action menu |
src/components/chat/TranslationView.tsx |
✅ Complete | Inline translation display |
src/components/chat/ContextExplanation.tsx |
✅ Complete | Cultural context modal |
src/components/chat/SlangDefinition.tsx |
✅ Complete | Slang definition modal |
src/components/chat/MessageBubble.tsx |
✅ Updated | Added AI props and TranslationView |
src/components/chat/MessageList.tsx |
✅ Updated | Added AI callbacks |
| File | Status | Purpose |
|---|---|---|
src/hooks/useAIFeatures.ts |
✅ Complete | AI state management for chat screen |
| File | Status | Purpose |
|---|---|---|
src/types/message.ts |
✅ Updated | Added AIMetadata interface |
src/config/firestoreSchema.ts |
✅ Updated | Documented aiMeta field |
| File | Status | Purpose |
|---|---|---|
_docs/AI_SETUP.md |
✅ Complete | Setup and deployment guide |
_docs/AI_FEATURES.md |
✅ Complete | User guide for AI features |
README.md |
✅ Updated | Added AI features section |
Steps:
# Option A: Firebase environment config (recommended for production)
cd functions
firebase functions:config:set openai.key="your_openai_api_key_here"
# Option B: Local .env file (for development)
cd functions
echo 'OPENAI_API_KEY=your_key_here' > .envGet API Key:
- Go to https://platform.openai.com/
- Sign up or log in
- Navigate to API Keys
- Create new secret key
- Copy the key
cd functions
npm run build # Compile TypeScript
firebase deploy --only functions:translateMessage,functions:explainContext,functions:defineSlangVerify deployment:
- Check Firebase Console > Functions
- Should see 3 new functions listed
The chat screen (app/chat/[id].tsx) needs to be manually updated to connect all the AI components.
Integration Guide:
AI features are already integrated into the main chat screen (app/chat/[id].tsx). See the implementation there for reference.
Quick Summary:
-
Add imports:
import { useAIFeatures } from '../../src/hooks/useAIFeatures'; import { MessageActions } from '../../src/components/chat/MessageActions'; import { ContextExplanation } from '../../src/components/chat/ContextExplanation'; import { SlangDefinition } from '../../src/components/chat/SlangDefinition';
-
Add AI hook:
const { selectedMessage, showActions, showContextExplanation, showSlangDefinition, contextExplanation, slangDefinition, handleMessageLongPress, closeActions, handleTranslate, handleExplainContext, handleDefineSlang, closeContextExplanation, closeSlangDefinition, getTranslationState, } = useAIFeatures();
-
Update MessageList props:
<MessageList // ... existing props onMessageLongPress={handleMessageLongPress} getTranslationState={getTranslationState} />
-
Add modals before closing tag:
{selectedMessage && ( <> <MessageActions visible={showActions} onClose={closeActions} message={selectedMessage} actions={[ { id: 'translate', label: '🌐 Translate to English', onPress: () => handleTranslate(selectedMessage, 'en'), }, { id: 'explain', label: '💡 Explain Cultural Context', onPress: () => handleExplainContext(selectedMessage), }, { id: 'define', label: '📖 Define Slang/Idiom', onPress: () => handleDefineSlang(selectedMessage), }, ]} /> <ContextExplanation visible={showContextExplanation} onClose={closeContextExplanation} explanation={contextExplanation.text} isLoading={contextExplanation.isLoading} error={contextExplanation.error} messageText={selectedMessage.text} /> <SlangDefinition visible={showSlangDefinition} onClose={closeSlangDefinition} definition={slangDefinition.text} isLoading={slangDefinition.isLoading} error={slangDefinition.error} messageText={selectedMessage.text} /> </> )}
After integration and deployment:
-
Build and run the app:
npx expo run:android
-
Test translation:
- Send a message in Spanish: "Hola, ¿cómo estás?"
- Long-press the message
- Select "Translate to English"
- Verify translation appears
-
Test context explanation:
- Send a message with an idiom: "Break a leg!"
- Long-press and select "Explain Cultural Context"
- Verify explanation modal appears
-
Test slang definition:
- Send a message with slang: "That's fire 🔥"
- Long-press and select "Define Slang/Idiom"
- Verify definition modal appears
-
Test caching:
- Translate a message
- Navigate away and back to the conversation
- Long-press and translate again
- Verify instant result (< 100ms)
Create unit tests for AI services:
__tests__/services/ai/translationService.test.ts__tests__/services/ai/contextService.test.ts__tests__/services/ai/definitionService.test.ts
Test cases:
- Cache hit (translation exists)
- Cache miss (calls Cloud Function)
- Error handling (network failure, rate limit)
firebase functions:config:set openai.model="gpt-4-turbo"Edit functions/src/utils/rateLimiter.ts:
const MAX_REQUESTS_PER_WINDOW = 20; // Increase from 10Query the aiUsage collection in Firestore Console to see:
- Token usage per request
- Cost per request
- Total usage by user
-
OpenAI API Key Required:
- Features won't work without a valid API key
- API key must have credits/billing enabled
-
Manual Integration:
- Chat screen integration is manual (not automated)
- Requires careful placement of components
-
English-Only Translation:
- Currently hardcoded to translate to English
- Easy to extend to other languages
-
No Unit Tests:
- Services lack unit tests (recommended to add)
- Integration tests not implemented
-
No Language Detection:
- Translation doesn't detect source language
- OpenAI infers it from context
These are beyond PRD 2.1 scope:
- Auto-translation (translate automatically based on user preferences)
- Multi-language support (translate to any language, not just English)
- Language detection (display detected language)
- Smart replies (AI-generated response suggestions)
- Message tone analysis
- Sentiment detection
Based on typical usage:
| Action | Avg Tokens | Cost | With Caching |
|---|---|---|---|
| Translation (50 char msg) | 40 | $0.000006 | Free after 1st |
| Context Explanation | 100 | $0.000015 | Free after 1st |
| Slang Definition | 50 | $0.000008 | Free after 1st |
Example Monthly Cost:
- 100 users
- 10 translations/user/month
- = 1000 translations = ~$0.006
- Plus explanation/definitions: ~$0.02/month total
Very affordable for MVP!
- Check _docs/AI_SETUP.md
- Check _docs/AI_FEATURES.md
- Review the AI integration in
app/chat/[id].tsx - Check Cloud Functions logs:
firebase functions:log - Verify OpenAI API status: https://status.openai.com/
Before considering PRD 2.1 complete:
- OpenAI API key obtained and configured
- Cloud Functions deployed successfully
- Chat screen manually integrated (see integration guide)
- All 3 AI features tested and working
- Caching verified (instant reloads)
- Error handling tested (network failures, rate limits)
- Cost monitoring set up (review aiUsage collection)
- Documentation reviewed
- User guide shared with team
Once all items checked, PRD 2.1 is COMPLETE! 🎉