Complete DDS training workflow and delivery package
This commit is contained in:
parent
68dd83c7c2
commit
4c4b91064f
229 changed files with 11969 additions and 1024 deletions
|
|
@ -31,6 +31,7 @@ export interface Health {
|
|||
models_ready: boolean;
|
||||
embeddings_ready: boolean;
|
||||
scenarios_loaded: number;
|
||||
sessions_restored?: number;
|
||||
offline: boolean;
|
||||
demo_no_db?: boolean;
|
||||
}
|
||||
|
|
@ -42,10 +43,13 @@ export interface ScenarioSummary {
|
|||
level: Level;
|
||||
topics: string[];
|
||||
modes: SessionMode[];
|
||||
signs: string[];
|
||||
incident_code: string | null;
|
||||
dds: string | null;
|
||||
ticket: number | null;
|
||||
position: number | null;
|
||||
ekp_group: number | null;
|
||||
can_manage: boolean;
|
||||
}
|
||||
|
||||
export interface SessionInfo {
|
||||
|
|
@ -78,6 +82,7 @@ export interface TraineeInfo {
|
|||
id: string;
|
||||
name: string;
|
||||
group: string | null;
|
||||
service: string | null;
|
||||
}
|
||||
|
||||
export interface Attempt {
|
||||
|
|
@ -92,6 +97,7 @@ export interface Attempt {
|
|||
facts_required: number | null;
|
||||
hints: number | null;
|
||||
codes: Record<string, number>;
|
||||
recording_available: boolean;
|
||||
}
|
||||
|
||||
export interface Profile {
|
||||
|
|
@ -139,8 +145,12 @@ export function useSigns(prefix: string[], group: number | null = null) {
|
|||
});
|
||||
}
|
||||
|
||||
export const useTrainees = () =>
|
||||
useQuery({ queryKey: ["trainees"], queryFn: () => request<TraineeInfo[]>("/api/trainees") });
|
||||
export const useTrainees = (enabled = true) =>
|
||||
useQuery({
|
||||
queryKey: ["trainees"],
|
||||
queryFn: () => request<TraineeInfo[]>("/api/trainees"),
|
||||
enabled,
|
||||
});
|
||||
|
||||
export interface GroupInfo {
|
||||
id: string;
|
||||
|
|
@ -163,8 +173,19 @@ export interface GroupAnalytics {
|
|||
}[];
|
||||
}
|
||||
|
||||
export const useGroups = () =>
|
||||
useQuery({ queryKey: ["groups"], queryFn: () => request<GroupInfo[]>("/api/groups") });
|
||||
export interface GroupInsight {
|
||||
summary: string;
|
||||
priorities: string[];
|
||||
source: "local_qwen";
|
||||
personal_data_sent: false;
|
||||
}
|
||||
|
||||
export const useGroups = (enabled = true) =>
|
||||
useQuery({
|
||||
queryKey: ["groups"],
|
||||
queryFn: () => request<GroupInfo[]>("/api/groups"),
|
||||
enabled,
|
||||
});
|
||||
|
||||
export function useCreateGroup() {
|
||||
const cache = useQueryClient();
|
||||
|
|
@ -196,6 +217,14 @@ export const useGroupAnalytics = (groupId: string | null) =>
|
|||
refetchInterval: 15_000,
|
||||
});
|
||||
|
||||
export function useGroupInsight() {
|
||||
return useMutation({
|
||||
mutationFn: (groupId: string) => request<GroupInsight>(
|
||||
`/api/groups/${groupId}/analytics/insight`, { method: "POST" },
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export const useProfile = (traineeId: string | null) =>
|
||||
useQuery({
|
||||
queryKey: ["profile", traineeId],
|
||||
|
|
@ -208,3 +237,121 @@ export const useCreateSession = () =>
|
|||
mutationFn: (body: { scenario_id: string; mode: SessionMode; trainee?: string; group?: string }) =>
|
||||
request<SessionInfo>("/api/sessions", { method: "POST", body: JSON.stringify(body) }),
|
||||
});
|
||||
|
||||
export interface LearningMaterial {
|
||||
id: string;
|
||||
title: string;
|
||||
description: string;
|
||||
level: Level;
|
||||
kind: "text" | "file";
|
||||
body: string;
|
||||
scenario_id: string | null;
|
||||
file_name: string | null;
|
||||
media_type: string | null;
|
||||
file_size: number;
|
||||
file_sha256: string | null;
|
||||
active: boolean;
|
||||
created_by: string;
|
||||
created_at: string;
|
||||
assigned_at: string | null;
|
||||
completed_at: string | null;
|
||||
assignment_count: number;
|
||||
}
|
||||
|
||||
export interface MaterialCreateInput {
|
||||
title: string;
|
||||
description: string;
|
||||
level: Level;
|
||||
kind: "text" | "file";
|
||||
body: string;
|
||||
scenario_id: string | null;
|
||||
file_name?: string;
|
||||
media_type?: string;
|
||||
content_base64?: string;
|
||||
}
|
||||
|
||||
export const useMaterials = (includeArchived = false) => useQuery({
|
||||
queryKey: ["materials", includeArchived],
|
||||
queryFn: () => request<LearningMaterial[]>(
|
||||
`/api/materials${includeArchived ? "?include_archived=true" : ""}`,
|
||||
),
|
||||
});
|
||||
|
||||
function invalidateMaterials(cache: ReturnType<typeof useQueryClient>) {
|
||||
return cache.invalidateQueries({ queryKey: ["materials"] });
|
||||
}
|
||||
|
||||
export function useCreateMaterial() {
|
||||
const cache = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (body: MaterialCreateInput) => request<LearningMaterial>("/api/materials", {
|
||||
method: "POST", body: JSON.stringify(body),
|
||||
}),
|
||||
onSuccess: () => void invalidateMaterials(cache),
|
||||
});
|
||||
}
|
||||
|
||||
export function useUpdateMaterial() {
|
||||
const cache = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ id, patch }: { id: string; patch: Partial<LearningMaterial> }) =>
|
||||
request<LearningMaterial>(`/api/materials/${id}`, {
|
||||
method: "PATCH", body: JSON.stringify(patch),
|
||||
}),
|
||||
onSuccess: () => void invalidateMaterials(cache),
|
||||
});
|
||||
}
|
||||
|
||||
export function useArchiveMaterial() {
|
||||
const cache = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => request<LearningMaterial>(`/api/materials/${id}`, {
|
||||
method: "DELETE",
|
||||
}),
|
||||
onSuccess: () => void invalidateMaterials(cache),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignMaterial() {
|
||||
const cache = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ materialId, traineeId }: { materialId: string; traineeId: string }) =>
|
||||
request<LearningMaterial>(`/api/materials/${materialId}/assign/${traineeId}`, {
|
||||
method: "PUT",
|
||||
}),
|
||||
onSuccess: () => void invalidateMaterials(cache),
|
||||
});
|
||||
}
|
||||
|
||||
export function useAssignMaterialGroup() {
|
||||
const cache = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: ({ materialId, groupId }: { materialId: string; groupId: string }) =>
|
||||
request<{ material_id: string; assigned: number }>(
|
||||
`/api/materials/${materialId}/assign-group/${groupId}`, { method: "PUT" },
|
||||
),
|
||||
onSuccess: () => void invalidateMaterials(cache),
|
||||
});
|
||||
}
|
||||
|
||||
export function useCompleteMaterial() {
|
||||
const cache = useQueryClient();
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => request<LearningMaterial>(`/api/materials/${id}/complete`, {
|
||||
method: "POST",
|
||||
}),
|
||||
onSuccess: () => void invalidateMaterials(cache),
|
||||
});
|
||||
}
|
||||
|
||||
export function useStartMaterialPractice() {
|
||||
return useMutation({
|
||||
mutationFn: (id: string) => request<{
|
||||
session_id: string;
|
||||
scenario_id: string;
|
||||
mode: "self";
|
||||
exercise: "card";
|
||||
path: string;
|
||||
}>(`/api/materials/${id}/start`, { method: "POST" }),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,25 +15,33 @@ import type {
|
|||
|
||||
export type ChannelStatus = "connecting" | "open" | "reconnecting" | "closed";
|
||||
|
||||
interface ChannelOptions<In> {
|
||||
interface ChannelOptions<In, Out> {
|
||||
onEvent?: (event: In) => void;
|
||||
/** Бинарный кадр: звук звонящего, без обёртки JSON. */
|
||||
onBinary?: (frame: ArrayBuffer) => void;
|
||||
onStatus?: (status: ChannelStatus) => void;
|
||||
/** Ключ безопасного идемпотентного события, которое можно повторить после
|
||||
* краткого разрыва. null запрещает буферизацию команды. */
|
||||
queueKey?: (event: Out) => string | null;
|
||||
}
|
||||
|
||||
const RECONNECT_MIN_MS = 500;
|
||||
const RECONNECT_MAX_MS = 5_000;
|
||||
const BUFFER_MAX_AGE_MS = 35_000;
|
||||
const BUFFER_MAX_ITEMS = 100;
|
||||
|
||||
interface Buffered<Out> { key: string; event: Out; at: number }
|
||||
|
||||
export class Channel<In extends { type: string }, Out extends { type: string }> {
|
||||
private socket: WebSocket | null = null;
|
||||
private attempt = 0;
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
private closedByUs = false;
|
||||
private buffered: Buffered<Out>[] = [];
|
||||
|
||||
constructor(
|
||||
private readonly path: string,
|
||||
private readonly options: ChannelOptions<In> = {},
|
||||
private readonly options: ChannelOptions<In, Out> = {},
|
||||
) {}
|
||||
|
||||
connect(): this {
|
||||
|
|
@ -51,6 +59,10 @@ export class Channel<In extends { type: string }, Out extends { type: string }>
|
|||
socket.onopen = () => {
|
||||
this.attempt = 0;
|
||||
this.options.onStatus?.("open");
|
||||
const now = Date.now();
|
||||
const pending = this.buffered.filter((item) => now - item.at <= BUFFER_MAX_AGE_MS);
|
||||
this.buffered = [];
|
||||
for (const item of pending) socket.send(JSON.stringify(item.event));
|
||||
};
|
||||
socket.onmessage = (message) => {
|
||||
if (typeof message.data !== "string") {
|
||||
|
|
@ -78,11 +90,32 @@ export class Channel<In extends { type: string }, Out extends { type: string }>
|
|||
return this.socket?.readyState === WebSocket.OPEN;
|
||||
}
|
||||
|
||||
// Пока соединения нет, события не копятся: истина — на сервере, и после
|
||||
// переподключения интерфейс получит актуальное состояние, а не очередь старых правок.
|
||||
send(event: Out): boolean {
|
||||
if (!this.isOpen) return false;
|
||||
this.socket!.send(JSON.stringify(event));
|
||||
if (this.isOpen) {
|
||||
this.socket!.send(JSON.stringify(event));
|
||||
return true;
|
||||
}
|
||||
const key = this.options.queueKey?.(event) ?? null;
|
||||
if (!key) return false;
|
||||
const existing = this.buffered.find((item) => item.key === key);
|
||||
if (existing) {
|
||||
// Частые kio.patch объединяем: последнее значение каждого поля важнее,
|
||||
// но правки разных полей не должны затереть друг друга.
|
||||
if (existing.event.type === "kio.patch" && event.type === "kio.patch") {
|
||||
const previousPatch = existing.event as unknown as { fields: Record<string, unknown> };
|
||||
const nextPatch = event as unknown as { fields: Record<string, unknown> };
|
||||
existing.event = {
|
||||
...existing.event,
|
||||
fields: { ...previousPatch.fields, ...nextPatch.fields },
|
||||
} as Out;
|
||||
} else {
|
||||
existing.event = event;
|
||||
}
|
||||
existing.at = Date.now();
|
||||
} else {
|
||||
this.buffered.push({ key, event, at: Date.now() });
|
||||
if (this.buffered.length > BUFFER_MAX_ITEMS) this.buffered.shift();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -96,22 +129,36 @@ export class Channel<In extends { type: string }, Out extends { type: string }>
|
|||
this.closedByUs = true;
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
this.socket?.close();
|
||||
this.buffered = [];
|
||||
}
|
||||
}
|
||||
|
||||
export const callChannel = (sessionId: string, options?: ChannelOptions<ServerToTrainee>) =>
|
||||
new Channel<ServerToTrainee, TraineeToServer>(`/ws/call/${sessionId}`, options);
|
||||
export const callChannel = (
|
||||
sessionId: string,
|
||||
options?: ChannelOptions<ServerToTrainee, TraineeToServer>,
|
||||
) => new Channel<ServerToTrainee, TraineeToServer>(`/ws/call/${sessionId}`, {
|
||||
...options,
|
||||
queueKey: (event) => event.type === "kio.patch" ? "kio.patch" : null,
|
||||
});
|
||||
|
||||
/** Наблюдатель: отправлять нечего и нельзя — `Out` равен `never`. */
|
||||
export const observeChannel = (sessionId: string, options?: ChannelOptions<ServerToObserver>) =>
|
||||
export const observeChannel = (sessionId: string, options?: ChannelOptions<ServerToObserver, never>) =>
|
||||
new Channel<ServerToObserver, never>(`/ws/observe/${sessionId}`, options);
|
||||
|
||||
/** Станция ДДС: только JSON, аудио здесь нет вообще. */
|
||||
export const stationChannel = (
|
||||
sessionId: string,
|
||||
role: string,
|
||||
options?: ChannelOptions<ServerToStation>,
|
||||
) => new Channel<ServerToStation, StationToServer>(`/ws/station/${sessionId}?role=${role}`, options);
|
||||
options?: ChannelOptions<ServerToStation, StationToServer>,
|
||||
) => new Channel<ServerToStation, StationToServer>(`/ws/station/${sessionId}?role=${role}`, {
|
||||
...options,
|
||||
queueKey: (event) => {
|
||||
if (event.type === "card.reply") return `card.reply:${event.card_id}`;
|
||||
if (event.type === "crew.select") return "crew.select";
|
||||
if (event.type === "zone.decision") return "zone.decision";
|
||||
return null;
|
||||
},
|
||||
});
|
||||
|
||||
/** Преподаватель: только передача, входящих в этом канале нет. */
|
||||
export const controlChannel = (sessionId: string, options?: { onStatus?: (status: ChannelStatus) => void }) =>
|
||||
|
|
|
|||
Loading…
Reference in a new issue