feat: machine and instance storage

This commit is contained in:
Cristina Poncela Cubeiro
2026-06-18 13:09:48 +02:00
parent 92e28e9c51
commit 5f60fc01d7
4 changed files with 85 additions and 1 deletions
+2
View File
@@ -2,3 +2,5 @@ export * from "./config.ts";
export * from "./ipc/client.ts";
export * from "./ipc/protocol.ts";
export * from "./ipc/server.ts";
export * from "./storage.ts";
export * from "./types.ts";
+3 -1
View File
@@ -1,3 +1,5 @@
import type { InstanceStatus } from "../types.ts";
export interface SpawnRequest {
type: "spawn";
cwd: string;
@@ -31,7 +33,7 @@ export type OrchestratorRequest = RequestMap[keyof RequestMap];
export interface InstanceSummary {
id: string;
status: "starting" | "online" | "stopping" | "stopped" | "error";
status: InstanceStatus;
cwd: string;
label?: string;
sessionId?: string;
+62
View File
@@ -0,0 +1,62 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { getInstancesPath, getMachinePath, getOrchestratorDir } from "./config.ts";
import type { InstanceRecord, MachineRecord } from "./types.ts";
function ensureOrchestratorDir(): void {
const orchestratorDir = getOrchestratorDir();
if (!existsSync(orchestratorDir)) {
mkdirSync(orchestratorDir, { recursive: true });
}
}
export function loadMachine(): MachineRecord | undefined {
const machinePath = getMachinePath();
if (!existsSync(machinePath)) {
return undefined;
}
const data = readFileSync(machinePath, "utf-8");
return JSON.parse(data) as MachineRecord;
}
export function saveMachine(machine: MachineRecord): void {
ensureOrchestratorDir();
writeFileSync(getMachinePath(), JSON.stringify(machine, null, 2));
}
export function loadInstances(): InstanceRecord[] {
const instancesPath = getInstancesPath();
if (!existsSync(instancesPath)) {
return [];
}
const data = readFileSync(instancesPath, "utf-8");
return JSON.parse(data) as InstanceRecord[];
}
export function saveInstances(instances: InstanceRecord[]): void {
ensureOrchestratorDir();
writeFileSync(getInstancesPath(), JSON.stringify(instances, null, 2));
}
export function getInstance(instanceId: string): InstanceRecord | undefined {
return loadInstances().find((instance) => instance.id === instanceId);
}
export function upsertInstance(instance: InstanceRecord): void {
const instances = loadInstances();
const index = instances.findIndex((existing) => existing.id === instance.id);
if (index === -1) {
instances.push(instance);
saveInstances(instances);
return;
}
instances[index] = instance;
saveInstances(instances);
}
export function removeInstance(instanceId: string): void {
const instances = loadInstances().filter((instance) => instance.id !== instanceId);
saveInstances(instances);
}
+18
View File
@@ -0,0 +1,18 @@
export type InstanceStatus = "starting" | "online" | "stopping" | "stopped" | "error";
export interface MachineRecord {
id: string;
createdAt: string;
lastSeenAt?: string;
label?: string;
}
export interface InstanceRecord {
id: string;
status: InstanceStatus;
cwd: string;
createdAt: string;
lastSeenAt?: string;
label?: string;
sessionId?: string;
}