base.cjs 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.convertRunnableToTool = exports.RunnableToolLike = exports.RunnablePick = exports.RunnableAssign = exports._coerceToRunnable = exports.RunnableWithFallbacks = exports.RunnableParallel = exports.RunnableLambda = exports.RunnableTraceable = exports.RunnableMap = exports.RunnableSequence = exports.RunnableRetry = exports.RunnableEach = exports.RunnableBinding = exports.Runnable = exports._coerceToDict = void 0;
  7. const zod_1 = require("zod");
  8. const p_retry_1 = __importDefault(require("p-retry"));
  9. const uuid_1 = require("uuid");
  10. const traceable_1 = require("langsmith/singletons/traceable");
  11. const log_stream_js_1 = require("../tracers/log_stream.cjs");
  12. const event_stream_js_1 = require("../tracers/event_stream.cjs");
  13. const serializable_js_1 = require("../load/serializable.cjs");
  14. const stream_js_1 = require("../utils/stream.cjs");
  15. const signal_js_1 = require("../utils/signal.cjs");
  16. const config_js_1 = require("./config.cjs");
  17. const async_caller_js_1 = require("../utils/async_caller.cjs");
  18. const root_listener_js_1 = require("../tracers/root_listener.cjs");
  19. const utils_js_1 = require("./utils.cjs");
  20. const index_js_1 = require("../singletons/index.cjs");
  21. const graph_js_1 = require("./graph.cjs");
  22. const wrappers_js_1 = require("./wrappers.cjs");
  23. const iter_js_1 = require("./iter.cjs");
  24. const utils_js_2 = require("../tools/utils.cjs");
  25. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  26. function _coerceToDict(value, defaultKey) {
  27. return value &&
  28. !Array.isArray(value) &&
  29. // eslint-disable-next-line no-instanceof/no-instanceof
  30. !(value instanceof Date) &&
  31. typeof value === "object"
  32. ? value
  33. : { [defaultKey]: value };
  34. }
  35. exports._coerceToDict = _coerceToDict;
  36. /**
  37. * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
  38. * transformed.
  39. */
  40. class Runnable extends serializable_js_1.Serializable {
  41. constructor() {
  42. super(...arguments);
  43. Object.defineProperty(this, "lc_runnable", {
  44. enumerable: true,
  45. configurable: true,
  46. writable: true,
  47. value: true
  48. });
  49. Object.defineProperty(this, "name", {
  50. enumerable: true,
  51. configurable: true,
  52. writable: true,
  53. value: void 0
  54. });
  55. }
  56. getName(suffix) {
  57. const name =
  58. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  59. this.name ?? this.constructor.lc_name() ?? this.constructor.name;
  60. return suffix ? `${name}${suffix}` : name;
  61. }
  62. /**
  63. * Bind arguments to a Runnable, returning a new Runnable.
  64. * @param kwargs
  65. * @returns A new RunnableBinding that, when invoked, will apply the bound args.
  66. *
  67. * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
  68. */
  69. bind(kwargs) {
  70. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  71. return new RunnableBinding({ bound: this, kwargs, config: {} });
  72. }
  73. /**
  74. * Return a new Runnable that maps a list of inputs to a list of outputs,
  75. * by calling invoke() with each input.
  76. *
  77. * @deprecated This will be removed in the next breaking release.
  78. */
  79. map() {
  80. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  81. return new RunnableEach({ bound: this });
  82. }
  83. /**
  84. * Add retry logic to an existing runnable.
  85. * @param fields.stopAfterAttempt The number of attempts to retry.
  86. * @param fields.onFailedAttempt A function that is called when a retry fails.
  87. * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
  88. */
  89. withRetry(fields) {
  90. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  91. return new RunnableRetry({
  92. bound: this,
  93. kwargs: {},
  94. config: {},
  95. maxAttemptNumber: fields?.stopAfterAttempt,
  96. ...fields,
  97. });
  98. }
  99. /**
  100. * Bind config to a Runnable, returning a new Runnable.
  101. * @param config New configuration parameters to attach to the new runnable.
  102. * @returns A new RunnableBinding with a config matching what's passed.
  103. */
  104. withConfig(config) {
  105. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  106. return new RunnableBinding({
  107. bound: this,
  108. config,
  109. kwargs: {},
  110. });
  111. }
  112. /**
  113. * Create a new runnable from the current one that will try invoking
  114. * other passed fallback runnables if the initial invocation fails.
  115. * @param fields.fallbacks Other runnables to call if the runnable errors.
  116. * @returns A new RunnableWithFallbacks.
  117. */
  118. withFallbacks(fields) {
  119. const fallbacks = Array.isArray(fields) ? fields : fields.fallbacks;
  120. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  121. return new RunnableWithFallbacks({
  122. runnable: this,
  123. fallbacks,
  124. });
  125. }
  126. _getOptionsList(options, length = 0) {
  127. if (Array.isArray(options) && options.length !== length) {
  128. throw new Error(`Passed "options" must be an array with the same length as the inputs, but got ${options.length} options for ${length} inputs`);
  129. }
  130. if (Array.isArray(options)) {
  131. return options.map(config_js_1.ensureConfig);
  132. }
  133. if (length > 1 && !Array.isArray(options) && options.runId) {
  134. console.warn("Provided runId will be used only for the first element of the batch.");
  135. const subsequent = Object.fromEntries(Object.entries(options).filter(([key]) => key !== "runId"));
  136. return Array.from({ length }, (_, i) => (0, config_js_1.ensureConfig)(i === 0 ? options : subsequent));
  137. }
  138. return Array.from({ length }, () => (0, config_js_1.ensureConfig)(options));
  139. }
  140. async batch(inputs, options, batchOptions) {
  141. const configList = this._getOptionsList(options ?? {}, inputs.length);
  142. const maxConcurrency = configList[0]?.maxConcurrency ?? batchOptions?.maxConcurrency;
  143. const caller = new async_caller_js_1.AsyncCaller({
  144. maxConcurrency,
  145. onFailedAttempt: (e) => {
  146. throw e;
  147. },
  148. });
  149. const batchCalls = inputs.map((input, i) => caller.call(async () => {
  150. try {
  151. const result = await this.invoke(input, configList[i]);
  152. return result;
  153. }
  154. catch (e) {
  155. if (batchOptions?.returnExceptions) {
  156. return e;
  157. }
  158. throw e;
  159. }
  160. }));
  161. return Promise.all(batchCalls);
  162. }
  163. /**
  164. * Default streaming implementation.
  165. * Subclasses should override this method if they support streaming output.
  166. * @param input
  167. * @param options
  168. */
  169. async *_streamIterator(input, options) {
  170. yield this.invoke(input, options);
  171. }
  172. /**
  173. * Stream output in chunks.
  174. * @param input
  175. * @param options
  176. * @returns A readable stream that is also an iterable.
  177. */
  178. async stream(input, options) {
  179. // Buffer the first streamed chunk to allow for initial errors
  180. // to surface immediately.
  181. const config = (0, config_js_1.ensureConfig)(options);
  182. const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
  183. generator: this._streamIterator(input, config),
  184. config,
  185. });
  186. await wrappedGenerator.setup;
  187. return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
  188. }
  189. _separateRunnableConfigFromCallOptions(options) {
  190. let runnableConfig;
  191. if (options === undefined) {
  192. runnableConfig = (0, config_js_1.ensureConfig)(options);
  193. }
  194. else {
  195. runnableConfig = (0, config_js_1.ensureConfig)({
  196. callbacks: options.callbacks,
  197. tags: options.tags,
  198. metadata: options.metadata,
  199. runName: options.runName,
  200. configurable: options.configurable,
  201. recursionLimit: options.recursionLimit,
  202. maxConcurrency: options.maxConcurrency,
  203. runId: options.runId,
  204. timeout: options.timeout,
  205. signal: options.signal,
  206. });
  207. }
  208. const callOptions = { ...options };
  209. delete callOptions.callbacks;
  210. delete callOptions.tags;
  211. delete callOptions.metadata;
  212. delete callOptions.runName;
  213. delete callOptions.configurable;
  214. delete callOptions.recursionLimit;
  215. delete callOptions.maxConcurrency;
  216. delete callOptions.runId;
  217. delete callOptions.timeout;
  218. delete callOptions.signal;
  219. return [runnableConfig, callOptions];
  220. }
  221. async _callWithConfig(func, input, options) {
  222. const config = (0, config_js_1.ensureConfig)(options);
  223. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
  224. const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), config.runId, config?.runType, undefined, undefined, config?.runName ?? this.getName());
  225. delete config.runId;
  226. let output;
  227. try {
  228. const promise = func.call(this, input, config, runManager);
  229. output = await (0, signal_js_1.raceWithSignal)(promise, options?.signal);
  230. }
  231. catch (e) {
  232. await runManager?.handleChainError(e);
  233. throw e;
  234. }
  235. await runManager?.handleChainEnd(_coerceToDict(output, "output"));
  236. return output;
  237. }
  238. /**
  239. * Internal method that handles batching and configuration for a runnable
  240. * It takes a function, input values, and optional configuration, and
  241. * returns a promise that resolves to the output values.
  242. * @param func The function to be executed for each input value.
  243. * @param input The input values to be processed.
  244. * @param config Optional configuration for the function execution.
  245. * @returns A promise that resolves to the output values.
  246. */
  247. async _batchWithConfig(func, inputs, options, batchOptions) {
  248. const optionsList = this._getOptionsList(options ?? {}, inputs.length);
  249. const callbackManagers = await Promise.all(optionsList.map(config_js_1.getCallbackManagerForConfig));
  250. const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
  251. const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), optionsList[i].runId, optionsList[i].runType, undefined, undefined, optionsList[i].runName ?? this.getName());
  252. delete optionsList[i].runId;
  253. return handleStartRes;
  254. }));
  255. let outputs;
  256. try {
  257. const promise = func.call(this, inputs, optionsList, runManagers, batchOptions);
  258. outputs = await (0, signal_js_1.raceWithSignal)(promise, optionsList?.[0]?.signal);
  259. }
  260. catch (e) {
  261. await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
  262. throw e;
  263. }
  264. await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(outputs, "output"))));
  265. return outputs;
  266. }
  267. /**
  268. * Helper method to transform an Iterator of Input values into an Iterator of
  269. * Output values, with callbacks.
  270. * Use this to implement `stream()` or `transform()` in Runnable subclasses.
  271. */
  272. async *_transformStreamWithConfig(inputGenerator, transformer, options) {
  273. let finalInput;
  274. let finalInputSupported = true;
  275. let finalOutput;
  276. let finalOutputSupported = true;
  277. const config = (0, config_js_1.ensureConfig)(options);
  278. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
  279. async function* wrapInputForTracing() {
  280. for await (const chunk of inputGenerator) {
  281. if (finalInputSupported) {
  282. if (finalInput === undefined) {
  283. finalInput = chunk;
  284. }
  285. else {
  286. try {
  287. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  288. finalInput = (0, stream_js_1.concat)(finalInput, chunk);
  289. }
  290. catch {
  291. finalInput = undefined;
  292. finalInputSupported = false;
  293. }
  294. }
  295. }
  296. yield chunk;
  297. }
  298. }
  299. let runManager;
  300. try {
  301. const pipe = await (0, stream_js_1.pipeGeneratorWithSetup)(transformer.bind(this), wrapInputForTracing(), async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, config.runId, config.runType, undefined, undefined, config.runName ?? this.getName()), options?.signal, config);
  302. delete config.runId;
  303. runManager = pipe.setup;
  304. const streamEventsHandler = runManager?.handlers.find(event_stream_js_1.isStreamEventsHandler);
  305. let iterator = pipe.output;
  306. if (streamEventsHandler !== undefined && runManager !== undefined) {
  307. iterator = streamEventsHandler.tapOutputIterable(runManager.runId, iterator);
  308. }
  309. const streamLogHandler = runManager?.handlers.find(log_stream_js_1.isLogStreamHandler);
  310. if (streamLogHandler !== undefined && runManager !== undefined) {
  311. iterator = streamLogHandler.tapOutputIterable(runManager.runId, iterator);
  312. }
  313. for await (const chunk of iterator) {
  314. yield chunk;
  315. if (finalOutputSupported) {
  316. if (finalOutput === undefined) {
  317. finalOutput = chunk;
  318. }
  319. else {
  320. try {
  321. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  322. finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
  323. }
  324. catch {
  325. finalOutput = undefined;
  326. finalOutputSupported = false;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. catch (e) {
  333. await runManager?.handleChainError(e, undefined, undefined, undefined, {
  334. inputs: _coerceToDict(finalInput, "input"),
  335. });
  336. throw e;
  337. }
  338. await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
  339. }
  340. getGraph(_) {
  341. const graph = new graph_js_1.Graph();
  342. // TODO: Add input schema for runnables
  343. const inputNode = graph.addNode({
  344. name: `${this.getName()}Input`,
  345. schema: zod_1.z.any(),
  346. });
  347. const runnableNode = graph.addNode(this);
  348. // TODO: Add output schemas for runnables
  349. const outputNode = graph.addNode({
  350. name: `${this.getName()}Output`,
  351. schema: zod_1.z.any(),
  352. });
  353. graph.addEdge(inputNode, runnableNode);
  354. graph.addEdge(runnableNode, outputNode);
  355. return graph;
  356. }
  357. /**
  358. * Create a new runnable sequence that runs each individual runnable in series,
  359. * piping the output of one runnable into another runnable or runnable-like.
  360. * @param coerceable A runnable, function, or object whose values are functions or runnables.
  361. * @returns A new runnable sequence.
  362. */
  363. pipe(coerceable) {
  364. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  365. return new RunnableSequence({
  366. first: this,
  367. last: _coerceToRunnable(coerceable),
  368. });
  369. }
  370. /**
  371. * Pick keys from the dict output of this runnable. Returns a new runnable.
  372. */
  373. pick(keys) {
  374. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  375. return this.pipe(new RunnablePick(keys));
  376. }
  377. /**
  378. * Assigns new fields to the dict output of this runnable. Returns a new runnable.
  379. */
  380. assign(mapping) {
  381. return this.pipe(
  382. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  383. new RunnableAssign(
  384. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  385. new RunnableMap({ steps: mapping })));
  386. }
  387. /**
  388. * Default implementation of transform, which buffers input and then calls stream.
  389. * Subclasses should override this method if they can start producing output while
  390. * input is still being generated.
  391. * @param generator
  392. * @param options
  393. */
  394. async *transform(generator, options) {
  395. let finalChunk;
  396. for await (const chunk of generator) {
  397. if (finalChunk === undefined) {
  398. finalChunk = chunk;
  399. }
  400. else {
  401. // Make a best effort to gather, for any type that supports concat.
  402. // This method should throw an error if gathering fails.
  403. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  404. finalChunk = (0, stream_js_1.concat)(finalChunk, chunk);
  405. }
  406. }
  407. yield* this._streamIterator(finalChunk, (0, config_js_1.ensureConfig)(options));
  408. }
  409. /**
  410. * Stream all output from a runnable, as reported to the callback system.
  411. * This includes all inner runs of LLMs, Retrievers, Tools, etc.
  412. * Output is streamed as Log objects, which include a list of
  413. * jsonpatch ops that describe how the state of the run has changed in each
  414. * step, and the final state of the run.
  415. * The jsonpatch ops can be applied in order to construct state.
  416. * @param input
  417. * @param options
  418. * @param streamOptions
  419. */
  420. async *streamLog(input, options, streamOptions) {
  421. const logStreamCallbackHandler = new log_stream_js_1.LogStreamCallbackHandler({
  422. ...streamOptions,
  423. autoClose: false,
  424. _schemaFormat: "original",
  425. });
  426. const config = (0, config_js_1.ensureConfig)(options);
  427. yield* this._streamLog(input, logStreamCallbackHandler, config);
  428. }
  429. async *_streamLog(input, logStreamCallbackHandler, config) {
  430. const { callbacks } = config;
  431. if (callbacks === undefined) {
  432. // eslint-disable-next-line no-param-reassign
  433. config.callbacks = [logStreamCallbackHandler];
  434. }
  435. else if (Array.isArray(callbacks)) {
  436. // eslint-disable-next-line no-param-reassign
  437. config.callbacks = callbacks.concat([logStreamCallbackHandler]);
  438. }
  439. else {
  440. const copiedCallbacks = callbacks.copy();
  441. copiedCallbacks.addHandler(logStreamCallbackHandler, true);
  442. // eslint-disable-next-line no-param-reassign
  443. config.callbacks = copiedCallbacks;
  444. }
  445. const runnableStreamPromise = this.stream(input, config);
  446. async function consumeRunnableStream() {
  447. try {
  448. const runnableStream = await runnableStreamPromise;
  449. for await (const chunk of runnableStream) {
  450. const patch = new log_stream_js_1.RunLogPatch({
  451. ops: [
  452. {
  453. op: "add",
  454. path: "/streamed_output/-",
  455. value: chunk,
  456. },
  457. ],
  458. });
  459. await logStreamCallbackHandler.writer.write(patch);
  460. }
  461. }
  462. finally {
  463. await logStreamCallbackHandler.writer.close();
  464. }
  465. }
  466. const runnableStreamConsumePromise = consumeRunnableStream();
  467. try {
  468. for await (const log of logStreamCallbackHandler) {
  469. yield log;
  470. }
  471. }
  472. finally {
  473. await runnableStreamConsumePromise;
  474. }
  475. }
  476. streamEvents(input, options, streamOptions) {
  477. let stream;
  478. if (options.version === "v1") {
  479. stream = this._streamEventsV1(input, options, streamOptions);
  480. }
  481. else if (options.version === "v2") {
  482. stream = this._streamEventsV2(input, options, streamOptions);
  483. }
  484. else {
  485. throw new Error(`Only versions "v1" and "v2" of the schema are currently supported.`);
  486. }
  487. if (options.encoding === "text/event-stream") {
  488. return (0, wrappers_js_1.convertToHttpEventStream)(stream);
  489. }
  490. else {
  491. return stream_js_1.IterableReadableStream.fromAsyncGenerator(stream);
  492. }
  493. }
  494. async *_streamEventsV2(input, options, streamOptions) {
  495. const eventStreamer = new event_stream_js_1.EventStreamCallbackHandler({
  496. ...streamOptions,
  497. autoClose: false,
  498. });
  499. const config = (0, config_js_1.ensureConfig)(options);
  500. const runId = config.runId ?? (0, uuid_1.v4)();
  501. config.runId = runId;
  502. const callbacks = config.callbacks;
  503. if (callbacks === undefined) {
  504. config.callbacks = [eventStreamer];
  505. }
  506. else if (Array.isArray(callbacks)) {
  507. config.callbacks = callbacks.concat(eventStreamer);
  508. }
  509. else {
  510. const copiedCallbacks = callbacks.copy();
  511. copiedCallbacks.addHandler(eventStreamer, true);
  512. // eslint-disable-next-line no-param-reassign
  513. config.callbacks = copiedCallbacks;
  514. }
  515. const abortController = new AbortController();
  516. // Call the runnable in streaming mode,
  517. // add each chunk to the output stream
  518. const outerThis = this;
  519. async function consumeRunnableStream() {
  520. try {
  521. let signal;
  522. if (options?.signal) {
  523. if ("any" in AbortSignal) {
  524. // Use native AbortSignal.any() if available (Node 19+)
  525. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  526. signal = AbortSignal.any([
  527. abortController.signal,
  528. options.signal,
  529. ]);
  530. }
  531. else {
  532. // Fallback for Node 18 and below - just use the provided signal
  533. signal = options.signal;
  534. // Ensure we still abort our controller when the parent signal aborts
  535. options.signal.addEventListener("abort", () => {
  536. abortController.abort();
  537. }, { once: true });
  538. }
  539. }
  540. else {
  541. signal = abortController.signal;
  542. }
  543. const runnableStream = await outerThis.stream(input, {
  544. ...config,
  545. signal,
  546. });
  547. const tappedStream = eventStreamer.tapOutputIterable(runId, runnableStream);
  548. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  549. for await (const _ of tappedStream) {
  550. // Just iterate so that the callback handler picks up events
  551. if (abortController.signal.aborted)
  552. break;
  553. }
  554. }
  555. finally {
  556. await eventStreamer.finish();
  557. }
  558. }
  559. const runnableStreamConsumePromise = consumeRunnableStream();
  560. let firstEventSent = false;
  561. let firstEventRunId;
  562. try {
  563. for await (const event of eventStreamer) {
  564. // This is a work-around an issue where the inputs into the
  565. // chain are not available until the entire input is consumed.
  566. // As a temporary solution, we'll modify the input to be the input
  567. // that was passed into the chain.
  568. if (!firstEventSent) {
  569. event.data.input = input;
  570. firstEventSent = true;
  571. firstEventRunId = event.run_id;
  572. yield event;
  573. continue;
  574. }
  575. if (event.run_id === firstEventRunId && event.event.endsWith("_end")) {
  576. // If it's the end event corresponding to the root runnable
  577. // we dont include the input in the event since it's guaranteed
  578. // to be included in the first event.
  579. if (event.data?.input) {
  580. delete event.data.input;
  581. }
  582. }
  583. yield event;
  584. }
  585. }
  586. finally {
  587. abortController.abort();
  588. await runnableStreamConsumePromise;
  589. }
  590. }
  591. async *_streamEventsV1(input, options, streamOptions) {
  592. let runLog;
  593. let hasEncounteredStartEvent = false;
  594. const config = (0, config_js_1.ensureConfig)(options);
  595. const rootTags = config.tags ?? [];
  596. const rootMetadata = config.metadata ?? {};
  597. const rootName = config.runName ?? this.getName();
  598. const logStreamCallbackHandler = new log_stream_js_1.LogStreamCallbackHandler({
  599. ...streamOptions,
  600. autoClose: false,
  601. _schemaFormat: "streaming_events",
  602. });
  603. const rootEventFilter = new utils_js_1._RootEventFilter({
  604. ...streamOptions,
  605. });
  606. const logStream = this._streamLog(input, logStreamCallbackHandler, config);
  607. for await (const log of logStream) {
  608. if (!runLog) {
  609. runLog = log_stream_js_1.RunLog.fromRunLogPatch(log);
  610. }
  611. else {
  612. runLog = runLog.concat(log);
  613. }
  614. if (runLog.state === undefined) {
  615. throw new Error(`Internal error: "streamEvents" state is missing. Please open a bug report.`);
  616. }
  617. // Yield the start event for the root runnable if it hasn't been seen.
  618. // The root run is never filtered out
  619. if (!hasEncounteredStartEvent) {
  620. hasEncounteredStartEvent = true;
  621. const state = { ...runLog.state };
  622. const event = {
  623. run_id: state.id,
  624. event: `on_${state.type}_start`,
  625. name: rootName,
  626. tags: rootTags,
  627. metadata: rootMetadata,
  628. data: {
  629. input,
  630. },
  631. };
  632. if (rootEventFilter.includeEvent(event, state.type)) {
  633. yield event;
  634. }
  635. }
  636. const paths = log.ops
  637. .filter((op) => op.path.startsWith("/logs/"))
  638. .map((op) => op.path.split("/")[2]);
  639. const dedupedPaths = [...new Set(paths)];
  640. for (const path of dedupedPaths) {
  641. let eventType;
  642. let data = {};
  643. const logEntry = runLog.state.logs[path];
  644. if (logEntry.end_time === undefined) {
  645. if (logEntry.streamed_output.length > 0) {
  646. eventType = "stream";
  647. }
  648. else {
  649. eventType = "start";
  650. }
  651. }
  652. else {
  653. eventType = "end";
  654. }
  655. if (eventType === "start") {
  656. // Include the inputs with the start event if they are available.
  657. // Usually they will NOT be available for components that operate
  658. // on streams, since those components stream the input and
  659. // don't know its final value until the end of the stream.
  660. if (logEntry.inputs !== undefined) {
  661. data.input = logEntry.inputs;
  662. }
  663. }
  664. else if (eventType === "end") {
  665. if (logEntry.inputs !== undefined) {
  666. data.input = logEntry.inputs;
  667. }
  668. data.output = logEntry.final_output;
  669. }
  670. else if (eventType === "stream") {
  671. const chunkCount = logEntry.streamed_output.length;
  672. if (chunkCount !== 1) {
  673. throw new Error(`Expected exactly one chunk of streamed output, got ${chunkCount} instead. Encountered in: "${logEntry.name}"`);
  674. }
  675. data = { chunk: logEntry.streamed_output[0] };
  676. // Clean up the stream, we don't need it anymore.
  677. // And this avoids duplicates as well!
  678. logEntry.streamed_output = [];
  679. }
  680. yield {
  681. event: `on_${logEntry.type}_${eventType}`,
  682. name: logEntry.name,
  683. run_id: logEntry.id,
  684. tags: logEntry.tags,
  685. metadata: logEntry.metadata,
  686. data,
  687. };
  688. }
  689. // Finally, we take care of the streaming output from the root chain
  690. // if there is any.
  691. const { state } = runLog;
  692. if (state.streamed_output.length > 0) {
  693. const chunkCount = state.streamed_output.length;
  694. if (chunkCount !== 1) {
  695. throw new Error(`Expected exactly one chunk of streamed output, got ${chunkCount} instead. Encountered in: "${state.name}"`);
  696. }
  697. const data = { chunk: state.streamed_output[0] };
  698. // Clean up the stream, we don't need it anymore.
  699. state.streamed_output = [];
  700. const event = {
  701. event: `on_${state.type}_stream`,
  702. run_id: state.id,
  703. tags: rootTags,
  704. metadata: rootMetadata,
  705. name: rootName,
  706. data,
  707. };
  708. if (rootEventFilter.includeEvent(event, state.type)) {
  709. yield event;
  710. }
  711. }
  712. }
  713. const state = runLog?.state;
  714. if (state !== undefined) {
  715. // Finally, yield the end event for the root runnable.
  716. const event = {
  717. event: `on_${state.type}_end`,
  718. name: rootName,
  719. run_id: state.id,
  720. tags: rootTags,
  721. metadata: rootMetadata,
  722. data: {
  723. output: state.final_output,
  724. },
  725. };
  726. if (rootEventFilter.includeEvent(event, state.type))
  727. yield event;
  728. }
  729. }
  730. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  731. static isRunnable(thing) {
  732. return (0, utils_js_1.isRunnableInterface)(thing);
  733. }
  734. /**
  735. * Bind lifecycle listeners to a Runnable, returning a new Runnable.
  736. * The Run object contains information about the run, including its id,
  737. * type, input, output, error, startTime, endTime, and any tags or metadata
  738. * added to the run.
  739. *
  740. * @param {Object} params - The object containing the callback functions.
  741. * @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
  742. * @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
  743. * @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
  744. */
  745. withListeners({ onStart, onEnd, onError, }) {
  746. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  747. return new RunnableBinding({
  748. bound: this,
  749. config: {},
  750. configFactories: [
  751. (config) => ({
  752. callbacks: [
  753. new root_listener_js_1.RootListenersTracer({
  754. config,
  755. onStart,
  756. onEnd,
  757. onError,
  758. }),
  759. ],
  760. }),
  761. ],
  762. });
  763. }
  764. /**
  765. * Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
  766. * which contains the runnable, name, description and schema.
  767. *
  768. * @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
  769. *
  770. * @param fields
  771. * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
  772. * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
  773. * @param {z.ZodType<T>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
  774. * @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
  775. */
  776. asTool(fields) {
  777. return convertRunnableToTool(this, fields);
  778. }
  779. }
  780. exports.Runnable = Runnable;
  781. /**
  782. * Wraps a runnable and applies partial config upon invocation.
  783. *
  784. * @example
  785. * ```typescript
  786. * import {
  787. * type RunnableConfig,
  788. * RunnableLambda,
  789. * } from "@langchain/core/runnables";
  790. *
  791. * const enhanceProfile = (
  792. * profile: Record<string, any>,
  793. * config?: RunnableConfig
  794. * ) => {
  795. * if (config?.configurable?.role) {
  796. * return { ...profile, role: config.configurable.role };
  797. * }
  798. * return profile;
  799. * };
  800. *
  801. * const runnable = RunnableLambda.from(enhanceProfile);
  802. *
  803. * // Bind configuration to the runnable to set the user's role dynamically
  804. * const adminRunnable = runnable.bind({ configurable: { role: "Admin" } });
  805. * const userRunnable = runnable.bind({ configurable: { role: "User" } });
  806. *
  807. * const result1 = await adminRunnable.invoke({
  808. * name: "Alice",
  809. * email: "alice@example.com"
  810. * });
  811. *
  812. * // { name: "Alice", email: "alice@example.com", role: "Admin" }
  813. *
  814. * const result2 = await userRunnable.invoke({
  815. * name: "Bob",
  816. * email: "bob@example.com"
  817. * });
  818. *
  819. * // { name: "Bob", email: "bob@example.com", role: "User" }
  820. * ```
  821. */
  822. class RunnableBinding extends Runnable {
  823. static lc_name() {
  824. return "RunnableBinding";
  825. }
  826. constructor(fields) {
  827. super(fields);
  828. Object.defineProperty(this, "lc_namespace", {
  829. enumerable: true,
  830. configurable: true,
  831. writable: true,
  832. value: ["langchain_core", "runnables"]
  833. });
  834. Object.defineProperty(this, "lc_serializable", {
  835. enumerable: true,
  836. configurable: true,
  837. writable: true,
  838. value: true
  839. });
  840. Object.defineProperty(this, "bound", {
  841. enumerable: true,
  842. configurable: true,
  843. writable: true,
  844. value: void 0
  845. });
  846. Object.defineProperty(this, "config", {
  847. enumerable: true,
  848. configurable: true,
  849. writable: true,
  850. value: void 0
  851. });
  852. Object.defineProperty(this, "kwargs", {
  853. enumerable: true,
  854. configurable: true,
  855. writable: true,
  856. value: void 0
  857. });
  858. Object.defineProperty(this, "configFactories", {
  859. enumerable: true,
  860. configurable: true,
  861. writable: true,
  862. value: void 0
  863. });
  864. this.bound = fields.bound;
  865. this.kwargs = fields.kwargs;
  866. this.config = fields.config;
  867. this.configFactories = fields.configFactories;
  868. }
  869. getName(suffix) {
  870. return this.bound.getName(suffix);
  871. }
  872. async _mergeConfig(...options) {
  873. const config = (0, config_js_1.mergeConfigs)(this.config, ...options);
  874. return (0, config_js_1.mergeConfigs)(config, ...(this.configFactories
  875. ? await Promise.all(this.configFactories.map(async (configFactory) => await configFactory(config)))
  876. : []));
  877. }
  878. /**
  879. * Binds the runnable with the specified arguments.
  880. * @param kwargs The arguments to bind the runnable with.
  881. * @returns A new instance of the `RunnableBinding` class that is bound with the specified arguments.
  882. *
  883. * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
  884. */
  885. bind(kwargs) {
  886. return new this.constructor({
  887. bound: this.bound,
  888. kwargs: { ...this.kwargs, ...kwargs },
  889. config: this.config,
  890. });
  891. }
  892. withConfig(config) {
  893. return new this.constructor({
  894. bound: this.bound,
  895. kwargs: this.kwargs,
  896. config: { ...this.config, ...config },
  897. });
  898. }
  899. withRetry(fields) {
  900. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  901. return new RunnableRetry({
  902. bound: this.bound,
  903. kwargs: this.kwargs,
  904. config: this.config,
  905. maxAttemptNumber: fields?.stopAfterAttempt,
  906. ...fields,
  907. });
  908. }
  909. async invoke(input, options) {
  910. return this.bound.invoke(input, await this._mergeConfig((0, config_js_1.ensureConfig)(options), this.kwargs));
  911. }
  912. async batch(inputs, options, batchOptions) {
  913. const mergedOptions = Array.isArray(options)
  914. ? await Promise.all(options.map(async (individualOption) => this._mergeConfig((0, config_js_1.ensureConfig)(individualOption), this.kwargs)))
  915. : await this._mergeConfig((0, config_js_1.ensureConfig)(options), this.kwargs);
  916. return this.bound.batch(inputs, mergedOptions, batchOptions);
  917. }
  918. async *_streamIterator(input, options) {
  919. yield* this.bound._streamIterator(input, await this._mergeConfig((0, config_js_1.ensureConfig)(options), this.kwargs));
  920. }
  921. async stream(input, options) {
  922. return this.bound.stream(input, await this._mergeConfig((0, config_js_1.ensureConfig)(options), this.kwargs));
  923. }
  924. async *transform(generator, options) {
  925. yield* this.bound.transform(generator, await this._mergeConfig((0, config_js_1.ensureConfig)(options), this.kwargs));
  926. }
  927. streamEvents(input, options, streamOptions) {
  928. // eslint-disable-next-line @typescript-eslint/no-this-alias
  929. const outerThis = this;
  930. const generator = async function* () {
  931. yield* outerThis.bound.streamEvents(input, {
  932. ...(await outerThis._mergeConfig((0, config_js_1.ensureConfig)(options), outerThis.kwargs)),
  933. version: options.version,
  934. }, streamOptions);
  935. };
  936. return stream_js_1.IterableReadableStream.fromAsyncGenerator(generator());
  937. }
  938. static isRunnableBinding(
  939. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  940. thing
  941. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  942. ) {
  943. return thing.bound && Runnable.isRunnable(thing.bound);
  944. }
  945. /**
  946. * Bind lifecycle listeners to a Runnable, returning a new Runnable.
  947. * The Run object contains information about the run, including its id,
  948. * type, input, output, error, startTime, endTime, and any tags or metadata
  949. * added to the run.
  950. *
  951. * @param {Object} params - The object containing the callback functions.
  952. * @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
  953. * @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
  954. * @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
  955. */
  956. withListeners({ onStart, onEnd, onError, }) {
  957. return new RunnableBinding({
  958. bound: this.bound,
  959. kwargs: this.kwargs,
  960. config: this.config,
  961. configFactories: [
  962. (config) => ({
  963. callbacks: [
  964. new root_listener_js_1.RootListenersTracer({
  965. config,
  966. onStart,
  967. onEnd,
  968. onError,
  969. }),
  970. ],
  971. }),
  972. ],
  973. });
  974. }
  975. }
  976. exports.RunnableBinding = RunnableBinding;
  977. /**
  978. * A runnable that delegates calls to another runnable
  979. * with each element of the input sequence.
  980. * @example
  981. * ```typescript
  982. * import { RunnableEach, RunnableLambda } from "@langchain/core/runnables";
  983. *
  984. * const toUpperCase = (input: string): string => input.toUpperCase();
  985. * const addGreeting = (input: string): string => `Hello, ${input}!`;
  986. *
  987. * const upperCaseLambda = RunnableLambda.from(toUpperCase);
  988. * const greetingLambda = RunnableLambda.from(addGreeting);
  989. *
  990. * const chain = new RunnableEach({
  991. * bound: upperCaseLambda.pipe(greetingLambda),
  992. * });
  993. *
  994. * const result = await chain.invoke(["alice", "bob", "carol"])
  995. *
  996. * // ["Hello, ALICE!", "Hello, BOB!", "Hello, CAROL!"]
  997. * ```
  998. *
  999. * @deprecated This will be removed in the next breaking release.
  1000. */
  1001. class RunnableEach extends Runnable {
  1002. static lc_name() {
  1003. return "RunnableEach";
  1004. }
  1005. constructor(fields) {
  1006. super(fields);
  1007. Object.defineProperty(this, "lc_serializable", {
  1008. enumerable: true,
  1009. configurable: true,
  1010. writable: true,
  1011. value: true
  1012. });
  1013. Object.defineProperty(this, "lc_namespace", {
  1014. enumerable: true,
  1015. configurable: true,
  1016. writable: true,
  1017. value: ["langchain_core", "runnables"]
  1018. });
  1019. Object.defineProperty(this, "bound", {
  1020. enumerable: true,
  1021. configurable: true,
  1022. writable: true,
  1023. value: void 0
  1024. });
  1025. this.bound = fields.bound;
  1026. }
  1027. /**
  1028. * Binds the runnable with the specified arguments.
  1029. * @param kwargs The arguments to bind the runnable with.
  1030. * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments.
  1031. *
  1032. * @deprecated Use {@link withConfig} instead. This will be removed in the next breaking release.
  1033. */
  1034. bind(kwargs) {
  1035. return new RunnableEach({
  1036. bound: this.bound.bind(kwargs),
  1037. });
  1038. }
  1039. /**
  1040. * Invokes the runnable with the specified input and configuration.
  1041. * @param input The input to invoke the runnable with.
  1042. * @param config The configuration to invoke the runnable with.
  1043. * @returns A promise that resolves to the output of the runnable.
  1044. */
  1045. async invoke(inputs, config) {
  1046. return this._callWithConfig(this._invoke.bind(this), inputs, config);
  1047. }
  1048. /**
  1049. * A helper method that is used to invoke the runnable with the specified input and configuration.
  1050. * @param input The input to invoke the runnable with.
  1051. * @param config The configuration to invoke the runnable with.
  1052. * @returns A promise that resolves to the output of the runnable.
  1053. */
  1054. async _invoke(inputs, config, runManager) {
  1055. return this.bound.batch(inputs, (0, config_js_1.patchConfig)(config, { callbacks: runManager?.getChild() }));
  1056. }
  1057. /**
  1058. * Bind lifecycle listeners to a Runnable, returning a new Runnable.
  1059. * The Run object contains information about the run, including its id,
  1060. * type, input, output, error, startTime, endTime, and any tags or metadata
  1061. * added to the run.
  1062. *
  1063. * @param {Object} params - The object containing the callback functions.
  1064. * @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
  1065. * @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
  1066. * @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
  1067. */
  1068. withListeners({ onStart, onEnd, onError, }) {
  1069. return new RunnableEach({
  1070. bound: this.bound.withListeners({ onStart, onEnd, onError }),
  1071. });
  1072. }
  1073. }
  1074. exports.RunnableEach = RunnableEach;
  1075. /**
  1076. * Base class for runnables that can be retried a
  1077. * specified number of times.
  1078. * @example
  1079. * ```typescript
  1080. * import {
  1081. * RunnableLambda,
  1082. * RunnableRetry,
  1083. * } from "@langchain/core/runnables";
  1084. *
  1085. * // Simulate an API call that fails
  1086. * const simulateApiCall = (input: string): string => {
  1087. * console.log(`Attempting API call with input: ${input}`);
  1088. * throw new Error("API call failed due to network issue");
  1089. * };
  1090. *
  1091. * const apiCallLambda = RunnableLambda.from(simulateApiCall);
  1092. *
  1093. * // Apply retry logic using the .withRetry() method
  1094. * const apiCallWithRetry = apiCallLambda.withRetry({ stopAfterAttempt: 3 });
  1095. *
  1096. * // Alternatively, create a RunnableRetry instance manually
  1097. * const manualRetry = new RunnableRetry({
  1098. * bound: apiCallLambda,
  1099. * maxAttemptNumber: 3,
  1100. * config: {},
  1101. * });
  1102. *
  1103. * // Example invocation using the .withRetry() method
  1104. * const res = await apiCallWithRetry
  1105. * .invoke("Request 1")
  1106. * .catch((error) => {
  1107. * console.error("Failed after multiple retries:", error.message);
  1108. * });
  1109. *
  1110. * // Example invocation using the manual retry instance
  1111. * const res2 = await manualRetry
  1112. * .invoke("Request 2")
  1113. * .catch((error) => {
  1114. * console.error("Failed after multiple retries:", error.message);
  1115. * });
  1116. * ```
  1117. */
  1118. class RunnableRetry extends RunnableBinding {
  1119. static lc_name() {
  1120. return "RunnableRetry";
  1121. }
  1122. constructor(fields) {
  1123. super(fields);
  1124. Object.defineProperty(this, "lc_namespace", {
  1125. enumerable: true,
  1126. configurable: true,
  1127. writable: true,
  1128. value: ["langchain_core", "runnables"]
  1129. });
  1130. Object.defineProperty(this, "maxAttemptNumber", {
  1131. enumerable: true,
  1132. configurable: true,
  1133. writable: true,
  1134. value: 3
  1135. });
  1136. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1137. Object.defineProperty(this, "onFailedAttempt", {
  1138. enumerable: true,
  1139. configurable: true,
  1140. writable: true,
  1141. value: () => { }
  1142. });
  1143. this.maxAttemptNumber = fields.maxAttemptNumber ?? this.maxAttemptNumber;
  1144. this.onFailedAttempt = fields.onFailedAttempt ?? this.onFailedAttempt;
  1145. }
  1146. _patchConfigForRetry(attempt, config, runManager) {
  1147. const tag = attempt > 1 ? `retry:attempt:${attempt}` : undefined;
  1148. return (0, config_js_1.patchConfig)(config, { callbacks: runManager?.getChild(tag) });
  1149. }
  1150. async _invoke(input, config, runManager) {
  1151. return (0, p_retry_1.default)((attemptNumber) => super.invoke(input, this._patchConfigForRetry(attemptNumber, config, runManager)), {
  1152. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1153. onFailedAttempt: (error) => this.onFailedAttempt(error, input),
  1154. retries: Math.max(this.maxAttemptNumber - 1, 0),
  1155. randomize: true,
  1156. });
  1157. }
  1158. /**
  1159. * Method that invokes the runnable with the specified input, run manager,
  1160. * and config. It handles the retry logic by catching any errors and
  1161. * recursively invoking itself with the updated config for the next retry
  1162. * attempt.
  1163. * @param input The input for the runnable.
  1164. * @param runManager The run manager for the runnable.
  1165. * @param config The config for the runnable.
  1166. * @returns A promise that resolves to the output of the runnable.
  1167. */
  1168. async invoke(input, config) {
  1169. return this._callWithConfig(this._invoke.bind(this), input, config);
  1170. }
  1171. async _batch(inputs, configs, runManagers, batchOptions) {
  1172. const resultsMap = {};
  1173. try {
  1174. await (0, p_retry_1.default)(async (attemptNumber) => {
  1175. const remainingIndexes = inputs
  1176. .map((_, i) => i)
  1177. .filter((i) => resultsMap[i.toString()] === undefined ||
  1178. // eslint-disable-next-line no-instanceof/no-instanceof
  1179. resultsMap[i.toString()] instanceof Error);
  1180. const remainingInputs = remainingIndexes.map((i) => inputs[i]);
  1181. const patchedConfigs = remainingIndexes.map((i) => this._patchConfigForRetry(attemptNumber, configs?.[i], runManagers?.[i]));
  1182. const results = await super.batch(remainingInputs, patchedConfigs, {
  1183. ...batchOptions,
  1184. returnExceptions: true,
  1185. });
  1186. let firstException;
  1187. for (let i = 0; i < results.length; i += 1) {
  1188. const result = results[i];
  1189. const resultMapIndex = remainingIndexes[i];
  1190. // eslint-disable-next-line no-instanceof/no-instanceof
  1191. if (result instanceof Error) {
  1192. if (firstException === undefined) {
  1193. firstException = result;
  1194. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1195. firstException.input = remainingInputs[i];
  1196. }
  1197. }
  1198. resultsMap[resultMapIndex.toString()] = result;
  1199. }
  1200. if (firstException) {
  1201. throw firstException;
  1202. }
  1203. return results;
  1204. }, {
  1205. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1206. onFailedAttempt: (error) => this.onFailedAttempt(error, error.input),
  1207. retries: Math.max(this.maxAttemptNumber - 1, 0),
  1208. randomize: true,
  1209. });
  1210. }
  1211. catch (e) {
  1212. if (batchOptions?.returnExceptions !== true) {
  1213. throw e;
  1214. }
  1215. }
  1216. return Object.keys(resultsMap)
  1217. .sort((a, b) => parseInt(a, 10) - parseInt(b, 10))
  1218. .map((key) => resultsMap[parseInt(key, 10)]);
  1219. }
  1220. async batch(inputs, options, batchOptions) {
  1221. return this._batchWithConfig(this._batch.bind(this), inputs, options, batchOptions);
  1222. }
  1223. }
  1224. exports.RunnableRetry = RunnableRetry;
  1225. /**
  1226. * A sequence of runnables, where the output of each is the input of the next.
  1227. * @example
  1228. * ```typescript
  1229. * const promptTemplate = PromptTemplate.fromTemplate(
  1230. * "Tell me a joke about {topic}",
  1231. * );
  1232. * const chain = RunnableSequence.from([promptTemplate, new ChatOpenAI({})]);
  1233. * const result = await chain.invoke({ topic: "bears" });
  1234. * ```
  1235. */
  1236. class RunnableSequence extends Runnable {
  1237. static lc_name() {
  1238. return "RunnableSequence";
  1239. }
  1240. constructor(fields) {
  1241. super(fields);
  1242. Object.defineProperty(this, "first", {
  1243. enumerable: true,
  1244. configurable: true,
  1245. writable: true,
  1246. value: void 0
  1247. });
  1248. Object.defineProperty(this, "middle", {
  1249. enumerable: true,
  1250. configurable: true,
  1251. writable: true,
  1252. value: []
  1253. });
  1254. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1255. Object.defineProperty(this, "last", {
  1256. enumerable: true,
  1257. configurable: true,
  1258. writable: true,
  1259. value: void 0
  1260. });
  1261. Object.defineProperty(this, "omitSequenceTags", {
  1262. enumerable: true,
  1263. configurable: true,
  1264. writable: true,
  1265. value: false
  1266. });
  1267. Object.defineProperty(this, "lc_serializable", {
  1268. enumerable: true,
  1269. configurable: true,
  1270. writable: true,
  1271. value: true
  1272. });
  1273. Object.defineProperty(this, "lc_namespace", {
  1274. enumerable: true,
  1275. configurable: true,
  1276. writable: true,
  1277. value: ["langchain_core", "runnables"]
  1278. });
  1279. this.first = fields.first;
  1280. this.middle = fields.middle ?? this.middle;
  1281. this.last = fields.last;
  1282. this.name = fields.name;
  1283. this.omitSequenceTags = fields.omitSequenceTags ?? this.omitSequenceTags;
  1284. }
  1285. get steps() {
  1286. return [this.first, ...this.middle, this.last];
  1287. }
  1288. async invoke(input, options) {
  1289. const config = (0, config_js_1.ensureConfig)(options);
  1290. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
  1291. const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), config.runId, undefined, undefined, undefined, config?.runName);
  1292. delete config.runId;
  1293. let nextStepInput = input;
  1294. let finalOutput;
  1295. try {
  1296. const initialSteps = [this.first, ...this.middle];
  1297. for (let i = 0; i < initialSteps.length; i += 1) {
  1298. const step = initialSteps[i];
  1299. const promise = step.invoke(nextStepInput, (0, config_js_1.patchConfig)(config, {
  1300. callbacks: runManager?.getChild(this.omitSequenceTags ? undefined : `seq:step:${i + 1}`),
  1301. }));
  1302. nextStepInput = await (0, signal_js_1.raceWithSignal)(promise, options?.signal);
  1303. }
  1304. // TypeScript can't detect that the last output of the sequence returns RunOutput, so call it out of the loop here
  1305. if (options?.signal?.aborted) {
  1306. throw new Error("Aborted");
  1307. }
  1308. finalOutput = await this.last.invoke(nextStepInput, (0, config_js_1.patchConfig)(config, {
  1309. callbacks: runManager?.getChild(this.omitSequenceTags ? undefined : `seq:step:${this.steps.length}`),
  1310. }));
  1311. }
  1312. catch (e) {
  1313. await runManager?.handleChainError(e);
  1314. throw e;
  1315. }
  1316. await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
  1317. return finalOutput;
  1318. }
  1319. async batch(inputs, options, batchOptions) {
  1320. const configList = this._getOptionsList(options ?? {}, inputs.length);
  1321. const callbackManagers = await Promise.all(configList.map(config_js_1.getCallbackManagerForConfig));
  1322. const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
  1323. const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), configList[i].runId, undefined, undefined, undefined, configList[i].runName);
  1324. delete configList[i].runId;
  1325. return handleStartRes;
  1326. }));
  1327. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1328. let nextStepInputs = inputs;
  1329. try {
  1330. for (let i = 0; i < this.steps.length; i += 1) {
  1331. const step = this.steps[i];
  1332. const promise = step.batch(nextStepInputs, runManagers.map((runManager, j) => {
  1333. const childRunManager = runManager?.getChild(this.omitSequenceTags ? undefined : `seq:step:${i + 1}`);
  1334. return (0, config_js_1.patchConfig)(configList[j], { callbacks: childRunManager });
  1335. }), batchOptions);
  1336. nextStepInputs = await (0, signal_js_1.raceWithSignal)(promise, configList[0]?.signal);
  1337. }
  1338. }
  1339. catch (e) {
  1340. await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(e)));
  1341. throw e;
  1342. }
  1343. await Promise.all(runManagers.map((runManager) => runManager?.handleChainEnd(_coerceToDict(nextStepInputs, "output"))));
  1344. return nextStepInputs;
  1345. }
  1346. async *_streamIterator(input, options) {
  1347. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(options);
  1348. const { runId, ...otherOptions } = options ?? {};
  1349. const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherOptions?.runName);
  1350. const steps = [this.first, ...this.middle, this.last];
  1351. let concatSupported = true;
  1352. let finalOutput;
  1353. async function* inputGenerator() {
  1354. yield input;
  1355. }
  1356. try {
  1357. let finalGenerator = steps[0].transform(inputGenerator(), (0, config_js_1.patchConfig)(otherOptions, {
  1358. callbacks: runManager?.getChild(this.omitSequenceTags ? undefined : `seq:step:1`),
  1359. }));
  1360. for (let i = 1; i < steps.length; i += 1) {
  1361. const step = steps[i];
  1362. finalGenerator = await step.transform(finalGenerator, (0, config_js_1.patchConfig)(otherOptions, {
  1363. callbacks: runManager?.getChild(this.omitSequenceTags ? undefined : `seq:step:${i + 1}`),
  1364. }));
  1365. }
  1366. for await (const chunk of finalGenerator) {
  1367. options?.signal?.throwIfAborted();
  1368. yield chunk;
  1369. if (concatSupported) {
  1370. if (finalOutput === undefined) {
  1371. finalOutput = chunk;
  1372. }
  1373. else {
  1374. try {
  1375. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1376. finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
  1377. }
  1378. catch (e) {
  1379. finalOutput = undefined;
  1380. concatSupported = false;
  1381. }
  1382. }
  1383. }
  1384. }
  1385. }
  1386. catch (e) {
  1387. await runManager?.handleChainError(e);
  1388. throw e;
  1389. }
  1390. await runManager?.handleChainEnd(_coerceToDict(finalOutput, "output"));
  1391. }
  1392. getGraph(config) {
  1393. const graph = new graph_js_1.Graph();
  1394. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1395. let currentLastNode = null;
  1396. this.steps.forEach((step, index) => {
  1397. const stepGraph = step.getGraph(config);
  1398. if (index !== 0) {
  1399. stepGraph.trimFirstNode();
  1400. }
  1401. if (index !== this.steps.length - 1) {
  1402. stepGraph.trimLastNode();
  1403. }
  1404. graph.extend(stepGraph);
  1405. const stepFirstNode = stepGraph.firstNode();
  1406. if (!stepFirstNode) {
  1407. throw new Error(`Runnable ${step} has no first node`);
  1408. }
  1409. if (currentLastNode) {
  1410. graph.addEdge(currentLastNode, stepFirstNode);
  1411. }
  1412. currentLastNode = stepGraph.lastNode();
  1413. });
  1414. return graph;
  1415. }
  1416. pipe(coerceable) {
  1417. if (RunnableSequence.isRunnableSequence(coerceable)) {
  1418. return new RunnableSequence({
  1419. first: this.first,
  1420. middle: this.middle.concat([
  1421. this.last,
  1422. coerceable.first,
  1423. ...coerceable.middle,
  1424. ]),
  1425. last: coerceable.last,
  1426. name: this.name ?? coerceable.name,
  1427. });
  1428. }
  1429. else {
  1430. return new RunnableSequence({
  1431. first: this.first,
  1432. middle: [...this.middle, this.last],
  1433. last: _coerceToRunnable(coerceable),
  1434. name: this.name,
  1435. });
  1436. }
  1437. }
  1438. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1439. static isRunnableSequence(thing) {
  1440. return Array.isArray(thing.middle) && Runnable.isRunnable(thing);
  1441. }
  1442. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1443. static from([first, ...runnables], nameOrFields) {
  1444. let extra = {};
  1445. if (typeof nameOrFields === "string") {
  1446. extra.name = nameOrFields;
  1447. }
  1448. else if (nameOrFields !== undefined) {
  1449. extra = nameOrFields;
  1450. }
  1451. return new RunnableSequence({
  1452. ...extra,
  1453. first: _coerceToRunnable(first),
  1454. middle: runnables.slice(0, -1).map(_coerceToRunnable),
  1455. last: _coerceToRunnable(runnables[runnables.length - 1]),
  1456. });
  1457. }
  1458. }
  1459. exports.RunnableSequence = RunnableSequence;
  1460. /**
  1461. * A runnable that runs a mapping of runnables in parallel,
  1462. * and returns a mapping of their outputs.
  1463. * @example
  1464. * ```typescript
  1465. * const mapChain = RunnableMap.from({
  1466. * joke: PromptTemplate.fromTemplate("Tell me a joke about {topic}").pipe(
  1467. * new ChatAnthropic({}),
  1468. * ),
  1469. * poem: PromptTemplate.fromTemplate("write a 2-line poem about {topic}").pipe(
  1470. * new ChatAnthropic({}),
  1471. * ),
  1472. * });
  1473. * const result = await mapChain.invoke({ topic: "bear" });
  1474. * ```
  1475. */
  1476. class RunnableMap extends Runnable {
  1477. static lc_name() {
  1478. return "RunnableMap";
  1479. }
  1480. getStepsKeys() {
  1481. return Object.keys(this.steps);
  1482. }
  1483. constructor(fields) {
  1484. super(fields);
  1485. Object.defineProperty(this, "lc_namespace", {
  1486. enumerable: true,
  1487. configurable: true,
  1488. writable: true,
  1489. value: ["langchain_core", "runnables"]
  1490. });
  1491. Object.defineProperty(this, "lc_serializable", {
  1492. enumerable: true,
  1493. configurable: true,
  1494. writable: true,
  1495. value: true
  1496. });
  1497. Object.defineProperty(this, "steps", {
  1498. enumerable: true,
  1499. configurable: true,
  1500. writable: true,
  1501. value: void 0
  1502. });
  1503. this.steps = {};
  1504. for (const [key, value] of Object.entries(fields.steps)) {
  1505. this.steps[key] = _coerceToRunnable(value);
  1506. }
  1507. }
  1508. static from(steps) {
  1509. return new RunnableMap({ steps });
  1510. }
  1511. async invoke(input, options) {
  1512. const config = (0, config_js_1.ensureConfig)(options);
  1513. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
  1514. const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
  1515. input,
  1516. }, config.runId, undefined, undefined, undefined, config?.runName);
  1517. delete config.runId;
  1518. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1519. const output = {};
  1520. try {
  1521. const promises = Object.entries(this.steps).map(async ([key, runnable]) => {
  1522. output[key] = await runnable.invoke(input, (0, config_js_1.patchConfig)(config, {
  1523. callbacks: runManager?.getChild(`map:key:${key}`),
  1524. }));
  1525. });
  1526. await (0, signal_js_1.raceWithSignal)(Promise.all(promises), options?.signal);
  1527. }
  1528. catch (e) {
  1529. await runManager?.handleChainError(e);
  1530. throw e;
  1531. }
  1532. await runManager?.handleChainEnd(output);
  1533. return output;
  1534. }
  1535. async *_transform(generator, runManager, options) {
  1536. // shallow copy steps to ignore changes while iterating
  1537. const steps = { ...this.steps };
  1538. // each step gets a copy of the input iterator
  1539. const inputCopies = (0, stream_js_1.atee)(generator, Object.keys(steps).length);
  1540. // start the first iteration of each output iterator
  1541. const tasks = new Map(Object.entries(steps).map(([key, runnable], i) => {
  1542. const gen = runnable.transform(inputCopies[i], (0, config_js_1.patchConfig)(options, {
  1543. callbacks: runManager?.getChild(`map:key:${key}`),
  1544. }));
  1545. return [key, gen.next().then((result) => ({ key, gen, result }))];
  1546. }));
  1547. // yield chunks as they become available,
  1548. // starting new iterations as needed,
  1549. // until all iterators are done
  1550. while (tasks.size) {
  1551. const promise = Promise.race(tasks.values());
  1552. const { key, result, gen } = await (0, signal_js_1.raceWithSignal)(promise, options?.signal);
  1553. tasks.delete(key);
  1554. if (!result.done) {
  1555. yield { [key]: result.value };
  1556. tasks.set(key, gen.next().then((result) => ({ key, gen, result })));
  1557. }
  1558. }
  1559. }
  1560. transform(generator, options) {
  1561. return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
  1562. }
  1563. async stream(input, options) {
  1564. async function* generator() {
  1565. yield input;
  1566. }
  1567. const config = (0, config_js_1.ensureConfig)(options);
  1568. const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
  1569. generator: this.transform(generator(), config),
  1570. config,
  1571. });
  1572. await wrappedGenerator.setup;
  1573. return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
  1574. }
  1575. }
  1576. exports.RunnableMap = RunnableMap;
  1577. /**
  1578. * A runnable that wraps a traced LangSmith function.
  1579. */
  1580. class RunnableTraceable extends Runnable {
  1581. constructor(fields) {
  1582. super(fields);
  1583. Object.defineProperty(this, "lc_serializable", {
  1584. enumerable: true,
  1585. configurable: true,
  1586. writable: true,
  1587. value: false
  1588. });
  1589. Object.defineProperty(this, "lc_namespace", {
  1590. enumerable: true,
  1591. configurable: true,
  1592. writable: true,
  1593. value: ["langchain_core", "runnables"]
  1594. });
  1595. Object.defineProperty(this, "func", {
  1596. enumerable: true,
  1597. configurable: true,
  1598. writable: true,
  1599. value: void 0
  1600. });
  1601. if (!(0, traceable_1.isTraceableFunction)(fields.func)) {
  1602. throw new Error("RunnableTraceable requires a function that is wrapped in traceable higher-order function");
  1603. }
  1604. this.func = fields.func;
  1605. }
  1606. async invoke(input, options) {
  1607. const [config] = this._getOptionsList(options ?? {}, 1);
  1608. const callbacks = await (0, config_js_1.getCallbackManagerForConfig)(config);
  1609. const promise = this.func((0, config_js_1.patchConfig)(config, { callbacks }), input);
  1610. return (0, signal_js_1.raceWithSignal)(promise, config?.signal);
  1611. }
  1612. async *_streamIterator(input, options) {
  1613. const [config] = this._getOptionsList(options ?? {}, 1);
  1614. const result = await this.invoke(input, options);
  1615. if ((0, iter_js_1.isAsyncIterable)(result)) {
  1616. for await (const item of result) {
  1617. config?.signal?.throwIfAborted();
  1618. yield item;
  1619. }
  1620. return;
  1621. }
  1622. if ((0, iter_js_1.isIterator)(result)) {
  1623. while (true) {
  1624. config?.signal?.throwIfAborted();
  1625. const state = result.next();
  1626. if (state.done)
  1627. break;
  1628. yield state.value;
  1629. }
  1630. return;
  1631. }
  1632. yield result;
  1633. }
  1634. static from(func) {
  1635. return new RunnableTraceable({ func });
  1636. }
  1637. }
  1638. exports.RunnableTraceable = RunnableTraceable;
  1639. function assertNonTraceableFunction(func) {
  1640. if ((0, traceable_1.isTraceableFunction)(func)) {
  1641. throw new Error("RunnableLambda requires a function that is not wrapped in traceable higher-order function. This shouldn't happen.");
  1642. }
  1643. }
  1644. /**
  1645. * A runnable that wraps an arbitrary function that takes a single argument.
  1646. * @example
  1647. * ```typescript
  1648. * import { RunnableLambda } from "@langchain/core/runnables";
  1649. *
  1650. * const add = (input: { x: number; y: number }) => input.x + input.y;
  1651. *
  1652. * const multiply = (input: { value: number; multiplier: number }) =>
  1653. * input.value * input.multiplier;
  1654. *
  1655. * // Create runnables for the functions
  1656. * const addLambda = RunnableLambda.from(add);
  1657. * const multiplyLambda = RunnableLambda.from(multiply);
  1658. *
  1659. * // Chain the lambdas for a mathematical operation
  1660. * const chainedLambda = addLambda.pipe((result) =>
  1661. * multiplyLambda.invoke({ value: result, multiplier: 2 })
  1662. * );
  1663. *
  1664. * // Example invocation of the chainedLambda
  1665. * const result = await chainedLambda.invoke({ x: 2, y: 3 });
  1666. *
  1667. * // Will log "10" (since (2 + 3) * 2 = 10)
  1668. * ```
  1669. */
  1670. class RunnableLambda extends Runnable {
  1671. static lc_name() {
  1672. return "RunnableLambda";
  1673. }
  1674. constructor(fields) {
  1675. if ((0, traceable_1.isTraceableFunction)(fields.func)) {
  1676. // eslint-disable-next-line no-constructor-return
  1677. return RunnableTraceable.from(fields.func);
  1678. }
  1679. super(fields);
  1680. Object.defineProperty(this, "lc_namespace", {
  1681. enumerable: true,
  1682. configurable: true,
  1683. writable: true,
  1684. value: ["langchain_core", "runnables"]
  1685. });
  1686. Object.defineProperty(this, "func", {
  1687. enumerable: true,
  1688. configurable: true,
  1689. writable: true,
  1690. value: void 0
  1691. });
  1692. assertNonTraceableFunction(fields.func);
  1693. this.func = fields.func;
  1694. }
  1695. static from(func) {
  1696. return new RunnableLambda({
  1697. func,
  1698. });
  1699. }
  1700. async _invoke(input, config, runManager) {
  1701. return new Promise((resolve, reject) => {
  1702. const childConfig = (0, config_js_1.patchConfig)(config, {
  1703. callbacks: runManager?.getChild(),
  1704. recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
  1705. });
  1706. void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(childConfig), async () => {
  1707. try {
  1708. let output = await this.func(input, {
  1709. ...childConfig,
  1710. });
  1711. if (output && Runnable.isRunnable(output)) {
  1712. if (config?.recursionLimit === 0) {
  1713. throw new Error("Recursion limit reached.");
  1714. }
  1715. output = await output.invoke(input, {
  1716. ...childConfig,
  1717. recursionLimit: (childConfig.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
  1718. });
  1719. }
  1720. else if ((0, iter_js_1.isAsyncIterable)(output)) {
  1721. let finalOutput;
  1722. for await (const chunk of (0, iter_js_1.consumeAsyncIterableInContext)(childConfig, output)) {
  1723. config?.signal?.throwIfAborted();
  1724. if (finalOutput === undefined) {
  1725. finalOutput = chunk;
  1726. }
  1727. else {
  1728. // Make a best effort to gather, for any type that supports concat.
  1729. try {
  1730. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1731. finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
  1732. }
  1733. catch (e) {
  1734. finalOutput = chunk;
  1735. }
  1736. }
  1737. }
  1738. output = finalOutput;
  1739. }
  1740. else if ((0, iter_js_1.isIterableIterator)(output)) {
  1741. let finalOutput;
  1742. for (const chunk of (0, iter_js_1.consumeIteratorInContext)(childConfig, output)) {
  1743. config?.signal?.throwIfAborted();
  1744. if (finalOutput === undefined) {
  1745. finalOutput = chunk;
  1746. }
  1747. else {
  1748. // Make a best effort to gather, for any type that supports concat.
  1749. try {
  1750. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1751. finalOutput = (0, stream_js_1.concat)(finalOutput, chunk);
  1752. }
  1753. catch (e) {
  1754. finalOutput = chunk;
  1755. }
  1756. }
  1757. }
  1758. output = finalOutput;
  1759. }
  1760. resolve(output);
  1761. }
  1762. catch (e) {
  1763. reject(e);
  1764. }
  1765. });
  1766. });
  1767. }
  1768. async invoke(input, options) {
  1769. return this._callWithConfig(this._invoke.bind(this), input, options);
  1770. }
  1771. async *_transform(generator, runManager, config) {
  1772. let finalChunk;
  1773. for await (const chunk of generator) {
  1774. if (finalChunk === undefined) {
  1775. finalChunk = chunk;
  1776. }
  1777. else {
  1778. // Make a best effort to gather, for any type that supports concat.
  1779. try {
  1780. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  1781. finalChunk = (0, stream_js_1.concat)(finalChunk, chunk);
  1782. }
  1783. catch (e) {
  1784. finalChunk = chunk;
  1785. }
  1786. }
  1787. }
  1788. const childConfig = (0, config_js_1.patchConfig)(config, {
  1789. callbacks: runManager?.getChild(),
  1790. recursionLimit: (config?.recursionLimit ?? config_js_1.DEFAULT_RECURSION_LIMIT) - 1,
  1791. });
  1792. const output = await new Promise((resolve, reject) => {
  1793. void index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig((0, config_js_1.pickRunnableConfigKeys)(childConfig), async () => {
  1794. try {
  1795. const res = await this.func(finalChunk, {
  1796. ...childConfig,
  1797. config: childConfig,
  1798. });
  1799. resolve(res);
  1800. }
  1801. catch (e) {
  1802. reject(e);
  1803. }
  1804. });
  1805. });
  1806. if (output && Runnable.isRunnable(output)) {
  1807. if (config?.recursionLimit === 0) {
  1808. throw new Error("Recursion limit reached.");
  1809. }
  1810. const stream = await output.stream(finalChunk, childConfig);
  1811. for await (const chunk of stream) {
  1812. yield chunk;
  1813. }
  1814. }
  1815. else if ((0, iter_js_1.isAsyncIterable)(output)) {
  1816. for await (const chunk of (0, iter_js_1.consumeAsyncIterableInContext)(childConfig, output)) {
  1817. config?.signal?.throwIfAborted();
  1818. yield chunk;
  1819. }
  1820. }
  1821. else if ((0, iter_js_1.isIterableIterator)(output)) {
  1822. for (const chunk of (0, iter_js_1.consumeIteratorInContext)(childConfig, output)) {
  1823. config?.signal?.throwIfAborted();
  1824. yield chunk;
  1825. }
  1826. }
  1827. else {
  1828. yield output;
  1829. }
  1830. }
  1831. transform(generator, options) {
  1832. return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
  1833. }
  1834. async stream(input, options) {
  1835. async function* generator() {
  1836. yield input;
  1837. }
  1838. const config = (0, config_js_1.ensureConfig)(options);
  1839. const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
  1840. generator: this.transform(generator(), config),
  1841. config,
  1842. });
  1843. await wrappedGenerator.setup;
  1844. return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
  1845. }
  1846. }
  1847. exports.RunnableLambda = RunnableLambda;
  1848. /**
  1849. * A runnable that runs a mapping of runnables in parallel,
  1850. * and returns a mapping of their outputs.
  1851. * @example
  1852. * ```typescript
  1853. * import {
  1854. * RunnableLambda,
  1855. * RunnableParallel,
  1856. * } from "@langchain/core/runnables";
  1857. *
  1858. * const addYears = (age: number): number => age + 5;
  1859. * const yearsToFifty = (age: number): number => 50 - age;
  1860. * const yearsToHundred = (age: number): number => 100 - age;
  1861. *
  1862. * const addYearsLambda = RunnableLambda.from(addYears);
  1863. * const milestoneFiftyLambda = RunnableLambda.from(yearsToFifty);
  1864. * const milestoneHundredLambda = RunnableLambda.from(yearsToHundred);
  1865. *
  1866. * // Pipe will coerce objects into RunnableParallel by default, but we
  1867. * // explicitly instantiate one here to demonstrate
  1868. * const sequence = addYearsLambda.pipe(
  1869. * RunnableParallel.from({
  1870. * years_to_fifty: milestoneFiftyLambda,
  1871. * years_to_hundred: milestoneHundredLambda,
  1872. * })
  1873. * );
  1874. *
  1875. * // Invoke the sequence with a single age input
  1876. * const res = sequence.invoke(25);
  1877. *
  1878. * // { years_to_fifty: 25, years_to_hundred: 75 }
  1879. * ```
  1880. */
  1881. class RunnableParallel extends RunnableMap {
  1882. }
  1883. exports.RunnableParallel = RunnableParallel;
  1884. /**
  1885. * A Runnable that can fallback to other Runnables if it fails.
  1886. * External APIs (e.g., APIs for a language model) may at times experience
  1887. * degraded performance or even downtime.
  1888. *
  1889. * In these cases, it can be useful to have a fallback Runnable that can be
  1890. * used in place of the original Runnable (e.g., fallback to another LLM provider).
  1891. *
  1892. * Fallbacks can be defined at the level of a single Runnable, or at the level
  1893. * of a chain of Runnables. Fallbacks are tried in order until one succeeds or
  1894. * all fail.
  1895. *
  1896. * While you can instantiate a `RunnableWithFallbacks` directly, it is usually
  1897. * more convenient to use the `withFallbacks` method on an existing Runnable.
  1898. *
  1899. * When streaming, fallbacks will only be called on failures during the initial
  1900. * stream creation. Errors that occur after a stream starts will not fallback
  1901. * to the next Runnable.
  1902. *
  1903. * @example
  1904. * ```typescript
  1905. * import {
  1906. * RunnableLambda,
  1907. * RunnableWithFallbacks,
  1908. * } from "@langchain/core/runnables";
  1909. *
  1910. * const primaryOperation = (input: string): string => {
  1911. * if (input !== "safe") {
  1912. * throw new Error("Primary operation failed due to unsafe input");
  1913. * }
  1914. * return `Processed: ${input}`;
  1915. * };
  1916. *
  1917. * // Define a fallback operation that processes the input differently
  1918. * const fallbackOperation = (input: string): string =>
  1919. * `Fallback processed: ${input}`;
  1920. *
  1921. * const primaryRunnable = RunnableLambda.from(primaryOperation);
  1922. * const fallbackRunnable = RunnableLambda.from(fallbackOperation);
  1923. *
  1924. * // Apply the fallback logic using the .withFallbacks() method
  1925. * const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
  1926. *
  1927. * // Alternatively, create a RunnableWithFallbacks instance manually
  1928. * const manualFallbackChain = new RunnableWithFallbacks({
  1929. * runnable: primaryRunnable,
  1930. * fallbacks: [fallbackRunnable],
  1931. * });
  1932. *
  1933. * // Example invocation using .withFallbacks()
  1934. * const res = await runnableWithFallback
  1935. * .invoke("unsafe input")
  1936. * .catch((error) => {
  1937. * console.error("Failed after all attempts:", error.message);
  1938. * });
  1939. *
  1940. * // "Fallback processed: unsafe input"
  1941. *
  1942. * // Example invocation using manual instantiation
  1943. * const res = await manualFallbackChain
  1944. * .invoke("safe")
  1945. * .catch((error) => {
  1946. * console.error("Failed after all attempts:", error.message);
  1947. * });
  1948. *
  1949. * // "Processed: safe"
  1950. * ```
  1951. */
  1952. class RunnableWithFallbacks extends Runnable {
  1953. static lc_name() {
  1954. return "RunnableWithFallbacks";
  1955. }
  1956. constructor(fields) {
  1957. super(fields);
  1958. Object.defineProperty(this, "lc_namespace", {
  1959. enumerable: true,
  1960. configurable: true,
  1961. writable: true,
  1962. value: ["langchain_core", "runnables"]
  1963. });
  1964. Object.defineProperty(this, "lc_serializable", {
  1965. enumerable: true,
  1966. configurable: true,
  1967. writable: true,
  1968. value: true
  1969. });
  1970. Object.defineProperty(this, "runnable", {
  1971. enumerable: true,
  1972. configurable: true,
  1973. writable: true,
  1974. value: void 0
  1975. });
  1976. Object.defineProperty(this, "fallbacks", {
  1977. enumerable: true,
  1978. configurable: true,
  1979. writable: true,
  1980. value: void 0
  1981. });
  1982. this.runnable = fields.runnable;
  1983. this.fallbacks = fields.fallbacks;
  1984. }
  1985. *runnables() {
  1986. yield this.runnable;
  1987. for (const fallback of this.fallbacks) {
  1988. yield fallback;
  1989. }
  1990. }
  1991. async invoke(input, options) {
  1992. const config = (0, config_js_1.ensureConfig)(options);
  1993. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
  1994. const { runId, ...otherConfigFields } = config;
  1995. const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
  1996. const childConfig = (0, config_js_1.patchConfig)(otherConfigFields, {
  1997. callbacks: runManager?.getChild(),
  1998. });
  1999. const res = await index_js_1.AsyncLocalStorageProviderSingleton.runWithConfig(childConfig, async () => {
  2000. let firstError;
  2001. for (const runnable of this.runnables()) {
  2002. config?.signal?.throwIfAborted();
  2003. try {
  2004. const output = await runnable.invoke(input, childConfig);
  2005. await runManager?.handleChainEnd(_coerceToDict(output, "output"));
  2006. return output;
  2007. }
  2008. catch (e) {
  2009. if (firstError === undefined) {
  2010. firstError = e;
  2011. }
  2012. }
  2013. }
  2014. if (firstError === undefined) {
  2015. throw new Error("No error stored at end of fallback.");
  2016. }
  2017. await runManager?.handleChainError(firstError);
  2018. throw firstError;
  2019. });
  2020. return res;
  2021. }
  2022. async *_streamIterator(input, options) {
  2023. const config = (0, config_js_1.ensureConfig)(options);
  2024. const callbackManager_ = await (0, config_js_1.getCallbackManagerForConfig)(config);
  2025. const { runId, ...otherConfigFields } = config;
  2026. const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), runId, undefined, undefined, undefined, otherConfigFields?.runName);
  2027. let firstError;
  2028. let stream;
  2029. for (const runnable of this.runnables()) {
  2030. config?.signal?.throwIfAborted();
  2031. const childConfig = (0, config_js_1.patchConfig)(otherConfigFields, {
  2032. callbacks: runManager?.getChild(),
  2033. });
  2034. try {
  2035. const originalStream = await runnable.stream(input, childConfig);
  2036. stream = (0, iter_js_1.consumeAsyncIterableInContext)(childConfig, originalStream);
  2037. break;
  2038. }
  2039. catch (e) {
  2040. if (firstError === undefined) {
  2041. firstError = e;
  2042. }
  2043. }
  2044. }
  2045. if (stream === undefined) {
  2046. const error = firstError ?? new Error("No error stored at end of fallback.");
  2047. await runManager?.handleChainError(error);
  2048. throw error;
  2049. }
  2050. let output;
  2051. try {
  2052. for await (const chunk of stream) {
  2053. yield chunk;
  2054. try {
  2055. output = output === undefined ? output : (0, stream_js_1.concat)(output, chunk);
  2056. }
  2057. catch (e) {
  2058. output = undefined;
  2059. }
  2060. }
  2061. }
  2062. catch (e) {
  2063. await runManager?.handleChainError(e);
  2064. throw e;
  2065. }
  2066. await runManager?.handleChainEnd(_coerceToDict(output, "output"));
  2067. }
  2068. async batch(inputs, options, batchOptions) {
  2069. if (batchOptions?.returnExceptions) {
  2070. throw new Error("Not implemented.");
  2071. }
  2072. const configList = this._getOptionsList(options ?? {}, inputs.length);
  2073. const callbackManagers = await Promise.all(configList.map((config) => (0, config_js_1.getCallbackManagerForConfig)(config)));
  2074. const runManagers = await Promise.all(callbackManagers.map(async (callbackManager, i) => {
  2075. const handleStartRes = await callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), configList[i].runId, undefined, undefined, undefined, configList[i].runName);
  2076. delete configList[i].runId;
  2077. return handleStartRes;
  2078. }));
  2079. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  2080. let firstError;
  2081. for (const runnable of this.runnables()) {
  2082. configList[0].signal?.throwIfAborted();
  2083. try {
  2084. const outputs = await runnable.batch(inputs, runManagers.map((runManager, j) => (0, config_js_1.patchConfig)(configList[j], {
  2085. callbacks: runManager?.getChild(),
  2086. })), batchOptions);
  2087. await Promise.all(runManagers.map((runManager, i) => runManager?.handleChainEnd(_coerceToDict(outputs[i], "output"))));
  2088. return outputs;
  2089. }
  2090. catch (e) {
  2091. if (firstError === undefined) {
  2092. firstError = e;
  2093. }
  2094. }
  2095. }
  2096. if (!firstError) {
  2097. throw new Error("No error stored at end of fallbacks.");
  2098. }
  2099. await Promise.all(runManagers.map((runManager) => runManager?.handleChainError(firstError)));
  2100. throw firstError;
  2101. }
  2102. }
  2103. exports.RunnableWithFallbacks = RunnableWithFallbacks;
  2104. // TODO: Figure out why the compiler needs help eliminating Error as a RunOutput type
  2105. function _coerceToRunnable(coerceable) {
  2106. if (typeof coerceable === "function") {
  2107. return new RunnableLambda({ func: coerceable });
  2108. }
  2109. else if (Runnable.isRunnable(coerceable)) {
  2110. return coerceable;
  2111. }
  2112. else if (!Array.isArray(coerceable) && typeof coerceable === "object") {
  2113. const runnables = {};
  2114. for (const [key, value] of Object.entries(coerceable)) {
  2115. runnables[key] = _coerceToRunnable(value);
  2116. }
  2117. return new RunnableMap({
  2118. steps: runnables,
  2119. });
  2120. }
  2121. else {
  2122. throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
  2123. }
  2124. }
  2125. exports._coerceToRunnable = _coerceToRunnable;
  2126. /**
  2127. * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
  2128. * @example
  2129. * ```typescript
  2130. * import {
  2131. * RunnableAssign,
  2132. * RunnableLambda,
  2133. * RunnableParallel,
  2134. * } from "@langchain/core/runnables";
  2135. *
  2136. * const calculateAge = (x: { birthYear: number }): { age: number } => {
  2137. * const currentYear = new Date().getFullYear();
  2138. * return { age: currentYear - x.birthYear };
  2139. * };
  2140. *
  2141. * const createGreeting = (x: { name: string }): { greeting: string } => {
  2142. * return { greeting: `Hello, ${x.name}!` };
  2143. * };
  2144. *
  2145. * const mapper = RunnableParallel.from({
  2146. * age_step: RunnableLambda.from(calculateAge),
  2147. * greeting_step: RunnableLambda.from(createGreeting),
  2148. * });
  2149. *
  2150. * const runnableAssign = new RunnableAssign({ mapper });
  2151. *
  2152. * const res = await runnableAssign.invoke({ name: "Alice", birthYear: 1990 });
  2153. *
  2154. * // { name: "Alice", birthYear: 1990, age_step: { age: 34 }, greeting_step: { greeting: "Hello, Alice!" } }
  2155. * ```
  2156. */
  2157. class RunnableAssign extends Runnable {
  2158. static lc_name() {
  2159. return "RunnableAssign";
  2160. }
  2161. constructor(fields) {
  2162. // eslint-disable-next-line no-instanceof/no-instanceof
  2163. if (fields instanceof RunnableMap) {
  2164. // eslint-disable-next-line no-param-reassign
  2165. fields = { mapper: fields };
  2166. }
  2167. super(fields);
  2168. Object.defineProperty(this, "lc_namespace", {
  2169. enumerable: true,
  2170. configurable: true,
  2171. writable: true,
  2172. value: ["langchain_core", "runnables"]
  2173. });
  2174. Object.defineProperty(this, "lc_serializable", {
  2175. enumerable: true,
  2176. configurable: true,
  2177. writable: true,
  2178. value: true
  2179. });
  2180. Object.defineProperty(this, "mapper", {
  2181. enumerable: true,
  2182. configurable: true,
  2183. writable: true,
  2184. value: void 0
  2185. });
  2186. this.mapper = fields.mapper;
  2187. }
  2188. async invoke(input, options) {
  2189. const mapperResult = await this.mapper.invoke(input, options);
  2190. return {
  2191. ...input,
  2192. ...mapperResult,
  2193. };
  2194. }
  2195. async *_transform(generator, runManager, options) {
  2196. // collect mapper keys
  2197. const mapperKeys = this.mapper.getStepsKeys();
  2198. // create two input gens, one for the mapper, one for the input
  2199. const [forPassthrough, forMapper] = (0, stream_js_1.atee)(generator);
  2200. // create mapper output gen
  2201. const mapperOutput = this.mapper.transform(forMapper, (0, config_js_1.patchConfig)(options, { callbacks: runManager?.getChild() }));
  2202. // start the mapper
  2203. const firstMapperChunkPromise = mapperOutput.next();
  2204. // yield the passthrough
  2205. for await (const chunk of forPassthrough) {
  2206. if (typeof chunk !== "object" || Array.isArray(chunk)) {
  2207. throw new Error(`RunnableAssign can only be used with objects as input, got ${typeof chunk}`);
  2208. }
  2209. const filtered = Object.fromEntries(Object.entries(chunk).filter(([key]) => !mapperKeys.includes(key)));
  2210. if (Object.keys(filtered).length > 0) {
  2211. yield filtered;
  2212. }
  2213. }
  2214. // yield the mapper output
  2215. yield (await firstMapperChunkPromise).value;
  2216. for await (const chunk of mapperOutput) {
  2217. yield chunk;
  2218. }
  2219. }
  2220. transform(generator, options) {
  2221. return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
  2222. }
  2223. async stream(input, options) {
  2224. async function* generator() {
  2225. yield input;
  2226. }
  2227. const config = (0, config_js_1.ensureConfig)(options);
  2228. const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
  2229. generator: this.transform(generator(), config),
  2230. config,
  2231. });
  2232. await wrappedGenerator.setup;
  2233. return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
  2234. }
  2235. }
  2236. exports.RunnableAssign = RunnableAssign;
  2237. /**
  2238. * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
  2239. * Useful for streaming, can be automatically created and chained by calling `runnable.pick();`.
  2240. * @example
  2241. * ```typescript
  2242. * import { RunnablePick } from "@langchain/core/runnables";
  2243. *
  2244. * const inputData = {
  2245. * name: "John",
  2246. * age: 30,
  2247. * city: "New York",
  2248. * country: "USA",
  2249. * email: "john.doe@example.com",
  2250. * phone: "+1234567890",
  2251. * };
  2252. *
  2253. * const basicInfoRunnable = new RunnablePick(["name", "city"]);
  2254. *
  2255. * // Example invocation
  2256. * const res = await basicInfoRunnable.invoke(inputData);
  2257. *
  2258. * // { name: 'John', city: 'New York' }
  2259. * ```
  2260. */
  2261. class RunnablePick extends Runnable {
  2262. static lc_name() {
  2263. return "RunnablePick";
  2264. }
  2265. constructor(fields) {
  2266. if (typeof fields === "string" || Array.isArray(fields)) {
  2267. // eslint-disable-next-line no-param-reassign
  2268. fields = { keys: fields };
  2269. }
  2270. super(fields);
  2271. Object.defineProperty(this, "lc_namespace", {
  2272. enumerable: true,
  2273. configurable: true,
  2274. writable: true,
  2275. value: ["langchain_core", "runnables"]
  2276. });
  2277. Object.defineProperty(this, "lc_serializable", {
  2278. enumerable: true,
  2279. configurable: true,
  2280. writable: true,
  2281. value: true
  2282. });
  2283. Object.defineProperty(this, "keys", {
  2284. enumerable: true,
  2285. configurable: true,
  2286. writable: true,
  2287. value: void 0
  2288. });
  2289. this.keys = fields.keys;
  2290. }
  2291. async _pick(input) {
  2292. if (typeof this.keys === "string") {
  2293. return input[this.keys];
  2294. }
  2295. else {
  2296. const picked = this.keys
  2297. .map((key) => [key, input[key]])
  2298. .filter((v) => v[1] !== undefined);
  2299. return picked.length === 0 ? undefined : Object.fromEntries(picked);
  2300. }
  2301. }
  2302. async invoke(input, options) {
  2303. return this._callWithConfig(this._pick.bind(this), input, options);
  2304. }
  2305. async *_transform(generator) {
  2306. for await (const chunk of generator) {
  2307. const picked = await this._pick(chunk);
  2308. if (picked !== undefined) {
  2309. yield picked;
  2310. }
  2311. }
  2312. }
  2313. transform(generator, options) {
  2314. return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
  2315. }
  2316. async stream(input, options) {
  2317. async function* generator() {
  2318. yield input;
  2319. }
  2320. const config = (0, config_js_1.ensureConfig)(options);
  2321. const wrappedGenerator = new stream_js_1.AsyncGeneratorWithSetup({
  2322. generator: this.transform(generator(), config),
  2323. config,
  2324. });
  2325. await wrappedGenerator.setup;
  2326. return stream_js_1.IterableReadableStream.fromAsyncGenerator(wrappedGenerator);
  2327. }
  2328. }
  2329. exports.RunnablePick = RunnablePick;
  2330. class RunnableToolLike extends RunnableBinding {
  2331. constructor(fields) {
  2332. const sequence = RunnableSequence.from([
  2333. RunnableLambda.from(async (input) => {
  2334. let toolInput;
  2335. if ((0, utils_js_2._isToolCall)(input)) {
  2336. try {
  2337. toolInput = await this.schema.parseAsync(input.args);
  2338. }
  2339. catch (e) {
  2340. throw new utils_js_2.ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(input.args));
  2341. }
  2342. }
  2343. else {
  2344. toolInput = input;
  2345. }
  2346. return toolInput;
  2347. }).withConfig({ runName: `${fields.name}:parse_input` }),
  2348. fields.bound,
  2349. ]).withConfig({ runName: fields.name });
  2350. super({
  2351. bound: sequence,
  2352. config: fields.config ?? {},
  2353. });
  2354. Object.defineProperty(this, "name", {
  2355. enumerable: true,
  2356. configurable: true,
  2357. writable: true,
  2358. value: void 0
  2359. });
  2360. Object.defineProperty(this, "description", {
  2361. enumerable: true,
  2362. configurable: true,
  2363. writable: true,
  2364. value: void 0
  2365. });
  2366. Object.defineProperty(this, "schema", {
  2367. enumerable: true,
  2368. configurable: true,
  2369. writable: true,
  2370. value: void 0
  2371. });
  2372. this.name = fields.name;
  2373. this.description = fields.description;
  2374. this.schema = fields.schema;
  2375. }
  2376. static lc_name() {
  2377. return "RunnableToolLike";
  2378. }
  2379. }
  2380. exports.RunnableToolLike = RunnableToolLike;
  2381. /**
  2382. * Given a runnable and a Zod schema, convert the runnable to a tool.
  2383. *
  2384. * @template RunInput The input type for the runnable.
  2385. * @template RunOutput The output type for the runnable.
  2386. *
  2387. * @param {Runnable<RunInput, RunOutput>} runnable The runnable to convert to a tool.
  2388. * @param fields
  2389. * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
  2390. * @param {string | undefined} [fields.description] The description of the tool. Falls back to the description on the Zod schema if not provided, or undefined if neither are provided.
  2391. * @param {z.ZodType<RunInput>} [fields.schema] The Zod schema for the input of the tool. Infers the Zod type from the input type of the runnable.
  2392. * @returns {RunnableToolLike<z.ZodType<RunInput>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
  2393. */
  2394. function convertRunnableToTool(runnable, fields) {
  2395. const name = fields.name ?? runnable.getName();
  2396. const description = fields.description ?? fields.schema?.description;
  2397. if (fields.schema.constructor === zod_1.z.ZodString) {
  2398. return new RunnableToolLike({
  2399. name,
  2400. description,
  2401. schema: zod_1.z
  2402. .object({
  2403. input: zod_1.z.string(),
  2404. })
  2405. .transform((input) => input.input),
  2406. bound: runnable,
  2407. });
  2408. }
  2409. return new RunnableToolLike({
  2410. name,
  2411. description,
  2412. schema: fields.schema,
  2413. bound: runnable,
  2414. });
  2415. }
  2416. exports.convertRunnableToTool = convertRunnableToTool;