@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
42 lines (41 loc) • 1.63 kB
TypeScript
/**
* SSE Stream Interceptor
*
* A zero-overhead TransformStream that taps Anthropic SSE streaming responses
* to extract telemetry data (token usage, model info, content blocks, thinking
* blocks, tool use) while passing every byte through to the client unmodified
* and without delay.
*
* The interceptor buffers partial SSE events internally (chunks may split
* across event boundaries) but never holds back any bytes from the readable
* side of the stream.
*
* Usage:
* const { stream, telemetry } = createSSEInterceptor();
* upstreamResponse.body.pipeThrough(stream).pipeTo(clientWritable);
* const data = await telemetry; // resolves on stream end
*/
import type { SSEInterceptorOptions, SSEInterceptorResult, ParsedSSEBuffer } from "../types/index.js";
/**
* Incrementally parse SSE events from a growing buffer of text.
*
* SSE events are separated by a blank line (`\n\n`). Each event consists of
* field lines (`event: ...`, `data: ...`). We consume complete events and
* return them, leaving any trailing partial event in the buffer.
*/
export declare function extractSSEEvents(buffer: string): ParsedSSEBuffer;
/**
* Create an SSE interceptor that extracts telemetry from an Anthropic
* streaming response while passing all bytes through unmodified.
*
* ```ts
* const { stream, telemetry } = createSSEInterceptor();
* upstreamResponse.body
* .pipeThrough(stream)
* .pipeTo(clientWritable);
*
* const data = await telemetry;
* console.log(data.usage.totalTokens);
* ```
*/
export declare function createSSEInterceptor(options?: SSEInterceptorOptions): SSEInterceptorResult;