fix(chat): 智能体支持配置分级显示

This commit is contained in:
sophie committed 2026-06-25 17:37:48 +08:00
1 parent a451951e7a
commit 70d7435b5f
27 files changed
+645 -76

No files matched your search

@@ -16,6 +16,14 @@ import AgentPermissionModal from './components/AgentPermissionModal.vue';
import AgentTestModal from './components/AgentTestModal.vue';
import { useAgentsAdmin } from './composables/useAgentsAdmin';
const formatAgentCategory = (agent: { extra_config?: Record<string, unknown> }) => {
const extra = agent.extra_config && typeof agent.extra_config === 'object' ? agent.extra_config : {};
const category = String(extra.category || '').trim();
const subCategory = String(extra.sub_category || '').trim();
if (category && subCategory) return `${category} / ${subCategory}`;
return category || subCategory || '—';
};
const { t } = useI18n();
const {
@@ -111,8 +119,9 @@ onMounted(() => {
</div>
<table v-else class="admin-table table-fixed w-full">
<colgroup>
<col class="w-72" />
<col class="w-64" />
<col class="w-[4.5rem]" />
<col class="w-36" />
<col class="w-32" />
<col class="w-[5.5rem]" />
<col />
@@ -121,6 +130,7 @@ onMounted(() => {
<tr>
<th class="px-4 py-4 text-sm">{{ t('admin.agent.headers.name') }}</th>
<th class="px-2 py-4 text-sm">{{ t('admin.agent.headers.type') }}</th>
<th class="px-2 py-4 text-sm">{{ t('admin.agent.headers.category') }}</th>
<th class="px-2 py-4 text-sm">{{ t('admin.agent.headers.config') }}</th>
<th class="px-2 py-4 text-sm">{{ t('admin.agent.headers.status') }}</th>
<th class="px-4 py-4 text-sm text-right">{{ t('admin.agent.headers.actions') }}</th>
@@ -143,6 +153,9 @@ onMounted(() => {
{{ agent.type }}
</span>
</td>
<td class="px-2 py-5 text-xs text-secondary max-w-36 truncate" :title="formatAgentCategory(agent)">
{{ formatAgentCategory(agent) }}
</td>
<td class="px-2 py-5 font-mono text-xs text-muted max-w-32 truncate">
<span v-if="agent.type === 'api'" class="truncate" :title="String(agent.dify_app_id)">
<span class="text-muted">{{ t('admin.agent.idLabel') }}:</span> {{ agent.dify_app_id }}
@@ -107,6 +107,41 @@ const { t } = useI18n();
/>
</div>
<div class="col-span-1 md:col-span-2 rounded-2xl border border-default bg-surface-soft/40 p-4">
<div class="mb-4 text-xs font-bold uppercase tracking-wider text-muted">{{ t('admin.agent.sections.category') }}</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.fields.category') }}</label>
<input
v-model="form.category"
type="text"
class="w-full input-field text-sm"
:placeholder="t('admin.agent.fields.categoryPlaceholder')"
/>
</div>
<div class="space-y-2">
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.fields.subCategory') }}</label>
<input
v-model="form.sub_category"
type="text"
class="w-full input-field text-sm"
:placeholder="t('admin.agent.fields.subCategoryPlaceholder')"
/>
</div>
<div class="space-y-2">
<label class="text-xs font-semibold text-secondary">{{ t('admin.agent.fields.sortOrder') }}</label>
<input
v-model.number="form.sort_order"
type="number"
min="0"
step="1"
class="w-full input-field text-sm"
:placeholder="t('admin.agent.fields.sortOrderPlaceholder')"
/>
</div>
</div>
</div>
<div class="col-span-1 md:col-span-2 space-y-3">
<div class="flex flex-wrap items-center justify-between gap-3">
<label class="text-xs font-bold text-muted uppercase tracking-wider">常见问题</label>
@@ -91,6 +91,28 @@ const applyToolRunProgressToExtra = (extraConfig: Record<string, unknown>, form:
delete extraConfig.tool_run_progress;
};
/**
* 将表单分类字段写入 extra_config(P1 轻量方案)。
*/
const applyAgentCategoryToExtra = (extraConfig: Record<string, unknown>, form: AgentFormState): void => {
const category = String(form.category || '').trim();
const subCategory = String(form.sub_category || '').trim();
const sortRaw = Number(form.sort_order);
const sortOrder = Number.isFinite(sortRaw) && sortRaw >= 0 ? Math.floor(sortRaw) : 0;
if (category) {
extraConfig.category = category;
} else {
delete extraConfig.category;
}
if (subCategory) {
extraConfig.sub_category = subCategory;
} else {
delete extraConfig.sub_category;
}
extraConfig.sort_order = sortOrder;
};
export function useAgentsAdmin() {
const { t } = useI18n();
const msg = useMessage();
@@ -177,6 +199,11 @@ export function useAgentsAdmin() {
suggested_questions_style: agent.extra_config?.suggested_questions_style === 'styled' ? 'styled' : 'plain',
suggested_questions: normalizeSuggestedQuestions(agent.extra_config?.suggested_questions),
is_active: agent.is_active,
category: String(extra.category || ''),
sub_category: String(extra.sub_category || ''),
sort_order: Number(extra.sort_order) >= 0 && Number.isFinite(Number(extra.sort_order))
? Math.floor(Number(extra.sort_order))
: 0,
...toolRunProgress,
};
isEditing.value = true;
@@ -252,6 +279,7 @@ export function useAgentsAdmin() {
} else {
applyToolRunProgressToExtra(extra_config, { ...form.value, tool_run_progress_enabled: false });
}
applyAgentCategoryToExtra(extra_config, form.value);
try {
const editingId = isEditing.value ? Number(form.value.id) : null;
@@ -46,6 +46,12 @@ export interface AgentFormState {
suggested_questions_style: 'plain' | 'styled';
suggested_questions: AgentSuggestedQuestionItem[];
is_active: boolean;
/** 一级分类,写入 extra_config.category */
category: string;
/** 二级分类,写入 extra_config.sub_category */
sub_category: string;
/** 排序权重,写入 extra_config.sort_order */
sort_order: number;
/** 是否启用 extra_config.capabilities 中的 tool_run_progress */
tool_run_progress_enabled: boolean;
/** 对应 ScheduleTask.id,写入 extra_config.tool_run_progress.schedule_task_id */
@@ -66,6 +72,9 @@ export const createEmptyAgentForm = (): AgentFormState => ({
suggested_questions_style: 'plain',
suggested_questions: [],
is_active: true,
category: '',
sub_category: '',
sort_order: 0,
tool_run_progress_enabled: false,
schedule_task_id: null,
poll_interval_seconds: 2,
@@ -5,11 +5,10 @@
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from 'vue';
import { BookOpen, History, LayoutGrid } from 'lucide-vue-next';
import AgentCategoryList from '@/shared/components/AgentCategoryList.vue';
import type { AgentWithCategory } from '@/shared/utils/agent-category.utils';
export interface ChatMobileHeaderAgent {
id: number;
name: string;
}
export interface ChatMobileHeaderAgent extends AgentWithCategory {}
defineProps<{
agentName: string;
@@ -99,21 +98,12 @@ onBeforeUnmount(() => {
class="absolute right-4 top-12 z-50 w-72 rounded-2xl border border-default bg-surface p-3 shadow-xl"
>
<div class="mb-2 text-xs font-medium text-muted">{{ $t('chat.agents') }}</div>
<div class="grid grid-cols-2 gap-2">
<button
v-for="agent in userAgents"
:key="agent.id"
type="button"
class="flex flex-col items-center gap-1.5 rounded-xl px-2 py-3 text-center transition-colors hover-surface-soft"
@click="onSwitchAgent(agent)"
>
<div class="flex h-9 w-9 items-center justify-center rounded-lg bg-brand-50 text-sm font-bold text-brand-600">
{{ agent.name.charAt(0) }}
</div>
<span class="w-full truncate text-xs font-medium text-secondary">{{ agent.name }}</span>
</button>
</div>
<div v-if="userAgents.length === 0" class="py-3 text-center text-xs text-muted">{{ $t('chat.noOtherAgents') }}</div>
<AgentCategoryList
v-if="userAgents.length > 0"
:agents="userAgents"
@select="onSwitchAgent"
/>
<div v-else class="py-3 text-center text-xs text-muted">{{ $t('chat.noOtherAgents') }}</div>
</div>
</header>
</template>
@@ -20,13 +20,11 @@ import {
X,
} from 'lucide-vue-next';
import LocaleToggleButton from '@/shared/components/LocaleToggleButton.vue';
import AgentCategoryList from '@/shared/components/AgentCategoryList.vue';
import type { AgentWithCategory } from '@/shared/utils/agent-category.utils';
import type { DisplayHistoryItem } from '../types/chat.types';
export interface ChatSidebarAgent {
id: number;
name: string;
icon_url?: string;
}
export interface ChatSidebarAgent extends AgentWithCategory {}
const collapsed = defineModel<boolean>('collapsed', { required: true });
const agentPanelOpen = defineModel<boolean>('agentPanelOpen', { required: true });
@@ -227,29 +225,26 @@ const userInitial = (fallback: string) => {
</div>
<div class="mt-auto flex flex-col p-4">
<div v-if="userAgents.length > 0 && agentPanelOpen" class="chat-agent-panel">
<div class="mb-2 flex items-center gap-1.5 text-xs font-semibold text-muted">
<LayoutGrid class="h-3.5 w-3.5" /> {{ $t('chat.agents') }}
</div>
<div class="grid grid-cols-2 gap-x-2 gap-y-1.5">
<div v-if="userAgents.length > 0" class="chat-agent-panel">
<div class="mb-2 flex items-center justify-between gap-2">
<div class="flex items-center gap-1.5 text-xs font-semibold text-muted">
<LayoutGrid class="h-3.5 w-3.5" /> {{ $t('chat.agents') }}
<span class="font-normal opacity-70">({{ userAgents.length }})</span>
</div>
<button
v-for="agent in userAgents"
:key="agent.id"
type="button"
class="grid w-full min-w-0 grid-cols-[1rem_1fr] items-center gap-1.5 rounded-xl px-2 py-2 text-left text-xs font-medium text-secondary transition-colors hover-surface-soft hover:shadow-sm"
@click="emit('switch-agent', agent)"
class="text-[10px] font-medium text-brand-600 hover:text-brand-700"
@click="agentPanelOpen = !agentPanelOpen"
>
<div
v-if="agent.icon_url"
class="h-4 w-4 shrink-0 rounded bg-contain bg-center bg-no-repeat"
:style="{ backgroundImage: `url(${agent.icon_url})` }"
/>
<div v-else class="flex h-4 w-4 shrink-0 items-center justify-center rounded bg-brand-100 text-[9px] font-bold text-brand-600">
{{ agent.name.charAt(0) }}
</div>
<span class="min-w-0 truncate">{{ agent.name }}</span>
{{ agentPanelOpen ? $t('chat.collapseAgents') : $t('chat.expandAgents') }}
</button>
</div>
<AgentCategoryList
v-show="agentPanelOpen"
:agents="userAgents"
compact
@select="emit('switch-agent', $event)"
/>
</div>
<div ref="userMenuRef" class="relative flex items-center justify-between text-muted">
@@ -6,6 +6,7 @@ import { computed, ref, watch, type ComputedRef } from 'vue';
import { BusinessError } from '@/shared/api/client';
import { getUserAgent, listUserAgents } from '@/features/auth/api/user.api';
import { setLastChatAgentId } from '@/shared/utils/lastChatAgent';
import { sortAgentsByCategory } from '@/shared/utils/agent-category.utils';
import type { ChatDeps } from './chat.deps';
import type { UseChatGuidesReturn } from './useChatGuides';
@@ -75,7 +76,16 @@ export function useChatAgent(options: UseChatAgentOptions) {
const { t, msg, router } = deps;
const currentAgent = ref<any>(null);
const userAgents = ref<Array<{ id: number; name: string; description: string; icon_url?: string; dify_app_type?: 'chatflow' | 'workflow' }>>([]);
const userAgents = ref<Array<{
id: number;
name: string;
description: string;
icon_url?: string;
dify_app_type?: 'chatflow' | 'workflow';
category?: string | null;
sub_category?: string | null;
sort_order?: number;
}>>([]);
const showAgentPanel = ref(true);
const selectedKb = ref(DEFAULT_KB);
const kbOptions = [DEFAULT_KB, ERP_KB];
@@ -162,12 +172,12 @@ export function useChatAgent(options: UseChatAgentOptions) {
const loadUserAgents = async () => {
try {
const agents = await listUserAgents();
userAgents.value = Array.isArray(agents)
const filtered = Array.isArray(agents)
? agents.filter((a: any) => a.id !== currentAgentId.value && (a.dify_app_type || 'chatflow') === 'chatflow')
: [];
if (userAgents.value.length === 0) {
showAgentPanel.value = false;
}
userAgents.value = sortAgentsByCategory(filtered);
// [STEP 1] 有可切换智能体时默认展开面板;避免切换 Agent 后列表「消失」
showAgentPanel.value = userAgents.value.length > 0;
} catch {
userAgents.value = [];
showAgentPanel.value = false;
@@ -16,6 +16,12 @@ import {
type WorkspaceUser,
} from '../types/workspace.types';
import type { WorkspaceDeps } from './workspace.deps';
import {
extractAgentCategories,
filterAgentsByQuery,
normalizeAgentCategoryFields,
sortAgentsByCategory,
} from '@/shared/utils/agent-category.utils';
interface HomeOptions {
currentUser: Ref<WorkspaceUser | null>;
@@ -41,6 +47,7 @@ export function useWorkspaceHome(deps: WorkspaceDeps, options: HomeOptions) {
{ id: 'all_agents', visible: true, collapsed: false, title: 'workspace.availableAgents' },
]);
const searchQuery = ref('');
const selectedCategory = ref<string | null>(null);
const showAnnouncementModal = ref(false);
const selectedAnnouncement = ref<WorkspaceAnnouncement | null>(null);
const userReport = ref<unknown>(null);
@@ -53,13 +60,18 @@ export function useWorkspaceHome(deps: WorkspaceDeps, options: HomeOptions) {
const favoriteAgents = computed(() => agents.value.filter((a) => favoriteAgentIds.value.includes(a.id)));
const chatflowAgents = computed(() =>
sortAgentsByCategory(agents.value.filter((a) => (a.dify_app_type || 'chatflow') === 'chatflow')),
);
const agentCategories = computed(() => extractAgentCategories(chatflowAgents.value));
const otherAgents = computed(() => {
const chatflowAgents = agents.value.filter((a) => (a.dify_app_type || 'chatflow') === 'chatflow');
if (!searchQuery.value.trim()) return chatflowAgents;
const q = searchQuery.value.toLowerCase();
return chatflowAgents.filter(
(a) => a.name.toLowerCase().includes(q) || a.description.toLowerCase().includes(q),
);
let list = chatflowAgents.value;
if (selectedCategory.value) {
list = list.filter((a) => normalizeAgentCategoryFields(a).category === selectedCategory.value);
}
return filterAgentsByQuery(list, searchQuery.value);
});
const filteredQuickQuestions = computed(() => {
@@ -85,6 +97,7 @@ export function useWorkspaceHome(deps: WorkspaceDeps, options: HomeOptions) {
favoriteAgentIds.value = [];
embeddedAgents.value = [];
userReport.value = null;
selectedCategory.value = null;
};
const fetchHome = async () => {
@@ -322,6 +335,8 @@ export function useWorkspaceHome(deps: WorkspaceDeps, options: HomeOptions) {
SECTION_TITLE_KEY_MAP,
layoutSections,
searchQuery,
selectedCategory,
agentCategories,
showAnnouncementModal,
selectedAnnouncement,
userReport,
@@ -161,6 +161,26 @@ const ws = inject(WORKSPACE_KEY)! as any;
<!-- Content: All Agents -->
<div v-if="section.id === 'all_agents'">
<div class="mb-4 flex flex-wrap gap-2">
<button
type="button"
class="rounded-full px-3 py-1.5 text-xs font-medium transition-colors"
:class="!ws.selectedCategory ? 'bg-brand-600 text-white' : 'bg-surface-soft text-secondary hover:bg-slate-100'"
@click="ws.selectedCategory = null"
>
{{ $t('workspace.agentCategory.all') }}
</button>
<button
v-for="category in ws.agentCategories"
:key="category"
type="button"
class="rounded-full px-3 py-1.5 text-xs font-medium transition-colors"
:class="ws.selectedCategory === category ? 'bg-brand-600 text-white' : 'bg-surface-soft text-secondary hover:bg-slate-100'"
@click="ws.selectedCategory = category"
>
{{ category }}
</button>
</div>
<div class="mb-6">
<input v-model="ws.searchQuery" type="text" :placeholder="$t('workspace.search')" class="input-field max-w-md shadow-sm" />
</div>
@@ -29,6 +29,9 @@ export interface WorkspaceAgent {
description: string;
icon_url?: string;
dify_app_type?: 'chatflow' | 'workflow';
category?: string | null;
sub_category?: string | null;
sort_order?: number;
}
export interface WorkspaceQuickQuestion {