@axiom-lattice/core 2.1.42 → 2.1.43

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.d.mts CHANGED
@@ -2290,6 +2290,7 @@ interface IMessageQueueStore {
2290
2290
  markProcessing(messageId: string): Promise<void>;
2291
2291
  markCompleted(messageId: string): Promise<void>;
2292
2292
  clearCompletedMessages(threadId: string): Promise<void>;
2293
+ resetProcessingToPending(threadId: string): Promise<number>;
2293
2294
  }
2294
2295
  /**
2295
2296
  * Agent executor function type
@@ -2857,6 +2858,7 @@ declare class InMemoryThreadMessageQueueStore implements IMessageQueueStore {
2857
2858
  markProcessing(messageId: string): Promise<void>;
2858
2859
  markCompleted(messageId: string): Promise<void>;
2859
2860
  clearCompletedMessages(threadId: string): Promise<void>;
2861
+ resetProcessingToPending(threadId: string): Promise<number>;
2860
2862
  }
2861
2863
 
2862
2864
  /**
@@ -5048,9 +5050,9 @@ declare class Agent {
5048
5050
  }>;
5049
5051
  /**
5050
5052
  * Start queue processor if not already running
5051
- * Private method used internally by addMessage
5053
+ * Public method to allow external triggering (e.g., from recovery)
5052
5054
  */
5053
- private startQueueProcessorIfNeeded;
5055
+ startQueueProcessorIfNeeded(): Promise<void>;
5054
5056
  /**
5055
5057
  * Remove pending message
5056
5058
  */
@@ -5067,6 +5069,12 @@ declare class Agent {
5067
5069
  }[]>;
5068
5070
  get_draw_graph(): Promise<string>;
5069
5071
  getRunStatus(): Promise<ThreadStatus>;
5072
+ /**
5073
+ * Resume task processing after server restart
5074
+ * Resets any stuck processing messages to pending and starts queue processing
5075
+ * Note: Does not rely on LangGraph state as it may be stale after crash/restart
5076
+ */
5077
+ resumeTask(): Promise<void>;
5070
5078
  /**
5071
5079
  * Abort the current agent execution
5072
5080
  * This will cancel any ongoing invoke or stream operations
@@ -5129,6 +5137,20 @@ declare class AgentInstanceManager {
5129
5137
  * Clear all agent instances (use with caution)
5130
5138
  */
5131
5139
  clearAll(): void;
5140
+ /**
5141
+ * Restore agent instances for threads with pending messages after server restart
5142
+ * Queries the message queue store for threads with pending/processing messages
5143
+ * and recreates agent instances to resume processing
5144
+ */
5145
+ restore(): Promise<{
5146
+ restored: number;
5147
+ errors: number;
5148
+ }>;
5149
+ /**
5150
+ * Restore a single thread
5151
+ * Delegates actual recovery logic to Agent.resumeTask()
5152
+ */
5153
+ private restoreThread;
5132
5154
  }
5133
5155
  declare const agentInstanceManager: AgentInstanceManager;
5134
5156
 
package/dist/index.d.ts CHANGED
@@ -2290,6 +2290,7 @@ interface IMessageQueueStore {
2290
2290
  markProcessing(messageId: string): Promise<void>;
2291
2291
  markCompleted(messageId: string): Promise<void>;
2292
2292
  clearCompletedMessages(threadId: string): Promise<void>;
2293
+ resetProcessingToPending(threadId: string): Promise<number>;
2293
2294
  }
2294
2295
  /**
2295
2296
  * Agent executor function type
@@ -2857,6 +2858,7 @@ declare class InMemoryThreadMessageQueueStore implements IMessageQueueStore {
2857
2858
  markProcessing(messageId: string): Promise<void>;
2858
2859
  markCompleted(messageId: string): Promise<void>;
2859
2860
  clearCompletedMessages(threadId: string): Promise<void>;
2861
+ resetProcessingToPending(threadId: string): Promise<number>;
2860
2862
  }
2861
2863
 
2862
2864
  /**
@@ -5048,9 +5050,9 @@ declare class Agent {
5048
5050
  }>;
5049
5051
  /**
5050
5052
  * Start queue processor if not already running
5051
- * Private method used internally by addMessage
5053
+ * Public method to allow external triggering (e.g., from recovery)
5052
5054
  */
5053
- private startQueueProcessorIfNeeded;
5055
+ startQueueProcessorIfNeeded(): Promise<void>;
5054
5056
  /**
5055
5057
  * Remove pending message
5056
5058
  */
@@ -5067,6 +5069,12 @@ declare class Agent {
5067
5069
  }[]>;
5068
5070
  get_draw_graph(): Promise<string>;
5069
5071
  getRunStatus(): Promise<ThreadStatus>;
5072
+ /**
5073
+ * Resume task processing after server restart
5074
+ * Resets any stuck processing messages to pending and starts queue processing
5075
+ * Note: Does not rely on LangGraph state as it may be stale after crash/restart
5076
+ */
5077
+ resumeTask(): Promise<void>;
5070
5078
  /**
5071
5079
  * Abort the current agent execution
5072
5080
  * This will cancel any ongoing invoke or stream operations
@@ -5129,6 +5137,20 @@ declare class AgentInstanceManager {
5129
5137
  * Clear all agent instances (use with caution)
5130
5138
  */
5131
5139
  clearAll(): void;
5140
+ /**
5141
+ * Restore agent instances for threads with pending messages after server restart
5142
+ * Queries the message queue store for threads with pending/processing messages
5143
+ * and recreates agent instances to resume processing
5144
+ */
5145
+ restore(): Promise<{
5146
+ restored: number;
5147
+ errors: number;
5148
+ }>;
5149
+ /**
5150
+ * Restore a single thread
5151
+ * Delegates actual recovery logic to Agent.resumeTask()
5152
+ */
5153
+ private restoreThread;
5132
5154
  }
5133
5155
  declare const agentInstanceManager: AgentInstanceManager;
5134
5156
 
package/dist/index.js CHANGED
@@ -6654,36 +6654,35 @@ var InMemoryThreadMessageQueueStore = class {
6654
6654
  }
6655
6655
  async addMessageAtHead(threadId, content, type = "system") {
6656
6656
  const threadMessages = this.getMessagesForThread(threadId);
6657
- threadMessages.forEach((msg) => {
6658
- msg.sequence += 1;
6659
- });
6660
6657
  let tenantId = "default";
6661
6658
  let assistantId = "";
6662
6659
  if (threadMessages.length > 0) {
6663
6660
  tenantId = threadMessages[0].tenantId || "default";
6664
6661
  assistantId = threadMessages[0].assistantId || "";
6665
6662
  }
6663
+ const nextSequence = threadMessages.length;
6666
6664
  const message = {
6667
6665
  id: this.generateId(),
6668
6666
  content,
6669
6667
  type,
6670
- sequence: 0,
6668
+ sequence: nextSequence,
6671
6669
  createdAt: /* @__PURE__ */ new Date(),
6672
6670
  status: "pending",
6673
6671
  tenantId,
6674
6672
  assistantId,
6675
- priority: 0
6673
+ priority: 100
6674
+ // High priority for head messages (STEER/Command)
6676
6675
  };
6677
- threadMessages.unshift(message);
6676
+ threadMessages.push(message);
6678
6677
  return message;
6679
6678
  }
6680
6679
  async getPendingMessages(threadId) {
6681
6680
  const threadMessages = this.getMessagesForThread(threadId);
6682
- return threadMessages.filter((msg) => msg.status === "pending" || !msg.status).sort((a, b) => a.sequence - b.sequence).map(({ status, tenantId, assistantId, ...message }) => message);
6681
+ return threadMessages.filter((msg) => msg.status === "pending" || !msg.status).sort((a, b) => (b.priority || 0) - (a.priority || 0) || a.sequence - b.sequence).map(({ status, tenantId, assistantId, ...message }) => message);
6683
6682
  }
6684
6683
  async getProcessingMessages(threadId) {
6685
6684
  const threadMessages = this.getMessagesForThread(threadId);
6686
- return threadMessages.filter((msg) => msg.status === "processing").sort((a, b) => a.sequence - b.sequence).map(({ status, tenantId, assistantId, ...message }) => message);
6685
+ return threadMessages.filter((msg) => msg.status === "processing").sort((a, b) => (b.priority || 0) - (a.priority || 0) || a.sequence - b.sequence).map(({ status, tenantId, assistantId, ...message }) => message);
6687
6686
  }
6688
6687
  async getQueueSize(threadId) {
6689
6688
  const pending = await this.getPendingMessages(threadId);
@@ -6692,11 +6691,11 @@ var InMemoryThreadMessageQueueStore = class {
6692
6691
  async getThreadsWithPendingMessages() {
6693
6692
  const result = [];
6694
6693
  for (const [threadId, messages] of this.messages.entries()) {
6695
- const pendingMessages = messages.filter(
6696
- (msg) => msg.status === "pending" || !msg.status
6694
+ const pendingOrProcessingMessages = messages.filter(
6695
+ (msg) => msg.status === "pending" || msg.status === "processing" || !msg.status
6697
6696
  );
6698
- if (pendingMessages.length > 0) {
6699
- const firstMessage = pendingMessages[0];
6697
+ if (pendingOrProcessingMessages.length > 0) {
6698
+ const firstMessage = pendingOrProcessingMessages[0];
6700
6699
  result.push({
6701
6700
  tenantId: firstMessage.tenantId || "default",
6702
6701
  assistantId: firstMessage.assistantId || "",
@@ -6746,6 +6745,20 @@ var InMemoryThreadMessageQueueStore = class {
6746
6745
  this.messages.set(threadId, filtered);
6747
6746
  }
6748
6747
  }
6748
+ async resetProcessingToPending(threadId) {
6749
+ const messages = this.messages.get(threadId);
6750
+ if (!messages) {
6751
+ return 0;
6752
+ }
6753
+ let count = 0;
6754
+ for (const msg of messages) {
6755
+ if (msg.status === "processing") {
6756
+ msg.status = "pending";
6757
+ count++;
6758
+ }
6759
+ }
6760
+ return count;
6761
+ }
6749
6762
  };
6750
6763
 
6751
6764
  // src/store_lattice/StoreLatticeManager.ts
@@ -10760,6 +10773,9 @@ function createTaskTool(options) {
10760
10773
  }
10761
10774
  return returnCommandWithStateUpdate(result, config.toolCall.id);
10762
10775
  } catch (error) {
10776
+ if (error instanceof import_langgraph7.GraphInterrupt) {
10777
+ throw error;
10778
+ }
10763
10779
  return new import_langgraph7.Command({
10764
10780
  update: {
10765
10781
  messages: [
@@ -14105,7 +14121,7 @@ var AgentParamsBuilder = class {
14105
14121
  * @param options build options
14106
14122
  * @returns Agent build parameters
14107
14123
  */
14108
- buildParams(agentLattice, options) {
14124
+ async buildParams(agentLattice, options) {
14109
14125
  const skills = (0, import_protocols4.isDeepAgentConfig)(agentLattice.config) ? agentLattice.config.skillCategories : void 0;
14110
14126
  const toolKeys = options?.overrideTools || (0, import_protocols4.getToolsFromConfig)(agentLattice.config);
14111
14127
  const tools = toolKeys.map((toolKey) => {
@@ -14125,8 +14141,8 @@ var AgentParamsBuilder = class {
14125
14141
  throw new Error(`Model "${modelKey}" does not exist`);
14126
14142
  }
14127
14143
  const subAgentKeys = (0, import_protocols4.getSubAgentsFromConfig)(agentLattice.config);
14128
- const subAgents = subAgentKeys.map((agentKey) => {
14129
- const subAgentLattice = this.getAgentLatticeFunc(agentKey);
14144
+ const subAgents = await Promise.all(subAgentKeys.map(async (agentKey) => {
14145
+ const subAgentLattice = await this.getAgentLatticeFunc(agentKey);
14130
14146
  if (!subAgentLattice) {
14131
14147
  throw new Error(`SubAgent "${agentKey}" does not exist`);
14132
14148
  }
@@ -14135,7 +14151,7 @@ var AgentParamsBuilder = class {
14135
14151
  config: subAgentLattice.config,
14136
14152
  client: subAgentLattice.client
14137
14153
  };
14138
- });
14154
+ }));
14139
14155
  let internalSubAgents = [];
14140
14156
  if ((0, import_protocols4.isDeepAgentConfig)(agentLattice.config)) {
14141
14157
  internalSubAgents = agentLattice.config.internalSubAgents?.map((i) => ({
@@ -14213,7 +14229,7 @@ var AgentLatticeManager = class _AgentLatticeManager extends BaseLatticeManager
14213
14229
  }
14214
14230
  const config = assistantToConfig(assistant);
14215
14231
  const agentLattice = {
14216
- config,
14232
+ config: { ...config, tenantId },
14217
14233
  client: void 0
14218
14234
  };
14219
14235
  return agentLattice;
@@ -14520,13 +14536,13 @@ var AgentLatticeManager = class _AgentLatticeManager extends BaseLatticeManager
14520
14536
  * @param options 构建选项
14521
14537
  * @returns 返回Agent构建参数
14522
14538
  */
14523
- buildAgentParams(agentLattice, options) {
14539
+ async buildAgentParams(agentLattice, options) {
14524
14540
  const tenantId = agentLattice.config.tenantId || "default";
14525
- const paramsBuilder = new AgentParamsBuilder((key) => {
14526
- this.initializeClient(tenantId, key);
14541
+ const paramsBuilder = new AgentParamsBuilder(async (key) => {
14542
+ await this.initializeClientAsync(tenantId, key);
14527
14543
  return this.getAgentLatticeWithTenant(tenantId, key);
14528
14544
  });
14529
- const params = paramsBuilder.buildParams(agentLattice, options);
14545
+ const params = await paramsBuilder.buildParams(agentLattice, options);
14530
14546
  return {
14531
14547
  ...params,
14532
14548
  tenantId
@@ -14547,7 +14563,7 @@ var AgentLatticeManager = class _AgentLatticeManager extends BaseLatticeManager
14547
14563
  };
14548
14564
  const factory = AgentGraphBuilderFactory.getInstance();
14549
14565
  const builder = factory.getBuilder(resolvedConfig.type);
14550
- const params = this.buildAgentParams(resolvedLattice, options);
14566
+ const params = await this.buildAgentParams(resolvedLattice, options);
14551
14567
  return await builder.build(resolvedLattice, params);
14552
14568
  }
14553
14569
  /**
@@ -16962,7 +16978,7 @@ var Agent = class {
16962
16978
  command,
16963
16979
  custom_run_config
16964
16980
  }, signal) => {
16965
- const runnable_agent = await getAgentClient(this.tenant_id, this.assistant_id);
16981
+ const runnable_agent = await getAgentClientAsync(this.tenant_id, this.assistant_id);
16966
16982
  const agentLattice = agentLatticeManager.getAgentLatticeWithTenant(this.tenant_id, this.assistant_id);
16967
16983
  const { messages, ...rest } = input;
16968
16984
  const lifecycleManager = this;
@@ -17099,10 +17115,12 @@ var Agent = class {
17099
17115
  const input = {
17100
17116
  messages: [new import_langchain61.HumanMessage({ id: p.content.id, content: p.content.message })]
17101
17117
  };
17118
+ const queueMessageData = p.content.queueMessage || {};
17102
17119
  try {
17103
17120
  await this.agentStreamExecutor({
17104
17121
  input,
17105
- command: p.content.command
17122
+ command: queueMessageData.command || p.content.command,
17123
+ custom_run_config: queueMessageData.custom_run_config
17106
17124
  }, signal);
17107
17125
  await this.queueStore?.markCompleted(p.id);
17108
17126
  const runStatus = await this.getRunStatus();
@@ -17166,8 +17184,12 @@ var Agent = class {
17166
17184
  queueMode: "collect" /* COLLECT */
17167
17185
  });
17168
17186
  });
17187
+ const firstQueueMessage = remainingPendings[0]?.content?.queueMessage || {};
17169
17188
  try {
17170
- await this.agentStreamExecutor({ input: { messages: userMessages } }, signal);
17189
+ await this.agentStreamExecutor({
17190
+ input: { messages: userMessages },
17191
+ custom_run_config: firstQueueMessage.custom_run_config
17192
+ }, signal);
17171
17193
  const runStatus = await this.getRunStatus();
17172
17194
  const state = await this.getCurrentState();
17173
17195
  for (const p of remainingPendings) {
@@ -17229,8 +17251,12 @@ var Agent = class {
17229
17251
  timestamp: /* @__PURE__ */ new Date(),
17230
17252
  queueMode: "followup" /* FOLLOWUP */
17231
17253
  });
17254
+ const queueMessageData = p.content.queueMessage || {};
17232
17255
  try {
17233
- await this.agentStreamExecutor({ input: { messages: [message] } }, signal);
17256
+ await this.agentStreamExecutor({
17257
+ input: { messages: [message] },
17258
+ custom_run_config: queueMessageData.custom_run_config
17259
+ }, signal);
17234
17260
  await this.queueStore?.markCompleted(p.id);
17235
17261
  const runStatus = await this.getRunStatus();
17236
17262
  const state = await this.getCurrentState();
@@ -17411,11 +17437,14 @@ var Agent = class {
17411
17437
  }
17412
17438
  const content = {
17413
17439
  message: queueMessage.input.message,
17414
- id: messageId
17440
+ id: messageId,
17441
+ // Store complete queue message data for recovery
17442
+ queueMessage: {
17443
+ input: queueMessage.input,
17444
+ command: queueMessage.command,
17445
+ custom_run_config: queueMessage.custom_run_config
17446
+ }
17415
17447
  };
17416
- if (queueMessage.command) {
17417
- content.command = queueMessage.command;
17418
- }
17419
17448
  if (isHighPriority) {
17420
17449
  await store.addMessageAtHead(this.thread_id, content, "human");
17421
17450
  } else {
@@ -17443,7 +17472,7 @@ var Agent = class {
17443
17472
  }
17444
17473
  /**
17445
17474
  * Start queue processor if not already running
17446
- * Private method used internally by addMessage
17475
+ * Public method to allow external triggering (e.g., from recovery)
17447
17476
  */
17448
17477
  async startQueueProcessorIfNeeded() {
17449
17478
  const store = this.getQueueStore();
@@ -17531,6 +17560,30 @@ var Agent = class {
17531
17560
  }
17532
17561
  return "idle" /* IDLE */;
17533
17562
  }
17563
+ /**
17564
+ * Resume task processing after server restart
17565
+ * Resets any stuck processing messages to pending and starts queue processing
17566
+ * Note: Does not rely on LangGraph state as it may be stale after crash/restart
17567
+ */
17568
+ async resumeTask() {
17569
+ try {
17570
+ await agentLatticeManager.initializeClientAsync(this.tenant_id, this.assistant_id);
17571
+ } catch (error) {
17572
+ console.error(`[Agent] Failed to initialize agent lattice for ${this.assistant_id} (tenant: ${this.tenant_id}):`, error);
17573
+ throw error;
17574
+ }
17575
+ const runStatus = await this.getRunStatus();
17576
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
17577
+ console.log(`[Agent] Skipping resume for ${this.assistant_id} (tenant: ${this.tenant_id}) - agent is in interrupted state`);
17578
+ return;
17579
+ }
17580
+ const store = this.getQueueStore();
17581
+ const resetCount = await store.resetProcessingToPending(this.thread_id);
17582
+ if (resetCount > 0) {
17583
+ console.log(`[Agent] Reset ${resetCount} processing messages to pending for thread ${this.thread_id}`);
17584
+ }
17585
+ await this.startQueueProcessorIfNeeded();
17586
+ }
17534
17587
  /**
17535
17588
  * Abort the current agent execution
17536
17589
  * This will cancel any ongoing invoke or stream operations
@@ -17640,6 +17693,57 @@ var AgentInstanceManager = class _AgentInstanceManager {
17640
17693
  });
17641
17694
  this.agents.clear();
17642
17695
  }
17696
+ /**
17697
+ * Restore agent instances for threads with pending messages after server restart
17698
+ * Queries the message queue store for threads with pending/processing messages
17699
+ * and recreates agent instances to resume processing
17700
+ */
17701
+ async restore() {
17702
+ const stats = { restored: 0, errors: 0 };
17703
+ try {
17704
+ const queueStoreLattice = storeLatticeManager.getStoreLattice("default", "threadMessageQueue");
17705
+ const queueStore = queueStoreLattice.store;
17706
+ const threadsWithPending = await queueStore.getThreadsWithPendingMessages();
17707
+ if (threadsWithPending.length === 0) {
17708
+ console.log("[AgentInstanceManager] No threads with pending messages found");
17709
+ return stats;
17710
+ }
17711
+ console.log(`[AgentInstanceManager] Found ${threadsWithPending.length} threads with pending messages, restoring...`);
17712
+ for (const threadInfo of threadsWithPending) {
17713
+ try {
17714
+ await this.restoreThread(threadInfo, queueStore);
17715
+ stats.restored++;
17716
+ } catch (error) {
17717
+ console.error(`[AgentInstanceManager] Failed to restore thread ${threadInfo.threadId}:`, error);
17718
+ stats.errors++;
17719
+ }
17720
+ }
17721
+ console.log(`[AgentInstanceManager] Restore complete: ${stats.restored} restored, ${stats.errors} errors`);
17722
+ return stats;
17723
+ } catch (error) {
17724
+ console.error("[AgentInstanceManager] Restore failed:", error);
17725
+ throw error;
17726
+ }
17727
+ }
17728
+ /**
17729
+ * Restore a single thread
17730
+ * Delegates actual recovery logic to Agent.resumeTask()
17731
+ */
17732
+ async restoreThread(threadInfo, queueStore) {
17733
+ const { tenantId, assistantId, threadId } = threadInfo;
17734
+ const threadParams = {
17735
+ tenant_id: tenantId,
17736
+ assistant_id: assistantId,
17737
+ thread_id: threadId,
17738
+ workspace_id: "default",
17739
+ // TODO: Get from thread store
17740
+ project_id: "default"
17741
+ // TODO: Get from thread store
17742
+ };
17743
+ const agent = this.getAgent(threadParams);
17744
+ await agent.resumeTask();
17745
+ console.log(`[AgentInstanceManager] Restored thread ${threadId}`);
17746
+ }
17643
17747
  };
17644
17748
  var agentInstanceManager = AgentInstanceManager.getInstance();
17645
17749