|
| 1 | +/** |
| 2 | + * Typed provider interfaces for cross-package communication. |
| 3 | + * |
| 4 | + * Backend packages (@runanywhere/web-llamacpp, @runanywhere/web-onnx) implement |
| 5 | + * these interfaces and register instances via `ExtensionPoint.registerProvider()`. |
| 6 | + * Core code (e.g. VoicePipeline) retrieves them at runtime via |
| 7 | + * `ExtensionPoint.getProvider()` with full compile-time type safety. |
| 8 | + * |
| 9 | + * Replaces the previous implicit `globalThis.__runanywhere_*` contract. |
| 10 | + * See: https://github.com/RunanywhereAI/runanywhere-sdks/issues/371 |
| 11 | + */ |
| 12 | + |
| 13 | +// --------------------------------------------------------------------------- |
| 14 | +// Provider Capability Keys |
| 15 | +// --------------------------------------------------------------------------- |
| 16 | + |
| 17 | +/** |
| 18 | + * Typed capability keys for the provider registry. |
| 19 | + * Each key maps to exactly one provider interface. |
| 20 | + */ |
| 21 | +export type ProviderCapability = 'llm' | 'stt' | 'tts'; |
| 22 | + |
| 23 | +// --------------------------------------------------------------------------- |
| 24 | +// Provider Interfaces |
| 25 | +// --------------------------------------------------------------------------- |
| 26 | + |
| 27 | +/** |
| 28 | + * LLM (text generation) provider — implemented by @runanywhere/web-llamacpp. |
| 29 | + * |
| 30 | + * Only the subset of the TextGeneration API that cross-package consumers |
| 31 | + * (e.g. VoicePipeline) depend on. Backend packages may expose additional |
| 32 | + * methods beyond this interface. |
| 33 | + */ |
| 34 | +export interface LLMProvider { |
| 35 | + generateStream( |
| 36 | + prompt: string, |
| 37 | + options?: { |
| 38 | + maxTokens?: number; |
| 39 | + temperature?: number; |
| 40 | + systemPrompt?: string; |
| 41 | + }, |
| 42 | + ): Promise<{ |
| 43 | + stream: AsyncIterable<string>; |
| 44 | + result: Promise<{ |
| 45 | + text: string; |
| 46 | + tokensUsed: number; |
| 47 | + tokensPerSecond: number; |
| 48 | + [key: string]: unknown; |
| 49 | + }>; |
| 50 | + cancel: () => void; |
| 51 | + }>; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * STT (speech-to-text) provider — implemented by @runanywhere/web-onnx. |
| 56 | + * |
| 57 | + * Only the subset of the STT API that cross-package consumers depend on. |
| 58 | + */ |
| 59 | +export interface STTProvider { |
| 60 | + transcribe( |
| 61 | + audio: Float32Array, |
| 62 | + options?: { sampleRate?: number }, |
| 63 | + ): Promise<{ |
| 64 | + text: string; |
| 65 | + [key: string]: unknown; |
| 66 | + }>; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * TTS (text-to-speech) provider — implemented by @runanywhere/web-onnx. |
| 71 | + * |
| 72 | + * Only the subset of the TTS API that cross-package consumers depend on. |
| 73 | + */ |
| 74 | +export interface TTSProvider { |
| 75 | + synthesize( |
| 76 | + text: string, |
| 77 | + options?: { speed?: number }, |
| 78 | + ): Promise<{ |
| 79 | + audioData: Float32Array; |
| 80 | + sampleRate: number; |
| 81 | + durationMs: number; |
| 82 | + processingTimeMs: number; |
| 83 | + [key: string]: unknown; |
| 84 | + }>; |
| 85 | +} |
| 86 | + |
| 87 | +// --------------------------------------------------------------------------- |
| 88 | +// Provider Type Map (capability key → interface) |
| 89 | +// --------------------------------------------------------------------------- |
| 90 | + |
| 91 | +/** |
| 92 | + * Maps each `ProviderCapability` string to its corresponding interface. |
| 93 | + * Used by `registerProvider` / `getProvider` for compile-time type safety. |
| 94 | + */ |
| 95 | +export interface ProviderMap { |
| 96 | + llm: LLMProvider; |
| 97 | + stt: STTProvider; |
| 98 | + tts: TTSProvider; |
| 99 | +} |
0 commit comments