The application currently fails to fetch texts from the Zohar when the AI suggests references in the "Parasha structure" (e.g., "Zohar, Metzora 2:1-3"). This is caused by two factors:
- Contradictory AI Instructions: The system prompt explicitly tells the AI to use "Parasha structure" for Zohar, but these references often don't map cleanly to Sefaria's text segments, leading to "starts at X" errors.
- Brittle Error Handling: When a specific Ref fails (400/404), the application simply shows a toast error ("Could not fetch text..."), leaving the user stuck.
This plan proposes a holistic solution that fixes the "Source" (the AI's instructions) and significantly improves the "Consumer" (the client's resilience) by introducing a "Search Fallback" and "Smart Disambiguation" system.
Important
This plan changes the AI System Prompt in api/chat.js. This will affect future AI responses.
Note
This plan introduces a new UI flow: a "Disambiguation Modal" (or similar selection mechanism) that appears when a direct text fetch fails, offering search results instead of just an error.
- Update System Instruction:
- Remove the contradictory instruction banning Volume:Page for Zohar.
- Explicitly recommend Volume:Page (e.g.,
Zohar 1:1a) as the primary, most reliable citation format. - Add a warning about verse accuracy in Zohar.
- Add
searchSefariaText(query, book):- Implement a function to call Sefaria's Search API.
- This will be used as a fallback when
getSefariaTextfails. - It will search for the text content or the ref string within the specific book index if known.
- Enhance
getSefariaText:- Parse specific Sefaria error messages (like "Zohar, X starts at Y").
- Return structured error objects that can guide the UI (e.g.,
{ error: "REF_MISMATCH", suggestions: [...] }).
- Update
handleAddSource:- Current behavior:
getSefariaText->null->showToast. - New behavior:
- Try
getSefariaText. - If successful, add source.
- If failed:
- Trigger a "Resolving..." state.
- Call
searchSefariaText(source.ref). - If search results found, set a
disambiguationOptionsstate (to be handled by UI). - If no results, then show the error toast.
- Try
- Current behavior:
- A new modal component.
- Props:
isOpen,originalRef,options,onSelect,onCancel. - UI: "We couldn't find exact text for '{originalRef}'. Did you mean one of these?"
- Lists top 3-5 search results with brief snippets.
- User clicks -> calls
onSelect(newRef)-> adds to sheet.
- Integrate the
SourceDisambiguationModal(or pass the need for it up to the parent). - Ensure that when
useSheetManagertriggers disambiguation, this modal appears over the chat or sheet.
- Not applicable for this API-integration heavy change.
- AI Reference Test: Ask the AI "Give me a Zohar text about leprosy derived from Metzora".
- Verify it now suggests a Volume:Page ref (e.g., "Zohar 3:55a") or a valid Parasha ref.
- Failure Fallback Test:
- Manually try to add a broken ref:
Zohar, Metzora 2:1-3. - Verify that instead of a "Could not fetch text" toast, the Disambiguation Modal appears.
- Verify the modal shows relevant Zohar sections (e.g., "Zohar, Leviticus, Tazria..." or "Zohar 3:55a").
- Select an option and verify it adds correctly to the sheet.
- Manually try to add a broken ref: