@iota-uz/sdk 0.4.39 → 0.4.41

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.
@@ -4265,6 +4265,7 @@ declare class ChatMachine {
4265
4265
  private _ensureSessionSyncAfterStream;
4266
4266
  private _finalizeSuccessfulSend;
4267
4267
  private _handleSendError;
4268
+ private _preservePartialAssistant;
4268
4269
  /**
4269
4270
  * Public entry point (no options). Calls _sendMessageCore internally.
4270
4271
  */
@@ -4265,6 +4265,7 @@ declare class ChatMachine {
4265
4265
  private _ensureSessionSyncAfterStream;
4266
4266
  private _finalizeSuccessfulSend;
4267
4267
  private _handleSendError;
4268
+ private _preservePartialAssistant;
4268
4269
  /**
4269
4270
  * Public entry point (no options). Calls _sendMessageCore internally.
4270
4271
  */
@@ -1169,6 +1169,15 @@ var RateLimiter = class {
1169
1169
  }
1170
1170
  };
1171
1171
 
1172
+ // ui/src/bichat/types/index.ts
1173
+ var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
1174
+ MessageRole2["User"] = "user";
1175
+ MessageRole2["Assistant"] = "assistant";
1176
+ MessageRole2["System"] = "system";
1177
+ MessageRole2["Tool"] = "tool";
1178
+ return MessageRole2;
1179
+ })(MessageRole || {});
1180
+
1172
1181
  // ui/src/applet-devtools/enabled.ts
1173
1182
  function shouldEnableAppletDevtools() {
1174
1183
  if (typeof window === "undefined") {
@@ -1710,15 +1719,6 @@ function getSessionDebugUsage(turns) {
1710
1719
  };
1711
1720
  }
1712
1721
 
1713
- // ui/src/bichat/types/index.ts
1714
- var MessageRole = /* @__PURE__ */ ((MessageRole2) => {
1715
- MessageRole2["User"] = "user";
1716
- MessageRole2["Assistant"] = "assistant";
1717
- MessageRole2["System"] = "system";
1718
- MessageRole2["Tool"] = "tool";
1719
- return MessageRole2;
1720
- })(MessageRole || {});
1721
-
1722
1722
  // ui/src/bichat/context/chatHelpers.ts
1723
1723
  var ARTIFACT_TOOL_NAMES = /* @__PURE__ */ new Set([
1724
1724
  "code_interpreter",
@@ -2529,6 +2529,25 @@ var ChatMachine = class {
2529
2529
  return;
2530
2530
  }
2531
2531
  const prev = this.state.messaging.turns;
2532
+ const localPartialTurn = prev.slice().reverse().find((turn) => turn.assistantTurn?.id.startsWith("assistant-partial-"));
2533
+ if (localPartialTurn) {
2534
+ if (fetchedTurns.length === 0) {
2535
+ fetchedTurns = prev;
2536
+ } else {
2537
+ const correspondingIndex = fetchedTurns.findIndex(
2538
+ (turn, index) => turn.userTurn.id === localPartialTurn.userTurn.id || index === fetchedTurns.length - 1 && turn.userTurn.content === localPartialTurn.userTurn.content
2539
+ );
2540
+ if (correspondingIndex >= 0) {
2541
+ if (!fetchedTurns[correspondingIndex].assistantTurn) {
2542
+ fetchedTurns = fetchedTurns.map(
2543
+ (turn, index) => index === correspondingIndex ? { ...turn, assistantTurn: localPartialTurn.assistantTurn } : turn
2544
+ );
2545
+ }
2546
+ } else {
2547
+ fetchedTurns = [...fetchedTurns, localPartialTurn];
2548
+ }
2549
+ }
2550
+ }
2532
2551
  const hasPendingUserOnly = prev.length > 0 && !prev[prev.length - 1].assistantTurn;
2533
2552
  const patch = {};
2534
2553
  const effectivePendingQuestion = pendingQuestion === void 0 ? this.state.messaging.pendingQuestion : pendingQuestion;
@@ -2634,8 +2653,9 @@ var ChatMachine = class {
2634
2653
  this.abortController = controller;
2635
2654
  const epoch = this.viewEpoch;
2636
2655
  this._updateMessaging({ isStreaming: true });
2656
+ let accumulatedContent = "";
2637
2657
  try {
2638
- let accumulatedContent = "";
2658
+ let terminalError = null;
2639
2659
  await this.dataSource.resumeStream(
2640
2660
  sessionId,
2641
2661
  runId,
@@ -2655,13 +2675,27 @@ var ChatMachine = class {
2655
2675
  this._handleToolStart(chunk.tool);
2656
2676
  } else if (chunk.type === "tool_end" && chunk.tool) {
2657
2677
  this._handleToolEnd(chunk.tool);
2658
- } else if (chunk.type === "done" || chunk.type === "error") {
2678
+ } else if (chunk.type === "error") {
2679
+ terminalError = new Error(chunk.error || "Stream error");
2659
2680
  }
2660
2681
  },
2661
2682
  controller.signal
2662
2683
  );
2684
+ if (terminalError) {
2685
+ throw terminalError;
2686
+ }
2663
2687
  clearRunMarker(sessionId);
2664
2688
  await this._syncSessionFromServer(sessionId, true, epoch);
2689
+ } catch (err) {
2690
+ if (this._isCurrentEpoch(epoch) && !(err instanceof Error && err.name === "AbortError")) {
2691
+ this._preservePartialAssistant(accumulatedContent);
2692
+ const normalized = normalizeRPCError(err, "Failed to resume stream");
2693
+ this._updateMessaging({
2694
+ streamError: normalized.userMessage,
2695
+ streamErrorRetryable: normalized.retryable
2696
+ });
2697
+ }
2698
+ throw err;
2665
2699
  } finally {
2666
2700
  if (this.abortController === controller) {
2667
2701
  this.abortController = null;
@@ -2944,7 +2978,8 @@ var ChatMachine = class {
2944
2978
  model,
2945
2979
  tempTurnId,
2946
2980
  controller,
2947
- epoch
2981
+ epoch,
2982
+ onContent
2948
2983
  } = params;
2949
2984
  let accumulatedContent = "";
2950
2985
  let createdSessionId;
@@ -2974,6 +3009,7 @@ var ChatMachine = class {
2974
3009
  }
2975
3010
  if ((chunk.type === "chunk" || chunk.type === "content") && chunk.content) {
2976
3011
  accumulatedContent += chunk.content;
3012
+ onContent(accumulatedContent);
2977
3013
  this._updateMessaging({ streamingContent: accumulatedContent });
2978
3014
  } else if (chunk.type === "thinking" && chunk.content) {
2979
3015
  this._handleThinkingChunk(chunk.content);
@@ -3059,7 +3095,7 @@ var ChatMachine = class {
3059
3095
  this._clearStreamError();
3060
3096
  this.lastSendAttempt = null;
3061
3097
  }
3062
- _handleSendError(err, content, tempTurn, epoch) {
3098
+ _handleSendError(err, content, tempTurn, epoch, partialAssistantContent) {
3063
3099
  if (!this._isCurrentEpoch(epoch)) {
3064
3100
  return false;
3065
3101
  }
@@ -3089,13 +3125,52 @@ var ChatMachine = class {
3089
3125
  };
3090
3126
  }
3091
3127
  this._updateInput({ message: "", inputError: null });
3128
+ const turns = this._preservePartialAssistant(
3129
+ partialAssistantContent,
3130
+ tempTurn.id
3131
+ );
3092
3132
  this._updateMessaging({
3133
+ turns,
3093
3134
  streamError: normalized.userMessage,
3094
3135
  streamErrorRetryable: normalized.retryable
3095
3136
  });
3096
3137
  console.error("Send message error:", err);
3097
3138
  return false;
3098
3139
  }
3140
+ _preservePartialAssistant(content, targetTurnId) {
3141
+ if (content.trim() === "") {
3142
+ return this.state.messaging.turns;
3143
+ }
3144
+ let targetIndex = targetTurnId ? this.state.messaging.turns.findIndex((turn) => turn.id === targetTurnId) : -1;
3145
+ if (!targetTurnId) {
3146
+ for (let i = this.state.messaging.turns.length - 1; i >= 0; i--) {
3147
+ if (!this.state.messaging.turns[i].assistantTurn) {
3148
+ targetIndex = i;
3149
+ break;
3150
+ }
3151
+ }
3152
+ }
3153
+ if (targetIndex < 0) {
3154
+ return this.state.messaging.turns;
3155
+ }
3156
+ const turns = this.state.messaging.turns.map(
3157
+ (turn, index) => index === targetIndex ? {
3158
+ ...turn,
3159
+ assistantTurn: {
3160
+ id: generateTempId("assistant-partial"),
3161
+ role: "assistant" /* Assistant */,
3162
+ content,
3163
+ citations: [],
3164
+ artifacts: [],
3165
+ codeOutputs: [],
3166
+ lifecycle: "complete",
3167
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
3168
+ }
3169
+ } : turn
3170
+ );
3171
+ this._updateMessaging({ turns });
3172
+ return turns;
3173
+ }
3099
3174
  // ── Send message ────────────────────────────────────────────────────────
3100
3175
  /**
3101
3176
  * Public entry point (no options). Calls _sendMessageCore internally.
@@ -3164,6 +3239,7 @@ var ChatMachine = class {
3164
3239
  const prevTurns = this.state.messaging.turns;
3165
3240
  this._insertOptimisticTurn(prevTurns, tempTurn, replaceFromMessageID);
3166
3241
  let shouldDrainQueue = true;
3242
+ let partialAssistantContent = "";
3167
3243
  try {
3168
3244
  const { activeSessionId, shouldNavigateAfter } = await this._resolveSendSession(curSessionId, curDebugMode);
3169
3245
  this.sendingSessionId = activeSessionId || null;
@@ -3179,7 +3255,10 @@ var ChatMachine = class {
3179
3255
  model: this.state.session.model,
3180
3256
  tempTurnId: tempTurn.id,
3181
3257
  controller,
3182
- epoch
3258
+ epoch,
3259
+ onContent: (streamedContent) => {
3260
+ partialAssistantContent = streamedContent;
3261
+ }
3183
3262
  });
3184
3263
  if (!this._isCurrentEpoch(epoch)) {
3185
3264
  } else if (stopped) {
@@ -3192,8 +3271,10 @@ var ChatMachine = class {
3192
3271
  this._clearStreamError();
3193
3272
  const syncId = createdSessionId || activeSessionId;
3194
3273
  if (syncId && syncId !== "new") {
3195
- await this._syncSessionFromServer(syncId, true, epoch).catch(() => {
3196
- });
3274
+ await this._syncSessionFromServer(syncId, true, epoch).catch(
3275
+ () => {
3276
+ }
3277
+ );
3197
3278
  }
3198
3279
  } else {
3199
3280
  await this._ensureSessionSyncAfterStream(
@@ -3206,7 +3287,13 @@ var ChatMachine = class {
3206
3287
  this._finalizeSuccessfulSend(targetSessionId, shouldNavigateAfter);
3207
3288
  }
3208
3289
  } catch (err) {
3209
- shouldDrainQueue = this._handleSendError(err, content, tempTurn, epoch);
3290
+ shouldDrainQueue = this._handleSendError(
3291
+ err,
3292
+ content,
3293
+ tempTurn,
3294
+ epoch,
3295
+ partialAssistantContent
3296
+ );
3210
3297
  } finally {
3211
3298
  if (this._isCurrentEpoch(epoch)) {
3212
3299
  this._updateMessaging({