@gemies/sdk 0.0.0-stage → 1.1.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.
- package/CHANGELOG.md +1678 -0
- package/LICENSE +202 -0
- package/NOTICE +15 -0
- package/README.md +516 -2
- package/package.json +105 -4
- package/src/accounts.ts +29 -0
- package/src/animations.ts +5 -0
- package/src/ask-eager.ts +8 -0
- package/src/ask-text.ts +11 -0
- package/src/ask.ts +12 -0
- package/src/avatar.ts +13 -0
- package/src/character.ts +1 -0
- package/src/components/AmbientField.tsx +580 -0
- package/src/components/ComposerDropZone.tsx +235 -0
- package/src/components/ComposerSuggestions.tsx +45 -0
- package/src/components/GemieAccounts.tsx +433 -0
- package/src/components/GemieAskCore.tsx +184 -0
- package/src/components/GemieAskProvider.tsx +18 -0
- package/src/components/GemieAskProviderLazy.tsx +27 -0
- package/src/components/GemieAskProviderText.tsx +25 -0
- package/src/components/GemieAskSheet.tsx +51 -0
- package/src/components/GemieAskSheetWithVoice.tsx +10 -0
- package/src/components/GemieAvatar.tsx +57 -0
- package/src/components/GemieAvatarIcon.tsx +35 -0
- package/src/components/GemieChatContent.tsx +900 -0
- package/src/components/GemieChatHeader.tsx +123 -0
- package/src/components/GemieChatScreen.tsx +170 -0
- package/src/components/GemieChatSheet.tsx +368 -0
- package/src/components/GemieChatWithVoice.tsx +13 -0
- package/src/components/GemieComposer.tsx +388 -0
- package/src/components/GemieGroupWelcome.tsx +37 -0
- package/src/components/GemieParticipants.tsx +133 -0
- package/src/components/GemieParticipantsHeader.tsx +27 -0
- package/src/components/GemieRecentConversations.tsx +65 -0
- package/src/components/GemieSharedConversation.tsx +87 -0
- package/src/components/GemieWelcomeMessage.tsx +58 -0
- package/src/components/IdentityMark.tsx +225 -0
- package/src/components/Image.tsx +12 -0
- package/src/components/InertWhen.tsx +48 -0
- package/src/components/PlanPreviewCard.tsx +53 -0
- package/src/components/QuestionCard.tsx +64 -0
- package/src/components/ReplyMarkdown.tsx +373 -0
- package/src/components/RichBlocks.tsx +195 -0
- package/src/components/VoiceModeIcon.tsx +18 -0
- package/src/components/canvas/CanvasComponent.tsx +65 -0
- package/src/components/canvas/ChartRenderer.tsx +73 -0
- package/src/components/canvas/CodeRenderer.tsx +20 -0
- package/src/components/canvas/DashboardRenderer.tsx +70 -0
- package/src/components/canvas/DeckRenderer.tsx +43 -0
- package/src/components/canvas/FormRenderer.tsx +101 -0
- package/src/components/canvas/MarkdownRenderer.tsx +14 -0
- package/src/components/canvas/TableRenderer.tsx +47 -0
- package/src/components/icons/RiEmojiPicker.tsx +39 -0
- package/src/components/icons/RiMoreDots.tsx +32 -0
- package/src/components/icons/RiReplyLine.tsx +25 -0
- package/src/components/thought/FilesAndSources.tsx +120 -0
- package/src/components/thought/ThoughtPanel.tsx +426 -0
- package/src/components/thought/ToolStep.tsx +134 -0
- package/src/components/thread/AgentExchangeView.tsx +112 -0
- package/src/components/thread/AgentResultCard.tsx +125 -0
- package/src/components/thread/AgentTaskCard.tsx +127 -0
- package/src/components/thread/ChatBubble.tsx +256 -0
- package/src/components/thread/GemieThread.tsx +455 -0
- package/src/components/thread/MessageBlockBoundary.tsx +78 -0
- package/src/components/thread/NewConversationOffer.tsx +54 -0
- package/src/components/thread/ThreadRow.tsx +598 -0
- package/src/components/thread/ThreadSearch.tsx +130 -0
- package/src/components/thread/ThreadSkeleton.tsx +30 -0
- package/src/components/thread/ToolResultCard.tsx +254 -0
- package/src/components/thread/TurnMenu.tsx +54 -0
- package/src/components/thread/TurnStatusLine.tsx +34 -0
- package/src/components/voice/GemieVoiceCall.tsx +21 -0
- package/src/components/voice/GemieVoiceCall.web.tsx +17 -0
- package/src/components/voice/VoiceCallContext.ts +13 -0
- package/src/components/voice/VoiceControls.tsx +106 -0
- package/src/components/voice/VoiceSession.tsx +120 -0
- package/src/components/voice/withVoiceCall.tsx +25 -0
- package/src/csp.ts +50 -0
- package/src/dictation.ts +40 -0
- package/src/hooks/useAmbientWave.ts +184 -0
- package/src/hooks/useAtBottom.ts +51 -0
- package/src/hooks/useAttachMenu.ts +93 -0
- package/src/hooks/useAttachmentIntake.ts +401 -0
- package/src/hooks/useAudioLevelMonitor.ts +46 -0
- package/src/hooks/useAudioLevels.ts +51 -0
- package/src/hooks/useChatComposer.ts +309 -0
- package/src/hooks/useChatVoice.ts +129 -0
- package/src/hooks/useComposerSuggestions.ts +84 -0
- package/src/hooks/useDictation.ts +292 -0
- package/src/hooks/useDocumentPicker.ts +11 -0
- package/src/hooks/useFailedTurnToast.ts +43 -0
- package/src/hooks/useFocusWhenPresented.ts +41 -0
- package/src/hooks/useGemieActivity.ts +31 -0
- package/src/hooks/useImagePicker.ts +38 -0
- package/src/hooks/useMessageReactions.ts +119 -0
- package/src/hooks/useMessageVote.ts +50 -0
- package/src/hooks/useOpenThought.ts +79 -0
- package/src/hooks/usePendingQuestions.ts +19 -0
- package/src/hooks/useReadAloud.ts +92 -0
- package/src/hooks/useShareHandler.ts +24 -0
- package/src/hooks/useSoundEffects.ts +137 -0
- package/src/hooks/useTTS.ts +303 -0
- package/src/hooks/useThoughtScope.ts +59 -0
- package/src/hooks/useThreadRows.ts +157 -0
- package/src/hooks/useThreadView.ts +67 -0
- package/src/hooks/useTimelineWindow.ts +256 -0
- package/src/hooks/useVoiceLauncher.ts +180 -0
- package/src/hooks/useVoiceRoom.ts +212 -0
- package/src/index.ts +141 -0
- package/src/lib/activity-socket.ts +102 -0
- package/src/lib/activity-store.ts +53 -0
- package/src/lib/add-menu.ts +7 -0
- package/src/lib/agent-character.ts +28 -0
- package/src/lib/alia-character.ts +14 -0
- package/src/lib/alia-gemie.ts +6 -0
- package/src/lib/attachment-intake.ts +154 -0
- package/src/lib/attachment-tiles.ts +74 -0
- package/src/lib/audio-level.ts +130 -0
- package/src/lib/audio-request-auth.ts +4 -0
- package/src/lib/audio-session-binding.ts +27 -0
- package/src/lib/bubbles.tsx +16 -0
- package/src/lib/call-thread.ts +33 -0
- package/src/lib/canvas-result.ts +21 -0
- package/src/lib/catalogue-tool.ts +22 -0
- package/src/lib/citations.ts +192 -0
- package/src/lib/clipboard.ts +23 -0
- package/src/lib/content-utils.ts +46 -0
- package/src/lib/copy-en.ts +333 -0
- package/src/lib/copy-es.ts +330 -0
- package/src/lib/copy.ts +38 -0
- package/src/lib/deferred/document-picker.ts +2 -0
- package/src/lib/deferred/document-picker.web.ts +12 -0
- package/src/lib/deferred/expo-audio.ts +2 -0
- package/src/lib/deferred/expo-audio.web.ts +12 -0
- package/src/lib/deferred/image-data-url.ts +5 -0
- package/src/lib/deferred/image-data-url.web.ts +12 -0
- package/src/lib/dictation-protocol.ts +142 -0
- package/src/lib/dictation-recorder-types.ts +37 -0
- package/src/lib/dictation-recorder.native.ts +90 -0
- package/src/lib/dictation-recorder.ts +131 -0
- package/src/lib/dictation-stream.ts +238 -0
- package/src/lib/dictation.ts +216 -0
- package/src/lib/document-picker.ts +31 -0
- package/src/lib/domain.ts +12 -0
- package/src/lib/flights.ts +33 -0
- package/src/lib/focus-return.ts +66 -0
- package/src/lib/greeting.ts +15 -0
- package/src/lib/image-data-url.ts +35 -0
- package/src/lib/keyboard.native.tsx +25 -0
- package/src/lib/keyboard.tsx +55 -0
- package/src/lib/labels-es.ts +113 -0
- package/src/lib/labels.tsx +207 -0
- package/src/lib/origins.ts +28 -0
- package/src/lib/participants.ts +97 -0
- package/src/lib/pcm.ts +57 -0
- package/src/lib/power-level-options.ts +66 -0
- package/src/lib/questions.ts +92 -0
- package/src/lib/speech-language.ts +20 -0
- package/src/lib/speech-messages.ts +54 -0
- package/src/lib/speech-synthesis.ts +156 -0
- package/src/lib/suggestions.ts +53 -0
- package/src/lib/task-labels.ts +35 -0
- package/src/lib/text-input.ts +10 -0
- package/src/lib/tool-call-status.ts +28 -0
- package/src/lib/tool-icons.ts +36 -0
- package/src/lib/tool-registry.ts +71 -0
- package/src/lib/tool-renderers.tsx +32 -0
- package/src/lib/turn-state.ts +15 -0
- package/src/lib/utils.ts +35 -0
- package/src/lib/voice-audio-engine-types.ts +59 -0
- package/src/lib/voice-audio-engine.native.ts +232 -0
- package/src/lib/voice-audio-engine.ts +206 -0
- package/src/lib/voice-call-request.ts +49 -0
- package/src/lib/voice-call.ts +627 -0
- package/src/lib/voice-controls-labels.ts +31 -0
- package/src/lib/voice-levels.ts +64 -0
- package/src/lib/voice-protocol.ts +328 -0
- package/src/lib/voice-worklets.ts +19 -0
- package/src/lib/work-log.ts +370 -0
- package/src/metro/chart-importers.json +18 -0
- package/src/metro/granular-charts.js +3 -0
- package/src/metro/index.cjs +227 -0
- package/src/metro/index.d.cts +5 -0
- package/src/runtime/agents.ts +151 -0
- package/src/runtime/api.ts +144 -0
- package/src/runtime/app-assertion.ts +51 -0
- package/src/runtime/attachment-utils.ts +97 -0
- package/src/runtime/chat-turn-control.ts +74 -0
- package/src/runtime/composer-draft-store.ts +270 -0
- package/src/runtime/config.tsx +222 -0
- package/src/runtime/conversation-dates.ts +31 -0
- package/src/runtime/conversation-store.ts +49 -0
- package/src/runtime/conversations.ts +633 -0
- package/src/runtime/error-utils.ts +144 -0
- package/src/runtime/gemie-accounts.ts +182 -0
- package/src/runtime/host-tools.ts +290 -0
- package/src/runtime/index.ts +137 -0
- package/src/runtime/message-history.ts +124 -0
- package/src/runtime/message-reactions.ts +214 -0
- package/src/runtime/model.ts +39 -0
- package/src/runtime/oxy-files.ts +37 -0
- package/src/runtime/power-levels.ts +66 -0
- package/src/runtime/query-keys.ts +60 -0
- package/src/runtime/random-uuid.ts +25 -0
- package/src/runtime/reaction-cache.ts +45 -0
- package/src/runtime/room-events.ts +333 -0
- package/src/runtime/routes.ts +85 -0
- package/src/runtime/sharing.ts +217 -0
- package/src/runtime/sse-frame-reader.ts +88 -0
- package/src/runtime/stream-events.ts +154 -0
- package/src/runtime/stream-outcome.ts +21 -0
- package/src/runtime/thought-store.ts +103 -0
- package/src/runtime/thread/agent-activity.ts +261 -0
- package/src/runtime/thread/agent-exchange.ts +94 -0
- package/src/runtime/thread/automation-labels.ts +79 -0
- package/src/runtime/thread/conversation-export.ts +135 -0
- package/src/runtime/thread/message-days.ts +120 -0
- package/src/runtime/thread/message-processor.ts +93 -0
- package/src/runtime/thread/speaker-changes.ts +66 -0
- package/src/runtime/thread/task-utils.ts +11 -0
- package/src/runtime/thread/thought-utils.ts +750 -0
- package/src/runtime/thread/thread-history.ts +139 -0
- package/src/runtime/thread/timeline.ts +202 -0
- package/src/runtime/thread/title-tags.ts +8 -0
- package/src/runtime/thread/tool-cards.ts +387 -0
- package/src/runtime/turn-failure.ts +108 -0
- package/src/runtime/turn-preparation.ts +21 -0
- package/src/runtime/types.ts +172 -0
- package/src/runtime/usage-limit-error.ts +44 -0
- package/src/runtime/use-agent-activity.ts +80 -0
- package/src/runtime/use-conversation-stream.ts +1669 -0
- package/src/runtime/use-conversation-thread.ts +590 -0
- package/src/runtime/use-gemie-thread.ts +54 -0
- package/src/runtime/use-room-events.ts +75 -0
- package/src/runtime/use-start-conversation.ts +107 -0
- package/src/runtime/use-thread-history.ts +73 -0
- package/src/runtime/use-thread-search.ts +100 -0
- package/src/runtime/use-turn-edit.ts +99 -0
- package/src/text.ts +19 -0
- package/src/theme.ts +16 -0
- package/src/tools.ts +27 -0
- package/src/types.ts +161 -0
- package/src/voice-messages.ts +2 -0
- package/src/voice.ts +67 -0
- package/tsconfig.json +17 -0
- package/worklets/voice-processors.js +152 -0
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import type { AvatarFlights } from '@oxy.so/bloom/avatar-flight';
|
|
2
|
+
import { useOxy } from '@oxy.so/services/ui/client';
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
4
|
+
import type { TextInputKeyPressEvent } from 'react-native';
|
|
5
|
+
import type { SharedValue } from 'react-native-reanimated';
|
|
6
|
+
import { groupFirstSend } from '../components/GemieParticipants';
|
|
7
|
+
import { leadToProfile } from '../lib/flights';
|
|
8
|
+
import type { GemieLead, GemieParticipant } from '../lib/participants';
|
|
9
|
+
import type { TextInputHandle } from '../lib/text-input';
|
|
10
|
+
import { imageUriToDataUrl } from '../lib/image-data-url';
|
|
11
|
+
import {
|
|
12
|
+
useComposerDraft,
|
|
13
|
+
useComposerDraftStore,
|
|
14
|
+
useDraftAddress,
|
|
15
|
+
} from '../runtime/composer-draft-store';
|
|
16
|
+
import type { SendOptions } from '../runtime/types';
|
|
17
|
+
import type { ConversationThread } from '../runtime/use-conversation-thread';
|
|
18
|
+
import { useTurnEdit } from '../runtime/use-turn-edit';
|
|
19
|
+
import type { GemieAttachment, GemieSuggestion } from '../types';
|
|
20
|
+
import { useComposerSuggestions, useSuggestionQuery } from './useComposerSuggestions';
|
|
21
|
+
import { useFocusWhenPresented } from './useFocusWhenPresented';
|
|
22
|
+
|
|
23
|
+
/** A turn of the person's: the text, and the photos (`data:` or `https:` URLs) sent with it. */
|
|
24
|
+
export interface GemieTurn {
|
|
25
|
+
text: string;
|
|
26
|
+
images?: ReadonlyArray<string>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface UseChatComposerOptions {
|
|
30
|
+
conversationId: string | undefined;
|
|
31
|
+
thread: ConversationThread;
|
|
32
|
+
/** Nothing is stored: the first turn is sent without creating a conversation (ghost mode). */
|
|
33
|
+
ephemeral: boolean;
|
|
34
|
+
/** The composer is closed (the usage limit): nothing may be sent. */
|
|
35
|
+
closed: boolean;
|
|
36
|
+
/** A write is done in the present, so it ends a jump into the thread's past. */
|
|
37
|
+
onWrite: () => void;
|
|
38
|
+
/**
|
|
39
|
+
* Hands the first turn of an empty chat to the host instead of sending it here (a sheet opens
|
|
40
|
+
* the conversation in a new sheet over this one).
|
|
41
|
+
*/
|
|
42
|
+
onStart: ((turn: GemieTurn) => void) | undefined;
|
|
43
|
+
/** The avatar flights: the greeting leaves with the first turn. */
|
|
44
|
+
flights: AvatarFlights;
|
|
45
|
+
/** The members when the chat is a group: its first message flies all their avatars, not the lead's. */
|
|
46
|
+
group?: readonly GemieParticipant[];
|
|
47
|
+
/** Whose greeting it is: the lead's avatar is the one that flies. */
|
|
48
|
+
lead: GemieLead;
|
|
49
|
+
/** A turn sent once as the chat mounts: the conversation a sheet was opened for. */
|
|
50
|
+
initialTurn: GemieTurn | undefined;
|
|
51
|
+
/** The signed-out person sent a turn: the host asks them to sign in. */
|
|
52
|
+
onSignInRequired: (() => void) | undefined;
|
|
53
|
+
/** A refusal to say, with its title. */
|
|
54
|
+
reportError: (message: string, title?: string) => void;
|
|
55
|
+
/** The suggestions over the composer on an empty chat, and the host's side of finding them. */
|
|
56
|
+
welcomeSuggestions: GemieSuggestion[] | undefined;
|
|
57
|
+
suggestionMatches: GemieSuggestion[] | undefined;
|
|
58
|
+
onSuggestionQuery: ((query: string) => void) | undefined;
|
|
59
|
+
onSuggestionPicked: ((suggestion: GemieSuggestion) => void) | undefined;
|
|
60
|
+
/** The sheet this chat is in: the composer takes focus once it is up. */
|
|
61
|
+
presentedProgress: SharedValue<number> | undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A photo handed over as a `data:` URL, as the composer would hold it. */
|
|
65
|
+
function dataUrlAttachment(url: string, index: number): GemieAttachment {
|
|
66
|
+
const mimeType = /^data:([^;,]+)/.exec(url)?.[1] ?? 'image/jpeg';
|
|
67
|
+
return {
|
|
68
|
+
id: `turn-photo-${index}`,
|
|
69
|
+
type: 'image',
|
|
70
|
+
name: `photo-${index + 1}`,
|
|
71
|
+
uri: url,
|
|
72
|
+
size: 0,
|
|
73
|
+
mimeType,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Everything the person does with the composer: the draft they are writing (kept per conversation,
|
|
79
|
+
* shared with every other surface), sending it, rewriting a question already sent, answering
|
|
80
|
+
* Alia's questions, picking a suggestion, asking for a reply again.
|
|
81
|
+
*
|
|
82
|
+
* Sending is the one path every turn takes. The draft is cleared at once and handed back if the
|
|
83
|
+
* request fails; an edit replaces the question it rewrites; an empty chat may hand its first turn
|
|
84
|
+
* to the host; a signed-out person is sent to sign in with their draft kept.
|
|
85
|
+
*/
|
|
86
|
+
export function useChatComposer({
|
|
87
|
+
conversationId,
|
|
88
|
+
thread,
|
|
89
|
+
ephemeral,
|
|
90
|
+
closed,
|
|
91
|
+
onWrite,
|
|
92
|
+
onStart,
|
|
93
|
+
flights,
|
|
94
|
+
group,
|
|
95
|
+
lead,
|
|
96
|
+
initialTurn,
|
|
97
|
+
onSignInRequired,
|
|
98
|
+
reportError,
|
|
99
|
+
welcomeSuggestions,
|
|
100
|
+
suggestionMatches,
|
|
101
|
+
onSuggestionQuery,
|
|
102
|
+
onSuggestionPicked,
|
|
103
|
+
presentedProgress,
|
|
104
|
+
}: UseChatComposerOptions) {
|
|
105
|
+
const { isAuthenticated } = useOxy();
|
|
106
|
+
const {
|
|
107
|
+
messages,
|
|
108
|
+
isLoading,
|
|
109
|
+
sendMessage,
|
|
110
|
+
createNewConversation,
|
|
111
|
+
editMessage,
|
|
112
|
+
regenerateMessage,
|
|
113
|
+
turnOptionsOf,
|
|
114
|
+
} = thread;
|
|
115
|
+
|
|
116
|
+
// ── The draft ──
|
|
117
|
+
const target = conversationId ?? null;
|
|
118
|
+
const draft = useComposerDraft(target);
|
|
119
|
+
const address = useDraftAddress(target);
|
|
120
|
+
const photos = draft.attachments;
|
|
121
|
+
/** What a send carries beyond the text: the connector and skills chosen for this turn. */
|
|
122
|
+
const turnOptions = useMemo<SendOptions>(
|
|
123
|
+
() => ({ mcpServerId: draft.mcpServerId, skillNames: draft.skillNames }),
|
|
124
|
+
[draft.mcpServerId, draft.skillNames],
|
|
125
|
+
);
|
|
126
|
+
const setText = useCallback(
|
|
127
|
+
(text: string) => useComposerDraftStore.getState().setText(address, text),
|
|
128
|
+
[address],
|
|
129
|
+
);
|
|
130
|
+
const addAttachment = useCallback(
|
|
131
|
+
(attachment: GemieAttachment) =>
|
|
132
|
+
useComposerDraftStore.getState().addAttachment(address, attachment),
|
|
133
|
+
[address],
|
|
134
|
+
);
|
|
135
|
+
const removeAttachment = useCallback(
|
|
136
|
+
(id: string) => useComposerDraftStore.getState().removeAttachment(address, id),
|
|
137
|
+
[address],
|
|
138
|
+
);
|
|
139
|
+
const edit = useTurnEdit({ target, messages, turnOptionsOf, onEditMessage: editMessage });
|
|
140
|
+
const { editing } = edit;
|
|
141
|
+
|
|
142
|
+
// ── Sending ──
|
|
143
|
+
// A turn goes into the open conversation; with none open, it creates one and is sent from it —
|
|
144
|
+
// unless nothing is to be stored.
|
|
145
|
+
const sendTurn = useCallback(
|
|
146
|
+
(text: string, attachments?: GemieAttachment[], options?: SendOptions) =>
|
|
147
|
+
conversationId || ephemeral
|
|
148
|
+
? sendMessage(text, attachments, options)
|
|
149
|
+
: createNewConversation(text, attachments, options),
|
|
150
|
+
[conversationId, ephemeral, sendMessage, createNewConversation],
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
// The first turn of an empty chat goes to the host when it asked for it; every other turn is
|
|
154
|
+
// sent here.
|
|
155
|
+
const firstTurn = messages.length === 0;
|
|
156
|
+
/** What leaves the empty chat with its first message: a group's members, or the greeting's lead. */
|
|
157
|
+
const firstFlights = useMemo(
|
|
158
|
+
() => (group === undefined ? [leadToProfile(lead)] : groupFirstSend(group)),
|
|
159
|
+
[group, lead],
|
|
160
|
+
);
|
|
161
|
+
const startsElsewhere = onStart !== undefined && firstTurn;
|
|
162
|
+
const submitTurn = useCallback(
|
|
163
|
+
async (text: string, attachments: GemieAttachment[] = [], options?: SendOptions) => {
|
|
164
|
+
onWrite();
|
|
165
|
+
if (startsElsewhere) {
|
|
166
|
+
const images = await Promise.all(
|
|
167
|
+
attachments.map((photo) => imageUriToDataUrl(photo.uri, photo.mimeType)),
|
|
168
|
+
);
|
|
169
|
+
// The host's conversation sheet opens first so that the flight, drawn when it launches, is
|
|
170
|
+
// above it; this chat stays on the greeting, so the avatar can still be measured.
|
|
171
|
+
onStart({ text, ...(images.length > 0 ? { images } : {}) });
|
|
172
|
+
await flights.launch(firstFlights);
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
// The greeting leaves with the first turn: measure it before it does.
|
|
176
|
+
if (firstTurn) await flights.launch(firstFlights);
|
|
177
|
+
await sendTurn(text, attachments, options);
|
|
178
|
+
},
|
|
179
|
+
[startsElsewhere, onStart, firstTurn, flights, firstFlights, sendTurn, onWrite],
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
// The conversation this chat was opened for, sent once.
|
|
183
|
+
const initialSent = useRef(false);
|
|
184
|
+
useEffect(() => {
|
|
185
|
+
if (!initialTurn || initialSent.current) return;
|
|
186
|
+
initialSent.current = true;
|
|
187
|
+
void sendTurn(initialTurn.text, initialTurn.images?.map(dataUrlAttachment));
|
|
188
|
+
}, [initialTurn, sendTurn]);
|
|
189
|
+
|
|
190
|
+
const submit = useCallback(
|
|
191
|
+
async (value: string) => {
|
|
192
|
+
const text = value.trim();
|
|
193
|
+
if ((!text && photos.length === 0) || isLoading || closed) return;
|
|
194
|
+
// An edit rewrites the question's words; an attachment alone does not make a replacement.
|
|
195
|
+
if (editing !== undefined && !text) return;
|
|
196
|
+
// Signed out: the host asks for the sign-in instead of firing a request that would 401. The
|
|
197
|
+
// draft stays in the composer for after.
|
|
198
|
+
if (!isAuthenticated && onSignInRequired) {
|
|
199
|
+
onSignInRequired();
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const attachments = photos.length > 0 ? photos : undefined;
|
|
203
|
+
// Cleared optimistically: the send path restores text, attachments and the turn's connector
|
|
204
|
+
// and skills to this draft if the request fails.
|
|
205
|
+
useComposerDraftStore.getState().clear(address);
|
|
206
|
+
try {
|
|
207
|
+
if (editing !== undefined) await edit.submit(text, turnOptions, attachments);
|
|
208
|
+
else await submitTurn(text, attachments, turnOptions);
|
|
209
|
+
} catch (err) {
|
|
210
|
+
// Nothing was sent (the photos could not be read): keep the draft.
|
|
211
|
+
useComposerDraftStore.getState().restore(address, { text: value, attachments: photos });
|
|
212
|
+
reportError(err instanceof Error ? err.message : 'Could not attach the photos');
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
[
|
|
216
|
+
photos,
|
|
217
|
+
isLoading,
|
|
218
|
+
closed,
|
|
219
|
+
editing,
|
|
220
|
+
isAuthenticated,
|
|
221
|
+
onSignInRequired,
|
|
222
|
+
address,
|
|
223
|
+
edit,
|
|
224
|
+
turnOptions,
|
|
225
|
+
submitTurn,
|
|
226
|
+
reportError,
|
|
227
|
+
],
|
|
228
|
+
);
|
|
229
|
+
|
|
230
|
+
// A reply again, sent the way its question was (the composer's choice when it is unknown).
|
|
231
|
+
const latest = useRef({ regenerateMessage, turnOptions });
|
|
232
|
+
latest.current = { regenerateMessage, turnOptions };
|
|
233
|
+
const regenerate = useCallback((assistantMessageId: string) => {
|
|
234
|
+
const { regenerateMessage: again, turnOptions: fallback } = latest.current;
|
|
235
|
+
void again(assistantMessageId, fallback);
|
|
236
|
+
}, []);
|
|
237
|
+
|
|
238
|
+
// ── Suggestions over the composer on an empty chat ──
|
|
239
|
+
const query = useSuggestionQuery(draft.text, firstTurn && !closed);
|
|
240
|
+
useEffect(() => {
|
|
241
|
+
onSuggestionQuery?.(query);
|
|
242
|
+
}, [query, onSuggestionQuery]);
|
|
243
|
+
// Read through a ref: it is a prop of the suggestions' rows and the send path changes per token.
|
|
244
|
+
const submitRef = useRef(submit);
|
|
245
|
+
useEffect(() => {
|
|
246
|
+
submitRef.current = submit;
|
|
247
|
+
});
|
|
248
|
+
const pickSuggestion = useCallback(
|
|
249
|
+
(suggestion: GemieSuggestion) => {
|
|
250
|
+
onSuggestionPicked?.(suggestion);
|
|
251
|
+
// A template has blanks to fill, so it goes into the composer for the person to finish
|
|
252
|
+
// instead of being sent with its placeholders.
|
|
253
|
+
if (suggestion.template) setText(suggestion.text);
|
|
254
|
+
else void submitRef.current(suggestion.text);
|
|
255
|
+
},
|
|
256
|
+
[onSuggestionPicked, setText],
|
|
257
|
+
);
|
|
258
|
+
const suggestions = useComposerSuggestions({
|
|
259
|
+
draft: draft.text,
|
|
260
|
+
enabled: firstTurn && !closed,
|
|
261
|
+
welcome: welcomeSuggestions,
|
|
262
|
+
matches: suggestionMatches,
|
|
263
|
+
onPick: pickSuggestion,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
// ── The field: focus, and its keys ──
|
|
267
|
+
const inputRef = useRef<TextInputHandle | null>(null);
|
|
268
|
+
useFocusWhenPresented(presentedProgress, () => inputRef.current?.focus());
|
|
269
|
+
// Starting an edit puts the caret in the composer that now holds the question: the Edit press
|
|
270
|
+
// left focus on the row's button, so typing went nowhere and Escape could not reach the edit.
|
|
271
|
+
const editingId = editing?.messageId;
|
|
272
|
+
useEffect(() => {
|
|
273
|
+
if (editingId !== undefined) inputRef.current?.focus();
|
|
274
|
+
}, [editingId]);
|
|
275
|
+
const { onKeyPress: onSuggestionKeyPress } = suggestions;
|
|
276
|
+
const cancelEdit = edit.cancel;
|
|
277
|
+
const onKeyPress = useCallback(
|
|
278
|
+
(event: TextInputKeyPressEvent) => {
|
|
279
|
+
// Escape leaves an edit, as its Cancel does: it is the top-most thing open while the
|
|
280
|
+
// question is being rewritten.
|
|
281
|
+
if (editing !== undefined && event.nativeEvent.key === 'Escape') {
|
|
282
|
+
event.preventDefault();
|
|
283
|
+
cancelEdit();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
onSuggestionKeyPress(event);
|
|
287
|
+
},
|
|
288
|
+
[editing, cancelEdit, onSuggestionKeyPress],
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
text: draft.text,
|
|
293
|
+
setText,
|
|
294
|
+
photos,
|
|
295
|
+
addAttachment,
|
|
296
|
+
removeAttachment,
|
|
297
|
+
address,
|
|
298
|
+
turnOptions,
|
|
299
|
+
edit,
|
|
300
|
+
editing,
|
|
301
|
+
firstTurn,
|
|
302
|
+
sendTurn,
|
|
303
|
+
submit,
|
|
304
|
+
regenerate,
|
|
305
|
+
suggestions: { ...suggestions, onPick: pickSuggestion },
|
|
306
|
+
inputRef,
|
|
307
|
+
onKeyPress,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { useOxy } from '@oxy.so/services/ui/client';
|
|
2
|
+
import { useQueryClient } from '@tanstack/react-query';
|
|
3
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
4
|
+
import type { GemieAvatarState } from '../components/GemieAvatar';
|
|
5
|
+
import { callThread } from '../lib/call-thread';
|
|
6
|
+
import { turnActivityState } from '../lib/turn-state';
|
|
7
|
+
import { useGemieRuntime } from '../runtime/config';
|
|
8
|
+
import { gemieQueryKeys } from '../runtime/query-keys';
|
|
9
|
+
import type { Message } from '../runtime/types';
|
|
10
|
+
import { type SoundSources, useVoiceSoundEffects } from './useSoundEffects';
|
|
11
|
+
import { type UseVoiceLauncherOptions, useVoiceLauncher } from './useVoiceLauncher';
|
|
12
|
+
|
|
13
|
+
/** What a host tells the chat about its voice call (see {@link UseVoiceLauncherOptions}). */
|
|
14
|
+
export type GemieVoiceOptions = Omit<
|
|
15
|
+
UseVoiceLauncherOptions,
|
|
16
|
+
'apiUrl' | 'conversationId' | 'agentId' | 'accountId' | 'resolveConversation' | 'isFocused'
|
|
17
|
+
> & {
|
|
18
|
+
/** The cues played as the call thinks, uses tools, connects and ends. Omitted, the call is silent. */
|
|
19
|
+
sounds?: SoundSources;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export interface UseChatVoiceOptions {
|
|
23
|
+
/** The host's say: on by default, `false` for a text-only chat, an object for its callbacks. */
|
|
24
|
+
options: false | GemieVoiceOptions | undefined;
|
|
25
|
+
conversationId: string | undefined;
|
|
26
|
+
agentId: string | undefined;
|
|
27
|
+
/** Whether the screen holding the chat is the one on show. A live call ends when it is not. */
|
|
28
|
+
isFocused: boolean;
|
|
29
|
+
/** Makes the conversation a call lives in when none is open (after the gate). */
|
|
30
|
+
ensureConversation: () => Promise<string | undefined>;
|
|
31
|
+
messages: Message[];
|
|
32
|
+
setMessages: (messages: Message[]) => void;
|
|
33
|
+
/** The conversation's stored messages: a call message shows as stored once it is. */
|
|
34
|
+
storedMessages: readonly Message[] | undefined;
|
|
35
|
+
/** A text turn is in flight. */
|
|
36
|
+
isLoading: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The chat's voice call.
|
|
41
|
+
*
|
|
42
|
+
* The launcher owns whether a call is open, the gate before one opens and what it reports back
|
|
43
|
+
* (for the ambient wave, the cues and the transcript); the call itself loads on the first one.
|
|
44
|
+
* The transcript is merged into the thread: the text messages the chat held when the call
|
|
45
|
+
* started, then the call's, so the conversation reads as one. And what Alia is doing — the
|
|
46
|
+
* header's mark — follows the call while one is open and the text turn otherwise.
|
|
47
|
+
*/
|
|
48
|
+
export function useChatVoice({
|
|
49
|
+
options,
|
|
50
|
+
conversationId,
|
|
51
|
+
agentId,
|
|
52
|
+
isFocused,
|
|
53
|
+
ensureConversation,
|
|
54
|
+
messages,
|
|
55
|
+
setMessages,
|
|
56
|
+
storedMessages,
|
|
57
|
+
isLoading,
|
|
58
|
+
}: UseChatVoiceOptions) {
|
|
59
|
+
const runtime = useGemieRuntime();
|
|
60
|
+
const { user } = useOxy();
|
|
61
|
+
const queryClient = useQueryClient();
|
|
62
|
+
const enabled = options !== false;
|
|
63
|
+
const { sounds, ...launcherOptions }: GemieVoiceOptions =
|
|
64
|
+
options === false || options === undefined ? {} : options;
|
|
65
|
+
const { onCallEnded } = launcherOptions;
|
|
66
|
+
|
|
67
|
+
const refreshConversation = useCallback(
|
|
68
|
+
(id: string) => {
|
|
69
|
+
void queryClient.invalidateQueries({ queryKey: gemieQueryKeys.conversations.detail(id) });
|
|
70
|
+
void queryClient.invalidateQueries({ queryKey: gemieQueryKeys.conversations.all });
|
|
71
|
+
},
|
|
72
|
+
[queryClient],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const launcher = useVoiceLauncher({
|
|
76
|
+
apiUrl: runtime.apiUrl,
|
|
77
|
+
conversationId,
|
|
78
|
+
agentId,
|
|
79
|
+
accountId: user?.id ?? null,
|
|
80
|
+
locale: runtime.speech.language,
|
|
81
|
+
voice: runtime.speech.voice,
|
|
82
|
+
resolveConversation: ensureConversation,
|
|
83
|
+
onMessagesSaved: refreshConversation,
|
|
84
|
+
isFocused,
|
|
85
|
+
...launcherOptions,
|
|
86
|
+
// The call's transcript is in the thread, whatever else the host refreshes.
|
|
87
|
+
onCallEnded: () => {
|
|
88
|
+
if (conversationId !== undefined) refreshConversation(conversationId);
|
|
89
|
+
onCallEnded?.();
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
const { isActive, state, start } = launcher;
|
|
93
|
+
const agentState = state?.agentState;
|
|
94
|
+
|
|
95
|
+
useVoiceSoundEffects({
|
|
96
|
+
isVoiceActive: isActive,
|
|
97
|
+
agentState: agentState ?? 'idle',
|
|
98
|
+
isConnected: state?.isConnected ?? false,
|
|
99
|
+
sounds: sounds ?? {},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// The transcript, after the text messages the chat held when the call started.
|
|
103
|
+
const textBeforeCall = useRef<Message[]>([]);
|
|
104
|
+
const voiceMessages = state?.messages;
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
if (!voiceMessages) return;
|
|
107
|
+
setMessages(callThread(textBeforeCall.current, voiceMessages, storedMessages));
|
|
108
|
+
}, [voiceMessages, storedMessages, setMessages]);
|
|
109
|
+
|
|
110
|
+
const latestMessages = useRef(messages);
|
|
111
|
+
latestMessages.current = messages;
|
|
112
|
+
const activate = useCallback(() => {
|
|
113
|
+
if (isActive) return;
|
|
114
|
+
textBeforeCall.current = [...latestMessages.current];
|
|
115
|
+
void start();
|
|
116
|
+
}, [isActive, start]);
|
|
117
|
+
|
|
118
|
+
// What Alia is doing, which the header's mark shows.
|
|
119
|
+
const markState = useMemo<GemieAvatarState>(() => {
|
|
120
|
+
if (isActive) {
|
|
121
|
+
if (agentState === 'speaking') return 'writing';
|
|
122
|
+
if (agentState === 'thinking') return 'thinking';
|
|
123
|
+
return 'idle';
|
|
124
|
+
}
|
|
125
|
+
return turnActivityState(messages, isLoading);
|
|
126
|
+
}, [messages, isLoading, isActive, agentState]);
|
|
127
|
+
|
|
128
|
+
return { enabled, launcher, isActive, state, agentState, activate, markState };
|
|
129
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import type { TextInputKeyPressEvent } from 'react-native';
|
|
3
|
+
import {
|
|
4
|
+
type Completion,
|
|
5
|
+
SEARCH_DEBOUNCE_MS,
|
|
6
|
+
SEARCH_FROM,
|
|
7
|
+
composerCompletions,
|
|
8
|
+
} from '../lib/suggestions';
|
|
9
|
+
import type { GemieSuggestion } from '../types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The draft, once typing has settled, for the host to search with; empty while the draft is
|
|
13
|
+
* too short to search or suggestions are off. The data stays the host's: the Alia app asks its
|
|
14
|
+
* API through react-query, a host of the SDK chat asks whatever it has.
|
|
15
|
+
*/
|
|
16
|
+
export function useSuggestionQuery(draft: string, enabled: boolean): string {
|
|
17
|
+
const [query, setQuery] = useState('');
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const next = enabled ? draft.trim() : '';
|
|
20
|
+
if (next.length < SEARCH_FROM) {
|
|
21
|
+
setQuery('');
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const timer = setTimeout(() => setQuery(next), SEARCH_DEBOUNCE_MS);
|
|
25
|
+
return () => clearTimeout(timer);
|
|
26
|
+
}, [draft, enabled]);
|
|
27
|
+
return query;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The suggestions over a composer: the rows for the draft, which one the keyboard is on, and
|
|
32
|
+
* the composer's own `onKeyPress` (arrows move, Enter picks the selected row, Escape closes).
|
|
33
|
+
* Enter is left to the composer while no row is selected.
|
|
34
|
+
*/
|
|
35
|
+
export function useComposerSuggestions<T extends GemieSuggestion>({
|
|
36
|
+
draft,
|
|
37
|
+
enabled,
|
|
38
|
+
welcome,
|
|
39
|
+
matches,
|
|
40
|
+
onPick,
|
|
41
|
+
}: {
|
|
42
|
+
draft: string;
|
|
43
|
+
/** Only a fresh conversation offers suggestions. */
|
|
44
|
+
enabled: boolean;
|
|
45
|
+
welcome: readonly T[] | undefined;
|
|
46
|
+
matches: readonly T[] | undefined;
|
|
47
|
+
onPick: (suggestion: T) => void;
|
|
48
|
+
}) {
|
|
49
|
+
const [selected, setSelected] = useState(-1);
|
|
50
|
+
const [dismissed, setDismissed] = useState(false);
|
|
51
|
+
|
|
52
|
+
// A new draft reopens the list and clears the keyboard selection.
|
|
53
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: the draft is the trigger
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
setDismissed(false);
|
|
56
|
+
setSelected(-1);
|
|
57
|
+
}, [draft]);
|
|
58
|
+
|
|
59
|
+
const completions: Completion<T>[] = useMemo(
|
|
60
|
+
() => (enabled && !dismissed ? composerCompletions(draft, welcome, matches) : []),
|
|
61
|
+
[enabled, dismissed, draft, welcome, matches],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const onKeyPress = useCallback(
|
|
65
|
+
(event: TextInputKeyPressEvent) => {
|
|
66
|
+
if (completions.length === 0) return;
|
|
67
|
+
const key = event.nativeEvent.key;
|
|
68
|
+
if (key === 'ArrowDown' || key === 'ArrowUp') {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
const step = key === 'ArrowDown' ? 1 : -1;
|
|
71
|
+
setSelected((current) => (current + step + completions.length) % completions.length);
|
|
72
|
+
} else if (key === 'Enter' && selected >= 0) {
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
onPick(completions[selected]!.suggestion);
|
|
75
|
+
} else if (key === 'Escape') {
|
|
76
|
+
event.preventDefault();
|
|
77
|
+
setDismissed(true);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
[completions, selected, onPick],
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
return { completions, selected, onKeyPress };
|
|
84
|
+
}
|