@alia.onl/sdk 22.7.0 → 22.8.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 CHANGED
@@ -1,5 +1,22 @@
1
1
  # @alia.onl/sdk
2
2
 
3
+ ## 22.8.0
4
+
5
+ - **Streaming dictation (ADR 0024).** `@alia.onl/sdk/dictation` gains
6
+ `openDictationStream({ apiUrl, getToken | token, language?, onEvent, signal?, openSocket?,
7
+ endTimeoutMs? })`: the caller's PCM16 LE mono 24 kHz frames go to
8
+ `GET /v1/audio/transcriptions/stream`, and Alia's events come back while the person speaks:
9
+ `dictation.ready`, `transcript.interim` / `transcript.delta` / `transcript.final` per numbered
10
+ `segment`, `error { code, retryable }` and one `dictation.closed { reason }`. A segment's
11
+ `transcript.final` is the only text to keep. It returns `{ append, commit, end, close, closed }`;
12
+ `end()` resolves once the finals have arrived. The protocol module (`DICTATION_PROTOCOL_VERSION`
13
+ 1, `parseDictationServerEvent`, `dictationStreamUrl`) is exported beside it. It reaches no React
14
+ or native module (`check:entries`). `useDictation` still records a clip for batch
15
+ transcription; adopting the stream there is a follow-up.
16
+ - The API that serves the stream ships with this release; against an older API the socket's
17
+ path answers 404 and the stream reports `TRANSCRIPTION_FAILED`, so batch
18
+ `requestTranscription` stays the fallback.
19
+
3
20
  ## 22.7.0
4
21
 
5
22
  - **Image results for awaited host tools.** An awaited tool's `execute` may answer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alia.onl/sdk",
3
- "version": "22.7.0",
3
+ "version": "22.8.0",
4
4
  "description": "Alia AI chat SDK — the conversation runtime, chat UI, voice calls and dictation for Oxy apps",
5
5
  "main": "src/index.ts",
6
6
  "react-native": "src/index.ts",
package/src/dictation.ts CHANGED
@@ -1,7 +1,8 @@
1
- // @alia.onl/sdk/dictation — dictation's request, without a component, a hook or a recorder
2
- // `POST /v1/audio/transcriptions` (ADR 0017 §7) as plain functions: an app that records audio its
3
- // own way (Astro's browser shell) sends its clips from here and reaches no React, no native module
4
- // and no Oxy client. `useDictation` and `AliaComposer` use the same request.
1
+ // @alia.onl/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 `AliaComposer` use the same batch request.
5
6
 
6
7
  export {
7
8
  DictationError,
@@ -18,3 +19,22 @@ export type {
18
19
  } from './lib/dictation';
19
20
  export type { AudioRequestAuthentication } from './lib/audio-request-auth';
20
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,142 @@
1
+ /**
2
+ * The client half of Alia's streaming dictation protocol,
3
+ * `GET /v1/audio/transcriptions/stream` (ADR 0024).
4
+ *
5
+ * The server's copy is `packages/api/src/lib/dictation/protocol.ts`; the SDK
6
+ * does not depend on the API package, so the shapes are restated here and
7
+ * `DICTATION_PROTOCOL_VERSION` is what turns a mismatch into a refusal instead
8
+ * of a misunderstanding. The API's tests hold the two copies equal.
9
+ *
10
+ * - Binary, client → Alia: microphone PCM16 LE, 24 kHz, mono.
11
+ * - Text: one JSON event per frame, both ways.
12
+ *
13
+ * Segments are Alia's numbers, from 0, in the order the person spoke them. A
14
+ * segment's `transcript.final` is the only text a client keeps:
15
+ * `transcript.interim` (the segment's provisional text, whole, replacing the
16
+ * last) and `transcript.delta` (locked text appended) are display state the
17
+ * final supersedes.
18
+ */
19
+
20
+ export const DICTATION_PROTOCOL_VERSION = 1;
21
+
22
+ export const DICTATION_STREAM_PATH = '/v1/audio/transcriptions/stream';
23
+
24
+ /** PCM16 mono at this rate: the one encoding the stream takes. */
25
+ export const DICTATION_SAMPLE_RATE = 24_000;
26
+
27
+ /** What a dictation ends with that is not a failure. */
28
+ export type DictationCloseReason =
29
+ | 'ended'
30
+ | 'max-duration'
31
+ | 'idle-timeout'
32
+ | 'error'
33
+ | 'server-shutdown';
34
+
35
+ /** Why a dictation failed, as Alia names it. */
36
+ export type DictationStreamErrorCode =
37
+ | 'TRANSCRIPTION_UNAVAILABLE'
38
+ | 'TRANSCRIPTION_LIMITED'
39
+ | 'TRANSCRIPTION_REJECTED'
40
+ | 'TRANSCRIPTION_FAILED'
41
+ | 'CLIP_TOO_LONG'
42
+ | 'INVALID_REQUEST'
43
+ | 'NOT_AUTHENTICATED'
44
+ | 'CREDENTIAL_NOT_SUPPORTED';
45
+
46
+ export type DictationServerEvent =
47
+ | { readonly type: 'dictation.ready'; readonly maxDurationMs: number }
48
+ | { readonly type: 'transcript.interim'; readonly segment: number; readonly text: string }
49
+ | { readonly type: 'transcript.delta'; readonly segment: number; readonly text: string }
50
+ | { readonly type: 'transcript.final'; readonly segment: number; readonly text: string }
51
+ | {
52
+ readonly type: 'error';
53
+ readonly code: DictationStreamErrorCode;
54
+ readonly message: string;
55
+ readonly retryable: boolean;
56
+ }
57
+ | { readonly type: 'dictation.closed'; readonly reason: DictationCloseReason };
58
+
59
+ export type DictationClientEvent =
60
+ | { readonly type: 'auth'; readonly token: string }
61
+ | {
62
+ readonly type: 'dictation.start';
63
+ readonly protocol: number;
64
+ readonly language?: string;
65
+ readonly sampleRate: number;
66
+ }
67
+ | { readonly type: 'dictation.commit' }
68
+ | { readonly type: 'dictation.end' };
69
+
70
+ const CLOSE_REASONS: ReadonlySet<string> = new Set<DictationCloseReason>([
71
+ 'ended',
72
+ 'max-duration',
73
+ 'idle-timeout',
74
+ 'error',
75
+ 'server-shutdown',
76
+ ]);
77
+
78
+ const ERROR_CODES: ReadonlySet<string> = new Set<DictationStreamErrorCode>([
79
+ 'TRANSCRIPTION_UNAVAILABLE',
80
+ 'TRANSCRIPTION_LIMITED',
81
+ 'TRANSCRIPTION_REJECTED',
82
+ 'TRANSCRIPTION_FAILED',
83
+ 'CLIP_TOO_LONG',
84
+ 'INVALID_REQUEST',
85
+ 'NOT_AUTHENTICATED',
86
+ 'CREDENTIAL_NOT_SUPPORTED',
87
+ ]);
88
+
89
+ const isSegment = (value: unknown): value is number =>
90
+ typeof value === 'number' && Number.isInteger(value) && value >= 0;
91
+
92
+ /** One server text frame, or `null` when it is not one of protocol 1's events. */
93
+ export function parseDictationServerEvent(text: string): DictationServerEvent | null {
94
+ let value: unknown;
95
+ try {
96
+ value = JSON.parse(text);
97
+ } catch {
98
+ return null;
99
+ }
100
+ if (value === null || typeof value !== 'object') return null;
101
+ const event = value as Record<string, unknown>;
102
+ switch (event.type) {
103
+ case 'dictation.ready':
104
+ return typeof event.maxDurationMs === 'number' && event.maxDurationMs > 0
105
+ ? { type: 'dictation.ready', maxDurationMs: event.maxDurationMs }
106
+ : null;
107
+ case 'transcript.interim':
108
+ case 'transcript.delta':
109
+ case 'transcript.final':
110
+ return isSegment(event.segment) && typeof event.text === 'string'
111
+ ? { type: event.type, segment: event.segment, text: event.text }
112
+ : null;
113
+ case 'error':
114
+ return typeof event.code === 'string' &&
115
+ ERROR_CODES.has(event.code) &&
116
+ typeof event.message === 'string'
117
+ ? {
118
+ type: 'error',
119
+ code: event.code as DictationStreamErrorCode,
120
+ message: event.message,
121
+ retryable: event.retryable === true,
122
+ }
123
+ : null;
124
+ case 'dictation.closed':
125
+ return typeof event.reason === 'string' && CLOSE_REASONS.has(event.reason)
126
+ ? { type: 'dictation.closed', reason: event.reason as DictationCloseReason }
127
+ : null;
128
+ default:
129
+ return null;
130
+ }
131
+ }
132
+
133
+ /** `wss://…/v1/audio/transcriptions/stream` for an Alia API origin. */
134
+ export function dictationStreamUrl(apiUrl: string): string {
135
+ const base = apiUrl.replace(/\/+$/, '');
136
+ const ws = base.startsWith('https://')
137
+ ? `wss://${base.slice('https://'.length)}`
138
+ : base.startsWith('http://')
139
+ ? `ws://${base.slice('http://'.length)}`
140
+ : base;
141
+ return `${ws}${DICTATION_STREAM_PATH}`;
142
+ }
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Streaming dictation's network half (ADR 0024): the caller's microphone PCM to
3
+ * `GET /v1/audio/transcriptions/stream`, and Alia's segments back while the
4
+ * person speaks.
5
+ *
6
+ * It records nothing and draws nothing: the caller brings PCM16 LE mono at
7
+ * 24 kHz from its own recorder (an AudioWorklet, a native module) and shows
8
+ * the segments its own way. A segment's `transcript.final` is the only text to
9
+ * keep; `transcript.interim` and `transcript.delta` are display state the
10
+ * final supersedes. When the stream cannot open (`TRANSCRIPTION_UNAVAILABLE`),
11
+ * batch `requestTranscription` is the fallback.
12
+ */
13
+
14
+ import {
15
+ type DictationClientEvent,
16
+ type DictationCloseReason,
17
+ type DictationServerEvent,
18
+ DICTATION_PROTOCOL_VERSION,
19
+ DICTATION_SAMPLE_RATE,
20
+ dictationStreamUrl,
21
+ parseDictationServerEvent,
22
+ } from './dictation-protocol';
23
+
24
+ /** The WebSocket the stream drives: a browser's, React Native's or a test's. */
25
+ export interface DictationSocket {
26
+ binaryType: string;
27
+ readonly readyState: number;
28
+ onopen: ((event: unknown) => void) | null;
29
+ onmessage: ((event: { data: unknown }) => void) | null;
30
+ onclose: ((event: unknown) => void) | null;
31
+ onerror: ((event: unknown) => void) | null;
32
+ send(data: string | ArrayBufferLike | ArrayBufferView): void;
33
+ close(code?: number, reason?: string): void;
34
+ }
35
+
36
+ /** How the stream proves the person: their Oxy session token, read once at open. */
37
+ export type DictationStreamAuthentication =
38
+ | {
39
+ readonly getToken: (
40
+ target: string,
41
+ options?: { signal?: AbortSignal },
42
+ ) => string | null | Promise<string | null>;
43
+ readonly token?: never;
44
+ }
45
+ | { readonly token: string; readonly getToken?: never };
46
+
47
+ export type DictationStreamOptions = DictationStreamAuthentication & {
48
+ /** Alia's API origin, `https://api.alia.onl`. */
49
+ readonly apiUrl: string;
50
+ /** BCP 47; omitted lets the model detect the language. */
51
+ readonly language?: string;
52
+ /**
53
+ * Every event, in order. It always ends with one `dictation.closed`; a failure
54
+ * comes as `error` just before it. A connection Alia never answered on is
55
+ * reported as `TRANSCRIPTION_FAILED`, retryable.
56
+ */
57
+ readonly onEvent: (event: DictationServerEvent) => void;
58
+ /** Aborting it is `close()`. */
59
+ readonly signal?: AbortSignal;
60
+ /** The socket factory; the platform's `WebSocket` by default. */
61
+ readonly openSocket?: (url: string) => DictationSocket;
62
+ /** How long `end()` waits for Alia's `dictation.closed` before closing the socket. */
63
+ readonly endTimeoutMs?: number;
64
+ };
65
+
66
+ export interface DictationStream {
67
+ /** One microphone frame: PCM16 LE, mono, {@link DICTATION_SAMPLE_RATE} Hz. */
68
+ append(pcm16: Uint8Array): void;
69
+ /** End the current segment now (push-to-talk). */
70
+ commit(): void;
71
+ /** No more audio: resolves with how the dictation closed, once the finals have arrived. */
72
+ end(): Promise<DictationCloseReason>;
73
+ /** Drop the dictation now; nothing more is reported. */
74
+ close(): void;
75
+ /** Settles with how the dictation closed, whatever closed it. */
76
+ readonly closed: Promise<DictationCloseReason>;
77
+ }
78
+
79
+ const OPEN = 1;
80
+ const DEFAULT_END_TIMEOUT_MS = 8_000;
81
+ /** Audio held until the socket is open and the start sent: ten seconds. */
82
+ const MAX_HELD_BYTES = 10 * DICTATION_SAMPLE_RATE * 2;
83
+
84
+ function defaultSocket(url: string): DictationSocket {
85
+ const WebSocketClass = (globalThis as { WebSocket?: new (url: string) => DictationSocket })
86
+ .WebSocket;
87
+ if (!WebSocketClass) throw new Error('This platform has no WebSocket');
88
+ return new WebSocketClass(url);
89
+ }
90
+
91
+ /** Open one streaming dictation. */
92
+ export function openDictationStream(options: DictationStreamOptions): DictationStream {
93
+ const url = dictationStreamUrl(options.apiUrl);
94
+ const credentialAbort = new AbortController();
95
+ let socket: DictationSocket | null = null;
96
+ let started = false;
97
+ let finished = false;
98
+ let held: Uint8Array[] = [];
99
+ let heldBytes = 0;
100
+ let endRequested = false;
101
+ let endTimer: ReturnType<typeof setTimeout> | null = null;
102
+ let resolveClosed!: (reason: DictationCloseReason) => void;
103
+ const closed = new Promise<DictationCloseReason>((resolve) => {
104
+ resolveClosed = resolve;
105
+ });
106
+
107
+ const sendEvent = (event: DictationClientEvent) => {
108
+ if (socket?.readyState === OPEN) socket.send(JSON.stringify(event));
109
+ };
110
+
111
+ const finish = (reason: DictationCloseReason, report: boolean) => {
112
+ if (finished) return;
113
+ finished = true;
114
+ if (endTimer !== null) clearTimeout(endTimer);
115
+ credentialAbort.abort();
116
+ held = [];
117
+ const current = socket;
118
+ socket = null;
119
+ if (current) {
120
+ current.onmessage = null;
121
+ current.onclose = null;
122
+ current.onerror = null;
123
+ if (current.readyState === OPEN || current.readyState === 0) current.close(1000);
124
+ }
125
+ if (report) options.onEvent({ type: 'dictation.closed', reason });
126
+ resolveClosed(reason);
127
+ };
128
+
129
+ const fail = () => {
130
+ if (finished) return;
131
+ options.onEvent({
132
+ type: 'error',
133
+ code: 'TRANSCRIPTION_FAILED',
134
+ message: 'The dictation lost its connection to Alia.',
135
+ retryable: true,
136
+ });
137
+ finish('error', true);
138
+ };
139
+
140
+ const readToken = async (): Promise<string | null> => {
141
+ if (options.token !== undefined) return options.token || null;
142
+ try {
143
+ const token = await options.getToken(url, { signal: credentialAbort.signal });
144
+ return token || null;
145
+ } catch {
146
+ return null;
147
+ }
148
+ };
149
+
150
+ const begin = async (opened: DictationSocket) => {
151
+ const token = await readToken();
152
+ if (finished || socket !== opened) return;
153
+ if (!token) {
154
+ options.onEvent({
155
+ type: 'error',
156
+ code: 'NOT_AUTHENTICATED',
157
+ message: 'Sign in to Alia to dictate.',
158
+ retryable: false,
159
+ });
160
+ finish('error', true);
161
+ return;
162
+ }
163
+ sendEvent({ type: 'auth', token });
164
+ sendEvent({
165
+ type: 'dictation.start',
166
+ protocol: DICTATION_PROTOCOL_VERSION,
167
+ sampleRate: DICTATION_SAMPLE_RATE,
168
+ ...(options.language === undefined ? {} : { language: options.language }),
169
+ });
170
+ started = true;
171
+ for (const frame of held) opened.send(frame);
172
+ held = [];
173
+ heldBytes = 0;
174
+ if (endRequested) sendEvent({ type: 'dictation.end' });
175
+ };
176
+
177
+ try {
178
+ socket = (options.openSocket ?? defaultSocket)(url);
179
+ } catch {
180
+ queueMicrotask(fail);
181
+ }
182
+ if (socket) {
183
+ const opened = socket;
184
+ opened.binaryType = 'arraybuffer';
185
+ opened.onopen = () => {
186
+ void begin(opened);
187
+ };
188
+ opened.onmessage = (message) => {
189
+ if (typeof message.data !== 'string') return;
190
+ const event = parseDictationServerEvent(message.data);
191
+ if (event === null || finished) return;
192
+ if (event.type === 'dictation.closed') {
193
+ options.onEvent(event);
194
+ finish(event.reason, false);
195
+ return;
196
+ }
197
+ options.onEvent(event);
198
+ };
199
+ opened.onclose = () => fail();
200
+ opened.onerror = () => undefined;
201
+ }
202
+
203
+ const close = () => finish('ended', false);
204
+ if (options.signal) {
205
+ if (options.signal.aborted) queueMicrotask(close);
206
+ else options.signal.addEventListener('abort', close, { once: true });
207
+ }
208
+
209
+ return {
210
+ append(pcm16) {
211
+ if (finished || endRequested || pcm16.byteLength === 0) return;
212
+ if (started && socket?.readyState === OPEN) {
213
+ socket.send(pcm16);
214
+ return;
215
+ }
216
+ if (heldBytes + pcm16.byteLength > MAX_HELD_BYTES) return;
217
+ held.push(pcm16.slice());
218
+ heldBytes += pcm16.byteLength;
219
+ },
220
+ commit() {
221
+ if (!finished && started) sendEvent({ type: 'dictation.commit' });
222
+ },
223
+ end() {
224
+ if (finished) return closed;
225
+ if (!endRequested) {
226
+ endRequested = true;
227
+ if (started) sendEvent({ type: 'dictation.end' });
228
+ endTimer = setTimeout(
229
+ () => finish('ended', true),
230
+ options.endTimeoutMs ?? DEFAULT_END_TIMEOUT_MS,
231
+ );
232
+ }
233
+ return closed;
234
+ },
235
+ close,
236
+ closed,
237
+ };
238
+ }