feat(training): 培训配置改造

This commit is contained in:
runny committed 2026-09-03 08:45:05 +08:00
1 parent d21c83433e
commit 15225ef1ce
15 files changed
+602 -56

No files matched your search

@@ -64,19 +64,39 @@ const showVideoModal = computed({
});
const lastVideoTime = ref(0);
const stageStep = computed(() => {
if (workspace.stage === 'video') return 1;
if (workspace.stage === 'summary') return 2;
if (workspace.stage === 'exam') return 3;
return 4;
const learnerStageOrder = ['video', 'summary', 'exam', 'result'] as const;
const learnerStageIconMap = {
video: PlayCircle,
summary: BookOpen,
exam: ClipboardCheck,
result: ShieldCheck,
} as const;
const currentLearnerStageIndex = computed(() => learnerStageOrder.indexOf(workspace.stage));
const showLearnerProgressModule = computed(() => workspace.learnerProgressItems.length > 0);
const learnerProgressItems = computed(() => workspace.learnerProgressItems.map((item, index) => {
const stageIndex = learnerStageOrder.indexOf(item.stage as typeof learnerStageOrder[number]);
const status = stageIndex >= 0 && currentLearnerStageIndex.value >= 0
? currentLearnerStageIndex.value > stageIndex
? 'completed'
: currentLearnerStageIndex.value === stageIndex
? 'active'
: 'pending'
: 'pending';
return {
...item,
icon: learnerStageIconMap[item.stage as keyof typeof learnerStageIconMap] || BookOpen,
orderLabel: String(index + 1).padStart(2, '0'),
status,
};
}));
const learnerStepIntroTitle = computed(() => {
const [title] = String(workspace.learnerStepIntroText || '').split(/\r?\n/);
return title?.trim() || '';
});
const learnerStepIntroDescription = computed(() => {
const [, ...restLines] = String(workspace.learnerStepIntroText || '').split(/\r?\n/);
return restLines.join('\n').trim();
});
const stageItems = computed(() => ([
{ key: 'video', label: 'chat.training.steps.video', icon: PlayCircle },
{ key: 'summary', label: 'chat.training.steps.summary', icon: BookOpen },
{ key: 'exam', label: 'chat.training.steps.exam', icon: ClipboardCheck },
{ key: 'result', label: 'chat.training.steps.result', icon: ShieldCheck },
]));
const userExpandedOverride = ref<boolean | null>(null);
// HR 管理员与普通员工首次进入培训 Agent 时都默认展开工作区;新建对话仍按既有约束强制收起。
@@ -327,7 +347,9 @@ watch(
const isCompactMode = computed(() => !workspaceExpanded.value);
const isLearnerExamStage = computed(() => !isCompactMode.value && workspace.activeView === 'learner' && workspace.stage === 'exam');
const learnerWorkspaceGridClass = computed(() => (
'grid gap-2.5 p-3 md:grid-cols-[minmax(0,1.7fr)_208px] md:p-4 lg:gap-3 lg:grid-cols-[minmax(0,1.85fr)_220px] 2xl:grid-cols-[minmax(0,1.95fr)_240px]'
workspace.stage === 'exam' || workspace.stage === 'result' || showLearnerProgressModule.value
? 'grid gap-2.5 p-3 md:grid-cols-[minmax(0,1.7fr)_208px] md:p-4 lg:gap-3 lg:grid-cols-[minmax(0,1.85fr)_220px] 2xl:grid-cols-[minmax(0,1.95fr)_240px]'
: 'grid gap-2.5 p-3 md:p-4'
));
const learnerWorkspaceMainClass = computed(() => 'min-w-0 space-y-3');
const progressCardClass = computed(() => 'rounded-2xl border border-default bg-white p-2.5 shadow-sm');
@@ -344,7 +366,7 @@ const compactSummary = computed(() => {
if (workspace.activeView === 'admin') {
return `${workspace.stats.total}${t('chat.training.hr.stats.total')} / ${workspace.stats.passRate}% ${t('chat.training.hr.stats.passRate')}`;
}
if (workspace.stage === 'video') return t('chat.training.video.title');
if (workspace.stage === 'video') return learnerStepIntroTitle.value || t('chat.training.video.title');
if (workspace.stage === 'summary') return t('chat.training.summary.title');
if (workspace.stage === 'exam') {
return t('chat.training.exam.questionCounter', {
@@ -688,14 +710,14 @@ const currentSafetyStageText = computed(() => {
</div>
<template v-else-if="workspace.learnerTab !== 'safety'">
<div :class="learnerWorkspaceMainClass">
<div :class="progressCardClass">
<div v-if="showLearnerProgressModule" :class="progressCardClass">
<div class="flex flex-col gap-1.5 lg:flex-row lg:items-start lg:justify-between">
<div>
<div class="text-xs font-semibold uppercase tracking-[0.18em] text-brand-600">
{{ $t('chat.training.progressLabel') }}
</div>
<div class="mt-0.5 text-[11px] leading-4.5 text-secondary md:text-xs">
{{ $t('chat.training.progressHint') }}
<div v-if="workspace.learnerProgressHintText" class="mt-0.5 text-[11px] leading-4.5 text-secondary md:text-xs">
{{ workspace.learnerProgressHintText }}
</div>
</div>
<div class="rounded-lg bg-surface-soft px-2.5 py-1 text-center">
@@ -706,27 +728,27 @@ const currentSafetyStageText = computed(() => {
<div class="mt-2 grid grid-cols-2 gap-1 lg:grid-cols-4">
<div
v-for="(item, index) in stageItems"
:key="item.key"
v-for="item in learnerProgressItems"
:key="item.stage"
class="cursor-pointer rounded-xl border px-2 py-1.5 transition hover:border-brand-300"
:class="[
stageStep >= index + 1 ? 'border-brand-200 bg-brand-50/70' : 'border-default bg-surface-soft/70',
canSwitchToStage(String(item.key)) ? 'cursor-pointer hover:border-brand-300' : 'cursor-not-allowed opacity-60',
item.status === 'completed' || item.status === 'active' ? 'border-brand-200 bg-brand-50/70' : 'border-default bg-surface-soft/70',
canSwitchToStage(String(item.stage)) ? 'cursor-pointer hover:border-brand-300' : 'cursor-not-allowed opacity-60',
]"
@click="handleStageCardClick(String(item.key))"
@click="handleStageCardClick(String(item.stage))"
>
<div class="flex items-center gap-2">
<div
class="flex h-6 w-6 shrink-0 items-center justify-center rounded-lg"
:class="stageStep >= index + 1 ? 'bg-brand-600 text-white' : 'bg-white text-muted'"
:class="item.status === 'completed' || item.status === 'active' ? 'bg-brand-600 text-white' : 'bg-white text-muted'"
>
<component :is="item.icon" class="h-3 w-3" />
</div>
<div class="min-w-0">
<div class="text-[10px] font-semibold uppercase tracking-[0.14em] text-muted">
{{ String(index + 1).padStart(2, '0') }}
{{ item.orderLabel }}
</div>
<div class="whitespace-normal text-[12px] font-medium leading-4 text-primary">{{ $t(item.label) }}</div>
<div class="whitespace-normal text-[12px] font-medium leading-4 text-primary">{{ item.label }}</div>
</div>
</div>
</div>
@@ -734,10 +756,10 @@ const currentSafetyStageText = computed(() => {
</div>
<div v-if="workspace.stage === 'video'" class="rounded-2xl border border-default bg-white p-4 shadow-sm">
<div>
<div v-if="learnerStepIntroTitle || learnerStepIntroDescription">
<div>
<h3 class="text-base font-semibold text-primary md:text-lg">{{ $t('chat.training.video.title') }}</h3>
<p class="mt-1 text-sm leading-6 text-secondary">{{ $t('chat.training.video.description') }}</p>
<h3 v-if="learnerStepIntroTitle" class="text-base font-semibold text-primary md:text-lg">{{ learnerStepIntroTitle }}</h3>
<p v-if="learnerStepIntroDescription" class="mt-1 whitespace-pre-line text-sm leading-6 text-secondary">{{ learnerStepIntroDescription }}</p>
</div>
</div>
<div class="mt-3">
@@ -983,30 +1005,30 @@ const currentSafetyStageText = computed(() => {
<div class="min-w-0 md:sticky md:top-3 md:self-start space-y-4">
<!-- 流程进度面板 (仅在非考试阶段展示) -->
<div v-if="workspace.stage !== 'exam' && workspace.stage !== 'result'" :class="questionNavPanelClass">
<div v-if="showLearnerProgressModule && workspace.stage !== 'exam' && workspace.stage !== 'result'" :class="questionNavPanelClass">
<div class="flex items-center gap-2 mb-3">
<Target class="h-4 w-4 text-brand-600" />
<div class="text-sm font-semibold text-primary">{{ $t('chat.training.progressLabel') }}</div>
</div>
<div class="space-y-2">
<div
v-for="(item, index) in stageItems"
:key="item.key"
v-for="item in learnerProgressItems"
:key="item.stage"
class="flex items-center gap-3 p-2 rounded-lg border transition"
:class="[
stageStep > index + 1 ? 'border-emerald-200 bg-emerald-50/50' :
stageStep === index + 1 ? 'border-brand-300 bg-brand-50' :
item.status === 'completed' ? 'border-emerald-200 bg-emerald-50/50' :
item.status === 'active' ? 'border-brand-300 bg-brand-50' :
'border-slate-100 bg-slate-50/50 opacity-70'
]"
>
<div class="flex h-6 w-6 shrink-0 items-center justify-center rounded-full"
:class="stageStep > index + 1 ? 'bg-emerald-500 text-white' : stageStep === index + 1 ? 'bg-brand-500 text-white shadow-sm' : 'bg-slate-200 text-slate-400'"
:class="item.status === 'completed' ? 'bg-emerald-500 text-white' : item.status === 'active' ? 'bg-brand-500 text-white shadow-sm' : 'bg-slate-200 text-slate-400'"
>
<CheckCircle2 v-if="stageStep > index + 1" class="h-4 w-4" />
<span v-else class="text-xs font-bold">{{ index + 1 }}</span>
<CheckCircle2 v-if="item.status === 'completed'" class="h-4 w-4" />
<span v-else class="text-xs font-bold">{{ Number(item.orderLabel) }}</span>
</div>
<div class="min-w-0 flex-1">
<div class="text-xs font-semibold" :class="stageStep === index + 1 ? 'text-brand-700' : 'text-slate-700'">{{ $t(item.label) }}</div>
<div class="text-xs font-semibold" :class="item.status === 'active' ? 'text-brand-700' : 'text-slate-700'">{{ item.label }}</div>
</div>
</div>
</div>
@@ -2,7 +2,8 @@
* features/chat/composables/useTrainingWorkspace.ts
* 用途:封装员工培训 Agent 的工作区状态,包括学员考试流、进度持久化与 HR 真实统计导出。
* 依赖:Vue reactivity、vue-i18n、training.api,以及 training.types。
* 逻辑:仅在培训 Agent 下启用;学员端按“视频 -> 知识点 -> 一题一答 -> 结算”推进,并把进度同步到后端,聊天区只保留问答能力。
* 逻辑:仅在培训 Agent 下启用;学员端按“视频 -> 知识点 -> 一题一答 -> 结算”推进,并把进度同步到后端;
* 非安全培训页额外按后台配置动态渲染进度项与步骤说明文案。
*/
import { computed, ref, watch, type ComputedRef } from 'vue';
import { useI18n } from 'vue-i18n';
@@ -39,6 +40,8 @@ import type {
import {
SAFETY_STAGE_GUIDE_USAGE_MAP,
matchesTrainingAgent,
resolveLearnerCopyText,
resolveLearnerProgressItems,
resolveTrainingContentConfig,
resolveSafetyTrainingVideoGuide,
type TrainingGuideLike,
@@ -269,6 +272,9 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
const value = (summaries as Record<string, unknown>)[safetyViewStage.value];
return Array.isArray(value) ? value.map((item) => String(item)) : [];
});
const learnerProgressItems = computed(() => resolveLearnerProgressItems(publishedConfig.value));
const learnerProgressHintText = computed(() => resolveLearnerCopyText(publishedConfig.value, 'progress_hint_text'));
const learnerStepIntroText = computed(() => resolveLearnerCopyText(publishedConfig.value, 'step_intro_text'));
const currentQuestion = computed(() => questionBank.value[currentQuestionIndex.value] || null);
const totalQuestionCount = computed(() => questionBank.value.length);
@@ -1159,6 +1165,9 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
hasPublishedTrainingConfig,
publishedTrainingMaterials,
publishedSafetySummary,
learnerProgressItems,
learnerProgressHintText,
learnerStepIntroText,
activeView,
learnerTab,
trainingTypes,
@@ -62,6 +62,11 @@ export interface TrainingPublishedConfigResource {
} | null;
}
export interface TrainingProgressItem {
stage: string;
label: string;
}
export interface TrainingPublishedConfig {
id: number;
trainingType: TrainingType;
@@ -1,9 +1,9 @@
/**
* features/chat/training/trainingWorkspace.utils.test.ts
* 用途:锁定培训工作区 Agent 识别契约(content_profile / extra_config,非名称)。
* 用途:锁定培训工作区 Agent 识别契约,以及培训进度项/文本配置的解析规则。
*/
import { describe, expect, it } from 'vitest';
import { matchesTrainingAgent } from './trainingWorkspace.utils';
import { matchesTrainingAgent, resolveLearnerCopyText, resolveLearnerProgressItems } from './trainingWorkspace.utils';
describe('matchesTrainingAgent', () => {
it('matches handler_config.content_profile=training', () => {
@@ -31,3 +31,61 @@ describe('matchesTrainingAgent', () => {
})).toBe(false);
});
});
describe('resolveLearnerProgressItems', () => {
it('returns configured progress items in order', () => {
expect(resolveLearnerProgressItems({
id: 1,
trainingType: 'custom',
version: 'v1',
passScore: 90,
settings: {
progress_items: [
{ stage: 'video', label: '观看视频' },
{ stage: 'exam', label: '参加考试' },
],
},
resources: [],
})).toEqual([
{ stage: 'video', label: '观看视频' },
{ stage: 'exam', label: '参加考试' },
]);
});
it('filters invalid or duplicate items', () => {
expect(resolveLearnerProgressItems({
id: 1,
trainingType: 'custom',
version: 'v1',
passScore: 90,
settings: {
progress_items: [
{ stage: 'video', label: '观看视频' },
{ stage: 'video', label: '重复步骤' },
{ stage: '', label: '空阶段' },
{ stage: 'summary', label: '' },
],
},
resources: [],
})).toEqual([
{ stage: 'video', label: '观看视频' },
]);
});
});
describe('resolveLearnerCopyText', () => {
it('returns trimmed text when configured', () => {
expect(resolveLearnerCopyText({
id: 1,
trainingType: 'custom',
version: 'v1',
passScore: 90,
settings: { progress_hint_text: ' 按要求完成学习 ' },
resources: [],
}, 'progress_hint_text')).toBe('按要求完成学习');
});
it('returns empty string for missing text', () => {
expect(resolveLearnerCopyText(null, 'step_intro_text')).toBe('');
});
});
@@ -2,9 +2,9 @@
* features/chat/training/trainingWorkspace.utils.ts
* 用途:沉淀新人培训工作区的纯函数规则,便于前端复用与单测。
* 依赖:无运行时副作用,仅依赖 Agent extra_config 普通对象与 training.types。
* 逻辑:统一判断某个 Agent 是否启用培训工作区,并解析培训视频、题库与复盘配置。
* 逻辑:统一判断某个 Agent 是否启用培训工作区,并解析培训视频、题库、复盘配置与动态进度文案。
*/
import type { TrainingContentConfig, TrainingPublishedConfig, TrainingQuestion, TrainingQuestionOption } from './training.types';
import type { TrainingContentConfig, TrainingProgressItem, TrainingPublishedConfig, TrainingQuestion, TrainingQuestionOption } from './training.types';
type TrainingAgentLike = {
name?: string | null;
@@ -153,6 +153,40 @@ const normalizeQuestionBank = (
const DEFAULT_PASS_SCORE = 90;
/**
* 解析学员侧培训进度项。
* AI 触发时机:当工作区需要按后台配置渲染流程卡片时调用;若后台未配置任何有效项,则返回空数组供页面整体隐藏该模块。
*/
export function resolveLearnerProgressItems(publishedConfig?: TrainingPublishedConfig | null): TrainingProgressItem[] {
const settings = asRecord(publishedConfig?.settings);
const rawItems = settings.progress_items;
if (!Array.isArray(rawItems)) return [];
const seenStages = new Set<string>();
return rawItems
.map((item) => {
const raw = asRecord(item);
const stage = String(raw.stage || raw.key || '').trim();
const label = String(raw.label || raw.title || '').trim();
if (!stage || !label || seenStages.has(stage)) return null;
seenStages.add(stage);
return { stage, label };
})
.filter((item): item is TrainingProgressItem => Boolean(item));
}
/**
* 读取学员侧长文本配置。
* AI 触发时机:培训进度说明和步骤引导只在后台返回非空文本时渲染,避免页面出现空占位。
*/
export function resolveLearnerCopyText(
publishedConfig: TrainingPublishedConfig | null | undefined,
key: 'progress_hint_text' | 'step_intro_text',
): string {
const settings = asRecord(publishedConfig?.settings);
return String(settings[key] || '').trim();
}
/**
* 解析培训视频地址。
* AI 触发时机:当培训工作区需要决定播放哪个培训视频时使用,若 Agent 未配置专属视频,则回退到默认地址。