Documentation Index
Fetch the complete documentation index at: https://mintlify.com/remorses/kimaki/llms.txt
Use this file to discover all available pages before exploring further.
System Architecture
Kimaki is a Discord bot that orchestrates OpenCode AI coding sessions. It acts as a bridge between Discord’s messaging interface and OpenCode’s AI-powered development environment.Core Architecture
One Bot Per Machine
Kimaki follows a one bot per machine architecture by design. Each Discord bot instance you create is tied to a single machine where it runs.kimaki on a computer:
- It spawns OpenCode servers for projects on that machine
- The bot can only access directories where it’s running
- Channel metadata stores which bot (app ID) owns each channel
- Create a separate Discord bot for each machine
- Run
kimakion each machine with its own bot token - Add all bots to the same Discord server
Component Overview
Discord Bot (discord-bot.ts)
The main event loop that handles Discord interactions:
- Message routing: Routes messages from text channels and threads to OpenCode sessions
- Permission checks: Validates user permissions (Server Owner, Administrator, Manage Server, or “Kimaki” role)
- Worktree management: Creates isolated git worktrees for feature development
- Voice processing: Transcribes voice messages using Gemini API
Session Handler (session-handler.ts)
Manages the lifecycle of OpenCode sessions:
- Session creation/reuse: Creates new sessions or reuses existing ones per thread
- Event streaming: Subscribes to OpenCode SSE events for real-time updates
- Abort handling: Manages session interrupts when new messages arrive
- Message queue: Queues follow-up messages while AI is responding
OpenCode Server Manager (opencode.ts)
Spawns and maintains OpenCode API servers:
- Per-directory servers: One OpenCode server process per project directory
- Auto-restart: Automatically restarts crashed servers with exponential backoff
- Port allocation: Dynamically allocates ports for each server
- Health checks: Monitors server health via
/api/healthendpoint
SQLite Database (database.ts)
Persists state between bot restarts:
<data-dir>/discord-sessions.db (default: ~/.kimaki/discord-sessions.db)
Channel Management (channel-management.ts)
Handles Discord channel creation and metadata:
- Creates text channels under “Kimaki” category
- Creates voice channels under “Kimaki Audio” category
- Stores channel configuration in SQLite (not in channel topic)
- Maps channels to project directories
Data Flow
Message Flow: Discord → OpenCode
Response Flow: OpenCode → Discord
Process Architecture
Bot Process
OpenCode Server Processes
Plugin System
Kimaki extends OpenCode via plugins (opencode-plugin.ts):
- Runs inside OpenCode server process (not bot process)
- Cannot access bot-side config directly
- Receives bot state via
KIMAKI_*env vars - Provides custom tools:
kimaki_question,kimaki_action_buttons,kimaki_file_upload
Lock Port & Single Instance
Kimaki enforces single-instance behavior by binding a lock port:Logging & Debugging
Log File
Location:<data-dir>/kimaki.log (default: ~/.kimaki/kimaki.log)
- Reset on every bot startup
- Contains logs from current run only
- Use for debugging session failures and internal issues
Heap Snapshots
Graceful Restart
Multiple Instances
Run multiple bot instances on the same machine:- Database
- Projects directory
- Lock port
- Discord bot credentials
Network Topology
127.0.0.1 (localhost only) for security. Only the Kimaki bot process has network access to Discord.
Security Model
- Permission checks: Every message validates Discord roles/permissions
- Tool permissions: OpenCode
permission.askevents trigger Discord buttons - No remote code execution: Users cannot run arbitrary commands without approval
- Isolated worktrees: Each thread can use a separate git worktree
- Local-only servers: OpenCode servers only bind to localhost
Performance Considerations
Connection Pool
session.prompt) get blocked.
Thread Message Queue
Error Handling
Kimaki useserrore for type-safe error handling throughout:
- Console (with prefixes like
[SESSION],[OPENCODE]) - Sentry (for production monitoring)
- Log file (
kimaki.log)