fix(sidechat): 在 setup 里装回 installModelSelection,切模型后新模型收到 [model changed]
0.2.2 把路由挪到 AgentModel 后,0.30.0 适配把 installModelSelection 一并删了;但该函数仍注册 agent/pre-step 监听来追加 [model changed] 提示,宿主 composeAgent 在两条分支都先装它、再 presets.mount。恢复为共享 threadSetup:5 个 setup 分支先 install 再 mount,并补 create / cold-resume 的监听回归测试。
This commit is contained in:
1 parent
11e6c8e63a
commit
fe291fa0af
2 files changed
+55
-7
No files matched your search
+25
-7
@@ -27,6 +27,7 @@ import { randomUUID } from 'node:crypto'
|
||||
import { createUserMessage, ReasoningEffortId, type ContentBlock, type UserMessage } from '@deepseek-ai/dsh-llm'
|
||||
import type { MessageId } from '@deepseek-ai/dsh-llm/brand'
|
||||
import {
|
||||
installModelSelection,
|
||||
type Agent,
|
||||
type AgentSetup,
|
||||
type CreateAgentOptions,
|
||||
@@ -192,21 +193,38 @@ interface SidebarLlmService {
|
||||
}): Promise<unknown>
|
||||
}
|
||||
|
||||
/** Build the setup every side thread shares: mount the preset (when one
|
||||
* resolved) and install the host's model-selection pre-step listener.
|
||||
*
|
||||
* 0.2.2 moved ROUTING onto the agent's own `AgentModel`, but the
|
||||
* `[model changed: …]` notice is still appended by the listener
|
||||
* `installModelSelection` registers, and the host installs it in every one of
|
||||
* its own setups (`api-session-controller`'s composeAgent, `dsh-acp`,
|
||||
* `dsh-headless`). The 0.2.2 adaptation dropped it here (#1134), so the next
|
||||
* model kept answering as if the turns above had used its own route. Install
|
||||
* once per agent, before `presets.mount` — the host's order. */
|
||||
function threadSetup(mount?: (agentCtx: CordisContext) => Promise<void>): AgentSetup {
|
||||
return async (agentCtx: CordisContext) => {
|
||||
installModelSelection(agentCtx)
|
||||
if (mount !== undefined) await mount(agentCtx)
|
||||
}
|
||||
}
|
||||
|
||||
/** Resolve the parent's preset and build the child's composition setup
|
||||
* (mirror of api-proxy's composeAgent minus the model-selection install —
|
||||
* the child carries the parent's provider/model in agentOptions). */
|
||||
* (mirror of api-proxy's composeAgent: preset mount + model-selection install,
|
||||
* while the child carries the parent's provider/model in agentOptions). */
|
||||
async function composeChildSetup(
|
||||
ctx: Context,
|
||||
presetId: string | undefined,
|
||||
): Promise<{ agentPreset?: string; setup: AgentSetup }> {
|
||||
const presets = ctx.get('agentPresets') as SidebarAgentPresetsService | undefined
|
||||
if (presets === undefined) {
|
||||
return { setup: () => Promise.resolve() }
|
||||
return { setup: threadSetup() }
|
||||
}
|
||||
const resolved = await presets.resolve(presetId)
|
||||
return {
|
||||
agentPreset: resolved.id,
|
||||
setup: async (agentCtx: CordisContext) => { await presets.mount(agentCtx, resolved.id) },
|
||||
setup: threadSetup(async (agentCtx) => { await presets.mount(agentCtx, resolved.id) }),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,16 +236,16 @@ async function composePersistedSetup(
|
||||
): Promise<AgentSetup> {
|
||||
const persistence = ctx.get('sessionPersistence') as SidebarSessionPersistenceService | undefined
|
||||
if (persistence === undefined) {
|
||||
return () => Promise.resolve()
|
||||
return threadSetup()
|
||||
}
|
||||
const inspected = await readColdThreadSession(persistence, childId)
|
||||
const presetId = resolvePresetId(inspected.header, inspected.events)
|
||||
const presets = ctx.get('agentPresets') as SidebarAgentPresetsService | undefined
|
||||
if (presets === undefined || presetId === undefined) {
|
||||
return () => Promise.resolve()
|
||||
return threadSetup()
|
||||
}
|
||||
const resolved = await presets.resolve(presetId)
|
||||
return async (agentCtx: CordisContext) => { await presets.mount(agentCtx, resolved.id) }
|
||||
return threadSetup(async (agentCtx) => { await presets.mount(agentCtx, resolved.id) })
|
||||
}
|
||||
|
||||
/** One text-block prompt (the thread boundary + question, or a follow-up). */
|
||||
|
||||
@@ -152,6 +152,36 @@ describe('sidechat.start', () => {
|
||||
expect(get).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('installs the model-switch listener in the create and cold-resume setups (#1134)', async () => {
|
||||
// 0.2.2 kept the `[model changed: …]` notice in `installModelSelection`'s
|
||||
// `agent/pre-step` listener even though routing moved onto `AgentModel`.
|
||||
// The host installs it in every setup; a side thread that skips it leaves
|
||||
// the next model unaware that the turns above used another route.
|
||||
const services = happyServices(agent('parent'), agent('child'))
|
||||
const api = buildSidechatApi(ctxWith(services))
|
||||
await api['sidechat.start']({ sessionId: 'parent', question: 'q' })
|
||||
const createSetup = (services.create.mock.calls[0] as unknown as [
|
||||
{ setup: (ctx: unknown) => Promise<void> },
|
||||
])[0].setup
|
||||
const on = vi.fn((_event: string) => () => {})
|
||||
await createSetup({ on })
|
||||
expect(on.mock.calls.filter(([type]) => type === 'agent/pre-step')).toHaveLength(1)
|
||||
|
||||
// Cold resume builds its own setup from the persisted record; that branch
|
||||
// must install the listener too.
|
||||
const child = agent('child-resume')
|
||||
const resumed = happyServices(undefined, child)
|
||||
resumed.agents.get = vi.fn((_id: unknown) => undefined)
|
||||
const resumeApi = buildSidechatApi(ctxWith(resumed))
|
||||
await resumeApi['sidechat.model']({ childId: 'child-resume', provider: 'p3', model: 'm3' })
|
||||
const resumeSetup = (resumed.resume.mock.calls[0] as unknown as [
|
||||
{ setup: (ctx: unknown) => Promise<void> },
|
||||
])[0].setup
|
||||
const onResume = vi.fn((_event: string) => () => {})
|
||||
await resumeSetup({ on: onResume })
|
||||
expect(onResume.mock.calls.filter(([type]) => type === 'agent/pre-step')).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('stamps current cwd after a truncated seed so the real working-directory projection cannot restore an older cwd', async () => {
|
||||
const original = resolve('/original-project')
|
||||
const previous = resolve('/previous-execution')
|
||||
|
||||
Reference in new issue
Block a user