@mitralab.io/sdk-core 0.3.5 → 0.3.7

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
@@ -1719,6 +1719,7 @@ function createAgentTasksModule(transport, errors = defaultSdkCoreErrorFactory)
1719
1719
  size: options.size,
1720
1720
  sort: options.sort,
1721
1721
  kinds: options.kinds,
1722
+ include: options.include,
1722
1723
  before: options.before
1723
1724
  }
1724
1725
  }),
@@ -3883,12 +3884,14 @@ var AgentDirectChannel = class {
3883
3884
  const otherLog = historyCursor?.boxLog !== void 0 && channel.boxLog !== void 0 && historyCursor.boxLog !== channel.boxLog;
3884
3885
  const historyFrom = historyCursor === void 0 ? void 0 : otherLog ? 0 : historyCursor.sequence;
3885
3886
  let cursor = historyFrom ?? channel.lastSequence;
3887
+ let boxLog = channel.boxLog;
3886
3888
  const abort = new AbortController();
3887
3889
  const onAbort = () => abort.abort(signal?.reason);
3888
3890
  signal?.addEventListener("abort", onAbort, { once: true });
3889
- const onFrame = (event) => {
3890
- const hello = helloOf(event);
3891
+ const onFrame = (frame) => {
3892
+ const hello = helloOf(frame);
3891
3893
  if (hello) thread = hello.threadId;
3894
+ const event = hello && boxLog !== void 0 ? { ...frame, payload: { ...asObject(frame.payload), boxLog } } : frame;
3892
3895
  if (typeof event.sequence === "number" && event.sequence > cursor) cursor = event.sequence;
3893
3896
  if (isSubagentFrame(event)) {
3894
3897
  observer.onEvent(event);
@@ -3919,6 +3922,7 @@ var AgentDirectChannel = class {
3919
3922
  onFrame,
3920
3923
  onLost,
3921
3924
  replayFrom: (next) => {
3925
+ boxLog = next.boxLog;
3922
3926
  const decide = (hello) => {
3923
3927
  const nextBox = boxAddress(next.wsUrl);
3924
3928
  const zeroed = next.lastSequence === 0 && cursor > 0;
@@ -4425,12 +4429,22 @@ function isChatRow(message) {
4425
4429
  var REQUEST_ROW_KINDS = {
4426
4430
  APPROVAL: "approval",
4427
4431
  USER_INPUT_REQUEST: "userInputRequest",
4428
- NOTIFICATION: "notification"
4432
+ NOTIFICATION: "notification",
4433
+ PLAN: "plan"
4429
4434
  };
4430
4435
  function notificationFieldsOf(payload) {
4431
4436
  const { source, outcome, summary, detail } = payload;
4432
4437
  return { source, outcome, summary, ...detail !== void 0 ? { detail } : {} };
4433
4438
  }
4439
+ function planFieldsOf(payload) {
4440
+ const { planId, steps, explanation, status } = payload;
4441
+ return {
4442
+ planId: planId ?? null,
4443
+ steps: Array.isArray(steps) ? steps : [],
4444
+ ...explanation !== void 0 ? { explanation } : {},
4445
+ status: status ?? null
4446
+ };
4447
+ }
4434
4448
  function parseJson(text) {
4435
4449
  try {
4436
4450
  return JSON.parse(text);
@@ -4460,7 +4474,10 @@ function toAgentTimelineItem(message) {
4460
4474
  ...timelineIdentityOf(message),
4461
4475
  ...parentToolIdOf(message.parentToolId)
4462
4476
  };
4463
- return requestKind === "notification" ? { ...item, kind: requestKind, ...notificationFieldsOf(payload) } : { ...item, kind: requestKind };
4477
+ if (requestKind === "notification") {
4478
+ return { ...item, kind: requestKind, ...notificationFieldsOf(payload) };
4479
+ }
4480
+ return requestKind === "plan" ? { ...item, kind: requestKind, ...planFieldsOf(payload) } : { ...item, kind: requestKind };
4464
4481
  }
4465
4482
  }
4466
4483
  if (message.type === "TOOL_USE") {
@@ -4611,6 +4628,9 @@ var CoreAgentTaskSession = class {
4611
4628
  _hasOlder = false;
4612
4629
  // Moves whenever `history` is replaced, so a `loadOlder` still reading drops what it read.
4613
4630
  historyGeneration = 0;
4631
+ // Whether the box the channel follows counts in the log of the history's rows. A box whose hello
4632
+ // names another log, or reports a log shorter than the history's, has sequences that start over.
4633
+ rowsInChannelLog = true;
4614
4634
  olderRead = null;
4615
4635
  get taskId() {
4616
4636
  return this._taskId;
@@ -4706,7 +4726,8 @@ var CoreAgentTaskSession = class {
4706
4726
  const page = await this.dependencies.tasks.listMessages(this._taskId, {
4707
4727
  ...options.limit !== void 0 ? { size: options.limit } : {},
4708
4728
  sort: "createdAt,desc",
4709
- kinds: "all"
4729
+ kinds: "all",
4730
+ include: "plan"
4710
4731
  });
4711
4732
  this.historyLimit = options.limit;
4712
4733
  this.replaceHistory(page);
@@ -4736,7 +4757,8 @@ var CoreAgentTaskSession = class {
4736
4757
  before: oldest.id,
4737
4758
  ...this.historyLimit !== void 0 ? { size: this.historyLimit } : {},
4738
4759
  sort: "createdAt,desc",
4739
- kinds: "all"
4760
+ kinds: "all",
4761
+ include: "plan"
4740
4762
  });
4741
4763
  } catch (error) {
4742
4764
  if (generation !== this.historyGeneration || this.isClosed()) return [];
@@ -4751,6 +4773,7 @@ var CoreAgentTaskSession = class {
4751
4773
  }
4752
4774
  replaceHistory(page) {
4753
4775
  this.historyGeneration += 1;
4776
+ this.rowsInChannelLog = true;
4754
4777
  this._hasOlder = hasOlderRows(page);
4755
4778
  this._history = [...page.content].reverse().map(toAgentTimelineItem);
4756
4779
  }
@@ -5168,7 +5191,8 @@ var CoreAgentTaskSession = class {
5168
5191
  const page = await this.dependencies.tasks.listMessages(this._taskId, {
5169
5192
  size: 100,
5170
5193
  sort: "createdAt,desc",
5171
- kinds: "all"
5194
+ kinds: "all",
5195
+ include: "plan"
5172
5196
  });
5173
5197
  this.turnBaselineIds = new Set(page.content.map((message) => message.id));
5174
5198
  }
@@ -5177,7 +5201,8 @@ var CoreAgentTaskSession = class {
5177
5201
  const page = await this.dependencies.tasks.listMessages(this._taskId, {
5178
5202
  size: 100,
5179
5203
  sort: "createdAt,desc",
5180
- kinds: "all"
5204
+ kinds: "all",
5205
+ include: "plan"
5181
5206
  });
5182
5207
  this.replaceHistory(page);
5183
5208
  this.emit("historyLoaded", { history: this.history });
@@ -5331,6 +5356,15 @@ var CoreAgentTaskSession = class {
5331
5356
  ...typeof lifecycle?.clientMessageId === "string" ? { clientMessageId: lifecycle.clientMessageId } : {}
5332
5357
  });
5333
5358
  break;
5359
+ case "hello": {
5360
+ const lastSequence = payload?.lastSequence;
5361
+ const history = historyCursorOf(this._history);
5362
+ const rowsLog = [...this._history].reverse().find((item) => item.boxLog !== void 0)?.boxLog;
5363
+ if (typeof payload?.boxLog === "number" && rowsLog !== void 0 && payload.boxLog !== rowsLog || typeof lastSequence === "number" && history !== void 0 && lastSequence < history.sequence) {
5364
+ this.rowsInChannelLog = false;
5365
+ }
5366
+ break;
5367
+ }
5334
5368
  case "backgroundTasks":
5335
5369
  this._backgroundTasks = backgroundTasksOf(payload?.tasks);
5336
5370
  this.emit("backgroundTasks", { tasks: this._backgroundTasks });
@@ -5419,6 +5453,15 @@ var CoreAgentTaskSession = class {
5419
5453
  });
5420
5454
  }
5421
5455
  break;
5456
+ case "plan":
5457
+ if (payload && this.livePlan(event, payload)) {
5458
+ this.emit("plan", {
5459
+ ...payload,
5460
+ ...planFieldsOf(payload),
5461
+ ...pieceIdentityOf(event, payload)
5462
+ });
5463
+ }
5464
+ break;
5422
5465
  default:
5423
5466
  break;
5424
5467
  }
@@ -5598,6 +5641,7 @@ var CoreAgentTaskSession = class {
5598
5641
  const text = (id !== void 0 ? this.sentTexts.get(id) : void 0) ?? boxText;
5599
5642
  if (typeof text !== "string" || !text) return;
5600
5643
  const sequence = typeof event.sequence === "number" ? event.sequence : void 0;
5644
+ if ((id === void 0 || !this.sentTexts.has(id)) && this.beforeLoadedRows(sequence)) return;
5601
5645
  this.livePiece(
5602
5646
  (item) => item.kind === "user" && (id !== void 0 && item.clientMessageId === id || sequence !== void 0 && item.sequence === sequence && item.text === text),
5603
5647
  () => ({
@@ -5611,6 +5655,20 @@ var CoreAgentTaskSession = class {
5611
5655
  (item) => item
5612
5656
  );
5613
5657
  }
5658
+ // Every connection to the box brings the running turn's `stepStart` again, and a history loaded
5659
+ // while that turn ran can start after its question (mitra-core-sdk#140: 121 rows after it, a
5660
+ // page of 100). Before the oldest row the history holds, with older rows still on the server,
5661
+ // the question is one of those rows, and `loadOlder` brings it in its place.
5662
+ beforeLoadedRows(sequence) {
5663
+ if (sequence === void 0 || !this._hasOlder || !this.rowsInChannelLog) return false;
5664
+ const rows = this._history.filter(
5665
+ (item) => !item.id.startsWith("live:") && typeof item.sequence === "number"
5666
+ );
5667
+ const oldest = rows[0];
5668
+ const newest = rows[rows.length - 1];
5669
+ if (oldest?.sequence === void 0 || oldest.boxLog !== newest?.boxLog) return false;
5670
+ return sequence < oldest.sequence;
5671
+ }
5614
5672
  liveText(event, payload, kind, parentToolId) {
5615
5673
  const itemId = typeof payload?.itemId === "string" ? payload.itemId : void 0;
5616
5674
  if (itemId === void 0) return;
@@ -5681,6 +5739,44 @@ var CoreAgentTaskSession = class {
5681
5739
  (item) => item
5682
5740
  );
5683
5741
  }
5742
+ // T3 sends the list whole on each update: the item takes the last one where it first came, as
5743
+ // the Copilot's row does. An update the item already holds (a replay after the history) leaves
5744
+ // it as it is and answers false.
5745
+ livePlan(event, payload) {
5746
+ const itemId = typeof payload.itemId === "string" ? payload.itemId : void 0;
5747
+ const sequence = typeof event.sequence === "number" ? event.sequence : void 0;
5748
+ if (itemId === void 0 && sequence === void 0) return true;
5749
+ let applied = true;
5750
+ this.livePiece(
5751
+ (item) => item.kind === "plan" && (itemId !== void 0 ? item.itemId === itemId : item.sequence === sequence),
5752
+ () => ({
5753
+ id: `live:plan:${itemId ?? sequence}`,
5754
+ kind: "plan",
5755
+ payload,
5756
+ ...planFieldsOf(payload),
5757
+ at: liveAtOf(event),
5758
+ ...sequence === void 0 ? {} : { sequence },
5759
+ ...itemId === void 0 ? {} : { itemId }
5760
+ }),
5761
+ (item) => {
5762
+ const held = item.lastSequence ?? item.sequence;
5763
+ if (sequence !== void 0 && held !== void 0 && sequence <= held) {
5764
+ applied = false;
5765
+ return item;
5766
+ }
5767
+ const fields = planFieldsOf(payload);
5768
+ const next = {
5769
+ ...item,
5770
+ payload,
5771
+ ...fields,
5772
+ ...sequence === void 0 ? {} : { lastSequence: sequence }
5773
+ };
5774
+ if (fields.explanation === void 0) delete next.explanation;
5775
+ return next;
5776
+ }
5777
+ );
5778
+ return applied;
5779
+ }
5684
5780
  // A child thread's frames are the subagent's conversation: they open no turn and settle no
5685
5781
  // message. Its text and tools still reach `delta`, `content` and `tool` as they did in 0.2.13,
5686
5782
  // which knew no subagents, so an app that only bumps the version sees what it saw before.
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.7",
4
4
  "description": "Environment-neutral contracts and modules shared by Mitra JavaScript SDKs",
5
5
  "type": "module",
6
6
  "sideEffects": false,