AI Content Analysis (Read Mode)
Relevant source files
The following files were used as context for generating this wiki page:- src/hooks/read/read.tsx
- src/types/content.ts
- src/utils/prompts/analyze.ts
- src/utils/prompts/explain.ts
- src/entrypoints/side.content/components/side-content/content.tsx
- src/entrypoints/side.content/utils/article.ts
- src/utils/content/summary.ts
- src/entrypoints/side.content/components/side-content/explanation.tsx
The AI Content Analysis, or "Read Mode," is a core feature of Read Frog that transforms web articles into interactive language learning lessons. It leverages Large Language Models (LLMs) to perform a multi-phase analysis of a webpage's main content. The process begins by extracting the text, analyzing it to determine if it's an article and to generate a summary, and then proceeds to a detailed, sentence-by-sentence explanation. This includes translations, vocabulary breakdowns, and grammatical insights tailored to the user's specified language level.
The entire workflow is designed to be robust, with structured data validation, batch processing for large articles, and clear state management to provide feedback to the user throughout the process. The feature is primarily implemented through a series of React hooks and AI prompt generation utilities.
High-Level Architecture
The Read Mode follows a sequential, multi-step process orchestrated by the useReadArticle hook. It begins with user interaction and progresses through content extraction, AI-driven analysis, and finally, a detailed AI-driven explanation which is rendered in the side panel.
This diagram illustrates the overall flow of the AI Content Analysis feature.
Core Components & Hooks
The logic is encapsulated within several custom React hooks that manage state, interact with the AI service, and handle the data processing pipeline.
| Hook | Responsibility | Source File |
|---|---|---|
useReadArticle | The main orchestrator. It initiates the entire analysis flow, from content extraction to final explanation, and manages the overall state. | src/hooks/read/read.tsx |
useAnalyzeContent | Performs the initial analysis of the extracted content. It calls the AI to determine if the text is an article, detect its language, and generate a summary. | src/hooks/read/read.tsx |
useExplainArticle | Manages the detailed, sentence-by-sentence explanation. It batches paragraphs and sends them to the AI for in-depth analysis. | src/hooks/read/read.tsx |
useExtractContent | Extracts the main article content and paragraphs from the current webpage's DOM. (Used as a dependency for useReadArticle). | src/hooks/read/extract.tsx |
Sources: src/hooks/read/read.tsx
Data Flow and State Management
Process States
The system transitions through several states to provide feedback to the user. These states are managed by the readStateAtom (Jotai) and are updated by the core hooks.
The state machine diagram below shows the possible transitions during the analysis process.
Data Models
The entire process relies on structured data, with schemas defined using zod to ensure the AI's output is valid and can be safely processed by the UI.
This class diagram shows the relationships between the main data structures.
Sources: src/types/content.ts
articleAnalysisSchema
This schema validates the output from the initial analysis phase.
| Field | Type | Description |
|---|---|---|
isArticle | boolean | A flag indicating if the AI believes the content is an article. |
detectedLang | string | The ISO 639-3 code for the detected language of the content. |
summary | string | A brief summary of the article. |
introduction | string | An introductory sentence or paragraph. |
terms | string[] | A list of key terms found in the article. |
Sources: src/types/content.ts:19-25
articleExplanationSchema
This schema validates the detailed explanation for each batch of paragraphs. The final result is a flattened array of SentenceExplanation objects.
| Field | Type | Description |
|---|---|---|
paragraphs | SentenceExplanation[][] | A 2D array where the outer array represents paragraphs and the inner array represents sentences within that paragraph. |
originalSentence | string | The original, corrected sentence from the article. |
translatedSentence | string | The sentence translated into the user's target language. |
words | ArticleWord[] | An array of key words/phrases with explanations. |
explanation | string | A detailed explanation of the sentence's grammar, context, or nuances. |
Sources: src/types/content.ts:39-51
AI Interaction
The interaction with the LLM is split into two distinct phases, each with its own dedicated prompt and data schema.
Phase 1: Content Analysis
The useAnalyzeContent hook is responsible for this phase. It sends the article's title and content to the AI. The AI is guided by the prompt from getAnalyzePrompt to analyze the text and return a JSON object matching articleAnalysisSchema. This step is crucial for deciding the subsequent workflow and for gathering context (like the summary) for the next phase.
The prompt instructs the AI to act as a multilingual content analyst and provides a strict JSON output format.
Sources: src/hooks/read/read.tsx:38-83, src/utils/prompts/analyze.ts
Phase 2: Content Explanation
If the content is identified as an article (or the user forces continuation), the useExplainArticle hook takes over.
Batch Processing
To handle large articles and avoid exceeding token limits, the content is processed in batches. Paragraphs are grouped together into batches, ensuring each batch does not exceed a MAX_CHARACTERS limit of 1000 characters. These batches are then sent to the AI sequentially with a fixed delay using the sendInBatchesWithFixedDelay utility.
Sources: src/hooks/read/read.tsx:163-189
Explanation Prompt
For each batch, the explainBatch function calls the AI with a detailed prompt generated by getExplainPrompt. This prompt is highly contextual, instructing the AI to act as a language teacher for a student of a specific level (langLevel).
The prompt includes:
- Source and target languages.
- The user's language proficiency level (
beginner,intermediate,advanced). - The overall article summary from Phase 1 for context.
- The batch of paragraphs to be explained.
- A strict JSON output format (
articleExplanationSchema) with examples.
Sources: src/hooks/read/read.tsx:99-145, src/utils/prompts/explain.ts
Full Sequence Diagram
This diagram shows the complete sequence of interactions for the "Read Article" feature.
Content Extraction & Preparation
Before any AI analysis can occur, the text must be extracted from the webpage.
flattenToParagraphs: This utility function, located insrc/entrypoints/side.content/utils/article.ts, traverses the DOM of the article's main container. It identifies "block-level leaf" nodes and extracts their text content, cleaning and trimming it to form an array of paragraphs. This ensures that only meaningful content is sent for analysis, filtering out navigation, ads, and other non-essential elements. Paragraphs shorter than 20 characters are discarded.generateArticleSummary: While primarily used for the "AI Smart Context" translation feature, this function demonstrates a similar analysis pattern. It takes the page title and text content to generate a 2-3 sentence summary, which is then used to provide context for other AI operations.
Sources: src/entrypoints/side.content/utils/article.ts:6-118, src/utils/content/summary.ts
Conclusion
The AI Content Analysis (Read Mode) is a sophisticated, multi-stage system that provides deep linguistic insights into web articles. By breaking the process into two main phases—a high-level analysis and a detailed, batched explanation—it can handle large amounts of text efficiently. The use of zod schemas for structured data validation and detailed, role-based AI prompts ensures a high-quality, reliable output. The entire feature is a powerful demonstration of how LLMs can be orchestrated to create a rich, contextual, and personalized educational tool.