refactor: 重构前端骨架,及admin模块子路由和懒加载
refactor: 优化多次请求,并使用pinia状态管理
This commit is contained in:
1 parent
f877f61efd
commit
61693d014d
74 files changed
+9493
-6418
No files matched your search
@@ -0,0 +1,253 @@
|
||||
/**
|
||||
* features/admin/modules/agents/AgentsPage.vue
|
||||
*
|
||||
* Agent 管理独立页面(Phase 1 首个拆分模块)。
|
||||
* 路由:/admin/agents
|
||||
*/
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onMounted } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { Bot, Plus, Trash2, Users } from 'lucide-vue-next';
|
||||
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog.vue';
|
||||
import AgentPermissionModal from './components/AgentPermissionModal.vue';
|
||||
import AgentTestModal from './components/AgentTestModal.vue';
|
||||
import { useAgentsAdmin } from './composables/useAgentsAdmin';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const {
|
||||
agents,
|
||||
activeUsers,
|
||||
isLoading,
|
||||
isSaving,
|
||||
isEditing,
|
||||
selectedAgentId,
|
||||
form,
|
||||
showTestModal,
|
||||
isTesting,
|
||||
testingAgent,
|
||||
testResult,
|
||||
showPermissionModal,
|
||||
permissionAgent,
|
||||
permissionUserIds,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
resetForm,
|
||||
selectAgent,
|
||||
submitAgent,
|
||||
deleteAgent,
|
||||
openTestModal,
|
||||
runAgentTest,
|
||||
openPermissionModal,
|
||||
togglePermissionUser,
|
||||
savePermissions,
|
||||
closeDeleteConfirm,
|
||||
confirmDelete,
|
||||
} = useAgentsAdmin();
|
||||
|
||||
onMounted(() => {
|
||||
void loadAll();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="admin-table-section">
|
||||
<div v-if="isLoading" class="flex items-center justify-center py-20">
|
||||
<div class="animate-spin rounded-full h-10 w-10 border-b-2 border-brand-600" />
|
||||
</div>
|
||||
|
||||
<template v-else>
|
||||
<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>
|
||||
<button type="button" class="btn-primary" @click="resetForm">
|
||||
<Plus class="w-5 h-5" />
|
||||
<span class="font-medium">{{ t('admin.agent.new') }}</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="admin-form-panel">
|
||||
<h3 class="section-title mb-6">{{ isEditing ? t('admin.agent.edit') : t('admin.agent.create') }}</h3>
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">{{ t('admin.agent.fields.name') }}</label>
|
||||
<input
|
||||
v-model="form.name"
|
||||
type="text"
|
||||
class="w-full border-default rounded-xl px-4 py-3 text-base focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 outline-none transition-all shadow-sm"
|
||||
:placeholder="t('admin.agent.fields.namePlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">{{ t('admin.agent.fields.type') }}</label>
|
||||
<select v-model="form.type" class="w-full input-field">
|
||||
<option value="api">{{ t('admin.agent.fields.types.api') }}</option>
|
||||
<option value="iframe">{{ t('admin.agent.fields.types.iframe') }}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<template v-if="form.type === 'api'">
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">Dify应用类型</label>
|
||||
<select v-model="form.dify_app_type" class="w-full input-field text-sm focus:ring-blue-500/20 focus:border-blue-500 outline-none shadow-sm">
|
||||
<option value="chatflow">Chatflow</option>
|
||||
<option value="workflow">Workflow</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">{{ t('admin.agent.fields.appId') }}</label>
|
||||
<input v-model="form.dify_app_id" type="text" class="w-full input-field text-sm font-mono focus:ring-blue-500/20 focus:border-blue-500 outline-none shadow-sm" />
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">API 基础 URL</label>
|
||||
<input
|
||||
v-model="form.api_base_url"
|
||||
type="text"
|
||||
class="w-full input-field text-sm font-mono focus:ring-blue-500/20 focus:border-blue-500 outline-none shadow-sm"
|
||||
:placeholder="t('admin.agent.fields.baseUrlPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">{{ t('admin.agent.fields.apiKey') }}</label>
|
||||
<input v-model="form.dify_api_key" type="password" class="w-full input-field text-sm font-mono focus:ring-blue-500/20 focus:border-blue-500 outline-none shadow-sm" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template v-if="form.type === 'iframe'">
|
||||
<div class="col-span-1 md:col-span-2 space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">{{ t('admin.agent.fields.iframeUrl') }}</label>
|
||||
<input v-model="form.iframe_url" type="text" class="w-full input-field text-sm font-mono focus:ring-blue-500/20 focus:border-blue-500 outline-none shadow-sm" placeholder="https://..." />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<div class="col-span-1 md:col-span-2 space-y-2">
|
||||
<label class="text-xs font-bold text-muted uppercase tracking-wider">{{ t('admin.agent.fields.description') }}</label>
|
||||
<input
|
||||
v-model="form.description"
|
||||
type="text"
|
||||
class="w-full input-field focus:ring-blue-500/20 focus:border-blue-500 outline-none shadow-sm"
|
||||
:placeholder="t('admin.agent.fields.descPlaceholder')"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-end">
|
||||
<button type="button" class="btn-primary btn-lg" @click="submitAgent">
|
||||
<Plus v-if="!isEditing" class="w-5 h-5" />
|
||||
{{ isEditing ? t('admin.update') : t('admin.add') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="admin-table-wrap">
|
||||
<div v-if="agents.length === 0" class="admin-table-empty">
|
||||
<Bot class="w-12 h-12 mx-auto text-muted mb-3" />
|
||||
<p class="text-lg text-secondary">{{ t('admin.agent.noAgents') }}</p>
|
||||
</div>
|
||||
<table v-else class="admin-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="px-8 py-4 text-sm">{{ t('admin.agent.headers.name') }}</th>
|
||||
<th class="px-8 py-4 text-sm">{{ t('admin.agent.headers.type') }}</th>
|
||||
<th class="px-8 py-4 text-sm">{{ t('admin.agent.headers.config') }}</th>
|
||||
<th class="px-8 py-4 text-sm">{{ t('admin.agent.headers.status') }}</th>
|
||||
<th class="px-8 py-4 text-sm text-right">{{ t('admin.agent.headers.actions') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr
|
||||
v-for="agent in agents"
|
||||
:key="agent.id"
|
||||
class="group cursor-pointer"
|
||||
:class="{ 'bg-blue-50/50': selectedAgentId === agent.id }"
|
||||
@click="selectAgent(agent)"
|
||||
>
|
||||
<td class="px-8 py-5">
|
||||
<div class="font-bold text-primary text-base">{{ agent.name }}</div>
|
||||
<div class="section-desc mt-1">{{ agent.description }}</div>
|
||||
</td>
|
||||
<td class="px-8 py-5">
|
||||
<span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-surface-soft text-primary uppercase tracking-wide">
|
||||
{{ agent.type }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-8 py-5 font-mono text-xs text-muted max-w-xs truncate">
|
||||
<span v-if="agent.type === 'api'" class="flex items-center gap-1">
|
||||
<span class="text-muted">{{ t('admin.agent.idLabel') }}:</span> {{ agent.dify_app_id }}
|
||||
</span>
|
||||
<span v-else class="flex items-center gap-1" :title="String(agent.extra_config?.iframe_url || '')">
|
||||
<span class="text-muted">{{ t('admin.agent.urlLabel') }}:</span> {{ agent.extra_config?.iframe_url }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-8 py-5">
|
||||
<span
|
||||
class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-medium border"
|
||||
:class="agent.is_active ? 'bg-green-50 text-green-700 border-green-100' : 'bg-surface-soft text-secondary border-default'"
|
||||
>
|
||||
<span class="w-2 h-2 rounded-full" :class="agent.is_active ? 'bg-green-500' : 'bg-surface-strong'" />
|
||||
{{ agent.is_active ? t('admin.status.active') : t('admin.status.disabled') }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-8 py-5 text-right flex justify-end gap-2">
|
||||
<button
|
||||
type="button"
|
||||
class="p-2 text-emerald-600 bg-emerald-50 hover:bg-emerald-100 rounded-lg transition-all"
|
||||
:title="t('admin.agent.test')"
|
||||
@click.stop="openTestModal(agent)"
|
||||
>
|
||||
{{ t('admin.agent.test') }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="p-2 text-blue-600 bg-blue-50 hover:bg-blue-100 rounded-lg transition-all"
|
||||
:title="t('admin.agent.manageAccess')"
|
||||
@click.stop="openPermissionModal(agent)"
|
||||
>
|
||||
<Users class="w-5 h-5" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="p-2 text-muted hover:text-red-600 hover:bg-red-50 rounded-lg transition-all"
|
||||
:title="t('admin.agent.delete')"
|
||||
@click.stop="deleteAgent(agent.id)"
|
||||
>
|
||||
<Trash2 class="w-5 h-5" />
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<AgentTestModal
|
||||
:visible="showTestModal"
|
||||
:agent="testingAgent"
|
||||
:result="testResult"
|
||||
:is-testing="isTesting"
|
||||
@close="showTestModal = false"
|
||||
@retest="runAgentTest()"
|
||||
/>
|
||||
|
||||
<AgentPermissionModal
|
||||
:visible="showPermissionModal"
|
||||
:agent="permissionAgent"
|
||||
:users="activeUsers()"
|
||||
:selected-user-ids="permissionUserIds"
|
||||
:is-saving="isSaving"
|
||||
@close="showPermissionModal = false"
|
||||
@save="savePermissions"
|
||||
@toggle-user="togglePermissionUser"
|
||||
/>
|
||||
|
||||
<DeleteConfirmDialog
|
||||
v-model="showDeleteConfirmDialog"
|
||||
:message="deleteConfirmMessage"
|
||||
@confirm="confirmDelete"
|
||||
@cancel="closeDeleteConfirm"
|
||||
/>
|
||||
</section>
|
||||
</template>
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* features/admin/modules/agents/components/AgentPermissionModal.vue
|
||||
*
|
||||
* 为指定 Agent 批量勾选可访问用户的权限弹窗。
|
||||
*/
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import type { AdminUserRow } from '@/features/admin/types/admin.types';
|
||||
import type { AgentRow } from '@/features/admin/types/agent.types';
|
||||
|
||||
defineProps<{
|
||||
visible: boolean;
|
||||
agent: AgentRow | null;
|
||||
users: AdminUserRow[];
|
||||
selectedUserIds: number[];
|
||||
isSaving: boolean;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
close: [];
|
||||
save: [];
|
||||
toggleUser: [userId: number];
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="visible && agent" class="admin-modal-overlay items-center">
|
||||
<div class="admin-modal-panel max-w-2xl max-h-[90vh] animate-fade-in-up">
|
||||
<div class="px-8 py-6 border-b border-subtle flex justify-between items-center">
|
||||
<div>
|
||||
<h3 class="text-lg font-bold text-primary">{{ t('admin.perm.agentModal.title') }}</h3>
|
||||
<p class="text-sm text-muted">
|
||||
{{ t('admin.perm.agentModal.agent') }}
|
||||
<span class="font-medium text-blue-600">{{ agent.name }}</span>
|
||||
</p>
|
||||
</div>
|
||||
<button type="button" class="btn-icon-ds rounded-full" @click="emit('close')">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 overflow-y-auto p-8">
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<div
|
||||
v-for="user in users"
|
||||
:key="user.id"
|
||||
class="flex items-center gap-3 p-3 rounded-xl border transition-all cursor-pointer"
|
||||
:class="selectedUserIds.includes(user.id) ? 'border-blue-200 bg-blue-50/50' : 'border-default hover-surface-soft'"
|
||||
@click="emit('toggleUser', user.id)"
|
||||
>
|
||||
<div
|
||||
class="w-5 h-5 rounded border flex items-center justify-center transition-colors"
|
||||
:class="selectedUserIds.includes(user.id) ? 'bg-blue-600 border-blue-600' : 'bg-surface border-default'"
|
||||
>
|
||||
<svg
|
||||
v-if="selectedUserIds.includes(user.id)"
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
class="h-3.5 w-3.5 text-white"
|
||||
viewBox="0 0 20 20"
|
||||
fill="currentColor"
|
||||
>
|
||||
<path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd" />
|
||||
</svg>
|
||||
</div>
|
||||
<div>
|
||||
<div class="font-medium text-primary text-sm">{{ user.username }}</div>
|
||||
<div class="text-xs text-muted">{{ user.real_name || user.email }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="admin-modal-footer admin-modal-footer--muted px-8 py-6">
|
||||
<button type="button" class="btn-ghost" @click="emit('close')">
|
||||
{{ t('admin.cancel') }}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="isSaving"
|
||||
class="btn-primary disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
@click="emit('save')"
|
||||
>
|
||||
<div v-if="isSaving" class="animate-spin rounded-full h-4 w-4 border-2 border-white border-t-transparent" />
|
||||
{{ t('admin.perm.agentModal.save') }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* features/admin/modules/agents/components/AgentTestModal.vue
|
||||
*
|
||||
* Agent 连通性测试弹窗:展示请求 URL、输出信息与分析结果。
|
||||
*/
|
||||
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import type { AgentRow, AgentTestResult } from '@/features/admin/types/agent.types';
|
||||
|
||||
defineProps<{
|
||||
visible: boolean;
|
||||
agent: AgentRow | null;
|
||||
result: AgentTestResult | null;
|
||||
isTesting: boolean;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
close: [];
|
||||
retest: [];
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div v-if="visible && agent" class="admin-modal-overlay items-center">
|
||||
<div class="admin-modal-panel max-w-3xl max-h-[90vh] animate-fade-in-up">
|
||||
<div class="px-8 py-6 border-b border-subtle flex justify-between items-start">
|
||||
<div>
|
||||
<h3 class="text-lg font-bold text-primary">{{ t('admin.agent.testModalTitle') }}</h3>
|
||||
<p class="section-desc mt-1">
|
||||
{{ t('admin.agent.testModalAgent') }}:
|
||||
<span class="font-medium text-blue-600">{{ agent.name }}</span>
|
||||
</p>
|
||||
</div>
|
||||
<button type="button" class="btn-icon-ds rounded-full" @click="emit('close')">✕</button>
|
||||
</div>
|
||||
|
||||
<div class="flex-1 overflow-y-auto p-8 space-y-5">
|
||||
<div class="flex items-center justify-between">
|
||||
<div class="text-sm text-secondary">
|
||||
<span v-if="result?.tested_at">{{ t('admin.agent.testedAt') }}:{{ result.tested_at }}</span>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
:disabled="isTesting"
|
||||
class="btn-primary btn-sm disabled:opacity-50"
|
||||
@click="emit('retest')"
|
||||
>
|
||||
{{ isTesting ? t('admin.agent.testing') : t('admin.agent.retest') }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div v-if="isTesting" class="text-sm text-muted">{{ t('admin.agent.testingInProgress') }}</div>
|
||||
|
||||
<template v-if="result">
|
||||
<div>
|
||||
<div class="text-xs font-semibold text-muted uppercase tracking-wider mb-2">{{ t('admin.agent.requestUrl') }}</div>
|
||||
<div class="admin-code-block">{{ result.request_url || '-' }}</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="text-xs font-semibold text-muted uppercase tracking-wider mb-2">{{ t('admin.agent.outputInfo') }}</div>
|
||||
<pre class="admin-code-block max-h-64 overflow-y-auto whitespace-pre-wrap">{{ result.output_info || '-' }}</pre>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<div class="text-xs font-semibold text-muted uppercase tracking-wider mb-2">{{ t('admin.agent.analysis') }}</div>
|
||||
<div
|
||||
class="p-3 rounded-lg border text-sm"
|
||||
:class="result.success ? 'bg-green-50 border-green-200 text-green-800' : 'bg-red-50 border-red-200 text-red-800'"
|
||||
>
|
||||
{{ result.analysis }}
|
||||
<span v-if="result.http_status">(HTTP {{ result.http_status }})</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,305 @@
|
||||
/**
|
||||
* features/admin/modules/agents/composables/useAgentsAdmin.ts
|
||||
*
|
||||
* Agent 管理模块的业务逻辑:列表 CRUD、连通性测试、权限分配。
|
||||
* 从原 AdminView.vue 的 agents 相关 script 抽取,供 AgentsPage 使用。
|
||||
*/
|
||||
|
||||
import { ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import {
|
||||
createAdminAgent,
|
||||
deleteAdminAgent,
|
||||
listAdminAgents,
|
||||
listAdminUsers,
|
||||
saveAdminAgentPermissions,
|
||||
testAdminAgent,
|
||||
updateAdminAgent,
|
||||
} from '@/api/admin';
|
||||
import { useMessage } from '@/composables/useMessage';
|
||||
import type { AdminUserRow } from '@/features/admin/types/admin.types';
|
||||
import type { AgentFormState, AgentRow, AgentTestResult } from '@/features/admin/types/agent.types';
|
||||
import { createEmptyAgentForm } from '@/features/admin/types/agent.types';
|
||||
|
||||
export function useAgentsAdmin() {
|
||||
const { t } = useI18n();
|
||||
const msg = useMessage();
|
||||
|
||||
const agents = ref<AgentRow[]>([]);
|
||||
const users = ref<AdminUserRow[]>([]);
|
||||
const isLoading = ref(true);
|
||||
const isSaving = ref(false);
|
||||
const isEditing = ref(false);
|
||||
const selectedAgentId = ref<number | null>(null);
|
||||
const form = ref<AgentFormState>(createEmptyAgentForm());
|
||||
|
||||
const showTestModal = ref(false);
|
||||
const isTesting = ref(false);
|
||||
const testingAgent = ref<AgentRow | null>(null);
|
||||
const testResult = ref<AgentTestResult | null>(null);
|
||||
|
||||
const showPermissionModal = ref(false);
|
||||
const permissionAgent = ref<AgentRow | null>(null);
|
||||
const permissionUserIds = ref<number[]>([]);
|
||||
|
||||
const showDeleteConfirmDialog = ref(false);
|
||||
const deleteConfirmMessage = ref('');
|
||||
let deleteConfirmAction: (() => Promise<void> | void) | null = null;
|
||||
|
||||
const activeUsers = () => users.value.filter((u) => u.status !== 'pending');
|
||||
|
||||
const loadAgents = async (): Promise<void> => {
|
||||
try {
|
||||
agents.value = await listAdminAgents();
|
||||
} catch (e) {
|
||||
console.error('Error loading agents:', e);
|
||||
agents.value = [];
|
||||
}
|
||||
};
|
||||
|
||||
const loadUsers = async (): Promise<void> => {
|
||||
try {
|
||||
users.value = await listAdminUsers();
|
||||
} catch (e) {
|
||||
console.error('Error loading users:', e);
|
||||
users.value = [];
|
||||
}
|
||||
};
|
||||
|
||||
const loadAll = async (): Promise<void> => {
|
||||
isLoading.value = true;
|
||||
await Promise.all([loadAgents(), loadUsers()]);
|
||||
isLoading.value = false;
|
||||
};
|
||||
|
||||
const resetForm = (): void => {
|
||||
selectedAgentId.value = null;
|
||||
form.value = createEmptyAgentForm();
|
||||
isEditing.value = false;
|
||||
};
|
||||
|
||||
const selectAgent = (agent: AgentRow): void => {
|
||||
selectedAgentId.value = agent.id;
|
||||
form.value = {
|
||||
id: agent.id,
|
||||
name: agent.name,
|
||||
dify_app_id: agent.dify_app_id,
|
||||
dify_api_key: agent.dify_api_key || '',
|
||||
description: agent.description || '',
|
||||
type: agent.type,
|
||||
dify_app_type: agent.extra_config?.dify_app_type === 'workflow' ? 'workflow' : 'chatflow',
|
||||
api_base_url: agent.api_base_url || '',
|
||||
iframe_url: String(agent.extra_config?.iframe_url || ''),
|
||||
is_active: agent.is_active,
|
||||
};
|
||||
isEditing.value = true;
|
||||
};
|
||||
|
||||
const openDeleteConfirm = (
|
||||
message: string,
|
||||
action: () => Promise<void> | void,
|
||||
): void => {
|
||||
deleteConfirmMessage.value = message;
|
||||
deleteConfirmAction = action;
|
||||
showDeleteConfirmDialog.value = true;
|
||||
};
|
||||
|
||||
const closeDeleteConfirm = (): void => {
|
||||
showDeleteConfirmDialog.value = false;
|
||||
deleteConfirmAction = null;
|
||||
};
|
||||
|
||||
const confirmDelete = async (): Promise<void> => {
|
||||
if (!deleteConfirmAction) return;
|
||||
try {
|
||||
await deleteConfirmAction();
|
||||
} finally {
|
||||
closeDeleteConfirm();
|
||||
}
|
||||
};
|
||||
|
||||
const submitAgent = async (): Promise<void> => {
|
||||
if (!form.value.name) {
|
||||
msg.warning(t('admin.dept.msg.nameRequired'));
|
||||
return;
|
||||
}
|
||||
|
||||
isSaving.value = true;
|
||||
const extra_config: Record<string, unknown> = {};
|
||||
if (form.value.type === 'iframe' && form.value.iframe_url) {
|
||||
extra_config.iframe_url = form.value.iframe_url;
|
||||
}
|
||||
if (form.value.type === 'api') {
|
||||
extra_config.dify_app_type = form.value.dify_app_type === 'workflow' ? 'workflow' : 'chatflow';
|
||||
}
|
||||
|
||||
try {
|
||||
const editingId = isEditing.value ? Number(form.value.id) : null;
|
||||
if (isEditing.value && form.value.id != null) {
|
||||
const payload: Record<string, unknown> = {
|
||||
name: form.value.name,
|
||||
dify_app_id: form.value.dify_app_id,
|
||||
description: form.value.description,
|
||||
type: form.value.type,
|
||||
api_base_url: form.value.api_base_url,
|
||||
extra_config,
|
||||
is_active: form.value.is_active,
|
||||
};
|
||||
if (form.value.dify_api_key) {
|
||||
payload.dify_api_key = form.value.dify_api_key;
|
||||
}
|
||||
await updateAdminAgent(form.value.id, payload);
|
||||
} else {
|
||||
await createAdminAgent({
|
||||
name: form.value.name,
|
||||
dify_app_id: form.value.dify_app_id,
|
||||
dify_api_key: form.value.dify_api_key,
|
||||
description: form.value.description,
|
||||
type: form.value.type,
|
||||
api_base_url: form.value.api_base_url,
|
||||
extra_config,
|
||||
});
|
||||
}
|
||||
|
||||
await loadAgents();
|
||||
if (editingId) {
|
||||
const updated = agents.value.find((a) => a.id === editingId);
|
||||
if (updated) selectAgent(updated);
|
||||
} else {
|
||||
resetForm();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const deleteAgent = async (id: number): Promise<void> => {
|
||||
openDeleteConfirm(t('admin.agent.confirmDelete'), async () => {
|
||||
try {
|
||||
await deleteAdminAgent(id);
|
||||
await loadAgents();
|
||||
if (selectedAgentId.value === id) {
|
||||
resetForm();
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.requestFailed'));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const openTestModal = (agent: AgentRow): void => {
|
||||
testingAgent.value = agent;
|
||||
testResult.value = null;
|
||||
showTestModal.value = true;
|
||||
void runAgentTest(agent);
|
||||
};
|
||||
|
||||
const runAgentTest = async (agent?: AgentRow): Promise<void> => {
|
||||
const target = agent || testingAgent.value;
|
||||
if (!target) return;
|
||||
isTesting.value = true;
|
||||
try {
|
||||
const data = await testAdminAgent(target.id);
|
||||
testResult.value = {
|
||||
success: !!data.success,
|
||||
request_url: data.request_url || '',
|
||||
output_info: data.output_info || '',
|
||||
analysis: data.analysis || (data.success ? t('admin.agent.testDone') : t('admin.agent.testFailed')),
|
||||
http_status: data.http_status,
|
||||
tested_at: data.tested_at,
|
||||
};
|
||||
|
||||
if (data.success) {
|
||||
const idx = agents.value.findIndex((a) => a.id === target.id);
|
||||
const row = idx >= 0 ? agents.value[idx] : undefined;
|
||||
if (row) {
|
||||
row.is_active = true;
|
||||
}
|
||||
if (selectedAgentId.value === target.id) {
|
||||
form.value.is_active = true;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
testResult.value = {
|
||||
success: false,
|
||||
request_url: '',
|
||||
output_info: (e as Error).message || t('admin.agent.networkError'),
|
||||
analysis: t('admin.agent.testFailedRequestException'),
|
||||
http_status: 0,
|
||||
};
|
||||
} finally {
|
||||
isTesting.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
const openPermissionModal = (agent: AgentRow): void => {
|
||||
permissionAgent.value = agent;
|
||||
permissionUserIds.value = users.value
|
||||
.filter((u) => u.agent_ids.includes(agent.id))
|
||||
.map((u) => u.id);
|
||||
showPermissionModal.value = true;
|
||||
};
|
||||
|
||||
const togglePermissionUser = (userId: number): void => {
|
||||
const index = permissionUserIds.value.indexOf(userId);
|
||||
if (index === -1) {
|
||||
permissionUserIds.value.push(userId);
|
||||
} else {
|
||||
permissionUserIds.value.splice(index, 1);
|
||||
}
|
||||
};
|
||||
|
||||
const savePermissions = async (): Promise<void> => {
|
||||
if (!permissionAgent.value) return;
|
||||
isSaving.value = true;
|
||||
try {
|
||||
await saveAdminAgentPermissions(
|
||||
permissionAgent.value.id,
|
||||
permissionUserIds.value,
|
||||
);
|
||||
await loadUsers();
|
||||
showPermissionModal.value = false;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
msg.error((e as Error).message || t('admin.msg.failed'));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
agents,
|
||||
users,
|
||||
activeUsers,
|
||||
isLoading,
|
||||
isSaving,
|
||||
isEditing,
|
||||
selectedAgentId,
|
||||
form,
|
||||
showTestModal,
|
||||
isTesting,
|
||||
testingAgent,
|
||||
testResult,
|
||||
showPermissionModal,
|
||||
permissionAgent,
|
||||
permissionUserIds,
|
||||
showDeleteConfirmDialog,
|
||||
deleteConfirmMessage,
|
||||
loadAll,
|
||||
resetForm,
|
||||
selectAgent,
|
||||
submitAgent,
|
||||
deleteAgent,
|
||||
openTestModal,
|
||||
runAgentTest,
|
||||
openPermissionModal,
|
||||
togglePermissionUser,
|
||||
savePermissions,
|
||||
closeDeleteConfirm,
|
||||
confirmDelete,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user