@paymanai/payman-typescript-ask-sdk 4.0.5 → 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.
package/dist/index.d.mts CHANGED
@@ -180,6 +180,20 @@ type ChatConfig = {
180
180
  stageQueryParam?: string;
181
181
  /** Session owner params sent with ask requests. */
182
182
  sessionParams: SessionParams;
183
+ /**
184
+ * BCP-47 language tag forwarded to the server as `locale` (e.g. "en-US",
185
+ * "ja-JP"). Defaults to `navigator.language` in the browser and falls
186
+ * back to "en-US" if neither is available. The server uses this to
187
+ * localize agent responses; required field on the playground endpoint.
188
+ */
189
+ locale?: string;
190
+ /**
191
+ * IANA timezone id forwarded to the server as `timezone` (e.g.
192
+ * "America/Los_Angeles"). Defaults to the runtime's resolved zone via
193
+ * `Intl.DateTimeFormat().resolvedOptions().timeZone`; falls back to
194
+ * "UTC" if Intl is unavailable.
195
+ */
196
+ timezone?: string;
183
197
  /** Custom placeholder text */
184
198
  placeholder?: string;
185
199
  /** Empty state text */
package/dist/index.d.ts CHANGED
@@ -180,6 +180,20 @@ type ChatConfig = {
180
180
  stageQueryParam?: string;
181
181
  /** Session owner params sent with ask requests. */
182
182
  sessionParams: SessionParams;
183
+ /**
184
+ * BCP-47 language tag forwarded to the server as `locale` (e.g. "en-US",
185
+ * "ja-JP"). Defaults to `navigator.language` in the browser and falls
186
+ * back to "en-US" if neither is available. The server uses this to
187
+ * localize agent responses; required field on the playground endpoint.
188
+ */
189
+ locale?: string;
190
+ /**
191
+ * IANA timezone id forwarded to the server as `timezone` (e.g.
192
+ * "America/Los_Angeles"). Defaults to the runtime's resolved zone via
193
+ * `Intl.DateTimeFormat().resolvedOptions().timeZone`; falls back to
194
+ * "UTC" if Intl is unavailable.
195
+ */
196
+ timezone?: string;
183
197
  /** Custom placeholder text */
184
198
  placeholder?: string;
185
199
  /** Empty state text */
package/dist/index.js CHANGED
@@ -966,9 +966,28 @@ function buildRequestBody(config, userMessage, sessionId, options) {
966
966
  sessionOwnerId,
967
967
  sessionOwnerLabel: sessionOwner?.name || void 0,
968
968
  sessionAttributes,
969
- analysisMode: options?.analysisMode
969
+ analysisMode: options?.analysisMode,
970
+ locale: resolveLocale(config.locale),
971
+ timezone: resolveTimezone(config.timezone)
970
972
  };
971
973
  }
974
+ function resolveLocale(configured) {
975
+ if (configured && configured.trim().length > 0) return configured.trim();
976
+ if (typeof navigator !== "undefined") {
977
+ const navLocale = navigator.language;
978
+ if (navLocale && navLocale.length > 0) return navLocale;
979
+ }
980
+ return "en-US";
981
+ }
982
+ function resolveTimezone(configured) {
983
+ if (configured && configured.trim().length > 0) return configured.trim();
984
+ try {
985
+ const zone = Intl.DateTimeFormat().resolvedOptions().timeZone;
986
+ if (zone && zone.length > 0) return zone;
987
+ } catch {
988
+ }
989
+ return "UTC";
990
+ }
972
991
  function buildStreamingUrl(config) {
973
992
  const endpoint = config.api.streamEndpoint || DEFAULT_STREAM_ENDPOINT;
974
993
  const stage = config.stage || "DEV";
@@ -1412,14 +1431,24 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1412
1431
  }
1413
1432
 
1414
1433
  // src/hooks/useChatV2.ts
1434
+ function getStoredOrInitialMessages(config) {
1435
+ if (!config.userId) return config.initialMessages ?? [];
1436
+ const stored = chatStore.get(config.userId);
1437
+ if (stored.length > 0) return stored;
1438
+ if (config.initialMessages?.length) {
1439
+ chatStore.set(config.userId, config.initialMessages);
1440
+ return config.initialMessages;
1441
+ }
1442
+ return [];
1443
+ }
1444
+ function getSessionIdFromMessages(messages) {
1445
+ return messages.find((message) => message.sessionId)?.sessionId;
1446
+ }
1415
1447
  function useChatV2(config, callbacks = {}) {
1416
- const [messages, setMessages] = react.useState(() => {
1417
- if (config.userId) return chatStore.get(config.userId);
1418
- return config.initialMessages ?? [];
1419
- });
1448
+ const [messages, setMessages] = react.useState(() => getStoredOrInitialMessages(config));
1420
1449
  const [isWaitingForResponse, setIsWaitingForResponse] = react.useState(false);
1421
1450
  const sessionIdRef = react.useRef(
1422
- config.userId ? chatStore.get(config.userId).find((m) => m.sessionId)?.sessionId ?? config.initialSessionId ?? void 0 : config.initialSessionId ?? void 0
1451
+ getSessionIdFromMessages(getStoredOrInitialMessages(config)) ?? config.initialSessionId ?? void 0
1423
1452
  );
1424
1453
  const prevUserIdRef = react.useRef(config.userId);
1425
1454
  const callbacksRef = react.useRef(callbacks);
@@ -1663,7 +1692,7 @@ function useChatV2(config, callbacks = {}) {
1663
1692
  setIsWaitingForResponse(active.isWaiting);
1664
1693
  }
1665
1694
  return unsubscribe;
1666
- }, []);
1695
+ }, [config.userId]);
1667
1696
  react.useEffect(() => {
1668
1697
  if (!config.userId) return;
1669
1698
  const toSave = messages.filter((m) => !m.isStreaming);
@@ -1671,6 +1700,13 @@ function useChatV2(config, callbacks = {}) {
1671
1700
  chatStore.set(config.userId, toSave);
1672
1701
  }
1673
1702
  }, [messages, config.userId]);
1703
+ react.useEffect(() => {
1704
+ if (!config.userId || activeStreamStore.has(config.userId)) return;
1705
+ if (!config.initialMessages?.length || messagesRef.current.length > 0) return;
1706
+ chatStore.set(config.userId, config.initialMessages);
1707
+ setMessages(config.initialMessages);
1708
+ sessionIdRef.current = getSessionIdFromMessages(config.initialMessages) ?? config.initialSessionId;
1709
+ }, [config.initialMessages, config.initialSessionId, config.userId]);
1674
1710
  react.useEffect(() => {
1675
1711
  const prevUserId = prevUserIdRef.current;
1676
1712
  prevUserIdRef.current = config.userId;
@@ -1682,11 +1718,19 @@ function useChatV2(config, callbacks = {}) {
1682
1718
  setIsWaitingForResponse(false);
1683
1719
  setUserActionState({ request: null, result: null, clearOtpTrigger: 0 });
1684
1720
  } else if (config.userId) {
1685
- const stored = chatStore.get(config.userId);
1686
- setMessages(stored);
1687
- sessionIdRef.current = stored.find((m) => m.sessionId)?.sessionId;
1721
+ const active = activeStreamStore.get(config.userId);
1722
+ if (active) {
1723
+ setMessages(active.messages);
1724
+ setIsWaitingForResponse(active.isWaiting);
1725
+ sessionIdRef.current = getSessionIdFromMessages(active.messages) ?? config.initialSessionId;
1726
+ return;
1727
+ }
1728
+ const nextMessages = getStoredOrInitialMessages(config);
1729
+ setMessages(nextMessages);
1730
+ sessionIdRef.current = getSessionIdFromMessages(nextMessages) ?? config.initialSessionId;
1731
+ setIsWaitingForResponse(false);
1688
1732
  }
1689
- }, [config.userId]);
1733
+ }, [config]);
1690
1734
  return {
1691
1735
  messages,
1692
1736
  sendMessage,