{"version":3,"sources":["../src/template.ts"],"names":["PromptTemplateError","FrameworkError","template","constructor","message","context","ValidationPromptTemplateError","PromptTemplate","Serializable","config","functions","trim","text","render","replace","defaults","schema","toJsonSchema","escape","Boolean","customTags","register","validateInput","input","validator","createSchemaValidator","coerceTypes","success","errors","fork","customizer","clone","newConfig","isPlainObject","ValueError","updatedInput","Object","assign","pickBy","_","k","getProp","undefined","view","output","Mustache","tags","identity","match","loadSnapshot","data","createSnapshot"],"mappings":";;;;;;;;;;;;;;;AA2DO,MAAMA,4BAA+CC,yBAAAA,CAAAA;EA3D5D;;;AA4DEC,EAAAA,QAAAA;EAEAC,WAAYC,CAAAA,OAAAA,EAAiBF,UAA6BG,OAAsB,EAAA;AAC9E,IAAMD,KAAAA,CAAAA,OAAAA,EAAS,EAAI,EAAA;AACjBC,MAAAA;KACF,CAAA;AACA,IAAA,IAAA,CAAKH,QAAWA,GAAAA,QAAAA;AAClB;AACF;AAEO,MAAMI,sCAAyDN,mBAAAA,CAAAA;EAtEtE;;;AAsE8F;AAEvF,MAAMO,uBAA0CC,6BAAAA,CAAAA;EAxEvD;;;AAyEYC,EAAAA,MAAAA;AAEV,EAAA,OAAcC,SAAY,GAAA;IACxBC,IAAM,kBAAA,MAAA,CAAA,MAAM,CAACC,IAAAA,EAAcC,MAAAA,KAAAA;AACzB,MAAA,OAAOA,MAAOD,CAAAA,IAAAA,CAAME,CAAAA,OAAAA,CAAQ,YAAY,EAAA,CAAA;KADpC,EAAA,MAAA;AAGR,GAAA;AAEAX,EAAAA,WAAAA,CAAYM,MAA+E,EAAA;AACzF,IAAK,KAAA,EAAA;AACL,IAAA,IAAA,CAAKA,MAAS,GAAA;MACZ,GAAGA,MAAAA;MACHM,QAAWN,EAAAA,MAAAA,CAAOM,YAAY,EAAC;MAC/BC,MAAQC,EAAAA,uBAAAA,CAAaR,OAAOO,MAAM,CAAA;MAClCE,MAAQC,EAAAA,OAAAA,CAAQV,OAAOS,MAAM,CAAA;AAC7BE,MAAA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,KAAAA,kBAAAA,CAAQN,YAAc,EAAA;AAACK,MAAAA;AAAE,KAAA,CAAA,KAAME,MAAAA,CAAAA,CAAAA;AAGxE,IAAA,IAAA,CAAKpB,cAAca,YAAAA,CAAAA;AACnB,IAAA,MAAMQ,IAA4B,GAAA;AAChC,MAAA,GAAG,KAAKlC,MAAOC,CAAAA,SAAAA;MACf,GAAGyB;AACL,KAAA;AAEA,IAAMS,MAAAA,MAAAA,GAASC,0BAAShC,MACtB,CAAA,IAAA,CAAKJ,OAAOP,QACZyC,EAAAA,IAAAA,EACA,EACA,EAAA;AACEG,MAAAA,IAAAA,EAAM,KAAKrC,MAAOW,CAAAA,UAAAA;MAClB,GAAI,CAAC,IAAKX,CAAAA,MAAAA,CAAOS,MAAU,IAAA;AACzBA,QAAAA,MAAAA,EAAQ6B,eAAAA;AACV;KACF,CAAA;AAGF,IAAIH,IAAAA,MAAAA,CAAOI,KAAM,CAAA,gBAAA,CAAmB,EAAA;AAClC,MAAM,MAAA,IAAI1C,6BACR,CAAA,0DAAA,EACA,IACA,EAAA;AACEsC,QAAAA;OACF,CAAA;AAEJ;AAEA,IAAOA,OAAAA,MAAAA;AACT;AAEAK,EAAAA,YAAAA,CAAaC,IAA8C,EAAA;AACzD,IAAA,IAAA,CAAKzC,SAASyC,IAAKzC,CAAAA,MAAAA;AACrB;EAEA0C,cAAiB,GAAA;AACf,IAAO,OAAA;AACL1C,MAAAA,MAAAA,EAAQ,IAAKA,CAAAA;AACf,KAAA;AACF;AACF","file":"template.cjs","sourcesContent":["/**\n * Copyright 2025 IBM Corp.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n *     http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { FrameworkError, ValueError } from \"@/errors.js\";\nimport { ObjectLike, PlainObject } from \"@/internals/types.js\";\nimport { clone, identity, isPlainObject, pickBy } from \"remeda\";\nimport Mustache from \"mustache\";\nimport { Serializable } from \"@/internals/serializable.js\";\nimport { z, ZodType } from \"zod\";\nimport { createSchemaValidator, toJsonSchema } from \"@/internals/helpers/schema.js\";\nimport type { SchemaObject, ValidateFunction } from \"ajv\";\nimport { getProp } from \"@/internals/helpers/object.js\";\n\ntype PostInfer<T> = T extends PlainObject\n  ? {\n      [K in keyof T]: T[K] extends Date ? string : T[K];\n    }\n  : T;\ntype InferValue<T> = T extends ZodType<infer A> ? PostInfer<A> : never;\nexport type PromptTemplateRenderFn<K extends ZodType> = (this: InferValue<K>) => any;\n\nexport type PromptTemplateRenderInput<T extends ZodType, T2 extends z.input<T> = z.input<T>> = {\n  [K in Extract<keyof T2, string>]: T2[K] | PromptTemplateRenderFn<T> | undefined;\n};\n\nexport interface PromptTemplateInput<T extends ZodType> {\n  template: string;\n  customTags?: [string, string];\n  escape?: boolean;\n  schema: SchemaObject;\n  defaults?: Partial<InferValue<T>>;\n  functions?: Record<string, PromptTemplateRenderFn<T>>;\n}\n\ntype PromptTemplateConstructor<T extends ZodType, N> = N extends ZodType\n  ? Omit<PromptTemplateInput<N>, \"schema\" | \"functions\" | \"defaults\"> & {\n      schema: N;\n      functions?: Record<string, PromptTemplateRenderFn<N | T>>;\n      defaults?: Partial<InferValue<N | T>>;\n    }\n  : Omit<PromptTemplateInput<T>, \"schema\"> & { schema: T | SchemaObject };\n\ntype Customizer<T extends ZodType, N> =\n  | ((config: Required<PromptTemplateInput<T>>) => PromptTemplateConstructor<T, N>)\n  | ((config: Required<PromptTemplateInput<T>>) => void);\n\nexport class PromptTemplateError<T extends ZodType> extends FrameworkError {\n  template: PromptTemplate<T>;\n\n  constructor(message: string, template: PromptTemplate<T>, context?: ObjectLike) {\n    super(message, [], {\n      context,\n    });\n    this.template = template;\n  }\n}\n\nexport class ValidationPromptTemplateError<T extends ZodType> extends PromptTemplateError<T> {}\n\nexport class PromptTemplate<T extends ZodType> extends Serializable {\n  protected config: Required<PromptTemplateInput<T>>;\n\n  public static functions = {\n    trim: () => (text: string, render: (value: string) => string) => {\n      return render(text).replace(/(,\\s*$)/g, \"\");\n    },\n  };\n\n  constructor(config: Omit<PromptTemplateInput<T>, \"schema\"> & { schema: T | SchemaObject }) {\n    super();\n    this.config = {\n      ...config,\n      defaults: (config.defaults ?? {}) as Partial<InferValue<T>>,\n      schema: toJsonSchema(config.schema),\n      escape: Boolean(config.escape),\n      customTags: config.customTags ?? [\"{{\", \"}}\"],\n      functions: {\n        ...PromptTemplate.functions,\n        ...config.functions,\n      },\n    };\n  }\n\n  static {\n    this.register();\n  }\n\n  validateInput(input: unknown): asserts input is T {\n    const validator = createSchemaValidator(this.config.schema, {\n      coerceTypes: false,\n    }) as ValidateFunction<T>;\n\n    const success = validator(input);\n    if (!success) {\n      throw new ValidationPromptTemplateError(\n        `Template cannot be rendered because input does not adhere to the template schema.`,\n        this,\n        {\n          errors: validator.errors,\n        },\n      );\n    }\n  }\n\n  fork(customizer: Customizer<T, SchemaObject>): PromptTemplate<T>;\n  fork<R extends ZodType>(customizer: Customizer<T, R>): PromptTemplate<R>;\n  fork<R extends ZodType>(\n    customizer: Customizer<T, SchemaObject> | Customizer<T, R>,\n  ): PromptTemplate<T | R> {\n    const config = clone(this.config);\n    const newConfig = customizer?.(config) ?? config;\n    if (!isPlainObject(newConfig)) {\n      throw new ValueError(\"Return type from customizer must be a config or nothing.\");\n    }\n    return new PromptTemplate(newConfig);\n  }\n\n  render(input: PromptTemplateRenderInput<T>): string {\n    const updatedInput: typeof input = { ...input };\n    Object.assign(\n      updatedInput,\n      pickBy(this.config.defaults, (_, k) => getProp(updatedInput, [k]) === undefined),\n    );\n\n    this.validateInput(updatedInput);\n    const view: Record<string, any> = {\n      ...this.config.functions,\n      ...updatedInput,\n    };\n\n    const output = Mustache.render(\n      this.config.template,\n      view,\n      {},\n      {\n        tags: this.config.customTags,\n        ...(!this.config.escape && {\n          escape: identity(),\n        }),\n      },\n    );\n\n    if (output.match(/\\[object (.+)]/)) {\n      throw new ValidationPromptTemplateError(\n        \"Rendered template contains incorrectly serialized value!\",\n        this,\n        {\n          output,\n        },\n      );\n    }\n\n    return output;\n  }\n\n  loadSnapshot(data: ReturnType<typeof this.createSnapshot>) {\n    this.config = data.config;\n  }\n\n  createSnapshot() {\n    return {\n      config: this.config,\n    };\n  }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-namespace\nexport namespace PromptTemplate {\n  export type infer<T> = PromptTemplate<ZodType<T>>;\n}\n"]}