/** * /config 关闭后让正在运行的会话跟上配置(纯逻辑,便于测试)。 * * 状态栏的模型名、上下文大小、压缩阈值都读 useChat 的 providerRef,而它只在启动和 * /model、/effort 时更新——在 /config 里改了上下文窗口,运行中的会话仍用旧值。 * 流程:打开 /config 时 snapshotForConfig 记下"会话跑在哪条模型配置上",关闭时 * modelIdToReload 决定重新解析哪条,carrySessionEffort 保住 /effort 的会话级覆盖。 */ import type { MinimalAgentConfig } from '../config/configFile.js'; import type { ReasoningEffort } from '../config/reasoningEffort.js'; import type { Provider } from '../types.js'; export interface ConfigOpenSnapshot { /** 会话当前所用的本机模型配置 id;在线模型或匹配不上时为 null。 */ modelId: string | null; baseModelId: string | null; /** 该配置条目里写的推理强度(用来区分「会话级 /effort 覆盖」与「配置值」)。 */ configEffort?: ReasoningEffort; } export function snapshotForConfig( current: Provider, config: MinimalAgentConfig | null, ): ConfigOpenSnapshot { const entry = current.source === 'gateway' ? undefined : config?.models.find((m) => m.baseURL === current.baseURL && m.model === current.model); return { modelId: entry?.id ?? null, baseModelId: config?.baseModelId ?? null, ...(entry?.reasoningEffort ? { configEffort: entry.reasoningEffort } : {}), }; } /** 关闭 /config 后应重新解析的模型 id;null = 不动当前 provider。 */ export function modelIdToReload( open: ConfigOpenSnapshot, config: MinimalAgentConfig | null, ): string | null { if (!config) return null; // 用户在 /config 里换了基座:会话跟随(这是他刚做的明确选择)。 if (config.baseModelId !== open.baseModelId) return config.baseModelId ?? null; if (!open.modelId) return null; if (config.models.some((m) => m.id === open.modelId)) return open.modelId; return config.baseModelId ?? null; // 会话用的模型被删了 → 退回基座 } /** 配置里的推理强度没动过时,保留会话里 /effort 设的值;改了就以配置为准。 */ export function carrySessionEffort( current: Provider, open: ConfigOpenSnapshot, next: Provider, ): Provider { const configUnchanged = open.configEffort === next.reasoningEffort; const sessionDiffers = current.reasoningEffort !== open.configEffort; if (!configUnchanged || !sessionDiffers) return next; const { reasoningEffort: _dropped, ...rest } = next; return (current.reasoningEffort ? { ...rest, reasoningEffort: current.reasoningEffort } : rest) as Provider; }