@gemies/sdk 0.0.0-stage → 1.0.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 (242) hide show
  1. package/CHANGELOG.md +1662 -0
  2. package/LICENSE +202 -0
  3. package/NOTICE +15 -0
  4. package/README.md +516 -2
  5. package/package.json +102 -4
  6. package/src/animations.ts +5 -0
  7. package/src/ask-eager.ts +8 -0
  8. package/src/ask-text.ts +11 -0
  9. package/src/ask.ts +12 -0
  10. package/src/avatar.ts +13 -0
  11. package/src/character.ts +1 -0
  12. package/src/components/AmbientField.tsx +580 -0
  13. package/src/components/ComposerDropZone.tsx +235 -0
  14. package/src/components/ComposerSuggestions.tsx +45 -0
  15. package/src/components/GemieAskCore.tsx +184 -0
  16. package/src/components/GemieAskProvider.tsx +18 -0
  17. package/src/components/GemieAskProviderLazy.tsx +27 -0
  18. package/src/components/GemieAskProviderText.tsx +25 -0
  19. package/src/components/GemieAskSheet.tsx +51 -0
  20. package/src/components/GemieAskSheetWithVoice.tsx +10 -0
  21. package/src/components/GemieAvatar.tsx +57 -0
  22. package/src/components/GemieAvatarIcon.tsx +35 -0
  23. package/src/components/GemieChatContent.tsx +900 -0
  24. package/src/components/GemieChatHeader.tsx +123 -0
  25. package/src/components/GemieChatScreen.tsx +170 -0
  26. package/src/components/GemieChatSheet.tsx +368 -0
  27. package/src/components/GemieChatWithVoice.tsx +13 -0
  28. package/src/components/GemieComposer.tsx +388 -0
  29. package/src/components/GemieGroupWelcome.tsx +37 -0
  30. package/src/components/GemieParticipants.tsx +133 -0
  31. package/src/components/GemieParticipantsHeader.tsx +27 -0
  32. package/src/components/GemieRecentConversations.tsx +65 -0
  33. package/src/components/GemieSharedConversation.tsx +87 -0
  34. package/src/components/GemieWelcomeMessage.tsx +58 -0
  35. package/src/components/IdentityMark.tsx +225 -0
  36. package/src/components/Image.tsx +12 -0
  37. package/src/components/InertWhen.tsx +48 -0
  38. package/src/components/PlanPreviewCard.tsx +53 -0
  39. package/src/components/QuestionCard.tsx +64 -0
  40. package/src/components/ReplyMarkdown.tsx +373 -0
  41. package/src/components/RichBlocks.tsx +195 -0
  42. package/src/components/VoiceModeIcon.tsx +18 -0
  43. package/src/components/canvas/CanvasComponent.tsx +65 -0
  44. package/src/components/canvas/ChartRenderer.tsx +73 -0
  45. package/src/components/canvas/CodeRenderer.tsx +20 -0
  46. package/src/components/canvas/DashboardRenderer.tsx +70 -0
  47. package/src/components/canvas/DeckRenderer.tsx +43 -0
  48. package/src/components/canvas/FormRenderer.tsx +101 -0
  49. package/src/components/canvas/MarkdownRenderer.tsx +14 -0
  50. package/src/components/canvas/TableRenderer.tsx +47 -0
  51. package/src/components/icons/RiEmojiPicker.tsx +39 -0
  52. package/src/components/icons/RiMoreDots.tsx +32 -0
  53. package/src/components/icons/RiReplyLine.tsx +25 -0
  54. package/src/components/thought/FilesAndSources.tsx +120 -0
  55. package/src/components/thought/ThoughtPanel.tsx +426 -0
  56. package/src/components/thought/ToolStep.tsx +134 -0
  57. package/src/components/thread/AgentExchangeView.tsx +112 -0
  58. package/src/components/thread/AgentResultCard.tsx +125 -0
  59. package/src/components/thread/AgentTaskCard.tsx +127 -0
  60. package/src/components/thread/ChatBubble.tsx +256 -0
  61. package/src/components/thread/GemieThread.tsx +455 -0
  62. package/src/components/thread/MessageBlockBoundary.tsx +78 -0
  63. package/src/components/thread/NewConversationOffer.tsx +54 -0
  64. package/src/components/thread/ThreadRow.tsx +598 -0
  65. package/src/components/thread/ThreadSearch.tsx +130 -0
  66. package/src/components/thread/ThreadSkeleton.tsx +30 -0
  67. package/src/components/thread/ToolResultCard.tsx +254 -0
  68. package/src/components/thread/TurnMenu.tsx +54 -0
  69. package/src/components/thread/TurnStatusLine.tsx +34 -0
  70. package/src/components/voice/GemieVoiceCall.tsx +21 -0
  71. package/src/components/voice/GemieVoiceCall.web.tsx +17 -0
  72. package/src/components/voice/VoiceCallContext.ts +13 -0
  73. package/src/components/voice/VoiceControls.tsx +106 -0
  74. package/src/components/voice/VoiceSession.tsx +120 -0
  75. package/src/components/voice/withVoiceCall.tsx +25 -0
  76. package/src/csp.ts +50 -0
  77. package/src/dictation.ts +40 -0
  78. package/src/hooks/useAmbientWave.ts +184 -0
  79. package/src/hooks/useAtBottom.ts +51 -0
  80. package/src/hooks/useAttachMenu.ts +93 -0
  81. package/src/hooks/useAttachmentIntake.ts +401 -0
  82. package/src/hooks/useAudioLevelMonitor.ts +46 -0
  83. package/src/hooks/useAudioLevels.ts +51 -0
  84. package/src/hooks/useChatComposer.ts +309 -0
  85. package/src/hooks/useChatVoice.ts +129 -0
  86. package/src/hooks/useComposerSuggestions.ts +84 -0
  87. package/src/hooks/useDictation.ts +292 -0
  88. package/src/hooks/useDocumentPicker.ts +11 -0
  89. package/src/hooks/useFailedTurnToast.ts +43 -0
  90. package/src/hooks/useFocusWhenPresented.ts +41 -0
  91. package/src/hooks/useGemieActivity.ts +31 -0
  92. package/src/hooks/useImagePicker.ts +38 -0
  93. package/src/hooks/useMessageReactions.ts +119 -0
  94. package/src/hooks/useMessageVote.ts +50 -0
  95. package/src/hooks/useOpenThought.ts +79 -0
  96. package/src/hooks/usePendingQuestions.ts +19 -0
  97. package/src/hooks/useReadAloud.ts +92 -0
  98. package/src/hooks/useShareHandler.ts +24 -0
  99. package/src/hooks/useSoundEffects.ts +137 -0
  100. package/src/hooks/useTTS.ts +303 -0
  101. package/src/hooks/useThoughtScope.ts +59 -0
  102. package/src/hooks/useThreadRows.ts +157 -0
  103. package/src/hooks/useThreadView.ts +67 -0
  104. package/src/hooks/useTimelineWindow.ts +256 -0
  105. package/src/hooks/useVoiceLauncher.ts +180 -0
  106. package/src/hooks/useVoiceRoom.ts +212 -0
  107. package/src/index.ts +140 -0
  108. package/src/lib/activity-socket.ts +102 -0
  109. package/src/lib/activity-store.ts +53 -0
  110. package/src/lib/add-menu.ts +7 -0
  111. package/src/lib/agent-character.ts +28 -0
  112. package/src/lib/alia-character.ts +14 -0
  113. package/src/lib/attachment-intake.ts +154 -0
  114. package/src/lib/attachment-tiles.ts +74 -0
  115. package/src/lib/audio-level.ts +130 -0
  116. package/src/lib/audio-request-auth.ts +4 -0
  117. package/src/lib/audio-session-binding.ts +27 -0
  118. package/src/lib/bubbles.tsx +16 -0
  119. package/src/lib/call-thread.ts +33 -0
  120. package/src/lib/canvas-result.ts +21 -0
  121. package/src/lib/catalogue-tool.ts +22 -0
  122. package/src/lib/citations.ts +192 -0
  123. package/src/lib/clipboard.ts +23 -0
  124. package/src/lib/content-utils.ts +46 -0
  125. package/src/lib/copy-en.ts +291 -0
  126. package/src/lib/copy-es.ts +287 -0
  127. package/src/lib/copy.ts +38 -0
  128. package/src/lib/deferred/document-picker.ts +2 -0
  129. package/src/lib/deferred/document-picker.web.ts +12 -0
  130. package/src/lib/deferred/expo-audio.ts +2 -0
  131. package/src/lib/deferred/expo-audio.web.ts +12 -0
  132. package/src/lib/deferred/image-data-url.ts +5 -0
  133. package/src/lib/deferred/image-data-url.web.ts +12 -0
  134. package/src/lib/dictation-protocol.ts +142 -0
  135. package/src/lib/dictation-recorder-types.ts +37 -0
  136. package/src/lib/dictation-recorder.native.ts +90 -0
  137. package/src/lib/dictation-recorder.ts +131 -0
  138. package/src/lib/dictation-stream.ts +238 -0
  139. package/src/lib/dictation.ts +216 -0
  140. package/src/lib/document-picker.ts +31 -0
  141. package/src/lib/domain.ts +12 -0
  142. package/src/lib/flights.ts +33 -0
  143. package/src/lib/focus-return.ts +66 -0
  144. package/src/lib/greeting.ts +15 -0
  145. package/src/lib/image-data-url.ts +35 -0
  146. package/src/lib/keyboard.native.tsx +25 -0
  147. package/src/lib/keyboard.tsx +55 -0
  148. package/src/lib/labels-es.ts +113 -0
  149. package/src/lib/labels.tsx +207 -0
  150. package/src/lib/origins.ts +28 -0
  151. package/src/lib/participants.ts +97 -0
  152. package/src/lib/pcm.ts +57 -0
  153. package/src/lib/power-level-options.ts +66 -0
  154. package/src/lib/questions.ts +92 -0
  155. package/src/lib/speech-language.ts +20 -0
  156. package/src/lib/speech-messages.ts +54 -0
  157. package/src/lib/speech-synthesis.ts +156 -0
  158. package/src/lib/suggestions.ts +53 -0
  159. package/src/lib/task-labels.ts +35 -0
  160. package/src/lib/text-input.ts +10 -0
  161. package/src/lib/tool-call-status.ts +28 -0
  162. package/src/lib/tool-icons.ts +36 -0
  163. package/src/lib/tool-registry.ts +71 -0
  164. package/src/lib/tool-renderers.tsx +32 -0
  165. package/src/lib/turn-state.ts +15 -0
  166. package/src/lib/utils.ts +35 -0
  167. package/src/lib/voice-audio-engine-types.ts +59 -0
  168. package/src/lib/voice-audio-engine.native.ts +232 -0
  169. package/src/lib/voice-audio-engine.ts +206 -0
  170. package/src/lib/voice-call-request.ts +49 -0
  171. package/src/lib/voice-call.ts +627 -0
  172. package/src/lib/voice-controls-labels.ts +31 -0
  173. package/src/lib/voice-levels.ts +64 -0
  174. package/src/lib/voice-protocol.ts +328 -0
  175. package/src/lib/voice-worklets.ts +19 -0
  176. package/src/lib/work-log.ts +370 -0
  177. package/src/metro/chart-importers.json +18 -0
  178. package/src/metro/granular-charts.js +3 -0
  179. package/src/metro/index.cjs +227 -0
  180. package/src/metro/index.d.cts +5 -0
  181. package/src/runtime/agents.ts +151 -0
  182. package/src/runtime/api.ts +144 -0
  183. package/src/runtime/app-assertion.ts +51 -0
  184. package/src/runtime/attachment-utils.ts +97 -0
  185. package/src/runtime/chat-turn-control.ts +74 -0
  186. package/src/runtime/composer-draft-store.ts +270 -0
  187. package/src/runtime/config.tsx +222 -0
  188. package/src/runtime/conversation-dates.ts +31 -0
  189. package/src/runtime/conversation-store.ts +49 -0
  190. package/src/runtime/conversations.ts +633 -0
  191. package/src/runtime/error-utils.ts +144 -0
  192. package/src/runtime/host-tools.ts +290 -0
  193. package/src/runtime/index.ts +137 -0
  194. package/src/runtime/message-history.ts +124 -0
  195. package/src/runtime/message-reactions.ts +214 -0
  196. package/src/runtime/model.ts +39 -0
  197. package/src/runtime/oxy-files.ts +37 -0
  198. package/src/runtime/power-levels.ts +66 -0
  199. package/src/runtime/query-keys.ts +49 -0
  200. package/src/runtime/random-uuid.ts +25 -0
  201. package/src/runtime/reaction-cache.ts +45 -0
  202. package/src/runtime/room-events.ts +333 -0
  203. package/src/runtime/routes.ts +81 -0
  204. package/src/runtime/sharing.ts +217 -0
  205. package/src/runtime/sse-frame-reader.ts +88 -0
  206. package/src/runtime/stream-events.ts +154 -0
  207. package/src/runtime/stream-outcome.ts +21 -0
  208. package/src/runtime/thought-store.ts +103 -0
  209. package/src/runtime/thread/agent-activity.ts +261 -0
  210. package/src/runtime/thread/agent-exchange.ts +94 -0
  211. package/src/runtime/thread/automation-labels.ts +79 -0
  212. package/src/runtime/thread/conversation-export.ts +135 -0
  213. package/src/runtime/thread/message-days.ts +120 -0
  214. package/src/runtime/thread/message-processor.ts +93 -0
  215. package/src/runtime/thread/speaker-changes.ts +66 -0
  216. package/src/runtime/thread/task-utils.ts +11 -0
  217. package/src/runtime/thread/thought-utils.ts +750 -0
  218. package/src/runtime/thread/thread-history.ts +139 -0
  219. package/src/runtime/thread/timeline.ts +202 -0
  220. package/src/runtime/thread/title-tags.ts +8 -0
  221. package/src/runtime/thread/tool-cards.ts +387 -0
  222. package/src/runtime/turn-failure.ts +108 -0
  223. package/src/runtime/turn-preparation.ts +21 -0
  224. package/src/runtime/types.ts +172 -0
  225. package/src/runtime/usage-limit-error.ts +44 -0
  226. package/src/runtime/use-agent-activity.ts +80 -0
  227. package/src/runtime/use-conversation-stream.ts +1669 -0
  228. package/src/runtime/use-conversation-thread.ts +590 -0
  229. package/src/runtime/use-gemie-thread.ts +54 -0
  230. package/src/runtime/use-room-events.ts +75 -0
  231. package/src/runtime/use-start-conversation.ts +107 -0
  232. package/src/runtime/use-thread-history.ts +73 -0
  233. package/src/runtime/use-thread-search.ts +100 -0
  234. package/src/runtime/use-turn-edit.ts +99 -0
  235. package/src/text.ts +19 -0
  236. package/src/theme.ts +16 -0
  237. package/src/tools.ts +27 -0
  238. package/src/types.ts +161 -0
  239. package/src/voice-messages.ts +2 -0
  240. package/src/voice.ts +67 -0
  241. package/tsconfig.json +17 -0
  242. package/worklets/voice-processors.js +152 -0
package/src/ask.ts ADDED
@@ -0,0 +1,12 @@
1
+ /// <reference types="nativewind/types" />
2
+ // @gemies/sdk/ask — the app's one Alia sheet, without the chat in the entry bundle
3
+ // `GemieAskProvider` and `useAskGemie` alone: the sheet they open is loaded with the first `ask`.
4
+ // An app mounts the provider at its root from here rather than from the root entry, which
5
+ // would bring the whole chat along with it.
6
+
7
+ export { GemieAskProvider, useAskGemie } from './components/GemieAskProviderLazy';
8
+ export type {
9
+ GemieAsk,
10
+ GemieAskAgents,
11
+ GemieAskProviderProps,
12
+ } from './components/GemieAskProviderLazy';
package/src/avatar.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The avatar leaf entry: Alia's live avatar and what makes it react, without the chat.
3
+ *
4
+ * An app puts her on a button in its header (`GemieAvatarIcon`) and keeps the chat itself behind a
5
+ * lazy import, so the SDK's conversation UI is not in the entry bundle just to draw a face. This
6
+ * entry reaches the avatar, the activity store and its one socket, and nothing of the sheet, the
7
+ * composer, the markdown renderer or the voice call (`check:entries` holds that).
8
+ */
9
+ export { ALIA_AVATAR, GemieAvatar } from './components/GemieAvatar';
10
+ export type { GemieAvatarProps, GemieAvatarState } from './components/GemieAvatar';
11
+ export { GemieAvatarIcon } from './components/GemieAvatarIcon';
12
+ export { useGemieActivity } from './hooks/useGemieActivity';
13
+ export type { UseGemieActivityOptions } from './hooks/useGemieActivity';
@@ -0,0 +1 @@
1
+ export { DEFAULT_AGENT_CHARACTER, readAgentCharacter } from './lib/agent-character';
@@ -0,0 +1,580 @@
1
+ import { memo, useEffect, useRef, useState } from 'react';
2
+ import { AppState, Platform, View, type LayoutChangeEvent, type ViewStyle } from 'react-native';
3
+ import Animated, {
4
+ cancelAnimation,
5
+ Easing,
6
+ useAnimatedReaction,
7
+ useAnimatedStyle,
8
+ useSharedValue,
9
+ withDelay,
10
+ withRepeat,
11
+ withSequence,
12
+ withTiming,
13
+ type SharedValue,
14
+ } from 'react-native-reanimated';
15
+ import { usePrefersReducedMotion } from '@oxy.so/bloom/hooks';
16
+ import Svg, { Circle, Defs, RadialGradient, Rect, Stop } from 'react-native-svg';
17
+ import type { AgentState } from '../types';
18
+
19
+ /**
20
+ * Cross-platform blur. React Native 0.86 types `filter` on `ViewStyle`, but the
21
+ * two platforms take different shapes: a CSS string on web, the filter-function
22
+ * array on native.
23
+ */
24
+ function blurStyle(radius: number): ViewStyle {
25
+ return Platform.OS === 'web' ? { filter: `blur(${radius}px)` } : { filter: [{ blur: radius }] };
26
+ }
27
+
28
+ /** The blobs enter over 2.4s. */
29
+ const ENTER_DURATION = 2400;
30
+ const ENTER_EASE = Easing.bezier(0.22, 0.7, 0.24, 1);
31
+ /** The enter keyframes reach full opacity at 60% of the run. */
32
+ const ENTER_FADE_DURATION = ENTER_DURATION * 0.6;
33
+ /** Pointer parallax settle. */
34
+ const PARALLAX_DURATION = 550;
35
+ const PARALLAX_EASE = Easing.bezier(0.2, 0.7, 0.2, 1);
36
+ /** Per-blob opacity — dark mode needs less to read the same. */
37
+ const BLOB_OPACITY_LIGHT = 0.55;
38
+ const BLOB_OPACITY_DARK = 0.4;
39
+ /** One blur over the whole field, not per blob. */
40
+ const FIELD_BLUR = 70;
41
+ /** How long the intensity target takes to ramp, matching the voice overlay. */
42
+ const INTENSITY_RAMP = 500;
43
+ /** Exit: the field sinks and dims while whatever is behind it rises. */
44
+ const EXIT_OPACITY_DURATION = 800;
45
+ const EXIT_TRANSFORM_DURATION = 900;
46
+ const EXIT_EASE = Easing.bezier(0.2, 0.7, 0.2, 1);
47
+ /**
48
+ * A fast beat layered on top of the slow float, scaled by live amplitude.
49
+ *
50
+ * Removed once, for looking like a second canned animation, and put back: it is
51
+ * part of how the field reads while DICTATING, which is the behaviour that was
52
+ * asked for. Being scaled by the level is what makes it legitimate — at silence
53
+ * the whole term is zero and the blob does not move at all.
54
+ */
55
+ const PULSE_DURATION = 900;
56
+
57
+ /**
58
+ * How the field lays itself out in a container that is not a screen. The blobs are sized in
59
+ * `vmax` of the field and laid out across its width, which on a short, wide card (the chat's
60
+ * sheet) would make them half as big and pile them on the centre. The `sheet` layout draws
61
+ * them at the size of a phone screen (`reference`), spread wider across the width (`spread`)
62
+ * and shifted back toward the right (`shift`, a share of the width, since the spread leaves the
63
+ * composition leaning left), cropped by the card's edges: the blue, warm and green of the
64
+ * intro side by side. Asked for, not inferred from the height, so a short app screen is
65
+ * never reinterpreted.
66
+ */
67
+ const SHEET_LAYOUT = { reference: 800, spread: 1.4, shift: 0.17 } as const;
68
+
69
+ /** One keyframe stop of a float loop: [translateX in vw, translateY in vh, scale]. */
70
+ type FloatStop = readonly [number, number, number];
71
+
72
+ type FlourishConfig = {
73
+ /** Diameter, in vmax. */
74
+ w: number;
75
+ /** Where the blob enters from, in vw / vh. */
76
+ sx: number;
77
+ sy: number;
78
+ /** Where it comes to rest, in vw / vh. */
79
+ rx: number;
80
+ ry: number;
81
+ /** Entrance delay, in ms. */
82
+ delay: number;
83
+ /** Parallax reach at the pointer's extremes, in px. */
84
+ px: number;
85
+ py: number;
86
+ /** Float loop period, in ms. */
87
+ fdur: number;
88
+ /** How strongly this blob answers the live amplitude. */
89
+ amp: number;
90
+ /** The float keyframes at 33% and 66%; both ends of the loop are the origin. */
91
+ float: readonly [FloatStop, FloatStop];
92
+ };
93
+
94
+ /**
95
+ * The three blobs are centred and then pushed down by `ry` (48–57vh), which is
96
+ * what lays them along the bottom edge.
97
+ */
98
+ const FLOURISHES: readonly FlourishConfig[] = [
99
+ {
100
+ w: 51,
101
+ sx: -52,
102
+ sy: 70,
103
+ rx: -22,
104
+ ry: 50,
105
+ delay: 850,
106
+ px: 120,
107
+ py: 72,
108
+ fdur: 8000,
109
+ amp: 1,
110
+ float: [
111
+ [4, 2, 1.08],
112
+ [2, -3, 0.96],
113
+ ],
114
+ },
115
+ {
116
+ w: 47,
117
+ sx: 52,
118
+ sy: 72,
119
+ rx: 20,
120
+ ry: 48,
121
+ delay: 1030,
122
+ px: -144,
123
+ py: 88,
124
+ fdur: 11000,
125
+ amp: 1.2,
126
+ float: [
127
+ [-4, 3, 0.94],
128
+ [-2, -2, 1.06],
129
+ ],
130
+ },
131
+ {
132
+ w: 49,
133
+ sx: 12,
134
+ sy: 80,
135
+ rx: -2,
136
+ ry: 57,
137
+ delay: 1190,
138
+ px: 104,
139
+ py: -76,
140
+ fdur: 9500,
141
+ amp: 0.9,
142
+ float: [
143
+ [-3, -3, 1.06],
144
+ [2, 2, 0.95],
145
+ ],
146
+ },
147
+ ];
148
+
149
+ /**
150
+ * One hue per blob, per agent state. Idle and listening carry the field's own
151
+ * triad; thinking and speaking keep the amber and indigo the voice visualiser
152
+ * already established, so a call still reads as a call.
153
+ *
154
+ * These are literal hex values rather than theme tokens because the palette IS
155
+ * the effect — a single themed hue collapses into a flat wash.
156
+ */
157
+ const PALETTES: Record<AgentState, readonly [string, string, string]> = {
158
+ idle: ['#8b5ae0', '#b8c24a', '#f26a21'],
159
+ listening: ['#8b5ae0', '#b8c24a', '#f26a21'],
160
+ thinking: ['#92400e', '#eab308', '#fbbf24'],
161
+ speaking: ['#1e2870', '#6366f1', '#9333ea'],
162
+ };
163
+
164
+ /** The warm ellipse low in the frame that keeps the bottom edge from going cold. */
165
+ const REST_COLOR = '#c7a377';
166
+ const REST_OPACITY_LIGHT = 0.122;
167
+ const REST_OPACITY_DARK = 0.078;
168
+
169
+ /** Smoothstep, so each float segment eases in and out the way CSS does. */
170
+ function smooth(t: number) {
171
+ 'worklet';
172
+ return t * t * (3 - 2 * t);
173
+ }
174
+
175
+ /**
176
+ * One ambient blob, in three nested layers: an outer box that enters and comes
177
+ * to rest, a parallax layer that answers the pointer, and an inner circle that
178
+ * carries the endless float and the live amplitude. Keeping them nested means
179
+ * no transform has to know about the others.
180
+ */
181
+ function Flourish({
182
+ config,
183
+ index,
184
+ color,
185
+ vw,
186
+ vh,
187
+ vmax,
188
+ fieldWidth,
189
+ fieldHeight,
190
+ blobOpacity,
191
+ blend,
192
+ entrance,
193
+ running,
194
+ waveAmplitude,
195
+ pointerX,
196
+ pointerY,
197
+ }: {
198
+ config: FlourishConfig;
199
+ index: number;
200
+ color: string;
201
+ vw: number;
202
+ vh: number;
203
+ vmax: number;
204
+ fieldWidth: number;
205
+ fieldHeight: number;
206
+ blobOpacity: number;
207
+ blend: 'multiply' | 'screen';
208
+ entrance: boolean;
209
+ /** Whether the endless loops tick. See `paused` on {@link AmbientFieldProps}. */
210
+ running: boolean;
211
+ waveAmplitude: SharedValue<number>;
212
+ pointerX: SharedValue<number>;
213
+ pointerY: SharedValue<number>;
214
+ }) {
215
+ // Without an entrance the blob is simply already at rest.
216
+ const enter = useSharedValue(entrance ? 0 : 1);
217
+ const fade = useSharedValue(entrance ? 0 : 1);
218
+ const phase = useSharedValue(0);
219
+ const pulse = useSharedValue(0);
220
+
221
+ // Starting an animation on mount is imperative by nature: there is no
222
+ // derived-state or event-handler form of "run once, then loop forever".
223
+ useEffect(() => {
224
+ if (!entrance) return;
225
+ enter.value = withDelay(
226
+ config.delay,
227
+ withTiming(1, { duration: ENTER_DURATION, easing: ENTER_EASE }),
228
+ );
229
+ fade.value = withDelay(
230
+ config.delay,
231
+ withTiming(1, { duration: ENTER_FADE_DURATION, easing: ENTER_EASE }),
232
+ );
233
+ }, [enter, fade, entrance, config.delay]);
234
+
235
+ /**
236
+ * The two endless loops: started once, stopped while the field is not on
237
+ * show, and resumed where they stopped — the float runs out the rest of its
238
+ * lap and the beat the rest of its swing before looping again, so a field
239
+ * coming back into view does not jump. The entrance is finite and is left
240
+ * to finish.
241
+ */
242
+ const loopsStarted = useRef(false);
243
+ useEffect(() => {
244
+ if (!running) {
245
+ cancelAnimation(phase);
246
+ cancelAnimation(pulse);
247
+ return;
248
+ }
249
+ const lap = withRepeat(
250
+ withTiming(1, { duration: config.fdur, easing: Easing.linear }),
251
+ -1,
252
+ false,
253
+ );
254
+ const beat = (to: number) =>
255
+ withRepeat(
256
+ withTiming(to, { duration: PULSE_DURATION, easing: Easing.inOut(Easing.sin) }),
257
+ -1,
258
+ true,
259
+ );
260
+ if (!loopsStarted.current) {
261
+ loopsStarted.current = true;
262
+ phase.value = withDelay(entrance ? config.delay + ENTER_DURATION : 0, lap);
263
+ pulse.value = beat(1);
264
+ return;
265
+ }
266
+ const at = phase.value;
267
+ // 1 and 0 are the same pose (the origin), so the lap restarts seamlessly.
268
+ phase.value = withSequence(
269
+ withTiming(1, { duration: (1 - at) * config.fdur, easing: Easing.linear }),
270
+ withTiming(0, { duration: 0 }),
271
+ lap,
272
+ );
273
+ pulse.value = withSequence(
274
+ withTiming(1, {
275
+ duration: (1 - pulse.value) * PULSE_DURATION,
276
+ easing: Easing.inOut(Easing.sin),
277
+ }),
278
+ beat(0),
279
+ );
280
+ }, [running, phase, pulse, entrance, config.delay, config.fdur]);
281
+
282
+ const size = config.w * vmax;
283
+
284
+ const enterStyle = useAnimatedStyle(() => {
285
+ const t = enter.value;
286
+ const amp = waveAmplitude.value;
287
+ return {
288
+ // Live speech brightens the field on top of its resting opacity.
289
+ opacity: Math.min(1, fade.value * blobOpacity * (1 + amp * 0.5)),
290
+ transform: [
291
+ { translateX: (config.sx + (config.rx - config.sx) * t) * vw },
292
+ { translateY: (config.sy + (config.ry - config.sy) * t) * vh },
293
+ { scale: 0.55 + 0.45 * t },
294
+ ],
295
+ };
296
+ });
297
+
298
+ const parallaxStyle = useAnimatedStyle(() => ({
299
+ transform: [
300
+ { translateX: pointerX.value * config.px },
301
+ { translateY: pointerY.value * config.py },
302
+ ],
303
+ }));
304
+
305
+ const floatStyle = useAnimatedStyle(() => {
306
+ const [a, b] = config.float;
307
+ const p = phase.value;
308
+ // Four stops — origin, a, b, origin — each segment eased like ease-in-out.
309
+ let from: FloatStop = [0, 0, 1];
310
+ let to: FloatStop = a;
311
+ let local = p * 3;
312
+ if (p >= 2 / 3) {
313
+ from = b;
314
+ to = [0, 0, 1];
315
+ local = (p - 2 / 3) * 3;
316
+ } else if (p >= 1 / 3) {
317
+ from = a;
318
+ to = b;
319
+ local = (p - 1 / 3) * 3;
320
+ }
321
+ const e = smooth(local);
322
+ const amp = waveAmplitude.value;
323
+ // Amplitude swells the blob and adds a faster beat than the slow float.
324
+ const ampScale = 1 + amp * (0.3 * config.amp + 0.3 * (pulse.value - 0.5));
325
+ return {
326
+ transform: [
327
+ { translateX: (from[0] + (to[0] - from[0]) * e) * vw },
328
+ { translateY: (from[1] + (to[1] - from[1]) * e) * vh },
329
+ { scale: (from[2] + (to[2] - from[2]) * e) * ampScale },
330
+ ],
331
+ };
332
+ });
333
+
334
+ const gradientId = `ambient-flourish-${index}`;
335
+
336
+ return (
337
+ <Animated.View
338
+ style={[
339
+ {
340
+ position: 'absolute',
341
+ left: fieldWidth / 2 - size / 2,
342
+ top: fieldHeight / 2 - size / 2,
343
+ width: size,
344
+ height: size,
345
+ },
346
+ enterStyle,
347
+ ]}
348
+ >
349
+ <Animated.View style={[{ width: '100%', height: '100%' }, parallaxStyle]}>
350
+ <Animated.View
351
+ // Blending is what stops three overlapping blobs reading as grey mud.
352
+ style={[{ width: '100%', height: '100%', mixBlendMode: blend }, floatStyle]}
353
+ >
354
+ <Svg width={size} height={size}>
355
+ <Defs>
356
+ {/* An off-centre origin, fading to nothing well inside the circle. */}
357
+ <RadialGradient id={gradientId} cx="35%" cy="35%" r="64%">
358
+ <Stop offset="0%" stopColor={color} stopOpacity={1} />
359
+ <Stop offset="100%" stopColor={color} stopOpacity={0} />
360
+ </RadialGradient>
361
+ </Defs>
362
+ <Circle cx={size / 2} cy={size / 2} r={size / 2} fill={`url(#${gradientId})`} />
363
+ </Svg>
364
+ </Animated.View>
365
+ </Animated.View>
366
+ </Animated.View>
367
+ );
368
+ }
369
+
370
+ export interface AmbientFieldProps {
371
+ /** `screen` (default) fills a page; `sheet` is for a short card, see `SHEET_LAYOUT`. */
372
+ layout?: 'screen' | 'sheet';
373
+ /**
374
+ * Combined amplitude from `useAmbientWave` — swells and brightens the blobs.
375
+ * Omit where there is no audio (the intro) and the field simply rests.
376
+ */
377
+ waveAmplitude?: SharedValue<number>;
378
+ /** Drives the palette. Defaults to the resting triad. */
379
+ agentState?: AgentState;
380
+ /** Overall opacity target; ramps over 500ms when it changes. */
381
+ intensity?: number;
382
+ isDarkMode?: boolean;
383
+ /** Play the staggered 2.4s entrance. Off by default — only the intro wants it. */
384
+ entrance?: boolean;
385
+ /** Pointer parallax, normalised to -1..1. Omit for no parallax. */
386
+ pointerX?: SharedValue<number>;
387
+ /** @see pointerX */
388
+ pointerY?: SharedValue<number>;
389
+ /** Sink and dim the field while whatever sits behind it rises into view. */
390
+ exiting?: boolean;
391
+ /**
392
+ * Stop the endless loops: the screen holding the field is not the one on
393
+ * show (a route pushed over it keeps it mounted). The field also stops by
394
+ * itself while the app is in the background or the browser tab is hidden.
395
+ */
396
+ paused?: boolean;
397
+ }
398
+
399
+ /**
400
+ * The ambient background shared by the welcome intro and the chat: three
401
+ * blurred, drifting colour fields over a warm floor glow.
402
+ *
403
+ * It fills its parent, so the caller decides where it sits and what it sits
404
+ * behind. Every animation is driven through shared values and
405
+ * `useAnimatedReaction` rather than mapper-started styles, because
406
+ * mapper-started animations do not tick on reanimated-web.
407
+ *
408
+ * It is decoration: out of the accessibility tree on every platform, and it
409
+ * never takes a touch.
410
+ *
411
+ * **Reduced motion.** Every animation here uses Reanimated's default
412
+ * `ReduceMotion.System`, which with the setting on resolves a timing to its end
413
+ * at once, ends a `withRepeat` after one instant lap and does not start a
414
+ * reversing one at all — so the entrance lands at rest, the float settles at
415
+ * its origin and the beat never runs, with no frame work. What that does not
416
+ * cover is the pointer parallax, which is a direct response rather than an
417
+ * animation: it jumps instead of gliding, so it is switched off here. The
418
+ * response to live audio stays — it is how a call shows it is listening and
419
+ * speaking, not an ornament.
420
+ *
421
+ * **Reduced transparency** has nothing to act on: the field is the backmost
422
+ * layer, nothing is seen THROUGH it, and the translucent surfaces over it are
423
+ * Bloom's, which follow that setting themselves.
424
+ */
425
+ export const AmbientField = memo(function AmbientField({
426
+ layout = 'screen',
427
+ waveAmplitude,
428
+ agentState = 'idle',
429
+ intensity = 1,
430
+ isDarkMode = false,
431
+ entrance = false,
432
+ pointerX,
433
+ pointerY,
434
+ exiting = false,
435
+ paused = false,
436
+ }: AmbientFieldProps) {
437
+ const [field, setField] = useState({ width: 0, height: 0 });
438
+ const reduceMotion = usePrefersReducedMotion();
439
+
440
+ // On web, AppState follows the page's visibility.
441
+ const [inForeground, setInForeground] = useState(AppState.currentState !== 'background');
442
+ useEffect(() => {
443
+ const subscription = AppState.addEventListener('change', (state) => {
444
+ setInForeground(state !== 'background');
445
+ });
446
+ return () => subscription.remove();
447
+ }, []);
448
+ const running = !paused && inForeground;
449
+
450
+ // Fallbacks so the hook count never depends on which optional shared values
451
+ // the caller passed.
452
+ const silence = useSharedValue(0);
453
+ const idlePointerX = useSharedValue(0);
454
+ const idlePointerY = useSharedValue(0);
455
+ const amplitude = waveAmplitude ?? silence;
456
+ const px = (reduceMotion ? undefined : pointerX) ?? idlePointerX;
457
+ const py = (reduceMotion ? undefined : pointerY) ?? idlePointerY;
458
+
459
+ const intensityRamp = useSharedValue(intensity);
460
+ useAnimatedReaction(
461
+ () => intensity,
462
+ (target, previous) => {
463
+ if (target === previous) return;
464
+ intensityRamp.value = withTiming(target, { duration: INTENSITY_RAMP });
465
+ },
466
+ [intensity],
467
+ );
468
+
469
+ const exitOpacity = useSharedValue(0);
470
+ const exitTransform = useSharedValue(0);
471
+ useAnimatedReaction(
472
+ () => exiting,
473
+ (target, previous) => {
474
+ if (target === previous) return;
475
+ const to = target ? 1 : 0;
476
+ exitOpacity.value = withTiming(to, {
477
+ duration: EXIT_OPACITY_DURATION,
478
+ easing: EXIT_EASE,
479
+ });
480
+ exitTransform.value = withTiming(to, {
481
+ duration: EXIT_TRANSFORM_DURATION,
482
+ easing: EXIT_EASE,
483
+ });
484
+ },
485
+ [exiting],
486
+ );
487
+
488
+ const handleLayout = (event: LayoutChangeEvent) => {
489
+ const { width, height } = event.nativeEvent.layout;
490
+ setField({ width, height });
491
+ };
492
+
493
+ const sheet = layout === 'sheet';
494
+ const vw = (field.width / 100) * (sheet ? SHEET_LAYOUT.spread : 1);
495
+ const vh = field.height / 100;
496
+ const vmax = Math.max(field.width, field.height, sheet ? SHEET_LAYOUT.reference : 0) / 100;
497
+ const shiftX = sheet ? field.width * SHEET_LAYOUT.shift : 0;
498
+
499
+ const fieldStyle = useAnimatedStyle(() => ({
500
+ // On the way out the field keeps 45% of its opacity, sinking as it goes.
501
+ opacity: intensityRamp.value * (1 - exitOpacity.value * 0.55),
502
+ transform: [
503
+ { translateY: exitTransform.value * 12 * vh },
504
+ { scale: 1 + exitTransform.value * 0.1 },
505
+ ],
506
+ }));
507
+
508
+ const restStyle = useAnimatedStyle(() => ({
509
+ opacity: intensityRamp.value * (1 - exitOpacity.value * 0.5),
510
+ }));
511
+
512
+ const palette = PALETTES[agentState];
513
+
514
+ return (
515
+ <View
516
+ className="absolute inset-0"
517
+ pointerEvents="none"
518
+ // One prop for every platform: react-native-web writes `aria-hidden`, and
519
+ // React Native turns it into `accessibilityElementsHidden` (iOS) and
520
+ // `importantForAccessibility="no-hide-descendants"` (Android).
521
+ aria-hidden
522
+ onLayout={handleLayout}
523
+ >
524
+ {field.width > 0 ? (
525
+ <>
526
+ <Animated.View style={[{ position: 'absolute', inset: 0 }, restStyle]}>
527
+ <Svg width={field.width} height={field.height}>
528
+ <Defs>
529
+ <RadialGradient id="ambient-rest" cx="50%" cy="92%" rx="57%" ry="37%">
530
+ <Stop
531
+ offset="0%"
532
+ stopColor={REST_COLOR}
533
+ stopOpacity={isDarkMode ? REST_OPACITY_DARK : REST_OPACITY_LIGHT}
534
+ />
535
+ <Stop offset="70%" stopColor={REST_COLOR} stopOpacity={0} />
536
+ </RadialGradient>
537
+ </Defs>
538
+ <Rect
539
+ x={0}
540
+ y={0}
541
+ width={field.width}
542
+ height={field.height}
543
+ fill="url(#ambient-rest)"
544
+ />
545
+ </Svg>
546
+ </Animated.View>
547
+
548
+ <Animated.View
549
+ style={[{ position: 'absolute', inset: 0 }, blurStyle(FIELD_BLUR), fieldStyle]}
550
+ >
551
+ <View style={{ position: 'absolute', inset: 0, transform: [{ translateX: shiftX }] }}>
552
+ {FLOURISHES.map((config, index) => (
553
+ <Flourish
554
+ key={config.w}
555
+ config={config}
556
+ index={index}
557
+ color={palette[index] as string}
558
+ vw={vw}
559
+ vh={vh}
560
+ vmax={vmax}
561
+ fieldWidth={field.width}
562
+ fieldHeight={field.height}
563
+ blobOpacity={isDarkMode ? BLOB_OPACITY_DARK : BLOB_OPACITY_LIGHT}
564
+ blend={isDarkMode ? 'screen' : 'multiply'}
565
+ entrance={entrance}
566
+ running={running}
567
+ waveAmplitude={amplitude}
568
+ pointerX={px}
569
+ pointerY={py}
570
+ />
571
+ ))}
572
+ </View>
573
+ </Animated.View>
574
+ </>
575
+ ) : null}
576
+ </View>
577
+ );
578
+ });
579
+
580
+ export { PARALLAX_DURATION, PARALLAX_EASE };