feat: Chat页面模型选择支持JSON格式的model字段

- parseAgentModels新增支持解析JSON数组格式[{id, name}]
- 模型选择下拉框使用模型id作为value,name作为label展示
- 选择模型时同时设置model_id和model字段
- 发起聊天请求时携带model_id参数
- 兼容旧格式:逗号分隔的字符串
main
sp mac bookpro 2605 2026-06-03 02:05:13 +08:00
parent 40c5396a80
commit 1f795e446d
1 changed files with 47 additions and 13 deletions

View File

@ -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 (
<div className="chat-shell">
@ -928,7 +955,14 @@ export default function ChatPage() {
options={modelOptions}
suffixIcon={<DownOutlined className="chat-model-select-arrow" />}
placeholder="选择模型"
onChange={(value) => setOverrides((o) => ({ ...o, model: String(value) }))}
onChange={(modelId) => {
const selectedModel = agentModels.find((m) => m.id === modelId);
setOverrides((o) => ({
...o,
model_id: String(modelId),
model: selectedModel?.name || String(modelId)
}));
}}
/>
<Upload