@mitralab.io/sdk-core 0.3.4 → 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
  }
@@ -4952,6 +4952,9 @@ var CoreAgentTaskSession = class {
4952
4952
  if (this.isClosed()) return this.handOverAfterClose(input, clientMessageId, waiter);
4953
4953
  await this.captureTurnBaseline();
4954
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
+ }
4955
4958
  this._content = "";
4956
4959
  this.recoveryUsed = false;
4957
4960
  this.recoveredTerminalReason = void 0;
@@ -4969,6 +4972,37 @@ var CoreAgentTaskSession = class {
4969
4972
  this.acceptOnce(clientMessageId);
4970
4973
  return true;
4971
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
+ }
4972
5006
  // A prompt whose task exists is never dropped by close; the task outlives the session. Apps
4973
5007
  // close the creating session inside taskCreated and reopen by task id, so the input still goes
4974
5008
  // out over REST. Only a prompt that never reached `handOver` comes here: the Copilot does not
@@ -5064,6 +5098,23 @@ var CoreAgentTaskSession = class {
5064
5098
  this.steers = this.steers.filter((pending) => pending !== steered);
5065
5099
  this.strayedSteers.push(steered);
5066
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
+ }
5067
5118
  forgetSteer(steered) {
5068
5119
  this.steers = this.steers.filter((pending) => pending !== steered);
5069
5120
  this.strayedSteers = this.strayedSteers.filter((pending) => pending !== steered);
@@ -5291,6 +5342,8 @@ var CoreAgentTaskSession = class {
5291
5342
  if (id === this.openingMessageId || id === void 0 && event.type === "stepStart") {
5292
5343
  this.openingAwaitsStart = false;
5293
5344
  this.forgetFinishedTurn(lifecycle);
5345
+ } else if (event.type === "stepStart" && this.openingAwaitsStart) {
5346
+ this.openingQueuedBehind();
5294
5347
  }
5295
5348
  } else if (this.openingAwaitsStart && TURN_TAIL_TYPES.has(event.type) && typeof lifecycle?.turnId === "string") {
5296
5349
  this.rememberFinishedTurn(lifecycle);
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;
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;
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
  }
@@ -4880,6 +4880,9 @@ var CoreAgentTaskSession = class {
4880
4880
  if (this.isClosed()) return this.handOverAfterClose(input, clientMessageId, waiter);
4881
4881
  await this.captureTurnBaseline();
4882
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
+ }
4883
4886
  this._content = "";
4884
4887
  this.recoveryUsed = false;
4885
4888
  this.recoveredTerminalReason = void 0;
@@ -4897,6 +4900,37 @@ var CoreAgentTaskSession = class {
4897
4900
  this.acceptOnce(clientMessageId);
4898
4901
  return true;
4899
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
+ }
4900
4934
  // A prompt whose task exists is never dropped by close; the task outlives the session. Apps
4901
4935
  // close the creating session inside taskCreated and reopen by task id, so the input still goes
4902
4936
  // out over REST. Only a prompt that never reached `handOver` comes here: the Copilot does not
@@ -4992,6 +5026,23 @@ var CoreAgentTaskSession = class {
4992
5026
  this.steers = this.steers.filter((pending) => pending !== steered);
4993
5027
  this.strayedSteers.push(steered);
4994
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
+ }
4995
5046
  forgetSteer(steered) {
4996
5047
  this.steers = this.steers.filter((pending) => pending !== steered);
4997
5048
  this.strayedSteers = this.strayedSteers.filter((pending) => pending !== steered);
@@ -5219,6 +5270,8 @@ var CoreAgentTaskSession = class {
5219
5270
  if (id === this.openingMessageId || id === void 0 && event.type === "stepStart") {
5220
5271
  this.openingAwaitsStart = false;
5221
5272
  this.forgetFinishedTurn(lifecycle);
5273
+ } else if (event.type === "stepStart" && this.openingAwaitsStart) {
5274
+ this.openingQueuedBehind();
5222
5275
  }
5223
5276
  } else if (this.openingAwaitsStart && TURN_TAIL_TYPES.has(event.type) && typeof lifecycle?.turnId === "string") {
5224
5277
  this.rememberFinishedTurn(lifecycle);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitralab.io/sdk-core",
3
- "version": "0.3.4",
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,