Package Exports
- opencode-codetime
Readme
opencode-codetime
CodeTime plugin for OpenCode -- track your AI coding activity and time spent.
Features
- Automatic time tracking -- sends coding events to CodeTime when OpenCode reads, edits, or writes files
- Check your coding time -- ask the AI "what's my coding time?" and it fetches your stats via the
codetimetool - Language detection -- detects 90+ programming languages from file extensions
- Git integration -- captures current branch and remote origin
- Project identification -- shows as
[opencode] project-nameon your CodeTime dashboard - Rate-limited -- one heartbeat per 2 minutes to avoid API spam
- Session lifecycle -- flushes pending events on session end so no data is lost
- Zero config -- just set your token and go
Prerequisites
A CodeTime account. Sign up for free at codetime.dev.
Get your upload token from CodeTime Settings.
Installation
Via opencode config (recommended)
Add to your opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-codetime"]
}From source
git clone https://github.com/roman-pinchuk/opencode-codetime
cd opencode-codetime
npm install && npm run buildThen copy the built files to your OpenCode plugins directory:
mkdir -p ~/.config/opencode/plugins
cp dist/*.js ~/.config/opencode/plugins/Configuration
Set your CodeTime upload token as an environment variable:
export CODETIME_TOKEN="your-upload-token-here"Add it to your shell profile (~/.bashrc, ~/.zshrc, etc.) to persist across sessions.
The plugin validates the token on startup and logs a warning if it is missing or invalid.
How It Works
The plugin hooks into OpenCode's event system to detect file activity.
OpenCode events Plugin CodeTime API
+-----------------------+ +------------------+ +------------------------+
| message.part.updated | --> | Extract files | --> | POST /v3/users/event-log|
| (edit, write, read, | | Detect language | | |
| patch, multiedit) | | Rate limit (2m) | | { eventTime, language, |
+-----------------------+ +------------------+ | project, relativeFile,|
| session.idle | --> | Force flush | | editor: "opencode", |
| session.deleted | | pending events | | platform, git info } |
+-----------------------+ +------------------+ +------------------------+What gets tracked
Each heartbeat sent to CodeTime includes:
| Field | Value |
|---|---|
eventTime |
Unix timestamp of the event |
language |
Detected from file extension (e.g. TypeScript, Python) |
project |
[opencode] directory-name |
relativeFile |
File path relative to the project root |
editor |
opencode |
platform |
OS platform (darwin, linux, windows) |
gitOrigin |
Remote origin URL (if available) |
gitBranch |
Current branch name (if available) |
Hooks used
| Hook | Purpose |
|---|---|
event |
Listens for message.part.updated (tool completions) and session.idle/session.deleted (flush) |
chat.message |
Processes pending heartbeats on chat activity |
tool |
Registers codetime tool to check today's coding time |
Tool tracking
| Tool | Data Extracted |
|---|---|
read |
File path from title |
edit |
File path from filediff metadata |
write |
File path from metadata |
patch |
File paths from output |
multiedit |
File paths from each edit result |
Project Structure
src/
index.ts Main plugin entry point with event hooks
codetime.ts CodeTime API client (token validation, heartbeats)
state.ts Rate limiter (max 1 heartbeat per 2 minutes)
language.ts File extension to language name mapping
git.ts Git branch and remote origin extraction
logger.ts Structured logging via OpenCode SDKDevelopment
# Install dependencies
npm install
# Type check
npm run typecheck
# Build
npm run buildTroubleshooting
Plugin not loading
- Verify
opencode.jsonsyntax - Check that
CODETIME_TOKENis set in your environment - Check OpenCode logs for
opencode-codetimemessages
Heartbeats not appearing in CodeTime
- Verify your token at codetime.dev/dashboard/settings
- Make sure you are editing files (not just chatting) -- heartbeats fire on tool completions
- Wait up to 2 minutes for rate-limited heartbeats to flush
- Check that the CodeTime API is reachable:
curl -s https://api.codetime.dev/v3
Token validation fails
- Regenerate your token at codetime.dev/dashboard/settings
- Make sure the token has no leading/trailing whitespace