@lumerahq/ui 0.20.5 → 0.21.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/{RecordSheet-SPyDKXhe.js → RecordSheet-2eblaDCh.js} +5 -4
  3. package/dist/{audit-log-Dmzbqtc0.js → audit-log-BNaHuJ1Y.js} +1 -1
  4. package/dist/{automations-DD15itWa.js → automations-3V0uflxE.js} +300 -37
  5. package/dist/components/agent-chat/AgentChatComposer.d.ts.map +1 -1
  6. package/dist/components/index.js +1 -1
  7. package/dist/{highlighted-body-OFNGDK62-V4GWTV8L.js → highlighted-body-OFNGDK62-DH1q0LXj.js} +1 -1
  8. package/dist/hooks/index.d.ts +1 -0
  9. package/dist/hooks/index.d.ts.map +1 -1
  10. package/dist/hooks/index.js +5 -4
  11. package/dist/hooks/use-agent-chat.d.ts.map +1 -1
  12. package/dist/hooks/use-chat-draft.d.ts +3 -0
  13. package/dist/hooks/use-chat-draft.d.ts.map +1 -0
  14. package/dist/hooks/use-file-upload.d.ts.map +1 -1
  15. package/dist/hooks/use-update-blocker.d.ts +6 -0
  16. package/dist/hooks/use-update-blocker.d.ts.map +1 -0
  17. package/dist/index.js +47 -45
  18. package/dist/lib/bridge.d.ts +4 -29
  19. package/dist/lib/bridge.d.ts.map +1 -1
  20. package/dist/lib/customer-app-updates.d.ts +3 -0
  21. package/dist/lib/customer-app-updates.d.ts.map +1 -0
  22. package/dist/lib/draft-hooks.d.ts +24 -0
  23. package/dist/lib/draft-hooks.d.ts.map +1 -0
  24. package/dist/lib/drafts.d.ts +17 -0
  25. package/dist/lib/drafts.d.ts.map +1 -0
  26. package/dist/lib/index.d.ts +2 -0
  27. package/dist/lib/index.d.ts.map +1 -1
  28. package/dist/lib/index.js +41 -40
  29. package/dist/lib/safe-updates.d.ts +25 -0
  30. package/dist/lib/safe-updates.d.ts.map +1 -0
  31. package/dist/mermaid-GHXKKRXX-0N4Lxc6D.js +4 -0
  32. package/dist/ui.css +4 -0
  33. package/dist/{use-automation-run-D69xZvR9.js → use-automation-run-C0JNbLzy.js} +157 -7
  34. package/dist/{use-sql-table-_aMNbZ6w.js → use-sql-table-iSVW_brl.js} +4 -2
  35. package/package.json +1 -1
  36. package/dist/mermaid-GHXKKRXX-wgWycT_j.js +0 -4
@@ -1,6 +1,6 @@
1
- import { useEffect, useState, useRef, useCallback, useMemo } from "react";
1
+ import { useState, useEffect, useCallback, useLayoutEffect, useRef, useSyncExternalStore, useMemo } from "react";
2
+ import { a4 as updateBlockers, a5 as getDraftOwner, a6 as subscribeDraftOwner, i as ensureAutomationRun, g as createAutomationRun, c as cancelAutomationRun, a as automationStatuses, m as getAutomationRun } from "./automations-3V0uflxE.js";
2
3
  import { useQueryClient, useMutation, useQuery } from "@tanstack/react-query";
3
- import { i as ensureAutomationRun, g as createAutomationRun, c as cancelAutomationRun, a as automationStatuses, m as getAutomationRun } from "./automations-DD15itWa.js";
4
4
  function isActiveStatus(status) {
5
5
  return status === "queued" || status === "claimed" || status === "preparing" || status === "submitting" || status === "running";
6
6
  }
@@ -36,6 +36,149 @@ function replayedOutputRequestId(event) {
36
36
  function dropAssistantMessagesForRequest(messages, requestId) {
37
37
  return messages.filter((message) => !(message.role === "assistant" && message.requestId === requestId));
38
38
  }
39
+ const MAX_AGE = 7 * 864e5;
40
+ function getStorage() {
41
+ try {
42
+ return sessionStorage;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+ function read(key, storage) {
48
+ if (!key) return null;
49
+ try {
50
+ const saved = JSON.parse(storage?.getItem(key) ?? "null");
51
+ if (saved && Date.now() - saved.at < MAX_AGE && "value" in saved) return saved;
52
+ } catch {
53
+ }
54
+ return null;
55
+ }
56
+ class DraftSession {
57
+ listeners = /* @__PURE__ */ new Set();
58
+ value;
59
+ revision;
60
+ key;
61
+ empty;
62
+ storage = getStorage();
63
+ constructor(key, initial, empty) {
64
+ this.key = key;
65
+ this.empty = empty;
66
+ const saved = read(key, this.storage);
67
+ this.value = saved ? saved.value : initial;
68
+ this.revision = saved?.revision ?? this.newRevision();
69
+ }
70
+ newRevision() {
71
+ return `${Date.now()}:${Math.random()}`;
72
+ }
73
+ subscribe = (listener) => {
74
+ this.listeners.add(listener);
75
+ return () => {
76
+ this.listeners.delete(listener);
77
+ };
78
+ };
79
+ getSnapshot = () => this.value;
80
+ set(value) {
81
+ if (Object.is(this.value, value)) return;
82
+ this.value = value;
83
+ this.revision = this.newRevision();
84
+ if (this.key) {
85
+ try {
86
+ this.storage?.setItem(this.key, JSON.stringify({ at: Date.now(), value, revision: this.revision }));
87
+ } catch {
88
+ }
89
+ }
90
+ for (const listener of this.listeners) listener();
91
+ }
92
+ captureAcknowledgement = () => {
93
+ const revision = this.revision;
94
+ return () => {
95
+ if (this.revision !== revision) return;
96
+ const saved = read(this.key, this.storage);
97
+ if (saved?.revision && saved.revision !== revision) return;
98
+ this.set(this.empty);
99
+ };
100
+ };
101
+ }
102
+ const documents = /* @__PURE__ */ new WeakMap();
103
+ function draftSession(identity, key, initial, empty, preferInitial = false) {
104
+ const storage = getStorage();
105
+ if (!key || !storage) return new DraftSession(key, initial, empty);
106
+ let sessions = documents.get(storage);
107
+ if (!sessions) {
108
+ sessions = /* @__PURE__ */ new Map();
109
+ documents.set(storage, sessions);
110
+ }
111
+ let session = sessions.get(identity);
112
+ if (!session) {
113
+ session = new DraftSession(key, initial, empty);
114
+ sessions.set(identity, session);
115
+ }
116
+ if (preferInitial) session.set(initial);
117
+ return session;
118
+ }
119
+ function createDraftHook({ useCallback: useCallback2, useLayoutEffect: useLayoutEffect2, useRef: useRef2, useState: useState2, useSyncExternalStore: useSyncExternalStore2 }) {
120
+ return function useScopedDraft2(options) {
121
+ const { identity, key, initial, empty, preferInitial, carryFrom } = options;
122
+ const [state, setState] = useState2(() => ({
123
+ identity,
124
+ session: draftSession(identity, key, initial, empty, preferInitial)
125
+ }));
126
+ let session = state.session;
127
+ if (state.identity !== identity) {
128
+ const carry = state.identity === carryFrom && !Object.is(state.session.getSnapshot(), empty);
129
+ const seed = carry ? state.session.getSnapshot() : initial;
130
+ session = draftSession(identity, key, seed, empty, carry || preferInitial);
131
+ setState({ identity, session });
132
+ }
133
+ const live = useRef2({ identity, mounted: true });
134
+ useLayoutEffect2(() => {
135
+ live.current = { identity, mounted: true };
136
+ return () => {
137
+ live.current.mounted = false;
138
+ };
139
+ }, [identity]);
140
+ const value = useSyncExternalStore2(session.subscribe, session.getSnapshot, session.getSnapshot);
141
+ const setValue = useCallback2(
142
+ (next) => {
143
+ if (!live.current.mounted || live.current.identity !== identity) return;
144
+ session.set(typeof next === "function" ? next(session.getSnapshot()) : next);
145
+ },
146
+ [session, identity]
147
+ );
148
+ return [value, setValue, session.captureAcknowledgement];
149
+ };
150
+ }
151
+ function createUpdateBlockerHook({ useLayoutEffect: useLayoutEffect2, useRef: useRef2 }) {
152
+ return function useUpdateBlocker2(options) {
153
+ const current = useRef2(options);
154
+ useLayoutEffect2(() => {
155
+ current.current = options;
156
+ return void 0;
157
+ });
158
+ useLayoutEffect2(
159
+ () => updateBlockers.register(
160
+ () => current.current.blocked,
161
+ () => current.current.reason
162
+ ),
163
+ []
164
+ );
165
+ };
166
+ }
167
+ const useScopedDraft = createDraftHook({ useCallback, useLayoutEffect, useRef, useState, useSyncExternalStore });
168
+ function useChatDraft(agent, session) {
169
+ const [owner, setOwner] = useState(getDraftOwner);
170
+ useEffect(() => subscribeDraftOwner(() => setOwner(getDraftOwner())), []);
171
+ const scope = JSON.stringify([agent, session]);
172
+ const identity = JSON.stringify(["customer", owner, scope]);
173
+ const key = owner ? `lumera:chat-draft:${JSON.stringify([owner, agent, session])}` : null;
174
+ return useScopedDraft({
175
+ identity,
176
+ key,
177
+ initial: "",
178
+ empty: "",
179
+ carryFrom: owner ? JSON.stringify(["customer", null, scope]) : void 0
180
+ });
181
+ }
39
182
  const QUEUED_DISCOVERY_POLL_MS = 2e3;
40
183
  const IDLE_DISCOVERY_POLL_MS = 1e4;
41
184
  const HIDDEN_DISCOVERY_POLL_MS = 3e4;
@@ -120,6 +263,7 @@ function useDiscoveryPoll({
120
263
  };
121
264
  }, [enabled, hasQueuedPending, isRefreshInFlight, isTransportActive, refresh]);
122
265
  }
266
+ const useUpdateBlocker = createUpdateBlockerHook({ useLayoutEffect, useRef });
123
267
  const DEFAULT_SESSION_ID = "default";
124
268
  const DEFAULT_HISTORY_LIMIT = 10;
125
269
  const DEFAULT_MAX_BUFFERED_MESSAGES = 100;
@@ -157,7 +301,7 @@ function useAgentChat({
157
301
  const [messages, setMessagesState] = useState(
158
302
  () => trimMessages(initialMessages, maxBufferedMessages)
159
303
  );
160
- const [input, setInput] = useState("");
304
+ const [input, setInput, captureAcknowledgement] = useChatDraft(agentId, sessionId);
161
305
  const [pendingFiles, setPendingFiles] = useState([]);
162
306
  const [page, setPage] = useState(initialPage ?? null);
163
307
  const [tokenUsage, setTokenUsage] = useState(null);
@@ -503,6 +647,9 @@ function useAgentChat({
503
647
  const requestId = idFactory();
504
648
  setIsSubmitting(true);
505
649
  setError(null);
650
+ const acknowledgeDraft = message === void 0 || content === input.trim() ? captureAcknowledgement() : () => {
651
+ };
652
+ const submittedIdentity = identityKeyRef.current;
506
653
  try {
507
654
  const result = await transport.sendMessage({
508
655
  message: content,
@@ -511,9 +658,9 @@ function useAgentChat({
511
658
  files: pendingFiles,
512
659
  ...webSearch && webSearchOn !== void 0 && { webSearchEnabled: webSearchOn }
513
660
  });
514
- if (!mountedRef.current) return result;
661
+ if (result.accepted) acknowledgeDraft();
662
+ if (!mountedRef.current || identityKeyRef.current !== submittedIdentity) return result;
515
663
  if (result.accepted) {
516
- setInput("");
517
664
  setPendingFiles([]);
518
665
  if (result.pending) {
519
666
  setMessages((current) => mergeUniqueMessages([...current, result.pending]));
@@ -526,10 +673,10 @@ function useAgentChat({
526
673
  }
527
674
  return result;
528
675
  } catch (err) {
529
- handleError(err);
676
+ if (mountedRef.current && identityKeyRef.current === submittedIdentity) handleError(err);
530
677
  return { accepted: false, requestId, error: err instanceof Error ? err.message : String(err) };
531
678
  } finally {
532
- if (mountedRef.current) setIsSubmitting(false);
679
+ if (mountedRef.current && identityKeyRef.current === submittedIdentity) setIsSubmitting(false);
533
680
  }
534
681
  },
535
682
  [
@@ -544,6 +691,7 @@ function useAgentChat({
544
691
  pendingFiles,
545
692
  sessionId,
546
693
  setMessages,
694
+ captureAcknowledgement,
547
695
  startSubscription,
548
696
  transport,
549
697
  webSearch,
@@ -758,6 +906,7 @@ function useAgentChat({
758
906
  const canAbort = !mutationsBlocked && !isAborting && hasActive;
759
907
  const phase = derivePhase({ isLoadingHistory, isLoadingEarlier, isSubmitting, isStreaming, isAborting, error });
760
908
  const isWorking = isSubmitting || isStreaming || isAborting || isCompacting || hasActive;
909
+ useUpdateBlocker({ blocked: input.length > 0 || pendingFiles.length > 0 || isWorking, reason: "Unfinished chat" });
761
910
  const isTransportActiveCb = useCallback(
762
911
  () => isStreamingRef.current || isSubmittingRef.current || isAbortingRef.current,
763
912
  []
@@ -1374,5 +1523,6 @@ export {
1374
1523
  areAgentChatMutationsBlocked as a,
1375
1524
  useAgentChatSessions as b,
1376
1525
  useAutomationRun as c,
1526
+ useUpdateBlocker as d,
1377
1527
  useAgentChat as u
1378
1528
  };
@@ -1,7 +1,8 @@
1
1
  import { useQueryClient, useQuery, useMutation, useInfiniteQuery } from "@tanstack/react-query";
2
2
  import { useState, useCallback, useEffect, useMemo } from "react";
3
- import { a as automationStatuses, G as listRunsByAgent, g as createAutomationRun, a1 as uploadFile, J as onInitMessage, T as pbSql, V as pbUpdateRecord } from "./automations-DD15itWa.js";
4
- import { n as normalizeRecordAuditCollections, e as normalizeRecordAuditPageSize, l as listRecordAuditLog, g as getRecordAuditLogSnapshot } from "./audit-log-Dmzbqtc0.js";
3
+ import { a as automationStatuses, H as listRunsByAgent, g as createAutomationRun, a2 as uploadFile, K as onInitMessage, U as pbSql, W as pbUpdateRecord } from "./automations-3V0uflxE.js";
4
+ import { d as useUpdateBlocker } from "./use-automation-run-C0JNbLzy.js";
5
+ import { n as normalizeRecordAuditCollections, e as normalizeRecordAuditPageSize, l as listRecordAuditLog, g as getRecordAuditLogSnapshot } from "./audit-log-BNaHuJ1Y.js";
5
6
  function useAutomationAgent({
6
7
  agentId,
7
8
  limit = 5,
@@ -85,6 +86,7 @@ function useAutomationAgent({
85
86
  function useFileUpload(options) {
86
87
  const { collectionId, fieldName, onSuccess, onError } = options;
87
88
  const [status, setStatus] = useState("idle");
89
+ useUpdateBlocker({ blocked: status === "uploading", reason: "Uploading a file" });
88
90
  const [error, setError] = useState(null);
89
91
  const [result, setResult] = useState(null);
90
92
  const upload = useCallback(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lumerahq/ui",
3
- "version": "0.20.5",
3
+ "version": "0.21.0",
4
4
  "type": "module",
5
5
  "sideEffects": [
6
6
  "*.css"
@@ -1,4 +0,0 @@
1
- import { H as po } from "./RecordSheet-SPyDKXhe.js";
2
- export {
3
- po as Mermaid
4
- };