Files
electric-cherry/src/routes/api/agent/heartbeat/+server.ts
T

24 lines
1.4 KiB
TypeScript

import { json } from '@sveltejs/kit';
import { heartbeat, pendingCreationCancellations, shouldDeferPlutoniumPreparation } from '$lib/server/hosts/index.js';
import { authenticateAgent, hostApiError, objectBody } from '$lib/server/hosts/http.js';
import { reconcileHostPreparation } from '$lib/server/hosts/preparation.js';
import { reconcileHostLibraryCleanup } from '$lib/server/game-files/index.js';
import { fleetSettings } from '$lib/server/settings.js';
export async function POST({ request }: { request: Request }) {
const authenticated = authenticateAgent(request);
try {
const body = await objectBody(request);
const host = heartbeat(authenticated.id, body.inventory);
const inventory = body.inventory as Record<string, unknown>;
if (Array.isArray(inventory.libraryCaches)) {
const { updateHostLibraryCaches } = await import('$lib/server/game-files/index.js');
updateHostLibraryCaches(host.id, inventory.libraryCaches, { snapshot: true });
}
reconcileHostPreparation(host.id);
reconcileHostLibraryCleanup(host.id);
return json({ host, cancelledCreations: pendingCreationCancellations(host.id), heartbeatSeconds: 15,
deferPlutoniumPreparation: shouldDeferPlutoniumPreparation(host.id),
observationKeepDays: fleetSettings().observationKeepDays, serverTime: new Date().toISOString() });
} catch (cause) { return hostApiError(cause); }
}