Skip to content

Commit 04419b4

Browse files
committed
fix: 修复编辑交易员时「AI模型配置不存在或未启用」错误
问题: - 编辑交易员并修改系统提示词模板时,保存失败 - 错误提示:AI模型配置不存在或未启用 根本原因: 1. 后端 API 返回的 ai_model 被截断(admin_deepseek → deepseek) 2. 前端验证时找不到对应的模型 ID(enabledModels 存的是完整 ID) 3. API 缺少 system_prompt_template 字段 修复内容: - api/server.go: 移除 AI model ID 截断逻辑,返回完整 ID - api/server.go: 在 handleGetTraderConfig 中添加 system_prompt_template 字段 - web/src/types.ts: TraderConfigData 接口添加 system_prompt_template 字段 - web/src/components/AITradersPage.tsx: 添加 fallback 机制和详细日志 测试: - 编辑交易员 → 修改系统提示词模板 → 保存成功 - Console 输出验证日志,不再报错
1 parent 8faa2b3 commit 04419b4

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

api/server.go

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -801,30 +801,24 @@ func (s *Server) handleGetTraderConfig(c *gin.Context) {
801801
}
802802
}
803803

804-
// AIModelID 应该已经是 provider(如 "deepseek"),直接使用
805-
// 如果是旧数据格式(如 "admin_deepseek"),提取 provider 部分
806-
aiModelID := traderConfig.AIModelID
807-
// 兼容旧数据:如果包含下划线,提取最后一部分作为 provider
808-
if strings.Contains(aiModelID, "_") {
809-
parts := strings.Split(aiModelID, "_")
810-
aiModelID = parts[len(parts)-1]
811-
}
812-
804+
// 返回完整的 AIModelID(如 "admin_deepseek"),不要截断
805+
// 前端需要完整 ID 来验证模型是否存在
813806
result := map[string]interface{}{
814-
"trader_id": traderConfig.ID,
815-
"trader_name": traderConfig.Name,
816-
"ai_model": aiModelID,
817-
"exchange_id": traderConfig.ExchangeID,
818-
"initial_balance": traderConfig.InitialBalance,
819-
"btc_eth_leverage": traderConfig.BTCETHLeverage,
820-
"altcoin_leverage": traderConfig.AltcoinLeverage,
821-
"trading_symbols": traderConfig.TradingSymbols,
822-
"custom_prompt": traderConfig.CustomPrompt,
823-
"override_base_prompt": traderConfig.OverrideBasePrompt,
824-
"is_cross_margin": traderConfig.IsCrossMargin,
825-
"use_coin_pool": traderConfig.UseCoinPool,
826-
"use_oi_top": traderConfig.UseOITop,
827-
"is_running": isRunning,
807+
"trader_id": traderConfig.ID,
808+
"trader_name": traderConfig.Name,
809+
"ai_model": traderConfig.AIModelID, // 使用完整 ID
810+
"exchange_id": traderConfig.ExchangeID,
811+
"initial_balance": traderConfig.InitialBalance,
812+
"btc_eth_leverage": traderConfig.BTCETHLeverage,
813+
"altcoin_leverage": traderConfig.AltcoinLeverage,
814+
"trading_symbols": traderConfig.TradingSymbols,
815+
"custom_prompt": traderConfig.CustomPrompt,
816+
"override_base_prompt": traderConfig.OverrideBasePrompt,
817+
"system_prompt_template": traderConfig.SystemPromptTemplate, // 添加此字段
818+
"is_cross_margin": traderConfig.IsCrossMargin,
819+
"use_coin_pool": traderConfig.UseCoinPool,
820+
"use_oi_top": traderConfig.UseOITop,
821+
"is_running": isRunning,
828822
}
829823

830824
c.JSON(http.StatusOK, result)

web/src/components/AITradersPage.tsx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,34 +165,60 @@ export function AITradersPage({ onTraderSelect }: AITradersPageProps) {
165165
if (!editingTrader) return;
166166

167167
try {
168-
const model = enabledModels?.find(m => m.id === data.ai_model_id);
169-
const exchange = enabledExchanges?.find(e => e.id === data.exchange_id);
168+
// 1. 确保 ai_model_id 有值(使用 fallback)
169+
const ai_model_id = data.ai_model_id || editingTrader.ai_model;
170+
if (!ai_model_id) {
171+
console.error('缺少 AI 模型配置', { data, editingTrader });
172+
alert('错误:缺少 AI 模型配置');
173+
return;
174+
}
175+
176+
// 2. 确保 exchange_id 有值(使用 fallback)
177+
const exchange_id = data.exchange_id || editingTrader.exchange_id;
178+
if (!exchange_id) {
179+
console.error('缺少交易所配置', { data, editingTrader });
180+
alert('错误:缺少交易所配置');
181+
return;
182+
}
170183

184+
// 3. 验证模型存在
185+
console.log('验证 AI 模型:', { ai_model_id, enabledModels: enabledModels?.map(m => m.id) });
186+
const model = enabledModels?.find(m => m.id === ai_model_id);
171187
if (!model) {
172-
alert(t('modelConfigNotExist', language));
188+
console.error('找不到模型:', {
189+
ai_model_id,
190+
availableModels: enabledModels?.map(m => ({ id: m.id, name: m.name }))
191+
});
192+
alert(`AI模型配置不存在或未启用: ${ai_model_id}\n\n可用的模型: ${enabledModels?.map(m => m.name).join(', ')}`);
173193
return;
174194
}
175195

196+
// 4. 验证交易所存在
197+
const exchange = enabledExchanges?.find(e => e.id === exchange_id);
176198
if (!exchange) {
199+
console.error('找不到交易所:', { exchange_id });
177200
alert(t('exchangeConfigNotExist', language));
178201
return;
179202
}
180-
203+
204+
// 5. 构建请求(使用修正后的值)
181205
const request = {
182206
name: data.name,
183-
ai_model_id: data.ai_model_id,
184-
exchange_id: data.exchange_id,
207+
ai_model_id: ai_model_id, // 使用修正后的值
208+
exchange_id: exchange_id, // 使用修正后的值
185209
initial_balance: data.initial_balance,
186210
btc_eth_leverage: data.btc_eth_leverage,
187211
altcoin_leverage: data.altcoin_leverage,
188212
trading_symbols: data.trading_symbols,
189213
custom_prompt: data.custom_prompt,
190214
override_base_prompt: data.override_base_prompt,
215+
system_prompt_template: data.system_prompt_template, // 添加此字段
191216
is_cross_margin: data.is_cross_margin,
192217
use_coin_pool: data.use_coin_pool,
193218
use_oi_top: data.use_oi_top
194219
};
195-
220+
221+
console.log('保存交易员配置:', request);
196222
await api.updateTrader(editingTrader.trader_id, request);
197223
setShowEditModal(false);
198224
setEditingTrader(null);

web/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export interface TraderConfigData {
194194
trading_symbols: string;
195195
custom_prompt: string;
196196
override_base_prompt: boolean;
197+
system_prompt_template: string; // 添加系统提示词模板字段
197198
is_cross_margin: boolean;
198199
use_coin_pool: boolean;
199200
use_oi_top: boolean;

0 commit comments

Comments
 (0)