PLUGIN

Report

1 star

2 Downloads

Give LLM the ability to run JavaScript/TypeScript code in a sandboxed environment using Deno.

src / findLMStudioHome.ts

import { existsSync, readFileSync, realpathSync, writeFileSync } from "fs";
import { homedir } from "os";
import { join } from "path";

let lmstudioHome: string | null = null;

export function findLMStudioHome() {
  if (lmstudioHome !== null) {
    return lmstudioHome;
  }

  // if applicable, convert relative path to absolute and follow the symlink
  const resolvedHomeDir = realpathSync(homedir());

  const pointerFilePath = join(resolvedHomeDir, ".lmstudio-home-pointer");
  if (existsSync(pointerFilePath)) {
    lmstudioHome = readFileSync(pointerFilePath, "utf-8").trim();
    return lmstudioHome;
  }

  // See if ~/.cache/lm-studio exists. If it does, use it.
  const cacheHome = join(resolvedHomeDir, ".cache", "lm-studio");
  if (existsSync(cacheHome)) {
    lmstudioHome = cacheHome;
    writeFileSync(pointerFilePath, lmstudioHome, "utf-8");
    return lmstudioHome;
  }

  // Otherwise, fallback to ~/.lmstudio
  const home = join(resolvedHomeDir, ".lmstudio");
  lmstudioHome = home;
  writeFileSync(pointerFilePath, lmstudioHome, "utf-8");
  return lmstudioHome;
}