Feature: AI Chat
Relevant source files
The following files were used as context for generating this wiki page:The AI Chat feature provides an intelligent conversational interface, allowing users to ask questions in natural language about their legal documents. The primary goal is to offer context-aware answers and clarifications on complex legal terms and clauses. Users can access this feature from a quick action card on the Home screen.
Currently, the frontend implementation is functional but uses a simulated AI response. The backend is equipped with a generic Large Language Model (LLM) invocation service, invokeLLM, designed to connect to the forge.manus.im API, which will eventually power the real-time chat responses.
Sources: design.md, app/(tabs)/chat.tsx:28-33, app/(tabs)/index.tsx:78-86
Architecture Overview
The AI Chat feature is composed of a frontend React Native component and a backend LLM service. The frontend is responsible for the user interface and state management, while the backend service handles communication with the external LLM API.
This diagram illustrates the high-level user flow for the AI Chat feature.
Frontend Implementation
The chat interface is built using React Native components within the app/(tabs)/chat.tsx file. It manages the conversation state locally and renders the message history.
Sources: app/(tabs)/chat.tsx
UI Components and State
The ChatScreen component uses useState to manage the following:
messages: An array ofMessageobjects representing the conversation.inputText: The string value of the user's current message in the input field.isLoading: A boolean to indicate when the "AI" is "thinking".
The UI is constructed with:
- A
FlatListto efficiently render the list of messages. - A
TextInputfor message input. TouchableOpacitycomponents for sending messages and selecting suggested questions.
Sources: app/(tabs)/chat.tsx:16-25
Data Structures
The primary data structure on the frontend is the Message interface.
| Property | Type | Description |
|---|---|---|
id | string | A unique identifier for the message. |
text | string | The content of the message. |
sender | "user" | "ai" | Indicates who sent the message. |
timestamp | string | The time the message was sent. |
Sources: app/(tabs)/chat.tsx:9-14
Simulated Chat Flow
When a user sends a message, the handleSendMessage function is triggered. The current implementation does not make a real API call. Instead, it simulates a response after a 1.5-second delay.
This sequence diagram shows the current simulated message flow.
Sources: app/(tabs)/chat.tsx:40-58
Suggested Questions
To guide the user, the chat screen displays a list of suggested questions when the conversation is new. Tapping a suggestion populates the text input. These questions are retrieved using the internationalization function t().
Sources: app/(tabs)/chat.tsx:27-31, 93-104, lib/translations.ts:102-105
Backend LLM Service
The backend service for handling LLM interactions is defined in server/_core/llm.ts. It provides a generic function, invokeLLM, to communicate with a configured LLM provider, specified as forge.manus.im.
Sources: server/_core/llm.ts
Core Function: invokeLLM
This asynchronous function is the central point for all LLM communications. It constructs a request payload and sends it to the Forge API endpoint.
Key Operations:
- Asserts that an API key (
ENV.forgeApiKey) is available. - Normalizes the
messagesarray into the format expected by the API. - Sets the model to
gemini-2.5-flashand amax_tokenslimit of 32768. - Handles
toolsandtool_choiceparameters if provided. - Constructs the final API endpoint URL, defaulting to
https://forge.manus.im/v1/chat/completions. - Sends a
POSTrequest with the payload and authorization header. - Returns the JSON response or throws an error if the request fails.
Sources: server/_core/llm.ts:153-154, 202-243
This sequence diagram illustrates the invokeLLM function's interaction with the Forge API.
Sources: server/_core/llm.ts:202-243
Data Models
The service defines several type aliases to model the data exchanged with the LLM API.
This diagram shows the relationships between the main data types.
Sources: server/_core/llm.ts:5-84
Key parameters for the invokeLLM function are defined in InvokeParams.
| Parameter | Type | Description |
|---|---|---|
messages | Message[] | An array of message objects forming the conversation history. |
tools | Tool[] | (Optional) A list of functions the model may call. |
toolChoice | ToolChoice | (Optional) Controls which function is called by the model. |
maxTokens | number | (Optional) The maximum number of tokens to generate. Hardcoded to 32768. |
outputSchema | OutputSchema | (Optional) Defines a JSON schema for the model's output. |
responseFormat | ResponseFormat | (Optional) Specifies the output format, e.g., json_object. |
Sources: server/_core/llm.ts:50-61, 222
Internationalization
All user-facing text in the AI Chat feature is managed through a centralized translation system, allowing for support in English (EN), Spanish (ES), and German (DE).
Sources: lib/translations.ts:3, 290
Chat-Specific Translations
The translations object in lib/translations.ts contains a "Chat Screen" section with keys for all UI elements.
| Key | English (EN) | Spanish (ES) | German (DE) |
|---|---|---|---|
chatWithAI | Chat with AI | Chat con IA | Chat mit KI |
chatWithAIDesc | Ask questions about your legal documents | Haz preguntas sobre tus documentos legales | Stelle Fragen zu deinen Rechtsdokumenten |
suggestedQuestions | Suggested Questions | Preguntas Sugeridas | Vorgeschlagene Fragen |
whatAreTheMainRisks | What are the main risks? | ¿Cuáles son los principales riesgos? | Was sind die Hauptrisiken? |
askQuestion | Ask a question... | Haz una pregunta... | Stelle eine Frage... |
aiThinking | AI is thinking... | La IA está pensando... | KI denkt nach... |
helloIAmYourLegal | Hello! I'm your Legal AI assistant... | ¡Hola! Soy tu asistente Legal AI... | Hallo! Ich bin dein Legal AI Assistent... |
thisIsASimulated | This is a simulated AI response... | Esta es una respuesta simulada de IA... | Dies ist eine simulierte KI-Antwort... |
Sources: lib/translations.ts:99-108, 233-242, 367-376
Conclusion
The AI Chat feature is a cornerstone of the application, designed to provide interactive legal analysis. The frontend is fully built out with a responsive, internationalized UI, though it currently relies on simulated data. The backend architecture is in place with the invokeLLM service, ready to be integrated with the frontend to replace the simulation with live, intelligent responses from the gemini-2.5-flash model via the Forge API. The next step is to connect the handleSendMessage function in app/(tabs)/chat.tsx to a new API route that utilizes server/_core/llm.ts.