devbonzai 2.1.4 → 2.1.6

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/templates/receiver.js +101 -31
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devbonzai",
3
- "version": "2.1.4",
3
+ "version": "2.1.6",
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": {
@@ -1525,9 +1525,21 @@ app.post('/prompt_agent_stream', (req, res) => {
1525
1525
  res.setHeader('Access-Control-Allow-Origin', '*');
1526
1526
  res.flushHeaders();
1527
1527
 
1528
- // Helper to send SSE events
1528
+ // Helper to send SSE events with robustness checks
1529
1529
  const sendEvent = (type, data) => {
1530
- res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
1530
+ try {
1531
+ // Check if response is still writable - try to send even if clientDisconnected flag is set
1532
+ // because the response stream might still be open
1533
+ if (res.destroyed || res.closed) {
1534
+ console.log(`⚠️ [prompt_agent_stream] Response already closed, cannot send ${type} event`);
1535
+ return false;
1536
+ }
1537
+ res.write(`data: ${JSON.stringify({ type, ...data })}\n\n`);
1538
+ return true;
1539
+ } catch (e) {
1540
+ console.log(`⚠️ [prompt_agent_stream] Error sending ${type} event:`, e.message);
1541
+ return false;
1542
+ }
1531
1543
  };
1532
1544
 
1533
1545
  // Capture beforeCommit
@@ -1607,21 +1619,40 @@ app.post('/prompt_agent_stream', (req, res) => {
1607
1619
  if (!proc.killed) proc.kill('SIGKILL');
1608
1620
  }, 5000);
1609
1621
 
1622
+ // Always try to send complete event when timeout occurs
1623
+ // Only skip if we've already sent it
1610
1624
  if (!responseSent) {
1611
- responseSent = true;
1612
- sendEvent('error', {
1613
- error: 'Process timeout',
1614
- message: `cursor-agent exceeded timeout of ${timeoutMs / 1000} seconds`
1615
- });
1616
- sendEvent('complete', {
1617
- code: -1,
1618
- stdout,
1619
- stderr,
1620
- changedFiles: Array.from(changedFiles),
1621
- beforeCommit,
1622
- afterCommit: ''
1623
- });
1624
- res.end();
1625
+ try {
1626
+ // Check if response is still writable - try to send even if clientDisconnected flag is set
1627
+ // because the response stream might still be open
1628
+ if (res.destroyed || res.closed) {
1629
+ console.log('⚠️ [prompt_agent_stream] Response already closed, cannot send timeout events');
1630
+ } else {
1631
+ responseSent = true;
1632
+ sendEvent('error', {
1633
+ error: 'Process timeout',
1634
+ message: `cursor-agent exceeded timeout of ${timeoutMs / 1000} seconds`
1635
+ });
1636
+ sendEvent('complete', {
1637
+ code: -1,
1638
+ stdout,
1639
+ stderr,
1640
+ changedFiles: Array.from(changedFiles),
1641
+ beforeCommit,
1642
+ afterCommit: ''
1643
+ });
1644
+ // Send stop event after complete
1645
+ sendEvent('stop', {});
1646
+ res.end();
1647
+ console.log('✅ [prompt_agent_stream] Sent timeout error, complete, and stop events');
1648
+ }
1649
+ } catch (e) {
1650
+ console.log('⚠️ [prompt_agent_stream] Error sending timeout events:', e.message);
1651
+ // Don't set responseSent = true on error, in case we can retry
1652
+ // But realistically, if there's an error, the connection is probably dead
1653
+ }
1654
+ } else {
1655
+ console.log('⚠️ [prompt_agent_stream] Timeout events already sent, skipping');
1625
1656
  }
1626
1657
  }
1627
1658
  }, timeoutMs);
@@ -1638,10 +1669,30 @@ app.post('/prompt_agent_stream', (req, res) => {
1638
1669
  console.log('❌ [prompt_agent_stream] Process error:', error.message);
1639
1670
  clearInterval(pollInterval);
1640
1671
  if (timeoutId) clearTimeout(timeoutId);
1672
+
1673
+ // Always try to send error event when process error occurs
1674
+ // Only skip if we've already sent it
1641
1675
  if (!responseSent) {
1642
- responseSent = true;
1643
- sendEvent('error', { error: error.message });
1644
- res.end();
1676
+ try {
1677
+ // Check if response is still writable - try to send even if clientDisconnected flag is set
1678
+ // because the response stream might still be open
1679
+ if (res.destroyed || res.closed) {
1680
+ console.log('⚠️ [prompt_agent_stream] Response already closed, cannot send error event');
1681
+ } else {
1682
+ responseSent = true;
1683
+ sendEvent('error', { error: error.message });
1684
+ // Send stop event after error
1685
+ sendEvent('stop', {});
1686
+ res.end();
1687
+ console.log('✅ [prompt_agent_stream] Sent error and stop events');
1688
+ }
1689
+ } catch (e) {
1690
+ console.log('⚠️ [prompt_agent_stream] Error sending error event:', e.message);
1691
+ // Don't set responseSent = true on error, in case we can retry
1692
+ // But realistically, if there's an error, the connection is probably dead
1693
+ }
1694
+ } else {
1695
+ console.log('⚠️ [prompt_agent_stream] Error event already sent, skipping');
1645
1696
  }
1646
1697
  });
1647
1698
 
@@ -1657,17 +1708,37 @@ app.post('/prompt_agent_stream', (req, res) => {
1657
1708
  // Ignore
1658
1709
  }
1659
1710
 
1711
+ // Always try to send complete event when process finishes
1712
+ // Check actual response stream state rather than relying on responseSent flag
1713
+ // Only skip if we've already sent it (responseSent flag prevents duplicates)
1660
1714
  if (!responseSent) {
1661
- responseSent = true;
1662
- sendEvent('complete', {
1663
- code,
1664
- stdout,
1665
- stderr,
1666
- changedFiles: Array.from(changedFiles),
1667
- beforeCommit,
1668
- afterCommit
1669
- });
1670
- res.end();
1715
+ try {
1716
+ // Check if response is still writable - check actual stream state
1717
+ if (res.destroyed || res.closed) {
1718
+ console.log('⚠️ [prompt_agent_stream] Response already closed, cannot send complete event');
1719
+ } else {
1720
+ // Send events and only set flag after successful send
1721
+ sendEvent('complete', {
1722
+ code,
1723
+ stdout,
1724
+ stderr,
1725
+ changedFiles: Array.from(changedFiles),
1726
+ beforeCommit,
1727
+ afterCommit
1728
+ });
1729
+ // Send stop event after complete
1730
+ sendEvent('stop', {});
1731
+ res.end();
1732
+ responseSent = true;
1733
+ console.log('✅ [prompt_agent_stream] Sent complete and stop events');
1734
+ }
1735
+ } catch (e) {
1736
+ console.log('⚠️ [prompt_agent_stream] Error sending complete event:', e.message);
1737
+ // Don't set responseSent = true on error, in case we can retry
1738
+ // But realistically, if there's an error, the connection is probably dead
1739
+ }
1740
+ } else {
1741
+ console.log('⚠️ [prompt_agent_stream] Complete event already sent, skipping');
1671
1742
  }
1672
1743
  });
1673
1744
 
@@ -1675,8 +1746,7 @@ app.post('/prompt_agent_stream', (req, res) => {
1675
1746
  req.on('close', () => {
1676
1747
  console.log('🔵 [prompt_agent_stream] Client disconnected (process continues in background)');
1677
1748
  // Don't kill the process - let it complete
1678
- // Just mark that we shouldn't try to send more events
1679
- responseSent = true;
1749
+ // Don't set responseSent here - let proc.on('close') check actual stream state
1680
1750
  });
1681
1751
  });
1682
1752