Thank you for your interest in contributing! This document provides guidelines for developing and testing the plugin.
-
Clone the repository
git clone https://github.com/braintrustdata/braintrust-opencode-plugin cd braintrust-opencode-plugin -
Install dependencies
bun install
-
Set up environment variables
cp .env.example .env # Edit .env and add your BRAINTRUST_API_KEY -
Build the plugin
bun run build
# Build once
bun run build
# Watch mode (rebuilds on file changes)
bun run devbun run typecheckTo test your changes with OpenCode:
# Option 1: Use the dist directory directly
opencode --plugin-dir ./dist
# Option 2: Copy to your local plugins directory
cp dist/index.js ~/.config/opencode/plugin/braintrust.jssrc/
├── index.ts # Main plugin entry point
├── client.ts # Braintrust API client
├── tracing.ts # Session tracing hooks
└── tools.ts # Braintrust tools for the AI
dist/ # Build output (generated)
└── index.js # Bundled plugin
The main plugin export that:
- Initializes the Braintrust client
- Registers tracing hooks if enabled
- Registers custom tools
Handles:
- API URL discovery via login endpoint
- Project creation/retrieval
- Span insertion
- SQL query execution
Implements OpenCode plugin hooks:
event: Listens to session lifecycle eventschat.message: Captures user messages and creates turn spanstool.execute.before/after: Records tool executions
Defines custom tools available to the AI:
braintrust_query_logs: Execute SQL queriesbraintrust_list_projects: List projectsbraintrust_log_data: Manually log databraintrust_get_experiments: View experiments
- Add the tool definition in
src/tools.ts:
braintrust_your_tool: tool({
description: "Description of what your tool does",
args: {
param1: tool.schema.string().describe("Parameter description"),
},
async execute(args) {
// Tool implementation
return "result"
},
})- Use the
BraintrustClientmethods to interact with the API - Build and test
- Add the hook in
src/tracing.tswithincreateTracingHooks():
"hook.name": async (input, output) => {
// Hook implementation
}- Update span data as needed
- Build and test
- Build the plugin
- Set
BRAINTRUST_DEBUG=trueto see detailed logs - Run OpenCode with your plugin
- Check Braintrust for traces
- Try using the tools in a conversation
Start a session and verify:
- Session span is created with metadata
- Turn spans are created for each user message
- Tool spans are created for tool executions
- Spans have proper parent-child relationships
In an OpenCode session, try:
- "Query my Braintrust logs for the last 10 entries"
- "What projects do I have in Braintrust?"
- "Log this data to Braintrust with a score of 0.9"
- Use TypeScript strict mode
- Add JSDoc comments for public APIs
- Use async/await for asynchronous code
- Handle errors gracefully with try/catch
- Log debug information when
config.debugis true
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes
- Test thoroughly
- Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Update version in
package.json - Update CHANGELOG.md - Move items from
[Unreleased]to a new version section - Commit the version bump
git add package.json git commit -m "chore: release v0.x.x" - Create a git tag
git tag v0.x.x
- Push to GitHub
git push origin main --tags
- Login to npm (if not already logged in)
npm login
- Publish to npm
npm publish --access public
Note: You must be a member of the
@braintrustnpm organization to publish. Contact an org admin to get access at https://www.npmjs.com/settings/braintrust/members
Note: The
prepublishOnlyscript automatically runsbun run buildbefore publishing. Scoped packages require--access publicto be publicly visible.
- Open an issue for bugs or feature requests
- Check existing issues and PRs
- Refer to OpenCode Plugin Documentation
- Refer to Braintrust Documentation
By contributing, you agree that your contributions will be licensed under the MIT License.