@mitralab.io/sdk-core 0.2.0 → 0.2.1

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
@@ -3158,6 +3158,9 @@ var CoreAgentTaskSession = class {
3158
3158
  createPromise = null;
3159
3159
  dispatching = false;
3160
3160
  recoveryUsed = false;
3161
+ // Turns the server already ended. A box flushes the text it had buffered for a stopped turn
3162
+ // after the interrupted terminal (dev, 2026-09-14); that text must not open a turn of its own.
3163
+ finishedTurnIds = [];
3161
3164
  recoveryPromise = null;
3162
3165
  recoveryGeneration = 0;
3163
3166
  recoveredTerminalReason;
@@ -3222,6 +3225,7 @@ var CoreAgentTaskSession = class {
3222
3225
  if (this._status !== "streaming" && this._status !== "cancelled") return;
3223
3226
  try {
3224
3227
  await this.sendInput({ type: "interrupt" });
3228
+ if (this._status !== "streaming") return;
3225
3229
  this.setStatus("cancelled");
3226
3230
  this.emit("cancelled", {});
3227
3231
  if (this.cancelTimer) clearTimeout(this.cancelTimer);
@@ -3475,7 +3479,11 @@ var CoreAgentTaskSession = class {
3475
3479
  this.emit("raw", event);
3476
3480
  const payload = asObject(event.payload);
3477
3481
  switch (event.type) {
3482
+ // A box transmite o texto do agente ao vivo em `textDelta` e grava o mesmo texto no log
3483
+ // como `textChunk`. Uma repeticao depois de uma queda devolve chunks, entao trata-los como
3484
+ // texto e o que faz a resposta recuperada aparecer; sem isso ela chega e morre aqui.
3478
3485
  case "textDelta":
3486
+ case "textChunk":
3479
3487
  this.consumeDelta(payload, "text");
3480
3488
  break;
3481
3489
  case "thinking":
@@ -3492,7 +3500,14 @@ var CoreAgentTaskSession = class {
3492
3500
  break;
3493
3501
  case "stepFinish": {
3494
3502
  const reason = typeof payload?.reason === "string" ? payload.reason : "unknown";
3503
+ const lifecycle = payload?.lifecycle;
3504
+ if (reason === "interrupted" || lifecycle?.interruptTerminal === true) {
3505
+ this.rememberFinishedTurn(lifecycle);
3506
+ this.finishTurn("interrupted");
3507
+ break;
3508
+ }
3495
3509
  if (reason === "stop" || reason === "endTurn") {
3510
+ this.rememberFinishedTurn(lifecycle);
3496
3511
  if (this.recoveryUsed) {
3497
3512
  this.recoveredTerminalReason = reason;
3498
3513
  if (!this.recoveryPromise) {
@@ -3520,7 +3535,18 @@ var CoreAgentTaskSession = class {
3520
3535
  break;
3521
3536
  }
3522
3537
  }
3538
+ rememberFinishedTurn(lifecycle) {
3539
+ const turnId = typeof lifecycle?.turnId === "string" ? lifecycle.turnId : null;
3540
+ if (!turnId) return;
3541
+ this.finishedTurnIds.push(turnId);
3542
+ if (this.finishedTurnIds.length > 8) this.finishedTurnIds.shift();
3543
+ }
3523
3544
  consumeDelta(payload, kind) {
3545
+ const lifecycle = asObject(payload?.lifecycle);
3546
+ const turnId = typeof lifecycle?.turnId === "string" ? lifecycle.turnId : null;
3547
+ if (turnId && this.finishedTurnIds.includes(turnId)) return;
3548
+ const idle = this._status !== "streaming" && this._status !== "cancelled";
3549
+ if (idle && lifecycle && !turnId) return;
3524
3550
  const text = typeof payload?.text === "string" ? payload.text : "";
3525
3551
  if (this._status !== "streaming" && this._status !== "cancelled") {
3526
3552
  this.setStatus("streaming");
package/dist/index.js CHANGED
@@ -3086,6 +3086,9 @@ var CoreAgentTaskSession = class {
3086
3086
  createPromise = null;
3087
3087
  dispatching = false;
3088
3088
  recoveryUsed = false;
3089
+ // Turns the server already ended. A box flushes the text it had buffered for a stopped turn
3090
+ // after the interrupted terminal (dev, 2026-09-14); that text must not open a turn of its own.
3091
+ finishedTurnIds = [];
3089
3092
  recoveryPromise = null;
3090
3093
  recoveryGeneration = 0;
3091
3094
  recoveredTerminalReason;
@@ -3150,6 +3153,7 @@ var CoreAgentTaskSession = class {
3150
3153
  if (this._status !== "streaming" && this._status !== "cancelled") return;
3151
3154
  try {
3152
3155
  await this.sendInput({ type: "interrupt" });
3156
+ if (this._status !== "streaming") return;
3153
3157
  this.setStatus("cancelled");
3154
3158
  this.emit("cancelled", {});
3155
3159
  if (this.cancelTimer) clearTimeout(this.cancelTimer);
@@ -3403,7 +3407,11 @@ var CoreAgentTaskSession = class {
3403
3407
  this.emit("raw", event);
3404
3408
  const payload = asObject(event.payload);
3405
3409
  switch (event.type) {
3410
+ // A box transmite o texto do agente ao vivo em `textDelta` e grava o mesmo texto no log
3411
+ // como `textChunk`. Uma repeticao depois de uma queda devolve chunks, entao trata-los como
3412
+ // texto e o que faz a resposta recuperada aparecer; sem isso ela chega e morre aqui.
3406
3413
  case "textDelta":
3414
+ case "textChunk":
3407
3415
  this.consumeDelta(payload, "text");
3408
3416
  break;
3409
3417
  case "thinking":
@@ -3420,7 +3428,14 @@ var CoreAgentTaskSession = class {
3420
3428
  break;
3421
3429
  case "stepFinish": {
3422
3430
  const reason = typeof payload?.reason === "string" ? payload.reason : "unknown";
3431
+ const lifecycle = payload?.lifecycle;
3432
+ if (reason === "interrupted" || lifecycle?.interruptTerminal === true) {
3433
+ this.rememberFinishedTurn(lifecycle);
3434
+ this.finishTurn("interrupted");
3435
+ break;
3436
+ }
3423
3437
  if (reason === "stop" || reason === "endTurn") {
3438
+ this.rememberFinishedTurn(lifecycle);
3424
3439
  if (this.recoveryUsed) {
3425
3440
  this.recoveredTerminalReason = reason;
3426
3441
  if (!this.recoveryPromise) {
@@ -3448,7 +3463,18 @@ var CoreAgentTaskSession = class {
3448
3463
  break;
3449
3464
  }
3450
3465
  }
3466
+ rememberFinishedTurn(lifecycle) {
3467
+ const turnId = typeof lifecycle?.turnId === "string" ? lifecycle.turnId : null;
3468
+ if (!turnId) return;
3469
+ this.finishedTurnIds.push(turnId);
3470
+ if (this.finishedTurnIds.length > 8) this.finishedTurnIds.shift();
3471
+ }
3451
3472
  consumeDelta(payload, kind) {
3473
+ const lifecycle = asObject(payload?.lifecycle);
3474
+ const turnId = typeof lifecycle?.turnId === "string" ? lifecycle.turnId : null;
3475
+ if (turnId && this.finishedTurnIds.includes(turnId)) return;
3476
+ const idle = this._status !== "streaming" && this._status !== "cancelled";
3477
+ if (idle && lifecycle && !turnId) return;
3452
3478
  const text = typeof payload?.text === "string" ? payload.text : "";
3453
3479
  if (this._status !== "streaming" && this._status !== "cancelled") {
3454
3480
  this.setStatus("streaming");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mitralab.io/sdk-core",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Environment-neutral contracts and modules shared by Mitra JavaScript SDKs",
5
5
  "type": "module",
6
6
  "sideEffects": false,