src / storageLocation.ts

import { homedir } from "node:os";
import { isAbsolute, join } from "node:path";
import { DocumentStore } from "./store";

export function storageDirectory(env: NodeJS.ProcessEnv = process.env): string {
  const override = env.LMSTUDIO_FILE_SEARCH_DATA_DIR;
  if (override !== undefined) {
    if (!isAbsolute(override)) throw new Error("LMSTUDIO_FILE_SEARCH_DATA_DIR must be an absolute path.");
    return override;
  }
  return join(homedir(), ".lmstudio", "plugin-data", "yuanjumay", "file-search");
}

// Tool discovery and preprocessing do not always receive a working directory,
// and can receive different prediction controllers. Both must use the same
// persistent location, independent of controller and process working directory.
export function createDocumentStore(): DocumentStore {
  return new DocumentStore(storageDirectory());
}