{"version":3,"file":"structured.cjs","names":["ChatPromptTemplate","RunnableBinding"],"sources":["../../src/prompts/structured.ts"],"sourcesContent":["import { ChatPromptValueInterface } from \"../prompt_values.js\";\nimport { RunnableLike, Runnable, RunnableBinding } from \"../runnables/base.js\";\nimport { RunnableConfig } from \"../runnables/config.js\";\nimport { InputValues } from \"../utils/types/index.js\";\nimport {\n  BaseMessagePromptTemplateLike,\n  ChatPromptTemplate,\n  ChatPromptTemplateInput,\n} from \"./chat.js\";\n\nfunction isWithStructuredOutput(x: unknown): x is {\n  withStructuredOutput: (...arg: unknown[]) => Runnable;\n} {\n  return (\n    typeof x === \"object\" &&\n    x != null &&\n    \"withStructuredOutput\" in x &&\n    typeof x.withStructuredOutput === \"function\"\n  );\n}\n\nfunction isRunnableBinding(x: unknown): x is RunnableBinding<unknown, unknown> {\n  return (\n    typeof x === \"object\" &&\n    x != null &&\n    \"lc_id\" in x &&\n    Array.isArray(x.lc_id) &&\n    x.lc_id.join(\"/\") === \"langchain_core/runnables/RunnableBinding\"\n  );\n}\n\n/**\n * Interface for the input of a ChatPromptTemplate.\n */\nexport interface StructuredPromptInput<\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 ChatPromptTemplateInput<RunInput, PartialVariableName> {\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  schema: Record<string, any>;\n  method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\";\n}\n\nexport class StructuredPrompt<\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>\n  extends ChatPromptTemplate<RunInput, PartialVariableName>\n  implements StructuredPromptInput<RunInput, PartialVariableName>\n{\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  schema: Record<string, any>;\n\n  method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\";\n\n  lc_namespace = [\"langchain_core\", \"prompts\", \"structured\"];\n\n  get lc_aliases(): Record<string, string> {\n    return {\n      ...super.lc_aliases,\n      schema: \"schema_\",\n    };\n  }\n\n  constructor(input: StructuredPromptInput<RunInput, PartialVariableName>) {\n    super(input);\n    this.schema = input.schema;\n    this.method = input.method;\n  }\n\n  pipe<NewRunOutput>(\n    coerceable: RunnableLike<ChatPromptValueInterface, NewRunOutput>\n  ): Runnable<RunInput, Exclude<NewRunOutput, Error>, RunnableConfig> {\n    if (isWithStructuredOutput(coerceable)) {\n      return super.pipe(coerceable.withStructuredOutput(this.schema));\n    }\n\n    if (\n      isRunnableBinding(coerceable) &&\n      isWithStructuredOutput(coerceable.bound)\n    ) {\n      return super.pipe(\n        new RunnableBinding({\n          bound: coerceable.bound.withStructuredOutput(\n            this.schema,\n            ...(this.method ? [{ method: this.method }] : [])\n          ),\n          kwargs: coerceable.kwargs ?? {},\n          config: coerceable.config,\n          configFactories: coerceable.configFactories,\n        })\n      );\n    }\n\n    throw new Error(\n      `Structured prompts need to be piped to a language model that supports the \"withStructuredOutput()\" method.`\n    );\n  }\n\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  static fromMessagesAndSchema<RunInput extends InputValues = any>(\n    promptMessages: (\n      | ChatPromptTemplate<InputValues, string>\n      | BaseMessagePromptTemplateLike\n    )[],\n    schema: StructuredPromptInput[\"schema\"],\n    method?: \"jsonMode\" | \"jsonSchema\" | \"functionMode\"\n    // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  ): ChatPromptTemplate<RunInput, any> {\n    return StructuredPrompt.fromMessages<\n      RunInput,\n      StructuredPromptInput<RunInput>\n    >(promptMessages, { schema, method });\n  }\n}\n"],"mappings":";;;AAUA,SAAS,uBAAuB,GAE9B;AACA,QACE,OAAO,MAAM,YACb,KAAK,QACL,0BAA0B,KAC1B,OAAO,EAAE,yBAAyB;;AAItC,SAAS,kBAAkB,GAAoD;AAC7E,QACE,OAAO,MAAM,YACb,KAAK,QACL,WAAW,KACX,MAAM,QAAQ,EAAE,MAAM,IACtB,EAAE,MAAM,KAAK,IAAI,KAAK;;AAkB1B,IAAa,mBAAb,MAAa,yBAMHA,aAAAA,mBAEV;CAEE;CAEA;CAEA,eAAe;EAAC;EAAkB;EAAW;EAAa;CAE1D,IAAI,aAAqC;AACvC,SAAO;GACL,GAAG,MAAM;GACT,QAAQ;GACT;;CAGH,YAAY,OAA6D;AACvE,QAAM,MAAM;AACZ,OAAK,SAAS,MAAM;AACpB,OAAK,SAAS,MAAM;;CAGtB,KACE,YACkE;AAClE,MAAI,uBAAuB,WAAW,CACpC,QAAO,MAAM,KAAK,WAAW,qBAAqB,KAAK,OAAO,CAAC;AAGjE,MACE,kBAAkB,WAAW,IAC7B,uBAAuB,WAAW,MAAM,CAExC,QAAO,MAAM,KACX,IAAIC,aAAAA,gBAAgB;GAClB,OAAO,WAAW,MAAM,qBACtB,KAAK,QACL,GAAI,KAAK,SAAS,CAAC,EAAE,QAAQ,KAAK,QAAQ,CAAC,GAAG,EAAE,CACjD;GACD,QAAQ,WAAW,UAAU,EAAE;GAC/B,QAAQ,WAAW;GACnB,iBAAiB,WAAW;GAC7B,CAAC,CACH;AAGH,QAAM,IAAI,MACR,6GACD;;CAIH,OAAO,sBACL,gBAIA,QACA,QAEmC;AACnC,SAAO,iBAAiB,aAGtB,gBAAgB;GAAE;GAAQ;GAAQ,CAAC"}