@ai-ecoverse/pi-audio 0.0.0-stage → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,191 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to the Licensor for inclusion in the Work by the copyright
52
+ owner or by an individual or Legal Entity authorized to submit on
53
+ behalf of the copyright owner. For the purposes of this definition,
54
+ "submitted" means any form of electronic, verbal, or written
55
+ communication sent to the Licensor or its representatives, including
56
+ but not limited to communication on electronic mailing lists, source
57
+ code control systems, and issue tracking systems that are managed by,
58
+ or on behalf of, the Licensor for the purpose of discussing and
59
+ improving the Work, but excluding communication that is conspicuously
60
+ marked or otherwise designated in writing by the copyright owner as
61
+ "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ Copyright 2025 ai-ecoverse
180
+
181
+ Licensed under the Apache License, Version 2.0 (the "License");
182
+ you may not use this file except in compliance with the License.
183
+ You may obtain a copy of the License at
184
+
185
+ http://www.apache.org/licenses/LICENSE-2.0
186
+
187
+ Unless required by applicable law or agreed to in writing, software
188
+ distributed under the License is distributed on an "AS IS" BASIS,
189
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190
+ See the License for the specific language governing permissions and
191
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,31 @@
1
- # Temporary Holding Version
1
+ # pi-audio
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Voice bindings for [pi](https://github.com/earendil-works/pi): speech-to-text, text-to-speech and realtime speech, for pi-coding-agent plugins and [slicc-agent](https://github.com/ai-ecoverse/slicc-agent).
4
+
5
+ ```sh
6
+ npm install @ai-ecoverse/pi-audio
7
+ ```
8
+
9
+ The package is a TypeScript ES module library for Node 24 and newer. It peers `@earendil-works/pi-ai`.
10
+
11
+ Transcription, speech, and realtime models are added to pi-ai's `ModelTypeMap` by declaration merging. They are not chat models: `Models.getAllModels()` does not list them. Register them with `registerVoiceModel` and look them up with `getVoiceModel` / `listVoiceModels`. Operations dispatch on `model.api` through `registerTranscriptionApiProvider`, `registerSpeechApiProvider`, and `registerRealtimeApiProvider`. A stored chat credential is reused with `resolveVoiceAuth(models, voiceModel)`, which calls `Models.getAuth` for that provider. Provider adapters ship as their own subpath exports (`@ai-ecoverse/pi-audio/openai`, and the rest) so a host loads only the ones it uses.
12
+
13
+ ## Development
14
+
15
+ ```sh
16
+ npm ci
17
+ npm run lint
18
+ npm test
19
+ ```
20
+
21
+ - `npm run lint` runs [slicc-lint](https://github.com/ai-ecoverse/slicc-shared-web): Biome, the type check, the no-comments rule, no unit tests in git, and the AGENTS.md limit. lefthook runs the same checks before each commit.
22
+ - `npm test` builds `dist/` and runs the integration tests in `test/integration/`.
23
+ - Unit tests live in the gitignored `test/unit/` and run with `npm run test:unit`.
24
+
25
+ ## Releases
26
+
27
+ Every push to `main` runs semantic-release, which reads the conventional commit messages, tags the version and writes the GitHub release. A second job then publishes the tag to npm through trusted publishing (OIDC), with provenance, from GitHub-hosted runners. Nothing is published from a branch or from a laptop.
28
+
29
+ ## License
30
+
31
+ Apache-2.0
package/dist/audio.js ADDED
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=audio.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
package/dist/auth.js ADDED
@@ -0,0 +1,7 @@
1
+ function resolveVoiceAuth(models, model, overrides) {
2
+ return models.getAuth(model, overrides);
3
+ }
4
+ export {
5
+ resolveVoiceAuth
6
+ };
7
+ //# sourceMappingURL=auth.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/auth.ts"],
4
+ "sourcesContent": ["import type { AuthResult, Models } from '@earendil-works/pi-ai';\nimport type { VoiceModel } from './models.ts';\n\nexport function resolveVoiceAuth(\n models: Models,\n model: VoiceModel,\n overrides?: Parameters<Models['getAuth']>[1]\n): Promise<AuthResult | undefined> {\n return models.getAuth(model, overrides);\n}\n"],
5
+ "mappings": "AAGO,SAAS,iBACd,QACA,OACA,WACiC;AACjC,SAAO,OAAO,QAAQ,OAAO,SAAS;AACxC;",
6
+ "names": []
7
+ }
@@ -0,0 +1,14 @@
1
+ function blankUsage() {
2
+ return {
3
+ input: 0,
4
+ output: 0,
5
+ cacheRead: 0,
6
+ cacheWrite: 0,
7
+ totalTokens: 0,
8
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
9
+ };
10
+ }
11
+ export {
12
+ blankUsage
13
+ };
14
+ //# sourceMappingURL=blank-usage.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/blank-usage.ts"],
4
+ "sourcesContent": ["import type { Usage } from '@earendil-works/pi-ai';\n\nexport function blankUsage(): Usage {\n return {\n input: 0,\n output: 0,\n cacheRead: 0,\n cacheWrite: 0,\n totalTokens: 0,\n cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n };\n}\n"],
5
+ "mappings": "AAEO,SAAS,aAAoB;AAClC,SAAO;AAAA,IACL,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,YAAY;AAAA,IACZ,aAAa;AAAA,IACb,MAAM,EAAE,OAAO,GAAG,QAAQ,GAAG,WAAW,GAAG,YAAY,GAAG,OAAO,EAAE;AAAA,EACrE;AACF;",
6
+ "names": []
7
+ }
package/dist/errors.js ADDED
@@ -0,0 +1,12 @@
1
+ class VoiceRequestError extends Error {
2
+ reason;
3
+ constructor(reason, message) {
4
+ super(message);
5
+ this.name = "VoiceRequestError";
6
+ this.reason = reason;
7
+ }
8
+ }
9
+ export {
10
+ VoiceRequestError
11
+ };
12
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/errors.ts"],
4
+ "sourcesContent": ["export class VoiceRequestError extends Error {\n readonly reason: 'aborted' | 'error';\n\n constructor(reason: 'aborted' | 'error', message: string) {\n super(message);\n this.name = 'VoiceRequestError';\n this.reason = reason;\n }\n}\n"],
5
+ "mappings": "AAAO,MAAM,0BAA0B,MAAM;AAAA,EAClC;AAAA,EAET,YAAY,QAA6B,SAAiB;AACxD,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;",
6
+ "names": []
7
+ }
package/dist/index.js ADDED
@@ -0,0 +1,37 @@
1
+ import { resolveVoiceAuth } from "./auth.js";
2
+ import { blankUsage } from "./blank-usage.js";
3
+ import { VoiceRequestError } from "./errors.js";
4
+ import {
5
+ getVoiceModel,
6
+ listVoiceModels,
7
+ registerVoiceModel
8
+ } from "./models.js";
9
+ import {
10
+ connectRealtime,
11
+ listen,
12
+ registerRealtimeApiProvider,
13
+ registerSpeechApiProvider,
14
+ registerTranscriptionApiProvider,
15
+ speak,
16
+ transcribe
17
+ } from "./registry.js";
18
+ import { SpeechEventStream } from "./speech.js";
19
+ import { TranscriptionEventStream } from "./transcription.js";
20
+ export {
21
+ SpeechEventStream,
22
+ TranscriptionEventStream,
23
+ VoiceRequestError,
24
+ blankUsage,
25
+ connectRealtime,
26
+ getVoiceModel,
27
+ listVoiceModels,
28
+ listen,
29
+ registerRealtimeApiProvider,
30
+ registerSpeechApiProvider,
31
+ registerTranscriptionApiProvider,
32
+ registerVoiceModel,
33
+ resolveVoiceAuth,
34
+ speak,
35
+ transcribe
36
+ };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts"],
4
+ "sourcesContent": ["export type {\n AudioChunk,\n AudioEncoding,\n AudioFormat,\n AudioSink,\n AudioSource,\n AudioTag,\n CharAlignment,\n TextSpan,\n} from './audio.ts';\nexport { resolveVoiceAuth } from './auth.ts';\nexport { blankUsage } from './blank-usage.ts';\nexport { VoiceRequestError } from './errors.ts';\nexport type {\n Delivery,\n DeliverySupport,\n KnownRealtimeApi,\n KnownSpeechApi,\n KnownTranscriptionApi,\n RealtimeApi,\n RealtimeModel,\n SpeechApi,\n SpeechModel,\n TranscriptionApi,\n TranscriptionModel,\n VoiceInfo,\n VoiceModel,\n VoiceModelType,\n} from './models.ts';\nexport {\n getVoiceModel,\n listVoiceModels,\n registerVoiceModel,\n} from './models.ts';\nexport type {\n RealtimeApiProvider,\n RealtimeContext,\n RealtimeEvent,\n RealtimeOptions,\n RealtimeSession,\n RealtimeToken,\n} from './realtime.ts';\nexport {\n connectRealtime,\n listen,\n registerRealtimeApiProvider,\n registerSpeechApiProvider,\n registerTranscriptionApiProvider,\n speak,\n transcribe,\n} from './registry.ts';\nexport type { SpeechApiProvider, SpeechEvent, SpeechInput } from './speech.ts';\nexport { SpeechEventStream } from './speech.ts';\nexport type {\n AudioInput,\n ListenOptions,\n Transcript,\n TranscriptionApiProvider,\n TranscriptionEvent,\n TranscriptSegment,\n} from './transcription.ts';\nexport { TranscriptionEventStream } from './transcription.ts';\n"],
5
+ "mappings": "AAUA,SAAS,wBAAwB;AACjC,SAAS,kBAAkB;AAC3B,SAAS,yBAAyB;AAiBlC;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AASP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,yBAAyB;AASlC,SAAS,gCAAgC;",
6
+ "names": []
7
+ }
package/dist/models.js ADDED
@@ -0,0 +1,29 @@
1
+ const catalog = /* @__PURE__ */ new Map();
2
+ function catalogKey(type, provider, id) {
3
+ return `${type}\0${provider}\0${id}`;
4
+ }
5
+ function registerVoiceModel(model) {
6
+ const key = catalogKey(model.type, model.provider, model.id);
7
+ catalog.set(key, model);
8
+ return () => {
9
+ if (catalog.get(key) === model) catalog.delete(key);
10
+ };
11
+ }
12
+ function getVoiceModel(type, provider, id) {
13
+ return catalog.get(catalogKey(type, provider, id));
14
+ }
15
+ function listVoiceModels(type, provider) {
16
+ const found = [];
17
+ for (const model of catalog.values()) {
18
+ if (model.type !== type) continue;
19
+ if (provider !== void 0 && model.provider !== provider) continue;
20
+ found.push(model);
21
+ }
22
+ return found;
23
+ }
24
+ export {
25
+ getVoiceModel,
26
+ listVoiceModels,
27
+ registerVoiceModel
28
+ };
29
+ //# sourceMappingURL=models.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/models.ts"],
4
+ "sourcesContent": ["import type { BaseModel } from '@earendil-works/pi-ai';\nimport type { AudioFormat } from './audio.ts';\n\nexport type KnownTranscriptionApi =\n | 'openai-transcriptions'\n | 'openai-realtime-transcription'\n | 'xai-stt'\n | 'elevenlabs-scribe'\n | 'mistral-transcriptions'\n | 'fish-asr'\n | 'cloudflare-ai-run';\n\nexport type TranscriptionApi = KnownTranscriptionApi | (string & {});\n\nexport type KnownSpeechApi =\n | 'openai-speech'\n | 'xai-tts'\n | 'elevenlabs-tts'\n | 'mistral-speech'\n | 'fish-tts'\n | 'google-tts'\n | 'cloudflare-ai-run'\n | 'wavespeed';\n\nexport type SpeechApi = KnownSpeechApi | (string & {});\n\nexport type KnownRealtimeApi =\n | 'openai-realtime'\n | 'xai-realtime'\n | 'gemini-live'\n | 'elevenlabs-agents'\n | 'fish-agents';\n\nexport type RealtimeApi = KnownRealtimeApi | (string & {});\n\nexport type Delivery = 'interrupt' | 'when_idle' | 'silent';\n\nexport type DeliverySupport = 'native' | 'approximate' | 'unsupported';\n\nexport interface VoiceInfo {\n id: string;\n name: string;\n languages?: string[];\n}\n\nexport interface TranscriptionModel<TApi extends TranscriptionApi>\n extends Omit<BaseModel<TApi>, 'input'> {\n type: 'transcription';\n streaming: boolean;\n turnDetection: 'none' | 'vad' | 'semantic';\n inputFormats: AudioFormat[];\n languages?: string[];\n diarization?: boolean;\n}\n\nexport interface SpeechModel<TApi extends SpeechApi> extends Omit<BaseModel<TApi>, 'input'> {\n type: 'speech';\n textStreaming: boolean;\n outputFormats: AudioFormat[];\n voices?: VoiceInfo[];\n cloning?: boolean;\n}\n\nexport interface RealtimeModel<TApi extends RealtimeApi> extends Omit<BaseModel<TApi>, 'input'> {\n type: 'realtime';\n transports: ('websocket' | 'webrtc')[];\n toolCalls: 'blocking' | 'async';\n inputFormat: AudioFormat;\n outputFormat: AudioFormat;\n video?: { maxFps: number };\n maxSessionMs?: number;\n resumable: boolean;\n ephemeralAuth: boolean;\n voices: VoiceInfo[];\n cascade?: { stt: string; llm: string; tts: string };\n provisioning: 'none' | 'agent';\n delivery: Record<Delivery, DeliverySupport>;\n truncation: 'client' | 'server' | 'none';\n liveToolUpdate: boolean;\n toolTimeoutMs?: { min: number; max: number };\n}\n\ndeclare module '@earendil-works/pi-ai' {\n interface ModelTypeMap {\n transcription: TranscriptionModel<TranscriptionApi>;\n speech: SpeechModel<SpeechApi>;\n realtime: RealtimeModel<RealtimeApi>;\n }\n}\n\nexport type VoiceModel =\n | TranscriptionModel<TranscriptionApi>\n | SpeechModel<SpeechApi>\n | RealtimeModel<RealtimeApi>;\n\nexport type VoiceModelType = VoiceModel['type'];\n\nconst catalog = new Map<string, VoiceModel>();\n\nfunction catalogKey(type: string, provider: string, id: string): string {\n return `${type}\\0${provider}\\0${id}`;\n}\n\nexport function registerVoiceModel(model: VoiceModel): () => void {\n const key = catalogKey(model.type, model.provider, model.id);\n catalog.set(key, model);\n return () => {\n if (catalog.get(key) === model) catalog.delete(key);\n };\n}\n\nexport function getVoiceModel<TType extends VoiceModelType>(\n type: TType,\n provider: string,\n id: string\n): Extract<VoiceModel, { type: TType }> | undefined {\n return catalog.get(catalogKey(type, provider, id)) as\n | Extract<VoiceModel, { type: TType }>\n | undefined;\n}\n\nexport function listVoiceModels<TType extends VoiceModelType>(\n type: TType,\n provider?: string\n): Extract<VoiceModel, { type: TType }>[] {\n const found: Extract<VoiceModel, { type: TType }>[] = [];\n for (const model of catalog.values()) {\n if (model.type !== type) continue;\n if (provider !== undefined && model.provider !== provider) continue;\n found.push(model as Extract<VoiceModel, { type: TType }>);\n }\n return found;\n}\n"],
5
+ "mappings": "AAiGA,MAAM,UAAU,oBAAI,IAAwB;AAE5C,SAAS,WAAW,MAAc,UAAkB,IAAoB;AACtE,SAAO,GAAG,IAAI,KAAK,QAAQ,KAAK,EAAE;AACpC;AAEO,SAAS,mBAAmB,OAA+B;AAChE,QAAM,MAAM,WAAW,MAAM,MAAM,MAAM,UAAU,MAAM,EAAE;AAC3D,UAAQ,IAAI,KAAK,KAAK;AACtB,SAAO,MAAM;AACX,QAAI,QAAQ,IAAI,GAAG,MAAM,MAAO,SAAQ,OAAO,GAAG;AAAA,EACpD;AACF;AAEO,SAAS,cACd,MACA,UACA,IACkD;AAClD,SAAO,QAAQ,IAAI,WAAW,MAAM,UAAU,EAAE,CAAC;AAGnD;AAEO,SAAS,gBACd,MACA,UACwC;AACxC,QAAM,QAAgD,CAAC;AACvD,aAAW,SAAS,QAAQ,OAAO,GAAG;AACpC,QAAI,MAAM,SAAS,KAAM;AACzB,QAAI,aAAa,UAAa,MAAM,aAAa,SAAU;AAC3D,UAAM,KAAK,KAA6C;AAAA,EAC1D;AACA,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=realtime.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,57 @@
1
+ const transcriptionApis = /* @__PURE__ */ new Map();
2
+ const speechApis = /* @__PURE__ */ new Map();
3
+ const realtimeApis = /* @__PURE__ */ new Map();
4
+ function missing(api) {
5
+ return new Error(`No API provider registered for api: ${api}`);
6
+ }
7
+ function registerTranscriptionApiProvider(provider) {
8
+ const entry = { provider };
9
+ transcriptionApis.set(provider.api, entry);
10
+ return () => {
11
+ if (transcriptionApis.get(provider.api) === entry) transcriptionApis.delete(provider.api);
12
+ };
13
+ }
14
+ function registerSpeechApiProvider(provider) {
15
+ const entry = { provider };
16
+ speechApis.set(provider.api, entry);
17
+ return () => {
18
+ if (speechApis.get(provider.api) === entry) speechApis.delete(provider.api);
19
+ };
20
+ }
21
+ function registerRealtimeApiProvider(provider) {
22
+ const entry = { provider };
23
+ realtimeApis.set(provider.api, entry);
24
+ return () => {
25
+ if (realtimeApis.get(provider.api) === entry) realtimeApis.delete(provider.api);
26
+ };
27
+ }
28
+ function transcribe(model, input, options) {
29
+ const transcribeModel = transcriptionApis.get(model.api)?.provider.transcribe;
30
+ if (!transcribeModel) return Promise.reject(missing(model.api));
31
+ return transcribeModel(model, input, options);
32
+ }
33
+ function listen(model, audio, options) {
34
+ const listenModel = transcriptionApis.get(model.api)?.provider.listen;
35
+ if (!listenModel) throw missing(model.api);
36
+ return listenModel(model, audio, options);
37
+ }
38
+ function speak(model, input, options) {
39
+ const speakModel = speechApis.get(model.api)?.provider.speak;
40
+ if (!speakModel) throw missing(model.api);
41
+ return speakModel(model, input, options);
42
+ }
43
+ function connectRealtime(model, context, options) {
44
+ const connect = realtimeApis.get(model.api)?.provider.connect;
45
+ if (!connect) return Promise.reject(missing(model.api));
46
+ return connect(model, context, options);
47
+ }
48
+ export {
49
+ connectRealtime,
50
+ listen,
51
+ registerRealtimeApiProvider,
52
+ registerSpeechApiProvider,
53
+ registerTranscriptionApiProvider,
54
+ speak,
55
+ transcribe
56
+ };
57
+ //# sourceMappingURL=registry.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/registry.ts"],
4
+ "sourcesContent": ["import type { ProviderRequestOptions } from '@earendil-works/pi-ai';\nimport type { AudioSource } from './audio.ts';\nimport type { RealtimeModel, SpeechModel, TranscriptionModel } from './models.ts';\nimport type {\n RealtimeApiProvider,\n RealtimeContext,\n RealtimeOptions,\n RealtimeSession,\n} from './realtime.ts';\nimport type { SpeechApiProvider, SpeechEventStream, SpeechInput } from './speech.ts';\nimport type {\n AudioInput,\n ListenOptions,\n Transcript,\n TranscriptionApiProvider,\n TranscriptionEventStream,\n} from './transcription.ts';\n\ninterface TranscriptionEntry {\n provider: TranscriptionApiProvider;\n}\n\ninterface SpeechEntry {\n provider: SpeechApiProvider;\n}\n\ninterface RealtimeEntry {\n provider: RealtimeApiProvider;\n}\n\nconst transcriptionApis = new Map<string, TranscriptionEntry>();\nconst speechApis = new Map<string, SpeechEntry>();\nconst realtimeApis = new Map<string, RealtimeEntry>();\n\nfunction missing(api: string): Error {\n return new Error(`No API provider registered for api: ${api}`);\n}\n\nexport function registerTranscriptionApiProvider(provider: TranscriptionApiProvider): () => void {\n const entry = { provider };\n transcriptionApis.set(provider.api, entry);\n return () => {\n if (transcriptionApis.get(provider.api) === entry) transcriptionApis.delete(provider.api);\n };\n}\n\nexport function registerSpeechApiProvider(provider: SpeechApiProvider): () => void {\n const entry = { provider };\n speechApis.set(provider.api, entry);\n return () => {\n if (speechApis.get(provider.api) === entry) speechApis.delete(provider.api);\n };\n}\n\nexport function registerRealtimeApiProvider(provider: RealtimeApiProvider): () => void {\n const entry = { provider };\n realtimeApis.set(provider.api, entry);\n return () => {\n if (realtimeApis.get(provider.api) === entry) realtimeApis.delete(provider.api);\n };\n}\n\nexport function transcribe(\n model: TranscriptionModel<string>,\n input: AudioInput,\n options?: ProviderRequestOptions\n): Promise<Transcript> {\n const transcribeModel = transcriptionApis.get(model.api)?.provider.transcribe;\n if (!transcribeModel) return Promise.reject(missing(model.api));\n return transcribeModel(model, input, options);\n}\n\nexport function listen(\n model: TranscriptionModel<string>,\n audio: AudioSource,\n options?: ListenOptions\n): TranscriptionEventStream {\n const listenModel = transcriptionApis.get(model.api)?.provider.listen;\n if (!listenModel) throw missing(model.api);\n return listenModel(model, audio, options);\n}\n\nexport function speak(\n model: SpeechModel<string>,\n input: SpeechInput,\n options?: ProviderRequestOptions\n): SpeechEventStream {\n const speakModel = speechApis.get(model.api)?.provider.speak;\n if (!speakModel) throw missing(model.api);\n return speakModel(model, input, options);\n}\n\nexport function connectRealtime(\n model: RealtimeModel<string>,\n context: RealtimeContext,\n options?: RealtimeOptions\n): Promise<RealtimeSession> {\n const connect = realtimeApis.get(model.api)?.provider.connect;\n if (!connect) return Promise.reject(missing(model.api));\n return connect(model, context, options);\n}\n"],
5
+ "mappings": "AA8BA,MAAM,oBAAoB,oBAAI,IAAgC;AAC9D,MAAM,aAAa,oBAAI,IAAyB;AAChD,MAAM,eAAe,oBAAI,IAA2B;AAEpD,SAAS,QAAQ,KAAoB;AACnC,SAAO,IAAI,MAAM,uCAAuC,GAAG,EAAE;AAC/D;AAEO,SAAS,iCAAiC,UAAgD;AAC/F,QAAM,QAAQ,EAAE,SAAS;AACzB,oBAAkB,IAAI,SAAS,KAAK,KAAK;AACzC,SAAO,MAAM;AACX,QAAI,kBAAkB,IAAI,SAAS,GAAG,MAAM,MAAO,mBAAkB,OAAO,SAAS,GAAG;AAAA,EAC1F;AACF;AAEO,SAAS,0BAA0B,UAAyC;AACjF,QAAM,QAAQ,EAAE,SAAS;AACzB,aAAW,IAAI,SAAS,KAAK,KAAK;AAClC,SAAO,MAAM;AACX,QAAI,WAAW,IAAI,SAAS,GAAG,MAAM,MAAO,YAAW,OAAO,SAAS,GAAG;AAAA,EAC5E;AACF;AAEO,SAAS,4BAA4B,UAA2C;AACrF,QAAM,QAAQ,EAAE,SAAS;AACzB,eAAa,IAAI,SAAS,KAAK,KAAK;AACpC,SAAO,MAAM;AACX,QAAI,aAAa,IAAI,SAAS,GAAG,MAAM,MAAO,cAAa,OAAO,SAAS,GAAG;AAAA,EAChF;AACF;AAEO,SAAS,WACd,OACA,OACA,SACqB;AACrB,QAAM,kBAAkB,kBAAkB,IAAI,MAAM,GAAG,GAAG,SAAS;AACnE,MAAI,CAAC,gBAAiB,QAAO,QAAQ,OAAO,QAAQ,MAAM,GAAG,CAAC;AAC9D,SAAO,gBAAgB,OAAO,OAAO,OAAO;AAC9C;AAEO,SAAS,OACd,OACA,OACA,SAC0B;AAC1B,QAAM,cAAc,kBAAkB,IAAI,MAAM,GAAG,GAAG,SAAS;AAC/D,MAAI,CAAC,YAAa,OAAM,QAAQ,MAAM,GAAG;AACzC,SAAO,YAAY,OAAO,OAAO,OAAO;AAC1C;AAEO,SAAS,MACd,OACA,OACA,SACmB;AACnB,QAAM,aAAa,WAAW,IAAI,MAAM,GAAG,GAAG,SAAS;AACvD,MAAI,CAAC,WAAY,OAAM,QAAQ,MAAM,GAAG;AACxC,SAAO,WAAW,OAAO,OAAO,OAAO;AACzC;AAEO,SAAS,gBACd,OACA,SACA,SAC0B;AAC1B,QAAM,UAAU,aAAa,IAAI,MAAM,GAAG,GAAG,SAAS;AACtD,MAAI,CAAC,QAAS,QAAO,QAAQ,OAAO,QAAQ,MAAM,GAAG,CAAC;AACtD,SAAO,QAAQ,OAAO,SAAS,OAAO;AACxC;",
6
+ "names": []
7
+ }
package/dist/speech.js ADDED
@@ -0,0 +1,31 @@
1
+ import { EventStream } from "@earendil-works/pi-ai";
2
+ import { blankUsage } from "./blank-usage.js";
3
+ import { VoiceRequestError } from "./errors.js";
4
+ import { streamResult } from "./stream-result.js";
5
+ class SpeechEventStream extends EventStream {
6
+ #settled = streamResult();
7
+ constructor() {
8
+ super(
9
+ (event) => event.type === "done" || event.type === "error",
10
+ (event) => event.type === "done" ? event.usage : blankUsage()
11
+ );
12
+ }
13
+ push(event) {
14
+ super.push(event);
15
+ if (event.type === "done") this.#settled.finish(event.usage);
16
+ else if (event.type === "error")
17
+ this.#settled.fail(new VoiceRequestError(event.reason, event.error));
18
+ }
19
+ end(result) {
20
+ super.end(result);
21
+ if (result !== void 0) this.#settled.finish(result);
22
+ else this.#settled.fail(new VoiceRequestError("error", "Speech ended without usage"));
23
+ }
24
+ result() {
25
+ return this.#settled.result;
26
+ }
27
+ }
28
+ export {
29
+ SpeechEventStream
30
+ };
31
+ //# sourceMappingURL=speech.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/speech.ts"],
4
+ "sourcesContent": ["import type { ProviderRequestOptions, Usage } from '@earendil-works/pi-ai';\nimport { EventStream } from '@earendil-works/pi-ai';\nimport type { AudioChunk, AudioFormat, CharAlignment, TextSpan } from './audio.ts';\nimport { blankUsage } from './blank-usage.ts';\nimport { VoiceRequestError } from './errors.ts';\nimport type { SpeechModel } from './models.ts';\nimport { streamResult } from './stream-result.ts';\n\nexport interface SpeechInput {\n text: string | AsyncIterable<string>;\n voice: string;\n language?: string;\n instructions?: string;\n format?: AudioFormat;\n}\n\nexport type SpeechEvent =\n | { type: 'start'; format: AudioFormat }\n | { type: 'audio'; chunk: AudioChunk; span: TextSpan; alignment?: CharAlignment }\n | { type: 'done'; usage: Usage }\n | { type: 'error'; reason: 'aborted' | 'error'; error: string };\n\nexport class SpeechEventStream extends EventStream<SpeechEvent, Usage> {\n readonly #settled = streamResult<Usage>();\n\n constructor() {\n super(\n (event) => event.type === 'done' || event.type === 'error',\n (event) => (event.type === 'done' ? event.usage : blankUsage())\n );\n }\n\n override push(event: SpeechEvent): void {\n super.push(event);\n if (event.type === 'done') this.#settled.finish(event.usage);\n else if (event.type === 'error')\n this.#settled.fail(new VoiceRequestError(event.reason, event.error));\n }\n\n override end(result?: Usage): void {\n super.end(result);\n if (result !== undefined) this.#settled.finish(result);\n else this.#settled.fail(new VoiceRequestError('error', 'Speech ended without usage'));\n }\n\n override result(): Promise<Usage> {\n return this.#settled.result;\n }\n}\n\nexport interface SpeechApiProvider<TApi extends string = string> {\n api: TApi;\n speak: (\n model: SpeechModel<TApi>,\n input: SpeechInput,\n options?: ProviderRequestOptions\n ) => SpeechEventStream;\n}\n"],
5
+ "mappings": "AACA,SAAS,mBAAmB;AAE5B,SAAS,kBAAkB;AAC3B,SAAS,yBAAyB;AAElC,SAAS,oBAAoB;AAgBtB,MAAM,0BAA0B,YAAgC;AAAA,EAC5D,WAAW,aAAoB;AAAA,EAExC,cAAc;AACZ;AAAA,MACE,CAAC,UAAU,MAAM,SAAS,UAAU,MAAM,SAAS;AAAA,MACnD,CAAC,UAAW,MAAM,SAAS,SAAS,MAAM,QAAQ,WAAW;AAAA,IAC/D;AAAA,EACF;AAAA,EAES,KAAK,OAA0B;AACtC,UAAM,KAAK,KAAK;AAChB,QAAI,MAAM,SAAS,OAAQ,MAAK,SAAS,OAAO,MAAM,KAAK;AAAA,aAClD,MAAM,SAAS;AACtB,WAAK,SAAS,KAAK,IAAI,kBAAkB,MAAM,QAAQ,MAAM,KAAK,CAAC;AAAA,EACvE;AAAA,EAES,IAAI,QAAsB;AACjC,UAAM,IAAI,MAAM;AAChB,QAAI,WAAW,OAAW,MAAK,SAAS,OAAO,MAAM;AAAA,QAChD,MAAK,SAAS,KAAK,IAAI,kBAAkB,SAAS,4BAA4B,CAAC;AAAA,EACtF;AAAA,EAES,SAAyB;AAChC,WAAO,KAAK,SAAS;AAAA,EACvB;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,28 @@
1
+ function streamResult() {
2
+ let settled = false;
3
+ let resolve;
4
+ let reject;
5
+ const result = new Promise((res, rej) => {
6
+ resolve = res;
7
+ reject = rej;
8
+ });
9
+ void result.catch(() => {
10
+ });
11
+ return {
12
+ result,
13
+ finish(value) {
14
+ if (settled) return;
15
+ settled = true;
16
+ resolve(value);
17
+ },
18
+ fail(error) {
19
+ if (settled) return;
20
+ settled = true;
21
+ reject(error);
22
+ }
23
+ };
24
+ }
25
+ export {
26
+ streamResult
27
+ };
28
+ //# sourceMappingURL=stream-result.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/stream-result.ts"],
4
+ "sourcesContent": ["export interface StreamResult<T> {\n readonly result: Promise<T>;\n finish(value: T): void;\n fail(error: Error): void;\n}\n\nexport function streamResult<T>(): StreamResult<T> {\n let settled = false;\n let resolve!: (value: T) => void;\n let reject!: (error: Error) => void;\n const result = new Promise<T>((res, rej) => {\n resolve = res;\n reject = rej;\n });\n void result.catch(() => {});\n return {\n result,\n finish(value) {\n if (settled) return;\n settled = true;\n resolve(value);\n },\n fail(error) {\n if (settled) return;\n settled = true;\n reject(error);\n },\n };\n}\n"],
5
+ "mappings": "AAMO,SAAS,eAAmC;AACjD,MAAI,UAAU;AACd,MAAI;AACJ,MAAI;AACJ,QAAM,SAAS,IAAI,QAAW,CAAC,KAAK,QAAQ;AAC1C,cAAU;AACV,aAAS;AAAA,EACX,CAAC;AACD,OAAK,OAAO,MAAM,MAAM;AAAA,EAAC,CAAC;AAC1B,SAAO;AAAA,IACL;AAAA,IACA,OAAO,OAAO;AACZ,UAAI,QAAS;AACb,gBAAU;AACV,cAAQ,KAAK;AAAA,IACf;AAAA,IACA,KAAK,OAAO;AACV,UAAI,QAAS;AACb,gBAAU;AACV,aAAO,KAAK;AAAA,IACd;AAAA,EACF;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,37 @@
1
+ import { EventStream } from "@earendil-works/pi-ai";
2
+ import { blankUsage } from "./blank-usage.js";
3
+ import { VoiceRequestError } from "./errors.js";
4
+ import { streamResult } from "./stream-result.js";
5
+ function blankTranscript() {
6
+ return { text: "", usage: blankUsage() };
7
+ }
8
+ class TranscriptionEventStream extends EventStream {
9
+ #settled = streamResult();
10
+ constructor() {
11
+ super(
12
+ (event) => event.type === "done" || event.type === "error",
13
+ (event) => event.type === "done" ? event.transcript : blankTranscript()
14
+ );
15
+ }
16
+ push(event) {
17
+ super.push(event);
18
+ if (event.type === "done") this.#settled.finish(event.transcript);
19
+ else if (event.type === "error")
20
+ this.#settled.fail(new VoiceRequestError(event.reason, event.error));
21
+ }
22
+ end(result) {
23
+ super.end(result);
24
+ if (result !== void 0) this.#settled.finish(result);
25
+ else
26
+ this.#settled.fail(
27
+ new VoiceRequestError("error", "Transcription ended without a transcript")
28
+ );
29
+ }
30
+ result() {
31
+ return this.#settled.result;
32
+ }
33
+ }
34
+ export {
35
+ TranscriptionEventStream
36
+ };
37
+ //# sourceMappingURL=transcription.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/transcription.ts"],
4
+ "sourcesContent": ["import type { ProviderRequestOptions, Usage } from '@earendil-works/pi-ai';\nimport { EventStream } from '@earendil-works/pi-ai';\nimport type { AudioSource } from './audio.ts';\nimport { blankUsage } from './blank-usage.ts';\nimport { VoiceRequestError } from './errors.ts';\nimport type { TranscriptionModel } from './models.ts';\nimport { streamResult } from './stream-result.ts';\n\nexport type AudioInput = { data: Uint8Array; mimeType: string } | { url: string };\n\nexport interface TranscriptSegment {\n text: string;\n start: number;\n end: number;\n speaker?: string;\n}\n\nexport interface Transcript {\n text: string;\n language?: string;\n segments?: TranscriptSegment[];\n usage: Usage;\n}\n\nexport type TranscriptionEvent =\n | { type: 'speech_start'; at: number }\n | { type: 'partial'; segmentId: string; text: string }\n | { type: 'final'; segmentId: string; text: string; confidence?: number }\n | { type: 'turn_end'; text: string; at: number }\n | { type: 'done'; transcript: Transcript }\n | { type: 'error'; reason: 'aborted' | 'error'; error: string };\n\nexport interface ListenOptions extends ProviderRequestOptions {\n language?: string;\n vocabulary?: string[];\n}\n\nfunction blankTranscript(): Transcript {\n return { text: '', usage: blankUsage() };\n}\n\nexport class TranscriptionEventStream extends EventStream<TranscriptionEvent, Transcript> {\n readonly #settled = streamResult<Transcript>();\n\n constructor() {\n super(\n (event) => event.type === 'done' || event.type === 'error',\n (event) => (event.type === 'done' ? event.transcript : blankTranscript())\n );\n }\n\n override push(event: TranscriptionEvent): void {\n super.push(event);\n if (event.type === 'done') this.#settled.finish(event.transcript);\n else if (event.type === 'error')\n this.#settled.fail(new VoiceRequestError(event.reason, event.error));\n }\n\n override end(result?: Transcript): void {\n super.end(result);\n if (result !== undefined) this.#settled.finish(result);\n else\n this.#settled.fail(\n new VoiceRequestError('error', 'Transcription ended without a transcript')\n );\n }\n\n override result(): Promise<Transcript> {\n return this.#settled.result;\n }\n}\n\nexport interface TranscriptionApiProvider<TApi extends string = string> {\n api: TApi;\n transcribe?: (\n model: TranscriptionModel<TApi>,\n input: AudioInput,\n options?: ProviderRequestOptions\n ) => Promise<Transcript>;\n listen?: (\n model: TranscriptionModel<TApi>,\n audio: AudioSource,\n options?: ListenOptions\n ) => TranscriptionEventStream;\n}\n"],
5
+ "mappings": "AACA,SAAS,mBAAmB;AAE5B,SAAS,kBAAkB;AAC3B,SAAS,yBAAyB;AAElC,SAAS,oBAAoB;AA+B7B,SAAS,kBAA8B;AACrC,SAAO,EAAE,MAAM,IAAI,OAAO,WAAW,EAAE;AACzC;AAEO,MAAM,iCAAiC,YAA4C;AAAA,EAC/E,WAAW,aAAyB;AAAA,EAE7C,cAAc;AACZ;AAAA,MACE,CAAC,UAAU,MAAM,SAAS,UAAU,MAAM,SAAS;AAAA,MACnD,CAAC,UAAW,MAAM,SAAS,SAAS,MAAM,aAAa,gBAAgB;AAAA,IACzE;AAAA,EACF;AAAA,EAES,KAAK,OAAiC;AAC7C,UAAM,KAAK,KAAK;AAChB,QAAI,MAAM,SAAS,OAAQ,MAAK,SAAS,OAAO,MAAM,UAAU;AAAA,aACvD,MAAM,SAAS;AACtB,WAAK,SAAS,KAAK,IAAI,kBAAkB,MAAM,QAAQ,MAAM,KAAK,CAAC;AAAA,EACvE;AAAA,EAES,IAAI,QAA2B;AACtC,UAAM,IAAI,MAAM;AAChB,QAAI,WAAW,OAAW,MAAK,SAAS,OAAO,MAAM;AAAA;AAEnD,WAAK,SAAS;AAAA,QACZ,IAAI,kBAAkB,SAAS,0CAA0C;AAAA,MAC3E;AAAA,EACJ;AAAA,EAES,SAA8B;AACrC,WAAO,KAAK,SAAS;AAAA,EACvB;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,36 @@
1
+ export type AudioEncoding = 'pcm16' | 'pcmf32' | 'mulaw' | 'alaw' | 'opus' | 'mp3' | 'wav' | 'flac';
2
+ export interface AudioFormat {
3
+ encoding: AudioEncoding;
4
+ sampleRate: number;
5
+ channels: 1 | 2;
6
+ }
7
+ export interface AudioChunk {
8
+ data: Uint8Array;
9
+ format: AudioFormat;
10
+ timestamp: number;
11
+ }
12
+ export type AudioSource = AsyncIterable<AudioChunk>;
13
+ export type TextSpan = {
14
+ entryId: string;
15
+ contentIndex: number;
16
+ start: number;
17
+ end: number;
18
+ } | {
19
+ itemId: string;
20
+ start: number;
21
+ end: number;
22
+ };
23
+ export interface CharAlignment {
24
+ startMs: number[];
25
+ durationMs: number[];
26
+ }
27
+ export interface AudioTag {
28
+ span: TextSpan;
29
+ alignment?: CharAlignment;
30
+ }
31
+ export interface AudioSink {
32
+ write(chunk: AudioChunk, tag?: AudioTag): void;
33
+ flush(): void;
34
+ readonly playedMs: number;
35
+ onPlayback(listener: (tag: AudioTag, offsetMs: number) => void): () => void;
36
+ }
@@ -0,0 +1,3 @@
1
+ import type { AuthResult, Models } from '@earendil-works/pi-ai';
2
+ import type { VoiceModel } from './models.ts';
3
+ export declare function resolveVoiceAuth(models: Models, model: VoiceModel, overrides?: Parameters<Models['getAuth']>[1]): Promise<AuthResult | undefined>;
@@ -0,0 +1,2 @@
1
+ import type { Usage } from '@earendil-works/pi-ai';
2
+ export declare function blankUsage(): Usage;
@@ -0,0 +1,4 @@
1
+ export declare class VoiceRequestError extends Error {
2
+ readonly reason: 'aborted' | 'error';
3
+ constructor(reason: 'aborted' | 'error', message: string);
4
+ }
@@ -0,0 +1,12 @@
1
+ export type { AudioChunk, AudioEncoding, AudioFormat, AudioSink, AudioSource, AudioTag, CharAlignment, TextSpan, } from './audio.ts';
2
+ export { resolveVoiceAuth } from './auth.ts';
3
+ export { blankUsage } from './blank-usage.ts';
4
+ export { VoiceRequestError } from './errors.ts';
5
+ export type { Delivery, DeliverySupport, KnownRealtimeApi, KnownSpeechApi, KnownTranscriptionApi, RealtimeApi, RealtimeModel, SpeechApi, SpeechModel, TranscriptionApi, TranscriptionModel, VoiceInfo, VoiceModel, VoiceModelType, } from './models.ts';
6
+ export { getVoiceModel, listVoiceModels, registerVoiceModel, } from './models.ts';
7
+ export type { RealtimeApiProvider, RealtimeContext, RealtimeEvent, RealtimeOptions, RealtimeSession, RealtimeToken, } from './realtime.ts';
8
+ export { connectRealtime, listen, registerRealtimeApiProvider, registerSpeechApiProvider, registerTranscriptionApiProvider, speak, transcribe, } from './registry.ts';
9
+ export type { SpeechApiProvider, SpeechEvent, SpeechInput } from './speech.ts';
10
+ export { SpeechEventStream } from './speech.ts';
11
+ export type { AudioInput, ListenOptions, Transcript, TranscriptionApiProvider, TranscriptionEvent, TranscriptSegment, } from './transcription.ts';
12
+ export { TranscriptionEventStream } from './transcription.ts';
@@ -0,0 +1,73 @@
1
+ import type { BaseModel } from '@earendil-works/pi-ai';
2
+ import type { AudioFormat } from './audio.ts';
3
+ export type KnownTranscriptionApi = 'openai-transcriptions' | 'openai-realtime-transcription' | 'xai-stt' | 'elevenlabs-scribe' | 'mistral-transcriptions' | 'fish-asr' | 'cloudflare-ai-run';
4
+ export type TranscriptionApi = KnownTranscriptionApi | (string & {});
5
+ export type KnownSpeechApi = 'openai-speech' | 'xai-tts' | 'elevenlabs-tts' | 'mistral-speech' | 'fish-tts' | 'google-tts' | 'cloudflare-ai-run' | 'wavespeed';
6
+ export type SpeechApi = KnownSpeechApi | (string & {});
7
+ export type KnownRealtimeApi = 'openai-realtime' | 'xai-realtime' | 'gemini-live' | 'elevenlabs-agents' | 'fish-agents';
8
+ export type RealtimeApi = KnownRealtimeApi | (string & {});
9
+ export type Delivery = 'interrupt' | 'when_idle' | 'silent';
10
+ export type DeliverySupport = 'native' | 'approximate' | 'unsupported';
11
+ export interface VoiceInfo {
12
+ id: string;
13
+ name: string;
14
+ languages?: string[];
15
+ }
16
+ export interface TranscriptionModel<TApi extends TranscriptionApi> extends Omit<BaseModel<TApi>, 'input'> {
17
+ type: 'transcription';
18
+ streaming: boolean;
19
+ turnDetection: 'none' | 'vad' | 'semantic';
20
+ inputFormats: AudioFormat[];
21
+ languages?: string[];
22
+ diarization?: boolean;
23
+ }
24
+ export interface SpeechModel<TApi extends SpeechApi> extends Omit<BaseModel<TApi>, 'input'> {
25
+ type: 'speech';
26
+ textStreaming: boolean;
27
+ outputFormats: AudioFormat[];
28
+ voices?: VoiceInfo[];
29
+ cloning?: boolean;
30
+ }
31
+ export interface RealtimeModel<TApi extends RealtimeApi> extends Omit<BaseModel<TApi>, 'input'> {
32
+ type: 'realtime';
33
+ transports: ('websocket' | 'webrtc')[];
34
+ toolCalls: 'blocking' | 'async';
35
+ inputFormat: AudioFormat;
36
+ outputFormat: AudioFormat;
37
+ video?: {
38
+ maxFps: number;
39
+ };
40
+ maxSessionMs?: number;
41
+ resumable: boolean;
42
+ ephemeralAuth: boolean;
43
+ voices: VoiceInfo[];
44
+ cascade?: {
45
+ stt: string;
46
+ llm: string;
47
+ tts: string;
48
+ };
49
+ provisioning: 'none' | 'agent';
50
+ delivery: Record<Delivery, DeliverySupport>;
51
+ truncation: 'client' | 'server' | 'none';
52
+ liveToolUpdate: boolean;
53
+ toolTimeoutMs?: {
54
+ min: number;
55
+ max: number;
56
+ };
57
+ }
58
+ declare module '@earendil-works/pi-ai' {
59
+ interface ModelTypeMap {
60
+ transcription: TranscriptionModel<TranscriptionApi>;
61
+ speech: SpeechModel<SpeechApi>;
62
+ realtime: RealtimeModel<RealtimeApi>;
63
+ }
64
+ }
65
+ export type VoiceModel = TranscriptionModel<TranscriptionApi> | SpeechModel<SpeechApi> | RealtimeModel<RealtimeApi>;
66
+ export type VoiceModelType = VoiceModel['type'];
67
+ export declare function registerVoiceModel(model: VoiceModel): () => void;
68
+ export declare function getVoiceModel<TType extends VoiceModelType>(type: TType, provider: string, id: string): Extract<VoiceModel, {
69
+ type: TType;
70
+ }> | undefined;
71
+ export declare function listVoiceModels<TType extends VoiceModelType>(type: TType, provider?: string): Extract<VoiceModel, {
72
+ type: TType;
73
+ }>[];
@@ -0,0 +1,87 @@
1
+ import type { Message, ProviderRequestOptions, Tool, ToolCall, ToolResultMessage, Usage } from '@earendil-works/pi-ai';
2
+ import type { AudioChunk, CharAlignment, TextSpan } from './audio.ts';
3
+ import type { Delivery, RealtimeModel } from './models.ts';
4
+ export interface RealtimeContext {
5
+ instructions: string;
6
+ tools: Tool[];
7
+ voice: string;
8
+ history?: Message[];
9
+ }
10
+ export interface RealtimeToken {
11
+ value: string;
12
+ expiresAt: number;
13
+ model: string;
14
+ }
15
+ export interface RealtimeOptions extends ProviderRequestOptions {
16
+ token?: RealtimeToken;
17
+ transport?: 'websocket' | 'webrtc';
18
+ gateway?: {
19
+ url: string;
20
+ token: string;
21
+ };
22
+ resumeHandle?: string;
23
+ }
24
+ export type RealtimeEvent = {
25
+ type: 'session_start';
26
+ sessionId: string;
27
+ } | {
28
+ type: 'input_speech_start';
29
+ } | {
30
+ type: 'input_transcript';
31
+ itemId: string;
32
+ text: string;
33
+ final: boolean;
34
+ } | {
35
+ type: 'output_audio';
36
+ itemId: string;
37
+ chunk: AudioChunk;
38
+ span?: TextSpan;
39
+ alignment?: CharAlignment;
40
+ } | {
41
+ type: 'output_transcript';
42
+ itemId: string;
43
+ text: string;
44
+ final: boolean;
45
+ } | {
46
+ type: 'tool_call';
47
+ call: ToolCall;
48
+ blocking: boolean;
49
+ } | {
50
+ type: 'tool_call_cancelled';
51
+ callId: string;
52
+ } | {
53
+ type: 'response_done';
54
+ itemId: string;
55
+ interrupted: boolean;
56
+ usage: Usage;
57
+ } | {
58
+ type: 'go_away';
59
+ timeLeftMs: number;
60
+ } | {
61
+ type: 'resumption';
62
+ handle: string;
63
+ } | {
64
+ type: 'error';
65
+ error: string;
66
+ fatal: boolean;
67
+ };
68
+ export interface RealtimeSession extends AsyncIterable<RealtimeEvent> {
69
+ readonly model: RealtimeModel<string>;
70
+ appendAudio(chunk: AudioChunk): void;
71
+ appendVideoFrame?(jpeg: Uint8Array): void;
72
+ sendText(text: string): void;
73
+ inject(text: string, delivery: Delivery): void;
74
+ respond(callId: string, result: ToolResultMessage, delivery?: Delivery): void;
75
+ interrupt(heard?: {
76
+ itemId: string;
77
+ heardMs: number;
78
+ }): void;
79
+ update(patch: Partial<Pick<RealtimeContext, 'instructions' | 'tools' | 'voice'>>): void;
80
+ close(): Promise<{
81
+ usage: Usage;
82
+ }>;
83
+ }
84
+ export interface RealtimeApiProvider<TApi extends string = string> {
85
+ api: TApi;
86
+ connect: (model: RealtimeModel<TApi>, context: RealtimeContext, options?: RealtimeOptions) => Promise<RealtimeSession>;
87
+ }
@@ -0,0 +1,13 @@
1
+ import type { ProviderRequestOptions } from '@earendil-works/pi-ai';
2
+ import type { AudioSource } from './audio.ts';
3
+ import type { RealtimeModel, SpeechModel, TranscriptionModel } from './models.ts';
4
+ import type { RealtimeApiProvider, RealtimeContext, RealtimeOptions, RealtimeSession } from './realtime.ts';
5
+ import type { SpeechApiProvider, SpeechEventStream, SpeechInput } from './speech.ts';
6
+ import type { AudioInput, ListenOptions, Transcript, TranscriptionApiProvider, TranscriptionEventStream } from './transcription.ts';
7
+ export declare function registerTranscriptionApiProvider(provider: TranscriptionApiProvider): () => void;
8
+ export declare function registerSpeechApiProvider(provider: SpeechApiProvider): () => void;
9
+ export declare function registerRealtimeApiProvider(provider: RealtimeApiProvider): () => void;
10
+ export declare function transcribe(model: TranscriptionModel<string>, input: AudioInput, options?: ProviderRequestOptions): Promise<Transcript>;
11
+ export declare function listen(model: TranscriptionModel<string>, audio: AudioSource, options?: ListenOptions): TranscriptionEventStream;
12
+ export declare function speak(model: SpeechModel<string>, input: SpeechInput, options?: ProviderRequestOptions): SpeechEventStream;
13
+ export declare function connectRealtime(model: RealtimeModel<string>, context: RealtimeContext, options?: RealtimeOptions): Promise<RealtimeSession>;
@@ -0,0 +1,38 @@
1
+ import type { ProviderRequestOptions, Usage } from '@earendil-works/pi-ai';
2
+ import { EventStream } from '@earendil-works/pi-ai';
3
+ import type { AudioChunk, AudioFormat, CharAlignment, TextSpan } from './audio.ts';
4
+ import type { SpeechModel } from './models.ts';
5
+ export interface SpeechInput {
6
+ text: string | AsyncIterable<string>;
7
+ voice: string;
8
+ language?: string;
9
+ instructions?: string;
10
+ format?: AudioFormat;
11
+ }
12
+ export type SpeechEvent = {
13
+ type: 'start';
14
+ format: AudioFormat;
15
+ } | {
16
+ type: 'audio';
17
+ chunk: AudioChunk;
18
+ span: TextSpan;
19
+ alignment?: CharAlignment;
20
+ } | {
21
+ type: 'done';
22
+ usage: Usage;
23
+ } | {
24
+ type: 'error';
25
+ reason: 'aborted' | 'error';
26
+ error: string;
27
+ };
28
+ export declare class SpeechEventStream extends EventStream<SpeechEvent, Usage> {
29
+ #private;
30
+ constructor();
31
+ push(event: SpeechEvent): void;
32
+ end(result?: Usage): void;
33
+ result(): Promise<Usage>;
34
+ }
35
+ export interface SpeechApiProvider<TApi extends string = string> {
36
+ api: TApi;
37
+ speak: (model: SpeechModel<TApi>, input: SpeechInput, options?: ProviderRequestOptions) => SpeechEventStream;
38
+ }
@@ -0,0 +1,6 @@
1
+ export interface StreamResult<T> {
2
+ readonly result: Promise<T>;
3
+ finish(value: T): void;
4
+ fail(error: Error): void;
5
+ }
6
+ export declare function streamResult<T>(): StreamResult<T>;
@@ -0,0 +1,62 @@
1
+ import type { ProviderRequestOptions, Usage } from '@earendil-works/pi-ai';
2
+ import { EventStream } from '@earendil-works/pi-ai';
3
+ import type { AudioSource } from './audio.ts';
4
+ import type { TranscriptionModel } from './models.ts';
5
+ export type AudioInput = {
6
+ data: Uint8Array;
7
+ mimeType: string;
8
+ } | {
9
+ url: string;
10
+ };
11
+ export interface TranscriptSegment {
12
+ text: string;
13
+ start: number;
14
+ end: number;
15
+ speaker?: string;
16
+ }
17
+ export interface Transcript {
18
+ text: string;
19
+ language?: string;
20
+ segments?: TranscriptSegment[];
21
+ usage: Usage;
22
+ }
23
+ export type TranscriptionEvent = {
24
+ type: 'speech_start';
25
+ at: number;
26
+ } | {
27
+ type: 'partial';
28
+ segmentId: string;
29
+ text: string;
30
+ } | {
31
+ type: 'final';
32
+ segmentId: string;
33
+ text: string;
34
+ confidence?: number;
35
+ } | {
36
+ type: 'turn_end';
37
+ text: string;
38
+ at: number;
39
+ } | {
40
+ type: 'done';
41
+ transcript: Transcript;
42
+ } | {
43
+ type: 'error';
44
+ reason: 'aborted' | 'error';
45
+ error: string;
46
+ };
47
+ export interface ListenOptions extends ProviderRequestOptions {
48
+ language?: string;
49
+ vocabulary?: string[];
50
+ }
51
+ export declare class TranscriptionEventStream extends EventStream<TranscriptionEvent, Transcript> {
52
+ #private;
53
+ constructor();
54
+ push(event: TranscriptionEvent): void;
55
+ end(result?: Transcript): void;
56
+ result(): Promise<Transcript>;
57
+ }
58
+ export interface TranscriptionApiProvider<TApi extends string = string> {
59
+ api: TApi;
60
+ transcribe?: (model: TranscriptionModel<TApi>, input: AudioInput, options?: ProviderRequestOptions) => Promise<Transcript>;
61
+ listen?: (model: TranscriptionModel<TApi>, audio: AudioSource, options?: ListenOptions) => TranscriptionEventStream;
62
+ }
package/package.json CHANGED
@@ -1,6 +1,61 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/pi-audio",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Voice bindings for pi: speech-to-text, text-to-speech and realtime speech for pi-coding-agent plugins and slicc-agent",
5
+ "type": "module",
6
+ "license": "Apache-2.0",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/ai-ecoverse/pi-audio.git"
10
+ },
11
+ "engines": {
12
+ "node": ">=24"
13
+ },
14
+ "fledgling": {
15
+ "provider": "github",
16
+ "workflow": "release.yml",
17
+ "repo": "ai-ecoverse/pi-audio",
18
+ "publish": true
19
+ },
20
+ "publishConfig": {
21
+ "access": "public",
22
+ "provenance": true
23
+ },
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/types/index.d.ts",
27
+ "default": "./dist/index.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist"
32
+ ],
33
+ "scripts": {
34
+ "prepare": "lefthook install",
35
+ "build": "node build.mjs && tsc -p tsconfig.build.json",
36
+ "prepack": "npm run build",
37
+ "pretest": "npm run build",
38
+ "test": "node --test 'test/integration/*.test.mjs'",
39
+ "test:unit": "NODE_NO_WARNINGS=1 mcr -c node_modules/@ai-ecoverse/slicc-shared-web/mcr.config.mjs node --test 'test/unit/**/*.test.mjs'",
40
+ "typecheck": "tsc",
41
+ "lint": "slicc-lint",
42
+ "format": "biome check --write .",
43
+ "trust": "npx fledgling sync -y --skip-publish",
44
+ "trust:dry": "npx fledgling sync --dry-run -y --skip-publish",
45
+ "release": "semantic-release"
46
+ },
47
+ "devDependencies": {
48
+ "@ai-ecoverse/slicc-shared-web": "1.21.0",
49
+ "@biomejs/biome": "2.5.15",
50
+ "@earendil-works/pi-ai": "1.0.4",
51
+ "@types/node": "24.19.1",
52
+ "esbuild": "0.28.2",
53
+ "lefthook": "2.2.1",
54
+ "monocart-coverage-reports": "2.13.2",
55
+ "semantic-release": "25.0.9",
56
+ "typescript": "7.0.2"
57
+ },
58
+ "peerDependencies": {
59
+ "@earendil-works/pi-ai": "1.0.4"
60
+ }
61
+ }