fix(agent): persist provider selection across restarts

Move session metadata loading before provider resolution so that
the stored provider from a previous setProvider() call is used
in the fallback chain (options > session meta > credentials > default)
instead of always falling back to "kimi-coding".

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jiang Bohan 2026-02-09 15:41:56 +08:00
parent 5c7d913128
commit 15f6604a16

View file

@ -111,8 +111,15 @@ export class Agent {
this.reasoningMode = options.reasoningMode ?? "stream";
this.output = createAgentOutput({ stdout, stderr: this.stderr, reasoningMode: this.reasoningMode });
// Resolve provider and model from options > env vars > defaults
const defaultProvider = options.provider ?? credentialManager.getLlmProvider() ?? "kimi-coding";
// Load session metadata early so stored provider/model can inform defaults
this.sessionId = options.sessionId ?? uuidv7();
const storedMeta = (() => {
const tempSession = new SessionManager({ sessionId: this.sessionId });
return tempSession.getMeta();
})();
// Resolve provider and model from options > session meta > env vars > defaults
const defaultProvider = options.provider ?? storedMeta?.provider ?? credentialManager.getLlmProvider() ?? "kimi-coding";
if (options.authProfileId) {
const profileProvider = options.authProfileId.includes(":")
? options.authProfileId.split(":")[0]!
@ -194,16 +201,7 @@ export class Agent {
});
}
this.sessionId = options.sessionId ?? uuidv7();
// 解析 model用于获取 context window
const storedMeta = (() => {
// 临时创建 session 获取 meta避免循环依赖
const tempSession = new SessionManager({ sessionId: this.sessionId });
return tempSession.getMeta();
})();
const effectiveProvider = resolvedModel ? this.resolvedProvider : (options.provider ?? storedMeta?.provider);
const effectiveProvider = this.resolvedProvider;
const effectiveModel = resolvedModel ?? options.model ?? storedMeta?.model;
let model = resolveModel({ ...options, provider: effectiveProvider, model: effectiveModel });