{"version":3,"file":"multi_route.cjs","names":["BaseChain"],"sources":["../../../src/chains/router/multi_route.ts"],"sourcesContent":["import { ChainValues } from \"@langchain/core/utils/types\";\nimport {\n  CallbackManagerForChainRun,\n  Callbacks,\n} from \"@langchain/core/callbacks/manager\";\nimport { BaseChain, ChainInputs } from \"../../chains/base.js\";\n\n/**\n * A type that represents the inputs for the MultiRouteChain. It is a\n * recursive type that can contain nested objects, arrays, strings, and\n * numbers.\n */\ntype Inputs = {\n  [key: string]: Inputs | Inputs[] | string | string[] | number | number[];\n};\n\n/**\n * An interface that represents the route returned by the RouterChain. It\n * includes optional fields for the destination and nextInputs.\n */\nexport interface Route {\n  destination?: string;\n  nextInputs: { [key: string]: Inputs };\n}\n\n/**\n * An interface that extends the ChainInputs interface and adds additional\n * properties for the routerChain, destinationChains, defaultChain, and\n * silentErrors. It represents the input expected by the MultiRouteChain\n * class.\n */\nexport interface MultiRouteChainInput extends ChainInputs {\n  routerChain: RouterChain;\n  destinationChains: { [name: string]: BaseChain };\n  defaultChain: BaseChain;\n  silentErrors?: boolean;\n}\n\n/**\n * A class that represents a router chain. It\n * extends the BaseChain class and provides functionality for routing\n * inputs to different chains.\n */\nexport abstract class RouterChain extends BaseChain {\n  get outputKeys(): string[] {\n    return [\"destination\", \"next_inputs\"];\n  }\n\n  async route(inputs: ChainValues, callbacks?: Callbacks): Promise<Route> {\n    const result = await this.call(inputs, callbacks);\n    return {\n      destination: result.destination,\n      nextInputs: result.next_inputs,\n    };\n  }\n}\n\n/**\n * A class that represents a multi-route chain.\n * It extends the BaseChain class and provides functionality for routing\n * inputs to different chains based on a router chain.\n */\nexport class MultiRouteChain extends BaseChain {\n  static lc_name() {\n    return \"MultiRouteChain\";\n  }\n\n  routerChain: RouterChain;\n\n  destinationChains: { [name: string]: BaseChain };\n\n  defaultChain: BaseChain;\n\n  silentErrors = false;\n\n  constructor(fields: MultiRouteChainInput) {\n    super(fields);\n    this.routerChain = fields.routerChain;\n    this.destinationChains = fields.destinationChains;\n    this.defaultChain = fields.defaultChain;\n    this.silentErrors = fields.silentErrors ?? this.silentErrors;\n  }\n\n  get inputKeys(): string[] {\n    return this.routerChain.inputKeys;\n  }\n\n  get outputKeys(): string[] {\n    return [];\n  }\n\n  async _call(\n    values: ChainValues,\n    runManager?: CallbackManagerForChainRun\n  ): Promise<ChainValues> {\n    const { destination, nextInputs } = await this.routerChain.route(\n      values,\n      runManager?.getChild()\n    );\n    await runManager?.handleText(\n      `${destination}: ${JSON.stringify(nextInputs)}`\n    );\n    if (!destination) {\n      return this.defaultChain\n        .call(nextInputs, runManager?.getChild())\n        .catch((err) => {\n          throw new Error(`Error in default chain: ${err}`);\n        });\n    }\n    if (destination in this.destinationChains) {\n      return this.destinationChains[destination]\n        .call(nextInputs, runManager?.getChild())\n        .catch((err) => {\n          throw new Error(`Error in ${destination} chain: ${err}`);\n        });\n    }\n    if (this.silentErrors) {\n      return this.defaultChain\n        .call(nextInputs, runManager?.getChild())\n        .catch((err) => {\n          throw new Error(`Error in default chain: ${err}`);\n        });\n    }\n    throw new Error(\n      `Destination ${destination} not found in destination chains with keys ${Object.keys(\n        this.destinationChains\n      )}`\n    );\n  }\n\n  _chainType(): string {\n    return \"multi_route_chain\";\n  }\n}\n"],"mappings":";;;;;;;AA2CA,IAAsB,cAAtB,cAA0CA,aAAAA,UAAU;CAClD,IAAI,aAAuB;AACzB,SAAO,CAAC,eAAe,cAAc;;CAGvC,MAAM,MAAM,QAAqB,WAAuC;EACtE,MAAM,SAAS,MAAM,KAAK,KAAK,QAAQ,UAAU;AACjD,SAAO;GACL,aAAa,OAAO;GACpB,YAAY,OAAO;GACpB;;;;;;;;AASL,IAAa,kBAAb,cAAqCA,aAAAA,UAAU;CAC7C,OAAO,UAAU;AACf,SAAO;;CAGT;CAEA;CAEA;CAEA,eAAe;CAEf,YAAY,QAA8B;AACxC,QAAM,OAAO;AACb,OAAK,cAAc,OAAO;AAC1B,OAAK,oBAAoB,OAAO;AAChC,OAAK,eAAe,O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