@paymanai/payman-typescript-ask-sdk 1.2.0 → 1.2.2

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
@@ -89,6 +89,8 @@ type StreamingStep = {
89
89
  status: "in_progress" | "completed" | "error" | "pending";
90
90
  timestamp: number;
91
91
  elapsedMs?: number;
92
+ thinkingText?: string;
93
+ isThinking?: boolean;
92
94
  };
93
95
  type ChunkDisplay = {
94
96
  id?: string;
@@ -117,6 +119,10 @@ type MessageDisplay = {
117
119
  currentExecutingStepId?: string;
118
120
  /** Result of user action (OTP approval/rejection) */
119
121
  userActionResult?: UserActionResult;
122
+ /** Current thinking block text (live, resets per block) */
123
+ activeThinkingText?: string;
124
+ /** All thinking accumulated across blocks (for post-stream display) */
125
+ allThinkingText?: string;
120
126
  };
121
127
  type SessionParams = {
122
128
  id?: string;
package/dist/index.d.ts CHANGED
@@ -89,6 +89,8 @@ type StreamingStep = {
89
89
  status: "in_progress" | "completed" | "error" | "pending";
90
90
  timestamp: number;
91
91
  elapsedMs?: number;
92
+ thinkingText?: string;
93
+ isThinking?: boolean;
92
94
  };
93
95
  type ChunkDisplay = {
94
96
  id?: string;
@@ -117,6 +119,10 @@ type MessageDisplay = {
117
119
  currentExecutingStepId?: string;
118
120
  /** Result of user action (OTP approval/rejection) */
119
121
  userActionResult?: UserActionResult;
122
+ /** Current thinking block text (live, resets per block) */
123
+ activeThinkingText?: string;
124
+ /** All thinking accumulated across blocks (for post-stream display) */
125
+ allThinkingText?: string;
120
126
  };
121
127
  type SessionParams = {
122
128
  id?: string;
package/dist/index.js CHANGED
@@ -158,6 +158,10 @@ function getEventMessage(event) {
158
158
  return "Verification code resent";
159
159
  case "USER_ACTION_FAILED":
160
160
  return "Verification failed";
161
+ case "INTENT_THINKING":
162
+ return event.workerName ? `${event.workerName} thinking...` : "Thinking...";
163
+ case "INTENT_THINKING_CONT":
164
+ return event.message || "";
161
165
  default:
162
166
  return eventType;
163
167
  }
@@ -195,6 +199,16 @@ function completeLastInProgressStep(steps) {
195
199
  function processStreamEvent(event, state) {
196
200
  const eventType = event.eventType;
197
201
  const message = getEventMessage(event);
202
+ if (eventType !== "INTENT_THINKING" && eventType !== "INTENT_THINKING_CONT") {
203
+ if (state.currentThinkingStepId) {
204
+ const thinkingStep = state.steps.find((s) => s.id === state.currentThinkingStepId);
205
+ if (thinkingStep) {
206
+ thinkingStep.isThinking = false;
207
+ }
208
+ state.currentThinkingStepId = void 0;
209
+ }
210
+ state.activeThinkingText = void 0;
211
+ }
198
212
  if ((eventType === "COMPLETED" || eventType === "WORKFLOW_COMPLETED") && event.response !== void 0) {
199
213
  const content = extractResponseContent(event.response);
200
214
  if (content) {
@@ -395,6 +409,33 @@ function processStreamEvent(event, state) {
395
409
  timestamp: Date.now(),
396
410
  elapsedMs: event.elapsedMs
397
411
  });
412
+ } else if (eventType === "INTENT_THINKING") {
413
+ if (state.currentThinkingStepId) {
414
+ const prev = state.steps.find((s) => s.id === state.currentThinkingStepId);
415
+ if (prev) prev.isThinking = false;
416
+ }
417
+ const lastInProgress = [...state.steps].reverse().find((s) => s.status === "in_progress");
418
+ if (lastInProgress) {
419
+ lastInProgress.thinkingText = "";
420
+ lastInProgress.isThinking = true;
421
+ state.currentThinkingStepId = lastInProgress.id;
422
+ } else {
423
+ state.currentThinkingStepId = void 0;
424
+ }
425
+ state.activeThinkingText = "";
426
+ if (state.allThinkingText) state.allThinkingText += "\n\n";
427
+ } else if (eventType === "INTENT_THINKING_CONT") {
428
+ const delta = event.message || "";
429
+ if (!delta) return state;
430
+ if (state.currentThinkingStepId) {
431
+ const step = state.steps.find((s) => s.id === state.currentThinkingStepId);
432
+ if (step) {
433
+ step.thinkingText = (step.thinkingText || "") + delta;
434
+ }
435
+ }
436
+ if (state.activeThinkingText == null) state.activeThinkingText = "";
437
+ state.activeThinkingText += delta;
438
+ state.allThinkingText += delta;
398
439
  }
399
440
  return state;
400
441
  }
@@ -418,7 +459,9 @@ ${state.errorMessage}` : "",
418
459
  steps: state.hasError ? [] : [...state.steps],
419
460
  currentExecutingStepId: state.hasError ? void 0 : state.currentExecutingStepId,
420
461
  isCancelled: false,
421
- userActionResult: state.userActionResult
462
+ userActionResult: state.userActionResult,
463
+ activeThinkingText: state.hasError ? void 0 : state.activeThinkingText,
464
+ allThinkingText: state.hasError ? void 0 : state.allThinkingText
422
465
  };
423
466
  }
424
467
  function createErrorMessageUpdate(error, state) {
@@ -461,7 +504,9 @@ ${state.errorMessage}` : state.accumulatedContent || "",
461
504
  steps: state.hasError ? [] : [...state.steps],
462
505
  isCancelled: false,
463
506
  currentExecutingStepId: void 0,
464
- userActionResult: state.userActionResult
507
+ userActionResult: state.userActionResult,
508
+ activeThinkingText: void 0,
509
+ allThinkingText: state.hasError ? void 0 : state.allThinkingText
465
510
  };
466
511
  }
467
512
  function createCancelledMessageUpdate(steps, currentMessage) {
@@ -570,11 +615,61 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
570
615
  steps: [],
571
616
  stepCounter: 0,
572
617
  currentExecutingStepId: void 0,
618
+ currentThinkingStepId: void 0,
573
619
  hasError: false,
574
620
  errorMessage: "",
575
621
  userActionRequest: void 0,
576
622
  userActionPending: false,
577
- userActionResult: void 0
623
+ userActionResult: void 0,
624
+ allThinkingText: ""
625
+ };
626
+ const THROTTLE_MS = 120;
627
+ const CHARS_PER_TICK = 10;
628
+ const displayedLengthRef = { current: 0 };
629
+ let throttleIntervalId = null;
630
+ const getActiveStepMessage = () => state.steps.find((s) => s.id === state.currentExecutingStepId)?.message || [...state.steps].reverse().find((s) => s.status === "in_progress")?.message;
631
+ const advanceDisplayLength = (full) => {
632
+ let newLen = Math.min(displayedLengthRef.current + CHARS_PER_TICK, full.length);
633
+ if (newLen > 0 && newLen < full.length) {
634
+ const code = full.charCodeAt(newLen - 1);
635
+ if (code >= 55296 && code <= 56319) {
636
+ newLen = Math.min(newLen + 1, full.length);
637
+ }
638
+ }
639
+ displayedLengthRef.current = newLen;
640
+ };
641
+ const clearThrottle = () => {
642
+ if (throttleIntervalId != null) {
643
+ clearInterval(throttleIntervalId);
644
+ throttleIntervalId = null;
645
+ }
646
+ };
647
+ abortController.signal.addEventListener("abort", clearThrottle, { once: true });
648
+ const ensureThrottle = () => {
649
+ if (throttleIntervalId != null) return;
650
+ throttleIntervalId = setInterval(() => {
651
+ if (abortController.signal.aborted) {
652
+ clearThrottle();
653
+ return;
654
+ }
655
+ const full = state.activeThinkingText ?? "";
656
+ if (displayedLengthRef.current < full.length) {
657
+ advanceDisplayLength(full);
658
+ const displayText = full.slice(0, displayedLengthRef.current);
659
+ setMessages(
660
+ (prev) => prev.map(
661
+ (msg) => msg.id === streamingId ? {
662
+ ...msg,
663
+ ...createStreamingMessageUpdate({
664
+ ...state,
665
+ activeThinkingText: displayText,
666
+ currentMessage: getActiveStepMessage() || "Thinking..."
667
+ })
668
+ } : msg
669
+ )
670
+ );
671
+ }
672
+ }, THROTTLE_MS);
578
673
  };
579
674
  const currentConfig = configRef.current;
580
675
  const requestBody = buildRequestBody(currentConfig, userMessage, sessionId);
@@ -589,22 +684,33 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
589
684
  }
590
685
  if (event.executionId) state.executionId = event.executionId;
591
686
  if (event.sessionId) state.currentSessionId = event.sessionId;
687
+ const activeThinkingLengthBeforeProcess = state.activeThinkingText?.length ?? 0;
592
688
  processStreamEvent(event, state);
593
689
  const eventType = event.eventType;
690
+ if (eventType === "INTENT_THINKING") {
691
+ displayedLengthRef.current = 0;
692
+ ensureThrottle();
693
+ } else if (eventType !== "INTENT_THINKING_CONT") {
694
+ displayedLengthRef.current = activeThinkingLengthBeforeProcess;
695
+ clearThrottle();
696
+ }
594
697
  if (eventType === "USER_ACTION_REQUIRED" && state.userActionRequest) {
595
698
  callbacksRef.current.onUserActionRequired?.(state.userActionRequest);
596
699
  } else if (eventType.startsWith("USER_ACTION_") && eventType !== "USER_ACTION_REQUIRED") {
597
700
  const msg = event.message?.trim() || event.errorMessage?.trim() || getEventMessage(event);
598
701
  callbacksRef.current.onUserActionEvent?.(eventType, msg);
599
702
  }
703
+ const isIntentThinkingEvent = eventType === "INTENT_THINKING" || eventType === "INTENT_THINKING_CONT";
600
704
  const rawMessage = event.message?.trim() || event.errorMessage?.trim();
601
- const currentMessage = rawMessage || (event.eventType?.startsWith("USER_ACTION_") ? getEventMessage(event) : void 0);
705
+ const currentMessage = isIntentThinkingEvent ? getActiveStepMessage() || "Thinking..." : rawMessage || (event.eventType?.startsWith("USER_ACTION_") ? getEventMessage(event) : void 0);
706
+ const displayThinking = state.activeThinkingText != null ? state.activeThinkingText.slice(0, displayedLengthRef.current) : state.activeThinkingText;
602
707
  setMessages(
603
708
  (prev) => prev.map(
604
709
  (msg) => msg.id === streamingId ? {
605
710
  ...msg,
606
711
  ...createStreamingMessageUpdate({
607
712
  ...state,
713
+ activeThinkingText: displayThinking,
608
714
  currentMessage
609
715
  })
610
716
  } : msg
@@ -612,10 +718,19 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
612
718
  );
613
719
  },
614
720
  onError: (error) => {
721
+ clearThrottle();
615
722
  setIsWaitingForResponse(false);
616
723
  if (error.name !== "AbortError") {
617
724
  callbacksRef.current.onError?.(error);
618
725
  }
726
+ if (state.userActionPending) {
727
+ state.userActionPending = false;
728
+ state.userActionRequest = void 0;
729
+ callbacksRef.current.onUserActionEvent?.(
730
+ "USER_ACTION_FAILED",
731
+ "Connection lost. Please try again."
732
+ );
733
+ }
619
734
  setMessages(
620
735
  (prev) => prev.map(
621
736
  (msg) => msg.id === streamingId ? {
@@ -626,7 +741,16 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
626
741
  );
627
742
  },
628
743
  onComplete: () => {
744
+ clearThrottle();
629
745
  setIsWaitingForResponse(false);
746
+ if (state.userActionPending) {
747
+ state.userActionPending = false;
748
+ state.userActionRequest = void 0;
749
+ callbacksRef.current.onUserActionEvent?.(
750
+ "USER_ACTION_FAILED",
751
+ "Verification could not be completed."
752
+ );
753
+ }
630
754
  if (state.currentSessionId && state.currentSessionId !== sessionId) {
631
755
  callbacksRef.current.onSessionIdChange?.(state.currentSessionId);
632
756
  }
@@ -642,12 +766,22 @@ function useStreamManager(config, callbacks, setMessages, setIsWaitingForRespons
642
766
  callbacksRef.current.onStreamComplete?.(finalMessage);
643
767
  }
644
768
  });
769
+ clearThrottle();
645
770
  return state.currentSessionId;
646
771
  } catch (error) {
772
+ clearThrottle();
647
773
  setIsWaitingForResponse(false);
648
774
  if (error.name !== "AbortError") {
649
775
  callbacksRef.current.onError?.(error);
650
776
  }
777
+ if (state.userActionPending) {
778
+ state.userActionPending = false;
779
+ state.userActionRequest = void 0;
780
+ callbacksRef.current.onUserActionEvent?.(
781
+ "USER_ACTION_FAILED",
782
+ "Connection lost. Please try again."
783
+ );
784
+ }
651
785
  setMessages(
652
786
  (prev) => prev.map(
653
787
  (msg) => msg.id === streamingId ? {