feat(安全培训): 新增员工安全培训相关功能

This commit is contained in:
runny committed 2026-08-12 11:56:35 +08:00
1 parent cbc3cb959f
commit 8e5c57790c
50 files changed
+3769 -87

No files matched your search

@@ -1,12 +1,18 @@
/**
* features/chat/training/training.types.ts
* 用途:定义入职培训聊天工作区的题库、考试进度、HR 统计记录等共享类型。
* 依赖:被 useTrainingWorkspace 与 TrainingWorkspacePanel 共同引用。
* 逻辑:统一约束“学员考试流”和“HR 管理流”的前端数据结构,避免组件内散落匿名对象。
* 用途:定义员工培训工作区的共享类型,覆盖入职培训、安全培训、考试过程和 HR 统计记录。
* 依赖:被 useTrainingWorkspace、training.api 与 TrainingWorkspacePanel 共同引用。
* 逻辑:统一约束“多培训类型学员流程”和“HR 管理流”的前端数据结构,避免组件内散落匿名对象。
*/
export type TrainingType = 'onboarding' | 'safety';
export type TrainingStage = 'video' | 'summary' | 'exam' | 'result';
export type TrainingExamMode = 'unified' | 'per_level';
export type SafetyStage = 'company' | 'department' | 'team' | 'exam' | 'result';
export type TrainingWorkspaceView = 'learner' | 'admin';
export type TrainingQuestionType = 'single' | 'boolean';
@@ -32,6 +38,33 @@ export interface TrainingContentConfig {
questionBank: TrainingQuestion[];
}
export interface TrainingPublishedConfigResource {
id?: number | null;
resourceType?: string | null;
trainingLevel?: Exclude<SafetyStage, 'exam' | 'result'> | null;
agentGuideId?: number | null;
inlineQuestions?: unknown[];
title?: string | null;
sortOrder?: number;
requiredDuration?: number;
resource?: {
id?: number;
fileName?: string;
fileType?: string;
fileUrl?: string;
guideUsage?: string;
} | null;
}
export interface TrainingPublishedConfig {
id: number;
trainingType: TrainingType;
version: string;
passScore: number;
settings: Record<string, unknown>;
resources: TrainingPublishedConfigResource[];
}
export interface TrainingAnswerRecord {
questionId: string;
selectedOptionId: string;
@@ -50,8 +83,17 @@ export interface TrainingExamTransitionFeedback {
export type TrainingRecordStatus = 'passed' | 'failed' | 'in_progress';
export interface SafetyProgressState {
company: string;
department: string;
team: string;
resource_progress?: Record<string, { duration: number; completed: boolean }>;
}
export interface TrainingRecord {
id: string | number;
trainingType?: TrainingType;
trainingVersion?: string;
name: string;
employeeId: string;
department: string;
@@ -64,6 +106,9 @@ export interface TrainingRecord {
export interface TrainingProgressRecord extends TrainingRecord {
stage: TrainingStage;
examMode: TrainingExamMode;
safetyStage: SafetyStage | null;
safetyProgress: SafetyProgressState;
currentQuestionIndex: number;
passed: boolean;
answerDetails: TrainingAnswerRecord[];
@@ -4,7 +4,7 @@
* 依赖:无运行时副作用,仅依赖 Agent extra_config 普通对象与 training.types。
* 逻辑:统一判断某个 Agent 是否启用培训工作区,并解析培训视频、题库与复盘配置。
*/
import type { TrainingContentConfig, TrainingQuestion, TrainingQuestionOption } from './training.types';
import type { TrainingContentConfig, TrainingPublishedConfig, TrainingQuestion, TrainingQuestionOption } from './training.types';
type TrainingAgentLike = {
name?: string | null;
@@ -24,6 +24,11 @@ export type TrainingGuideLike = {
} | null | undefined;
export const TRAINING_CONTENT_PROFILE = 'training';
export const SAFETY_STAGE_GUIDE_USAGE_MAP = {
company: 'training_safety_company_video',
department: 'training_safety_department_video',
team: 'training_safety_team_video',
} as const;
/**
* 判断当前 Agent 是否应启用培训工作区。
@@ -143,6 +148,30 @@ export function resolveTrainingVideoGuide(guides: TrainingGuideLike[] | null | u
);
}
/**
* 按安全培训当前级别查找对应视频指南。
* AI 触发时机:当员工在安全培训中切换“公司级 / 部门级 / 班组级”阶段时,工作区必须优先展示该级别绑定的视频,而不是沿用通用安全培训视频。
*/
export function resolveSafetyTrainingVideoGuide(
guides: TrainingGuideLike[] | null | undefined,
safetyStage: keyof typeof SAFETY_STAGE_GUIDE_USAGE_MAP | 'exam' | 'result',
preferredGuideIds: number[] = [],
): TrainingGuideLike {
const normalized = Array.isArray(guides) ? guides : [];
const stageUsage = safetyStage in SAFETY_STAGE_GUIDE_USAGE_MAP
? SAFETY_STAGE_GUIDE_USAGE_MAP[safetyStage as keyof typeof SAFETY_STAGE_GUIDE_USAGE_MAP]
: null;
return (
normalized.find((guide) => guide?.id != null && preferredGuideIds.includes(Number(guide.id)) && guide?.file_type === 'video' && (!stageUsage || guide?.guide_usage === stageUsage))
|| normalized.find((guide) => guide?.id != null && preferredGuideIds.includes(Number(guide.id)) && guide?.file_type === 'video')
|| (stageUsage
? normalized.find((guide) => guide?.guide_usage === stageUsage && guide?.file_type === 'video')
: null)
|| normalized.find((guide) => guide?.guide_usage === 'training_safety_video' && guide?.file_type === 'video')
|| null
);
}
/**
* 查找培训工作区优先使用的试题 PDF。
* AI 触发时机:当员工培训页需要为学员提供“查看试题 PDF”入口时使用,避免把普通指南 PDF 错当成考试资料。
@@ -163,24 +192,35 @@ export function resolveTrainingContentConfig(
agent: TrainingAgentLike,
fallback: TrainingContentConfig,
booleanOptions: TrainingQuestionOption[],
publishedConfig?: TrainingPublishedConfig | null,
): TrainingContentConfig {
if (!publishedConfig) {
return { passScore: fallback.passScore, summaryPoints: [], summaryChecklist: [], questionBank: [] };
}
const extraConfig = asRecord(agent?.extra_config);
const trainingConfig = asRecord(extraConfig.training_config);
const mergedConfig = {
...extraConfig,
...trainingConfig,
...(publishedConfig.settings || {}),
};
const publishedQuestions = (publishedConfig?.resources || [])
.filter((resource) => resource.resourceType === 'inline_questions')
.flatMap((resource) => Array.isArray(resource.inlineQuestions) ? resource.inlineQuestions : []);
const summaryPoints = normalizeStringList(mergedConfig.summary_points ?? mergedConfig.summaryPoints);
const summaryChecklist = normalizeStringList(mergedConfig.summary_checklist ?? mergedConfig.summaryChecklist);
const questionBank = normalizeQuestionBank(
mergedConfig.question_bank ?? mergedConfig.questionBank,
publishedQuestions.length
? publishedQuestions
: mergedConfig.questions ?? mergedConfig.question_bank ?? mergedConfig.questionBank,
booleanOptions,
);
return {
passScore: normalizePassScore(
mergedConfig.pass_score ?? mergedConfig.passScore,
publishedConfig?.passScore ?? mergedConfig.pass_score ?? mergedConfig.passScore,
fallback.passScore || DEFAULT_PASS_SCORE,
),
summaryPoints: summaryPoints.length ? summaryPoints : fallback.summaryPoints,