83 lines
3.3 KiB
TypeScript
83 lines
3.3 KiB
TypeScript
/**
|
|
* features/chat/training/trainingChatQuestions.ts
|
|
* 用途:统一维护员工培训 Agent 的员工版 / HR 版聊天常见问题默认值与解析逻辑。
|
|
* 依赖:被 Admin 后台培训配置表单与 Chat 欢迎区共同引用,避免默认问题与字段名出现多份散落副本。
|
|
* 逻辑:优先读取 extra_config.training_config 中的角色化问题列表;未配置时回退到内置示例,显式保存空数组时尊重为空。
|
|
*/
|
|
|
|
export type TrainingChatQuestionRole = 'employee' | 'hr';
|
|
|
|
const TRAINING_ROLE_QUESTION_KEYS: Record<TrainingChatQuestionRole, string[]> = {
|
|
employee: ['employee_chat_suggested_questions', 'learner_chat_suggested_questions'],
|
|
hr: ['hr_chat_suggested_questions', 'admin_chat_suggested_questions'],
|
|
};
|
|
|
|
export const DEFAULT_TRAINING_EMPLOYEE_CHAT_QUESTIONS = [
|
|
'这次员工培训主要学习什么内容?',
|
|
'完成培训需要经过哪些步骤?',
|
|
'员工培训的通过规则是什么?',
|
|
'开始考试前需要注意什么?',
|
|
];
|
|
|
|
export const DEFAULT_TRAINING_HR_CHAT_QUESTIONS = [
|
|
'现在有多少人参加了培训?',
|
|
'当前培训通过率是多少?',
|
|
'哪些员工还没有完成培训?',
|
|
'我想查看本月培训成绩统计',
|
|
];
|
|
|
|
/** 返回指定培训角色的默认问题,供管理表单初始化使用。 */
|
|
export const getDefaultTrainingChatQuestions = (role: TrainingChatQuestionRole): string[] => (
|
|
role === 'hr'
|
|
? cloneQuestions(DEFAULT_TRAINING_HR_CHAT_QUESTIONS)
|
|
: cloneQuestions(DEFAULT_TRAINING_EMPLOYEE_CHAT_QUESTIONS)
|
|
);
|
|
|
|
const asRecord = (value: unknown): Record<string, unknown> => (
|
|
value && typeof value === 'object' && !Array.isArray(value)
|
|
? value as Record<string, unknown>
|
|
: {}
|
|
);
|
|
|
|
const cloneQuestions = (questions: string[]) => questions.map((item) => String(item || '').trim()).filter(Boolean);
|
|
|
|
/**
|
|
* 归一化后台保存的常见问题数组。
|
|
* AI 触发时机:培训配置允许 string[] 与 { question }[] 混存时,需要统一还原成纯文本列表供表单和聊天欢迎区复用。
|
|
*/
|
|
const normalizeTrainingChatQuestionList = (value: unknown): string[] => {
|
|
if (!Array.isArray(value)) return [];
|
|
return value
|
|
.map((item) => {
|
|
if (typeof item === 'string') return item.trim();
|
|
if (!item || typeof item !== 'object') return '';
|
|
return String((item as Record<string, unknown>).question || (item as Record<string, unknown>).text || '').trim();
|
|
})
|
|
.filter(Boolean);
|
|
};
|
|
|
|
const getDefaultQuestions = getDefaultTrainingChatQuestions;
|
|
|
|
/**
|
|
* 读取培训配置中的角色化聊天常见问题。
|
|
* AI 触发时机:员工培训 Agent 需要按当前用户角色切换欢迎区问题时,必须先判断后台是否显式配置该角色问题,再决定是否回退默认示例。
|
|
*/
|
|
export const resolveTrainingRoleChatQuestions = (
|
|
extraConfig: unknown,
|
|
role: TrainingChatQuestionRole,
|
|
): { configured: boolean; questions: string[] } => {
|
|
const trainingConfig = asRecord(asRecord(extraConfig).training_config);
|
|
const keys = TRAINING_ROLE_QUESTION_KEYS[role];
|
|
const matchedKey = keys.find((key) => Object.prototype.hasOwnProperty.call(trainingConfig, key));
|
|
if (!matchedKey) {
|
|
return {
|
|
configured: false,
|
|
questions: getDefaultQuestions(role),
|
|
};
|
|
}
|
|
return {
|
|
configured: true,
|
|
questions: normalizeTrainingChatQuestionList(trainingConfig[matchedKey]),
|
|
};
|
|
};
|