Files
meta-mcp/src/storage.ts

68 lines
1.7 KiB
TypeScript

import { randomUUID } from 'crypto';
import fs from 'fs';
import path from 'path';
import type { MetaRequest, MetaResponse } from './types.js';
export type Job = {
id: string;
request: MetaRequest;
createdAt: number;
updatedAt: number;
response: MetaResponse;
};
const storagePath = '/root/.meta-mcp/jobs.json';
const jobs = new Map<string, Job>();
function persist() {
const dir = path.dirname(storagePath);
fs.mkdirSync(dir, { recursive: true });
fs.writeFileSync(storagePath, JSON.stringify([...jobs.values()], null, 2));
}
function hydrate() {
if (!fs.existsSync(storagePath)) return;
const raw = fs.readFileSync(storagePath, 'utf8');
const parsed = JSON.parse(raw) as Job[];
for (const job of parsed) {
jobs.set(job.id, job);
}
}
hydrate();
export function createJob(request: MetaRequest): Job {
const id = randomUUID();
const now = Date.now();
const response: MetaResponse = {
status: 'queued',
message: 'Request received. Preparing build pipeline.',
jobId: id,
};
const job: Job = { id, request, createdAt: now, updatedAt: now, response };
jobs.set(id, job);
persist();
return job;
}
export function updateJob(id: string, patch: Partial<MetaResponse>): Job | null {
const job = jobs.get(id);
if (!job) return null;
job.updatedAt = Date.now();
if (patch.logs?.length) {
const existing = job.response.logs ?? [];
patch.logs = [...existing, ...patch.logs].slice(-200);
}
job.response = { ...job.response, ...patch };
persist();
return job;
}
export function getJob(id: string): Job | null {
return jobs.get(id) ?? null;
}
export function listJobs(): Job[] {
return [...jobs.values()].sort((a, b) => b.createdAt - a.createdAt);
}