mcp-server-debug-thinking
Version:
Graph-based MCP server for systematic debugging using Problem-Solution Trees and Hypothesis-Experiment-Learning cycles
123 lines • 3.48 kB
JavaScript
import fs from "fs/promises";
import { createReadStream } from "fs";
import * as readline from "readline";
import { logger } from "./logger.js";
export async function ensureDirectory(dirPath) {
try {
await fs.mkdir(dirPath, { recursive: true });
}
catch (error) {
logger.error(`Failed to create directory ${dirPath}:`, error);
throw error;
}
}
export async function readJsonFile(filePath) {
try {
const data = await fs.readFile(filePath, "utf-8");
return JSON.parse(data);
}
catch (error) {
if (error.code === "ENOENT") {
return null;
}
logger.error(`Failed to read JSON file ${filePath}:`, error);
throw error;
}
}
export async function writeJsonFile(filePath, data) {
try {
await fs.writeFile(filePath, JSON.stringify(data, null, 2));
}
catch (error) {
logger.error(`Failed to write JSON file ${filePath}:`, error);
throw error;
}
}
export async function listJsonFiles(dirPath) {
try {
const files = await fs.readdir(dirPath);
return files.filter((f) => f.endsWith(".json"));
}
catch (error) {
if (error.code === "ENOENT") {
return [];
}
logger.error(`Failed to list files in ${dirPath}:`, error);
throw error;
}
}
// JSONL operations
export async function appendJsonLine(filePath, data) {
try {
const line = `${JSON.stringify(data)}\n`;
await fs.appendFile(filePath, line, "utf-8");
}
catch (error) {
logger.error(`Failed to append to JSONL file ${filePath}:`, error);
throw error;
}
}
export async function readJsonLines(filePath) {
try {
const fileStream = createReadStream(filePath, { encoding: "utf-8" });
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
const lines = [];
for await (const line of rl) {
if (line.trim()) {
try {
lines.push(JSON.parse(line));
}
catch (_parseError) {
logger.warn(`Skipping invalid JSON line in ${filePath}: ${line}`);
}
}
}
return lines;
}
catch (error) {
if (error.code === "ENOENT") {
return [];
}
logger.error(`Failed to read JSONL file ${filePath}:`, error);
throw error;
}
}
export async function* readJsonLinesStream(filePath) {
try {
const fileStream = createReadStream(filePath, { encoding: "utf-8" });
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of rl) {
if (line.trim()) {
try {
yield JSON.parse(line);
}
catch (_parseError) {
logger.warn(`Skipping invalid JSON line in ${filePath}: ${line}`);
}
}
}
}
catch (error) {
if (error.code === "ENOENT") {
return;
}
logger.error(`Failed to stream JSONL file ${filePath}:`, error);
throw error;
}
}
export async function fileExists(filePath) {
try {
await fs.access(filePath);
return true;
}
catch {
return false;
}
}
//# sourceMappingURL=storage.js.map