@guideai/react-native 0.2.0 → 0.6.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.
@@ -0,0 +1,407 @@
1
+ import React from 'react';
2
+
3
+ /** A picture drawn for an answer: a PNG to show, and the SVG it was drawn as. */
4
+ interface HeadlessImage {
5
+ url: string;
6
+ svg: string | null;
7
+ alt: string;
8
+ width: number;
9
+ height: number;
10
+ }
11
+
12
+ interface GuideAIMessage {
13
+ id: string;
14
+ role: 'user' | 'assistant';
15
+ content: string;
16
+ /** When the message was sent or the answer started (ms since epoch). */
17
+ createdAt: number;
18
+ /** On an answer: the user message it replied to. */
19
+ question?: string;
20
+ /** True while tokens are still arriving for this message. */
21
+ streaming?: boolean;
22
+ error?: boolean;
23
+ /**
24
+ * Follow-up suggestions to draw as chips. A tap is just
25
+ * `send(suggestion)`. The site's own when the agent offered some,
26
+ * otherwise the `followUps` the app configured.
27
+ */
28
+ suggestions?: string[];
29
+ /**
30
+ * This answer can be explained on the whiteboard (the Blueprint has the
31
+ * block, and the answer is finished). Draw a button that calls
32
+ * `explainOnWhiteboard(id)`.
33
+ */
34
+ whiteboard?: boolean;
35
+ /**
36
+ * Pictures the agent drew. They are also in `content` as Markdown
37
+ * images where they belong; use this list to place them yourself.
38
+ */
39
+ images?: HeadlessImage[];
40
+ /**
41
+ * A change to the user's data waiting for their yes ("Create the
42
+ * course"). Draw Approve / Cancel and call `confirm(id)` or `cancel(id)`.
43
+ */
44
+ pendingAction?: {
45
+ label: string;
46
+ tool: string;
47
+ };
48
+ }
49
+ /** A recording the host made (expo-audio or any recorder) and saved to a file. */
50
+ interface GuideAIVoiceRecording {
51
+ /** `file://…` URI of the finished recording. */
52
+ uri: string;
53
+ /** e.g. 'audio/m4a'. Omitted: guessed from the file extension. */
54
+ mimeType?: string;
55
+ /** BCP-47 language the user spoke, e.g. 'en-NG'. Defaults to 'en-US'. */
56
+ languageCode?: string;
57
+ }
58
+ interface GuideAIVoiceResult {
59
+ /** What was heard, already sent as the next message; null when nothing was. */
60
+ text: string | null;
61
+ /** The answer to it, as shown (Markdown); null when the turn failed. */
62
+ answer?: string | null;
63
+ /** Why nothing was sent, in words to show the user. */
64
+ error?: string;
65
+ }
66
+ /**
67
+ * Everything a custom chat UI needs, and nothing about how to draw it.
68
+ *
69
+ * A host owns two things — their design, and the text the user typed.
70
+ * This hook owns the rest: the conversation, the streaming, the tool
71
+ * calls, the transport. That split is what lets the agent be upgraded
72
+ * underneath a host's UI without them changing a line.
73
+ */
74
+ interface UseGuideAIResult {
75
+ /**
76
+ * The conversation so far, oldest first, ready to draw. An answer that
77
+ * has not produced any text yet is left out; `typing` covers it.
78
+ */
79
+ messages: GuideAIMessage[];
80
+ /** An answer is on its way but has no text yet: draw a typing indicator. */
81
+ typing: boolean;
82
+ /**
83
+ * Chips to draw under the latest answer: its suggestions, or the app's
84
+ * `followUps`. Empty while a turn runs and before the first answer.
85
+ */
86
+ followUps: string[];
87
+ /** The Blueprint has the whiteboard block. */
88
+ whiteboardAvailable: boolean;
89
+ /**
90
+ * Explain an answer on the whiteboard, with the question it answered.
91
+ * Mount `<GuideAIWhiteboard />` once to show the board.
92
+ */
93
+ explainOnWhiteboard(messageId: string): Promise<boolean>;
94
+ /**
95
+ * The current input text, and its setter.
96
+ *
97
+ * Managed here so a host binds it straight to their TextInput rather
98
+ * than reimplementing "clear on send, block while busy" — the two
99
+ * rules every chat UI needs and each one gets subtly wrong.
100
+ */
101
+ input: string;
102
+ setInput(text: string): void;
103
+ /**
104
+ * Send `input`, or an explicit string.
105
+ *
106
+ * Ignores empty text and calls made while a turn is in flight, so a
107
+ * host does not have to guard their send button.
108
+ */
109
+ send(question?: string): Promise<string | null>;
110
+ /** True from send() until the turn settles. Drives a disabled state. */
111
+ busy: boolean;
112
+ /** Whether `send()` would do anything right now. */
113
+ canSend: boolean;
114
+ /** Status text from the agent loop ("Thinking", "Reading cart"). */
115
+ status: string | null;
116
+ /** Re-send the last user message. Useful after a failed turn. */
117
+ retry(): Promise<void>;
118
+ /** Start a new conversation. The current one stays in the chat history (useConversations). */
119
+ clear(): void;
120
+ /** Approve the message's `pendingAction`; the agent carries it out. */
121
+ confirm(messageId: string): Promise<void>;
122
+ /** Decline the message's `pendingAction`. Nothing is changed. */
123
+ cancel(messageId: string): void;
124
+ /**
125
+ * Push-to-talk: transcribe a recording and send it as the next message.
126
+ * Recording is the host's (no native module here); this uploads it.
127
+ */
128
+ sendVoice(recording: GuideAIVoiceRecording): Promise<GuideAIVoiceResult>;
129
+ /** True while a recording is being transcribed. */
130
+ transcribing: boolean;
131
+ }
132
+
133
+ /** A language the user can speak in, as the picker shows it. */
134
+ interface VoiceLanguage {
135
+ /** Code the recognizer takes, e.g. 'en', 'fr', 'yo'. */
136
+ code: string;
137
+ /** Its name in that language, e.g. 'Français'. */
138
+ label: string;
139
+ }
140
+ /** Offered when the app gives no list. */
141
+ declare const DEFAULT_VOICE_LANGUAGES: VoiceLanguage[];
142
+ interface VoiceLanguageOptions {
143
+ /** The language spoken, fixed by the app. Given, the user is never asked. */
144
+ languageCode?: string;
145
+ /** Languages the picker offers. Defaults to DEFAULT_VOICE_LANGUAGES. */
146
+ languages?: VoiceLanguage[];
147
+ }
148
+ interface VoiceLanguageFields {
149
+ /** The language the user speaks; null until they chose one. */
150
+ language: VoiceLanguage | null;
151
+ /** The languages to offer. */
152
+ languages: VoiceLanguage[];
153
+ /**
154
+ * Show the language picker (one for the app): the mic or live voice was
155
+ * started before a language was chosen, or `changeLanguage()` was called.
156
+ * Draw `languages`; a tap calls `chooseLanguage(code)`.
157
+ */
158
+ choosingLanguage: boolean;
159
+ /** Pick the language (remembered). Whatever asked for it then starts. */
160
+ chooseLanguage(code: string): Promise<void>;
161
+ /** Open the picker to change the language. */
162
+ changeLanguage(): void;
163
+ /** Close the picker without choosing. */
164
+ cancelLanguage(): void;
165
+ }
166
+
167
+ interface UseVoiceInputOptions extends VoiceLanguageOptions {
168
+ /** A recording stops and is sent after this long. Default 60 s. */
169
+ maxSeconds?: number;
170
+ }
171
+ interface UseVoiceInputResult extends VoiceLanguageFields {
172
+ /** The mic is on. */
173
+ recording: boolean;
174
+ /** What was said is being turned into text (then sent). */
175
+ transcribing: boolean;
176
+ /** Why the last attempt failed, in words to show the user; null when it didn't. */
177
+ error: string | null;
178
+ /** Start recording. False when the microphone was refused or is busy. */
179
+ start(): Promise<boolean>;
180
+ /** Stop, transcribe and send. Resolves with what was heard, or null. */
181
+ stop(): Promise<string | null>;
182
+ /** Stop and throw the recording away. */
183
+ cancel(): Promise<void>;
184
+ /** Start when idle, stop and send when recording: one mic button. */
185
+ toggle(): Promise<void>;
186
+ }
187
+ /**
188
+ * Push-to-talk for a chat from `useGuideAI()`:
189
+ *
190
+ * const chat = useGuideAI();
191
+ * const mic = useVoiceInput(chat);
192
+ * <Button onPress={mic.toggle} title={mic.recording ? 'Send' : 'Talk'} />
193
+ */
194
+ declare function useVoiceInput(chat: Pick<UseGuideAIResult, 'sendVoice' | 'transcribing' | 'busy'>, options?: UseVoiceInputOptions): UseVoiceInputResult;
195
+
196
+ /** Something that says a line aloud. `say` resolves when it finishes: true if it was heard. */
197
+ interface GuideAISpeech {
198
+ say(text: string): Promise<boolean>;
199
+ stop(): void;
200
+ }
201
+
202
+ /**
203
+ * GuideAI's natural voice, played on the device. `say` takes any length:
204
+ * it is cut into lines the endpoint takes, and the next line is fetched
205
+ * while the current one plays.
206
+ */
207
+ declare function createSpeech(): GuideAISpeech;
208
+
209
+ type LiveVoiceState =
210
+ /** Not in a call. */
211
+ 'off'
212
+ /** Waiting for the user to speak. */
213
+ | 'listening'
214
+ /** The user is speaking. */
215
+ | 'hearing'
216
+ /** What they said is being understood and answered. */
217
+ | 'thinking'
218
+ /** The answer is being read aloud. */
219
+ | 'speaking';
220
+ interface UseLiveVoiceOptions extends VoiceLanguageOptions {
221
+ /** A call with nobody speaking ends after this long. Default 30 s. */
222
+ idleSeconds?: number;
223
+ /** Silence that ends what the user is saying. Default 1.2 s. */
224
+ pauseSeconds?: number;
225
+ }
226
+ interface UseLiveVoiceResult extends VoiceLanguageFields {
227
+ state: LiveVoiceState;
228
+ /** In a call (any state but 'off'). */
229
+ active: boolean;
230
+ /** Mic level 0–1 while listening, for a pulsing button. */
231
+ level: number;
232
+ /** Why the call stopped or a turn failed, in words to show; null otherwise. */
233
+ error: string | null;
234
+ /** Start the call. False when the microphone was refused or no language is chosen yet. */
235
+ start(): Promise<boolean>;
236
+ /** End the call. */
237
+ stop(): void;
238
+ /** Cut the answer short and listen again. */
239
+ interrupt(): void;
240
+ }
241
+ /**
242
+ * A live voice call for a chat from `useGuideAI()`:
243
+ *
244
+ * const chat = useGuideAI();
245
+ * const live = useLiveVoice(chat);
246
+ * <Button onPress={live.active ? live.stop : live.start} title={live.active ? 'End' : 'Talk'} />
247
+ */
248
+ declare function useLiveVoice(chat: Pick<UseGuideAIResult, 'sendVoice' | 'busy' | 'transcribing'>, options?: UseLiveVoiceOptions): UseLiveVoiceResult;
249
+
250
+ /** The answer as plain words to say. */
251
+ declare function toSpeakable(markdown: string): string;
252
+
253
+ interface GuideAITheme {
254
+ /** Page background behind the conversation. */
255
+ background: string;
256
+ /** Secondary surfaces: the composer pill, assistant bubbles. */
257
+ surface: string;
258
+ /**
259
+ * An OPAQUE surface, for anything floating over app content — the
260
+ * nudge tooltip. `surface` is translucent by design, which would let
261
+ * the page show through the text behind it.
262
+ */
263
+ surfaceRaised: string;
264
+ /** Primary text. */
265
+ text: string;
266
+ /** Placeholder, status text, timestamps. */
267
+ textMuted: string;
268
+ /** Send button, links, list markers, the avatar glow. */
269
+ accent: string;
270
+ /** Text drawn on top of `accent`. */
271
+ onAccent: string;
272
+ /** The user's own message bubble. */
273
+ userBubble: string;
274
+ /** Text inside the user's bubble. */
275
+ onUserBubble: string;
276
+ /** Failed-message background. */
277
+ error: string;
278
+ /** Hairlines and dividers. */
279
+ border: string;
280
+ /** Corner radius for the composer and bubbles. */
281
+ radius: number;
282
+ /** Base body size; headings and code derive from it. */
283
+ fontSize: number;
284
+ }
285
+ /** A host's overrides, merged onto a base. */
286
+ type GuideAIThemeOverrides = Partial<GuideAITheme>;
287
+
288
+ interface GuideAIChatProps {
289
+ /** Colours and radii. Partial — unset tokens keep their defaults. */
290
+ theme?: GuideAIThemeOverrides;
291
+ placeholder?: string;
292
+ /** Greeting shown before the first message. */
293
+ greeting?: string;
294
+ /** Replaces the whole pre-conversation state, greeting included. */
295
+ emptyState?: React.ReactNode;
296
+ /**
297
+ * Show the mic (push-to-talk) and live voice buttons. They work in
298
+ * <GuideAIVoiceChat> from '@guideai/react-native/voice'; here, without
299
+ * voice, nothing is shown.
300
+ */
301
+ showMic?: boolean;
302
+ /** Show the chat history button (previous conversations, new chat). Default true. */
303
+ showHistory?: boolean;
304
+ /**
305
+ * Voice, wired by <GuideAIVoiceChat>. Called as a hook with this chat.
306
+ * Must not change between renders.
307
+ */
308
+ useVoice?: (chat: UseGuideAIResult) => GuideAIChatVoice;
309
+ /** Rendered at the left of the composer — an attach action, usually. */
310
+ leadingAccessory?: React.ReactNode;
311
+ /**
312
+ * Shown in the header beside the close button. Omitted, the header
313
+ * carries only the close control.
314
+ */
315
+ title?: string;
316
+ /**
317
+ * Called by the header's close button. Supplied automatically by
318
+ * <GuideAILauncher>; pass it yourself when embedding the chat in your
319
+ * own route.
320
+ *
321
+ * The header renders regardless — it carries the assistant's name and
322
+ * the Talk to a person action — but without this there is no close
323
+ * control, since an embedded chat that is a whole screen should be
324
+ * dismissed by the host's own navigation.
325
+ */
326
+ onClose?: () => void;
327
+ /**
328
+ * The visitor asked for a person.
329
+ *
330
+ * Mirrors the web SDK, where `handleRequestHuman()` closes the AI chat
331
+ * and dispatches `guideai:open-support` so the visitor ends up in one
332
+ * conversation rather than two. React Native has no support panel to
333
+ * hand off to, so the host supplies the destination — their own support
334
+ * route, Intercom, a phone dialler.
335
+ */
336
+ onTalkToHuman?: () => void;
337
+ /**
338
+ * Show the Talk to a person button without asking the backend.
339
+ *
340
+ * Left unset, the button appears only when the live-support service says
341
+ * a human is reachable — the same `support/public/status` check the web
342
+ * SDK's `refreshHelpSupport()` makes, so switching Live Support on in the
343
+ * dashboard takes effect without shipping a new build.
344
+ */
345
+ humanHandoverAvailable?: boolean;
346
+ /**
347
+ * Extra space above the navbar, for the status bar / notch.
348
+ *
349
+ * The sheet is presented `statusBarTranslucent` on Android, so its
350
+ * content starts at y=0 — underneath the clock and the notification
351
+ * icons. Defaulted rather than required because this package will not
352
+ * take a peer dependency on react-native-safe-area-context for one
353
+ * measurement; a host that already has it should pass the real inset:
354
+ *
355
+ * const { top } = useSafeAreaInsets();
356
+ * <GuideAIChat topInset={top} />
357
+ */
358
+ topInset?: number;
359
+ }
360
+ /** What <GuideAIVoiceChat> plugs in: push-to-talk and live voice. */
361
+ interface GuideAIChatVoice {
362
+ mic: UseVoiceInputResult;
363
+ live: UseLiveVoiceResult;
364
+ }
365
+
366
+ type GuideAIVoiceChatProps = Omit<GuideAIChatProps, 'useVoice'>;
367
+ declare function GuideAIVoiceChat(props: GuideAIVoiceChatProps): React.ReactElement;
368
+
369
+ /** Every word the panel shows, for translation. */
370
+ interface GuideAILiveVoiceLabels {
371
+ eyebrow: string;
372
+ listeningTitle: string;
373
+ listeningText: string;
374
+ hearingTitle: string;
375
+ hearingText: string;
376
+ thinkingTitle: string;
377
+ thinkingText: string;
378
+ speakingTitle: string;
379
+ speakingText: string;
380
+ end: string;
381
+ /** Before the language name: "Language" → "Language: English". */
382
+ language: string;
383
+ }
384
+ declare const DEFAULT_LIVE_VOICE_LABELS: GuideAILiveVoiceLabels;
385
+ interface GuideAILiveVoiceProps {
386
+ live: UseLiveVoiceResult;
387
+ theme?: GuideAIThemeOverrides;
388
+ labels?: Partial<GuideAILiveVoiceLabels>;
389
+ }
390
+ declare function GuideAILiveVoice({ live, theme: overrides, labels: given }: GuideAILiveVoiceProps): React.ReactElement | null;
391
+
392
+ interface GuideAILanguagePickerLabels {
393
+ title: string;
394
+ hint: string;
395
+ cancel: string;
396
+ }
397
+ declare const DEFAULT_LANGUAGE_PICKER_LABELS: GuideAILanguagePickerLabels;
398
+ interface GuideAILanguagePickerProps {
399
+ /** useVoiceInput() or useLiveVoice(): they share one choice and one picker. */
400
+ voice: VoiceLanguageFields;
401
+ theme?: GuideAIThemeOverrides;
402
+ labels?: Partial<GuideAILanguagePickerLabels>;
403
+ }
404
+ declare function GuideAILanguagePicker({ voice, theme: overrides, labels: given }: GuideAILanguagePickerProps): React.ReactElement | null;
405
+
406
+ export { DEFAULT_LANGUAGE_PICKER_LABELS, DEFAULT_LIVE_VOICE_LABELS, DEFAULT_VOICE_LANGUAGES, GuideAILanguagePicker, GuideAILiveVoice, GuideAIVoiceChat, createSpeech, toSpeakable, useLiveVoice, useVoiceInput };
407
+ export type { GuideAILanguagePickerLabels, GuideAILanguagePickerProps, GuideAILiveVoiceLabels, GuideAILiveVoiceProps, GuideAIVoiceChatProps, LiveVoiceState, UseLiveVoiceOptions, UseLiveVoiceResult, UseVoiceInputOptions, UseVoiceInputResult, VoiceLanguage };