@devicai/ui 0.57.0 → 0.59.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 (223) hide show
  1. package/README.md +186 -0
  2. package/dist/cjs/api/assistantInfo.js.map +1 -1
  3. package/dist/cjs/api/client.js +17 -1
  4. package/dist/cjs/api/client.js.map +1 -1
  5. package/dist/cjs/api/types.js.map +1 -1
  6. package/dist/cjs/components/AICommandBar/AICommandBar.js +12 -8
  7. package/dist/cjs/components/AICommandBar/AICommandBar.js.map +1 -1
  8. package/dist/cjs/components/AICommandBar/useAICommandBar.js +12 -6
  9. package/dist/cjs/components/AICommandBar/useAICommandBar.js.map +1 -1
  10. package/dist/cjs/components/AIElementWrapper/AIElementWrapper.js +19 -15
  11. package/dist/cjs/components/AIElementWrapper/AIElementWrapper.js.map +1 -1
  12. package/dist/cjs/components/AIElementWrapper/activeWrapperRegistry.js.map +1 -1
  13. package/dist/cjs/components/AIElementWrapper/useAIElementWrapper.js +12 -6
  14. package/dist/cjs/components/AIElementWrapper/useAIElementWrapper.js.map +1 -1
  15. package/dist/cjs/components/AIGenerationButton/AIGenerationButton.js +13 -7
  16. package/dist/cjs/components/AIGenerationButton/AIGenerationButton.js.map +1 -1
  17. package/dist/cjs/components/AIGenerationButton/useAIGenerationButton.js +11 -5
  18. package/dist/cjs/components/AIGenerationButton/useAIGenerationButton.js.map +1 -1
  19. package/dist/cjs/components/ChatDrawer/ChatDrawer.js +28 -13
  20. package/dist/cjs/components/ChatDrawer/ChatDrawer.js.map +1 -1
  21. package/dist/cjs/components/ChatDrawer/ChatInput.js +21 -15
  22. package/dist/cjs/components/ChatDrawer/ChatInput.js.map +1 -1
  23. package/dist/cjs/components/ChatDrawer/ChatMessages.js +54 -13
  24. package/dist/cjs/components/ChatDrawer/ChatMessages.js.map +1 -1
  25. package/dist/cjs/components/ChatDrawer/CompactionWidget.js +13 -4
  26. package/dist/cjs/components/ChatDrawer/CompactionWidget.js.map +1 -1
  27. package/dist/cjs/components/ChatDrawer/ConversationSelector.js +6 -4
  28. package/dist/cjs/components/ChatDrawer/ConversationSelector.js.map +1 -1
  29. package/dist/cjs/components/ChatDrawer/ErrorBoundary.js.map +1 -1
  30. package/dist/cjs/components/ChatDrawer/GuardrailNotice.js +40 -0
  31. package/dist/cjs/components/ChatDrawer/GuardrailNotice.js.map +1 -0
  32. package/dist/cjs/components/ChatDrawer/HandoffSubagentWidget.js +3 -1
  33. package/dist/cjs/components/ChatDrawer/HandoffSubagentWidget.js.map +1 -1
  34. package/dist/cjs/components/ChatDrawer/LimitBanner.js +19 -9
  35. package/dist/cjs/components/ChatDrawer/LimitBanner.js.map +1 -1
  36. package/dist/cjs/components/ChatDrawer/QueueNotice.js +19 -7
  37. package/dist/cjs/components/ChatDrawer/QueueNotice.js.map +1 -1
  38. package/dist/cjs/components/ChatDrawer/RecalledMemoriesWidget.js +17 -5
  39. package/dist/cjs/components/ChatDrawer/RecalledMemoriesWidget.js.map +1 -1
  40. package/dist/cjs/components/ChatDrawer/ReferenceChip.js +3 -1
  41. package/dist/cjs/components/ChatDrawer/ReferenceChip.js.map +1 -1
  42. package/dist/cjs/components/ChatDrawer/ToolTimeline.js.map +1 -1
  43. package/dist/cjs/components/ChatDrawer/UsageBar.js +23 -18
  44. package/dist/cjs/components/ChatDrawer/UsageBar.js.map +1 -1
  45. package/dist/cjs/components/ChatDrawer/pastedText.js.map +1 -1
  46. package/dist/cjs/components/CoreMemoryModal/CoreMemoryModal.js +20 -9
  47. package/dist/cjs/components/CoreMemoryModal/CoreMemoryModal.js.map +1 -1
  48. package/dist/cjs/components/Feedback/FeedbackModal.js +5 -1
  49. package/dist/cjs/components/Feedback/FeedbackModal.js.map +1 -1
  50. package/dist/cjs/components/Feedback/MessageActions.js +3 -1
  51. package/dist/cjs/components/Feedback/MessageActions.js.map +1 -1
  52. package/dist/cjs/components/IntegrationsModal/ConnectFieldsForm.js +12 -8
  53. package/dist/cjs/components/IntegrationsModal/ConnectFieldsForm.js.map +1 -1
  54. package/dist/cjs/components/IntegrationsModal/IntegrationLogo.js.map +1 -1
  55. package/dist/cjs/components/IntegrationsModal/IntegrationsHint.js +5 -3
  56. package/dist/cjs/components/IntegrationsModal/IntegrationsHint.js.map +1 -1
  57. package/dist/cjs/components/IntegrationsModal/IntegrationsLauncher.js +12 -3
  58. package/dist/cjs/components/IntegrationsModal/IntegrationsLauncher.js.map +1 -1
  59. package/dist/cjs/components/IntegrationsModal/IntegrationsModal.js.map +1 -1
  60. package/dist/cjs/components/IntegrationsModal/IntegrationsPanel.js +34 -24
  61. package/dist/cjs/components/IntegrationsModal/IntegrationsPanel.js.map +1 -1
  62. package/dist/cjs/components/IntegrationsModal/IntegrationsToggle.js +15 -6
  63. package/dist/cjs/components/IntegrationsModal/IntegrationsToggle.js.map +1 -1
  64. package/dist/cjs/components/IntegrationsModal/McpConnectForm.js +15 -7
  65. package/dist/cjs/components/IntegrationsModal/McpConnectForm.js.map +1 -1
  66. package/dist/cjs/components/IntegrationsModal/McpServersSection.js +32 -19
  67. package/dist/cjs/components/IntegrationsModal/McpServersSection.js.map +1 -1
  68. package/dist/cjs/components/IntegrationsModal/integrationChoice.js.map +1 -1
  69. package/dist/cjs/components/IntegrationsModal/useIntegrations.js +3 -1
  70. package/dist/cjs/components/IntegrationsModal/useIntegrations.js.map +1 -1
  71. package/dist/cjs/components/IntegrationsModal/useTenantMcp.js +3 -1
  72. package/dist/cjs/components/IntegrationsModal/useTenantMcp.js.map +1 -1
  73. package/dist/cjs/components/ThreadStateTag/ThreadStateTag.js +63 -40
  74. package/dist/cjs/components/ThreadStateTag/ThreadStateTag.js.map +1 -1
  75. package/dist/cjs/components/theme.js.map +1 -1
  76. package/dist/cjs/hooks/useDevicChat.js +21 -7
  77. package/dist/cjs/hooks/useDevicChat.js.map +1 -1
  78. package/dist/cjs/hooks/useModelInterface.js.map +1 -1
  79. package/dist/cjs/hooks/usePolling.js +117 -7
  80. package/dist/cjs/hooks/usePolling.js.map +1 -1
  81. package/dist/cjs/hooks/useSpeechRecording.js +8 -4
  82. package/dist/cjs/hooks/useSpeechRecording.js.map +1 -1
  83. package/dist/cjs/i18n/translate.js +48 -0
  84. package/dist/cjs/i18n/translate.js.map +1 -0
  85. package/dist/cjs/i18n/useTranslations.js +57 -0
  86. package/dist/cjs/i18n/useTranslations.js.map +1 -0
  87. package/dist/cjs/index.js +11 -0
  88. package/dist/cjs/index.js.map +1 -1
  89. package/dist/cjs/provider/DevicContext.js.map +1 -1
  90. package/dist/cjs/provider/DevicProvider.js +5 -1
  91. package/dist/cjs/provider/DevicProvider.js.map +1 -1
  92. package/dist/cjs/provider/sharedSession.js.map +1 -1
  93. package/dist/cjs/styles.css +1 -1
  94. package/dist/cjs/utils/avatar.js.map +1 -1
  95. package/dist/cjs/utils/consumeChatStream.js +44 -0
  96. package/dist/cjs/utils/consumeChatStream.js.map +1 -0
  97. package/dist/cjs/utils/index.js.map +1 -1
  98. package/dist/cjs/utils/logger.js.map +1 -1
  99. package/dist/cjs/utils/toolGroups.js.map +1 -1
  100. package/dist/esm/api/assistantInfo.js.map +1 -1
  101. package/dist/esm/api/client.d.ts +5 -1
  102. package/dist/esm/api/client.js +17 -1
  103. package/dist/esm/api/client.js.map +1 -1
  104. package/dist/esm/api/types.d.ts +12 -1
  105. package/dist/esm/api/types.js.map +1 -1
  106. package/dist/esm/components/AICommandBar/AICommandBar.js +13 -9
  107. package/dist/esm/components/AICommandBar/AICommandBar.js.map +1 -1
  108. package/dist/esm/components/AICommandBar/AICommandBar.types.d.ts +16 -0
  109. package/dist/esm/components/AICommandBar/useAICommandBar.d.ts +2 -0
  110. package/dist/esm/components/AICommandBar/useAICommandBar.js +13 -7
  111. package/dist/esm/components/AICommandBar/useAICommandBar.js.map +1 -1
  112. package/dist/esm/components/AIElementWrapper/AIElementWrapper.js +19 -15
  113. package/dist/esm/components/AIElementWrapper/AIElementWrapper.js.map +1 -1
  114. package/dist/esm/components/AIElementWrapper/AIElementWrapper.types.d.ts +16 -0
  115. package/dist/esm/components/AIElementWrapper/activeWrapperRegistry.js.map +1 -1
  116. package/dist/esm/components/AIElementWrapper/useAIElementWrapper.d.ts +2 -0
  117. package/dist/esm/components/AIElementWrapper/useAIElementWrapper.js +13 -7
  118. package/dist/esm/components/AIElementWrapper/useAIElementWrapper.js.map +1 -1
  119. package/dist/esm/components/AIGenerationButton/AIGenerationButton.js +14 -8
  120. package/dist/esm/components/AIGenerationButton/AIGenerationButton.js.map +1 -1
  121. package/dist/esm/components/AIGenerationButton/AIGenerationButton.types.d.ts +16 -0
  122. package/dist/esm/components/AIGenerationButton/useAIGenerationButton.d.ts +2 -0
  123. package/dist/esm/components/AIGenerationButton/useAIGenerationButton.js +12 -6
  124. package/dist/esm/components/AIGenerationButton/useAIGenerationButton.js.map +1 -1
  125. package/dist/esm/components/ChatDrawer/ChatDrawer.js +29 -14
  126. package/dist/esm/components/ChatDrawer/ChatDrawer.js.map +1 -1
  127. package/dist/esm/components/ChatDrawer/ChatDrawer.types.d.ts +41 -0
  128. package/dist/esm/components/ChatDrawer/ChatInput.js +21 -15
  129. package/dist/esm/components/ChatDrawer/ChatInput.js.map +1 -1
  130. package/dist/esm/components/ChatDrawer/ChatMessages.d.ts +1 -1
  131. package/dist/esm/components/ChatDrawer/ChatMessages.js +54 -13
  132. package/dist/esm/components/ChatDrawer/ChatMessages.js.map +1 -1
  133. package/dist/esm/components/ChatDrawer/CompactionWidget.js +13 -4
  134. package/dist/esm/components/ChatDrawer/CompactionWidget.js.map +1 -1
  135. package/dist/esm/components/ChatDrawer/ConversationSelector.js +6 -4
  136. package/dist/esm/components/ChatDrawer/ConversationSelector.js.map +1 -1
  137. package/dist/esm/components/ChatDrawer/ErrorBoundary.js.map +1 -1
  138. package/dist/esm/components/ChatDrawer/GuardrailNotice.d.ts +36 -0
  139. package/dist/esm/components/ChatDrawer/GuardrailNotice.js +38 -0
  140. package/dist/esm/components/ChatDrawer/GuardrailNotice.js.map +1 -0
  141. package/dist/esm/components/ChatDrawer/HandoffSubagentWidget.js +3 -1
  142. package/dist/esm/components/ChatDrawer/HandoffSubagentWidget.js.map +1 -1
  143. package/dist/esm/components/ChatDrawer/LimitBanner.js +19 -9
  144. package/dist/esm/components/ChatDrawer/LimitBanner.js.map +1 -1
  145. package/dist/esm/components/ChatDrawer/QueueNotice.js +19 -7
  146. package/dist/esm/components/ChatDrawer/QueueNotice.js.map +1 -1
  147. package/dist/esm/components/ChatDrawer/RecalledMemoriesWidget.js +17 -5
  148. package/dist/esm/components/ChatDrawer/RecalledMemoriesWidget.js.map +1 -1
  149. package/dist/esm/components/ChatDrawer/ReferenceChip.js +3 -1
  150. package/dist/esm/components/ChatDrawer/ReferenceChip.js.map +1 -1
  151. package/dist/esm/components/ChatDrawer/ToolTimeline.js.map +1 -1
  152. package/dist/esm/components/ChatDrawer/UsageBar.js +23 -18
  153. package/dist/esm/components/ChatDrawer/UsageBar.js.map +1 -1
  154. package/dist/esm/components/ChatDrawer/index.d.ts +2 -0
  155. package/dist/esm/components/ChatDrawer/pastedText.js.map +1 -1
  156. package/dist/esm/components/CoreMemoryModal/CoreMemoryModal.js +20 -9
  157. package/dist/esm/components/CoreMemoryModal/CoreMemoryModal.js.map +1 -1
  158. package/dist/esm/components/Feedback/FeedbackModal.js +5 -1
  159. package/dist/esm/components/Feedback/FeedbackModal.js.map +1 -1
  160. package/dist/esm/components/Feedback/MessageActions.js +3 -1
  161. package/dist/esm/components/Feedback/MessageActions.js.map +1 -1
  162. package/dist/esm/components/IntegrationsModal/ConnectFieldsForm.js +12 -8
  163. package/dist/esm/components/IntegrationsModal/ConnectFieldsForm.js.map +1 -1
  164. package/dist/esm/components/IntegrationsModal/IntegrationLogo.js.map +1 -1
  165. package/dist/esm/components/IntegrationsModal/IntegrationsHint.js +5 -3
  166. package/dist/esm/components/IntegrationsModal/IntegrationsHint.js.map +1 -1
  167. package/dist/esm/components/IntegrationsModal/IntegrationsLauncher.js +12 -3
  168. package/dist/esm/components/IntegrationsModal/IntegrationsLauncher.js.map +1 -1
  169. package/dist/esm/components/IntegrationsModal/IntegrationsModal.js.map +1 -1
  170. package/dist/esm/components/IntegrationsModal/IntegrationsPanel.d.ts +1 -1
  171. package/dist/esm/components/IntegrationsModal/IntegrationsPanel.js +34 -24
  172. package/dist/esm/components/IntegrationsModal/IntegrationsPanel.js.map +1 -1
  173. package/dist/esm/components/IntegrationsModal/IntegrationsToggle.d.ts +1 -1
  174. package/dist/esm/components/IntegrationsModal/IntegrationsToggle.js +15 -6
  175. package/dist/esm/components/IntegrationsModal/IntegrationsToggle.js.map +1 -1
  176. package/dist/esm/components/IntegrationsModal/McpConnectForm.js +15 -7
  177. package/dist/esm/components/IntegrationsModal/McpConnectForm.js.map +1 -1
  178. package/dist/esm/components/IntegrationsModal/McpServersSection.js +32 -19
  179. package/dist/esm/components/IntegrationsModal/McpServersSection.js.map +1 -1
  180. package/dist/esm/components/IntegrationsModal/integrationChoice.js.map +1 -1
  181. package/dist/esm/components/IntegrationsModal/useIntegrations.js +3 -1
  182. package/dist/esm/components/IntegrationsModal/useIntegrations.js.map +1 -1
  183. package/dist/esm/components/IntegrationsModal/useTenantMcp.js +3 -1
  184. package/dist/esm/components/IntegrationsModal/useTenantMcp.js.map +1 -1
  185. package/dist/esm/components/ThreadStateTag/ThreadStateTag.js +63 -40
  186. package/dist/esm/components/ThreadStateTag/ThreadStateTag.js.map +1 -1
  187. package/dist/esm/components/theme.js.map +1 -1
  188. package/dist/esm/hooks/index.d.ts +1 -1
  189. package/dist/esm/hooks/useDevicChat.d.ts +6 -0
  190. package/dist/esm/hooks/useDevicChat.js +23 -9
  191. package/dist/esm/hooks/useDevicChat.js.map +1 -1
  192. package/dist/esm/hooks/useModelInterface.js.map +1 -1
  193. package/dist/esm/hooks/usePolling.d.ts +35 -0
  194. package/dist/esm/hooks/usePolling.js +114 -8
  195. package/dist/esm/hooks/usePolling.js.map +1 -1
  196. package/dist/esm/hooks/useSpeechRecording.js +8 -4
  197. package/dist/esm/hooks/useSpeechRecording.js.map +1 -1
  198. package/dist/esm/i18n/index.d.ts +4 -0
  199. package/dist/esm/i18n/translate.d.ts +23 -0
  200. package/dist/esm/i18n/translate.js +43 -0
  201. package/dist/esm/i18n/translate.js.map +1 -0
  202. package/dist/esm/i18n/types.d.ts +27 -0
  203. package/dist/esm/i18n/useTranslations.d.ts +32 -0
  204. package/dist/esm/i18n/useTranslations.js +53 -0
  205. package/dist/esm/i18n/useTranslations.js.map +1 -0
  206. package/dist/esm/index.d.ts +4 -2
  207. package/dist/esm/index.js +3 -0
  208. package/dist/esm/index.js.map +1 -1
  209. package/dist/esm/provider/DevicContext.js.map +1 -1
  210. package/dist/esm/provider/DevicProvider.d.ts +1 -1
  211. package/dist/esm/provider/DevicProvider.js +5 -1
  212. package/dist/esm/provider/DevicProvider.js.map +1 -1
  213. package/dist/esm/provider/sharedSession.js.map +1 -1
  214. package/dist/esm/provider/types.d.ts +51 -0
  215. package/dist/esm/styles.css +1 -1
  216. package/dist/esm/utils/avatar.js.map +1 -1
  217. package/dist/esm/utils/consumeChatStream.d.ts +6 -0
  218. package/dist/esm/utils/consumeChatStream.js +42 -0
  219. package/dist/esm/utils/consumeChatStream.js.map +1 -0
  220. package/dist/esm/utils/index.js.map +1 -1
  221. package/dist/esm/utils/logger.js.map +1 -1
  222. package/dist/esm/utils/toolGroups.js.map +1 -1
  223. package/package.json +3 -1
@@ -13,6 +13,14 @@ const DEFAULT_POLLING_INTERVAL_MS = 1000;
13
13
  * request loop against the API rather than a faster chat.
14
14
  */
15
15
  const MIN_POLLING_INTERVAL_MS = 250;
16
+ /**
17
+ * A stream that has carried nothing (no snapshot, no keep-alive) for this long
18
+ * is presumed dead and reopened. The API sends a keep-alive every 15 s, so a
19
+ * healthy connection never gets near it.
20
+ */
21
+ const DEFAULT_STREAM_SILENCE_MS = 30000;
22
+ /** Pause between a stream ending (the server's lifetime) and the next one. */
23
+ const STREAM_RECONNECT_DELAY_MS = 250;
16
24
  /**
17
25
  * Picks the first usable cadence out of the candidates, in priority order
18
26
  * (component prop, then provider, then the caller's own default), ignoring
@@ -27,6 +35,24 @@ function resolvePollingInterval(...candidates) {
27
35
  }
28
36
  return DEFAULT_POLLING_INTERVAL_MS;
29
37
  }
38
+ /**
39
+ * Whether a conversation in progress is followed over a server-sent event
40
+ * stream instead of polled. Off until the integrator opts in; the flag will
41
+ * default to on once the streaming endpoint has been exercised in the field.
42
+ */
43
+ const DEFAULT_STREAMING = false;
44
+ /**
45
+ * Picks the first explicit choice out of the candidates, in priority order
46
+ * (component prop, then provider), ignoring anything that is not a boolean
47
+ * and falling back to `DEFAULT_STREAMING`.
48
+ */
49
+ function resolveStreaming(...candidates) {
50
+ for (const value of candidates) {
51
+ if (typeof value === 'boolean')
52
+ return value;
53
+ }
54
+ return DEFAULT_STREAMING;
55
+ }
30
56
  /**
31
57
  * Hook for polling real-time chat history
32
58
  *
@@ -44,6 +70,18 @@ function usePolling(chatUid, fetchFn, options = {}) {
44
70
  const [error, setError] = React.useState(null);
45
71
  const intervalRef = React.useRef(null);
46
72
  const isMountedRef = React.useRef(true);
73
+ const chatUidRef = React.useRef(chatUid);
74
+ chatUidRef.current = chatUid;
75
+ const streamFnRef = React.useRef(options.streamFn);
76
+ streamFnRef.current = options.streamFn;
77
+ const lastStreamAt = React.useRef(0);
78
+ // While a stream connection is open the timer makes no request at all: the
79
+ // stream is the source, the poll only the recovery.
80
+ const streamAliveRef = React.useRef(false);
81
+ const lastStreamActivity = React.useRef(0);
82
+ const streamAttemptRef = React.useRef(null);
83
+ const streamSilenceRef = React.useRef(options.streamSilenceMs ?? DEFAULT_STREAM_SILENCE_MS);
84
+ streamSilenceRef.current = options.streamSilenceMs ?? DEFAULT_STREAM_SILENCE_MS;
47
85
  // Refs for callbacks and options to avoid stale closures and unnecessary re-renders
48
86
  const onStopRef = React.useRef(onStop);
49
87
  const holdOpenRef = React.useRef(holdOpen);
@@ -72,13 +110,28 @@ function usePolling(chatUid, fetchFn, options = {}) {
72
110
  armedIntervalRef.current = null;
73
111
  isPollingRef.current = false;
74
112
  }, []);
75
- const fetchData = React.useCallback(async () => {
113
+ const fetchData = React.useCallback(async (snapshot, force = false) => {
76
114
  logRef.current.log('[usePolling] fetchData called, isMounted:', isMountedRef.current);
77
115
  if (!isMountedRef.current)
78
116
  return;
79
117
  try {
80
118
  logRef.current.log('[usePolling] Fetching...');
81
- const result = await fetchFnRef.current();
119
+ const started = Date.now();
120
+ const startedChat = chatUidRef.current;
121
+ if (!snapshot && !force && streamAliveRef.current) {
122
+ // A stream that has gone quiet for too long is presumed dead: drop it
123
+ // so the loop below reopens it. Until then, nothing is asked.
124
+ if (started - lastStreamActivity.current > streamSilenceRef.current) {
125
+ logRef.current.log('[usePolling] Stream silent for too long, reconnecting');
126
+ streamAttemptRef.current?.abort();
127
+ }
128
+ return;
129
+ }
130
+ const result = snapshot || await fetchFnRef.current();
131
+ if (startedChat !== chatUidRef.current)
132
+ return;
133
+ if (!snapshot && lastStreamAt.current > started)
134
+ return;
82
135
  logRef.current.log('[usePolling] Fetch result:', { status: result.status, messageCount: result.chatHistory?.length });
83
136
  if (!isMountedRef.current)
84
137
  return;
@@ -108,14 +161,65 @@ function usePolling(chatUid, fetchFn, options = {}) {
108
161
  setIsPolling(false);
109
162
  }
110
163
  }, [clearPolling]);
164
+ React.useEffect(() => {
165
+ if (!enabled || !chatUid || !isPolling || !streamFnRef.current)
166
+ return;
167
+ const controller = new AbortController();
168
+ lastStreamAt.current = 0;
169
+ const leave = () => {
170
+ streamAliveRef.current = false;
171
+ streamAttemptRef.current = null;
172
+ lastStreamAt.current = 0;
173
+ };
174
+ const connect = async () => {
175
+ while (!controller.signal.aborted && isPollingRef.current) {
176
+ // One controller per attempt, so the silence watchdog can drop a dead
177
+ // connection without ending the loop.
178
+ const attempt = new AbortController();
179
+ const abortAttempt = () => attempt.abort();
180
+ controller.signal.addEventListener('abort', abortAttempt);
181
+ streamAttemptRef.current = attempt;
182
+ streamAliveRef.current = true;
183
+ lastStreamActivity.current = Date.now();
184
+ let failed = false;
185
+ try {
186
+ await streamFnRef.current(async (snapshot) => {
187
+ if (attempt.signal.aborted || !isPollingRef.current)
188
+ return;
189
+ lastStreamAt.current = Date.now();
190
+ lastStreamActivity.current = lastStreamAt.current;
191
+ await fetchData(snapshot);
192
+ }, attempt.signal, () => { lastStreamActivity.current = Date.now(); });
193
+ }
194
+ catch {
195
+ // Our own abort is a reconnect; anything else means the stream is
196
+ // not available here, and the poll is the recovery path.
197
+ failed = !attempt.signal.aborted;
198
+ }
199
+ finally {
200
+ controller.signal.removeEventListener('abort', abortAttempt);
201
+ }
202
+ leave();
203
+ if (failed || controller.signal.aborted)
204
+ break;
205
+ // The server closes a stream after its lifetime; reopen right away.
206
+ await new Promise(resolve => setTimeout(resolve, STREAM_RECONNECT_DELAY_MS));
207
+ }
208
+ leave();
209
+ };
210
+ void connect();
211
+ return () => { controller.abort(); leave(); };
212
+ }, [enabled, chatUid, isPolling, fetchData]);
111
213
  const start = React.useCallback(() => {
112
214
  if (intervalRef.current)
113
215
  return;
114
216
  isPollingRef.current = true;
115
217
  setIsPolling(true);
116
218
  setError(null);
117
- // Immediate first fetch
118
- fetchData();
219
+ // Immediate first fetch — unless a stream is about to open, whose first
220
+ // frame is that same state.
221
+ if (!streamFnRef.current)
222
+ fetchData();
119
223
  // Set up interval
120
224
  armedIntervalRef.current = intervalValueRef.current;
121
225
  intervalRef.current = setInterval(fetchData, intervalValueRef.current);
@@ -125,7 +229,7 @@ function usePolling(chatUid, fetchFn, options = {}) {
125
229
  setIsPolling(false);
126
230
  }, [clearPolling]);
127
231
  const refetch = React.useCallback(async () => {
128
- await fetchData();
232
+ await fetchData(undefined, true);
129
233
  }, [fetchData]);
130
234
  // Auto-start polling when enabled and chatUid is set
131
235
  React.useEffect(() => {
@@ -145,8 +249,10 @@ function usePolling(chatUid, fetchFn, options = {}) {
145
249
  isPollingRef.current = true;
146
250
  setIsPolling(true);
147
251
  setError(null);
148
- // Immediate first fetch
149
- fetchData();
252
+ // Immediate first fetch — unless a stream is about to open, whose first
253
+ // frame is that same state.
254
+ if (!streamFnRef.current)
255
+ fetchData();
150
256
  // Set up interval
151
257
  armedIntervalRef.current = intervalValueRef.current;
152
258
  intervalRef.current = setInterval(fetchData, intervalValueRef.current);
@@ -193,7 +299,11 @@ function usePolling(chatUid, fetchFn, options = {}) {
193
299
  }
194
300
 
195
301
  exports.DEFAULT_POLLING_INTERVAL_MS = DEFAULT_POLLING_INTERVAL_MS;
302
+ exports.DEFAULT_STREAMING = DEFAULT_STREAMING;
303
+ exports.DEFAULT_STREAM_SILENCE_MS = DEFAULT_STREAM_SILENCE_MS;
196
304
  exports.MIN_POLLING_INTERVAL_MS = MIN_POLLING_INTERVAL_MS;
305
+ exports.STREAM_RECONNECT_DELAY_MS = STREAM_RECONNECT_DELAY_MS;
197
306
  exports.resolvePollingInterval = resolvePollingInterval;
307
+ exports.resolveStreaming = resolveStreaming;
198
308
  exports.usePolling = usePolling;
199
309
  //# sourceMappingURL=usePolling.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"usePolling.js","sources":["../../../../src/hooks/usePolling.ts"],"sourcesContent":["import { useState, useEffect, useRef, useCallback, useMemo } from 'react';\nimport type { RealtimeChatHistory, RealtimeStatus } from '../api/types';\nimport { createLogger } from '../utils/logger';\n\n/**\n * Cadence used when nobody configures one.\n */\nexport const DEFAULT_POLLING_INTERVAL_MS = 1000;\n\n/**\n * Floor applied to any configured cadence. A conversation in flight is polled\n * from every mounted component, so a value below this turns the widget into a\n * request loop against the API rather than a faster chat.\n */\nexport const MIN_POLLING_INTERVAL_MS = 250;\n\n/**\n * Picks the first usable cadence out of the candidates, in priority order\n * (component prop, then provider, then the caller's own default), ignoring\n * anything that is not a positive finite number and clamping the winner to\n * `MIN_POLLING_INTERVAL_MS`.\n */\nexport function resolvePollingInterval(\n ...candidates: Array<number | undefined | null>\n): number {\n for (const value of candidates) {\n if (typeof value === 'number' && Number.isFinite(value) && value > 0) {\n return Math.max(value, MIN_POLLING_INTERVAL_MS);\n }\n }\n return DEFAULT_POLLING_INTERVAL_MS;\n}\n\nexport interface UsePollingOptions {\n /**\n * Polling interval in milliseconds\n * @default 1000\n */\n interval?: number;\n\n /**\n * Whether polling is enabled\n * @default true\n */\n enabled?: boolean;\n\n /**\n * Statuses that should stop polling\n * @default ['completed', 'error']\n */\n stopStatuses?: RealtimeStatus[];\n\n /**\n * Consulted when the status says the conversation is done. Returning true\n * keeps the poll running and skips `onStop`.\n *\n * A run can settle while the conversation still owes an answer to something\n * queued: between one run liquidating and the follow-up run marking itself as\n * processing, the realtime state reads as finished. Stopping in that window\n * leaves the answer invisible until a reload. Whoever passes this is\n * responsible for bounding it — the hook will hold open for as long as it is\n * told to.\n */\n holdOpen?: (data: RealtimeChatHistory) => boolean;\n\n /**\n * Callback when polling stops\n */\n onStop?: (data: RealtimeChatHistory | null) => void;\n\n /**\n * Callback on each poll update\n */\n onUpdate?: (data: RealtimeChatHistory) => void;\n\n /**\n * Callback on poll error\n */\n onError?: (error: Error) => void;\n\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n}\n\nexport interface UsePollingResult {\n /**\n * Current polling data\n */\n data: RealtimeChatHistory | null;\n\n /**\n * Whether polling is currently active\n */\n isPolling: boolean;\n\n /**\n * Last error that occurred\n */\n error: Error | null;\n\n /**\n * Start polling\n */\n start: () => void;\n\n /**\n * Stop polling\n */\n stop: () => void;\n\n /**\n * Manually trigger a fetch\n */\n refetch: () => Promise<void>;\n}\n\n/**\n * Hook for polling real-time chat history\n *\n * @param chatUid - The chat UID to poll for\n * @param fetchFn - Function that fetches the realtime history\n * @param options - Polling options\n */\nexport function usePolling(\n chatUid: string | null,\n fetchFn: () => Promise<RealtimeChatHistory>,\n options: UsePollingOptions = {}\n): UsePollingResult {\n const {\n interval = DEFAULT_POLLING_INTERVAL_MS,\n enabled = true,\n stopStatuses = ['completed', 'error'],\n holdOpen,\n onStop,\n onUpdate,\n onError,\n debug = false,\n } = options;\n\n const log = useMemo(() => createLogger(debug), [debug]);\n const logRef = useRef(log);\n logRef.current = log;\n\n const [data, setData] = useState<RealtimeChatHistory | null>(null);\n const [isPolling, setIsPolling] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);\n const isMountedRef = useRef(true);\n\n // Refs for callbacks and options to avoid stale closures and unnecessary re-renders\n const onStopRef = useRef(onStop);\n const holdOpenRef = useRef(holdOpen);\n const onUpdateRef = useRef(onUpdate);\n const onErrorRef = useRef(onError);\n const fetchFnRef = useRef(fetchFn);\n const stopStatusesRef = useRef(stopStatuses);\n const intervalValueRef = useRef(interval);\n const isPollingRef = useRef(false);\n // Cadence the running timer was armed with, so a change can be detected.\n const armedIntervalRef = useRef<number | null>(null);\n\n useEffect(() => {\n onStopRef.current = onStop;\n holdOpenRef.current = holdOpen;\n onUpdateRef.current = onUpdate;\n onErrorRef.current = onError;\n fetchFnRef.current = fetchFn;\n stopStatusesRef.current = stopStatuses;\n intervalValueRef.current = interval;\n });\n\n const clearPolling = useCallback(() => {\n if (intervalRef.current) {\n clearInterval(intervalRef.current);\n intervalRef.current = null;\n }\n armedIntervalRef.current = null;\n isPollingRef.current = false;\n }, []);\n\n const fetchData = useCallback(async () => {\n logRef.current.log('[usePolling] fetchData called, isMounted:', isMountedRef.current);\n if (!isMountedRef.current) return;\n\n try {\n logRef.current.log('[usePolling] Fetching...');\n const result = await fetchFnRef.current();\n logRef.current.log('[usePolling] Fetch result:', { status: result.status, messageCount: result.chatHistory?.length });\n\n if (!isMountedRef.current) return;\n\n setData(result);\n setError(null);\n onUpdateRef.current?.(result);\n\n // Check if we should stop polling\n const shouldStop =\n stopStatusesRef.current.includes(result.status) &&\n !holdOpenRef.current?.(result);\n logRef.current.log('[usePolling] Should stop?', shouldStop, 'stopStatuses:', stopStatusesRef.current, 'current status:', result.status);\n if (shouldStop) {\n logRef.current.log('[usePolling] Stopping polling due to status:', result.status);\n clearPolling();\n setIsPolling(false);\n onStopRef.current?.(result);\n }\n } catch (err) {\n logRef.current.error('[usePolling] Fetch error:', err);\n if (!isMountedRef.current) return;\n\n const error = err instanceof Error ? err : new Error(String(err));\n setError(error);\n onErrorRef.current?.(error);\n\n // Stop polling on error\n clearPolling();\n setIsPolling(false);\n }\n }, [clearPolling]);\n\n const start = useCallback(() => {\n if (intervalRef.current) return;\n\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch\n fetchData();\n\n // Set up interval\n armedIntervalRef.current = intervalValueRef.current;\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n }, [fetchData]);\n\n const stop = useCallback(() => {\n clearPolling();\n setIsPolling(false);\n }, [clearPolling]);\n\n const refetch = useCallback(async () => {\n await fetchData();\n }, [fetchData]);\n\n // Auto-start polling when enabled and chatUid is set\n useEffect(() => {\n logRef.current.log('[usePolling] Auto-start effect triggered:', { enabled, chatUid, isPollingRef: isPollingRef.current, intervalRef: !!intervalRef.current });\n\n if (!enabled || !chatUid) {\n logRef.current.log('[usePolling] Not enabled or no chatUid, stopping if active');\n // Stop polling if disabled or no chatUid\n if (isPollingRef.current) {\n clearPolling();\n setIsPolling(false);\n }\n return;\n }\n\n // Start polling if not already polling\n if (!isPollingRef.current) {\n logRef.current.log('[usePolling] Starting polling, interval:', intervalValueRef.current);\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch\n fetchData();\n\n // Set up interval\n armedIntervalRef.current = intervalValueRef.current;\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n logRef.current.log('[usePolling] Interval set:', intervalRef.current);\n } else {\n logRef.current.log('[usePolling] Already polling, skipping start');\n }\n\n // Only cleanup on unmount, not on every dependency change\n }, [enabled, chatUid, fetchData, clearPolling]);\n\n // Re-arm the timer when the cadence changes while a conversation is already\n // in flight. Without this a new `interval` would only take effect on the next\n // conversation, which is not what changing a provider setting looks like.\n useEffect(() => {\n if (!isPollingRef.current || armedIntervalRef.current === null) return;\n if (armedIntervalRef.current === interval) return;\n\n logRef.current.log('[usePolling] Interval changed mid-flight:', {\n from: armedIntervalRef.current,\n to: interval,\n });\n if (intervalRef.current) clearInterval(intervalRef.current);\n armedIntervalRef.current = interval;\n intervalRef.current = setInterval(fetchData, interval);\n }, [interval, fetchData]);\n\n // Cleanup on unmount\n useEffect(() => {\n isMountedRef.current = true;\n return () => {\n isMountedRef.current = false;\n clearPolling();\n };\n }, [clearPolling]);\n\n return {\n data,\n isPolling,\n error,\n start,\n stop,\n refetch,\n };\n}\n"],"names":["useMemo","createLogger","useRef","useState","useEffect","useCallback"],"mappings":";;;;;AAIA;;AAEG;AACI,MAAM,2BAA2B,GAAG;AAE3C;;;;AAIG;AACI,MAAM,uBAAuB,GAAG;AAEvC;;;;;AAKG;AACG,SAAU,sBAAsB,CACpC,GAAG,UAA4C,EAAA;AAE/C,IAAA,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE;AAC9B,QAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE;YACpE,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,uBAAuB,CAAC;QACjD;IACF;AACA,IAAA,OAAO,2BAA2B;AACpC;AAwFA;;;;;;AAMG;AACG,SAAU,UAAU,CACxB,OAAsB,EACtB,OAA2C,EAC3C,UAA6B,EAAE,EAAA;AAE/B,IAAA,MAAM,EACJ,QAAQ,GAAG,2BAA2B,EACtC,OAAO,GAAG,IAAI,EACd,YAAY,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,EACrC,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,OAAO,EACP,KAAK,GAAG,KAAK,GACd,GAAG,OAAO;AAEX,IAAA,MAAM,GAAG,GAAGA,aAAO,CAAC,MAAMC,mBAAY,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC;AACvD,IAAA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;IAK/CA,eAAS,CAAC,MAAK;QACb,IAAI,CAAC,YAAY,CAAC,OAAO,IAAI,gBAAgB,CAAC,OAAO,KAAK,IAAI;YAAE;AAChE,QAAA,IAAI,gBAAgB,CAAC,OAAO,KAAK,QAAQ;YAAE;AAE3C,QAAA,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,2CAA2C,EAAE;YAC9D,IAAI,EAAE,gBAAgB,CAAC,OAAO;AAC9B,YAAA,EAAE,EAAE,QAAQ;AACb,SAAA,CAAC;QACF,IAAI,WAAW,CAAC,OAAO;AAAE,YAAA,aAAa,CAAC,WAAW,CAAC,OAAO,CAAC;AAC3D,QAAA,gBAAgB,CAAC,OAAO,GAAG,QAAQ;QACnC,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC;AACxD,IAAA,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;;IAGzBA,eAAS,CAAC,MAAK;AACb,QAAA,YAAY,CAAC,OAAO,GAAG,IAAI;AAC3B,QAAA,OAAO,MAAK;AACV,YAAA,YAAY,CAAC,OAAO,GAAG,KAAK;AAC5B,YAAA,YAAY,EAAE;AAChB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC;IAElB,OAAO;QACL,IAAI;QACJ,SAAS;QACT,KAAK;QACL,KAAK;QACL,IAAI;QACJ,OAAO;KACR;AACH;;;;;"}
1
+ {"version":3,"file":"usePolling.js","sources":["../../../../src/hooks/usePolling.ts"],"sourcesContent":["import { useState, useEffect, useRef, useCallback, useMemo } from 'react';\nimport type { RealtimeChatHistory, RealtimeStatus } from '../api/types';\nimport { createLogger } from '../utils/logger';\n\n/**\n * Cadence used when nobody configures one.\n */\nexport const DEFAULT_POLLING_INTERVAL_MS = 1000;\n\n/**\n * Floor applied to any configured cadence. A conversation in flight is polled\n * from every mounted component, so a value below this turns the widget into a\n * request loop against the API rather than a faster chat.\n */\nexport const MIN_POLLING_INTERVAL_MS = 250;\n\n/**\n * A stream that has carried nothing (no snapshot, no keep-alive) for this long\n * is presumed dead and reopened. The API sends a keep-alive every 15 s, so a\n * healthy connection never gets near it.\n */\nexport const DEFAULT_STREAM_SILENCE_MS = 30_000;\n\n/** Pause between a stream ending (the server's lifetime) and the next one. */\nexport const STREAM_RECONNECT_DELAY_MS = 250;\n\n/**\n * Picks the first usable cadence out of the candidates, in priority order\n * (component prop, then provider, then the caller's own default), ignoring\n * anything that is not a positive finite number and clamping the winner to\n * `MIN_POLLING_INTERVAL_MS`.\n */\nexport function resolvePollingInterval(\n ...candidates: Array<number | undefined | null>\n): number {\n for (const value of candidates) {\n if (typeof value === 'number' && Number.isFinite(value) && value > 0) {\n return Math.max(value, MIN_POLLING_INTERVAL_MS);\n }\n }\n return DEFAULT_POLLING_INTERVAL_MS;\n}\n\n/**\n * Whether a conversation in progress is followed over a server-sent event\n * stream instead of polled. Off until the integrator opts in; the flag will\n * default to on once the streaming endpoint has been exercised in the field.\n */\nexport const DEFAULT_STREAMING = false;\n\n/**\n * Picks the first explicit choice out of the candidates, in priority order\n * (component prop, then provider), ignoring anything that is not a boolean\n * and falling back to `DEFAULT_STREAMING`.\n */\nexport function resolveStreaming(\n ...candidates: Array<boolean | undefined | null>\n): boolean {\n for (const value of candidates) {\n if (typeof value === 'boolean') return value;\n }\n return DEFAULT_STREAMING;\n}\n\nexport interface UsePollingOptions {\n /**\n * Opens a server-sent event stream for the conversation and feeds every\n * snapshot it carries. While the connection is open the timer below makes\n * no request at all; when the stream ends it is reopened, and only when it\n * fails does the timer take over. `onActivity` reports every chunk received\n * (keep-alives included) so a silent connection can be told from a dead one.\n * Absent, the hook only polls.\n */\n streamFn?: (\n onSnapshot: (data: RealtimeChatHistory) => Promise<void>,\n signal: AbortSignal,\n onActivity?: () => void,\n ) => Promise<void>;\n\n /**\n * How long (ms) an open stream may stay silent before it is presumed dead\n * and reopened.\n * @default 30000\n */\n streamSilenceMs?: number;\n /**\n * Polling interval in milliseconds\n * @default 1000\n */\n interval?: number;\n\n /**\n * Whether polling is enabled\n * @default true\n */\n enabled?: boolean;\n\n /**\n * Statuses that should stop polling\n * @default ['completed', 'error']\n */\n stopStatuses?: RealtimeStatus[];\n\n /**\n * Consulted when the status says the conversation is done. Returning true\n * keeps the poll running and skips `onStop`.\n *\n * A run can settle while the conversation still owes an answer to something\n * queued: between one run liquidating and the follow-up run marking itself as\n * processing, the realtime state reads as finished. Stopping in that window\n * leaves the answer invisible until a reload. Whoever passes this is\n * responsible for bounding it — the hook will hold open for as long as it is\n * told to.\n */\n holdOpen?: (data: RealtimeChatHistory) => boolean;\n\n /**\n * Callback when polling stops\n */\n onStop?: (data: RealtimeChatHistory | null) => void;\n\n /**\n * Callback on each poll update\n */\n onUpdate?: (data: RealtimeChatHistory) => void;\n\n /**\n * Callback on poll error\n */\n onError?: (error: Error) => void;\n\n /**\n * Enable debug logging\n * @default false\n */\n debug?: boolean;\n}\n\nexport interface UsePollingResult {\n /**\n * Current polling data\n */\n data: RealtimeChatHistory | null;\n\n /**\n * Whether polling is currently active\n */\n isPolling: boolean;\n\n /**\n * Last error that occurred\n */\n error: Error | null;\n\n /**\n * Start polling\n */\n start: () => void;\n\n /**\n * Stop polling\n */\n stop: () => void;\n\n /**\n * Manually trigger a fetch\n */\n refetch: () => Promise<void>;\n}\n\n/**\n * Hook for polling real-time chat history\n *\n * @param chatUid - The chat UID to poll for\n * @param fetchFn - Function that fetches the realtime history\n * @param options - Polling options\n */\nexport function usePolling(\n chatUid: string | null,\n fetchFn: () => Promise<RealtimeChatHistory>,\n options: UsePollingOptions = {}\n): UsePollingResult {\n const {\n interval = DEFAULT_POLLING_INTERVAL_MS,\n enabled = true,\n stopStatuses = ['completed', 'error'],\n holdOpen,\n onStop,\n onUpdate,\n onError,\n debug = false,\n } = options;\n\n const log = useMemo(() => createLogger(debug), [debug]);\n const logRef = useRef(log);\n logRef.current = log;\n\n const [data, setData] = useState<RealtimeChatHistory | null>(null);\n const [isPolling, setIsPolling] = useState(false);\n const [error, setError] = useState<Error | null>(null);\n\n const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);\n const isMountedRef = useRef(true);\n const chatUidRef = useRef(chatUid);\n chatUidRef.current = chatUid;\n const streamFnRef = useRef(options.streamFn);\n streamFnRef.current = options.streamFn;\n const lastStreamAt = useRef(0);\n // While a stream connection is open the timer makes no request at all: the\n // stream is the source, the poll only the recovery.\n const streamAliveRef = useRef(false);\n const lastStreamActivity = useRef(0);\n const streamAttemptRef = useRef<AbortController | null>(null);\n const streamSilenceRef = useRef(options.streamSilenceMs ?? DEFAULT_STREAM_SILENCE_MS);\n streamSilenceRef.current = options.streamSilenceMs ?? DEFAULT_STREAM_SILENCE_MS;\n\n // Refs for callbacks and options to avoid stale closures and unnecessary re-renders\n const onStopRef = useRef(onStop);\n const holdOpenRef = useRef(holdOpen);\n const onUpdateRef = useRef(onUpdate);\n const onErrorRef = useRef(onError);\n const fetchFnRef = useRef(fetchFn);\n const stopStatusesRef = useRef(stopStatuses);\n const intervalValueRef = useRef(interval);\n const isPollingRef = useRef(false);\n // Cadence the running timer was armed with, so a change can be detected.\n const armedIntervalRef = useRef<number | null>(null);\n\n useEffect(() => {\n onStopRef.current = onStop;\n holdOpenRef.current = holdOpen;\n onUpdateRef.current = onUpdate;\n onErrorRef.current = onError;\n fetchFnRef.current = fetchFn;\n stopStatusesRef.current = stopStatuses;\n intervalValueRef.current = interval;\n });\n\n const clearPolling = useCallback(() => {\n if (intervalRef.current) {\n clearInterval(intervalRef.current);\n intervalRef.current = null;\n }\n armedIntervalRef.current = null;\n isPollingRef.current = false;\n }, []);\n\n const fetchData = useCallback(async (snapshot?: RealtimeChatHistory, force = false) => {\n logRef.current.log('[usePolling] fetchData called, isMounted:', isMountedRef.current);\n if (!isMountedRef.current) return;\n\n try {\n logRef.current.log('[usePolling] Fetching...');\n const started = Date.now();\n const startedChat = chatUidRef.current;\n if (!snapshot && !force && streamAliveRef.current) {\n // A stream that has gone quiet for too long is presumed dead: drop it\n // so the loop below reopens it. Until then, nothing is asked.\n if (started - lastStreamActivity.current > streamSilenceRef.current) {\n logRef.current.log('[usePolling] Stream silent for too long, reconnecting');\n streamAttemptRef.current?.abort();\n }\n return;\n }\n const result = snapshot || await fetchFnRef.current();\n if (startedChat !== chatUidRef.current) return;\n if (!snapshot && lastStreamAt.current > started) return;\n logRef.current.log('[usePolling] Fetch result:', { status: result.status, messageCount: result.chatHistory?.length });\n\n if (!isMountedRef.current) return;\n\n setData(result);\n setError(null);\n onUpdateRef.current?.(result);\n\n // Check if we should stop polling\n const shouldStop =\n stopStatusesRef.current.includes(result.status) &&\n !holdOpenRef.current?.(result);\n logRef.current.log('[usePolling] Should stop?', shouldStop, 'stopStatuses:', stopStatusesRef.current, 'current status:', result.status);\n if (shouldStop) {\n logRef.current.log('[usePolling] Stopping polling due to status:', result.status);\n clearPolling();\n setIsPolling(false);\n onStopRef.current?.(result);\n }\n } catch (err) {\n logRef.current.error('[usePolling] Fetch error:', err);\n if (!isMountedRef.current) return;\n\n const error = err instanceof Error ? err : new Error(String(err));\n setError(error);\n onErrorRef.current?.(error);\n\n // Stop polling on error\n clearPolling();\n setIsPolling(false);\n }\n }, [clearPolling]);\n\n useEffect(() => {\n if (!enabled || !chatUid || !isPolling || !streamFnRef.current) return;\n const controller = new AbortController();\n lastStreamAt.current = 0;\n const leave = () => {\n streamAliveRef.current = false;\n streamAttemptRef.current = null;\n lastStreamAt.current = 0;\n };\n const connect = async () => {\n while (!controller.signal.aborted && isPollingRef.current) {\n // One controller per attempt, so the silence watchdog can drop a dead\n // connection without ending the loop.\n const attempt = new AbortController();\n const abortAttempt = () => attempt.abort();\n controller.signal.addEventListener('abort', abortAttempt);\n streamAttemptRef.current = attempt;\n streamAliveRef.current = true;\n lastStreamActivity.current = Date.now();\n let failed = false;\n try {\n await streamFnRef.current!(\n async snapshot => {\n if (attempt.signal.aborted || !isPollingRef.current) return;\n lastStreamAt.current = Date.now();\n lastStreamActivity.current = lastStreamAt.current;\n await fetchData(snapshot);\n },\n attempt.signal,\n () => { lastStreamActivity.current = Date.now(); },\n );\n } catch {\n // Our own abort is a reconnect; anything else means the stream is\n // not available here, and the poll is the recovery path.\n failed = !attempt.signal.aborted;\n } finally {\n controller.signal.removeEventListener('abort', abortAttempt);\n }\n leave();\n if (failed || controller.signal.aborted) break;\n // The server closes a stream after its lifetime; reopen right away.\n await new Promise(resolve => setTimeout(resolve, STREAM_RECONNECT_DELAY_MS));\n }\n leave();\n };\n void connect();\n return () => { controller.abort(); leave(); };\n }, [enabled, chatUid, isPolling, fetchData]);\n\n const start = useCallback(() => {\n if (intervalRef.current) return;\n\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch — unless a stream is about to open, whose first\n // frame is that same state.\n if (!streamFnRef.current) fetchData();\n\n // Set up interval\n armedIntervalRef.current = intervalValueRef.current;\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n }, [fetchData]);\n\n const stop = useCallback(() => {\n clearPolling();\n setIsPolling(false);\n }, [clearPolling]);\n\n const refetch = useCallback(async () => {\n await fetchData(undefined, true);\n }, [fetchData]);\n\n // Auto-start polling when enabled and chatUid is set\n useEffect(() => {\n logRef.current.log('[usePolling] Auto-start effect triggered:', { enabled, chatUid, isPollingRef: isPollingRef.current, intervalRef: !!intervalRef.current });\n\n if (!enabled || !chatUid) {\n logRef.current.log('[usePolling] Not enabled or no chatUid, stopping if active');\n // Stop polling if disabled or no chatUid\n if (isPollingRef.current) {\n clearPolling();\n setIsPolling(false);\n }\n return;\n }\n\n // Start polling if not already polling\n if (!isPollingRef.current) {\n logRef.current.log('[usePolling] Starting polling, interval:', intervalValueRef.current);\n isPollingRef.current = true;\n setIsPolling(true);\n setError(null);\n\n // Immediate first fetch — unless a stream is about to open, whose first\n // frame is that same state.\n if (!streamFnRef.current) fetchData();\n\n // Set up interval\n armedIntervalRef.current = intervalValueRef.current;\n intervalRef.current = setInterval(fetchData, intervalValueRef.current);\n logRef.current.log('[usePolling] Interval set:', intervalRef.current);\n } else {\n logRef.current.log('[usePolling] Already polling, skipping start');\n }\n\n // Only cleanup on unmount, not on every dependency change\n }, [enabled, chatUid, fetchData, clearPolling]);\n\n // Re-arm the timer when the cadence changes while a conversation is already\n // in flight. Without this a new `interval` would only take effect on the next\n // conversation, which is not what changing a provider setting looks like.\n useEffect(() => {\n if (!isPollingRef.current || armedIntervalRef.current === null) return;\n if (armedIntervalRef.current === interval) return;\n\n logRef.current.log('[usePolling] Interval changed mid-flight:', {\n from: armedIntervalRef.current,\n to: interval,\n });\n if (intervalRef.current) clearInterval(intervalRef.current);\n armedIntervalRef.current = interval;\n intervalRef.current = setInterval(fetchData, interval);\n }, [interval, fetchData]);\n\n // Cleanup on unmount\n useEffect(() => {\n isMountedRef.current = true;\n return () => {\n isMountedRef.current = false;\n clearPolling();\n };\n }, [clearPolling]);\n\n return {\n data,\n isPolling,\n error,\n start,\n stop,\n refetch,\n };\n}\n"],"names":["useMemo","createLogger","useRef","useState","useEffect","useCallback"],"mappings":";;;;;AAIA;;AAEG;AACI,MAAM,2BAA2B,GAAG;AAE3C;;;;AAIG;AACI,MAAM,uBAAuB,GAAG;AAEvC;;;;AAIG;AACI,MAAM,yBAAyB,GAAG;AAEzC;AACO,MAAM,yBAAyB,GAAG;AAEzC;;;;;AAKG;AACG,SAAU,sBAAsB,CACpC,GAAG,UAA4C,EAAA;AAE/C,IAAA,KAAK,MAAM,KAAK,IAAI,UAAU,EAAE;AAC9B,QAAA,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE;YACpE,OAAO,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,uBAAuB,CAAC;QACjD;IACF;AACA,IAAA,OAAO,2BAA2B;AACpC;AAEA;;;;AAIG;AACI,MAAM,iBAAiB,GAAG;AAEjC;;;;AAIG;AACG,SAAU,gBAAgB,CAC9B,GAA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F,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;;;;IAK/CA,eAAS,CAAC,MAAK;QACb,IAAI,CAAC,YAAY,CAAC,OAAO,IAAI,gBAAgB,CAAC,OAAO,KAAK,IAAI;YAAE;AAChE,QAAA,IAAI,gBAAgB,CAAC,OAAO,KAAK,QAAQ;YAAE;AAE3C,QAAA,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,2CAA2C,EAAE;YAC9D,IAAI,EAAE,gBAAgB,CAAC,OAAO;AAC9B,YAAA,EAAE,EAAE,QAAQ;AACb,SAAA,CAAC;QACF,IAAI,WAAW,CAAC,OAAO;AAAE,YAAA,aAAa,CAAC,WAAW,CAAC,OAAO,CAAC;AAC3D,QAAA,gBAAgB,CAAC,OAAO,GAAG,QAAQ;QACnC,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,SAAS,EAAE,QAAQ,CAAC;AACxD,IAAA,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC;;IAGzBA,eAAS,CAAC,MAAK;AACb,QAAA,YAAY,CAAC,OAAO,GAAG,IAAI;AAC3B,QAAA,OAAO,MAAK;AACV,YAAA,YAAY,CAAC,OAAO,GAAG,KAAK;AAC5B,YAAA,YAAY,EAAE;AAChB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,YAAY,CAAC,CAAC;IAElB,OAAO;QACL,IAAI;QACJ,SAAS;QACT,KAAK;QACL,KAAK;QACL,IAAI;QACJ,OAAO;KACR;AACH;;;;;;;;;;;"}
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var React = require('react');
4
+ var useTranslations = require('../i18n/useTranslations.js');
4
5
 
5
6
  const PREFERRED_MIME_TYPES = [
6
7
  'audio/webm;codecs=opus',
@@ -29,6 +30,7 @@ function pickMimeType(preferred) {
29
30
  */
30
31
  function useSpeechRecording(options = {}) {
31
32
  const { bars = 5, mimeType, autoStop = true, autoStopSilenceMs = 1000, autoStopCountdownMs = 1000, autoStopSilenceRatio = 0.1, autoStopSilenceLevel = 0.02, autoStopSpeechLevel = 0.12, onAutoStop, } = options;
33
+ const t = useTranslations.useTranslations();
32
34
  const isSupported = typeof navigator !== 'undefined' &&
33
35
  !!navigator.mediaDevices?.getUserMedia &&
34
36
  typeof MediaRecorder !== 'undefined';
@@ -196,7 +198,7 @@ function useSpeechRecording(options = {}) {
196
198
  }, [bars, evaluateAutoStop]);
197
199
  const start = React.useCallback(async () => {
198
200
  if (!isSupported) {
199
- setError('Audio recording is not supported in this browser');
201
+ setError(t('Audio recording is not supported in this browser'));
200
202
  return;
201
203
  }
202
204
  if (mediaRecorderRef.current)
@@ -255,12 +257,14 @@ function useSpeechRecording(options = {}) {
255
257
  cleanupAudioGraph();
256
258
  mediaRecorderRef.current = null;
257
259
  const message = e?.name === 'NotAllowedError'
258
- ? 'Microphone permission denied'
259
- : `Could not start recording: ${e?.message || 'unknown error'}`;
260
+ ? t('Microphone permission denied')
261
+ : t('Could not start recording: {error}', {
262
+ error: e?.message || t('unknown error'),
263
+ });
260
264
  setError(message);
261
265
  setStatus('idle');
262
266
  }
263
- }, [isSupported, mimeType, bars, tick, cleanupAudioGraph, resetAutoStop]);
267
+ }, [isSupported, mimeType, bars, tick, cleanupAudioGraph, resetAutoStop, t]);
264
268
  const pause = React.useCallback(() => {
265
269
  const recorder = mediaRecorderRef.current;
266
270
  if (recorder && recorder.state === 'recording') {
@@ -1 +1 @@
1
- {"version":3,"file":"useSpeechRecording.js","sources":["../../../../src/hooks/useSpeechRecording.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\n/**\n * Recording lifecycle state.\n * - `idle`: not recording\n * - `recording`: actively capturing audio\n * - `paused`: capture paused, can be resumed\n */\nexport type SpeechRecordingStatus = 'idle' | 'recording' | 'paused';\n\nexport interface UseSpeechRecordingOptions {\n /** Number of equalizer bars to expose in `levels`. @default 5 */\n bars?: number;\n /** Preferred MediaRecorder mime type. Falls back to a supported one. */\n mimeType?: string;\n /**\n * Auto-stop the recording after a short silence, but only once speech has\n * actually been detected. When it triggers, the countdown runs and then\n * `onAutoStop` is fired. Disable to require a manual confirm. @default true\n */\n autoStop?: boolean;\n /**\n * Continuous silence (ms) that must elapse — after speech was detected —\n * before the auto-stop countdown begins. @default 1000\n */\n autoStopSilenceMs?: number;\n /** Duration (ms) of the auto-stop countdown shown before firing. @default 1000 */\n autoStopCountdownMs?: number;\n /**\n * Silence is **adaptive**: the threshold is this fraction of the loudest\n * speech observed in the recording (e.g. 0.1 = 10% of peak voice). This makes\n * it robust to ambient noise — a quiet room and a loud one calibrate\n * differently. @default 0.1\n */\n autoStopSilenceRatio?: number;\n /**\n * Absolute floor (0..1) for the adaptive silence threshold, so it never drops\n * so low that background hiss reads as \"sound\". @default 0.02\n */\n autoStopSilenceLevel?: number;\n /**\n * Absolute floor (0..1) a peak must clear to first count as speech (arms the\n * detector / calibrates the reference loudness). @default 0.12\n */\n autoStopSpeechLevel?: number;\n /**\n * Fired when the silence countdown completes. The consumer decides what\n * \"stop\" means (e.g. confirm + transcribe). Read fresh on every frame, so it\n * does not need to be memoized.\n */\n onAutoStop?: () => void;\n}\n\nexport interface UseSpeechRecordingResult {\n status: SpeechRecordingStatus;\n isRecording: boolean;\n isPaused: boolean;\n /** Whether the browser supports audio recording. */\n isSupported: boolean;\n /** Normalized amplitude per bar (0..1), updated in real time for the equalizer. */\n levels: number[];\n /** Elapsed recording time in milliseconds (excludes paused time). */\n durationMs: number;\n /** Last error message, if any (e.g. permission denied). */\n error: string | null;\n /** True while the silence-triggered auto-stop countdown is running. */\n isAutoStopping: boolean;\n /** Auto-stop countdown progress, 1 (full) → 0 (empty), for an inverted ring. */\n autoStopProgress: number;\n /** True once a real voice peak was detected in the current recording. */\n speechDetected: boolean;\n /** Request mic access and start recording. */\n start: () => Promise<void>;\n /** Pause an active recording. */\n pause: () => void;\n /** Resume a paused recording. */\n resume: () => void;\n /** Stop and return the recorded audio as a Blob (null if nothing captured). */\n stop: () => Promise<Blob | null>;\n /** Stop and discard the recording without producing a Blob. */\n cancel: () => void;\n}\n\nconst PREFERRED_MIME_TYPES = [\n 'audio/webm;codecs=opus',\n 'audio/webm',\n 'audio/ogg;codecs=opus',\n 'audio/mp4',\n];\n\nfunction pickMimeType(preferred?: string): string | undefined {\n if (typeof MediaRecorder === 'undefined') return undefined;\n const candidates = preferred\n ? [preferred, ...PREFERRED_MIME_TYPES]\n : PREFERRED_MIME_TYPES;\n for (const type of candidates) {\n if (MediaRecorder.isTypeSupported(type)) return type;\n }\n return undefined;\n}\n\n/**\n * Encapsulates microphone capture via MediaRecorder plus a Web Audio analyser\n * that exposes live amplitude levels for an equalizer-style animation.\n *\n * The hook owns all teardown: stopping tracks, closing the AudioContext and\n * cancelling the animation frame on stop/cancel/unmount.\n */\nexport function useSpeechRecording(\n options: UseSpeechRecordingOptions = {},\n): UseSpeechRecordingResult {\n const {\n bars = 5,\n mimeType,\n autoStop = true,\n autoStopSilenceMs = 1000,\n autoStopCountdownMs = 1000,\n autoStopSilenceRatio = 0.1,\n autoStopSilenceLevel = 0.02,\n autoStopSpeechLevel = 0.12,\n onAutoStop,\n } = options;\n\n const isSupported =\n typeof navigator !== 'undefined' &&\n !!navigator.mediaDevices?.getUserMedia &&\n typeof MediaRecorder !== 'undefined';\n\n const [status, setStatus] = useState<SpeechRecordingStatus>('idle');\n const [levels, setLevels] = useState<number[]>(() => new Array(bars).fill(0));\n const [durationMs, setDurationMs] = useState(0);\n const [error, setError] = useState<string | null>(null);\n const [isAutoStopping, setIsAutoStopping] = useState(false);\n const [autoStopProgress, setAutoStopProgress] = useState(1);\n const [speechDetected, setSpeechDetected] = useState(false);\n\n const mediaRecorderRef = useRef<MediaRecorder | null>(null);\n const streamRef = useRef<MediaStream | null>(null);\n const chunksRef = useRef<Blob[]>([]);\n const audioContextRef = useRef<AudioContext | null>(null);\n const analyserRef = useRef<AnalyserNode | null>(null);\n const rafRef = useRef<number | null>(null);\n const startedAtRef = useRef<number>(0);\n const accumulatedRef = useRef<number>(0);\n const stopResolveRef = useRef<((blob: Blob | null) => void) | null>(null);\n const discardRef = useRef<boolean>(false);\n\n // --- Auto-stop (silence detection) state, all kept in refs so the rAF loop\n // reads it without forcing `tick` to re-subscribe. ---\n const hasSpeechRef = useRef(false); // user has spoken at least once\n const loudestRef = useRef(0); // loudest peak seen (reference for adaptive silence)\n const silenceStartRef = useRef<number | null>(null); // when current silence began\n const autoStopStartRef = useRef<number | null>(null); // when the countdown started\n const autoStopFiredRef = useRef(false); // guard against re-firing before stop\n // Latest-config ref so the loop always sees fresh thresholds and callback.\n const autoStopCfgRef = useRef({\n autoStop,\n autoStopSilenceMs,\n autoStopCountdownMs,\n autoStopSilenceRatio,\n autoStopSilenceLevel,\n autoStopSpeechLevel,\n onAutoStop,\n });\n autoStopCfgRef.current = {\n autoStop,\n autoStopSilenceMs,\n autoStopCountdownMs,\n autoStopSilenceRatio,\n autoStopSilenceLevel,\n autoStopSpeechLevel,\n onAutoStop,\n };\n\n // Reset all auto-stop tracking. `keepSpeech` preserves the \"user has spoken\"\n // flag across a pause/resume so it doesn't re-arm from scratch.\n const resetAutoStop = useCallback((keepSpeech = false) => {\n if (!keepSpeech) {\n hasSpeechRef.current = false;\n loudestRef.current = 0;\n setSpeechDetected(false);\n }\n silenceStartRef.current = null;\n autoStopStartRef.current = null;\n autoStopFiredRef.current = false;\n setIsAutoStopping(false);\n setAutoStopProgress(1);\n }, []);\n\n // Evaluate the current peak amplitude against the silence/speech thresholds\n // and drive the auto-stop countdown. Called once per animation frame.\n const evaluateAutoStop = useCallback((peak: number) => {\n const cfg = autoStopCfgRef.current;\n if (!cfg.autoStop || autoStopFiredRef.current) return;\n const now = Date.now();\n\n // 1) Arm only once a real voice peak (absolute floor) has been heard. This\n // also seeds the reference loudness so ambient noise alone can't arm it.\n if (!hasSpeechRef.current) {\n if (peak >= cfg.autoStopSpeechLevel) {\n hasSpeechRef.current = true;\n loudestRef.current = peak;\n setSpeechDetected(true);\n }\n return;\n }\n\n // 2) Track the loudest voice so the silence threshold scales with how loud\n // the user actually speaks (robust to ambient noise).\n if (peak > loudestRef.current) loudestRef.current = peak;\n\n // 3) Adaptive thresholds: silence = ratio of the loudest voice (with an\n // absolute floor); activity sits above it for hysteresis, so background\n // noise between the two doesn't keep cancelling the countdown.\n const silenceThreshold = Math.max(\n cfg.autoStopSilenceLevel,\n cfg.autoStopSilenceRatio * loudestRef.current,\n );\n const activityThreshold = silenceThreshold * 1.8;\n\n if (peak >= activityThreshold) {\n // Talking again: reset silence and cancel any pending countdown.\n silenceStartRef.current = null;\n if (autoStopStartRef.current !== null) {\n autoStopStartRef.current = null;\n setIsAutoStopping(false);\n setAutoStopProgress(1);\n }\n return;\n }\n\n if (peak <= silenceThreshold) {\n if (silenceStartRef.current === null) silenceStartRef.current = now;\n\n if (autoStopStartRef.current === null) {\n // Waiting out the silence window before the countdown begins.\n if (now - silenceStartRef.current >= cfg.autoStopSilenceMs) {\n autoStopStartRef.current = now;\n setIsAutoStopping(true);\n setAutoStopProgress(1);\n }\n } else {\n // Countdown running: drain the ring 1 → 0, then fire.\n const elapsed = now - autoStopStartRef.current;\n const progress = Math.max(0, 1 - elapsed / cfg.autoStopCountdownMs);\n setAutoStopProgress(progress);\n if (elapsed >= cfg.autoStopCountdownMs) {\n autoStopFiredRef.current = true;\n autoStopStartRef.current = null;\n silenceStartRef.current = null;\n setIsAutoStopping(false);\n setAutoStopProgress(1);\n cfg.onAutoStop?.();\n }\n }\n }\n // Hysteresis deadzone (silence < peak < activity): keep timers running.\n }, []);\n\n const cleanupAudioGraph = useCallback(() => {\n if (rafRef.current !== null) {\n cancelAnimationFrame(rafRef.current);\n rafRef.current = null;\n }\n if (audioContextRef.current) {\n audioContextRef.current.close().catch(() => undefined);\n audioContextRef.current = null;\n }\n analyserRef.current = null;\n if (streamRef.current) {\n streamRef.current.getTracks().forEach((t) => t.stop());\n streamRef.current = null;\n }\n }, []);\n\n // Drives both the equalizer levels and the duration counter.\n const tick = useCallback(() => {\n const analyser = analyserRef.current;\n if (analyser) {\n const data = new Uint8Array(analyser.frequencyBinCount);\n analyser.getByteFrequencyData(data);\n const bucketSize = Math.floor(data.length / bars) || 1;\n const next: number[] = [];\n let peak = 0;\n for (let i = 0; i < bars; i++) {\n let sum = 0;\n for (let j = 0; j < bucketSize; j++) {\n sum += data[i * bucketSize + j] ?? 0;\n }\n // Normalize 0..255 -> 0..1 with a small floor so bars stay visible.\n const level = Math.min(1, sum / bucketSize / 255);\n next.push(level);\n if (level > peak) peak = level;\n }\n setLevels(next);\n evaluateAutoStop(peak);\n }\n setDurationMs(accumulatedRef.current + (Date.now() - startedAtRef.current));\n rafRef.current = requestAnimationFrame(tick);\n }, [bars, evaluateAutoStop]);\n\n const start = useCallback(async () => {\n if (!isSupported) {\n setError('Audio recording is not supported in this browser');\n return;\n }\n if (mediaRecorderRef.current) return;\n\n setError(null);\n try {\n const stream = await navigator.mediaDevices.getUserMedia({ audio: true });\n streamRef.current = stream;\n\n const selectedType = pickMimeType(mimeType);\n const recorder = selectedType\n ? new MediaRecorder(stream, { mimeType: selectedType })\n : new MediaRecorder(stream);\n mediaRecorderRef.current = recorder;\n chunksRef.current = [];\n discardRef.current = false;\n resetAutoStop();\n\n recorder.ondataavailable = (e: BlobEvent) => {\n if (e.data && e.data.size > 0) chunksRef.current.push(e.data);\n };\n recorder.onstop = () => {\n const type = recorder.mimeType || selectedType || 'audio/webm';\n const blob = discardRef.current\n ? null\n : new Blob(chunksRef.current, { type });\n chunksRef.current = [];\n cleanupAudioGraph();\n mediaRecorderRef.current = null;\n setStatus('idle');\n setLevels(new Array(bars).fill(0));\n setDurationMs(0);\n accumulatedRef.current = 0;\n resetAutoStop();\n const resolve = stopResolveRef.current;\n stopResolveRef.current = null;\n resolve?.(blob);\n };\n\n // Web Audio analyser for the equalizer animation.\n const AudioCtx =\n window.AudioContext ||\n (window as unknown as { webkitAudioContext: typeof AudioContext })\n .webkitAudioContext;\n const audioContext = new AudioCtx();\n const source = audioContext.createMediaStreamSource(stream);\n const analyser = audioContext.createAnalyser();\n analyser.fftSize = 64;\n source.connect(analyser);\n audioContextRef.current = audioContext;\n analyserRef.current = analyser;\n\n accumulatedRef.current = 0;\n startedAtRef.current = Date.now();\n recorder.start(100);\n setStatus('recording');\n rafRef.current = requestAnimationFrame(tick);\n } catch (e) {\n cleanupAudioGraph();\n mediaRecorderRef.current = null;\n const message =\n (e as Error)?.name === 'NotAllowedError'\n ? 'Microphone permission denied'\n : `Could not start recording: ${(e as Error)?.message || 'unknown error'}`;\n setError(message);\n setStatus('idle');\n }\n }, [isSupported, mimeType, bars, tick, cleanupAudioGraph, resetAutoStop]);\n\n const pause = useCallback(() => {\n const recorder = mediaRecorderRef.current;\n if (recorder && recorder.state === 'recording') {\n recorder.pause();\n accumulatedRef.current += Date.now() - startedAtRef.current;\n if (rafRef.current !== null) {\n cancelAnimationFrame(rafRef.current);\n rafRef.current = null;\n }\n setLevels(new Array(bars).fill(0));\n // Drop any in-flight silence/countdown but remember that speech happened.\n resetAutoStop(true);\n setStatus('paused');\n }\n }, [bars, resetAutoStop]);\n\n const resume = useCallback(() => {\n const recorder = mediaRecorderRef.current;\n if (recorder && recorder.state === 'paused') {\n recorder.resume();\n startedAtRef.current = Date.now();\n // Fresh silence timing on resume so it doesn't fire immediately.\n resetAutoStop(true);\n rafRef.current = requestAnimationFrame(tick);\n setStatus('recording');\n }\n }, [tick, resetAutoStop]);\n\n const stop = useCallback((): Promise<Blob | null> => {\n const recorder = mediaRecorderRef.current;\n if (!recorder) return Promise.resolve(null);\n discardRef.current = false;\n return new Promise<Blob | null>((resolve) => {\n stopResolveRef.current = resolve;\n recorder.stop();\n });\n }, []);\n\n const cancel = useCallback(() => {\n const recorder = mediaRecorderRef.current;\n if (!recorder) {\n cleanupAudioGraph();\n setStatus('idle');\n setLevels(new Array(bars).fill(0));\n setDurationMs(0);\n resetAutoStop();\n return;\n }\n discardRef.current = true;\n recorder.stop();\n }, [bars, cleanupAudioGraph, resetAutoStop]);\n\n // Teardown on unmount.\n useEffect(() => {\n return () => {\n const recorder = mediaRecorderRef.current;\n if (recorder && recorder.state !== 'inactive') {\n discardRef.current = true;\n try {\n recorder.stop();\n } catch {\n // ignore\n }\n }\n cleanupAudioGraph();\n };\n }, [cleanupAudioGraph]);\n\n return {\n status,\n isRecording: status === 'recording',\n isPaused: status === 'paused',\n isSupported,\n levels,\n durationMs,\n error,\n isAutoStopping,\n autoStopProgress,\n speechDetected,\n start,\n pause,\n resume,\n stop,\n cancel,\n };\n}\n"],"names":["useState","useRef","useCallback","useEffect"],"mappings":";;;;AAmFA,MAAM,oBAAoB,GAAG;IAC3B,wBAAwB;IACxB,YAAY;IACZ,uBAAuB;IACvB,WAAW;CACZ;AAED,SAAS,YAAY,CAAC,SAAkB,EAAA;IACtC,IAAI,OAAO,aAAa,KAAK,WAAW;AAAE,QAAA,OAAO,SAAS;IAC1D,MAAM,UAAU,GAAG;AACjB,UAAE,CAAC,SAAS,EAAE,GAAG,oBAAoB;UACnC,oBAAoB;AACxB,IAAA,KAAK,MAAM,IAAI,IAAI,UAAU,EAAE;AAC7B,QAAA,IAAI,aAAa,CAAC,eAAe,CAAC,IAAI,CAAC;AAAE,YAAA,OAAO,IAAI;IACtD;AACA,IAAA,OAAO,SAAS;AAClB;AAEA;;;;;;AAMG;AACG,SAAU,kBAAkB,CAChC,OAAA,GAAqC,EAAE,EAAA;AAEvC,IAAA,MAAM,EACJ,IAAI,GAAG,CAAC,EACR,QAAQ,EACR,QAAQ,GAAG,IAAI,EACf,iBAAiB,GAAG,IAAI,EACxB,mBAAmB,GAAG,IAAI,EAC1B,oBAAoB,GAAG,GAAG,EAC1B,oBAAoB,GAAG,IAAI,EAC3B,mBAAmB,GAAG,IAAI,EAC1B,UAAU,GACX,GAAG,OAAO;AAEX,IAAA,MAAM,WAAW,GACf,OAAO,SAAS,KAAK,WAAW;AAChC,QAAA,CAAC,CAAC,SA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,gBAAA,UAAU,CAAC,OAAO,GAAG,IAAI;AACzB,gBAAA,IAAI;oBACF,QAAQ,CAAC,IAAI,EAAE;gBACjB;AAAE,gBAAA,MAAM;;gBAER;YACF;AACA,YAAA,iBAAiB,EAAE;AACrB,QAAA,CAAC;AACH,IAAA,CAAC,EAAE,CAAC,iBAAiB,CAAC,CAAC;IAEvB,OAAO;QACL,MAAM;QACN,WAAW,EAAE,MAAM,KAAK,WAAW;QACnC,QAAQ,EAAE,MAAM,KAAK,QAAQ;QAC7B,WAAW;QACX,MAAM;QACN,UAAU;QACV,KAAK;QACL,cAAc;QACd,gBAAgB;QAChB,cAAc;QACd,KAAK;QACL,KAAK;QACL,MAAM;QACN,IAAI;QACJ,MAAM;KACP;AACH;;"}
1
+ {"version":3,"file":"useSpeechRecording.js","sources":["../../../../src/hooks/useSpeechRecording.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\nimport { useTranslations } from '../i18n';\n\n/**\n * Recording lifecycle state.\n * - `idle`: not recording\n * - `recording`: actively capturing audio\n * - `paused`: capture paused, can be resumed\n */\nexport type SpeechRecordingStatus = 'idle' | 'recording' | 'paused';\n\nexport interface UseSpeechRecordingOptions {\n /** Number of equalizer bars to expose in `levels`. @default 5 */\n bars?: number;\n /** Preferred MediaRecorder mime type. Falls back to a supported one. */\n mimeType?: string;\n /**\n * Auto-stop the recording after a short silence, but only once speech has\n * actually been detected. When it triggers, the countdown runs and then\n * `onAutoStop` is fired. Disable to require a manual confirm. @default true\n */\n autoStop?: boolean;\n /**\n * Continuous silence (ms) that must elapse — after speech was detected —\n * before the auto-stop countdown begins. @default 1000\n */\n autoStopSilenceMs?: number;\n /** Duration (ms) of the auto-stop countdown shown before firing. @default 1000 */\n autoStopCountdownMs?: number;\n /**\n * Silence is **adaptive**: the threshold is this fraction of the loudest\n * speech observed in the recording (e.g. 0.1 = 10% of peak voice). This makes\n * it robust to ambient noise — a quiet room and a loud one calibrate\n * differently. @default 0.1\n */\n autoStopSilenceRatio?: number;\n /**\n * Absolute floor (0..1) for the adaptive silence threshold, so it never drops\n * so low that background hiss reads as \"sound\". @default 0.02\n */\n autoStopSilenceLevel?: number;\n /**\n * Absolute floor (0..1) a peak must clear to first count as speech (arms the\n * detector / calibrates the reference loudness). @default 0.12\n */\n autoStopSpeechLevel?: number;\n /**\n * Fired when the silence countdown completes. The consumer decides what\n * \"stop\" means (e.g. confirm + transcribe). Read fresh on every frame, so it\n * does not need to be memoized.\n */\n onAutoStop?: () => void;\n}\n\nexport interface UseSpeechRecordingResult {\n status: SpeechRecordingStatus;\n isRecording: boolean;\n isPaused: boolean;\n /** Whether the browser supports audio recording. */\n isSupported: boolean;\n /** Normalized amplitude per bar (0..1), updated in real time for the equalizer. */\n levels: number[];\n /** Elapsed recording time in milliseconds (excludes paused time). */\n durationMs: number;\n /** Last error message, if any (e.g. permission denied). */\n error: string | null;\n /** True while the silence-triggered auto-stop countdown is running. */\n isAutoStopping: boolean;\n /** Auto-stop countdown progress, 1 (full) → 0 (empty), for an inverted ring. */\n autoStopProgress: number;\n /** True once a real voice peak was detected in the current recording. */\n speechDetected: boolean;\n /** Request mic access and start recording. */\n start: () => Promise<void>;\n /** Pause an active recording. */\n pause: () => void;\n /** Resume a paused recording. */\n resume: () => void;\n /** Stop and return the recorded audio as a Blob (null if nothing captured). */\n stop: () => Promise<Blob | null>;\n /** Stop and discard the recording without producing a Blob. */\n cancel: () => void;\n}\n\nconst PREFERRED_MIME_TYPES = [\n 'audio/webm;codecs=opus',\n 'audio/webm',\n 'audio/ogg;codecs=opus',\n 'audio/mp4',\n];\n\nfunction pickMimeType(preferred?: string): string | undefined {\n if (typeof MediaRecorder === 'undefined') return undefined;\n const candidates = preferred\n ? [preferred, ...PREFERRED_MIME_TYPES]\n : PREFERRED_MIME_TYPES;\n for (const type of candidates) {\n if (MediaRecorder.isTypeSupported(type)) return type;\n }\n return undefined;\n}\n\n/**\n * Encapsulates microphone capture via MediaRecorder plus a Web Audio analyser\n * that exposes live amplitude levels for an equalizer-style animation.\n *\n * The hook owns all teardown: stopping tracks, closing the AudioContext and\n * cancelling the animation frame on stop/cancel/unmount.\n */\nexport function useSpeechRecording(\n options: UseSpeechRecordingOptions = {},\n): UseSpeechRecordingResult {\n const {\n bars = 5,\n mimeType,\n autoStop = true,\n autoStopSilenceMs = 1000,\n autoStopCountdownMs = 1000,\n autoStopSilenceRatio = 0.1,\n autoStopSilenceLevel = 0.02,\n autoStopSpeechLevel = 0.12,\n onAutoStop,\n } = options;\n\n const t = useTranslations();\n\n const isSupported =\n typeof navigator !== 'undefined' &&\n !!navigator.mediaDevices?.getUserMedia &&\n typeof MediaRecorder !== 'undefined';\n\n const [status, setStatus] = useState<SpeechRecordingStatus>('idle');\n const [levels, setLevels] = useState<number[]>(() => new Array(bars).fill(0));\n const [durationMs, setDurationMs] = useState(0);\n const [error, setError] = useState<string | null>(null);\n const [isAutoStopping, setIsAutoStopping] = useState(false);\n const [autoStopProgress, setAutoStopProgress] = useState(1);\n const [speechDetected, setSpeechDetected] = useState(false);\n\n const mediaRecorderRef = useRef<MediaRecorder | null>(null);\n const streamRef = useRef<MediaStream | null>(null);\n const chunksRef = useRef<Blob[]>([]);\n const audioContextRef = useRef<AudioContext | null>(null);\n const analyserRef = useRef<AnalyserNode | null>(null);\n const rafRef = useRef<number | null>(null);\n const startedAtRef = useRef<number>(0);\n const accumulatedRef = useRef<number>(0);\n const stopResolveRef = useRef<((blob: Blob | null) => void) | null>(null);\n const discardRef = useRef<boolean>(false);\n\n // --- Auto-stop (silence detection) state, all kept in refs so the rAF loop\n // reads it without forcing `tick` to re-subscribe. ---\n const hasSpeechRef = useRef(false); // user has spoken at least once\n const loudestRef = useRef(0); // loudest peak seen (reference for adaptive silence)\n const silenceStartRef = useRef<number | null>(null); // when current silence began\n const autoStopStartRef = useRef<number | null>(null); // when the countdown started\n const autoStopFiredRef = useRef(false); // guard against re-firing before stop\n // Latest-config ref so the loop always sees fresh thresholds and callback.\n const autoStopCfgRef = useRef({\n autoStop,\n autoStopSilenceMs,\n autoStopCountdownMs,\n autoStopSilenceRatio,\n autoStopSilenceLevel,\n autoStopSpeechLevel,\n onAutoStop,\n });\n autoStopCfgRef.current = {\n autoStop,\n autoStopSilenceMs,\n autoStopCountdownMs,\n autoStopSilenceRatio,\n autoStopSilenceLevel,\n autoStopSpeechLevel,\n onAutoStop,\n };\n\n // Reset all auto-stop tracking. `keepSpeech` preserves the \"user has spoken\"\n // flag across a pause/resume so it doesn't re-arm from scratch.\n const resetAutoStop = useCallback((keepSpeech = false) => {\n if (!keepSpeech) {\n hasSpeechRef.current = false;\n loudestRef.current = 0;\n setSpeechDetected(false);\n }\n silenceStartRef.current = null;\n autoStopStartRef.current = null;\n autoStopFiredRef.current = false;\n setIsAutoStopping(false);\n setAutoStopProgress(1);\n }, []);\n\n // Evaluate the current peak amplitude against the silence/speech thresholds\n // and drive the auto-stop countdown. Called once per animation frame.\n const evaluateAutoStop = useCallback((peak: number) => {\n const cfg = autoStopCfgRef.current;\n if (!cfg.autoStop || autoStopFiredRef.current) return;\n const now = Date.now();\n\n // 1) Arm only once a real voice peak (absolute floor) has been heard. This\n // also seeds the reference loudness so ambient noise alone can't arm it.\n if (!hasSpeechRef.current) {\n if (peak >= cfg.autoStopSpeechLevel) {\n hasSpeechRef.current = true;\n loudestRef.current = peak;\n setSpeechDetected(true);\n }\n return;\n }\n\n // 2) Track the loudest voice so the silence threshold scales with how loud\n // the user actually speaks (robust to ambient noise).\n if (peak > loudestRef.current) loudestRef.current = peak;\n\n // 3) Adaptive thresholds: silence = ratio of the loudest voice (with an\n // absolute floor); activity sits above it for hysteresis, so background\n // noise between the two doesn't keep cancelling the countdown.\n const silenceThreshold = Math.max(\n cfg.autoStopSilenceLevel,\n cfg.autoStopSilenceRatio * loudestRef.current,\n );\n const activityThreshold = silenceThreshold * 1.8;\n\n if (peak >= activityThreshold) {\n // Talking again: reset silence and cancel any pending countdown.\n silenceStartRef.current = null;\n if (autoStopStartRef.current !== null) {\n autoStopStartRef.current = null;\n setIsAutoStopping(false);\n setAutoStopProgress(1);\n }\n return;\n }\n\n if (peak <= silenceThreshold) {\n if (silenceStartRef.current === null) silenceStartRef.current = now;\n\n if (autoStopStartRef.current === null) {\n // Waiting out the silence window before the countdown begins.\n if (now - silenceStartRef.current >= cfg.autoStopSilenceMs) {\n autoStopStartRef.current = now;\n setIsAutoStopping(true);\n setAutoStopProgress(1);\n }\n } else {\n // Countdown running: drain the ring 1 → 0, then fire.\n const elapsed = now - autoStopStartRef.current;\n const progress = Math.max(0, 1 - elapsed / cfg.autoStopCountdownMs);\n setAutoStopProgress(progress);\n if (elapsed >= cfg.autoStopCountdownMs) {\n autoStopFiredRef.current = true;\n autoStopStartRef.current = null;\n silenceStartRef.current = null;\n setIsAutoStopping(false);\n setAutoStopProgress(1);\n cfg.onAutoStop?.();\n }\n }\n }\n // Hysteresis deadzone (silence < peak < activity): keep timers running.\n }, []);\n\n const cleanupAudioGraph = useCallback(() => {\n if (rafRef.current !== null) {\n cancelAnimationFrame(rafRef.current);\n rafRef.current = null;\n }\n if (audioContextRef.current) {\n audioContextRef.current.close().catch(() => undefined);\n audioContextRef.current = null;\n }\n analyserRef.current = null;\n if (streamRef.current) {\n streamRef.current.getTracks().forEach((t) => t.stop());\n streamRef.current = null;\n }\n }, []);\n\n // Drives both the equalizer levels and the duration counter.\n const tick = useCallback(() => {\n const analyser = analyserRef.current;\n if (analyser) {\n const data = new Uint8Array(analyser.frequencyBinCount);\n analyser.getByteFrequencyData(data);\n const bucketSize = Math.floor(data.length / bars) || 1;\n const next: number[] = [];\n let peak = 0;\n for (let i = 0; i < bars; i++) {\n let sum = 0;\n for (let j = 0; j < bucketSize; j++) {\n sum += data[i * bucketSize + j] ?? 0;\n }\n // Normalize 0..255 -> 0..1 with a small floor so bars stay visible.\n const level = Math.min(1, sum / bucketSize / 255);\n next.push(level);\n if (level > peak) peak = level;\n }\n setLevels(next);\n evaluateAutoStop(peak);\n }\n setDurationMs(accumulatedRef.current + (Date.now() - startedAtRef.current));\n rafRef.current = requestAnimationFrame(tick);\n }, [bars, evaluateAutoStop]);\n\n const start = useCallback(async () => {\n if (!isSupported) {\n setError(t('Audio recording is not supported in this browser'));\n return;\n }\n if (mediaRecorderRef.current) return;\n\n setError(null);\n try {\n const stream = await navigator.mediaDevices.getUserMedia({ audio: true });\n streamRef.current = stream;\n\n const selectedType = pickMimeType(mimeType);\n const recorder = selectedType\n ? new MediaRecorder(stream, { mimeType: selectedType })\n : new MediaRecorder(stream);\n mediaRecorderRef.current = recorder;\n chunksRef.current = [];\n discardRef.current = false;\n resetAutoStop();\n\n recorder.ondataavailable = (e: BlobEvent) => {\n if (e.data && e.data.size > 0) chunksRef.current.push(e.data);\n };\n recorder.onstop = () => {\n const type = recorder.mimeType || selectedType || 'audio/webm';\n const blob = discardRef.current\n ? null\n : new Blob(chunksRef.current, { type });\n chunksRef.current = [];\n cleanupAudioGraph();\n mediaRecorderRef.current = null;\n setStatus('idle');\n setLevels(new Array(bars).fill(0));\n setDurationMs(0);\n accumulatedRef.current = 0;\n resetAutoStop();\n const resolve = stopResolveRef.current;\n stopResolveRef.current = null;\n resolve?.(blob);\n };\n\n // Web Audio analyser for the equalizer animation.\n const AudioCtx =\n window.AudioContext ||\n (window as unknown as { webkitAudioContext: typeof AudioContext })\n .webkitAudioContext;\n const audioContext = new AudioCtx();\n const source = audioContext.createMediaStreamSource(stream);\n const analyser = audioContext.createAnalyser();\n analyser.fftSize = 64;\n source.connect(analyser);\n audioContextRef.current = audioContext;\n analyserRef.current = analyser;\n\n accumulatedRef.current = 0;\n startedAtRef.current = Date.now();\n recorder.start(100);\n setStatus('recording');\n rafRef.current = requestAnimationFrame(tick);\n } catch (e) {\n cleanupAudioGraph();\n mediaRecorderRef.current = null;\n const message =\n (e as Error)?.name === 'NotAllowedError'\n ? t('Microphone permission denied')\n : t('Could not start recording: {error}', {\n error: (e as Error)?.message || t('unknown error'),\n });\n setError(message);\n setStatus('idle');\n }\n }, [isSupported, mimeType, bars, tick, cleanupAudioGraph, resetAutoStop, t]);\n\n const pause = useCallback(() => {\n const recorder = mediaRecorderRef.current;\n if (recorder && recorder.state === 'recording') {\n recorder.pause();\n accumulatedRef.current += Date.now() - startedAtRef.current;\n if (rafRef.current !== null) {\n cancelAnimationFrame(rafRef.current);\n rafRef.current = null;\n }\n setLevels(new Array(bars).fill(0));\n // Drop any in-flight silence/countdown but remember that speech happened.\n resetAutoStop(true);\n setStatus('paused');\n }\n }, [bars, resetAutoStop]);\n\n const resume = useCallback(() => {\n const recorder = mediaRecorderRef.current;\n if (recorder && recorder.state === 'paused') {\n recorder.resume();\n startedAtRef.current = Date.now();\n // Fresh silence timing on resume so it doesn't fire immediately.\n resetAutoStop(true);\n rafRef.current = requestAnimationFrame(tick);\n setStatus('recording');\n }\n }, [tick, resetAutoStop]);\n\n const stop = useCallback((): Promise<Blob | null> => {\n const recorder = mediaRecorderRef.current;\n if (!recorder) return Promise.resolve(null);\n discardRef.current = false;\n return new Promise<Blob | null>((resolve) => {\n stopResolveRef.current = resolve;\n recorder.stop();\n });\n }, []);\n\n const cancel = useCallback(() => {\n const recorder = mediaRecorderRef.current;\n if (!recorder) {\n cleanupAudioGraph();\n setStatus('idle');\n setLevels(new Array(bars).fill(0));\n setDurationMs(0);\n resetAutoStop();\n return;\n }\n discardRef.current = true;\n recorder.stop();\n }, [bars, cleanupAudioGraph, resetAutoStop]);\n\n // Teardown on unmount.\n useEffect(() => {\n return () => {\n const recorder = mediaRecorderRef.current;\n if (recorder && recorder.state !== 'inactive') {\n discardRef.current = true;\n try {\n recorder.stop();\n } catch {\n // ignore\n }\n }\n cleanupAudioGraph();\n };\n }, [cleanupAudioGraph]);\n\n return {\n status,\n isRecording: status === 'recording',\n isPaused: status === 'paused',\n isSupported,\n levels,\n durationMs,\n error,\n isAutoStopping,\n autoStopProgress,\n speechDetected,\n start,\n pause,\n resume,\n stop,\n cancel,\n };\n}\n"],"names":["useTranslations","useState","useRef","useCall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C,CAAC;IAEvB,OAAO;QACL,MAAM;QACN,WAAW,EAAE,MAAM,KAAK,WAAW;QACnC,QAAQ,EAAE,MAAM,KAAK,QAAQ;QAC7B,WAAW;QACX,MAAM;QACN,UAAU;QACV,KAAK;QACL,cAAc;QACd,gBAAgB;QAChB,cAAc;QACd,KAAK;QACL,KAAK;QACL,MAAM;QACN,IAAI;QACJ,MAAM;KACP;AACH;;;;"}
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Replace `{name}` placeholders. A placeholder with no matching value is
5
+ * dropped rather than left in the text — half-substituted braces on screen
6
+ * read as a bug to the end user.
7
+ */
8
+ function fillTemplate(template, vars) {
9
+ if (!vars)
10
+ return template;
11
+ return template.replace(/\{(\w+)\}/g, (_, name) => {
12
+ const value = vars[name];
13
+ return value === undefined || value === null ? '' : String(value);
14
+ });
15
+ }
16
+ /**
17
+ * Look one English text up in the dictionary, then fill its placeholders.
18
+ * An absent entry falls back to the English text itself, so a partial
19
+ * dictionary is perfectly usable.
20
+ */
21
+ function translate(dictionary, text, vars) {
22
+ return fillTemplate(dictionary?.[text] ?? text, vars);
23
+ }
24
+ /**
25
+ * Build a translator bound to one dictionary. Stable for as long as the
26
+ * dictionary is, so it is safe in a dependency list.
27
+ */
28
+ function createTranslator(dictionary) {
29
+ return (text, vars) => translate(dictionary, text, vars);
30
+ }
31
+ /**
32
+ * Merge dictionaries, later ones winning. `undefined` layers are skipped so
33
+ * callers can pass optional ones straight in.
34
+ */
35
+ function mergeTranslations(...layers) {
36
+ const present = layers.filter(Boolean);
37
+ if (present.length === 0)
38
+ return undefined;
39
+ if (present.length === 1)
40
+ return present[0];
41
+ return Object.assign({}, ...present);
42
+ }
43
+
44
+ exports.createTranslator = createTranslator;
45
+ exports.fillTemplate = fillTemplate;
46
+ exports.mergeTranslations = mergeTranslations;
47
+ exports.translate = translate;
48
+ //# sourceMappingURL=translate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"translate.js","sources":["../../../../src/i18n/translate.ts"],"sourcesContent":["import type { DevicTranslations, Translator } from './types';\n\n/**\n * Replace `{name}` placeholders. A placeholder with no matching value is\n * dropped rather than left in the text — half-substituted braces on screen\n * read as a bug to the end user.\n */\nexport function fillTemplate(\n template: string,\n vars?: Record<string, string | number>\n): string {\n if (!vars) return template;\n return template.replace(/\\{(\\w+)\\}/g, (_, name: string) => {\n const value = vars[name];\n return value === undefined || value === null ? '' : String(value);\n });\n}\n\n/**\n * Look one English text up in the dictionary, then fill its placeholders.\n * An absent entry falls back to the English text itself, so a partial\n * dictionary is perfectly usable.\n */\nexport function translate(\n dictionary: DevicTranslations | undefined,\n text: string,\n vars?: Record<string, string | number>\n): string {\n return fillTemplate(dictionary?.[text] ?? text, vars);\n}\n\n/**\n * Build a translator bound to one dictionary. Stable for as long as the\n * dictionary is, so it is safe in a dependency list.\n */\nexport function createTranslator(dictionary?: DevicTranslations): Translator {\n return (text, vars) => translate(dictionary, text, vars);\n}\n\n/**\n * Merge dictionaries, later ones winning. `undefined` layers are skipped so\n * callers can pass optional ones straight in.\n */\nexport function mergeTranslations(\n ...layers: (DevicTranslations | undefined)[]\n): DevicTranslations | undefined {\n const present = layers.filter(Boolean) as DevicTranslations[];\n if (present.length === 0) return undefined;\n if (present.length === 1) return present[0];\n return Object.assign({}, ...present) as DevicTranslations;\n}\n"],"names":[],"mappings":";;AAEA;;;;AAIG;AACG,SAAU,YAAY,CAC1B,QAAgB,EAChB,IAAsC,EAAA;AAEtC,IAAA,IAAI,CAAC,IAAI;AAAE,QAAA,OAAO,QAAQ;IAC1B,OAAO,QAAQ,CAAC,OAAO,CAAC,YAAY,EAAE,CAAC,CAAC,EAAE,IAAY,KAAI;AACxD,QAAA,MAAM,KAAK,GAAG,IAAI,CAAC,IAAI,CAAC;AACxB,QAAA,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,GAAG,EAAE,GAAG,MAAM,CAAC,KAAK,CAAC;AACnE,IAAA,CAAC,CAAC;AACJ;AAEA;;;;AAIG;SACa,SAAS,CACvB,UAAyC,EACzC,IAAY,EACZ,IAAsC,EAAA;AAEtC,IAAA,OAAO,YAAY,CAAC,UAAU,GAAG,IAAI,CAAC,IAAI,IAAI,EAAE,IAAI,CAAC;AACvD;AAEA;;;AAGG;AACG,SAAU,gBAAgB,CAAC,UAA8B,EAAA;AAC7D,IAAA,OAAO,CAAC,IAAI,EAAE,IAAI,KAAK,SAAS,CAAC,UAAU,EAAE,IAAI,EAAE,IAAI,CAAC;AAC1D;AAEA;;;AAGG;AACG,SAAU,iBAAiB,CAC/B,GAAG,MAAyC,EAAA;IAE5C,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAwB;AAC7D,IAAA,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;AAAE,QAAA,OAAO,SAAS;AAC1C,IAAA,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;AAAE,QAAA,OAAO,OAAO,CAAC,CAAC,CAAC;IAC3C,OAAO,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,OAAO,CAAsB;AAC3D;;;;;;;"}
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ var jsxRuntime = require('react/jsx-runtime');
4
+ var React = require('react');
5
+ var DevicContext = require('../provider/DevicContext.js');
6
+ var translate = require('./translate.js');
7
+
8
+ /**
9
+ * Dictionary contributed by an enclosing component, on top of the provider's.
10
+ *
11
+ * It exists so `<ChatDrawer options={{ translations }}>` reaches the input,
12
+ * the message list and the dialogs it opens without every one of them taking
13
+ * a prop for it. Nesting merges rather than replaces.
14
+ */
15
+ const TranslationsScope = React.createContext(undefined);
16
+ /**
17
+ * Scopes a dictionary to a subtree. Rarely needed directly — the components
18
+ * that take a `translations` option already wrap their own subtree with it —
19
+ * but useful to translate one region of a page differently.
20
+ */
21
+ function DevicTranslationsProvider({ translations, children, }) {
22
+ const outer = React.useContext(TranslationsScope);
23
+ const value = React.useMemo(() => translate.mergeTranslations(outer, translations), [outer, translations]);
24
+ return (jsxRuntime.jsx(TranslationsScope.Provider, { value: value, children: children }));
25
+ }
26
+ /**
27
+ * The dictionary in force here: the provider's, plus any scoped by an
28
+ * enclosing component, plus the `local` argument. Later layers win.
29
+ */
30
+ function useTranslationsDictionary(local) {
31
+ const context = DevicContext.useOptionalDevicContext();
32
+ const scoped = React.useContext(TranslationsScope);
33
+ const global = context?.translations;
34
+ return React.useMemo(() => translate.mergeTranslations(global, scoped, local), [global, scoped, local]);
35
+ }
36
+ /**
37
+ * Returns `t`, the translator for every text this library renders itself.
38
+ *
39
+ * Works with or without a `DevicProvider`: with no dictionary anywhere it
40
+ * returns the English text unchanged, which is what a component mounted
41
+ * standalone gets.
42
+ *
43
+ * @example
44
+ * ```tsx
45
+ * const t = useTranslations();
46
+ * <button aria-label={t('New chat')}>…</button>
47
+ * ```
48
+ */
49
+ function useTranslations(local) {
50
+ const dictionary = useTranslationsDictionary(local);
51
+ return React.useMemo(() => translate.createTranslator(dictionary), [dictionary]);
52
+ }
53
+
54
+ exports.DevicTranslationsProvider = DevicTranslationsProvider;
55
+ exports.useTranslations = useTranslations;
56
+ exports.useTranslationsDictionary = useTranslationsDictionary;
57
+ //# sourceMappingURL=useTranslations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useTranslations.js","sources":["../../../../src/i18n/useTranslations.tsx"],"sourcesContent":["import React, { createContext, useContext, useMemo } from 'react';\nimport { useOptionalDevicContext } from '../provider/DevicContext';\nimport { createTranslator, mergeTranslations } from './translate';\nimport type { DevicTranslations, Translator } from './types';\n\n/**\n * Dictionary contributed by an enclosing component, on top of the provider's.\n *\n * It exists so `<ChatDrawer options={{ translations }}>` reaches the input,\n * the message list and the dialogs it opens without every one of them taking\n * a prop for it. Nesting merges rather than replaces.\n */\nconst TranslationsScope = createContext<DevicTranslations | undefined>(undefined);\n\nexport interface DevicTranslationsProviderProps {\n /** Entries added on top of the enclosing dictionaries. */\n translations?: DevicTranslations;\n children: React.ReactNode;\n}\n\n/**\n * Scopes a dictionary to a subtree. Rarely needed directly — the components\n * that take a `translations` option already wrap their own subtree with it —\n * but useful to translate one region of a page differently.\n */\nexport function DevicTranslationsProvider({\n translations,\n children,\n}: DevicTranslationsProviderProps): JSX.Element {\n const outer = useContext(TranslationsScope);\n const value = useMemo(\n () => mergeTranslations(outer, translations),\n [outer, translations]\n );\n return (\n <TranslationsScope.Provider value={value}>\n {children}\n </TranslationsScope.Provider>\n );\n}\n\n/**\n * The dictionary in force here: the provider's, plus any scoped by an\n * enclosing component, plus the `local` argument. Later layers win.\n */\nexport function useTranslationsDictionary(\n local?: DevicTranslations\n): DevicTranslations | undefined {\n const context = useOptionalDevicContext();\n const scoped = useContext(TranslationsScope);\n const global = context?.translations;\n return useMemo(\n () => mergeTranslations(global, scoped, local),\n [global, scoped, local]\n );\n}\n\n/**\n * Returns `t`, the translator for every text this library renders itself.\n *\n * Works with or without a `DevicProvider`: with no dictionary anywhere it\n * returns the English text unchanged, which is what a component mounted\n * standalone gets.\n *\n * @example\n * ```tsx\n * const t = useTranslations();\n * <button aria-label={t('New chat')}>…</button>\n * ```\n */\nexport function useTranslations(local?: DevicTranslations): Translator {\n const dictionary = useTranslationsDictionary(local);\n return useMemo(() => createTranslator(dictionary), [dictionary]);\n}\n"],"names":["createContext","useContext","useMemo","mergeTranslations","_jsx","useOptionalDevicContext","createTranslator"],"mappings":";;;;;;;AAKA;;;;;;AAMG;AACH,MAAM,iBAAiB,GAAGA,mBAAa,CAAgC,SAAS,CAAC;AAQjF;;;;AAIG;SACa,yBAAyB,CAAC,EACxC,YAAY,EACZ,QAAQ,GACuB,EAAA;AAC/B,IAAA,MAAM,KAAK,GAAGC,gBAAU,CAAC,iBAAiB,CAAC;IAC3C,MAAM,KAAK,GAAGC,aAAO,CACnB,MAAMC,2BAAiB,CAAC,KAAK,EAAE,YAAY,CAAC,EAC5C,CAAC,KAAK,EAAE,YAAY,CAAC,CACtB;AACD,IAAA,QACEC,cAAA,CAAC,iBAAiB,CAAC,QAAQ,EAAA,EAAC,KAAK,EAAE,KAAK,EAAA,QAAA,EACrC,QAAQ,EAAA,CACkB;AAEjC;AAEA;;;AAGG;AACG,SAAU,yBAAyB,CACvC,KAAyB,EAAA;AAEzB,IAAA,MAAM,OAAO,GAAGC,oCAAuB,EAAE;AACzC,IAAA,MAAM,MAAM,GAAGJ,gBAAU,CAAC,iBAAiB,CAAC;AAC5C,IAAA,MAAM,MAAM,GAAG,OAAO,EAAE,YAAY;IACpC,OAAOC,aAAO,CACZ,MAAMC,2BAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,EAC9C,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,CAAC,CACxB;AACH;AAEA;;;;;;;;;;;;AAYG;AACG,SAAU,eAAe,CAAC,KAAyB,EAAA;AACvD,IAAA,MAAM,UAAU,GAAG,yBAAyB,CAAC,KAAK,CAAC;AACnD,IAAA,OAAOD,aAAO,CAAC,MAAMI,0BAAgB,CAAC,UAAU,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC;AAClE;;;;;;"}
package/dist/cjs/index.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  var DevicProvider = require('./provider/DevicProvider.js');
4
4
  var DevicContext = require('./provider/DevicContext.js');
5
+ var useTranslations = require('./i18n/useTranslations.js');
6
+ var translate = require('./i18n/translate.js');
5
7
  var ChatDrawer = require('./components/ChatDrawer/ChatDrawer.js');
6
8
  var ChatMessages = require('./components/ChatDrawer/ChatMessages.js');
7
9
  var ChatInput = require('./components/ChatDrawer/ChatInput.js');
@@ -14,6 +16,7 @@ var LimitBanner = require('./components/ChatDrawer/LimitBanner.js');
14
16
  var QueueNotice = require('./components/ChatDrawer/QueueNotice.js');
15
17
  var RecalledMemoriesWidget = require('./components/ChatDrawer/RecalledMemoriesWidget.js');
16
18
  var CompactionWidget = require('./components/ChatDrawer/CompactionWidget.js');
19
+ var GuardrailNotice = require('./components/ChatDrawer/GuardrailNotice.js');
17
20
  var CoreMemoryModal = require('./components/CoreMemoryModal/CoreMemoryModal.js');
18
21
  var IntegrationsModal = require('./components/IntegrationsModal/IntegrationsModal.js');
19
22
  var IntegrationsPanel = require('./components/IntegrationsModal/IntegrationsPanel.js');
@@ -54,6 +57,13 @@ exports.DevicProvider = DevicProvider.DevicProvider;
54
57
  exports.DevicContext = DevicContext.DevicContext;
55
58
  exports.useDevicContext = DevicContext.useDevicContext;
56
59
  exports.useOptionalDevicContext = DevicContext.useOptionalDevicContext;
60
+ exports.DevicTranslationsProvider = useTranslations.DevicTranslationsProvider;
61
+ exports.useTranslations = useTranslations.useTranslations;
62
+ exports.useTranslationsDictionary = useTranslations.useTranslationsDictionary;
63
+ exports.createTranslator = translate.createTranslator;
64
+ exports.fillTemplate = translate.fillTemplate;
65
+ exports.mergeTranslations = translate.mergeTranslations;
66
+ exports.translate = translate.translate;
57
67
  exports.ChatDrawer = ChatDrawer.ChatDrawer;
58
68
  exports.ChatMessages = ChatMessages.ChatMessages;
59
69
  exports.ChatInput = ChatInput.ChatInput;
@@ -66,6 +76,7 @@ exports.LimitBanner = LimitBanner.LimitBanner;
66
76
  exports.QueueNotice = QueueNotice.QueueNotice;
67
77
  exports.RecalledMemoriesWidget = RecalledMemoriesWidget.RecalledMemoriesWidget;
68
78
  exports.CompactionWidget = CompactionWidget.CompactionWidget;
79
+ exports.GuardrailNotice = GuardrailNotice.GuardrailNotice;
69
80
  exports.CoreMemoryModal = CoreMemoryModal.CoreMemoryModal;
70
81
  exports.DEFAULT_CORE_MEMORY_LABELS = CoreMemoryModal.DEFAULT_CORE_MEMORY_LABELS;
71
82
  exports.IntegrationsModal = IntegrationsModal.IntegrationsModal;
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}