UNPKG

@langchain/langgraph

Version:
79 lines (78 loc) 2.8 kB
import { ensureLangGraphConfig } from "./pregel/utils/config.js"; import { AsyncLocalStorageProviderSingleton } from "@langchain/core/singletons"; import { Runnable, mergeConfigs, patchConfig } from "@langchain/core/runnables"; //#region src/utils.ts var RunnableCallable = class extends Runnable { lc_namespace = ["langgraph"]; func; tags; config; trace = true; recurse = true; constructor(fields) { super(); this.name = fields.name ?? fields.func.name; this.func = fields.func; this.config = fields.tags ? { tags: fields.tags } : void 0; this.trace = fields.trace ?? this.trace; this.recurse = fields.recurse ?? this.recurse; } async _tracedInvoke(input, config, runManager) { return new Promise((resolve, reject) => { const childConfig = patchConfig(config, { callbacks: runManager?.getChild() }); AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => { try { resolve(await this.func(input, childConfig)); } catch (e) { reject(e); } }); }); } async invoke(input, options) { let returnValue; const config = ensureLangGraphConfig(options); const mergedConfig = mergeConfigs(this.config, config); if (this.trace) returnValue = await this._callWithConfig(this._tracedInvoke, input, mergedConfig); else returnValue = await AsyncLocalStorageProviderSingleton.runWithConfig(mergedConfig, async () => this.func(input, mergedConfig)); if (Runnable.isRunnable(returnValue) && this.recurse) return await AsyncLocalStorageProviderSingleton.runWithConfig(mergedConfig, async () => returnValue.invoke(input, mergedConfig)); return returnValue; } }; function* prefixGenerator(generator, prefix) { if (prefix === void 0) yield* generator; else for (const value of generator) yield [prefix, value]; } async function gatherIterator(i) { const out = []; for await (const item of await i) out.push(item); return out; } function gatherIteratorSync(i) { const out = []; for (const item of i) out.push(item); return out; } function patchConfigurable(config, patch) { if (!config) return { configurable: patch }; else if (!("configurable" in config)) return { ...config, configurable: patch }; else return { ...config, configurable: { ...config.configurable, ...patch } }; } function isAsyncGeneratorFunction(val) { return val != null && typeof val === "function" && val instanceof Object.getPrototypeOf(async function* () {}).constructor; } function isGeneratorFunction(val) { return val != null && typeof val === "function" && val instanceof Object.getPrototypeOf(function* () {}).constructor; } //#endregion export { RunnableCallable, gatherIterator, gatherIteratorSync, isAsyncGeneratorFunction, isGeneratorFunction, patchConfigurable, prefixGenerator }; //# sourceMappingURL=utils.js.map