feat(core): add shared Zod config/WS schemas and SQLite WAL migrations

This commit is contained in:
tyuXX 2026-11-26 18:19:33 +00:00
parent 48676a1285
commit 2c60483a90
12 changed files with 1007 additions and 0 deletions

139
src/shared/ws-protocol.ts Normal file
View file

@ -0,0 +1,139 @@
import { z } from 'zod';
export type VaultState = 'uninitialized' | 'locked' | 'unlocked';
export interface ConfigWarning {
filePath: string;
code: string;
message: string;
}
export interface ConfigError {
filePath: string;
message: string;
issues?: Array<{ path: string; message: string }>;
}
export interface MetricSample {
ts: number;
cpuPct: number;
memUsedMb: number;
memTotalMb: number;
netRxBps: number;
netTxBps: number;
load1m: number;
uptimeSec: number;
}
export interface DiskSample {
mountPoint: string;
fsType: string;
usedMb: number;
totalMb: number;
usedPct: number;
}
export interface ProbeSample {
probeId: string;
ok: boolean;
statusCode?: number;
latencyMs: number;
error?: string;
}
export interface AlertEventSummary {
id: string;
ruleId: string;
serverId?: string;
severity: 'warn' | 'critical' | 'resolved';
message: string;
ts: number;
}
export interface HostKeyAlert {
serverId: string;
host: string;
port: number;
expectedFingerprint: string;
actualFingerprint: string;
}
export const ClientWsFrameSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('ping'),
ts: z.number().finite(),
}),
z.object({
type: z.literal('sub'),
channels: z.array(z.string().min(1).max(128)).max(64),
}),
z.object({
type: z.literal('unsub'),
channels: z.array(z.string().min(1).max(128)).max(64),
}),
z.object({
type: z.literal('safe-mode'),
enabled: z.boolean(),
}),
z.object({
type: z.literal('pty:input'),
sessionId: z.string().min(1).max(64),
data: z.string().max(65536),
}),
z.object({
type: z.literal('pty:resize'),
sessionId: z.string().min(1).max(64),
cols: z.number().int().min(10).max(500),
rows: z.number().int().min(4).max(200),
}),
z.object({
type: z.literal('ai:cancel'),
chatId: z.string().min(1).max(64),
}),
]);
export type ClientWsFrame = z.infer<typeof ClientWsFrameSchema>;
export type ServerWsFrame =
| { type: 'pong'; ts: number; serverTs: number }
| {
type: 'hello';
vaultState: VaultState;
safeMode: boolean;
elevatedUntil: number | null;
}
| {
type: 'config:updated';
entityType: 'server' | 'system' | 'workflow' | 'snippets' | 'alerts' | 'settings' | 'all';
id?: string;
warnings: ConfigWarning[];
errors: ConfigError[];
}
| { type: 'security:panic-lock' }
| { type: 'error'; code: string; message: string }
| {
type: 'metrics:tick';
serverId: string;
sample: MetricSample;
disks: DiskSample[];
probes?: ProbeSample[];
}
| { type: 'pty:output'; sessionId: string; data: string; replay?: boolean }
| { type: 'pty:exit'; sessionId: string; exitCode: number | null }
| { type: 'stream:chunk'; jobId: string; stream: 'stdout' | 'stderr' | 'meta'; data: string }
| {
type: 'alerts:update';
activeCount: number;
event?: AlertEventSummary;
hostKeyMismatch?: HostKeyAlert;
}
| { type: 'ai:delta'; chatId: string; chunk: string; done?: boolean; error?: string };
export const WS_CHANNELS = {
METRICS: 'metrics',
ALERTS: 'alerts',
CONFIG: 'config',
pty: (sessionId: string) => `pty:${sessionId}`,
stream: (jobId: string) => `stream:${jobId}`,
ai: (chatId: string) => `ai:${chatId}`,
} as const;