feat(chat): 应用目录只配置成聚合应用

This commit is contained in:
sophie committed 2026-07-09 14:43:09 +08:00
1 parent 5bef77f86c
commit dab04d4c3e
28 files changed
+797 -138

No files matched your search

@@ -1,16 +1,16 @@
/**
* shared/components/AgentCategoryList.vue
* 用途:按一级/二级分类展示可切换的 Agent 列表,支持搜索与折叠分组。
* 依赖:agent-category.utils、vue-i18n。
* 依赖:agent-category.utils、vue-i18n、lucide-vue-next。
* 逻辑: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 { ChevronDown, ChevronRight, FolderTree, Search } from 'lucide-vue-next';
import {
filterAgentsByQuery,
groupAgentsByCategory,
sortAgentsByCategory,
type AgentWithCategory,
} from '@/shared/utils/agent-category.utils';
@@ -33,121 +33,261 @@ const emit = defineEmits<{
const { t } = useI18n();
const searchQuery = ref('');
const collapsedCategories = ref<Set<string>>(new Set());
const collapsedDirectories = ref<Set<number>>(new Set());
const uncategorizedLabel = computed(() => t('chat.agentCategory.uncategorized'));
const directoryMemberIds = computed(() => {
const ids = new Set<number>();
for (const agent of props.agents) {
for (const member of agent.directory_members || []) {
ids.add(member.id);
}
}
return ids;
});
const filteredAgents = computed(() => filterAgentsByQuery(props.agents, searchQuery.value));
const categoryGroups = computed(() =>
groupAgentsByCategory(filteredAgents.value, uncategorizedLabel.value),
/**
* 判断是否为聚合目录 Agent(含 directory_members 或显式 is_aggregate)。
*/
const isAggregateAgent = (agent: AgentWithCategory) => (
agent.is_aggregate === true || (agent.directory_members || []).length > 0
);
/**
* 同步分类折叠态:新增分类默认展开,移除已不存在的折叠记录。
* 搜索时同时匹配目录自身与其成员名称/分类。
*/
const syncCollapsedState = () => {
const keys = new Set(categoryGroups.value.map((group) => group.category));
const next = new Set<string>();
for (const key of collapsedCategories.value) {
const matchesQuery = (agent: AgentWithCategory, query: string) => {
if (!query.trim()) return true;
return filterAgentsByQuery([agent], query).length > 0
|| (agent.directory_members || []).some((member) => filterAgentsByQuery([member], query).length > 0);
};
const filteredAgents = computed(() => {
const q = searchQuery.value.trim();
return props.agents.filter((agent) => {
if (directoryMemberIds.value.has(agent.id)) return false;
return matchesQuery(agent, q);
});
});
const aggregateAgents = computed(() => (
sortAgentsByCategory(filteredAgents.value.filter((agent) => isAggregateAgent(agent)))
));
const standardAgents = computed(() => (
sortAgentsByCategory(filteredAgents.value.filter((agent) => !isAggregateAgent(agent)))
));
const visibleAgentsCount = computed(() => aggregateAgents.value.length + standardAgents.value.length);
/**
* 同步折叠态:清理已消失目录,并在 defaultExpanded=false 时默认收起。
*/
const syncCollapsedDirectories = () => {
const keys = new Set(aggregateAgents.value.map((agent) => agent.id));
const next = new Set<number>();
for (const key of collapsedDirectories.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);
for (const agent of aggregateAgents.value) {
if (!next.has(agent.id)) next.add(agent.id);
}
}
collapsedCategories.value = next;
collapsedDirectories.value = next;
};
watch(categoryGroups, syncCollapsedState, { immediate: true });
watch(aggregateAgents, syncCollapsedDirectories, { immediate: true });
const toggleCategory = (category: string) => {
const next = new Set(collapsedCategories.value);
if (next.has(category)) {
next.delete(category);
/**
* 切换指定聚合目录的展开/折叠。
*/
const toggleDirectory = (agentId: number) => {
const next = new Set(collapsedDirectories.value);
if (next.has(agentId)) {
next.delete(agentId);
} else {
next.add(category);
next.add(agentId);
}
collapsedCategories.value = next;
collapsedDirectories.value = next;
};
const isCategoryCollapsed = (category: string) => collapsedCategories.value.has(category);
const isDirectoryCollapsed = (agentId: number) => collapsedDirectories.value.has(agentId);
const isActiveAgent = (agent: AgentWithCategory) => agent.id === props.activeAgentId;
/**
* 按当前搜索词过滤目录成员;目录名命中时展示全部成员。
*/
const visibleDirectoryMembers = (agent: AgentWithCategory) => {
const q = searchQuery.value.trim();
const members = agent.directory_members || [];
if (!q || filterAgentsByQuery([agent], q).length > 0) return members;
return members.filter((member) => filterAgentsByQuery([member], q).length > 0);
};
/**
* 选中 Agent;已激活则忽略,避免重复切换。
*/
const onSelect = (agent: AgentWithCategory) => {
if (isActiveAgent(agent)) return;
emit('select', agent);
};
/**
* 统一 Agent 行的激活/默认样式类。
* @param active - 是否为当前选中项
* @param dense - 是否为目录成员等更紧凑的行
*/
const agentRowClass = (active: boolean, dense = false) => {
const base = dense
? 'group relative flex w-full min-w-0 items-center gap-2 rounded-lg px-2 py-1.5 text-left text-[11px] font-medium transition-all duration-150'
: 'group relative flex w-full min-w-0 items-center gap-2 rounded-xl px-2.5 py-2 text-left text-xs font-medium transition-all duration-150';
if (active) {
return `${base} cursor-default bg-brand-50 text-brand-700 shadow-sm ring-1 ring-brand-100/80 dark:bg-brand-900/25 dark:text-brand-300 dark:ring-brand-800/40`;
}
return `${base} text-secondary hover:bg-surface hover:text-primary hover:shadow-sm`;
};
</script>
<template>
<div class="space-y-2">
<div class="space-y-2.5">
<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" />
<Search class="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-muted/80" />
<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"
class="w-full rounded-xl border border-subtle bg-surface py-2 pl-8 pr-2.5 text-xs text-primary shadow-sm placeholder:text-muted/70 transition-shadow focus:border-brand-300 focus:outline-none focus:ring-2 focus:ring-brand-400/25"
:placeholder="t('chat.agentCategory.searchPlaceholder')"
/>
</div>
<div v-if="categoryGroups.length === 0" class="py-2 text-center text-xs text-muted">
<div v-if="visibleAgentsCount === 0" class="rounded-xl bg-surface-soft/50 py-4 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)"
<div v-else class="max-h-72 space-y-2.5 overflow-y-auto pr-0.5 scrollbar-thin">
<!-- 聚合目录 -->
<div v-if="aggregateAgents.length" class="space-y-2">
<div
v-for="agent in aggregateAgents"
:key="agent.id"
class="overflow-hidden rounded-2xl border border-subtle bg-surface shadow-sm"
>
<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"
<div class="flex min-w-0 items-stretch">
<button
type="button"
class="flex w-7 shrink-0 items-center justify-center text-muted transition-colors hover:bg-surface-soft hover:text-brand-600"
:title="isDirectoryCollapsed(agent.id) ? t('chat.expandAgents') : t('chat.collapseAgents')"
@click.stop="toggleDirectory(agent.id)"
>
{{ 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 transition-colors"
<ChevronRight
v-if="isDirectoryCollapsed(agent.id)"
class="h-3 w-3 transition-transform"
/>
<ChevronDown v-else class="h-3 w-3" />
</button>
<button
type="button"
class="flex min-w-0 flex-1 items-center gap-1.5 py-1.5 pr-2.5 text-left transition-colors"
:class="isActiveAgent(agent)
? 'cursor-default bg-brand-50/80'
: 'hover:bg-surface-soft/80'"
:aria-current="isActiveAgent(agent) ? 'page' : undefined"
@click="onSelect(agent)"
>
<span
class="flex h-5 w-5 shrink-0 items-center justify-center rounded-md"
:class="isActiveAgent(agent)
? 'cursor-default bg-brand-50 text-brand-700 shadow-sm ring-1 ring-brand-100 dark:bg-brand-900/20 dark:text-brand-300 dark:ring-brand-800/50'
: 'text-secondary hover-surface-soft hover:shadow-sm'"
:aria-current="isActiveAgent(agent) ? 'page' : undefined"
@click="onSelect(agent)"
? 'bg-brand-100 text-brand-600 dark:bg-brand-900/40 dark:text-brand-300'
: 'bg-surface-strong text-brand-500'"
>
<FolderTree class="h-3 w-3" />
</span>
<span
class="min-w-0 truncate text-xs font-semibold tracking-tight"
:class="isActiveAgent(agent) ? 'text-brand-700 dark:text-brand-300' : 'text-primary'"
>
{{ agent.name }}
</span>
<span
v-if="visibleDirectoryMembers(agent).length"
class="ml-auto shrink-0 rounded px-1 py-px text-[10px] font-medium tabular-nums text-muted"
>
{{ visibleDirectoryMembers(agent).length }}
</span>
</button>
</div>
<div
v-show="!isDirectoryCollapsed(agent.id)"
class="border-t border-subtle bg-surface-soft/40 px-2 py-2"
>
<div class="grid grid-cols-2 gap-1">
<button
v-for="member in visibleDirectoryMembers(agent)"
:key="`${agent.id}-${member.id}`"
type="button"
:class="agentRowClass(isActiveAgent(member), true)"
:title="member.name"
:aria-current="isActiveAgent(member) ? 'page' : undefined"
@click="onSelect(member)"
>
<span
v-if="isActiveAgent(member)"
class="absolute inset-y-1 left-0 w-0.5 rounded-full bg-brand-500"
/>
<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})` }"
v-if="member.icon_url"
class="h-5 w-5 shrink-0 rounded-md bg-contain bg-center bg-no-repeat ring-1 ring-black/5"
:style="{ backgroundImage: `url(${member.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"
class="flex h-5 w-5 shrink-0 items-center justify-center rounded-md bg-brand-50 text-[10px] font-bold text-brand-600 ring-1 ring-brand-100/60 dark:bg-brand-900/30 dark:text-brand-300 dark:ring-brand-800/40"
>
{{ agent.name.charAt(0) }}
{{ member.name.charAt(0) }}
</div>
<span class="min-w-0 truncate">{{ agent.name }}</span>
<span class="min-w-0 truncate leading-tight">{{ member.name }}</span>
</button>
</div>
</template>
</div>
</div>
</section>
</div>
<!-- 普通智能体 -->
<div
v-if="standardAgents.length"
:class="[
aggregateAgents.length ? 'border-t border-subtle/80 pt-2.5' : '',
compact ? 'grid grid-cols-2 gap-1' : 'grid grid-cols-2 gap-1.5',
]"
>
<button
v-for="agent in standardAgents"
:key="agent.id"
type="button"
:class="agentRowClass(isActiveAgent(agent))"
:title="agent.name"
:aria-current="isActiveAgent(agent) ? 'page' : undefined"
@click="onSelect(agent)"
>
<span
v-if="isActiveAgent(agent)"
class="absolute inset-y-1.5 left-0 w-0.5 rounded-full bg-brand-500"
/>
<div
v-if="agent.icon_url"
class="h-5 w-5 shrink-0 rounded-md bg-contain bg-center bg-no-repeat ring-1 ring-black/5"
:style="{ backgroundImage: `url(${agent.icon_url})` }"
/>
<div
v-else
class="flex h-5 w-5 shrink-0 items-center justify-center rounded-md bg-brand-50 text-[10px] font-bold text-brand-600 ring-1 ring-brand-100/60 dark:bg-brand-900/30 dark:text-brand-300 dark:ring-brand-800/40"
>
{{ agent.name.charAt(0) }}
</div>
<span class="min-w-0 truncate leading-tight">{{ agent.name }}</span>
</button>
</div>
</div>
</div>
</template>