Files
syops/frontend/src/features/admin/modules/agents/AgentsPage.vue
T

308 lines
10 KiB
Vue

/**
* features/admin/modules/agents/AgentsPage.vue
*
* 用途:管理控制台的应用管理页,提供应用列表、创建/编辑弹窗、测试与授权入口。
* 依赖:useAgentsAdmin 业务编排、Element Plus 表格与按钮、AgentFormModal/AgentTestModal/AgentPermissionModal 弹窗。
* 逻辑:页面加载后先展示应用列表;目录、用户和排程在后台补齐。权限组启用后隐藏按人授权按钮。
* 列表使用 Element stripe 斑马纹。选中行浅底画在单元格上,盖过斑马纹。
*/
<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useI18n } from 'vue-i18n';
import { Plus } from '@element-plus/icons-vue';
import {
ElButton,
ElEmpty,
ElLoading,
ElTable,
ElTableColumn,
ElTag,
} from 'element-plus';
import 'element-plus/dist/index.css';
import 'element-plus/theme-chalk/dark/css-vars.css';
import '@/features/admin/styles/element-trial.css';
import AgentFormModal from './components/AgentFormModal.vue';
import AgentPermissionModal from './components/AgentPermissionModal.vue';
import AgentTestModal from './components/AgentTestModal.vue';
import { useAgentsAdmin } from './composables/useAgentsAdmin';
import type { AgentRow } from '@/features/admin/types/agent.types';
const { t } = useI18n();
const vLoading = ElLoading.directive;
const {
agents,
typeCatalog,
runtimeProviders,
conversationModes,
syncContentProfileFromIntentRoutes,
intentRouteOptions,
contentProfileOptions,
scheduleTasks,
trainingExamPdfGuides,
activeUsers,
isLoading,
isSaving,
isImportingTrainingPdf,
isEditing,
selectedAgentId,
form,
showTestModal,
isTesting,
testingAgent,
testResult,
showPermissionModal,
permissionAgent,
permissionUserIds,
rolesConfigured,
rolesChecked,
loadAll,
resetForm,
selectAgent,
submitAgent,
addSuggestedQuestion,
removeSuggestedQuestion,
addTrainingQuestion,
removeTrainingQuestion,
addTrainingOption,
removeTrainingOption,
importTrainingQuestionBankFromPdf,
deleteAgent,
openTestModal,
runAgentTest,
openPermissionModal,
togglePermissionUser,
savePermissions,
} = useAgentsAdmin();
/* 列表仅展示名称、状态和操作;类型/聚合/配置仍由编辑弹窗维护。 */
const formatAgentCode = (agent: AgentRow) => agent.agent_code || 'generic';
const formatAgentAggregationPrimary = (agent: AgentRow) => (
agent.is_aggregate ? t('admin.agent.directory.aggregateMode') : t('admin.agent.directory.normalMode')
);
const formatAgentAggregationSecondary = (agent: AgentRow) => {
const memberCount = Number(agent.directory_member_count || agent.directory_members?.length || 0);
if (agent.is_aggregate) return t('admin.agent.directory.memberCount', { count: memberCount });
return t('admin.agent.directory.normalListHint');
};
/** 配置列主行:Dify App ID 或 Iframe URL 摘要。 */
const formatAgentConfigPrimary = (agent: AgentRow) => {
if (agent.runtime_provider === 'iframe' || agent.type === 'iframe') {
const url = String(agent.iframe_url || '').trim();
return url || '—';
}
const appId = String(agent.dify_app_id || '').trim();
return appId || '—';
};
/** 配置列副行:配置类型说明。 */
const formatAgentConfigSecondary = (agent: AgentRow) => {
if (agent.runtime_provider === 'iframe' || agent.type === 'iframe') {
return t('admin.agent.fields.types.iframe');
}
const appType = agent.dify_app_type === 'workflow' ? 'Workflow' : 'Chatflow';
return `${t('admin.agent.fields.types.api')} · ${appType}`;
};
/**
* 列表「类型」列主文案:优先 Catalog 中文 label,否则 agent_code。
*/
const formatAgentTypeLabel = (agent: AgentRow) => {
const code = formatAgentCode(agent);
const entry = typeCatalog.value.find((item) => item.agent_code === code);
return entry?.label || code;
};
/**
* 列表「类型」列副文案:runtime_provider 四语言展示。
*/
const formatRuntimeProviderLabel = (agent: AgentRow) => {
const provider = agent.runtime_provider || (agent.type === 'iframe' ? 'iframe' : 'dify');
const key = `admin.agent.fields.runtimeProviders.${provider}`;
const translated = t(key);
return translated === key ? provider : translated;
};
void [formatAgentAggregationPrimary, formatAgentAggregationSecondary, formatAgentConfigPrimary,
formatAgentConfigSecondary, formatAgentTypeLabel, formatRuntimeProviderLabel];
const showFormDialog = ref(false);
/**
* 打开新建应用弹窗并重置表单状态。
* 核心逻辑:先调用 resetForm 清空编辑上下文,再展示表单弹窗,避免继承上一次选中应用的数据。
*/
const openCreateDialog = (): void => {
resetForm();
showFormDialog.value = true;
};
/**
* 打开编辑应用弹窗并载入当前行数据。
* 核心逻辑:复用 selectAgent 的表单映射逻辑,确保列表行、权限弹窗和保存接口使用同一份状态。
*/
const openEditDialog = (agent: Parameters<typeof selectAgent>[0]): void => {
selectAgent(agent);
showFormDialog.value = true;
};
/**
* 点击表格行时打开编辑弹窗。
* 核心逻辑:Element 行对象按 id 找回完整应用,避免把表格行类型直接交给 selectAgent。
*/
const onRowClick = (row: { id?: number }): void => {
const agent = agents.value.find((item) => item.id === row.id);
if (agent) openEditDialog(agent);
};
/**
* 当前选中应用的行样式。
* 核心逻辑:编辑弹窗打开前,用 selectedAgentId 给对应行加上浅底。
*/
const agentRowClass = ({ row }: { row: AgentRow }): string => (
selectedAgentId.value === row.id ? 'agents-row-selected' : ''
);
/**
* 保存应用表单,成功后关闭弹窗。
* 核心逻辑:submitAgent 会处理校验、接口调用和列表刷新;仅在返回 true 时收起弹窗。
*/
const saveAgentForm = async (): Promise<void> => {
const saved = await submitAgent();
if (saved) showFormDialog.value = false;
};
onMounted(() => {
void loadAll();
});
</script>
<template>
<section class="admin-table-section min-h-64">
<div class="admin-table-header admin-table-header--stacked">
<div>
<h2 class="section-title">{{ t('admin.agent.title') }}</h2>
<p class="section-desc mt-1">{{ t('admin.agent.subtitle') }}</p>
</div>
<ElButton type="primary" :icon="Plus" @click="openCreateDialog">
{{ t('admin.agent.new') }}
</ElButton>
</div>
<ElEmpty v-if="!isLoading && agents.length === 0" :description="t('admin.agent.noAgents')" />
<ElTable
v-else
v-loading="isLoading"
:data="agents"
row-key="id"
class="agents-element-table hx-admin-table"
stripe
:row-class-name="agentRowClass"
@row-click="onRowClick"
>
<ElTableColumn :label="t('admin.agent.headers.name')" min-width="280">
<template #default="{ row }">
<p class="truncate" :title="(row as AgentRow).name">
{{ (row as AgentRow).name }}
</p>
<p
v-if="(row as AgentRow).description"
class="truncate hx-element-hint"
:title="(row as AgentRow).description"
>
{{ (row as AgentRow).description }}
</p>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.agent.headers.status')" width="140">
<template #default="{ row }">
<ElTag :type="(row as AgentRow).is_active ? 'success' : 'info'" effect="light">
{{ (row as AgentRow).is_active ? t('admin.status.active') : t('admin.status.disabled') }}
</ElTag>
</template>
</ElTableColumn>
<ElTableColumn :label="t('admin.agent.headers.actions')" min-width="280" align="right">
<template #default="{ row }">
<div class="inline-flex items-center justify-end" @click.stop>
<ElButton link type="primary" @click="openEditDialog(row as AgentRow)">
{{ t('admin.edit') }}
</ElButton>
<ElButton
v-if="rolesChecked && !rolesConfigured"
link
type="primary"
@click="openPermissionModal(row as AgentRow)"
>
{{ t('admin.agent.manageAccess') }}
</ElButton>
<ElButton link type="primary" @click="openTestModal(row as AgentRow)">
{{ t('admin.agent.test') }}
</ElButton>
<ElButton link type="danger" @click="deleteAgent((row as AgentRow).id)">
{{ t('admin.agent.delete') }}
</ElButton>
</div>
</template>
</ElTableColumn>
</ElTable>
<AgentTestModal
:visible="showTestModal"
:agent="testingAgent"
:result="testResult"
:is-testing="isTesting"
@close="showTestModal = false"
@retest="runAgentTest()"
/>
<AgentFormModal
:visible="showFormDialog"
:is-editing="isEditing"
:is-saving="isSaving"
:is-importing-training-pdf="isImportingTrainingPdf"
:form="form"
:schedule-tasks="scheduleTasks"
:training-exam-pdf-guides="trainingExamPdfGuides"
:type-catalog="typeCatalog"
:intent-route-options="intentRouteOptions"
:content-profile-options="contentProfileOptions"
:runtime-providers="runtimeProviders"
:conversation-modes="conversationModes"
:agent-options="agents"
:sync-content-profile-from-intent-routes="syncContentProfileFromIntentRoutes"
@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
:visible="showPermissionModal"
:agent="permissionAgent"
:users="activeUsers()"
:selected-user-ids="permissionUserIds"
:is-saving="isSaving"
@close="showPermissionModal = false"
@save="savePermissions"
@toggle-user="togglePermissionUser"
/>
</section>
</template>
<style scoped>
.agents-element-table :deep(.agents-row-selected td.el-table__cell) {
background: color-mix(in srgb, #0284c7 8%, var(--el-bg-color, #fff));
}
.agents-element-table :deep(.el-table__row) {
cursor: pointer;
}
</style>