Using Local Models (Ollama & LM Studio)
Relevant source files
The following files were used as context for generating this wiki page:
- src/ipc/handlers/local_model_ollama_handler.ts
- src/ipc/handlers/local_model_lmstudio_handler.ts
- src/hooks/useLocalModels.ts
- e2e-tests/ollama.spec.ts
- e2e-tests/lm_studio.spec.ts
- src/prompts/system_prompt.ts
- e2e-tests/snapshots/local_agent_basic.spec.ts_local-agent---read-then-edit-1.aria.yml
- src/prompts/local_agent_prompt.ts
The Dyad application supports integration with local Large Language Models (LLMs) through services like Ollama and LM Studio. This functionality enables a special local-agent chat mode, where the AI can directly interact with the user's local file system using a set of predefined tools. This allows for powerful, on-device code generation and modification tasks without relying on cloud-based models.
This system is composed of three main parts: a frontend React hook for UI integration, backend IPC handlers for managing the local model server processes, and a specialized system prompt that instructs the AI on how to behave as a local agent.
Architecture Overview
The local model feature connects the user interface with external local model servers via Electron's IPC (Inter-Process Communication) mechanism. The UI triggers actions, the backend handlers manage the server's lifecycle (start/stop), and the AI model, running locally, receives prompts and can execute tools that affect the local codebase.
The following diagram illustrates the high-level data flow:
Frontend Integration: useLocalModels
The useLocalModels hook is the primary interface for the frontend to interact with the local models feature. It manages the state and provides functions to control the local model servers.
Sources: src/hooks/useLocalModels.ts
State and Functions
The hook provides the following state variables and functions:
| Name | Type | Description |
|---|---|---|
localModels | LocalModel[] | An array of available local models (e.g., Ollama, LM Studio). |
selectedLocalModel | LocalModel | null | The currently selected local model. |
isLocalModelRunning | boolean | Indicates if the selected local model's server is running. |
fetchLocalModels() | () => Promise<void> | Fetches the list of available local models. |
startLocalModel() | (model: LocalModel) => Promise<void> | Starts the server for the specified local model. |
stopLocalModel() | (model: LocalModel) => Promise<void> | Stops the server for the specified local model. |
setSelectedLocalModel() | (model: LocalModel | null) => void | Sets the selected local model in the application state. |
Sources: src/hooks/useLocalModels.ts:18-30, src/hooks/useLocalModels.ts:51-91
Interaction Flow
The following sequence diagram shows how a React component would use the useLocalModels hook to start a local model.
Backend IPC Handlers
The backend consists of IPC handlers that listen for commands from the frontend to manage the lifecycle of local model servers. There are separate handlers for Ollama and LM Studio.
Ollama Handler
The local_model_ollama_handler.ts is responsible for downloading (if necessary), starting, and stopping the Ollama server process.
Sources: src/ipc/handlers/local_model_ollama_handler.ts
IPC Channels
| Channel | Direction | Description |
|---|---|---|
local-model:ollama:start | FE -> BE | Initiates the startup sequence for the Ollama server. |
local-model:ollama:stop | FE -> BE | Stops the running Ollama server process. |
local-model:ollama:get-status | FE -> BE | Retrieves the current running status of the Ollama server. |
Sources: src/ipc/handlers/local_model_ollama_handler.ts:133-135
Startup Logic
The startOllama function contains platform-specific logic to ensure the Ollama server is running.
The handler also includes logic to download Ollama if it's not found on the system.
LM Studio Handler
The local_model_lmstudio_handler.ts is responsible for locating and launching the LM Studio application to start its server. Unlike the Ollama handler, it does not manage downloads.
Sources: src/ipc/handlers/local_model_lmstudio_handler.ts
IPC Channels
| Channel | Direction | Description |
|---|---|---|
local-model:lmstudio:start | FE -> BE | Finds and launches the LM Studio application. |
local-model:lmstudio:stop | FE -> BE | Stops the running LM Studio server process. |
local-model:lmstudio:get-status | FE -> BE | Checks if the LM Studio server is responsive. |
Sources: src/ipc/handlers/local_model_lmstudio_handler.ts:74-76
The start logic involves searching for the LM Studio application in common installation directories for the user's operating system (macOS and Windows).
Sources: src/ipc/handlers/local_model_lmstudio_handler.ts:25-56
Local Agent Mode
When a local model is active, the application can use the local-agent chat mode. This mode is enabled by a specific system prompt that instructs the AI to use a set of tools for local file system interaction.
Sources: src/prompts/system_prompt.ts:494-496, src/prompts/local_agent_prompt.ts
System Prompt
The constructLocalAgentPrompt function assembles a detailed system prompt. This prompt defines the AI's role as a "Dyad AI assistant" that can make real-time code changes. It strictly enforces a tool-calling paradigm.
Key instructions in the prompt include:
- Tool Usage: The AI is instructed to use tools to solve coding tasks and to read files before writing to them.
- Autonomy: The AI is told to make a plan and follow it without waiting for user confirmation.
- Available Tools: The prompt defines a schema for tools like
read_file,write_file,search_replace,add_dependency, andlist_files.
Sources: src/prompts/local_agent_prompt.ts:1-100
Tool-Based Interaction
The end-to-end tests and their snapshots demonstrate how the local agent uses these tools. For example, a user request to "read then edit" a file results in a sequence of tool calls.
The following diagram, based on test snapshots, shows this flow:
End-to-End Testing
The functionality is validated through E2E tests for both Ollama (ollama.spec.ts) and LM Studio (lm_studio.spec.ts). These tests follow a similar pattern:
- Start the Dyad application.
- Navigate to the local models settings.
- Click the button to start the respective local model server (Ollama or LM Studio).
- Wait for the UI to confirm the server is running.
- Select the local model for use.
- Send a prompt that requires a local file system action (e.g., "write a file").
- Assert that the AI responds with the correct tool call and that the action is reflected in the UI.
Sources: e2e-tests/ollama.spec.ts:10-39, e2e-tests/lm_studio.spec.ts:10-39
Summary
The local models integration is a powerful feature that allows Dyad to leverage on-device AI for direct codebase manipulation. It uses a combination of frontend state management, backend process management via IPC, and a specialized local-agent prompt to enable tool-based file system interactions. This architecture provides a secure and efficient way to perform AI-driven development locally.