evaldoc 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/.env.staging.example +19 -0
  2. package/.firebaserc +6 -0
  3. package/.github/workflows/auto-merge-to-main.yml +28 -0
  4. package/.github/workflows/ci.yml +27 -0
  5. package/.github/workflows/npxhub-publish.yml +170 -0
  6. package/.prettierignore +4 -0
  7. package/.prettierrc.json +7 -0
  8. package/README.md +142 -0
  9. package/SRS.md +448 -0
  10. package/apphosting.staging.yaml +48 -0
  11. package/apphosting.yaml +50 -0
  12. package/bin/evaldoc.cjs +105 -0
  13. package/cors.json +8 -0
  14. package/docs/ARCHITECTURE.md +769 -0
  15. package/docs/ARCHITECTURE_SUMMARY.md +323 -0
  16. package/docs/DEPLOYMENT.md +86 -0
  17. package/docs/DEPLOYMENT_STEPS.md +373 -0
  18. package/docs/DEPLOY_FIRESTORE_RULES.md +239 -0
  19. package/docs/DEPLOY_STORAGE_RULES.md +82 -0
  20. package/docs/DOCTOR_TUTORIAL.md +467 -0
  21. package/docs/FIREBASE_STORAGE_CORS_FIX.md +131 -0
  22. package/docs/FIXES_APPLIED.md +128 -0
  23. package/docs/IMPLEMENTATION.md +498 -0
  24. package/docs/IMPLEMENTATION_COMPLETE.md +317 -0
  25. package/docs/LLM-SETUP.md +216 -0
  26. package/docs/MANUAL_TEST_GUIDE.md +327 -0
  27. package/docs/PATIENT_GUIDE.md +451 -0
  28. package/docs/PLATFORM_OVERVIEW.md +194 -0
  29. package/docs/QUICKSTART.md +252 -0
  30. package/docs/QUICK_REFERENCE.md +222 -0
  31. package/docs/READY_TO_TEST.md +383 -0
  32. package/docs/RUN.md +237 -0
  33. package/docs/START_TESTING.md +130 -0
  34. package/docs/STRIPE_CONNECT_SETUP.md +454 -0
  35. package/docs/SYSTEM_READY.md +345 -0
  36. package/docs/THREADS_IMPLEMENTATION_COMPLETE.md +431 -0
  37. package/docs/THREAD_IMPLEMENTATION_GUIDE.md +345 -0
  38. package/docs/firebasefiles.md +21 -0
  39. package/docs/homepage.md +1208 -0
  40. package/docs/payments.md +159 -0
  41. package/eslint.config.js +76 -0
  42. package/firebase.json +27 -0
  43. package/firestore.indexes.json +88 -0
  44. package/firestore.rules +125 -0
  45. package/functions/package-lock.json +2884 -0
  46. package/functions/package.json +20 -0
  47. package/functions/src/analytics.js +101 -0
  48. package/functions/src/audit.js +28 -0
  49. package/functions/src/auth.js +19 -0
  50. package/functions/src/drafts.js +128 -0
  51. package/functions/src/feedback.js +164 -0
  52. package/functions/src/firebase-init.js +29 -0
  53. package/functions/src/i18n.js +134 -0
  54. package/functions/src/limits.js +27 -0
  55. package/functions/src/llm.js +271 -0
  56. package/functions/src/ner.js +162 -0
  57. package/functions/src/notifications.js +273 -0
  58. package/functions/src/orgs.js +127 -0
  59. package/functions/src/patient-api.js +545 -0
  60. package/functions/src/payment.js +830 -0
  61. package/functions/src/safety.js +188 -0
  62. package/functions/src/stripe-config.js +33 -0
  63. package/functions/src/topics.js +94 -0
  64. package/functions/src/triage.js +191 -0
  65. package/functions/src/utils.js +28 -0
  66. package/functions/src/validate.js +177 -0
  67. package/functions/test-firebase.js +36 -0
  68. package/jest.config.js +18 -0
  69. package/package.json +52 -0
  70. package/public/404.html +93 -0
  71. package/public/app/analytics.html +336 -0
  72. package/public/app/analytics.js +177 -0
  73. package/public/app/app.css +1400 -0
  74. package/public/app/app.js +1754 -0
  75. package/public/app/config.js +14 -0
  76. package/public/app/inbox-threads.js +843 -0
  77. package/public/app/inbox.html +598 -0
  78. package/public/app/inbox.js +397 -0
  79. package/public/app/index.html +612 -0
  80. package/public/assets/logo.jpg +0 -0
  81. package/public/chat.html +137 -0
  82. package/public/css/chat.css +870 -0
  83. package/public/css/landing-enhance.css +298 -0
  84. package/public/css/styles.css +1198 -0
  85. package/public/favicon.svg +5 -0
  86. package/public/find-doctor.html +220 -0
  87. package/public/index.html +399 -0
  88. package/public/js/chat-threads.js +513 -0
  89. package/public/js/chat.js +832 -0
  90. package/public/js/search.js +41 -0
  91. package/public/privacy.html +101 -0
  92. package/public/robots.txt +6 -0
  93. package/public/site.webmanifest +12 -0
  94. package/public/terms.html +110 -0
  95. package/scripts/create-test-provider.js +55 -0
  96. package/scripts/deploy-staging.sh +51 -0
  97. package/scripts/fix-provider.js +39 -0
  98. package/scripts/quick-test.sh +37 -0
  99. package/scripts/test-chat.sh +54 -0
  100. package/scripts/test-multi-turn.js +137 -0
  101. package/scripts/test-ner-simple.js +28 -0
  102. package/scripts/test-ner.js +60 -0
  103. package/scripts/test-stripe-connect.js +304 -0
  104. package/scripts/test-webhook-secret.sh +24 -0
  105. package/server.js +3864 -0
  106. package/storage.rules +22 -0
  107. package/test-api.json +1 -0
  108. package/tests/emulator/README.md +38 -0
  109. package/tests/emulator/counters.emulator.test.js +81 -0
  110. package/tests/integration/api-account-selfserve.test.js +312 -0
  111. package/tests/integration/api-admin-digest.test.js +202 -0
  112. package/tests/integration/api-chat-satisfaction.test.js +259 -0
  113. package/tests/integration/api-doctor-drafts.test.js +236 -0
  114. package/tests/integration/api-doctor-inbox.test.js +401 -0
  115. package/tests/integration/api-org.test.js +328 -0
  116. package/tests/integration/api-payment.test.js +664 -0
  117. package/tests/integration/api-provider-settings.test.js +475 -0
  118. package/tests/integration/api-qr.test.js +121 -0
  119. package/tests/integration/api-security.test.js +241 -0
  120. package/tests/integration/api-stripe-search.test.js +303 -0
  121. package/tests/integration/api-webhook-deletion.test.js +231 -0
  122. package/tests/mocks/firebase-admin.js +220 -0
  123. package/tests/unit/analytics.test.js +151 -0
  124. package/tests/unit/audit-bugs.test.js +203 -0
  125. package/tests/unit/chat-ui-logic.test.js +283 -0
  126. package/tests/unit/drafts.test.js +104 -0
  127. package/tests/unit/feedback.test.js +148 -0
  128. package/tests/unit/i18n.test.js +229 -0
  129. package/tests/unit/llm-retry.test.js +122 -0
  130. package/tests/unit/notifications.test.js +155 -0
  131. package/tests/unit/orgs.test.js +328 -0
  132. package/tests/unit/payment.test.js +889 -0
  133. package/tests/unit/safety-edge-cases.test.js +509 -0
  134. package/tests/unit/safety-srs-compliance.test.js +252 -0
  135. package/tests/unit/safety.test.js +291 -0
  136. package/tests/unit/server-helpers.test.js +774 -0
  137. package/tests/unit/topics-limits.test.js +158 -0
  138. package/tests/unit/triage.test.js +152 -0
  139. package/tests/unit/validate.test.js +408 -0
@@ -0,0 +1,252 @@
1
+ # Quick Start - Deploy EvalDoc to Firebase Online
2
+
3
+ This guide will get your EvalDoc application live in under 10 minutes (assuming Firebase CLI works).
4
+
5
+ ## Current Situation
6
+
7
+ โœ… **Code is ready** - All files configured for Firebase project `evaldocplatform`
8
+ โŒ **Not deployed yet** - Needs to be deployed to Firebase online services
9
+ โš ๏ธ **LLM is stubbed** - Works but gives fake responses (fix after deployment)
10
+
11
+ ---
12
+
13
+ ## Deploy Now (3 Steps)
14
+
15
+ ### Step 1: Login to Firebase
16
+
17
+ ```bash
18
+ npx firebase-tools@latest login
19
+ ```
20
+
21
+ This will open a browser for Google authentication. Login with the Google account that has access to the `evaldocplatform` project.
22
+
23
+ ### Step 2: Deploy Everything
24
+
25
+ ```bash
26
+ cd /Users/ridelink/Documents/GitHub/EvalDoc
27
+ npx firebase-tools@latest deploy
28
+ ```
29
+
30
+ This deploys:
31
+ - โœ… Firestore security rules
32
+ - โœ… Firestore indexes
33
+ - โœ… Cloud Functions (API backend)
34
+ - โœ… Hosting (frontend)
35
+
36
+ Wait 2-5 minutes for deployment to complete.
37
+
38
+ ### Step 3: Enable Firebase Authentication
39
+
40
+ 1. Go to [Firebase Console](https://console.firebase.google.com/)
41
+ 2. Select project: **evaldocplatform**
42
+ 3. Click **Authentication** in left menu
43
+ 4. Click **Get Started**
44
+ 5. Click **Email/Password** โ†’ **Enable** โ†’ **Save**
45
+
46
+ ---
47
+
48
+ ## Your App is Live! ๐ŸŽ‰
49
+
50
+ **Provider Console:** https://evaldocplatform.web.app/app
51
+
52
+ ### Test It
53
+
54
+ 1. **Create Provider Account**
55
+ - Visit: https://evaldocplatform.web.app/app
56
+ - Click "Create account"
57
+ - Enter email/password
58
+ - Fill in provider details
59
+ - Choose handle (e.g., "dr-smith")
60
+ - Select topics
61
+ - Change status to "Published"
62
+ - Click "Save settings"
63
+
64
+ 2. **Test Patient Chat**
65
+ - Visit: https://evaldocplatform.web.app/dr-smith (your handle)
66
+ - Select a topic
67
+ - Send a test message
68
+ - You'll get a response (stubbed for now)
69
+
70
+ 3. **Check Analytics**
71
+ - Go back to: https://evaldocplatform.web.app/app
72
+ - View your conversation stats
73
+
74
+ ---
75
+
76
+ ## What Works Now
77
+
78
+ โœ… Provider signup and authentication
79
+ โœ… Provider profile customization
80
+ โœ… Handle-based public links
81
+ โœ… Patient chat interface
82
+ โœ… Safety checks (blocks diagnosis/dosing)
83
+ โœ… PII redaction (SSN, email, phone)
84
+ โœ… Red flag escalation
85
+ โœ… Topic selection
86
+ โœ… Analytics tracking
87
+ โœ… Rate limiting (50 chats/month)
88
+
89
+ ## What's Stubbed
90
+
91
+ ๐ŸŸก **LLM Responses** - Currently just echoes with generic text
92
+ ๐ŸŸก **QR Code Generation** - Canvas exists but not implemented
93
+
94
+ ---
95
+
96
+ ## Next Step: Add Real AI Responses
97
+
98
+ The app works but gives fake responses. To fix:
99
+
100
+ ### Option A: OpenAI Integration (Easiest)
101
+
102
+ ```bash
103
+ cd functions
104
+ npm install openai
105
+ ```
106
+
107
+ Edit `functions/src/llm.js`:
108
+
109
+ ```javascript
110
+ const OpenAI = require('openai');
111
+
112
+ const openai = new OpenAI({
113
+ apiKey: process.env.OPENAI_API_KEY // Set via Firebase config
114
+ });
115
+
116
+ async function generateResponse({ topicConfig, message, provider }) {
117
+ try {
118
+ const completion = await openai.chat.completions.create({
119
+ model: "gpt-4o-mini", // Cheaper, faster
120
+ messages: [
121
+ {
122
+ role: "system",
123
+ content: `${topicConfig.prompt}
124
+ Provider: ${provider.displayName} (${provider.specialty})
125
+ CRITICAL: Never diagnose, prescribe, or give dosing instructions.
126
+ ${topicConfig.disclaimer}`
127
+ },
128
+ { role: "user", content: message }
129
+ ],
130
+ max_tokens: 150,
131
+ temperature: 0.3
132
+ });
133
+
134
+ return completion.choices[0].message.content;
135
+ } catch (error) {
136
+ console.error('OpenAI error:', error);
137
+ return "I'm having trouble responding right now. Please try again.";
138
+ }
139
+ }
140
+
141
+ module.exports = { generateResponse };
142
+ ```
143
+
144
+ Set your API key:
145
+ ```bash
146
+ npx firebase-tools@latest functions:config:set openai.key="sk-your-key-here"
147
+ ```
148
+
149
+ Redeploy:
150
+ ```bash
151
+ npx firebase-tools@latest deploy --only functions
152
+ ```
153
+
154
+ ### Option B: Anthropic Claude
155
+
156
+ ```bash
157
+ cd functions
158
+ npm install @anthropic-ai/sdk
159
+ ```
160
+
161
+ Similar integration pattern.
162
+
163
+ ### Option C: Google Vertex AI (Best for Firebase)
164
+
165
+ Already has Google Cloud credentials, no extra API keys needed!
166
+
167
+ ---
168
+
169
+ ## Troubleshooting
170
+
171
+ ### Firebase CLI not working?
172
+
173
+ If you get errors with `npx firebase-tools`, try:
174
+
175
+ 1. **Clean npm cache:**
176
+ ```bash
177
+ npm cache clean --force
178
+ ```
179
+
180
+ 2. **Install globally:**
181
+ ```bash
182
+ npm install -g firebase-tools@latest
183
+ firebase login
184
+ firebase deploy
185
+ ```
186
+
187
+ 3. **Use Firebase Console (Manual):**
188
+ - Go to https://console.firebase.google.com/
189
+ - Deploy functions, rules, and hosting manually
190
+
191
+ ### "Provider not found" error?
192
+
193
+ - Make sure you set status to "Published" (not "Draft")
194
+ - Check the handle is correct in the URL
195
+ - Wait 30 seconds after saving settings
196
+
197
+ ### Authentication not working?
198
+
199
+ - Verify Email/Password is enabled in Firebase Console
200
+ - Check browser console for errors
201
+ - Try incognito mode
202
+
203
+ ### Functions not deployed?
204
+
205
+ Check logs:
206
+ ```bash
207
+ npx firebase-tools@latest functions:log
208
+ ```
209
+
210
+ Or in Firebase Console โ†’ Functions โ†’ Logs
211
+
212
+ ---
213
+
214
+ ## Monitoring
215
+
216
+ After deployment:
217
+
218
+ - **Functions Dashboard:** https://console.firebase.google.com/project/evaldocplatform/functions
219
+ - **Firestore Data:** https://console.firebase.google.com/project/evaldocplatform/firestore
220
+ - **Authentication:** https://console.firebase.google.com/project/evaldocplatform/authentication
221
+ - **Hosting:** https://console.firebase.google.com/project/evaldocplatform/hosting
222
+
223
+ ---
224
+
225
+ ## Cost Estimate
226
+
227
+ With Firebase free tier:
228
+ - **50 providers ร— 50 chats/month = 2,500 chats/month**
229
+ - **Firestore:** ~7,500 reads, ~5,000 writes (within free tier)
230
+ - **Functions:** ~2,500 invocations (within free tier)
231
+ - **Hosting:** Minimal bandwidth (within free tier)
232
+
233
+ **Total cost: $0/month** (assuming you stay within free tier)
234
+
235
+ LLM costs (OpenAI GPT-4o-mini):
236
+ - ~$0.15 per 1M input tokens
237
+ - ~$0.60 per 1M output tokens
238
+ - **Estimated:** ~$0.50/month for 2,500 chats
239
+
240
+ ---
241
+
242
+ ## Support
243
+
244
+ Need help?
245
+ 1. Check [DEPLOYMENT.md](DEPLOYMENT.md) for detailed instructions
246
+ 2. Review [SRS.md](SRS.md) for requirements
247
+ 3. Check Firebase logs for errors
248
+ 4. Open GitHub issue (if repo set up)
249
+
250
+ ---
251
+
252
+ **You're ready to deploy! Run the 3 steps above to get live in 10 minutes.**
@@ -0,0 +1,222 @@
1
+ # EvalDoc Thread System - Quick Reference
2
+
3
+ ## ๐Ÿš€ Deploy in 3 Steps
4
+
5
+ ```bash
6
+ # 1. Enable Google Sign-In in Firebase Console
7
+ # โ†’ Authentication โ†’ Sign-in method โ†’ Enable Google
8
+
9
+ # 2. Deploy everything
10
+ firebase deploy --only firestore:rules,functions,hosting
11
+
12
+ # 3. Test
13
+ # Visit: your-domain.com/dr-handle
14
+ # Sign in with Google โ†’ Send message โ†’ Works!
15
+ ```
16
+
17
+ ---
18
+
19
+ ## ๐Ÿ“ Key Files
20
+
21
+ ### Frontend
22
+ - `public/chat.html` - Patient chat UI (with Google auth modal)
23
+ - `public/js/chat-threads.js` - Thread-based chat logic
24
+ - `public/app/inbox.html` - Doctor inbox UI
25
+ - `public/app/inbox-threads.js` - Thread inbox logic
26
+
27
+ ### Backend
28
+ - `functions/src/patient-api.js` - Patient API endpoints
29
+ - `functions/src/index.js` - Doctor API endpoints + routing
30
+ - `firestore.rules` - Security rules
31
+
32
+ ### Docs
33
+ - `DEPLOYMENT_STEPS.md` - Full deployment guide
34
+ - `THREAD_IMPLEMENTATION_GUIDE.md` - Technical details
35
+ - `ARCHITECTURE_SUMMARY.md` - Visual diagrams
36
+ - `THREADS_IMPLEMENTATION_COMPLETE.md` - Summary
37
+
38
+ ---
39
+
40
+ ## ๐Ÿ—‚ Data Structure
41
+
42
+ ```
43
+ patients/{patientId}
44
+ email, displayName, photoURL, doctorIds[]
45
+
46
+ threads/{thread_patientId_doctorId}
47
+ patientId, doctorId, lastMessageAt, unreadCounts, status
48
+ โ†’ messages/{messageId}
49
+ role, message, createdAt, readStatus
50
+ ```
51
+
52
+ ---
53
+
54
+ ## ๐Ÿ”Œ API Endpoints
55
+
56
+ ### Patient
57
+ ```
58
+ POST /api/patient/create Create profile
59
+ POST /api/patient/thread Get/create thread
60
+ POST /api/patient/message Send message
61
+ GET /api/patient/thread/:id/messages Load history
62
+ ```
63
+
64
+ ### Doctor
65
+ ```
66
+ GET /api/doctor/threads Get inbox
67
+ GET /api/doctor/thread/:id Get thread
68
+ POST /api/doctor/message Send reply
69
+ PATCH /api/doctor/thread/:id Update status
70
+ ```
71
+
72
+ ---
73
+
74
+ ## ๐ŸŽฏ Features
75
+
76
+ ### Patient
77
+ - Google Sign-In (one time)
78
+ - Persistent threads (never lost)
79
+ - Multiple doctors (separate threads)
80
+ - Works everywhere (cross-device)
81
+
82
+ ### Doctor
83
+ - See patient names (not IDs)
84
+ - Manual reply (text box)
85
+ - Override AI (toggle button)
86
+ - Unread indicators
87
+ - Archive/escalate threads
88
+
89
+ ---
90
+
91
+ ## ๐Ÿงช Test Scenarios
92
+
93
+ ### New Patient
94
+ ```
95
+ 1. Visit doctor link
96
+ 2. Try to send message
97
+ 3. Sign in with Google
98
+ 4. Message sends โ†’ AI responds
99
+ 5. Refresh โ†’ History persists โœ…
100
+ ```
101
+
102
+ ### Doctor Reply
103
+ ```
104
+ 1. Login to /app/inbox
105
+ 2. See patient thread (green dot if unread)
106
+ 3. Click thread
107
+ 4. Type reply โ†’ Send
108
+ 5. Patient receives message โœ…
109
+ ```
110
+
111
+ ### Manual Mode
112
+ ```
113
+ 1. Doctor clicks "Take Over"
114
+ 2. AI stops responding
115
+ 3. All messages are manual
116
+ 4. Toggle back to enable AI โœ…
117
+ ```
118
+
119
+ ---
120
+
121
+ ## ๐Ÿ” Security
122
+
123
+ - Patients: Only access own threads
124
+ - Doctors: Only access own threads
125
+ - All writes: Backend API only
126
+ - Google tokens: Verified server-side
127
+
128
+ ---
129
+
130
+ ## ๐Ÿ› Troubleshooting
131
+
132
+ ### "Unauthorized" error
133
+ - Check Google Sign-In is enabled
134
+ - Verify token in request headers
135
+ - Re-deploy Firestore rules
136
+
137
+ ### Messages not persisting
138
+ - Check Firestore console for `threads` collection
139
+ - Verify API endpoints are deployed
140
+ - Check browser console for errors
141
+
142
+ ### Google Sign-In fails
143
+ - Add domain to authorized domains
144
+ - Check Firebase config in `/app/config.js`
145
+ - Clear browser cache
146
+
147
+ ---
148
+
149
+ ## ๐Ÿ’ก Quick Tips
150
+
151
+ 1. **Thread ID format:** `thread_{patientId}_{doctorId}`
152
+ 2. **Message roles:** `patient`, `doctor`, `ai`
153
+ 3. **Status values:** `active`, `archived`, `escalated`
154
+ 4. **Override mode:** Disables AI, forces manual replies
155
+ 5. **Unread counts:** `unreadByPatient`, `unreadByDoctor`
156
+
157
+ ---
158
+
159
+ ## ๐Ÿ“Š Monitoring
160
+
161
+ ### Firebase Console
162
+ - **Authentication** โ†’ See new Google users
163
+ - **Firestore** โ†’ Check `threads` collection
164
+ - **Functions** โ†’ View API logs
165
+
166
+ ### Key Metrics
167
+ - New patient signups (Authentication)
168
+ - Thread creation rate (Firestore)
169
+ - Messages per thread (avg)
170
+ - Doctor response time (manual replies)
171
+
172
+ ---
173
+
174
+ ## ๐ŸŽจ Customization
175
+
176
+ ### Change Google button color
177
+ Edit `public/chat.html` โ†’ `#googleSignInBtn` style
178
+
179
+ ### Change thread status options
180
+ Edit `functions/src/patient-api.js` โ†’ `status` field
181
+
182
+ ### Add new message types
183
+ Add to `functions/src/patient-api.js` โ†’ `role` enum
184
+
185
+ ---
186
+
187
+ ## ๐Ÿ“ž Next Steps
188
+
189
+ ### Phase 3 (Optional)
190
+ - [ ] Push notifications
191
+ - [ ] Real-time updates (WebSockets)
192
+ - [ ] File sharing in threads
193
+ - [ ] Video consultations
194
+ - [ ] Export chat history
195
+
196
+ ---
197
+
198
+ ## โœ… Production Checklist
199
+
200
+ - [ ] Google Sign-In enabled
201
+ - [ ] Firestore rules deployed
202
+ - [ ] Functions deployed
203
+ - [ ] Hosting deployed
204
+ - [ ] Test patient flow
205
+ - [ ] Test doctor reply
206
+ - [ ] Monitor Firebase logs
207
+ - [ ] Check no console errors
208
+
209
+ ---
210
+
211
+ ## ๐Ÿ†˜ Need Help?
212
+
213
+ 1. Read `DEPLOYMENT_STEPS.md`
214
+ 2. Check Firebase Console logs
215
+ 3. Look at browser DevTools console
216
+ 4. Review Firestore security rules
217
+
218
+ ---
219
+
220
+ **Status:** โœ… Ready to Deploy
221
+ **Time to Deploy:** ~15 minutes
222
+ **Architecture:** Thread-based with Google Auth