import { CallbackManager, CallbackManagerForChainRun, BaseCallbackConfig } from "../../callbacks/manager.js"; import { Serializable } from "../../load/serializable.js"; import { IterableReadableStream } from "../../util/stream.js"; import { RunnableConfig } from "./config.js"; export type RunnableFunc = (input: RunInput) => RunOutput | Promise; export type RunnableLike = Runnable | RunnableFunc | { [key: string]: RunnableLike; }; export type RunnableBatchOptions = { maxConcurrency?: number; returnExceptions?: boolean; }; export type RunnableRetryFailedAttemptHandler = (error: any) => any; type RunnableConfigAndOptions = RunnableConfig & { runType?: string; }; /** * A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or * transformed. */ export declare abstract class Runnable extends Serializable { protected lc_runnable: boolean; abstract invoke(input: RunInput, options?: Partial): Promise; /** * Bind arguments to a Runnable, returning a new Runnable. * @param kwargs * @returns A new RunnableBinding that, when invoked, will apply the bound args. */ bind(kwargs: Partial): Runnable; /** * Return a new Runnable that maps a list of inputs to a list of outputs, * by calling invoke() with each input. */ map(): Runnable; /** * Add retry logic to an existing runnable. * @param kwargs * @returns A new RunnableRetry that, when invoked, will retry according to the parameters. */ withRetry(fields?: { stopAfterAttempt?: number; onFailedAttempt?: RunnableRetryFailedAttemptHandler; }): RunnableRetry; /** * Bind config to a Runnable, returning a new Runnable. * @param config New configuration parameters to attach to the new runnable. * @returns A new RunnableBinding with a config matching what's passed. */ withConfig(config: RunnableConfig): RunnableBinding; /** * Create a new runnable from the current one that will try invoking * other passed fallback runnables if the initial invocation fails. * @param fields.fallbacks Other runnables to call if the runnable errors. * @returns A new RunnableWithFallbacks. */ withFallbacks(fields: { fallbacks: Runnable[]; }): RunnableWithFallbacks; protected _getOptionsList(options: Partial | Partial[], length?: number): Partial[]; /** * Default implementation of batch, which calls invoke N times. * Subclasses should override this method if they can batch more efficiently. * @param inputs Array of inputs to each batch call. * @param options Either a single call options object to apply to each batch call or an array for each call. * @param batchOptions.maxConcurrency Maximum number of calls to run at once. * @param batchOptions.returnExceptions Whether to return errors rather than throwing on the first one * @returns An array of RunOutputs, or mixed RunOutputs and errors if batchOptions.returnExceptions is set */ batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions?: false; }): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions: true; }): Promise<(RunOutput | Error)[]>; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>; /** * Default streaming implementation. * Subclasses should override this method if they support streaming output. * @param input * @param options */ _streamIterator(input: RunInput, options?: Partial): AsyncGenerator; /** * Stream output in chunks. * @param input * @param options * @returns A readable stream that is also an iterable. */ stream(input: RunInput, options?: Partial): Promise>; protected _separateRunnableConfigFromCallOptions(options?: Partial): [RunnableConfig, Omit, keyof RunnableConfig>]; protected _callWithConfig(func: ((input: T) => Promise) | ((input: T, config?: RunnableConfig, runManager?: CallbackManagerForChainRun) => Promise), input: T, options?: RunnableConfigAndOptions): Promise; /** * Internal method that handles batching and configuration for a runnable * It takes a function, input values, and optional configuration, and * returns a promise that resolves to the output values. * @param func The function to be executed for each input value. * @param input The input values to be processed. * @param config Optional configuration for the function execution. * @returns A promise that resolves to the output values. */ _batchWithConfig(func: (inputs: T[], configs?: RunnableConfig[], runManagers?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions) => Promise<(RunOutput | Error)[]>, inputs: T[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>; /** * Helper method to transform an Iterator of Input values into an Iterator of * Output values, with callbacks. * Use this to implement `stream()` or `transform()` in Runnable subclasses. */ protected _transformStreamWithConfig(inputGenerator: AsyncGenerator, transformer: (generator: AsyncGenerator, runManager?: CallbackManagerForChainRun, options?: Partial) => AsyncGenerator, options?: RunnableConfig & { runType?: string; }): AsyncGenerator; _patchConfig(config?: Partial, callbackManager?: CallbackManager | undefined): Partial; /** * Create a new runnable sequence that runs each individual runnable in series, * piping the output of one runnable into another runnable or runnable-like. * @param coerceable A runnable, function, or object whose values are functions or runnables. * @returns A new runnable sequence. */ pipe(coerceable: RunnableLike): RunnableSequence>; /** * Default implementation of transform, which buffers input and then calls stream. * Subclasses should override this method if they can start producing output while * input is still being generated. * @param generator * @param options */ transform(generator: AsyncGenerator, options: Partial): AsyncGenerator; static isRunnable(thing: any): thing is Runnable; } export type RunnableBindingArgs = { bound: Runnable; kwargs: Partial; config: RunnableConfig; }; /** * A runnable that delegates calls to another runnable with a set of kwargs. */ export declare class RunnableBinding extends Runnable { static lc_name(): string; lc_namespace: string[]; lc_serializable: boolean; bound: Runnable; config: RunnableConfig; protected kwargs: Partial; constructor(fields: RunnableBindingArgs); _mergeConfig(options?: Record): Partial; bind(kwargs: Partial): RunnableBinding; withConfig(config: RunnableConfig): RunnableBinding; withRetry(fields?: { stopAfterAttempt?: number; onFailedAttempt?: RunnableRetryFailedAttemptHandler; }): RunnableRetry; invoke(input: RunInput, options?: Partial): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions?: false; }): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions: true; }): Promise<(RunOutput | Error)[]>; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>; _streamIterator(input: RunInput, options?: Partial | undefined): AsyncGenerator, void, unknown>; stream(input: RunInput, options?: Partial | undefined): Promise>; transform(generator: AsyncGenerator, options: Partial): AsyncGenerator; static isRunnableBinding(thing: any): thing is RunnableBinding; } /** * A runnable that delegates calls to another runnable * with each element of the input sequence. */ export declare class RunnableEach extends Runnable { static lc_name(): string; lc_serializable: boolean; lc_namespace: string[]; bound: Runnable; constructor(fields: { bound: Runnable; }); /** * Binds the runnable with the specified arguments. * @param args The arguments to bind the runnable with. * @returns A new instance of the `RunnableEach` class that is bound with the specified arguments. */ bind(kwargs: Partial): RunnableEach; /** * Invokes the runnable with the specified input and configuration. * @param input The input to invoke the runnable with. * @param config The configuration to invoke the runnable with. * @returns A promise that resolves to the output of the runnable. */ invoke(inputs: RunInputItem[], config?: Partial): Promise; /** * A helper method that is used to invoke the runnable with the specified input and configuration. * @param input The input to invoke the runnable with. * @param config The configuration to invoke the runnable with. * @returns A promise that resolves to the output of the runnable. */ protected _invoke(inputs: RunInputItem[], config?: Partial, runManager?: CallbackManagerForChainRun): Promise; } /** * Base class for runnables that can be retried a * specified number of times. */ export declare class RunnableRetry extends RunnableBinding { static lc_name(): string; lc_namespace: string[]; protected maxAttemptNumber: number; onFailedAttempt?: RunnableRetryFailedAttemptHandler; constructor(fields: RunnableBindingArgs & { maxAttemptNumber?: number; onFailedAttempt?: RunnableRetryFailedAttemptHandler; }); _patchConfigForRetry(attempt: number, config?: Partial, runManager?: CallbackManagerForChainRun): Partial; protected _invoke(input: RunInput, config?: CallOptions, runManager?: CallbackManagerForChainRun): Promise; /** * Method that invokes the runnable with the specified input, run manager, * and config. It handles the retry logic by catching any errors and * recursively invoking itself with the updated config for the next retry * attempt. * @param input The input for the runnable. * @param runManager The run manager for the runnable. * @param config The config for the runnable. * @returns A promise that resolves to the output of the runnable. */ invoke(input: RunInput, config?: CallOptions): Promise; _batch(inputs: RunInput[], configs?: RunnableConfig[], runManagers?: (CallbackManagerForChainRun | undefined)[], batchOptions?: RunnableBatchOptions): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions?: false; }): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions: true; }): Promise<(RunOutput | Error)[]>; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>; } /** * A sequence of runnables, where the output of each is the input of the next. */ export declare class RunnableSequence extends Runnable { static lc_name(): string; protected first: Runnable; protected middle: Runnable[]; protected last: Runnable; lc_serializable: boolean; lc_namespace: string[]; constructor(fields: { first: Runnable; middle?: Runnable[]; last: Runnable; }); get steps(): Runnable[]; invoke(input: RunInput, options?: RunnableConfig): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions?: false; }): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions: true; }): Promise<(RunOutput | Error)[]>; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>; _streamIterator(input: RunInput, options?: RunnableConfig): AsyncGenerator; pipe(coerceable: RunnableLike): RunnableSequence>; static isRunnableSequence(thing: any): thing is RunnableSequence; static from([first, ...runnables]: [ RunnableLike, ...RunnableLike[], RunnableLike ]): RunnableSequence>; } /** * A runnable that runs a mapping of runnables in parallel, * and returns a mapping of their outputs. */ export declare class RunnableMap extends Runnable> { static lc_name(): string; lc_namespace: string[]; lc_serializable: boolean; protected steps: Record>; constructor(fields: { steps: Record>; }); invoke(input: RunInput, options?: Partial): Promise>; } /** * A runnable that runs a callable. */ export declare class RunnableLambda extends Runnable { static lc_name(): string; lc_namespace: string[]; protected func: RunnableFunc; constructor(fields: { func: RunnableFunc; }); _invoke(input: RunInput, config?: Partial, runManager?: CallbackManagerForChainRun): Promise; invoke(input: RunInput, options?: Partial): Promise; } /** * A Runnable that can fallback to other Runnables if it fails. */ export declare class RunnableWithFallbacks extends Runnable { static lc_name(): string; lc_namespace: string[]; lc_serializable: boolean; protected runnable: Runnable; protected fallbacks: Runnable[]; constructor(fields: { runnable: Runnable; fallbacks: Runnable[]; }); runnables(): Generator, void, unknown>; invoke(input: RunInput, options?: Partial): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions?: false; }): Promise; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions & { returnExceptions: true; }): Promise<(RunOutput | Error)[]>; batch(inputs: RunInput[], options?: Partial | Partial[], batchOptions?: RunnableBatchOptions): Promise<(RunOutput | Error)[]>; } export declare function _coerceToRunnable(coerceable: RunnableLike): Runnable>; export {};