UNPKG

@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

47 lines (46 loc) • 2.26 kB
/** * Out-of-band cancellation for chained async streams. * * `AsyncGenerator.prototype.return()` is the obvious way to tell a stream its * consumer has gone away, and it does not work through a chain of generators. * The request is *queued* behind whatever `next()` is already in flight; it * cannot interrupt an `await`. A provider stream is wrapped several times * (lifecycle in `baseProvider`, MCP and pool wrappers in `neurolink`), and * during a pull every one of those layers is parked inside an `await` on the * layer below. So none of them can unwind, the innermost source is never * closed, and a teardown that waits for that to happen waits forever. * * This module is the side channel that does work. A wrapper registers a * cancel callback on the stream object it returns; the callback closes its own * upstream iterator and forwards the request further down. Because these are * ordinary function calls rather than generator protocol, they run immediately * — no queue, no dependence on any pending `next()` settling. * * Registration is optional and reading is defensive, so a stream that knows * nothing about this behaves exactly as it did before. */ /** * Register `cancel` as `stream`'s teardown hook and return the same object. * * Non-enumerable so the property never shows up in spreads, `JSON.stringify` * or logging of a stream handle. */ export declare function attachStreamCancel<S extends object>(stream: S, cancel: () => void): S; /** * Invoke `stream`'s cancel hook if it has one. * * Never throws. This runs from `finally` blocks during teardown, where the * consumer has already stopped listening — an exception here would replace * whatever outcome the caller was actually returning with a cleanup error. */ export declare function cancelStream(stream: unknown): void; /** * Close `iterator` without waiting for it and without surfacing a rejection. * * Deliberately not awaited: on a generator that is parked mid-`await` this * promise may never settle, which is the whole failure this module exists to * avoid. The call still propagates whenever the iterator can act on it. */ export declare function releaseIterator(iterator: { return?: (value?: unknown) => unknown; }): void;