freestyle-sandboxes 0.0.45 → 0.0.47

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.
@@ -0,0 +1,2162 @@
1
+ import { F as FreestyleExecuteScriptParamsConfiguration } from '../types.gen-CIf3ciN7.js';
2
+ import { z } from 'zod';
3
+
4
+ type AgentAction = {
5
+ tool: string;
6
+ toolInput: string | Record<string, any>;
7
+ log: string;
8
+ };
9
+ type AgentFinish = {
10
+ returnValues: Record<string, any>;
11
+ log: string;
12
+ };
13
+
14
+ type ChainValues = Record<string, any>;
15
+
16
+ interface SerializedFields {
17
+ [key: string]: any;
18
+ }
19
+
20
+ interface BaseSerialized<T extends string> {
21
+ lc: number;
22
+ type: T;
23
+ id: string[];
24
+ name?: string;
25
+ graph?: Record<string, any>;
26
+ }
27
+ interface SerializedConstructor extends BaseSerialized<"constructor"> {
28
+ kwargs: SerializedFields;
29
+ }
30
+ interface SerializedSecret extends BaseSerialized<"secret"> {
31
+ }
32
+ interface SerializedNotImplemented extends BaseSerialized<"not_implemented"> {
33
+ }
34
+ type Serialized = SerializedConstructor | SerializedSecret | SerializedNotImplemented;
35
+ interface SerializableInterface {
36
+ get lc_id(): string[];
37
+ }
38
+ declare abstract class Serializable implements SerializableInterface {
39
+ lc_serializable: boolean;
40
+ lc_kwargs: SerializedFields;
41
+ /**
42
+ * A path to the module that contains the class, eg. ["langchain", "llms"]
43
+ * Usually should be the same as the entrypoint the class is exported from.
44
+ */
45
+ abstract lc_namespace: string[];
46
+ /**
47
+ * The name of the serializable. Override to provide an alias or
48
+ * to preserve the serialized module name in minified environments.
49
+ *
50
+ * Implemented as a static method to support loading logic.
51
+ */
52
+ static lc_name(): string;
53
+ /**
54
+ * The final serialized identifier for the module.
55
+ */
56
+ get lc_id(): string[];
57
+ /**
58
+ * A map of secrets, which will be omitted from serialization.
59
+ * Keys are paths to the secret in constructor args, e.g. "foo.bar.baz".
60
+ * Values are the secret ids, which will be used when deserializing.
61
+ */
62
+ get lc_secrets(): {
63
+ [key: string]: string;
64
+ } | undefined;
65
+ /**
66
+ * A map of additional attributes to merge with constructor args.
67
+ * Keys are the attribute names, e.g. "foo".
68
+ * Values are the attribute values, which will be serialized.
69
+ * These attributes need to be accepted by the constructor as arguments.
70
+ */
71
+ get lc_attributes(): SerializedFields | undefined;
72
+ /**
73
+ * A map of aliases for constructor args.
74
+ * Keys are the attribute names, e.g. "foo".
75
+ * Values are the alias that will replace the key in serialization.
76
+ * This is used to eg. make argument names match Python.
77
+ */
78
+ get lc_aliases(): {
79
+ [key: string]: string;
80
+ } | undefined;
81
+ /**
82
+ * A manual list of keys that should be serialized.
83
+ * If not overridden, all fields passed into the constructor will be serialized.
84
+ */
85
+ get lc_serializable_keys(): string[] | undefined;
86
+ constructor(kwargs?: SerializedFields, ..._args: never[]);
87
+ toJSON(): Serialized;
88
+ toJSONNotImplemented(): SerializedNotImplemented;
89
+ }
90
+
91
+ interface StoredMessageData {
92
+ content: string;
93
+ role: string | undefined;
94
+ name: string | undefined;
95
+ tool_call_id: string | undefined;
96
+ additional_kwargs?: Record<string, any>;
97
+ /** Response metadata. For example: response headers, logprobs, token counts. */
98
+ response_metadata?: Record<string, any>;
99
+ id?: string;
100
+ }
101
+ interface StoredMessage {
102
+ type: string;
103
+ data: StoredMessageData;
104
+ }
105
+ type MessageType = "human" | "ai" | "generic" | "developer" | "system" | "function" | "tool" | "remove";
106
+ type ImageDetail = "auto" | "low" | "high";
107
+ type MessageContentText = {
108
+ type: "text";
109
+ text: string;
110
+ };
111
+ type MessageContentImageUrl = {
112
+ type: "image_url";
113
+ image_url: string | {
114
+ url: string;
115
+ detail?: ImageDetail;
116
+ };
117
+ };
118
+ type MessageContentComplex = MessageContentText | MessageContentImageUrl | (Record<string, any> & {
119
+ type?: "text" | "image_url" | string;
120
+ }) | (Record<string, any> & {
121
+ type?: never;
122
+ });
123
+ type MessageContent = string | MessageContentComplex[];
124
+ interface FunctionCall {
125
+ /**
126
+ * The arguments to call the function with, as generated by the model in JSON
127
+ * format. Note that the model does not always generate valid JSON, and may
128
+ * hallucinate parameters not defined by your function schema. Validate the
129
+ * arguments in your code before calling your function.
130
+ */
131
+ arguments: string;
132
+ /**
133
+ * The name of the function to call.
134
+ */
135
+ name: string;
136
+ }
137
+ type BaseMessageFields = {
138
+ content: MessageContent;
139
+ name?: string;
140
+ additional_kwargs?: {
141
+ /**
142
+ * @deprecated Use "tool_calls" field on AIMessages instead
143
+ */
144
+ function_call?: FunctionCall;
145
+ /**
146
+ * @deprecated Use "tool_calls" field on AIMessages instead
147
+ */
148
+ tool_calls?: OpenAIToolCall[];
149
+ [key: string]: unknown;
150
+ };
151
+ /** Response metadata. For example: response headers, logprobs, token counts. */
152
+ response_metadata?: Record<string, any>;
153
+ /**
154
+ * An optional unique identifier for the message. This should ideally be
155
+ * provided by the provider/model which created the message.
156
+ */
157
+ id?: string;
158
+ };
159
+ /**
160
+ * Base class for all types of messages in a conversation. It includes
161
+ * properties like `content`, `name`, and `additional_kwargs`. It also
162
+ * includes methods like `toDict()` and `_getType()`.
163
+ */
164
+ declare abstract class BaseMessage extends Serializable implements BaseMessageFields {
165
+ lc_namespace: string[];
166
+ lc_serializable: boolean;
167
+ get lc_aliases(): Record<string, string>;
168
+ /**
169
+ * @deprecated
170
+ * Use {@link BaseMessage.content} instead.
171
+ */
172
+ get text(): string;
173
+ /** The content of the message. */
174
+ content: MessageContent;
175
+ /** The name of the message sender in a multi-user chat. */
176
+ name?: string;
177
+ /** Additional keyword arguments */
178
+ additional_kwargs: NonNullable<BaseMessageFields["additional_kwargs"]>;
179
+ /** Response metadata. For example: response headers, logprobs, token counts. */
180
+ response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;
181
+ /**
182
+ * An optional unique identifier for the message. This should ideally be
183
+ * provided by the provider/model which created the message.
184
+ */
185
+ id?: string;
186
+ /**
187
+ * @deprecated Use .getType() instead or import the proper typeguard.
188
+ * For example:
189
+ *
190
+ * ```ts
191
+ * import { isAIMessage } from "@langchain/core/messages";
192
+ *
193
+ * const message = new AIMessage("Hello!");
194
+ * isAIMessage(message); // true
195
+ * ```
196
+ */
197
+ abstract _getType(): MessageType;
198
+ /** The type of the message. */
199
+ getType(): MessageType;
200
+ constructor(fields: string | BaseMessageFields,
201
+ /** @deprecated */
202
+ kwargs?: Record<string, unknown>);
203
+ toDict(): StoredMessage;
204
+ static lc_name(): string;
205
+ get _printableFields(): Record<string, unknown>;
206
+ _updateId(value: string | undefined): void;
207
+ get [Symbol.toStringTag](): any;
208
+ }
209
+ /**
210
+ * @deprecated Use "tool_calls" field on AIMessages instead
211
+ */
212
+ type OpenAIToolCall = {
213
+ /**
214
+ * The ID of the tool call.
215
+ */
216
+ id: string;
217
+ /**
218
+ * The function that the model called.
219
+ */
220
+ function: FunctionCall;
221
+ /**
222
+ * The type of the tool. Currently, only `function` is supported.
223
+ */
224
+ type: "function";
225
+ index?: number;
226
+ };
227
+ /**
228
+ * Represents a chunk of a message, which can be concatenated with other
229
+ * message chunks. It includes a method `_merge_kwargs_dict()` for merging
230
+ * additional keyword arguments from another `BaseMessageChunk` into this
231
+ * one. It also overrides the `__add__()` method to support concatenation
232
+ * of `BaseMessageChunk` instances.
233
+ */
234
+ declare abstract class BaseMessageChunk extends BaseMessage {
235
+ abstract concat(chunk: BaseMessageChunk): BaseMessageChunk;
236
+ }
237
+
238
+ declare const RUN_KEY = "__run";
239
+ /**
240
+ * Output of a single generation.
241
+ */
242
+ interface Generation {
243
+ /**
244
+ * Generated text output
245
+ */
246
+ text: string;
247
+ /**
248
+ * Raw generation info response from the provider.
249
+ * May include things like reason for finishing (e.g. in {@link OpenAI})
250
+ */
251
+ generationInfo?: Record<string, any>;
252
+ }
253
+ type GenerationChunkFields = {
254
+ text: string;
255
+ generationInfo?: Record<string, any>;
256
+ };
257
+ /**
258
+ * Chunk of a single generation. Used for streaming.
259
+ */
260
+ declare class GenerationChunk implements Generation {
261
+ text: string;
262
+ generationInfo?: Record<string, any>;
263
+ constructor(fields: GenerationChunkFields);
264
+ concat(chunk: GenerationChunk): GenerationChunk;
265
+ }
266
+ /**
267
+ * Contains all relevant information returned by an LLM.
268
+ */
269
+ type LLMResult = {
270
+ /**
271
+ * List of the things generated. Each input could have multiple {@link Generation | generations}, hence this is a list of lists.
272
+ */
273
+ generations: Generation[][];
274
+ /**
275
+ * Dictionary of arbitrary LLM-provider specific output.
276
+ */
277
+ llmOutput?: Record<string, any>;
278
+ /**
279
+ * Dictionary of run metadata
280
+ */
281
+ [RUN_KEY]?: Record<string, any>;
282
+ };
283
+ interface ChatGeneration extends Generation {
284
+ message: BaseMessage;
285
+ }
286
+ type ChatGenerationChunkFields = GenerationChunkFields & {
287
+ message: BaseMessageChunk;
288
+ };
289
+ declare class ChatGenerationChunk extends GenerationChunk implements ChatGeneration {
290
+ message: BaseMessageChunk;
291
+ constructor(fields: ChatGenerationChunkFields);
292
+ concat(chunk: ChatGenerationChunk): ChatGenerationChunk;
293
+ }
294
+
295
+ interface DocumentInput<Metadata extends Record<string, any> = Record<string, any>> {
296
+ pageContent: string;
297
+ metadata?: Metadata;
298
+ /**
299
+ * An optional identifier for the document.
300
+ *
301
+ * Ideally this should be unique across the document collection and formatted
302
+ * as a UUID, but this will not be enforced.
303
+ */
304
+ id?: string;
305
+ }
306
+ interface DocumentInterface<Metadata extends Record<string, any> = Record<string, any>> {
307
+ pageContent: string;
308
+ metadata: Metadata;
309
+ /**
310
+ * An optional identifier for the document.
311
+ *
312
+ * Ideally this should be unique across the document collection and formatted
313
+ * as a UUID, but this will not be enforced.
314
+ */
315
+ id?: string;
316
+ }
317
+ /**
318
+ * Interface for interacting with a document.
319
+ */
320
+ declare class Document<Metadata extends Record<string, any> = Record<string, any>> implements DocumentInput, DocumentInterface {
321
+ pageContent: string;
322
+ metadata: Metadata;
323
+ /**
324
+ * An optional identifier for the document.
325
+ *
326
+ * Ideally this should be unique across the document collection and formatted
327
+ * as a UUID, but this will not be enforced.
328
+ */
329
+ id?: string;
330
+ constructor(fields: DocumentInput<Metadata>);
331
+ }
332
+
333
+ type Error$1 = any;
334
+ /**
335
+ * Interface for the input parameters of the BaseCallbackHandler class. It
336
+ * allows to specify which types of events should be ignored by the
337
+ * callback handler.
338
+ */
339
+ interface BaseCallbackHandlerInput {
340
+ ignoreLLM?: boolean;
341
+ ignoreChain?: boolean;
342
+ ignoreAgent?: boolean;
343
+ ignoreRetriever?: boolean;
344
+ ignoreCustomEvent?: boolean;
345
+ _awaitHandler?: boolean;
346
+ raiseError?: boolean;
347
+ }
348
+ /**
349
+ * Interface for the indices of a new token produced by an LLM or Chat
350
+ * Model in streaming mode.
351
+ */
352
+ interface NewTokenIndices {
353
+ prompt: number;
354
+ completion: number;
355
+ }
356
+ type HandleLLMNewTokenCallbackFields = {
357
+ chunk?: GenerationChunk | ChatGenerationChunk;
358
+ };
359
+ /**
360
+ * Abstract class that provides a set of optional methods that can be
361
+ * overridden in derived classes to handle various events during the
362
+ * execution of a LangChain application.
363
+ */
364
+ declare abstract class BaseCallbackHandlerMethodsClass {
365
+ /**
366
+ * Called at the start of an LLM or Chat Model run, with the prompt(s)
367
+ * and the run ID.
368
+ */
369
+ handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
370
+ Promise<any> | any;
371
+ /**
372
+ * Called when an LLM/ChatModel in `streaming` mode produces a new token
373
+ */
374
+ handleLLMNewToken?(token: string,
375
+ /**
376
+ * idx.prompt is the index of the prompt that produced the token
377
+ * (if there are multiple prompts)
378
+ * idx.completion is the index of the completion that produced the token
379
+ * (if multiple completions per prompt are requested)
380
+ */
381
+ idx: NewTokenIndices, runId: string, parentRunId?: string, tags?: string[], fields?: HandleLLMNewTokenCallbackFields): // eslint-disable-next-line @typescript-eslint/no-explicit-any
382
+ Promise<any> | any;
383
+ /**
384
+ * Called if an LLM/ChatModel run encounters an error
385
+ */
386
+ handleLLMError?(err: Error$1, runId: string, parentRunId?: string, tags?: string[], extraParams?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
387
+ Promise<any> | any;
388
+ /**
389
+ * Called at the end of an LLM/ChatModel run, with the output and the run ID.
390
+ */
391
+ handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string, tags?: string[], extraParams?: Record<string, unknown>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
392
+ Promise<any> | any;
393
+ /**
394
+ * Called at the start of a Chat Model run, with the prompt(s)
395
+ * and the run ID.
396
+ */
397
+ handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
398
+ Promise<any> | any;
399
+ /**
400
+ * Called at the start of a Chain run, with the chain name and inputs
401
+ * and the run ID.
402
+ */
403
+ handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
404
+ Promise<any> | any;
405
+ /**
406
+ * Called if a Chain run encounters an error
407
+ */
408
+ handleChainError?(err: Error$1, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
409
+ inputs?: Record<string, unknown>;
410
+ }): // eslint-disable-next-line @typescript-eslint/no-explicit-any
411
+ Promise<any> | any;
412
+ /**
413
+ * Called at the end of a Chain run, with the outputs and the run ID.
414
+ */
415
+ handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], kwargs?: {
416
+ inputs?: Record<string, unknown>;
417
+ }): // eslint-disable-next-line @typescript-eslint/no-explicit-any
418
+ Promise<any> | any;
419
+ /**
420
+ * Called at the start of a Tool run, with the tool name and input
421
+ * and the run ID.
422
+ */
423
+ handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runName?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
424
+ Promise<any> | any;
425
+ /**
426
+ * Called if a Tool run encounters an error
427
+ */
428
+ handleToolError?(err: Error$1, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
429
+ Promise<any> | any;
430
+ /**
431
+ * Called at the end of a Tool run, with the tool output and the run ID.
432
+ */
433
+ handleToolEnd?(output: any, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
434
+ Promise<any> | any;
435
+ handleText?(text: string, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
436
+ /**
437
+ * Called when an agent is about to execute an action,
438
+ * with the action and the run ID.
439
+ */
440
+ handleAgentAction?(action: AgentAction, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
441
+ /**
442
+ * Called when an agent finishes execution, before it exits.
443
+ * with the final output and the run ID.
444
+ */
445
+ handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
446
+ handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
447
+ Promise<any> | any;
448
+ handleRetrieverEnd?(documents: DocumentInterface[], runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
449
+ Promise<any> | any;
450
+ handleRetrieverError?(err: Error$1, runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
451
+ Promise<any> | any;
452
+ handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[], metadata?: Record<string, any>): // eslint-disable-next-line @typescript-eslint/no-explicit-any
453
+ Promise<any> | any;
454
+ }
455
+ /**
456
+ * Base interface for callbacks. All methods are optional. If a method is not
457
+ * implemented, it will be ignored. If a method is implemented, it will be
458
+ * called at the appropriate time. All methods are called with the run ID of
459
+ * the LLM/ChatModel/Chain that is running, which is generated by the
460
+ * CallbackManager.
461
+ *
462
+ * @interface
463
+ */
464
+ type CallbackHandlerMethods = BaseCallbackHandlerMethodsClass;
465
+ /**
466
+ * Interface for handlers that can indicate a preference for streaming responses.
467
+ * When implemented, this allows the handler to signal whether it prefers to receive
468
+ * streaming responses from language models rather than complete responses.
469
+ */
470
+ interface CallbackHandlerPrefersStreaming {
471
+ readonly lc_prefer_streaming: boolean;
472
+ }
473
+ /**
474
+ * Abstract base class for creating callback handlers in the LangChain
475
+ * framework. It provides a set of optional methods that can be overridden
476
+ * in derived classes to handle various events during the execution of a
477
+ * LangChain application.
478
+ */
479
+ declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMethodsClass implements BaseCallbackHandlerInput, Serializable {
480
+ lc_serializable: boolean;
481
+ get lc_namespace(): ["langchain_core", "callbacks", string];
482
+ get lc_secrets(): {
483
+ [key: string]: string;
484
+ } | undefined;
485
+ get lc_attributes(): {
486
+ [key: string]: string;
487
+ } | undefined;
488
+ get lc_aliases(): {
489
+ [key: string]: string;
490
+ } | undefined;
491
+ get lc_serializable_keys(): string[] | undefined;
492
+ /**
493
+ * The name of the serializable. Override to provide an alias or
494
+ * to preserve the serialized module name in minified environments.
495
+ *
496
+ * Implemented as a static method to support loading logic.
497
+ */
498
+ static lc_name(): string;
499
+ /**
500
+ * The final serialized identifier for the module.
501
+ */
502
+ get lc_id(): string[];
503
+ lc_kwargs: SerializedFields;
504
+ abstract name: string;
505
+ ignoreLLM: boolean;
506
+ ignoreChain: boolean;
507
+ ignoreAgent: boolean;
508
+ ignoreRetriever: boolean;
509
+ ignoreCustomEvent: boolean;
510
+ raiseError: boolean;
511
+ awaitHandlers: boolean;
512
+ constructor(input?: BaseCallbackHandlerInput);
513
+ copy(): BaseCallbackHandler;
514
+ toJSON(): Serialized;
515
+ toJSONNotImplemented(): SerializedNotImplemented;
516
+ static fromMethods(methods: CallbackHandlerMethods): {
517
+ name: string;
518
+ lc_serializable: boolean;
519
+ readonly lc_namespace: ["langchain_core", "callbacks", string];
520
+ readonly lc_secrets: {
521
+ [key: string]: string;
522
+ } | undefined;
523
+ readonly lc_attributes: {
524
+ [key: string]: string;
525
+ } | undefined;
526
+ readonly lc_aliases: {
527
+ [key: string]: string;
528
+ } | undefined;
529
+ readonly lc_serializable_keys: string[] | undefined;
530
+ /**
531
+ * The final serialized identifier for the module.
532
+ */
533
+ readonly lc_id: string[];
534
+ lc_kwargs: SerializedFields;
535
+ ignoreLLM: boolean;
536
+ ignoreChain: boolean;
537
+ ignoreAgent: boolean;
538
+ ignoreRetriever: boolean;
539
+ ignoreCustomEvent: boolean;
540
+ raiseError: boolean;
541
+ awaitHandlers: boolean;
542
+ copy(): BaseCallbackHandler;
543
+ toJSON(): Serialized;
544
+ toJSONNotImplemented(): SerializedNotImplemented;
545
+ /**
546
+ * Called at the start of an LLM or Chat Model run, with the prompt(s)
547
+ * and the run ID.
548
+ */
549
+ handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runName?: string | undefined): any;
550
+ /**
551
+ * Called when an LLM/ChatModel in `streaming` mode produces a new token
552
+ */
553
+ handleLLMNewToken?(token: string, idx: NewTokenIndices, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, fields?: HandleLLMNewTokenCallbackFields | undefined): any;
554
+ /**
555
+ * Called if an LLM/ChatModel run encounters an error
556
+ */
557
+ handleLLMError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, extraParams?: Record<string, unknown> | undefined): any;
558
+ /**
559
+ * Called at the end of an LLM/ChatModel run, with the output and the run ID.
560
+ */
561
+ handleLLMEnd?(output: LLMResult, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, extraParams?: Record<string, unknown> | undefined): any;
562
+ /**
563
+ * Called at the start of a Chat Model run, with the prompt(s)
564
+ * and the run ID.
565
+ */
566
+ handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runName?: string | undefined): any;
567
+ /**
568
+ * Called at the start of a Chain run, with the chain name and inputs
569
+ * and the run ID.
570
+ */
571
+ handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined, runName?: string | undefined): any;
572
+ /**
573
+ * Called if a Chain run encounters an error
574
+ */
575
+ handleChainError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
576
+ inputs?: Record<string, unknown> | undefined;
577
+ } | undefined): any;
578
+ /**
579
+ * Called at the end of a Chain run, with the outputs and the run ID.
580
+ */
581
+ handleChainEnd?(outputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, kwargs?: {
582
+ inputs?: Record<string, unknown> | undefined;
583
+ } | undefined): any;
584
+ /**
585
+ * Called at the start of a Tool run, with the tool name and input
586
+ * and the run ID.
587
+ */
588
+ handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runName?: string | undefined): any;
589
+ /**
590
+ * Called if a Tool run encounters an error
591
+ */
592
+ handleToolError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
593
+ /**
594
+ * Called at the end of a Tool run, with the tool output and the run ID.
595
+ */
596
+ handleToolEnd?(output: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
597
+ handleText?(text: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
598
+ /**
599
+ * Called when an agent is about to execute an action,
600
+ * with the action and the run ID.
601
+ */
602
+ handleAgentAction?(action: AgentAction, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
603
+ /**
604
+ * Called when an agent finishes execution, before it exits.
605
+ * with the final output and the run ID.
606
+ */
607
+ handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
608
+ handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
609
+ handleRetrieverEnd?(documents: DocumentInterface<Record<string, any>>[], runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
610
+ handleRetrieverError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
611
+ handleCustomEvent?(eventName: string, data: any, runId: string, tags?: string[] | undefined, metadata?: Record<string, any> | undefined): any;
612
+ };
613
+ }
614
+
615
+ type KVMap = Record<string, any>;
616
+ type AttachmentData = Uint8Array | ArrayBuffer;
617
+ type AttachmentDescription = {
618
+ mimeType: string;
619
+ data: AttachmentData;
620
+ };
621
+ type Attachments = Record<string, [
622
+ string,
623
+ AttachmentData
624
+ ] | AttachmentDescription>;
625
+ /**
626
+ * A run can represent either a trace (root run)
627
+ * or a child run (~span).
628
+ */
629
+ interface BaseRun {
630
+ /** Optionally, a unique identifier for the run. */
631
+ id?: string;
632
+ /** A human-readable name for the run. */
633
+ name: string;
634
+ /** The epoch time at which the run started, if available. */
635
+ start_time?: number;
636
+ /** Specifies the type of run (tool, chain, llm, etc.). */
637
+ run_type: string;
638
+ /** The epoch time at which the run ended, if applicable. */
639
+ end_time?: number;
640
+ /** Any additional metadata or settings for the run. */
641
+ extra?: KVMap;
642
+ /** Error message, captured if the run faces any issues. */
643
+ error?: string;
644
+ /** Serialized state of the run for potential future use. */
645
+ serialized?: object;
646
+ /** Events like 'start', 'end' linked to the run. */
647
+ events?: KVMap[];
648
+ /** Inputs that were used to initiate the run. */
649
+ inputs: KVMap;
650
+ /** Outputs produced by the run, if any. */
651
+ outputs?: KVMap;
652
+ /** ID of an example that might be related to this run. */
653
+ reference_example_id?: string;
654
+ /** ID of a parent run, if this run is part of a larger operation. */
655
+ parent_run_id?: string;
656
+ /** Tags for further categorizing or annotating the run. */
657
+ tags?: string[];
658
+ /** Unique ID assigned to every run within this nested trace. **/
659
+ trace_id?: string;
660
+ /**
661
+ * The dotted order for the run.
662
+ *
663
+ * This is a string composed of {time}{run-uuid}.* so that a trace can be
664
+ * sorted in the order it was executed.
665
+ *
666
+ * Example:
667
+ * - Parent: 20230914T223155647Z1b64098b-4ab7-43f6-afee-992304f198d8
668
+ * - Children:
669
+ * - 20230914T223155647Z1b64098b-4ab7-43f6-afee-992304f198d8.20230914T223155649Z809ed3a2-0172-4f4d-8a02-a64e9b7a0f8a
670
+ * - 20230915T223155647Z1b64098b-4ab7-43f6-afee-992304f198d8.20230914T223155650Zc8d9f4c5-6c5a-4b2d-9b1c-3d9d7a7c5c7c
671
+ */
672
+ dotted_order?: string;
673
+ /**
674
+ * Attachments associated with the run.
675
+ * Each entry is a tuple of [mime_type, bytes]
676
+ */
677
+ attachments?: Attachments;
678
+ }
679
+
680
+ interface Run extends BaseRun {
681
+ id: string;
682
+ start_time: number;
683
+ execution_order: number;
684
+ child_runs: this[];
685
+ child_execution_order: number;
686
+ events: Array<{
687
+ name: string;
688
+ time: string;
689
+ kwargs?: Record<string, unknown>;
690
+ }>;
691
+ trace_id?: string;
692
+ dotted_order?: string;
693
+ }
694
+ declare abstract class BaseTracer extends BaseCallbackHandler {
695
+ protected runMap: Map<string, Run>;
696
+ constructor(_fields?: BaseCallbackHandlerInput);
697
+ copy(): this;
698
+ protected stringifyError(error: unknown): string;
699
+ protected abstract persistRun(run: Run): Promise<void>;
700
+ protected _addChildRun(parentRun: Run, childRun: Run): void;
701
+ _addRunToRunMap(run: Run): {
702
+ id: string;
703
+ start_time: number;
704
+ execution_order: number;
705
+ child_runs: Run[];
706
+ child_execution_order: number;
707
+ events: {
708
+ name: string;
709
+ time: string;
710
+ kwargs?: Record<string, unknown> | undefined;
711
+ }[];
712
+ trace_id?: string | undefined;
713
+ dotted_order?: string | undefined;
714
+ name: string;
715
+ run_type: string;
716
+ end_time?: number | undefined;
717
+ extra?: KVMap | undefined;
718
+ error?: string | undefined;
719
+ serialized?: object | undefined;
720
+ inputs: KVMap;
721
+ outputs?: KVMap | undefined;
722
+ reference_example_id?: string | undefined;
723
+ parent_run_id?: string | undefined;
724
+ tags?: string[] | undefined;
725
+ attachments?: Attachments | undefined;
726
+ };
727
+ protected _endTrace(run: Run): Promise<void>;
728
+ protected _getExecutionOrder(parentRunId: string | undefined): number;
729
+ /**
730
+ * Create and add a run to the run map for LLM start events.
731
+ * This must sometimes be done synchronously to avoid race conditions
732
+ * when callbacks are backgrounded, so we expose it as a separate method here.
733
+ */
734
+ _createRunForLLMStart(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): {
735
+ id: string;
736
+ start_time: number;
737
+ execution_order: number;
738
+ child_runs: Run[];
739
+ child_execution_order: number;
740
+ events: {
741
+ name: string;
742
+ time: string;
743
+ kwargs?: Record<string, unknown> | undefined;
744
+ }[];
745
+ trace_id?: string | undefined;
746
+ dotted_order?: string | undefined;
747
+ name: string;
748
+ run_type: string;
749
+ end_time?: number | undefined;
750
+ extra?: KVMap | undefined;
751
+ error?: string | undefined;
752
+ serialized?: object | undefined;
753
+ inputs: KVMap;
754
+ outputs?: KVMap | undefined;
755
+ reference_example_id?: string | undefined;
756
+ parent_run_id?: string | undefined;
757
+ tags?: string[] | undefined;
758
+ attachments?: Attachments | undefined;
759
+ };
760
+ handleLLMStart(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
761
+ /**
762
+ * Create and add a run to the run map for chat model start events.
763
+ * This must sometimes be done synchronously to avoid race conditions
764
+ * when callbacks are backgrounded, so we expose it as a separate method here.
765
+ */
766
+ _createRunForChatModelStart(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): {
767
+ id: string;
768
+ start_time: number;
769
+ execution_order: number;
770
+ child_runs: Run[];
771
+ child_execution_order: number;
772
+ events: {
773
+ name: string;
774
+ time: string;
775
+ kwargs?: Record<string, unknown> | undefined;
776
+ }[];
777
+ trace_id?: string | undefined;
778
+ dotted_order?: string | undefined;
779
+ name: string;
780
+ run_type: string;
781
+ end_time?: number | undefined;
782
+ extra?: KVMap | undefined;
783
+ error?: string | undefined;
784
+ serialized?: object | undefined;
785
+ inputs: KVMap;
786
+ outputs?: KVMap | undefined;
787
+ reference_example_id?: string | undefined;
788
+ parent_run_id?: string | undefined;
789
+ tags?: string[] | undefined;
790
+ attachments?: Attachments | undefined;
791
+ };
792
+ handleChatModelStart(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: KVMap, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
793
+ handleLLMEnd(output: LLMResult, runId: string, _parentRunId?: string, _tags?: string[], extraParams?: Record<string, unknown>): Promise<Run>;
794
+ handleLLMError(error: unknown, runId: string, _parentRunId?: string, _tags?: string[], extraParams?: Record<string, unknown>): Promise<Run>;
795
+ /**
796
+ * Create and add a run to the run map for chain start events.
797
+ * This must sometimes be done synchronously to avoid race conditions
798
+ * when callbacks are backgrounded, so we expose it as a separate method here.
799
+ */
800
+ _createRunForChainStart(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, runType?: string, name?: string): {
801
+ id: string;
802
+ start_time: number;
803
+ execution_order: number;
804
+ child_runs: Run[];
805
+ child_execution_order: number;
806
+ events: {
807
+ name: string;
808
+ time: string;
809
+ kwargs?: Record<string, unknown> | undefined;
810
+ }[];
811
+ trace_id?: string | undefined;
812
+ dotted_order?: string | undefined;
813
+ name: string;
814
+ run_type: string;
815
+ end_time?: number | undefined;
816
+ extra?: KVMap | undefined;
817
+ error?: string | undefined;
818
+ serialized?: object | undefined;
819
+ inputs: KVMap;
820
+ outputs?: KVMap | undefined;
821
+ reference_example_id?: string | undefined;
822
+ parent_run_id?: string | undefined;
823
+ tags?: string[] | undefined;
824
+ attachments?: Attachments | undefined;
825
+ };
826
+ handleChainStart(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, runType?: string, name?: string): Promise<Run>;
827
+ handleChainEnd(outputs: ChainValues, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
828
+ inputs?: Record<string, unknown>;
829
+ }): Promise<Run>;
830
+ handleChainError(error: unknown, runId: string, _parentRunId?: string, _tags?: string[], kwargs?: {
831
+ inputs?: Record<string, unknown>;
832
+ }): Promise<Run>;
833
+ /**
834
+ * Create and add a run to the run map for tool start events.
835
+ * This must sometimes be done synchronously to avoid race conditions
836
+ * when callbacks are backgrounded, so we expose it as a separate method here.
837
+ */
838
+ _createRunForToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): {
839
+ id: string;
840
+ start_time: number;
841
+ execution_order: number;
842
+ child_runs: Run[];
843
+ child_execution_order: number;
844
+ events: {
845
+ name: string;
846
+ time: string;
847
+ kwargs?: Record<string, unknown> | undefined;
848
+ }[];
849
+ trace_id?: string | undefined;
850
+ dotted_order?: string | undefined;
851
+ name: string;
852
+ run_type: string;
853
+ end_time?: number | undefined;
854
+ extra?: KVMap | undefined;
855
+ error?: string | undefined;
856
+ serialized?: object | undefined;
857
+ inputs: KVMap;
858
+ outputs?: KVMap | undefined;
859
+ reference_example_id?: string | undefined;
860
+ parent_run_id?: string | undefined;
861
+ tags?: string[] | undefined;
862
+ attachments?: Attachments | undefined;
863
+ };
864
+ handleToolStart(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
865
+ handleToolEnd(output: any, runId: string): Promise<Run>;
866
+ handleToolError(error: unknown, runId: string): Promise<Run>;
867
+ handleAgentAction(action: AgentAction, runId: string): Promise<void>;
868
+ handleAgentEnd(action: AgentFinish, runId: string): Promise<void>;
869
+ /**
870
+ * Create and add a run to the run map for retriever start events.
871
+ * This must sometimes be done synchronously to avoid race conditions
872
+ * when callbacks are backgrounded, so we expose it as a separate method here.
873
+ */
874
+ _createRunForRetrieverStart(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): {
875
+ id: string;
876
+ start_time: number;
877
+ execution_order: number;
878
+ child_runs: Run[];
879
+ child_execution_order: number;
880
+ events: {
881
+ name: string;
882
+ time: string;
883
+ kwargs?: Record<string, unknown> | undefined;
884
+ }[];
885
+ trace_id?: string | undefined;
886
+ dotted_order?: string | undefined;
887
+ name: string;
888
+ run_type: string;
889
+ end_time?: number | undefined;
890
+ extra?: KVMap | undefined;
891
+ error?: string | undefined;
892
+ serialized?: object | undefined;
893
+ inputs: KVMap;
894
+ outputs?: KVMap | undefined;
895
+ reference_example_id?: string | undefined;
896
+ parent_run_id?: string | undefined;
897
+ tags?: string[] | undefined;
898
+ attachments?: Attachments | undefined;
899
+ };
900
+ handleRetrieverStart(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: KVMap, name?: string): Promise<Run>;
901
+ handleRetrieverEnd(documents: Document<Record<string, unknown>>[], runId: string): Promise<Run>;
902
+ handleRetrieverError(error: unknown, runId: string): Promise<Run>;
903
+ handleText(text: string, runId: string): Promise<void>;
904
+ handleLLMNewToken(token: string, idx: NewTokenIndices, runId: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<Run>;
905
+ onRunCreate?(run: Run): void | Promise<void>;
906
+ onRunUpdate?(run: Run): void | Promise<void>;
907
+ onLLMStart?(run: Run): void | Promise<void>;
908
+ onLLMEnd?(run: Run): void | Promise<void>;
909
+ onLLMError?(run: Run): void | Promise<void>;
910
+ onChainStart?(run: Run): void | Promise<void>;
911
+ onChainEnd?(run: Run): void | Promise<void>;
912
+ onChainError?(run: Run): void | Promise<void>;
913
+ onToolStart?(run: Run): void | Promise<void>;
914
+ onToolEnd?(run: Run): void | Promise<void>;
915
+ onToolError?(run: Run): void | Promise<void>;
916
+ onAgentAction?(run: Run): void | Promise<void>;
917
+ onAgentEnd?(run: Run): void | Promise<void>;
918
+ onRetrieverStart?(run: Run): void | Promise<void>;
919
+ onRetrieverEnd?(run: Run): void | Promise<void>;
920
+ onRetrieverError?(run: Run): void | Promise<void>;
921
+ onText?(run: Run): void | Promise<void>;
922
+ onLLMNewToken?(run: Run, token: string, kwargs?: {
923
+ chunk: any;
924
+ }): void | Promise<void>;
925
+ }
926
+
927
+ type BaseCallbackManagerMethods = {
928
+ [K in keyof CallbackHandlerMethods]?: (...args: Parameters<Required<CallbackHandlerMethods>[K]>) => Promise<unknown>;
929
+ };
930
+ interface CallbackManagerOptions {
931
+ verbose?: boolean;
932
+ tracing?: boolean;
933
+ }
934
+ type Callbacks = CallbackManager | (BaseCallbackHandler | CallbackHandlerMethods)[];
935
+ interface BaseCallbackConfig {
936
+ /**
937
+ * Name for the tracer run for this call. Defaults to the name of the class.
938
+ */
939
+ runName?: string;
940
+ /**
941
+ * Tags for this call and any sub-calls (eg. a Chain calling an LLM).
942
+ * You can use these to filter calls.
943
+ */
944
+ tags?: string[];
945
+ /**
946
+ * Metadata for this call and any sub-calls (eg. a Chain calling an LLM).
947
+ * Keys should be strings, values should be JSON-serializable.
948
+ */
949
+ metadata?: Record<string, unknown>;
950
+ /**
951
+ * Callbacks for this call and any sub-calls (eg. a Chain calling an LLM).
952
+ * Tags are passed to all callbacks, metadata is passed to handle*Start callbacks.
953
+ */
954
+ callbacks?: Callbacks;
955
+ /**
956
+ * Unique identifier for the tracer run for this call. If not provided, a new UUID
957
+ * will be generated.
958
+ */
959
+ runId?: string;
960
+ }
961
+ /**
962
+ * Manage callbacks from different components of LangChain.
963
+ */
964
+ declare abstract class BaseCallbackManager {
965
+ abstract addHandler(handler: BaseCallbackHandler): void;
966
+ abstract removeHandler(handler: BaseCallbackHandler): void;
967
+ abstract setHandlers(handlers: BaseCallbackHandler[]): void;
968
+ setHandler(handler: BaseCallbackHandler): void;
969
+ }
970
+ /**
971
+ * Base class for run manager in LangChain.
972
+ */
973
+ declare class BaseRunManager {
974
+ readonly runId: string;
975
+ readonly handlers: BaseCallbackHandler[];
976
+ protected readonly inheritableHandlers: BaseCallbackHandler[];
977
+ protected readonly tags: string[];
978
+ protected readonly inheritableTags: string[];
979
+ protected readonly metadata: Record<string, unknown>;
980
+ protected readonly inheritableMetadata: Record<string, unknown>;
981
+ protected readonly _parentRunId?: string | undefined;
982
+ constructor(runId: string, handlers: BaseCallbackHandler[], inheritableHandlers: BaseCallbackHandler[], tags: string[], inheritableTags: string[], metadata: Record<string, unknown>, inheritableMetadata: Record<string, unknown>, _parentRunId?: string | undefined);
983
+ get parentRunId(): string | undefined;
984
+ handleText(text: string): Promise<void>;
985
+ handleCustomEvent(eventName: string, data: any, _runId?: string, _tags?: string[], _metadata?: Record<string, any>): Promise<void>;
986
+ }
987
+ /**
988
+ * Manages callbacks for retriever runs.
989
+ */
990
+ declare class CallbackManagerForRetrieverRun extends BaseRunManager implements BaseCallbackManagerMethods {
991
+ getChild(tag?: string): CallbackManager;
992
+ handleRetrieverEnd(documents: DocumentInterface[]): Promise<void>;
993
+ handleRetrieverError(err: Error | unknown): Promise<void>;
994
+ }
995
+ declare class CallbackManagerForLLMRun extends BaseRunManager implements BaseCallbackManagerMethods {
996
+ handleLLMNewToken(token: string, idx?: NewTokenIndices, _runId?: string, _parentRunId?: string, _tags?: string[], fields?: HandleLLMNewTokenCallbackFields): Promise<void>;
997
+ handleLLMError(err: Error | unknown, _runId?: string, _parentRunId?: string, _tags?: string[], extraParams?: Record<string, unknown>): Promise<void>;
998
+ handleLLMEnd(output: LLMResult, _runId?: string, _parentRunId?: string, _tags?: string[], extraParams?: Record<string, unknown>): Promise<void>;
999
+ }
1000
+ declare class CallbackManagerForChainRun extends BaseRunManager implements BaseCallbackManagerMethods {
1001
+ getChild(tag?: string): CallbackManager;
1002
+ handleChainError(err: Error | unknown, _runId?: string, _parentRunId?: string, _tags?: string[], kwargs?: {
1003
+ inputs?: Record<string, unknown>;
1004
+ }): Promise<void>;
1005
+ handleChainEnd(output: ChainValues, _runId?: string, _parentRunId?: string, _tags?: string[], kwargs?: {
1006
+ inputs?: Record<string, unknown>;
1007
+ }): Promise<void>;
1008
+ handleAgentAction(action: AgentAction): Promise<void>;
1009
+ handleAgentEnd(action: AgentFinish): Promise<void>;
1010
+ }
1011
+ declare class CallbackManagerForToolRun extends BaseRunManager implements BaseCallbackManagerMethods {
1012
+ getChild(tag?: string): CallbackManager;
1013
+ handleToolError(err: Error | unknown): Promise<void>;
1014
+ handleToolEnd(output: any): Promise<void>;
1015
+ }
1016
+ /**
1017
+ * @example
1018
+ * ```typescript
1019
+ * const prompt = PromptTemplate.fromTemplate("What is the answer to {question}?");
1020
+ *
1021
+ * // Example of using LLMChain with OpenAI and a simple prompt
1022
+ * const chain = new LLMChain({
1023
+ * llm: new ChatOpenAI({ temperature: 0.9 }),
1024
+ * prompt,
1025
+ * });
1026
+ *
1027
+ * // Running the chain with a single question
1028
+ * const result = await chain.call({
1029
+ * question: "What is the airspeed velocity of an unladen swallow?",
1030
+ * });
1031
+ * console.log("The answer is:", result);
1032
+ * ```
1033
+ */
1034
+ declare class CallbackManager extends BaseCallbackManager implements BaseCallbackManagerMethods {
1035
+ handlers: BaseCallbackHandler[];
1036
+ inheritableHandlers: BaseCallbackHandler[];
1037
+ tags: string[];
1038
+ inheritableTags: string[];
1039
+ metadata: Record<string, unknown>;
1040
+ inheritableMetadata: Record<string, unknown>;
1041
+ name: string;
1042
+ _parentRunId?: string;
1043
+ constructor(parentRunId?: string, options?: {
1044
+ handlers?: BaseCallbackHandler[];
1045
+ inheritableHandlers?: BaseCallbackHandler[];
1046
+ tags?: string[];
1047
+ inheritableTags?: string[];
1048
+ metadata?: Record<string, unknown>;
1049
+ inheritableMetadata?: Record<string, unknown>;
1050
+ });
1051
+ /**
1052
+ * Gets the parent run ID, if any.
1053
+ *
1054
+ * @returns The parent run ID.
1055
+ */
1056
+ getParentRunId(): string | undefined;
1057
+ handleLLMStart(llm: Serialized, prompts: string[], runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
1058
+ handleChatModelStart(llm: Serialized, messages: BaseMessage[][], runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
1059
+ handleChainStart(chain: Serialized, inputs: ChainValues, runId?: string, runType?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForChainRun>;
1060
+ handleToolStart(tool: Serialized, input: string, runId?: string, _parentRunId?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForToolRun>;
1061
+ handleRetrieverStart(retriever: Serialized, query: string, runId?: string, _parentRunId?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForRetrieverRun>;
1062
+ handleCustomEvent?(eventName: string, data: any, runId: string, _tags?: string[], _metadata?: Record<string, any>): Promise<any>;
1063
+ addHandler(handler: BaseCallbackHandler, inherit?: boolean): void;
1064
+ removeHandler(handler: BaseCallbackHandler): void;
1065
+ setHandlers(handlers: BaseCallbackHandler[], inherit?: boolean): void;
1066
+ addTags(tags: string[], inherit?: boolean): void;
1067
+ removeTags(tags: string[]): void;
1068
+ addMetadata(metadata: Record<string, unknown>, inherit?: boolean): void;
1069
+ removeMetadata(metadata: Record<string, unknown>): void;
1070
+ copy(additionalHandlers?: BaseCallbackHandler[], inherit?: boolean): CallbackManager;
1071
+ static fromHandlers(handlers: CallbackHandlerMethods): CallbackManager;
1072
+ static configure(inheritableHandlers?: Callbacks, localHandlers?: Callbacks, inheritableTags?: string[], localTags?: string[], inheritableMetadata?: Record<string, unknown>, localMetadata?: Record<string, unknown>, options?: CallbackManagerOptions): CallbackManager | undefined;
1073
+ static _configureSync(inheritableHandlers?: Callbacks, localHandlers?: Callbacks, inheritableTags?: string[], localTags?: string[], inheritableMetadata?: Record<string, unknown>, localMetadata?: Record<string, unknown>, options?: CallbackManagerOptions): CallbackManager | undefined;
1074
+ }
1075
+
1076
+ /**
1077
+ * A call to a tool.
1078
+ * @property {string} name - The name of the tool to be called
1079
+ * @property {Record<string, any>} args - The arguments to the tool call
1080
+ * @property {string} [id] - If provided, an identifier associated with the tool call
1081
+ */
1082
+ type ToolCall = {
1083
+ name: string;
1084
+ args: Record<string, any>;
1085
+ id?: string;
1086
+ type?: "tool_call";
1087
+ };
1088
+
1089
+ type IterableReadableStreamInterface<T> = ReadableStream<T> & AsyncIterable<T>;
1090
+
1091
+ type RunnableBatchOptions = {
1092
+ /** @deprecated Pass in via the standard runnable config object instead */
1093
+ maxConcurrency?: number;
1094
+ returnExceptions?: boolean;
1095
+ };
1096
+ type RunnableIOSchema = {
1097
+ name?: string;
1098
+ schema: z.ZodType;
1099
+ };
1100
+ /**
1101
+ * Base interface implemented by all runnables.
1102
+ * Used for cross-compatibility between different versions of LangChain core.
1103
+ *
1104
+ * Should not change on patch releases.
1105
+ */
1106
+ interface RunnableInterface<RunInput = any, RunOutput = any, CallOptions extends RunnableConfig = RunnableConfig> extends SerializableInterface {
1107
+ lc_serializable: boolean;
1108
+ invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
1109
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1110
+ returnExceptions?: false;
1111
+ }): Promise<RunOutput[]>;
1112
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1113
+ returnExceptions: true;
1114
+ }): Promise<(RunOutput | Error)[]>;
1115
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
1116
+ stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStreamInterface<RunOutput>>;
1117
+ transform(generator: AsyncGenerator<RunInput>, options: Partial<CallOptions>): AsyncGenerator<RunOutput>;
1118
+ getName(suffix?: string): string;
1119
+ }
1120
+ interface Edge {
1121
+ source: string;
1122
+ target: string;
1123
+ data?: string;
1124
+ conditional?: boolean;
1125
+ }
1126
+ interface Node {
1127
+ id: string;
1128
+ name: string;
1129
+ data: RunnableIOSchema | RunnableInterface;
1130
+ metadata?: Record<string, any>;
1131
+ }
1132
+ interface RunnableConfig<ConfigurableFieldType extends Record<string, any> = Record<string, any>> extends BaseCallbackConfig {
1133
+ /**
1134
+ * Runtime values for attributes previously made configurable on this Runnable,
1135
+ * or sub-Runnables.
1136
+ */
1137
+ configurable?: ConfigurableFieldType;
1138
+ /**
1139
+ * Maximum number of times a call can recurse. If not provided, defaults to 25.
1140
+ */
1141
+ recursionLimit?: number;
1142
+ /** Maximum number of parallel calls to make. */
1143
+ maxConcurrency?: number;
1144
+ /**
1145
+ * Timeout for this call in milliseconds.
1146
+ */
1147
+ timeout?: number;
1148
+ /**
1149
+ * Abort signal for this call.
1150
+ * If provided, the call will be aborted when the signal is aborted.
1151
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
1152
+ */
1153
+ signal?: AbortSignal;
1154
+ }
1155
+
1156
+ type Operation = AddOperation<any> | RemoveOperation | ReplaceOperation<any> | MoveOperation | CopyOperation | TestOperation<any> | GetOperation<any>;
1157
+ interface BaseOperation {
1158
+ path: string;
1159
+ }
1160
+ interface AddOperation<T> extends BaseOperation {
1161
+ op: "add";
1162
+ value: T;
1163
+ }
1164
+ interface RemoveOperation extends BaseOperation {
1165
+ op: "remove";
1166
+ }
1167
+ interface ReplaceOperation<T> extends BaseOperation {
1168
+ op: "replace";
1169
+ value: T;
1170
+ }
1171
+ interface MoveOperation extends BaseOperation {
1172
+ op: "move";
1173
+ from: string;
1174
+ }
1175
+ interface CopyOperation extends BaseOperation {
1176
+ op: "copy";
1177
+ from: string;
1178
+ }
1179
+ interface TestOperation<T> extends BaseOperation {
1180
+ op: "test";
1181
+ value: T;
1182
+ }
1183
+ interface GetOperation<T> extends BaseOperation {
1184
+ op: "_get";
1185
+ value: T;
1186
+ }
1187
+
1188
+ declare class IterableReadableStream<T> extends ReadableStream<T> implements IterableReadableStreamInterface<T> {
1189
+ reader: ReadableStreamDefaultReader<T>;
1190
+ ensureReader(): void;
1191
+ next(): Promise<IteratorResult<T>>;
1192
+ return(): Promise<IteratorResult<T>>;
1193
+ throw(e: any): Promise<IteratorResult<T>>;
1194
+ [Symbol.asyncIterator](): this;
1195
+ [Symbol.asyncDispose](): Promise<void>;
1196
+ static fromReadableStream<T>(stream: ReadableStream<T>): IterableReadableStream<T>;
1197
+ static fromAsyncGenerator<T>(generator: AsyncGenerator<T>): IterableReadableStream<T>;
1198
+ }
1199
+
1200
+ /**
1201
+ * Data associated with a StreamEvent.
1202
+ */
1203
+ type StreamEventData = {
1204
+ /**
1205
+ * The input passed to the runnable that generated the event.
1206
+ * Inputs will sometimes be available at the *START* of the runnable, and
1207
+ * sometimes at the *END* of the runnable.
1208
+ * If a runnable is able to stream its inputs, then its input by definition
1209
+ * won't be known until the *END* of the runnable when it has finished streaming
1210
+ * its inputs.
1211
+ */
1212
+ input?: any;
1213
+ /**
1214
+ * The output of the runnable that generated the event.
1215
+ * Outputs will only be available at the *END* of the runnable.
1216
+ * For most runnables, this field can be inferred from the `chunk` field,
1217
+ * though there might be some exceptions for special cased runnables (e.g., like
1218
+ * chat models), which may return more information.
1219
+ */
1220
+ output?: any;
1221
+ /**
1222
+ * A streaming chunk from the output that generated the event.
1223
+ * chunks support addition in general, and adding them up should result
1224
+ * in the output of the runnable that generated the event.
1225
+ */
1226
+ chunk?: any;
1227
+ };
1228
+ /**
1229
+ * A streaming event.
1230
+ *
1231
+ * Schema of a streaming event which is produced from the streamEvents method.
1232
+ */
1233
+ type StreamEvent = {
1234
+ /**
1235
+ * Event names are of the format: on_[runnable_type]_(start|stream|end).
1236
+ *
1237
+ * Runnable types are one of:
1238
+ * - llm - used by non chat models
1239
+ * - chat_model - used by chat models
1240
+ * - prompt -- e.g., ChatPromptTemplate
1241
+ * - tool -- LangChain tools
1242
+ * - chain - most Runnables are of this type
1243
+ *
1244
+ * Further, the events are categorized as one of:
1245
+ * - start - when the runnable starts
1246
+ * - stream - when the runnable is streaming
1247
+ * - end - when the runnable ends
1248
+ *
1249
+ * start, stream and end are associated with slightly different `data` payload.
1250
+ *
1251
+ * Please see the documentation for `EventData` for more details.
1252
+ */
1253
+ event: string;
1254
+ /** The name of the runnable that generated the event. */
1255
+ name: string;
1256
+ /**
1257
+ * An randomly generated ID to keep track of the execution of the given runnable.
1258
+ *
1259
+ * Each child runnable that gets invoked as part of the execution of a parent runnable
1260
+ * is assigned its own unique ID.
1261
+ */
1262
+ run_id: string;
1263
+ /**
1264
+ * Tags associated with the runnable that generated this event.
1265
+ * Tags are always inherited from parent runnables.
1266
+ */
1267
+ tags?: string[];
1268
+ /** Metadata associated with the runnable that generated this event. */
1269
+ metadata: Record<string, any>;
1270
+ /**
1271
+ * Event data.
1272
+ *
1273
+ * The contents of the event data depend on the event type.
1274
+ */
1275
+ data: StreamEventData;
1276
+ };
1277
+ interface EventStreamCallbackHandlerInput extends BaseCallbackHandlerInput {
1278
+ autoClose?: boolean;
1279
+ includeNames?: string[];
1280
+ includeTypes?: string[];
1281
+ includeTags?: string[];
1282
+ excludeNames?: string[];
1283
+ excludeTypes?: string[];
1284
+ excludeTags?: string[];
1285
+ }
1286
+
1287
+ /**
1288
+ * Interface that represents the structure of a log entry in the
1289
+ * `LogStreamCallbackHandler`.
1290
+ */
1291
+ type LogEntry = {
1292
+ /** ID of the sub-run. */
1293
+ id: string;
1294
+ /** Name of the object being run. */
1295
+ name: string;
1296
+ /** Type of the object being run, eg. prompt, chain, llm, etc. */
1297
+ type: string;
1298
+ /** List of tags for the run. */
1299
+ tags: string[];
1300
+ /** Key-value pairs of metadata for the run. */
1301
+ metadata: Record<string, any>;
1302
+ /** ISO-8601 timestamp of when the run started. */
1303
+ start_time: string;
1304
+ /** List of general output chunks streamed by this run. */
1305
+ streamed_output: any[];
1306
+ /** List of LLM tokens streamed by this run, if applicable. */
1307
+ streamed_output_str: string[];
1308
+ /** Inputs to this run. Not available currently via streamLog. */
1309
+ inputs?: any;
1310
+ /** Final output of this run. Only available after the run has finished successfully. */
1311
+ final_output?: any;
1312
+ /** ISO-8601 timestamp of when the run ended. Only available after the run has finished. */
1313
+ end_time?: string;
1314
+ };
1315
+ type RunState = {
1316
+ /** ID of the sub-run. */
1317
+ id: string;
1318
+ /** List of output chunks streamed by Runnable.stream() */
1319
+ streamed_output: any[];
1320
+ /** Final output of the run, usually the result of aggregating streamed_output. Only available after the run has finished successfully. */
1321
+ final_output?: any;
1322
+ /**
1323
+ * List of sub-runs contained in this run, if any, in the order they were started.
1324
+ * If filters were supplied, this list will contain only the runs that matched the filters.
1325
+ */
1326
+ logs: Record<string, LogEntry>;
1327
+ /** Name of the object being run. */
1328
+ name: string;
1329
+ /** Type of the object being run, eg. prompt, chain, llm, etc. */
1330
+ type: string;
1331
+ };
1332
+ /**
1333
+ * List of jsonpatch JSONPatchOperations, which describe how to create the run state
1334
+ * from an empty dict. This is the minimal representation of the log, designed to
1335
+ * be serialized as JSON and sent over the wire to reconstruct the log on the other
1336
+ * side. Reconstruction of the state can be done with any jsonpatch-compliant library,
1337
+ * see https://jsonpatch.com for more information.
1338
+ */
1339
+ declare class RunLogPatch {
1340
+ ops: Operation[];
1341
+ constructor(fields: {
1342
+ ops?: Operation[];
1343
+ });
1344
+ concat(other: RunLogPatch): RunLog;
1345
+ }
1346
+ declare class RunLog extends RunLogPatch {
1347
+ state: RunState;
1348
+ constructor(fields: {
1349
+ ops?: Operation[];
1350
+ state: RunState;
1351
+ });
1352
+ concat(other: RunLogPatch): RunLog;
1353
+ static fromRunLogPatch(patch: RunLogPatch): RunLog;
1354
+ }
1355
+ type SchemaFormat = "original" | "streaming_events";
1356
+ interface LogStreamCallbackHandlerInput extends BaseCallbackHandlerInput {
1357
+ autoClose?: boolean;
1358
+ includeNames?: string[];
1359
+ includeTypes?: string[];
1360
+ includeTags?: string[];
1361
+ excludeNames?: string[];
1362
+ excludeTypes?: string[];
1363
+ excludeTags?: string[];
1364
+ _schemaFormat?: SchemaFormat;
1365
+ }
1366
+ /**
1367
+ * Class that extends the `BaseTracer` class from the
1368
+ * `langchain.callbacks.tracers.base` module. It represents a callback
1369
+ * handler that logs the execution of runs and emits `RunLog` instances to a
1370
+ * `RunLogStream`.
1371
+ */
1372
+ declare class LogStreamCallbackHandler extends BaseTracer implements CallbackHandlerPrefersStreaming {
1373
+ protected autoClose: boolean;
1374
+ protected includeNames?: string[];
1375
+ protected includeTypes?: string[];
1376
+ protected includeTags?: string[];
1377
+ protected excludeNames?: string[];
1378
+ protected excludeTypes?: string[];
1379
+ protected excludeTags?: string[];
1380
+ protected _schemaFormat: SchemaFormat;
1381
+ protected rootId?: string;
1382
+ private keyMapByRunId;
1383
+ private counterMapByRunName;
1384
+ protected transformStream: TransformStream;
1385
+ writer: WritableStreamDefaultWriter;
1386
+ receiveStream: IterableReadableStream<RunLogPatch>;
1387
+ name: string;
1388
+ lc_prefer_streaming: boolean;
1389
+ constructor(fields?: LogStreamCallbackHandlerInput);
1390
+ [Symbol.asyncIterator](): IterableReadableStream<RunLogPatch>;
1391
+ protected persistRun(_run: Run): Promise<void>;
1392
+ _includeRun(run: Run): boolean;
1393
+ tapOutputIterable<T>(runId: string, output: AsyncGenerator<T>): AsyncGenerator<T>;
1394
+ onRunCreate(run: Run): Promise<void>;
1395
+ onRunUpdate(run: Run): Promise<void>;
1396
+ onLLMNewToken(run: Run, token: string, kwargs?: HandleLLMNewTokenCallbackFields): Promise<void>;
1397
+ }
1398
+
1399
+ declare class Graph {
1400
+ nodes: Record<string, Node>;
1401
+ edges: Edge[];
1402
+ constructor(params?: {
1403
+ nodes: Record<string, Node>;
1404
+ edges: Edge[];
1405
+ });
1406
+ toJSON(): Record<string, any>;
1407
+ addNode(data: RunnableInterface | RunnableIOSchema, id?: string, metadata?: Record<string, any>): Node;
1408
+ removeNode(node: Node): void;
1409
+ addEdge(source: Node, target: Node, data?: string, conditional?: boolean): Edge;
1410
+ firstNode(): Node | undefined;
1411
+ lastNode(): Node | undefined;
1412
+ /**
1413
+ * Add all nodes and edges from another graph.
1414
+ * Note this doesn't check for duplicates, nor does it connect the graphs.
1415
+ */
1416
+ extend(graph: Graph, prefix?: string): ({
1417
+ id: string;
1418
+ data: RunnableIOSchema | RunnableInterface<any, any, RunnableConfig<Record<string, any>>>;
1419
+ } | undefined)[];
1420
+ trimFirstNode(): void;
1421
+ trimLastNode(): void;
1422
+ /**
1423
+ * Return a new graph with all nodes re-identified,
1424
+ * using their unique, readable names where possible.
1425
+ */
1426
+ reid(): Graph;
1427
+ drawMermaid(params?: {
1428
+ withStyles?: boolean;
1429
+ curveStyle?: string;
1430
+ nodeColors?: Record<string, string>;
1431
+ wrapLabelNWords?: number;
1432
+ }): string;
1433
+ drawMermaidPng(params?: {
1434
+ withStyles?: boolean;
1435
+ curveStyle?: string;
1436
+ nodeColors?: Record<string, string>;
1437
+ wrapLabelNWords?: number;
1438
+ backgroundColor?: string;
1439
+ }): Promise<Blob>;
1440
+ }
1441
+
1442
+ type RunnableFunc<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = (input: RunInput, options: CallOptions | Record<string, any> | (Record<string, any> & CallOptions)) => RunOutput | Promise<RunOutput>;
1443
+ type RunnableMapLike<RunInput, RunOutput> = {
1444
+ [K in keyof RunOutput]: RunnableLike<RunInput, RunOutput[K]>;
1445
+ };
1446
+ type RunnableLike<RunInput = any, RunOutput = any, CallOptions extends RunnableConfig = RunnableConfig> = RunnableInterface<RunInput, RunOutput, CallOptions> | RunnableFunc<RunInput, RunOutput, CallOptions> | RunnableMapLike<RunInput, RunOutput>;
1447
+ type RunnableRetryFailedAttemptHandler = (error: any, input: any) => any;
1448
+ /**
1449
+ * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
1450
+ * transformed.
1451
+ */
1452
+ declare abstract class Runnable<RunInput = any, RunOutput = any, CallOptions extends RunnableConfig = RunnableConfig> extends Serializable implements RunnableInterface<RunInput, RunOutput, CallOptions> {
1453
+ protected lc_runnable: boolean;
1454
+ name?: string;
1455
+ getName(suffix?: string): string;
1456
+ abstract invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
1457
+ /**
1458
+ * Bind arguments to a Runnable, returning a new Runnable.
1459
+ * @param kwargs
1460
+ * @returns A new RunnableBinding that, when invoked, will apply the bound args.
1461
+ */
1462
+ bind(kwargs: Partial<CallOptions>): Runnable<RunInput, RunOutput, CallOptions>;
1463
+ /**
1464
+ * Return a new Runnable that maps a list of inputs to a list of outputs,
1465
+ * by calling invoke() with each input.
1466
+ */
1467
+ map(): Runnable<RunInput[], RunOutput[], CallOptions>;
1468
+ /**
1469
+ * Add retry logic to an existing runnable.
1470
+ * @param kwargs
1471
+ * @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
1472
+ */
1473
+ withRetry(fields?: {
1474
+ stopAfterAttempt?: number;
1475
+ onFailedAttempt?: RunnableRetryFailedAttemptHandler;
1476
+ }): RunnableRetry<RunInput, RunOutput, CallOptions>;
1477
+ /**
1478
+ * Bind config to a Runnable, returning a new Runnable.
1479
+ * @param config New configuration parameters to attach to the new runnable.
1480
+ * @returns A new RunnableBinding with a config matching what's passed.
1481
+ */
1482
+ withConfig(config: RunnableConfig): Runnable<RunInput, RunOutput, CallOptions>;
1483
+ /**
1484
+ * Create a new runnable from the current one that will try invoking
1485
+ * other passed fallback runnables if the initial invocation fails.
1486
+ * @param fields.fallbacks Other runnables to call if the runnable errors.
1487
+ * @returns A new RunnableWithFallbacks.
1488
+ */
1489
+ withFallbacks(fields: {
1490
+ fallbacks: Runnable<RunInput, RunOutput>[];
1491
+ } | Runnable<RunInput, RunOutput>[]): RunnableWithFallbacks<RunInput, RunOutput>;
1492
+ protected _getOptionsList<O extends CallOptions & {
1493
+ runType?: string;
1494
+ }>(options: Partial<O> | Partial<O>[], length?: number): Partial<O>[];
1495
+ /**
1496
+ * Default implementation of batch, which calls invoke N times.
1497
+ * Subclasses should override this method if they can batch more efficiently.
1498
+ * @param inputs Array of inputs to each batch call.
1499
+ * @param options Either a single call options object to apply to each batch call or an array for each call.
1500
+ * @param batchOptions.returnExceptions Whether to return errors rather than throwing on the first one
1501
+ * @returns An array of RunOutputs, or mixed RunOutputs and errors if batchOptions.returnExceptions is set
1502
+ */
1503
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1504
+ returnExceptions?: false;
1505
+ }): Promise<RunOutput[]>;
1506
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1507
+ returnExceptions: true;
1508
+ }): Promise<(RunOutput | Error)[]>;
1509
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
1510
+ /**
1511
+ * Default streaming implementation.
1512
+ * Subclasses should override this method if they support streaming output.
1513
+ * @param input
1514
+ * @param options
1515
+ */
1516
+ _streamIterator(input: RunInput, options?: Partial<CallOptions>): AsyncGenerator<RunOutput>;
1517
+ /**
1518
+ * Stream output in chunks.
1519
+ * @param input
1520
+ * @param options
1521
+ * @returns A readable stream that is also an iterable.
1522
+ */
1523
+ stream(input: RunInput, options?: Partial<CallOptions>): Promise<IterableReadableStream<RunOutput>>;
1524
+ protected _separateRunnableConfigFromCallOptions(options?: Partial<CallOptions>): [RunnableConfig, Omit<Partial<CallOptions>, keyof RunnableConfig>];
1525
+ protected _callWithConfig<T extends RunInput>(func: ((input: T) => Promise<RunOutput>) | ((input: T, config?: Partial<CallOptions>, runManager?: CallbackManagerForChainRun) => Promise<RunOutput>), input: T, options?: Partial<CallOptions> & {
1526
+ runType?: string;
1527
+ }): Promise<RunOutput>;
1528
+ /**
1529
+ * Internal method that handles batching and configuration for a runnable
1530
+ * It takes a function, input values, and optional configuration, and
1531
+ * returns a promise that resolves to the output values.
1532
+ * @param func The function to be executed for each input value.
1533
+ * @param input The input values to be processed.
1534
+ * @param config Optional configuration for the function execution.
1535
+ * @returns A promise that resolves to the output values.
1536
+ */
1537
+ _batchWithConfig<T extends RunInput>(func: (inputs: T[], options?: Partial<CallOptions>[], runManagers?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions) => Promise<(RunOutput | Error)[]>, inputs: T[], options?: Partial<CallOptions & {
1538
+ runType?: string;
1539
+ }> | Partial<CallOptions & {
1540
+ runType?: string;
1541
+ }>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
1542
+ /**
1543
+ * Helper method to transform an Iterator of Input values into an Iterator of
1544
+ * Output values, with callbacks.
1545
+ * Use this to implement `stream()` or `transform()` in Runnable subclasses.
1546
+ */
1547
+ protected _transformStreamWithConfig<I extends RunInput, O extends RunOutput>(inputGenerator: AsyncGenerator<I>, transformer: (generator: AsyncGenerator<I>, runManager?: CallbackManagerForChainRun, options?: Partial<CallOptions>) => AsyncGenerator<O>, options?: Partial<CallOptions> & {
1548
+ runType?: string;
1549
+ }): AsyncGenerator<O>;
1550
+ getGraph(_?: RunnableConfig): Graph;
1551
+ /**
1552
+ * Create a new runnable sequence that runs each individual runnable in series,
1553
+ * piping the output of one runnable into another runnable or runnable-like.
1554
+ * @param coerceable A runnable, function, or object whose values are functions or runnables.
1555
+ * @returns A new runnable sequence.
1556
+ */
1557
+ pipe<NewRunOutput>(coerceable: RunnableLike<RunOutput, NewRunOutput>): Runnable<RunInput, Exclude<NewRunOutput, Error>>;
1558
+ /**
1559
+ * Pick keys from the dict output of this runnable. Returns a new runnable.
1560
+ */
1561
+ pick(keys: string | string[]): Runnable;
1562
+ /**
1563
+ * Assigns new fields to the dict output of this runnable. Returns a new runnable.
1564
+ */
1565
+ assign(mapping: RunnableMapLike<Record<string, unknown>, Record<string, unknown>>): Runnable;
1566
+ /**
1567
+ * Default implementation of transform, which buffers input and then calls stream.
1568
+ * Subclasses should override this method if they can start producing output while
1569
+ * input is still being generated.
1570
+ * @param generator
1571
+ * @param options
1572
+ */
1573
+ transform(generator: AsyncGenerator<RunInput>, options: Partial<CallOptions>): AsyncGenerator<RunOutput>;
1574
+ /**
1575
+ * Stream all output from a runnable, as reported to the callback system.
1576
+ * This includes all inner runs of LLMs, Retrievers, Tools, etc.
1577
+ * Output is streamed as Log objects, which include a list of
1578
+ * jsonpatch ops that describe how the state of the run has changed in each
1579
+ * step, and the final state of the run.
1580
+ * The jsonpatch ops can be applied in order to construct state.
1581
+ * @param input
1582
+ * @param options
1583
+ * @param streamOptions
1584
+ */
1585
+ streamLog(input: RunInput, options?: Partial<CallOptions>, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): AsyncGenerator<RunLogPatch>;
1586
+ protected _streamLog(input: RunInput, logStreamCallbackHandler: LogStreamCallbackHandler, config: Partial<CallOptions>): AsyncGenerator<RunLogPatch>;
1587
+ /**
1588
+ * Generate a stream of events emitted by the internal steps of the runnable.
1589
+ *
1590
+ * Use to create an iterator over StreamEvents that provide real-time information
1591
+ * about the progress of the runnable, including StreamEvents from intermediate
1592
+ * results.
1593
+ *
1594
+ * A StreamEvent is a dictionary with the following schema:
1595
+ *
1596
+ * - `event`: string - Event names are of the format: on_[runnable_type]_(start|stream|end).
1597
+ * - `name`: string - The name of the runnable that generated the event.
1598
+ * - `run_id`: string - Randomly generated ID associated with the given execution of
1599
+ * the runnable that emitted the event. A child runnable that gets invoked as part of the execution of a
1600
+ * parent runnable is assigned its own unique ID.
1601
+ * - `tags`: string[] - The tags of the runnable that generated the event.
1602
+ * - `metadata`: Record<string, any> - The metadata of the runnable that generated the event.
1603
+ * - `data`: Record<string, any>
1604
+ *
1605
+ * Below is a table that illustrates some events that might be emitted by various
1606
+ * chains. Metadata fields have been omitted from the table for brevity.
1607
+ * Chain definitions have been included after the table.
1608
+ *
1609
+ * **ATTENTION** This reference table is for the V2 version of the schema.
1610
+ *
1611
+ * ```md
1612
+ * +----------------------+-----------------------------+------------------------------------------+
1613
+ * | event | input | output/chunk |
1614
+ * +======================+=============================+==========================================+
1615
+ * | on_chat_model_start | {"messages": BaseMessage[]} | |
1616
+ * +----------------------+-----------------------------+------------------------------------------+
1617
+ * | on_chat_model_stream | | AIMessageChunk("hello") |
1618
+ * +----------------------+-----------------------------+------------------------------------------+
1619
+ * | on_chat_model_end | {"messages": BaseMessage[]} | AIMessageChunk("hello world") |
1620
+ * +----------------------+-----------------------------+------------------------------------------+
1621
+ * | on_llm_start | {'input': 'hello'} | |
1622
+ * +----------------------+-----------------------------+------------------------------------------+
1623
+ * | on_llm_stream | | 'Hello' |
1624
+ * +----------------------+-----------------------------+------------------------------------------+
1625
+ * | on_llm_end | 'Hello human!' | |
1626
+ * +----------------------+-----------------------------+------------------------------------------+
1627
+ * | on_chain_start | | |
1628
+ * +----------------------+-----------------------------+------------------------------------------+
1629
+ * | on_chain_stream | | "hello world!" |
1630
+ * +----------------------+-----------------------------+------------------------------------------+
1631
+ * | on_chain_end | [Document(...)] | "hello world!, goodbye world!" |
1632
+ * +----------------------+-----------------------------+------------------------------------------+
1633
+ * | on_tool_start | {"x": 1, "y": "2"} | |
1634
+ * +----------------------+-----------------------------+------------------------------------------+
1635
+ * | on_tool_end | | {"x": 1, "y": "2"} |
1636
+ * +----------------------+-----------------------------+------------------------------------------+
1637
+ * | on_retriever_start | {"query": "hello"} | |
1638
+ * +----------------------+-----------------------------+------------------------------------------+
1639
+ * | on_retriever_end | {"query": "hello"} | [Document(...), ..] |
1640
+ * +----------------------+-----------------------------+------------------------------------------+
1641
+ * | on_prompt_start | {"question": "hello"} | |
1642
+ * +----------------------+-----------------------------+------------------------------------------+
1643
+ * | on_prompt_end | {"question": "hello"} | ChatPromptValue(messages: BaseMessage[]) |
1644
+ * +----------------------+-----------------------------+------------------------------------------+
1645
+ * ```
1646
+ *
1647
+ * The "on_chain_*" events are the default for Runnables that don't fit one of the above categories.
1648
+ *
1649
+ * In addition to the standard events above, users can also dispatch custom events.
1650
+ *
1651
+ * Custom events will be only be surfaced with in the `v2` version of the API!
1652
+ *
1653
+ * A custom event has following format:
1654
+ *
1655
+ * ```md
1656
+ * +-----------+------+------------------------------------------------------------+
1657
+ * | Attribute | Type | Description |
1658
+ * +===========+======+============================================================+
1659
+ * | name | str | A user defined name for the event. |
1660
+ * +-----------+------+------------------------------------------------------------+
1661
+ * | data | Any | The data associated with the event. This can be anything. |
1662
+ * +-----------+------+------------------------------------------------------------+
1663
+ * ```
1664
+ *
1665
+ * Here's an example:
1666
+ *
1667
+ * ```ts
1668
+ * import { RunnableLambda } from "@langchain/core/runnables";
1669
+ * import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch";
1670
+ * // Use this import for web environments that don't support "async_hooks"
1671
+ * // and manually pass config to child runs.
1672
+ * // import { dispatchCustomEvent } from "@langchain/core/callbacks/dispatch/web";
1673
+ *
1674
+ * const slowThing = RunnableLambda.from(async (someInput: string) => {
1675
+ * // Placeholder for some slow operation
1676
+ * await new Promise((resolve) => setTimeout(resolve, 100));
1677
+ * await dispatchCustomEvent("progress_event", {
1678
+ * message: "Finished step 1 of 2",
1679
+ * });
1680
+ * await new Promise((resolve) => setTimeout(resolve, 100));
1681
+ * return "Done";
1682
+ * });
1683
+ *
1684
+ * const eventStream = await slowThing.streamEvents("hello world", {
1685
+ * version: "v2",
1686
+ * });
1687
+ *
1688
+ * for await (const event of eventStream) {
1689
+ * if (event.event === "on_custom_event") {
1690
+ * console.log(event);
1691
+ * }
1692
+ * }
1693
+ * ```
1694
+ */
1695
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
1696
+ version: "v1" | "v2";
1697
+ }, streamOptions?: Omit<EventStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<StreamEvent>;
1698
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
1699
+ version: "v1" | "v2";
1700
+ encoding: "text/event-stream";
1701
+ }, streamOptions?: Omit<EventStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<Uint8Array>;
1702
+ private _streamEventsV2;
1703
+ private _streamEventsV1;
1704
+ static isRunnable(thing: any): thing is Runnable;
1705
+ /**
1706
+ * Bind lifecycle listeners to a Runnable, returning a new Runnable.
1707
+ * The Run object contains information about the run, including its id,
1708
+ * type, input, output, error, startTime, endTime, and any tags or metadata
1709
+ * added to the run.
1710
+ *
1711
+ * @param {Object} params - The object containing the callback functions.
1712
+ * @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
1713
+ * @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
1714
+ * @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
1715
+ */
1716
+ withListeners({ onStart, onEnd, onError, }: {
1717
+ onStart?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
1718
+ onEnd?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
1719
+ onError?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
1720
+ }): Runnable<RunInput, RunOutput, CallOptions>;
1721
+ /**
1722
+ * Convert a runnable to a tool. Return a new instance of `RunnableToolLike`
1723
+ * which contains the runnable, name, description and schema.
1724
+ *
1725
+ * @template {T extends RunInput = RunInput} RunInput - The input type of the runnable. Should be the same as the `RunInput` type of the runnable.
1726
+ *
1727
+ * @param fields
1728
+ * @param {string | undefined} [fields.name] The name of the tool. If not provided, it will default to the name of the runnable.
1729
+ * @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.
1730
+ * @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.
1731
+ * @returns {RunnableToolLike<z.ZodType<T>, RunOutput>} An instance of `RunnableToolLike` which is a runnable that can be used as a tool.
1732
+ */
1733
+ asTool<T extends RunInput = RunInput>(fields: {
1734
+ name?: string;
1735
+ description?: string;
1736
+ schema: z.ZodType<T>;
1737
+ }): RunnableToolLike<z.ZodType<T | ToolCall>, RunOutput>;
1738
+ }
1739
+ type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> = {
1740
+ bound: Runnable<RunInput, RunOutput, CallOptions>;
1741
+ kwargs?: Partial<CallOptions>;
1742
+ config: RunnableConfig;
1743
+ configFactories?: Array<(config: RunnableConfig) => RunnableConfig>;
1744
+ };
1745
+ /**
1746
+ * A runnable that delegates calls to another runnable with a set of kwargs.
1747
+ * @example
1748
+ * ```typescript
1749
+ * import {
1750
+ * type RunnableConfig,
1751
+ * RunnableLambda,
1752
+ * } from "@langchain/core/runnables";
1753
+ *
1754
+ * const enhanceProfile = (
1755
+ * profile: Record<string, any>,
1756
+ * config?: RunnableConfig
1757
+ * ) => {
1758
+ * if (config?.configurable?.role) {
1759
+ * return { ...profile, role: config.configurable.role };
1760
+ * }
1761
+ * return profile;
1762
+ * };
1763
+ *
1764
+ * const runnable = RunnableLambda.from(enhanceProfile);
1765
+ *
1766
+ * // Bind configuration to the runnable to set the user's role dynamically
1767
+ * const adminRunnable = runnable.bind({ configurable: { role: "Admin" } });
1768
+ * const userRunnable = runnable.bind({ configurable: { role: "User" } });
1769
+ *
1770
+ * const result1 = await adminRunnable.invoke({
1771
+ * name: "Alice",
1772
+ * email: "alice@example.com"
1773
+ * });
1774
+ *
1775
+ * // { name: "Alice", email: "alice@example.com", role: "Admin" }
1776
+ *
1777
+ * const result2 = await userRunnable.invoke({
1778
+ * name: "Bob",
1779
+ * email: "bob@example.com"
1780
+ * });
1781
+ *
1782
+ * // { name: "Bob", email: "bob@example.com", role: "User" }
1783
+ * ```
1784
+ */
1785
+ declare class RunnableBinding<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
1786
+ static lc_name(): string;
1787
+ lc_namespace: string[];
1788
+ lc_serializable: boolean;
1789
+ bound: Runnable<RunInput, RunOutput, CallOptions>;
1790
+ config: RunnableConfig;
1791
+ kwargs?: Partial<CallOptions>;
1792
+ configFactories?: Array<(config: RunnableConfig) => RunnableConfig | Promise<RunnableConfig>>;
1793
+ constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>);
1794
+ getName(suffix?: string | undefined): string;
1795
+ _mergeConfig(...options: (Partial<CallOptions> | RunnableConfig | undefined)[]): Promise<Partial<CallOptions>>;
1796
+ bind(kwargs: Partial<CallOptions>): RunnableBinding<RunInput, RunOutput, CallOptions>;
1797
+ withConfig(config: RunnableConfig): Runnable<RunInput, RunOutput, CallOptions>;
1798
+ withRetry(fields?: {
1799
+ stopAfterAttempt?: number;
1800
+ onFailedAttempt?: RunnableRetryFailedAttemptHandler;
1801
+ }): RunnableRetry<RunInput, RunOutput, CallOptions>;
1802
+ invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
1803
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1804
+ returnExceptions?: false;
1805
+ }): Promise<RunOutput[]>;
1806
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1807
+ returnExceptions: true;
1808
+ }): Promise<(RunOutput | Error)[]>;
1809
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
1810
+ _streamIterator(input: RunInput, options?: Partial<CallOptions> | undefined): AsyncGenerator<Awaited<RunOutput>, void, unknown>;
1811
+ stream(input: RunInput, options?: Partial<CallOptions> | undefined): Promise<IterableReadableStream<RunOutput>>;
1812
+ transform(generator: AsyncGenerator<RunInput>, options?: Partial<CallOptions>): AsyncGenerator<RunOutput>;
1813
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
1814
+ version: "v1" | "v2";
1815
+ }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<StreamEvent>;
1816
+ streamEvents(input: RunInput, options: Partial<CallOptions> & {
1817
+ version: "v1" | "v2";
1818
+ encoding: "text/event-stream";
1819
+ }, streamOptions?: Omit<LogStreamCallbackHandlerInput, "autoClose">): IterableReadableStream<Uint8Array>;
1820
+ static isRunnableBinding(thing: any): thing is RunnableBinding<any, any, any>;
1821
+ /**
1822
+ * Bind lifecycle listeners to a Runnable, returning a new Runnable.
1823
+ * The Run object contains information about the run, including its id,
1824
+ * type, input, output, error, startTime, endTime, and any tags or metadata
1825
+ * added to the run.
1826
+ *
1827
+ * @param {Object} params - The object containing the callback functions.
1828
+ * @param {(run: Run) => void} params.onStart - Called before the runnable starts running, with the Run object.
1829
+ * @param {(run: Run) => void} params.onEnd - Called after the runnable finishes running, with the Run object.
1830
+ * @param {(run: Run) => void} params.onError - Called if the runnable throws an error, with the Run object.
1831
+ */
1832
+ withListeners({ onStart, onEnd, onError, }: {
1833
+ onStart?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
1834
+ onEnd?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
1835
+ onError?: (run: Run, config?: RunnableConfig) => void | Promise<void>;
1836
+ }): Runnable<RunInput, RunOutput, CallOptions>;
1837
+ }
1838
+ /**
1839
+ * Base class for runnables that can be retried a
1840
+ * specified number of times.
1841
+ * @example
1842
+ * ```typescript
1843
+ * import {
1844
+ * RunnableLambda,
1845
+ * RunnableRetry,
1846
+ * } from "@langchain/core/runnables";
1847
+ *
1848
+ * // Simulate an API call that fails
1849
+ * const simulateApiCall = (input: string): string => {
1850
+ * console.log(`Attempting API call with input: ${input}`);
1851
+ * throw new Error("API call failed due to network issue");
1852
+ * };
1853
+ *
1854
+ * const apiCallLambda = RunnableLambda.from(simulateApiCall);
1855
+ *
1856
+ * // Apply retry logic using the .withRetry() method
1857
+ * const apiCallWithRetry = apiCallLambda.withRetry({ stopAfterAttempt: 3 });
1858
+ *
1859
+ * // Alternatively, create a RunnableRetry instance manually
1860
+ * const manualRetry = new RunnableRetry({
1861
+ * bound: apiCallLambda,
1862
+ * maxAttemptNumber: 3,
1863
+ * config: {},
1864
+ * });
1865
+ *
1866
+ * // Example invocation using the .withRetry() method
1867
+ * const res = await apiCallWithRetry
1868
+ * .invoke("Request 1")
1869
+ * .catch((error) => {
1870
+ * console.error("Failed after multiple retries:", error.message);
1871
+ * });
1872
+ *
1873
+ * // Example invocation using the manual retry instance
1874
+ * const res2 = await manualRetry
1875
+ * .invoke("Request 2")
1876
+ * .catch((error) => {
1877
+ * console.error("Failed after multiple retries:", error.message);
1878
+ * });
1879
+ * ```
1880
+ */
1881
+ declare class RunnableRetry<RunInput = any, RunOutput = any, CallOptions extends RunnableConfig = RunnableConfig> extends RunnableBinding<RunInput, RunOutput, CallOptions> {
1882
+ static lc_name(): string;
1883
+ lc_namespace: string[];
1884
+ protected maxAttemptNumber: number;
1885
+ onFailedAttempt: RunnableRetryFailedAttemptHandler;
1886
+ constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions> & {
1887
+ maxAttemptNumber?: number;
1888
+ onFailedAttempt?: RunnableRetryFailedAttemptHandler;
1889
+ });
1890
+ _patchConfigForRetry(attempt: number, config?: Partial<CallOptions>, runManager?: CallbackManagerForChainRun): Partial<CallOptions>;
1891
+ protected _invoke(input: RunInput, config?: CallOptions, runManager?: CallbackManagerForChainRun): Promise<RunOutput>;
1892
+ /**
1893
+ * Method that invokes the runnable with the specified input, run manager,
1894
+ * and config. It handles the retry logic by catching any errors and
1895
+ * recursively invoking itself with the updated config for the next retry
1896
+ * attempt.
1897
+ * @param input The input for the runnable.
1898
+ * @param runManager The run manager for the runnable.
1899
+ * @param config The config for the runnable.
1900
+ * @returns A promise that resolves to the output of the runnable.
1901
+ */
1902
+ invoke(input: RunInput, config?: CallOptions): Promise<RunOutput>;
1903
+ _batch<ReturnExceptions extends boolean = false>(inputs: RunInput[], configs?: RunnableConfig[], runManagers?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions): Promise<ReturnExceptions extends false ? RunOutput[] : (Error | RunOutput)[]>;
1904
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1905
+ returnExceptions?: false;
1906
+ }): Promise<RunOutput[]>;
1907
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
1908
+ returnExceptions: true;
1909
+ }): Promise<(RunOutput | Error)[]>;
1910
+ batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
1911
+ }
1912
+ /**
1913
+ * A Runnable that can fallback to other Runnables if it fails.
1914
+ * External APIs (e.g., APIs for a language model) may at times experience
1915
+ * degraded performance or even downtime.
1916
+ *
1917
+ * In these cases, it can be useful to have a fallback Runnable that can be
1918
+ * used in place of the original Runnable (e.g., fallback to another LLM provider).
1919
+ *
1920
+ * Fallbacks can be defined at the level of a single Runnable, or at the level
1921
+ * of a chain of Runnables. Fallbacks are tried in order until one succeeds or
1922
+ * all fail.
1923
+ *
1924
+ * While you can instantiate a `RunnableWithFallbacks` directly, it is usually
1925
+ * more convenient to use the `withFallbacks` method on an existing Runnable.
1926
+ *
1927
+ * When streaming, fallbacks will only be called on failures during the initial
1928
+ * stream creation. Errors that occur after a stream starts will not fallback
1929
+ * to the next Runnable.
1930
+ *
1931
+ * @example
1932
+ * ```typescript
1933
+ * import {
1934
+ * RunnableLambda,
1935
+ * RunnableWithFallbacks,
1936
+ * } from "@langchain/core/runnables";
1937
+ *
1938
+ * const primaryOperation = (input: string): string => {
1939
+ * if (input !== "safe") {
1940
+ * throw new Error("Primary operation failed due to unsafe input");
1941
+ * }
1942
+ * return `Processed: ${input}`;
1943
+ * };
1944
+ *
1945
+ * // Define a fallback operation that processes the input differently
1946
+ * const fallbackOperation = (input: string): string =>
1947
+ * `Fallback processed: ${input}`;
1948
+ *
1949
+ * const primaryRunnable = RunnableLambda.from(primaryOperation);
1950
+ * const fallbackRunnable = RunnableLambda.from(fallbackOperation);
1951
+ *
1952
+ * // Apply the fallback logic using the .withFallbacks() method
1953
+ * const runnableWithFallback = primaryRunnable.withFallbacks([fallbackRunnable]);
1954
+ *
1955
+ * // Alternatively, create a RunnableWithFallbacks instance manually
1956
+ * const manualFallbackChain = new RunnableWithFallbacks({
1957
+ * runnable: primaryRunnable,
1958
+ * fallbacks: [fallbackRunnable],
1959
+ * });
1960
+ *
1961
+ * // Example invocation using .withFallbacks()
1962
+ * const res = await runnableWithFallback
1963
+ * .invoke("unsafe input")
1964
+ * .catch((error) => {
1965
+ * console.error("Failed after all attempts:", error.message);
1966
+ * });
1967
+ *
1968
+ * // "Fallback processed: unsafe input"
1969
+ *
1970
+ * // Example invocation using manual instantiation
1971
+ * const res = await manualFallbackChain
1972
+ * .invoke("safe")
1973
+ * .catch((error) => {
1974
+ * console.error("Failed after all attempts:", error.message);
1975
+ * });
1976
+ *
1977
+ * // "Processed: safe"
1978
+ * ```
1979
+ */
1980
+ declare class RunnableWithFallbacks<RunInput, RunOutput> extends Runnable<RunInput, RunOutput> {
1981
+ static lc_name(): string;
1982
+ lc_namespace: string[];
1983
+ lc_serializable: boolean;
1984
+ runnable: Runnable<RunInput, RunOutput>;
1985
+ fallbacks: Runnable<RunInput, RunOutput>[];
1986
+ constructor(fields: {
1987
+ runnable: Runnable<RunInput, RunOutput>;
1988
+ fallbacks: Runnable<RunInput, RunOutput>[];
1989
+ });
1990
+ runnables(): Generator<Runnable<RunInput, RunOutput, RunnableConfig<Record<string, any>>>, void, unknown>;
1991
+ invoke(input: RunInput, options?: Partial<RunnableConfig>): Promise<RunOutput>;
1992
+ _streamIterator(input: RunInput, options?: Partial<RunnableConfig> | undefined): AsyncGenerator<RunOutput>;
1993
+ batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions & {
1994
+ returnExceptions?: false;
1995
+ }): Promise<RunOutput[]>;
1996
+ batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions & {
1997
+ returnExceptions: true;
1998
+ }): Promise<(RunOutput | Error)[]>;
1999
+ batch(inputs: RunInput[], options?: Partial<RunnableConfig> | Partial<RunnableConfig>[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>;
2000
+ }
2001
+ interface RunnableToolLikeArgs<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends Omit<RunnableBindingArgs<z.infer<RunInput>, RunOutput>, "config"> {
2002
+ name: string;
2003
+ description?: string;
2004
+ schema: RunInput;
2005
+ config?: RunnableConfig;
2006
+ }
2007
+ declare class RunnableToolLike<RunInput extends z.ZodType = z.ZodType, RunOutput = unknown> extends RunnableBinding<z.infer<RunInput>, RunOutput> {
2008
+ name: string;
2009
+ description?: string;
2010
+ schema: RunInput;
2011
+ constructor(fields: RunnableToolLikeArgs<RunInput, RunOutput>);
2012
+ static lc_name(): string;
2013
+ }
2014
+
2015
+ interface BaseLangChainParams {
2016
+ verbose?: boolean;
2017
+ callbacks?: Callbacks;
2018
+ tags?: string[];
2019
+ metadata?: Record<string, unknown>;
2020
+ }
2021
+ /**
2022
+ * Base class for language models, chains, tools.
2023
+ */
2024
+ declare abstract class BaseLangChain<RunInput, RunOutput, CallOptions extends RunnableConfig = RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> implements BaseLangChainParams {
2025
+ /**
2026
+ * Whether to print out response text.
2027
+ */
2028
+ verbose: boolean;
2029
+ callbacks?: Callbacks;
2030
+ tags?: string[];
2031
+ metadata?: Record<string, unknown>;
2032
+ get lc_attributes(): {
2033
+ [key: string]: undefined;
2034
+ } | undefined;
2035
+ constructor(params: BaseLangChainParams);
2036
+ }
2037
+
2038
+ type ResponseFormat = "content" | "content_and_artifact" | string;
2039
+ type ToolReturnType = any;
2040
+ type ZodObjectAny = z.ZodObject<any, any, any, any>;
2041
+ /**
2042
+ * Parameters for the Tool classes.
2043
+ */
2044
+ interface ToolParams extends BaseLangChainParams {
2045
+ /**
2046
+ * The tool response format.
2047
+ *
2048
+ * If "content" then the output of the tool is interpreted as the contents of a
2049
+ * ToolMessage. If "content_and_artifact" then the output is expected to be a
2050
+ * two-tuple corresponding to the (content, artifact) of a ToolMessage.
2051
+ *
2052
+ * @default "content"
2053
+ */
2054
+ responseFormat?: ResponseFormat;
2055
+ /**
2056
+ * Whether to show full details in the thrown parsing errors.
2057
+ *
2058
+ * @default false
2059
+ */
2060
+ verboseParsingErrors?: boolean;
2061
+ }
2062
+ type ToolRunnableConfig<ConfigurableFieldType extends Record<string, any> = Record<string, any>> = RunnableConfig<ConfigurableFieldType> & {
2063
+ toolCall?: ToolCall;
2064
+ };
2065
+ /**
2066
+ * Base class for Tools that accept input of any shape defined by a Zod schema.
2067
+ */
2068
+ declare abstract class StructuredTool<T extends ZodObjectAny = ZodObjectAny> extends BaseLangChain<(z.output<T> extends string ? string : never) | z.input<T> | ToolCall, ToolReturnType> {
2069
+ abstract name: string;
2070
+ abstract description: string;
2071
+ abstract schema: T | z.ZodEffects<T>;
2072
+ /**
2073
+ * Whether to return the tool's output directly.
2074
+ *
2075
+ * Setting this to true means that after the tool is called,
2076
+ * an agent should stop looping.
2077
+ */
2078
+ returnDirect: boolean;
2079
+ verboseParsingErrors: boolean;
2080
+ get lc_namespace(): string[];
2081
+ /**
2082
+ * The tool response format.
2083
+ *
2084
+ * If "content" then the output of the tool is interpreted as the contents of a
2085
+ * ToolMessage. If "content_and_artifact" then the output is expected to be a
2086
+ * two-tuple corresponding to the (content, artifact) of a ToolMessage.
2087
+ *
2088
+ * @default "content"
2089
+ */
2090
+ responseFormat?: ResponseFormat;
2091
+ constructor(fields?: ToolParams);
2092
+ protected abstract _call(arg: z.output<T>, runManager?: CallbackManagerForToolRun, parentConfig?: ToolRunnableConfig): Promise<ToolReturnType>;
2093
+ /**
2094
+ * Invokes the tool with the provided input and configuration.
2095
+ * @param input The input for the tool.
2096
+ * @param config Optional configuration for the tool.
2097
+ * @returns A Promise that resolves with a string.
2098
+ */
2099
+ invoke(input: (z.output<T> extends string ? string : never) | z.input<T> | ToolCall, config?: RunnableConfig): Promise<ToolReturnType>;
2100
+ /**
2101
+ * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
2102
+ *
2103
+ * Calls the tool with the provided argument, configuration, and tags. It
2104
+ * parses the input according to the schema, handles any errors, and
2105
+ * manages callbacks.
2106
+ * @param arg The input argument for the tool.
2107
+ * @param configArg Optional configuration or callbacks for the tool.
2108
+ * @param tags Optional tags for the tool.
2109
+ * @returns A Promise that resolves with a string.
2110
+ */
2111
+ call(arg: (z.output<T> extends string ? string : never) | z.input<T>, configArg?: Callbacks | ToolRunnableConfig,
2112
+ /** @deprecated */
2113
+ tags?: string[]): Promise<ToolReturnType>;
2114
+ }
2115
+ interface BaseDynamicToolInput extends ToolParams {
2116
+ name: string;
2117
+ description: string;
2118
+ /**
2119
+ * Whether to return the tool's output directly.
2120
+ *
2121
+ * Setting this to true means that after the tool is called,
2122
+ * an agent should stop looping.
2123
+ */
2124
+ returnDirect?: boolean;
2125
+ }
2126
+ /**
2127
+ * Interface for the input parameters of the DynamicStructuredTool class.
2128
+ */
2129
+ interface DynamicStructuredToolInput<T extends ZodObjectAny | Record<string, any> = ZodObjectAny> extends BaseDynamicToolInput {
2130
+ func: (input: BaseDynamicToolInput["responseFormat"] extends "content_and_artifact" ? ToolCall : T extends ZodObjectAny ? z.infer<T> : T, runManager?: CallbackManagerForToolRun, config?: RunnableConfig) => Promise<ToolReturnType>;
2131
+ schema: T extends ZodObjectAny ? T : T;
2132
+ }
2133
+ /**
2134
+ * A tool that can be created dynamically from a function, name, and
2135
+ * description, designed to work with structured data. It extends the
2136
+ * StructuredTool class and overrides the _call method to execute the
2137
+ * provided function when the tool is called.
2138
+ *
2139
+ * Schema can be passed as Zod or JSON schema. The tool will not validate
2140
+ * input if JSON schema is passed.
2141
+ */
2142
+ declare class DynamicStructuredTool<T extends ZodObjectAny | Record<string, any> = ZodObjectAny> extends StructuredTool<T extends ZodObjectAny ? T : ZodObjectAny> {
2143
+ static lc_name(): string;
2144
+ name: string;
2145
+ description: string;
2146
+ func: DynamicStructuredToolInput<T>["func"];
2147
+ schema: T extends ZodObjectAny ? T : ZodObjectAny;
2148
+ constructor(fields: DynamicStructuredToolInput<T>);
2149
+ /**
2150
+ * @deprecated Use .invoke() instead. Will be removed in 0.3.0.
2151
+ */
2152
+ call(arg: (T extends ZodObjectAny ? z.output<T> : T) | ToolCall, configArg?: RunnableConfig | Callbacks,
2153
+ /** @deprecated */
2154
+ tags?: string[]): Promise<ToolReturnType>;
2155
+ protected _call(arg: (T extends ZodObjectAny ? z.output<T> : T) | ToolCall, runManager?: CallbackManagerForToolRun, parentConfig?: RunnableConfig): Promise<ToolReturnType>;
2156
+ }
2157
+
2158
+ declare const executeTool: (config: FreestyleExecuteScriptParamsConfiguration & {
2159
+ apiKey: string;
2160
+ }) => DynamicStructuredTool;
2161
+
2162
+ export { executeTool };