stock-bot/apps/stock/orchestrator/src/backtest/native-worker-protocol.ts

38 lines
1.2 KiB
TypeScript

import { z } from 'zod';
import { replayTraceSchema } from '../contracts/native-replay';
import { strategyReplayRequestSchema } from '../contracts/strategy-replay';
import { BacktestConfigSchema } from '../types';
const finite = z.number().finite();
const legacyRequestSchema = z.object({
type: z.literal('run'),
config: BacktestConfigSchema,
data: z.array(
z.object({
symbol: z.string(),
timestamp: finite,
type: z.literal('bar'),
open: finite,
high: finite,
low: finite,
close: finite,
volume: finite,
vwap: finite,
})
),
});
export const nativeWorkerRequestSchema = z.union([
legacyRequestSchema,
replayTraceSchema,
strategyReplayRequestSchema,
]);
export const nativeWorkerMessageSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('progress'), progress: finite.min(0).max(100), message: z.string() }),
z.object({ type: z.literal('result'), result: z.string() }),
z.object({ type: z.literal('error'), message: z.string() }),
]);
export type NativeWorkerRequest = z.input<typeof nativeWorkerRequestSchema>;
export type NativeWorkerMessage = z.infer<typeof nativeWorkerMessageSchema>;