@braintrust/trace-opencode 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +260 -0
- package/dist/index.js +13452 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# @braintrust/trace-opencode
|
|
2
|
+
|
|
3
|
+
Braintrust integration plugin for [OpenCode](https://opencode.ai). Provides automatic session tracing and data access tools for your Braintrust workspace.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
### 1. Automatic Session Tracing
|
|
8
|
+
|
|
9
|
+
Traces your OpenCode sessions to Braintrust with hierarchical spans:
|
|
10
|
+
|
|
11
|
+
- **Session spans**: Root span for each OpenCode session with metadata (workspace, hostname, etc.)
|
|
12
|
+
- **Turn spans**: Captures each user-assistant interaction
|
|
13
|
+
- **Tool spans**: Records individual tool executions with inputs and outputs
|
|
14
|
+
|
|
15
|
+
### 2. Braintrust Data Access Tools
|
|
16
|
+
|
|
17
|
+
Custom tools available to the AI assistant:
|
|
18
|
+
|
|
19
|
+
- `braintrust_query_logs`: Execute SQL queries against your Braintrust logs
|
|
20
|
+
- `braintrust_list_projects`: List all projects in your organization
|
|
21
|
+
- `braintrust_log_data`: Manually log data for evaluation or tracking
|
|
22
|
+
- `braintrust_get_experiments`: View recent experiments
|
|
23
|
+
|
|
24
|
+
## Quick Start
|
|
25
|
+
|
|
26
|
+
Add to your OpenCode configuration (`opencode.json` or `~/.config/opencode/opencode.json`):
|
|
27
|
+
|
|
28
|
+
```json
|
|
29
|
+
{
|
|
30
|
+
"plugin": ["@braintrust/trace-opencode"]
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Then,
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
# Set your API key
|
|
38
|
+
export BRAINTRUST_API_KEY="your-api-key"
|
|
39
|
+
export TRACE_TO_BRAINTRUST="true"
|
|
40
|
+
|
|
41
|
+
# Run OpenCode
|
|
42
|
+
opencode
|
|
43
|
+
|
|
44
|
+
# View traces at:
|
|
45
|
+
# https://www.braintrust.dev/app/projects/opencode/logs
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Configuration
|
|
49
|
+
|
|
50
|
+
You can configure the plugin using a config file or environment variables.
|
|
51
|
+
|
|
52
|
+
### Config File
|
|
53
|
+
|
|
54
|
+
Create a `braintrust.json` file in one of these locations:
|
|
55
|
+
|
|
56
|
+
- `.opencode/braintrust.json` - Project-level config
|
|
57
|
+
- `~/.config/opencode/braintrust.json` - Global config
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
{
|
|
61
|
+
"trace_to_braintrust": true,
|
|
62
|
+
"project": "my-project",
|
|
63
|
+
"api_key": "your-api-key",
|
|
64
|
+
"debug": true
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Config Options
|
|
69
|
+
|
|
70
|
+
| Config Key | Env Var | Type | Default | Description |
|
|
71
|
+
|------------|---------|------|---------|-------------|
|
|
72
|
+
| `trace_to_braintrust` | `TRACE_TO_BRAINTRUST` | boolean | `false` | Enable/disable tracing |
|
|
73
|
+
| `project` | `BRAINTRUST_PROJECT` | string | `"opencode"` | Project name for traces |
|
|
74
|
+
| `debug` | `BRAINTRUST_DEBUG` | boolean | `false` | Enable debug logging |
|
|
75
|
+
| `api_key` | `BRAINTRUST_API_KEY` | string | | API key for authentication |
|
|
76
|
+
| `api_url` | `BRAINTRUST_API_URL` | string | `"https://api.braintrust.dev"` | API URL |
|
|
77
|
+
| `app_url` | `BRAINTRUST_APP_URL` | string | `"https://www.braintrust.dev"` | App URL |
|
|
78
|
+
| `org_name` | `BRAINTRUST_ORG_NAME` | string | | Organization name |
|
|
79
|
+
|
|
80
|
+
### Precedence
|
|
81
|
+
|
|
82
|
+
Configuration is loaded with the following precedence (later overrides earlier):
|
|
83
|
+
|
|
84
|
+
1. Default values
|
|
85
|
+
2. `~/.config/opencode/braintrust.json` (global config)
|
|
86
|
+
3. `.opencode/braintrust.json` (project config)
|
|
87
|
+
4. Environment variables (highest priority)
|
|
88
|
+
|
|
89
|
+
## Usage
|
|
90
|
+
|
|
91
|
+
### Viewing Traces
|
|
92
|
+
|
|
93
|
+
Once configured, your OpenCode sessions will automatically appear in your Braintrust project. Visit:
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
https://www.braintrust.dev/app/projects/<your-project>/logs
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Using Data Access Tools
|
|
100
|
+
|
|
101
|
+
The AI assistant can use Braintrust tools directly:
|
|
102
|
+
|
|
103
|
+
**Query logs:**
|
|
104
|
+
```
|
|
105
|
+
Can you show me the last 10 logs from Braintrust?
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
**Log data for evaluation:**
|
|
109
|
+
```
|
|
110
|
+
Log this output to Braintrust with a score of 0.9 for accuracy
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
**List projects:**
|
|
114
|
+
```
|
|
115
|
+
What Braintrust projects do I have access to?
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### SQL Query Examples
|
|
119
|
+
|
|
120
|
+
The `braintrust_query_logs` tool supports Braintrust's SQL dialect:
|
|
121
|
+
|
|
122
|
+
```sql
|
|
123
|
+
-- Recent logs
|
|
124
|
+
SELECT * FROM logs ORDER BY created DESC LIMIT 10
|
|
125
|
+
|
|
126
|
+
-- Logs with low scores
|
|
127
|
+
SELECT * FROM logs WHERE scores.Factuality < 0.5
|
|
128
|
+
|
|
129
|
+
-- Logs from the last hour
|
|
130
|
+
SELECT * FROM logs WHERE created > now() - interval 1 hour
|
|
131
|
+
|
|
132
|
+
-- Search by metadata
|
|
133
|
+
SELECT * FROM logs WHERE metadata.task_type = 'code_review'
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Trace Structure
|
|
137
|
+
|
|
138
|
+
Sessions are traced with the following hierarchy:
|
|
139
|
+
|
|
140
|
+
```
|
|
141
|
+
Session (task span)
|
|
142
|
+
├── metadata: session_id, workspace, hostname, username, os
|
|
143
|
+
├── Turn 1 (task span)
|
|
144
|
+
│ ├── input: "user message"
|
|
145
|
+
│ ├── metadata: turn_number, agent, model
|
|
146
|
+
│ ├── Tool 1 (tool span)
|
|
147
|
+
│ │ ├── input: tool arguments
|
|
148
|
+
│ │ └── output: tool result
|
|
149
|
+
│ └── Tool 2 (tool span)
|
|
150
|
+
├── Turn 2 (task span)
|
|
151
|
+
│ └── ...
|
|
152
|
+
└── metrics: total_turns, total_tool_calls
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## Troubleshooting
|
|
156
|
+
|
|
157
|
+
### Plugin not loading / class constructor error
|
|
158
|
+
|
|
159
|
+
If you see an error like `Cannot call a class constructor without |new|`:
|
|
160
|
+
|
|
161
|
+
1. Make sure you're using the latest build:
|
|
162
|
+
```bash
|
|
163
|
+
cd /path/to/braintrust-opencode-plugin
|
|
164
|
+
bun run build
|
|
165
|
+
cp dist/index.js ~/.config/opencode/plugin/braintrust.js
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
2. Or reinstall using the install script:
|
|
169
|
+
```bash
|
|
170
|
+
./install.sh
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### No traces appearing in Braintrust
|
|
174
|
+
|
|
175
|
+
1. Check that your API key is set:
|
|
176
|
+
```bash
|
|
177
|
+
echo $BRAINTRUST_API_KEY
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
2. Enable debug mode to see what's happening:
|
|
181
|
+
```bash
|
|
182
|
+
export BRAINTRUST_DEBUG=true
|
|
183
|
+
opencode
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
3. Check OpenCode logs for errors
|
|
187
|
+
|
|
188
|
+
4. Verify the plugin is loaded:
|
|
189
|
+
- Plugin should log "Braintrust plugin initialized" when OpenCode starts
|
|
190
|
+
|
|
191
|
+
### Tools not working
|
|
192
|
+
|
|
193
|
+
If the Braintrust tools aren't available to the AI:
|
|
194
|
+
|
|
195
|
+
1. Make sure `BRAINTRUST_API_KEY` is set
|
|
196
|
+
2. Check that the plugin loaded successfully
|
|
197
|
+
3. Try asking: "What tools do you have access to?"
|
|
198
|
+
|
|
199
|
+
### API connection errors
|
|
200
|
+
|
|
201
|
+
If you see connection errors:
|
|
202
|
+
|
|
203
|
+
1. Check your internet connection
|
|
204
|
+
2. Verify your API key is valid at https://www.braintrust.dev/app/settings
|
|
205
|
+
3. Check if there's a firewall blocking `api.braintrust.dev`
|
|
206
|
+
|
|
207
|
+
## Development
|
|
208
|
+
|
|
209
|
+
```bash
|
|
210
|
+
# Install dependencies
|
|
211
|
+
bun install
|
|
212
|
+
|
|
213
|
+
# Build
|
|
214
|
+
bun run build
|
|
215
|
+
|
|
216
|
+
# Type check
|
|
217
|
+
bun run typecheck
|
|
218
|
+
|
|
219
|
+
# Run locally with OpenCode
|
|
220
|
+
opencode --plugin-dir ./dist
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## API Reference
|
|
224
|
+
|
|
225
|
+
### BraintrustPlugin
|
|
226
|
+
|
|
227
|
+
The main plugin export. Automatically initializes when OpenCode loads.
|
|
228
|
+
|
|
229
|
+
### BraintrustClient
|
|
230
|
+
|
|
231
|
+
Low-level client for Braintrust API:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
import { BraintrustClient, loadConfig } from "opencode-braintrust"
|
|
235
|
+
|
|
236
|
+
const client = new BraintrustClient(loadConfig())
|
|
237
|
+
await client.initialize()
|
|
238
|
+
|
|
239
|
+
// Insert a span
|
|
240
|
+
await client.insertSpan({
|
|
241
|
+
id: "...",
|
|
242
|
+
span_id: "...",
|
|
243
|
+
root_span_id: "...",
|
|
244
|
+
input: "...",
|
|
245
|
+
output: "...",
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
// Query logs
|
|
249
|
+
const results = await client.queryLogs("SELECT * FROM logs LIMIT 10")
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
## License
|
|
253
|
+
|
|
254
|
+
MIT
|
|
255
|
+
|
|
256
|
+
## Related
|
|
257
|
+
|
|
258
|
+
- [Braintrust](https://braintrust.dev) - AI evaluation and observability platform
|
|
259
|
+
- [OpenCode](https://opencode.ai) - AI-powered coding assistant
|
|
260
|
+
- [braintrust-skill](https://github.com/braintrustdata/braintrust-claude-plugin) - Similar plugin for Claude Code
|