@paymanai/payman-typescript-ask-sdk 1.2.8 → 1.2.9

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.
@@ -557,7 +557,8 @@ function createFinalMessage(streamingId, state) {
557
557
  currentExecutingStepId: void 0,
558
558
  userActionResult: state.userActionResult,
559
559
  activeThinkingText: void 0,
560
- allThinkingText: state.hasError ? void 0 : state.allThinkingText
560
+ allThinkingText: state.hasError ? void 0 : state.allThinkingText,
561
+ isResolvingImages: state.hasError ? void 0 : state.isResolvingImages
561
562
  };
562
563
  }
563
564
  function createCancelledMessageUpdate(steps, currentMessage) {
@@ -611,6 +612,16 @@ function buildUserActionUrl(config, userActionId, action) {
611
612
  const encodedUserActionId = encodeURIComponent(userActionId);
612
613
  return `${config.api.baseUrl}${basePath}/user-action/${encodedUserActionId}/${action}`;
613
614
  }
615
+ function buildResolveImagesUrl(config) {
616
+ if (config.api.resolveImagesEndpoint) {
617
+ return `${config.api.baseUrl}${config.api.resolveImagesEndpoint}`;
618
+ }
619
+ const streamEndpoint = config.api.streamEndpoint || "/api/workflows/ask/stream";
620
+ const [endpointPath] = streamEndpoint.split("?");
621
+ const normalizedEndpointPath = endpointPath.replace(/\/+$/, "");
622
+ const basePath = normalizedEndpointPath.endsWith("/stream") ? normalizedEndpointPath.slice(0, -"/stream".length) : normalizedEndpointPath;
623
+ return `${config.api.baseUrl}${basePath}/resolve-image-urls`;
624
+ }
614
625
  function buildRequestHeaders(config) {
615
626
  const headers = {
616
627
  ...config.api.headers
@@ -729,6 +740,58 @@ var activeStreamStore = {
729
740
  streams.delete(key);
730
741
  }
731
742
  };
743
+
744
+ // src/utils/ragImageResolver.ts
745
+ var RAG_IMAGE_REGEX = /\/api\/rag\/chunks\/[^"'\s]+\/image/;
746
+ function hasRagImages(content) {
747
+ return RAG_IMAGE_REGEX.test(content);
748
+ }
749
+ async function waitForNextPaint(signal) {
750
+ if (signal?.aborted) return;
751
+ await new Promise((resolve) => {
752
+ let isSettled = false;
753
+ const finish = () => {
754
+ if (isSettled) return;
755
+ isSettled = true;
756
+ signal?.removeEventListener("abort", finish);
757
+ resolve();
758
+ };
759
+ signal?.addEventListener("abort", finish, { once: true });
760
+ if (typeof requestAnimationFrame === "function") {
761
+ requestAnimationFrame(() => {
762
+ setTimeout(finish, 0);
763
+ });
764
+ return;
765
+ }
766
+ setTimeout(finish, 0);
767
+ });
768
+ }
769
+ async function resolveRagImageUrls(config, content, signal) {
770
+ const url = buildResolveImagesUrl(config);
771
+ const baseHeaders = buildRequestHeaders(config);
772
+ const headers = { "Content-Type": "application/json", ...baseHeaders };
773
+ const response = await fetch(url, {
774
+ method: "POST",
775
+ headers,
776
+ body: JSON.stringify({ input: content }),
777
+ signal
778
+ });
779
+ if (!response.ok) {
780
+ const errorText = await response.text();
781
+ throw new Error(`HTTP ${response.status}: ${errorText}`);
782
+ }
783
+ const text = await response.text();
784
+ try {
785
+ const parsed = JSON.parse(text);
786
+ if (typeof parsed === "string") return parsed;
787
+ if (typeof parsed.output === "string") return parsed.output;
788
+ if (typeof parsed.result === "string") return parsed.result;
789
+ } catch {
790
+ }
791
+ return text;
792
+ }
793
+
794
+ // src/hooks/useStreamManager.ts
732
795
  function useStreamManager(config, callbacks, setMessages, setIsWaitingForResponse) {
733
796
  const abortControllerRef = react.useRef(null);
734
797
  const configRef = react.useRef(config);
@@ -894,9 +957,11 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
894
957
  if (state.currentSessionId && state.currentSessionId !== sessionId) {
895
958
  callbacksRef.current.onSessionIdChange?.(state.currentSessionId);
896
959
  }
960
+ const needsImageResolve = !state.hasError && !abortController.signal.aborted && hasRagImages(state.accumulatedContent);
897
961
  const finalMessage = createFinalMessage(streamingId, {
898
962
  ...state,
899
- sessionId: state.currentSessionId || sessionId
963
+ sessionId: state.currentSessionId || sessionId,
964
+ isResolvingImages: needsImageResolve
900
965
  });
901
966
  setMessages(
902
967
  (prev) => prev.map(
@@ -907,6 +972,30 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
907
972
  }
908
973
  });
909
974
  clearThrottle();
975
+ const shouldResolveImages = !abortController.signal.aborted && !state.hasError && hasRagImages(state.accumulatedContent);
976
+ if (shouldResolveImages) {
977
+ await waitForNextPaint(abortController.signal);
978
+ }
979
+ if (shouldResolveImages && !abortController.signal.aborted) {
980
+ try {
981
+ const resolvedContent = await resolveRagImageUrls(
982
+ currentConfig,
983
+ state.accumulatedContent,
984
+ abortController.signal
985
+ );
986
+ setMessages(
987
+ (prev) => prev.map(
988
+ (msg) => msg.id === streamingId ? { ...msg, content: resolvedContent, isResolvingImages: false } : msg
989
+ )
990
+ );
991
+ } catch {
992
+ setMessages(
993
+ (prev) => prev.map(
994
+ (msg) => msg.id === streamingId ? { ...msg, isResolvingImages: false } : msg
995
+ )
996
+ );
997
+ }
998
+ }
910
999
  return state.currentSessionId;
911
1000
  } catch (error) {
912
1001
  clearThrottle();