@gemies/sdk 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1662 -0
- package/LICENSE +202 -0
- package/NOTICE +15 -0
- package/README.md +516 -2
- package/package.json +102 -4
- 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/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 +140 -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/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 +291 -0
- package/src/lib/copy-es.ts +287 -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/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 +49 -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 +81 -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,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A live voice call, mounted only while voice mode is on.
|
|
3
|
+
*
|
|
4
|
+
* Mounting starts the call; unmounting ends it (`useVoiceRoom` stops listening
|
|
5
|
+
* and speaking on unmount), so the screen only has to decide whether to render it.
|
|
6
|
+
*
|
|
7
|
+
* It also says what happened, in the chat's language (`labels`): a failure, a call moved to
|
|
8
|
+
* another device or cut by the server, the time limit near. A hang-up says nothing. Every
|
|
9
|
+
* surface that offers a call gets this one, so they cannot disagree.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useEffect, useRef } from 'react';
|
|
13
|
+
import { useVoiceRoom } from '../../hooks/useVoiceRoom';
|
|
14
|
+
import { toastNotice } from '../../hooks/useVoiceLauncher';
|
|
15
|
+
import { useAudioLevelMonitor } from '../../hooks/useAudioLevelMonitor';
|
|
16
|
+
import { useAudioLevels } from '../../hooks/useAudioLevels';
|
|
17
|
+
import { useLabels } from '../../lib/labels';
|
|
18
|
+
import { VoiceControls } from './VoiceControls';
|
|
19
|
+
import type { VoiceSessionProps } from '../../types';
|
|
20
|
+
|
|
21
|
+
export function VoiceSession({
|
|
22
|
+
apiUrl,
|
|
23
|
+
conversationId,
|
|
24
|
+
agentId,
|
|
25
|
+
locale,
|
|
26
|
+
voice,
|
|
27
|
+
onMessagesSaved,
|
|
28
|
+
onNotice = toastNotice,
|
|
29
|
+
onStateChange,
|
|
30
|
+
onEnd,
|
|
31
|
+
}: VoiceSessionProps) {
|
|
32
|
+
const labels = useLabels();
|
|
33
|
+
const {
|
|
34
|
+
room,
|
|
35
|
+
callState,
|
|
36
|
+
roomState,
|
|
37
|
+
agentState,
|
|
38
|
+
isMuted,
|
|
39
|
+
errorCode,
|
|
40
|
+
endedReason,
|
|
41
|
+
endingSoonMs,
|
|
42
|
+
messages,
|
|
43
|
+
isConnected,
|
|
44
|
+
connect,
|
|
45
|
+
toggleMute,
|
|
46
|
+
} = useVoiceRoom({
|
|
47
|
+
apiUrl,
|
|
48
|
+
...(conversationId === undefined ? {} : { conversationId }),
|
|
49
|
+
...(agentId === undefined ? {} : { agentId }),
|
|
50
|
+
...(locale === undefined ? {} : { lang: locale }),
|
|
51
|
+
...(voice === undefined ? {} : { voicePreference: voice }),
|
|
52
|
+
...(onMessagesSaved === undefined ? {} : { onMessagesSaved }),
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const { captureLevel, playbackLevel } = useAudioLevelMonitor(room, isConnected);
|
|
56
|
+
const { waveAmplitude } = useAudioLevels({
|
|
57
|
+
captureLevel,
|
|
58
|
+
playbackLevel,
|
|
59
|
+
agentState,
|
|
60
|
+
isConnected,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Dial out once per mount. The ref keeps a new `connect` identity from
|
|
64
|
+
// placing a second call on top of the live one.
|
|
65
|
+
const dialedRef = useRef(false);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
if (dialedRef.current) return;
|
|
68
|
+
dialedRef.current = true;
|
|
69
|
+
void connect();
|
|
70
|
+
}, [connect]);
|
|
71
|
+
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
onStateChange({ isConnected, agentState, waveAmplitude, messages });
|
|
74
|
+
}, [isConnected, agentState, waveAmplitude, messages, onStateChange]);
|
|
75
|
+
|
|
76
|
+
// The call is about to reach its time limit: said once, and shown in the bar.
|
|
77
|
+
const notice = endingSoonMs === null ? null : labels.voiceEndingSoon;
|
|
78
|
+
const warnedRef = useRef(false);
|
|
79
|
+
useEffect(() => {
|
|
80
|
+
if (notice === null || warnedRef.current) return;
|
|
81
|
+
warnedRef.current = true;
|
|
82
|
+
onNotice({ kind: 'warning', text: notice });
|
|
83
|
+
}, [notice, onNotice]);
|
|
84
|
+
|
|
85
|
+
// End the call on failure, or once a call we actually reached ends. The
|
|
86
|
+
// "reached" flag is what keeps the pre-connect `disconnected` state — which
|
|
87
|
+
// every session starts in — from ending the call before it begins.
|
|
88
|
+
const reachedRoomRef = useRef(false);
|
|
89
|
+
const finishedRef = useRef(false);
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (finishedRef.current) return;
|
|
92
|
+
if (roomState !== 'disconnected') reachedRoomRef.current = true;
|
|
93
|
+
if (errorCode !== null || roomState === 'error') {
|
|
94
|
+
finishedRef.current = true;
|
|
95
|
+
onNotice({ kind: 'error', text: labels.voiceError(errorCode ?? 'realtime-failed') });
|
|
96
|
+
onEnd();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (roomState === 'disconnected' && reachedRoomRef.current) {
|
|
100
|
+
finishedRef.current = true;
|
|
101
|
+
// Moved to another device, the time limit, the credits: information. A hang-up: nothing.
|
|
102
|
+
const text = endedReason === null ? null : labels.voiceEnded(endedReason);
|
|
103
|
+
if (text !== null) onNotice({ kind: 'info', text });
|
|
104
|
+
onEnd();
|
|
105
|
+
}
|
|
106
|
+
}, [errorCode, endedReason, roomState, onEnd, onNotice, labels]);
|
|
107
|
+
|
|
108
|
+
return (
|
|
109
|
+
<VoiceControls
|
|
110
|
+
roomState={roomState}
|
|
111
|
+
agentState={agentState}
|
|
112
|
+
isMuted={isMuted}
|
|
113
|
+
onToggleMute={toggleMute}
|
|
114
|
+
onEnd={onEnd}
|
|
115
|
+
notice={notice}
|
|
116
|
+
callState={callState}
|
|
117
|
+
labels={labels.voiceControls}
|
|
118
|
+
/>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A chat component with the voice call: `Component` drawn under `VoiceCallContext`, which hands
|
|
3
|
+
* it `GemieVoiceCall`. The root entry and `@gemies/sdk/ask` export their chat through this; the
|
|
4
|
+
* text-only entries export the components as they are.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { ComponentType } from 'react';
|
|
8
|
+
import { GemieVoiceCall } from './GemieVoiceCall';
|
|
9
|
+
import { VoiceCallContext } from './VoiceCallContext';
|
|
10
|
+
|
|
11
|
+
export function withVoiceCall<P extends object>(
|
|
12
|
+
Component: ComponentType<P>,
|
|
13
|
+
displayName: string,
|
|
14
|
+
): ComponentType<P> {
|
|
15
|
+
// A ref is a prop of a function component (React 19), so it reaches a `forwardRef` `Component`.
|
|
16
|
+
function WithVoiceCall(props: P) {
|
|
17
|
+
return (
|
|
18
|
+
<VoiceCallContext.Provider value={GemieVoiceCall}>
|
|
19
|
+
<Component {...props} />
|
|
20
|
+
</VoiceCallContext.Provider>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
WithVoiceCall.displayName = displayName;
|
|
24
|
+
return WithVoiceCall;
|
|
25
|
+
}
|
package/src/csp.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@gemies/sdk/csp` — the Content-Security-Policy the SDK needs from a host page, in the
|
|
3
|
+
* shape of `@oxy.so/core`'s `OxyCspExtensions`, so a host composes it into its own policy
|
|
4
|
+
* instead of copying Alia's origins by hand:
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { withGemiesSdkCsp } from '@gemies/sdk/csp';
|
|
8
|
+
* createOxySecurityHeaders({ csp: withGemiesSdkCsp(APP_CSP_EXTENSIONS) });
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* Every source is derived from `lib/origins.ts`, the values the SDK's own URLs start from, and a
|
|
12
|
+
* test fails if the SDK's source names an origin this does not declare. A leaf entry: no React,
|
|
13
|
+
* no React Native, safe to import from a server.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import type { OxyCspDirective, OxyCspExtensions } from '@oxy.so/core/server';
|
|
17
|
+
import { GEMIES_API_URL, GEMIES_WEB_ORIGIN } from './lib/origins';
|
|
18
|
+
import { voiceSessionUrl } from './lib/voice-protocol';
|
|
19
|
+
|
|
20
|
+
const api = new URL(GEMIES_API_URL).origin;
|
|
21
|
+
const voiceSocket = new URL(voiceSessionUrl(GEMIES_API_URL)).origin;
|
|
22
|
+
|
|
23
|
+
/** What the SDK loads, by directive, and why. */
|
|
24
|
+
export const GEMIES_SDK_CSP = Object.freeze({
|
|
25
|
+
// The API (chat, dictation, speech, the activity socket's polling) and its WebSocket (the
|
|
26
|
+
// voice call, socket.io); the avatar runtime fetches its WebAssembly and data from the web app.
|
|
27
|
+
connectSrc: Object.freeze([api, voiceSocket, GEMIES_WEB_ORIGIN]),
|
|
28
|
+
// The avatar runtime and the voice call's worklets are modules served by the web app; the
|
|
29
|
+
// runtime compiles WebAssembly (`'wasm-unsafe-eval'` allows that and nothing else: no eval).
|
|
30
|
+
scriptSrc: Object.freeze([GEMIES_WEB_ORIGIN, "'wasm-unsafe-eval'"]),
|
|
31
|
+
// Stored attachments come from the API; an attachment's preview before upload is a blob.
|
|
32
|
+
imgSrc: Object.freeze([api, 'blob:']),
|
|
33
|
+
// Read-aloud clips are played from the API.
|
|
34
|
+
mediaSrc: Object.freeze([api]),
|
|
35
|
+
} satisfies OxyCspExtensions);
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The host's own CSP extensions with the SDK's added: each directive is the union, the host's
|
|
39
|
+
* sources first. `@oxy.so/core` dedupes when it builds the policy.
|
|
40
|
+
*/
|
|
41
|
+
export function withGemiesSdkCsp(host: OxyCspExtensions = {}): OxyCspExtensions {
|
|
42
|
+
const merged: Partial<Record<OxyCspDirective, readonly string[]>> = { ...host };
|
|
43
|
+
for (const [directive, sources] of Object.entries(GEMIES_SDK_CSP) as [
|
|
44
|
+
OxyCspDirective,
|
|
45
|
+
readonly string[],
|
|
46
|
+
][]) {
|
|
47
|
+
merged[directive] = [...(host[directive] ?? []), ...sources];
|
|
48
|
+
}
|
|
49
|
+
return merged;
|
|
50
|
+
}
|
package/src/dictation.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @gemies/sdk/dictation — dictation's network half, without a component, a hook or a recorder
|
|
2
|
+
// `POST /v1/audio/transcriptions` (ADR 0017 §7) and the streaming dictation socket
|
|
3
|
+
// `GET /v1/audio/transcriptions/stream` (ADR 0024) as plain functions: an app that records audio
|
|
4
|
+
// its own way (Astro's browser shell) dictates from here and reaches no React, no native module
|
|
5
|
+
// and no Oxy client. `useDictation` and `GemieComposer` use the same batch request.
|
|
6
|
+
|
|
7
|
+
export {
|
|
8
|
+
DictationError,
|
|
9
|
+
dictationFailureCode,
|
|
10
|
+
MAX_DICTATION_MS,
|
|
11
|
+
normalizeAudioMediaType,
|
|
12
|
+
requestTranscription,
|
|
13
|
+
} from './lib/dictation';
|
|
14
|
+
export type {
|
|
15
|
+
DictationClip,
|
|
16
|
+
DictationErrorCode,
|
|
17
|
+
TranscriptionRequest,
|
|
18
|
+
TranscriptionResult,
|
|
19
|
+
} from './lib/dictation';
|
|
20
|
+
export type { AudioRequestAuthentication } from './lib/audio-request-auth';
|
|
21
|
+
export type { VoiceErrorCode } from './lib/speech-messages';
|
|
22
|
+
export {
|
|
23
|
+
DICTATION_PROTOCOL_VERSION,
|
|
24
|
+
DICTATION_SAMPLE_RATE,
|
|
25
|
+
DICTATION_STREAM_PATH,
|
|
26
|
+
dictationStreamUrl,
|
|
27
|
+
parseDictationServerEvent,
|
|
28
|
+
} from './lib/dictation-protocol';
|
|
29
|
+
export type {
|
|
30
|
+
DictationCloseReason,
|
|
31
|
+
DictationServerEvent,
|
|
32
|
+
DictationStreamErrorCode,
|
|
33
|
+
} from './lib/dictation-protocol';
|
|
34
|
+
export { openDictationStream } from './lib/dictation-stream';
|
|
35
|
+
export type {
|
|
36
|
+
DictationSocket,
|
|
37
|
+
DictationStream,
|
|
38
|
+
DictationStreamAuthentication,
|
|
39
|
+
DictationStreamOptions,
|
|
40
|
+
} from './lib/dictation-stream';
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
useSharedValue,
|
|
4
|
+
useDerivedValue,
|
|
5
|
+
useAnimatedReaction,
|
|
6
|
+
withTiming,
|
|
7
|
+
withRepeat,
|
|
8
|
+
withSequence,
|
|
9
|
+
cancelAnimation,
|
|
10
|
+
Easing,
|
|
11
|
+
type SharedValue,
|
|
12
|
+
} from 'react-native-reanimated';
|
|
13
|
+
import { useDictationStore } from './useDictation';
|
|
14
|
+
import { LEVEL_ATTACK_MS, LEVEL_RELEASE_MS } from '../lib/audio-level';
|
|
15
|
+
import type { AgentState } from '../types';
|
|
16
|
+
|
|
17
|
+
export type AmbientWaveMode = 'voice' | 'tts' | 'stt' | 'thinking' | 'idle';
|
|
18
|
+
|
|
19
|
+
export interface AmbientWaveVoice {
|
|
20
|
+
isActive: boolean;
|
|
21
|
+
isConnected: boolean;
|
|
22
|
+
agentState: AgentState;
|
|
23
|
+
waveAmplitude: SharedValue<number>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface UseAmbientWaveOptions {
|
|
27
|
+
/** Live voice-call amplitude + state (present only while a call is being set up / active). */
|
|
28
|
+
voice?: AmbientWaveVoice;
|
|
29
|
+
/** Whether TTS ("read aloud") is currently playing. */
|
|
30
|
+
isTTSPlaying: boolean;
|
|
31
|
+
/** Live "read aloud" playback level, 0..1 (glides to 0 when not playing). */
|
|
32
|
+
ttsWaveAmplitude: SharedValue<number>;
|
|
33
|
+
/** Text-chat generation in flight (thinking or streaming) — gentle wave motion. */
|
|
34
|
+
isGenerating?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface UseAmbientWaveResult {
|
|
38
|
+
/** Combined amplitude — max of idle breath, voice, TTS, and STT sources. */
|
|
39
|
+
waveAmplitude: SharedValue<number>;
|
|
40
|
+
/** Palette-driving state for the visualizer. */
|
|
41
|
+
agentState: AgentState;
|
|
42
|
+
/** Overlay opacity target — subtle at idle, prominent while there is speech. */
|
|
43
|
+
intensity: number;
|
|
44
|
+
/** Semantic label for the active source. */
|
|
45
|
+
mode: AmbientWaveMode;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const IDLE_INTENSITY = 0.13;
|
|
49
|
+
const THINKING_INTENSITY = 0.2;
|
|
50
|
+
const ACTIVE_INTENSITY = 0.35;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Combines every wave-amplitude source into one always-on ambient signal for a
|
|
54
|
+
* single persistent overlay. Idle chat breathes subtly; a voice call, TTS
|
|
55
|
+
* playback, or STT recording intensify it. Because every source glides to 0 when
|
|
56
|
+
* inactive, `max(idleBreath, voice, tts, stt)` gives seamless cross-mode handoffs
|
|
57
|
+
* with no remounts.
|
|
58
|
+
*
|
|
59
|
+
* All animation starts are imperative (via `useAnimatedReaction`) so they tick on
|
|
60
|
+
* reanimated-web; the combiner is a pure-read `useDerivedValue`. Effect-free — no
|
|
61
|
+
* React `useEffect`.
|
|
62
|
+
*/
|
|
63
|
+
export function useAmbientWave({
|
|
64
|
+
voice,
|
|
65
|
+
isTTSPlaying,
|
|
66
|
+
ttsWaveAmplitude,
|
|
67
|
+
isGenerating,
|
|
68
|
+
}: UseAmbientWaveOptions): UseAmbientWaveResult {
|
|
69
|
+
// ── Idle breath: subtle 0.04↔0.09 swell (~7s), started once on mount ──
|
|
70
|
+
const idleBreath = useSharedValue(0.04);
|
|
71
|
+
useAnimatedReaction(
|
|
72
|
+
() => true,
|
|
73
|
+
(_current, previous) => {
|
|
74
|
+
if (previous !== null) return; // fire only on the initial run
|
|
75
|
+
idleBreath.value = withRepeat(
|
|
76
|
+
withSequence(
|
|
77
|
+
withTiming(0.09, { duration: 3500, easing: Easing.inOut(Easing.sin) }),
|
|
78
|
+
withTiming(0.04, { duration: 3500, easing: Easing.inOut(Easing.sin) }),
|
|
79
|
+
),
|
|
80
|
+
-1,
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
[],
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// ── STT metering smoothing (floor 0.08, fast attack / slow decay, →0 on stop) ──
|
|
87
|
+
//
|
|
88
|
+
// The recorder publishes on a 100ms poll, so what makes this read as speech
|
|
89
|
+
// rather than as ten steps a second is `withTiming` interpolating between
|
|
90
|
+
// them. Read-aloud reaches the same place by a different mechanism — its
|
|
91
|
+
// player pushes buffers far too fast to route through React — off the same
|
|
92
|
+
// two constants, but NOT with the same curve: this is a fixed-duration ease
|
|
93
|
+
// that is at the target after `LEVEL_ATTACK_MS`, and `audio-level`'s is an
|
|
94
|
+
// exponential whose time constant that is, so 60ms puts it at 63% and full
|
|
95
|
+
// travel takes about three times as long. Tuning the constant moves both, by
|
|
96
|
+
// different amounts. They are shared because one number should tune one
|
|
97
|
+
// feel, not because the two are interchangeable.
|
|
98
|
+
const sttIsRecording = useDictationStore((s) => s.isRecording);
|
|
99
|
+
const sttMetering = useDictationStore((s) => s.metering);
|
|
100
|
+
const stt = useSharedValue(0);
|
|
101
|
+
useAnimatedReaction(
|
|
102
|
+
() => (sttIsRecording ? Math.max(0.08, sttMetering) : -1),
|
|
103
|
+
(target) => {
|
|
104
|
+
if (target < 0) {
|
|
105
|
+
stt.value = withTiming(0, { duration: 300 });
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const duration = target > stt.value ? LEVEL_ATTACK_MS : LEVEL_RELEASE_MS;
|
|
109
|
+
stt.value = withTiming(target, { duration, easing: Easing.bezier(0.33, 1, 0.68, 1) });
|
|
110
|
+
},
|
|
111
|
+
[sttIsRecording, sttMetering],
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
// ── Thinking/generating motion (text chat, no voice) — gentle oscillation ──
|
|
115
|
+
const thinkingAmp = useSharedValue(0);
|
|
116
|
+
useAnimatedReaction(
|
|
117
|
+
() => isGenerating ?? false,
|
|
118
|
+
(generating, previous) => {
|
|
119
|
+
if (generating === previous) return;
|
|
120
|
+
if (generating) {
|
|
121
|
+
thinkingAmp.value = withRepeat(
|
|
122
|
+
withSequence(
|
|
123
|
+
withTiming(0.18, { duration: 1200, easing: Easing.inOut(Easing.sin) }),
|
|
124
|
+
withTiming(0.08, { duration: 1200, easing: Easing.inOut(Easing.sin) }),
|
|
125
|
+
),
|
|
126
|
+
-1,
|
|
127
|
+
);
|
|
128
|
+
} else {
|
|
129
|
+
cancelAnimation(thinkingAmp);
|
|
130
|
+
thinkingAmp.value = withTiming(0, { duration: 300 });
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
[isGenerating],
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
// ── Combined amplitude — pure reads only (ticks on web) ──
|
|
137
|
+
const voiceAmplitude = voice?.waveAmplitude;
|
|
138
|
+
const waveAmplitude = useDerivedValue(
|
|
139
|
+
() =>
|
|
140
|
+
Math.max(
|
|
141
|
+
idleBreath.value,
|
|
142
|
+
voiceAmplitude ? voiceAmplitude.value : 0,
|
|
143
|
+
ttsWaveAmplitude.value,
|
|
144
|
+
stt.value,
|
|
145
|
+
thinkingAmp.value,
|
|
146
|
+
),
|
|
147
|
+
[voiceAmplitude, ttsWaveAmplitude],
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
// ── Mode → palette state + overlay intensity (plain derived) ──
|
|
151
|
+
return useMemo<UseAmbientWaveResult>(() => {
|
|
152
|
+
if (voice?.isActive && voice.isConnected) {
|
|
153
|
+
return {
|
|
154
|
+
waveAmplitude,
|
|
155
|
+
agentState: voice.agentState,
|
|
156
|
+
intensity: ACTIVE_INTENSITY,
|
|
157
|
+
mode: 'voice',
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
if (isTTSPlaying) {
|
|
161
|
+
return { waveAmplitude, agentState: 'speaking', intensity: ACTIVE_INTENSITY, mode: 'tts' };
|
|
162
|
+
}
|
|
163
|
+
if (sttIsRecording) {
|
|
164
|
+
return { waveAmplitude, agentState: 'listening', intensity: ACTIVE_INTENSITY, mode: 'stt' };
|
|
165
|
+
}
|
|
166
|
+
if (isGenerating) {
|
|
167
|
+
return {
|
|
168
|
+
waveAmplitude,
|
|
169
|
+
agentState: 'thinking',
|
|
170
|
+
intensity: THINKING_INTENSITY,
|
|
171
|
+
mode: 'thinking',
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
return { waveAmplitude, agentState: 'idle', intensity: IDLE_INTENSITY, mode: 'idle' };
|
|
175
|
+
}, [
|
|
176
|
+
waveAmplitude,
|
|
177
|
+
voice?.isActive,
|
|
178
|
+
voice?.isConnected,
|
|
179
|
+
voice?.agentState,
|
|
180
|
+
isTTSPlaying,
|
|
181
|
+
sttIsRecording,
|
|
182
|
+
isGenerating,
|
|
183
|
+
]);
|
|
184
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
import type { NativeScrollEvent, NativeSyntheticEvent } from 'react-native';
|
|
3
|
+
|
|
4
|
+
/** How near the end still counts as being at the bottom, px. */
|
|
5
|
+
export const AT_BOTTOM_THRESHOLD = 50;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Whether the reader is at the end of the thread, for the jump-to-present
|
|
9
|
+
* button.
|
|
10
|
+
*
|
|
11
|
+
* ## What this replaced
|
|
12
|
+
*
|
|
13
|
+
* `use-scroll-to-bottom.ts`, 183 lines, which owned four things at once: the
|
|
14
|
+
* sticky follow, the upward pagination trigger, the anchor that held the
|
|
15
|
+
* reader's position while a page landed above them, and this. Bloom's
|
|
16
|
+
* `AiChatThread` owns the first three now — `followThreshold`,
|
|
17
|
+
* `onStartReached` and `maintainStartPosition`, added in 3.3.0 for exactly
|
|
18
|
+
* this adoption — and it implements them the same way, measuring the threshold
|
|
19
|
+
* against the height BEFORE the growth and adding a landed page's delta to the
|
|
20
|
+
* offset.
|
|
21
|
+
*
|
|
22
|
+
* What Bloom does NOT answer is this question, because it is the host's: a
|
|
23
|
+
* thread has no opinion about whether something should float over it offering
|
|
24
|
+
* to jump. So the one piece that did not move is the one that was never
|
|
25
|
+
* Bloom's.
|
|
26
|
+
*
|
|
27
|
+
* ## Why it is a ref plus a coarse boolean
|
|
28
|
+
*
|
|
29
|
+
* `onScroll` fires at 16ms while an answer streams, and this feeds a button
|
|
30
|
+
* that has exactly two appearances. Setting state on every event would
|
|
31
|
+
* re-render the whole screen sixty times a second to change nothing; the ref
|
|
32
|
+
* holds the live number and the state changes only when the ANSWER changes.
|
|
33
|
+
*/
|
|
34
|
+
export function useAtBottom(threshold: number = AT_BOTTOM_THRESHOLD) {
|
|
35
|
+
const [isAtBottom, setIsAtBottom] = useState(true);
|
|
36
|
+
const atBottom = useRef(true);
|
|
37
|
+
|
|
38
|
+
const onScroll = useCallback(
|
|
39
|
+
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
|
|
40
|
+
const { contentOffset, contentSize, layoutMeasurement } = event.nativeEvent;
|
|
41
|
+
const distance = contentSize.height - contentOffset.y - layoutMeasurement.height;
|
|
42
|
+
const next = distance <= threshold;
|
|
43
|
+
if (next === atBottom.current) return;
|
|
44
|
+
atBottom.current = next;
|
|
45
|
+
setIsAtBottom(next);
|
|
46
|
+
},
|
|
47
|
+
[threshold],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
return { isAtBottom, onScroll };
|
|
51
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { RiFileTextLine } from '@oxy.so/bloom/icons/RiFileTextLine';
|
|
2
|
+
import { RiImageLine } from '@oxy.so/bloom/icons/RiImageLine';
|
|
3
|
+
import { useCallback, useMemo } from 'react';
|
|
4
|
+
import type { GemieAddMenu } from '../lib/add-menu';
|
|
5
|
+
import { loadDocumentPicker } from '../lib/deferred/document-picker';
|
|
6
|
+
import type { IntakeKind } from '../lib/attachment-intake';
|
|
7
|
+
import { useLabels } from '../lib/labels';
|
|
8
|
+
import { GEMIE_FEATURES, type FeatureOffer } from '../runtime/agents';
|
|
9
|
+
import { type DraftAddress, useComposerDraftStore } from '../runtime/composer-draft-store';
|
|
10
|
+
import type { GemieAttachment } from '../types';
|
|
11
|
+
import { useImagePicker } from './useImagePicker';
|
|
12
|
+
|
|
13
|
+
/** What the chat offers to attach: each attachment feature's offer for this lead and person. */
|
|
14
|
+
export interface AttachOffers {
|
|
15
|
+
images: FeatureOffer;
|
|
16
|
+
documents: FeatureOffer;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const attachmentId = (prefix: string) =>
|
|
20
|
+
`${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The add menu a host that brings none gets: photos and files, each only where the lead offers it
|
|
24
|
+
* (`featureOffer`). A row the lead's feature gate keeps locked is still shown, and pressing it
|
|
25
|
+
* hands the feature to the host's upsell (`onFeatureLocked`) instead of opening a picker.
|
|
26
|
+
*/
|
|
27
|
+
export function useAttachMenu(
|
|
28
|
+
address: DraftAddress,
|
|
29
|
+
offers: AttachOffers,
|
|
30
|
+
onFeatureLocked: ((key: string) => void) | undefined,
|
|
31
|
+
): GemieAddMenu & { kinds: readonly IntakeKind[] } {
|
|
32
|
+
const labels = useLabels();
|
|
33
|
+
const { pickImage } = useImagePicker();
|
|
34
|
+
|
|
35
|
+
const groups = useMemo(() => {
|
|
36
|
+
const rows = [
|
|
37
|
+
...(offers.images === 'absent'
|
|
38
|
+
? []
|
|
39
|
+
: [{ id: 'photos', label: labels.addPhotos, icon: RiImageLine }]),
|
|
40
|
+
...(offers.documents === 'absent'
|
|
41
|
+
? []
|
|
42
|
+
: [{ id: 'documents', label: labels.addDocuments, icon: RiFileTextLine }]),
|
|
43
|
+
];
|
|
44
|
+
return rows.length === 0 ? [] : [{ label: labels.addMenuGroup, rows }];
|
|
45
|
+
}, [offers.images, offers.documents, labels.addMenuGroup, labels.addPhotos, labels.addDocuments]);
|
|
46
|
+
|
|
47
|
+
/** Dropped and pasted files are held to the same offer: only an available kind is taken. */
|
|
48
|
+
const kinds = useMemo<IntakeKind[]>(
|
|
49
|
+
() => [
|
|
50
|
+
...(offers.images === 'available' ? (['image'] as const) : []),
|
|
51
|
+
...(offers.documents === 'available' ? (['document'] as const) : []),
|
|
52
|
+
],
|
|
53
|
+
[offers.images, offers.documents],
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const onSelect = useCallback(
|
|
57
|
+
async (rowId: string) => {
|
|
58
|
+
const kind = rowId === 'photos' ? 'images' : rowId === 'documents' ? 'documents' : null;
|
|
59
|
+
if (kind === null) return;
|
|
60
|
+
if (offers[kind] === 'locked') {
|
|
61
|
+
onFeatureLocked?.(GEMIE_FEATURES[kind]);
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const store = useComposerDraftStore.getState();
|
|
65
|
+
const picked: GemieAttachment[] =
|
|
66
|
+
kind === 'images'
|
|
67
|
+
? ((await pickImage()) ?? []).map((asset) => ({
|
|
68
|
+
id: attachmentId('img'),
|
|
69
|
+
type: 'image' as const,
|
|
70
|
+
name: asset.name,
|
|
71
|
+
uri: asset.uri,
|
|
72
|
+
size: asset.size,
|
|
73
|
+
mimeType: asset.mimeType,
|
|
74
|
+
}))
|
|
75
|
+
: // On native the picker loads with the first document: a chat that never attaches one never does.
|
|
76
|
+
((await (await loadDocumentPicker()).pickDocuments()) ?? []).map((asset) => ({
|
|
77
|
+
id: attachmentId('doc'),
|
|
78
|
+
type: 'document' as const,
|
|
79
|
+
name: asset.name,
|
|
80
|
+
uri: asset.uri,
|
|
81
|
+
size: asset.size,
|
|
82
|
+
mimeType: asset.mimeType,
|
|
83
|
+
}));
|
|
84
|
+
for (const attachment of picked) store.addAttachment(address, attachment);
|
|
85
|
+
},
|
|
86
|
+
[offers, onFeatureLocked, pickImage, address],
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return useMemo(
|
|
90
|
+
() => ({ groups, kinds, onSelect: (rowId: string) => void onSelect(rowId) }),
|
|
91
|
+
[groups, kinds, onSelect],
|
|
92
|
+
);
|
|
93
|
+
}
|