@axiom-lattice/core 2.1.41 → 2.1.42

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.js CHANGED
@@ -10949,13 +10949,13 @@ var StoreBackend = class {
10949
10949
  * @returns List of all items matching the search criteria
10950
10950
  */
10951
10951
  async searchStorePaginated(store, namespace, options = {}) {
10952
- const { query, filter, pageSize = 100 } = options;
10952
+ const { query, filter: filter2, pageSize = 100 } = options;
10953
10953
  const allItems = [];
10954
10954
  let offset = 0;
10955
10955
  while (true) {
10956
10956
  const pageItems = await store.search(namespace, {
10957
10957
  query,
10958
- filter,
10958
+ filter: filter2,
10959
10959
  limit: pageSize,
10960
10960
  offset
10961
10961
  });
@@ -14625,7 +14625,10 @@ var ChunkBuffer = class {
14625
14625
  };
14626
14626
 
14627
14627
  // src/chunk_buffer_lattice/InMemoryChunkBuffer.ts
14628
+ var import_protocols5 = require("@axiom-lattice/protocols");
14628
14629
  var import_rxjs = require("rxjs");
14630
+ var import_rxjs_for_await = require("rxjs-for-await");
14631
+ var import_operators = require("rxjs/operators");
14629
14632
  var InMemoryChunkBuffer = class extends ChunkBuffer {
14630
14633
  constructor(config) {
14631
14634
  super();
@@ -14710,7 +14713,7 @@ var InMemoryChunkBuffer = class extends ChunkBuffer {
14710
14713
  let chunk;
14711
14714
  if (typeof arg2 === "string" && arg3 !== void 0) {
14712
14715
  chunk = {
14713
- type: "message_chunk",
14716
+ type: import_protocols5.MessageChunkTypes.AI,
14714
14717
  data: {
14715
14718
  id: arg2,
14716
14719
  content: arg3
@@ -14726,7 +14729,6 @@ var InMemoryChunkBuffer = class extends ChunkBuffer {
14726
14729
  buffer2.status = "active" /* ACTIVE */;
14727
14730
  }
14728
14731
  async completeThread(threadId) {
14729
- console.log("completeThread");
14730
14732
  const buffer2 = this.getBufferIfValid(threadId);
14731
14733
  if (buffer2) {
14732
14734
  buffer2.status = "completed" /* COMPLETED */;
@@ -14825,75 +14827,27 @@ var InMemoryChunkBuffer = class extends ChunkBuffer {
14825
14827
  buffer2.updatedAt = Date.now();
14826
14828
  }
14827
14829
  }
14828
- async *getNewChunksSinceContentIterator(threadId, messageId, knownContent) {
14830
+ async *getNewChunksSinceContentIterator(threadId, messageId, knownContent, stopTypes) {
14829
14831
  const buffer2 = this.getBufferIfValid(threadId);
14830
14832
  if (!buffer2) return;
14831
- let accumulatedContent = "";
14832
- const queue = [];
14833
- let resolveNext = null;
14834
- let errorNext = null;
14835
- let isCompleted = false;
14836
- let pendingError = null;
14837
- const subscription = buffer2.chunks$.pipe((0, import_rxjs.observeOn)(import_rxjs.asyncScheduler)).subscribe({
14838
- next: (chunk) => {
14839
- queue.push(chunk);
14840
- if (resolveNext) {
14841
- const resolve3 = resolveNext;
14842
- resolveNext = null;
14843
- resolve3();
14844
- }
14845
- },
14846
- error: (err) => {
14847
- pendingError = err;
14848
- if (errorNext) {
14849
- const reject = errorNext;
14850
- errorNext = null;
14851
- reject(err);
14852
- } else if (resolveNext) {
14853
- const resolve3 = resolveNext;
14854
- resolveNext = null;
14855
- resolve3();
14856
- }
14857
- },
14858
- complete: () => {
14859
- isCompleted = true;
14860
- if (resolveNext) {
14861
- const resolve3 = resolveNext;
14862
- resolveNext = null;
14863
- resolve3();
14864
- }
14865
- }
14866
- });
14833
+ const defaultStopTypes = [
14834
+ import_protocols5.MessageChunkTypes.MESSAGE_COMPLETED,
14835
+ import_protocols5.MessageChunkTypes.THREAD_IDLE
14836
+ ];
14837
+ const typesToStop = stopTypes ?? defaultStopTypes;
14867
14838
  let startYieldChunk = false;
14868
- try {
14869
- while (true) {
14870
- if (pendingError) {
14871
- throw pendingError;
14872
- }
14873
- if (queue.length === 0) {
14874
- if (isCompleted) break;
14875
- await new Promise((resolve3, reject) => {
14876
- resolveNext = resolve3;
14877
- errorNext = reject;
14878
- });
14879
- }
14880
- if (pendingError) {
14881
- throw pendingError;
14882
- }
14883
- while (queue.length > 0) {
14884
- const chunk = queue.shift();
14885
- if (!chunk) continue;
14886
- if (chunk.data?.id === messageId) {
14887
- startYieldChunk = true;
14888
- }
14889
- if (startYieldChunk) {
14890
- yield chunk;
14891
- }
14892
- }
14893
- }
14894
- } finally {
14895
- subscription.unsubscribe();
14896
- }
14839
+ console.log("start from messageId", messageId);
14840
+ const filtered$ = buffer2.chunks$.pipe(
14841
+ (0, import_rxjs.observeOn)(import_rxjs.asyncScheduler),
14842
+ // 1. 从指定 messageId 开始
14843
+ (0, import_operators.filter)((chunk) => {
14844
+ if (chunk.data?.id === messageId) startYieldChunk = true;
14845
+ return startYieldChunk;
14846
+ }),
14847
+ // 2. 包含指定的停止类型,但收到后停止
14848
+ (0, import_operators.takeWhile)((chunk) => !typesToStop.includes(chunk.type), true)
14849
+ );
14850
+ yield* (0, import_rxjs_for_await.eachValueFrom)(filtered$);
14897
14851
  }
14898
14852
  getStats() {
14899
14853
  let activeCount = 0;
@@ -14973,13 +14927,13 @@ var buffer = new InMemoryChunkBuffer({
14973
14927
  registerChunkBuffer("default", buffer);
14974
14928
 
14975
14929
  // src/schedule_lattice/ScheduleLatticeManager.ts
14976
- var import_protocols7 = require("@axiom-lattice/protocols");
14930
+ var import_protocols8 = require("@axiom-lattice/protocols");
14977
14931
 
14978
14932
  // src/schedule_lattice/DefaultScheduleClient.ts
14979
- var import_protocols6 = require("@axiom-lattice/protocols");
14933
+ var import_protocols7 = require("@axiom-lattice/protocols");
14980
14934
 
14981
14935
  // src/schedule_lattice/MemoryScheduleStorage.ts
14982
- var import_protocols5 = require("@axiom-lattice/protocols");
14936
+ var import_protocols6 = require("@axiom-lattice/protocols");
14983
14937
  var MemoryScheduleStorage = class {
14984
14938
  constructor() {
14985
14939
  this.tasks = /* @__PURE__ */ new Map();
@@ -15022,7 +14976,7 @@ var MemoryScheduleStorage = class {
15022
14976
  async getActiveTasks() {
15023
14977
  const result = [];
15024
14978
  for (const task of this.tasks.values()) {
15025
- if (task.status === import_protocols5.ScheduledTaskStatus.PENDING || task.status === import_protocols5.ScheduledTaskStatus.PAUSED) {
14979
+ if (task.status === import_protocols6.ScheduledTaskStatus.PENDING || task.status === import_protocols6.ScheduledTaskStatus.PAUSED) {
15026
14980
  result.push({ ...task });
15027
14981
  }
15028
14982
  }
@@ -15165,7 +15119,7 @@ var MemoryScheduleStorage = class {
15165
15119
  const cutoff = Date.now() - olderThanMs;
15166
15120
  let deleted = 0;
15167
15121
  for (const [taskId, task] of this.tasks.entries()) {
15168
- if ((task.status === import_protocols5.ScheduledTaskStatus.COMPLETED || task.status === import_protocols5.ScheduledTaskStatus.CANCELLED || task.status === import_protocols5.ScheduledTaskStatus.FAILED) && task.updatedAt < cutoff) {
15122
+ if ((task.status === import_protocols6.ScheduledTaskStatus.COMPLETED || task.status === import_protocols6.ScheduledTaskStatus.CANCELLED || task.status === import_protocols6.ScheduledTaskStatus.FAILED) && task.updatedAt < cutoff) {
15169
15123
  this.tasks.delete(taskId);
15170
15124
  deleted++;
15171
15125
  }
@@ -15446,10 +15400,10 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15446
15400
  tenantId: options.tenantId ?? "default",
15447
15401
  assistantId: options.assistantId,
15448
15402
  threadId: options.threadId,
15449
- executionType: import_protocols6.ScheduleExecutionType.ONCE,
15403
+ executionType: import_protocols7.ScheduleExecutionType.ONCE,
15450
15404
  executeAt,
15451
15405
  delayMs: options.delayMs,
15452
- status: import_protocols6.ScheduledTaskStatus.PENDING,
15406
+ status: import_protocols7.ScheduledTaskStatus.PENDING,
15453
15407
  runCount: 0,
15454
15408
  retryCount: 0,
15455
15409
  maxRetries: options.maxRetries ?? 0,
@@ -15492,11 +15446,11 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15492
15446
  tenantId: options.tenantId ?? "default",
15493
15447
  assistantId: options.assistantId,
15494
15448
  threadId: options.threadId,
15495
- executionType: import_protocols6.ScheduleExecutionType.CRON,
15449
+ executionType: import_protocols7.ScheduleExecutionType.CRON,
15496
15450
  cronExpression: options.cronExpression,
15497
15451
  timezone: options.timezone,
15498
15452
  nextRunAt: nextRunAt.getTime(),
15499
- status: import_protocols6.ScheduledTaskStatus.PENDING,
15453
+ status: import_protocols7.ScheduledTaskStatus.PENDING,
15500
15454
  runCount: 0,
15501
15455
  maxRuns: options.maxRuns,
15502
15456
  retryCount: 0,
@@ -15523,7 +15477,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15523
15477
  const task = await this.storage.get(taskId);
15524
15478
  if (task) {
15525
15479
  await this.storage.update(taskId, {
15526
- status: import_protocols6.ScheduledTaskStatus.CANCELLED
15480
+ status: import_protocols7.ScheduledTaskStatus.CANCELLED
15527
15481
  });
15528
15482
  console.log(`[Scheduler] Task cancelled: ${taskId}`);
15529
15483
  return true;
@@ -15532,11 +15486,11 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15532
15486
  }
15533
15487
  async pause(taskId) {
15534
15488
  const task = await this.storage.get(taskId);
15535
- if (!task || task.executionType !== import_protocols6.ScheduleExecutionType.CRON) {
15489
+ if (!task || task.executionType !== import_protocols7.ScheduleExecutionType.CRON) {
15536
15490
  console.error(`[Scheduler] Can only pause CRON tasks: ${taskId}`);
15537
15491
  return false;
15538
15492
  }
15539
- if (task.status !== import_protocols6.ScheduledTaskStatus.PENDING) {
15493
+ if (task.status !== import_protocols7.ScheduledTaskStatus.PENDING) {
15540
15494
  console.error(`[Scheduler] Can only pause PENDING tasks: ${taskId}`);
15541
15495
  return false;
15542
15496
  }
@@ -15546,24 +15500,24 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15546
15500
  this.timers.delete(taskId);
15547
15501
  }
15548
15502
  await this.storage.update(taskId, {
15549
- status: import_protocols6.ScheduledTaskStatus.PAUSED
15503
+ status: import_protocols7.ScheduledTaskStatus.PAUSED
15550
15504
  });
15551
15505
  console.log(`[Scheduler] Task paused: ${taskId}`);
15552
15506
  return true;
15553
15507
  }
15554
15508
  async resume(taskId) {
15555
15509
  const task = await this.storage.get(taskId);
15556
- if (!task || task.executionType !== import_protocols6.ScheduleExecutionType.CRON) {
15510
+ if (!task || task.executionType !== import_protocols7.ScheduleExecutionType.CRON) {
15557
15511
  console.error(`[Scheduler] Can only resume CRON tasks: ${taskId}`);
15558
15512
  return false;
15559
15513
  }
15560
- if (task.status !== import_protocols6.ScheduledTaskStatus.PAUSED) {
15514
+ if (task.status !== import_protocols7.ScheduledTaskStatus.PAUSED) {
15561
15515
  console.error(`[Scheduler] Can only resume PAUSED tasks: ${taskId}`);
15562
15516
  return false;
15563
15517
  }
15564
15518
  const nextRunAt = getNextCronTime(task.cronExpression);
15565
15519
  await this.storage.update(taskId, {
15566
- status: import_protocols6.ScheduledTaskStatus.PENDING,
15520
+ status: import_protocols7.ScheduledTaskStatus.PENDING,
15567
15521
  nextRunAt: nextRunAt.getTime()
15568
15522
  });
15569
15523
  const updatedTask = await this.storage.get(taskId);
@@ -15585,7 +15539,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15585
15539
  if (!task) {
15586
15540
  return -1;
15587
15541
  }
15588
- const targetTime = task.executionType === import_protocols6.ScheduleExecutionType.CRON ? task.nextRunAt : task.executeAt;
15542
+ const targetTime = task.executionType === import_protocols7.ScheduleExecutionType.CRON ? task.nextRunAt : task.executeAt;
15589
15543
  if (targetTime === void 0) {
15590
15544
  return -1;
15591
15545
  }
@@ -15607,7 +15561,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15607
15561
  const activeTasks = await this.storage.getActiveTasks();
15608
15562
  for (const task of activeTasks) {
15609
15563
  await this.storage.update(task.taskId, {
15610
- status: import_protocols6.ScheduledTaskStatus.CANCELLED
15564
+ status: import_protocols7.ScheduledTaskStatus.CANCELLED
15611
15565
  });
15612
15566
  }
15613
15567
  console.log(`[Scheduler] All tasks cancelled`);
@@ -15623,10 +15577,10 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15623
15577
  );
15624
15578
  continue;
15625
15579
  }
15626
- if (task.status === import_protocols6.ScheduledTaskStatus.PAUSED) {
15580
+ if (task.status === import_protocols7.ScheduledTaskStatus.PAUSED) {
15627
15581
  continue;
15628
15582
  }
15629
- if (task.executionType === import_protocols6.ScheduleExecutionType.ONCE) {
15583
+ if (task.executionType === import_protocols7.ScheduleExecutionType.ONCE) {
15630
15584
  if (task.executeAt && task.executeAt <= Date.now()) {
15631
15585
  console.log(
15632
15586
  `[Scheduler] Executing overdue one-time task: ${task.taskId}`
@@ -15635,7 +15589,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15635
15589
  } else {
15636
15590
  this.scheduleTimer(task);
15637
15591
  }
15638
- } else if (task.executionType === import_protocols6.ScheduleExecutionType.CRON) {
15592
+ } else if (task.executionType === import_protocols7.ScheduleExecutionType.CRON) {
15639
15593
  const nextRunAt = getNextCronTime(task.cronExpression);
15640
15594
  await this.storage.update(task.taskId, {
15641
15595
  nextRunAt: nextRunAt.getTime()
@@ -15659,7 +15613,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15659
15613
  }
15660
15614
  // ===== Private Methods =====
15661
15615
  scheduleTimer(task) {
15662
- const targetTime = task.executionType === import_protocols6.ScheduleExecutionType.CRON ? task.nextRunAt : task.executeAt;
15616
+ const targetTime = task.executionType === import_protocols7.ScheduleExecutionType.CRON ? task.nextRunAt : task.executeAt;
15663
15617
  if (targetTime === void 0) {
15664
15618
  console.error(`[Scheduler] No execution time for task: ${task.taskId}`);
15665
15619
  return;
@@ -15680,13 +15634,13 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15680
15634
  `[Scheduler] No handler for task type: ${task.taskType}, task: ${task.taskId}`
15681
15635
  );
15682
15636
  await this.storage.update(task.taskId, {
15683
- status: import_protocols6.ScheduledTaskStatus.FAILED,
15637
+ status: import_protocols7.ScheduledTaskStatus.FAILED,
15684
15638
  lastError: `No handler registered for task type: ${task.taskType}`
15685
15639
  });
15686
15640
  return;
15687
15641
  }
15688
15642
  await this.storage.update(task.taskId, {
15689
- status: import_protocols6.ScheduledTaskStatus.RUNNING
15643
+ status: import_protocols7.ScheduledTaskStatus.RUNNING
15690
15644
  });
15691
15645
  console.log(`[Scheduler] Executing task: ${task.taskId}`);
15692
15646
  try {
@@ -15698,17 +15652,17 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15698
15652
  await handler(latestTask.payload, latestTask);
15699
15653
  const newRunCount = latestTask.runCount + 1;
15700
15654
  console.log(`[Scheduler] Task completed: ${task.taskId}`);
15701
- if (task.executionType === import_protocols6.ScheduleExecutionType.ONCE) {
15655
+ if (task.executionType === import_protocols7.ScheduleExecutionType.ONCE) {
15702
15656
  await this.storage.update(task.taskId, {
15703
- status: import_protocols6.ScheduledTaskStatus.COMPLETED,
15657
+ status: import_protocols7.ScheduledTaskStatus.COMPLETED,
15704
15658
  runCount: newRunCount,
15705
15659
  lastRunAt: Date.now()
15706
15660
  });
15707
15661
  this.timers.delete(task.taskId);
15708
- } else if (task.executionType === import_protocols6.ScheduleExecutionType.CRON) {
15662
+ } else if (task.executionType === import_protocols7.ScheduleExecutionType.CRON) {
15709
15663
  if (task.maxRuns !== void 0 && newRunCount >= task.maxRuns) {
15710
15664
  await this.storage.update(task.taskId, {
15711
- status: import_protocols6.ScheduledTaskStatus.COMPLETED,
15665
+ status: import_protocols7.ScheduledTaskStatus.COMPLETED,
15712
15666
  runCount: newRunCount,
15713
15667
  lastRunAt: Date.now()
15714
15668
  });
@@ -15720,7 +15674,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15720
15674
  }
15721
15675
  if (task.expiresAt !== void 0 && Date.now() >= task.expiresAt) {
15722
15676
  await this.storage.update(task.taskId, {
15723
- status: import_protocols6.ScheduledTaskStatus.COMPLETED,
15677
+ status: import_protocols7.ScheduledTaskStatus.COMPLETED,
15724
15678
  runCount: newRunCount,
15725
15679
  lastRunAt: Date.now()
15726
15680
  });
@@ -15732,7 +15686,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15732
15686
  }
15733
15687
  const nextRunAt = getNextCronTime(task.cronExpression);
15734
15688
  await this.storage.update(task.taskId, {
15735
- status: import_protocols6.ScheduledTaskStatus.PENDING,
15689
+ status: import_protocols7.ScheduledTaskStatus.PENDING,
15736
15690
  runCount: newRunCount,
15737
15691
  lastRunAt: Date.now(),
15738
15692
  nextRunAt: nextRunAt.getTime(),
@@ -15758,7 +15712,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15758
15712
  `[Scheduler] Retrying task: ${task.taskId} (attempt ${newRetryCount}/${latestTask.maxRetries})`
15759
15713
  );
15760
15714
  await this.storage.update(task.taskId, {
15761
- status: import_protocols6.ScheduledTaskStatus.PENDING,
15715
+ status: import_protocols7.ScheduledTaskStatus.PENDING,
15762
15716
  retryCount: newRetryCount,
15763
15717
  lastError: errorMessage
15764
15718
  });
@@ -15768,7 +15722,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15768
15722
  );
15769
15723
  const retryTask = await this.storage.get(task.taskId);
15770
15724
  if (retryTask) {
15771
- if (task.executionType === import_protocols6.ScheduleExecutionType.ONCE) {
15725
+ if (task.executionType === import_protocols7.ScheduleExecutionType.ONCE) {
15772
15726
  retryTask.executeAt = Date.now() + retryDelay;
15773
15727
  } else {
15774
15728
  retryTask.nextRunAt = Date.now() + retryDelay;
@@ -15781,7 +15735,7 @@ var _DefaultScheduleClient = class _DefaultScheduleClient {
15781
15735
  }
15782
15736
  } else {
15783
15737
  await this.storage.update(task.taskId, {
15784
- status: import_protocols6.ScheduledTaskStatus.FAILED,
15738
+ status: import_protocols7.ScheduledTaskStatus.FAILED,
15785
15739
  retryCount: newRetryCount,
15786
15740
  lastError: errorMessage
15787
15741
  });
@@ -15833,9 +15787,9 @@ var ScheduleLatticeManager = class _ScheduleLatticeManager extends BaseLatticeMa
15833
15787
  } else {
15834
15788
  storage = new MemoryScheduleStorage();
15835
15789
  }
15836
- if (config.type === import_protocols7.ScheduleType.MEMORY) {
15790
+ if (config.type === import_protocols8.ScheduleType.MEMORY) {
15837
15791
  scheduleClient = new DefaultScheduleClient(storage);
15838
- } else if (config.type === import_protocols7.ScheduleType.POSTGRES) {
15792
+ } else if (config.type === import_protocols8.ScheduleType.POSTGRES) {
15839
15793
  if (!config.storage) {
15840
15794
  throw new Error(
15841
15795
  `PostgreSQL schedule storage must be provided. Please install @axiom-lattice/pg-stores and pass the storage in config.storage.`
@@ -16145,7 +16099,7 @@ var getVectorStoreLattice = (key) => vectorStoreLatticeManager.getVectorStoreLat
16145
16099
  var getVectorStoreClient = (key) => vectorStoreLatticeManager.getVectorStoreClient(key);
16146
16100
 
16147
16101
  // src/logger_lattice/LoggerLatticeManager.ts
16148
- var import_protocols8 = require("@axiom-lattice/protocols");
16102
+ var import_protocols9 = require("@axiom-lattice/protocols");
16149
16103
 
16150
16104
  // src/logger_lattice/PinoLoggerClient.ts
16151
16105
  var import_pino = __toESM(require("pino"));
@@ -16367,11 +16321,11 @@ var LoggerLatticeManager = class _LoggerLatticeManager extends BaseLatticeManage
16367
16321
  if (client) {
16368
16322
  loggerClient = client;
16369
16323
  } else {
16370
- if (config.type === import_protocols8.LoggerType.PINO) {
16324
+ if (config.type === import_protocols9.LoggerType.PINO) {
16371
16325
  loggerClient = new PinoLoggerClient(config);
16372
- } else if (config.type === import_protocols8.LoggerType.CONSOLE) {
16326
+ } else if (config.type === import_protocols9.LoggerType.CONSOLE) {
16373
16327
  loggerClient = new ConsoleLoggerClient(config);
16374
- } else if (config.type === import_protocols8.LoggerType.CUSTOM) {
16328
+ } else if (config.type === import_protocols9.LoggerType.CUSTOM) {
16375
16329
  throw new Error(
16376
16330
  `Custom logger client must be provided. Please pass the client to registerLattice.`
16377
16331
  );
@@ -17090,9 +17044,7 @@ var Agent = class {
17090
17044
  };
17091
17045
  }
17092
17046
  if (data) {
17093
- if (data.type !== "interrupt") {
17094
- await lifecycleManager.addChunk(data);
17095
- }
17047
+ lifecycleManager.addChunk(data);
17096
17048
  }
17097
17049
  }
17098
17050
  } catch (error) {
@@ -17109,8 +17061,8 @@ var Agent = class {
17109
17061
  while (!signal?.aborted) {
17110
17062
  const pendings = await this.getPendingMessages();
17111
17063
  if (pendings.length === 0) {
17112
- await this.tryToCompleteThread();
17113
17064
  const state = await this.getCurrentState();
17065
+ const runStatus = await this.getRunStatus();
17114
17066
  const pendingCount = await this.getQueueStore().getQueueSize(this.thread_id);
17115
17067
  this.publish("thread:idle", {
17116
17068
  type: "thread:idle",
@@ -17118,6 +17070,15 @@ var Agent = class {
17118
17070
  pendingCount,
17119
17071
  state
17120
17072
  });
17073
+ if (runStatus === "idle" /* IDLE */) {
17074
+ this.addChunk({
17075
+ type: "thread_idle",
17076
+ data: {
17077
+ id: this.thread_id,
17078
+ content: ""
17079
+ }
17080
+ });
17081
+ }
17121
17082
  break;
17122
17083
  }
17123
17084
  const firstMessage = pendings[0];
@@ -17144,16 +17105,41 @@ var Agent = class {
17144
17105
  command: p.content.command
17145
17106
  }, signal);
17146
17107
  await this.queueStore?.markCompleted(p.id);
17108
+ const runStatus = await this.getRunStatus();
17147
17109
  const state = await this.getCurrentState();
17148
- this.publish("message:completed", {
17149
- type: "message:completed",
17150
- messageId: p.content.id,
17151
- timestamp: /* @__PURE__ */ new Date(),
17152
- duration: Date.now() - startTime,
17153
- state
17154
- });
17110
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
17111
+ this.publish("message:interrupted", {
17112
+ type: "message:interrupted",
17113
+ messageId: p.content.id,
17114
+ timestamp: /* @__PURE__ */ new Date(),
17115
+ duration: Date.now() - startTime,
17116
+ state
17117
+ });
17118
+ } else {
17119
+ this.addChunk({
17120
+ type: "message_completed",
17121
+ data: {
17122
+ id: p.content.id,
17123
+ content: ""
17124
+ }
17125
+ });
17126
+ this.publish("message:completed", {
17127
+ type: "message:completed",
17128
+ messageId: p.content.id,
17129
+ timestamp: /* @__PURE__ */ new Date(),
17130
+ duration: Date.now() - startTime,
17131
+ state
17132
+ });
17133
+ }
17155
17134
  } catch (error) {
17156
17135
  console.error(`STEER/Command message ${p.id} execution failed:`, error);
17136
+ this.addChunk({
17137
+ type: "message_failed",
17138
+ data: {
17139
+ id: p.content.id,
17140
+ content: error instanceof Error ? error.message : String(error)
17141
+ }
17142
+ });
17157
17143
  this.publish("message:failed", {
17158
17144
  type: "message:failed",
17159
17145
  messageId: p.content.id,
@@ -17182,20 +17168,45 @@ var Agent = class {
17182
17168
  });
17183
17169
  try {
17184
17170
  await this.agentStreamExecutor({ input: { messages: userMessages } }, signal);
17171
+ const runStatus = await this.getRunStatus();
17185
17172
  const state = await this.getCurrentState();
17186
17173
  for (const p of remainingPendings) {
17187
17174
  await this.queueStore?.markCompleted(p.id);
17188
- this.publish("message:completed", {
17189
- type: "message:completed",
17190
- messageId: p.content.id,
17191
- timestamp: /* @__PURE__ */ new Date(),
17192
- duration: Date.now() - startTime,
17193
- state
17194
- });
17175
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
17176
+ this.publish("message:interrupted", {
17177
+ type: "message:interrupted",
17178
+ messageId: p.content.id,
17179
+ timestamp: /* @__PURE__ */ new Date(),
17180
+ duration: Date.now() - startTime,
17181
+ state
17182
+ });
17183
+ } else {
17184
+ this.addChunk({
17185
+ type: "message_completed",
17186
+ data: {
17187
+ id: p.content.id,
17188
+ content: ""
17189
+ }
17190
+ });
17191
+ this.publish("message:completed", {
17192
+ type: "message:completed",
17193
+ messageId: p.content.id,
17194
+ timestamp: /* @__PURE__ */ new Date(),
17195
+ duration: Date.now() - startTime,
17196
+ state
17197
+ });
17198
+ }
17195
17199
  }
17196
17200
  } catch (error) {
17197
17201
  console.error(`COLLECT mode execution failed:`, error);
17198
17202
  for (const p of remainingPendings) {
17203
+ this.addChunk({
17204
+ type: "message_failed",
17205
+ data: {
17206
+ id: p.content.id,
17207
+ content: error instanceof Error ? error.message : String(error)
17208
+ }
17209
+ });
17199
17210
  this.publish("message:failed", {
17200
17211
  type: "message:failed",
17201
17212
  messageId: p.content.id,
@@ -17221,16 +17232,41 @@ var Agent = class {
17221
17232
  try {
17222
17233
  await this.agentStreamExecutor({ input: { messages: [message] } }, signal);
17223
17234
  await this.queueStore?.markCompleted(p.id);
17235
+ const runStatus = await this.getRunStatus();
17224
17236
  const state = await this.getCurrentState();
17225
- this.publish("message:completed", {
17226
- type: "message:completed",
17227
- messageId: p.content.id,
17228
- timestamp: /* @__PURE__ */ new Date(),
17229
- duration: Date.now() - startTime,
17230
- state
17231
- });
17237
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
17238
+ this.publish("message:interrupted", {
17239
+ type: "message:interrupted",
17240
+ messageId: p.content.id,
17241
+ timestamp: /* @__PURE__ */ new Date(),
17242
+ duration: Date.now() - startTime,
17243
+ state
17244
+ });
17245
+ } else {
17246
+ this.addChunk({
17247
+ type: "message_completed",
17248
+ data: {
17249
+ id: p.content.id,
17250
+ content: ""
17251
+ }
17252
+ });
17253
+ this.publish("message:completed", {
17254
+ type: "message:completed",
17255
+ messageId: p.content.id,
17256
+ timestamp: /* @__PURE__ */ new Date(),
17257
+ duration: Date.now() - startTime,
17258
+ state
17259
+ });
17260
+ }
17232
17261
  } catch (error) {
17233
17262
  console.error(`FOLLOWUP mode message ${p.id} execution failed:`, error);
17263
+ this.addChunk({
17264
+ type: "message_failed",
17265
+ data: {
17266
+ id: p.content.id,
17267
+ content: error instanceof Error ? error.message : String(error)
17268
+ }
17269
+ });
17234
17270
  this.publish("message:failed", {
17235
17271
  type: "message:failed",
17236
17272
  messageId: p.content.id,
@@ -17260,11 +17296,12 @@ var Agent = class {
17260
17296
  addChunk(content) {
17261
17297
  return this.chunkBuffer.addChunk(this.thread_id, content);
17262
17298
  }
17263
- chunkStream(message_id, known_content = "") {
17299
+ chunkStream(message_id, stopTypes) {
17264
17300
  const stream = this.chunkBuffer.getNewChunksSinceContentIterator(
17265
17301
  this.thread_id,
17266
17302
  message_id,
17267
- known_content
17303
+ "",
17304
+ stopTypes
17268
17305
  );
17269
17306
  return {
17270
17307
  [Symbol.asyncIterator]: async function* () {
@@ -17319,11 +17356,6 @@ var Agent = class {
17319
17356
  const store = this.getQueueStore();
17320
17357
  return await store.getPendingMessages(this.thread_id);
17321
17358
  }
17322
- async tryToCompleteThread() {
17323
- setTimeout(async () => {
17324
- await this.chunkBuffer.completeThread(this.thread_id);
17325
- }, 100);
17326
- }
17327
17359
  getQueueStore() {
17328
17360
  if (!this.queueStore) {
17329
17361
  try {
@@ -17542,6 +17574,7 @@ var Agent = class {
17542
17574
  */
17543
17575
  publish(eventName, data) {
17544
17576
  const namespacedEvent = `${eventName}:${this.tenant_id}:${this.thread_id}`;
17577
+ console.log(namespacedEvent);
17545
17578
  event_bus_default.publish(namespacedEvent, data);
17546
17579
  }
17547
17580
  };