{"version":3,"file":"CachedStorageService.mjs","names":[],"sources":["../../../src/modules/cache/CachedStorageService.ts"],"sourcesContent":["import { AgentContext } from '../../agent'\nimport { InjectionSymbols } from '../../constants'\nimport { inject, injectable } from '../../plugins'\nimport { BaseRecord } from '../../storage/BaseRecord'\nimport type { BaseRecordConstructor, Query, QueryOptions, StorageService } from '../../storage/StorageService'\nimport { JsonTransformer } from '../../utils'\nimport { CacheModuleConfig } from './CacheModuleConfig'\n\n@injectable()\n// biome-ignore lint/suspicious/noExplicitAny: no explanation\nexport class CachedStorageService<T extends BaseRecord<any, any, any>> implements StorageService<T> {\n  public readonly supportsCursorPagination = false\n\n  public constructor(@inject(InjectionSymbols.StorageService) private storageService: StorageService<T>) {}\n\n  private cache(agentContext: AgentContext) {\n    return agentContext.resolve(CacheModuleConfig).cache\n  }\n\n  private getCacheKey(options: { type: string; id: string }) {\n    return `${options.type}:${options.id}`\n  }\n\n  public async save(agentContext: AgentContext, record: T): Promise<void> {\n    if (record.allowCache) {\n      await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON())\n    }\n\n    return await this.storageService.save(agentContext, record)\n  }\n\n  public async update(agentContext: AgentContext, record: T): Promise<void> {\n    if (record.allowCache) {\n      await this.cache(agentContext).set(agentContext, this.getCacheKey(record), record.toJSON())\n    }\n\n    return await this.storageService.update(agentContext, record)\n  }\n\n  public async delete(agentContext: AgentContext, record: T): Promise<void> {\n    if (record.allowCache) {\n      await this.cache(agentContext).remove(agentContext, this.getCacheKey(record))\n    }\n    return await this.storageService.delete(agentContext, record)\n  }\n\n  public async deleteById(\n    agentContext: AgentContext,\n    recordClass: BaseRecordConstructor<T>,\n    id: string\n  ): Promise<void> {\n    if (recordClass.allowCache) {\n      await this.cache(agentContext).remove(agentContext, this.getCacheKey({ ...recordClass, id }))\n    }\n    return await this.storageService.deleteById(agentContext, recordClass, id)\n  }\n\n  public async getById(agentContext: AgentContext, recordClass: BaseRecordConstructor<T>, id: string): Promise<T> {\n    if (recordClass.allowCache) {\n      const cachedValue = await this.cache(agentContext).get<T>(\n        agentContext,\n        this.getCacheKey({ type: recordClass.type, id })\n      )\n\n      if (cachedValue) return JsonTransformer.fromJSON<T>(cachedValue, recordClass)\n    }\n\n    const record = await this.storageService.getById(agentContext, recordClass, id)\n\n    if (recordClass.allowCache) {\n      await this.cache(agentContext).set(\n        agentContext,\n        this.getCacheKey({ type: recordClass.type, id }),\n        record.toJSON()\n      )\n    }\n\n    return record\n  }\n\n  // TODO: not in caching interface, yet\n  public async getAll(agentContext: AgentContext, recordClass: BaseRecordConstructor<T>): Promise<T[]> {\n    return await this.storageService.getAll(agentContext, recordClass)\n  }\n\n  // TODO: not in caching interface, yet\n  public async findByQuery(\n    agentContext: AgentContext,\n    recordClass: BaseRecordConstructor<T>,\n    query: Query<T>,\n    queryOptions?: QueryOptions\n  ): Promise<T[]> {\n    return await this.storageService.findByQuery(agentContext, recordClass, query, queryOptions)\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;AAUO,iCAAM,qBAAuF;CAGlG,AAAO,YAAY,AAAiD,gBAAmC;EAAnC;OAFpD,2BAA2B;;CAI3C,AAAQ,MAAM,cAA4B;AACxC,SAAO,aAAa,QAAQ,kBAAkB,CAAC;;CAGjD,AAAQ,YAAY,SAAuC;AACzD,SAAO,GAAG,QAAQ,KAAK,GAAG,QAAQ;;CAGpC,MAAa,KAAK,cAA4B,QAA0B;AACtE,MAAI,OAAO,WACT,OAAM,KAAK,MAAM,aAAa,CAAC,IAAI,cAAc,KAAK,YAAY,OAAO,EAAE,OAAO,QAAQ,CAAC;AAG7F,SAAO,MAAM,KAAK,eAAe,KAAK,cAAc,OAAO;;CAG7D,MAAa,OAAO,cAA4B,QAA0B;AACxE,MAAI,OAAO,WACT,OAAM,KAAK,MAAM,aAAa,CAAC,IAAI,cAAc,KAAK,YAAY,OAAO,EAAE,OAAO,QAAQ,CAAC;AAG7F,SAAO,MAAM,KAAK,eAAe,OAAO,cAAc,OAAO;;CAG/D,MAA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