@paymanai/payman-typescript-ask-sdk 1.2.7 → 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.
@@ -362,8 +362,10 @@ function processStreamEvent(event, state) {
362
362
  if (event.userActionRequest) {
363
363
  state.userActionRequest = {
364
364
  userActionId: event.userActionRequest.userActionId,
365
+ userActionType: event.userActionRequest.userActionType,
365
366
  message: event.userActionRequest.message,
366
- requestedSchema: event.userActionRequest.requestedSchema
367
+ requestedSchema: event.userActionRequest.requestedSchema,
368
+ metadata: event.userActionRequest.metadata
367
369
  };
368
370
  }
369
371
  state.userActionPending = true;
@@ -555,7 +557,8 @@ function createFinalMessage(streamingId, state) {
555
557
  currentExecutingStepId: void 0,
556
558
  userActionResult: state.userActionResult,
557
559
  activeThinkingText: void 0,
558
- allThinkingText: state.hasError ? void 0 : state.allThinkingText
560
+ allThinkingText: state.hasError ? void 0 : state.allThinkingText,
561
+ isResolvingImages: state.hasError ? void 0 : state.isResolvingImages
559
562
  };
560
563
  }
561
564
  function createCancelledMessageUpdate(steps, currentMessage) {
@@ -609,6 +612,16 @@ function buildUserActionUrl(config, userActionId, action) {
609
612
  const encodedUserActionId = encodeURIComponent(userActionId);
610
613
  return `${config.api.baseUrl}${basePath}/user-action/${encodedUserActionId}/${action}`;
611
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
+ }
612
625
  function buildRequestHeaders(config) {
613
626
  const headers = {
614
627
  ...config.api.headers
@@ -727,6 +740,58 @@ var activeStreamStore = {
727
740
  streams.delete(key);
728
741
  }
729
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
730
795
  function useStreamManager(config, callbacks, setMessages, setIsWaitingForResponse) {
731
796
  const abortControllerRef = react.useRef(null);
732
797
  const configRef = react.useRef(config);
@@ -892,9 +957,11 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
892
957
  if (state.currentSessionId && state.currentSessionId !== sessionId) {
893
958
  callbacksRef.current.onSessionIdChange?.(state.currentSessionId);
894
959
  }
960
+ const needsImageResolve = !state.hasError && !abortController.signal.aborted && hasRagImages(state.accumulatedContent);
895
961
  const finalMessage = createFinalMessage(streamingId, {
896
962
  ...state,
897
- sessionId: state.currentSessionId || sessionId
963
+ sessionId: state.currentSessionId || sessionId,
964
+ isResolvingImages: needsImageResolve
898
965
  });
899
966
  setMessages(
900
967
  (prev) => prev.map(
@@ -905,6 +972,30 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
905
972
  }
906
973
  });
907
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
+ }
908
999
  return state.currentSessionId;
909
1000
  } catch (error) {
910
1001
  clearThrottle();