replicas-engine 0.1.36 → 0.1.37

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/dist/src/index.js +66 -8
  2. package/package.json +1 -1
package/dist/src/index.js CHANGED
@@ -1592,6 +1592,44 @@ import { mkdir as mkdir4, appendFile as appendFile2, rm } from "fs/promises";
1592
1592
  import { homedir as homedir4 } from "os";
1593
1593
  var MAX_INTERRUPT_QUEUE_ITEMS2 = 1e3;
1594
1594
  var MAX_INTERRUPT_QUEUE_CHARS2 = 2e5;
1595
+ var PromptStream = class {
1596
+ queue = [];
1597
+ closed = false;
1598
+ waiters = [];
1599
+ push(message) {
1600
+ if (this.closed) return;
1601
+ const waiter = this.waiters.shift();
1602
+ if (waiter) {
1603
+ waiter({ value: message, done: false });
1604
+ return;
1605
+ }
1606
+ this.queue.push(message);
1607
+ }
1608
+ close() {
1609
+ if (this.closed) return;
1610
+ this.closed = true;
1611
+ for (const waiter of this.waiters) {
1612
+ waiter({ value: void 0, done: true });
1613
+ }
1614
+ this.waiters = [];
1615
+ }
1616
+ [Symbol.asyncIterator]() {
1617
+ return {
1618
+ next: () => {
1619
+ const nextItem = this.queue.shift();
1620
+ if (nextItem) {
1621
+ return Promise.resolve({ value: nextItem, done: false });
1622
+ }
1623
+ if (this.closed) {
1624
+ return Promise.resolve({ value: void 0, done: true });
1625
+ }
1626
+ return new Promise((resolve) => {
1627
+ this.waiters.push(resolve);
1628
+ });
1629
+ }
1630
+ };
1631
+ }
1632
+ };
1595
1633
  var ClaudeManager = class {
1596
1634
  workingDirectory;
1597
1635
  historyFile;
@@ -1600,6 +1638,8 @@ var ClaudeManager = class {
1600
1638
  messageQueue;
1601
1639
  baseSystemPrompt;
1602
1640
  activeQuery = null;
1641
+ activePromptStream = null;
1642
+ pendingInterrupt = false;
1603
1643
  constructor(workingDirectory) {
1604
1644
  if (workingDirectory) {
1605
1645
  this.workingDirectory = workingDirectory;
@@ -1640,10 +1680,14 @@ var ClaudeManager = class {
1640
1680
  maxChars: MAX_INTERRUPT_QUEUE_CHARS2
1641
1681
  });
1642
1682
  const isProcessing = this.messageQueue.isProcessing();
1643
- if (isProcessing && this.activeQuery) {
1644
- try {
1645
- await this.activeQuery.interrupt();
1646
- } catch {
1683
+ if (isProcessing) {
1684
+ this.pendingInterrupt = true;
1685
+ this.activePromptStream?.close();
1686
+ if (this.activeQuery) {
1687
+ try {
1688
+ await this.activeQuery.interrupt();
1689
+ } catch {
1690
+ }
1647
1691
  }
1648
1692
  }
1649
1693
  const linearSessionId = process.env.LINEAR_SESSION_ID;
@@ -1734,9 +1778,9 @@ var ClaudeManager = class {
1734
1778
  session_id: this.sessionId ?? ""
1735
1779
  };
1736
1780
  await this.recordEvent(userMessage);
1737
- const promptIterable = (async function* () {
1738
- yield userMessage;
1739
- })();
1781
+ const promptStream = new PromptStream();
1782
+ promptStream.push(userMessage);
1783
+ this.activePromptStream = promptStream;
1740
1784
  const startHooksInstruction = this.getStartHooksInstruction();
1741
1785
  const parts = [];
1742
1786
  if (this.baseSystemPrompt) {
@@ -1750,7 +1794,7 @@ var ClaudeManager = class {
1750
1794
  }
1751
1795
  const combinedInstructions = parts.length > 0 ? parts.join("\n\n") : void 0;
1752
1796
  const response = query({
1753
- prompt: promptIterable,
1797
+ prompt: promptStream,
1754
1798
  options: {
1755
1799
  resume: this.sessionId || void 0,
1756
1800
  cwd: this.workingDirectory,
@@ -1769,6 +1813,14 @@ var ClaudeManager = class {
1769
1813
  }
1770
1814
  });
1771
1815
  this.activeQuery = response;
1816
+ if (this.pendingInterrupt) {
1817
+ this.pendingInterrupt = false;
1818
+ this.activePromptStream?.close();
1819
+ try {
1820
+ await this.activeQuery.interrupt();
1821
+ } catch {
1822
+ }
1823
+ }
1772
1824
  let latestThoughtEvent = null;
1773
1825
  for await (const msg of response) {
1774
1826
  await this.handleMessage(msg);
@@ -1788,6 +1840,9 @@ var ClaudeManager = class {
1788
1840
  }
1789
1841
  }
1790
1842
  }
1843
+ if (msg.type === "result") {
1844
+ this.activePromptStream?.close();
1845
+ }
1791
1846
  }
1792
1847
  if (linearSessionId && latestThoughtEvent) {
1793
1848
  const responseEvent = linearThoughtToResponse(latestThoughtEvent);
@@ -1796,6 +1851,9 @@ var ClaudeManager = class {
1796
1851
  }
1797
1852
  } finally {
1798
1853
  this.activeQuery = null;
1854
+ this.activePromptStream?.close();
1855
+ this.activePromptStream = null;
1856
+ this.pendingInterrupt = false;
1799
1857
  const status = await getGitStatus(this.workingDirectory);
1800
1858
  const payload = linearSessionId ? { linearSessionId, status } : { status };
1801
1859
  monolithService.sendEvent({ type: "agent_turn_complete", payload }).catch(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replicas-engine",
3
- "version": "0.1.36",
3
+ "version": "0.1.37",
4
4
  "description": "Lightweight API server for Replicas workspaces",
5
5
  "type": "module",
6
6
  "main": "dist/src/index.js",