@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.
@@ -2454,6 +2454,33 @@ var ChatMachine = class {
2454
2454
  });
2455
2455
  }
2456
2456
  }
2457
+ async _resumeAcceptedRunOrPoll(sessionId, runId) {
2458
+ setRunMarker(sessionId, runId);
2459
+ try {
2460
+ await this._runResumeStream(sessionId, runId);
2461
+ } catch (err) {
2462
+ if (this.disposed) {
2463
+ return;
2464
+ }
2465
+ console.warn("[ChatMachine] resumeStream failed, switching to status polling fallback:", err);
2466
+ const getStreamStatus2 = this.dataSource.getStreamStatus;
2467
+ const status = getStreamStatus2 ? await getStreamStatus2(sessionId).catch(() => null) : null;
2468
+ if (!status?.active) {
2469
+ clearRunMarker(sessionId);
2470
+ await this._syncSessionFromServer(sessionId, true).catch(() => {
2471
+ });
2472
+ this._updateMessaging({ generationInProgress: false });
2473
+ return;
2474
+ }
2475
+ setRunMarker(sessionId, status.runId ?? runId);
2476
+ this._updateMessaging({
2477
+ isStreaming: false,
2478
+ loading: false,
2479
+ generationInProgress: true
2480
+ });
2481
+ this._startPassivePolling(sessionId);
2482
+ }
2483
+ }
2457
2484
  // =====================================================================
2458
2485
  // Private — actions
2459
2486
  // =====================================================================
@@ -2572,20 +2599,28 @@ var ChatMachine = class {
2572
2599
  streamingContent: ""
2573
2600
  });
2574
2601
  try {
2575
- const compactResult = await this.dataSource.compactSessionHistory(curSessionId);
2576
- const summary = compactResult.summary || "";
2602
+ const accepted = await this.dataSource.compactSessionHistory(curSessionId);
2603
+ if (!accepted.runId) {
2604
+ throw new Error("Async compaction run metadata is missing");
2605
+ }
2577
2606
  this._updateMessaging({
2578
- turns: applyTurnLifecycleForPendingQuestion([createCompactedSystemTurn(curSessionId, summary)], null),
2607
+ turns: applyTurnLifecycleForPendingQuestion(
2608
+ [createCompactedSystemTurn(curSessionId, "Compacting conversation history...")],
2609
+ null
2610
+ ),
2579
2611
  pendingQuestion: null
2580
2612
  });
2581
- const result = await this.dataSource.fetchSession(curSessionId);
2582
- if (result) {
2583
- this._updateSession({ session: result.session });
2584
- this._setTurnsFromFetch(result.turns, result.pendingQuestion || null);
2585
- } else {
2586
- this._setTurnsFromFetch([], null);
2613
+ await this._resumeAcceptedRunOrPoll(curSessionId, accepted.runId);
2614
+ if (!this.state.messaging.generationInProgress) {
2615
+ const result = await this.dataSource.fetchSession(curSessionId);
2616
+ if (result) {
2617
+ this._updateSession({ session: result.session });
2618
+ this._setTurnsFromFetch(result.turns, result.pendingQuestion || null);
2619
+ } else {
2620
+ this._setTurnsFromFetch([], null);
2621
+ }
2622
+ this._updateMessaging({ codeOutputs: [] });
2587
2623
  }
2588
- this._updateMessaging({ codeOutputs: [] });
2589
2624
  } catch (err) {
2590
2625
  const normalized = normalizeRPCError(err, "Failed to compact session history");
2591
2626
  this._updateInput({ inputError: normalized.userMessage });
@@ -2750,8 +2785,6 @@ var ChatMachine = class {
2750
2785
  this._notifySessionsUpdated("session_created", targetSessionId);
2751
2786
  if (this.onSessionCreated) {
2752
2787
  this.onSessionCreated(targetSessionId);
2753
- } else {
2754
- this.dataSource.navigateToSession?.(targetSessionId);
2755
2788
  }
2756
2789
  }
2757
2790
  this._clearStreamError();
@@ -3037,9 +3070,24 @@ var ChatMachine = class {
3037
3070
  return;
3038
3071
  }
3039
3072
  if (result.success) {
3073
+ this._updateMessaging({
3074
+ pendingQuestion: null,
3075
+ turns: applyTurnLifecycleForPendingQuestion(this.state.messaging.turns, null)
3076
+ });
3040
3077
  if (result.data) {
3041
- this._updateSession({ session: result.data.session });
3042
- this._setTurnsFromFetch(result.data.turns, result.data.pendingQuestion || null);
3078
+ await this._resumeAcceptedRunOrPoll(curSessionId, result.data.runId);
3079
+ if (!this.state.messaging.generationInProgress) {
3080
+ const fetchResult = await this.dataSource.fetchSession(curSessionId);
3081
+ if (this.disposed) {
3082
+ return;
3083
+ }
3084
+ if (fetchResult) {
3085
+ this._updateSession({ session: fetchResult.session });
3086
+ this._setTurnsFromFetch(fetchResult.turns, fetchResult.pendingQuestion || null);
3087
+ } else {
3088
+ this._updateSession({ error: "Failed to load updated session", errorRetryable: true });
3089
+ }
3090
+ }
3043
3091
  } else if (curSessionId !== "new") {
3044
3092
  const fetchResult = await this.dataSource.fetchSession(curSessionId);
3045
3093
  if (this.disposed) {
@@ -3083,7 +3131,9 @@ var ChatMachine = class {
3083
3131
  pendingQuestion: null,
3084
3132
  turns: applyTurnLifecycleForPendingQuestion(this.state.messaging.turns, null)
3085
3133
  });
3086
- if (curSessionId !== "new") {
3134
+ if (result.data) {
3135
+ await this._resumeAcceptedRunOrPoll(curSessionId, result.data.runId);
3136
+ } else if (curSessionId !== "new") {
3087
3137
  const fetchResult = await this.dataSource.fetchSession(curSessionId);
3088
3138
  if (this.disposed) {
3089
3139
  return;
@@ -16892,9 +16942,10 @@ function useHttpDataSourceConfigFromApplet(options) {
16892
16942
  rpcEndpoint,
16893
16943
  streamEndpoint,
16894
16944
  csrfToken,
16895
- timeout: options?.timeout ?? 12e4
16945
+ rpcTimeoutMs: options?.rpcTimeoutMs ?? 12e4,
16946
+ streamConnectTimeoutMs: options?.streamConnectTimeoutMs ?? 3e4
16896
16947
  };
16897
- }, [options?.timeout]);
16948
+ }, [options?.rpcTimeoutMs, options?.streamConnectTimeoutMs]);
16898
16949
  }
16899
16950
  var SESSION_PATH_REGEX = /\/session\/([^/]+)/;
16900
16951
  function useBichatRouter({
@@ -18060,7 +18111,23 @@ async function clearSessionHistory(callRPC, sessionId) {
18060
18111
  return callRPC("bichat.session.clear", { id: sessionId });
18061
18112
  }
18062
18113
  async function compactSessionHistory(callRPC, sessionId) {
18063
- return callRPC("bichat.session.compact", { id: sessionId });
18114
+ const result = await callRPC("bichat.session.compact", { id: sessionId });
18115
+ if (!result.accepted) {
18116
+ throw new Error("Session compact request was not accepted");
18117
+ }
18118
+ if (result.operation !== "session_compact") {
18119
+ throw new Error(`Unexpected async operation: ${result.operation}`);
18120
+ }
18121
+ if (!result.sessionId || !result.runId) {
18122
+ throw new Error("Missing async run metadata");
18123
+ }
18124
+ return {
18125
+ accepted: true,
18126
+ operation: result.operation,
18127
+ sessionId: result.sessionId,
18128
+ runId: result.runId,
18129
+ startedAt: result.startedAt
18130
+ };
18064
18131
  }
18065
18132
  async function listUsers(callRPC) {
18066
18133
  const data = await callRPC("bichat.user.list", {});
@@ -18179,6 +18246,9 @@ var TERMINAL_TYPES = /* @__PURE__ */ new Set(["done", "error"]);
18179
18246
  async function* parseBichatStream(reader) {
18180
18247
  let yieldedTerminal = false;
18181
18248
  for await (const event of parseSSEStream(reader)) {
18249
+ if (event.type === "ping") {
18250
+ continue;
18251
+ }
18182
18252
  const parsed = event;
18183
18253
  const inferredType = parsed.type || (parsed.content ? "content" : "error");
18184
18254
  const normalized = {
@@ -18516,7 +18586,7 @@ async function* sendMessage(deps, sessionId, content, attachments = [], signal,
18516
18586
  replaceFromMessageId: options?.replaceFromMessageID,
18517
18587
  attachments: streamAttachments
18518
18588
  };
18519
- const timeoutMs = deps.timeout ?? 0;
18589
+ const timeoutMs = deps.streamConnectTimeoutMs ?? 0;
18520
18590
  if (timeoutMs > 0) {
18521
18591
  connectionTimeoutID = setTimeout(() => {
18522
18592
  connectionTimedOut = true;
@@ -18551,7 +18621,7 @@ async function* sendMessage(deps, sessionId, content, attachments = [], signal,
18551
18621
  if (err.name === "AbortError") {
18552
18622
  yield {
18553
18623
  type: "error",
18554
- error: connectionTimedOut ? `Stream request timed out after ${deps.timeout}ms` : "Stream cancelled"
18624
+ error: connectionTimedOut ? `Stream request timed out after ${deps.streamConnectTimeoutMs}ms` : "Stream cancelled"
18555
18625
  };
18556
18626
  } else {
18557
18627
  yield {
@@ -18639,7 +18709,7 @@ async function resumeStream(deps, sessionId, runId, onChunk, signal) {
18639
18709
  const url = buildStreamUrl(deps, "/resume");
18640
18710
  const controller = new AbortController();
18641
18711
  let timeoutId;
18642
- const timeoutMs = deps.timeout;
18712
+ const timeoutMs = deps.connectTimeoutMs;
18643
18713
  if (timeoutMs != null && timeoutMs > 0) {
18644
18714
  timeoutId = setTimeout(() => controller.abort(), timeoutMs);
18645
18715
  }
@@ -18656,6 +18726,10 @@ async function resumeStream(deps, sessionId, runId, onChunk, signal) {
18656
18726
  if (!response.ok) {
18657
18727
  throw new Error(`Resume stream failed: HTTP ${response.status}`);
18658
18728
  }
18729
+ if (timeoutId !== void 0) {
18730
+ clearTimeout(timeoutId);
18731
+ timeoutId = void 0;
18732
+ }
18659
18733
  if (!response.body) {
18660
18734
  throw new Error("Resume response body is null");
18661
18735
  }
@@ -18689,11 +18763,7 @@ async function submitQuestionAnswers(callRPC, sessionId, questionId, answers) {
18689
18763
  });
18690
18764
  return {
18691
18765
  success: true,
18692
- data: {
18693
- session: toSession(result.session),
18694
- turns: normalizeTurns(sanitizeConversationTurns(result.turns, sessionId)),
18695
- pendingQuestion: sanitizePendingQuestion(result.pendingQuestion, sessionId)
18696
- }
18766
+ data: normalizeAsyncRunAccepted(result)
18697
18767
  };
18698
18768
  } catch (err) {
18699
18769
  return { success: false, error: err instanceof Error ? err.message : "Unknown error" };
@@ -18701,12 +18771,33 @@ async function submitQuestionAnswers(callRPC, sessionId, questionId, answers) {
18701
18771
  }
18702
18772
  async function rejectPendingQuestion(callRPC, sessionId) {
18703
18773
  try {
18704
- await callRPC("bichat.question.reject", { sessionId });
18705
- return { success: true };
18774
+ const result = await callRPC("bichat.question.reject", { sessionId });
18775
+ return { success: true, data: normalizeAsyncRunAccepted(result) };
18706
18776
  } catch (err) {
18707
18777
  return { success: false, error: err instanceof Error ? err.message : "Unknown error" };
18708
18778
  }
18709
18779
  }
18780
+ function isAsyncRunOperation(value) {
18781
+ return value === "question_submit" || value === "question_reject" || value === "session_compact";
18782
+ }
18783
+ function normalizeAsyncRunAccepted(input) {
18784
+ if (!input.accepted) {
18785
+ throw new Error("Async run request was not accepted");
18786
+ }
18787
+ if (!isAsyncRunOperation(input.operation)) {
18788
+ throw new Error(`Unexpected async operation: ${input.operation}`);
18789
+ }
18790
+ if (!input.sessionId || !input.runId) {
18791
+ throw new Error("Missing async run metadata");
18792
+ }
18793
+ return {
18794
+ accepted: true,
18795
+ operation: input.operation,
18796
+ sessionId: input.sessionId,
18797
+ runId: input.runId,
18798
+ startedAt: input.startedAt
18799
+ };
18800
+ }
18710
18801
 
18711
18802
  // ui/src/bichat/data/ArtifactManager.ts
18712
18803
  async function fetchSessionArtifacts(callRPC, sessionId, options) {
@@ -18778,15 +18869,13 @@ var HttpDataSource = class {
18778
18869
  this.config = {
18779
18870
  streamEndpoint: "/stream",
18780
18871
  uploadEndpoint: "/api/uploads",
18781
- timeout: 12e4,
18782
- ...config
18872
+ ...config,
18873
+ rpcTimeoutMs: typeof config.rpcTimeoutMs === "number" ? config.rpcTimeoutMs : 12e4,
18874
+ streamConnectTimeoutMs: typeof config.streamConnectTimeoutMs === "number" ? config.streamConnectTimeoutMs : 3e4
18783
18875
  };
18784
- if (config.navigateToSession) {
18785
- this.navigateToSession = config.navigateToSession;
18786
- }
18787
18876
  this.rpc = createAppletRPCClient({
18788
18877
  endpoint: `${this.config.baseUrl}${this.config.rpcEndpoint}`,
18789
- timeoutMs: this.config.timeout
18878
+ timeoutMs: this.config.rpcTimeoutMs
18790
18879
  });
18791
18880
  }
18792
18881
  // -------------------------------------------------------------------------
@@ -18906,7 +18995,7 @@ var HttpDataSource = class {
18906
18995
  baseUrl: this.config.baseUrl,
18907
18996
  streamEndpoint: this.config.streamEndpoint,
18908
18997
  createHeaders: (h) => this.createHeaders(h),
18909
- timeoutMs: this.config.timeout
18998
+ timeoutMs: this.config.rpcTimeoutMs
18910
18999
  },
18911
19000
  sessionId
18912
19001
  );
@@ -18917,7 +19006,7 @@ var HttpDataSource = class {
18917
19006
  baseUrl: this.config.baseUrl,
18918
19007
  streamEndpoint: this.config.streamEndpoint,
18919
19008
  createHeaders: (h) => this.createHeaders(h),
18920
- timeout: this.config.timeout
19009
+ connectTimeoutMs: this.config.streamConnectTimeoutMs
18921
19010
  },
18922
19011
  sessionId,
18923
19012
  runId,
@@ -18941,7 +19030,8 @@ var HttpDataSource = class {
18941
19030
  callRPC: this.boundCallRPC,
18942
19031
  baseUrl: this.config.baseUrl,
18943
19032
  streamEndpoint: this.config.streamEndpoint,
18944
- timeout: this.config.timeout,
19033
+ rpcTimeoutMs: this.config.rpcTimeoutMs,
19034
+ streamConnectTimeoutMs: this.config.streamConnectTimeoutMs,
18945
19035
  createHeaders: (additional) => this.createHeaders(additional),
18946
19036
  uploadFileFn: this.boundUploadFile,
18947
19037
  logAttachmentLifecycle: () => {
@@ -18992,17 +19082,6 @@ var HttpDataSource = class {
18992
19082
  async deleteSessionArtifact(artifactId) {
18993
19083
  return deleteSessionArtifact(this.boundCallRPC, artifactId);
18994
19084
  }
18995
- // -------------------------------------------------------------------------
18996
- // Navigation (optional, deprecated)
18997
- // -------------------------------------------------------------------------
18998
- /**
18999
- * @deprecated Pass `onSessionCreated` to `ChatSessionProvider` instead.
19000
- */
19001
- navigateToSession(sessionId) {
19002
- if (typeof window !== "undefined") {
19003
- window.location.href = `/chat/${sessionId}`;
19004
- }
19005
- }
19006
19085
  };
19007
19086
  function createHttpDataSource(config) {
19008
19087
  return new HttpDataSource(config);