@paymanai/payman-typescript-ask-sdk 4.0.4 → 4.0.6

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.
@@ -971,16 +971,40 @@ function createCancelledMessageUpdate(steps, currentMessage) {
971
971
  var DEFAULT_STREAM_ENDPOINT = "/api/playground/ask/stream";
972
972
  function buildRequestBody(config, userMessage, sessionId, options) {
973
973
  const sessionOwner = config.sessionParams;
974
+ const sessionOwnerId = sessionOwner?.id?.trim();
975
+ if (!sessionOwnerId) {
976
+ throw new Error("ChatConfig.sessionParams.id is required to send ask requests.");
977
+ }
974
978
  const sessionAttributes = sessionOwner?.attributes && Object.keys(sessionOwner.attributes).length > 0 ? sessionOwner.attributes : void 0;
975
979
  return {
976
980
  agentId: config.agentId,
977
981
  userInput: userMessage,
978
982
  sessionId,
983
+ sessionOwnerId,
979
984
  sessionOwnerLabel: sessionOwner?.name || void 0,
980
985
  sessionAttributes,
981
- analysisMode: options?.analysisMode
986
+ analysisMode: options?.analysisMode,
987
+ locale: resolveLocale(config.locale),
988
+ timezone: resolveTimezone(config.timezone)
982
989
  };
983
990
  }
991
+ function resolveLocale(configured) {
992
+ if (configured && configured.trim().length > 0) return configured.trim();
993
+ if (typeof navigator !== "undefined") {
994
+ const navLocale = navigator.language;
995
+ if (navLocale && navLocale.length > 0) return navLocale;
996
+ }
997
+ return "en-US";
998
+ }
999
+ function resolveTimezone(configured) {
1000
+ if (configured && configured.trim().length > 0) return configured.trim();
1001
+ try {
1002
+ const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
1003
+ if (zone && zone.length > 0) return zone;
1004
+ } catch {
1005
+ }
1006
+ return "UTC";
1007
+ }
984
1008
  function buildStreamingUrl(config) {
985
1009
  const endpoint = config.api.streamEndpoint || DEFAULT_STREAM_ENDPOINT;
986
1010
  const stage = config.stage || "DEV";
@@ -1197,16 +1221,16 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1197
1221
  )
1198
1222
  );
1199
1223
  };
1200
- const currentConfig = configRef.current;
1201
- const requestBody = buildRequestBody(
1202
- currentConfig,
1203
- userMessage,
1204
- sessionId,
1205
- options
1206
- );
1207
- const url = buildStreamingUrl(currentConfig);
1208
- const headers = buildRequestHeaders(currentConfig);
1209
1224
  try {
1225
+ const currentConfig = configRef.current;
1226
+ const requestBody = buildRequestBody(
1227
+ currentConfig,
1228
+ userMessage,
1229
+ sessionId,
1230
+ options
1231
+ );
1232
+ const url = buildStreamingUrl(currentConfig);
1233
+ const headers = buildRequestHeaders(currentConfig);
1210
1234
  await streamWorkflowEvents(url, requestBody, headers, {
1211
1235
  signal: abortController.signal,
1212
1236
  onEvent: (event) => {
@@ -1428,14 +1452,24 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1428
1452
  }
1429
1453
 
1430
1454
  // src/hooks/useChatV2.ts
1455
+ function getStoredOrInitialMessages(config) {
1456
+ if (!config.userId) return config.initialMessages ?? [];
1457
+ const stored = chatStore.get(config.userId);
1458
+ if (stored.length > 0) return stored;
1459
+ if (config.initialMessages?.length) {
1460
+ chatStore.set(config.userId, config.initialMessages);
1461
+ return config.initialMessages;
1462
+ }
1463
+ return [];
1464
+ }
1465
+ function getSessionIdFromMessages(messages) {
1466
+ return messages.find((message) => message.sessionId)?.sessionId;
1467
+ }
1431
1468
  function useChatV2(config, callbacks = {}) {
1432
- const [messages, setMessages] = react.useState(() => {
1433
- if (config.userId) return chatStore.get(config.userId);
1434
- return config.initialMessages ?? [];
1435
- });
1469
+ const [messages, setMessages] = react.useState(() => getStoredOrInitialMessages(config));
1436
1470
  const [isWaitingForResponse, setIsWaitingForResponse] = react.useState(false);
1437
1471
  const sessionIdRef = react.useRef(
1438
- config.userId ? chatStore.get(config.userId).find((m) => m.sessionId)?.sessionId ?? config.initialSessionId ?? void 0 : config.initialSessionId ?? void 0
1472
+ getSessionIdFromMessages(getStoredOrInitialMessages(config)) ?? config.initialSessionId ?? void 0
1439
1473
  );
1440
1474
  const prevUserIdRef = react.useRef(config.userId);
1441
1475
  const callbacksRef = react.useRef(callbacks);
@@ -1679,7 +1713,7 @@ function useChatV2(config, callbacks = {}) {
1679
1713
  setIsWaitingForResponse(active.isWaiting);
1680
1714
  }
1681
1715
  return unsubscribe;
1682
- }, []);
1716
+ }, [config.userId]);
1683
1717
  react.useEffect(() => {
1684
1718
  if (!config.userId) return;
1685
1719
  const toSave = messages.filter((m) => !m.isStreaming);
@@ -1687,6 +1721,13 @@ function useChatV2(config, callbacks = {}) {
1687
1721
  chatStore.set(config.userId, toSave);
1688
1722
  }
1689
1723
  }, [messages, config.userId]);
1724
+ react.useEffect(() => {
1725
+ if (!config.userId || activeStreamStore.has(config.userId)) return;
1726
+ if (!config.initialMessages?.length || messagesRef.current.length > 0) return;
1727
+ chatStore.set(config.userId, config.initialMessages);
1728
+ setMessages(config.initialMessages);
1729
+ sessionIdRef.current = getSessionIdFromMessages(config.initialMessages) ?? config.initialSessionId;
1730
+ }, [config.initialMessages, config.initialSessionId, config.userId]);
1690
1731
  react.useEffect(() => {
1691
1732
  const prevUserId = prevUserIdRef.current;
1692
1733
  prevUserIdRef.current = config.userId;
@@ -1698,11 +1739,19 @@ function useChatV2(config, callbacks = {}) {
1698
1739
  setIsWaitingForResponse(false);
1699
1740
  setUserActionState({ request: null, result: null, clearOtpTrigger: 0 });
1700
1741
  } else if (config.userId) {
1701
- const stored = chatStore.get(config.userId);
1702
- setMessages(stored);
1703
- sessionIdRef.current = stored.find((m) => m.sessionId)?.sessionId;
1742
+ const active = activeStreamStore.get(config.userId);
1743
+ if (active) {
1744
+ setMessages(active.messages);
1745
+ setIsWaitingForResponse(active.isWaiting);
1746
+ sessionIdRef.current = getSessionIdFromMessages(active.messages) ?? config.initialSessionId;
1747
+ return;
1748
+ }
1749
+ const nextMessages = getStoredOrInitialMessages(config);
1750
+ setMessages(nextMessages);
1751
+ sessionIdRef.current = getSessionIdFromMessages(nextMessages) ?? config.initialSessionId;
1752
+ setIsWaitingForResponse(false);
1704
1753
  }
1705
- }, [config.userId]);
1754
+ }, [config]);
1706
1755
  return {
1707
1756
  messages,
1708
1757
  sendMessage,