@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.mjs CHANGED
@@ -10788,13 +10788,13 @@ var StoreBackend = class {
10788
10788
  * @returns List of all items matching the search criteria
10789
10789
  */
10790
10790
  async searchStorePaginated(store, namespace, options = {}) {
10791
- const { query, filter, pageSize = 100 } = options;
10791
+ const { query, filter: filter2, pageSize = 100 } = options;
10792
10792
  const allItems = [];
10793
10793
  let offset = 0;
10794
10794
  while (true) {
10795
10795
  const pageItems = await store.search(namespace, {
10796
10796
  query,
10797
- filter,
10797
+ filter: filter2,
10798
10798
  limit: pageSize,
10799
10799
  offset
10800
10800
  });
@@ -14468,11 +14468,14 @@ var ChunkBuffer = class {
14468
14468
  };
14469
14469
 
14470
14470
  // src/chunk_buffer_lattice/InMemoryChunkBuffer.ts
14471
+ import { MessageChunkTypes } from "@axiom-lattice/protocols";
14471
14472
  import {
14472
14473
  ReplaySubject,
14473
14474
  observeOn,
14474
14475
  asyncScheduler
14475
14476
  } from "rxjs";
14477
+ import { eachValueFrom } from "rxjs-for-await";
14478
+ import { filter, takeWhile } from "rxjs/operators";
14476
14479
  var InMemoryChunkBuffer = class extends ChunkBuffer {
14477
14480
  constructor(config) {
14478
14481
  super();
@@ -14557,7 +14560,7 @@ var InMemoryChunkBuffer = class extends ChunkBuffer {
14557
14560
  let chunk;
14558
14561
  if (typeof arg2 === "string" && arg3 !== void 0) {
14559
14562
  chunk = {
14560
- type: "message_chunk",
14563
+ type: MessageChunkTypes.AI,
14561
14564
  data: {
14562
14565
  id: arg2,
14563
14566
  content: arg3
@@ -14573,7 +14576,6 @@ var InMemoryChunkBuffer = class extends ChunkBuffer {
14573
14576
  buffer2.status = "active" /* ACTIVE */;
14574
14577
  }
14575
14578
  async completeThread(threadId) {
14576
- console.log("completeThread");
14577
14579
  const buffer2 = this.getBufferIfValid(threadId);
14578
14580
  if (buffer2) {
14579
14581
  buffer2.status = "completed" /* COMPLETED */;
@@ -14672,75 +14674,27 @@ var InMemoryChunkBuffer = class extends ChunkBuffer {
14672
14674
  buffer2.updatedAt = Date.now();
14673
14675
  }
14674
14676
  }
14675
- async *getNewChunksSinceContentIterator(threadId, messageId, knownContent) {
14677
+ async *getNewChunksSinceContentIterator(threadId, messageId, knownContent, stopTypes) {
14676
14678
  const buffer2 = this.getBufferIfValid(threadId);
14677
14679
  if (!buffer2) return;
14678
- let accumulatedContent = "";
14679
- const queue = [];
14680
- let resolveNext = null;
14681
- let errorNext = null;
14682
- let isCompleted = false;
14683
- let pendingError = null;
14684
- const subscription = buffer2.chunks$.pipe(observeOn(asyncScheduler)).subscribe({
14685
- next: (chunk) => {
14686
- queue.push(chunk);
14687
- if (resolveNext) {
14688
- const resolve3 = resolveNext;
14689
- resolveNext = null;
14690
- resolve3();
14691
- }
14692
- },
14693
- error: (err) => {
14694
- pendingError = err;
14695
- if (errorNext) {
14696
- const reject = errorNext;
14697
- errorNext = null;
14698
- reject(err);
14699
- } else if (resolveNext) {
14700
- const resolve3 = resolveNext;
14701
- resolveNext = null;
14702
- resolve3();
14703
- }
14704
- },
14705
- complete: () => {
14706
- isCompleted = true;
14707
- if (resolveNext) {
14708
- const resolve3 = resolveNext;
14709
- resolveNext = null;
14710
- resolve3();
14711
- }
14712
- }
14713
- });
14680
+ const defaultStopTypes = [
14681
+ MessageChunkTypes.MESSAGE_COMPLETED,
14682
+ MessageChunkTypes.THREAD_IDLE
14683
+ ];
14684
+ const typesToStop = stopTypes ?? defaultStopTypes;
14714
14685
  let startYieldChunk = false;
14715
- try {
14716
- while (true) {
14717
- if (pendingError) {
14718
- throw pendingError;
14719
- }
14720
- if (queue.length === 0) {
14721
- if (isCompleted) break;
14722
- await new Promise((resolve3, reject) => {
14723
- resolveNext = resolve3;
14724
- errorNext = reject;
14725
- });
14726
- }
14727
- if (pendingError) {
14728
- throw pendingError;
14729
- }
14730
- while (queue.length > 0) {
14731
- const chunk = queue.shift();
14732
- if (!chunk) continue;
14733
- if (chunk.data?.id === messageId) {
14734
- startYieldChunk = true;
14735
- }
14736
- if (startYieldChunk) {
14737
- yield chunk;
14738
- }
14739
- }
14740
- }
14741
- } finally {
14742
- subscription.unsubscribe();
14743
- }
14686
+ console.log("start from messageId", messageId);
14687
+ const filtered$ = buffer2.chunks$.pipe(
14688
+ observeOn(asyncScheduler),
14689
+ // 1. 从指定 messageId 开始
14690
+ filter((chunk) => {
14691
+ if (chunk.data?.id === messageId) startYieldChunk = true;
14692
+ return startYieldChunk;
14693
+ }),
14694
+ // 2. 包含指定的停止类型,但收到后停止
14695
+ takeWhile((chunk) => !typesToStop.includes(chunk.type), true)
14696
+ );
14697
+ yield* eachValueFrom(filtered$);
14744
14698
  }
14745
14699
  getStats() {
14746
14700
  let activeCount = 0;
@@ -16946,9 +16900,7 @@ var Agent = class {
16946
16900
  };
16947
16901
  }
16948
16902
  if (data) {
16949
- if (data.type !== "interrupt") {
16950
- await lifecycleManager.addChunk(data);
16951
- }
16903
+ lifecycleManager.addChunk(data);
16952
16904
  }
16953
16905
  }
16954
16906
  } catch (error) {
@@ -16965,8 +16917,8 @@ var Agent = class {
16965
16917
  while (!signal?.aborted) {
16966
16918
  const pendings = await this.getPendingMessages();
16967
16919
  if (pendings.length === 0) {
16968
- await this.tryToCompleteThread();
16969
16920
  const state = await this.getCurrentState();
16921
+ const runStatus = await this.getRunStatus();
16970
16922
  const pendingCount = await this.getQueueStore().getQueueSize(this.thread_id);
16971
16923
  this.publish("thread:idle", {
16972
16924
  type: "thread:idle",
@@ -16974,6 +16926,15 @@ var Agent = class {
16974
16926
  pendingCount,
16975
16927
  state
16976
16928
  });
16929
+ if (runStatus === "idle" /* IDLE */) {
16930
+ this.addChunk({
16931
+ type: "thread_idle",
16932
+ data: {
16933
+ id: this.thread_id,
16934
+ content: ""
16935
+ }
16936
+ });
16937
+ }
16977
16938
  break;
16978
16939
  }
16979
16940
  const firstMessage = pendings[0];
@@ -17000,16 +16961,41 @@ var Agent = class {
17000
16961
  command: p.content.command
17001
16962
  }, signal);
17002
16963
  await this.queueStore?.markCompleted(p.id);
16964
+ const runStatus = await this.getRunStatus();
17003
16965
  const state = await this.getCurrentState();
17004
- this.publish("message:completed", {
17005
- type: "message:completed",
17006
- messageId: p.content.id,
17007
- timestamp: /* @__PURE__ */ new Date(),
17008
- duration: Date.now() - startTime,
17009
- state
17010
- });
16966
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
16967
+ this.publish("message:interrupted", {
16968
+ type: "message:interrupted",
16969
+ messageId: p.content.id,
16970
+ timestamp: /* @__PURE__ */ new Date(),
16971
+ duration: Date.now() - startTime,
16972
+ state
16973
+ });
16974
+ } else {
16975
+ this.addChunk({
16976
+ type: "message_completed",
16977
+ data: {
16978
+ id: p.content.id,
16979
+ content: ""
16980
+ }
16981
+ });
16982
+ this.publish("message:completed", {
16983
+ type: "message:completed",
16984
+ messageId: p.content.id,
16985
+ timestamp: /* @__PURE__ */ new Date(),
16986
+ duration: Date.now() - startTime,
16987
+ state
16988
+ });
16989
+ }
17011
16990
  } catch (error) {
17012
16991
  console.error(`STEER/Command message ${p.id} execution failed:`, error);
16992
+ this.addChunk({
16993
+ type: "message_failed",
16994
+ data: {
16995
+ id: p.content.id,
16996
+ content: error instanceof Error ? error.message : String(error)
16997
+ }
16998
+ });
17013
16999
  this.publish("message:failed", {
17014
17000
  type: "message:failed",
17015
17001
  messageId: p.content.id,
@@ -17038,20 +17024,45 @@ var Agent = class {
17038
17024
  });
17039
17025
  try {
17040
17026
  await this.agentStreamExecutor({ input: { messages: userMessages } }, signal);
17027
+ const runStatus = await this.getRunStatus();
17041
17028
  const state = await this.getCurrentState();
17042
17029
  for (const p of remainingPendings) {
17043
17030
  await this.queueStore?.markCompleted(p.id);
17044
- this.publish("message:completed", {
17045
- type: "message:completed",
17046
- messageId: p.content.id,
17047
- timestamp: /* @__PURE__ */ new Date(),
17048
- duration: Date.now() - startTime,
17049
- state
17050
- });
17031
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
17032
+ this.publish("message:interrupted", {
17033
+ type: "message:interrupted",
17034
+ messageId: p.content.id,
17035
+ timestamp: /* @__PURE__ */ new Date(),
17036
+ duration: Date.now() - startTime,
17037
+ state
17038
+ });
17039
+ } else {
17040
+ this.addChunk({
17041
+ type: "message_completed",
17042
+ data: {
17043
+ id: p.content.id,
17044
+ content: ""
17045
+ }
17046
+ });
17047
+ this.publish("message:completed", {
17048
+ type: "message:completed",
17049
+ messageId: p.content.id,
17050
+ timestamp: /* @__PURE__ */ new Date(),
17051
+ duration: Date.now() - startTime,
17052
+ state
17053
+ });
17054
+ }
17051
17055
  }
17052
17056
  } catch (error) {
17053
17057
  console.error(`COLLECT mode execution failed:`, error);
17054
17058
  for (const p of remainingPendings) {
17059
+ this.addChunk({
17060
+ type: "message_failed",
17061
+ data: {
17062
+ id: p.content.id,
17063
+ content: error instanceof Error ? error.message : String(error)
17064
+ }
17065
+ });
17055
17066
  this.publish("message:failed", {
17056
17067
  type: "message:failed",
17057
17068
  messageId: p.content.id,
@@ -17077,16 +17088,41 @@ var Agent = class {
17077
17088
  try {
17078
17089
  await this.agentStreamExecutor({ input: { messages: [message] } }, signal);
17079
17090
  await this.queueStore?.markCompleted(p.id);
17091
+ const runStatus = await this.getRunStatus();
17080
17092
  const state = await this.getCurrentState();
17081
- this.publish("message:completed", {
17082
- type: "message:completed",
17083
- messageId: p.content.id,
17084
- timestamp: /* @__PURE__ */ new Date(),
17085
- duration: Date.now() - startTime,
17086
- state
17087
- });
17093
+ if (runStatus === "interrupted" /* INTERRUPTED */) {
17094
+ this.publish("message:interrupted", {
17095
+ type: "message:interrupted",
17096
+ messageId: p.content.id,
17097
+ timestamp: /* @__PURE__ */ new Date(),
17098
+ duration: Date.now() - startTime,
17099
+ state
17100
+ });
17101
+ } else {
17102
+ this.addChunk({
17103
+ type: "message_completed",
17104
+ data: {
17105
+ id: p.content.id,
17106
+ content: ""
17107
+ }
17108
+ });
17109
+ this.publish("message:completed", {
17110
+ type: "message:completed",
17111
+ messageId: p.content.id,
17112
+ timestamp: /* @__PURE__ */ new Date(),
17113
+ duration: Date.now() - startTime,
17114
+ state
17115
+ });
17116
+ }
17088
17117
  } catch (error) {
17089
17118
  console.error(`FOLLOWUP mode message ${p.id} execution failed:`, error);
17119
+ this.addChunk({
17120
+ type: "message_failed",
17121
+ data: {
17122
+ id: p.content.id,
17123
+ content: error instanceof Error ? error.message : String(error)
17124
+ }
17125
+ });
17090
17126
  this.publish("message:failed", {
17091
17127
  type: "message:failed",
17092
17128
  messageId: p.content.id,
@@ -17116,11 +17152,12 @@ var Agent = class {
17116
17152
  addChunk(content) {
17117
17153
  return this.chunkBuffer.addChunk(this.thread_id, content);
17118
17154
  }
17119
- chunkStream(message_id, known_content = "") {
17155
+ chunkStream(message_id, stopTypes) {
17120
17156
  const stream = this.chunkBuffer.getNewChunksSinceContentIterator(
17121
17157
  this.thread_id,
17122
17158
  message_id,
17123
- known_content
17159
+ "",
17160
+ stopTypes
17124
17161
  );
17125
17162
  return {
17126
17163
  [Symbol.asyncIterator]: async function* () {
@@ -17175,11 +17212,6 @@ var Agent = class {
17175
17212
  const store = this.getQueueStore();
17176
17213
  return await store.getPendingMessages(this.thread_id);
17177
17214
  }
17178
- async tryToCompleteThread() {
17179
- setTimeout(async () => {
17180
- await this.chunkBuffer.completeThread(this.thread_id);
17181
- }, 100);
17182
- }
17183
17215
  getQueueStore() {
17184
17216
  if (!this.queueStore) {
17185
17217
  try {
@@ -17398,6 +17430,7 @@ var Agent = class {
17398
17430
  */
17399
17431
  publish(eventName, data) {
17400
17432
  const namespacedEvent = `${eventName}:${this.tenant_id}:${this.thread_id}`;
17433
+ console.log(namespacedEvent);
17401
17434
  event_bus_default.publish(namespacedEvent, data);
17402
17435
  }
17403
17436
  };