Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

1.5 KB
Raw Blame
import { existsSync, mkdirSync, readFileSync, writeFileSync, readdirSync, rmSync } from "node:fs";
import { join } from "node:path";
import { resolveDataDir } from "../data-dir.mjs";

function sessionsDir() {
  const dir = join(resolveDataDir(), "repl-sessions");
  if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
  return dir;
}

export function saveSession(name, session) {
  const path = join(sessionsDir(), `${name}.json`);
  writeFileSync(
    path,
    JSON.stringify({ ...session, name, updatedAt: new Date().toISOString() }, null, 2)
  );
}

export function loadSession(name) {
  const path = join(sessionsDir(), `${name}.json`);
  if (!existsSync(path)) throw new Error(`session '${name}' not found`);
  return JSON.parse(readFileSync(path, "utf8"));
}

export function listSessions() {
  const dir = sessionsDir();
  return readdirSync(dir)
    .filter((f) => f.endsWith(".json"))
    .map((f) => {
      try {
        const data = JSON.parse(readFileSync(join(dir, f), "utf8"));
        return {
          name: data.name || f.replace(".json", ""),
          updatedAt: data.updatedAt,
          model: data.model,
        };
      } catch {
        return { name: f.replace(".json", ""), updatedAt: null, model: null };
      }
    });
}

export function autosave(session) {
  try {
    saveSession("autosave", session);
  } catch {
    // autosave failure is not fatal
  }
}

export function deleteSession(name) {
  const path = join(sessionsDir(), `${name}.json`);
  if (existsSync(path)) rmSync(path);
}