@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.
package/dist/index.d.mts CHANGED
@@ -150,7 +150,8 @@ type MessageDisplay = {
150
150
  totalElapsedMs?: number;
151
151
  };
152
152
  type SessionParams = {
153
- id?: string;
153
+ /** Session owner ID sent to the ask API as `sessionOwnerId`. */
154
+ id: string;
154
155
  name?: string;
155
156
  attributes?: Record<string, string>;
156
157
  };
@@ -171,14 +172,28 @@ type ChatConfig = {
171
172
  api: APIConfig;
172
173
  /** Agent ID to run against the k2 playground endpoint */
173
174
  agentId: string;
174
- /** User ID for chatStore and activeStreamStore — required for context store messages management. Set to undefined on logout to clear stored messages. */
175
+ /** User ID for local chatStore and activeStreamStore only. This is not sent to the ask API. */
175
176
  userId?: string;
176
177
  /** Stage/Environment */
177
178
  stage?: AgentStage;
178
179
  /** Query param name for stage (default: "stage"). */
179
180
  stageQueryParam?: string;
180
- /** Session params */
181
- sessionParams?: SessionParams;
181
+ /** Session owner params sent with ask requests. */
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;
182
197
  /** Custom placeholder text */
183
198
  placeholder?: string;
184
199
  /** Empty state text */
package/dist/index.d.ts CHANGED
@@ -150,7 +150,8 @@ type MessageDisplay = {
150
150
  totalElapsedMs?: number;
151
151
  };
152
152
  type SessionParams = {
153
- id?: string;
153
+ /** Session owner ID sent to the ask API as `sessionOwnerId`. */
154
+ id: string;
154
155
  name?: string;
155
156
  attributes?: Record<string, string>;
156
157
  };
@@ -171,14 +172,28 @@ type ChatConfig = {
171
172
  api: APIConfig;
172
173
  /** Agent ID to run against the k2 playground endpoint */
173
174
  agentId: string;
174
- /** User ID for chatStore and activeStreamStore — required for context store messages management. Set to undefined on logout to clear stored messages. */
175
+ /** User ID for local chatStore and activeStreamStore only. This is not sent to the ask API. */
175
176
  userId?: string;
176
177
  /** Stage/Environment */
177
178
  stage?: AgentStage;
178
179
  /** Query param name for stage (default: "stage"). */
179
180
  stageQueryParam?: string;
180
- /** Session params */
181
- sessionParams?: SessionParams;
181
+ /** Session owner params sent with ask requests. */
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;
182
197
  /** Custom placeholder text */
183
198
  placeholder?: string;
184
199
  /** Empty state text */
package/dist/index.js CHANGED
@@ -954,16 +954,40 @@ function createCancelledMessageUpdate(steps, currentMessage) {
954
954
  var DEFAULT_STREAM_ENDPOINT = "/api/playground/ask/stream";
955
955
  function buildRequestBody(config, userMessage, sessionId, options) {
956
956
  const sessionOwner = config.sessionParams;
957
+ const sessionOwnerId = sessionOwner?.id?.trim();
958
+ if (!sessionOwnerId) {
959
+ throw new Error("ChatConfig.sessionParams.id is required to send ask requests.");
960
+ }
957
961
  const sessionAttributes = sessionOwner?.attributes && Object.keys(sessionOwner.attributes).length > 0 ? sessionOwner.attributes : void 0;
958
962
  return {
959
963
  agentId: config.agentId,
960
964
  userInput: userMessage,
961
965
  sessionId,
966
+ sessionOwnerId,
962
967
  sessionOwnerLabel: sessionOwner?.name || void 0,
963
968
  sessionAttributes,
964
- analysisMode: options?.analysisMode
969
+ analysisMode: options?.analysisMode,
970
+ locale: resolveLocale(config.locale),
971
+ timezone: resolveTimezone(config.timezone)
965
972
  };
966
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
+ }
967
991
  function buildStreamingUrl(config) {
968
992
  const endpoint = config.api.streamEndpoint || DEFAULT_STREAM_ENDPOINT;
969
993
  const stage = config.stage || "DEV";
@@ -1176,16 +1200,16 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1176
1200
  )
1177
1201
  );
1178
1202
  };
1179
- const currentConfig = configRef.current;
1180
- const requestBody = buildRequestBody(
1181
- currentConfig,
1182
- userMessage,
1183
- sessionId,
1184
- options
1185
- );
1186
- const url = buildStreamingUrl(currentConfig);
1187
- const headers = buildRequestHeaders(currentConfig);
1188
1203
  try {
1204
+ const currentConfig = configRef.current;
1205
+ const requestBody = buildRequestBody(
1206
+ currentConfig,
1207
+ userMessage,
1208
+ sessionId,
1209
+ options
1210
+ );
1211
+ const url = buildStreamingUrl(currentConfig);
1212
+ const headers = buildRequestHeaders(currentConfig);
1189
1213
  await streamWorkflowEvents(url, requestBody, headers, {
1190
1214
  signal: abortController.signal,
1191
1215
  onEvent: (event) => {
@@ -1407,14 +1431,24 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1407
1431
  }
1408
1432
 
1409
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
+ }
1410
1447
  function useChatV2(config, callbacks = {}) {
1411
- const [messages, setMessages] = react.useState(() => {
1412
- if (config.userId) return chatStore.get(config.userId);
1413
- return config.initialMessages ?? [];
1414
- });
1448
+ const [messages, setMessages] = react.useState(() => getStoredOrInitialMessages(config));
1415
1449
  const [isWaitingForResponse, setIsWaitingForResponse] = react.useState(false);
1416
1450
  const sessionIdRef = react.useRef(
1417
- 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
1418
1452
  );
1419
1453
  const prevUserIdRef = react.useRef(config.userId);
1420
1454
  const callbacksRef = react.useRef(callbacks);
@@ -1658,7 +1692,7 @@ function useChatV2(config, callbacks = {}) {
1658
1692
  setIsWaitingForResponse(active.isWaiting);
1659
1693
  }
1660
1694
  return unsubscribe;
1661
- }, []);
1695
+ }, [config.userId]);
1662
1696
  react.useEffect(() => {
1663
1697
  if (!config.userId) return;
1664
1698
  const toSave = messages.filter((m) => !m.isStreaming);
@@ -1666,6 +1700,13 @@ function useChatV2(config, callbacks = {}) {
1666
1700
  chatStore.set(config.userId, toSave);
1667
1701
  }
1668
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]);
1669
1710
  react.useEffect(() => {
1670
1711
  const prevUserId = prevUserIdRef.current;
1671
1712
  prevUserIdRef.current = config.userId;
@@ -1677,11 +1718,19 @@ function useChatV2(config, callbacks = {}) {
1677
1718
  setIsWaitingForResponse(false);
1678
1719
  setUserActionState({ request: null, result: null, clearOtpTrigger: 0 });
1679
1720
  } else if (config.userId) {
1680
- const stored = chatStore.get(config.userId);
1681
- setMessages(stored);
1682
- 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);
1683
1732
  }
1684
- }, [config.userId]);
1733
+ }, [config]);
1685
1734
  return {
1686
1735
  messages,
1687
1736
  sendMessage,