@swisper.ai/web-chat-ui 0.3.2 → 0.4.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 (85) hide show
  1. package/dist/components/chat-prompt-box/chat-prompt-box-context.d.ts +10 -1
  2. package/dist/components/chat-prompt-box/chat-prompt-box-context.d.ts.map +1 -1
  3. package/dist/components/chat-prompt-box/chat-prompt-box-voice-motion.d.ts +85 -0
  4. package/dist/components/chat-prompt-box/chat-prompt-box-voice-motion.d.ts.map +1 -0
  5. package/dist/components/chat-prompt-box/chat-prompt-box-voice.d.ts +20 -0
  6. package/dist/components/chat-prompt-box/chat-prompt-box-voice.d.ts.map +1 -0
  7. package/dist/components/chat-prompt-box/chat-prompt-box-voice.stories.d.ts +45 -0
  8. package/dist/components/chat-prompt-box/chat-prompt-box-voice.stories.d.ts.map +1 -0
  9. package/dist/components/chat-prompt-box/chat-prompt-box.d.ts +24 -1
  10. package/dist/components/chat-prompt-box/chat-prompt-box.d.ts.map +1 -1
  11. package/dist/components/chat-prompt-box/chat-prompt-box.stories.d.ts.map +1 -1
  12. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.d.ts +31 -0
  13. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.d.ts.map +1 -0
  14. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.d.ts +17 -0
  15. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.d.ts.map +1 -0
  16. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.d.ts +3 -0
  17. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.d.ts.map +1 -0
  18. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.d.ts +29 -0
  19. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.d.ts.map +1 -0
  20. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.d.ts +12 -0
  21. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.d.ts.map +1 -0
  22. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.d.ts +3 -0
  23. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.d.ts.map +1 -0
  24. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.d.ts +44 -0
  25. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.d.ts.map +1 -0
  26. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.d.ts +17 -0
  27. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.d.ts.map +1 -0
  28. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.d.ts +3 -0
  29. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.d.ts.map +1 -0
  30. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.d.ts +14 -0
  31. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.d.ts.map +1 -0
  32. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.d.ts +11 -0
  33. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.d.ts.map +1 -0
  34. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.d.ts +3 -0
  35. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.d.ts.map +1 -0
  36. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.d.ts +18 -0
  37. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.d.ts.map +1 -0
  38. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.d.ts +18 -0
  39. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.d.ts.map +1 -0
  40. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.d.ts +3 -0
  41. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.d.ts.map +1 -0
  42. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.d.ts +14 -0
  43. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.d.ts.map +1 -0
  44. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.d.ts +11 -0
  45. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.d.ts.map +1 -0
  46. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.d.ts +3 -0
  47. package/dist/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.d.ts.map +1 -0
  48. package/dist/components/chat-prompt-box/components/index.d.ts +7 -0
  49. package/dist/components/chat-prompt-box/components/index.d.ts.map +1 -0
  50. package/dist/components/chat-prompt-box/index.d.ts +5 -1
  51. package/dist/components/chat-prompt-box/index.d.ts.map +1 -1
  52. package/dist/components/chat-prompt-box/use-voice-motion.d.ts +22 -0
  53. package/dist/components/chat-prompt-box/use-voice-motion.d.ts.map +1 -0
  54. package/dist/index.css +1 -1
  55. package/dist/index.es.js +2285 -1443
  56. package/dist/index.es.js.map +1 -1
  57. package/package.json +3 -3
  58. package/src/components/chat-prompt-box/chat-prompt-box-context.tsx +35 -1
  59. package/src/components/chat-prompt-box/chat-prompt-box-voice-motion.ts +436 -0
  60. package/src/components/chat-prompt-box/chat-prompt-box-voice.stories.tsx +243 -0
  61. package/src/components/chat-prompt-box/chat-prompt-box-voice.tsx +191 -0
  62. package/src/components/chat-prompt-box/chat-prompt-box.json +66 -7
  63. package/src/components/chat-prompt-box/chat-prompt-box.stories.tsx +16 -0
  64. package/src/components/chat-prompt-box/chat-prompt-box.tsx +131 -77
  65. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.stories.tsx +155 -0
  66. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/chat-prompt-box-voice-bar-icon.tsx +80 -0
  67. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-bar-icon/index.ts +5 -0
  68. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.stories.tsx +107 -0
  69. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/chat-prompt-box-voice-button.tsx +101 -0
  70. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-button/index.ts +5 -0
  71. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.stories.tsx +177 -0
  72. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/chat-prompt-box-voice-light.tsx +283 -0
  73. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-light/index.ts +5 -0
  74. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.stories.tsx +50 -0
  75. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/chat-prompt-box-voice-mic-button.tsx +46 -0
  76. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-mic-button/index.ts +2 -0
  77. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.stories.tsx +149 -0
  78. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/chat-prompt-box-voice-panel.tsx +55 -0
  79. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-panel/index.ts +5 -0
  80. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.stories.tsx +50 -0
  81. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/chat-prompt-box-voice-sound-button.tsx +46 -0
  82. package/src/components/chat-prompt-box/components/chat-prompt-box-voice-sound-button/index.ts +2 -0
  83. package/src/components/chat-prompt-box/components/index.ts +6 -0
  84. package/src/components/chat-prompt-box/index.ts +23 -0
  85. package/src/components/chat-prompt-box/use-voice-motion.ts +184 -0
@@ -0,0 +1,243 @@
1
+ import type { Meta, StoryObj } from '@storybook/react-vite';
2
+ import { useEffect, useState } from 'react';
3
+ import { fn } from 'storybook/test';
4
+
5
+ import { AddIcon } from '@swisper.ai/icons';
6
+ import { IconButton } from '@swisper.ai/ui';
7
+
8
+ import { LONG_PROMPT } from './__stories__/fixtures';
9
+ import {
10
+ ChatPromptBox,
11
+ ChatPromptBoxActionsLeft,
12
+ ChatPromptBoxActionsRight,
13
+ ChatPromptBoxContent,
14
+ ChatPromptBoxInput,
15
+ } from './chat-prompt-box';
16
+ import type { ChatPromptBoxMode } from './chat-prompt-box';
17
+ import { ChatPromptBoxVoiceStateWord } from './chat-prompt-box-voice';
18
+ import {
19
+ ChatPromptBoxVoiceBarIcon,
20
+ ChatPromptBoxVoiceButton,
21
+ ChatPromptBoxVoiceMicButton,
22
+ ChatPromptBoxVoicePanel,
23
+ ChatPromptBoxVoiceSoundButton,
24
+ } from './components';
25
+
26
+ const ONE_LINE_PROMPT = 'Book me a table for two at Kronenhalle on Friday at 8';
27
+
28
+ const MODES: ChatPromptBoxMode[] = [
29
+ 'text',
30
+ 'connecting',
31
+ 'listening',
32
+ 'user-speaking',
33
+ 'thinking',
34
+ 'answering',
35
+ 'mic-muted',
36
+ 'reconnecting',
37
+ ];
38
+
39
+ const BAND_COUNT = 16;
40
+
41
+ /**
42
+ * A made-up voice for the stories: syllables (~4 a second) in phrases, with a little noise. In the
43
+ * app, `voiceLevel` and `voiceBands` come from the audio (e.g. an `AnalyserNode`).
44
+ */
45
+ function useSimulatedVoice(active: boolean) {
46
+ const [voice, setVoice] = useState<{ level: number; bands: number[] }>({ level: 0, bands: [] });
47
+
48
+ useEffect(() => {
49
+ if (!active) return undefined;
50
+ let frameId = 0;
51
+ const start = performance.now();
52
+ const tick = (now: number) => {
53
+ const t = (now - start) / 1000;
54
+ const phrase = Math.max(0, Math.sin(t * 0.9)) ** 0.4;
55
+ const syllable = 0.55 + 0.45 * Math.abs(Math.sin(t * 4.2 * Math.PI));
56
+ const level = Math.min(1, phrase * syllable * (0.85 + Math.random() * 0.15));
57
+ const bands = Array.from({ length: BAND_COUNT }, (_, i) => {
58
+ const tilt = 1 - i / BAND_COUNT;
59
+ return Math.min(1, level * (0.4 + 0.6 * tilt) * (0.7 + 0.3 * Math.sin(t * 3 + i)));
60
+ });
61
+ setVoice({ level, bands });
62
+ frameId = requestAnimationFrame(tick);
63
+ };
64
+ frameId = requestAnimationFrame(tick);
65
+ return () => cancelAnimationFrame(frameId);
66
+ }, [active]);
67
+
68
+ return active ? voice : { level: 0, bands: undefined };
69
+ }
70
+
71
+ interface VoiceComposerProps {
72
+ mode: ChatPromptBoxMode;
73
+ /** What the user has said so far. With text, the voice button becomes Send. */
74
+ transcript?: string;
75
+ onEndVoice: () => void;
76
+ onSend: () => void;
77
+ }
78
+
79
+ /**
80
+ * A voice composer as the app would build it. The mic and sound switches follow the spec: muting
81
+ * while Listening shows Mic muted, turning the sound off while Answering goes back to Listening.
82
+ */
83
+ function VoiceComposer({ mode, transcript = '', onEndVoice, onSend }: VoiceComposerProps) {
84
+ const hasText = transcript.length > 0;
85
+ const [micMuted, setMicMuted] = useState(false);
86
+ const [soundOff, setSoundOff] = useState(false);
87
+
88
+ let shownMode = mode;
89
+ if (micMuted && mode === 'listening') shownMode = 'mic-muted';
90
+ if (soundOff && mode === 'answering') shownMode = micMuted ? 'mic-muted' : 'listening';
91
+
92
+ const voice = useSimulatedVoice(shownMode === 'user-speaking' || shownMode === 'answering');
93
+
94
+ return (
95
+ <ChatPromptBox
96
+ mode={shownMode}
97
+ voiceLevel={voice.level}
98
+ voiceBands={shownMode === 'answering' ? voice.bands : undefined}
99
+ >
100
+ <ChatPromptBoxContent>
101
+ <ChatPromptBoxActionsLeft aria-label="Attachments">
102
+ <IconButton size="md" variant="tertiary" aria-label="Upload file">
103
+ <AddIcon />
104
+ </IconButton>
105
+ </ChatPromptBoxActionsLeft>
106
+ <ChatPromptBoxInput value={transcript} placeholder="How can I help?" autoFocus={false} />
107
+ <ChatPromptBoxActionsRight aria-label="Voice controls">
108
+ <ChatPromptBoxVoicePanel>
109
+ <ChatPromptBoxVoiceMicButton muted={micMuted} onMutedChange={setMicMuted} />
110
+ <ChatPromptBoxVoiceSoundButton muted={soundOff} onMutedChange={setSoundOff} />
111
+ <ChatPromptBoxVoiceStateWord />
112
+ <ChatPromptBoxVoiceButton
113
+ show={hasText ? 'send' : 'bars'}
114
+ onClick={hasText ? onSend : onEndVoice}
115
+ />
116
+ </ChatPromptBoxVoicePanel>
117
+ </ChatPromptBoxActionsRight>
118
+ </ChatPromptBoxContent>
119
+ </ChatPromptBox>
120
+ );
121
+ }
122
+
123
+ /** The main loop on a timer, to watch each state hand over to the next. */
124
+ const MAIN_LOOP: { mode: ChatPromptBoxMode; seconds: number }[] = [
125
+ { mode: 'connecting', seconds: 2 },
126
+ { mode: 'listening', seconds: 4 },
127
+ { mode: 'user-speaking', seconds: 4 },
128
+ { mode: 'thinking', seconds: 4.4 },
129
+ { mode: 'answering', seconds: 5 },
130
+ ];
131
+
132
+ function MainLoopComposer(props: Omit<VoiceComposerProps, 'mode'>) {
133
+ const [step, setStep] = useState(0);
134
+
135
+ useEffect(() => {
136
+ const timer = setTimeout(
137
+ () => setStep((current) => (current + 1 < MAIN_LOOP.length ? current + 1 : 1)),
138
+ MAIN_LOOP[step].seconds * 1000,
139
+ );
140
+ return () => clearTimeout(timer);
141
+ }, [step]);
142
+
143
+ return <VoiceComposer {...props} mode={MAIN_LOOP[step].mode} />;
144
+ }
145
+
146
+ const meta: Meta<typeof VoiceComposer> = {
147
+ title: 'Chat/ChatPromptBox/Voice',
148
+ component: VoiceComposer,
149
+ subcomponents: {
150
+ ChatPromptBoxVoicePanel,
151
+ ChatPromptBoxVoiceMicButton,
152
+ ChatPromptBoxVoiceSoundButton,
153
+ ChatPromptBoxVoiceStateWord,
154
+ ChatPromptBoxVoiceButton,
155
+ ChatPromptBoxVoiceBarIcon,
156
+ },
157
+ decorators: [
158
+ (Story) => (
159
+ // Room above the box for the voice light.
160
+ <div className="max-w-chat-area pt-[120px]">
161
+ <Story />
162
+ </div>
163
+ ),
164
+ ],
165
+ argTypes: {
166
+ mode: {
167
+ control: 'select',
168
+ options: MODES,
169
+ description:
170
+ '`mode` of `ChatPromptBox`. Sets the border, the voice light, the state word and the bars. In `user-speaking` and `answering` the story feeds a made-up voice into `voiceLevel` and `voiceBands`.',
171
+ },
172
+ transcript: {
173
+ control: 'text',
174
+ description: 'Text in the field. With text, the voice button becomes Send.',
175
+ },
176
+ onEndVoice: {
177
+ description: 'Called by the bars button (Close on hover), which ends voice mode.',
178
+ },
179
+ onSend: { description: 'Called by Send while there is text.' },
180
+ },
181
+ args: {
182
+ onEndVoice: fn(),
183
+ onSend: fn(),
184
+ },
185
+ };
186
+
187
+ export default meta;
188
+ type Story = StoryObj<typeof VoiceComposer>;
189
+
190
+ /** Until the connection is up and while the browser asks for the mic: a grey pulse, still bars, no light. */
191
+ export const Connecting: Story = { args: { mode: 'connecting' } };
192
+
193
+ /** The connection dropped. Looks like Connecting; once back, the box returns to its previous state. */
194
+ export const Reconnecting: Story = { args: { mode: 'reconnecting' } };
195
+
196
+ /** Nobody speaks: the border lights travel slowly, the light breathes and the bars barely move. Mute the mic to see Mic muted. */
197
+ export const Listening: Story = { args: { mode: 'listening' } };
198
+
199
+ /** The user speaks: the border stays as in Listening; the light and the bars follow the voice. */
200
+ export const UserSpeaking: Story = { args: { mode: 'user-speaking' } };
201
+
202
+ /** Listening with the mic off: a grey pulse as in Connecting, no light, low and slow bars. */
203
+ export const MicMuted: Story = { args: { mode: 'mic-muted' } };
204
+
205
+ /** What the user said fills the field and the voice button becomes Send. */
206
+ export const ListeningWithText: Story = {
207
+ args: { mode: 'listening', transcript: ONE_LINE_PROMPT },
208
+ };
209
+
210
+ /** A long transcript expands the box, as in text mode. */
211
+ export const ListeningMultiline: Story = {
212
+ args: { mode: 'listening', transcript: LONG_PROMPT },
213
+ };
214
+
215
+ /** Swisper is working out the answer: the lights turn on the 1.1 s beat and the bars wave with it. */
216
+ export const Thinking: Story = { args: { mode: 'thinking' } };
217
+
218
+ export const ThinkingWithText: Story = {
219
+ args: { mode: 'thinking', transcript: ONE_LINE_PROMPT },
220
+ };
221
+
222
+ export const ThinkingMultiline: Story = {
223
+ args: { mode: 'thinking', transcript: LONG_PROMPT },
224
+ };
225
+
226
+ /** Swisper speaks: the light follows the voice with a cream centre where it is loud, and the bars follow it. Turn the sound off to go back to Listening. */
227
+ export const Answering: Story = { args: { mode: 'answering' } };
228
+
229
+ export const AnsweringWithText: Story = {
230
+ args: { mode: 'answering', transcript: ONE_LINE_PROMPT },
231
+ };
232
+
233
+ export const AnsweringMultiline: Story = {
234
+ args: { mode: 'answering', transcript: LONG_PROMPT },
235
+ };
236
+
237
+ /**
238
+ * Connecting, then Listening (the bars jump with "Voice starts"), User speaking, Thinking and
239
+ * Answering, round and round. The border's pace, the light and the grey pulse ease between states.
240
+ */
241
+ export const MainLoop: Story = {
242
+ render: (args) => <MainLoopComposer onEndVoice={args.onEndVoice} onSend={args.onSend} />,
243
+ };
@@ -0,0 +1,191 @@
1
+ import { useState, type ComponentPropsWithoutRef, type ReactNode } from 'react';
2
+
3
+ import { cn } from '@swisper.ai/web-core';
4
+
5
+ import {
6
+ useChatPromptBoxContext,
7
+ useOptionalChatPromptBoxContext,
8
+ } from './chat-prompt-box-context';
9
+ import type { ChatPromptBoxMode } from './chat-prompt-box-context';
10
+ import { VOICE_MOTION_BY_MODE, type VoiceLightState } from './chat-prompt-box-voice-motion';
11
+ import { ChatPromptBoxVoiceLightLayers } from './components/chat-prompt-box-voice-light/chat-prompt-box-voice-light';
12
+ import { useVoiceMotionRef } from './use-voice-motion';
13
+
14
+ type VoiceMode = Exclude<ChatPromptBoxMode, 'text'>;
15
+
16
+ /*
17
+ * The ring behind the box: the box's 1px padding shows it as the border. The voice clock writes
18
+ * its CSS variables (`--voice-spot-a` / `-b` along the outline, `--voice-spot-strength`,
19
+ * `--voice-line-strength`, `--voice-pulse` and `--voice-pulse-mix`, the grey pulse over the
20
+ * lights). Colours are the ct/promptbox/voice tokens. Each light is a radial gradient whose radius
21
+ * is 22% of the box's width (70 to 180px); it travels the outline with `offset-path` and, without
22
+ * it, rests where the Echo DS frames draw it.
23
+ */
24
+ const CHAT_PROMPT_BOX_VOICE_STYLES = `
25
+ [data-slot='chat-prompt-box-voice-lights'] {
26
+ --voice-glow: color-mix(in srgb, var(--color-promptbox-voice-border-glow) calc((1 - var(--voice-pulse-mix, 0)) * 100%), transparent);
27
+ filter: drop-shadow(0 0 1.75px var(--voice-glow));
28
+ }
29
+ [data-slot='chat-prompt-box-voice-ring'][data-border='beat'] [data-slot='chat-prompt-box-voice-lights'] {
30
+ filter: drop-shadow(0 0 1.5px var(--voice-glow));
31
+ }
32
+ [data-slot='chat-prompt-box-voice-pulse'] {
33
+ filter: drop-shadow(0 0 3.6px color-mix(in srgb, var(--color-promptbox-voice-border-pulse-glow) calc(var(--voice-pulse, 0.45) / 0.73 * 60%), transparent));
34
+ }
35
+ [data-slot='chat-prompt-box-voice-spot'] {
36
+ position: absolute;
37
+ left: var(--spot-x);
38
+ top: var(--spot-y);
39
+ width: clamp(140px, 44%, 360px);
40
+ aspect-ratio: 1;
41
+ translate: -50% -50%;
42
+ border-radius: 9999px;
43
+ background: radial-gradient(circle closest-side, var(--color-promptbox-voice-border-core) 0%, var(--color-promptbox-voice-border-light) 30%, transparent 100%);
44
+ opacity: var(--voice-spot-strength, 0.73);
45
+ }
46
+ [data-slot='chat-prompt-box-voice-spot'][data-spot='a'] { --spot-x: 18.16%; --spot-y: 0%; }
47
+ [data-slot='chat-prompt-box-voice-spot'][data-spot='b'] { --spot-x: 81.84%; --spot-y: 100%; }
48
+ @supports (offset-path: rect(0 0 0 0)) {
49
+ [data-slot='chat-prompt-box-voice-spot'] {
50
+ left: 0;
51
+ top: 0;
52
+ translate: none;
53
+ offset-path: rect(0 100% 100% 0 round 16px);
54
+ offset-rotate: 0deg;
55
+ }
56
+ [data-slot='chat-prompt-box-voice-spot'][data-spot='a'] { offset-distance: var(--voice-spot-a, 8.3%); }
57
+ [data-slot='chat-prompt-box-voice-spot'][data-spot='b'] { offset-distance: var(--voice-spot-b, 58.3%); }
58
+ }
59
+ `;
60
+
61
+ /**
62
+ * The voice ring behind the box. One element in every voice mode, so moving between modes eases
63
+ * (the lights keep their place and the grey pulse fades over them) instead of remounting.
64
+ */
65
+ function ChatPromptBoxVoiceRing({ mode }: { mode: VoiceMode }) {
66
+ const { voiceMotion } = useChatPromptBoxContext();
67
+ const ref = useVoiceMotionRef<HTMLDivElement>(voiceMotion, 'ring');
68
+
69
+ return (
70
+ <div
71
+ ref={ref}
72
+ data-slot="chat-prompt-box-voice-ring"
73
+ data-border={VOICE_MOTION_BY_MODE[mode].border}
74
+ aria-hidden
75
+ className="absolute inset-0 z-0 rounded-[16px] pointer-events-none"
76
+ >
77
+ <div
78
+ data-slot="chat-prompt-box-voice-lights"
79
+ className="absolute inset-0 overflow-hidden rounded-[16px] bg-promptbox-border-background"
80
+ >
81
+ <div
82
+ className="absolute inset-0 bg-promptbox-voice-border-line"
83
+ style={{ opacity: 'var(--voice-line-strength, 1)' }}
84
+ />
85
+ <div data-slot="chat-prompt-box-voice-spot" data-spot="a" />
86
+ <div data-slot="chat-prompt-box-voice-spot" data-spot="b" />
87
+ </div>
88
+ <div
89
+ data-slot="chat-prompt-box-voice-pulse"
90
+ className="absolute inset-0 overflow-hidden rounded-[16px] bg-promptbox-voice-border-pulse-base"
91
+ style={{ opacity: 'var(--voice-pulse-mix, 0)' }}
92
+ >
93
+ <div
94
+ className="absolute inset-0 bg-promptbox-voice-border-pulse"
95
+ style={{ opacity: 'var(--voice-pulse, 0.45)' }}
96
+ />
97
+ </div>
98
+ </div>
99
+ );
100
+ }
101
+
102
+ /**
103
+ * The light's look: the mode's, or the last live one while it fades out (Connecting, Mic muted).
104
+ */
105
+ function useVoiceLightState(mode: VoiceMode): VoiceLightState {
106
+ const kind = VOICE_MOTION_BY_MODE[mode].light;
107
+ const [last, setLast] = useState<VoiceLightState>(kind === 'none' ? 'listening' : kind);
108
+
109
+ if (kind !== 'none' && kind !== last) {
110
+ setLast(kind);
111
+ }
112
+
113
+ return kind === 'none' ? last : kind;
114
+ }
115
+
116
+ /** The voice ring behind the box and the light above it, moved by the box's voice clock. */
117
+ export function ChatPromptBoxVoiceDecor({
118
+ mode,
119
+ bands,
120
+ }: {
121
+ mode: VoiceMode;
122
+ bands?: readonly number[];
123
+ }) {
124
+ const { voiceMotion } = useChatPromptBoxContext();
125
+ const lightRef = useVoiceMotionRef<HTMLDivElement>(voiceMotion, 'light');
126
+ const lightState = useVoiceLightState(mode);
127
+
128
+ return (
129
+ <>
130
+ <style>{CHAT_PROMPT_BOX_VOICE_STYLES}</style>
131
+ <ChatPromptBoxVoiceRing mode={mode} />
132
+ <ChatPromptBoxVoiceLightLayers
133
+ ref={lightRef}
134
+ state={lightState}
135
+ bands={bands}
136
+ className="absolute bottom-full inset-x-0 z-0"
137
+ />
138
+ </>
139
+ );
140
+ }
141
+
142
+ const STATE_WORD: Record<ChatPromptBoxMode, string | null> = {
143
+ text: null,
144
+ connecting: 'Connecting…',
145
+ listening: 'Listening',
146
+ 'user-speaking': 'Listening',
147
+ thinking: 'Thinking',
148
+ answering: 'Answering',
149
+ 'mic-muted': 'Mic muted',
150
+ reconnecting: 'Reconnecting…',
151
+ };
152
+
153
+ export interface ChatPromptBoxVoiceStateWordProps extends ComponentPropsWithoutRef<'span'> {
154
+ /** Overrides the word for the box's mode, e.g. a translation. */
155
+ children?: ReactNode;
156
+ }
157
+
158
+ /**
159
+ * The word for the voice state (`Listening`, `Thinking`, `Mic muted`, …), announced politely when it changes.
160
+ * Outside a `ChatPromptBox` it shows only its children. 104px wide on desktop; fills the panel on
161
+ * mobile.
162
+ */
163
+ export function ChatPromptBoxVoiceStateWord({
164
+ className,
165
+ children,
166
+ ...rest
167
+ }: ChatPromptBoxVoiceStateWordProps) {
168
+ const context = useOptionalChatPromptBoxContext();
169
+ const word = children ?? (context ? STATE_WORD[context.mode] : null);
170
+
171
+ if (word === null || word === undefined) {
172
+ return null;
173
+ }
174
+
175
+ return (
176
+ <span
177
+ data-slot="chat-prompt-box-voice-state-word"
178
+ aria-live="polite"
179
+ className={cn(
180
+ 'shrink-0 w-[104px]',
181
+ 'group-data-[device=mobile]/voice-panel:flex-1 group-data-[device=mobile]/voice-panel:w-auto group-data-[device=mobile]/voice-panel:min-w-0',
182
+ 'text-center text-[14px] leading-[1.6] italic',
183
+ 'text-promptbox-voice-panel-state-word',
184
+ className,
185
+ )}
186
+ {...rest}
187
+ >
188
+ {word}
189
+ </span>
190
+ );
191
+ }
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "../../../../int-eslint-config/schemas/component.schema.json",
3
3
  "component": "chat-prompt-box",
4
- "purpose": "Swisper's styled message composer: a ChatInput in a rounded box with hover and focus styling, a focus glow, and left and right action slots that sit inline while the text is one line and move under it once the text grows.",
4
+ "purpose": "Swisper's styled message composer: a ChatInput in a rounded box with hover and focus styling, a focus glow, and left and right action slots that sit inline while the text is one line and move under it once the text grows. ChatPromptBoxVoiceBarIcon is the five live bars inside the voice button, animated per voice state. ChatPromptBoxVoiceButton is the last button of the voice panel: the bars on a pressed Prominent button that turns into Close on hover, or Send while the field has text. ChatPromptBoxVoicePanel is the voice panel (Echo DS _Voice-panel), for desktop or mobile: compose ChatPromptBoxVoiceMicButton, ChatPromptBoxVoiceSoundButton (the mic and sound toggles), ChatPromptBoxVoiceStateWord and ChatPromptBoxVoiceButton inside it. ChatPromptBoxVoiceLight is the aurora above the box in voice mode (ChatPromptBox draws it itself and moves it with `voiceLevel` and `voiceBands`); use it on its own with `state`, `level` and `bands`.",
5
5
  "anatomy": [
6
6
  "import {",
7
7
  " ChatPromptBox,",
@@ -10,16 +10,45 @@
10
10
  " ChatPromptBoxInput,",
11
11
  " ChatPromptBoxActionsLeft,",
12
12
  " ChatPromptBoxActionsRight,",
13
+ " ChatPromptBoxVoiceStateWord,",
14
+ " ChatPromptBoxVoiceBarIcon,",
15
+ " ChatPromptBoxVoiceButton,",
16
+ " ChatPromptBoxVoiceLight,",
17
+ " ChatPromptBoxVoicePanel,",
18
+ " ChatPromptBoxVoiceMicButton,",
19
+ " ChatPromptBoxVoiceSoundButton,",
13
20
  "} from '@swisper.ai/web-chat-ui';",
21
+ "import { IconButton } from '@swisper.ai/ui';",
14
22
  "",
15
23
  "<ChatPromptBox>",
16
24
  " <ChatPromptBoxToolbar />",
17
25
  " <ChatPromptBoxContent>",
18
26
  " <ChatPromptBoxInput />",
19
27
  " <ChatPromptBoxActionsLeft />",
20
- " <ChatPromptBoxActionsRight />",
28
+ " <ChatPromptBoxActionsRight>",
29
+ " <IconButton aria-label=\"Voice mode\">",
30
+ " <ChatPromptBoxVoiceBarIcon state=\"listening\" />",
31
+ " </IconButton>",
32
+ " </ChatPromptBoxActionsRight>",
21
33
  " </ChatPromptBoxContent>",
22
- "</ChatPromptBox>"
34
+ "</ChatPromptBox>",
35
+ "",
36
+ "<ChatPromptBox mode=\"answering\" voiceLevel={level} voiceBands={bands}>",
37
+ " <ChatPromptBoxContent>",
38
+ " <ChatPromptBoxActionsLeft />",
39
+ " <ChatPromptBoxInput />",
40
+ " <ChatPromptBoxActionsRight>",
41
+ " <ChatPromptBoxVoicePanel>",
42
+ " <ChatPromptBoxVoiceMicButton muted={micMuted} onMutedChange={setMicMuted} />",
43
+ " <ChatPromptBoxVoiceSoundButton muted={soundOff} onMutedChange={setSoundOff} />",
44
+ " <ChatPromptBoxVoiceStateWord />",
45
+ " <ChatPromptBoxVoiceButton onClick={endVoice} />",
46
+ " </ChatPromptBoxVoicePanel>",
47
+ " </ChatPromptBoxActionsRight>",
48
+ " </ChatPromptBoxContent>",
49
+ "</ChatPromptBox>",
50
+ "",
51
+ "<ChatPromptBoxVoiceLight state=\"speaking\" level={level} bands={bands} />"
23
52
  ],
24
53
  "useWhen": [
25
54
  "The main composer of a Swisper chat, where the user types a question and sends it",
@@ -33,7 +62,8 @@
33
62
  ],
34
63
  "uxPrinciples": [
35
64
  "One primary action, send, sits at the end of the box; secondary tools such as attach go on the left.",
36
- "The box stays one line until the text needs more, then grows and moves the actions below the text so the text keeps the full width."
65
+ "The box stays one line until the text needs more, then grows and moves the actions below the text so the text keeps the full width.",
66
+ "In a voice conversation the mode shows what is happening: connecting, reconnecting and mic-muted pulse the border grey with no light; listening, user-speaking and answering run blue lights slowly round the border; thinking beats them; the light above the box breathes, beats or follows the voice. The state also shows in the state word and the bars; the panel itself looks the same in every state. Only thinking moves fast."
37
67
  ],
38
68
  "accessibility": {
39
69
  "required": [
@@ -55,8 +85,28 @@
55
85
  ]
56
86
  },
57
87
  "behaviour": {
58
- "states": ["default", "hover", "focused", "compact", "expanded", "disabled"],
88
+ "states": [
89
+ "default",
90
+ "hover",
91
+ "focused",
92
+ "compact",
93
+ "expanded",
94
+ "disabled",
95
+ "connecting",
96
+ "listening",
97
+ "thinking",
98
+ "answering",
99
+ "user-speaking",
100
+ "mic-muted",
101
+ "reconnecting"
102
+ ],
59
103
  "rules": [
104
+ "mode is text (default), connecting, listening, user-speaking, thinking, answering, mic-muted or reconnecting, set as data-mode on the root. Any voice mode drops the hover and focus styling and the focus glows and keeps one background",
105
+ "voiceLevel (0 to 1) is the speaker's loudness, the user in user-speaking and Swisper in answering; voiceBands (0 to 1 each, low to high) shape the light while answering. Update them every frame; they are read by the clock, not rendered",
106
+ "Voice motion follows the Echo DS spec (Prompt Box, Voice motion) on one requestAnimationFrame clock that writes CSS variables to the ring, the light and the bars. Border: connecting, reconnecting and mic-muted breathe grey (17% to 73% every 1.9s, ease-in-out, the glow with it); listening, user-speaking and answering run two lights half a turn apart round the outline once every 30s at 73%; thinking surges them a quarter turn (ease-in-out cubic over 0.6s) every 1.1s on top of 40°/s, 69% to 89% with the beat. Each light's radius is 22% of the box width (70 to 180px). Between modes the border's pace eases (0.45s) and the lights keep their place. Without offset-path the lights rest where the Echo DS frames draw them",
107
+ "The voice light above the box (outside it, 103px tall, height = level × 72px): listening breathes every 5.5s (8 to 25px, flat), user-speaking follows voiceLevel (0.35 × (0.35 + 0.65 × level)), thinking beats with the 1.1s beat (8 to 40px, fuller in the middle), answering follows voiceLevel up to 72px with a cream middle where loud and voiceBands shaping it; a faint line stays in the pauses. It rises in 0.08s and falls in 0.6s, also between modes; connecting, reconnecting and mic-muted fade it out",
108
+ "ChatPromptBoxVoicePanel is a role=group (aria-label Voice by default) for ChatPromptBoxVoiceMicButton, ChatPromptBoxVoiceSoundButton, ChatPromptBoxVoiceStateWord and ChatPromptBoxVoiceButton, in that order; put it in ChatPromptBoxActionsRight. Its background is the same in every state. device is desktop (default: 8px padding and gaps, 56px tall, flush in the corner) or mobile (4px, 48px tall, full width). ChatPromptBoxVoiceStateWord shows the word for the mode (Connecting…, Listening in listening and user-speaking, Thinking, Answering, Mic muted, Reconnecting…), aria-live polite; children replace it; 104px wide on desktop, fills the panel on mobile; outside a ChatPromptBox it shows only its children",
109
+ "ChatPromptBoxVoiceMicButton and ChatPromptBoxVoiceSoundButton are tertiary lg IconButtons with aria-pressed; muted swaps in the mic-off / sound-off icon; clicking calls onClick, then onMutedChange with the new value. aria-label defaults to Mute microphone / Mute sound",
60
110
  "The box starts compact: the input and both action groups share one row",
61
111
  "When the measured input is taller than 40px (the text wraps), the box expands: the actions are positioned along the bottom edge and the content gets bottom padding for them",
62
112
  "The box returns to compact when the input is emptied (e.g. after send) or unmounts; it does not shrink back while text remains",
@@ -67,7 +117,9 @@
67
117
  "Every part sets data-slot=chat-prompt-box-{part} and merges className last. On ChatPromptBox, className styles the root (the 1px border ring) and innerClassName the surface inside it that carries the background and padding",
68
118
  "On ChatPromptBoxInput, className styles the wrapper that lays the input out in the row and textareaClassName the editable element; its 72px max height is set with !, so override it with ! too",
69
119
  "ChatPromptBoxInput renders its children inside the editor, after its own focus plugin; use them for extra Lexical plugins",
70
- "ChatPromptBoxToolbar, ChatPromptBoxContent and both action groups pass other div props (aria-label, data-*, event handlers) to their element"
120
+ "ChatPromptBoxToolbar, ChatPromptBoxContent and both action groups pass other div props (aria-label, data-*, event handlers) to their element",
121
+ "ChatPromptBoxVoiceBarIcon renders five bars in a 20px box, aria-hidden (the button carries the label), in 0.5px steps. Unset state inside a voice-mode ChatPromptBox: the bars follow the box's mode and voiceLevel on its clock (reaching listening from connecting plays the Voice starts kicks). Otherwise state is still (2px dots), listening (shape × (0.32 + 0.04 sin(1.6t + 0.7i)), a sway every 3.9s), voice-starts (two kicks at 0 and 0.11s, middle bar first, 30ms per bar outwards, then listening by itself; remount with key to replay), thinking (one wave per 1.1s beat entering on the left, crest 14.5px), voice (follows level with a flicker per bar, 3.5 to 16px) or muted (shape × (0.24 + 0.07 sin(1.1t + 0.9i)), a sway every 5.7s)",
122
+ "Under prefers-reduced-motion there is no clock: the ring, the light and the bars rest on each state's Base look; the voice states still follow voiceLevel / level without easing"
71
123
  ]
72
124
  },
73
125
  "composition": {
@@ -78,6 +130,10 @@
78
130
  "web-chat-ui/ChatPromptBoxActionsLeft",
79
131
  "web-chat-ui/ChatPromptBoxActionsRight",
80
132
  "web-chat-ui/ChatPromptBoxToolbar",
133
+ "web-chat-ui/ChatPromptBoxVoiceStateWord",
134
+ "web-chat-ui/ChatPromptBoxVoicePanel",
135
+ "web-chat-ui/ChatPromptBoxVoiceMicButton",
136
+ "web-chat-ui/ChatPromptBoxVoiceSoundButton",
81
137
  "ui/icon-button"
82
138
  ],
83
139
  "avoidCombinations": [
@@ -122,7 +178,10 @@
122
178
  "categories": [
123
179
  "--color-promptbox-background-border-base",
124
180
  "--color-promptbox-background-base",
125
- "--color-promptbox-background-hover"
181
+ "--color-promptbox-background-hover",
182
+ "--color-promptbox-voice-bars-bar",
183
+ "--spacing-promptbox-voice-bars-gap",
184
+ "--radius-promptbox-voice-bars"
126
185
  ],
127
186
  "rule": "The border, focused background and hover use the promptbox tokens; extend the box with these tokens rather than hex colours."
128
187
  }
@@ -64,6 +64,22 @@ const meta: Meta<typeof ChatPromptBox> = {
64
64
  description:
65
65
  'Extra classes merged onto the surface inside the border, which carries the background and padding.',
66
66
  },
67
+ mode: {
68
+ control: 'select',
69
+ options: [
70
+ 'text',
71
+ 'connecting',
72
+ 'listening',
73
+ 'user-speaking',
74
+ 'thinking',
75
+ 'answering',
76
+ 'mic-muted',
77
+ 'reconnecting',
78
+ ],
79
+ description:
80
+ '`text` is the regular composer. The voice modes restyle the box for a voice conversation: a grey pulsing border while `connecting`, `reconnecting` and `mic-muted`; blue border lights and the voice light above the box while `listening`, `user-speaking`, `thinking` and `answering`. See Chat/ChatPromptBox/Voice for the voice panel.',
81
+ table: { defaultValue: { summary: 'text' } },
82
+ },
67
83
  onMouseDown: {
68
84
  description:
69
85
  'Called on mouse down anywhere in the box. The box also focuses the input unless a button, link or field was pressed.',