devbonzai 2.2.208 → 2.2.210

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devbonzai",
3
- "version": "2.2.208",
3
+ "version": "2.2.210",
4
4
  "description": "Quickly set up a local file server in any repository for browser-based file access",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -0,0 +1,42 @@
1
+ // Serves the bonzai capture client script for screenshot functionality
2
+ function bonzaiCaptureClientHandler(req, res) {
3
+ res.set('Content-Type', 'application/javascript');
4
+ res.send(`(function() {
5
+ if (window.__bonzaiCaptureLoaded) return;
6
+ window.__bonzaiCaptureLoaded = true;
7
+ var script = document.createElement('script');
8
+ script.src = 'https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js';
9
+ script.onload = function() {
10
+ window.addEventListener('message', function(e) {
11
+ if (!e.data || e.data.type !== 'BONZAI_CAPTURE_REQUEST') return;
12
+
13
+ var selection = e.data.selection;
14
+
15
+ html2canvas(document.body, {
16
+ x: selection.left + window.scrollX,
17
+ y: selection.top + window.scrollY,
18
+ width: selection.width,
19
+ height: selection.height
20
+ }).then(function(canvas) {
21
+ window.parent.postMessage({
22
+ type: 'BONZAI_SCREENSHOT',
23
+ data: {
24
+ id: String(Date.now()),
25
+ data: canvas.toDataURL('image/png'),
26
+ timestamp: new Date().toISOString()
27
+ }
28
+ }, '*');
29
+ }).catch(function(err) {
30
+ window.parent.postMessage({
31
+ type: 'BONZAI_SCREENSHOT_ERROR',
32
+ error: err.message
33
+ }, '*');
34
+ });
35
+ });
36
+ };
37
+ document.head.appendChild(script);
38
+ })();`);
39
+ }
40
+
41
+ module.exports = bonzaiCaptureClientHandler;
42
+
@@ -1,10 +1,33 @@
1
1
  const { spawn, execSync } = require('child_process');
2
2
  const { ROOT } = require('../config');
3
3
 
4
+ // Token estimation: ~4 characters per token (rough approximation for English text)
5
+ function estimateTokens(text) {
6
+ if (!text) return 0;
7
+ return Math.ceil(text.length / 4);
8
+ }
9
+
10
+ // Cost estimation based on Claude 3.5 Sonnet pricing ($/1M tokens)
11
+ // Input: $3/1M tokens, Output: $15/1M tokens
12
+ function estimateCost(inputTokens, outputTokens) {
13
+ const inputCostPerMillion = 3.0;
14
+ const outputCostPerMillion = 15.0;
15
+ const inputCost = (inputTokens / 1_000_000) * inputCostPerMillion;
16
+ const outputCost = (outputTokens / 1_000_000) * outputCostPerMillion;
17
+ return {
18
+ inputCost: parseFloat(inputCost.toFixed(6)),
19
+ outputCost: parseFloat(outputCost.toFixed(6)),
20
+ totalCost: parseFloat((inputCost + outputCost).toFixed(6))
21
+ };
22
+ }
23
+
4
24
  function promptAgentStreamHandler(req, res) {
5
25
  console.log('🔵 [prompt_agent_stream] Endpoint hit');
6
26
  const { prompt } = req.body;
7
27
  console.log('🔵 [prompt_agent_stream] Received prompt:', prompt ? `${prompt.substring(0, 50)}...` : 'none');
28
+
29
+ // Start execution timer
30
+ const startTime = Date.now();
8
31
 
9
32
  if (!prompt || typeof prompt !== 'string') {
10
33
  console.log('❌ [prompt_agent_stream] Error: prompt required');
@@ -56,8 +79,8 @@ function promptAgentStreamHandler(req, res) {
56
79
  // Ignore
57
80
  }
58
81
 
59
- // Send starting event
60
- sendEvent('start', { beforeCommit });
82
+ // Send starting event with timestamp
83
+ sendEvent('start', { beforeCommit, startTimestamp: startTime });
61
84
 
62
85
  // Set up file change tracking with real-time updates
63
86
  const changedFiles = new Set();
@@ -121,19 +144,34 @@ function promptAgentStreamHandler(req, res) {
121
144
  if (res.destroyed || res.closed) {
122
145
  console.log('⚠️ [prompt_agent_stream] Response already closed, cannot send timeout events');
123
146
  } else {
124
- responseSent = true;
125
- sendEvent('error', {
126
- error: 'Process timeout',
127
- message: `cursor-agent exceeded timeout of ${timeoutMs / 1000} seconds`
128
- });
129
- sendEvent('complete', {
130
- code: -1,
131
- stdout,
132
- stderr,
133
- changedFiles: Array.from(changedFiles),
134
- beforeCommit,
135
- afterCommit: ''
136
- });
147
+ responseSent = true;
148
+
149
+ // Calculate metrics
150
+ const executionTimeMs = Date.now() - startTime;
151
+ const inputTokens = estimateTokens(prompt);
152
+ const outputTokens = estimateTokens(stdout);
153
+ const costEstimate = estimateCost(inputTokens, outputTokens);
154
+
155
+ sendEvent('error', {
156
+ error: 'Process timeout',
157
+ message: `cursor-agent exceeded timeout of ${timeoutMs / 1000} seconds`
158
+ });
159
+ sendEvent('complete', {
160
+ code: -1,
161
+ stdout,
162
+ stderr,
163
+ changedFiles: Array.from(changedFiles),
164
+ beforeCommit,
165
+ afterCommit: '',
166
+ metrics: {
167
+ executionTimeMs,
168
+ executionTimeSec: parseFloat((executionTimeMs / 1000).toFixed(2)),
169
+ inputTokens,
170
+ outputTokens,
171
+ totalTokens: inputTokens + outputTokens,
172
+ costEstimate
173
+ }
174
+ });
137
175
  // Send stop event after complete
138
176
  sendEvent('stop', {});
139
177
  res.end();
@@ -210,6 +248,19 @@ function promptAgentStreamHandler(req, res) {
210
248
  if (res.destroyed || res.closed) {
211
249
  console.log('⚠️ [prompt_agent_stream] Response already closed, cannot send complete event');
212
250
  } else {
251
+ // Calculate metrics
252
+ const executionTimeMs = Date.now() - startTime;
253
+ const inputTokens = estimateTokens(prompt);
254
+ const outputTokens = estimateTokens(stdout);
255
+ const costEstimate = estimateCost(inputTokens, outputTokens);
256
+
257
+ console.log('📊 [prompt_agent_stream] Metrics:', {
258
+ executionTimeMs,
259
+ inputTokens,
260
+ outputTokens,
261
+ costEstimate
262
+ });
263
+
213
264
  // Send events and only set flag after successful send
214
265
  sendEvent('complete', {
215
266
  code,
@@ -217,7 +268,15 @@ function promptAgentStreamHandler(req, res) {
217
268
  stderr,
218
269
  changedFiles: Array.from(changedFiles),
219
270
  beforeCommit,
220
- afterCommit
271
+ afterCommit,
272
+ metrics: {
273
+ executionTimeMs,
274
+ executionTimeSec: parseFloat((executionTimeMs / 1000).toFixed(2)),
275
+ inputTokens,
276
+ outputTokens,
277
+ totalTokens: inputTokens + outputTokens,
278
+ costEstimate
279
+ }
221
280
  });
222
281
  // Send stop event after complete
223
282
  sendEvent('stop', {});
@@ -0,0 +1,55 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+ const { ROOT } = require('../config');
4
+
5
+ // Serves static files with HTML injection for bonzai capture
6
+ function serveHandler(req, res) {
7
+ try {
8
+ const requestedPath = req.query.path || 'index.html';
9
+ const filePath = path.join(ROOT, requestedPath);
10
+
11
+ // Security check: ensure path is within ROOT
12
+ if (!filePath.startsWith(ROOT)) {
13
+ return res.status(400).send('Invalid path');
14
+ }
15
+
16
+ // Check if file exists
17
+ if (!fs.existsSync(filePath)) {
18
+ return res.status(404).send('File not found');
19
+ }
20
+
21
+ // Read file content
22
+ let content = fs.readFileSync(filePath, 'utf8');
23
+
24
+ // Determine content type based on extension
25
+ const ext = path.extname(filePath).toLowerCase();
26
+ const contentTypes = {
27
+ '.html': 'text/html',
28
+ '.htm': 'text/html',
29
+ '.css': 'text/css',
30
+ '.js': 'application/javascript',
31
+ '.json': 'application/json',
32
+ '.png': 'image/png',
33
+ '.jpg': 'image/jpeg',
34
+ '.jpeg': 'image/jpeg',
35
+ '.gif': 'image/gif',
36
+ '.svg': 'image/svg+xml',
37
+ '.ico': 'image/x-icon'
38
+ };
39
+
40
+ const contentType = contentTypes[ext] || 'text/plain';
41
+
42
+ // Inject bonzai capture script for HTML files
43
+ if (ext === '.html' || ext === '.htm') {
44
+ content = content.replace('</body>', '<script src="/bonzai-capture-client.js"></script></body>');
45
+ }
46
+
47
+ res.set('Content-Type', contentType);
48
+ res.send(content);
49
+ } catch (e) {
50
+ res.status(500).send(e.message);
51
+ }
52
+ }
53
+
54
+ module.exports = serveHandler;
55
+
@@ -20,6 +20,8 @@ const revertJobHandler = require('./handlers/revert_job');
20
20
  const shutdownHandler = require('./handlers/shutdown');
21
21
  const scanCodeQualityHandler = require('./handlers/scan_code_quality');
22
22
  const scanStandardsHandler = require('./handlers/scan_standards');
23
+ const bonzaiCaptureClientHandler = require('./handlers/bonzai-capture-client');
24
+ const serveHandler = require('./handlers/serve');
23
25
 
24
26
  const app = express();
25
27
 
@@ -43,6 +45,8 @@ app.post('/revert_job', revertJobHandler);
43
45
  app.post('/shutdown', shutdownHandler);
44
46
  app.post('/scan_code_quality', scanCodeQualityHandler);
45
47
  app.post('/scan_standards', scanStandardsHandler);
48
+ app.get('/bonzai-capture-client.js', bonzaiCaptureClientHandler);
49
+ app.get('/serve', serveHandler);
46
50
 
47
51
  const port = 3001;
48
52
  app.listen(port, () => {