From 46ddf0652acaedf622356a4fafaf2bf8faf4431b Mon Sep 17 00:00:00 2001 From: yannyang Date: Mon, 3 Aug 2026 21:24:12 +0800 Subject: [PATCH] fix: fix show model name --- src/pages/AgentList/AgentListLogic.ts | 29 +++++++++++++++---- .../AgentList/components/AgentListWeb.tsx | 10 ++++--- src/pages/chat/components/ChatHeader.tsx | 27 +++++++++++++---- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/pages/AgentList/AgentListLogic.ts b/src/pages/AgentList/AgentListLogic.ts index 43d3a98..6b5636e 100644 --- a/src/pages/AgentList/AgentListLogic.ts +++ b/src/pages/AgentList/AgentListLogic.ts @@ -1,17 +1,23 @@ import { useEffect, useMemo, useState } from 'react'; -import { Agent, AgentAPI } from '../../api'; +import { Agent, AgentAPI, ModelAPI, AiModel } from '../../api'; import { useAuth } from '../../store/auth'; export function useAgentListLogic() { const { user } = useAuth(); const [list, setList] = useState([]); + const [models, setModels] = useState([]); const [loading, setLoading] = useState(false); const load = async () => { if (!user?.phone) return; setLoading(true); try { - setList(await AgentAPI.mine(user.phone)); + const [agentList, modelList] = await Promise.all([ + AgentAPI.mine(user.phone), + ModelAPI.list() + ]); + setList(agentList); + setModels(modelList); } finally { setLoading(false); } @@ -29,9 +35,17 @@ export function useAgentListLogic() { const isImageUrl = (url?: string): boolean => !!(url?.startsWith('http') || url?.startsWith('/')); const getModelLabel = (value: unknown): string => { + const findModelName = (id: string) => { + const model = models.find(m => m.id === id); + return model ? model.model_name : id; + }; + if (Array.isArray(value)) { const names = value - .map((item: any) => (typeof item === 'string' ? item : item?.name)) + .map((item: any) => { + if (typeof item === 'string') return findModelName(item); + return item?.name || findModelName(item?.id); + }) .map((v) => String(v || '').trim()) .filter(Boolean); return names.join('、'); @@ -43,7 +57,10 @@ export function useAgentListLogic() { const parsed = JSON.parse(raw); if (Array.isArray(parsed)) { const names = parsed - .map((item: any) => (typeof item === 'string' ? item : item?.name)) + .map((item: any) => { + if (typeof item === 'string') return findModelName(item); + return item?.name || findModelName(item?.id); + }) .map((v) => String(v || '').trim()) .filter(Boolean); return names.join('、'); @@ -54,11 +71,11 @@ export function useAgentListLogic() { if (raw.includes(',')) { return raw .split(',') - .map((s) => s.trim()) + .map((s) => findModelName(s.trim())) .filter(Boolean) .join('、'); } - return raw; + return findModelName(raw); }; const publicCount = useMemo(() => list.filter((a) => a.visibility === 'public').length, [list]); diff --git a/src/pages/AgentList/components/AgentListWeb.tsx b/src/pages/AgentList/components/AgentListWeb.tsx index 4fbbcb3..197f321 100644 --- a/src/pages/AgentList/components/AgentListWeb.tsx +++ b/src/pages/AgentList/components/AgentListWeb.tsx @@ -80,7 +80,9 @@ export default function AgentListWeb({ logic }: Props) { ) : ( - {list.map((a) => ( + {list.map((a) => { + const modelLabel = getModelLabel(a.model); + return (
@@ -124,14 +126,14 @@ export default function AgentListWeb({ logic }: Props) { 私有 )} - {getModelLabel(a.model) && ( + {modelLabel && ( - {getModelLabel(a.model)} + {modelLabel} )} @@ -168,7 +170,7 @@ export default function AgentListWeb({ logic }: Props) {
- ))} + )})}
)} diff --git a/src/pages/chat/components/ChatHeader.tsx b/src/pages/chat/components/ChatHeader.tsx index dddc748..0d314e9 100644 --- a/src/pages/chat/components/ChatHeader.tsx +++ b/src/pages/chat/components/ChatHeader.tsx @@ -1,9 +1,15 @@ +import { useState, useEffect } from 'react'; import { ApiOutlined, DeleteOutlined, EditOutlined, SettingOutlined, EllipsisOutlined, HistoryOutlined, LogoutOutlined } from '@ant-design/icons'; import { Button, Dropdown, Modal, Switch } from 'antd'; -import type { Agent } from '../../../api'; +import { Agent, ModelAPI, AiModel } from '../../../api'; import './ChatHeader.css'; -function formatAgentModel(raw: string | null | undefined) { +function formatAgentModel(raw: string | null | undefined, models: AiModel[]) { + const findModelName = (id: string) => { + const model = models.find(m => m.id === id); + return model ? model.model_name : id; + }; + const s = String(raw ?? '').trim(); if (!s) return '默认模型'; if (s.startsWith('[') || s.startsWith('{')) { @@ -11,19 +17,22 @@ function formatAgentModel(raw: string | null | undefined) { const parsed = JSON.parse(s); if (Array.isArray(parsed)) { const names = parsed - .map((x: any) => String(x?.name || x?.model || x?.id || '').trim()) + .map((x: any) => { + if (typeof x === 'string') return findModelName(x); + return String(x?.name || x?.model || findModelName(x?.id) || '').trim(); + }) .filter(Boolean); if (names.length > 2) { return names.slice(0, 2).join(', ') + '...'; } if (names.length) return names.join(', '); } else if (parsed && typeof parsed === 'object') { - const name = String((parsed as any).name || (parsed as any).model || (parsed as any).id || '').trim(); + const name = String((parsed as any).name || (parsed as any).model || findModelName((parsed as any).id) || '').trim(); if (name) return name; } } catch {} } - return s; + return findModelName(s); } const isImageUrl = (url: string) => url?.startsWith('http') || url?.startsWith('/'); @@ -40,7 +49,13 @@ export default function ChatHeader(props: { onLogout?: () => void; }) { const { agent, useStream, setUseStream, onOpenHistory, onOpenParams, onOpenMcp, onManageAgent, onClear, onLogout } = props; - const modelText = formatAgentModel(agent.model); + const [models, setModels] = useState([]); + + useEffect(() => { + ModelAPI.list().then(setModels).catch(console.error); + }, []); + + const modelText = formatAgentModel(agent.model, models); return (