@mitralab.io/sdk-core 0.3.5 → 0.3.6

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.cjs CHANGED
@@ -3955,12 +3955,14 @@ var AgentDirectChannel = class {
3955
3955
  const otherLog = historyCursor?.boxLog !== void 0 && channel.boxLog !== void 0 && historyCursor.boxLog !== channel.boxLog;
3956
3956
  const historyFrom = historyCursor === void 0 ? void 0 : otherLog ? 0 : historyCursor.sequence;
3957
3957
  let cursor = historyFrom ?? channel.lastSequence;
3958
+ let boxLog = channel.boxLog;
3958
3959
  const abort = new AbortController();
3959
3960
  const onAbort = () => abort.abort(signal?.reason);
3960
3961
  signal?.addEventListener("abort", onAbort, { once: true });
3961
- const onFrame = (event) => {
3962
- const hello = helloOf(event);
3962
+ const onFrame = (frame) => {
3963
+ const hello = helloOf(frame);
3963
3964
  if (hello) thread = hello.threadId;
3965
+ const event = hello && boxLog !== void 0 ? { ...frame, payload: { ...asObject(frame.payload), boxLog } } : frame;
3964
3966
  if (typeof event.sequence === "number" && event.sequence > cursor) cursor = event.sequence;
3965
3967
  if (isSubagentFrame(event)) {
3966
3968
  observer.onEvent(event);
@@ -3991,6 +3993,7 @@ var AgentDirectChannel = class {
3991
3993
  onFrame,
3992
3994
  onLost,
3993
3995
  replayFrom: (next) => {
3996
+ boxLog = next.boxLog;
3994
3997
  const decide = (hello) => {
3995
3998
  const nextBox = boxAddress(next.wsUrl);
3996
3999
  const zeroed = next.lastSequence === 0 && cursor > 0;
@@ -4683,6 +4686,9 @@ var CoreAgentTaskSession = class {
4683
4686
  _hasOlder = false;
4684
4687
  // Moves whenever `history` is replaced, so a `loadOlder` still reading drops what it read.
4685
4688
  historyGeneration = 0;
4689
+ // Whether the box the channel follows counts in the log of the history's rows. A box whose hello
4690
+ // names another log, or reports a log shorter than the history's, has sequences that start over.
4691
+ rowsInChannelLog = true;
4686
4692
  olderRead = null;
4687
4693
  get taskId() {
4688
4694
  return this._taskId;
@@ -4823,6 +4829,7 @@ var CoreAgentTaskSession = class {
4823
4829
  }
4824
4830
  replaceHistory(page) {
4825
4831
  this.historyGeneration += 1;
4832
+ this.rowsInChannelLog = true;
4826
4833
  this._hasOlder = hasOlderRows(page);
4827
4834
  this._history = [...page.content].reverse().map(toAgentTimelineItem);
4828
4835
  }
@@ -5403,6 +5410,15 @@ var CoreAgentTaskSession = class {
5403
5410
  ...typeof lifecycle?.clientMessageId === "string" ? { clientMessageId: lifecycle.clientMessageId } : {}
5404
5411
  });
5405
5412
  break;
5413
+ case "hello": {
5414
+ const lastSequence = payload?.lastSequence;
5415
+ const history = historyCursorOf(this._history);
5416
+ const rowsLog = [...this._history].reverse().find((item) => item.boxLog !== void 0)?.boxLog;
5417
+ if (typeof payload?.boxLog === "number" && rowsLog !== void 0 && payload.boxLog !== rowsLog || typeof lastSequence === "number" && history !== void 0 && lastSequence < history.sequence) {
5418
+ this.rowsInChannelLog = false;
5419
+ }
5420
+ break;
5421
+ }
5406
5422
  case "backgroundTasks":
5407
5423
  this._backgroundTasks = backgroundTasksOf(payload?.tasks);
5408
5424
  this.emit("backgroundTasks", { tasks: this._backgroundTasks });
@@ -5670,6 +5686,7 @@ var CoreAgentTaskSession = class {
5670
5686
  const text = (id !== void 0 ? this.sentTexts.get(id) : void 0) ?? boxText;
5671
5687
  if (typeof text !== "string" || !text) return;
5672
5688
  const sequence = typeof event.sequence === "number" ? event.sequence : void 0;
5689
+ if ((id === void 0 || !this.sentTexts.has(id)) && this.beforeLoadedRows(sequence)) return;
5673
5690
  this.livePiece(
5674
5691
  (item) => item.kind === "user" && (id !== void 0 && item.clientMessageId === id || sequence !== void 0 && item.sequence === sequence && item.text === text),
5675
5692
  () => ({
@@ -5683,6 +5700,20 @@ var CoreAgentTaskSession = class {
5683
5700
  (item) => item
5684
5701
  );
5685
5702
  }
5703
+ // Every connection to the box brings the running turn's `stepStart` again, and a history loaded
5704
+ // while that turn ran can start after its question (mitra-core-sdk#140: 121 rows after it, a
5705
+ // page of 100). Before the oldest row the history holds, with older rows still on the server,
5706
+ // the question is one of those rows, and `loadOlder` brings it in its place.
5707
+ beforeLoadedRows(sequence) {
5708
+ if (sequence === void 0 || !this._hasOlder || !this.rowsInChannelLog) return false;
5709
+ const rows = this._history.filter(
5710
+ (item) => !item.id.startsWith("live:") && typeof item.sequence === "number"
5711
+ );
5712
+ const oldest = rows[0];
5713
+ const newest = rows[rows.length - 1];
5714
+ if (oldest?.sequence === void 0 || oldest.boxLog !== newest?.boxLog) return false;
5715
+ return sequence < oldest.sequence;
5716
+ }
5686
5717
  liveText(event, payload, kind, parentToolId) {
5687
5718
  const itemId = typeof payload?.itemId === "string" ? payload.itemId : void 0;
5688
5719
  if (itemId === void 0) return;
package/dist/index.js CHANGED
@@ -3883,12 +3883,14 @@ var AgentDirectChannel = class {
3883
3883
  const otherLog = historyCursor?.boxLog !== void 0 && channel.boxLog !== void 0 && historyCursor.boxLog !== channel.boxLog;
3884
3884
  const historyFrom = historyCursor === void 0 ? void 0 : otherLog ? 0 : historyCursor.sequence;
3885
3885
  let cursor = historyFrom ?? channel.lastSequence;
3886
+ let boxLog = channel.boxLog;
3886
3887
  const abort = new AbortController();
3887
3888
  const onAbort = () => abort.abort(signal?.reason);
3888
3889
  signal?.addEventListener("abort", onAbort, { once: true });
3889
- const onFrame = (event) => {
3890
- const hello = helloOf(event);
3890
+ const onFrame = (frame) => {
3891
+ const hello = helloOf(frame);
3891
3892
  if (hello) thread = hello.threadId;
3893
+ const event = hello && boxLog !== void 0 ? { ...frame, payload: { ...asObject(frame.payload), boxLog } } : frame;
3892
3894
  if (typeof event.sequence === "number" && event.sequence > cursor) cursor = event.sequence;
3893
3895
  if (isSubagentFrame(event)) {
3894
3896
  observer.onEvent(event);
@@ -3919,6 +3921,7 @@ var AgentDirectChannel = class {
3919
3921
  onFrame,
3920
3922
  onLost,
3921
3923
  replayFrom: (next) => {
3924
+ boxLog = next.boxLog;
3922
3925
  const decide = (hello) => {
3923
3926
  const nextBox = boxAddress(next.wsUrl);
3924
3927
  const zeroed = next.lastSequence === 0 && cursor > 0;
@@ -4611,6 +4614,9 @@ var CoreAgentTaskSession = class {
4611
4614
  _hasOlder = false;
4612
4615
  // Moves whenever `history` is replaced, so a `loadOlder` still reading drops what it read.
4613
4616
  historyGeneration = 0;
4617
+ // Whether the box the channel follows counts in the log of the history's rows. A box whose hello
4618
+ // names another log, or reports a log shorter than the history's, has sequences that start over.
4619
+ rowsInChannelLog = true;
4614
4620
  olderRead = null;
4615
4621
  get taskId() {
4616
4622
  return this._taskId;
@@ -4751,6 +4757,7 @@ var CoreAgentTaskSession = class {
4751
4757
  }
4752
4758
  replaceHistory(page) {
4753
4759
  this.historyGeneration += 1;
4760
+ this.rowsInChannelLog = true;
4754
4761
  this._hasOlder = hasOlderRows(page);
4755
4762
  this._history = [...page.content].reverse().map(toAgentTimelineItem);
4756
4763
  }
@@ -5331,6 +5338,15 @@ var CoreAgentTaskSession = class {
5331
5338
  ...typeof lifecycle?.clientMessageId === "string" ? { clientMessageId: lifecycle.clientMessageId } : {}
5332
5339
  });
5333
5340
  break;
5341
+ case "hello": {
5342
+ const lastSequence = payload?.lastSequence;
5343
+ const history = historyCursorOf(this._history);
5344
+ const rowsLog = [...this._history].reverse().find((item) => item.boxLog !== void 0)?.boxLog;
5345
+ if (typeof payload?.boxLog === "number" && rowsLog !== void 0 && payload.boxLog !== rowsLog || typeof lastSequence === "number" && history !== void 0 && lastSequence < history.sequence) {
5346
+ this.rowsInChannelLog = false;
5347
+ }
5348
+ break;
5349
+ }
5334
5350
  case "backgroundTasks":
5335
5351
  this._backgroundTasks = backgroundTasksOf(payload?.tasks);
5336
5352
  this.emit("backgroundTasks", { tasks: this._backgroundTasks });
@@ -5598,6 +5614,7 @@ var CoreAgentTaskSession = class {
5598
5614
  const text = (id !== void 0 ? this.sentTexts.get(id) : void 0) ?? boxText;
5599
5615
  if (typeof text !== "string" || !text) return;
5600
5616
  const sequence = typeof event.sequence === "number" ? event.sequence : void 0;
5617
+ if ((id === void 0 || !this.sentTexts.has(id)) && this.beforeLoadedRows(sequence)) return;
5601
5618
  this.livePiece(
5602
5619
  (item) => item.kind === "user" && (id !== void 0 && item.clientMessageId === id || sequence !== void 0 && item.sequence === sequence && item.text === text),
5603
5620
  () => ({
@@ -5611,6 +5628,20 @@ var CoreAgentTaskSession = class {
5611
5628
  (item) => item
5612
5629
  );
5613
5630
  }
5631
+ // Every connection to the box brings the running turn's `stepStart` again, and a history loaded
5632
+ // while that turn ran can start after its question (mitra-core-sdk#140: 121 rows after it, a
5633
+ // page of 100). Before the oldest row the history holds, with older rows still on the server,
5634
+ // the question is one of those rows, and `loadOlder` brings it in its place.
5635
+ beforeLoadedRows(sequence) {
5636
+ if (sequence === void 0 || !this._hasOlder || !this.rowsInChannelLog) return false;
5637
+ const rows = this._history.filter(
5638
+ (item) => !item.id.startsWith("live:") && typeof item.sequence === "number"
5639
+ );
5640
+ const oldest = rows[0];
5641
+ const newest = rows[rows.length - 1];
5642
+ if (oldest?.sequence === void 0 || oldest.boxLog !== newest?.boxLog) return false;
5643
+ return sequence < oldest.sequence;
5644
+ }
5614
5645
  liveText(event, payload, kind, parentToolId) {
5615
5646
  const itemId = typeof payload?.itemId === "string" ? payload.itemId : void 0;
5616
5647
  if (itemId === void 0) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitralab.io/sdk-core",
3
- "version": "0.3.5",
3
+ "version": "0.3.6",
4
4
  "description": "Environment-neutral contracts and modules shared by Mitra JavaScript SDKs",
5
5
  "type": "module",
6
6
  "sideEffects": false,