feat(web): seat the plan control on conversation.input.plan over the projection

Rewrite ui-plan as a pure browser surface plugin. The control occupies the
composer's named plan seat (declared empty by ui-conversation); reads render
the host-computed plan projection through the standard-kit useProjection
(absent key = capability absence, hides the control), writes execute /plan
or /plan off through command.execute. The node half becomes the empty
roster apply: plan behavior (command, policy, projection unit) is owned by
dsh-plan-mode, already composed on the web roster with its policy in
cordis.yml. The superseded RPC-backed setPlanMode face, the WEB_PLAN_SECTION
duplicate, and the node-plugin spec are removed; the roster row moves from
the retired CLIENT_PACKAGES table to the cordis.yml dshClient roster.
This commit is contained in:
imccyu
2026-07-28 20:57:46 +08:00
parent f0f1966c97
commit 8dab7d2f92
12 changed files with 166 additions and 207 deletions

View File

@@ -1,3 +1,10 @@
/**
* ui-plan browser half on a real SlotsService: the plugin occupies the
* conversation-declared `conversation.input.plan` single seat; the injected
* face maps mode selections onto /plan command lines and folds admission
* outcomes into null (admitted) or a user-visible failure line; teardown
* empties the seat (HMR safety).
*/
import { Context } from 'cordis'
import { describe, expect, it, vi } from 'vitest'
import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
@@ -14,45 +21,55 @@ async function bench() {
const slots = ctx.get('slots') as SlotsService
slots.register({
name: 'root',
children: { 'conversation.composer.controls': { kind: 'list', scope: 'session' } },
children: { 'conversation.input.plan': { kind: 'single', scope: 'session' } },
} as never, () => null)
const setPlanMode = vi.fn(() => Promise.resolve({ ok: true, value: { active: false, pending: true } }))
ctx.provide('sessions', { manager: { get: () => ({ setPlanMode }) } })
const execute = vi.fn((_payload: { sessionId: SessionId; line: string }) =>
Promise.resolve({ result: { ok: true as const, value: { matched: true as const, commandId: 'c1' } } }))
ctx.provide('connection', { api: { commands: { execute } } })
ctx.provide('conversation', {})
return { ctx, slots, setPlanMode }
return { ctx, slots, execute }
}
describe('ui-plan browser apply', () => {
it('declares every service it binds', () => {
expect(inject).toEqual(['slots', 'sessions', 'conversation'])
expect(inject).toEqual(['slots', 'connection', 'conversation'])
})
it('fails loud when conversation did not declare the controls slot', async () => {
it('fails loud when conversation did not declare the plan seat', async () => {
const ctx = new Context()
await ctx.plugin(SlotsService).await()
ctx.provide('sessions', {})
ctx.provide('connection', {})
ctx.provide('conversation', {})
await expect(ctx.plugin({ inject: [...inject], apply }))
.rejects.toThrow(/slot "conversation.composer.controls" is not declared/)
.rejects.toThrow(/slot "conversation.input.plan" is not declared/)
})
it('registers the control, bridges host results, and unregisters on teardown', async () => {
it('registers the control, maps selections to /plan lines, and unregisters on teardown', async () => {
const b = await bench()
const fiber = b.ctx.plugin({ inject: [...inject], apply })
await fiber.await()
const entry = b.slots.entries('conversation.composer.controls')[0]!
const entry = b.slots.entries('conversation.input.plan')[0]!
expect(entry.component).toBe(PlanModeControl)
expect(entry.options).toMatchObject({ id: 'plan-mode', order: 10 })
const injected = (entry.inject as unknown as (id: SessionId) => PlanModeControlInjected)(SID)
await expect(injected.setPlanMode(true)).resolves.toBeNull()
expect(b.setPlanMode).toHaveBeenCalledWith(true)
b.setPlanMode.mockResolvedValueOnce({
ok: false, error: { code: 'session-not-found', message: 'gone', details: {} },
await expect(injected.setPlanMode(true)).resolves.toBeNull()
expect(b.execute).toHaveBeenLastCalledWith({ sessionId: SID, line: '/plan' })
await expect(injected.setPlanMode(false)).resolves.toBeNull()
expect(b.execute).toHaveBeenLastCalledWith({ sessionId: SID, line: '/plan off' })
// Business failure folds to the composer-visible line.
b.execute.mockResolvedValueOnce({
result: { ok: false as const, error: { code: 'session-not-found', message: 'gone', details: {} } },
} as never)
await expect(injected.setPlanMode(false)).resolves.toBe('gone(session-not-found)')
await expect(injected.setPlanMode(true)).resolves.toBe('gone(session-not-found)')
// Unmatched admission (plan-mode not composed host-side) is also a failure line.
b.execute.mockResolvedValueOnce({
result: { ok: true as const, value: { matched: false as const } },
} as never)
await expect(injected.setPlanMode(true)).resolves.toBe('未知命令:/plan')
await fiber.dispose()
expect(b.slots.entries('conversation.composer.controls')).toHaveLength(0)
expect(b.slots.entries('conversation.input.plan')).toHaveLength(0)
})
})

View File

@@ -1,39 +0,0 @@
import { Context } from 'cordis'
import { afterEach, describe, expect, it } from 'vitest'
import ToolRegistry from '@deepseek-ai/dsh-tools'
import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
import UserInteractionService from '@deepseek-ai/dsh-user-interaction'
import { EXIT_PLAN_MODE } from '@deepseek-ai/dsh-plan-mode'
import { WEB_PLAN_SECTION, apply, inject } from '../src/index.ts'
let ctx: Context | undefined
afterEach(async () => {
await ctx?.fiber.dispose()
ctx = undefined
})
describe('ui-plan node plugin', () => {
it('mounts the Web policy and stable exit tool for the selected feature lifecycle', async () => {
ctx = new Context()
await ctx.plugin(SystemPrompt)
await ctx.plugin(ToolRegistry)
await ctx.plugin(UserInteractionService)
const feature = ctx.plugin({ inject: [...inject], apply })
await feature.await()
expect(ctx.get('planMode')).toBeDefined()
expect(ctx.tools.get(EXIT_PLAN_MODE)).toBeDefined()
expect(WEB_PLAN_SECTION).toContain('Stay in plan mode until exit_plan_mode succeeds')
expect(WEB_PLAN_SECTION).toContain('Do not edit or write files')
expect(WEB_PLAN_SECTION).toContain('Make exit_plan_mode the only and final tool call')
expect((await ctx.systemPrompt.assemble()).sections)
.toEqual(expect.arrayContaining([expect.objectContaining({ name: 'plan:policy', text: '' })]))
await feature.dispose()
expect(ctx.get('planMode')).toBeUndefined()
expect(ctx.tools.get(EXIT_PLAN_MODE)).toBeUndefined()
expect((await ctx.systemPrompt.assemble()).sections.map(section => section.name))
.not.toContain('plan:policy')
})
})

View File

@@ -1,44 +1,38 @@
// @vitest-environment jsdom
/**
* PlanModeControl over the `plan` projection: an absent key (capability
* absence) hides the control; {active, pending} renders committed and
* pending-target labels; selection maps from the effective target and
* surfaces failures without mutating the host-confirmed state.
*/
import { afterEach, describe, expect, it, vi } from 'vitest'
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import type {
ConversationSnapshot, SessionId, SessionListState,
} from '@deepseek-ai/dsh-client-runtime/client'
import { createSnapshotStore } from '@deepseek-ai/dsh-client-runtime/client'
import { bindSnapshotSelector } from '@deepseek-ai/dsh-client-web-react'
import type { SnapshotSelectorHook } from '@deepseek-ai/dsh-client-ui-slots'
import type { PlanModeState } from '@deepseek-ai/dsh-client-connection/client'
import { PlanModeControl } from '../src/client/PlanModeControl.tsx'
import type { PlanProjection } from '@deepseek-ai/dsh-plan-mode/client'
import { PlanModeControl, type PlanModeControlProps } from '../src/client/PlanModeControl.tsx'
afterEach(cleanup)
const SID = 's-plan' as SessionId
function setup(
planMode: PlanModeState | null,
plan: PlanProjection | undefined,
setPlanMode = vi.fn(() => Promise.resolve<string | null>(null)),
running = false,
locked = false,
) {
const store = createSnapshotStore({ planMode, running })
const useSession = bindSnapshotSelector(store) as unknown as SnapshotSelectorHook<ConversationSnapshot>
const useSessions = (() => { throw new Error('unused') }) as unknown as SnapshotSelectorHook<SessionListState>
const view = render(
<PlanModeControl
sessionId={SID}
useSession={useSession}
useSessions={useSessions}
setPlanMode={setPlanMode}
/>,
)
const store = createSnapshotStore<{ value: PlanProjection | undefined }>({ value: plan })
const useProjection = (_key: string, selector?: (v: unknown) => unknown) =>
bindSnapshotSelector(store)(s => (selector ?? (v => v))(s.value))
const props = { useProjection, locked, setPlanMode } as unknown as PlanModeControlProps
const view = render(<PlanModeControl {...props} />)
return { store, setPlanMode, view }
}
describe('PlanModeControl', () => {
it('hides an unavailable capability and renders committed modes', () => {
const unavailable = setup(null)
expect(unavailable.view.container.innerHTML).toBe('')
it('hides an absent capability and renders committed modes', () => {
const absent = setup(undefined)
expect(absent.view.container.innerHTML).toBe('')
cleanup()
setup({ active: false })
setup({ active: false, pending: false })
expect(screen.getByTitle('当前为默认模式')).toBeTruthy()
const select = screen.getByRole('combobox', { name: '协作模式' }) as HTMLSelectElement
expect(select.value).toBe('default')
@@ -46,15 +40,14 @@ describe('PlanModeControl', () => {
.toBe('当前为默认模式')
})
it('treats pending field presence as the target, including pending false', () => {
it('renders the pending target as the opposite of the committed state', () => {
setup({ active: false, pending: true })
expect(screen.getByText('计划 · 待生效')).toBeTruthy()
expect(screen.getByTitle(/当前为默认模式/)).toBeTruthy()
const planSelect = screen.getByRole('combobox')
expect(document.getElementById(planSelect.getAttribute('aria-describedby') ?? '')?.textContent)
.toBe('当前为默认模式;计划模式将在下一次模型请求时生效')
cleanup()
setup({ active: true, pending: false })
setup({ active: true, pending: true })
expect(screen.getByText('默认 · 待生效')).toBeTruthy()
const defaultSelect = screen.getByRole('combobox') as HTMLSelectElement
expect(defaultSelect.value).toBe('default')
@@ -62,32 +55,39 @@ describe('PlanModeControl', () => {
.toBe('当前为计划模式;默认模式将在下一次模型请求时生效')
})
it('switches from the effective target and remains available while a turn runs', async () => {
it('switches from the effective target, disables during its own call, and follows the pushed projection', async () => {
let resolve!: (value: string | null) => void
const setPlanMode = vi.fn(() => new Promise<string | null>((done) => { resolve = done }))
const { store } = setup({ active: false }, setPlanMode, true)
const { store } = setup({ active: false, pending: false }, setPlanMode)
const select = screen.getByRole('combobox', { name: '协作模式' }) as HTMLSelectElement
expect(select.disabled).toBe(false)
fireEvent.change(select, { target: { value: 'plan' } })
expect(setPlanMode).toHaveBeenCalledWith(true)
expect(select.disabled).toBe(true)
store.set({ planMode: { active: false, pending: true }, running: true })
// The projection frame lands (command/run folded host-side).
store.set({ value: { active: false, pending: true } })
resolve(null)
await waitFor(() => {
expect((screen.getByRole('combobox') as HTMLSelectElement).disabled).toBe(false)
})
expect(screen.getByText('计划 · 待生效')).toBeTruthy()
// Re-selecting the effective target is a no-op.
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'plan' } })
expect(setPlanMode).toHaveBeenCalledTimes(1)
})
it('surfaces host and transport failures without changing the confirmed mode', async () => {
it('disables under the locked owner prop', () => {
setup({ active: false, pending: false }, vi.fn(), true)
expect((screen.getByRole('combobox') as HTMLSelectElement).disabled).toBe(true)
})
it('surfaces admission and transport failures without changing the confirmed mode', async () => {
const setPlanMode = vi.fn()
.mockResolvedValueOnce('host said no')
.mockRejectedValueOnce(new Error('network down'))
.mockRejectedValueOnce('socket closed')
setup({ active: false }, setPlanMode)
setup({ active: false, pending: false }, setPlanMode)
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'plan' } })
expect((await screen.findByText('模式切换失败')).getAttribute('title')).toBe('host said no')
expect(screen.getByTitle('当前为默认模式')).toBeTruthy()
@@ -103,7 +103,7 @@ describe('PlanModeControl', () => {
it('ignores in-flight fulfillment and rejection after unmount', () => {
let resolve!: (value: string | null) => void
const successful = setup(
{ active: false },
{ active: false, pending: false },
vi.fn(() => new Promise<string | null>((done) => { resolve = done })),
)
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'plan' } })
@@ -112,7 +112,7 @@ describe('PlanModeControl', () => {
let reject!: (reason: unknown) => void
const setPlanMode = vi.fn(() => new Promise<string | null>((_done, fail) => { reject = fail }))
const { view } = setup({ active: false }, setPlanMode)
const { view } = setup({ active: false, pending: false }, setPlanMode)
fireEvent.change(screen.getByRole('combobox'), { target: { value: 'plan' } })
view.unmount()
expect(() => { reject(new Error('late')) }).not.toThrow()