langchain 0.0.152 → 0.0.154

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 (72) hide show
  1. package/chat_models/fireworks.cjs +1 -0
  2. package/chat_models/fireworks.d.ts +1 -0
  3. package/chat_models/fireworks.js +1 -0
  4. package/dist/agents/executor.cjs +9 -2
  5. package/dist/agents/executor.js +9 -2
  6. package/dist/base_language/count_tokens.cjs +1 -1
  7. package/dist/base_language/count_tokens.js +1 -1
  8. package/dist/base_language/index.cjs +36 -0
  9. package/dist/base_language/index.d.ts +9 -1
  10. package/dist/base_language/index.js +36 -0
  11. package/dist/cache/base.cjs +24 -1
  12. package/dist/cache/base.d.ts +9 -0
  13. package/dist/cache/base.js +21 -0
  14. package/dist/cache/cloudflare_kv.cjs +2 -5
  15. package/dist/cache/cloudflare_kv.js +3 -6
  16. package/dist/cache/ioredis.cjs +16 -6
  17. package/dist/cache/ioredis.d.ts +5 -2
  18. package/dist/cache/ioredis.js +17 -7
  19. package/dist/cache/momento.cjs +6 -2
  20. package/dist/cache/momento.js +7 -3
  21. package/dist/cache/redis.cjs +3 -5
  22. package/dist/cache/redis.js +4 -6
  23. package/dist/cache/upstash_redis.cjs +2 -5
  24. package/dist/cache/upstash_redis.js +3 -6
  25. package/dist/chains/openai_functions/structured_output.d.ts +2 -2
  26. package/dist/chat_models/base.cjs +64 -20
  27. package/dist/chat_models/base.d.ts +8 -1
  28. package/dist/chat_models/base.js +64 -20
  29. package/dist/chat_models/fireworks.cjs +81 -0
  30. package/dist/chat_models/fireworks.d.ts +33 -0
  31. package/dist/chat_models/fireworks.js +77 -0
  32. package/dist/chat_models/ollama.cjs +22 -5
  33. package/dist/chat_models/ollama.d.ts +1 -2
  34. package/dist/chat_models/ollama.js +22 -5
  35. package/dist/chat_models/openai.d.ts +2 -2
  36. package/dist/llms/base.cjs +10 -26
  37. package/dist/llms/base.d.ts +4 -4
  38. package/dist/llms/base.js +4 -20
  39. package/dist/llms/fireworks.cjs +92 -0
  40. package/dist/llms/fireworks.d.ts +33 -0
  41. package/dist/llms/fireworks.js +88 -0
  42. package/dist/llms/ollama.cjs +24 -8
  43. package/dist/llms/ollama.d.ts +1 -2
  44. package/dist/llms/ollama.js +24 -8
  45. package/dist/llms/openai-chat.cjs +1 -5
  46. package/dist/llms/openai-chat.d.ts +1 -1
  47. package/dist/llms/openai-chat.js +1 -5
  48. package/dist/llms/openai.cjs +1 -1
  49. package/dist/llms/openai.d.ts +2 -2
  50. package/dist/llms/openai.js +1 -1
  51. package/dist/load/import_map.cjs +4 -2
  52. package/dist/load/import_map.d.ts +2 -0
  53. package/dist/load/import_map.js +2 -0
  54. package/dist/schema/index.cjs +50 -1
  55. package/dist/schema/index.d.ts +5 -0
  56. package/dist/schema/index.js +48 -0
  57. package/dist/schema/output_parser.cjs +38 -6
  58. package/dist/schema/output_parser.d.ts +20 -5
  59. package/dist/schema/output_parser.js +38 -6
  60. package/dist/schema/runnable/base.cjs +65 -10
  61. package/dist/schema/runnable/base.d.ts +17 -3
  62. package/dist/schema/runnable/base.js +65 -10
  63. package/dist/stores/message/utils.cjs +2 -50
  64. package/dist/stores/message/utils.d.ts +0 -14
  65. package/dist/stores/message/utils.js +2 -49
  66. package/dist/util/ollama.cjs +2 -2
  67. package/dist/util/ollama.d.ts +6 -0
  68. package/dist/util/ollama.js +2 -2
  69. package/llms/fireworks.cjs +1 -0
  70. package/llms/fireworks.d.ts +1 -0
  71. package/llms/fireworks.js +1 -0
  72. package/package.json +17 -1
@@ -386,6 +386,54 @@ export class ChatGenerationChunk extends GenerationChunk {
386
386
  });
387
387
  }
388
388
  }
389
+ /**
390
+ * Maps messages from an older format (V1) to the current `StoredMessage`
391
+ * format. If the message is already in the `StoredMessage` format, it is
392
+ * returned as is. Otherwise, it transforms the V1 message into a
393
+ * `StoredMessage`. This function is important for maintaining
394
+ * compatibility with older message formats.
395
+ */
396
+ function mapV1MessageToStoredMessage(message) {
397
+ // TODO: Remove this mapper when we deprecate the old message format.
398
+ if (message.data !== undefined) {
399
+ return message;
400
+ }
401
+ else {
402
+ const v1Message = message;
403
+ return {
404
+ type: v1Message.type,
405
+ data: {
406
+ content: v1Message.text,
407
+ role: v1Message.role,
408
+ name: undefined,
409
+ },
410
+ };
411
+ }
412
+ }
413
+ export function mapStoredMessageToChatMessage(message) {
414
+ const storedMessage = mapV1MessageToStoredMessage(message);
415
+ switch (storedMessage.type) {
416
+ case "human":
417
+ return new HumanMessage(storedMessage.data);
418
+ case "ai":
419
+ return new AIMessage(storedMessage.data);
420
+ case "system":
421
+ return new SystemMessage(storedMessage.data);
422
+ case "function":
423
+ if (storedMessage.data.name === undefined) {
424
+ throw new Error("Name must be defined for function messages");
425
+ }
426
+ return new FunctionMessage(storedMessage.data);
427
+ case "chat": {
428
+ if (storedMessage.data.role === undefined) {
429
+ throw new Error("Role must be defined for chat messages");
430
+ }
431
+ return new ChatMessage(storedMessage.data);
432
+ }
433
+ default:
434
+ throw new Error(`Got unexpected type: ${storedMessage.type}`);
435
+ }
436
+ }
389
437
  /**
390
438
  * Base PromptValue class. All prompt values should extend this class.
391
439
  */
@@ -162,20 +162,52 @@ class BytesOutputParser extends BaseTransformOutputParser {
162
162
  }
163
163
  exports.BytesOutputParser = BytesOutputParser;
164
164
  /**
165
- * Custom error class used to handle exceptions related to output parsing.
166
- * It extends the built-in `Error` class and adds an optional `output`
167
- * property that can hold the output that caused the exception.
165
+ * Exception that output parsers should raise to signify a parsing error.
166
+ *
167
+ * This exists to differentiate parsing errors from other code or execution errors
168
+ * that also may arise inside the output parser. OutputParserExceptions will be
169
+ * available to catch and handle in ways to fix the parsing error, while other
170
+ * errors will be raised.
171
+ *
172
+ * @param message - The error that's being re-raised or an error message.
173
+ * @param llmOutput - String model output which is error-ing.
174
+ * @param observation - String explanation of error which can be passed to a
175
+ * model to try and remediate the issue.
176
+ * @param sendToLLM - Whether to send the observation and llm_output back to an Agent
177
+ * after an OutputParserException has been raised. This gives the underlying
178
+ * model driving the agent the context that the previous output was improperly
179
+ * structured, in the hopes that it will update the output to the correct
180
+ * format.
168
181
  */
169
182
  class OutputParserException extends Error {
170
- constructor(message, output) {
183
+ constructor(message, llmOutput, observation, sendToLLM = false) {
171
184
  super(message);
172
- Object.defineProperty(this, "output", {
185
+ Object.defineProperty(this, "llmOutput", {
173
186
  enumerable: true,
174
187
  configurable: true,
175
188
  writable: true,
176
189
  value: void 0
177
190
  });
178
- this.output = output;
191
+ Object.defineProperty(this, "observation", {
192
+ enumerable: true,
193
+ configurable: true,
194
+ writable: true,
195
+ value: void 0
196
+ });
197
+ Object.defineProperty(this, "sendToLLM", {
198
+ enumerable: true,
199
+ configurable: true,
200
+ writable: true,
201
+ value: void 0
202
+ });
203
+ this.llmOutput = llmOutput;
204
+ this.observation = observation;
205
+ this.sendToLLM = sendToLLM;
206
+ if (sendToLLM) {
207
+ if (observation === undefined || llmOutput === undefined) {
208
+ throw new Error("Arguments 'observation' & 'llmOutput' are required if 'sendToLlm' is true");
209
+ }
210
+ }
179
211
  }
180
212
  }
181
213
  exports.OutputParserException = OutputParserException;
@@ -118,11 +118,26 @@ export declare class BytesOutputParser extends BaseTransformOutputParser<Uint8Ar
118
118
  getFormatInstructions(): string;
119
119
  }
120
120
  /**
121
- * Custom error class used to handle exceptions related to output parsing.
122
- * It extends the built-in `Error` class and adds an optional `output`
123
- * property that can hold the output that caused the exception.
121
+ * Exception that output parsers should raise to signify a parsing error.
122
+ *
123
+ * This exists to differentiate parsing errors from other code or execution errors
124
+ * that also may arise inside the output parser. OutputParserExceptions will be
125
+ * available to catch and handle in ways to fix the parsing error, while other
126
+ * errors will be raised.
127
+ *
128
+ * @param message - The error that's being re-raised or an error message.
129
+ * @param llmOutput - String model output which is error-ing.
130
+ * @param observation - String explanation of error which can be passed to a
131
+ * model to try and remediate the issue.
132
+ * @param sendToLLM - Whether to send the observation and llm_output back to an Agent
133
+ * after an OutputParserException has been raised. This gives the underlying
134
+ * model driving the agent the context that the previous output was improperly
135
+ * structured, in the hopes that it will update the output to the correct
136
+ * format.
124
137
  */
125
138
  export declare class OutputParserException extends Error {
126
- output?: string;
127
- constructor(message: string, output?: string);
139
+ llmOutput?: string;
140
+ observation?: string;
141
+ sendToLLM: boolean;
142
+ constructor(message: string, llmOutput?: string, observation?: string, sendToLLM?: boolean);
128
143
  }
@@ -154,19 +154,51 @@ export class BytesOutputParser extends BaseTransformOutputParser {
154
154
  }
155
155
  }
156
156
  /**
157
- * Custom error class used to handle exceptions related to output parsing.
158
- * It extends the built-in `Error` class and adds an optional `output`
159
- * property that can hold the output that caused the exception.
157
+ * Exception that output parsers should raise to signify a parsing error.
158
+ *
159
+ * This exists to differentiate parsing errors from other code or execution errors
160
+ * that also may arise inside the output parser. OutputParserExceptions will be
161
+ * available to catch and handle in ways to fix the parsing error, while other
162
+ * errors will be raised.
163
+ *
164
+ * @param message - The error that's being re-raised or an error message.
165
+ * @param llmOutput - String model output which is error-ing.
166
+ * @param observation - String explanation of error which can be passed to a
167
+ * model to try and remediate the issue.
168
+ * @param sendToLLM - Whether to send the observation and llm_output back to an Agent
169
+ * after an OutputParserException has been raised. This gives the underlying
170
+ * model driving the agent the context that the previous output was improperly
171
+ * structured, in the hopes that it will update the output to the correct
172
+ * format.
160
173
  */
161
174
  export class OutputParserException extends Error {
162
- constructor(message, output) {
175
+ constructor(message, llmOutput, observation, sendToLLM = false) {
163
176
  super(message);
164
- Object.defineProperty(this, "output", {
177
+ Object.defineProperty(this, "llmOutput", {
165
178
  enumerable: true,
166
179
  configurable: true,
167
180
  writable: true,
168
181
  value: void 0
169
182
  });
170
- this.output = output;
183
+ Object.defineProperty(this, "observation", {
184
+ enumerable: true,
185
+ configurable: true,
186
+ writable: true,
187
+ value: void 0
188
+ });
189
+ Object.defineProperty(this, "sendToLLM", {
190
+ enumerable: true,
191
+ configurable: true,
192
+ writable: true,
193
+ value: void 0
194
+ });
195
+ this.llmOutput = llmOutput;
196
+ this.observation = observation;
197
+ this.sendToLLM = sendToLLM;
198
+ if (sendToLLM) {
199
+ if (observation === undefined || llmOutput === undefined) {
200
+ throw new Error("Arguments 'observation' & 'llmOutput' are required if 'sendToLlm' is true");
201
+ }
202
+ }
171
203
  }
172
204
  }
@@ -37,7 +37,7 @@ class Runnable extends serializable_js_1.Serializable {
37
37
  */
38
38
  bind(kwargs) {
39
39
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
40
- return new RunnableBinding({ bound: this, kwargs });
40
+ return new RunnableBinding({ bound: this, kwargs, config: {} });
41
41
  }
42
42
  /**
43
43
  * Return a new Runnable that maps a list of inputs to a list of outputs,
@@ -48,19 +48,33 @@ class Runnable extends serializable_js_1.Serializable {
48
48
  return new RunnableEach({ bound: this });
49
49
  }
50
50
  /**
51
- * Bind arguments to a Runnable, returning a new Runnable.
51
+ * Add retry logic to an existing runnable.
52
52
  * @param kwargs
53
- * @returns A new RunnableBinding that, when invoked, will apply the bound args.
53
+ * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
54
54
  */
55
55
  withRetry(fields) {
56
56
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
57
57
  return new RunnableRetry({
58
58
  bound: this,
59
59
  kwargs: {},
60
+ config: {},
60
61
  maxAttemptNumber: fields?.stopAfterAttempt,
61
62
  ...fields,
62
63
  });
63
64
  }
65
+ /**
66
+ * Bind config to a Runnable, returning a new Runnable.
67
+ * @param config New configuration parameters to attach to the new runnable.
68
+ * @returns A new RunnableBinding with a config matching what's passed.
69
+ */
70
+ withConfig(config) {
71
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
72
+ return new RunnableBinding({
73
+ bound: this,
74
+ config,
75
+ kwargs: {},
76
+ });
77
+ }
64
78
  /**
65
79
  * Create a new runnable from the current one that will try invoking
66
80
  * other passed fallback runnables if the initial invocation fails.
@@ -312,6 +326,12 @@ class RunnableBinding extends Runnable {
312
326
  writable: true,
313
327
  value: void 0
314
328
  });
329
+ Object.defineProperty(this, "config", {
330
+ enumerable: true,
331
+ configurable: true,
332
+ writable: true,
333
+ value: void 0
334
+ });
315
335
  Object.defineProperty(this, "kwargs", {
316
336
  enumerable: true,
317
337
  configurable: true,
@@ -320,35 +340,70 @@ class RunnableBinding extends Runnable {
320
340
  });
321
341
  this.bound = fields.bound;
322
342
  this.kwargs = fields.kwargs;
343
+ this.config = fields.config;
344
+ }
345
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
346
+ _mergeConfig(options) {
347
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
348
+ const copy = { ...this.config };
349
+ if (options) {
350
+ for (const key of Object.keys(options)) {
351
+ if (key === "metadata") {
352
+ copy[key] = { ...copy[key], ...options[key] };
353
+ }
354
+ else if (key === "tags") {
355
+ copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
356
+ }
357
+ else {
358
+ copy[key] = options[key] ?? copy[key];
359
+ }
360
+ }
361
+ }
362
+ return copy;
323
363
  }
324
364
  bind(kwargs) {
325
- return new RunnableBinding({
365
+ return this.constructor({
326
366
  bound: this.bound,
327
367
  kwargs: { ...this.kwargs, ...kwargs },
368
+ config: this.config,
369
+ });
370
+ }
371
+ withConfig(config) {
372
+ return this.constructor({
373
+ bound: this.bound,
374
+ kwargs: this.kwargs,
375
+ config: { ...this.config, ...config },
376
+ });
377
+ }
378
+ withRetry(fields) {
379
+ return this.constructor({
380
+ bound: this.bound.withRetry(fields),
381
+ kwargs: this.kwargs,
382
+ config: this.config,
328
383
  });
329
384
  }
330
385
  async invoke(input, options) {
331
- return this.bound.invoke(input, { ...options, ...this.kwargs });
386
+ return this.bound.invoke(input, this._mergeConfig({ ...options, ...this.kwargs }));
332
387
  }
333
388
  async batch(inputs, options, batchOptions) {
334
389
  const mergedOptions = Array.isArray(options)
335
- ? options.map((individualOption) => ({
390
+ ? options.map((individualOption) => this._mergeConfig({
336
391
  ...individualOption,
337
392
  ...this.kwargs,
338
393
  }))
339
- : { ...options, ...this.kwargs };
394
+ : this._mergeConfig({ ...options, ...this.kwargs });
340
395
  return this.bound.batch(inputs, mergedOptions, batchOptions);
341
396
  }
342
397
  async *_streamIterator(input, options) {
343
- yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
398
+ yield* this.bound._streamIterator(input, this._mergeConfig({ ...options, ...this.kwargs }));
344
399
  }
345
400
  async stream(input, options) {
346
- return this.bound.stream(input, { ...options, ...this.kwargs });
401
+ return this.bound.stream(input, this._mergeConfig({ ...options, ...this.kwargs }));
347
402
  }
348
403
  async *transform(
349
404
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
350
405
  generator, options) {
351
- yield* this.bound.transform(generator, options);
406
+ yield* this.bound.transform(generator, this._mergeConfig({ ...options, ...this.kwargs }));
352
407
  }
353
408
  static isRunnableBinding(
354
409
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -33,14 +33,20 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
33
33
  */
34
34
  map(): Runnable<RunInput[], RunOutput[], CallOptions>;
35
35
  /**
36
- * Bind arguments to a Runnable, returning a new Runnable.
36
+ * Add retry logic to an existing runnable.
37
37
  * @param kwargs
38
- * @returns A new RunnableBinding that, when invoked, will apply the bound args.
38
+ * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
39
39
  */
40
40
  withRetry(fields?: {
41
41
  stopAfterAttempt?: number;
42
42
  onFailedAttempt?: RunnableRetryFailedAttemptHandler;
43
43
  }): RunnableRetry<RunInput, RunOutput, CallOptions>;
44
+ /**
45
+ * Bind config to a Runnable, returning a new Runnable.
46
+ * @param config New configuration parameters to attach to the new runnable.
47
+ * @returns A new RunnableBinding with a config matching what's passed.
48
+ */
49
+ withConfig(config: RunnableConfig): RunnableBinding<RunInput, RunOutput, CallOptions>;
44
50
  /**
45
51
  * Create a new runnable from the current one that will try invoking
46
52
  * other passed fallback runnables if the initial invocation fails.
@@ -122,18 +128,26 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
122
128
  export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig> = {
123
129
  bound: Runnable<RunInput, RunOutput, CallOptions>;
124
130
  kwargs: Partial<CallOptions>;
131
+ config: RunnableConfig;
125
132
  };
126
133
  /**
127
134
  * A runnable that delegates calls to another runnable with a set of kwargs.
128
135
  */
129
- export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends BaseCallbackConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
136
+ export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
130
137
  static lc_name(): string;
131
138
  lc_namespace: string[];
132
139
  lc_serializable: boolean;
133
140
  bound: Runnable<RunInput, RunOutput, CallOptions>;
141
+ config: RunnableConfig;
134
142
  protected kwargs: Partial<CallOptions>;
135
143
  constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>);
144
+ _mergeConfig(options?: Record<string, any>): Partial<CallOptions>;
136
145
  bind(kwargs: Partial<CallOptions>): RunnableBinding<RunInput, RunOutput, CallOptions>;
146
+ withConfig(config: RunnableConfig): RunnableBinding<RunInput, RunOutput, CallOptions>;
147
+ withRetry(fields?: {
148
+ stopAfterAttempt?: number;
149
+ onFailedAttempt?: RunnableRetryFailedAttemptHandler;
150
+ }): RunnableRetry<RunInput, RunOutput, CallOptions>;
137
151
  invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
138
152
  batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
139
153
  returnExceptions?: false;
@@ -31,7 +31,7 @@ export class Runnable extends Serializable {
31
31
  */
32
32
  bind(kwargs) {
33
33
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
34
- return new RunnableBinding({ bound: this, kwargs });
34
+ return new RunnableBinding({ bound: this, kwargs, config: {} });
35
35
  }
36
36
  /**
37
37
  * Return a new Runnable that maps a list of inputs to a list of outputs,
@@ -42,19 +42,33 @@ export class Runnable extends Serializable {
42
42
  return new RunnableEach({ bound: this });
43
43
  }
44
44
  /**
45
- * Bind arguments to a Runnable, returning a new Runnable.
45
+ * Add retry logic to an existing runnable.
46
46
  * @param kwargs
47
- * @returns A new RunnableBinding that, when invoked, will apply the bound args.
47
+ * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
48
48
  */
49
49
  withRetry(fields) {
50
50
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
51
51
  return new RunnableRetry({
52
52
  bound: this,
53
53
  kwargs: {},
54
+ config: {},
54
55
  maxAttemptNumber: fields?.stopAfterAttempt,
55
56
  ...fields,
56
57
  });
57
58
  }
59
+ /**
60
+ * Bind config to a Runnable, returning a new Runnable.
61
+ * @param config New configuration parameters to attach to the new runnable.
62
+ * @returns A new RunnableBinding with a config matching what's passed.
63
+ */
64
+ withConfig(config) {
65
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
66
+ return new RunnableBinding({
67
+ bound: this,
68
+ config,
69
+ kwargs: {},
70
+ });
71
+ }
58
72
  /**
59
73
  * Create a new runnable from the current one that will try invoking
60
74
  * other passed fallback runnables if the initial invocation fails.
@@ -305,6 +319,12 @@ export class RunnableBinding extends Runnable {
305
319
  writable: true,
306
320
  value: void 0
307
321
  });
322
+ Object.defineProperty(this, "config", {
323
+ enumerable: true,
324
+ configurable: true,
325
+ writable: true,
326
+ value: void 0
327
+ });
308
328
  Object.defineProperty(this, "kwargs", {
309
329
  enumerable: true,
310
330
  configurable: true,
@@ -313,35 +333,70 @@ export class RunnableBinding extends Runnable {
313
333
  });
314
334
  this.bound = fields.bound;
315
335
  this.kwargs = fields.kwargs;
336
+ this.config = fields.config;
337
+ }
338
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
339
+ _mergeConfig(options) {
340
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
341
+ const copy = { ...this.config };
342
+ if (options) {
343
+ for (const key of Object.keys(options)) {
344
+ if (key === "metadata") {
345
+ copy[key] = { ...copy[key], ...options[key] };
346
+ }
347
+ else if (key === "tags") {
348
+ copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
349
+ }
350
+ else {
351
+ copy[key] = options[key] ?? copy[key];
352
+ }
353
+ }
354
+ }
355
+ return copy;
316
356
  }
317
357
  bind(kwargs) {
318
- return new RunnableBinding({
358
+ return this.constructor({
319
359
  bound: this.bound,
320
360
  kwargs: { ...this.kwargs, ...kwargs },
361
+ config: this.config,
362
+ });
363
+ }
364
+ withConfig(config) {
365
+ return this.constructor({
366
+ bound: this.bound,
367
+ kwargs: this.kwargs,
368
+ config: { ...this.config, ...config },
369
+ });
370
+ }
371
+ withRetry(fields) {
372
+ return this.constructor({
373
+ bound: this.bound.withRetry(fields),
374
+ kwargs: this.kwargs,
375
+ config: this.config,
321
376
  });
322
377
  }
323
378
  async invoke(input, options) {
324
- return this.bound.invoke(input, { ...options, ...this.kwargs });
379
+ return this.bound.invoke(input, this._mergeConfig({ ...options, ...this.kwargs }));
325
380
  }
326
381
  async batch(inputs, options, batchOptions) {
327
382
  const mergedOptions = Array.isArray(options)
328
- ? options.map((individualOption) => ({
383
+ ? options.map((individualOption) => this._mergeConfig({
329
384
  ...individualOption,
330
385
  ...this.kwargs,
331
386
  }))
332
- : { ...options, ...this.kwargs };
387
+ : this._mergeConfig({ ...options, ...this.kwargs });
333
388
  return this.bound.batch(inputs, mergedOptions, batchOptions);
334
389
  }
335
390
  async *_streamIterator(input, options) {
336
- yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
391
+ yield* this.bound._streamIterator(input, this._mergeConfig({ ...options, ...this.kwargs }));
337
392
  }
338
393
  async stream(input, options) {
339
- return this.bound.stream(input, { ...options, ...this.kwargs });
394
+ return this.bound.stream(input, this._mergeConfig({ ...options, ...this.kwargs }));
340
395
  }
341
396
  async *transform(
342
397
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
343
398
  generator, options) {
344
- yield* this.bound.transform(generator, options);
399
+ yield* this.bound.transform(generator, this._mergeConfig({ ...options, ...this.kwargs }));
345
400
  }
346
401
  static isRunnableBinding(
347
402
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -1,32 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.mapChatMessagesToStoredMessages = exports.mapStoredMessagesToChatMessages = exports.mapV1MessageToStoredMessage = void 0;
3
+ exports.mapChatMessagesToStoredMessages = exports.mapStoredMessagesToChatMessages = void 0;
4
4
  const index_js_1 = require("../../schema/index.cjs");
5
- /**
6
- * Maps messages from an older format (V1) to the current `StoredMessage`
7
- * format. If the message is already in the `StoredMessage` format, it is
8
- * returned as is. Otherwise, it transforms the V1 message into a
9
- * `StoredMessage`. This function is important for maintaining
10
- * compatibility with older message formats.
11
- */
12
- function mapV1MessageToStoredMessage(message) {
13
- // TODO: Remove this mapper when we deprecate the old message format.
14
- if (message.data !== undefined) {
15
- return message;
16
- }
17
- else {
18
- const v1Message = message;
19
- return {
20
- type: v1Message.type,
21
- data: {
22
- content: v1Message.text,
23
- role: v1Message.role,
24
- name: undefined,
25
- },
26
- };
27
- }
28
- }
29
- exports.mapV1MessageToStoredMessage = mapV1MessageToStoredMessage;
30
5
  /**
31
6
  * Transforms an array of `StoredMessage` instances into an array of
32
7
  * `BaseMessage` instances. It uses the `mapV1MessageToStoredMessage`
@@ -36,30 +11,7 @@ exports.mapV1MessageToStoredMessage = mapV1MessageToStoredMessage;
36
11
  * messages for use in a chat context.
37
12
  */
38
13
  function mapStoredMessagesToChatMessages(messages) {
39
- return messages.map((message) => {
40
- const storedMessage = mapV1MessageToStoredMessage(message);
41
- switch (storedMessage.type) {
42
- case "human":
43
- return new index_js_1.HumanMessage(storedMessage.data);
44
- case "ai":
45
- return new index_js_1.AIMessage(storedMessage.data);
46
- case "system":
47
- return new index_js_1.SystemMessage(storedMessage.data);
48
- case "function":
49
- if (storedMessage.data.name === undefined) {
50
- throw new Error("Name must be defined for function messages");
51
- }
52
- return new index_js_1.FunctionMessage(storedMessage.data);
53
- case "chat": {
54
- if (storedMessage.data.role === undefined) {
55
- throw new Error("Role must be defined for chat messages");
56
- }
57
- return new index_js_1.ChatMessage(storedMessage.data);
58
- }
59
- default:
60
- throw new Error(`Got unexpected type: ${storedMessage.type}`);
61
- }
62
- });
14
+ return messages.map(index_js_1.mapStoredMessageToChatMessage);
63
15
  }
64
16
  exports.mapStoredMessagesToChatMessages = mapStoredMessagesToChatMessages;
65
17
  /**
@@ -1,17 +1,4 @@
1
1
  import { BaseMessage, StoredMessage } from "../../schema/index.js";
2
- interface StoredMessageV1 {
3
- type: string;
4
- role: string | undefined;
5
- text: string;
6
- }
7
- /**
8
- * Maps messages from an older format (V1) to the current `StoredMessage`
9
- * format. If the message is already in the `StoredMessage` format, it is
10
- * returned as is. Otherwise, it transforms the V1 message into a
11
- * `StoredMessage`. This function is important for maintaining
12
- * compatibility with older message formats.
13
- */
14
- export declare function mapV1MessageToStoredMessage(message: StoredMessage | StoredMessageV1): StoredMessage;
15
2
  /**
16
3
  * Transforms an array of `StoredMessage` instances into an array of
17
4
  * `BaseMessage` instances. It uses the `mapV1MessageToStoredMessage`
@@ -28,4 +15,3 @@ export declare function mapStoredMessagesToChatMessages(messages: StoredMessage[
28
15
  * is used to prepare chat messages for storage.
29
16
  */
30
17
  export declare function mapChatMessagesToStoredMessages(messages: BaseMessage[]): StoredMessage[];
31
- export {};