Merge branch 'dev' of https://gitee.com/justop/hxops into dev
This commit is contained in:
commit
b89f9bafb6
22 files changed
+492
-244
No files matched your search
@@ -12,6 +12,7 @@ import {
|
||||
type AgentRegistryFields,
|
||||
} from '@/shared/utils/agent-registry.utils';
|
||||
import { matchesTrainingAgent } from '../training/trainingWorkspace.utils';
|
||||
import { resolveTrainingRoleChatQuestions } from '../training/trainingChatQuestions';
|
||||
import type { ChatDeps } from './chat.deps';
|
||||
import type { UseChatGuidesReturn } from './useChatGuides';
|
||||
|
||||
@@ -26,13 +27,6 @@ const ERP_KB = 'ERP知识库';
|
||||
const TRAINING_AGENT_DESCRIPTION =
|
||||
'我是你的新人培训助手,会在同一个聊天页面内带你完成视频学习、知识点复盘、逐题考试和结果复盘。';
|
||||
|
||||
const TRAINING_AGENT_QUESTIONS: AgentSuggestedQuestionItem[] = [
|
||||
{ question: '这次员工培训主要学习什么内容?', group: '培训问答' },
|
||||
{ question: '员工培训的通过规则是什么?', group: '培训问答' },
|
||||
{ question: '这次培训的试题范围有哪些?', group: '培训问答' },
|
||||
{ question: '培训记录会如何统计?', group: '培训问答' },
|
||||
];
|
||||
|
||||
const normalizeAgentSuggestedQuestions = (value: unknown): AgentSuggestedQuestionItem[] => {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return value
|
||||
@@ -53,11 +47,12 @@ const normalizeAgentSuggestedQuestions = (value: unknown): AgentSuggestedQuestio
|
||||
export interface UseChatAgentOptions {
|
||||
deps: ChatDeps;
|
||||
currentAgentId: ComputedRef<number>;
|
||||
currentUser: ComputedRef<Record<string, unknown> | null>;
|
||||
chatGuides: Pick<UseChatGuidesReturn, 'loadGuides' | 'clearGuides'>;
|
||||
}
|
||||
|
||||
export function useChatAgent(options: UseChatAgentOptions) {
|
||||
const { deps, currentAgentId, chatGuides } = options;
|
||||
const { deps, currentAgentId, currentUser, chatGuides } = options;
|
||||
const { t, msg, router } = deps;
|
||||
|
||||
const currentAgent = ref<AgentRegistryFields | null>(null);
|
||||
@@ -98,6 +93,10 @@ export function useChatAgent(options: UseChatAgentOptions) {
|
||||
: kbOptions.filter((option) => option !== ERP_KB)
|
||||
));
|
||||
const kbDropdownDisabled = computed(() => availableKbOptions.value.length <= 1);
|
||||
const isTrainingAdminViewer = computed(() => Boolean(
|
||||
(currentUser.value as { feature_permissions?: { training_hr_manage?: boolean } } | null)
|
||||
?.feature_permissions?.training_hr_manage,
|
||||
));
|
||||
|
||||
watch(canSelectErpKnowledgeBase, (allowed) => {
|
||||
if (allowed) {
|
||||
@@ -129,8 +128,18 @@ export function useChatAgent(options: UseChatAgentOptions) {
|
||||
const configured = normalizeAgentSuggestedQuestions(
|
||||
currentAgent.value?.suggested_questions,
|
||||
);
|
||||
if (matchesTrainingAgent(currentAgent.value)) {
|
||||
const roleQuestions = resolveTrainingRoleChatQuestions(
|
||||
currentAgent.value?.extra_config,
|
||||
isTrainingAdminViewer.value ? 'hr' : 'employee',
|
||||
);
|
||||
if (roleQuestions.configured) {
|
||||
return roleQuestions.questions.map((question) => ({ question, group: '' }));
|
||||
}
|
||||
if (configured.length > 0) return configured;
|
||||
return roleQuestions.questions.map((question) => ({ question, group: '' }));
|
||||
}
|
||||
if (configured.length > 0) return configured;
|
||||
if (matchesTrainingAgent(currentAgent.value)) return TRAINING_AGENT_QUESTIONS;
|
||||
return [];
|
||||
});
|
||||
|
||||
|
||||
@@ -149,7 +149,7 @@ export function useChatPage() {
|
||||
};
|
||||
|
||||
const chatGuides = useChatGuides({ currentAgentId });
|
||||
const agent = useChatAgent({ deps, currentAgentId, chatGuides });
|
||||
const agent = useChatAgent({ deps, currentAgentId, currentUser: computed(() => currentUser.value), chatGuides });
|
||||
const attachmentApi = useChatAttachments({
|
||||
deps,
|
||||
currentAgentId,
|
||||
|
||||
@@ -199,10 +199,20 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
|
||||
const currentQuestion = computed(() => questionBank.value[currentQuestionIndex.value] || null);
|
||||
const totalQuestionCount = computed(() => questionBank.value.length);
|
||||
const currentQuestionNumber = computed(() => {
|
||||
if (totalQuestionCount.value <= 0) return 0;
|
||||
return Math.min(currentQuestionIndex.value + 1, totalQuestionCount.value);
|
||||
});
|
||||
const answeredCount = computed(() => answerRecords.value.length);
|
||||
const correctCount = computed(() => answerRecords.value.filter((item) => item.correct).length);
|
||||
const wrongCount = computed(() => answerRecords.value.filter((item) => !item.correct).length);
|
||||
const isPassed = computed(() => score.value >= passScore.value);
|
||||
const examReadyToSubmit = computed(() => (
|
||||
stage.value === 'exam'
|
||||
&& totalQuestionCount.value > 0
|
||||
&& answeredCount.value >= totalQuestionCount.value
|
||||
));
|
||||
const canEnterResultStage = computed(() => stage.value === 'result');
|
||||
|
||||
const learnerProgressPercent = computed(() => {
|
||||
if (stage.value === 'video') return 25;
|
||||
@@ -295,6 +305,29 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
].join('\n');
|
||||
};
|
||||
|
||||
/**
|
||||
* 基于当前答题记录重算成绩,保证“修改答案”后分数与结果页口径一致。
|
||||
* AI 触发时机:当培训考试支持点击题号返回修改答案时,不能再用“上一题答对就直接 +1”的增量算法。
|
||||
*/
|
||||
const recalculateScoreFromAnswers = (records: TrainingAnswerRecord[]) => calculateTrainingScore(
|
||||
records.filter((item) => item.correct).length,
|
||||
totalQuestionCount.value,
|
||||
);
|
||||
|
||||
/**
|
||||
* 覆盖或新增当前题的作答记录,避免同一道题在进度里出现重复答案。
|
||||
* AI 触发时机:当员工返回已答题修改答案时,必须覆盖旧答案而不是重复追加,否则分数和已答题数都会失真。
|
||||
*/
|
||||
const upsertAnswerRecord = (nextRecord: TrainingAnswerRecord) => {
|
||||
const existingIndex = answerRecords.value.findIndex((item) => item.questionId === nextRecord.questionId);
|
||||
if (existingIndex >= 0) {
|
||||
answerRecords.value.splice(existingIndex, 1, nextRecord);
|
||||
return true;
|
||||
}
|
||||
answerRecords.value.push(nextRecord);
|
||||
return false;
|
||||
};
|
||||
|
||||
const syncAnswerStateForCurrentQuestion = () => {
|
||||
const questionId = currentQuestion.value?.id;
|
||||
const currentAnswer = questionId
|
||||
@@ -305,6 +338,21 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
currentFeedbackCorrect.value = Boolean(currentAnswer?.correct);
|
||||
};
|
||||
|
||||
/**
|
||||
* 切换考试题目时,同步左侧题干、已答状态和反馈展示。
|
||||
* AI 触发时机:当员工点击右侧题号导航或后端恢复考试进度时,必须保证“当前题序号、选中项、答题对错反馈”完全一致。
|
||||
*/
|
||||
const syncExamQuestionIndex = (nextIndex: number) => {
|
||||
if (totalQuestionCount.value <= 0) {
|
||||
currentQuestionIndex.value = 0;
|
||||
syncAnswerStateForCurrentQuestion();
|
||||
return;
|
||||
}
|
||||
const normalizedIndex = Math.min(Math.max(nextIndex, 0), totalQuestionCount.value - 1);
|
||||
currentQuestionIndex.value = normalizedIndex;
|
||||
syncAnswerStateForCurrentQuestion();
|
||||
};
|
||||
|
||||
const applyProgressRecord = (record: TrainingProgressRecord | null) => {
|
||||
if (!record) {
|
||||
stage.value = 'video';
|
||||
@@ -314,7 +362,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
return;
|
||||
}
|
||||
stage.value = record.stage;
|
||||
currentQuestionIndex.value = record.currentQuestionIndex;
|
||||
syncExamQuestionIndex(record.currentQuestionIndex);
|
||||
score.value = Number(record.score || 0);
|
||||
attemptCount.value = Number(record.attempts || 0);
|
||||
answerRecords.value = Array.isArray(record.answerDetails) ? record.answerDetails : [];
|
||||
@@ -506,42 +554,42 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
void persistProgress();
|
||||
};
|
||||
|
||||
/**
|
||||
* 记录当前题答案;首次作答沿用自动推进,回看题目时则覆盖旧答案并实时重算分数。
|
||||
* AI 触发时机:当员工在逐题考试阶段点击右侧题号返回某题时,需要允许直接改答且不破坏“提交答卷后才结算”的流程。
|
||||
*/
|
||||
const selectAnswer = (optionId: string) => {
|
||||
if (answerLocked.value || !currentQuestion.value) return;
|
||||
if (!currentQuestion.value) return;
|
||||
const answeredQuestion = currentQuestion.value;
|
||||
const hadExistingAnswer = answerRecords.value.some((item) => item.questionId === answeredQuestion.id);
|
||||
selectedOptionId.value = optionId;
|
||||
answerLocked.value = true;
|
||||
const isCorrect = optionId === answeredQuestion.answer;
|
||||
currentFeedbackCorrect.value = isCorrect;
|
||||
const nextCorrectAnswerCount = correctCount.value + (isCorrect ? 1 : 0);
|
||||
const nextScore = calculateTrainingScore(nextCorrectAnswerCount, totalQuestionCount.value);
|
||||
score.value = nextScore;
|
||||
const answerRecord: TrainingAnswerRecord = {
|
||||
questionId: answeredQuestion.id,
|
||||
selectedOptionId: optionId,
|
||||
correct: isCorrect,
|
||||
};
|
||||
answerRecords.value.push(answerRecord);
|
||||
upsertAnswerRecord(answerRecord);
|
||||
const nextScore = recalculateScoreFromAnswers(answerRecords.value);
|
||||
score.value = nextScore;
|
||||
latestExamFeedback.value = buildExamFeedback(answeredQuestion, answerRecord, nextScore);
|
||||
|
||||
// [STEP 1] 最后一题直接进入结算,对齐文档里的“答完即公布最终成绩”。
|
||||
if (currentQuestionIndex.value >= totalQuestionCount.value - 1) {
|
||||
stage.value = 'result';
|
||||
completionDate.value = formatDate(new Date());
|
||||
latestSettledResult.value = {
|
||||
score: nextScore,
|
||||
correctCount: answerRecords.value.filter((item) => item.correct).length,
|
||||
wrongCount: answerRecords.value.filter((item) => !item.correct).length,
|
||||
isPassed: nextScore >= passScore.value,
|
||||
};
|
||||
// [STEP 1] 已答题被回看并改答时,停留在当前题,方便继续检查或跳到其他题。
|
||||
if (hadExistingAnswer) {
|
||||
void persistProgress();
|
||||
return;
|
||||
}
|
||||
|
||||
// [STEP 2] 非最后一题则在聊天流里自动反馈上一题,并紧接着展示下一题。
|
||||
currentQuestionIndex.value += 1;
|
||||
selectedOptionId.value = '';
|
||||
answerLocked.value = false;
|
||||
currentFeedbackCorrect.value = false;
|
||||
// [STEP 2] 最后一题首次答完后先停留在考试阶段,必须点击“提交答卷”才能进入结算结果。
|
||||
if (currentQuestionIndex.value >= totalQuestionCount.value - 1) {
|
||||
void persistProgress();
|
||||
return;
|
||||
}
|
||||
|
||||
// [STEP 3] 首次作答且不是最后一题时,自动推进到下一题。
|
||||
syncExamQuestionIndex(currentQuestionIndex.value + 1);
|
||||
void persistProgress();
|
||||
};
|
||||
|
||||
@@ -552,19 +600,30 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
const moveToNextQuestion = () => {
|
||||
if (!answerLocked.value) return;
|
||||
if (currentQuestionIndex.value >= totalQuestionCount.value - 1) {
|
||||
stage.value = 'result';
|
||||
completionDate.value = formatDate(new Date());
|
||||
void persistProgress();
|
||||
return;
|
||||
}
|
||||
currentQuestionIndex.value += 1;
|
||||
selectedOptionId.value = '';
|
||||
answerLocked.value = false;
|
||||
currentFeedbackCorrect.value = false;
|
||||
syncExamQuestionIndex(currentQuestionIndex.value + 1);
|
||||
latestExamFeedback.value = null;
|
||||
void persistProgress();
|
||||
};
|
||||
|
||||
/**
|
||||
* 统一提交整份答卷并进入结算结果。
|
||||
* AI 触发时机:当员工已完成全部题目但尚未结算时,必须显式点击“提交答卷”后才允许进入第 4 阶段。
|
||||
*/
|
||||
const submitExam = () => {
|
||||
if (!examReadyToSubmit.value) return;
|
||||
stage.value = 'result';
|
||||
completionDate.value = formatDate(new Date());
|
||||
latestSettledResult.value = {
|
||||
score: score.value,
|
||||
correctCount: answerRecords.value.filter((item) => item.correct).length,
|
||||
wrongCount: answerRecords.value.filter((item) => !item.correct).length,
|
||||
isPassed: score.value >= passScore.value,
|
||||
};
|
||||
void persistProgress();
|
||||
};
|
||||
|
||||
const restartExam = () => {
|
||||
stage.value = 'video';
|
||||
resetExamState();
|
||||
@@ -645,6 +704,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
activeView,
|
||||
stage,
|
||||
currentQuestionIndex,
|
||||
currentQuestionNumber,
|
||||
currentQuestion,
|
||||
totalQuestionCount,
|
||||
passScore,
|
||||
@@ -654,6 +714,8 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
selectedOptionId,
|
||||
answerLocked,
|
||||
currentFeedbackCorrect,
|
||||
examReadyToSubmit,
|
||||
canEnterResultStage,
|
||||
latestExamFeedback,
|
||||
latestSettledResult,
|
||||
answerRecords,
|
||||
@@ -681,7 +743,9 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
|
||||
markVideoCompleted,
|
||||
startExam,
|
||||
selectAnswer,
|
||||
syncExamQuestionIndex,
|
||||
moveToNextQuestion,
|
||||
submitExam,
|
||||
restartExam,
|
||||
exportRecords,
|
||||
showVideoModal,
|
||||
|
||||
Reference in new issue
Block a user