@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,64 @@
|
|
|
1
|
+
/** A level reading clamped to 0..1; anything not a finite number is 0. */
|
|
2
|
+
function clampLevel(level: number): number {
|
|
3
|
+
if (!Number.isFinite(level)) return 0;
|
|
4
|
+
return Math.min(1, Math.max(0, level));
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The two live levels of a voice call — what the microphone hears and what the
|
|
9
|
+
* call is saying — for whatever draws them.
|
|
10
|
+
*
|
|
11
|
+
* It replaced the LiveKit `Room` that `useAudioLevelMonitor` used to take and
|
|
12
|
+
* put analysers on. The voice call writes here from its audio engine's
|
|
13
|
+
* captured and played buffers; the monitor subscribes and hands React
|
|
14
|
+
* a throttled copy. Writes are cheap and never render anything by themselves.
|
|
15
|
+
*/
|
|
16
|
+
export interface VoiceLevelSource {
|
|
17
|
+
readonly capture: number;
|
|
18
|
+
readonly playback: number;
|
|
19
|
+
subscribe(listener: () => void): () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class VoiceLevelChannel implements VoiceLevelSource {
|
|
23
|
+
private captureLevel = 0;
|
|
24
|
+
private playbackLevel = 0;
|
|
25
|
+
private readonly listeners = new Set<() => void>();
|
|
26
|
+
|
|
27
|
+
get capture(): number {
|
|
28
|
+
return this.captureLevel;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
get playback(): number {
|
|
32
|
+
return this.playbackLevel;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
setCapture(level: number): void {
|
|
36
|
+
const next = clampLevel(level);
|
|
37
|
+
if (next === this.captureLevel) return;
|
|
38
|
+
this.captureLevel = next;
|
|
39
|
+
this.emit();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setPlayback(level: number): void {
|
|
43
|
+
const next = clampLevel(level);
|
|
44
|
+
if (next === this.playbackLevel) return;
|
|
45
|
+
this.playbackLevel = next;
|
|
46
|
+
this.emit();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
reset(): void {
|
|
50
|
+
this.setCapture(0);
|
|
51
|
+
this.setPlayback(0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
subscribe(listener: () => void): () => void {
|
|
55
|
+
this.listeners.add(listener);
|
|
56
|
+
return () => {
|
|
57
|
+
this.listeners.delete(listener);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
private emit(): void {
|
|
62
|
+
for (const listener of this.listeners) listener();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client half of Alia's voice-call protocol, `GET /v1/voice/session`
|
|
3
|
+
* (ADR 0017 §3–§4).
|
|
4
|
+
*
|
|
5
|
+
* The server's copy is `packages/api/src/lib/voice/protocol.ts`; the SDK does
|
|
6
|
+
* not depend on the API package, so the shapes are restated here and
|
|
7
|
+
* `VOICE_PROTOCOL_VERSION` is what turns a mismatch into a refusal instead of a
|
|
8
|
+
* misunderstanding.
|
|
9
|
+
*
|
|
10
|
+
* - Binary, client → Alia: microphone PCM16 LE, 24 kHz, mono, ~40 ms a frame.
|
|
11
|
+
* - Binary, Alia → client: `[u8 n][n bytes: assistant item id, UTF-8][PCM16 LE]`.
|
|
12
|
+
* - Text: one JSON event per frame. Every server event after
|
|
13
|
+
* `session.accepted` carries `seq`; a resume replays from `afterSeq`.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 2: every transcript and tool event names `messageId`, the message it belongs
|
|
18
|
+
* to as it is stored.
|
|
19
|
+
*/
|
|
20
|
+
export const VOICE_PROTOCOL_VERSION = 2;
|
|
21
|
+
|
|
22
|
+
export const VOICE_SESSION_PATH = '/v1/voice/session';
|
|
23
|
+
|
|
24
|
+
/** PCM16 mono at this rate, both directions. */
|
|
25
|
+
export const VOICE_SAMPLE_RATE = 24_000;
|
|
26
|
+
|
|
27
|
+
/** Samples in one ~40 ms microphone frame. */
|
|
28
|
+
export const VOICE_FRAME_SAMPLES = 960;
|
|
29
|
+
|
|
30
|
+
/** Milliseconds of audio in `bytes` of PCM16 mono at {@link VOICE_SAMPLE_RATE}. */
|
|
31
|
+
export function pcmBytesToMs(bytes: number): number {
|
|
32
|
+
return (bytes / 2 / VOICE_SAMPLE_RATE) * 1000;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type ProductVoice = 'male' | 'female';
|
|
36
|
+
|
|
37
|
+
/** Codes Alia ends a call with. */
|
|
38
|
+
export type VoiceServerErrorCode =
|
|
39
|
+
| 'not-authenticated'
|
|
40
|
+
| 'credential-not-supported'
|
|
41
|
+
| 'plan-required'
|
|
42
|
+
| 'credits-exhausted'
|
|
43
|
+
| 'agent-unavailable'
|
|
44
|
+
| 'invalid-request'
|
|
45
|
+
| 'resume-unavailable'
|
|
46
|
+
| 'realtime-unavailable'
|
|
47
|
+
| 'realtime-limited'
|
|
48
|
+
| 'realtime-rejected'
|
|
49
|
+
| 'realtime-failed';
|
|
50
|
+
|
|
51
|
+
const SERVER_ERROR_CODES: ReadonlySet<string> = new Set<VoiceServerErrorCode>([
|
|
52
|
+
'not-authenticated',
|
|
53
|
+
'credential-not-supported',
|
|
54
|
+
'plan-required',
|
|
55
|
+
'credits-exhausted',
|
|
56
|
+
'agent-unavailable',
|
|
57
|
+
'invalid-request',
|
|
58
|
+
'resume-unavailable',
|
|
59
|
+
'realtime-unavailable',
|
|
60
|
+
'realtime-limited',
|
|
61
|
+
'realtime-rejected',
|
|
62
|
+
'realtime-failed',
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
/** Why a call ended. `call-moved-elsewhere` is a reason, never an error. */
|
|
66
|
+
export type VoiceCloseReason =
|
|
67
|
+
| 'hangup'
|
|
68
|
+
| 'call-moved-elsewhere'
|
|
69
|
+
| 'max-duration'
|
|
70
|
+
| 'idle-timeout'
|
|
71
|
+
| 'credits-exhausted'
|
|
72
|
+
| 'error'
|
|
73
|
+
| 'server-shutdown';
|
|
74
|
+
|
|
75
|
+
const CLOSE_REASONS: ReadonlySet<string> = new Set<VoiceCloseReason>([
|
|
76
|
+
'hangup',
|
|
77
|
+
'call-moved-elsewhere',
|
|
78
|
+
'max-duration',
|
|
79
|
+
'idle-timeout',
|
|
80
|
+
'credits-exhausted',
|
|
81
|
+
'error',
|
|
82
|
+
'server-shutdown',
|
|
83
|
+
]);
|
|
84
|
+
|
|
85
|
+
export type VoiceRole = 'user' | 'assistant';
|
|
86
|
+
|
|
87
|
+
export type VoiceClientEvent =
|
|
88
|
+
/** `appAssertion`: the host's Oxy app assertion (ADR 0021); a socket cannot carry the header. */
|
|
89
|
+
| { type: 'auth'; token: string; appAssertion?: string }
|
|
90
|
+
| {
|
|
91
|
+
type: 'session.start';
|
|
92
|
+
protocol: number;
|
|
93
|
+
conversationId: string;
|
|
94
|
+
agentId?: string;
|
|
95
|
+
voice: ProductVoice;
|
|
96
|
+
locale: string;
|
|
97
|
+
}
|
|
98
|
+
| { type: 'session.resume'; protocol: number; voiceSessionId: string; afterSeq: number }
|
|
99
|
+
| { type: 'playback.interrupted'; itemId: string; playedMs: number }
|
|
100
|
+
| { type: 'session.end' };
|
|
101
|
+
|
|
102
|
+
export type VoiceServerEvent =
|
|
103
|
+
| { type: 'auth.ok' }
|
|
104
|
+
| { type: 'session.accepted'; voiceSessionId: string; callId: string }
|
|
105
|
+
| {
|
|
106
|
+
type: 'session.ready';
|
|
107
|
+
model: { id: string; displayName: string; publisher: string };
|
|
108
|
+
maxDurationMs: number;
|
|
109
|
+
idleTimeoutMs: number;
|
|
110
|
+
resumeWindowMs: number;
|
|
111
|
+
}
|
|
112
|
+
| { type: 'input.ready' }
|
|
113
|
+
| { type: 'user.speech_started'; itemId: string }
|
|
114
|
+
| { type: 'user.speech_stopped'; itemId: string }
|
|
115
|
+
/**
|
|
116
|
+
* `messageId` is the message the text belongs to, the id it is stored and
|
|
117
|
+
* announced under: an assistant turn's items are one message.
|
|
118
|
+
*/
|
|
119
|
+
| { type: 'transcript.delta'; role: VoiceRole; messageId: string; itemId: string; text: string }
|
|
120
|
+
| { type: 'transcript.done'; role: VoiceRole; messageId: string; itemId: string; text: string }
|
|
121
|
+
| { type: 'assistant.audio_done'; itemId: string }
|
|
122
|
+
| {
|
|
123
|
+
type: 'assistant.done';
|
|
124
|
+
itemId: string | null;
|
|
125
|
+
status: 'completed' | 'cancelled' | 'incomplete' | 'failed';
|
|
126
|
+
}
|
|
127
|
+
| { type: 'tool.started'; messageId: string; callId: string; name: string }
|
|
128
|
+
| { type: 'tool.finished'; messageId: string; callId: string; name: string; ok: boolean }
|
|
129
|
+
| { type: 'messages.saved'; conversationId: string; messageIds: string[] }
|
|
130
|
+
| { type: 'session.warning'; code: 'ending-soon'; remainingMs: number }
|
|
131
|
+
| { type: 'session.reconnecting' }
|
|
132
|
+
| { type: 'session.resumed'; afterSeq: number }
|
|
133
|
+
| {
|
|
134
|
+
type: 'error';
|
|
135
|
+
code: VoiceServerErrorCode;
|
|
136
|
+
message: string;
|
|
137
|
+
retryable: boolean;
|
|
138
|
+
reference: string;
|
|
139
|
+
}
|
|
140
|
+
| { type: 'session.closed'; reason: VoiceCloseReason };
|
|
141
|
+
|
|
142
|
+
/** A server event as received: numbered once a session exists. */
|
|
143
|
+
export type ReceivedVoiceEvent = VoiceServerEvent & { seq?: number };
|
|
144
|
+
|
|
145
|
+
const isString = (value: unknown): value is string => typeof value === 'string';
|
|
146
|
+
const isNumber = (value: unknown): value is number =>
|
|
147
|
+
typeof value === 'number' && Number.isFinite(value);
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A server event, or `null` for a frame this SDK does not understand — which
|
|
151
|
+
* it ignores rather than guesses at.
|
|
152
|
+
*/
|
|
153
|
+
export function parseVoiceServerEvent(raw: string): ReceivedVoiceEvent | null {
|
|
154
|
+
let value: unknown;
|
|
155
|
+
try {
|
|
156
|
+
value = JSON.parse(raw);
|
|
157
|
+
} catch {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
if (value === null || typeof value !== 'object') return null;
|
|
161
|
+
const event = value as Record<string, unknown>;
|
|
162
|
+
const seq = isNumber(event.seq) ? { seq: event.seq } : {};
|
|
163
|
+
switch (event.type) {
|
|
164
|
+
case 'auth.ok':
|
|
165
|
+
case 'input.ready':
|
|
166
|
+
case 'session.reconnecting':
|
|
167
|
+
return { type: event.type, ...seq };
|
|
168
|
+
case 'session.accepted':
|
|
169
|
+
return isString(event.voiceSessionId) && isString(event.callId)
|
|
170
|
+
? {
|
|
171
|
+
type: 'session.accepted',
|
|
172
|
+
voiceSessionId: event.voiceSessionId,
|
|
173
|
+
callId: event.callId,
|
|
174
|
+
...seq,
|
|
175
|
+
}
|
|
176
|
+
: null;
|
|
177
|
+
case 'session.ready': {
|
|
178
|
+
const model = event.model as Record<string, unknown> | undefined;
|
|
179
|
+
if (
|
|
180
|
+
!model ||
|
|
181
|
+
!isString(model.id) ||
|
|
182
|
+
!isNumber(event.maxDurationMs) ||
|
|
183
|
+
!isNumber(event.idleTimeoutMs) ||
|
|
184
|
+
!isNumber(event.resumeWindowMs)
|
|
185
|
+
)
|
|
186
|
+
return null;
|
|
187
|
+
return {
|
|
188
|
+
type: 'session.ready',
|
|
189
|
+
model: {
|
|
190
|
+
id: model.id,
|
|
191
|
+
displayName: isString(model.displayName) ? model.displayName : model.id,
|
|
192
|
+
publisher: isString(model.publisher) ? model.publisher : '',
|
|
193
|
+
},
|
|
194
|
+
maxDurationMs: event.maxDurationMs,
|
|
195
|
+
idleTimeoutMs: event.idleTimeoutMs,
|
|
196
|
+
resumeWindowMs: event.resumeWindowMs,
|
|
197
|
+
...seq,
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
case 'user.speech_started':
|
|
201
|
+
case 'user.speech_stopped':
|
|
202
|
+
case 'assistant.audio_done':
|
|
203
|
+
return isString(event.itemId) ? { type: event.type, itemId: event.itemId, ...seq } : null;
|
|
204
|
+
case 'transcript.delta':
|
|
205
|
+
case 'transcript.done':
|
|
206
|
+
return (event.role === 'user' || event.role === 'assistant') &&
|
|
207
|
+
isString(event.messageId) &&
|
|
208
|
+
isString(event.itemId) &&
|
|
209
|
+
isString(event.text)
|
|
210
|
+
? {
|
|
211
|
+
type: event.type,
|
|
212
|
+
role: event.role,
|
|
213
|
+
messageId: event.messageId,
|
|
214
|
+
itemId: event.itemId,
|
|
215
|
+
text: event.text,
|
|
216
|
+
...seq,
|
|
217
|
+
}
|
|
218
|
+
: null;
|
|
219
|
+
case 'assistant.done':
|
|
220
|
+
return event.itemId === null || isString(event.itemId)
|
|
221
|
+
? {
|
|
222
|
+
type: 'assistant.done',
|
|
223
|
+
itemId: event.itemId as string | null,
|
|
224
|
+
status:
|
|
225
|
+
event.status === 'cancelled' ||
|
|
226
|
+
event.status === 'incomplete' ||
|
|
227
|
+
event.status === 'failed'
|
|
228
|
+
? event.status
|
|
229
|
+
: 'completed',
|
|
230
|
+
...seq,
|
|
231
|
+
}
|
|
232
|
+
: null;
|
|
233
|
+
case 'tool.started':
|
|
234
|
+
return isString(event.messageId) && isString(event.callId) && isString(event.name)
|
|
235
|
+
? {
|
|
236
|
+
type: 'tool.started',
|
|
237
|
+
messageId: event.messageId,
|
|
238
|
+
callId: event.callId,
|
|
239
|
+
name: event.name,
|
|
240
|
+
...seq,
|
|
241
|
+
}
|
|
242
|
+
: null;
|
|
243
|
+
case 'tool.finished':
|
|
244
|
+
return isString(event.messageId) && isString(event.callId) && isString(event.name)
|
|
245
|
+
? {
|
|
246
|
+
type: 'tool.finished',
|
|
247
|
+
messageId: event.messageId,
|
|
248
|
+
callId: event.callId,
|
|
249
|
+
name: event.name,
|
|
250
|
+
ok: event.ok === true,
|
|
251
|
+
...seq,
|
|
252
|
+
}
|
|
253
|
+
: null;
|
|
254
|
+
case 'messages.saved':
|
|
255
|
+
return isString(event.conversationId) && Array.isArray(event.messageIds)
|
|
256
|
+
? {
|
|
257
|
+
type: 'messages.saved',
|
|
258
|
+
conversationId: event.conversationId,
|
|
259
|
+
messageIds: event.messageIds.filter(isString),
|
|
260
|
+
...seq,
|
|
261
|
+
}
|
|
262
|
+
: null;
|
|
263
|
+
case 'session.warning':
|
|
264
|
+
return event.code === 'ending-soon' && isNumber(event.remainingMs)
|
|
265
|
+
? { type: 'session.warning', code: 'ending-soon', remainingMs: event.remainingMs, ...seq }
|
|
266
|
+
: null;
|
|
267
|
+
case 'session.resumed':
|
|
268
|
+
return isNumber(event.afterSeq)
|
|
269
|
+
? { type: 'session.resumed', afterSeq: event.afterSeq, ...seq }
|
|
270
|
+
: null;
|
|
271
|
+
case 'error':
|
|
272
|
+
return {
|
|
273
|
+
type: 'error',
|
|
274
|
+
// A code this SDK does not know is still a failure of the call upstream.
|
|
275
|
+
code:
|
|
276
|
+
isString(event.code) && SERVER_ERROR_CODES.has(event.code)
|
|
277
|
+
? (event.code as VoiceServerErrorCode)
|
|
278
|
+
: 'realtime-failed',
|
|
279
|
+
message: isString(event.message) ? event.message : '',
|
|
280
|
+
retryable: event.retryable === true,
|
|
281
|
+
reference: isString(event.reference) ? event.reference : '',
|
|
282
|
+
...seq,
|
|
283
|
+
};
|
|
284
|
+
case 'session.closed':
|
|
285
|
+
return {
|
|
286
|
+
type: 'session.closed',
|
|
287
|
+
reason:
|
|
288
|
+
isString(event.reason) && CLOSE_REASONS.has(event.reason)
|
|
289
|
+
? (event.reason as VoiceCloseReason)
|
|
290
|
+
: 'error',
|
|
291
|
+
...seq,
|
|
292
|
+
};
|
|
293
|
+
default:
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** `[u8 n][n bytes: item id][PCM16]`, or `null` for a frame that is not one. */
|
|
299
|
+
export function decodeAssistantAudioFrame(
|
|
300
|
+
frame: Uint8Array,
|
|
301
|
+
): { itemId: string; pcm: Uint8Array } | null {
|
|
302
|
+
if (frame.byteLength < 2) return null;
|
|
303
|
+
const length = frame[0]!;
|
|
304
|
+
if (length === 0 || frame.byteLength < 1 + length) return null;
|
|
305
|
+
const itemId = new TextDecoder().decode(frame.subarray(1, 1 + length));
|
|
306
|
+
return { itemId, pcm: frame.subarray(1 + length) };
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** The test and dev counterpart: what the server sends. */
|
|
310
|
+
export function encodeAssistantAudioFrame(itemId: string, pcm: Uint8Array): Uint8Array {
|
|
311
|
+
const id = new TextEncoder().encode(itemId);
|
|
312
|
+
const frame = new Uint8Array(1 + id.byteLength + pcm.byteLength);
|
|
313
|
+
frame[0] = id.byteLength;
|
|
314
|
+
frame.set(id, 1);
|
|
315
|
+
frame.set(pcm, 1 + id.byteLength);
|
|
316
|
+
return frame;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** `https://api.gemies.ai` → `wss://api.gemies.ai/v1/voice/session`. */
|
|
320
|
+
export function voiceSessionUrl(apiUrl: string): string {
|
|
321
|
+
const base = apiUrl.replace(/\/+$/, '');
|
|
322
|
+
const ws = base.startsWith('https://')
|
|
323
|
+
? `wss://${base.slice('https://'.length)}`
|
|
324
|
+
: base.startsWith('http://')
|
|
325
|
+
? `ws://${base.slice('http://'.length)}`
|
|
326
|
+
: base;
|
|
327
|
+
return `${ws}${VOICE_SESSION_PATH}`;
|
|
328
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { GEMIES_WEB_ORIGIN } from './origins';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The voice call's AudioWorklet processors (`worklets/voice-processors.js`), as the web engine
|
|
5
|
+
* loads them.
|
|
6
|
+
*
|
|
7
|
+
* Not a `blob:` URL: a worklet module is a script, so a blob would need `script-src blob:` in
|
|
8
|
+
* every host's CSP, which would let any blob the page makes run as script. The Gemies web app serves
|
|
9
|
+
* the file instead, under `script-src https://gemies.ai` that the avatar already needs
|
|
10
|
+
* (`@gemies/sdk/csp`), with CORS open for the cross-origin `addModule`.
|
|
11
|
+
*
|
|
12
|
+
* The name is the file's content hash, so an SDK embedded in another app (on its own release
|
|
13
|
+
* cycle) always loads the bytes it was written against: `packages/app/public/sdk/voice-worklets/`
|
|
14
|
+
* keeps every revision ever published, and a test fails if this revision is not the hash of the
|
|
15
|
+
* file or is not served.
|
|
16
|
+
*/
|
|
17
|
+
export const VOICE_WORKLETS_REVISION = '543d4a8e42937e99';
|
|
18
|
+
|
|
19
|
+
export const VOICE_WORKLETS_URL = `${GEMIES_WEB_ORIGIN}/sdk/voice-worklets/${VOICE_WORKLETS_REVISION}.js`;
|