Chat Interface
Relevant source files
The following files were used as context for generating this wiki page:- src/ipc/handlers/chat_stream_handlers.ts
- src/prompts/system_prompt.ts
- src/prompts/summarize_chat_system_prompt.ts
- e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---dump-request-2.txt
- e2e-tests/snapshots/chat_mode.spec.ts_chat-mode-selector---ask-mode-1.txt
- e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt
- e2e-tests/snapshots/partial_response.spec.ts_partial-message-is-resumed-1.txt
- src/ipc/handlers/chat_stream_handlers.ts
The Chat Interface is the core system enabling users to interact with the Dyad AI assistant to build and modify web applications. It processes user prompts, manages conversation state, constructs context for the AI model, streams responses, and executes actions based on the AI's output. The entire interaction is handled through a real-time, streaming architecture.
The system is designed around a main IPC channel, chat:stream, which orchestrates the conversation flow. It supports various modes of interaction, from direct code generation (build mode) to conceptual guidance (ask mode) and automated task execution (local-agent mode). The AI's capabilities are defined by a structured system prompt and a set of custom XML-like tags (<dyad-*>) that are parsed to perform file system operations, manage dependencies, and control the application preview.
Architecture and Data Flow
The chat system is initiated when a user sends a message. This triggers a series of events handled by the main process, culminating in a streamed response back to the user interface.
High-Level Data Flow
The following diagram illustrates the general flow of a chat request from the user to the AI and back.
Detailed Sequence Diagram
This diagram shows the detailed sequence of operations within the chat:stream handler.
Chat Modes
The chat interface can operate in several distinct modes, each governed by a specific system prompt that dictates the AI's behavior and capabilities. The mode is determined by the chatMode setting.
Sources: src/prompts/system_prompt.ts:468-498
| Mode | Description | System Prompt Source |
|---|---|---|
build | The default mode. The AI acts as an editor, making real-time code changes using <dyad-*> tags. It can write, rename, and delete files, as well as add dependencies. | BUILD_SYSTEM_PROMPT |
ask | A conversational mode where the AI provides explanations and guidance on technical topics without writing any code. All <dyad-*> tags are strictly forbidden. | ASK_MODE_SYSTEM_PROMPT |
agent | An analysis mode where the AI determines what tools, APIs, or resources are needed to fulfill a request. It does not generate code but gathers information for the development phase. | AGENT_MODE_SYSTEM_PROMPT |
local-agent | An advanced mode that uses a tool-based approach to interact with the user's local file system. It can read files, list directories, and perform search-and-replace operations. | constructLocalAgentPrompt |
The system prompt is constructed by the constructSystemPrompt function, which selects the appropriate base prompt for the given chatMode and injects project-specific AI_RULES.
Sources: src/prompts/system_prompt.ts:460-482
System Prompts and AI Context
The AI's behavior is heavily guided by a detailed system prompt constructed before each request.
Prompt Construction
This diagram shows how a base prompt is selected based on the chat mode and combined with AI_RULES to form the final system prompt.
Sources: src/prompts/system_prompt.ts:460-482, src/prompts/system_prompt.ts:499-510
Contextual Information
In addition to the system prompt, the following information is provided to the AI to give it context for the user's request:
- Codebase Information: A formatted string containing the file structure and content of relevant files from the user's project. This is generated by
extractCodebase. - Chat History: A list of previous user and assistant messages. The history is limited to a configurable number of turns (
maxChatTurnsInContext). - Selected Components: If the user selects a component in the UI, its file path, name, and a code snippet are appended to the prompt.
- Attachments: Information about attached files, including their names and types. For text-based files, the content may be inlined. For files intended for the codebase, a special instruction is provided.
- Mentioned Apps: If the user mentions another app (e.g.,
@app:my-other-app), that app's codebase is included as read-only context.
Sources: src/ipc/handlers/chat_stream_handlers.ts:515-628
AI Response Processing
The AI responds with a stream of text that may contain special XML-like tags. These tags are instructions for the backend to perform actions.
Dyad Action Tags
The AI uses these tags to interact with the project. The tags are parsed from the final, complete response.
| Tag | Description | Attributes | Example from Prompts |
|---|---|---|---|
<dyad-write> | Creates a new file or overwrites an existing one. | path, description | <dyad-write path="src/components/Button.tsx" ...> |
<dyad-rename> | Renames or moves a file. | from, to | <dyad-rename from="src/components/UserProfile.tsx" to="src/components/ProfileCard.tsx"> |
<dyad-delete> | Deletes a file. | path | <dyad-delete path="src/components/Analytics.tsx"> |
<dyad-add-dependency> | Adds one or more npm packages. | packages (space-separated) | <dyad-add-dependency packages="react-hot-toast another-react-package"> |
<dyad-command> | Suggests a UI command for the user to run. | type (rebuild, restart, refresh) | <dyad-command type="rebuild"></dyad-command> |
<dyad-chat-summary> | Sets the title for the current chat session. | (none) | <dyad-chat-summary>Adding a new component</dyad-chat-summary> |
<dyad-security-finding> | Formats a security vulnerability report. | title, level | <dyad-security-finding title="SQL Injection..." level="critical"> |
<think> | Contains the AI's internal thought process. This is not shown to the user directly but is part of the stream. | (none) | <think>• **Identify the specific UI/FE bug...**</think> |
Sources: src/prompts/system_prompt.ts:70-300, e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt, src/prompts/summarize_chat_system_prompt.ts:31-34
Stream Handling and Cancellation
The chat:stream handler manages the lifecycle of the AI response stream.
- Active Streams: An
activeStreamsmap tracks ongoing requests using anAbortControllerfor eachchatId. - Cancellation: The
chat:cancelIPC handler aborts the corresponding stream. When a stream is aborted, the partial response received so far is saved to the database with a[Response cancelled by user]message. - Partial Responses: A
partialResponsesmap stores the incomplete response for a stream, ensuring it can be saved upon cancellation. - Unclosed Tags: The system includes logic to detect if a response stream ends with an unclosed
<dyad-write>tag. If so, it attempts to continue the stream to get the complete file content.
Sources: src/ipc/handlers/chat_stream_handlers.ts:121-125, src/ipc/handlers/chat_stream_handlers.ts:987-1008, src/ipc/handlers/chat_stream_handlers.ts:860-893
Automated Error Correction
To improve reliability, the system has an "auto-fix" feature for problems detected after the AI generates a response.
- Dry Run: For
buildmode, the system can perform a dry run of the proposed changes (dryRunSearchReplacefor Turbo Edits v2,generateProblemReportfor TypeScript errors). - Problem Detection: If the dry run reveals errors (e.g., TypeScript compilation errors), a problem report is generated.
- Re-prompting: The AI is re-prompted with the original context, its flawed response, and the list of problems, asking it to fix them.
- Loop: This process can repeat up to 2 times to resolve issues before finalizing the response.
This mechanism is enabled by the enableAutoFixProblems setting.
Sources: src/ipc/handlers/chat_stream_handlers.ts:894-985
Special Intents
Certain user prompts starting with a / or specific phrases trigger special behaviors by loading a different system prompt.
/security-review: This command replaces the standard system prompt withSECURITY_REVIEW_SYSTEM_PROMPT. The AI then acts as a security expert, analyzing the codebase for vulnerabilities and reporting them using the<dyad-security-finding>tag.Summarize from chat-id=<id>: This triggers theSUMMARIZE_CHAT_SYSTEM_PROMPT, causing the AI to summarize a previous chat session. It focuses on major changes and relevant files, outputting the result with a<dyad-chat-summary>tag.
Sources: src/ipc/handlers/chat_stream_handlers.ts:664-683, src/ipc/handlers/chat_stream_handlers.ts:699-702, src/prompts/summarize_chat_system_prompt.ts, e2e-tests/snapshots/security_review.spec.ts_security-review-1.txt