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

@@ -0,0 +1,144 @@
/**
* shared/components/AgentCategoryList.vue
* 用途:按一级/二级分类展示可切换的 Agent 列表,支持搜索与折叠分组。
* 依赖:agent-category.utils、vue-i18n。
* 逻辑:ChatSidebar、ChatMobileHeader 等入口传入 agents 与 select 事件即可复用。
*/
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n';
import { ChevronDown, ChevronRight, Search } from 'lucide-vue-next';
import {
filterAgentsByQuery,
groupAgentsByCategory,
type AgentWithCategory,
} from '@/shared/utils/agent-category.utils';
const props = withDefaults(defineProps<{
agents: AgentWithCategory[];
compact?: boolean;
showSearch?: boolean;
defaultExpanded?: boolean;
}>(), {
compact: false,
showSearch: true,
defaultExpanded: true,
});
const emit = defineEmits<{
select: [agent: AgentWithCategory];
}>();
const { t } = useI18n();
const searchQuery = ref('');
const collapsedCategories = ref<Set<string>>(new Set());
const uncategorizedLabel = computed(() => t('chat.agentCategory.uncategorized'));
const filteredAgents = computed(() => filterAgentsByQuery(props.agents, searchQuery.value));
const categoryGroups = computed(() =>
groupAgentsByCategory(filteredAgents.value, uncategorizedLabel.value),
);
/**
* 同步分类折叠态:新增分类默认展开,移除已不存在的折叠记录。
*/
const syncCollapsedState = () => {
const keys = new Set(categoryGroups.value.map((group) => group.category));
const next = new Set<string>();
for (const key of collapsedCategories.value) {
if (keys.has(key)) next.add(key);
}
if (!props.defaultExpanded) {
for (const group of categoryGroups.value) {
if (!next.has(group.category)) next.add(group.category);
}
}
collapsedCategories.value = next;
};
watch(categoryGroups, syncCollapsedState, { immediate: true });
const toggleCategory = (category: string) => {
const next = new Set(collapsedCategories.value);
if (next.has(category)) {
next.delete(category);
} else {
next.add(category);
}
collapsedCategories.value = next;
};
const isCategoryCollapsed = (category: string) => collapsedCategories.value.has(category);
const onSelect = (agent: AgentWithCategory) => {
emit('select', agent);
};
</script>
<template>
<div class="space-y-2">
<div v-if="showSearch" class="relative">
<Search class="pointer-events-none absolute left-2 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted" />
<input
v-model="searchQuery"
type="search"
class="w-full rounded-lg border border-default bg-surface py-1.5 pl-7 pr-2 text-xs text-primary placeholder:text-muted focus:border-brand-400 focus:outline-none focus:ring-1 focus:ring-brand-400"
:placeholder="t('chat.agentCategory.searchPlaceholder')"
/>
</div>
<div v-if="categoryGroups.length === 0" class="py-2 text-center text-xs text-muted">
{{ t('chat.agentCategory.noResults') }}
</div>
<div v-else class="max-h-48 space-y-2 overflow-y-auto scrollbar-thin">
<section v-for="group in categoryGroups" :key="group.category" class="space-y-1">
<button
type="button"
class="flex w-full items-center gap-1 rounded-md px-1 py-0.5 text-left text-[11px] font-semibold text-muted transition-colors hover:text-secondary"
@click="toggleCategory(group.category)"
>
<ChevronDown v-if="!isCategoryCollapsed(group.category)" class="h-3 w-3 shrink-0" />
<ChevronRight v-else class="h-3 w-3 shrink-0" />
<span class="min-w-0 flex-1 truncate">{{ group.category }}</span>
<span class="shrink-0 text-[10px] font-normal opacity-70">({{ group.agents.length }})</span>
</button>
<div v-show="!isCategoryCollapsed(group.category)" class="space-y-2 pl-1">
<template v-for="subGroup in group.subGroups" :key="`${group.category}-${subGroup.subCategory || '_'}`">
<div
v-if="subGroup.subCategory && group.subGroups.length > 1"
class="px-1 text-[10px] font-medium text-muted"
>
{{ subGroup.subCategory }}
</div>
<div :class="compact ? 'grid grid-cols-2 gap-x-2 gap-y-1.5' : 'grid grid-cols-2 gap-2'">
<button
v-for="agent in subGroup.agents"
: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="onSelect(agent)"
>
<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>
</button>
</div>
</template>
</div>
</section>
</div>
</div>
</template>