{"version":3,"file":"custom_format.cjs","names":["PromptTemplate"],"sources":["../../../src/experimental/prompts/custom_format.ts"],"sourcesContent":["import type { InputValues } from \"@langchain/core/utils/types\";\nimport {\n  type ParsedFStringNode,\n  PromptTemplate,\n  type PromptTemplateInput,\n  TypedPromptInputValues,\n} from \"@langchain/core/prompts\";\n\nexport type CustomFormatPromptTemplateInput<RunInput extends InputValues> =\n  Omit<PromptTemplateInput<RunInput, string>, \"templateFormat\"> & {\n    customParser: (template: string) => ParsedFStringNode[];\n    templateValidator?: (template: string, inputVariables: string[]) => boolean;\n    renderer: (template: string, values: InputValues) => string;\n  };\n\nexport class CustomFormatPromptTemplate<\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  RunInput extends InputValues = any,\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  PartialVariableName extends string = any,\n> extends PromptTemplate<RunInput, PartialVariableName> {\n  static lc_name() {\n    return \"CustomPromptTemplate\";\n  }\n\n  lc_serializable = false;\n\n  templateValidator?: (template: string, inputVariables: string[]) => boolean;\n\n  renderer: (template: string, values: InputValues) => string;\n\n  constructor(input: CustomFormatPromptTemplateInput<RunInput>) {\n    super(input);\n    Object.assign(this, input);\n\n    if (this.validateTemplate && this.templateValidator !== undefined) {\n      let totalInputVariables: string[] = this.inputVariables;\n      if (this.partialVariables) {\n        totalInputVariables = totalInputVariables.concat(\n          Object.keys(this.partialVariables)\n        );\n      }\n      if (typeof this.template === \"string\") {\n        this.templateValidator(this.template, totalInputVariables);\n      } else {\n        throw new Error(\n          `Must pass in string as template. Received: ${this.template}`\n        );\n      }\n    }\n  }\n\n  /**\n   * Load prompt template from a template\n   */\n  static fromTemplate<\n    // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n    RunInput extends InputValues = Record<string, any>,\n  >(\n    template: string,\n    {\n      customParser,\n      ...rest\n    }: Omit<\n      CustomFormatPromptTemplateInput<RunInput>,\n      \"template\" | \"inputVariables\"\n    >\n  ) {\n    const names = new Set<string>();\n    const nodes = customParser(template);\n    for (const node of nodes) {\n      if (node.type === \"variable\") {\n        names.add(node.name);\n      }\n    }\n    return new this<RunInput extends Symbol ? never : RunInput>({\n      // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n      inputVariables: [...names] as any[],\n      template,\n      customParser,\n      ...rest,\n    });\n  }\n\n  /**\n   * Formats the prompt template with the provided values.\n   * @param values The values to be used to format the prompt template.\n   * @returns A promise that resolves to a string which is the formatted prompt.\n   */\n  async format(values: TypedPromptInputValues<RunInput>): Promise<string> {\n    const allValues = await this.mergePartialAndUserVariables(values);\n    if (typeof this.template === \"string\") {\n      return this.renderer(this.template, allValues);\n    } else {\n      throw new Error(\n        `Must pass in string as template. Received: ${this.template}`\n      );\n    }\n  }\n}\n"],"mappings":";;;;;AAeA,IAAa,6BAAb,cAKUA,wBAAAA,eAA8C;CACtD,OAAO,UAAU;AACf,SAAO;;CAGT,kBAAkB;CAElB;CAEA;CAEA,YAAY,OAAkD;AAC5D,QAAM,MAAM;AACZ,SAAO,OAAO,MAAM,MAAM;AAE1B,MAAI,KAAK,oBAAoB,KAAK,sBAAsB,KAAA,GAAW;GACjE,IAAI,sBAAgC,KAAK;AACzC,OAAI,KAAK,iBACP,uBAAsB,oBAAoB,OACxC,OAAO,KAAK,KAAK,iBAAiB,CACnC;AAEH,OAAI,OAAO,KAAK,aAAa,SAC3B,MAAK,kBAAkB,KAAK,UAAU,oBAAoB;OAE1D,OAAM,IAAI,MACR,8CAA8C,KAAK,WACpD;;;;;;CAQP,OAAO,aAIL,UACA,EACE,cACA,GAAG,QAKL;EACA,MAAM,wBAAQ,IAAI,KAAa;EAC/B,MAAM,QAAQ,aAAa,SAAS;AACpC,OAAK,MAAM,QAAQ,MACjB,KAAI,KAAK,SAAS,WAChB,OAAM,IAAI,KAAK,KAAK;AAGxB,SAAO,IAAI,KAAiD;GAE1D,gBAAgB,CAAC,GAAG,MAAM;GAC1B;GACA;GACA,GAAG;GACJ,CAAC;;;;;;;CAQJ,MAAM,OAAO,QAA2D;EACtE,MAAM,YAAY,MAAM,KAAK,6BAA6B,OAAO;AACjE,MAAI,OAAO,KAAK,aAAa,SAC3B,QAAO,KAAK,SAAS,KAAK,UAAU,UAAU;MAE9C,OAAM,IAAI,MACR,8CAA8C,KAAK,WACpD"}