@yaks/speech 0.0.0-stage → 0.2.36

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/_dist/mod.d.ts ADDED
@@ -0,0 +1,677 @@
1
+ export { DEEPGRAM } from "./deepgram.js";
2
+ export { ELEVENLABS } from "./elevenlabs.js";
3
+ export { OPENAI, openaiLiveBackend } from "./openai.js";
4
+ export { base64, mulawed, mulawOf, muted, sounds, trimmed, unbase64 } from "./audio.js";
5
+ export { query } from "./query.js";
6
+ export { type Speech, speechFrom } from "./speech-from.js";
7
+ import { type Clock } from "@yaks/fp";
8
+ /** How audio is encoded: an MP3, or raw samples at a rate (`mulaw` is the
9
+ * telephone's 8 kHz μ-law; `linear16` is 16-bit PCM). */ export type Format = {
10
+ encoding: 'mp3';
11
+ } | {
12
+ encoding: 'mulaw' | 'linear16';
13
+ rate: number;
14
+ };
15
+ /** Raw samples at a rate, as a call streams them. */ export type Raw = Extract<Format, {
16
+ rate: number;
17
+ }>;
18
+ /** A voice a provider speaks in: the `id` a request names, what a person
19
+ * calls it, how it sounds, and where a sample of it plays. A provider marks
20
+ * a voice `custom` when it was made or added to the account's library. */ export type Voice = {
21
+ id: string;
22
+ name: string;
23
+ about: string;
24
+ sample?: string;
25
+ custom?: true;
26
+ };
27
+ /** A voice designed from a description, not yet kept: the id `keep` takes,
28
+ * and a sample of it. */ export type Design = {
29
+ id: string;
30
+ audio: Uint8Array;
31
+ type: string;
32
+ seconds: number;
33
+ };
34
+ /** The speeds a provider speaks at, as multiples of its own pace. */ export type Speeds = {
35
+ min: number;
36
+ max: number;
37
+ };
38
+ /** `speed` as near as `speeds` allows.
39
+ *
40
+ * ```ts
41
+ * import { equal } from '@yaks/testing'
42
+ * equal(paced({ min: 0.7, max: 1.5 }, 1.3), 1.3)
43
+ * equal(paced({ min: 0.25, max: 1.5 }, 2), 1.5)
44
+ * ```
45
+ */ export declare let paced: ({}: Speeds, speed?: number) => number;
46
+ /** A spoken line as prose. Apply after assembling transcript fragments,
47
+ * which can split words and directions as well as sentences. */ export declare let spokenProse: (text: string) => string;
48
+ /** A request as a provider words it: the path under its API, and the rest. */ export type Asked = {
49
+ path: string;
50
+ init: RequestInit;
51
+ };
52
+ /** A multipart request with an audio file, including recordings read as a stream. */ export declare let audioForm: (fields: Record<string, string>, audio: BodyInit, type: string) => RequestInit;
53
+ /** A provider's HTTP API, its base and key already given. */ export type Ask = (path: string, init?: RequestInit) => Promise<Response>;
54
+ /** A turn heard: what one speaker said, and when. `speaker` numbers the
55
+ * voices a provider tells apart from 0, and is 0 throughout where it tells
56
+ * none apart (its `speakers`); `from` and `to` are seconds into the audio,
57
+ * where the provider says. */ export type Turn = {
58
+ speaker: number;
59
+ text: string;
60
+ from?: number;
61
+ to?: number;
62
+ key?: string;
63
+ };
64
+ /** What one message on a turns stream means: the speaker started a turn,
65
+ * turns ended (`ended`, in order), or the stream failed. */ export type Cue = {
66
+ start?: true;
67
+ hearing?: {
68
+ text: string;
69
+ key?: string;
70
+ };
71
+ ended?: Turn[];
72
+ error?: string;
73
+ };
74
+ /** What one message on a talk stream means. */ export type Said = {
75
+ /** the caller started talking */ start?: true;
76
+ /** the caller stopped talking, and the model is about to answer */ stopped?: true;
77
+ /** what the caller said, once heard: nothing, when no words were */ heard?: string;
78
+ /** the next of the caller's words, heard as they talk */ hearing?: string;
79
+ /** the provider's identity for the input utterance */ key?: string;
80
+ /** audio the model speaks, in the stream's format */ audio?: Uint8Array;
81
+ /** what the model said, once said */ said?: string;
82
+ /** the part of a response the `audio` or the words `said` belong to, by
83
+ * its id, where a response has parts */ of?: string;
84
+ /** the model began a part it says before its answer, by its id: a word
85
+ * while it works, or its answer rehearsed */ aside?: string;
86
+ /** the next of the model's words, said as it speaks */ saying?: string;
87
+ /** the stream takes what is sent after its opening from now on, held
88
+ * until then */ ready?: true;
89
+ /** the model hands work to whoever runs the call, by this id: what the
90
+ * caller asked is in their words so far */ delegated?: string;
91
+ /** the model asks for a tool */ call?: {
92
+ id: string;
93
+ name: string;
94
+ args: string;
95
+ };
96
+ /** what the turn just finished cost, in dollars */ spent?: number;
97
+ /** the model's audio began playing to the caller, on a call its own line
98
+ * carries */ plays?: true;
99
+ /** the model began a response: no other may be asked for until it is done */ begun?: true;
100
+ /** the model's response ended */ done?: true;
101
+ /** the response last asked for was refused, one being under way: ask
102
+ * again once it is done */ busy?: true;
103
+ error?: string;
104
+ };
105
+ /** What a talk stream is set up with. */ export type Setup = {
106
+ model: string;
107
+ voice: string;
108
+ /** none on a call the provider's own line carries, which says its own */ format?: Raw;
109
+ /** how fast it speaks, within the talking model's `speeds` */ speed: number;
110
+ instructions: string;
111
+ tools: {
112
+ name: string;
113
+ description: string;
114
+ parameters: object;
115
+ }[];
116
+ /** its first words, said as the stream opens (`Talking.opens`) */ opens?: string;
117
+ /** words its hearing should expect, such as the names it is called by */ words?: string[];
118
+ /** a larger model a duplex model hands its work to on the provider's
119
+ * side, one of its `backends`, told `instructions` and calling `tools`
120
+ * through `told.call`; without one, the work goes to whoever runs the
121
+ * call (`told.delegate`) */ backend?: Backend;
122
+ };
123
+ /** A larger model a duplex model hands its work to on the provider's side. */ export type Backend = {
124
+ model: string;
125
+ instructions: string;
126
+ tools: Setup['tools'];
127
+ /** Whether the provider may search without going through the client. */ webSearch?: boolean;
128
+ };
129
+ /** A speech provider as data. */ export type Provider = {
130
+ name: string;
131
+ /** what a person calls it */ title: string;
132
+ /** the HTTP API's base */ api: string;
133
+ /** the streaming API's base */ live: string;
134
+ /** the header a request carries its key in, `authorization` unless
135
+ * named */ header?: string;
136
+ /** that header's value for a key */ authorization: (key: string) => string;
137
+ /** the WebSocket subprotocols a browser offers a key under */ protocols: (key: string) => string[];
138
+ /** a recording heard whole, priced by the minute of audio */ hear: {
139
+ model: string;
140
+ perMinute: number;
141
+ /** whether it tells one speaker from another */ speakers: boolean;
142
+ /** the largest recording it hears, in bytes */ bytes: number;
143
+ request: (audio: BodyInit, type: string, model: string) => Asked;
144
+ /** the words in its answer, the seconds heard and its turns, where it
145
+ * says */ read: (said: unknown) => {
146
+ text: string;
147
+ seconds?: number;
148
+ turns?: Turn[];
149
+ };
150
+ };
151
+ /** the models that hear audio as it streams, the first unless another
152
+ * is asked for (`streamOf`) */ turns: Stream[];
153
+ /** text spoken in `voice` unless another of `voices` is asked for, by
154
+ * the first of `models` unless another is (`speakerOf`) */ speak: {
155
+ voice: string;
156
+ voices: Voice[];
157
+ speeds: Speeds;
158
+ models: Speaker[];
159
+ /** `speed` is within `speeds`, `model` one of `models` */ request: (text: string, voice: string, format: Format, speed: number, model: string) => Asked;
160
+ /** what turns its audio into `format`, when it does not speak it */ audio?: (format: Format) => TransformStream<Uint8Array, Uint8Array> | undefined;
161
+ };
162
+ /** the models that hear and speak in one stream, answering as they go */ talk?: Talker[];
163
+ /** the account's credit left, in dollars, where the provider says */ balance?: (ask: Ask) => Promise<number>;
164
+ /** the voices the key's account speaks in, where it keeps its own: a
165
+ * page of them, and the next page's token while there are more */ voices?: {
166
+ request: (page?: string) => Asked;
167
+ read: (said: unknown) => {
168
+ voices: Voice[];
169
+ next?: string;
170
+ };
171
+ };
172
+ /** making voices of the account's own, where the provider does */ make?: {
173
+ /** voices designed from a description, each saying `text`, or words
174
+ * the provider picks */ design: (description: string, text?: string) => Asked;
175
+ designs: (said: unknown) => Design[];
176
+ /** a design kept as a voice of the account's */ keep: (design: string, name: string, description: string) => Asked;
177
+ /** a voice cloned from recordings of it */ clone: (name: string, samples: Blob[], description?: string) => Asked;
178
+ /** the id of the voice `keep` or `clone` made */ made: (said: unknown) => string;
179
+ };
180
+ };
181
+ /** A model that speaks text, priced by the thousand characters. */ export type Speaker = {
182
+ model: string;
183
+ perThousand: number;
184
+ };
185
+ /**
186
+ * The model of `provider`'s that speaks as `model` asks: that one where it
187
+ * has it, else its first.
188
+ *
189
+ * ```ts
190
+ * import { equal } from '@yaks/testing'
191
+ * import { ELEVENLABS, speakerOf } from '@yaks/speech'
192
+ * equal(speakerOf(ELEVENLABS, 'eleven_flash_v2_5').perThousand, 0.04)
193
+ * equal(speakerOf(ELEVENLABS, 'aura-2').model, 'eleven_v4')
194
+ * ```
195
+ */ export declare let speakerOf: (provider: Provider, model?: string | null) => Speaker;
196
+ /** A model that hears audio as it streams, told back a turn at a time, as
197
+ * its provider words it. */ export type Stream = {
198
+ model: string;
199
+ perMinute: number;
200
+ /** whether it tells one speaker from another */ speakers: boolean;
201
+ path: (format: Raw, model: string) => string;
202
+ /** what is sent once the stream opens, before any audio */ opening: (format: Raw, model: string) => string[];
203
+ /** raw samples as the stream carries them */ audio: (bytes: Uint8Array) => string | Uint8Array;
204
+ /** what reads one stream's messages, made as it opens, so a provider that
205
+ * tells a turn over several messages keeps its words until it ends */ reader: () => (data: unknown) => Cue;
206
+ /** what ends the stream: the provider tells what it still heard, then
207
+ * closes */ closing?: string;
208
+ };
209
+ /** The entry of `provider`'s that streams with `model`, or its first when
210
+ * none is given.
211
+ *
212
+ * ```ts
213
+ * import { equal } from '@yaks/testing'
214
+ * import { DEEPGRAM, streamOf } from '@yaks/speech'
215
+ * equal(streamOf(DEEPGRAM).model, 'flux-general-en')
216
+ * equal(streamOf(DEEPGRAM, 'nova-3').speakers, true)
217
+ * ```
218
+ */ export declare let streamOf: (provider: Provider, model?: string) => Stream;
219
+ /** Models that hear and speak in one stream, as their provider words it. */ export type Talker = {
220
+ /** the models it offers, the fastest first */ models: string[];
221
+ voice: string;
222
+ voices: Voice[];
223
+ speeds: Speeds;
224
+ path: (model: string) => string;
225
+ opening: (setup: Setup) => string[];
226
+ audio: (bytes: Uint8Array) => string;
227
+ /** what one message means, on a stream talking with `model`, handing its
228
+ * work to `backend` where it has one */ read: (data: unknown, model: string, backend?: string) => Said[];
229
+ /** a tool's answer given back, or a delegation's */ answer: (call: string, output: string) => string[];
230
+ /** something the model is told mid-stream, for what it says next,
231
+ * answered by nothing */ tell: (text: string) => string[];
232
+ /** calls its own line carries, such as one sent to it over SIP: their
233
+ * audio never passes here, and a stream joins one to hear and steer it */ calls?: {
234
+ /** a webhook's event that a call rang: its id and the headers it came
235
+ * with, their names lower case; undefined for any other event */ rang: (event: unknown) => {
236
+ id: string;
237
+ headers: Record<string, string>;
238
+ } | undefined;
239
+ /** pick a call up, talking as `setup` says */ accept: (call: string, setup: Setup) => Asked;
240
+ reject: (call: string) => Asked;
241
+ hangup: (call: string) => Asked;
242
+ /** the path of the stream that joins a call */ join: (call: string) => string;
243
+ /** what stops the audio playing to the caller */ hush: string;
244
+ };
245
+ } & (Turned | Duplex);
246
+ /** A model that takes a turn when asked: here is said when the caller's
247
+ * turn ended, and when to stop. */ export type Turned = {
248
+ duplex?: undefined;
249
+ /** the model asked for a response: to say `text`, or without it to go
250
+ * on, asking for `tools` alone when they are given, and with `call` for
251
+ * a call of one of them, never words; `instructions` are the response's
252
+ * in place of the session's */ respond: (asked?: {
253
+ text?: string;
254
+ tools?: Setup['tools'];
255
+ call?: boolean;
256
+ instructions?: string;
257
+ }) => string;
258
+ /** what stops the response under way */ cancel: string;
259
+ /** what ends the caller's turn now, before the model hears it end */ commit: string;
260
+ };
261
+ /** A model that hears and speaks at once, as a person on the phone does,
262
+ * and takes its own turns: it hears when the caller is done and yields when
263
+ * they talk over it, sending its audio as it is said, quiet and all. Its
264
+ * tools are whoever runs the call, which it hands work to (`delegated`). */ export type Duplex = {
265
+ duplex: true;
266
+ /** what a minute of the stream costs */ perMinute: number;
267
+ /** the model asked to say `text` now */ say: (text: string) => string[];
268
+ /** the model asked for its first words, `text`, once its session started;
269
+ * the session it is set up with (`Setup.opens`) asks the same */ opens: (text: string) => string[];
270
+ /** the larger models it hands its work to on the provider's side
271
+ * (`Setup.backend`), the quickest first */ backends: string[];
272
+ /** a backend's tool call answered, and its work let go on */ result: (call: string, output: string) => string[];
273
+ };
274
+ /** The bytes a second of raw audio takes, or undefined for a compressed one. */ export declare let perSecond: (format: Format) => number | undefined;
275
+ /** What speaking `text` costs, in dollars, by `model` (`speakerOf`). */ export declare let spoken: (provider: Provider, text: string, model?: string | null) => number;
276
+ /** What hearing `seconds` of audio costs, in dollars, on a model priced by
277
+ * the minute: a provider's `hear`, or one of its `turns`. */ export declare let heard: (priced: {
278
+ perMinute: number;
279
+ }, seconds: number) => number;
280
+ /** A provider's refusal: `status` is its HTTP status, and `retry` says a
281
+ * later try may go through (a 429 or 5xx, or no answer at all). */ export declare class SpeechError extends Error {
282
+ readonly status: number;
283
+ readonly retry: boolean;
284
+ constructor(message: string, status?: number);
285
+ }
286
+ /** Text spoken: the audio as it streams, its media type, and its cost. */ export type Spoken = {
287
+ body: ReadableStream<Uint8Array>;
288
+ type: string;
289
+ dollars: number;
290
+ };
291
+ /** A recording heard: its words, its turns, its length, and its cost. */ export type Heard = {
292
+ text: string;
293
+ turns: Turn[];
294
+ seconds: number;
295
+ dollars: number;
296
+ };
297
+ /** The seconds a stream heard, and what they cost. */ export type Spent = {
298
+ seconds: number;
299
+ dollars: number;
300
+ };
301
+ /** A stream being heard. */ export type Turns = {
302
+ send: (audio: Uint8Array) => void;
303
+ /** the seconds heard so far, and what they cost */ spent: () => Spent;
304
+ /** ends the stream at once: what it was still hearing is not told */ close: () => Spent;
305
+ /** ends the stream once the provider has told every turn it heard, which
306
+ * it does before it closes */ end: () => Promise<Spent>;
307
+ };
308
+ /** What a call to the provider needs. */ export type Calling = {
309
+ provider: Provider;
310
+ key: string;
311
+ fetch?: typeof fetch;
312
+ signal?: AbortSignal;
313
+ };
314
+ /** The provider's API, asked with the key; a refusal is a SpeechError. */ export declare let asking: ({}: Calling) => Ask;
315
+ /**
316
+ * `text` spoken: the audio as it streams from the provider, its media type,
317
+ * and what it cost. Raw samples come without a container, ready for a call.
318
+ *
319
+ * ```ts
320
+ * import { equal } from '@yaks/testing'
321
+ * import { DEEPGRAM, speak } from '@yaks/speech'
322
+ * let asked: string[] = []
323
+ * let fetch = (url: string | URL | Request) => {
324
+ * asked.push(String(url))
325
+ * return Promise.resolve(new Response(new Uint8Array([1, 2, 3])))
326
+ * }
327
+ * let said = await speak({
328
+ * provider: DEEPGRAM,
329
+ * key: 'k',
330
+ * text: 'Hello',
331
+ * format: { encoding: 'mulaw', rate: 8000 },
332
+ * fetch: fetch as typeof globalThis.fetch,
333
+ * })
334
+ * equal(said.type, 'audio/basic')
335
+ * equal(asked[0].includes('container=none'), true)
336
+ * ```
337
+ */ export declare let speak: (o: Calling & {
338
+ text: string;
339
+ voice?: string;
340
+ format?: Format;
341
+ /** a multiple of the provider's pace (`paced`) */ speed?: number;
342
+ /** one of the provider's `speak.models`, its first by default */ model?: string;
343
+ }) => Promise<Spoken>;
344
+ /**
345
+ * A recording heard whole: its words, its turns, how long it was, and what
346
+ * hearing it cost. A provider that tells speakers apart (`speakers`) answers
347
+ * a turn each time one of them speaks; one that cannot, the words as one
348
+ * speaker's.
349
+ *
350
+ * ```ts
351
+ * import { equal } from '@yaks/testing'
352
+ * import { hear, OPENAI } from '@yaks/speech'
353
+ * let fetch = () =>
354
+ * Promise.resolve(Response.json({ text: 'Hi there', usage: { seconds: 30 } }))
355
+ * let got = await hear({
356
+ * provider: OPENAI,
357
+ * key: 'k',
358
+ * audio: new Uint8Array([1]),
359
+ * type: 'audio/wav',
360
+ * fetch: fetch as typeof globalThis.fetch,
361
+ * })
362
+ * equal(got.turns, [{ speaker: 0, text: 'Hi there' }])
363
+ * equal(got.dollars, 0.00225)
364
+ * ```
365
+ */ export declare let hear: (o: Calling & {
366
+ audio: BodyInit;
367
+ type: string;
368
+ seconds?: number;
369
+ }) => Promise<Heard>;
370
+ /** The account's credit left, in dollars, or undefined where the provider
371
+ * does not say. */ export declare let balance: (o: Calling) => Promise<number | undefined>;
372
+ /**
373
+ * The voices the key's account speaks in, where the provider keeps them per
374
+ * account (ElevenLabs: its premade voices and the ones it designed, cloned or
375
+ * added), else the provider's own list.
376
+ *
377
+ * ```ts
378
+ * import { equal } from '@yaks/testing'
379
+ * import { DEEPGRAM, ELEVENLABS, voices } from '@yaks/speech'
380
+ * let pages = [
381
+ * { voices: [{ voice_id: 'a', name: 'A' }], has_more: true, next_page_token: 't' },
382
+ * { voices: [{ voice_id: 'b', name: 'B' }], has_more: false },
383
+ * ]
384
+ * let asked: string[] = []
385
+ * let fetch = (url: string | URL | Request, init?: RequestInit) => {
386
+ * asked.push(String(url) + ' ' + new Headers(init?.headers).get('xi-api-key'))
387
+ * return Promise.resolve(Response.json(pages.shift()))
388
+ * }
389
+ * let got = await voices({
390
+ * provider: ELEVENLABS,
391
+ * key: 'k',
392
+ * fetch: fetch as typeof globalThis.fetch,
393
+ * })
394
+ * equal(got.map((v) => v.id), ['a', 'b'])
395
+ * equal(asked[1].includes('next_page_token=t'), true)
396
+ * equal(asked[1].endsWith(' k'), true)
397
+ * equal((await voices({ provider: DEEPGRAM, key: 'k' })).length > 0, true)
398
+ * ```
399
+ */ export declare let voices: (o: Calling) => Promise<Voice[]>;
400
+ /**
401
+ * Voices designed from `description`, each a sample saying `text` (or words
402
+ * the provider picks), for one to be kept with `keep`.
403
+ *
404
+ * ```ts
405
+ * import { equal } from '@yaks/testing'
406
+ * import { design, ELEVENLABS } from '@yaks/speech'
407
+ * let fetch = () =>
408
+ * Promise.resolve(Response.json({
409
+ * previews: [{ generated_voice_id: 'g1', audio_base_64: 'AQID', duration_secs: 4 }],
410
+ * }))
411
+ * let got = await design({
412
+ * provider: ELEVENLABS,
413
+ * key: 'k',
414
+ * description: 'An old sea captain, gravelly and warm',
415
+ * fetch: fetch as typeof globalThis.fetch,
416
+ * })
417
+ * equal(got, [{ id: 'g1', audio: new Uint8Array([1, 2, 3]), type: 'audio/mpeg', seconds: 4 }])
418
+ * ```
419
+ */ export declare let design: (o: Calling & {
420
+ description: string;
421
+ text?: string;
422
+ }) => Promise<Design[]>;
423
+ /** A design (`design`) kept as a voice of the account's, answering its id. */ export declare let keep: (o: Calling & {
424
+ design: string;
425
+ name: string;
426
+ description: string;
427
+ }) => Promise<string>;
428
+ /**
429
+ * A voice cloned from recordings of it, answering its id. A provider refuses
430
+ * one the account's plan does not allow, with its reason.
431
+ *
432
+ * ```ts
433
+ * import { equal } from '@yaks/testing'
434
+ * import { clone, ELEVENLABS } from '@yaks/speech'
435
+ * let sent: FormData | undefined
436
+ * let fetch = (_: unknown, init?: RequestInit) => {
437
+ * sent = init?.body as FormData
438
+ * return Promise.resolve(Response.json({ voice_id: 'v9' }))
439
+ * }
440
+ * let id = await clone({
441
+ * provider: ELEVENLABS,
442
+ * key: 'k',
443
+ * name: 'Narrator',
444
+ * samples: [new Blob([new Uint8Array([1])], { type: 'audio/mpeg' })],
445
+ * fetch: fetch as typeof globalThis.fetch,
446
+ * })
447
+ * equal([id, sent?.get('name'), sent?.getAll('files').length], ['v9', 'Narrator', 1])
448
+ * ```
449
+ */ export declare let clone: (o: Calling & {
450
+ name: string;
451
+ samples: Blob[];
452
+ description?: string;
453
+ }) => Promise<string>;
454
+ /** The socket a stream speaks over: the platform's WebSocket, or a stand-in. */ export type Socket = {
455
+ send: (data: string | Uint8Array) => void;
456
+ close: (code?: number, reason?: string) => void;
457
+ addEventListener: (type: 'open' | 'message' | 'close' | 'error', listener: (event: {
458
+ data?: unknown;
459
+ code?: number;
460
+ }) => void) => void;
461
+ };
462
+ /** How a stream's key is offered: as a header where the platform's sockets
463
+ * take one, else as subprotocols. */ export type Keyed = {
464
+ authorization: string;
465
+ protocols: string[];
466
+ };
467
+ /** What makes a stream's socket. */ export type Open = (url: string, key: Keyed) => Socket;
468
+ /** What a stream tells as a speaker talks. */ export type Told = {
469
+ /** they started talking: whatever is playing to them should stop */ start?: () => void;
470
+ /** their words so far in one utterance, empty if it was discarded */ hearing?: (text: string, key?: string) => void;
471
+ /** they finished a turn: what they said, who said it and when */ end: (turn: Turn) => void;
472
+ /** the stream failed or closed before `close` was asked */ lost?: (reason: string) => void;
473
+ };
474
+ /**
475
+ * Audio heard as it streams, told back a turn at a time (`told`), by
476
+ * `model`, one of the provider's `turns` (its first by default). `send`
477
+ * takes raw samples in `format`; `close` ends the stream and answers how
478
+ * many seconds were heard and what they cost, and `end` answers the same
479
+ * once the provider has told the turns it was still hearing. `open` makes
480
+ * the socket, the platform's WebSocket by default, offering the key as
481
+ * subprotocols.
482
+ */ export declare let turns: (o: {
483
+ provider: Provider;
484
+ key: string;
485
+ format: Raw;
486
+ told: Told;
487
+ model?: string;
488
+ open?: Open;
489
+ }) => Turns;
490
+ /** Words supplied to a talking model, with their author where known. */ export type Words = string | {
491
+ text: string;
492
+ from: string;
493
+ };
494
+ /** What a talk stream tells as the caller and the model speak. */ export type Talked = {
495
+ /** the caller talked over the model: whatever is playing should stop */ over?: () => void;
496
+ /** the caller stopped talking, `ago` seconds before now (none: now); the
497
+ * model's answer is on its way */ stopped?: (ago?: number) => void;
498
+ /** the caller's words so far in one utterance, empty if discarded */ hearing?: (text: string, key?: string) => void;
499
+ /** what the caller said, once heard */ heard?: (text: string, key?: string) => void;
500
+ /** audio to play to the caller */ audio: (bytes: Uint8Array) => void;
501
+ /** what the model said, once said */ said?: (text: string) => void;
502
+ /** a tool the model asked for, answered with its output */ call: (name: string, args: string, speaking?: boolean) => Promise<Words>;
503
+ /** work a duplex model handed over: what the caller asked, in their
504
+ * words, answered with what to tell them */ delegate?: (request: string, speaking?: boolean) => Promise<Words>;
505
+ /** the model's response ended, whatever it said or asked in it */ done?: () => void;
506
+ /** the model's audio began playing, on a call its own line carries */ plays?: () => void;
507
+ /** the call a joined stream steers ended: its stream closed cleanly */ ended?: () => void;
508
+ /** the stream failed or closed before `close` was asked */ lost?: (reason: string) => void;
509
+ /** a duplex model refused something it was sent, its stream still open */ refused?: (reason: string) => void;
510
+ };
511
+ /** A talk stream: audio in, the model's audio and words out. */ export type Talk = {
512
+ send: (audio: Uint8Array) => void;
513
+ /** ask the model to say `text` */ say: (text: string, from?: string) => void;
514
+ /** tell the model something it knows from now on, saying nothing of it */ tell: (text: string, from?: string) => void;
515
+ /** the seconds heard so far, and what the stream cost */ spent: () => {
516
+ seconds: number;
517
+ dollars: number;
518
+ };
519
+ /** end the stream; what it cost */ close: () => {
520
+ seconds: number;
521
+ dollars: number;
522
+ };
523
+ };
524
+ /** Whether `text` holds a word a transcript may have made of `name`, which
525
+ * it mishears even when told to expect it: one of the same letters where
526
+ * `name` has consonants, and any vowels where it has vowels ("Amber" for
527
+ * "Ember"). A name of fewer than three consonants sounds like too many words
528
+ * to be told apart this way.
529
+ *
530
+ * ```ts
531
+ * import { equal } from '@yaks/testing'
532
+ * import { heardAs } from '@yaks/speech'
533
+ * let heard = ['Thanks, Amber.', 'ember, you there?', 'remember the barn',
534
+ * 'Embers']
535
+ * equal(heard.map((t) => heardAs(t, 'Ember')), [true, true, false, false])
536
+ * equal(heardAs('Eve said so', 'Ava'), false)
537
+ * ```
538
+ */ export declare let heardAs: (text: string, name: string) => boolean;
539
+ /** How many of `text`'s words say something, the `FILLERS` aside.
540
+ *
541
+ * ```ts
542
+ * import { equal } from '@yaks/testing'
543
+ * import { meant } from '@yaks/speech'
544
+ * equal(meant(' Okay. Ha. Cool, uh'), 0)
545
+ * equal(meant('Okay, hold on'), 2)
546
+ * ```
547
+ */ export declare let meant: (text: string) => number;
548
+ /** Whether `text` stops where a sentence goes on: on a comma or a dash, or
549
+ * on a word a sentence goes on after (`ON`: "the", "to", "is", "um"). A caller
550
+ * heard to stop there has more to say.
551
+ *
552
+ * ```ts
553
+ * import { equal } from '@yaks/testing'
554
+ * import { open } from '@yaks/speech'
555
+ * let lines = ['My address is', 'So what I need is, um', 'Okay,', 'Okay.',
556
+ * 'Forty-two Elm Street.', 'Can you come out tomorrow?', '']
557
+ * equal(lines.map(open), [true, true, true, false, false, false, false])
558
+ * ```
559
+ */ export declare let open: (text: string) => boolean;
560
+ /**
561
+ * A call's first words, such as its greeting, said before the caller is
562
+ * heard: a hello of theirs said over them, or the sound of the line picking
563
+ * up, is no turn to answer. `said` takes the words' audio as it is given to
564
+ * the line, which may be faster than it plays, and `done` that it is all
565
+ * given. `heard` takes the caller's audio a frame at a time, as the line
566
+ * sends it, and answers what whoever listens should hear: a quiet line
567
+ * (`muted`) until the words have played, which they have once the line has
568
+ * run as long as they last. Words not begun in 6 s are given up, and the
569
+ * caller is heard.
570
+ *
571
+ * ```ts
572
+ * import { equal } from '@yaks/testing'
573
+ * import { first, sounds } from '@yaks/speech'
574
+ * let mulaw = { encoding: 'mulaw', rate: 8000 } as const
575
+ * let words = first(mulaw)
576
+ * let hello = (seconds: number) =>
577
+ * sounds(words.heard(new Uint8Array(seconds * 8000).fill(0x10)), mulaw)
578
+ * equal(hello(0.5), false)
579
+ * words.said(new Uint8Array(8000))
580
+ * words.done()
581
+ * equal([hello(0.5), hello(0.5), hello(1)], [false, true, true])
582
+ * ```
583
+ */ export declare let first: (format: Raw) => {
584
+ said: (audio: Uint8Array) => void;
585
+ done: () => void;
586
+ heard: (audio: Uint8Array) => Uint8Array;
587
+ };
588
+ /** How a talking model is set up: its model, voice, pace, instructions and
589
+ * tools, each its provider's own where none is given. */ export type Talking = {
590
+ model?: string;
591
+ voice?: string;
592
+ /** a multiple of the provider's pace (`paced`) */ speed?: number;
593
+ instructions: string;
594
+ tools?: Setup['tools'];
595
+ /** its first words, said as the stream opens: the model is asked for them
596
+ * once, and the caller's audio reaches it as a quiet line until they have
597
+ * played, so a sound made as the call is picked up, or a hello said over
598
+ * them, is no turn it answers */ opens?: string;
599
+ /** Keep listening while the opening words play. */ interruptible?: boolean;
600
+ /** a duplex model's larger model on the provider's side (`Setup.backend`) */ backend?: Backend;
601
+ /** words its hearing should expect, such as the names it is called by */ words?: string[];
602
+ /** whether words heard are said to the model, such as naming it: while it
603
+ * listens, a turn they end is answered in words */ addressed?: (words: string) => boolean;
604
+ /** a turned model only listens (`HUSH`), always, as in a room of several
605
+ * people: each turn is put to it as a choice of tools, and it speaks only
606
+ * by calling `ANSWER` */ listening?: boolean;
607
+ };
608
+ /** The entry of `provider`'s that talks with `model`, or with its first
609
+ * model when none is given, if any. */ export declare let talkerOf: (provider: Provider, model?: string) => Talker | undefined;
610
+ /** A call the provider's own line carries picked up, its model set up as
611
+ * `talk` sets one up; `talk` with the `call` then joins it. */ export declare let accept: (o: Calling & Talking & {
612
+ call: string;
613
+ }) => Promise<void>;
614
+ /** A call the provider's own line carries, refused before it is picked up. */ export declare let reject: (o: Calling & {
615
+ call: string;
616
+ }) => Promise<void>;
617
+ /** A call the provider's own line carries, ended. */ export declare let hangup: (o: Calling & {
618
+ call: string;
619
+ }) => Promise<void>;
620
+ /**
621
+ * A model that hears and speaks in one stream: `send` takes the caller's raw
622
+ * samples in `format`, and `told` hears what the model says back and the
623
+ * tools it asks for, each answered with its output before it goes on. The
624
+ * provider's `talk` entry for `model` says how (its first model by default);
625
+ * one without it refuses.
626
+ *
627
+ * Given `opens`, the model's first words, it is asked for them as the stream
628
+ * opens, and the caller's audio reaches it as a quiet line (`muted`) until
629
+ * they have played: what the caller says over them, a hello or the sound of
630
+ * picking up, is no turn the model answers with a greeting of its own. A
631
+ * model that never begins them hears the caller after `SILENT`. A call its
632
+ * own line carries is asked for them too, its audio heard throughout.
633
+ *
634
+ * Asked to say given words (`opens`, `say`), a `Turned` model answers with
635
+ * no tool, and only its answer plays: a part it says before the answer
636
+ * (`Said.aside`) would be the words a second time.
637
+ *
638
+ * A `Duplex` model takes its own turns and hands its work to
639
+ * `told.delegate`; what it is asked to `say` while the caller talks is sent
640
+ * once they are `QUIET`. Everything below is for a `Turned` one, which
641
+ * takes a turn as it is told here.
642
+ *
643
+ * A caller who talks over the model stops it (`over`) once their words say
644
+ * something (`meant`): an "okay", a laugh, or a noise, lets it go on. The
645
+ * model writes its audio faster than it is said, so what it says is mostly
646
+ * still playing, its response long done, when the caller talks.
647
+ *
648
+ * The caller's turn ends when the model hears it end, or once they are
649
+ * `QUIET`, and is answered. What the model says to it waits for the caller's
650
+ * words, and is never said when they stopped where a sentence goes on
651
+ * (`open`), when the model asks to `LISTEN`, or when the caller talks again
652
+ * before it plays. A turn left unanswered is answered once they have been
653
+ * quiet `LONG`.
654
+ *
655
+ * Asked to stay silent (`HUSH`), or always (`listening`), the model only
656
+ * listens: each turn is still put to it once its words are heard, but as a
657
+ * choice of tools, never words, so it keeps hearing and may work without a
658
+ * word, and nothing is said until it calls `ANSWER`; a turn left unanswered
659
+ * stays so. A turn whose words are said to it (`addressed`) is answered.
660
+ *
661
+ * Given a `call` its own line carries, picked up with `accept`, the stream
662
+ * joins that call instead: the caller's audio and the model's go over the
663
+ * line, never through `send` or `told.audio`, and what plays is stopped on
664
+ * the line; `told.plays` says when the model's audio begins, and
665
+ * `told.ended` when the call ends. Its audio never heard here, a turn ends
666
+ * only when the model hears it end, and what the model says plays before
667
+ * the turn's words are heard.
668
+ */ export declare let talk: (o: Talking & {
669
+ provider: Provider;
670
+ key: string;
671
+ /** the caller's audio, on a stream that carries it */ format?: Raw;
672
+ /** the call a stream joins, one the provider's own line carries */ call?: string;
673
+ told: Talked;
674
+ open?: Open;
675
+ time?: Clock;
676
+ }) => Talk;
677
+ //# sourceMappingURL=mod.d.ts.map