@jamesmurdza/opencode-daytona
v0.1.22
Published
OpenCode plugin that automatically runs all sessions in Daytona sandboxes for isolated, reproducible development environments
Readme
Daytona Sandbox Plugin for OpenCode
This is an OpenCode plugin that automatically runs OpenCode sessions in Daytona sandboxes. Each session has it's own remote sandbox and local git branch, which are automatically kept in sync.
Features
- Securely isolate each OpenCode session in a sandbox environment
- Preserves sandbox environments indefinitely until the OpenCode session is deleted
- Generates live preview links when a server starts in the sandbox
- Synchronizes each OpenCode session to a local git branch
Usage
Installation
You can install this plugin globally or within a specific project.
To install it globally, edit ~/.config/opencode/opencode.json. To install for a specific project, add it to opencode.json in the project's root directory.
Adding this plugin to the plugins field will install it automatically when OpenCode starts:
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["@jamesmurdza/opencode-daytona"]
}Environment Configuration
This plugin requires a Daytona account and Daytona API key to create sandboxes.
Set your Daytona API key and URL as environment variables:
export DAYTONA_API_KEY="your-api-key"Or create a .env file in your project root:
DAYTONA_API_KEY=your-api-keyRunning OpenCode
Before starting OpenCode, ensure that your project is a git repository:
git initThen start OpenCode in your project using the OpenCode command:
opencodeOpenCode sessions running in Daytona sandboxes are not immediately distinguishable from local sessions. To ensure that the plugin is working, type pwd in the chat. If the response is /home/daytona, then the plugin is working correctly.
See branches created by OpenCode:
git branchContinue working with OpenCode's latest changes on your local system:
git checkout opencode/1To see live logs for debugging, run this command in a separate terminal:
tail -f ~/.local/share/opencode/log/daytona.logHow It Works
File Synchronization
The plugin uses git to synchronize files between the sandbox and your local system. This happens automatically and in the background, keeping your copy of the code up-to-date without exposing your system to the agent.
Sandbox Setup
When a new Daytona sandbox is created:
- The plugin looks for a git repository in the local directory. If none is found, file synchronization will be disabled.
- In the sandbox, a parallel repository to the local repository is created in the sandbox. An
opencodebranch is created in the sandbox repository. - A new
sandboxremote is added to the local repository using an SSH connection to the sandbox. - The
HEADof the local repository is pushed toopencode, and the sandbox repository is reset to match this initial state. - Each sandbox is assigned a unique incrementing branch number (1, 2, 3, etc.) that persists across sessions.
Synchronization
Each time the agent makes changes:
- A new commit is created in the sandbox repository on the
opencodebranch. - The plugin pulls the latest commits from the sandbox remote into a unique local branch named
opencode/1,opencode/2, etc. This keeps both environments in sync while isolating changes from different sandboxes in separate local branches.
The above workflow only synchronizes changes from the sandbox to your local system. To apply changes made locally, it is recommend to start with a new OpenCode session (and sandbox).
Session-sandbox data storage
The plugin keeps track of which sandbox belongs to each OpenCode project using local state files. This data is stored in a separate JSON file for each project:
- On macOS:
~/.local/share/opencode/storage/daytona/[projectid].json. - On Windows:
%LOCALAPPDATA%\opencode\storage\daytona\[projectid].json.
Each JSON file contains the sandbox metadata for each session in the project, including when the sandbox was created, and when it was last used.
The plugin uses XDG Base Directory specifical to resolve the path to this directory, using the convention set by OpenCode.
Development
This plugin is part of the Daytona monorepo.
Setup
First, clone the Daytona monorepo:
git clone -b feat/opencode-plugin https://github.com/jamesmurdza/daytona.git
git checkout feat/opencode-pluginInstall dependencies:
yarn installDevelopment and Testing
To modify the plugin, edit the source code files in libs/opencode-plugin/.opencode.
To test the OpenCode plugin, create a test project to run OpenCode in:
mkdir ~/myprojectAdd a symlink from the project directory to the plugin source code:
ln -s libs/opencode-plugin/.opencode ~/myprojectStart OpenCode in the project directory:
cd ~/myproject && opencodeUse the instructions from Running OpenCode above to check that the plugin is running and view live logs for debugging.
[!NOTE] When developing locally with a symlink, OpenCode loads the TypeScript source directly, so no build step is required.
Building
Build the plugin:
npx nx run opencode-plugin:buildThis compiles the TypeScript source files in .opencode/ to JavaScript in dist/.opencode/.
Publishing
Log into npm:
npm loginPublish the compiled JavaScript package to npm:
npx nx run opencode-plugin:publishThis will publish to npm with public access and use the version number from package.json.
Project Structure
libs/opencode-plugin/
├── .opencode/ # Source TypeScript files
│ ├── plugin/
│ │ ├── daytona/ # Main Daytona integration
│ │ │ └── ...
│ │ └── index.ts # Plugin entry point
├── dist/ # Build output
│ └── .opencode/ # Compiled JavaScript files
├── package.json # Package metadata (includes main/types)
├── project.json # Nx build configuration
├── tsconfig.json # TypeScript config
├── tsconfig.build.json # TypeScript config
└── README.mdLicense
Apache-2.0
