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