@axiom-lattice/core 2.1.3 → 2.1.4

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/dist/index.mjs CHANGED
@@ -1478,6 +1478,407 @@ import {
1478
1478
  } from "langchain";
1479
1479
  import { Command as Command2, getCurrentTaskInput as getCurrentTaskInput2 } from "@langchain/langgraph";
1480
1480
  import { HumanMessage } from "@langchain/core/messages";
1481
+
1482
+ // src/agent_worker/agent_worker_graph.ts
1483
+ import {
1484
+ StateGraph,
1485
+ END as END2,
1486
+ interrupt as interrupt2,
1487
+ Annotation
1488
+ } from "@langchain/langgraph";
1489
+
1490
+ // src/constants/agent_task_types.ts
1491
+ var AGENT_TASK_EVENT = "agent:execute";
1492
+
1493
+ // src/services/event_bus.ts
1494
+ import { EventEmitter } from "events";
1495
+
1496
+ // src/queue_lattice/QueueLatticeManager.ts
1497
+ import {
1498
+ QueueType
1499
+ } from "@axiom-lattice/protocols";
1500
+
1501
+ // src/queue_lattice/MemoryQueueClient.ts
1502
+ var queues = /* @__PURE__ */ new Map();
1503
+ var ensureQueue = (queueName) => {
1504
+ if (!queues.has(queueName)) {
1505
+ queues.set(queueName, []);
1506
+ }
1507
+ };
1508
+ var MemoryQueueClient = class {
1509
+ constructor(queueName = "tasks") {
1510
+ this.queueName = queueName;
1511
+ ensureQueue(this.queueName);
1512
+ }
1513
+ /**
1514
+ * Enqueue message (equivalent to lPush - push to left/beginning)
1515
+ */
1516
+ async push(item) {
1517
+ try {
1518
+ ensureQueue(this.queueName);
1519
+ const queue = queues.get(this.queueName);
1520
+ queue.unshift(item);
1521
+ const result = queue.length;
1522
+ console.log("lPush (memory)", result);
1523
+ return { data: result, error: null };
1524
+ } catch (error) {
1525
+ console.error(error);
1526
+ return { data: null, error };
1527
+ }
1528
+ }
1529
+ /**
1530
+ * Dequeue message (equivalent to rPop - pop from right/end)
1531
+ */
1532
+ async pop() {
1533
+ try {
1534
+ ensureQueue(this.queueName);
1535
+ const queue = queues.get(this.queueName);
1536
+ const message = queue.pop();
1537
+ if (message) {
1538
+ return { data: message, error: null };
1539
+ }
1540
+ return { data: null, error: null };
1541
+ } catch (error) {
1542
+ console.error(error);
1543
+ return { data: null, error };
1544
+ }
1545
+ }
1546
+ /**
1547
+ * Create queue (initialize in memory)
1548
+ */
1549
+ async createQueue() {
1550
+ try {
1551
+ ensureQueue(this.queueName);
1552
+ const queue = queues.get(this.queueName);
1553
+ const exists = queue !== void 0;
1554
+ return { success: true, queue_name: this.queueName };
1555
+ } catch (error) {
1556
+ console.error(error);
1557
+ return { success: false, error };
1558
+ }
1559
+ }
1560
+ };
1561
+
1562
+ // src/queue_lattice/QueueLatticeManager.ts
1563
+ var QueueLatticeManager = class _QueueLatticeManager extends BaseLatticeManager {
1564
+ /**
1565
+ * Get QueueLatticeManager singleton instance
1566
+ */
1567
+ static getInstance() {
1568
+ if (!_QueueLatticeManager._instance) {
1569
+ _QueueLatticeManager._instance = new _QueueLatticeManager();
1570
+ }
1571
+ return _QueueLatticeManager._instance;
1572
+ }
1573
+ /**
1574
+ * Get Lattice type prefix
1575
+ */
1576
+ getLatticeType() {
1577
+ return "queues";
1578
+ }
1579
+ /**
1580
+ * Register queue Lattice
1581
+ * @param key Lattice key name
1582
+ * @param config Queue configuration
1583
+ * @param client Optional queue client. If not provided, will create based on config type.
1584
+ * For REDIS type, client must be provided (use @axiom-lattice/queue-redis).
1585
+ */
1586
+ registerLattice(key, config, client) {
1587
+ let queueClient;
1588
+ if (client) {
1589
+ queueClient = client;
1590
+ } else {
1591
+ const queueName = config.queueName || process.env.QUEUE_NAME || "tasks";
1592
+ if (config.type === QueueType.REDIS) {
1593
+ throw new Error(
1594
+ `Redis queue client must be provided. Please install @axiom-lattice/queue-redis and pass the client to registerLattice. Example: registerLattice(key, config, new RedisQueueClient(queueName, options))`
1595
+ );
1596
+ } else {
1597
+ queueClient = new MemoryQueueClient(queueName);
1598
+ }
1599
+ }
1600
+ const queueLattice = {
1601
+ key,
1602
+ config,
1603
+ client: queueClient,
1604
+ push: async (item) => {
1605
+ return queueClient.push(item);
1606
+ },
1607
+ pop: async () => {
1608
+ return queueClient.pop();
1609
+ },
1610
+ createQueue: queueClient.createQueue ? async () => {
1611
+ return queueClient.createQueue();
1612
+ } : void 0
1613
+ };
1614
+ this.register(key, queueLattice);
1615
+ }
1616
+ /**
1617
+ * Get QueueLattice
1618
+ * @param key Lattice key name
1619
+ */
1620
+ getQueueLattice(key) {
1621
+ const queueLattice = this.get(key);
1622
+ if (!queueLattice) {
1623
+ throw new Error(`QueueLattice ${key} not found`);
1624
+ }
1625
+ return queueLattice;
1626
+ }
1627
+ /**
1628
+ * Get all Lattices
1629
+ */
1630
+ getAllLattices() {
1631
+ return this.getAll();
1632
+ }
1633
+ /**
1634
+ * Check if Lattice exists
1635
+ * @param key Lattice key name
1636
+ */
1637
+ hasLattice(key) {
1638
+ return this.has(key);
1639
+ }
1640
+ /**
1641
+ * Remove Lattice
1642
+ * @param key Lattice key name
1643
+ */
1644
+ removeLattice(key) {
1645
+ return this.remove(key);
1646
+ }
1647
+ /**
1648
+ * Clear all Lattices
1649
+ */
1650
+ clearLattices() {
1651
+ this.clear();
1652
+ }
1653
+ /**
1654
+ * Get Lattice count
1655
+ */
1656
+ getLatticeCount() {
1657
+ return this.count();
1658
+ }
1659
+ /**
1660
+ * Get Lattice key list
1661
+ */
1662
+ getLatticeKeys() {
1663
+ return this.keys();
1664
+ }
1665
+ };
1666
+ var queueLatticeManager = QueueLatticeManager.getInstance();
1667
+ var registerQueueLattice = (key, config, client) => queueLatticeManager.registerLattice(key, config, client);
1668
+ var getQueueLattice = (key) => queueLatticeManager.getQueueLattice(key);
1669
+
1670
+ // src/services/event_bus.ts
1671
+ var EventBus = class {
1672
+ constructor() {
1673
+ this.defaultQueueKey = "default";
1674
+ this.emitter = new EventEmitter();
1675
+ this.emitter.setMaxListeners(100);
1676
+ }
1677
+ /**
1678
+ * Set the default queue key for queue operations
1679
+ * @param queueKey Queue key name
1680
+ */
1681
+ setDefaultQueueKey(queueKey) {
1682
+ this.defaultQueueKey = queueKey;
1683
+ }
1684
+ /**
1685
+ * Publish event
1686
+ * @param eventName Event name
1687
+ * @param data Event data
1688
+ * @param useQueue Whether to use queue for publishing
1689
+ */
1690
+ publish(eventName, data, useQueue = false) {
1691
+ if (useQueue) {
1692
+ if (queueLatticeManager.hasLattice(this.defaultQueueKey)) {
1693
+ const queue = queueLatticeManager.getQueueLattice(this.defaultQueueKey);
1694
+ queue.push(data).catch((error) => {
1695
+ console.error("Failed to push event to queue:", error);
1696
+ });
1697
+ } else {
1698
+ console.warn(
1699
+ `Queue "${this.defaultQueueKey}" not found. Event will not be queued.`
1700
+ );
1701
+ }
1702
+ } else {
1703
+ this.emitter.emit(eventName, data);
1704
+ }
1705
+ }
1706
+ /**
1707
+ * Subscribe to event
1708
+ * @param eventName Event name
1709
+ * @param callback Callback function
1710
+ */
1711
+ subscribe(eventName, callback) {
1712
+ this.emitter.on(eventName, callback);
1713
+ }
1714
+ /**
1715
+ * Unsubscribe from event
1716
+ * @param eventName Event name
1717
+ * @param callback Callback function
1718
+ */
1719
+ unsubscribe(eventName, callback) {
1720
+ this.emitter.off(eventName, callback);
1721
+ }
1722
+ /**
1723
+ * Subscribe to event once
1724
+ * @param eventName Event name
1725
+ * @param callback Callback function
1726
+ */
1727
+ subscribeOnce(eventName, callback) {
1728
+ this.emitter.once(eventName, callback);
1729
+ }
1730
+ };
1731
+ var eventBus = new EventBus();
1732
+ var event_bus_default = eventBus;
1733
+
1734
+ // src/services/AgentManager.ts
1735
+ var AgentManager = class _AgentManager {
1736
+ constructor() {
1737
+ this.agents = [];
1738
+ }
1739
+ static getInstance() {
1740
+ if (!_AgentManager.instance) {
1741
+ _AgentManager.instance = new _AgentManager();
1742
+ }
1743
+ return _AgentManager.instance;
1744
+ }
1745
+ callAgentInQueue(queue, return_agent_state) {
1746
+ return new Promise((resolve, reject) => {
1747
+ const callback_event = `${queue.assistant_id}::${queue.thread_id}`;
1748
+ if (return_agent_state) {
1749
+ event_bus_default.subscribeOnce(callback_event, (data) => {
1750
+ if (data.success) {
1751
+ console.log("AgentManager callAgentInQueue success", data);
1752
+ resolve(data.state);
1753
+ } else {
1754
+ console.log("AgentManager callAgentInQueue error", data);
1755
+ reject(data.error);
1756
+ }
1757
+ });
1758
+ }
1759
+ try {
1760
+ event_bus_default.publish(
1761
+ AGENT_TASK_EVENT,
1762
+ {
1763
+ ...queue,
1764
+ callback_event
1765
+ },
1766
+ true
1767
+ );
1768
+ !return_agent_state && resolve({ callback_event_id: callback_event, success: true });
1769
+ } catch (error) {
1770
+ !return_agent_state && reject({
1771
+ callback_event_id: callback_event,
1772
+ success: false,
1773
+ error
1774
+ });
1775
+ }
1776
+ });
1777
+ }
1778
+ };
1779
+
1780
+ // src/agent_worker/agent_worker_graph.ts
1781
+ var AgentWorkerState = Annotation.Root({
1782
+ // Input parameters
1783
+ assistant_id: Annotation({
1784
+ reducer: (x, y) => y ?? x,
1785
+ default: () => ""
1786
+ }),
1787
+ thread_id: Annotation({
1788
+ reducer: (x, y) => y ?? x,
1789
+ default: () => ""
1790
+ }),
1791
+ input: Annotation({
1792
+ reducer: (x, y) => y ?? x,
1793
+ default: () => ({})
1794
+ }),
1795
+ command2: Annotation({
1796
+ reducer: (x, y) => y ?? x,
1797
+ default: () => void 0
1798
+ }),
1799
+ // Execution result
1800
+ result: Annotation({
1801
+ reducer: (x, y) => y ?? x,
1802
+ default: () => void 0
1803
+ }),
1804
+ // Interrupt data
1805
+ interruptValue: Annotation({
1806
+ reducer: (x, y) => y ?? x,
1807
+ default: () => void 0
1808
+ }),
1809
+ // Final state to return
1810
+ finalState: Annotation({
1811
+ reducer: (x, y) => y ?? x,
1812
+ default: () => void 0
1813
+ })
1814
+ });
1815
+ async function executeNode(state) {
1816
+ const { assistant_id, thread_id, input, command2 } = state;
1817
+ const callParams = {
1818
+ assistant_id,
1819
+ thread_id,
1820
+ input,
1821
+ command: command2
1822
+ };
1823
+ const result = await AgentManager.getInstance().callAgentInQueue(
1824
+ callParams,
1825
+ true
1826
+ );
1827
+ return {
1828
+ result
1829
+ };
1830
+ }
1831
+ async function interruptNode(state) {
1832
+ const { result } = state;
1833
+ const interruptValue = result?.tasks?.[0]?.interrupts?.[0]?.value;
1834
+ if (!interruptValue) {
1835
+ throw new Error(
1836
+ "Interrupt node called but no interrupt value found in result"
1837
+ );
1838
+ }
1839
+ const response = await interrupt2(interruptValue);
1840
+ return {
1841
+ command2: {
1842
+ resume: response
1843
+ }
1844
+ };
1845
+ }
1846
+ function shouldInterrupt(state) {
1847
+ const { result } = state;
1848
+ if (result?.tasks && Array.isArray(result.tasks) && result.tasks.length > 0) {
1849
+ if (result.tasks[0]?.interrupts && result.tasks[0].interrupts.length > 0) {
1850
+ return "interrupt";
1851
+ }
1852
+ }
1853
+ return "end";
1854
+ }
1855
+ function endNode(state) {
1856
+ const finalState = state.result;
1857
+ return {
1858
+ finalState,
1859
+ // Clear intermediate states
1860
+ result: void 0,
1861
+ interruptValue: void 0,
1862
+ command2: void 0
1863
+ };
1864
+ }
1865
+ function createAgentWorkerGraph() {
1866
+ const workflow = new StateGraph(AgentWorkerState);
1867
+ workflow.addNode("execute", executeNode);
1868
+ workflow.addNode("interrupt", interruptNode);
1869
+ workflow.addNode("end", endNode);
1870
+ workflow.setEntryPoint("execute");
1871
+ workflow.addConditionalEdges("execute", shouldInterrupt, {
1872
+ interrupt: "interrupt",
1873
+ end: "end"
1874
+ });
1875
+ workflow.addEdge("interrupt", "execute");
1876
+ workflow.addEdge("end", END2);
1877
+ return workflow.compile();
1878
+ }
1879
+ var agentWorkerGraph = createAgentWorkerGraph();
1880
+
1881
+ // src/deep_agent_new/middleware/subagents.ts
1481
1882
  var DEFAULT_SUBAGENT_PROMPT = "In order to complete the objective that the user asks of you, you have access to a number of standard tools.";
1482
1883
  var EXCLUDED_STATE_KEYS = ["messages", "todos", "jumpTo"];
1483
1884
  var DEFAULT_GENERAL_PURPOSE_DESCRIPTION = "General-purpose agent for researching complex questions, searching for files and content, and executing multi-step tasks. When you are searching for a keyword or file and are not confident that you will find the right match in the first few tries use this agent to perform the search for you. This agent has access to all tools as the main agent.";
@@ -1730,7 +2131,15 @@ function createTaskTool(options) {
1730
2131
  const currentState = getCurrentTaskInput2();
1731
2132
  const subagentState = filterStateForSubagent(currentState);
1732
2133
  subagentState.messages = [new HumanMessage({ content: description })];
1733
- const result = await subagent.invoke(subagentState, config);
2134
+ const subagent_thread_id = config.configurable?.thread_id + "_" + subagent_type + "_" + config.toolCall.id;
2135
+ console.log(subagent_thread_id);
2136
+ const workerResult = await agentWorkerGraph.invoke({
2137
+ assistant_id: subagent_type,
2138
+ thread_id: subagent_thread_id,
2139
+ input: { ...subagentState, message: description }
2140
+ });
2141
+ const result = workerResult.finalState?.values;
2142
+ console.log("workerResult", workerResult);
1734
2143
  if (!config.toolCall?.id) {
1735
2144
  throw new Error("Tool call ID is required for subagent invocation");
1736
2145
  }
@@ -2840,20 +3249,26 @@ var hasChunkBuffer = (key) => ChunkBufferLatticeManager.getInstance().has(key);
2840
3249
  // src/index.ts
2841
3250
  import * as Protocols from "@axiom-lattice/protocols";
2842
3251
  export {
3252
+ AGENT_TASK_EVENT,
2843
3253
  AgentConfig,
2844
3254
  AgentLatticeManager,
3255
+ AgentManager,
2845
3256
  AgentType,
2846
3257
  ChunkBuffer,
2847
3258
  ChunkBufferLatticeManager,
2848
3259
  GraphBuildOptions,
2849
3260
  InMemoryChunkBuffer,
2850
3261
  MemoryLatticeManager,
3262
+ MemoryQueueClient,
2851
3263
  MemoryType,
2852
3264
  ModelLatticeManager,
2853
3265
  Protocols,
3266
+ QueueLatticeManager,
2854
3267
  ThreadStatus,
2855
3268
  ToolLatticeManager,
2856
3269
  agentLatticeManager,
3270
+ eventBus,
3271
+ event_bus_default as eventBusDefault,
2857
3272
  getAgentClient,
2858
3273
  getAgentConfig,
2859
3274
  getAgentLattice,
@@ -2862,16 +3277,19 @@ export {
2862
3277
  getCheckpointSaver,
2863
3278
  getChunkBuffer,
2864
3279
  getModelLattice,
3280
+ getQueueLattice,
2865
3281
  getToolClient,
2866
3282
  getToolDefinition,
2867
3283
  getToolLattice,
2868
3284
  hasChunkBuffer,
2869
3285
  modelLatticeManager,
3286
+ queueLatticeManager,
2870
3287
  registerAgentLattice,
2871
3288
  registerAgentLattices,
2872
3289
  registerCheckpointSaver,
2873
3290
  registerChunkBuffer,
2874
3291
  registerModelLattice,
3292
+ registerQueueLattice,
2875
3293
  registerToolLattice,
2876
3294
  toolLatticeManager,
2877
3295
  validateAgentInput,