@mitralab.io/sdk-core 0.3.3 → 0.3.5

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
@@ -407,7 +407,7 @@ function expectFunctionVersion(value, context, errors) {
407
407
  if (typeof version.id !== "string") invalidField(context, "id", errors);
408
408
  if (typeof version.functionId !== "string") invalidField(context, "functionId", errors);
409
409
  if (typeof version.status !== "string") invalidField(context, "status", errors);
410
- if (typeof version.code !== "string") invalidField(context, "code", errors);
410
+ if (!isNullableString(version.code)) invalidField(context, "code", errors);
411
411
  if (version.inputSchema !== null && !isObject(version.inputSchema)) {
412
412
  invalidField(context, "inputSchema", errors);
413
413
  }
@@ -4377,6 +4377,7 @@ var TURN_TAIL_TYPES = /* @__PURE__ */ new Set([
4377
4377
  "toolResult",
4378
4378
  "stepFinish"
4379
4379
  ]);
4380
+ var STATE_FRAME_TYPES = /* @__PURE__ */ new Set(["providerUsage", "backgroundTasks"]);
4380
4381
  var COPILOT_INPUT_TYPES = /* @__PURE__ */ new Set([
4381
4382
  "message",
4382
4383
  "interrupt",
@@ -4498,6 +4499,10 @@ var REQUEST_ROW_KINDS = {
4498
4499
  USER_INPUT_REQUEST: "userInputRequest",
4499
4500
  NOTIFICATION: "notification"
4500
4501
  };
4502
+ function notificationFieldsOf(payload) {
4503
+ const { source, outcome, summary, detail } = payload;
4504
+ return { source, outcome, summary, ...detail !== void 0 ? { detail } : {} };
4505
+ }
4501
4506
  function parseJson(text) {
4502
4507
  try {
4503
4508
  return JSON.parse(text);
@@ -4520,14 +4525,14 @@ function toAgentTimelineItem(message) {
4520
4525
  if (requestKind) {
4521
4526
  const payload = asObject2(parseJson(message.content));
4522
4527
  if (payload) {
4523
- return {
4528
+ const item = {
4524
4529
  id: message.id,
4525
- kind: requestKind,
4526
4530
  payload,
4527
4531
  at: message.createdAt,
4528
4532
  ...timelineIdentityOf(message),
4529
4533
  ...parentToolIdOf(message.parentToolId)
4530
4534
  };
4535
+ return requestKind === "notification" ? { ...item, kind: requestKind, ...notificationFieldsOf(payload) } : { ...item, kind: requestKind };
4531
4536
  }
4532
4537
  }
4533
4538
  if (message.type === "TOOL_USE") {
@@ -4947,6 +4952,9 @@ var CoreAgentTaskSession = class {
4947
4952
  if (this.isClosed()) return this.handOverAfterClose(input, clientMessageId, waiter);
4948
4953
  await this.captureTurnBaseline();
4949
4954
  if (this.isClosed()) return this.handOverAfterClose(input, clientMessageId, waiter);
4955
+ if (this._status === "streaming" || this._status === "cancelled") {
4956
+ return this.sendBehindRunningTurn(input, clientMessageId, handedOver, waiter);
4957
+ }
4950
4958
  this._content = "";
4951
4959
  this.recoveryUsed = false;
4952
4960
  this.recoveredTerminalReason = void 0;
@@ -4964,6 +4972,37 @@ var CoreAgentTaskSession = class {
4964
4972
  this.acceptOnce(clientMessageId);
4965
4973
  return true;
4966
4974
  }
4975
+ async sendBehindRunningTurn(input, clientMessageId, handedOver, waiter) {
4976
+ const steered = {
4977
+ clientMessageId,
4978
+ ...waiter ? { waiter } : {},
4979
+ taken: false
4980
+ };
4981
+ this.steers.push(steered);
4982
+ const fail = (error) => {
4983
+ this.forgetSteer(steered);
4984
+ waiter?.reject(error);
4985
+ return false;
4986
+ };
4987
+ let taken;
4988
+ try {
4989
+ const { admitted } = await this.handOver(input, clientMessageId, true);
4990
+ handedOver();
4991
+ taken = await admitted;
4992
+ } catch (error) {
4993
+ handedOver();
4994
+ this.emitError("Failed to send Agent prompt", error);
4995
+ return fail(error);
4996
+ }
4997
+ if (!taken) {
4998
+ return fail(
4999
+ steered.refusal ?? new AgentTaskTurnError("The Agent box did not take the message.")
5000
+ );
5001
+ }
5002
+ if (!steered.queued) this.steerTaken(steered);
5003
+ this.acceptOnce(clientMessageId);
5004
+ return true;
5005
+ }
4967
5006
  // A prompt whose task exists is never dropped by close; the task outlives the session. Apps
4968
5007
  // close the creating session inside taskCreated and reopen by task id, so the input still goes
4969
5008
  // out over REST. Only a prompt that never reached `handOver` comes here: the Copilot does not
@@ -5059,6 +5098,23 @@ var CoreAgentTaskSession = class {
5059
5098
  this.steers = this.steers.filter((pending) => pending !== steered);
5060
5099
  this.strayedSteers.push(steered);
5061
5100
  }
5101
+ // Another message's turn started while ours waited for its own: one runs ahead of ours (a tab
5102
+ // that sent to the queue before its socket said a turn was running). Its frames are the running
5103
+ // turn's, not a finished one's, or its terminal is dropped and the turn never ends here. Ours
5104
+ // waits for the turn the box opens for it, as a queued steer does, so that turn answers it.
5105
+ openingQueuedBehind() {
5106
+ const id = this.openingMessageId;
5107
+ this.openingAwaitsStart = false;
5108
+ this.openingMessageId = void 0;
5109
+ if (id === void 0) return;
5110
+ this.strayedSteers.push({
5111
+ clientMessageId: id,
5112
+ ...this.activeWaiter ? { waiter: this.activeWaiter } : {},
5113
+ taken: false,
5114
+ queued: true
5115
+ });
5116
+ this.activeWaiter = void 0;
5117
+ }
5062
5118
  forgetSteer(steered) {
5063
5119
  this.steers = this.steers.filter((pending) => pending !== steered);
5064
5120
  this.strayedSteers = this.strayedSteers.filter((pending) => pending !== steered);
@@ -5286,6 +5342,8 @@ var CoreAgentTaskSession = class {
5286
5342
  if (id === this.openingMessageId || id === void 0 && event.type === "stepStart") {
5287
5343
  this.openingAwaitsStart = false;
5288
5344
  this.forgetFinishedTurn(lifecycle);
5345
+ } else if (event.type === "stepStart" && this.openingAwaitsStart) {
5346
+ this.openingQueuedBehind();
5289
5347
  }
5290
5348
  } else if (this.openingAwaitsStart && TURN_TAIL_TYPES.has(event.type) && typeof lifecycle?.turnId === "string") {
5291
5349
  this.rememberFinishedTurn(lifecycle);
@@ -5544,6 +5602,7 @@ var CoreAgentTaskSession = class {
5544
5602
  }
5545
5603
  repeatsTheRunningTurn(event, lifecycle) {
5546
5604
  if (this._status !== "streaming" && this._status !== "cancelled") return false;
5605
+ if (STATE_FRAME_TYPES.has(event.type)) return false;
5547
5606
  const turnId = lifecycle?.turnId;
5548
5607
  if (typeof turnId !== "string") return false;
5549
5608
  if (typeof event.sequence === "number") {
@@ -5680,15 +5739,17 @@ var CoreAgentTaskSession = class {
5680
5739
  if (itemId === void 0 && sequence === void 0) return;
5681
5740
  this.livePiece(
5682
5741
  (item) => item.kind === kind && (itemId !== void 0 ? item.itemId === itemId : item.sequence === sequence),
5683
- () => ({
5684
- id: `live:${kind}:${itemId ?? sequence}`,
5685
- kind,
5686
- payload,
5687
- at: liveAtOf(event),
5688
- ...sequence === void 0 ? {} : { sequence },
5689
- ...itemId === void 0 ? {} : { itemId },
5690
- ...parentToolIdOf(parentToolId)
5691
- }),
5742
+ () => {
5743
+ const item = {
5744
+ id: `live:${kind}:${itemId ?? sequence}`,
5745
+ payload,
5746
+ at: liveAtOf(event),
5747
+ ...sequence === void 0 ? {} : { sequence },
5748
+ ...itemId === void 0 ? {} : { itemId },
5749
+ ...parentToolIdOf(parentToolId)
5750
+ };
5751
+ return kind === "notification" ? { ...item, kind, ...notificationFieldsOf(payload) } : { ...item, kind };
5752
+ },
5692
5753
  (item) => item
5693
5754
  );
5694
5755
  }
package/dist/index.d.cts CHANGED
@@ -363,7 +363,7 @@ interface FunctionVersion {
363
363
  id: string;
364
364
  functionId: string;
365
365
  status: string;
366
- code: string;
366
+ code: string | null;
367
367
  inputSchema: Record<string, unknown> | null;
368
368
  outputSchema: Record<string, unknown> | null;
369
369
  secrets: string[] | null;
@@ -1232,7 +1232,7 @@ interface AgentMessage {
1232
1232
  sender: string;
1233
1233
  /**
1234
1234
  * `TEXT`, `ERROR`, `TOOL_USE`; asked with `kinds: "all"`, also `REASONING`, `SUBAGENT_TEXT`,
1235
- * `APPROVAL` and `USER_INPUT_REQUEST`.
1235
+ * `APPROVAL`, `USER_INPUT_REQUEST` and `NOTIFICATION`.
1236
1236
  */
1237
1237
  type: string;
1238
1238
  content: string;
@@ -2442,14 +2442,14 @@ type AgentTimelineItem = ({
2442
2442
  id: string;
2443
2443
  /**
2444
2444
  * Background work that ended (a command, a subagent, a monitor), the item T3 puts in the turn
2445
- * the provider opens for it (`NOTIFICATION` row): the box's `notification` frame as it came,
2446
- * with `source`, `outcome`, `summary` and `detail`.
2445
+ * the provider opens for it (`NOTIFICATION` row): `source`, `outcome`, `summary` and `detail`
2446
+ * as the live `notification` event carries them, and the box's frame as it came in `payload`.
2447
2447
  */
2448
2448
  kind: "notification";
2449
2449
  payload: Record<string, unknown>;
2450
2450
  at: string;
2451
2451
  parentToolId?: string;
2452
- } & AgentTimelineItemIdentity) | ({
2452
+ } & Pick<AgentNotification, "source" | "outcome" | "summary" | "detail"> & AgentTimelineItemIdentity) | ({
2453
2453
  id: string;
2454
2454
  kind: "tool";
2455
2455
  tool: AgentToolEvent;
package/dist/index.d.ts CHANGED
@@ -363,7 +363,7 @@ interface FunctionVersion {
363
363
  id: string;
364
364
  functionId: string;
365
365
  status: string;
366
- code: string;
366
+ code: string | null;
367
367
  inputSchema: Record<string, unknown> | null;
368
368
  outputSchema: Record<string, unknown> | null;
369
369
  secrets: string[] | null;
@@ -1232,7 +1232,7 @@ interface AgentMessage {
1232
1232
  sender: string;
1233
1233
  /**
1234
1234
  * `TEXT`, `ERROR`, `TOOL_USE`; asked with `kinds: "all"`, also `REASONING`, `SUBAGENT_TEXT`,
1235
- * `APPROVAL` and `USER_INPUT_REQUEST`.
1235
+ * `APPROVAL`, `USER_INPUT_REQUEST` and `NOTIFICATION`.
1236
1236
  */
1237
1237
  type: string;
1238
1238
  content: string;
@@ -2442,14 +2442,14 @@ type AgentTimelineItem = ({
2442
2442
  id: string;
2443
2443
  /**
2444
2444
  * Background work that ended (a command, a subagent, a monitor), the item T3 puts in the turn
2445
- * the provider opens for it (`NOTIFICATION` row): the box's `notification` frame as it came,
2446
- * with `source`, `outcome`, `summary` and `detail`.
2445
+ * the provider opens for it (`NOTIFICATION` row): `source`, `outcome`, `summary` and `detail`
2446
+ * as the live `notification` event carries them, and the box's frame as it came in `payload`.
2447
2447
  */
2448
2448
  kind: "notification";
2449
2449
  payload: Record<string, unknown>;
2450
2450
  at: string;
2451
2451
  parentToolId?: string;
2452
- } & AgentTimelineItemIdentity) | ({
2452
+ } & Pick<AgentNotification, "source" | "outcome" | "summary" | "detail"> & AgentTimelineItemIdentity) | ({
2453
2453
  id: string;
2454
2454
  kind: "tool";
2455
2455
  tool: AgentToolEvent;
package/dist/index.js CHANGED
@@ -335,7 +335,7 @@ function expectFunctionVersion(value, context, errors) {
335
335
  if (typeof version.id !== "string") invalidField(context, "id", errors);
336
336
  if (typeof version.functionId !== "string") invalidField(context, "functionId", errors);
337
337
  if (typeof version.status !== "string") invalidField(context, "status", errors);
338
- if (typeof version.code !== "string") invalidField(context, "code", errors);
338
+ if (!isNullableString(version.code)) invalidField(context, "code", errors);
339
339
  if (version.inputSchema !== null && !isObject(version.inputSchema)) {
340
340
  invalidField(context, "inputSchema", errors);
341
341
  }
@@ -4305,6 +4305,7 @@ var TURN_TAIL_TYPES = /* @__PURE__ */ new Set([
4305
4305
  "toolResult",
4306
4306
  "stepFinish"
4307
4307
  ]);
4308
+ var STATE_FRAME_TYPES = /* @__PURE__ */ new Set(["providerUsage", "backgroundTasks"]);
4308
4309
  var COPILOT_INPUT_TYPES = /* @__PURE__ */ new Set([
4309
4310
  "message",
4310
4311
  "interrupt",
@@ -4426,6 +4427,10 @@ var REQUEST_ROW_KINDS = {
4426
4427
  USER_INPUT_REQUEST: "userInputRequest",
4427
4428
  NOTIFICATION: "notification"
4428
4429
  };
4430
+ function notificationFieldsOf(payload) {
4431
+ const { source, outcome, summary, detail } = payload;
4432
+ return { source, outcome, summary, ...detail !== void 0 ? { detail } : {} };
4433
+ }
4429
4434
  function parseJson(text) {
4430
4435
  try {
4431
4436
  return JSON.parse(text);
@@ -4448,14 +4453,14 @@ function toAgentTimelineItem(message) {
4448
4453
  if (requestKind) {
4449
4454
  const payload = asObject2(parseJson(message.content));
4450
4455
  if (payload) {
4451
- return {
4456
+ const item = {
4452
4457
  id: message.id,
4453
- kind: requestKind,
4454
4458
  payload,
4455
4459
  at: message.createdAt,
4456
4460
  ...timelineIdentityOf(message),
4457
4461
  ...parentToolIdOf(message.parentToolId)
4458
4462
  };
4463
+ return requestKind === "notification" ? { ...item, kind: requestKind, ...notificationFieldsOf(payload) } : { ...item, kind: requestKind };
4459
4464
  }
4460
4465
  }
4461
4466
  if (message.type === "TOOL_USE") {
@@ -4875,6 +4880,9 @@ var CoreAgentTaskSession = class {
4875
4880
  if (this.isClosed()) return this.handOverAfterClose(input, clientMessageId, waiter);
4876
4881
  await this.captureTurnBaseline();
4877
4882
  if (this.isClosed()) return this.handOverAfterClose(input, clientMessageId, waiter);
4883
+ if (this._status === "streaming" || this._status === "cancelled") {
4884
+ return this.sendBehindRunningTurn(input, clientMessageId, handedOver, waiter);
4885
+ }
4878
4886
  this._content = "";
4879
4887
  this.recoveryUsed = false;
4880
4888
  this.recoveredTerminalReason = void 0;
@@ -4892,6 +4900,37 @@ var CoreAgentTaskSession = class {
4892
4900
  this.acceptOnce(clientMessageId);
4893
4901
  return true;
4894
4902
  }
4903
+ async sendBehindRunningTurn(input, clientMessageId, handedOver, waiter) {
4904
+ const steered = {
4905
+ clientMessageId,
4906
+ ...waiter ? { waiter } : {},
4907
+ taken: false
4908
+ };
4909
+ this.steers.push(steered);
4910
+ const fail = (error) => {
4911
+ this.forgetSteer(steered);
4912
+ waiter?.reject(error);
4913
+ return false;
4914
+ };
4915
+ let taken;
4916
+ try {
4917
+ const { admitted } = await this.handOver(input, clientMessageId, true);
4918
+ handedOver();
4919
+ taken = await admitted;
4920
+ } catch (error) {
4921
+ handedOver();
4922
+ this.emitError("Failed to send Agent prompt", error);
4923
+ return fail(error);
4924
+ }
4925
+ if (!taken) {
4926
+ return fail(
4927
+ steered.refusal ?? new AgentTaskTurnError("The Agent box did not take the message.")
4928
+ );
4929
+ }
4930
+ if (!steered.queued) this.steerTaken(steered);
4931
+ this.acceptOnce(clientMessageId);
4932
+ return true;
4933
+ }
4895
4934
  // A prompt whose task exists is never dropped by close; the task outlives the session. Apps
4896
4935
  // close the creating session inside taskCreated and reopen by task id, so the input still goes
4897
4936
  // out over REST. Only a prompt that never reached `handOver` comes here: the Copilot does not
@@ -4987,6 +5026,23 @@ var CoreAgentTaskSession = class {
4987
5026
  this.steers = this.steers.filter((pending) => pending !== steered);
4988
5027
  this.strayedSteers.push(steered);
4989
5028
  }
5029
+ // Another message's turn started while ours waited for its own: one runs ahead of ours (a tab
5030
+ // that sent to the queue before its socket said a turn was running). Its frames are the running
5031
+ // turn's, not a finished one's, or its terminal is dropped and the turn never ends here. Ours
5032
+ // waits for the turn the box opens for it, as a queued steer does, so that turn answers it.
5033
+ openingQueuedBehind() {
5034
+ const id = this.openingMessageId;
5035
+ this.openingAwaitsStart = false;
5036
+ this.openingMessageId = void 0;
5037
+ if (id === void 0) return;
5038
+ this.strayedSteers.push({
5039
+ clientMessageId: id,
5040
+ ...this.activeWaiter ? { waiter: this.activeWaiter } : {},
5041
+ taken: false,
5042
+ queued: true
5043
+ });
5044
+ this.activeWaiter = void 0;
5045
+ }
4990
5046
  forgetSteer(steered) {
4991
5047
  this.steers = this.steers.filter((pending) => pending !== steered);
4992
5048
  this.strayedSteers = this.strayedSteers.filter((pending) => pending !== steered);
@@ -5214,6 +5270,8 @@ var CoreAgentTaskSession = class {
5214
5270
  if (id === this.openingMessageId || id === void 0 && event.type === "stepStart") {
5215
5271
  this.openingAwaitsStart = false;
5216
5272
  this.forgetFinishedTurn(lifecycle);
5273
+ } else if (event.type === "stepStart" && this.openingAwaitsStart) {
5274
+ this.openingQueuedBehind();
5217
5275
  }
5218
5276
  } else if (this.openingAwaitsStart && TURN_TAIL_TYPES.has(event.type) && typeof lifecycle?.turnId === "string") {
5219
5277
  this.rememberFinishedTurn(lifecycle);
@@ -5472,6 +5530,7 @@ var CoreAgentTaskSession = class {
5472
5530
  }
5473
5531
  repeatsTheRunningTurn(event, lifecycle) {
5474
5532
  if (this._status !== "streaming" && this._status !== "cancelled") return false;
5533
+ if (STATE_FRAME_TYPES.has(event.type)) return false;
5475
5534
  const turnId = lifecycle?.turnId;
5476
5535
  if (typeof turnId !== "string") return false;
5477
5536
  if (typeof event.sequence === "number") {
@@ -5608,15 +5667,17 @@ var CoreAgentTaskSession = class {
5608
5667
  if (itemId === void 0 && sequence === void 0) return;
5609
5668
  this.livePiece(
5610
5669
  (item) => item.kind === kind && (itemId !== void 0 ? item.itemId === itemId : item.sequence === sequence),
5611
- () => ({
5612
- id: `live:${kind}:${itemId ?? sequence}`,
5613
- kind,
5614
- payload,
5615
- at: liveAtOf(event),
5616
- ...sequence === void 0 ? {} : { sequence },
5617
- ...itemId === void 0 ? {} : { itemId },
5618
- ...parentToolIdOf(parentToolId)
5619
- }),
5670
+ () => {
5671
+ const item = {
5672
+ id: `live:${kind}:${itemId ?? sequence}`,
5673
+ payload,
5674
+ at: liveAtOf(event),
5675
+ ...sequence === void 0 ? {} : { sequence },
5676
+ ...itemId === void 0 ? {} : { itemId },
5677
+ ...parentToolIdOf(parentToolId)
5678
+ };
5679
+ return kind === "notification" ? { ...item, kind, ...notificationFieldsOf(payload) } : { ...item, kind };
5680
+ },
5620
5681
  (item) => item
5621
5682
  );
5622
5683
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitralab.io/sdk-core",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "Environment-neutral contracts and modules shared by Mitra JavaScript SDKs",
5
5
  "type": "module",
6
6
  "sideEffects": false,