{"version":3,"file":"in_memory.cjs","names":["Docstore"],"sources":["../../../src/stores/doc/in_memory.ts"],"sourcesContent":["import { Document } from \"@langchain/core/documents\";\nimport { BaseStoreInterface } from \"@langchain/core/stores\";\nimport { Docstore } from \"./base.js\";\n\n/**\n * Class for storing and retrieving documents in memory asynchronously.\n * Extends the Docstore class.\n */\nexport class InMemoryDocstore\n  extends Docstore\n  implements BaseStoreInterface<string, Document>\n{\n  _docs: Map<string, Document>;\n\n  constructor(docs?: Map<string, Document>) {\n    super();\n    this._docs = docs ?? new Map();\n  }\n\n  /**\n   * Searches for a document in the store based on its ID.\n   * @param search The ID of the document to search for.\n   * @returns The document with the given ID.\n   */\n  async search(search: string): Promise<Document> {\n    const result = this._docs.get(search);\n    if (!result) {\n      throw new Error(`ID ${search} not found.`);\n    } else {\n      return result;\n    }\n  }\n\n  /**\n   * Adds new documents to the store.\n   * @param texts An object where the keys are document IDs and the values are the documents themselves.\n   * @returns Void\n   */\n  async add(texts: Record<string, Document>): Promise<void> {\n    const keys = [...this._docs.keys()];\n    const overlapping = Object.keys(texts).filter((x) => keys.includes(x));\n\n    if (overlapping.length > 0) {\n      throw new Error(`Tried to add ids that already exist: ${overlapping}`);\n    }\n\n    for (const [key, value] of Object.entries(texts)) {\n      this._docs.set(key, value);\n    }\n  }\n\n  async mget(keys: string[]): Promise<Document[]> {\n    return Promise.all(keys.map((key) => this.search(key)));\n  }\n\n  async mset(keyValuePairs: [string, Document][]): Promise<void> {\n    await Promise.all(\n      keyValuePairs.map(([key, value]) => this.add({ [key]: value }))\n    );\n  }\n\n  async mdelete(_keys: string[]): Promise<void> {\n    throw new Error(\"Not implemented.\");\n  }\n\n  // oxlint-disable-next-line require-yield\n  async *yieldKeys(_prefix?: string): AsyncGenerator<string> {\n    throw new Error(\"Not implemented\");\n  }\n}\n\n/**\n * Class for storing and retrieving documents in memory synchronously.\n */\nexport class SynchronousInMemoryDocstore {\n  _docs: Map<string, Document>;\n\n  constructor(docs?: Map<string, Document>) {\n    this._docs = docs ?? new Map();\n  }\n\n  /**\n   * Searches for a document in the store based on its ID.\n   * @param search The ID of the document to search for.\n   * @returns The document with the given ID.\n   */\n  search(search: string): Document {\n    const result = this._docs.get(search);\n    if (!result) {\n      throw new Error(`ID ${search} not found.`);\n    } else {\n      return result;\n    }\n  }\n\n  /**\n   * Adds new documents to the store.\n   * @param texts An object where the keys are document IDs and the values are the documents themselves.\n   * @returns Void\n   */\n  add(texts: Record<string, Document>): void {\n    const keys = [...this._docs.keys()];\n    const overlapping = Object.keys(texts).filter((x) => keys.includes(x));\n\n    if (overlapping.length > 0) {\n      throw new Error(`Tried to add ids that already exist: ${overlapping}`);\n    }\n\n    for (const [key, value] of Object.entries(texts)) {\n      this._docs.set(key, value);\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;AAQA,IAAa,mBAAb,cACUA,wBAAAA,SAEV;CACE;CAEA,YAAY,MAA8B;AACxC,SAAO;AACP,OAAK,QAAQ,wBAAQ,IAAI,KAAK;;;;;;;CAQhC,MAAM,OAAO,QAAmC;EAC9C,MAAM,SAAS,KAAK,MAAM,IAAI,OAAO;AACrC,MAAI,CAAC,OACH,OAAM,IAAI,MAAM,MAAM,OAAO,aAAa;MAE1C,QAAO;;;;;;;CASX,MAAM,IAAI,OAAgD;EACxD,MAAM,OAAO,CAAC,GAAG,KAAK,MAAM,MAAM,CAAC;EACnC,MAAM,cAAc,OAAO,KAAK,MAAM,CAAC,QAAQ,MAAM,KAAK,SAAS,EAAE,CAAC;AAEtE,MAAI,YAAY,SAAS,EACvB,OAAM,IAAI,MAAM,wCAAwC,cAAc;AAGxE,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,MAAM,CAC9C,MAAK,MAAM,IAAI,KAAK,MAAM;;CAI9B,MAAM,KAAK,MAAqC;AAC9C,SAAO,QAAQ,IAAI,KAAK,KAAK,QAAQ,KAAK,OAAO,IAAI,CAAC,CAAC;;CAGzD,MAAM,KAAK,eAAoD;AAC7D,QAAM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