# Feature Design: Automatic Title Generation (feat-auto-title-generation) ## 1. Overview This feature automatically generates meaningful titles for uploaded documents and chat sessions using AI. It aims to replace generic filenames and "New Conversation" labels with content-aware titles, improving user experience and organization. ## 2. Requirements ### 2.1 Document Title Generation - **Trigger**: Automatically triggered after text extraction (Fast or Precise mode). - **Process**: 1. Extract a sample of the document content (first 2,000 - 3,000 characters). 2. Send the content to the default LLM with a specific generation prompt. 3. Update the `KnowledgeBase` record with the generated title. - **Rules**: - The title should be concise (less than 50 characters). - It should be in the user's preferred language (defaulting to the detected document language if possible). - Output should be "raw" (no preamble like "The title is..."). ### 2.2 Chat Title Generation - **Trigger**: Triggered after the first user message and its corresponding assistant response are recorded. - **Process**: 1. Collect the initial message pair. 2. Send the pair to the default LLM with a generation prompt. 3. Update the `SearchHistory` record's `title` field. - **Rules**: Same as document titles. ## 3. Technical Design ### 3.1 Data Model Changes - **KnowledgeBase Entity**: Add a `title` field (nullable, optional). If empty, fallback to `originalName`. - **SearchHistory Entity**: No changes required (has `title`). ### 3.2 Backend Implementation #### KnowledgeBaseService - Add `generateTitle(kbId: string)` method. - Hook into `processFile` after `updateStatus(kbId, FileStatus.EXTRACTED)`. #### ChatService / SearchHistoryService - Add logic to check if the session title is still the default (usually the first message snippet) and trigger `generateTitle(historyId: string)` after the first assistant response. #### Prompt Design - **Document Prompt**: ```text You are a document analyzer. Read the provided text and generate a concise, professional title (max 50 chars). Return ONLY the title. Language: {userLanguage} Text: {contentSample} ``` - **Chat Prompt**: ```text Based on the following conversation snippet, generate a short, descriptive title (max 50 chars) that summarizes the topic. Return ONLY the title. Language: {userLanguage} Snippet: User: {userMessage} AI: {aiResponse} ``` ## 4. Verification Plan ### Automated Tests - Integration tests in `KnowledgeBaseService` to verify the title field is updated after processing. - Mock LLM responses to ensure the title update logic works. ### Manual Verification - Upload various files (PDF, Word, TXT) and verify the displayed title in the knowledge base list. - Start a new chat, send a message, and check the sidebar for the updated session title.