@paymanai/payman-typescript-ask-sdk 4.0.26 → 4.0.27

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.js CHANGED
@@ -1148,6 +1148,9 @@ function isUserActionExpired(prompt) {
1148
1148
 
1149
1149
  // src/hooks/useChatV2.ts
1150
1150
  var EMPTY_USER_ACTION_STATE2 = { prompts: [], notifications: [] };
1151
+ function promptSlotKey(p) {
1152
+ return p.toolCallId || p.userActionId;
1153
+ }
1151
1154
  function upsertPrompt(prompts, req) {
1152
1155
  const idx = prompts.findIndex(
1153
1156
  (p) => req.toolCallId ? p.toolCallId === req.toolCallId : p.userActionId === req.userActionId
@@ -1194,6 +1197,7 @@ function useChatV2(config, callbacks = {}) {
1194
1197
  configRef.current = config;
1195
1198
  const messagesRef = react.useRef(messages);
1196
1199
  messagesRef.current = messages;
1200
+ const pendingSubmissionsRef = react.useRef(/* @__PURE__ */ new Map());
1197
1201
  const storeAwareSetMessages = react.useCallback(
1198
1202
  (updater) => {
1199
1203
  const streamUserId = streamUserIdRef.current;
@@ -1228,6 +1232,8 @@ function useChatV2(config, callbacks = {}) {
1228
1232
  if (!config.userId) return EMPTY_USER_ACTION_STATE2;
1229
1233
  return activeStreamStore.get(config.userId)?.userActionState ?? EMPTY_USER_ACTION_STATE2;
1230
1234
  });
1235
+ const userActionStateRef = react.useRef(userActionState);
1236
+ userActionStateRef.current = userActionState;
1231
1237
  const storeAwareSetUserActionState = react.useCallback(
1232
1238
  (updater) => {
1233
1239
  const streamUserId = streamUserIdRef.current;
@@ -1255,6 +1261,12 @@ function useChatV2(config, callbacks = {}) {
1255
1261
  onExecutionTraceClick: (data) => callbacksRef.current.onExecutionTraceClick?.(data),
1256
1262
  onSessionIdChange: (sessionId) => callbacksRef.current.onSessionIdChange?.(sessionId),
1257
1263
  onUserActionRequired: (request) => {
1264
+ const requestSlotKey = promptSlotKey(request);
1265
+ for (const [pendingId, slotKey] of pendingSubmissionsRef.current) {
1266
+ if (slotKey === requestSlotKey) {
1267
+ pendingSubmissionsRef.current.delete(pendingId);
1268
+ }
1269
+ }
1258
1270
  storeAwareSetUserActionState((prev) => ({
1259
1271
  ...prev,
1260
1272
  prompts: upsertPrompt(prev.prompts, request)
@@ -1266,6 +1278,28 @@ function useChatV2(config, callbacks = {}) {
1266
1278
  (prev) => prev.notifications.some((n) => n.id === notification.id) ? prev : { ...prev, notifications: [...prev.notifications, notification] }
1267
1279
  );
1268
1280
  callbacksRef.current.onUserNotification?.(notification);
1281
+ },
1282
+ // A submitted-but-unconfirmed prompt (see `submitUserAction`) is only
1283
+ // safe to drop once the stream has demonstrably moved on. `onEvent`
1284
+ // in useStreamManagerV2 calls `onStepsUpdate` for *every* processed
1285
+ // stream event, unconditionally — so the first time this fires
1286
+ // after a submission, the event in hand is either the rejection
1287
+ // retry (a fresh `USER_ACTION_REQUIRED`, which the handler above
1288
+ // already deleted this pending entry for, earlier in the very same
1289
+ // event) or genuine forward progress (RUN_IN_PROGRESS, a tool call,
1290
+ // content deltas, RUN_COMPLETED, ...). Anything still pending by
1291
+ // the time we get here therefore means the run resumed normally —
1292
+ // clear it.
1293
+ onStepsUpdate: (steps) => {
1294
+ if (pendingSubmissionsRef.current.size > 0) {
1295
+ const resolved = [...pendingSubmissionsRef.current.keys()];
1296
+ pendingSubmissionsRef.current.clear();
1297
+ storeAwareSetUserActionState((prev) => ({
1298
+ ...prev,
1299
+ prompts: prev.prompts.filter((p) => !resolved.includes(p.userActionId))
1300
+ }));
1301
+ }
1302
+ callbacksRef.current.onStepsUpdate?.(steps);
1269
1303
  }
1270
1304
  // eslint-disable-next-line react-hooks/exhaustive-deps
1271
1305
  }), []);
@@ -1401,6 +1435,7 @@ function useChatV2(config, callbacks = {}) {
1401
1435
  [storeAwareSetUserActionState]
1402
1436
  );
1403
1437
  const removePrompt = react.useCallback((userActionId) => {
1438
+ pendingSubmissionsRef.current.delete(userActionId);
1404
1439
  storeAwareSetUserActionState((prev) => ({
1405
1440
  ...prev,
1406
1441
  prompts: prev.prompts.filter((p) => p.userActionId !== userActionId)
@@ -1411,7 +1446,10 @@ function useChatV2(config, callbacks = {}) {
1411
1446
  setPromptStatus(userActionId, "submitting");
1412
1447
  try {
1413
1448
  await submitUserAction(configRef.current, userActionId, content);
1414
- removePrompt(userActionId);
1449
+ const prompt = userActionStateRef.current.prompts.find(
1450
+ (p) => p.userActionId === userActionId
1451
+ );
1452
+ pendingSubmissionsRef.current.set(userActionId, prompt ? promptSlotKey(prompt) : userActionId);
1415
1453
  } catch (error) {
1416
1454
  if (error instanceof UserActionStaleError) {
1417
1455
  setPromptStatus(userActionId, "stale");
@@ -1422,7 +1460,7 @@ function useChatV2(config, callbacks = {}) {
1422
1460
  throw error;
1423
1461
  }
1424
1462
  },
1425
- [removePrompt, setPromptStatus]
1463
+ [setPromptStatus]
1426
1464
  );
1427
1465
  const cancelUserAction2 = react.useCallback(
1428
1466
  async (userActionId) => {