GCoordinator/src/shared/ws-protocol.ts

190 lines
4.5 KiB
TypeScript

import { z } from 'zod';
export type VaultState = 'uninitialized' | 'locked' | 'unlocked';
export type ServerConnectionStatus =
| 'disconnected'
| 'connecting'
| 'connected'
| 'tofu_pending'
| 'host_key_mismatch'
| 'auth_error'
| 'vault_locked'
| 'disabled';
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 PendingTofuInfo {
serverId: string;
hostPortKey: string;
host: string;
port: number;
algorithm: string;
fingerprint: string;
firstSeenAt: number;
}
export interface HostKeyAlert {
serverId: string;
hostPortKey: string;
host: string;
port: number;
algorithm: string;
expectedFingerprint: string;
actualFingerprint: string;
detectedAt: number;
}
export interface ServerRuntimeState {
serverId: string;
status: ServerConnectionStatus;
osDetected?: 'linux' | 'bsd' | 'macos';
lastError?: string;
connectedAt?: number;
tofuPending?: PendingTofuInfo;
hostKeyMismatch?: HostKeyAlert;
}
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;
vaultRuntimeReady?: boolean;
safeMode: boolean;
elevatedUntil: number | null;
}
| {
type: 'vault:state';
vaultState: VaultState;
vaultRuntimeReady: boolean;
isPanicLocked: boolean;
}
| {
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: 'servers:state'; states: ServerRuntimeState[] }
| {
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: 'workflow:run';
runId: string;
workflowId: string;
status: 'queued' | 'running' | 'succeeded' | 'failed' | 'cancelled' | 'skipped';
triggerSource: string;
startedAt: number;
finishedAt?: number | null;
}
| { type: 'ai:delta'; chatId: string; chunk: string; done?: boolean; error?: string };
export const WS_CHANNELS = {
METRICS: 'metrics',
ALERTS: 'alerts',
CONFIG: 'config',
WORKFLOWS: 'workflows',
pty: (sessionId: string) => `pty:${sessionId}`,
stream: (jobId: string) => `stream:${jobId}`,
ai: (chatId: string) => `ai:${chatId}`,
} as const;