@paymanai/payman-typescript-ask-sdk 4.0.10 → 4.0.13

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
@@ -13,10 +13,95 @@ function generateId() {
13
13
  });
14
14
  }
15
15
 
16
+ // src/utils/chatStore.ts
17
+ var memoryStore = /* @__PURE__ */ new Map();
18
+ var chatStore = {
19
+ get(key) {
20
+ return memoryStore.get(key) ?? [];
21
+ },
22
+ set(key, messages) {
23
+ memoryStore.set(key, messages);
24
+ },
25
+ delete(key) {
26
+ memoryStore.delete(key);
27
+ }
28
+ };
29
+
30
+ // src/utils/activeStreamStore.ts
31
+ var streams = /* @__PURE__ */ new Map();
32
+ var activeStreamStore = {
33
+ has(key) {
34
+ return streams.has(key);
35
+ },
36
+ get(key) {
37
+ const entry = streams.get(key);
38
+ if (!entry) return null;
39
+ return { messages: entry.messages, isWaiting: entry.isWaiting };
40
+ },
41
+ // Called before startStream — registers the controller and initial messages
42
+ start(key, abortController, initialMessages) {
43
+ const existing = streams.get(key);
44
+ streams.set(key, {
45
+ messages: initialMessages,
46
+ isWaiting: true,
47
+ abortController,
48
+ listeners: existing?.listeners ?? /* @__PURE__ */ new Set()
49
+ });
50
+ },
51
+ // Called by the stream on every event — applies the same updater pattern React uses
52
+ applyMessages(key, updater) {
53
+ const entry = streams.get(key);
54
+ if (!entry) return;
55
+ const next = typeof updater === "function" ? updater(entry.messages) : updater;
56
+ entry.messages = next;
57
+ entry.listeners.forEach((l) => l(next, entry.isWaiting));
58
+ },
59
+ setWaiting(key, waiting) {
60
+ const entry = streams.get(key);
61
+ if (!entry) return;
62
+ entry.isWaiting = waiting;
63
+ entry.listeners.forEach((l) => l(entry.messages, waiting));
64
+ },
65
+ // Called when stream completes — persists to chatStore and cleans up
66
+ complete(key) {
67
+ const entry = streams.get(key);
68
+ if (!entry) return;
69
+ entry.isWaiting = false;
70
+ entry.listeners.forEach((l) => l(entry.messages, false));
71
+ const toSave = entry.messages.filter((m) => !m.isStreaming);
72
+ if (toSave.length > 0) chatStore.set(key, toSave);
73
+ streams.delete(key);
74
+ },
75
+ // Subscribe — returns unsubscribe fn. Component calls this on mount, cleanup on unmount.
76
+ subscribe(key, listener) {
77
+ const entry = streams.get(key);
78
+ if (!entry) return () => {
79
+ };
80
+ entry.listeners.add(listener);
81
+ return () => {
82
+ streams.get(key)?.listeners.delete(listener);
83
+ };
84
+ },
85
+ // Rename an entry — used when the server assigns a new session ID mid-stream
86
+ rename(oldKey, newKey) {
87
+ const entry = streams.get(oldKey);
88
+ if (!entry) return;
89
+ streams.set(newKey, entry);
90
+ streams.delete(oldKey);
91
+ },
92
+ // Explicit user cancel — aborts the controller and removes the entry
93
+ abort(key) {
94
+ const entry = streams.get(key);
95
+ if (!entry) return;
96
+ entry.abortController.abort();
97
+ streams.delete(key);
98
+ }
99
+ };
100
+
16
101
  // src/utils/streamingClient.ts
17
102
  function yieldAfterProgressEvent(event) {
18
103
  const t = event.eventType;
19
- if (t === "RUN_IN_PROGRESS" || t === "INTENT_PROGRESS" || t === "AGGREGATOR_THINKING_CONT" || t === "INTENT_THINKING_CONT" || t === "THINKING_DELTA") {
104
+ if (t === "RUN_IN_PROGRESS" || t === "INTENT_PROGRESS" || t === "THINKING_DELTA") {
20
105
  return new Promise((resolve) => setTimeout(resolve, 0));
21
106
  }
22
107
  return Promise.resolve();
@@ -276,40 +361,10 @@ function classifyUserActionKind(action, schema) {
276
361
  return isVerificationSchema(schema) ? "verification" : "form";
277
362
  }
278
363
  }
279
- function userActionHeader(kind) {
280
- return kind === "verification" ? "**Verification required**" : "**Action required**";
281
- }
282
- function workingPhaseDetailForDisplay(raw) {
283
- const t = raw.trim();
284
- if (!t) return "";
285
- if (/^Identified\s+\d+\s+tasks?\s+to\s+execute\.?$/i.test(t)) {
286
- return "";
287
- }
288
- return t;
289
- }
290
364
  function getEventText(event, field) {
291
365
  const value = event[field];
292
366
  return typeof value === "string" ? value.trim() : "";
293
367
  }
294
- function shouldShowIntentHeader(event) {
295
- const workerName = getEventText(event, "workerName");
296
- const intentId = getEventText(event, "intentId");
297
- return Boolean(workerName && intentId && workerName === intentId);
298
- }
299
- function addThinkingHeader(state, header) {
300
- state.formattedThinkingText += (state.formattedThinkingText ? "\n" : "") + header;
301
- }
302
- function addThinkingDetail(state, detail) {
303
- const trimmed = detail.trim();
304
- if (!trimmed) return;
305
- state.formattedThinkingText += (state.formattedThinkingText ? "\n" : "") + trimmed;
306
- }
307
- function addThinkingLine(state, header, detail) {
308
- state.formattedThinkingText += (state.formattedThinkingText ? "\n" : "") + header + "\n" + detail;
309
- }
310
- function appendThinkingText(state, text) {
311
- state.formattedThinkingText += text;
312
- }
313
368
  function updateExecutionStageMessage(state, msg) {
314
369
  if (!msg) return;
315
370
  for (let i = state.steps.length - 1; i >= 0; i--) {
@@ -330,9 +385,7 @@ function completeLastInProgressStep(steps) {
330
385
  }
331
386
  function createInitialV2State() {
332
387
  return {
333
- formattedThinkingText: "",
334
388
  finalResponse: "",
335
- currentWorker: "",
336
389
  lastEventType: "",
337
390
  sessionId: void 0,
338
391
  executionId: void 0,
@@ -379,10 +432,6 @@ function processStreamEventV2(rawEvent, state) {
379
432
  return state;
380
433
  }
381
434
  if (typeof eventType === "string" && eventType.toUpperCase() === "THINKING_DELTA") {
382
- const text = typeof event.text === "string" ? event.text : "";
383
- if (text) {
384
- appendThinkingText(state, text);
385
- }
386
435
  if (event.executionId) state.executionId = event.executionId;
387
436
  if (event.sessionId) state.sessionId = event.sessionId;
388
437
  state.lastEventType = "THINKING_DELTA";
@@ -393,7 +442,6 @@ function processStreamEventV2(rawEvent, state) {
393
442
  const message = getEventMessage(event);
394
443
  switch (eventType) {
395
444
  case "WORKFLOW_STARTED":
396
- case "STARTED":
397
445
  state.lastEventType = eventType;
398
446
  break;
399
447
  case "INTENT_PROGRESS": {
@@ -406,97 +454,7 @@ function processStreamEventV2(rawEvent, state) {
406
454
  state.lastEventType = eventType;
407
455
  break;
408
456
  }
409
- case "INTENT_THINKING": {
410
- const worker = getEventText(event, "workerName") || "Worker";
411
- const msg = getEventText(event, "message") || "Thinking...";
412
- const showHeader = shouldShowIntentHeader(event);
413
- if (worker !== state.currentWorker) {
414
- state.currentWorker = worker;
415
- if (showHeader && msg && msg !== worker) {
416
- addThinkingLine(state, `**${worker}**`, msg);
417
- } else if (showHeader) {
418
- addThinkingHeader(state, `**${worker}**`);
419
- } else if (msg !== "Thinking...") {
420
- addThinkingDetail(state, msg);
421
- }
422
- } else if ((showHeader || msg !== "Thinking...")) {
423
- appendThinkingText(state, "\n" + msg);
424
- }
425
- const lastInProgress = [...state.steps].reverse().find((s) => s.status === "in_progress");
426
- if (lastInProgress) {
427
- lastInProgress.thinkingText = "";
428
- lastInProgress.isThinking = true;
429
- }
430
- state.lastEventType = "INTENT_THINKING";
431
- break;
432
- }
433
- case "INTENT_THINKING_CONT": {
434
- const msg = event.message || "";
435
- if (!msg) break;
436
- if (state.lastEventType === "INTENT_THINKING") {
437
- appendThinkingText(state, "\n" + msg);
438
- } else {
439
- appendThinkingText(state, msg);
440
- }
441
- const thinkingStep = [...state.steps].reverse().find((s) => s.isThinking);
442
- if (thinkingStep) {
443
- thinkingStep.thinkingText = (thinkingStep.thinkingText || "") + msg;
444
- }
445
- state.lastEventType = "INTENT_THINKING_CONT";
446
- break;
447
- }
448
- case "ORCHESTRATOR_THINKING": {
449
- addThinkingLine(state, "**Planning**", event.message || "Understanding your request...");
450
- const stepId = `step-${state.stepCounter++}`;
451
- state.steps.push({
452
- id: stepId,
453
- eventType,
454
- message,
455
- status: "in_progress",
456
- timestamp: Date.now(),
457
- elapsedMs: event.elapsedMs
458
- });
459
- state.currentExecutingStepId = stepId;
460
- state.lastEventType = eventType;
461
- break;
462
- }
463
- case "ORCHESTRATOR_COMPLETED": {
464
- const workingDetail = workingPhaseDetailForDisplay(message);
465
- if (workingDetail) {
466
- addThinkingLine(state, "**Working**", workingDetail);
467
- } else {
468
- addThinkingHeader(state, "**Working**");
469
- }
470
- state.steps.push({
471
- id: `step-${state.stepCounter++}`,
472
- eventType: "WORKING",
473
- message: workingDetail,
474
- status: "completed",
475
- timestamp: Date.now()
476
- });
477
- const step = state.steps.find((s) => s.eventType === "ORCHESTRATOR_THINKING" && s.status === "in_progress");
478
- if (step) {
479
- step.status = "completed";
480
- if (event.elapsedMs) step.elapsedMs = event.elapsedMs;
481
- if (step.id === state.currentExecutingStepId) state.currentExecutingStepId = void 0;
482
- }
483
- state.lastEventType = eventType;
484
- break;
485
- }
486
457
  case "INTENT_STARTED": {
487
- const worker = getEventText(event, "workerName") || "Worker";
488
- const msg = getEventText(event, "message") || "Starting...";
489
- const showHeader = shouldShowIntentHeader(event);
490
- state.currentWorker = worker;
491
- if (showHeader && msg !== worker) {
492
- addThinkingLine(state, `**${worker}**`, msg);
493
- } else if (showHeader) {
494
- addThinkingHeader(state, `**${worker}**`);
495
- } else {
496
- addThinkingDetail(state, msg);
497
- }
498
- const thinkingStep = state.steps.find((s) => s.isThinking);
499
- if (thinkingStep) thinkingStep.isThinking = false;
500
458
  const stepId = `step-${state.stepCounter++}`;
501
459
  state.steps.push({
502
460
  id: stepId,
@@ -515,55 +473,18 @@ function processStreamEventV2(rawEvent, state) {
515
473
  const intentStep = state.steps.find((s) => s.eventType === "INTENT_STARTED" && s.status === "in_progress");
516
474
  if (intentStep) {
517
475
  intentStep.status = "completed";
518
- intentStep.isThinking = false;
519
476
  if (event.elapsedMs) intentStep.elapsedMs = event.elapsedMs;
520
477
  if (intentStep.id === state.currentExecutingStepId) state.currentExecutingStepId = void 0;
521
478
  }
522
479
  state.lastEventType = eventType;
523
480
  break;
524
481
  }
525
- case "AGGREGATOR_THINKING": {
526
- addThinkingLine(state, "**Finalizing**", event.message || "Preparing response...");
527
- const stepId = `step-${state.stepCounter++}`;
528
- state.steps.push({
529
- id: stepId,
530
- eventType,
531
- message,
532
- status: "in_progress",
533
- timestamp: Date.now(),
534
- elapsedMs: event.elapsedMs
535
- });
536
- state.currentExecutingStepId = stepId;
537
- state.lastEventType = eventType;
538
- break;
539
- }
540
- case "AGGREGATOR_COMPLETED": {
541
- appendThinkingText(state, "\n" + (event.message || "Response ready"));
542
- const step = state.steps.find((s) => s.eventType === "AGGREGATOR_THINKING" && s.status === "in_progress");
543
- if (step) {
544
- step.status = "completed";
545
- if (event.elapsedMs) step.elapsedMs = event.elapsedMs;
546
- if (step.id === state.currentExecutingStepId) state.currentExecutingStepId = void 0;
547
- }
548
- state.lastEventType = eventType;
549
- break;
550
- }
551
- case "WORKFLOW_COMPLETED":
552
- case "COMPLETED": {
482
+ case "WORKFLOW_COMPLETED": {
553
483
  const totalTime = Number(event.totalTimeMs);
554
484
  if (Number.isFinite(totalTime) && totalTime > 0) {
555
485
  state.totalElapsedMs = totalTime;
556
486
  }
557
- let content = extractResponseContent(event.response);
558
- const trace = event.trace && typeof event.trace === "object" ? event.trace : null;
559
- if (!content && trace?.workflowMsg && typeof trace.workflowMsg === "string") {
560
- content = trace.workflowMsg;
561
- }
562
- if (!content && trace?.aggregator && typeof trace.aggregator === "object") {
563
- const agg = trace.aggregator;
564
- if (typeof agg.response === "string") content = agg.response;
565
- else content = extractResponseContent(agg.response);
566
- }
487
+ const content = extractResponseContent(event.response);
567
488
  if (content) {
568
489
  state.finalResponse = content;
569
490
  if (event.trace && typeof event.trace === "object") {
@@ -578,7 +499,6 @@ function processStreamEventV2(rawEvent, state) {
578
499
  state.steps.forEach((step) => {
579
500
  if (step.status === "in_progress") {
580
501
  step.status = "completed";
581
- step.isThinking = false;
582
502
  }
583
503
  });
584
504
  state.lastEventType = eventType;
@@ -597,7 +517,6 @@ function processStreamEventV2(rawEvent, state) {
597
517
  };
598
518
  state.notifications.push(notification);
599
519
  state.lastNotification = notification;
600
- if (promptMessage) addThinkingDetail(state, promptMessage);
601
520
  state.lastEventType = eventType;
602
521
  break;
603
522
  }
@@ -623,9 +542,6 @@ function processStreamEventV2(rawEvent, state) {
623
542
  };
624
543
  upsertUserAction(state, request);
625
544
  state.lastUserAction = request;
626
- const header = userActionHeader(kind);
627
- if (promptMessage) addThinkingLine(state, header, promptMessage);
628
- else addThinkingHeader(state, header);
629
545
  const stepId = `step-${state.stepCounter++}`;
630
546
  state.steps.push({
631
547
  id: stepId,
@@ -649,27 +565,15 @@ function processStreamEventV2(rawEvent, state) {
649
565
  };
650
566
  state.notifications.push(notification);
651
567
  state.lastNotification = notification;
652
- if (noteMessage) addThinkingDetail(state, noteMessage);
653
568
  }
654
569
  state.lastEventType = eventType;
655
570
  break;
656
571
  }
657
572
  case "WORKFLOW_ERROR":
658
- case "ERROR":
659
573
  state.hasError = true;
660
574
  state.errorMessage = event.errorMessage || event.message || "Workflow error";
661
575
  state.lastEventType = eventType;
662
576
  break;
663
- case "INTENT_ERROR": {
664
- state.errorMessage = message || event.errorMessage || "An error occurred";
665
- const intentStep = state.steps.find((s) => s.eventType === "INTENT_STARTED" && s.status === "in_progress");
666
- if (intentStep) {
667
- intentStep.status = "error";
668
- intentStep.isThinking = false;
669
- }
670
- state.lastEventType = eventType;
671
- break;
672
- }
673
577
  // ---- K2 pipeline stage lifecycle events ----
674
578
  //
675
579
  // The k2-server playground streaming API emits
@@ -790,84 +694,6 @@ function processStreamEventV2(rawEvent, state) {
790
694
  return state;
791
695
  }
792
696
 
793
- // src/utils/messageStateManager.ts
794
- function buildFormattedThinking(steps, allThinkingText) {
795
- const parts = [];
796
- const safeSteps = steps ?? [];
797
- const cleanAll = allThinkingText.replace(/^\s+/, "");
798
- if (cleanAll) {
799
- const firstStepWithThinking = safeSteps.find(
800
- (s) => s.thinkingText && s.thinkingText.trim()
801
- );
802
- if (!firstStepWithThinking) {
803
- parts.push("**Preflight**");
804
- parts.push(cleanAll);
805
- } else {
806
- const stepText = firstStepWithThinking.thinkingText.trim();
807
- const idx = cleanAll.indexOf(stepText);
808
- if (idx > 0) {
809
- const orphaned = cleanAll.substring(0, idx).replace(/\s+$/, "");
810
- if (orphaned) {
811
- parts.push("**Preflight**");
812
- parts.push(orphaned);
813
- }
814
- }
815
- }
816
- }
817
- for (const step of safeSteps) {
818
- switch (step.eventType) {
819
- case "STAGE_STARTED": {
820
- if (step.message) parts.push(`**${step.message}**`);
821
- break;
822
- }
823
- case "ORCHESTRATOR_THINKING":
824
- parts.push("**Planning**");
825
- if (step.message) parts.push(step.message);
826
- break;
827
- case "INTENT_STARTED": {
828
- let label = step.message || "Processing";
829
- const started = label.match(/^(.+?)\s+started$/i);
830
- const progress = label.match(/^(.+?)\s+in progress$/i);
831
- if (started) label = started[1];
832
- else if (progress) label = progress[1];
833
- parts.push(`**${label}**`);
834
- if (step.thinkingText) parts.push(step.thinkingText);
835
- break;
836
- }
837
- case "INTENT_PROGRESS": {
838
- if (step.thinkingText) parts.push(step.thinkingText);
839
- else if (step.message) parts.push(step.message);
840
- break;
841
- }
842
- case "AGGREGATOR_THINKING":
843
- parts.push("**Finalizing**");
844
- if (step.message) parts.push(step.message);
845
- break;
846
- case "USER_ACTION_REQUIRED":
847
- parts.push("**Action required**");
848
- if (step.message) parts.push(step.message);
849
- break;
850
- }
851
- }
852
- return parts.length > 0 ? parts.join("\n") : allThinkingText;
853
- }
854
- function createCancelledMessageUpdate(steps, currentMessage) {
855
- const updatedSteps = steps.map((step) => {
856
- if (step.status === "in_progress") {
857
- return { ...step, status: "pending" };
858
- }
859
- return step;
860
- });
861
- return {
862
- isStreaming: false,
863
- isCancelled: true,
864
- steps: updatedSteps,
865
- currentExecutingStepId: void 0,
866
- // Preserve currentMessage so UI can show it with X icon
867
- currentMessage: currentMessage || "Thinking..."
868
- };
869
- }
870
-
871
697
  // src/utils/requestBuilder.ts
872
698
  var DEFAULT_STREAM_ENDPOINT = "/api/playground/ask/stream";
873
699
  function buildRequestBody(config, userMessage, sessionId, options) {
@@ -941,129 +767,6 @@ function buildRequestHeaders(config) {
941
767
  return headers;
942
768
  }
943
769
 
944
- // src/utils/userActionClient.ts
945
- var UserActionStaleError = class extends Error {
946
- constructor(userActionId, message = "User action is no longer actionable") {
947
- super(message);
948
- __publicField(this, "userActionId");
949
- this.name = "UserActionStaleError";
950
- this.userActionId = userActionId;
951
- }
952
- };
953
- async function sendUserActionRequest(config, userActionId, action, data) {
954
- const url = buildUserActionUrl(config, userActionId, action);
955
- const baseHeaders = buildRequestHeaders(config);
956
- const hasBody = data !== void 0;
957
- const headers = hasBody ? { "Content-Type": "application/json", ...baseHeaders } : baseHeaders;
958
- const response = await fetch(url, {
959
- method: "POST",
960
- headers,
961
- body: hasBody ? JSON.stringify(data) : void 0
962
- });
963
- if (response.status === 404) {
964
- throw new UserActionStaleError(userActionId);
965
- }
966
- if (!response.ok) {
967
- const errorText = await response.text();
968
- throw new Error(`HTTP ${response.status}: ${errorText}`);
969
- }
970
- return await response.json();
971
- }
972
- async function submitUserAction(config, userActionId, content) {
973
- return sendUserActionRequest(config, userActionId, "submit", content ?? {});
974
- }
975
- async function cancelUserAction(config, userActionId) {
976
- return sendUserActionRequest(config, userActionId, "cancel");
977
- }
978
- async function resendUserAction(config, userActionId) {
979
- return sendUserActionRequest(config, userActionId, "resend");
980
- }
981
-
982
- // src/utils/chatStore.ts
983
- var memoryStore = /* @__PURE__ */ new Map();
984
- var chatStore = {
985
- get(key) {
986
- return memoryStore.get(key) ?? [];
987
- },
988
- set(key, messages) {
989
- memoryStore.set(key, messages);
990
- },
991
- delete(key) {
992
- memoryStore.delete(key);
993
- }
994
- };
995
-
996
- // src/utils/activeStreamStore.ts
997
- var streams = /* @__PURE__ */ new Map();
998
- var activeStreamStore = {
999
- has(key) {
1000
- return streams.has(key);
1001
- },
1002
- get(key) {
1003
- const entry = streams.get(key);
1004
- if (!entry) return null;
1005
- return { messages: entry.messages, isWaiting: entry.isWaiting };
1006
- },
1007
- // Called before startStream — registers the controller and initial messages
1008
- start(key, abortController, initialMessages) {
1009
- const existing = streams.get(key);
1010
- streams.set(key, {
1011
- messages: initialMessages,
1012
- isWaiting: true,
1013
- abortController,
1014
- listeners: existing?.listeners ?? /* @__PURE__ */ new Set()
1015
- });
1016
- },
1017
- // Called by the stream on every event — applies the same updater pattern React uses
1018
- applyMessages(key, updater) {
1019
- const entry = streams.get(key);
1020
- if (!entry) return;
1021
- const next = typeof updater === "function" ? updater(entry.messages) : updater;
1022
- entry.messages = next;
1023
- entry.listeners.forEach((l) => l(next, entry.isWaiting));
1024
- },
1025
- setWaiting(key, waiting) {
1026
- const entry = streams.get(key);
1027
- if (!entry) return;
1028
- entry.isWaiting = waiting;
1029
- entry.listeners.forEach((l) => l(entry.messages, waiting));
1030
- },
1031
- // Called when stream completes — persists to chatStore and cleans up
1032
- complete(key) {
1033
- const entry = streams.get(key);
1034
- if (!entry) return;
1035
- entry.isWaiting = false;
1036
- entry.listeners.forEach((l) => l(entry.messages, false));
1037
- const toSave = entry.messages.filter((m) => !m.isStreaming);
1038
- if (toSave.length > 0) chatStore.set(key, toSave);
1039
- streams.delete(key);
1040
- },
1041
- // Subscribe — returns unsubscribe fn. Component calls this on mount, cleanup on unmount.
1042
- subscribe(key, listener) {
1043
- const entry = streams.get(key);
1044
- if (!entry) return () => {
1045
- };
1046
- entry.listeners.add(listener);
1047
- return () => {
1048
- streams.get(key)?.listeners.delete(listener);
1049
- };
1050
- },
1051
- // Rename an entry — used when the server assigns a new session ID mid-stream
1052
- rename(oldKey, newKey) {
1053
- const entry = streams.get(oldKey);
1054
- if (!entry) return;
1055
- streams.set(newKey, entry);
1056
- streams.delete(oldKey);
1057
- },
1058
- // Explicit user cancel — aborts the controller and removes the entry
1059
- abort(key) {
1060
- const entry = streams.get(key);
1061
- if (!entry) return;
1062
- entry.abortController.abort();
1063
- streams.delete(key);
1064
- }
1065
- };
1066
-
1067
770
  // src/utils/ragImageResolver.ts
1068
771
  var RAG_IMAGE_REGEX = /\/api\/rag\/chunks\/[^"'\s]+\/image/;
1069
772
  function hasRagImages(content) {
@@ -1178,7 +881,6 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1178
881
  streamProgress: "error",
1179
882
  isError: true,
1180
883
  errorDetails: state.errorMessage,
1181
- formattedThinkingText: state.formattedThinkingText || void 0,
1182
884
  steps: [...state.steps],
1183
885
  currentExecutingStepId: void 0,
1184
886
  executionId: state.executionId,
@@ -1191,7 +893,6 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1191
893
  currentMessage,
1192
894
  streamProgress: "processing",
1193
895
  isError: false,
1194
- formattedThinkingText: state.formattedThinkingText || void 0,
1195
896
  steps: [...state.steps],
1196
897
  currentExecutingStepId: state.currentExecutingStepId,
1197
898
  executionId: state.executionId,
@@ -1218,7 +919,6 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1218
919
  errorDetails: isAborted ? void 0 : error.message,
1219
920
  content: isAborted ? state.finalResponse || "" : state.finalResponse || FRIENDLY_ERROR_MESSAGE,
1220
921
  currentMessage: isAborted ? "Thinking..." : void 0,
1221
- formattedThinkingText: state.formattedThinkingText || void 0,
1222
922
  steps: [...state.steps].map((step) => {
1223
923
  if (step.status === "in_progress" && isAborted) {
1224
924
  return { ...step, status: "pending" };
@@ -1257,7 +957,6 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1257
957
  steps: state.hasError ? [] : [...state.steps],
1258
958
  isCancelled: false,
1259
959
  currentExecutingStepId: void 0,
1260
- formattedThinkingText: state.hasError ? void 0 : state.formattedThinkingText || void 0,
1261
960
  isResolvingImages: needsImageResolve,
1262
961
  totalElapsedMs: state.hasError ? void 0 : state.totalElapsedMs
1263
962
  };
@@ -1310,7 +1009,6 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1310
1009
  isCancelled: isAborted,
1311
1010
  errorDetails: isAborted ? void 0 : error.message,
1312
1011
  content: isAborted ? state.finalResponse || "" : state.finalResponse || FRIENDLY_ERROR_MESSAGE,
1313
- formattedThinkingText: state.formattedThinkingText || void 0,
1314
1012
  steps: [...state.steps].map((step) => {
1315
1013
  if (step.status === "in_progress" && isAborted) {
1316
1014
  return { ...step, status: "pending" };
@@ -1336,6 +1034,61 @@ function useStreamManagerV2(config, callbacks, setMessages, setIsWaitingForRespo
1336
1034
  };
1337
1035
  }
1338
1036
 
1037
+ // src/utils/messageStateManager.ts
1038
+ function createCancelledMessageUpdate(steps, currentMessage) {
1039
+ const updatedSteps = steps.map((step) => {
1040
+ if (step.status === "in_progress") {
1041
+ return { ...step, status: "pending" };
1042
+ }
1043
+ return step;
1044
+ });
1045
+ return {
1046
+ isStreaming: false,
1047
+ isCancelled: true,
1048
+ steps: updatedSteps,
1049
+ currentExecutingStepId: void 0,
1050
+ currentMessage: currentMessage || "Thinking..."
1051
+ };
1052
+ }
1053
+
1054
+ // src/utils/userActionClient.ts
1055
+ var UserActionStaleError = class extends Error {
1056
+ constructor(userActionId, message = "User action is no longer actionable") {
1057
+ super(message);
1058
+ __publicField(this, "userActionId");
1059
+ this.name = "UserActionStaleError";
1060
+ this.userActionId = userActionId;
1061
+ }
1062
+ };
1063
+ async function sendUserActionRequest(config, userActionId, action, data) {
1064
+ const url = buildUserActionUrl(config, userActionId, action);
1065
+ const baseHeaders = buildRequestHeaders(config);
1066
+ const hasBody = data !== void 0;
1067
+ const headers = hasBody ? { "Content-Type": "application/json", ...baseHeaders } : baseHeaders;
1068
+ const response = await fetch(url, {
1069
+ method: "POST",
1070
+ headers,
1071
+ body: hasBody ? JSON.stringify(data) : void 0
1072
+ });
1073
+ if (response.status === 404) {
1074
+ throw new UserActionStaleError(userActionId);
1075
+ }
1076
+ if (!response.ok) {
1077
+ const errorText = await response.text();
1078
+ throw new Error(`HTTP ${response.status}: ${errorText}`);
1079
+ }
1080
+ return await response.json();
1081
+ }
1082
+ async function submitUserAction(config, userActionId, content) {
1083
+ return sendUserActionRequest(config, userActionId, "submit", content ?? {});
1084
+ }
1085
+ async function cancelUserAction(config, userActionId) {
1086
+ return sendUserActionRequest(config, userActionId, "cancel");
1087
+ }
1088
+ async function resendUserAction(config, userActionId) {
1089
+ return sendUserActionRequest(config, userActionId, "resend");
1090
+ }
1091
+
1339
1092
  // src/hooks/useChatV2.ts
1340
1093
  var EMPTY_USER_ACTION_STATE = { prompts: [], notifications: [] };
1341
1094
  function upsertPrompt(prompts, req) {
@@ -2049,6 +1802,6 @@ function migrateActiveStream(oldUserId, newUserId) {
2049
1802
  activeStreamStore.rename(oldUserId, newUserId);
2050
1803
  }
2051
1804
 
2052
- export { UserActionStaleError, buildContent, buildFormattedThinking, cancelUserAction, classifyField, classifyUserActionKind, coerceValue, createInitialV2State, defaultValueFor, generateId, getOptions, isNestedOrUnsupported, isRequired, migrateActiveStream, processStreamEventV2, renderableFields, resendUserAction, streamWorkflowEvents, submitUserAction, useChatV2, useVoice, validateField, validateForm };
1805
+ export { UserActionStaleError, buildContent, cancelUserAction, classifyField, classifyUserActionKind, coerceValue, createInitialV2State, defaultValueFor, generateId, getOptions, isNestedOrUnsupported, isRequired, migrateActiveStream, processStreamEventV2, renderableFields, resendUserAction, streamWorkflowEvents, submitUserAction, useChatV2, useVoice, validateField, validateForm };
2053
1806
  //# sourceMappingURL=index.mjs.map
2054
1807
  //# sourceMappingURL=index.mjs.map