Fix security, resilience, and type-safety issues in AI chat and sync pipeline#5
Conversation
… network retry, toStoragePath, RLS policy, ts-nocheck, shared contextSelector Co-authored-by: yassinekolsi <196232305+yassinekolsi@users.noreply.github.com>
|
@copilot with one check before you do. Open PR 2's actual diff in geminiService.ts and confirm whether BASE_RETRY_DELAY_MS is extracted as a named constant. If it is, PR 2 is strictly better than PR 1 on every axis and you can merge it as-is. If the constant is missing, leave a comment asking for it to be added — it is a one-line change and worth doing while the file is already open. Do not merge PR 1 instead just to get that constant; PR 1's incomplete type coverage is a bigger omission than a missing constant name. |
yassinekolsi
left a comment
There was a problem hiding this comment.
https://gh.io/copilot-coding-agent-docs with one check before you do. Open PR 2's actual diff in geminiService.ts and confirm whether BASE_RETRY_DELAY_MS is extracted as a named constant. If it is, PR 2 is strictly better than PR 1 on every axis and you can merge it as-is. If the constant is missing, leave a comment asking for it to be added — it is a one-line change and worth doing while the file is already open. Do not merge PR 1 instead just to get that constant; PR 1's incomplete type coverage is a bigger omission than a missing constant name.
Co-authored-by: yassinekolsi <196232305+yassinekolsi@users.noreply.github.com>
|
There was a problem hiding this comment.
Pull request overview
Improves the security and robustness of the AI chat + sync pipeline by tightening clinic-scoped authorization, hardening edge-function query behavior, and normalizing data types between Supabase (JSONB) and local SQLite (JSON string).
Changes:
- Tightens AI chat authorization (removes fallback doctor profile) and strengthens
ai_conversationsINSERT RLS to validatepatient_idbelongs to the same clinic. - Improves correctness/resilience: guards empty
.in()queries, retries network failures in Gemini fetch loop, and normalizesextracted_datato a JSON string during sync. - Refactors fallback chat context building to use shared
selectTopKlogic.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| supabase/migrations/004_ai_chat.sql | Adds clinic-consistency validation for ai_conversations inserts via RLS. |
| supabase/functions/extract-handwriting/index.ts | Makes invalid/unknown image URLs fail fast (empty storage path) to produce a clearer 400. |
| supabase/functions/ai-chat-patient/index.ts | Removes auth bypass fallback, adds session-first record fetching, and improves type checking. |
| services/geminiService.ts | Retries transient network failures within the backoff loop. |
| services/cloudSync.ts | Ensures extracted_data is serialized before writing to SQLite. |
| services/aiChat.ts | Deduplicates context selection via shared contextSelector. |
| package-lock.json | Updates dependency metadata and introduces a react-dom resolution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
cloudSync.ts: JSON.stringify extracted_data before calling updateRecordExtractiongeminiService.ts: wrap fetch in try/catch to handle network errors in retry loop; extract BASE_RETRY_DELAY_MS as named constantextract-handwriting/index.ts: return empty string on invalid URL in toStoragePath004_ai_chat.sql: add patient clinic ownership check to INSERT policyai-chat-patient/index.ts: remove @ts-nocheck, enforce auth (require valid doctor profile), fix empty sessions array, add SessionRow/RecordRow typesaiChat.ts: reuse shared contextSelector.ts helpers instead of duplicating logic📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.