UNPKG

@agentkai/core

Version:

AgentKai核心包,提供AI助手系统的基础功能

59 lines (58 loc) 1.83 kB
import { Logger } from '../utils/logger'; /** * 存储抽象类 * 提供通用的数据存储接口,可由不同的具体存储实现(文件系统、浏览器IndexedDB等) */ export class StorageProvider { /** * 创建存储实例 * @param basePath 存储基础路径 * @param name 存储名称(用于日志) */ constructor(basePath, name) { Object.defineProperty(this, "logger", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "basePath", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.basePath = basePath; this.logger = new Logger(`Storage:${name}`); } /** * 获取存储基础路径 * @returns 存储基础路径 */ getBasePath() { return this.basePath; } /** * 保存数据 * 支持两种调用方式: * 1. save(data) - 直接保存带有id的数据对象 * 2. save(id, data) - 指定id和数据对象分别保存 * * @param idOrData 数据对象或者数据ID * @param data 当第一个参数为ID时的数据对象 */ async save(idOrData, data) { if (typeof idOrData === 'string' && data) { // 形式: save(id, data) const objectWithId = { ...data, id: idOrData }; return this.saveData(objectWithId); } else if (typeof idOrData === 'object' && idOrData.id) { // 形式: save(data) return this.saveData(idOrData); } else { throw new Error('Invalid arguments: save requires either an ID and data object, or a data object with an ID'); } } }