@langchain/langgraph-sdk
Version:
Client library for interacting with the LangGraph API
52 lines (51 loc) • 2 kB
JavaScript
const require_subscription = require("../../client/stream/subscription.cjs");
const require_namespace = require("../namespace.cjs");
const require_runtime = require("./runtime.cjs");
//#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(",")}|${require_namespace.namespaceKey(ns)}`,
namespace: ns,
initial: [],
open({ thread, store, rootBus }) {
if (!replay && require_namespace.isRootNamespace(ns) && chs.every((c) => rootBus.channels.includes(c))) {
const requestedSet = new Set(chs);
const matches = (event) => {
const channel = require_subscription.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 require_runtime.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
exports.channelProjection = channelProjection;
//# sourceMappingURL=channel.cjs.map