@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
52 lines (51 loc) • 1.92 kB
JavaScript
import { inferChannel } from "../../client/stream/subscription.js";
import { isRootNamespace, namespaceKey } from "../namespace.js";
import { openProjectionSubscription } from "./runtime.js";
//#region src/stream/projections/channel.ts
/** Max events retained per raw subscription. Older events are dropped. */
const DEFAULT_BUFFER = 4096;
function channelProjection(channels, namespace, options = {}) {
const chs = [...channels].sort();
const ns = [...namespace];
const bufferSize = options.bufferSize ?? DEFAULT_BUFFER;
const replay = options.replay ?? true;
return {
key: `channel|${bufferSize}|${replay ? "replay" : "live"}|${chs.join(",")}|${namespaceKey(ns)}`,
namespace: ns,
initial: [],
open({ thread, store, rootBus }) {
if (!replay && isRootNamespace(ns) && chs.every((c) => rootBus.channels.includes(c))) {
const requestedSet = new Set(chs);
const matches = (event) => {
const channel = inferChannel(event);
if (channel === void 0) return false;
return requestedSet.has(channel) || channel.startsWith("custom:") && requestedSet.has("custom");
};
const push = (event) => {
if (!matches(event)) return;
const current = store.getSnapshot();
const next = current.length >= bufferSize ? [...current.slice(current.length - bufferSize + 1), event] : [...current, event];
store.setValue(next);
};
const unsubscribe = rootBus.subscribe(push);
return { dispose() {
unsubscribe();
} };
}
return openProjectionSubscription({
thread,
channels: chs,
namespace: ns,
resumeOnPause: true,
onEvent(event) {
const current = store.getSnapshot();
const next = current.length >= bufferSize ? [...current.slice(current.length - bufferSize + 1), event] : [...current, event];
store.setValue(next);
}
});
}
};
}
//#endregion
export { channelProjection };
//# sourceMappingURL=channel.js.map