feat: Chat页面模型选择支持JSON格式的model字段
- parseAgentModels新增支持解析JSON数组格式[{id, name}]
- 模型选择下拉框使用模型id作为value,name作为label展示
- 选择模型时同时设置model_id和model字段
- 发起聊天请求时携带model_id参数
- 兼容旧格式:逗号分隔的字符串
main
parent
40c5396a80
commit
1f795e446d
|
|
@ -32,11 +32,31 @@ interface StreamingState {
|
||||||
toolCalls: ToolCallTrace[];
|
toolCalls: ToolCallTrace[];
|
||||||
}
|
}
|
||||||
|
|
||||||
const parseAgentModels = (value?: string) =>
|
interface AgentModelOption {
|
||||||
String(value || '')
|
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(',')
|
.split(',')
|
||||||
.map((item) => item.trim())
|
.map((item) => item.trim())
|
||||||
.filter(Boolean);
|
.filter(Boolean)
|
||||||
|
.map((item) => ({ id: item, name: item }));
|
||||||
|
};
|
||||||
|
|
||||||
export default function ChatPage() {
|
export default function ChatPage() {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
|
|
@ -155,9 +175,14 @@ export default function ChatPage() {
|
||||||
}
|
}
|
||||||
const a = await AgentAPI.detail(id);
|
const a = await AgentAPI.detail(id);
|
||||||
setAgent(a);
|
setAgent(a);
|
||||||
const firstModel = parseAgentModels(a.model)[0];
|
const models = parseAgentModels(a.model);
|
||||||
|
const firstModel = models[0];
|
||||||
if (firstModel) {
|
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();
|
const ctrl = new AbortController();
|
||||||
abortRef.current = ctrl;
|
abortRef.current = ctrl;
|
||||||
|
|
||||||
const model = overrides.model || parseAgentModels(agent?.model)[0] || '';
|
const models = parseAgentModels(agent?.model);
|
||||||
const modelId = overrides.model_id || '';
|
const model = overrides.model || models[0]?.name || '';
|
||||||
|
const modelId = overrides.model_id || models[0]?.id || '';
|
||||||
const attText = buildAttachmentsText();
|
const attText = buildAttachmentsText();
|
||||||
const content = attText ? `${text}\n\n${attText}` : text;
|
const content = attText ? `${text}\n\n${attText}` : text;
|
||||||
|
|
||||||
|
|
@ -427,7 +453,8 @@ export default function ChatPage() {
|
||||||
scrollBottom(true);
|
scrollBottom(true);
|
||||||
const attText = buildAttachmentsText();
|
const attText = buildAttachmentsText();
|
||||||
const content = attText ? `${text}\n\n${attText}` : text;
|
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 {
|
try {
|
||||||
const res = await ChatAPI.send(
|
const res = await ChatAPI.send(
|
||||||
id,
|
id,
|
||||||
|
|
@ -612,11 +639,11 @@ export default function ChatPage() {
|
||||||
const isImageUrl = (url: string) => url?.startsWith('http') || url?.startsWith('/');
|
const isImageUrl = (url: string) => url?.startsWith('http') || url?.startsWith('/');
|
||||||
const agentModel = agent?.model || '';
|
const agentModel = agent?.model || '';
|
||||||
const agentModels = parseAgentModels(agentModel);
|
const agentModels = parseAgentModels(agentModel);
|
||||||
const modelOptions = agentModels.map((modelName) => ({
|
const modelOptions = agentModels.map((model) => ({
|
||||||
value: modelName,
|
value: model.id,
|
||||||
label: modelName
|
label: model.name
|
||||||
}));
|
}));
|
||||||
const activeModelValue = overrides.model || '';
|
const activeModelValue = overrides.model_id || '';
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="chat-shell">
|
<div className="chat-shell">
|
||||||
|
|
@ -928,7 +955,14 @@ export default function ChatPage() {
|
||||||
options={modelOptions}
|
options={modelOptions}
|
||||||
suffixIcon={<DownOutlined className="chat-model-select-arrow" />}
|
suffixIcon={<DownOutlined className="chat-model-select-arrow" />}
|
||||||
placeholder="选择模型"
|
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
|
<Upload
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue