fix: 切换 Provider 后不生效 + Ollama 模式 API Key 输入框仍显示

1. reloadAdapter 加 try-catch: 配置中间状态 createAdapter 抛异常时不阻断 IPC, 记录日志即可

2. sendMessage 时兜底调用 reloadAdapter: 确保每次发消息用最新 adapter, 不依赖 config:set 的逐字段 reload

3. Ollama 模式隐藏 API Key 输入框, 切换到 Ollama 时自动清空旧 key

4. 切换 Provider 时自动填充默认 URL(仅旧 URL 为默认值或空时)
This commit is contained in:
thzxx
2026-07-09 20:35:13 +08:00
parent dc5f8aedcd
commit 153468f8f9
3 changed files with 38 additions and 13 deletions
+3
View File
@@ -52,6 +52,9 @@ export function registerAllIPCHandlers(
ipcMain.handle('agent:sendMessage', async (_event, userMessage: MetonaMessage, sessionId: string) => {
log.info('[AGENT] sendMessage:', sessionId, userMessage.content.slice(0, 80));
// 发送消息前确保 Adapter 使用最新配置(防止 config:set 中间状态导致 reload 失败)
reloadAdapter();
// TRACE 层:开始录制
sessionRecorder.startRecording(sessionId);
+14 -9
View File
@@ -230,15 +230,20 @@ async function initialize(): Promise<void> {
// ===== 热重载 Adapter 回调(设置变更时触发)=====
const reloadAdapter = () => {
const newAdapter = createAdapter();
agentLoop.setAdapter(newAdapter);
// Provider 切换时同步 contextLength:仅 Ollama 使用 numCtx 作为有效上下文窗口
const provider = configService.get<string>('llm.provider') ?? '';
if (provider === 'ollama') {
const numCtx = configService.get<number>('ollama.numCtx');
agentLoop.updateConfig({ contextLength: numCtx ?? undefined });
} else {
agentLoop.updateConfig({ contextLength: undefined });
try {
const newAdapter = createAdapter();
agentLoop.setAdapter(newAdapter);
// Provider 切换时同步 contextLength:仅 Ollama 使用 numCtx 作为有效上下文窗口
const provider = configService.get<string>('llm.provider') ?? '';
if (provider === 'ollama') {
const numCtx = configService.get<number>('ollama.numCtx');
agentLoop.updateConfig({ contextLength: numCtx ?? undefined });
} else {
agentLoop.updateConfig({ contextLength: undefined });
}
log.info(`[CONFIG] Adapter reloaded: provider=${provider}`);
} catch (err) {
log.error(`[CONFIG] Failed to reload adapter: ${(err as Error).message}`);
}
};