@langchain/langgraph
Version:
94 lines (93 loc) • 4.31 kB
TypeScript
import { Namespace } from "./types.js";
import { Channel, SubscribeParams } from "@langchain/protocol";
//#region src/stream/subscription.d.ts
/**
* Minimal protocol-event shape consumed by {@link inferChannel} and
* {@link matchesSubscription}.
*
* Both the core {@link ProtocolEvent} and the wire-level `Event` from
* `@langchain/protocol` structurally satisfy this contract, so the same
* predicates can drive in-process fan-out, buffered replay, and server-side
* (SSE / WebSocket) event-sink filtering without coupling to a single event
* type.
*/
interface MatchableEvent {
/** Logical stream channel; see {@link inferChannel}. */
readonly method: string;
/** Monotonic sequence number, when present. Used by the `since` cursor. */
readonly seq?: number;
readonly params: {
/** Namespace of the node or scope that emitted this event. */readonly namespace: Namespace; /** Opaque channel payload; shape depends on `method`. */
readonly data?: unknown;
};
}
/**
* Strip dynamic suffixes (after `:`) from a namespace segment.
*
* Server-emitted namespaces contain runtime-generated suffixes like
* `"fetcher:abc-uuid"`, while user-supplied subscription filters are typically
* static names (`"fetcher"`). Mirrors `normalize_namespace_segment` in
* `api/langgraph_api/protocol/namespace.py`.
*
* @param segment - Raw namespace segment.
* @returns The stable graph-oriented portion of the segment.
*/
declare function normalizeNamespaceSegment(segment: string): string;
/**
* Whether `namespace` starts with `prefix`.
*
* Segments are compared literally first; if the prefix segment itself contains
* no `:`, the candidate segment is also compared after its dynamic suffix is
* stripped (see {@link normalizeNamespaceSegment}). This mirrors
* `is_prefix_match` in `api/langgraph_api/protocol/namespace.py` so server-side
* filtering and client-side per-subscription narrowing stay consistent.
*
* @param namespace - Event namespace to test.
* @param prefix - Subscription namespace prefix.
*/
declare function isPrefixMatch(namespace: Namespace, prefix: Namespace): boolean;
/**
* The base protocol subscription channels, excluding the templated
* `custom:<name>` form. This is the runtime counterpart to the `Channel`
* union from `@langchain/protocol` and mirrors the channel set a server
* recognizes when filtering its event sinks.
*/
declare const SUPPORTED_CHANNELS: Set<Channel>;
/**
* Whether `value` names a protocol subscription channel — either a base
* channel (`"messages"`, `"values"`, …) or a named custom channel
* (`"custom:<name>"`). Unknown/future method names return `false`,
* mirroring {@link inferChannel}.
*
* @param value - Candidate channel name.
*/
declare function isSupportedChannel(value: string): value is Channel;
/**
* Maps a protocol event method to its subscription {@link Channel}.
*
* Returns `undefined` for unrecognized methods so that new server-side
* channels (e.g. from extension transformers) don't break existing
* subscribers. The `custom` method resolves to the named `custom:<name>`
* channel when the payload carries a `name`, otherwise the bare `custom`
* channel. Both `"input"` and the wire-level `"input.requested"` map to the
* `input` channel.
*
* @param event - Event whose method should be mapped to a channel.
*/
declare function inferChannel(event: MatchableEvent): Channel | undefined;
/**
* Returns whether an event should be delivered for a subscription definition.
*
* When the definition carries a `since` replay cursor, events at or before
* that sequence number are excluded — letting the same predicate drive both
* live fan-out and buffered replay over a {@link StreamChannel}.
*
* @param event - Event being checked for delivery.
* @param definition - Subscription filter definition to evaluate against.
* The optional `since` field (a `seq` cursor) is read leniently because it
* is not a declared field on the base {@link SubscribeParams} shape.
*/
declare function matchesSubscription(event: MatchableEvent, definition: SubscribeParams): boolean;
//#endregion
export { MatchableEvent, SUPPORTED_CHANNELS, inferChannel, isPrefixMatch, isSupportedChannel, matchesSubscription, normalizeNamespaceSegment };
//# sourceMappingURL=subscription.d.ts.map