@langchain/core 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/dist/callbacks/manager.d.ts +0 -5
  3. package/dist/chat_history.d.ts +1 -0
  4. package/dist/language_models/base.d.ts +2 -0
  5. package/dist/output_parsers/index.cjs +1 -0
  6. package/dist/output_parsers/index.d.ts +1 -0
  7. package/dist/output_parsers/index.js +1 -0
  8. package/dist/output_parsers/json.cjs +137 -0
  9. package/dist/output_parsers/json.d.ts +17 -0
  10. package/dist/output_parsers/json.js +131 -0
  11. package/dist/output_parsers/list.cjs +121 -2
  12. package/dist/output_parsers/list.d.ts +21 -2
  13. package/dist/output_parsers/list.js +119 -2
  14. package/dist/prompt_values.cjs +3 -0
  15. package/dist/prompt_values.d.ts +1 -0
  16. package/dist/prompt_values.js +3 -0
  17. package/dist/prompts/chat.cjs +22 -2
  18. package/dist/prompts/chat.d.ts +4 -2
  19. package/dist/prompts/chat.js +22 -2
  20. package/dist/runnables/base.cjs +290 -31
  21. package/dist/runnables/base.d.ts +72 -20
  22. package/dist/runnables/base.js +289 -32
  23. package/dist/runnables/config.cjs +7 -3
  24. package/dist/runnables/config.d.ts +13 -2
  25. package/dist/runnables/config.js +5 -1
  26. package/dist/runnables/history.cjs +3 -3
  27. package/dist/runnables/history.d.ts +5 -6
  28. package/dist/runnables/history.js +3 -3
  29. package/dist/runnables/index.cjs +5 -2
  30. package/dist/runnables/index.d.ts +3 -3
  31. package/dist/runnables/index.js +3 -2
  32. package/dist/runnables/passthrough.cjs +5 -31
  33. package/dist/runnables/passthrough.d.ts +3 -11
  34. package/dist/runnables/passthrough.js +4 -29
  35. package/dist/utils/stream.cjs +113 -1
  36. package/dist/utils/stream.d.ts +13 -0
  37. package/dist/utils/stream.js +109 -0
  38. package/dist/utils/testing/index.cjs +14 -1
  39. package/dist/utils/testing/index.d.ts +5 -0
  40. package/dist/utils/testing/index.js +14 -1
  41. package/package.json +1 -1
@@ -2,8 +2,8 @@ import pRetry from "p-retry";
2
2
  import { CallbackManager, } from "../callbacks/manager.js";
3
3
  import { LogStreamCallbackHandler, RunLogPatch, } from "../tracers/log_stream.js";
4
4
  import { Serializable } from "../load/serializable.js";
5
- import { IterableReadableStream, } from "../utils/stream.js";
6
- import { getCallbackMangerForConfig, mergeConfigs, } from "./config.js";
5
+ import { IterableReadableStream, concat, atee, AsyncGeneratorWithSetup, } from "../utils/stream.js";
6
+ import { DEFAULT_RECURSION_LIMIT, getCallbackManagerForConfig, mergeConfigs, } from "./config.js";
7
7
  import { AsyncCaller } from "../utils/async_caller.js";
8
8
  import { RootListenersTracer } from "../tracers/root_listener.js";
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -25,6 +25,18 @@ export class Runnable extends Serializable {
25
25
  writable: true,
26
26
  value: true
27
27
  });
28
+ Object.defineProperty(this, "name", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ }
35
+ getName(suffix) {
36
+ const name =
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
+ this.name ?? this.constructor.lc_name() ?? this.constructor.name;
39
+ return suffix ? `${name}${suffix}` : name;
28
40
  }
29
41
  /**
30
42
  * Bind arguments to a Runnable, returning a new Runnable.
@@ -139,17 +151,19 @@ export class Runnable extends Serializable {
139
151
  tags: options.tags,
140
152
  metadata: options.metadata,
141
153
  runName: options.runName,
154
+ configurable: options.configurable,
142
155
  };
143
156
  const callOptions = { ...options };
144
157
  delete callOptions.callbacks;
145
158
  delete callOptions.tags;
146
159
  delete callOptions.metadata;
147
160
  delete callOptions.runName;
161
+ delete callOptions.configurable;
148
162
  return [runnableConfig, callOptions];
149
163
  }
150
164
  async _callWithConfig(func, input, options) {
151
- const callbackManager_ = await getCallbackMangerForConfig(options);
152
- const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName);
165
+ const callbackManager_ = await getCallbackManagerForConfig(options);
166
+ const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, options?.runType, undefined, undefined, options?.runName ?? this.getName());
153
167
  let output;
154
168
  try {
155
169
  output = await func.bind(this)(input, options, runManager);
@@ -172,8 +186,8 @@ export class Runnable extends Serializable {
172
186
  */
173
187
  async _batchWithConfig(func, inputs, options, batchOptions) {
174
188
  const optionsList = this._getOptionsList(options ?? {}, inputs.length);
175
- const callbackManagers = await Promise.all(optionsList.map(getCallbackMangerForConfig));
176
- const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, optionsList[i].runType, undefined, undefined, optionsList[i].runName)));
189
+ const callbackManagers = await Promise.all(optionsList.map(getCallbackManagerForConfig));
190
+ const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, optionsList[i].runType, undefined, undefined, optionsList[i].runName ?? this.getName())));
177
191
  let outputs;
178
192
  try {
179
193
  outputs = await func(inputs, optionsList, runManagers, batchOptions);
@@ -195,16 +209,10 @@ export class Runnable extends Serializable {
195
209
  let finalInputSupported = true;
196
210
  let finalOutput;
197
211
  let finalOutputSupported = true;
198
- const callbackManager_ = await getCallbackMangerForConfig(options);
199
- let runManager;
200
- const serializedRepresentation = this.toJSON();
212
+ const callbackManager_ = await getCallbackManagerForConfig(options);
213
+ const inputGeneratorWithSetup = new AsyncGeneratorWithSetup(inputGenerator, async () => callbackManager_?.handleChainStart(this.toJSON(), { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName ?? this.getName()));
201
214
  async function* wrapInputForTracing() {
202
- for await (const chunk of inputGenerator) {
203
- if (!runManager) {
204
- // Start the run manager AFTER the iterator starts to preserve
205
- // tracing order
206
- runManager = await callbackManager_?.handleChainStart(serializedRepresentation, { input: "" }, undefined, options?.runType, undefined, undefined, options?.runName);
207
- }
215
+ for await (const chunk of inputGeneratorWithSetup) {
208
216
  if (finalInputSupported) {
209
217
  if (finalInput === undefined) {
210
218
  finalInput = chunk;
@@ -212,7 +220,7 @@ export class Runnable extends Serializable {
212
220
  else {
213
221
  try {
214
222
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
215
- finalInput = finalInput.concat(chunk);
223
+ finalInput = concat(finalInput, chunk);
216
224
  }
217
225
  catch {
218
226
  finalInput = undefined;
@@ -223,9 +231,8 @@ export class Runnable extends Serializable {
223
231
  yield chunk;
224
232
  }
225
233
  }
226
- const wrappedInputGenerator = wrapInputForTracing();
227
234
  try {
228
- const outputIterator = transformer(wrappedInputGenerator, runManager, options);
235
+ const outputIterator = transformer(wrapInputForTracing(), inputGeneratorWithSetup.setup, options);
229
236
  for await (const chunk of outputIterator) {
230
237
  yield chunk;
231
238
  if (finalOutputSupported) {
@@ -235,7 +242,7 @@ export class Runnable extends Serializable {
235
242
  else {
236
243
  try {
237
244
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
238
- finalOutput = finalOutput.concat(chunk);
245
+ finalOutput = concat(finalOutput, chunk);
239
246
  }
240
247
  catch {
241
248
  finalOutput = undefined;
@@ -246,14 +253,16 @@ export class Runnable extends Serializable {
246
253
  }
247
254
  }
248
255
  catch (e) {
256
+ const runManager = await inputGeneratorWithSetup.setup;
249
257
  await runManager?.handleChainError(e, undefined, undefined, undefined, {
250
258
  inputs: _coerceToDict(finalInput, "input"),
251
259
  });
252
260
  throw e;
253
261
  }
262
+ const runManager = await inputGeneratorWithSetup.setup;
254
263
  await runManager?.handleChainEnd(finalOutput ?? {}, undefined, undefined, undefined, { inputs: _coerceToDict(finalInput, "input") });
255
264
  }
256
- _patchConfig(config = {}, callbackManager = undefined) {
265
+ _patchConfig(config = {}, callbackManager = undefined, recursionLimit = undefined) {
257
266
  const newConfig = { ...config };
258
267
  if (callbackManager !== undefined) {
259
268
  /**
@@ -263,6 +272,9 @@ export class Runnable extends Serializable {
263
272
  delete newConfig.runName;
264
273
  return { ...newConfig, callbacks: callbackManager };
265
274
  }
275
+ if (recursionLimit !== undefined) {
276
+ newConfig.recursionLimit = recursionLimit;
277
+ }
266
278
  return newConfig;
267
279
  }
268
280
  /**
@@ -278,6 +290,23 @@ export class Runnable extends Serializable {
278
290
  last: _coerceToRunnable(coerceable),
279
291
  });
280
292
  }
293
+ /**
294
+ * Pick keys from the dict output of this runnable. Returns a new runnable.
295
+ */
296
+ pick(keys) {
297
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
298
+ return this.pipe(new RunnablePick(keys));
299
+ }
300
+ /**
301
+ * Assigns new fields to the dict output of this runnable. Returns a new runnable.
302
+ */
303
+ assign(mapping) {
304
+ return this.pipe(
305
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
306
+ new RunnableAssign(
307
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
308
+ new RunnableMap({ steps: mapping })));
309
+ }
281
310
  /**
282
311
  * Default implementation of transform, which buffers input and then calls stream.
283
312
  * Subclasses should override this method if they can start producing output while
@@ -295,7 +324,7 @@ export class Runnable extends Serializable {
295
324
  // Make a best effort to gather, for any type that supports concat.
296
325
  // This method should throw an error if gathering fails.
297
326
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
298
- finalChunk = finalChunk.concat(chunk);
327
+ finalChunk = concat(finalChunk, chunk);
299
328
  }
300
329
  }
301
330
  yield* this._streamIterator(finalChunk, options);
@@ -444,6 +473,9 @@ export class RunnableBinding extends Runnable {
444
473
  this.config = fields.config;
445
474
  this.configFactories = fields.configFactories;
446
475
  }
476
+ getName(suffix) {
477
+ return this.bound.getName(suffix);
478
+ }
447
479
  async _mergeConfig(
448
480
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
449
481
  options) {
@@ -453,21 +485,24 @@ export class RunnableBinding extends Runnable {
453
485
  : []));
454
486
  }
455
487
  bind(kwargs) {
456
- return this.constructor({
488
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
489
+ return new this.constructor({
457
490
  bound: this.bound,
458
491
  kwargs: { ...this.kwargs, ...kwargs },
459
492
  config: this.config,
460
493
  });
461
494
  }
462
495
  withConfig(config) {
463
- return this.constructor({
496
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
497
+ return new this.constructor({
464
498
  bound: this.bound,
465
499
  kwargs: this.kwargs,
466
500
  config: { ...this.config, ...config },
467
501
  });
468
502
  }
469
503
  withRetry(fields) {
470
- return this.constructor({
504
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
505
+ return new this.constructor({
471
506
  bound: this.bound.withRetry(fields),
472
507
  kwargs: this.kwargs,
473
508
  config: this.config,
@@ -766,12 +801,13 @@ export class RunnableSequence extends Runnable {
766
801
  this.first = fields.first;
767
802
  this.middle = fields.middle ?? this.middle;
768
803
  this.last = fields.last;
804
+ this.name = fields.name;
769
805
  }
770
806
  get steps() {
771
807
  return [this.first, ...this.middle, this.last];
772
808
  }
773
809
  async invoke(input, options) {
774
- const callbackManager_ = await getCallbackMangerForConfig(options);
810
+ const callbackManager_ = await getCallbackManagerForConfig(options);
775
811
  const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
776
812
  let nextStepInput = input;
777
813
  let finalOutput;
@@ -793,7 +829,7 @@ export class RunnableSequence extends Runnable {
793
829
  }
794
830
  async batch(inputs, options, batchOptions) {
795
831
  const configList = this._getOptionsList(options ?? {}, inputs.length);
796
- const callbackManagers = await Promise.all(configList.map(getCallbackMangerForConfig));
832
+ const callbackManagers = await Promise.all(configList.map(getCallbackManagerForConfig));
797
833
  const runManagers = await Promise.all(callbackManagers.map((callbackManager, i) => callbackManager?.handleChainStart(this.toJSON(), _coerceToDict(inputs[i], "input"), undefined, undefined, undefined, undefined, configList[i].runName)));
798
834
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
799
835
  let nextStepInputs = inputs;
@@ -814,7 +850,7 @@ export class RunnableSequence extends Runnable {
814
850
  return finalOutputs;
815
851
  }
816
852
  async *_streamIterator(input, options) {
817
- const callbackManager_ = await getCallbackMangerForConfig(options);
853
+ const callbackManager_ = await getCallbackManagerForConfig(options);
818
854
  const runManager = await callbackManager_?.handleChainStart(this.toJSON(), _coerceToDict(input, "input"), undefined, undefined, undefined, undefined, options?.runName);
819
855
  const steps = [this.first, ...this.middle, this.last];
820
856
  let concatSupported = true;
@@ -837,7 +873,7 @@ export class RunnableSequence extends Runnable {
837
873
  else {
838
874
  try {
839
875
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
840
- finalOutput = finalOutput.concat(chunk);
876
+ finalOutput = concat(finalOutput, chunk);
841
877
  }
842
878
  catch (e) {
843
879
  finalOutput = undefined;
@@ -863,6 +899,7 @@ export class RunnableSequence extends Runnable {
863
899
  ...coerceable.middle,
864
900
  ]),
865
901
  last: coerceable.last,
902
+ name: this.name ?? coerceable.name,
866
903
  });
867
904
  }
868
905
  else {
@@ -870,6 +907,7 @@ export class RunnableSequence extends Runnable {
870
907
  first: this.first,
871
908
  middle: [...this.middle, this.last],
872
909
  last: _coerceToRunnable(coerceable),
910
+ name: this.name,
873
911
  });
874
912
  }
875
913
  }
@@ -878,11 +916,12 @@ export class RunnableSequence extends Runnable {
878
916
  return Array.isArray(thing.middle) && Runnable.isRunnable(thing);
879
917
  }
880
918
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
881
- static from([first, ...runnables]) {
919
+ static from([first, ...runnables], name) {
882
920
  return new RunnableSequence({
883
921
  first: _coerceToRunnable(first),
884
922
  middle: runnables.slice(0, -1).map(_coerceToRunnable),
885
923
  last: _coerceToRunnable(runnables[runnables.length - 1]),
924
+ name,
886
925
  });
887
926
  }
888
927
  }
@@ -938,7 +977,7 @@ export class RunnableMap extends Runnable {
938
977
  return new RunnableMap({ steps });
939
978
  }
940
979
  async invoke(input, options) {
941
- const callbackManager_ = await getCallbackMangerForConfig(options);
980
+ const callbackManager_ = await getCallbackManagerForConfig(options);
942
981
  const runManager = await callbackManager_?.handleChainStart(this.toJSON(), {
943
982
  input,
944
983
  }, undefined, undefined, undefined, undefined, options?.runName);
@@ -946,7 +985,7 @@ export class RunnableMap extends Runnable {
946
985
  const output = {};
947
986
  try {
948
987
  await Promise.all(Object.entries(this.steps).map(async ([key, runnable]) => {
949
- output[key] = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild(key)));
988
+ output[key] = await runnable.invoke(input, this._patchConfig(options, runManager?.getChild(`map:key:${key}`)));
950
989
  }));
951
990
  }
952
991
  catch (e) {
@@ -956,6 +995,38 @@ export class RunnableMap extends Runnable {
956
995
  await runManager?.handleChainEnd(output);
957
996
  return output;
958
997
  }
998
+ async *_transform(generator, runManagerPromise, options) {
999
+ // shallow copy steps to ignore changes while iterating
1000
+ const steps = { ...this.steps };
1001
+ // each step gets a copy of the input iterator
1002
+ const inputCopies = atee(generator, Object.keys(steps).length);
1003
+ const runManager = await runManagerPromise;
1004
+ // start the first iteration of each output iterator
1005
+ const tasks = new Map(Object.entries(steps).map(([key, runnable], i) => {
1006
+ const gen = runnable.transform(inputCopies[i], this._patchConfig(options, runManager?.getChild(`map:key:${key}`)));
1007
+ return [key, gen.next().then((result) => ({ key, gen, result }))];
1008
+ }));
1009
+ // yield chunks as they become available,
1010
+ // starting new iterations as needed,
1011
+ // until all iterators are done
1012
+ while (tasks.size) {
1013
+ const { key, result, gen } = await Promise.race(tasks.values());
1014
+ tasks.delete(key);
1015
+ if (!result.done) {
1016
+ yield { [key]: result.value };
1017
+ tasks.set(key, gen.next().then((result) => ({ key, gen, result })));
1018
+ }
1019
+ }
1020
+ }
1021
+ transform(generator, options) {
1022
+ return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
1023
+ }
1024
+ async stream(input, options) {
1025
+ async function* generator() {
1026
+ yield input;
1027
+ }
1028
+ return IterableReadableStream.fromAsyncGenerator(this.transform(generator(), options));
1029
+ }
959
1030
  }
960
1031
  /**
961
1032
  * A runnable that runs a callable.
@@ -988,13 +1059,57 @@ export class RunnableLambda extends Runnable {
988
1059
  async _invoke(input, config, runManager) {
989
1060
  let output = await this.func(input, { config });
990
1061
  if (output && Runnable.isRunnable(output)) {
991
- output = await output.invoke(input, this._patchConfig(config, runManager?.getChild()));
1062
+ if (config?.recursionLimit === 0) {
1063
+ throw new Error("Recursion limit reached.");
1064
+ }
1065
+ output = await output.invoke(input, this._patchConfig(config, runManager?.getChild(), (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1));
992
1066
  }
993
1067
  return output;
994
1068
  }
995
1069
  async invoke(input, options) {
996
1070
  return this._callWithConfig(this._invoke, input, options);
997
1071
  }
1072
+ async *_transform(generator, runManagerPromise, config) {
1073
+ let finalChunk;
1074
+ for await (const chunk of generator) {
1075
+ if (finalChunk === undefined) {
1076
+ finalChunk = chunk;
1077
+ }
1078
+ else {
1079
+ // Make a best effort to gather, for any type that supports concat.
1080
+ try {
1081
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1082
+ finalChunk = concat(finalChunk, chunk);
1083
+ }
1084
+ catch (e) {
1085
+ finalChunk = chunk;
1086
+ }
1087
+ }
1088
+ }
1089
+ const output = await this.func(finalChunk, { config });
1090
+ if (output && Runnable.isRunnable(output)) {
1091
+ if (config?.recursionLimit === 0) {
1092
+ throw new Error("Recursion limit reached.");
1093
+ }
1094
+ const runManager = await runManagerPromise;
1095
+ const stream = await output.stream(finalChunk, this._patchConfig(config, runManager?.getChild(), (config?.recursionLimit ?? DEFAULT_RECURSION_LIMIT) - 1));
1096
+ for await (const chunk of stream) {
1097
+ yield chunk;
1098
+ }
1099
+ }
1100
+ else {
1101
+ yield output;
1102
+ }
1103
+ }
1104
+ transform(generator, options) {
1105
+ return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
1106
+ }
1107
+ async stream(input, options) {
1108
+ async function* generator() {
1109
+ yield input;
1110
+ }
1111
+ return IterableReadableStream.fromAsyncGenerator(this.transform(generator(), options));
1112
+ }
998
1113
  }
999
1114
  export class RunnableParallel extends RunnableMap {
1000
1115
  }
@@ -1111,3 +1226,145 @@ export function _coerceToRunnable(coerceable) {
1111
1226
  throw new Error(`Expected a Runnable, function or object.\nInstead got an unsupported type.`);
1112
1227
  }
1113
1228
  }
1229
+ /**
1230
+ * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
1231
+ */
1232
+ export class RunnableAssign extends Runnable {
1233
+ static lc_name() {
1234
+ return "RunnableAssign";
1235
+ }
1236
+ constructor(fields) {
1237
+ // eslint-disable-next-line no-instanceof/no-instanceof
1238
+ if (fields instanceof RunnableMap) {
1239
+ // eslint-disable-next-line no-param-reassign
1240
+ fields = { mapper: fields };
1241
+ }
1242
+ super(fields);
1243
+ Object.defineProperty(this, "lc_namespace", {
1244
+ enumerable: true,
1245
+ configurable: true,
1246
+ writable: true,
1247
+ value: ["langchain_core", "runnables"]
1248
+ });
1249
+ Object.defineProperty(this, "lc_serializable", {
1250
+ enumerable: true,
1251
+ configurable: true,
1252
+ writable: true,
1253
+ value: true
1254
+ });
1255
+ Object.defineProperty(this, "mapper", {
1256
+ enumerable: true,
1257
+ configurable: true,
1258
+ writable: true,
1259
+ value: void 0
1260
+ });
1261
+ this.mapper = fields.mapper;
1262
+ }
1263
+ async invoke(input, options) {
1264
+ const mapperResult = await this.mapper.invoke(input, options);
1265
+ return {
1266
+ ...input,
1267
+ ...mapperResult,
1268
+ };
1269
+ }
1270
+ async *_transform(generator, runManagerPromise, options) {
1271
+ // collect mapper keys
1272
+ const mapperKeys = this.mapper.getStepsKeys();
1273
+ // create two input gens, one for the mapper, one for the input
1274
+ const [forPassthrough, forMapper] = atee(generator);
1275
+ const runManager = await runManagerPromise;
1276
+ // create mapper output gen
1277
+ const mapperOutput = this.mapper.transform(forMapper, this._patchConfig(options, runManager?.getChild()));
1278
+ // start the mapper
1279
+ const firstMapperChunkPromise = mapperOutput.next();
1280
+ // yield the passthrough
1281
+ for await (const chunk of forPassthrough) {
1282
+ if (typeof chunk !== "object" || Array.isArray(chunk)) {
1283
+ throw new Error(`RunnableAssign can only be used with objects as input, got ${typeof chunk}`);
1284
+ }
1285
+ const filtered = Object.fromEntries(Object.entries(chunk).filter(([key]) => !mapperKeys.includes(key)));
1286
+ if (Object.keys(filtered).length > 0) {
1287
+ yield filtered;
1288
+ }
1289
+ }
1290
+ // yield the mapper output
1291
+ yield (await firstMapperChunkPromise).value;
1292
+ for await (const chunk of mapperOutput) {
1293
+ yield chunk;
1294
+ }
1295
+ }
1296
+ transform(generator, options) {
1297
+ return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
1298
+ }
1299
+ async stream(input, options) {
1300
+ async function* generator() {
1301
+ yield input;
1302
+ }
1303
+ return IterableReadableStream.fromAsyncGenerator(this.transform(generator(), options));
1304
+ }
1305
+ }
1306
+ /**
1307
+ * A runnable that assigns key-value pairs to inputs of type `Record<string, unknown>`.
1308
+ */
1309
+ export class RunnablePick extends Runnable {
1310
+ static lc_name() {
1311
+ return "RunnablePick";
1312
+ }
1313
+ constructor(fields) {
1314
+ if (typeof fields === "string" || Array.isArray(fields)) {
1315
+ // eslint-disable-next-line no-param-reassign
1316
+ fields = { keys: fields };
1317
+ }
1318
+ super(fields);
1319
+ Object.defineProperty(this, "lc_namespace", {
1320
+ enumerable: true,
1321
+ configurable: true,
1322
+ writable: true,
1323
+ value: ["langchain_core", "runnables"]
1324
+ });
1325
+ Object.defineProperty(this, "lc_serializable", {
1326
+ enumerable: true,
1327
+ configurable: true,
1328
+ writable: true,
1329
+ value: true
1330
+ });
1331
+ Object.defineProperty(this, "keys", {
1332
+ enumerable: true,
1333
+ configurable: true,
1334
+ writable: true,
1335
+ value: void 0
1336
+ });
1337
+ this.keys = fields.keys;
1338
+ }
1339
+ async _pick(input) {
1340
+ if (typeof this.keys === "string") {
1341
+ return input[this.keys];
1342
+ }
1343
+ else {
1344
+ const picked = this.keys
1345
+ .map((key) => [key, input[key]])
1346
+ .filter((v) => v[1] !== undefined);
1347
+ return picked.length === 0 ? undefined : Object.fromEntries(picked);
1348
+ }
1349
+ }
1350
+ async invoke(input, options) {
1351
+ return this._callWithConfig(this._pick.bind(this), input, options);
1352
+ }
1353
+ async *_transform(generator) {
1354
+ for await (const chunk of generator) {
1355
+ const picked = await this._pick(chunk);
1356
+ if (picked !== undefined) {
1357
+ yield picked;
1358
+ }
1359
+ }
1360
+ }
1361
+ transform(generator, options) {
1362
+ return this._transformStreamWithConfig(generator, this._transform.bind(this), options);
1363
+ }
1364
+ async stream(input, options) {
1365
+ async function* generator() {
1366
+ yield input;
1367
+ }
1368
+ return IterableReadableStream.fromAsyncGenerator(this.transform(generator(), options));
1369
+ }
1370
+ }
@@ -1,11 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mergeConfigs = exports.getCallbackMangerForConfig = void 0;
3
+ exports.mergeConfigs = exports.getCallbackManagerForConfig = exports.DEFAULT_RECURSION_LIMIT = void 0;
4
4
  const manager_js_1 = require("../callbacks/manager.cjs");
5
- async function getCallbackMangerForConfig(config) {
5
+ exports.DEFAULT_RECURSION_LIMIT = 25;
6
+ async function getCallbackManagerForConfig(config) {
6
7
  return manager_js_1.CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
7
8
  }
8
- exports.getCallbackMangerForConfig = getCallbackMangerForConfig;
9
+ exports.getCallbackManagerForConfig = getCallbackManagerForConfig;
9
10
  function mergeConfigs(config,
10
11
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
11
12
  options) {
@@ -19,6 +20,9 @@ options) {
19
20
  else if (key === "tags") {
20
21
  copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
21
22
  }
23
+ else if (key === "configurable") {
24
+ copy[key] = { ...copy[key], ...options[key] };
25
+ }
22
26
  else if (key === "callbacks") {
23
27
  const baseCallbacks = copy.callbacks;
24
28
  const providedCallbacks = options.callbacks ?? config.callbacks;
@@ -1,4 +1,15 @@
1
1
  import { type BaseCallbackConfig, CallbackManager } from "../callbacks/manager.js";
2
- export type RunnableConfig = BaseCallbackConfig;
3
- export declare function getCallbackMangerForConfig(config?: RunnableConfig): Promise<CallbackManager | undefined>;
2
+ export declare const DEFAULT_RECURSION_LIMIT = 25;
3
+ export interface RunnableConfig extends BaseCallbackConfig {
4
+ /**
5
+ * Runtime values for attributes previously made configurable on this Runnable,
6
+ * or sub-Runnables.
7
+ */
8
+ configurable?: Record<string, any>;
9
+ /**
10
+ * Maximum number of times a call can recurse. If not provided, defaults to 25.
11
+ */
12
+ recursionLimit?: number;
13
+ }
14
+ export declare function getCallbackManagerForConfig(config?: RunnableConfig): Promise<CallbackManager | undefined>;
4
15
  export declare function mergeConfigs<CallOptions extends RunnableConfig>(config: RunnableConfig, options?: Record<string, any>): Partial<CallOptions>;
@@ -1,5 +1,6 @@
1
1
  import { CallbackManager, } from "../callbacks/manager.js";
2
- export async function getCallbackMangerForConfig(config) {
2
+ export const DEFAULT_RECURSION_LIMIT = 25;
3
+ export async function getCallbackManagerForConfig(config) {
3
4
  return CallbackManager.configure(config?.callbacks, undefined, config?.tags, undefined, config?.metadata);
4
5
  }
5
6
  export function mergeConfigs(config,
@@ -15,6 +16,9 @@ options) {
15
16
  else if (key === "tags") {
16
17
  copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
17
18
  }
19
+ else if (key === "configurable") {
20
+ copy[key] = { ...copy[key], ...options[key] };
21
+ }
18
22
  else if (key === "callbacks") {
19
23
  const baseCallbacks = copy.callbacks;
20
24
  const providedCallbacks = options.callbacks ?? config.callbacks;
@@ -93,16 +93,16 @@ class RunnableWithMessageHistory extends base_js_1.RunnableBinding {
93
93
  }
94
94
  throw new Error(`Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(newOutputValue, null, 2)}`);
95
95
  }
96
- _enterHistory(
96
+ async _enterHistory(
97
97
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
98
98
  input, kwargs) {
99
99
  const history = kwargs?.config?.configurable?.messageHistory;
100
100
  if (this.historyMessagesKey) {
101
- return history.messages;
101
+ return history.getMessages();
102
102
  }
103
103
  const inputVal = input ||
104
104
  (this.inputMessagesKey ? input[this.inputMessagesKey] : undefined);
105
- const historyMessages = history ? history.messages : [];
105
+ const historyMessages = history ? await history.getMessages() : [];
106
106
  const returnType = [
107
107
  ...historyMessages,
108
108
  ...this._getInputMessages(inputVal),
@@ -1,11 +1,10 @@
1
- import { BaseCallbackConfig } from "../callbacks/manager.js";
2
1
  import { BaseChatMessageHistory, BaseListChatMessageHistory } from "../chat_history.js";
3
2
  import { BaseMessage } from "../messages/index.js";
4
3
  import { Run } from "../tracers/base.js";
5
4
  import { Runnable, RunnableBinding, type RunnableBindingArgs } from "./base.js";
6
5
  import { RunnableConfig } from "./config.js";
7
6
  type GetSessionHistoryCallable = (...args: Array<any>) => Promise<BaseChatMessageHistory | BaseListChatMessageHistory> | BaseChatMessageHistory | BaseListChatMessageHistory;
8
- export interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<RunnableBindingArgs<RunInput, RunOutput, BaseCallbackConfig>, "bound" | "config"> {
7
+ export interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends Omit<RunnableBindingArgs<RunInput, RunOutput>, "bound" | "config"> {
9
8
  runnable: Runnable<RunInput, RunOutput>;
10
9
  getMessageHistory: GetSessionHistoryCallable;
11
10
  inputMessagesKey?: string;
@@ -13,7 +12,7 @@ export interface RunnableWithMessageHistoryInputs<RunInput, RunOutput> extends O
13
12
  historyMessagesKey?: string;
14
13
  config?: RunnableConfig;
15
14
  }
16
- export declare class RunnableWithMessageHistory<RunInput, RunOutput> extends RunnableBinding<RunInput, RunOutput, BaseCallbackConfig> {
15
+ export declare class RunnableWithMessageHistory<RunInput, RunOutput> extends RunnableBinding<RunInput, RunOutput> {
17
16
  runnable: Runnable<RunInput, RunOutput>;
18
17
  inputMessagesKey?: string;
19
18
  outputMessagesKey?: string;
@@ -24,8 +23,8 @@ export declare class RunnableWithMessageHistory<RunInput, RunOutput> extends Run
24
23
  _getOutputMessages(outputValue: string | BaseMessage | Array<BaseMessage> | Record<string, any>): Array<BaseMessage>;
25
24
  _enterHistory(input: any, kwargs?: {
26
25
  config?: RunnableConfig;
27
- }): Array<BaseMessage>;
28
- _exitHistory(run: Run, config: BaseCallbackConfig): Promise<void>;
29
- _mergeConfig(...configs: Array<BaseCallbackConfig | undefined>): Promise<Partial<BaseCallbackConfig>>;
26
+ }): Promise<BaseMessage[]>;
27
+ _exitHistory(run: Run, config: RunnableConfig): Promise<void>;
28
+ _mergeConfig(...configs: Array<RunnableConfig | undefined>): Promise<Partial<RunnableConfig>>;
30
29
  }
31
30
  export {};
@@ -90,16 +90,16 @@ export class RunnableWithMessageHistory extends RunnableBinding {
90
90
  }
91
91
  throw new Error(`Expected a string, BaseMessage, or array of BaseMessages. Received: ${JSON.stringify(newOutputValue, null, 2)}`);
92
92
  }
93
- _enterHistory(
93
+ async _enterHistory(
94
94
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
95
  input, kwargs) {
96
96
  const history = kwargs?.config?.configurable?.messageHistory;
97
97
  if (this.historyMessagesKey) {
98
- return history.messages;
98
+ return history.getMessages();
99
99
  }
100
100
  const inputVal = input ||
101
101
  (this.inputMessagesKey ? input[this.inputMessagesKey] : undefined);
102
- const historyMessages = history ? history.messages : [];
102
+ const historyMessages = history ? await history.getMessages() : [];
103
103
  const returnType = [
104
104
  ...historyMessages,
105
105
  ...this._getInputMessages(inputVal),