@ai-ecoverse/pi-audio 0.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +27 -5
  3. package/dist/audio.js +1 -0
  4. package/dist/audio.js.map +7 -0
  5. package/dist/auth.js +7 -0
  6. package/dist/auth.js.map +7 -0
  7. package/dist/blank-usage.js +14 -0
  8. package/dist/blank-usage.js.map +7 -0
  9. package/dist/errors.js +12 -0
  10. package/dist/errors.js.map +7 -0
  11. package/dist/faux/index.js +15 -0
  12. package/dist/faux/index.js.map +7 -0
  13. package/dist/faux/realtime.js +137 -0
  14. package/dist/faux/realtime.js.map +7 -0
  15. package/dist/faux/shared.js +37 -0
  16. package/dist/faux/shared.js.map +7 -0
  17. package/dist/faux/speech.js +108 -0
  18. package/dist/faux/speech.js.map +7 -0
  19. package/dist/faux/transcription.js +152 -0
  20. package/dist/faux/transcription.js.map +7 -0
  21. package/dist/index.js +37 -0
  22. package/dist/index.js.map +7 -0
  23. package/dist/models.js +29 -0
  24. package/dist/models.js.map +7 -0
  25. package/dist/realtime.js +1 -0
  26. package/dist/realtime.js.map +7 -0
  27. package/dist/registry.js +57 -0
  28. package/dist/registry.js.map +7 -0
  29. package/dist/speech.js +31 -0
  30. package/dist/speech.js.map +7 -0
  31. package/dist/stream-result.js +28 -0
  32. package/dist/stream-result.js.map +7 -0
  33. package/dist/transcription.js +37 -0
  34. package/dist/transcription.js.map +7 -0
  35. package/dist/types/audio.d.ts +36 -0
  36. package/dist/types/auth.d.ts +3 -0
  37. package/dist/types/blank-usage.d.ts +2 -0
  38. package/dist/types/errors.d.ts +4 -0
  39. package/dist/types/faux/index.d.ts +3 -0
  40. package/dist/types/faux/realtime.d.ts +53 -0
  41. package/dist/types/faux/shared.d.ts +16 -0
  42. package/dist/types/faux/speech.d.ts +18 -0
  43. package/dist/types/faux/transcription.d.ts +27 -0
  44. package/dist/types/index.d.ts +12 -0
  45. package/dist/types/models.d.ts +73 -0
  46. package/dist/types/realtime.d.ts +87 -0
  47. package/dist/types/registry.d.ts +13 -0
  48. package/dist/types/speech.d.ts +38 -0
  49. package/dist/types/stream-result.d.ts +6 -0
  50. package/dist/types/transcription.d.ts +62 -0
  51. package/package.json +56 -1
@@ -0,0 +1,16 @@
1
+ import type { AudioFormat } from '../audio.ts';
2
+ export declare const zeroCost: {
3
+ input: number;
4
+ output: number;
5
+ cacheRead: number;
6
+ cacheWrite: number;
7
+ };
8
+ export declare function pcm(sampleRate?: number): AudioFormat;
9
+ export declare function modelKey(model: {
10
+ provider: string;
11
+ id: string;
12
+ }): string;
13
+ export declare function decodeBytes(data: Uint8Array): string;
14
+ export declare function encodeChars(text: string): Uint8Array;
15
+ export declare function errorMessage(error: unknown): string;
16
+ export declare function shareApi(registrations: Map<string, () => void>, api: string, register: () => () => void, stillUsed: () => boolean): () => void;
@@ -0,0 +1,18 @@
1
+ import { type SpeechModel } from '../models.ts';
2
+ import { type SpeechInput } from '../speech.ts';
3
+ export interface FauxSpeechOptions {
4
+ api?: string;
5
+ provider?: string;
6
+ id?: string;
7
+ name?: string;
8
+ alignment?: boolean;
9
+ chunkChars?: number;
10
+ textStreaming?: boolean;
11
+ }
12
+ export interface FauxSpeech {
13
+ model: SpeechModel<string>;
14
+ calls: SpeechInput[];
15
+ setAlignment(alignment: boolean): void;
16
+ unregister(): void;
17
+ }
18
+ export declare function registerFauxSpeech(options?: FauxSpeechOptions): FauxSpeech;
@@ -0,0 +1,27 @@
1
+ import type { AudioChunk } from '../audio.ts';
2
+ import { type TranscriptionModel } from '../models.ts';
3
+ import { type AudioInput, type TranscriptionEvent } from '../transcription.ts';
4
+ export interface FauxTranscriptionCue {
5
+ at: number | 'end';
6
+ events: TranscriptionEvent[];
7
+ }
8
+ export interface FauxTranscriptionOptions {
9
+ api?: string;
10
+ provider?: string;
11
+ id?: string;
12
+ name?: string;
13
+ turnDetection?: TranscriptionModel<string>['turnDetection'];
14
+ cues?: FauxTranscriptionCue[];
15
+ }
16
+ export interface FauxTranscriptionCall {
17
+ kind: 'transcribe' | 'listen';
18
+ input?: AudioInput;
19
+ chunks: AudioChunk[];
20
+ }
21
+ export interface FauxTranscription {
22
+ model: TranscriptionModel<string>;
23
+ calls: FauxTranscriptionCall[];
24
+ setCues(cues: FauxTranscriptionCue[] | undefined): void;
25
+ unregister(): void;
26
+ }
27
+ export declare function registerFauxTranscription(options?: FauxTranscriptionOptions): FauxTranscription;
@@ -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,10 +1,65 @@
1
1
  {
2
2
  "name": "@ai-ecoverse/pi-audio",
3
- "version": "0.0.0",
3
+ "version": "1.1.0",
4
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",
5
6
  "license": "Apache-2.0",
6
7
  "repository": {
7
8
  "type": "git",
8
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
+ "./faux": {
30
+ "types": "./dist/types/faux/index.d.ts",
31
+ "default": "./dist/faux/index.js"
32
+ }
33
+ },
34
+ "files": [
35
+ "dist"
36
+ ],
37
+ "scripts": {
38
+ "prepare": "lefthook install",
39
+ "build": "node build.mjs && tsc -p tsconfig.build.json",
40
+ "prepack": "npm run build",
41
+ "pretest": "npm run build",
42
+ "test": "node --test 'test/integration/*.test.mjs'",
43
+ "test:unit": "NODE_NO_WARNINGS=1 mcr -c node_modules/@ai-ecoverse/slicc-shared-web/mcr.config.mjs node --test 'test/unit/**/*.test.mjs'",
44
+ "typecheck": "tsc",
45
+ "lint": "slicc-lint",
46
+ "format": "biome check --write .",
47
+ "trust": "npx fledgling sync -y --skip-publish",
48
+ "trust:dry": "npx fledgling sync --dry-run -y --skip-publish",
49
+ "release": "semantic-release"
50
+ },
51
+ "devDependencies": {
52
+ "@ai-ecoverse/slicc-shared-web": "1.21.0",
53
+ "@biomejs/biome": "2.5.15",
54
+ "@earendil-works/pi-ai": "1.1.0",
55
+ "@types/node": "24.19.1",
56
+ "esbuild": "0.28.2",
57
+ "lefthook": "2.2.1",
58
+ "monocart-coverage-reports": "2.13.2",
59
+ "semantic-release": "25.0.9",
60
+ "typescript": "7.0.2"
61
+ },
62
+ "peerDependencies": {
63
+ "@earendil-works/pi-ai": "1.1.0"
9
64
  }
10
65
  }