fix: inherit current session model for new sessions instead of picking arbitrary

This commit is contained in:
Aisland
2026-08-21 22:52:21 +08:00
parent 819a3c6d1e
commit e8f1c80708
5 changed files with 162 additions and 22 deletions
+76 -8
View File
@@ -340,9 +340,32 @@ async function ensureSessionPreset(sessionId: string): Promise<void> {
} catch { /* 修复失败不阻断主流程,错误仍按原路径提示 */ }
}
/** 按「用户既有选择」在可用目录里找最贴近的模型(不擅自选):
* 1) id 完全一致;2) id/名称含基准模型的家族词(pro/max/mini/flash…);3) 目录首个。 */
function pickInheritedModel(groups: any[], baseModel: string | undefined): { provider: string; model: string } | null {
if (!Array.isArray(groups) || groups.length === 0) return null
const flat: { provider: string; model: string; name: string }[] = []
for (const g of groups) {
if (!g?.id || !Array.isArray(g.models)) continue
for (const m of g.models) if (m?.id) flat.push({ provider: g.id, model: m.id, name: String(m.name ?? '') })
}
if (flat.length === 0) return null
const base = String(baseModel ?? '').toLowerCase()
if (base) {
const exact = flat.find((f) => f.model.toLowerCase() === base)
if (exact) return { provider: exact.provider, model: exact.model }
const tokens = base.split(/[-_.]/).filter((t) => t && (/^(pro|max|mini|flash|lite|ultra|plus|sonnet|opus|haiku|turbo)$/i.test(t) || t.length >= 4))
for (const tok of tokens) {
const hit = flat.find((f) => f.model.toLowerCase().includes(tok) || f.name.toLowerCase().includes(tok))
if (hit) return { provider: hit.provider, model: hit.model }
}
}
return { provider: flat[0].provider, model: flat[0].model }
}
/** 新会话模型选择修复(真正的根因修复):当前选择指向已删 provider(routable=false)时,
* 用 session.selectModel 切到模型目录里第一个可用 provider 的首个模型——
* 该 API 同时把新选择存为默认,顺带修复后续所有新会话。 */
* 不擅自选模型——① 优先继承用户当前会话正在用的模型;② 兜底按失效选择里的模型家族词
* 找同款;③ 最后才目录首个。session.selectModel 同时把新选择存为默认,顺带修复后续新会话。 */
async function ensureSessionModel(sessionId: string): Promise<void> {
const api = hostApi?.sessions
if (!api || typeof api.models !== 'function' || typeof api.selectModel !== 'function') return
@@ -350,13 +373,58 @@ async function ensureSessionModel(sessionId: string): Promise<void> {
const mRes = await api.models({ sessionId })
if (!mRes?.result?.ok) return
const val = mRes.result.value
if (val?.routable === true) return
const groups = val?.groups
if (!Array.isArray(groups) || groups.length === 0) return
const g = groups[0]
const model = g?.models?.[0]?.id
if (!g?.id || !model) return
await api.selectModel({ sessionId, provider: g.id, model })
let target: { provider: string; model: string } | null = null
let effort: string | undefined
// ① 无条件继承用户当前会话的选择(用户正在用哪个模型,新会话就用哪个)
try {
const cur = sessionBridge?.list?.getSnapshot?.()?.current
if (cur && cur !== sessionId) {
const cRes = await api.models({ sessionId: cur })
if (cRes?.result?.ok) {
const cval = cRes.result.value
if (cval?.routable === true && cval?.current?.provider && cval?.current?.model) {
const same = val?.current?.provider === cval.current.provider && val?.current?.model === cval.current.model
if (!same) {
target = { provider: cval.current.provider, model: cval.current.model }
effort = typeof cval.current.reasoningEffort === 'string' ? cval.current.reasoningEffort : undefined
}
}
}
}
} catch {}
// ② 无当前会话且新会话选择不可用 → 最近会话众数(用户习惯)
if (!target && val?.routable !== true) {
try {
const snap = sessionBridge?.list?.getSnapshot?.()
const byId: Record<string, any> = snap?.byId ?? {}
const ids: string[] = (snap?.ids ?? []).filter((x: string) => x !== sessionId)
const scored: { key: string; provider: string; model: string; effort?: string; count: number }[] = []
await Promise.all(ids.slice(0, 6).map(async (sid) => {
const row = byId[sid]
if (row?.blank === true) return
try {
const r2 = await api.models({ sessionId: sid })
if (!r2?.result?.ok) return
const cv = r2.result.value
if (cv?.routable !== true || !cv?.current?.provider || !cv?.current?.model) return
const key = cv.current.provider + '|' + cv.current.model
const found = scored.find((s) => s.key === key)
if (found) found.count++
else scored.push({ key, provider: cv.current.provider, model: cv.current.model, effort: typeof cv.current.reasoningEffort === 'string' ? cv.current.reasoningEffort : undefined, count: 1 })
} catch {}
}))
scored.sort((a, b) => b.count - a.count)
if (scored[0]) {
target = { provider: scored[0].provider, model: scored[0].model }
effort = scored[0].effort
}
} catch {}
}
// ③/④ 兜底(仅当新会话选择不可用):家族词 → 目录首个
if (!target && val?.routable !== true) target = pickInheritedModel(groups, val?.current?.model)
if (!target) return
await api.selectModel({ sessionId, provider: target.provider, model: target.model, ...(effort !== undefined ? { reasoningEffort: effort } : {}) })
} catch { /* 修复失败不阻断主流程,错误仍按原路径提示 */ }
}
+43 -11
View File
@@ -27,7 +27,7 @@ export type BuiltinType = 'browser' | 'anim' | 'explorer' | 'scm' | 'tasks' | 't
export type SplitContent =
| { kind: 'iframe'; url: string; title?: string }
| { kind: 'builtin'; type: BuiltinType }
| { kind: 'builtin'; type: BuiltinType; url?: string }
| { kind: 'file'; path: string }
/** 一个内容标签页 */
@@ -650,6 +650,27 @@ export const splitStore: SplitState = {
this.notify()
},
/** 更新指定标签页的内容(浏览器/动画窗地址栏回车时回写),并持久化到布局条目 */
setTabContent(row, index, tabId, content) {
const spec = this.spec
if (!spec) return
const mutate = (pane: SplitPane): SplitPane => {
const tabs = (pane.tabs ?? []).map((t) => (t.id === tabId ? { ...t, content, title: tabTitleOf(content) } : t))
return { ...pane, tabs }
}
if (row === 'left') {
if (spec.left && index === 0) this.spec = { ...spec, left: mutate(spec.left) }
} else if (row === 'top') {
const top = [...(spec.top ?? [])]
if (top[index]) { top[index] = mutate(top[index]); this.spec = { ...spec, top } }
} else {
const main = [...spec.main]
if (main[index]) { main[index] = mutate(main[index]); this.spec = { ...spec, main } }
}
this.onSpecMutated?.(this.spec)
this.notify()
},
openTab(row, i, content) {
const spec = this.spec
if (!spec) return
@@ -903,12 +924,18 @@ function makeDividerHandler(kind: 'left' | 'chat' | 'top' | 'pane' | 'topPane',
}
/** 浏览器内置窗:地址栏 + 前往;刷新统一在标签栏最左(重挂载 iframe,跨域也可靠) */
function BrowserPane(props: { reloadKey: number }) {
const [url, setUrl] = useState('https://example.com')
const [src, setSrc] = useState('https://example.com')
function BrowserPane(props: { row: PaneRow; index: number; tabId: string; content: SplitContent; reloadKey: number }) {
const initial = props.content?.url || 'https://example.com'
const [url, setUrl] = useState(initial)
const [src, setSrc] = useState(initial)
const go = () => {
const u = url.trim()
setSrc(/^(\/|https?:\/\/)/i.test(u) ? u : 'about:blank')
const ok = /^(\/|https?:\/\/)/i.test(u) ? u : 'about:blank'
setSrc(ok)
if (ok !== 'about:blank') {
// 地址回写:刷新/重开布局时保持当前网址
splitStore.setTabContent(props.row, props.index, props.tabId, { kind: 'builtin', type: 'browser', url: ok })
}
}
return (
<>
@@ -933,12 +960,17 @@ function IframePane(props: { url: string; title?: string; reloadKey: number }) {
}
/** 动画播放窗:iframe 壳 + 地址栏(站内自带项目/场景列表、播放、画幅切换、导出等全部控件) */
function AnimPane(props: { reloadKey: number }) {
const [url, setUrl] = useState('')
const [src, setSrc] = useState('about:blank')
function AnimPane(props: { row: PaneRow; index: number; tabId: string; content: SplitContent; reloadKey: number }) {
const initial = props.content?.url || ''
const [url, setUrl] = useState(initial)
const [src, setSrc] = useState(initial || 'about:blank')
const go = () => {
const u = url.trim()
setSrc(/^(\/|https?:\/\/)/i.test(u) ? u : 'about:blank')
const ok = /^(\/|https?:\/\/)/i.test(u) ? u : 'about:blank'
setSrc(ok)
if (ok !== 'about:blank') {
splitStore.setTabContent(props.row, props.index, props.tabId, { kind: 'builtin', type: 'anim', url: ok })
}
}
return (
<>
@@ -1704,8 +1736,8 @@ function PaneTabBody(props: { tab: PaneTab; row: PaneRow; index: number; paneTit
if (content.kind === 'file') {
return <FileViewer path={content.path} />
}
if (content.type === 'browser') return <BrowserPane reloadKey={props.reloadKey} />
if (content.type === 'anim') return <AnimPane reloadKey={props.reloadKey} />
if (content.type === 'browser') return <BrowserPane row={props.row} index={props.index} tabId={props.tab.id} content={content} reloadKey={props.reloadKey} />
if (content.type === 'anim') return <AnimPane row={props.row} index={props.index} tabId={props.tab.id} content={content} reloadKey={props.reloadKey} />
if (content.type === 'console') return <ConsolePane />
if (content.type === 'explorer') return <ExplorerPane row={props.row} index={props.index} />
if (content.type === 'scm') return <GitPane />