{"version":3,"file":"reduce.cjs","names":[],"sources":["../../../src/chains/combine_documents/reduce.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\n\n/**\n * Splits a list of documents into sublists based on a maximum token limit.\n *\n * @param {Document[]} docs - The list of documents to be split.\n * @param {Function} lengthFunc - A function that calculates the number of tokens in a list of documents.\n * @param {number} tokenMax - The maximum number of tokens allowed in a sublist.\n *\n * @returns {Document[][]} - A list of document sublists, each sublist contains documents whose total number of tokens does not exceed the tokenMax.\n *\n * @throws {Error} - Throws an error if a single document has more tokens than the tokenMax.\n */\nexport function splitListOfDocs(\n  docs: Document[],\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  lengthFunc: (...args: any[]) => any,\n  tokenMax: number\n): Document[][] {\n  const newResultDocList: Document[][] = [];\n  let subResultDocs: Document[] = [];\n  for (const doc of docs) {\n    subResultDocs.push(doc);\n    const numTokens = lengthFunc(subResultDocs);\n    if (numTokens > tokenMax) {\n      if (subResultDocs.length === 1) {\n        throw new Error(\n          \"A single document was longer than the context length, we cannot handle this.\"\n        );\n      }\n      newResultDocList.push(subResultDocs.slice(0, -1));\n      subResultDocs = subResultDocs.slice(-1);\n    }\n  }\n  newResultDocList.push(subResultDocs);\n  return newResultDocList;\n}\n\n/**\n * Collapses a list of documents into a single document.\n *\n * This function takes a list of documents and a function to combine the content of these documents.\n * It combines the content of the documents using the provided function and merges the metadata of all documents.\n * If a metadata key is present in multiple documents, the values are concatenated with a comma separator.\n *\n * @param {Document[]} docs - The list of documents to be collapsed.\n * @param {Function} combineDocumentFunc - A function that combines the content of a list of documents into a single string. This function should return a promise that resolves to the combined string.\n *\n * @returns {Promise<Document>} - A promise that resolves to a single document with combined content and merged metadata.\n *\n * @throws {Error} - Throws an error if the combineDocumentFunc does not return a promise or if the promise does not resolve to a string.\n */\nexport async function collapseDocs(\n  docs: Document[],\n  combineDocumentFunc: (docs: Document[]) => Promise<string>\n): Promise<Document> {\n  const result = await combineDocumentFunc(docs);\n  return { pageContent: result, metadata: collapseDocsMetadata(docs) };\n}\n\nfunction collapseDocsMetadata(docs: Document[]): Document[\"metadata\"] {\n  const combinedMetadata: Record<string, string> = {};\n  for (const key in docs[0].metadata) {\n    if (key in docs[0].metadata) {\n      combinedMetadata[key] = String(docs[0].metadata[key]);\n    }\n  }\n  for (const doc of docs.slice(1)) {\n    for (const key in doc.metadata) {\n      if (key in combinedMetadata) {\n        combinedMetadata[key] += `, ${doc.metadata[key]}`;\n      } else {\n        combinedMetadata[key] = String(doc.metadata[key]);\n      }\n    }\n  }\n  return combinedMetadata;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAaA,SAAgB,gBACd,MAEA,YACA,UACc;CACd,MAAM,mBAAiC,EAAE;CACzC,IAAI,gBAA4B,EAAE;AAClC,MAAK,MAAM,OAAO,MAAM;AACtB,gBAAc,KAAK,IAAI;AAEvB,MADkB,WAAW,cAAc,GAC3B,UAAU;AACxB,OAAI,cAAc,WAAW,EAC3B,OAAM,IAAI,MACR,+EACD;AAEH,oBAAiB,KAAK,cAAc,MAAM,GAAG,GAAG,CAAC;AACjD,mBAAgB,cAAc,MAAM,GAAG;;;AAG3C,kBAAiB,KAAK,cAAc;AACpC,QAAO;;;;;;;;;;;;;;;;AAiBT,eAAsB,aACpB,MACA,qBACmB;AAEnB,QAAO;EAAE,aADM,MAAM,oBAAoB,KAAK;EAChB,UAAU,qBAAqB,KAAK;EAAE;;AAGtE,SAAS,qBAAqB,MAAwC;CACpE,MAAM,mBAA2C,EAAE;AACnD,MAAK,MAAM,OAAO,KAAK,GAAG,SACxB,KAAI,OAAO,KAAK,GAAG,SACjB,kBAAiB,OAAO,OAAO,KAAK,GAAG,SAAS,KAAK;AAGzD,MAAK,MAAM,OAAO,KAAK,MAAM,EAAE,CAC7B,MAAK,MAAM,OAAO,IAAI,SACpB,KAAI,OAAO,iBACT,kBAAiB,QAAQ,KAAK,IAAI,SAAS;KAE3C,kBAAiB,OAAO,OAAO,IAAI,SAAS,KAAK;AAIvD,QAAO"}