@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.d.mts CHANGED
@@ -126,6 +126,8 @@ type MessageDisplay = {
126
126
  activeThinkingText?: string;
127
127
  /** All thinking accumulated across blocks (for post-stream display) */
128
128
  allThinkingText?: string;
129
+ /** True while RAG image URLs are being resolved after the final response is shown */
130
+ isResolvingImages?: boolean;
129
131
  };
130
132
  type SessionParams = {
131
133
  id?: string;
@@ -141,6 +143,8 @@ type APIConfig = {
141
143
  headers?: Record<string, string>;
142
144
  /** API endpoint for streaming (default: /api/workflows/ask/stream) */
143
145
  streamEndpoint?: string;
146
+ /** API endpoint for resolving RAG image URLs (default: /api/playground/ask/resolve-image-urls) */
147
+ resolveImagesEndpoint?: string;
144
148
  };
145
149
  type ChatConfig = {
146
150
  /** API configuration - required for the library to make calls */
package/dist/index.d.ts CHANGED
@@ -126,6 +126,8 @@ type MessageDisplay = {
126
126
  activeThinkingText?: string;
127
127
  /** All thinking accumulated across blocks (for post-stream display) */
128
128
  allThinkingText?: string;
129
+ /** True while RAG image URLs are being resolved after the final response is shown */
130
+ isResolvingImages?: boolean;
129
131
  };
130
132
  type SessionParams = {
131
133
  id?: string;
@@ -141,6 +143,8 @@ type APIConfig = {
141
143
  headers?: Record<string, string>;
142
144
  /** API endpoint for streaming (default: /api/workflows/ask/stream) */
143
145
  streamEndpoint?: string;
146
+ /** API endpoint for resolving RAG image URLs (default: /api/playground/ask/resolve-image-urls) */
147
+ resolveImagesEndpoint?: string;
144
148
  };
145
149
  type ChatConfig = {
146
150
  /** API configuration - required for the library to make calls */
package/dist/index.js CHANGED
@@ -531,7 +531,8 @@ function createFinalMessage(streamingId, state) {
531
531
  currentExecutingStepId: void 0,
532
532
  userActionResult: state.userActionResult,
533
533
  activeThinkingText: void 0,
534
- allThinkingText: state.hasError ? void 0 : state.allThinkingText
534
+ allThinkingText: state.hasError ? void 0 : state.allThinkingText,
535
+ isResolvingImages: state.hasError ? void 0 : state.isResolvingImages
535
536
  };
536
537
  }
537
538
  function createCancelledMessageUpdate(steps, currentMessage) {
@@ -585,6 +586,16 @@ function buildUserActionUrl(config, userActionId, action) {
585
586
  const encodedUserActionId = encodeURIComponent(userActionId);
586
587
  return `${config.api.baseUrl}${basePath}/user-action/${encodedUserActionId}/${action}`;
587
588
  }
589
+ function buildResolveImagesUrl(config) {
590
+ if (config.api.resolveImagesEndpoint) {
591
+ return `${config.api.baseUrl}${config.api.resolveImagesEndpoint}`;
592
+ }
593
+ const streamEndpoint = config.api.streamEndpoint || "/api/workflows/ask/stream";
594
+ const [endpointPath] = streamEndpoint.split("?");
595
+ const normalizedEndpointPath = endpointPath.replace(/\/+$/, "");
596
+ const basePath = normalizedEndpointPath.endsWith("/stream") ? normalizedEndpointPath.slice(0, -"/stream".length) : normalizedEndpointPath;
597
+ return `${config.api.baseUrl}${basePath}/resolve-image-urls`;
598
+ }
588
599
  function buildRequestHeaders(config) {
589
600
  const headers = {
590
601
  ...config.api.headers
@@ -699,6 +710,58 @@ var activeStreamStore = {
699
710
  streams.delete(key);
700
711
  }
701
712
  };
713
+
714
+ // src/utils/ragImageResolver.ts
715
+ var RAG_IMAGE_REGEX = /\/api\/rag\/chunks\/[^"'\s]+\/image/;
716
+ function hasRagImages(content) {
717
+ return RAG_IMAGE_REGEX.test(content);
718
+ }
719
+ async function waitForNextPaint(signal) {
720
+ if (signal?.aborted) return;
721
+ await new Promise((resolve) => {
722
+ let isSettled = false;
723
+ const finish = () => {
724
+ if (isSettled) return;
725
+ isSettled = true;
726
+ signal?.removeEventListener("abort", finish);
727
+ resolve();
728
+ };
729
+ signal?.addEventListener("abort", finish, { once: true });
730
+ if (typeof requestAnimationFrame === "function") {
731
+ requestAnimationFrame(() => {
732
+ setTimeout(finish, 0);
733
+ });
734
+ return;
735
+ }
736
+ setTimeout(finish, 0);
737
+ });
738
+ }
739
+ async function resolveRagImageUrls(config, content, signal) {
740
+ const url = buildResolveImagesUrl(config);
741
+ const baseHeaders = buildRequestHeaders(config);
742
+ const headers = { "Content-Type": "application/json", ...baseHeaders };
743
+ const response = await fetch(url, {
744
+ method: "POST",
745
+ headers,
746
+ body: JSON.stringify({ input: content }),
747
+ signal
748
+ });
749
+ if (!response.ok) {
750
+ const errorText = await response.text();
751
+ throw new Error(`HTTP ${response.status}: ${errorText}`);
752
+ }
753
+ const text = await response.text();
754
+ try {
755
+ const parsed = JSON.parse(text);
756
+ if (typeof parsed === "string") return parsed;
757
+ if (typeof parsed.output === "string") return parsed.output;
758
+ if (typeof parsed.result === "string") return parsed.result;
759
+ } catch {
760
+ }
761
+ return text;
762
+ }
763
+
764
+ // src/hooks/useStreamManager.ts
702
765
  function useStreamManager(config, callbacks, setMessages, setIsWaitingForResponse) {
703
766
  const abortControllerRef = react.useRef(null);
704
767
  const configRef = react.useRef(config);
@@ -864,9 +927,11 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
864
927
  if (state.currentSessionId && state.currentSessionId !== sessionId) {
865
928
  callbacksRef.current.onSessionIdChange?.(state.currentSessionId);
866
929
  }
930
+ const needsImageResolve = !state.hasError && !abortController.signal.aborted && hasRagImages(state.accumulatedContent);
867
931
  const finalMessage = createFinalMessage(streamingId, {
868
932
  ...state,
869
- sessionId: state.currentSessionId || sessionId
933
+ sessionId: state.currentSessionId || sessionId,
934
+ isResolvingImages: needsImageResolve
870
935
  });
871
936
  setMessages(
872
937
  (prev) => prev.map(
@@ -877,6 +942,30 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
877
942
  }
878
943
  });
879
944
  clearThrottle();
945
+ const shouldResolveImages = !abortController.signal.aborted && !state.hasError && hasRagImages(state.accumulatedContent);
946
+ if (shouldResolveImages) {
947
+ await waitForNextPaint(abortController.signal);
948
+ }
949
+ if (shouldResolveImages && !abortController.signal.aborted) {
950
+ try {
951
+ const resolvedContent = await resolveRagImageUrls(
952
+ currentConfig,
953
+ state.accumulatedContent,
954
+ abortController.signal
955
+ );
956
+ setMessages(
957
+ (prev) => prev.map(
958
+ (msg) => msg.id === streamingId ? { ...msg, content: resolvedContent, isResolvingImages: false } : msg
959
+ )
960
+ );
961
+ } catch {
962
+ setMessages(
963
+ (prev) => prev.map(
964
+ (msg) => msg.id === streamingId ? { ...msg, isResolvingImages: false } : msg
965
+ )
966
+ );
967
+ }
968
+ }
880
969
  return state.currentSessionId;
881
970
  } catch (error) {
882
971
  clearThrottle();