@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
66 lines (65 loc) • 3.24 kB
TypeScript
/**
* Transcribe WebSocket — `transcribeStream()` over a socket.
*
* Protocol (one transcription per connection):
* 1. The client's first frame is TEXT: a JSON config with the fields of
* `POST /agent/transcribe` minus the audio (`provider`, `model`,
* `language`, `dictionary`, `correction`, `fallback`, `streaming` …).
* 2. Then BINARY frames of PCM16LE mono audio at `streaming.sampleRate`
* (default 16000).
* 3. A TEXT frame `{"type":"end"}` (or closing the socket) ends the audio; the
* server flushes the last utterance. `{"type":"output","languages":[…]}`
* changes the output languages from the next utterance on; any other text
* frame ends the audio, as before.
*
* The server sends one TEXT frame per `TranscribeStreamEvent` (JSON), then
* closes with 1000. A bad config is answered with an `error` event and close
* code 1008.
*
* Sessions (one call, several speakers, listeners in their own languages):
* a speaker connects to the stream path with `?session=<id>&speaker=<name>`
* and uses the protocol above; a listener connects to the subscribe path
* (default `<path>/subscribe`) with `?session=<id>` and sends
* `{"type":"subscribe","languages":["es"],"speakers":[…]}` — again at any time
* to change them. Listeners receive every event of the speakers they follow,
* tagged with `speaker`, and only the `output` events of their languages; a
* session renders each utterance once into the union of its listeners'
* languages. A session lives while a speaker or a listener is connected. The
* speaker that opens a session may add `&secret=<value>`; every later speaker
* and listener must then present the same secret, so a session id alone does
* not admit a stranger (`requireSessionSecret` makes the secret mandatory).
*
* Limits: sockets are capped in all and per remote address (503 before the
* handshake), a socket that sends faster than the engine takes is paused at a
* high-water mark, and queued audio is budgeted across the process.
*
* OpenAI Realtime transcription (default path `/v1/realtime`,
* `?intent=transcription`): the same pipeline behind OpenAI's protocol, for
* clients that already speak it (see realtimeTranscription.ts).
*
* The route adapters do not handle upgrades, so this attaches to the Node
* `http.Server` the caller owns, next to whatever else is listening there; it
* answers only upgrades on its own path.
*
* @module server/websocket/transcribeWebSocket
*/
import type { Server as HttpServer } from "node:http";
import type { NeuroLink } from "../../neurolink.js";
import type { ServerTranscribeWebSocketOptions } from "../../types/index.js";
/**
* Serve `transcribeStream()` on `path` (default
* `/v1/audio/transcriptions/stream`) of an existing HTTP server.
*
* @example
* ```typescript
* const server = http.createServer(app);
* const ws = await attachTranscribeWebSocket(server, neurolink, {
* authToken: process.env.NEUROLINK_SERVER_API_KEY?.split(","),
* });
* server.listen(3000);
* // later: ws.close();
* ```
*/
export declare function attachTranscribeWebSocket(server: HttpServer, neurolink: NeuroLink, options?: ServerTranscribeWebSocketOptions): Promise<{
close: () => void;
}>;