{"version":3,"file":"dict.cjs","names":["Runnable","renderTemplate"],"sources":["../../src/prompts/dict.ts"],"sourcesContent":["import { Runnable } from \"../runnables/base.js\";\nimport type { InputValues } from \"../utils/types/index.js\";\nimport { TypedPromptInputValues } from \"./base.js\";\nimport { parseTemplate, renderTemplate, TemplateFormat } from \"./template.js\";\n\nexport class DictPromptTemplate<\n  RunInput extends InputValues = InputValues,\n  RunOutput extends Record<string, unknown> = Record<string, unknown>,\n> extends Runnable<TypedPromptInputValues<RunInput>, RunOutput> {\n  lc_namespace = [\"langchain_core\", \"prompts\", \"dict\"];\n\n  lc_serializable = true;\n\n  template: Record<string, unknown>;\n\n  templateFormat: TemplateFormat;\n\n  inputVariables: Array<Extract<keyof RunInput, string>>;\n\n  static lc_name() {\n    return \"DictPromptTemplate\";\n  }\n\n  constructor(fields: {\n    template: Record<string, unknown>;\n    templateFormat?: TemplateFormat;\n  }) {\n    const templateFormat = fields.templateFormat ?? \"f-string\";\n    const inputVariables = _getInputVariables(\n      fields.template,\n      templateFormat\n    ) as Array<Extract<keyof RunInput, string>>;\n    super({ inputVariables, ...fields });\n    this.template = fields.template;\n    this.templateFormat = templateFormat;\n    this.inputVariables = inputVariables;\n  }\n\n  async format(values: TypedPromptInputValues<RunInput>): Promise<RunOutput> {\n    return _insertInputVariables(\n      this.template,\n      values,\n      this.templateFormat\n    ) as RunOutput;\n  }\n\n  async invoke(\n    values: TypedPromptInputValues<InputValues>\n  ): Promise<RunOutput> {\n    return await this._callWithConfig(this.format.bind(this), values, {\n      runType: \"prompt\",\n    });\n  }\n}\n\nfunction _getInputVariables(\n  template: Record<string, unknown>,\n  templateFormat: TemplateFormat\n): Array<Extract<keyof InputValues, string>> {\n  const inputVariables: Array<Extract<keyof InputValues, string>> = [];\n  for (const v of Object.values(template)) {\n    if (typeof v === \"string\") {\n      parseTemplate(v, templateFormat).forEach((t) => {\n        if (t.type === \"variable\") {\n          inputVariables.push(t.name);\n        }\n      });\n    } else if (Array.isArray(v)) {\n      for (const x of v) {\n        if (typeof x === \"string\") {\n          parseTemplate(x, templateFormat).forEach((t) => {\n            if (t.type === \"variable\") {\n              inputVariables.push(t.name);\n            }\n          });\n        } else if (typeof x === \"object\") {\n          inputVariables.push(..._getInputVariables(x, templateFormat));\n        }\n      }\n    } else if (typeof v === \"object\" && v !== null) {\n      inputVariables.push(\n        ..._getInputVariables(v as Record<string, unknown>, templateFormat)\n      );\n    }\n  }\n  return Array.from(new Set(inputVariables));\n}\n\nfunction _insertInputVariables(\n  template: Record<string, unknown>,\n  inputs: TypedPromptInputValues<InputValues>,\n  templateFormat: TemplateFormat\n): Record<string, unknown> {\n  const formatted: Record<string, unknown> = {};\n  for (const [k, v] of Object.entries(template)) {\n    if (typeof v === \"string\") {\n      formatted[k] = renderTemplate(v, templateFormat, inputs);\n    } else if (Array.isArray(v)) {\n      const formattedV: Array<unknown> = [];\n      for (const x of v) {\n        if (typeof x === \"string\") {\n          formattedV.push(renderTemplate(x, templateFormat, inputs));\n        } else if (typeof x === \"object\") {\n          formattedV.push(_insertInputVariables(x, inputs, templateFormat));\n        }\n      }\n      formatted[k] = formattedV;\n    } else if (typeof v === \"object\" && v !== null) {\n      formatted[k] = _insertInputVariables(\n        v as Record<string, unknown>,\n        inputs,\n        templateFormat\n      );\n    } else {\n      formatted[k] = v;\n    }\n  }\n  return formatted;\n}\n"],"mappings":";;;AAKA,IAAa,qBAAb,cAGUA,aAAAA,SAAsD;CAC9D,eAAe;EAAC;EAAkB;EAAW;CAAM;CAEnD,kBAAkB;CAElB;CAEA;CAEA;CAEA,OAAO,UAAU;EACf,OAAO;CACT;CAEA,YAAY,QAGT;EACD,MAAM,iBAAiB,OAAO,kBAAkB;EAChD,MAAM,iBAAiB,mBACrB,OAAO,UACP,cACF;EACA,MAAM;GAAE;GAAgB,GAAG;EAAO,CAAC;EACnC,KAAK,WAAW,OAAO;EACvB,KAAK,iBAAiB;EACtB,KAAK,iBAAiB;CACxB;CAEA,MAAM,OAAO,QAA8D;EAC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