fix(training-pdf-import): 修复培训试题PDF导入判断题解析
This commit is contained in:
1 parent
3a22df736e
commit
91758ef2b4
19 files changed
+1767
-12
No files matched your search
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* features/admin/modules/agents/AgentsPage.vue
|
||||
*
|
||||
* 用途:管理控制台的应用管理页,提供应用列表、创建/编辑弹窗、测试与授权入口。
|
||||
@@ -29,9 +29,11 @@ const { t } = useI18n();
|
||||
const {
|
||||
agents,
|
||||
scheduleTasks,
|
||||
trainingExamPdfGuides,
|
||||
activeUsers,
|
||||
isLoading,
|
||||
isSaving,
|
||||
isImportingTrainingPdf,
|
||||
isEditing,
|
||||
selectedAgentId,
|
||||
form,
|
||||
@@ -50,6 +52,11 @@ const {
|
||||
submitAgent,
|
||||
addSuggestedQuestion,
|
||||
removeSuggestedQuestion,
|
||||
addTrainingQuestion,
|
||||
removeTrainingQuestion,
|
||||
addTrainingOption,
|
||||
removeTrainingOption,
|
||||
importTrainingQuestionBankFromPdf,
|
||||
deleteAgent,
|
||||
openTestModal,
|
||||
runAgentTest,
|
||||
@@ -229,12 +236,19 @@ onMounted(() => {
|
||||
:visible="showFormDialog"
|
||||
:is-editing="isEditing"
|
||||
:is-saving="isSaving"
|
||||
:is-importing-training-pdf="isImportingTrainingPdf"
|
||||
:form="form"
|
||||
:schedule-tasks="scheduleTasks"
|
||||
:training-exam-pdf-guides="trainingExamPdfGuides"
|
||||
@close="showFormDialog = false"
|
||||
@save="saveAgentForm"
|
||||
@add-suggested-question="addSuggestedQuestion"
|
||||
@remove-suggested-question="removeSuggestedQuestion"
|
||||
@add-training-question="addTrainingQuestion"
|
||||
@remove-training-question="removeTrainingQuestion"
|
||||
@add-training-option="addTrainingOption"
|
||||
@remove-training-option="removeTrainingOption"
|
||||
@import-training-pdf="importTrainingQuestionBankFromPdf"
|
||||
/>
|
||||
|
||||
<AgentPermissionModal
|
||||
|
||||
@@ -7,9 +7,9 @@
|
||||
*/
|
||||
|
||||
<script setup lang="ts">
|
||||
import { Plus, Trash2, X } from 'lucide-vue-next';
|
||||
import { FileDown, Plus, Trash2, X } from 'lucide-vue-next';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import type { AgentFormState } from '@/features/admin/types/agent.types';
|
||||
import type { AgentFormState, AgentGuideSummary } from '@/features/admin/types/agent.types';
|
||||
import type { ScheduleTask } from '@/shared/types/schedule.types';
|
||||
import AgentToolRunProgressConfig from './AgentToolRunProgressConfig.vue';
|
||||
|
||||
@@ -17,8 +17,10 @@ defineProps<{
|
||||
visible: boolean;
|
||||
isEditing: boolean;
|
||||
isSaving: boolean;
|
||||
isImportingTrainingPdf: boolean;
|
||||
form: AgentFormState;
|
||||
scheduleTasks: ScheduleTask[];
|
||||
trainingExamPdfGuides: AgentGuideSummary[];
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
@@ -26,9 +28,40 @@ const emit = defineEmits<{
|
||||
save: [];
|
||||
addSuggestedQuestion: [];
|
||||
removeSuggestedQuestion: [index: number];
|
||||
addTrainingQuestion: [];
|
||||
removeTrainingQuestion: [index: number];
|
||||
addTrainingOption: [questionIndex: number];
|
||||
removeTrainingOption: [questionIndex: number, optionIndex: number];
|
||||
importTrainingPdf: [guideId?: number | null];
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
const optionLabels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
|
||||
|
||||
/**
|
||||
* 切换培训题型时同步修正表单结构。
|
||||
* 核心逻辑:判断题固定使用 true/false;单选题至少保留两个选项,并把答案回落到首个可用选项。
|
||||
*/
|
||||
const handleTrainingQuestionTypeChange = (form: AgentFormState, index: number) => {
|
||||
const question = form.training_question_bank[index];
|
||||
if (!question) return;
|
||||
if (question.type === 'boolean') {
|
||||
question.options = [];
|
||||
question.answer = question.answer === 'false' ? 'false' : 'true';
|
||||
return;
|
||||
}
|
||||
if (!Array.isArray(question.options)) {
|
||||
question.options = ['', ''];
|
||||
}
|
||||
while (question.options.length < 2) {
|
||||
question.options.push('');
|
||||
}
|
||||
const validAnswerIds = question.options
|
||||
.map((item) => String(item || '').trim())
|
||||
.filter(Boolean)
|
||||
.map((_, optionIndex) => String.fromCharCode(97 + optionIndex));
|
||||
question.answer = validAnswerIds.includes(question.answer) ? question.answer : (validAnswerIds[0] || 'a');
|
||||
};
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -202,6 +235,218 @@ const { t } = useI18n();
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-span-1 md:col-span-2 rounded-2xl border border-default bg-surface-soft/40 p-4">
|
||||
<div class="flex flex-col gap-3 md:flex-row md:items-start md:justify-between">
|
||||
<div>
|
||||
<div class="text-xs font-bold uppercase tracking-wider text-muted">{{ t('admin.agent.training.title') }}</div>
|
||||
<p class="mt-1 text-sm leading-6 text-secondary">{{ t('admin.agent.training.subtitle') }}</p>
|
||||
</div>
|
||||
<label class="inline-flex items-center gap-2 rounded-xl border border-default bg-white px-3 py-2 text-sm text-secondary">
|
||||
<input v-model="form.training_workspace_enabled" type="checkbox" class="accent-brand-600" />
|
||||
<span>{{ t('admin.agent.training.enable') }}</span>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<p class="mt-3 text-xs leading-5 text-muted">
|
||||
{{ form.training_workspace_enabled ? t('admin.agent.training.enableHint') : t('admin.agent.training.disabledHint') }}
|
||||
</p>
|
||||
|
||||
<div class="mt-4 grid grid-cols-1 gap-4 md:grid-cols-3">
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.passScore') }}</label>
|
||||
<input
|
||||
v-model.number="form.training_pass_score"
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
step="1"
|
||||
class="w-full input-field text-sm"
|
||||
/>
|
||||
<p class="text-xs leading-5 text-muted">{{ t('admin.agent.training.passScoreHint') }}</p>
|
||||
</div>
|
||||
<div class="space-y-2 md:col-span-2">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.summaryPoints') }}</label>
|
||||
<textarea
|
||||
v-model="form.training_summary_points_text"
|
||||
rows="5"
|
||||
class="w-full input-field text-sm"
|
||||
:placeholder="t('admin.agent.training.summaryPointsPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-2 md:col-span-3">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.summaryChecklist') }}</label>
|
||||
<textarea
|
||||
v-model="form.training_summary_checklist_text"
|
||||
rows="4"
|
||||
class="w-full input-field text-sm"
|
||||
:placeholder="t('admin.agent.training.summaryChecklistPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-5 space-y-3">
|
||||
<div class="flex flex-wrap items-center justify-between gap-3">
|
||||
<div>
|
||||
<div class="text-sm font-semibold text-primary">{{ t('admin.agent.training.questionBank') }}</div>
|
||||
<p class="mt-1 text-xs leading-5 text-muted">{{ t('admin.agent.training.questionHint') }}</p>
|
||||
</div>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-1 rounded-lg border border-default bg-white px-3 py-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft disabled:cursor-not-allowed disabled:opacity-60"
|
||||
:disabled="!isEditing || trainingExamPdfGuides.length === 0 || isImportingTrainingPdf"
|
||||
@click="emit('importTrainingPdf', trainingExamPdfGuides[0]?.id || null)"
|
||||
>
|
||||
<FileDown class="h-4 w-4" />
|
||||
{{ isImportingTrainingPdf ? t('admin.agent.training.importingPdf') : t('admin.agent.training.importFromPdf') }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-1 rounded-lg border border-default bg-white px-3 py-1.5 text-xs font-medium text-secondary transition-colors hover:bg-surface-soft"
|
||||
@click="emit('addTrainingQuestion')"
|
||||
>
|
||||
<Plus class="h-4 w-4" />
|
||||
{{ t('admin.agent.training.addQuestion') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p class="text-xs leading-5 text-muted">
|
||||
{{
|
||||
!isEditing
|
||||
? t('admin.agent.training.importPdfCreateFirst')
|
||||
: trainingExamPdfGuides.length > 0
|
||||
? t('admin.agent.training.importPdfHint', { fileName: trainingExamPdfGuides[0]?.file_name || '--' })
|
||||
: t('admin.agent.training.importPdfMissing')
|
||||
}}
|
||||
</p>
|
||||
|
||||
<div v-if="form.training_question_bank.length === 0" class="rounded-xl border border-dashed border-default bg-white px-4 py-5 text-sm text-muted">
|
||||
{{ t('admin.agent.training.emptyQuestionBank') }}
|
||||
</div>
|
||||
|
||||
<div v-else class="space-y-4">
|
||||
<div
|
||||
v-for="(question, questionIndex) in form.training_question_bank"
|
||||
:key="questionIndex"
|
||||
class="rounded-2xl border border-default bg-white p-4"
|
||||
>
|
||||
<div class="mb-4 flex flex-wrap items-center justify-between gap-3">
|
||||
<div class="text-sm font-semibold text-primary">
|
||||
{{ t('admin.agent.training.questionCardTitle', { index: questionIndex + 1 }) }}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-1 rounded-lg border border-red-200 bg-red-50 px-3 py-1.5 text-xs font-medium text-red-600 transition-colors hover:bg-red-100"
|
||||
@click="emit('removeTrainingQuestion', questionIndex)"
|
||||
>
|
||||
<Trash2 class="h-4 w-4" />
|
||||
{{ t('admin.agent.training.removeQuestion') }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-1 gap-4 md:grid-cols-3">
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.questionId') }}</label>
|
||||
<input
|
||||
v-model="question.id"
|
||||
type="text"
|
||||
class="w-full input-field text-sm"
|
||||
:placeholder="t('admin.agent.training.questionIdPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.questionType') }}</label>
|
||||
<select
|
||||
v-model="question.type"
|
||||
class="w-full input-field text-sm"
|
||||
@change="handleTrainingQuestionTypeChange(form, questionIndex)"
|
||||
>
|
||||
<option value="single">{{ t('admin.agent.training.single') }}</option>
|
||||
<option value="boolean">{{ t('admin.agent.training.boolean') }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.correctAnswer') }}</label>
|
||||
<select v-model="question.answer" class="w-full input-field text-sm">
|
||||
<template v-if="question.type === 'boolean'">
|
||||
<option value="true">{{ t('admin.agent.training.booleanTrue') }}</option>
|
||||
<option value="false">{{ t('admin.agent.training.booleanFalse') }}</option>
|
||||
</template>
|
||||
<template v-else>
|
||||
<option
|
||||
v-for="(_, optionIndex) in question.options.filter((item) => String(item || '').trim())"
|
||||
:key="`${questionIndex}-${optionIndex}`"
|
||||
:value="String.fromCharCode(97 + optionIndex)"
|
||||
>
|
||||
{{ t('admin.agent.training.optionLabel', { label: optionLabels[optionIndex] || optionIndex + 1 }) }}
|
||||
</option>
|
||||
</template>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="space-y-2 md:col-span-3">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.questionPrompt') }}</label>
|
||||
<textarea
|
||||
v-model="question.prompt"
|
||||
rows="3"
|
||||
class="w-full input-field text-sm"
|
||||
:placeholder="t('admin.agent.training.questionPromptPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div class="space-y-2 md:col-span-3">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.questionExplanation') }}</label>
|
||||
<textarea
|
||||
v-model="question.explanation"
|
||||
rows="3"
|
||||
class="w-full input-field text-sm"
|
||||
:placeholder="t('admin.agent.training.questionExplanationPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div v-if="question.type === 'single'" class="space-y-3 md:col-span-3">
|
||||
<div class="flex flex-wrap items-center justify-between gap-3">
|
||||
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.training.questionOptions') }}</label>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-1 rounded-lg border border-default bg-surface-soft px-3 py-1.5 text-xs font-medium text-secondary transition-colors hover:bg-slate-100"
|
||||
@click="emit('addTrainingOption', questionIndex)"
|
||||
>
|
||||
<Plus class="h-4 w-4" />
|
||||
{{ t('admin.agent.training.addOption') }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-3 md:grid-cols-2">
|
||||
<div
|
||||
v-for="(_, optionIndex) in question.options"
|
||||
:key="`${questionIndex}-option-${optionIndex}`"
|
||||
class="flex items-center gap-2 rounded-xl border border-default bg-surface-soft/40 p-3"
|
||||
>
|
||||
<span class="min-w-6 text-xs font-semibold text-muted">{{ optionLabels[optionIndex] || optionIndex + 1 }}.</span>
|
||||
<input
|
||||
v-model="question.options[optionIndex]"
|
||||
type="text"
|
||||
class="w-full input-field text-sm"
|
||||
:placeholder="t('admin.agent.training.optionPlaceholder')"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex h-10 w-10 items-center justify-center rounded-lg border border-red-200 bg-red-50 text-red-600 transition-colors hover:bg-red-100"
|
||||
@click="emit('removeTrainingOption', questionIndex, optionIndex)"
|
||||
>
|
||||
<Trash2 class="h-4 w-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<AgentToolRunProgressConfig
|
||||
v-if="form.type === 'api' && form.dify_app_type !== 'workflow'"
|
||||
:form="form"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/**
|
||||
/**
|
||||
* features/admin/modules/agents/composables/useAgentsAdmin.ts
|
||||
*
|
||||
* Agent 管理模块的业务逻辑:列表 CRUD、连通性测试、权限分配。
|
||||
@@ -10,6 +10,8 @@ import { useI18n } from 'vue-i18n';
|
||||
import {
|
||||
createAdminAgent,
|
||||
deleteAdminAgent,
|
||||
importAdminAgentTrainingQuestionBankFromPdf,
|
||||
listAdminAgentGuides,
|
||||
listAdminAgents,
|
||||
listAdminUsers,
|
||||
saveAdminAgentPermissions,
|
||||
@@ -19,10 +21,19 @@ import {
|
||||
import { listAdminSchedules } from '@/features/admin/api/schedule.api';
|
||||
import { useMessage } from '@/shared/composables/useMessage';
|
||||
import type { AdminUserRow } from '@/features/admin/types/admin.types';
|
||||
import type { AgentFormState, AgentRow, AgentSuggestedQuestionItem, AgentTestResult } from '@/features/admin/types/agent.types';
|
||||
import { createEmptyAgentForm } from '@/features/admin/types/agent.types';
|
||||
import type {
|
||||
AgentFormState,
|
||||
AgentRow,
|
||||
AgentSuggestedQuestionItem,
|
||||
AgentTestResult,
|
||||
AgentTrainingQuestionFormItem,
|
||||
AgentGuideSummary,
|
||||
} from '@/features/admin/types/agent.types';
|
||||
import { createEmptyAgentForm, createEmptyTrainingQuestionForm } from '@/features/admin/types/agent.types';
|
||||
import type { ScheduleTask } from '@/shared/types/schedule.types';
|
||||
|
||||
const TRAINING_OPTION_IDS = 'abcdefghijklmnopqrstuvwxyz'.split('');
|
||||
|
||||
const normalizeSuggestedQuestions = (value: unknown): AgentSuggestedQuestionItem[] => {
|
||||
if (!Array.isArray(value)) return [];
|
||||
return value
|
||||
@@ -39,6 +50,169 @@ const normalizeSuggestedQuestions = (value: unknown): AgentSuggestedQuestionItem
|
||||
.filter((item): item is AgentSuggestedQuestionItem => !!item);
|
||||
};
|
||||
|
||||
const asRecord = (value: unknown): Record<string, unknown> => (
|
||||
value && typeof value === 'object' && !Array.isArray(value)
|
||||
? value as Record<string, unknown>
|
||||
: {}
|
||||
);
|
||||
|
||||
const toMultilineText = (value: unknown) => (
|
||||
Array.isArray(value)
|
||||
? value.map((item) => String(item || '').trim()).filter(Boolean).join('\n')
|
||||
: ''
|
||||
);
|
||||
|
||||
const splitMultilineText = (value: string): string[] => value
|
||||
.split(/\r?\n/)
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean);
|
||||
|
||||
type TrainingImportPayload = Awaited<ReturnType<typeof importAdminAgentTrainingQuestionBankFromPdf>>;
|
||||
|
||||
/**
|
||||
* 从 Agent extra_config 解析员工培训配置表单字段。
|
||||
* 核心逻辑:优先读取 extra_config.training_config,兼容历史顶层字段;单选题选项统一收敛成纯文本数组,便于后台表单维护。
|
||||
*/
|
||||
const parseTrainingConfigFromExtra = (extra: Record<string, unknown> | undefined) => {
|
||||
const trainingConfig = asRecord(extra?.training_config);
|
||||
const merged = {
|
||||
...asRecord(extra),
|
||||
...trainingConfig,
|
||||
};
|
||||
const rawQuestionBank: unknown[] = Array.isArray(merged.question_bank ?? merged.questionBank)
|
||||
? (merged.question_bank ?? merged.questionBank) as unknown[]
|
||||
: [];
|
||||
const questionBank: AgentTrainingQuestionFormItem[] = rawQuestionBank
|
||||
.map((item: unknown, index: number): AgentTrainingQuestionFormItem => {
|
||||
const raw = asRecord(item);
|
||||
const type: AgentTrainingQuestionFormItem['type'] = raw.type === 'boolean' ? 'boolean' : 'single';
|
||||
const rawOptions: unknown[] = Array.isArray(raw.options) ? raw.options : [];
|
||||
const optionLabels = type === 'single'
|
||||
? rawOptions
|
||||
.map((option: unknown) => {
|
||||
if (typeof option === 'string') return option.trim();
|
||||
return String(asRecord(option).label || '').trim();
|
||||
})
|
||||
.filter(Boolean)
|
||||
: [];
|
||||
while (type === 'single' && optionLabels.length < 2) {
|
||||
optionLabels.push('');
|
||||
}
|
||||
return {
|
||||
id: String(raw.id || '').trim() || `q${index + 1}`,
|
||||
type,
|
||||
prompt: String(raw.prompt || '').trim(),
|
||||
explanation: String(raw.explanation || '').trim(),
|
||||
options: optionLabels,
|
||||
answer: type === 'boolean'
|
||||
? (String(raw.answer || '').trim() === 'false' ? 'false' : 'true')
|
||||
: (() => {
|
||||
const answer = String(raw.answer || '').trim();
|
||||
const optionIds = optionLabels.map((_, optionIndex) => TRAINING_OPTION_IDS[optionIndex]).filter(Boolean);
|
||||
return optionIds.includes(answer) ? answer : (optionIds[0] || 'a');
|
||||
})(),
|
||||
};
|
||||
})
|
||||
.filter((item) => Boolean(item.prompt || item.id || item.options.length));
|
||||
const hasTrainingContent = questionBank.length > 0
|
||||
|| Boolean(toMultilineText(merged.summary_points ?? merged.summaryPoints))
|
||||
|| Boolean(toMultilineText(merged.summary_checklist ?? merged.summaryChecklist));
|
||||
|
||||
return {
|
||||
training_workspace_enabled:
|
||||
extra?.training_workspace_enabled === true
|
||||
|| extra?.training_workspace_enabled === 'true'
|
||||
|| extra?.training_mode === true
|
||||
|| extra?.training_mode === 'true'
|
||||
|| extra?.training_agent === true
|
||||
|| extra?.training_agent === 'true'
|
||||
|| hasTrainingContent,
|
||||
training_pass_score: Number.isFinite(Number(merged.pass_score ?? merged.passScore))
|
||||
? Math.max(0, Math.min(100, Math.round(Number(merged.pass_score ?? merged.passScore))))
|
||||
: 90,
|
||||
training_summary_points_text: toMultilineText(merged.summary_points ?? merged.summaryPoints),
|
||||
training_summary_checklist_text: toMultilineText(merged.summary_checklist ?? merged.summaryChecklist),
|
||||
training_question_bank: questionBank,
|
||||
};
|
||||
};
|
||||
|
||||
const buildTrainingQuestionPayload = (question: AgentTrainingQuestionFormItem, index: number) => {
|
||||
const id = String(question.id || '').trim() || `q${index + 1}`;
|
||||
const prompt = String(question.prompt || '').trim();
|
||||
const explanation = String(question.explanation || '').trim();
|
||||
if (question.type === 'boolean') {
|
||||
return {
|
||||
id,
|
||||
type: 'boolean' as const,
|
||||
prompt,
|
||||
answer: question.answer === 'false' ? 'false' : 'true',
|
||||
explanation,
|
||||
};
|
||||
}
|
||||
|
||||
const normalizedOptions = question.options
|
||||
.map((label) => String(label || '').trim())
|
||||
.filter(Boolean)
|
||||
.map((label, optionIndex) => ({
|
||||
id: TRAINING_OPTION_IDS[optionIndex] || `option_${optionIndex + 1}`,
|
||||
label,
|
||||
}));
|
||||
const optionIds = normalizedOptions.map((item) => item.id);
|
||||
return {
|
||||
id,
|
||||
type: 'single' as const,
|
||||
prompt,
|
||||
answer: optionIds.includes(question.answer) ? question.answer : (optionIds[0] || 'a'),
|
||||
explanation,
|
||||
options: normalizedOptions,
|
||||
};
|
||||
};
|
||||
|
||||
const hasTrainingConfigContent = (form: AgentFormState) => (
|
||||
form.training_workspace_enabled
|
||||
|| Boolean(form.training_summary_points_text.trim())
|
||||
|| Boolean(form.training_summary_checklist_text.trim())
|
||||
|| form.training_question_bank.length > 0
|
||||
);
|
||||
|
||||
/**
|
||||
* 生成培训试题 PDF 导入完成后的反馈文案。
|
||||
* 核心逻辑:先反馈题库导入结果,再补充 OCR 回退与诊断提示,避免 HR 误以为扫描件导入结果可直接信任。
|
||||
*/
|
||||
const buildTrainingPdfImportMessages = (
|
||||
payload: TrainingImportPayload,
|
||||
t: ReturnType<typeof useI18n>['t'],
|
||||
): { primary: string; notices: string[] } => {
|
||||
const skipped = Array.isArray(payload.skipped_question_numbers) ? payload.skipped_question_numbers : [];
|
||||
const extractionWarnings = Array.isArray(payload.extraction_warnings)
|
||||
? payload.extraction_warnings.map((item) => String(item || '').trim()).filter(Boolean)
|
||||
: [];
|
||||
|
||||
const primary = skipped.length
|
||||
? t('admin.agent.training.importPdfPartialSuccess', {
|
||||
count: payload.parsed_count,
|
||||
skipped: skipped.join('、'),
|
||||
})
|
||||
: t('admin.agent.training.importPdfSuccess', {
|
||||
count: payload.parsed_count,
|
||||
fileName: payload.guide_file_name,
|
||||
});
|
||||
|
||||
const notices: string[] = [];
|
||||
if (payload.extraction_mode === 'ocr') {
|
||||
notices.push(t('admin.agent.training.importPdfOcrUsed'));
|
||||
} else if (payload.extraction_mode === 'ocr_fallback') {
|
||||
notices.push(t('admin.agent.training.importPdfOcrFallback'));
|
||||
}
|
||||
if (extractionWarnings.length) {
|
||||
notices.push(t('admin.agent.training.importPdfWarnings', {
|
||||
warnings: extractionWarnings.join(';'),
|
||||
}));
|
||||
}
|
||||
|
||||
return { primary, notices };
|
||||
};
|
||||
|
||||
/**
|
||||
* 从 Agent extra_config 解析 tool_run_progress 表单字段。
|
||||
* 核心逻辑:capabilities 含 tool_run_progress 时视为启用;task_id 与 schedule_task_id 等价。
|
||||
@@ -120,8 +294,10 @@ export function useAgentsAdmin() {
|
||||
const agents = ref<AgentRow[]>([]);
|
||||
const users = ref<AdminUserRow[]>([]);
|
||||
const scheduleTasks = ref<ScheduleTask[]>([]);
|
||||
const trainingExamPdfGuides = ref<AgentGuideSummary[]>([]);
|
||||
const isLoading = ref(true);
|
||||
const isSaving = ref(false);
|
||||
const isImportingTrainingPdf = ref(false);
|
||||
const isEditing = ref(false);
|
||||
const selectedAgentId = ref<number | null>(null);
|
||||
const form = ref<AgentFormState>(createEmptyAgentForm());
|
||||
@@ -168,6 +344,22 @@ export function useAgentsAdmin() {
|
||||
}
|
||||
};
|
||||
|
||||
const loadTrainingExamPdfGuides = async (agentId: number | null | undefined): Promise<void> => {
|
||||
if (!agentId) {
|
||||
trainingExamPdfGuides.value = [];
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const guides = await listAdminAgentGuides(agentId);
|
||||
trainingExamPdfGuides.value = (Array.isArray(guides) ? guides : []).filter((guide) => (
|
||||
guide?.file_type === 'pdf' && guide?.guide_usage === 'training_exam_pdf'
|
||||
));
|
||||
} catch (e) {
|
||||
console.error('Error loading training exam PDF guides:', e);
|
||||
trainingExamPdfGuides.value = [];
|
||||
}
|
||||
};
|
||||
|
||||
const loadAll = async (): Promise<void> => {
|
||||
isLoading.value = true;
|
||||
await Promise.all([loadAgents(), loadUsers(), loadScheduleTasks()]);
|
||||
@@ -177,6 +369,7 @@ export function useAgentsAdmin() {
|
||||
const resetForm = (): void => {
|
||||
selectedAgentId.value = null;
|
||||
form.value = createEmptyAgentForm();
|
||||
trainingExamPdfGuides.value = [];
|
||||
isEditing.value = false;
|
||||
};
|
||||
|
||||
@@ -185,6 +378,7 @@ export function useAgentsAdmin() {
|
||||
? agent.extra_config
|
||||
: {};
|
||||
const toolRunProgress = parseToolRunProgressFromExtra(extra);
|
||||
const trainingConfig = parseTrainingConfigFromExtra(extra);
|
||||
selectedAgentId.value = agent.id;
|
||||
form.value = {
|
||||
id: agent.id,
|
||||
@@ -205,8 +399,10 @@ export function useAgentsAdmin() {
|
||||
? Math.floor(Number(extra.sort_order))
|
||||
: 0,
|
||||
...toolRunProgress,
|
||||
...trainingConfig,
|
||||
};
|
||||
isEditing.value = true;
|
||||
void loadTrainingExamPdfGuides(agent.id);
|
||||
};
|
||||
|
||||
const openDeleteConfirm = (
|
||||
@@ -242,6 +438,32 @@ export function useAgentsAdmin() {
|
||||
msg.warning(t('admin.dept.msg.nameRequired'));
|
||||
return false;
|
||||
}
|
||||
if (form.value.training_workspace_enabled) {
|
||||
if (form.value.training_question_bank.length === 0) {
|
||||
msg.warning(t('admin.agent.training.validation.questionRequired'));
|
||||
return false;
|
||||
}
|
||||
for (let index = 0; index < form.value.training_question_bank.length; index += 1) {
|
||||
const question = form.value.training_question_bank[index];
|
||||
if (!question) continue;
|
||||
if (!String(question.prompt || '').trim()) {
|
||||
msg.warning(t('admin.agent.training.validation.questionPromptRequired', { index: index + 1 }));
|
||||
return false;
|
||||
}
|
||||
if (question.type === 'single') {
|
||||
const validOptions = question.options.map((item) => String(item || '').trim()).filter(Boolean);
|
||||
if (validOptions.length < 2) {
|
||||
msg.warning(t('admin.agent.training.validation.questionSingleOptions', { index: index + 1 }));
|
||||
return false;
|
||||
}
|
||||
const validAnswerIds = validOptions.map((_, optionIndex) => TRAINING_OPTION_IDS[optionIndex]).filter(Boolean);
|
||||
if (!validAnswerIds.includes(question.answer)) {
|
||||
msg.warning(t('admin.agent.training.validation.questionAnswerRequired', { index: index + 1 }));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const supportsToolRunProgress = form.value.type === 'api' && form.value.dify_app_type !== 'workflow';
|
||||
if (supportsToolRunProgress && form.value.tool_run_progress_enabled && !form.value.schedule_task_id) {
|
||||
msg.warning(t('admin.agent.toolRunProgress.scheduleTaskRequired'));
|
||||
@@ -280,6 +502,17 @@ export function useAgentsAdmin() {
|
||||
applyToolRunProgressToExtra(extra_config, { ...form.value, tool_run_progress_enabled: false });
|
||||
}
|
||||
applyAgentCategoryToExtra(extra_config, form.value);
|
||||
extra_config.training_workspace_enabled = Boolean(form.value.training_workspace_enabled);
|
||||
if (hasTrainingConfigContent(form.value)) {
|
||||
extra_config.training_config = {
|
||||
pass_score: Math.max(0, Math.min(100, Math.round(Number(form.value.training_pass_score) || 90))),
|
||||
summary_points: splitMultilineText(form.value.training_summary_points_text),
|
||||
summary_checklist: splitMultilineText(form.value.training_summary_checklist_text),
|
||||
question_bank: form.value.training_question_bank.map((question, index) => buildTrainingQuestionPayload(question, index)),
|
||||
};
|
||||
} else {
|
||||
delete extra_config.training_config;
|
||||
}
|
||||
|
||||
try {
|
||||
const editingId = isEditing.value ? Number(form.value.id) : null;
|
||||
@@ -336,6 +569,70 @@ export function useAgentsAdmin() {
|
||||
form.value.suggested_questions.splice(index, 1);
|
||||
};
|
||||
|
||||
const addTrainingQuestion = (): void => {
|
||||
form.value.training_question_bank.push(createEmptyTrainingQuestionForm());
|
||||
};
|
||||
|
||||
const removeTrainingQuestion = (index: number): void => {
|
||||
form.value.training_question_bank.splice(index, 1);
|
||||
};
|
||||
|
||||
const addTrainingOption = (questionIndex: number): void => {
|
||||
const question = form.value.training_question_bank[questionIndex];
|
||||
if (!question || question.type !== 'single') return;
|
||||
question.options.push('');
|
||||
};
|
||||
|
||||
const removeTrainingOption = (questionIndex: number, optionIndex: number): void => {
|
||||
const question = form.value.training_question_bank[questionIndex];
|
||||
if (!question || question.type !== 'single') return;
|
||||
question.options.splice(optionIndex, 1);
|
||||
if (question.options.length < 2) {
|
||||
question.options.push('');
|
||||
}
|
||||
const validAnswerIds = question.options
|
||||
.map((item) => String(item || '').trim())
|
||||
.filter(Boolean)
|
||||
.map((_, normalizedIndex) => TRAINING_OPTION_IDS[normalizedIndex])
|
||||
.filter(Boolean);
|
||||
question.answer = validAnswerIds.includes(question.answer) ? question.answer : (validAnswerIds[0] || 'a');
|
||||
};
|
||||
|
||||
/**
|
||||
* 从已上传的培训试题 PDF 导入题库到当前 Agent 表单。
|
||||
* 核心逻辑:解析结果只回填当前表单,不直接保存数据库,确保 HR 可以先检查题干、选项和答案后再提交。
|
||||
*/
|
||||
const importTrainingQuestionBankFromPdf = async (guideId?: number | null): Promise<void> => {
|
||||
const agentId = Number(form.value.id);
|
||||
if (!agentId) {
|
||||
msg.warning(t('admin.agent.training.importPdfAgentRequired'));
|
||||
return;
|
||||
}
|
||||
isImportingTrainingPdf.value = true;
|
||||
try {
|
||||
const payload = await importAdminAgentTrainingQuestionBankFromPdf(agentId, { guide_id: guideId ?? null });
|
||||
form.value.training_question_bank = (payload.question_bank || []).map((question, index) => ({
|
||||
id: String(question.id || '').trim() || `q${index + 1}`,
|
||||
type: question.type === 'boolean' ? 'boolean' : 'single',
|
||||
prompt: String(question.prompt || '').trim(),
|
||||
explanation: String(question.explanation || '').trim(),
|
||||
options: question.type === 'boolean'
|
||||
? []
|
||||
: (question.options || []).map((option) => String(option.label || '').replace(/^[A-D]\.\s*/i, '').trim()),
|
||||
answer: String(question.answer || '').trim() || (question.type === 'boolean' ? 'true' : 'a'),
|
||||
}));
|
||||
form.value.training_workspace_enabled = true;
|
||||
const importMessages = buildTrainingPdfImportMessages(payload, t);
|
||||
msg.success(importMessages.primary);
|
||||
importMessages.notices.forEach((notice) => msg.warning(notice));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
} finally {
|
||||
isImportingTrainingPdf.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const deleteAgent = async (id: number): Promise<void> => {
|
||||
openDeleteConfirm(t('admin.agent.confirmDelete'), async () => {
|
||||
try {
|
||||
@@ -436,9 +733,11 @@ export function useAgentsAdmin() {
|
||||
agents,
|
||||
users,
|
||||
scheduleTasks,
|
||||
trainingExamPdfGuides,
|
||||
activeUsers,
|
||||
isLoading,
|
||||
isSaving,
|
||||
isImportingTrainingPdf,
|
||||
isEditing,
|
||||
selectedAgentId,
|
||||
form,
|
||||
@@ -457,6 +756,11 @@ export function useAgentsAdmin() {
|
||||
submitAgent,
|
||||
addSuggestedQuestion,
|
||||
removeSuggestedQuestion,
|
||||
addTrainingQuestion,
|
||||
removeTrainingQuestion,
|
||||
addTrainingOption,
|
||||
removeTrainingOption,
|
||||
importTrainingQuestionBankFromPdf,
|
||||
deleteAgent,
|
||||
openTestModal,
|
||||
runAgentTest,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<script setup lang="ts">
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, computed } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { Plus, Trash2, Edit2, Play, FileText, Image as ImageIcon } from 'lucide-vue-next';
|
||||
@@ -19,6 +19,7 @@ const form = ref({
|
||||
file_name: '',
|
||||
file_path: '',
|
||||
file_type: 'pdf',
|
||||
guide_usage: 'general',
|
||||
file_size: 0,
|
||||
remark: '',
|
||||
sort_order: 0,
|
||||
@@ -68,6 +69,7 @@ const openCreateModal = () => {
|
||||
file_name: '',
|
||||
file_path: '',
|
||||
file_type: 'pdf',
|
||||
guide_usage: 'general',
|
||||
file_size: 0,
|
||||
remark: '',
|
||||
sort_order: 0,
|
||||
@@ -84,6 +86,7 @@ const openEditModal = (guide: any) => {
|
||||
file_name: guide.file_name,
|
||||
file_path: guide.file_path,
|
||||
file_type: guide.file_type,
|
||||
guide_usage: guide.guide_usage || 'general',
|
||||
file_size: guide.file_size,
|
||||
remark: guide.remark,
|
||||
sort_order: guide.sort_order,
|
||||
@@ -105,6 +108,12 @@ const handleFileUpload = async (event: Event) => {
|
||||
form.value.file_path = res.file_path;
|
||||
form.value.file_type = res.file_type;
|
||||
form.value.file_size = res.file_size;
|
||||
if (form.value.guide_usage === 'training_video' && res.file_type !== 'video') {
|
||||
form.value.guide_usage = 'general';
|
||||
}
|
||||
if (form.value.guide_usage === 'training_exam_pdf' && res.file_type !== 'pdf') {
|
||||
form.value.guide_usage = 'general';
|
||||
}
|
||||
if (!form.value.file_name) {
|
||||
form.value.file_name = res.file_name;
|
||||
}
|
||||
@@ -215,6 +224,7 @@ const getIcon = (type: string) => {
|
||||
<tr>
|
||||
<th class="px-6 py-4">类型</th>
|
||||
<th class="px-6 py-4">资料名称</th>
|
||||
<th class="px-6 py-4">{{ t('admin.platformApps.guideUsage') }}</th>
|
||||
<th class="px-6 py-4">Agent应用</th>
|
||||
<th class="px-6 py-4">文件大小</th>
|
||||
<th class="px-6 py-4">排序</th>
|
||||
@@ -231,6 +241,11 @@ const getIcon = (type: string) => {
|
||||
<div class="font-medium text-default">{{ guide.file_name }}</div>
|
||||
<div class="text-xs text-muted mt-1 truncate max-w-[200px]">{{ guide.remark }}</div>
|
||||
</td>
|
||||
<td class="px-6 py-4 text-muted">
|
||||
<span v-if="guide.guide_usage === 'training_video'">{{ t('admin.platformApps.trainingVideo') }}</span>
|
||||
<span v-else-if="guide.guide_usage === 'training_exam_pdf'">{{ t('admin.platformApps.trainingExamPdf') }}</span>
|
||||
<span v-else>{{ t('admin.platformApps.generalGuide') }}</span>
|
||||
</td>
|
||||
<td class="px-6 py-4">
|
||||
<span class="inline-flex items-center px-2 py-1 rounded-md text-xs font-medium bg-brand-50 text-brand-700 border border-brand-100">
|
||||
{{ currentAgentName }}
|
||||
@@ -296,6 +311,15 @@ const getIcon = (type: string) => {
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-4">
|
||||
<div class="space-y-2">
|
||||
<label class="text-sm font-medium text-default">{{ t('admin.platformApps.guideUsage') }}</label>
|
||||
<select v-model="form.guide_usage" class="input-field w-full">
|
||||
<option value="general">{{ t('admin.platformApps.generalGuide') }}</option>
|
||||
<option value="training_video" :disabled="form.file_type !== 'video'">{{ t('admin.platformApps.trainingVideo') }}</option>
|
||||
<option value="training_exam_pdf" :disabled="form.file_type !== 'pdf'">{{ t('admin.platformApps.trainingExamPdf') }}</option>
|
||||
</select>
|
||||
<p class="text-xs text-muted">{{ t('admin.platformApps.guideUsageHint') }}</p>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-sm font-medium text-default">排序权重</label>
|
||||
<input v-model.number="form.sort_order" type="number" class="input-field w-full" />
|
||||
|
||||
Reference in new issue
Block a user