diff --git a/src/pages/ChatPage.tsx b/src/pages/ChatPage.tsx index 9f13c1e..14904e1 100644 --- a/src/pages/ChatPage.tsx +++ b/src/pages/ChatPage.tsx @@ -32,11 +32,31 @@ interface StreamingState { toolCalls: ToolCallTrace[]; } -const parseAgentModels = (value?: string) => - String(value || '') +interface AgentModelOption { + id: string; + name: string; +} + +const parseAgentModels = (value?: string): AgentModelOption[] => { + if (!value) return []; + // 尝试解析 JSON 格式 + try { + const parsed = JSON.parse(value); + if (Array.isArray(parsed)) { + return parsed.map((item: any) => ({ + id: typeof item === 'object' ? item.id : String(item), + name: typeof item === 'object' ? item.name : String(item) + })); + } + } catch { + // 兼容旧格式:逗号分隔的字符串 + } + return String(value || '') .split(',') .map((item) => item.trim()) - .filter(Boolean); + .filter(Boolean) + .map((item) => ({ id: item, name: item })); +}; export default function ChatPage() { const { id } = useParams(); @@ -155,9 +175,14 @@ export default function ChatPage() { } const a = await AgentAPI.detail(id); setAgent(a); - const firstModel = parseAgentModels(a.model)[0]; + const models = parseAgentModels(a.model); + const firstModel = models[0]; if (firstModel) { - setOverrides((o) => ({ ...o, model: o.model || firstModel })); + setOverrides((o) => ({ + ...o, + model: o.model || firstModel.name, + model_id: o.model_id || firstModel.id + })); } }; @@ -257,8 +282,9 @@ export default function ChatPage() { const ctrl = new AbortController(); abortRef.current = ctrl; - const model = overrides.model || parseAgentModels(agent?.model)[0] || ''; - const modelId = overrides.model_id || ''; + const models = parseAgentModels(agent?.model); + const model = overrides.model || models[0]?.name || ''; + const modelId = overrides.model_id || models[0]?.id || ''; const attText = buildAttachmentsText(); const content = attText ? `${text}\n\n${attText}` : text; @@ -427,7 +453,8 @@ export default function ChatPage() { scrollBottom(true); const attText = buildAttachmentsText(); const content = attText ? `${text}\n\n${attText}` : text; - const model = overrides.model || parseAgentModels(agent?.model)[0] || ''; + const models = parseAgentModels(agent?.model); + const model = overrides.model || models[0]?.name || ''; try { const res = await ChatAPI.send( id, @@ -612,11 +639,11 @@ export default function ChatPage() { const isImageUrl = (url: string) => url?.startsWith('http') || url?.startsWith('/'); const agentModel = agent?.model || ''; const agentModels = parseAgentModels(agentModel); - const modelOptions = agentModels.map((modelName) => ({ - value: modelName, - label: modelName + const modelOptions = agentModels.map((model) => ({ + value: model.id, + label: model.name })); - const activeModelValue = overrides.model || ''; + const activeModelValue = overrides.model_id || ''; return (