@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.
package/dist/index.mjs CHANGED
@@ -529,7 +529,8 @@ function createFinalMessage(streamingId, state) {
529
529
  currentExecutingStepId: void 0,
530
530
  userActionResult: state.userActionResult,
531
531
  activeThinkingText: void 0,
532
- allThinkingText: state.hasError ? void 0 : state.allThinkingText
532
+ allThinkingText: state.hasError ? void 0 : state.allThinkingText,
533
+ isResolvingImages: state.hasError ? void 0 : state.isResolvingImages
533
534
  };
534
535
  }
535
536
  function createCancelledMessageUpdate(steps, currentMessage) {
@@ -583,6 +584,16 @@ function buildUserActionUrl(config, userActionId, action) {
583
584
  const encodedUserActionId = encodeURIComponent(userActionId);
584
585
  return `${config.api.baseUrl}${basePath}/user-action/${encodedUserActionId}/${action}`;
585
586
  }
587
+ function buildResolveImagesUrl(config) {
588
+ if (config.api.resolveImagesEndpoint) {
589
+ return `${config.api.baseUrl}${config.api.resolveImagesEndpoint}`;
590
+ }
591
+ const streamEndpoint = config.api.streamEndpoint || "/api/workflows/ask/stream";
592
+ const [endpointPath] = streamEndpoint.split("?");
593
+ const normalizedEndpointPath = endpointPath.replace(/\/+$/, "");
594
+ const basePath = normalizedEndpointPath.endsWith("/stream") ? normalizedEndpointPath.slice(0, -"/stream".length) : normalizedEndpointPath;
595
+ return `${config.api.baseUrl}${basePath}/resolve-image-urls`;
596
+ }
586
597
  function buildRequestHeaders(config) {
587
598
  const headers = {
588
599
  ...config.api.headers
@@ -697,6 +708,58 @@ var activeStreamStore = {
697
708
  streams.delete(key);
698
709
  }
699
710
  };
711
+
712
+ // src/utils/ragImageResolver.ts
713
+ var RAG_IMAGE_REGEX = /\/api\/rag\/chunks\/[^"'\s]+\/image/;
714
+ function hasRagImages(content) {
715
+ return RAG_IMAGE_REGEX.test(content);
716
+ }
717
+ async function waitForNextPaint(signal) {
718
+ if (signal?.aborted) return;
719
+ await new Promise((resolve) => {
720
+ let isSettled = false;
721
+ const finish = () => {
722
+ if (isSettled) return;
723
+ isSettled = true;
724
+ signal?.removeEventListener("abort", finish);
725
+ resolve();
726
+ };
727
+ signal?.addEventListener("abort", finish, { once: true });
728
+ if (typeof requestAnimationFrame === "function") {
729
+ requestAnimationFrame(() => {
730
+ setTimeout(finish, 0);
731
+ });
732
+ return;
733
+ }
734
+ setTimeout(finish, 0);
735
+ });
736
+ }
737
+ async function resolveRagImageUrls(config, content, signal) {
738
+ const url = buildResolveImagesUrl(config);
739
+ const baseHeaders = buildRequestHeaders(config);
740
+ const headers = { "Content-Type": "application/json", ...baseHeaders };
741
+ const response = await fetch(url, {
742
+ method: "POST",
743
+ headers,
744
+ body: JSON.stringify({ input: content }),
745
+ signal
746
+ });
747
+ if (!response.ok) {
748
+ const errorText = await response.text();
749
+ throw new Error(`HTTP ${response.status}: ${errorText}`);
750
+ }
751
+ const text = await response.text();
752
+ try {
753
+ const parsed = JSON.parse(text);
754
+ if (typeof parsed === "string") return parsed;
755
+ if (typeof parsed.output === "string") return parsed.output;
756
+ if (typeof parsed.result === "string") return parsed.result;
757
+ } catch {
758
+ }
759
+ return text;
760
+ }
761
+
762
+ // src/hooks/useStreamManager.ts
700
763
  function useStreamManager(config, callbacks, setMessages, setIsWaitingForResponse) {
701
764
  const abortControllerRef = useRef(null);
702
765
  const configRef = useRef(config);
@@ -862,9 +925,11 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
862
925
  if (state.currentSessionId && state.currentSessionId !== sessionId) {
863
926
  callbacksRef.current.onSessionIdChange?.(state.currentSessionId);
864
927
  }
928
+ const needsImageResolve = !state.hasError && !abortController.signal.aborted && hasRagImages(state.accumulatedContent);
865
929
  const finalMessage = createFinalMessage(streamingId, {
866
930
  ...state,
867
- sessionId: state.currentSessionId || sessionId
931
+ sessionId: state.currentSessionId || sessionId,
932
+ isResolvingImages: needsImageResolve
868
933
  });
869
934
  setMessages(
870
935
  (prev) => prev.map(
@@ -875,6 +940,30 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
875
940
  }
876
941
  });
877
942
  clearThrottle();
943
+ const shouldResolveImages = !abortController.signal.aborted && !state.hasError && hasRagImages(state.accumulatedContent);
944
+ if (shouldResolveImages) {
945
+ await waitForNextPaint(abortController.signal);
946
+ }
947
+ if (shouldResolveImages && !abortController.signal.aborted) {
948
+ try {
949
+ const resolvedContent = await resolveRagImageUrls(
950
+ currentConfig,
951
+ state.accumulatedContent,
952
+ abortController.signal
953
+ );
954
+ setMessages(
955
+ (prev) => prev.map(
956
+ (msg) => msg.id === streamingId ? { ...msg, content: resolvedContent, isResolvingImages: false } : msg
957
+ )
958
+ );
959
+ } catch {
960
+ setMessages(
961
+ (prev) => prev.map(
962
+ (msg) => msg.id === streamingId ? { ...msg, isResolvingImages: false } : msg
963
+ )
964
+ );
965
+ }
966
+ }
878
967
  return state.currentSessionId;
879
968
  } catch (error) {
880
969
  clearThrottle();