104 lines
3.5 KiB
TypeScript
104 lines
3.5 KiB
TypeScript
import { Hono } from 'hono';
|
|
import { z } from 'zod';
|
|
import { getVerifiedHttpPrincipal } from '@stock-bot/di';
|
|
import {
|
|
paperActivationSchema,
|
|
paperConnectionSchema,
|
|
paperFrameSchema,
|
|
paperHaltSchema,
|
|
PaperRuntimeError,
|
|
type PaperReadiness,
|
|
} from '../../contracts/paper-runtime';
|
|
import { ExecutionConflictError } from '../../execution/repository';
|
|
import type { OrchestratorServices } from '../../types';
|
|
|
|
export function createPaperRoutes(services: OrchestratorServices): Hono {
|
|
const app = new Hono();
|
|
const runtime = () => {
|
|
const value = services.custom?.CanonicalPaperRuntime;
|
|
if (!value) {
|
|
throw new PaperRuntimeError('not-ready', 'Canonical paper execution is not configured');
|
|
}
|
|
return value;
|
|
};
|
|
app.use('*', async (c, next) => {
|
|
c.header('Cache-Control', 'no-store');
|
|
if (c.req.method !== 'GET') {
|
|
const principal = getVerifiedHttpPrincipal(c.req.raw);
|
|
const feed = c.req.path.includes('/feed/');
|
|
if (principal !== 'operator' && !(feed && principal === 'service')) {
|
|
return c.json(
|
|
{ error: feed ? 'Verified feed producer required' : 'Human operator required' },
|
|
403
|
|
);
|
|
}
|
|
}
|
|
await next();
|
|
return undefined;
|
|
});
|
|
app.onError((error, c) => {
|
|
if (error instanceof z.ZodError || error instanceof SyntaxError) {
|
|
return c.json({ error: 'Invalid paper request' }, 400);
|
|
}
|
|
if (error instanceof PaperRuntimeError) {
|
|
return c.json(
|
|
{ error: error.message, code: error.kind },
|
|
error.kind === 'invalid-input' ? 400 : 409
|
|
);
|
|
}
|
|
if (error instanceof ExecutionConflictError) {
|
|
return c.json({ error: error.message, code: 'conflict' }, 409);
|
|
}
|
|
services.logger.error('Canonical paper request failed', { error });
|
|
return c.json({ error: 'Canonical paper runtime unavailable' }, 503);
|
|
});
|
|
app.get('/readiness', async c => {
|
|
if (!services.custom?.CanonicalPaperRuntime) {
|
|
const unavailable: PaperReadiness = {
|
|
configured: false,
|
|
accountId: null,
|
|
sourceId: null,
|
|
active: false,
|
|
ready: false,
|
|
operatorHalted: true,
|
|
connected: false,
|
|
reasons: ['not-configured'],
|
|
lastReceivedAt: null,
|
|
lastAvailableAt: null,
|
|
requiresHistoricalRecovery: false,
|
|
pendingInput: null,
|
|
pendingBatch: null,
|
|
pendingDecisionSequence: null,
|
|
nativeStatus: null,
|
|
risk: null,
|
|
};
|
|
return c.json(unavailable);
|
|
}
|
|
return c.json(await runtime().status());
|
|
});
|
|
app.post('/activate', async c =>
|
|
c.json(await runtime().activate(paperActivationSchema.parse(await c.req.json())))
|
|
);
|
|
app.post('/halt', async c =>
|
|
c.json(await runtime().halt(paperHaltSchema.parse(await c.req.json()).reason))
|
|
);
|
|
app.post('/feed/connect', async c =>
|
|
c.json(await runtime().connect(paperConnectionSchema.parse(await c.req.json())))
|
|
);
|
|
app.post('/feed/disconnect', async c =>
|
|
c.json(await runtime().disconnect(paperConnectionSchema.parse(await c.req.json())))
|
|
);
|
|
app.post('/feed/frames', async c => {
|
|
const input: unknown = await c.req.json();
|
|
let frame;
|
|
try {
|
|
frame = paperFrameSchema.parse(input);
|
|
} catch {
|
|
// Decimal transforms can throw ordinary errors. Classify only input
|
|
// parsing here; storage/native failures must still surface as unavailable.
|
|
throw new PaperRuntimeError('invalid-input', 'Invalid paper frame');
|
|
}
|
|
return c.json(await runtime().ingest(frame));
|
|
});
|
|
return app;
|
|
}
|