@simfinity/constellation-react 0.0.1

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.
@@ -0,0 +1,261 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+ import { WebClientConfig, SessionStartParameters, SessionStats, EventHandlers, SessionConfig } from '@simfinity/constellation-client';
4
+
5
+ /**
6
+ * State machine
7
+ */
8
+ type AudioPlaybackState = {
9
+ playing: boolean;
10
+ };
11
+
12
+ /**
13
+ * Audio capturing settings.
14
+ * Includes a basic Voice-Activation-Detection implementation with minimum energy threshold
15
+ * and silence tracking to detect end-of-speech.
16
+ * Default values are provided and should be adequate in most typical setups.
17
+ */
18
+ declare class AudioCaptureConfig {
19
+ readonly sampleRate = 24000;
20
+ /**
21
+ * Minimum amount of energy for a noise to be considered as actual input and be recorded.
22
+ * - Below it, audio data is silently discarded
23
+ * - Above it, onAudioChunk events containing the audio stream will be fired
24
+ *
25
+ * @remarks
26
+ * This is the main parameter for noise-cancelling
27
+ */
28
+ silenceThreshold: number;
29
+ /**
30
+ * Once an input was detected, this is the duration of silence required before
31
+ * detecting the end-of-speech and triggering the onSilenceCommit event.
32
+ *
33
+ * IMPORTANT:
34
+ * Internally, duration is computed on audio chunks count, not system clock.
35
+ * A chunk is usually 8ms, therefore to see the effect of a silence duration change, it must be
36
+ * such that the number of chunks count changes e.g. going from 800ms to 810ms increases
37
+ * the chunk count by one.
38
+ *
39
+ * @remarks
40
+ * This is the main voice-activation-detection parameter.
41
+ */
42
+ silenceDurationMs: number;
43
+ /**
44
+ * Minimum input duration: below it, an input will not trigger an onSilenceCommit event.
45
+ *
46
+ * @remarks
47
+ * This is an important interruption parameter: it prevents very short inputs from
48
+ * interrupting an ongoing audio response.
49
+ */
50
+ minSpeechDurationMs: number;
51
+ }
52
+ /**
53
+ * Client callback functions for the different audio events, provided at start time.
54
+ */
55
+ type CaptureStartParams = {
56
+ /**
57
+ * Fired when a valid audio chunk containing sound was captured:
58
+ * This event only occurs when confirmed input voice was detected.
59
+ *
60
+ * @param base64Chunk
61
+ */
62
+ onAudioChunk: (base64Chunk: string) => void;
63
+ /**
64
+ * Fired after a silence period, following a confirmed input and marks the end of the input.
65
+ * This event necessarily occurs after at least one onAudioChunk (most likely many).
66
+ */
67
+ onSilenceCommit?: () => void;
68
+ /**
69
+ * Fired very early in the voice detection process. This event marks the beginning
70
+ * of an internal process to assess whether the sound captured should convert to an actual input.
71
+ */
72
+ onSpeechConsidered?: () => void;
73
+ /**
74
+ * Fired when speech is confirmed: marks the end of the process started when onSpeechConsidered was fired.
75
+ * The captured input has finally been determined to be real voice input.
76
+ * Upon receiving this event, it is not yet known what the input is or how long it will be, but there is
77
+ * a guarantee that an input audio message will be produced:
78
+ * - onAudioChunk events can be expected next
79
+ * - When working with conversational-interruptions, this is typically the right moment to trigger them
80
+ */
81
+ onSpeechConfirmed?: () => void;
82
+ };
83
+ /**
84
+ * State machine
85
+ */
86
+ type AudioCaptureState = {
87
+ recording: boolean;
88
+ speaking: boolean;
89
+ muted: boolean;
90
+ };
91
+
92
+ /**
93
+ * React context provider wrapping a Constellation WebClient instance.
94
+ *
95
+ * This component exposes session lifecycle state and client control
96
+ * functions to its descendant components.
97
+ *
98
+ * A ConstellationProvider MUST wrap any component that calls:
99
+ * - useConstellationClient()
100
+ * - useConstellationSession()
101
+ *
102
+ * @param client A pre-configured WebClient instance from "@simfinity/constellation-client".
103
+ * @param children The DOM descendant components
104
+ *
105
+ * @example
106
+ * ```tsx
107
+ * import WebClient from "@simfinity/constellation-client";
108
+ * import { ConstellationProvider } from "@simfinity/constellation-ui";
109
+ *
110
+ * const client = new WebClient({
111
+ * sessionEndpoint: "https://your-api-endpoint",
112
+ * streamingEndpoint: "wss://your-stream-endpoint",
113
+ * key: "YOUR_SECRET_KEY",
114
+ * });
115
+ *
116
+ * function App() {
117
+ * return (
118
+ * <ConstellationProvider client={client}>
119
+ * <Chat />
120
+ * </ConstellationProvider>
121
+ * );
122
+ * }
123
+ * ```
124
+ *
125
+ * @remarks
126
+ * This provider does NOT automatically start or join sessions.
127
+ * The lifecycle must be controlled explicitly via useConstellationClient().
128
+ */
129
+ declare function ConstellationProvider({ config, children, }: {
130
+ config: WebClientConfig;
131
+ children: React.ReactNode;
132
+ }): react_jsx_runtime.JSX.Element;
133
+
134
+ /**
135
+ * An instance of constellation client.
136
+ * If a constellation session is already started & in context, this client
137
+ * can resume interacting with it:
138
+ * Either stop it & start a new session, or keep it alive and re-join it.
139
+ */
140
+ interface ConstellationSession {
141
+ startSession: (params: SessionStartParameters) => Promise<void>;
142
+ endSession: () => Promise<SessionStats>;
143
+ joinSession: (audio: boolean, handlers: ConstellationEventHandlers) => Promise<void>;
144
+ configureSession: (settings: SessionConfig) => void;
145
+ sendText: (text: string) => void;
146
+ sendWhisper: (text: string) => void;
147
+ audioDevice: () => AudioDevice;
148
+ sessionId: string | null;
149
+ }
150
+ type ConstellationEventHandlers = Omit<EventHandlers, 'onAudioResponseChunk' | 'onAudioResponseEnd'>;
151
+ interface AudioDevice {
152
+ in: {
153
+ start: () => Promise<void>;
154
+ stop: () => Promise<void>;
155
+ setMuted: (muted: boolean) => void;
156
+ };
157
+ out: {};
158
+ }
159
+ /**
160
+ * Hook exposing all lifecycle control functions required to
161
+ * interact with a Constellation session.
162
+ *
163
+ * Internally uses the WebClient instance maintained in context by the ConstellationProvider.
164
+ *
165
+ * IMPORTANT SESSION ORDER:
166
+ *
167
+ * 1) startSession(params)
168
+ * → Creates a persistent server-side session (REST)
169
+ * → A session remains alive and can be re-joined within 5 minutes of inactivity
170
+ * → params.voiceEnabled true means this session will support both text and audio message exchange
171
+ *
172
+ * 2) joinSession(audioEnabled, handlers)
173
+ * → Opens WebSocket stream and begins event flow
174
+ * → audioEnabled (when joining an audioEnabled session) to join in voice mode
175
+ * → handlers are the callback functions to receive server events
176
+ *
177
+ * 3) configureSession(settings?) (optional)
178
+ * → Update of the settings affecting the LLM behaviour/context
179
+ * → This can be done at any time mid-stream
180
+ * → May not be supported by some LLMs
181
+ * → Can be provided initially in the startSession parameters
182
+ *
183
+ * 4) sendText(...) OR sendAudioChunk(...) + commitAudio()
184
+ * → Input-sending methods text & audio
185
+ *
186
+ * 5) endSession()
187
+ * → Closes the persistent server-side session
188
+ * → For a clean implementation the client should close the stream first
189
+ *
190
+ * This hook does NOT expose the underlying WebClient directly.
191
+ * It provides a controlled abstraction safe for React usage.
192
+ *
193
+ * @throws Error if used outside ConstellationProvider.
194
+ *
195
+ * @returns a SessionClient instance providing lifecycle control methods:
196
+ *
197
+ * startSession
198
+ * - Creates a new persistent session on the server.
199
+ * - Must be called before joinSession().
200
+ *
201
+ * joinSession
202
+ * - Opens the WebSocket stream.
203
+ * - Handlers are required to receive model responses.
204
+ *
205
+ * configureSession
206
+ * - Updates temperature, instructions, max tokens.
207
+ * - Does NOT trigger a response.
208
+ * - The instructions are the "system prompt" giving context to the model
209
+ *
210
+ * sendText
211
+ * - Sends a user text message.
212
+ * - Triggers model response.
213
+ *
214
+ * sendAudioChunk
215
+ * - Sends base64 PCM16 16k Hertz, audio chunk.
216
+ * - Accumulates until commitAudio() or silence detection.
217
+ *
218
+ * commitAudio
219
+ * - Forces model to process accumulated audio and generate a response.
220
+ *
221
+ * endSession
222
+ * - Closes session and frees server resources.
223
+ *
224
+ * @example
225
+ * ```tsx
226
+ * const client = useConstellationClient();
227
+ * const params: SessionStartParameters = {
228
+ * llmProvider: "openai",
229
+ * voiceEnabled: false,
230
+ * voiceName: "ballad",
231
+ * behaviour: {
232
+ * temperature: 0.9,
233
+ * instructions: "Just have a nice and casual conversation.",
234
+ * }
235
+ * }
236
+ * await client.startSession(params);
237
+ * await client.joinSession(false, {
238
+ * onStreamClosed: () => console.log,
239
+ * onTranscriptResponse: (text) => console.log(text),
240
+ * });
241
+ * client.sendText("Hello world");
242
+ * client.endSession();
243
+ * ```
244
+ */
245
+ declare function useConstellationSession(): ConstellationSession;
246
+
247
+ /**
248
+ * React hook to get UI reactive AudioPlayback state updates.
249
+ *
250
+ * @param selector
251
+ */
252
+ declare function useAudioPlaybackState<T>(selector: (state: AudioPlaybackState) => T): T;
253
+
254
+ /**
255
+ * React hook to subscribe to AudioCapture state updates.
256
+ *
257
+ * @param selector
258
+ */
259
+ declare function useAudioCaptureState<T>(selector: (state: AudioCaptureState) => T): T;
260
+
261
+ export { AudioCaptureConfig, type AudioCaptureState, type AudioPlaybackState, type CaptureStartParams, ConstellationProvider, type ConstellationSession, useAudioCaptureState, useAudioPlaybackState, useConstellationSession };
@@ -0,0 +1,261 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React from 'react';
3
+ import { WebClientConfig, SessionStartParameters, SessionStats, EventHandlers, SessionConfig } from '@simfinity/constellation-client';
4
+
5
+ /**
6
+ * State machine
7
+ */
8
+ type AudioPlaybackState = {
9
+ playing: boolean;
10
+ };
11
+
12
+ /**
13
+ * Audio capturing settings.
14
+ * Includes a basic Voice-Activation-Detection implementation with minimum energy threshold
15
+ * and silence tracking to detect end-of-speech.
16
+ * Default values are provided and should be adequate in most typical setups.
17
+ */
18
+ declare class AudioCaptureConfig {
19
+ readonly sampleRate = 24000;
20
+ /**
21
+ * Minimum amount of energy for a noise to be considered as actual input and be recorded.
22
+ * - Below it, audio data is silently discarded
23
+ * - Above it, onAudioChunk events containing the audio stream will be fired
24
+ *
25
+ * @remarks
26
+ * This is the main parameter for noise-cancelling
27
+ */
28
+ silenceThreshold: number;
29
+ /**
30
+ * Once an input was detected, this is the duration of silence required before
31
+ * detecting the end-of-speech and triggering the onSilenceCommit event.
32
+ *
33
+ * IMPORTANT:
34
+ * Internally, duration is computed on audio chunks count, not system clock.
35
+ * A chunk is usually 8ms, therefore to see the effect of a silence duration change, it must be
36
+ * such that the number of chunks count changes e.g. going from 800ms to 810ms increases
37
+ * the chunk count by one.
38
+ *
39
+ * @remarks
40
+ * This is the main voice-activation-detection parameter.
41
+ */
42
+ silenceDurationMs: number;
43
+ /**
44
+ * Minimum input duration: below it, an input will not trigger an onSilenceCommit event.
45
+ *
46
+ * @remarks
47
+ * This is an important interruption parameter: it prevents very short inputs from
48
+ * interrupting an ongoing audio response.
49
+ */
50
+ minSpeechDurationMs: number;
51
+ }
52
+ /**
53
+ * Client callback functions for the different audio events, provided at start time.
54
+ */
55
+ type CaptureStartParams = {
56
+ /**
57
+ * Fired when a valid audio chunk containing sound was captured:
58
+ * This event only occurs when confirmed input voice was detected.
59
+ *
60
+ * @param base64Chunk
61
+ */
62
+ onAudioChunk: (base64Chunk: string) => void;
63
+ /**
64
+ * Fired after a silence period, following a confirmed input and marks the end of the input.
65
+ * This event necessarily occurs after at least one onAudioChunk (most likely many).
66
+ */
67
+ onSilenceCommit?: () => void;
68
+ /**
69
+ * Fired very early in the voice detection process. This event marks the beginning
70
+ * of an internal process to assess whether the sound captured should convert to an actual input.
71
+ */
72
+ onSpeechConsidered?: () => void;
73
+ /**
74
+ * Fired when speech is confirmed: marks the end of the process started when onSpeechConsidered was fired.
75
+ * The captured input has finally been determined to be real voice input.
76
+ * Upon receiving this event, it is not yet known what the input is or how long it will be, but there is
77
+ * a guarantee that an input audio message will be produced:
78
+ * - onAudioChunk events can be expected next
79
+ * - When working with conversational-interruptions, this is typically the right moment to trigger them
80
+ */
81
+ onSpeechConfirmed?: () => void;
82
+ };
83
+ /**
84
+ * State machine
85
+ */
86
+ type AudioCaptureState = {
87
+ recording: boolean;
88
+ speaking: boolean;
89
+ muted: boolean;
90
+ };
91
+
92
+ /**
93
+ * React context provider wrapping a Constellation WebClient instance.
94
+ *
95
+ * This component exposes session lifecycle state and client control
96
+ * functions to its descendant components.
97
+ *
98
+ * A ConstellationProvider MUST wrap any component that calls:
99
+ * - useConstellationClient()
100
+ * - useConstellationSession()
101
+ *
102
+ * @param client A pre-configured WebClient instance from "@simfinity/constellation-client".
103
+ * @param children The DOM descendant components
104
+ *
105
+ * @example
106
+ * ```tsx
107
+ * import WebClient from "@simfinity/constellation-client";
108
+ * import { ConstellationProvider } from "@simfinity/constellation-ui";
109
+ *
110
+ * const client = new WebClient({
111
+ * sessionEndpoint: "https://your-api-endpoint",
112
+ * streamingEndpoint: "wss://your-stream-endpoint",
113
+ * key: "YOUR_SECRET_KEY",
114
+ * });
115
+ *
116
+ * function App() {
117
+ * return (
118
+ * <ConstellationProvider client={client}>
119
+ * <Chat />
120
+ * </ConstellationProvider>
121
+ * );
122
+ * }
123
+ * ```
124
+ *
125
+ * @remarks
126
+ * This provider does NOT automatically start or join sessions.
127
+ * The lifecycle must be controlled explicitly via useConstellationClient().
128
+ */
129
+ declare function ConstellationProvider({ config, children, }: {
130
+ config: WebClientConfig;
131
+ children: React.ReactNode;
132
+ }): react_jsx_runtime.JSX.Element;
133
+
134
+ /**
135
+ * An instance of constellation client.
136
+ * If a constellation session is already started & in context, this client
137
+ * can resume interacting with it:
138
+ * Either stop it & start a new session, or keep it alive and re-join it.
139
+ */
140
+ interface ConstellationSession {
141
+ startSession: (params: SessionStartParameters) => Promise<void>;
142
+ endSession: () => Promise<SessionStats>;
143
+ joinSession: (audio: boolean, handlers: ConstellationEventHandlers) => Promise<void>;
144
+ configureSession: (settings: SessionConfig) => void;
145
+ sendText: (text: string) => void;
146
+ sendWhisper: (text: string) => void;
147
+ audioDevice: () => AudioDevice;
148
+ sessionId: string | null;
149
+ }
150
+ type ConstellationEventHandlers = Omit<EventHandlers, 'onAudioResponseChunk' | 'onAudioResponseEnd'>;
151
+ interface AudioDevice {
152
+ in: {
153
+ start: () => Promise<void>;
154
+ stop: () => Promise<void>;
155
+ setMuted: (muted: boolean) => void;
156
+ };
157
+ out: {};
158
+ }
159
+ /**
160
+ * Hook exposing all lifecycle control functions required to
161
+ * interact with a Constellation session.
162
+ *
163
+ * Internally uses the WebClient instance maintained in context by the ConstellationProvider.
164
+ *
165
+ * IMPORTANT SESSION ORDER:
166
+ *
167
+ * 1) startSession(params)
168
+ * → Creates a persistent server-side session (REST)
169
+ * → A session remains alive and can be re-joined within 5 minutes of inactivity
170
+ * → params.voiceEnabled true means this session will support both text and audio message exchange
171
+ *
172
+ * 2) joinSession(audioEnabled, handlers)
173
+ * → Opens WebSocket stream and begins event flow
174
+ * → audioEnabled (when joining an audioEnabled session) to join in voice mode
175
+ * → handlers are the callback functions to receive server events
176
+ *
177
+ * 3) configureSession(settings?) (optional)
178
+ * → Update of the settings affecting the LLM behaviour/context
179
+ * → This can be done at any time mid-stream
180
+ * → May not be supported by some LLMs
181
+ * → Can be provided initially in the startSession parameters
182
+ *
183
+ * 4) sendText(...) OR sendAudioChunk(...) + commitAudio()
184
+ * → Input-sending methods text & audio
185
+ *
186
+ * 5) endSession()
187
+ * → Closes the persistent server-side session
188
+ * → For a clean implementation the client should close the stream first
189
+ *
190
+ * This hook does NOT expose the underlying WebClient directly.
191
+ * It provides a controlled abstraction safe for React usage.
192
+ *
193
+ * @throws Error if used outside ConstellationProvider.
194
+ *
195
+ * @returns a SessionClient instance providing lifecycle control methods:
196
+ *
197
+ * startSession
198
+ * - Creates a new persistent session on the server.
199
+ * - Must be called before joinSession().
200
+ *
201
+ * joinSession
202
+ * - Opens the WebSocket stream.
203
+ * - Handlers are required to receive model responses.
204
+ *
205
+ * configureSession
206
+ * - Updates temperature, instructions, max tokens.
207
+ * - Does NOT trigger a response.
208
+ * - The instructions are the "system prompt" giving context to the model
209
+ *
210
+ * sendText
211
+ * - Sends a user text message.
212
+ * - Triggers model response.
213
+ *
214
+ * sendAudioChunk
215
+ * - Sends base64 PCM16 16k Hertz, audio chunk.
216
+ * - Accumulates until commitAudio() or silence detection.
217
+ *
218
+ * commitAudio
219
+ * - Forces model to process accumulated audio and generate a response.
220
+ *
221
+ * endSession
222
+ * - Closes session and frees server resources.
223
+ *
224
+ * @example
225
+ * ```tsx
226
+ * const client = useConstellationClient();
227
+ * const params: SessionStartParameters = {
228
+ * llmProvider: "openai",
229
+ * voiceEnabled: false,
230
+ * voiceName: "ballad",
231
+ * behaviour: {
232
+ * temperature: 0.9,
233
+ * instructions: "Just have a nice and casual conversation.",
234
+ * }
235
+ * }
236
+ * await client.startSession(params);
237
+ * await client.joinSession(false, {
238
+ * onStreamClosed: () => console.log,
239
+ * onTranscriptResponse: (text) => console.log(text),
240
+ * });
241
+ * client.sendText("Hello world");
242
+ * client.endSession();
243
+ * ```
244
+ */
245
+ declare function useConstellationSession(): ConstellationSession;
246
+
247
+ /**
248
+ * React hook to get UI reactive AudioPlayback state updates.
249
+ *
250
+ * @param selector
251
+ */
252
+ declare function useAudioPlaybackState<T>(selector: (state: AudioPlaybackState) => T): T;
253
+
254
+ /**
255
+ * React hook to subscribe to AudioCapture state updates.
256
+ *
257
+ * @param selector
258
+ */
259
+ declare function useAudioCaptureState<T>(selector: (state: AudioCaptureState) => T): T;
260
+
261
+ export { AudioCaptureConfig, type AudioCaptureState, type AudioPlaybackState, type CaptureStartParams, ConstellationProvider, type ConstellationSession, useAudioCaptureState, useAudioPlaybackState, useConstellationSession };