@lumerahq/ui 0.19.4 → 0.19.6-dev.0

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.
@@ -1,6 +1,6 @@
1
1
  import { useEffect, useState, useRef, useCallback, useMemo } from "react";
2
2
  import { useQueryClient, useMutation, useQuery } from "@tanstack/react-query";
3
- import { J as ensureAutomationRun, H as createAutomationRun, D as cancelAutomationRun, C as automationStatuses, M as getAutomationRun } from "./automations-C6SMookl.js";
3
+ import { O as ensureAutomationRun, M as createAutomationRun, J as cancelAutomationRun, I as automationStatuses, R as getAutomationRun } from "./automations-TTSbZaKC.js";
4
4
  const QUEUED_DISCOVERY_POLL_MS = 2e3;
5
5
  const IDLE_DISCOVERY_POLL_MS = 1e4;
6
6
  const HIDDEN_DISCOVERY_POLL_MS = 3e4;
@@ -109,6 +109,8 @@ function useAgentChat({
109
109
  watchdogTimeoutMs = 3e4,
110
110
  watchdogPollMs = 1e3,
111
111
  discoveryPoll = false,
112
+ webSearch = false,
113
+ webSearchDefault = false,
112
114
  idFactory = defaultIDFactory,
113
115
  onMessagesChange,
114
116
  onTurnComplete,
@@ -130,7 +132,9 @@ function useAgentChat({
130
132
  const [isSubmitting, setIsSubmitting] = useState(false);
131
133
  const [isStreaming, setIsStreaming] = useState(false);
132
134
  const [isAborting, setIsAborting] = useState(false);
135
+ const [isCompacting, setIsCompacting] = useState(false);
133
136
  const [historyLoaded, setHistoryLoaded] = useState(false);
137
+ const [webSearchOn, setWebSearchOn] = useState(webSearchDefault);
134
138
  const messagesRef = useRef(messages);
135
139
  const subscriptionRef = useRef(null);
136
140
  const activeRequestIdRef = useRef(void 0);
@@ -184,7 +188,12 @@ function useAgentChat({
184
188
  const applyHistoryResult = useCallback(
185
189
  (result) => {
186
190
  const projected = mergePendingMessages(result.messages, result.pending ?? []);
187
- setMessages((current) => mergeUniqueMessages([...projected, ...missingActiveUserMessages(current, projected)]));
191
+ setMessages(
192
+ (current) => mergeUniqueMessages([
193
+ ...carryThinkingStamps(current, projected),
194
+ ...missingActiveUserMessages(current, projected)
195
+ ])
196
+ );
188
197
  setPage(result.page ?? null);
189
198
  },
190
199
  [setMessages]
@@ -306,11 +315,15 @@ function useAgentChat({
306
315
  if (!requestId) return;
307
316
  clearWatchdog();
308
317
  setMessages(
309
- (current) => current.map(
310
- (messageItem) => messageItem.role === "user" && messageItem.requestId === requestId ? { ...messageItem, status, error: errorMessage ?? messageItem.error } : messageItem
318
+ (current) => closeOpenThinking(
319
+ current.map(
320
+ (messageItem) => messageItem.role === "user" && messageItem.requestId === requestId ? { ...messageItem, status, error: errorMessage ?? messageItem.error } : messageItem
321
+ ),
322
+ requestId
311
323
  )
312
324
  );
313
325
  setIsStreaming(false);
326
+ setIsCompacting(false);
314
327
  const turn = buildAgentChatTurns(messagesRef.current).find((item) => item.requestId === requestId);
315
328
  if (turn) onTurnComplete?.(turn);
316
329
  if (!transport.subscribe) return;
@@ -351,6 +364,11 @@ function useAgentChat({
351
364
  markUserTurnRunning(setMessages, event.requestId);
352
365
  return;
353
366
  case "info":
367
+ if (typeof event.compacting === "boolean") {
368
+ setIsCompacting(event.compacting);
369
+ markUserTurnRunning(setMessages, event.requestId);
370
+ return;
371
+ }
354
372
  setMessages(
355
373
  (current) => appendAssistantBlock(current, event.requestId, {
356
374
  type: "info",
@@ -441,7 +459,8 @@ function useAgentChat({
441
459
  message: content,
442
460
  requestId,
443
461
  sessionId,
444
- files: pendingFiles
462
+ files: pendingFiles,
463
+ ...webSearch && { webSearchEnabled: webSearchOn }
445
464
  });
446
465
  if (!mountedRef.current) return result;
447
466
  if (result.accepted) {
@@ -475,6 +494,58 @@ function useAgentChat({
475
494
  sessionId,
476
495
  setMessages,
477
496
  startSubscription,
497
+ transport,
498
+ webSearch,
499
+ webSearchOn
500
+ ]
501
+ );
502
+ const retry = useCallback(
503
+ async (requestId) => {
504
+ if (mutationsBlocked) return { accepted: false, error: mutationBlockedError };
505
+ const failed = messagesRef.current.find((message) => message.role === "user" && message.requestId === requestId);
506
+ if (!failed) return { accepted: false, error: "Nothing to retry" };
507
+ if (!transport.retry) return sendMessage(failed.content);
508
+ if (isSubmitting) return { accepted: false, error: "Already sending" };
509
+ setIsSubmitting(true);
510
+ setError(null);
511
+ try {
512
+ const result = await transport.retry({ agentId, sessionId, requestId });
513
+ if (!mountedRef.current) return result;
514
+ if (result.accepted) {
515
+ const nextRequestId = result.requestId ?? requestId;
516
+ const replacement = {
517
+ ...failed,
518
+ ...result.pending ?? {},
519
+ id: result.pending?.id ?? failed.id,
520
+ requestId: nextRequestId,
521
+ retryRootRequestId: failed.retryRootRequestId ?? requestId,
522
+ status: result.pending?.status ?? "queued",
523
+ error: void 0,
524
+ errorCategory: void 0
525
+ };
526
+ setMessages((current) => [...current.filter((message) => message.requestId !== requestId), replacement]);
527
+ startSubscription(nextRequestId);
528
+ } else if (result.error) {
529
+ handleError(new Error(result.error));
530
+ }
531
+ return result;
532
+ } catch (err) {
533
+ handleError(err);
534
+ return { accepted: false, requestId, error: err instanceof Error ? err.message : String(err) };
535
+ } finally {
536
+ if (mountedRef.current) setIsSubmitting(false);
537
+ }
538
+ },
539
+ [
540
+ agentId,
541
+ handleError,
542
+ isSubmitting,
543
+ mutationBlockedError,
544
+ mutationsBlocked,
545
+ sendMessage,
546
+ sessionId,
547
+ setMessages,
548
+ startSubscription,
478
549
  transport
479
550
  ]
480
551
  );
@@ -560,6 +631,7 @@ function useAgentChat({
560
631
  setIsStreaming(false);
561
632
  setIsSubmitting(false);
562
633
  setIsAborting(false);
634
+ setIsCompacting(false);
563
635
  setHistoryLoaded(false);
564
636
  }, [identityKey, clearWatchdog, initialPage, setMessages]);
565
637
  useEffect(() => {
@@ -577,7 +649,7 @@ function useAgentChat({
577
649
  const canSubmit = !mutationsBlocked && !isSubmitting && !hasUploadingFiles && !hasFailedUploads && (input.trim().length > 0 || pendingFiles.length > 0) && (!hasActive || queueLength < maxQueuedMessages);
578
650
  const canAbort = !mutationsBlocked && !isAborting && hasActive;
579
651
  const phase = derivePhase({ isLoadingHistory, isLoadingEarlier, isSubmitting, isStreaming, isAborting, error });
580
- const isWorking = isSubmitting || isStreaming || isAborting || hasActive;
652
+ const isWorking = isSubmitting || isStreaming || isAborting || isCompacting || hasActive;
581
653
  const isTransportActiveCb = useCallback(
582
654
  () => isStreamingRef.current || isSubmittingRef.current || isAbortingRef.current,
583
655
  []
@@ -612,6 +684,7 @@ function useAgentChat({
612
684
  isSubmitting,
613
685
  isStreaming,
614
686
  isAborting,
687
+ isCompacting,
615
688
  canLoadEarlier: Boolean(page?.hasMoreBefore && page.beforeCursor),
616
689
  queueLength,
617
690
  mutationsBlocked,
@@ -620,9 +693,12 @@ function useAgentChat({
620
693
  isWorking,
621
694
  canSubmit,
622
695
  canAbort,
696
+ webSearchEnabled: webSearch ? webSearchOn : void 0,
697
+ setWebSearchEnabled: setWebSearchOn,
623
698
  loadHistory,
624
699
  loadEarlier,
625
700
  sendMessage,
701
+ retry,
626
702
  abort
627
703
  };
628
704
  }
@@ -672,7 +748,24 @@ function mergeUniqueMessages(messages) {
672
748
  keys.forEach((key) => seen.add(key));
673
749
  merged.push(message);
674
750
  }
675
- return merged;
751
+ return collapseRetryAttempts(merged);
752
+ }
753
+ function collapseRetryAttempts(messages) {
754
+ const rootByRequest = /* @__PURE__ */ new Map();
755
+ const latestByRoot = /* @__PURE__ */ new Map();
756
+ for (const message of messages) {
757
+ if (message.role !== "user" || !message.requestId) continue;
758
+ const root = message.retryRootRequestId || message.requestId;
759
+ rootByRequest.set(message.requestId, root);
760
+ if (message.retryRootRequestId || !latestByRoot.has(root)) latestByRoot.set(root, message.requestId);
761
+ }
762
+ if (latestByRoot.size === 0) return messages;
763
+ return messages.filter((message) => {
764
+ if (!message.requestId) return true;
765
+ const root = rootByRequest.get(message.requestId) ?? message.requestId;
766
+ const latest = latestByRoot.get(root);
767
+ return !latest || latest === message.requestId;
768
+ });
676
769
  }
677
770
  function trimMessages(messages, max) {
678
771
  return max > 0 && messages.length > max ? messages.slice(-max) : messages;
@@ -693,6 +786,7 @@ function mergePendingMessages(messages, pending) {
693
786
  ...matched,
694
787
  content: matched?.content || pendingMessage.content,
695
788
  timestamp: matched?.timestamp || pendingMessage.timestamp,
789
+ retryRootRequestId: matched?.retryRootRequestId ?? pendingMessage.retryRootRequestId,
696
790
  status: matched?.status && !isActiveStatus(matched.status) ? matched.status : pendingMessage.status,
697
791
  error: matched?.error ?? pendingMessage.error,
698
792
  errorCategory: matched?.errorCategory ?? pendingMessage.errorCategory
@@ -703,6 +797,31 @@ function mergePendingMessages(messages, pending) {
703
797
  }
704
798
  return next;
705
799
  }
800
+ function carryThinkingStamps(current, projected) {
801
+ const liveByRequest = /* @__PURE__ */ new Map();
802
+ for (const message of current) {
803
+ if (message.role !== "assistant" || !message.requestId) continue;
804
+ if (message.blocks?.some((block) => block.type === "thinking" && block.startedAt)) {
805
+ liveByRequest.set(message.requestId, message);
806
+ }
807
+ }
808
+ if (liveByRequest.size === 0) return projected;
809
+ return projected.map((message) => {
810
+ const live = message.role === "assistant" && message.requestId ? liveByRequest.get(message.requestId) : void 0;
811
+ if (!live || !message.blocks || live.blocks === message.blocks) return message;
812
+ const liveThinking = (live.blocks ?? []).filter((block) => block.type === "thinking");
813
+ let index = 0;
814
+ let changed = false;
815
+ const blocks = message.blocks.map((block) => {
816
+ if (block.type !== "thinking") return block;
817
+ const stamped = liveThinking[index++];
818
+ if (!stamped || stamped.type !== "thinking" || !stamped.startedAt || block.startedAt) return block;
819
+ changed = true;
820
+ return { ...block, startedAt: stamped.startedAt, completedAt: stamped.completedAt };
821
+ });
822
+ return changed ? { ...message, blocks } : message;
823
+ });
824
+ }
706
825
  function missingActiveUserMessages(current, projected) {
707
826
  const projectedRequestIds = new Set(projected.map((message) => message.requestId).filter(Boolean));
708
827
  return current.filter(
@@ -734,18 +853,21 @@ function insertChronologically(messages, message) {
734
853
  }
735
854
  function upsertAssistantToolBlock(messages, requestId, toolCall) {
736
855
  const block = { type: "tool_call", toolCall };
737
- const next = [...messages];
856
+ const next = closeOpenThinking([...messages], requestId);
738
857
  const index = latestAssistantIndex(next, requestId);
739
858
  if (index < 0) {
740
- return appendAssistantBlock(messages, requestId, block);
859
+ return appendAssistantBlock(next, requestId, block);
741
860
  }
742
861
  const message = next[index];
743
862
  const blocks = [...message?.blocks ?? []];
744
863
  const blockIndex = blocks.findIndex((item) => item.type === "tool_call" && item.toolCall.id === toolCall.id);
745
864
  if (blockIndex >= 0) {
746
- blocks[blockIndex] = block;
865
+ const existing = blocks[blockIndex];
866
+ const startedAt = toolCall.startedAt ?? (existing?.type === "tool_call" ? existing.toolCall.startedAt : void 0);
867
+ const completedAt = toolCall.completedAt ?? (toolCall.status !== "running" ? nowISO() : void 0);
868
+ blocks[blockIndex] = { type: "tool_call", toolCall: { ...toolCall, startedAt, completedAt } };
747
869
  } else {
748
- blocks.push(block);
870
+ blocks.push({ type: "tool_call", toolCall: { ...toolCall, startedAt: toolCall.startedAt ?? nowISO() } });
749
871
  }
750
872
  next[index] = {
751
873
  ...message,
@@ -755,7 +877,7 @@ function upsertAssistantToolBlock(messages, requestId, toolCall) {
755
877
  return next;
756
878
  }
757
879
  function appendAssistantBlock(messages, requestId, block) {
758
- const next = [...messages];
880
+ const next = closeOpenThinking([...messages], requestId);
759
881
  const index = latestAssistantIndex(next, requestId);
760
882
  if (index >= 0) {
761
883
  const message = next[index];
@@ -776,16 +898,16 @@ function appendAssistantBlock(messages, requestId, block) {
776
898
  }
777
899
  function applyAssistantDelta(messages, requestId, blockType, delta) {
778
900
  if (!delta) return messages;
779
- const next = [...messages];
901
+ const next = blockType === "text" ? closeOpenThinking([...messages], requestId) : [...messages];
780
902
  const index = latestAssistantIndex(next, requestId);
903
+ const fresh = () => blockType === "text" ? { type: "text", content: delta } : { type: "thinking", content: delta, startedAt: nowISO() };
781
904
  if (index < 0) {
782
- const block = blockType === "text" ? { type: "text", content: delta } : { type: "thinking", content: delta };
783
905
  next.push({
784
906
  id: `assistant:${requestId ?? defaultIDFactory()}`,
785
907
  role: "assistant",
786
908
  content: blockType === "text" ? delta : "",
787
909
  requestId,
788
- blocks: [block]
910
+ blocks: [fresh()]
789
911
  });
790
912
  return next;
791
913
  }
@@ -795,7 +917,7 @@ function applyAssistantDelta(messages, requestId, blockType, delta) {
795
917
  if (last?.type === blockType) {
796
918
  blocks[blocks.length - 1] = { ...last, content: last.content + delta };
797
919
  } else {
798
- blocks.push(blockType === "text" ? { type: "text", content: delta } : { type: "thinking", content: delta });
920
+ blocks.push(fresh());
799
921
  }
800
922
  next[index] = {
801
923
  ...message,
@@ -804,6 +926,23 @@ function applyAssistantDelta(messages, requestId, blockType, delta) {
804
926
  };
805
927
  return next;
806
928
  }
929
+ function closeOpenThinking(messages, requestId) {
930
+ const index = latestAssistantIndex(messages, requestId);
931
+ if (index < 0) return messages;
932
+ const message = messages[index];
933
+ const blocks = message?.blocks ?? [];
934
+ const last = blocks[blocks.length - 1];
935
+ if (last?.type !== "thinking" || !last.startedAt || last.completedAt) return messages;
936
+ const next = [...messages];
937
+ next[index] = {
938
+ ...message,
939
+ blocks: [...blocks.slice(0, -1), { ...last, completedAt: nowISO() }]
940
+ };
941
+ return next;
942
+ }
943
+ function nowISO() {
944
+ return (/* @__PURE__ */ new Date()).toISOString();
945
+ }
807
946
  function latestAssistantIndex(messages, requestId) {
808
947
  for (let index = messages.length - 1; index >= 0; index -= 1) {
809
948
  const message = messages[index];
@@ -1134,7 +1273,9 @@ function useAutomationRun(options) {
1134
1273
  }
1135
1274
  export {
1136
1275
  areAgentChatMutationsBlocked as a,
1137
- useAgentChatSessions as b,
1138
- useAutomationRun as c,
1276
+ collapseRetryAttempts as b,
1277
+ carryThinkingStamps as c,
1278
+ useAgentChatSessions as d,
1279
+ useAutomationRun as e,
1139
1280
  useAgentChat as u
1140
1281
  };
@@ -1,7 +1,7 @@
1
1
  import { useQueryClient, useQuery, useMutation, useInfiniteQuery } from "@tanstack/react-query";
2
2
  import { useState, useCallback, useEffect, useMemo } from "react";
3
- import { V as listRunsByAgent, C as automationStatuses, H as createAutomationRun, A as uploadFile, o as onInitMessage, v as pbSql, x as pbUpdateRecord } from "./automations-C6SMookl.js";
4
- import { n as normalizeRecordAuditCollections, d as normalizeRecordAuditPageSize, l as listRecordAuditLog, g as getRecordAuditLogSnapshot } from "./audit-log-CBcctM1l.js";
3
+ import { _ as listRunsByAgent, I as automationStatuses, M as createAutomationRun, H as uploadFile, o as onInitMessage, A as pbSql, D as pbUpdateRecord } from "./automations-TTSbZaKC.js";
4
+ import { n as normalizeRecordAuditCollections, d as normalizeRecordAuditPageSize, l as listRecordAuditLog, g as getRecordAuditLogSnapshot } from "./audit-log-BFPtNHQh.js";
5
5
  function useAutomationAgent({
6
6
  agentId,
7
7
  limit = 5,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumerahq/ui",
3
- "version": "0.19.4",
3
+ "version": "0.19.6-dev.0",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "*.css"
@@ -1,4 +0,0 @@
1
- import { G } from "./RecordSheet-BGfO2C8P.js";
2
- export {
3
- G as Mermaid
4
- };