@iota-uz/sdk 0.4.24 → 0.4.25

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.
@@ -2442,6 +2442,33 @@ var ChatMachine = class {
2442
2442
  });
2443
2443
  }
2444
2444
  }
2445
+ async _resumeAcceptedRunOrPoll(sessionId, runId) {
2446
+ setRunMarker(sessionId, runId);
2447
+ try {
2448
+ await this._runResumeStream(sessionId, runId);
2449
+ } catch (err) {
2450
+ if (this.disposed) {
2451
+ return;
2452
+ }
2453
+ console.warn("[ChatMachine] resumeStream failed, switching to status polling fallback:", err);
2454
+ const getStreamStatus2 = this.dataSource.getStreamStatus;
2455
+ const status = getStreamStatus2 ? await getStreamStatus2(sessionId).catch(() => null) : null;
2456
+ if (!status?.active) {
2457
+ clearRunMarker(sessionId);
2458
+ await this._syncSessionFromServer(sessionId, true).catch(() => {
2459
+ });
2460
+ this._updateMessaging({ generationInProgress: false });
2461
+ return;
2462
+ }
2463
+ setRunMarker(sessionId, status.runId ?? runId);
2464
+ this._updateMessaging({
2465
+ isStreaming: false,
2466
+ loading: false,
2467
+ generationInProgress: true
2468
+ });
2469
+ this._startPassivePolling(sessionId);
2470
+ }
2471
+ }
2445
2472
  // =====================================================================
2446
2473
  // Private — actions
2447
2474
  // =====================================================================
@@ -2560,20 +2587,28 @@ var ChatMachine = class {
2560
2587
  streamingContent: ""
2561
2588
  });
2562
2589
  try {
2563
- const compactResult = await this.dataSource.compactSessionHistory(curSessionId);
2564
- const summary = compactResult.summary || "";
2590
+ const accepted = await this.dataSource.compactSessionHistory(curSessionId);
2591
+ if (!accepted.runId) {
2592
+ throw new Error("Async compaction run metadata is missing");
2593
+ }
2565
2594
  this._updateMessaging({
2566
- turns: applyTurnLifecycleForPendingQuestion([createCompactedSystemTurn(curSessionId, summary)], null),
2595
+ turns: applyTurnLifecycleForPendingQuestion(
2596
+ [createCompactedSystemTurn(curSessionId, "Compacting conversation history...")],
2597
+ null
2598
+ ),
2567
2599
  pendingQuestion: null
2568
2600
  });
2569
- const result = await this.dataSource.fetchSession(curSessionId);
2570
- if (result) {
2571
- this._updateSession({ session: result.session });
2572
- this._setTurnsFromFetch(result.turns, result.pendingQuestion || null);
2573
- } else {
2574
- this._setTurnsFromFetch([], null);
2601
+ await this._resumeAcceptedRunOrPoll(curSessionId, accepted.runId);
2602
+ if (!this.state.messaging.generationInProgress) {
2603
+ const result = await this.dataSource.fetchSession(curSessionId);
2604
+ if (result) {
2605
+ this._updateSession({ session: result.session });
2606
+ this._setTurnsFromFetch(result.turns, result.pendingQuestion || null);
2607
+ } else {
2608
+ this._setTurnsFromFetch([], null);
2609
+ }
2610
+ this._updateMessaging({ codeOutputs: [] });
2575
2611
  }
2576
- this._updateMessaging({ codeOutputs: [] });
2577
2612
  } catch (err) {
2578
2613
  const normalized = normalizeRPCError(err, "Failed to compact session history");
2579
2614
  this._updateInput({ inputError: normalized.userMessage });
@@ -2738,8 +2773,6 @@ var ChatMachine = class {
2738
2773
  this._notifySessionsUpdated("session_created", targetSessionId);
2739
2774
  if (this.onSessionCreated) {
2740
2775
  this.onSessionCreated(targetSessionId);
2741
- } else {
2742
- this.dataSource.navigateToSession?.(targetSessionId);
2743
2776
  }
2744
2777
  }
2745
2778
  this._clearStreamError();
@@ -3025,9 +3058,24 @@ var ChatMachine = class {
3025
3058
  return;
3026
3059
  }
3027
3060
  if (result.success) {
3061
+ this._updateMessaging({
3062
+ pendingQuestion: null,
3063
+ turns: applyTurnLifecycleForPendingQuestion(this.state.messaging.turns, null)
3064
+ });
3028
3065
  if (result.data) {
3029
- this._updateSession({ session: result.data.session });
3030
- this._setTurnsFromFetch(result.data.turns, result.data.pendingQuestion || null);
3066
+ await this._resumeAcceptedRunOrPoll(curSessionId, result.data.runId);
3067
+ if (!this.state.messaging.generationInProgress) {
3068
+ const fetchResult = await this.dataSource.fetchSession(curSessionId);
3069
+ if (this.disposed) {
3070
+ return;
3071
+ }
3072
+ if (fetchResult) {
3073
+ this._updateSession({ session: fetchResult.session });
3074
+ this._setTurnsFromFetch(fetchResult.turns, fetchResult.pendingQuestion || null);
3075
+ } else {
3076
+ this._updateSession({ error: "Failed to load updated session", errorRetryable: true });
3077
+ }
3078
+ }
3031
3079
  } else if (curSessionId !== "new") {
3032
3080
  const fetchResult = await this.dataSource.fetchSession(curSessionId);
3033
3081
  if (this.disposed) {
@@ -3071,7 +3119,9 @@ var ChatMachine = class {
3071
3119
  pendingQuestion: null,
3072
3120
  turns: applyTurnLifecycleForPendingQuestion(this.state.messaging.turns, null)
3073
3121
  });
3074
- if (curSessionId !== "new") {
3122
+ if (result.data) {
3123
+ await this._resumeAcceptedRunOrPoll(curSessionId, result.data.runId);
3124
+ } else if (curSessionId !== "new") {
3075
3125
  const fetchResult = await this.dataSource.fetchSession(curSessionId);
3076
3126
  if (this.disposed) {
3077
3127
  return;
@@ -16880,9 +16930,10 @@ function useHttpDataSourceConfigFromApplet(options) {
16880
16930
  rpcEndpoint,
16881
16931
  streamEndpoint,
16882
16932
  csrfToken,
16883
- timeout: options?.timeout ?? 12e4
16933
+ rpcTimeoutMs: options?.rpcTimeoutMs ?? 12e4,
16934
+ streamConnectTimeoutMs: options?.streamConnectTimeoutMs ?? 3e4
16884
16935
  };
16885
- }, [options?.timeout]);
16936
+ }, [options?.rpcTimeoutMs, options?.streamConnectTimeoutMs]);
16886
16937
  }
16887
16938
  var SESSION_PATH_REGEX = /\/session\/([^/]+)/;
16888
16939
  function useBichatRouter({
@@ -18048,7 +18099,23 @@ async function clearSessionHistory(callRPC, sessionId) {
18048
18099
  return callRPC("bichat.session.clear", { id: sessionId });
18049
18100
  }
18050
18101
  async function compactSessionHistory(callRPC, sessionId) {
18051
- return callRPC("bichat.session.compact", { id: sessionId });
18102
+ const result = await callRPC("bichat.session.compact", { id: sessionId });
18103
+ if (!result.accepted) {
18104
+ throw new Error("Session compact request was not accepted");
18105
+ }
18106
+ if (result.operation !== "session_compact") {
18107
+ throw new Error(`Unexpected async operation: ${result.operation}`);
18108
+ }
18109
+ if (!result.sessionId || !result.runId) {
18110
+ throw new Error("Missing async run metadata");
18111
+ }
18112
+ return {
18113
+ accepted: true,
18114
+ operation: result.operation,
18115
+ sessionId: result.sessionId,
18116
+ runId: result.runId,
18117
+ startedAt: result.startedAt
18118
+ };
18052
18119
  }
18053
18120
  async function listUsers(callRPC) {
18054
18121
  const data = await callRPC("bichat.user.list", {});
@@ -18167,6 +18234,9 @@ var TERMINAL_TYPES = /* @__PURE__ */ new Set(["done", "error"]);
18167
18234
  async function* parseBichatStream(reader) {
18168
18235
  let yieldedTerminal = false;
18169
18236
  for await (const event of parseSSEStream(reader)) {
18237
+ if (event.type === "ping") {
18238
+ continue;
18239
+ }
18170
18240
  const parsed = event;
18171
18241
  const inferredType = parsed.type || (parsed.content ? "content" : "error");
18172
18242
  const normalized = {
@@ -18504,7 +18574,7 @@ async function* sendMessage(deps, sessionId, content, attachments = [], signal,
18504
18574
  replaceFromMessageId: options?.replaceFromMessageID,
18505
18575
  attachments: streamAttachments
18506
18576
  };
18507
- const timeoutMs = deps.timeout ?? 0;
18577
+ const timeoutMs = deps.streamConnectTimeoutMs ?? 0;
18508
18578
  if (timeoutMs > 0) {
18509
18579
  connectionTimeoutID = setTimeout(() => {
18510
18580
  connectionTimedOut = true;
@@ -18539,7 +18609,7 @@ async function* sendMessage(deps, sessionId, content, attachments = [], signal,
18539
18609
  if (err.name === "AbortError") {
18540
18610
  yield {
18541
18611
  type: "error",
18542
- error: connectionTimedOut ? `Stream request timed out after ${deps.timeout}ms` : "Stream cancelled"
18612
+ error: connectionTimedOut ? `Stream request timed out after ${deps.streamConnectTimeoutMs}ms` : "Stream cancelled"
18543
18613
  };
18544
18614
  } else {
18545
18615
  yield {
@@ -18627,7 +18697,7 @@ async function resumeStream(deps, sessionId, runId, onChunk, signal) {
18627
18697
  const url = buildStreamUrl(deps, "/resume");
18628
18698
  const controller = new AbortController();
18629
18699
  let timeoutId;
18630
- const timeoutMs = deps.timeout;
18700
+ const timeoutMs = deps.connectTimeoutMs;
18631
18701
  if (timeoutMs != null && timeoutMs > 0) {
18632
18702
  timeoutId = setTimeout(() => controller.abort(), timeoutMs);
18633
18703
  }
@@ -18644,6 +18714,10 @@ async function resumeStream(deps, sessionId, runId, onChunk, signal) {
18644
18714
  if (!response.ok) {
18645
18715
  throw new Error(`Resume stream failed: HTTP ${response.status}`);
18646
18716
  }
18717
+ if (timeoutId !== void 0) {
18718
+ clearTimeout(timeoutId);
18719
+ timeoutId = void 0;
18720
+ }
18647
18721
  if (!response.body) {
18648
18722
  throw new Error("Resume response body is null");
18649
18723
  }
@@ -18677,11 +18751,7 @@ async function submitQuestionAnswers(callRPC, sessionId, questionId, answers) {
18677
18751
  });
18678
18752
  return {
18679
18753
  success: true,
18680
- data: {
18681
- session: toSession(result.session),
18682
- turns: normalizeTurns(sanitizeConversationTurns(result.turns, sessionId)),
18683
- pendingQuestion: sanitizePendingQuestion(result.pendingQuestion, sessionId)
18684
- }
18754
+ data: normalizeAsyncRunAccepted(result)
18685
18755
  };
18686
18756
  } catch (err) {
18687
18757
  return { success: false, error: err instanceof Error ? err.message : "Unknown error" };
@@ -18689,12 +18759,33 @@ async function submitQuestionAnswers(callRPC, sessionId, questionId, answers) {
18689
18759
  }
18690
18760
  async function rejectPendingQuestion(callRPC, sessionId) {
18691
18761
  try {
18692
- await callRPC("bichat.question.reject", { sessionId });
18693
- return { success: true };
18762
+ const result = await callRPC("bichat.question.reject", { sessionId });
18763
+ return { success: true, data: normalizeAsyncRunAccepted(result) };
18694
18764
  } catch (err) {
18695
18765
  return { success: false, error: err instanceof Error ? err.message : "Unknown error" };
18696
18766
  }
18697
18767
  }
18768
+ function isAsyncRunOperation(value) {
18769
+ return value === "question_submit" || value === "question_reject" || value === "session_compact";
18770
+ }
18771
+ function normalizeAsyncRunAccepted(input) {
18772
+ if (!input.accepted) {
18773
+ throw new Error("Async run request was not accepted");
18774
+ }
18775
+ if (!isAsyncRunOperation(input.operation)) {
18776
+ throw new Error(`Unexpected async operation: ${input.operation}`);
18777
+ }
18778
+ if (!input.sessionId || !input.runId) {
18779
+ throw new Error("Missing async run metadata");
18780
+ }
18781
+ return {
18782
+ accepted: true,
18783
+ operation: input.operation,
18784
+ sessionId: input.sessionId,
18785
+ runId: input.runId,
18786
+ startedAt: input.startedAt
18787
+ };
18788
+ }
18698
18789
 
18699
18790
  // ui/src/bichat/data/ArtifactManager.ts
18700
18791
  async function fetchSessionArtifacts(callRPC, sessionId, options) {
@@ -18766,15 +18857,13 @@ var HttpDataSource = class {
18766
18857
  this.config = {
18767
18858
  streamEndpoint: "/stream",
18768
18859
  uploadEndpoint: "/api/uploads",
18769
- timeout: 12e4,
18770
- ...config
18860
+ ...config,
18861
+ rpcTimeoutMs: typeof config.rpcTimeoutMs === "number" ? config.rpcTimeoutMs : 12e4,
18862
+ streamConnectTimeoutMs: typeof config.streamConnectTimeoutMs === "number" ? config.streamConnectTimeoutMs : 3e4
18771
18863
  };
18772
- if (config.navigateToSession) {
18773
- this.navigateToSession = config.navigateToSession;
18774
- }
18775
18864
  this.rpc = createAppletRPCClient({
18776
18865
  endpoint: `${this.config.baseUrl}${this.config.rpcEndpoint}`,
18777
- timeoutMs: this.config.timeout
18866
+ timeoutMs: this.config.rpcTimeoutMs
18778
18867
  });
18779
18868
  }
18780
18869
  // -------------------------------------------------------------------------
@@ -18894,7 +18983,7 @@ var HttpDataSource = class {
18894
18983
  baseUrl: this.config.baseUrl,
18895
18984
  streamEndpoint: this.config.streamEndpoint,
18896
18985
  createHeaders: (h) => this.createHeaders(h),
18897
- timeoutMs: this.config.timeout
18986
+ timeoutMs: this.config.rpcTimeoutMs
18898
18987
  },
18899
18988
  sessionId
18900
18989
  );
@@ -18905,7 +18994,7 @@ var HttpDataSource = class {
18905
18994
  baseUrl: this.config.baseUrl,
18906
18995
  streamEndpoint: this.config.streamEndpoint,
18907
18996
  createHeaders: (h) => this.createHeaders(h),
18908
- timeout: this.config.timeout
18997
+ connectTimeoutMs: this.config.streamConnectTimeoutMs
18909
18998
  },
18910
18999
  sessionId,
18911
19000
  runId,
@@ -18929,7 +19018,8 @@ var HttpDataSource = class {
18929
19018
  callRPC: this.boundCallRPC,
18930
19019
  baseUrl: this.config.baseUrl,
18931
19020
  streamEndpoint: this.config.streamEndpoint,
18932
- timeout: this.config.timeout,
19021
+ rpcTimeoutMs: this.config.rpcTimeoutMs,
19022
+ streamConnectTimeoutMs: this.config.streamConnectTimeoutMs,
18933
19023
  createHeaders: (additional) => this.createHeaders(additional),
18934
19024
  uploadFileFn: this.boundUploadFile,
18935
19025
  logAttachmentLifecycle: () => {
@@ -18980,17 +19070,6 @@ var HttpDataSource = class {
18980
19070
  async deleteSessionArtifact(artifactId) {
18981
19071
  return deleteSessionArtifact(this.boundCallRPC, artifactId);
18982
19072
  }
18983
- // -------------------------------------------------------------------------
18984
- // Navigation (optional, deprecated)
18985
- // -------------------------------------------------------------------------
18986
- /**
18987
- * @deprecated Pass `onSessionCreated` to `ChatSessionProvider` instead.
18988
- */
18989
- navigateToSession(sessionId) {
18990
- if (typeof window !== "undefined") {
18991
- window.location.href = `/chat/${sessionId}`;
18992
- }
18993
- }
18994
19073
  };
18995
19074
  function createHttpDataSource(config) {
18996
19075
  return new HttpDataSource(config);