@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,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The web voice-call engine: one `AudioContext`, the microphone through
|
|
3
|
+
* `getUserMedia` (echo cancellation, noise suppression, automatic gain), an
|
|
4
|
+
* `AudioWorklet` that turns it into 24 kHz PCM16 frames, and a second
|
|
5
|
+
* `AudioWorklet` that plays the answer from a jitter-buffered queue.
|
|
6
|
+
*
|
|
7
|
+
* Both worklets run in the same context, so the page's audio graph is one
|
|
8
|
+
* stack. Whether Chrome's echo canceller removes Web Audio playback (it is
|
|
9
|
+
* proven for audio played through an `RTCPeerConnection`) is a release gate of
|
|
10
|
+
* ADR 0017 §4, measured on real hardware; the fallback it names (routing
|
|
11
|
+
* playback through a local `RTCPeerConnection` loopback) is not built until the
|
|
12
|
+
* measurement says it is needed.
|
|
13
|
+
*
|
|
14
|
+
* The worklets are one module served by the Gemies web app (`./voice-worklets.ts`),
|
|
15
|
+
* never a `blob:` URL a host's CSP would have to allow as script. Their numbers
|
|
16
|
+
* (stream rate, frame size, prebuffer) are passed as `processorOptions`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { levelOfPcm16, pcm16ToFloat } from './pcm';
|
|
20
|
+
import { VOICE_FRAME_SAMPLES, VOICE_SAMPLE_RATE } from './voice-protocol';
|
|
21
|
+
import { VOICE_WORKLETS_URL } from './voice-worklets';
|
|
22
|
+
import {
|
|
23
|
+
VoiceAudioEngineError,
|
|
24
|
+
type VoiceAudioEngine,
|
|
25
|
+
type VoiceAudioEngineHandlers,
|
|
26
|
+
} from './voice-audio-engine-types';
|
|
27
|
+
|
|
28
|
+
export * from './voice-audio-engine-types';
|
|
29
|
+
|
|
30
|
+
/** Played before the first sample of a burst, so network jitter does not stutter it. */
|
|
31
|
+
const PREBUFFER_MS = 80;
|
|
32
|
+
/** A burst shorter than the prebuffer still plays once nothing more arrives for this long. */
|
|
33
|
+
const PREBUFFER_IDLE_MS = 100;
|
|
34
|
+
|
|
35
|
+
function deviceError(caught: unknown): VoiceAudioEngineError {
|
|
36
|
+
const name = (caught as { name?: unknown } | null)?.name;
|
|
37
|
+
if (name === 'NotAllowedError' || name === 'SecurityError' || name === 'PermissionDeniedError')
|
|
38
|
+
return new VoiceAudioEngineError('mic-denied');
|
|
39
|
+
return new VoiceAudioEngineError(
|
|
40
|
+
'mic-unavailable',
|
|
41
|
+
typeof name === 'string' ? name : 'The microphone could not be opened',
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Level of the RMS a worklet reported, on the dictation meter's dBFS scale. */
|
|
46
|
+
function levelOfRms(rms: number): number {
|
|
47
|
+
if (rms <= 0) return 0;
|
|
48
|
+
return Math.min(1, Math.max(0, (20 * Math.log10(rms) + 60) / 60));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function createVoiceAudioEngine(): VoiceAudioEngine {
|
|
52
|
+
let context: AudioContext | null = null;
|
|
53
|
+
let stream: MediaStream | null = null;
|
|
54
|
+
let capture: AudioWorkletNode | null = null;
|
|
55
|
+
let playback: AudioWorkletNode | null = null;
|
|
56
|
+
let stopped = false;
|
|
57
|
+
let pendingFlush: ((value: { itemId: string; playedMs: number } | null) => void) | null = null;
|
|
58
|
+
let lastProgress: { itemId: string; playedMs: number } | null = null;
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
async start(handlers: VoiceAudioEngineHandlers): Promise<void> {
|
|
62
|
+
const media = globalThis.navigator?.mediaDevices;
|
|
63
|
+
const AudioContextCtor = globalThis.AudioContext;
|
|
64
|
+
if (!media?.getUserMedia || !AudioContextCtor || !('AudioWorkletNode' in globalThis)) {
|
|
65
|
+
throw new VoiceAudioEngineError('voice-unavailable');
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
stream = await media.getUserMedia({
|
|
69
|
+
audio: {
|
|
70
|
+
echoCancellation: true,
|
|
71
|
+
noiseSuppression: true,
|
|
72
|
+
autoGainControl: true,
|
|
73
|
+
channelCount: 1,
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
} catch (caught: unknown) {
|
|
77
|
+
throw deviceError(caught);
|
|
78
|
+
}
|
|
79
|
+
if (stopped) {
|
|
80
|
+
for (const track of stream.getTracks()) track.stop();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
context = new AudioContextCtor({ latencyHint: 'interactive' });
|
|
85
|
+
try {
|
|
86
|
+
await context.audioWorklet.addModule(VOICE_WORKLETS_URL);
|
|
87
|
+
} catch {
|
|
88
|
+
throw new VoiceAudioEngineError('voice-unavailable');
|
|
89
|
+
}
|
|
90
|
+
await context.resume().catch(() => undefined);
|
|
91
|
+
|
|
92
|
+
const source = context.createMediaStreamSource(stream);
|
|
93
|
+
capture = new AudioWorkletNode(context, 'alia-voice-capture', {
|
|
94
|
+
numberOfInputs: 1,
|
|
95
|
+
numberOfOutputs: 0,
|
|
96
|
+
channelCount: 1,
|
|
97
|
+
processorOptions: {
|
|
98
|
+
streamSampleRate: VOICE_SAMPLE_RATE,
|
|
99
|
+
frameSamples: VOICE_FRAME_SAMPLES,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
capture.port.onmessage = (event: MessageEvent<ArrayBuffer>) => {
|
|
103
|
+
if (stopped) return;
|
|
104
|
+
const pcm = new Uint8Array(event.data);
|
|
105
|
+
handlers.onCaptureLevel(levelOfPcm16(pcm));
|
|
106
|
+
handlers.onCaptureFrame(pcm);
|
|
107
|
+
};
|
|
108
|
+
source.connect(capture);
|
|
109
|
+
|
|
110
|
+
playback = new AudioWorkletNode(context, 'alia-voice-playback', {
|
|
111
|
+
numberOfInputs: 0,
|
|
112
|
+
numberOfOutputs: 1,
|
|
113
|
+
outputChannelCount: [context.destination.channelCount || 2],
|
|
114
|
+
processorOptions: {
|
|
115
|
+
streamSampleRate: VOICE_SAMPLE_RATE,
|
|
116
|
+
prebufferMs: PREBUFFER_MS,
|
|
117
|
+
prebufferIdleMs: PREBUFFER_IDLE_MS,
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
playback.port.onmessage = (event: MessageEvent) => {
|
|
121
|
+
const message = event.data as
|
|
122
|
+
| { type: 'started'; itemId: string }
|
|
123
|
+
| { type: 'progress'; itemId: string | null; playedMs: number; rms: number }
|
|
124
|
+
| { type: 'idle' }
|
|
125
|
+
| { type: 'flushed'; itemId: string | null; playedMs: number | null };
|
|
126
|
+
if (stopped) return;
|
|
127
|
+
switch (message.type) {
|
|
128
|
+
case 'started':
|
|
129
|
+
handlers.onPlaybackStarted(message.itemId);
|
|
130
|
+
return;
|
|
131
|
+
case 'progress':
|
|
132
|
+
lastProgress =
|
|
133
|
+
message.itemId === null
|
|
134
|
+
? null
|
|
135
|
+
: { itemId: message.itemId, playedMs: message.playedMs };
|
|
136
|
+
handlers.onPlaybackLevel(levelOfRms(message.rms));
|
|
137
|
+
return;
|
|
138
|
+
case 'idle':
|
|
139
|
+
handlers.onPlaybackLevel(0);
|
|
140
|
+
handlers.onPlaybackIdle();
|
|
141
|
+
return;
|
|
142
|
+
case 'flushed': {
|
|
143
|
+
const resolve = pendingFlush;
|
|
144
|
+
pendingFlush = null;
|
|
145
|
+
handlers.onPlaybackLevel(0);
|
|
146
|
+
resolve?.(
|
|
147
|
+
message.itemId === null || message.playedMs === null
|
|
148
|
+
? null
|
|
149
|
+
: { itemId: message.itemId, playedMs: Math.round(message.playedMs) },
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
playback.connect(context.destination);
|
|
155
|
+
|
|
156
|
+
for (const track of stream.getAudioTracks()) {
|
|
157
|
+
track.addEventListener('ended', () => {
|
|
158
|
+
if (!stopped) handlers.onError(new VoiceAudioEngineError('mic-unavailable'));
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
enqueue(itemId: string, pcm16: Uint8Array): void {
|
|
164
|
+
if (stopped || playback === null) return;
|
|
165
|
+
const samples = pcm16ToFloat(pcm16);
|
|
166
|
+
playback.port.postMessage({ type: 'chunk', itemId, samples }, [samples.buffer]);
|
|
167
|
+
},
|
|
168
|
+
|
|
169
|
+
flush(): Promise<{ itemId: string; playedMs: number } | null> {
|
|
170
|
+
if (stopped || playback === null) return Promise.resolve(null);
|
|
171
|
+
const fallback = lastProgress;
|
|
172
|
+
return new Promise((resolve) => {
|
|
173
|
+
// The worklet answers within a render quantum; if the context is
|
|
174
|
+
// suspended it never will, and the last progress report is the answer.
|
|
175
|
+
const timer = setTimeout(() => {
|
|
176
|
+
pendingFlush = null;
|
|
177
|
+
resolve(
|
|
178
|
+
fallback === null ? null : { ...fallback, playedMs: Math.round(fallback.playedMs) },
|
|
179
|
+
);
|
|
180
|
+
}, 250);
|
|
181
|
+
pendingFlush = (value) => {
|
|
182
|
+
clearTimeout(timer);
|
|
183
|
+
resolve(value);
|
|
184
|
+
};
|
|
185
|
+
playback?.port.postMessage({ type: 'flush' });
|
|
186
|
+
});
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
stop(): void {
|
|
190
|
+
if (stopped) return;
|
|
191
|
+
stopped = true;
|
|
192
|
+
pendingFlush?.(null);
|
|
193
|
+
pendingFlush = null;
|
|
194
|
+
capture?.port.close();
|
|
195
|
+
playback?.port.close();
|
|
196
|
+
capture?.disconnect();
|
|
197
|
+
playback?.disconnect();
|
|
198
|
+
for (const track of stream?.getTracks() ?? []) track.stop();
|
|
199
|
+
void context?.close().catch(() => undefined);
|
|
200
|
+
capture = null;
|
|
201
|
+
playback = null;
|
|
202
|
+
stream = null;
|
|
203
|
+
context = null;
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { create } from 'zustand';
|
|
2
|
+
|
|
3
|
+
/** A call somebody asked for, waiting for the screen that shows its conversation. */
|
|
4
|
+
export interface VoiceCallRequest {
|
|
5
|
+
readonly conversationId: string;
|
|
6
|
+
readonly accountId: string | null;
|
|
7
|
+
readonly at: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** How long a request waits for its screen. A navigation that never arrives must not open a call later. */
|
|
11
|
+
export const CALL_REQUEST_TTL_MS = 15_000;
|
|
12
|
+
|
|
13
|
+
interface VoiceCallRequestStore {
|
|
14
|
+
request: VoiceCallRequest | null;
|
|
15
|
+
ask: (conversationId: string, accountId: string | null) => void;
|
|
16
|
+
clear: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Where a call is asked for, apart from where it opens.
|
|
21
|
+
*
|
|
22
|
+
* Starting a call and showing it are the same thing when the conversation is already on screen,
|
|
23
|
+
* and two screens apart when it is not: a new-chat route makes the conversation and the host
|
|
24
|
+
* navigates to it. Both go the same way. The launcher AND the screen of the conversation meet
|
|
25
|
+
* here — it asks, and whichever launcher is mounted for that conversation (that account, in time)
|
|
26
|
+
* takes the request and opens the call. The sheet, the screen and the app share this one path.
|
|
27
|
+
*/
|
|
28
|
+
export const useVoiceCallRequests = create<VoiceCallRequestStore>((set) => ({
|
|
29
|
+
request: null,
|
|
30
|
+
ask: (conversationId, accountId) =>
|
|
31
|
+
set({ request: { conversationId, accountId, at: Date.now() } }),
|
|
32
|
+
clear: () => set({ request: null }),
|
|
33
|
+
}));
|
|
34
|
+
|
|
35
|
+
/** Whether `request` is for this conversation and account, and still worth opening. */
|
|
36
|
+
export function isRequestFor(
|
|
37
|
+
request: VoiceCallRequest | null,
|
|
38
|
+
conversationId: string | undefined,
|
|
39
|
+
accountId: string | null,
|
|
40
|
+
now = Date.now(),
|
|
41
|
+
): boolean {
|
|
42
|
+
return (
|
|
43
|
+
request !== null &&
|
|
44
|
+
conversationId !== undefined &&
|
|
45
|
+
request.conversationId === conversationId &&
|
|
46
|
+
request.accountId === accountId &&
|
|
47
|
+
now - request.at <= CALL_REQUEST_TTL_MS
|
|
48
|
+
);
|
|
49
|
+
}
|