import {
  App,
  Config,
  IMidwayApplication,
  Inject,
  Provide,
} from '@midwayjs/core';
import { KnowChromaStore } from './chroma';
import { KnowStoreBase } from './base';
import { KnowDataTypeService } from '../service/data/type';

/**
 * 存储器
 */
export const StoreType = {
  // Chroma 存储， 云端存储， 需要安装 Chroma 服务
  chroma: KnowChromaStore,
};

// 存储器类型键
export type StoreTypes = keyof typeof StoreType;

/**
 * 存储器
 */
@Provide()
export class KnowStore {
  @Config('module.know.store')
  store: StoreTypes;

  @Config('module.know.prefix')
  prefix: string;

  @Inject()
  knowDataTypeService: KnowDataTypeService;

  @App()
  app: IMidwayApplication;

  /**
   * 获得存储器
   * @param knowId
   */
  async get(knowId: number): Promise<KnowStoreBase> {
    const embedding = await this.knowDataTypeService.getEmbedding(knowId);
    const store: KnowStoreBase = await this.app
      .getApplicationContext()
      .getAsync(StoreType[this.store]);
    store.set(embedding);
    return store;
  }
}
