refactor: apply repository naming contract

Apply the accepted pre-release package, service, type, directory, and role renames as one repository-wide change.
This commit is contained in:
Tianyi Cui
2026-08-13 00:36:22 +08:00
parent 101df7cf58
commit a2d0f7f411
3281 changed files with 21730 additions and 21592 deletions

View File

@@ -0,0 +1,6 @@
# Bilingual-pair consistency record (docs/i18n/README.md): the git blob hash of each
# side as of the last confirmed-consistent state. Both languages carry equal authority;
# after editing either side, bring the other along and re-record with:
# pnpm run verify-translation-pairing --write packages/client/ui-commands/README.md
README.md: 67110ffd8c1ad11e56ca9293a9064c66dd08c81d
README.zh.md: 40fe21850dd289d2a5c91bd88d4f22c087731b80

View File

@@ -0,0 +1,29 @@
# @deepseek-ai/dsh-client-ui-commands
English | [中文](README.zh.md)
Client command API (`ctx.commandUi`): the session-keyed command-directory cache, the `/` command source with `matchSpace`/`matchEnter` decision hooks, three-kind dispatch (`execute` / `popupSelect` / `leadingInput`), and popupSelect registration for business packages. The [web command Agent Note](../../../.agents/notes/implemented/architecture/2026-07-25-web-command-surfaces-and-assembly.zh.md) records the decision.
`src/client/contract.ts` is the fixed business contract: `CommandUiContract.register(name, spec)` and `decorate(name, spec)` are everything a business package consumes; `CommandUiSpec{options, onSelect}` keeps popup data self-contained — the shell component belongs to this package and business packages never see it. A contribution is a client-owned command (a host-name collision fails loud); a decoration adds a bare-invocation popup to an EXISTING host command. The host keeps its catalog row, argument claim (space / argued Enter), and lifecycle logging, and a decorated name with no host row in the session's directory never fires. Command kinds derive per dispatch, never per registration: a host descriptor with `input` is `leadingInput`, a registered `CommandUiSpec` is `popupSelect`, and everything else is `execute`.
`CommandDirectory` (`src/client/directory.ts`) is the one wire-derived cache, keyed by session. Ordinary sessions fetch through `command.list({sessionId})`, and the source's scope-birth `warm` hook prewarms the session's entry. Catalog-addressed continuable children resolve an empty command directory locally: `command.list` is Agent-bound, so prewarming it would activate a child merely to view persisted history. Entries are soft-invalidated by the forwarded `commands/change` owner event (old snapshots serve while the repull flies) and by forwarded `agent-preset/selected` for that one session (recomposing an agent registers nothing, so the registry-wide signal never fires for it), hard-invalidated by `connection/reset`, and epoch-guarded so a superseded pull can never overwrite a newer one. `matchSpace` answers synchronously from this cache only; `matchEnter` strong-waits it on the SubmitAttempt signal and rejects on warmup failure — a `/` line is never silently downgraded to a plain prompt.
After `command.execute` returns a matched command result, this browser emits local `command/executed(sessionId, name, result)`. Other clients receive the durable command nodes through the Host event stream but never this acknowledgment, so a browser-only side effect can select successful results from the client that submitted the command without treating Session replay as an action request. Listener failures are logged and contained one by one; they cannot change the already-admitted command result or prevent later listeners from running.
Menu queries fuzzy-match ordered, case-insensitive subsequences of command names. Prefixes rank first; separator boundaries, adjacent characters, and shorter gaps rank the remaining matches, with directory and contribution order breaking ties. This affects discovery only: space and Enter still require an exact command name. Rationale: [Web slash-command fuzzy discovery](../../../.agents/notes/implemented/feature/2026-08-04-web-slash-command-fuzzy-discovery.md).
`PopupSelectController` (`src/client/popup.ts`) is the headless shell state: `PopupSelectView` self-registers into `conversation.input.overlay` (the SlotMap key is ui-conversation's; this package pulls the declaration in with a type-only import — no runtime edge). The shell is a transient layer holding focus while open; token-segment consumption after onSelect runs both branches through `consumeTokenSegment` (menu-path span CAS, enter-path bare-token equality) against the draft face the wiring layer binds via `bindDraft`.
The `/client` entrypoint exports the plugin body (`apply`/`inject`), `CommandUiRuntime`, the directory and popup classes with their state types, and the fixed contract types; the shell component itself is internal to the overlay registration.
## Model Experience
Indirectly, through the host `command.execute` RPC this package's dispatch and `claim.submit` paths trigger: a matched command's handler mutates host domain state that other packages project into the next request (the `/plan` handler flips plan mode, whose owning package injects its `plan:policy` system-prompt section), while the command line itself, the detached result, and every menu/notice rendering stay client-side and never enter the session log.
#### KV Cache effect
None directly; this package neither assembles nor sends a provider request. Command handlers it triggers may change what the owning host packages contribute to the next request's system prompt (a section appearing or disappearing replaces earlier request tokens and invalidates the provider prefix from that point), but that effect is owned and documented by each command's host package.
## Known Limitations and Deferred Work
- **Detached-result notices fall back to the console off-session** — the fire-and-forget paths route results to the triggering session's composer via `SessionInput.notify`; after session teardown the console line is the only remaining surface.

View File

@@ -0,0 +1,29 @@
# @deepseek-ai/dsh-client-ui-commands
[English](README.md) | 中文
客户端命令 API(`ctx.commandUi`):以会话为 key 的命令目录缓存、带 `matchSpace`/`matchEnter` 决策钩子的 `/` 命令 source、三类派发(`execute`/`popupSelect`/`leadingInput`),以及面向业务包的 popupSelect 注册。[Web 命令 Agent Note](../../../.agents/notes/implemented/architecture/2026-07-25-web-command-surfaces-and-assembly.zh.md) 记录了这项决策。
`src/client/contract.ts` 是固定的业务 API 约定:`CommandUiContract.register(name, spec)` 与 `decorate(name, spec)` 是业务包消费的全部内容;`CommandUiSpec{options, onSelect}` 自己提供 popup 数据——外层组件归本包所有,业务包永远见不到它。贡献项是客户端自有命令(与 host 命令同名时会明确报错);装饰项则为**已存在的** host 命令添加裸调用 popup。host 保留目录行、带参 claim(空格/带参数的 Enter)与生命周期记账,被装饰的名字若在会话目录中无 host 行,则永不触发。命令类型按每次派发派生,绝不在注册时定型:带 `input` 的 host descriptor 是 `leadingInput`,注册了 `CommandUiSpec` 的是 `popupSelect`,其余全部是 `execute`。
`CommandDirectory`(`src/client/directory.ts`)是唯一的 wire 派生缓存,以会话为 key。普通会话通过 `command.list({sessionId})` 拉取,source 的 scope 出生 `warm` 钩子会预热该会话的缓存项。由目录寻址的可继续子代理会在客户端解析为空命令目录:`command.list` 绑定 Agent,若预热它,就会仅因查看持久化历史而激活子代理。缓存项由转发的 owner 事件 `commands/change` 软失效(重拉在途期间旧快照继续服务),也由转发的 `agent-preset/selected` 对该会话单独软失效(重组 agent 不产生任何注册,注册表级信号不会为它触发),由 `connection/reset` 硬失效,并以 epoch 把关,被取代的旧拉取永远无法覆盖更新的结果。`matchSpace` 只凭该缓存同步应答;`matchEnter` 在 SubmitAttempt 信号上强等缓存,预热失败即拒绝——`/` 开头的一行绝不会被静默降级为普通提示词。
`command.execute` 返回已匹配的命令结果后,当前浏览器会发布本地 `command/executed(sessionId, name, result)`。其他客户端只会通过 Host 事件流收到持久命令节点,不会收到这条确认,因此浏览器专属副作用可以筛选由实际提交命令的客户端收到的成功结果,而不会把 Session 回放当成操作请求。监听器失败会逐项记录并隔离,不会改变已经准入的命令结果,也不会阻止后续监听器运行。
菜单查询会按顺序且不区分大小写地模糊匹配命令名的子序列。前缀排名最高;其余匹配项按分隔符边界优先、相邻字符优先、间隔越短越优先的规则排序,若仍同分,则以目录顺序和贡献项顺序打破平局。此行为只影响命令发现:space 和 Enter 仍要求命令名精确匹配。原理:[Web 斜杠命令模糊发现](../../../.agents/notes/implemented/feature/2026-08-04-web-slash-command-fuzzy-discovery.md)。
`PopupSelectController`(`src/client/popup.ts`)是不含界面的外壳状态:`PopupSelectView` 自行注册进 `conversation.input.overlay`(SlotMap key 归 ui-conversation 所有;本包只以 type-only 导入引入该声明——没有运行时依赖边)。壳是打开期间持有焦点的瞬态层;onSelect 之后的 token 片段消费在两条分支上都经 `consumeTokenSegment` 执行(菜单路径做 span CAS,回车路径做裸 token 相等比较),作用于接线层经 `bindDraft` 绑定的草稿表层。
`/client` 入口导出插件主体(`apply`/`inject`)、`CommandUiRuntime`、目录类和 popup 类及其状态类型,以及固定的约定类型;外层组件本身是 overlay 注册的内部实现。
## 模型体验
间接影响,途径是本包的派发与 `claim.submit` 路径触发的 host `command.execute` RPC:匹配命中的命令,其 handler 会修改 host 领域状态,其他包再把该状态投影进下一个请求(`/plan` 的 handler 翻转 plan 模式,其归属包注入 `plan:policy` 系统提示词 section),而命令行本身、detached result 与所有菜单/notice 渲染都留在客户端,永不进入会话日志。
#### KV Cache 影响
无直接影响;该包既不组装也不发送提供方请求。它触发的命令 handler 可能改变归属 host 包对下一个请求系统提示词的贡献(某个 section 的出现或消失会替换较早的请求 token,并使提供方前缀从该点起失效),但这一影响由各命令的 host 包拥有并记录。
## 已知限制与暂缓事项
- **脱离会话后,detached result 的 notice 回退到 console**:fire-and-forget 路径经 `SessionInput.notify` 把结果送到触发会话的 composer;会话销毁后,console 输出行是仅剩的呈现面。

View File

@@ -0,0 +1,87 @@
{
"name": "@deepseek-ai/dsh-client-ui-commands",
"description": "Client command surface: global directory cache, '/' source, three command UI kinds, popupSelect registry",
"version": "0.0.1-rc.2",
"publishConfig": {
"access": "restricted"
},
"repository": {
"type": "git",
"url": "git+https://github.com/deepseek-ai/deepseek-harness.git",
"directory": "packages/client/ui-commands"
},
"type": "module",
"main": "lib/index.js",
"types": "lib/types/index.d.ts",
"exports": {
".": {
"types": "./lib/types/index.d.ts",
"default": "./lib/index.js"
},
"./invariant": {
"types": "./lib/types/invariant.d.ts",
"default": "./lib/invariant.js"
},
"./client": {
"types": "./lib/types/client/index.d.ts",
"default": "./lib/client.js"
},
"./src/*": "./src/*",
"./package.json": "./package.json"
},
"dsh": {
"client": {
"inject": [
"@deepseek-ai/dsh-api-remotes",
"@deepseek-ai/dsh-client-runtime",
"@deepseek-ai/dsh-client-locale",
"@deepseek-ai/dsh-client-ui-input-trigger",
"@deepseek-ai/dsh-client-ui-conversation"
],
"platform": "web"
}
},
"scripts": {
"bundle": "tsdown",
"watch": "tsdown --watch"
},
"license": "BSD-3-Clause",
"dependencies": {
"clsx": "^2.0.0"
},
"peerDependencies": {
"@deepseek-ai/dsh-api-remotes": "workspace:^",
"@deepseek-ai/dsh-client-locale": "workspace:^",
"@deepseek-ai/dsh-client-runtime": "workspace:^",
"@deepseek-ai/dsh-client-ui-conversation": "workspace:^",
"@deepseek-ai/dsh-client-ui-primitives": "workspace:^",
"@deepseek-ai/dsh-client-ui-input-trigger": "workspace:^",
"@deepseek-ai/dsh-client-ui-slots": "workspace:^",
"@deepseek-ai/dsh-commands": "workspace:^",
"@deepseek-ai/dsh-invariants": "workspace:^",
"@deepseek-ai/cordis": "workspace:^",
"react": "^18.2.0"
},
"devDependencies": {
"@deepseek-ai/dsh-api-remotes": "workspace:^",
"@deepseek-ai/dsh-client-connection": "workspace:^",
"@deepseek-ai/dsh-client-locale": "workspace:^",
"@deepseek-ai/dsh-client-runtime": "workspace:^",
"@deepseek-ai/dsh-client-test-runtime": "workspace:^",
"@deepseek-ai/dsh-client-ui-conversation": "workspace:^",
"@deepseek-ai/dsh-client-ui-primitives": "workspace:^",
"@deepseek-ai/dsh-client-ui-input-trigger": "workspace:^",
"@deepseek-ai/dsh-client-ui-slots": "workspace:^",
"@deepseek-ai/dsh-commands": "workspace:^",
"@deepseek-ai/dsh-invariants": "workspace:^",
"@types/react": "~18.3.1",
"@deepseek-ai/cordis": "workspace:^",
"react": "^18.2.0"
},
"files": [
"lib/index.js",
"lib/invariant.js",
"lib/client.js",
"lib/types/**/*.d.ts"
]
}

View File

@@ -0,0 +1,118 @@
/* Official popupSelect shell card: menu-surface tokens (same family as
* ui-primitives Menu.module.css — figma MenuDropdown r12 / hairline /
* shadow-lv3), anchored by the conversation.input.overlay slot. */
.card {
/* The overlay anchor is a zero-height strip on the composer card's top
edge; entries float themselves above it (same rule as MenuView). */
position: absolute;
bottom: calc(100% + 4px);
left: 0;
z-index: 100;
padding: 4px;
display: flex;
flex-direction: column;
min-width: min(220px, 100%);
/* Never wider than the composer card (the overlay anchor's width): long
rows truncate instead of pushing the card past the composer's edge. */
max-width: 100%;
/* Height cap: the 320px design maximum, clamped at runtime to the space
* above the composer (inline max-height set in PopupSelectView.tsx). */
max-height: 320px;
overflow: hidden;
/* Elevated surface: the scrollbar thumb takes the l2 elevation tokens
(see ui-theme styles/scrollbar.css for the rebinding contract). */
--dsh-scrollbar-thumb: var(--dsw-alias-scrollbar-bg-l2);
--dsh-scrollbar-thumb-hover: var(--dsw-alias-scrollbar-hover-l2);
border: 1px solid var(--dsw-alias-border-inverted);
border-radius: 12px;
background: var(--dsw-specific-menu);
box-shadow: var(--dsw-shadow-lv3);
outline: none;
}
.viewport {
display: flex;
flex-direction: column;
min-height: 0;
overflow-y: auto;
}
.row {
display: flex;
align-items: center;
gap: 8px;
padding: 6px 8px;
border-radius: 8px;
cursor: pointer;
font-size: 13px;
color: var(--dsw-alias-label-primary);
}
.rowActive {
background: var(--dsw-alias-interactive-bg-hover);
}
.label {
flex: 1 1 auto;
min-width: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.detail {
font-size: 12px;
color: var(--dsw-alias-label-tertiary);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.check {
display: inline-flex;
flex: none;
color: var(--dsw-alias-label-primary);
}
.status {
padding: 8px 10px;
font-size: 13px;
color: var(--dsw-alias-label-tertiary);
}
.search {
margin: 2px 2px 4px;
padding: 6px 8px;
border: 1px solid var(--dsw-alias-border-inverted);
border-radius: 8px;
background: transparent;
font-size: 13px;
color: var(--dsw-alias-label-primary);
outline: none;
}
.error {
display: flex;
align-items: center;
gap: 8px;
padding: 6px 8px;
font-size: 12px;
color: var(--dsw-alias-state-error-primary);
}
.errorText {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
}
.retry {
padding: 2px 8px;
border: 1px solid var(--dsw-alias-border-inverted);
border-radius: 6px;
background: transparent;
font-size: 12px;
color: var(--dsw-alias-label-primary);
cursor: pointer;
}

View File

@@ -0,0 +1,176 @@
/**
* Official popupSelect shell: renders one session's PopupSelectController
* store into the conversation.input.overlay anchor. Unlike the slash menu
* (combobox — textarea keeps focus), this shell HOLDS focus while open: the
* inner search input takes focus, plain typing filters the loaded options
* locally, Enter/↑↓ drive the filtered highlight (scrolled into view), Escape
* dismisses back to the composer, and ←→ keep the search input's native
* caret. Any pointer interaction outside the box dismisses (the click's own
* target takes focus). Closed state renders null; the overlay slot stays
* mounted. The card height clamps to the space above the composer.
*/
import { useEffect, useRef } from 'react'
import { useSyncExternalStore } from 'react'
import clsx from 'clsx'
import { IconCheckOutline16, RiskConfirmation, useAnchoredMaxHeight } from '@deepseek-ai/dsh-client-ui-primitives'
import type { PropsLocale } from '@deepseek-ai/dsh-client-ui-slots'
import { filterOptions } from './popup.ts'
import type { PopupSelectController } from './popup.ts'
import css from './PopupSelectView.module.css'
/** Design cap on the card height (same MenuDropdown family as the slash menu). */
const MAX_HEIGHT = 320
/** Injected business face of the popupSelect overlay entry. */
export interface PopupSelectInjected {
/** The session's shell controller (state store + verbs; the view never touches the open-context type). */
popup: PopupSelectController
}
/** Full shell props: injected face + the locale seat. */
export type PopupSelectViewProps = PopupSelectInjected & PropsLocale<'command'>
/**
* Render the popupSelect shell overlay entry.
* @param props - injected face: the session's shell controller; `t` rides the standard locale seat.
* @returns the select card while open; null while closed.
*/
export function PopupSelectView({ popup, t }: PopupSelectViewProps) {
const state = useSyncExternalStore(
fn => popup.state.subscribe(fn),
() => popup.state.getSnapshot(),
)
const cardRef = useRef<HTMLDivElement>(null)
const searchRef = useRef<HTMLInputElement>(null)
// The card is bottom-anchored above the composer; clamp the design cap to
// the space above it, re-measured on every store update.
const maxHeight = useAnchoredMaxHeight(cardRef, MAX_HEIGHT, state)
const active = state.open ? state.active : null
// The search input keeps focus while arrows move a virtual highlight, so
// the browser never scrolls the active row into view — do it here.
useEffect(() => {
if (active === null) return
cardRef.current?.querySelector('[aria-selected="true"]')?.scrollIntoView({ block: 'nearest' })
}, [active])
// Focus ownership: the search input grabs on open, and ANY outside
// pointer interaction dismisses —
// capture phase so a click landing anywhere else (textarea included)
// closes the shell before its own handlers run; that click's target then
// takes focus naturally, so no focusComposer here.
useEffect(() => {
if (!state.open || state.confirming !== null) return
const onPointerDown = (ev: PointerEvent): void => {
if (cardRef.current !== null && ev.target instanceof Node && cardRef.current.contains(ev.target)) return
popup.dismiss()
}
document.addEventListener('pointerdown', onPointerDown, true)
return () => { document.removeEventListener('pointerdown', onPointerDown, true) }
}, [state.open, state.confirming, popup])
// Focus the search input after it mounts (separate effect so the ref is populated).
useEffect(() => {
if (state.open && state.confirming === null) searchRef.current?.focus()
}, [state.open, state.confirming])
if (!state.open) return null
const rows = filterOptions(state.options, state.search)
const confirmation = state.confirming?.confirmation
const onKeyDown = (ev: React.KeyboardEvent<HTMLDivElement>): void => {
// ArrowLeft/ArrowRight fall through on purpose: the search input keeps
// its native caret movement.
switch (ev.key) {
case 'ArrowDown':
ev.preventDefault()
popup.move(1)
return
case 'ArrowUp':
ev.preventDefault()
popup.move(-1)
return
case 'Enter':
ev.preventDefault()
void popup.select(state.active)
return
case 'Escape':
ev.preventDefault()
popup.dismiss({ focusComposer: true })
return
default:
}
}
return (
<>
{state.confirming === null && (
<div
ref={cardRef}
className={css.card}
style={{ maxHeight }}
aria-label={t('overlay.aria', { command: String(state.command) })}
onKeyDown={onKeyDown}
>
<input
ref={searchRef}
className={css.search}
type="text"
placeholder={t('search.placeholder')}
aria-label={t('search.aria')}
value={state.search}
readOnly={state.submitting}
onChange={(ev) => { popup.setSearch(ev.currentTarget.value) }}
/>
{state.error !== null && (
<div className={css.error} role="alert">
<span className={css.errorText}>{state.error}</span>
{state.status === 'failed' && (
<button type="button" className={css.retry} onClick={() => { popup.retry() }}>{t('retry')}</button>
)}
</div>
)}
{state.status === 'pending' && <div className={css.status}>{t('status.loading')}</div>}
{state.submitting && <div className={css.status}>{t('status.applying')}</div>}
{state.status === 'ready' && rows.length === 0 && <div className={css.status}>{t('status.empty')}</div>}
{state.status === 'ready' && (
<div role="listbox" aria-label={t('listbox.aria', { command: String(state.command) })} className={css.viewport}>
{rows.map((option, index) => (
<div
key={option.id}
role="option"
aria-selected={index === state.active}
className={clsx(css.row, index === state.active && css.rowActive)}
// mousedown would race the document capture listener; the shell
// owns focus anyway, so a plain click (inside the card → no
// dismiss) works.
onClick={() => { void popup.select(index) }}
onMouseEnter={() => { popup.highlight(index) }}
>
<span className={css.label}>{option.label}</span>
{option.detail !== undefined && <span className={css.detail}>{option.detail}</span>}
{option.active === true && <span className={css.check}><IconCheckOutline16 /></span>}
</div>
))}
</div>
)}
</div>
)}
{confirmation !== undefined && (
<RiskConfirmation
open
title={confirmation.title}
description={confirmation.description}
acknowledgeLabel={confirmation.acknowledgeLabel}
cancelLabel={confirmation.cancelLabel}
confirmLabel={confirmation.confirmLabel}
acknowledged={state.acknowledged}
onAcknowledgedChange={(value) => { popup.acknowledge(value) }}
onCancel={() => { popup.cancelConfirmation() }}
onConfirm={() => { void popup.confirm() }}
/>
)}
</>
)
}

View File

@@ -0,0 +1,89 @@
/**
* Frozen contract of the client command surface. Types only. The
* CommandUiRuntime (`ctx.commandUi`) implements this face; business packages
* consume `register` alone.
*/
import type { ClientContext } from '@deepseek-ai/dsh-client-runtime/client'
import type { ClientSessionContext } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
/** Copy for an option that must be acknowledged before onSelect can run. */
export interface SelectConfirmation {
readonly title: string
readonly description: string
readonly acknowledgeLabel: string
readonly cancelLabel: string
readonly confirmLabel: string
}
/** One option row of a popupSelect shell. */
export interface SelectOption {
readonly id: string
readonly label: string
readonly detail?: string
readonly active?: boolean
/** Optional in-page risk gate owned by the shared popup shell. */
readonly confirmation?: SelectConfirmation
}
/**
* Business registration for the popupSelect command kind. Data is
* self-served: options/onSelect use the business package's own protocol.
* The shell component is owned by ui-commands; business never sees it. Both
* callbacks receive the ClientSessionContext captured at popup open.
*/
export type CommandUiSpec = {
readonly kind: 'popupSelect'
options(session: ClientSessionContext, signal: AbortSignal): Promise<readonly SelectOption[]>
onSelect(option: SelectOption, session: ClientSessionContext): void | Promise<void>
}
/**
* One client-owned command contribution: a slash-menu entry whose behavior
* lives entirely on the client (no host descriptor). Merged with the host
* catalog by name — a collision with a host command fails loud at candidate
* synthesis, never shadows.
*/
export interface CommandContribution {
/** Command name without the leading slash (unique across contributions). */
readonly name: string
/** Menu row description. */
readonly description: string
/** Capability filter, called with a fresh projection per candidate pass. */
available(session: ClientSessionContext): boolean
/** The command's UI behavior (this phase: popupSelect only). */
readonly ui: CommandUiSpec
}
/**
* A UI decoration hung on one HOST command: what its BARE invocation does on
* this client. Not a second command — the host command keeps its catalog
* row, its argument claim (space / argued enter), and its lifecycle logging;
* the decoration replaces only the bare menu-pick/enter with a popup whose
* onSelect typically submits a completed line back through command.execute.
* A decoration never manufactures a row: a name with no host catalog entry
* in the session's directory simply never reaches the decoration.
*/
export interface CommandDecoration {
/** The HOST command name this decorates (without the leading slash). */
readonly name: string
/** Capability filter, called with a fresh projection per bare invocation. */
available(session: ClientSessionContext): boolean
/** The bare-invocation UI (this phase: popupSelect only). */
readonly ui: CommandUiSpec
}
/** The `ctx.commandUi` service face visible to business packages. */
export interface CommandUiContract {
/**
* Register one client command contribution; effect disposer. Duplicate
* names throw at registration.
*/
register(contribution: CommandContribution): () => void
/**
* Hang a bare-invocation decoration on one host command; effect disposer.
* Duplicate names throw at registration.
*/
decorate(decoration: CommandDecoration): () => void
/** Resolve the per-session popup controller for one session scope (wiring/overlay layer). */
popupFor(actx: ClientContext): unknown
}

View File

@@ -0,0 +1,172 @@
/**
* Command-directory cache keyed by session: one entry per served catalog —
* every session is agent-backed, so `command.list({sessionId})` is the only
* request fields. Each entry keeps the single-flight / soft-hard invalidation
* / epoch-guard behavior of the original global cache; the session-key axis
* is the only extra dimension.
*/
import type { CommandDescriptor } from '@deepseek-ai/dsh-commands/types'
import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
export type { CommandDescriptor } from '@deepseek-ai/dsh-commands/types'
/**
* cold = never pulled; pending = pull in flight with nothing servable;
* ready = snapshot serving (a soft-invalidate repull keeps this status);
* failed = last winning pull rejected, snapshot dropped.
*/
export type DirectoryStatus = 'cold' | 'pending' | 'ready' | 'failed'
/** Injected pull (the service binds command.list off the root connection). */
export type FetchCommands = (sessionId: SessionId) => Promise<readonly CommandDescriptor[]>
/** One session key's cache cell. */
class Entry {
state: DirectoryStatus = 'cold'
commands: readonly CommandDescriptor[] = []
/** Bumped at each pull start; only the latest pull may publish its outcome. */
epoch = 0
lastError: unknown
waiters: Array<() => void> = []
}
/** The session-keyed directory cache. Plain class — the owning service wires events and RPC. */
export class CommandDirectory {
private readonly entries = new Map<SessionId, Entry>()
constructor(private readonly fetchCommands: FetchCommands) {}
/**
* Current cache status for one session.
* @param sessionId - session key.
* @returns the entry status (cold when never touched).
*/
status(sessionId: SessionId): DirectoryStatus {
return this.entries.get(sessionId)?.state ?? 'cold'
}
/**
* Synchronous exact-name lookup over one session's hot snapshot.
* @param sessionId - session key.
* @param name - command name without the leading slash.
* @returns the descriptor, or undefined when absent or the entry is not ready.
*/
resolve(sessionId: SessionId, name: string): CommandDescriptor | undefined {
const entry = this.entries.get(sessionId)
if (entry === undefined || entry.state !== 'ready') return undefined
return entry.commands.find(c => c.name === name)
}
/** Soft invalidation (commands-changed): background repull on every touched key; ready snapshots keep serving. */
invalidateAll(): void {
for (const key of this.entries.keys()) void this.refresh(key)
}
/**
* Hard reset on reconnect: every entry drops its snapshot (the agent world
* may have changed shape across the generation) and prewarms.
*/
resetConnected(): void {
for (const [key, entry] of this.entries) {
entry.state = 'cold'
entry.commands = []
void this.refresh(key)
}
}
/**
* Fire-and-forget prewarm of one session (the command source's scope-birth
* warm hook lands here).
* @param sessionId - session key.
*/
warm(sessionId: SessionId): void {
const entry = this.entry(sessionId)
if (entry.state === 'cold' || entry.state === 'failed') void this.refresh(sessionId)
}
/**
* Start one pull for one session. Publishes ready/failed only while it is
* still the key's latest pull (epoch guard); a ready snapshot is not
* demoted while the pull flies.
* @param sessionId - session key.
* @returns settled when this pull's outcome is published or discarded.
*/
async refresh(sessionId: SessionId): Promise<void> {
const entry = this.entry(sessionId)
const epoch = ++entry.epoch
if (entry.state !== 'ready') entry.state = 'pending'
try {
const commands = await this.fetchCommands(sessionId)
if (epoch !== entry.epoch) return
entry.commands = commands
entry.state = 'ready'
entry.lastError = undefined
} catch (error) {
if (epoch !== entry.epoch) return
entry.commands = []
entry.state = 'failed'
entry.lastError = error
} finally {
if (epoch === entry.epoch) notifyWaiters(entry)
}
}
/**
* Strong-wait until one session's catalog is servable (the enter-
* adjudication "directory must be reached" rule): ready returns at once;
* cold/failed launch a fresh pull; pending joins the flying one. Rejects
* when the awaited pull fails or the signal aborts.
* @param sessionId - session key.
* @param signal - attempt-scoped abort (the SubmitAttempt signal).
* @returns the hot command snapshot.
*/
async ensureReady(sessionId: SessionId, signal: AbortSignal): Promise<readonly CommandDescriptor[]> {
const entry = this.entry(sessionId)
while (true) {
if (entry.state === 'ready') return entry.commands
if (entry.state !== 'pending') void this.refresh(sessionId)
await settled(entry, signal)
if (entry.state === 'failed') {
throw new Error(`command directory warmup failed: ${entry.lastError instanceof Error ? entry.lastError.message : String(entry.lastError)}`)
}
// Still pending (the awaited pull was superseded) → wait for the winner.
}
}
private entry(sessionId: SessionId): Entry {
let entry = this.entries.get(sessionId)
if (entry === undefined) {
entry = new Entry()
this.entries.set(sessionId, entry)
}
return entry
}
}
/** One settlement tick for one entry: resolves at the next winning publish, rejects on abort. */
function settled(entry: Entry, signal: AbortSignal): Promise<void> {
if (signal.aborted) return Promise.reject(abortReason(signal))
return new Promise((resolve, reject) => {
const waiter = (): void => {
signal.removeEventListener('abort', onAbort)
resolve()
}
const onAbort = (): void => {
entry.waiters = entry.waiters.filter(w => w !== waiter)
reject(abortReason(signal))
}
signal.addEventListener('abort', onAbort, { once: true })
entry.waiters.push(waiter)
})
}
function notifyWaiters(entry: Entry): void {
const woken = entry.waiters
entry.waiters = []
for (const wake of woken) wake()
}
/** Normalize an abort into an Error rejection. */
function abortReason(signal: AbortSignal): Error {
return signal.reason instanceof Error ? signal.reason : new Error('command directory wait aborted')
}

View File

@@ -0,0 +1,73 @@
/**
* Command UI plugin, browser half: CommandUiRuntime (`ctx.commandUi`) owning the
* capability-keyed directory cache, the '/' command source, the client
* contribution registry, and the per-session popupSelect controllers; the
* popupSelect shell self-registers into conversation.input.overlay with
* per-session resolution.
*/
import type { ClientContext } from '@deepseek-ai/dsh-client-runtime/client'
// Type-only: pulls the 'conversation.input.overlay' SlotMap declaration (the
// key's owner) into this program so the overlay registration below typechecks
// against the real declaration — no runtime edge to ui-conversation.
import type {} from '@deepseek-ai/dsh-client-ui-conversation/client'
// Type-only: pulls the locale plugin's Context merge (ctx.locale).
import type {} from '@deepseek-ai/dsh-client-locale/client'
import { CommandUiRuntime } from './service.ts'
import type { PopupSelectInjected } from './PopupSelectView.tsx'
import { PopupSelectView } from './PopupSelectView.tsx'
import { en, zh, type CommandKey } from './locales.ts'
export { CommandUiRuntime } from './service.ts'
export { CommandDirectory } from './directory.ts'
export type { CommandDescriptor, DirectoryStatus } from './directory.ts'
export { filterOptions, PopupSelectController } from './popup.ts'
export type { PopupSelectDeps, PopupSpec, PopupState, TokenSegment } from './popup.ts'
export type { PopupSelectInjected, PopupSelectViewProps } from './PopupSelectView.tsx'
export type {
CommandContribution, CommandDecoration, CommandUiContract, CommandUiSpec, SelectConfirmation, SelectOption,
} from './contract.ts'
export type { CommandKey } from './locales.ts'
declare module '@deepseek-ai/cordis' {
interface Context {
commandUi: CommandUiRuntime
}
}
declare module '@deepseek-ai/dsh-client-ui-slots' {
interface LocaleNamespaceMap {
/** The popupSelect shell's copy. */
command: CommandKey
}
}
/** Dictionary namespace owned by this plugin. */
const NS = 'command'
/** Required services: the '/' source registry, session scopes, commands Remote, and locale registry. */
export const inject = ['inputTriggers', 'sessions', 'remote', 'remote.commands', 'locale']
/**
* Client plugin body: mount the service, then register the popupSelect shell
* into the input overlay once its declarer is up.
* @param ctx - client root context.
*/
export function apply(ctx: ClientContext): void {
ctx.effect(() => ctx.locale.register(NS, { zh, en }), 'ui-commands: dictionaries')
ctx.plugin(CommandUiRuntime)
ctx.inject(['slots', 'commandUi', 'sessions'], (scope: ClientContext) => {
const command = scope.commandUi
const sessions = scope.sessions
scope.slots.inject('conversation.input.overlay', () => scope.slots.register({
name: 'conversation.input.overlay',
id: 'command-popup',
order: 1,
locale: NS,
inject: (sessionId): PopupSelectInjected => {
const actx = sessions.scope(sessionId)
if (actx === undefined) throw new Error(`ui-commands: session "${String(sessionId)}" resolved no scope`)
return { popup: command.popupFor(actx) }
},
}, PopupSelectView))
})
}

View File

@@ -0,0 +1,26 @@
/** `command` namespace dictionaries (the popupSelect shell's copy). */
/** Simplified Chinese dictionary (the key-set source of truth). */
export const zh = {
'search.placeholder': '搜索…',
'search.aria': '筛选选项',
'status.loading': '正在加载选项…',
'status.applying': '正在应用…',
'status.empty': '无选项',
'overlay.aria': '/{command} 选项',
'listbox.aria': '/{command} 匹配项',
} satisfies Record<string, string>
/** The command namespace key union. */
export type CommandKey = keyof typeof zh
/** English dictionary, checked complete against the zh key set. */
export const en = {
'search.placeholder': 'Search…',
'search.aria': 'Filter options',
'status.loading': 'Loading options…',
'status.applying': 'Applying…',
'status.empty': 'No options',
'overlay.aria': '/{command} options',
'listbox.aria': '/{command} matches',
} satisfies Record<CommandKey, string>

View File

@@ -0,0 +1,292 @@
/**
* Headless popupSelect shell state: one controller per client
* session, owned by CommandUiRuntime's per-session map and torn down by the
* session scope disposer. The shell is a transient layer (never in the input
* state machine): it loads options once, filters them locally against the
* shell's own search text, and settles a selection through the context
* captured at open time. Draft consumption and composer focus are injected
* callbacks — the session wiring dispatches the consume-token event (the
* Input side owns the span/bare-token CAS guard) and focuses the composer;
* the controller never touches the input machine.
*/
import { createSnapshotStore } from '@deepseek-ai/dsh-client-runtime/client'
import type { SnapshotStore } from '@deepseek-ai/dsh-client-runtime/client'
import type { TokenSpan } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
import type { SelectOption } from './contract.ts'
/**
* The command token segment snapshotted at shell-open time, replayed to the
* injected {@link PopupSelectDeps.consume} callback after a successful
* selection. The Input side guards it: a menu-path span consumes iff draftRev
* is unchanged, an enter-path line iff the trimmed draft still equals the
* bare token.
*/
export type TokenSegment =
| { readonly via: 'menu'; readonly span: TokenSpan }
| { readonly via: 'enter'; readonly token: string }
/**
* Structural business spec the shell settles against — the popupSelect half
* of CommandUiSpec, generic in the context value the opener captures (the
* session wiring passes its session projection; the controller only carries
* it from open() to the callbacks).
*/
export interface PopupSpec<TCtx> {
/** Load the option rows once per open (retry after failure reuses the same signal). */
options(context: TCtx, signal: AbortSignal): Promise<readonly SelectOption[]>
/** Settle the picked option against the open-time context. */
onSelect(option: SelectOption, context: TCtx): void | Promise<void>
}
/** Injected session-wiring callbacks of one controller (tests pass fakes). */
export interface PopupSelectDeps {
/**
* Consume the open-time token segment after a successful onSelect (the
* wiring dispatches the consume-token event to the opening session).
* @param segment - the open-time token segment snapshot.
* @returns whether the token was consumed; false (CAS miss) is benign and
* never retried.
*/
consume(segment: TokenSegment): boolean
/** Return focus to the session composer (successful settle and Escape close paths). */
focusComposer(): void
}
/** Popup shell state (the shell component renders from here; closed = render null). */
export interface PopupState {
readonly open: boolean
/** Command name the shell is open for (null while closed). */
readonly command: string | null
/** Options-load lifecycle; 'failed' keeps the shell open for retry(). */
readonly status: 'pending' | 'ready' | 'failed'
/** Options as loaded — never re-fetched per keystroke; views render {@link filterOptions} over them. */
readonly options: readonly SelectOption[]
/** Local filter text over the loaded options. */
readonly search: string
/** Highlight index into the filtered row list (0 when empty/pending). */
readonly active: number
/** A select() settlement is in flight: further select/search/highlight no-op until it settles. */
readonly submitting: boolean
/** Option waiting for explicit risk acknowledgement; null during normal selection. */
readonly confirming: SelectOption | null
/** Caller-controlled checkbox state for the pending confirmation. */
readonly acknowledged: boolean
/** Surfaced settlement failure (options load or onSelect); null when none. */
readonly error: string | null
}
const CLOSED: PopupState = {
open: false, command: null, status: 'pending', options: [], search: '', active: 0,
submitting: false, confirming: null, acknowledged: false, error: null,
}
/**
* Filter option rows against the shell's local search text (case-insensitive
* substring over label and detail; blank search keeps every row).
* @param options - the loaded rows.
* @param search - the shell's search text.
* @returns the rows the shell shows and highlights over.
*/
export function filterOptions(options: readonly SelectOption[], search: string): readonly SelectOption[] {
const query = search.trim().toLowerCase()
if (query === '') return options
return options.filter(o => o.label.toLowerCase().includes(query) || (o.detail?.toLowerCase().includes(query) ?? false))
}
/** One open shell's bindings (spec + open-time context + segment snapshot + options-fetch abort). */
interface OpenBinding<TCtx> {
readonly command: string
readonly spec: PopupSpec<TCtx>
readonly context: TCtx
readonly segment: TokenSegment
readonly abort: AbortController
}
/** The shell's error-strip line for a settlement failure. */
function errorText(error: unknown): string {
return error instanceof Error ? error.message : String(error)
}
/**
* Headless controller of one session's popupSelect shell. Late settlements
* lose their write rights through binding identity: dismiss/dispose/reopen
* swap the binding, so a settling options fetch or onSelect that no longer
* matches writes nothing and consumes nothing.
*/
export class PopupSelectController<TCtx = unknown> {
/** Shell state store (the overlay component subscribes here). */
readonly state: SnapshotStore<PopupState> = createSnapshotStore<PopupState>(CLOSED)
private binding: OpenBinding<TCtx> | null = null
/**
* @param deps - session-wiring callbacks (token consumption + composer focus).
*/
constructor(private readonly deps: PopupSelectDeps) {}
/**
* Open the shell for one command: publish pending state and fetch options
* once through the business spec. A reopen supersedes the previous shell
* (its options fetch is aborted, its late settlements are dropped).
* @param command - command name the shell serves.
* @param spec - the registered popupSelect spec.
* @param context - open-time context snapshot, handed verbatim to options/onSelect.
* @param segment - open-time token segment snapshot for post-select consumption.
*/
open(command: string, spec: PopupSpec<TCtx>, context: TCtx, segment: TokenSegment): void {
this.binding?.abort.abort()
const binding: OpenBinding<TCtx> = { command, spec, context, segment, abort: new AbortController() }
this.binding = binding
this.state.set({ ...CLOSED, open: true, command })
this.load(binding)
}
/** Run the one options fetch of a binding; settlement rights die with the binding. */
private load(binding: OpenBinding<TCtx>): void {
binding.spec.options(binding.context, binding.abort.signal).then(
(options) => {
if (this.binding !== binding) return
this.state.set({ ...this.state.getSnapshot(), status: 'ready', options, active: 0, error: null })
},
(error: unknown) => {
if (this.binding !== binding) return
console.error(`[ui-commands] popupSelect options failed for /${binding.command}:`, error)
this.state.set({ ...this.state.getSnapshot(), status: 'failed', options: [], active: 0, error: errorText(error) })
},
)
}
/** Re-run a failed options fetch (search survives; no-op unless status is 'failed'). */
retry(): void {
const binding = this.binding
const s = this.state.getSnapshot()
if (binding === null || !s.open || s.status !== 'failed') return
this.state.set({ ...s, status: 'pending', error: null })
this.load(binding)
}
/**
* Replace the local search text (pure local filter — the provider is never
* re-queried) and rebase the highlight onto the new filtered list.
* @param search - the shell search input's text.
*/
setSearch(search: string): void {
const s = this.state.getSnapshot()
if (!s.open || s.submitting || s.confirming !== null || search === s.search) return
this.state.set({ ...s, search, active: 0 })
}
/**
* Move the highlight across the filtered rows (wraps around; no-op unless
* options are ready and no selection is in flight).
* @param dir - +1 down, -1 up.
*/
move(dir: 1 | -1): void {
const s = this.state.getSnapshot()
if (!s.open || s.status !== 'ready' || s.submitting || s.confirming !== null) return
const rows = filterOptions(s.options, s.search)
if (rows.length === 0) return
const active = (s.active + dir + rows.length) % rows.length
this.state.set({ ...s, active })
}
/**
* Set the highlight directly (pointer hover; no-op unless ready, idle, and
* in filtered range).
* @param index - filtered-row index.
*/
highlight(index: number): void {
const s = this.state.getSnapshot()
if (!s.open || s.status !== 'ready' || s.submitting || s.confirming !== null) return
if (index < 0 || index >= filterOptions(s.options, s.search).length || index === s.active) return
this.state.set({ ...s, active: index })
}
/**
* Select one filtered row: single-flight — the first call enters
* `submitting` and later calls no-op until it settles. Success consumes the
* open-time token segment (a false CAS answer is benign), closes, and
* returns focus to the composer. Failure keeps the shell open with search,
* highlight, and token intact, surfaces the error, and re-arms select as
* the retry.
* @param index - filtered-row index (callers pass the highlight or the clicked row).
* @returns settled when the attempt has closed the shell or surfaced its failure.
*/
async select(index: number): Promise<void> {
const binding = this.binding
const s = this.state.getSnapshot()
if (binding === null || !s.open || s.status !== 'ready' || s.submitting || s.confirming !== null) return
const option = filterOptions(s.options, s.search)[index]
if (option === undefined) return
if (option.confirmation !== undefined) {
this.state.set({ ...s, confirming: option, acknowledged: false, error: null })
return
}
await this.settle(binding, option)
}
/**
* Update the explicit checkbox for the currently pending risk gate.
* @param acknowledged - whether the user has acknowledged the displayed risk.
*/
acknowledge(acknowledged: boolean): void {
const s = this.state.getSnapshot()
if (!s.open || s.submitting || s.confirming === null || s.acknowledged === acknowledged) return
this.state.set({ ...s, acknowledged })
}
/** Cancel only the risk gate and return to the still-open option picker. */
cancelConfirmation(): void {
const s = this.state.getSnapshot()
if (!s.open || s.submitting || s.confirming === null) return
this.state.set({ ...s, confirming: null, acknowledged: false })
}
/** Settle the gated option only after the checkbox is acknowledged. */
async confirm(): Promise<void> {
const binding = this.binding
const s = this.state.getSnapshot()
if (binding === null || !s.open || s.submitting || s.confirming === null || !s.acknowledged) return
await this.settle(binding, s.confirming)
}
/** Run the business settlement for an already admitted option. */
private async settle(binding: OpenBinding<TCtx>, option: SelectOption): Promise<void> {
const s = this.state.getSnapshot()
if (this.binding !== binding || !s.open || s.submitting) return
this.state.set({ ...s, submitting: true, confirming: null, acknowledged: false, error: null })
try {
await binding.spec.onSelect(option, binding.context)
} catch (error) {
console.error(`[ui-commands] popupSelect onSelect failed for /${binding.command}:`, error)
if (this.binding !== binding) return // dismissed/reopened/disposed while onSelect flew
this.state.set({ ...this.state.getSnapshot(), submitting: false, error: errorText(error) })
return
}
if (this.binding !== binding) return // late success: no state write, no consumption
this.deps.consume(binding.segment)
this.binding = null
this.state.set(CLOSED)
this.deps.focusComposer()
}
/**
* Close the shell; aborts a flying options fetch and revokes settlement
* rights. An outside pointer interaction dismisses plainly (the click's own
* target takes focus); Escape passes focusComposer to return focus explicitly.
* @param opts - focusComposer: also restore composer focus (Escape path).
*/
dismiss(opts?: { readonly focusComposer?: boolean }): void {
if (this.binding === null) return
this.binding.abort.abort()
this.binding = null
this.state.set(CLOSED)
if (opts?.focusComposer === true) this.deps.focusComposer()
}
/** Scope-teardown disposer: abort in-flight work and clear state (no focus side effect). */
dispose(): void {
this.binding?.abort.abort()
this.binding = null
this.state.set(CLOSED)
}
}

View File

@@ -0,0 +1,454 @@
/**
* CommandUiRuntime (`ctx.commandUi`): the '/' command source over the
* session-keyed directory, the client-contribution registry, and the
* per-session popupSelect controllers. Candidate synthesis merges the host
* catalog with contributions by availability, then fuzzy query/position
* filtering; a host/contribution name collision fails loud. Every execute
* addresses the session's agent by sessionId — sessions are always
* agent-backed.
*/
import { Service } from '@deepseek-ai/cordis'
import type { Context } from '@deepseek-ai/cordis'
// Type-only: pulls the ctx.remote merge and the forwarded-event key face
// (`commands/change` rides the allowlist) into this program.
import type {} from '@deepseek-ai/dsh-api-remotes/client'
import type { CommandResult } from '@deepseek-ai/dsh-commands/types'
import type { ClientContext, ISessions, SessionId } from '@deepseek-ai/dsh-client-runtime/client'
import type {
CandidateRequest, ClientSessionContext, CommandClaim, PickOutcome, InputTriggerCandidate, InputTriggerPick,
SubmitOutcome,
} from '@deepseek-ai/dsh-client-ui-input-trigger/client'
import type { CommandContribution, CommandDecoration, CommandUiContract } from './contract.ts'
import type { CommandDescriptor } from './directory.ts'
import { CommandDirectory } from './directory.ts'
import { PopupSelectController } from './popup.ts'
import type { TokenSegment } from './popup.ts'
declare module '@deepseek-ai/cordis' {
interface Events {
/**
* This browser client completed one admitted Host command execution.
* Other clients receive the durable command nodes but never this local
* submission acknowledgment.
* @param sessionId - Session addressed by the local submission.
* @param name - Executed command name without the leading slash.
* @param result - Host command result returned to this browser.
* @mode emit
*/
'command/executed'(sessionId: SessionId, name: string, result: CommandResult): void
}
}
/** Recover the command name from a line the Host confirmed as executed. */
function submittedCommandName(line: string): string {
const trimmed = line.trim()
const separator = trimmed.search(/\s/u)
return (separator === -1 ? trimmed : trimmed.slice(0, separator)).slice(1)
}
/** Live mutable state in one holder (service methods run behind the caller-ctx tracker). */
interface LiveState {
readonly contributions: Map<string, CommandContribution>
readonly decorations: Map<string, CommandDecoration>
readonly popups: Map<SessionId, PopupSelectController<ClientSessionContext>>
}
/** One fuzzy match with its stable source position. */
interface RankedCandidate {
readonly candidate: InputTriggerCandidate
readonly index: number
readonly prefix: boolean
readonly score: number
}
/** Extra weight for command-name starts and separator boundaries. */
function boundaryBonus(name: string, index: number): number {
return index === 0 || name.charAt(index - 1) === '-' || name.charAt(index - 1) === '_' ? 8 : 0
}
/**
* Score the strongest ordered-subsequence alignment in O(name × query).
* Boundary and adjacent matches earn weight; skipped and leading characters
* cost weight.
*/
function fuzzyScore(name: string, query: string): number | undefined {
if (query === '') return 0
if (query.length > name.length) return undefined
const noMatch = Number.NEGATIVE_INFINITY
let previous = Array<number>(name.length).fill(noMatch)
for (let index = 0; index < name.length; index++) {
if (name.charAt(index) === query.charAt(0)) previous[index] = 1 + boundaryBonus(name, index) - index
}
for (let queryIndex = 1; queryIndex < query.length; queryIndex++) {
const current = Array<number>(name.length).fill(noMatch)
let bestGapped = noMatch
for (let index = 0; index < name.length; index++) {
const gappedIndex = index - 2
if (gappedIndex >= 0) {
const prior = previous[gappedIndex] ?? noMatch
if (prior !== noMatch) bestGapped = Math.max(bestGapped, prior + gappedIndex)
}
if (name.charAt(index) !== query.charAt(queryIndex)) continue
const bonus = 1 + boundaryBonus(name, index)
const adjacent = index > 0 ? previous[index - 1] ?? noMatch : noMatch
if (adjacent !== noMatch) current[index] = adjacent + bonus + 4
if (bestGapped !== noMatch) current[index] = Math.max(current[index] ?? noMatch, bestGapped + bonus + 1 - index)
}
previous = current
}
let best = noMatch
for (const score of previous) best = Math.max(best, score)
return best === noMatch ? undefined : best
}
/** Case-insensitive fuzzy filtering with stable ordering for equal matches. */
function fuzzyCandidates(candidates: readonly InputTriggerCandidate[], rawQuery: string): readonly InputTriggerCandidate[] {
const query = rawQuery.toLowerCase()
if (query === '') return candidates
const ranked: RankedCandidate[] = []
candidates.forEach((candidate, index) => {
const name = candidate.name.toLowerCase()
const score = fuzzyScore(name, query)
if (score !== undefined) ranked.push({ candidate, index, prefix: name.startsWith(query), score })
})
ranked.sort((left, right) =>
Number(right.prefix) - Number(left.prefix) || right.score - left.score || left.index - right.index)
return ranked.map(match => match.candidate)
}
/** Command surface: session-keyed directory + '/' source + contribution registry + per-session popups. */
export class CommandUiRuntime extends Service implements CommandUiContract {
static inject = ['inputTriggers', 'sessions', 'remote', 'remote.commands']
private readonly directory: CommandDirectory
private readonly live: LiveState = { contributions: new Map(), decorations: new Map(), popups: new Map() }
/**
* @param ctx - owning root context (plugin fiber; the service registers
* itself as `command` and follows that fiber's lifetime).
*/
constructor(ctx: Context) {
super(ctx, 'commandUi')
this.directory = new CommandDirectory(async (sessionId) => {
if (this.sessions().subagentAddress(sessionId) !== undefined) return []
const result = await ctx.remote.commands.list(sessionId)
if (!result.ok) throw new Error(`command.list failed: ${result.error.code}: ${result.error.message}`)
return result.value
})
const inputTriggers = ctx.get('inputTriggers')
if (inputTriggers === undefined) throw new Error('ui-commands: slash service unavailable')
ctx.effect(() => inputTriggers.registerSource({
trigger: '/',
name: 'command',
candidates: (session, req) => this.candidates(session, req),
onPick: pick => this.dispatch(pick),
matchSpace: (session, token) => this.matchSpace(session, token),
matchEnter: (session, line, signal) => this.matchEnter(session, line, signal),
warm: (session) => { this.directory.warm(session.sessionId) },
}), 'command: slash source')
ctx.remote.$on('commands/change', () => { this.directory.invalidateAll() })
// A preset switch changes which commands one session's agent resolves and
// registers nothing globally, so the registry-wide signal above never
// fires for it: repull that key alone, soft, so the old snapshot serves
// the menu until the new one lands.
ctx.remote.$on('agent-preset/selected', (sessionId) => { void this.directory.refresh(sessionId) })
ctx.on('connection/reset', () => { this.directory.resetConnected() })
}
/**
* Register one client command contribution; effect disposer (rides the
* caller's fiber). Duplicate names throw.
* @param contribution - the contribution (descriptor + availability + popup spec).
* @returns the disposer removing the registration.
*/
register(contribution: CommandContribution): () => void {
const dispose = this.ctx.effect(() => {
const { contributions } = this.live
if (contributions.has(contribution.name)) {
throw new Error(`ui-commands: duplicate contribution for /${contribution.name}`)
}
contributions.set(contribution.name, contribution)
return () => { contributions.delete(contribution.name) }
}, 'command.register()')
return () => { void dispose() }
}
/**
* Hang a bare-invocation decoration on one host command; effect disposer
* (rides the caller's fiber). Duplicate names throw.
* @param decoration - host command name + availability + popup spec.
* @returns the disposer removing the registration.
*/
decorate(decoration: CommandDecoration): () => void {
const dispose = this.ctx.effect(() => {
const { decorations } = this.live
if (decorations.has(decoration.name)) {
throw new Error(`ui-commands: duplicate decoration for /${decoration.name}`)
}
decorations.set(decoration.name, decoration)
return () => { decorations.delete(decoration.name) }
}, 'command.decorate()')
return () => { void dispose() }
}
/**
* Resolve the per-session popup controller (lazy; dies with the session
* scope). The controller's consume callback dispatches the scoped
* consume-token event back to this session; focusComposer reaches the
* composer through the overlay slot currency.
* @param actx - session-scope ctx.
* @returns the resident controller.
*/
popupFor(actx: ClientContext): PopupSelectController<ClientSessionContext> {
const sessions = this.sessions()
const id = sessions.scopeOf(actx)
if (id === undefined) throw new Error('command.popupFor requires a session scope')
const { popups } = this.live
const existing = popups.get(id)
if (existing !== undefined) return existing
const controller = new PopupSelectController<ClientSessionContext>({
consume: segment => actx.bail(actx, 'slash/input-consume-token', {
guard: segment.via === 'menu'
? { kind: 'span', span: segment.span }
: { kind: 'bare-token', token: segment.token },
}) === true,
focusComposer: () => { this.focusHooks.get(id)?.() },
})
popups.set(id, controller)
actx.effect(() => () => {
controller.dispose()
popups.delete(id)
this.focusHooks.delete(id)
}, 'command: session popup')
return controller
}
/** Composer focus hooks by session (the overlay wiring binds the textarea focus here). */
private readonly focusHooks = new Map<SessionId, () => void>()
/**
* Bind one session's composer-focus hook (overlay slot wiring; unbind on unmount).
* @param id - session id.
* @param focus - textarea focus callback.
* @returns the unbind disposer.
*/
bindComposerFocus(id: SessionId, focus: () => void): () => void {
this.focusHooks.set(id, focus)
return () => {
if (this.focusHooks.get(id) === focus) this.focusHooks.delete(id)
}
}
/** Menu candidates: host catalog + contribution availability, then position filtering and fuzzy name ranking. */
private async candidates(session: ClientSessionContext, req: CandidateRequest): Promise<readonly InputTriggerCandidate[]> {
const list = await this.directory.ensureReady(session.sessionId, req.signal)
const rows: InputTriggerCandidate[] = []
const seen = new Set<string>()
for (const c of list) {
seen.add(c.name)
rows.push({ name: c.name, description: c.description, ...(c.input !== undefined ? { hint: c.input.hint } : {}) })
}
for (const contribution of this.live.contributions.values()) {
if (!contribution.available(session)) continue
if (seen.has(contribution.name)) {
throw new Error(`ui-commands: contribution /${contribution.name} collides with a host command`)
}
rows.push({ name: contribution.name, description: contribution.description })
}
return fuzzyCandidates(
rows.filter(c => req.position === 'leading' || c.hint === undefined),
req.query,
)
}
/** Decision table, menu column: contribution/decorated-host → popup; host input → claim; host bare → detached execute. */
private dispatch(pick: InputTriggerPick): PickOutcome {
const name = pick.candidate.name
const contribution = this.live.contributions.get(name)
if (contribution !== undefined && contribution.available(pick.session)) {
this.openPopup(name, contribution.ui, pick.session, { via: 'menu', span: pick.span })
return 'handled'
}
const desc = this.directory.resolve(pick.session.sessionId, name)
if (desc === undefined) return undefined // snapshot swapped between menu and pick → miss
// A decoration replaces the HOST row's bare invocation with its popup;
// it decorates only a resolvable host command (checked above), never
// manufactures one, and never touches the argument claim below.
const decoration = this.live.decorations.get(name)
if (decoration !== undefined && decoration.available(pick.session)) {
this.openPopup(name, decoration.ui, pick.session, { via: 'menu', span: pick.span })
return 'handled'
}
if (desc.input !== undefined) return { claim: this.leadingClaim(desc, pick.session) }
// Menu-pick execute consumes the trigger span before the detached run
// (scoped event; the input owns the CAS guard).
this.consumeVia(pick.session.sessionId, { via: 'menu', span: pick.span })
this.runDetached(desc, pick.session, `/${name}`)
return 'handled'
}
/** Decision table, space column: hot-key sync check; only host leadingInput claims. */
private matchSpace(session: ClientSessionContext, token: string): PickOutcome {
if (!token.startsWith('/')) return undefined
const name = token.slice(1)
if (this.live.contributions.has(name)) return undefined // popup kinds never claim on space
const desc = this.directory.resolve(session.sessionId, name)
if (desc === undefined || desc.input === undefined) return undefined
return { claim: this.leadingClaim(desc, session) }
}
/**
* Decision table, enter column. Strong-waits the session's catalog (a
* warmup failure rejects — never a silent downgrade). Contributions and
* bare host commands act on the bare token only; leadingInput claims
* args-tolerant.
*/
private async matchEnter(session: ClientSessionContext, line: string, signal: AbortSignal): Promise<PickOutcome> {
const trimmed = line.trim()
if (!trimmed.startsWith('/')) return undefined
const ws = trimmed.search(/\s/)
const token = ws === -1 ? trimmed : trimmed.slice(0, ws)
const bare = ws === -1
const name = token.slice(1)
if (name === '') return undefined
const contribution = this.live.contributions.get(name)
if (contribution !== undefined && contribution.available(session)) {
if (!bare) return undefined
this.openPopup(name, contribution.ui, session, { via: 'enter', token })
return 'handled'
}
await this.directory.ensureReady(session.sessionId, signal)
const desc = this.directory.resolve(session.sessionId, name)
if (desc === undefined) return undefined
// Bare enter on a decorated host command opens its popup; an argued line
// never consults the decoration (the claim/detached paths below own it).
if (bare) {
const decoration = this.live.decorations.get(name)
if (decoration !== undefined && decoration.available(session)) {
this.openPopup(name, decoration.ui, session, { via: 'enter', token })
return 'handled'
}
}
if (desc.input !== undefined) return { claim: this.leadingClaim(desc, session) }
if (!bare) return undefined
this.consumeVia(session.sessionId, { via: 'enter', token })
this.runDetached(desc, session, trimmed)
return 'handled'
}
/** Open the session's popup for one contribution or decoration (menu pick / bare enter). */
private openPopup(
name: string,
ui: CommandContribution['ui'],
session: ClientSessionContext,
segment: TokenSegment,
): void {
const actx = this.scopeFor(session.sessionId)
if (actx === undefined) return
this.popupFor(actx).open(name, ui, session, segment)
}
/** Build the leadingInput claim: token `/name ` + the command.execute submit transaction. */
private leadingClaim(desc: CommandDescriptor, session: ClientSessionContext): CommandClaim {
const token = `/${desc.name} `
return {
token,
...(desc.input !== undefined ? { hint: desc.input.hint } : {}),
submit: (args, _actx) => this.execute(session, token + args),
}
}
/**
* The command.execute transaction, addressed to the session's agent — pure
* admission semantics. An unmatched line reports an error outcome (the
* composer's immediate admission feedback); an admitted command reports
* plain success regardless of its handler outcome, because the host
* executor durably logged the lifecycle (`command/run`/`command/done`) and
* the outcome renders as a persistent flow node — the composer never
* echoes it. Transport failures throw.
*/
private async execute(
session: ClientSessionContext,
line: string,
): Promise<SubmitOutcome> {
const result = await this.ctx.remote.commands.execute(session.sessionId, line)
if (!result.ok) throw new Error(`command.execute failed: ${result.error.code}: ${result.error.message}`)
if (result.value === undefined) return { kind: 'error', text: `unknown or malformed command: ${line}` }
this.notifyExecuted(session.sessionId, submittedCommandName(line), result.value.result)
return { kind: 'success' }
}
/** Publish the local acknowledgment without letting an observer change command admission. */
private notifyExecuted(sessionId: SessionId, name: string, result: CommandResult): void {
const args = ['command/executed', sessionId, name, result]
for (const listener of this.ctx.events.dispatch('emit', args) as Array<(...listenerArgs: unknown[]) => unknown>) {
try {
const returned = listener(sessionId, name, result)
if (returned != null && typeof (returned as PromiseLike<unknown>).then === 'function') {
void Promise.resolve(returned as PromiseLike<unknown>).then(undefined, (error: unknown) => {
this.warnExecutedListenerFailure(name, error)
})
}
} catch (error) {
this.warnExecutedListenerFailure(name, error)
}
}
}
/** Log one contained `command/executed` observer failure. */
private warnExecutedListenerFailure(name: string, error: unknown): void {
this.ctx.logger.warn('client command: a command/executed listener for "%s" failed', name)
this.ctx.logger.warn(error)
}
/**
* Fire-and-forget execute for the internal ('handled') paths. Outcomes are
* NOT surfaced here: the host executor durably logs the command lifecycle
* (`command/run`/`command/done`), and the mux-broadcast events render as a
* persistent flow node on every tab. Only a transport/admission failure —
* which never entered a handler and therefore never logged — falls back to
* the composer notice as immediate feedback.
*/
private runDetached(desc: CommandDescriptor, session: ClientSessionContext, line: string): void {
void this.execute(session, line).then(
(outcome) => {
// matched:false maps to an error outcome with no logged lifecycle.
if (outcome.kind === 'error') this.noticeFor(session.sessionId, 'error', outcome.text ?? `/${desc.name} failed`)
},
(error: unknown) => {
this.noticeFor(session.sessionId, 'error', error instanceof Error ? error.message : String(error))
},
)
}
/** Dispatch a consume-token event to one session (menu-pick / bare-enter execute paths). */
private consumeVia(id: SessionId, segment: TokenSegment): void {
const actx = this.scopeFor(id)
if (actx === undefined) return
actx.bail(actx, 'slash/input-consume-token', {
guard: segment.via === 'menu'
? { kind: 'span', span: segment.span }
: { kind: 'bare-token', token: segment.token },
})
}
/** Route an admission/transport failure to the session's composer notice channel (scope gone = attempt died with it). */
private noticeFor(id: SessionId, level: 'info' | 'error', text: string): void {
const actx = this.scopeFor(id)
if (actx === undefined) return
const conversation = actx.get('conversation')
if (conversation === undefined) return
conversation.input.for(actx).notify(level, text)
}
/** id → actx interchange (registered exchange point: this service coordinates for projection-only sources). */
private scopeFor(id: SessionId): ClientContext | undefined {
return this.sessions().scope(id)
}
private sessions(): ISessions {
const sessions = this.ctx.get('sessions')
if (sessions === undefined) throw new Error('ui-commands: sessions service unavailable')
return sessions
}
}

View File

@@ -0,0 +1,6 @@
declare module '*.module.css' {
const classes: Record<string, string>
export default classes
}
declare module '*.css'

View File

@@ -0,0 +1,10 @@
/**
* Command UI plugin, node half. Pure UI plugin: the empty apply exists so
* the plugin appears in the host cordis.yml / Loader; the browser half ships
* via exports["./client"], discovered through the package.json dsh.client
* declaration. The host command registry itself mounts separately
* (bootHost + CommandUiRuntime).
*/
/** Host plugin body — no host-side behavior for the command UI plugin. */
export function apply(): void {}

View File

@@ -0,0 +1,31 @@
/**
* Package-owned invariant companion for `@deepseek-ai/dsh-client-ui-commands`.
* @module @deepseek-ai/dsh-client-ui-commands/invariant
*/
/* jscpd:ignore-start */
import type { Context } from '@deepseek-ai/cordis'
import type { InvariantInstaller } from '@deepseek-ai/dsh-invariants'
const PACKAGE_NAME = '@deepseek-ai/dsh-client-ui-commands'
/** Cordis companion plugin name. */
export const name = 'client-ui-commands-invariant'
/** Service required before the companion can reserve package ownership. */
export const inject = ['invariants']
/**
* No runtime invariant: a browser-side source over the wire command
* directory — it emits no cordis events and owns no cross-plugin mutable
* state; dispatch and cache behavior are asserted by this package's specs.
*/
const install: InvariantInstaller = () => {}
/**
* Register this package's invariant companion.
* @param ctx - Cordis context carrying the invariant service.
* @returns the installed registration's disposer after setup succeeds.
*/
export const apply = (ctx: Context): Promise<() => void> =>
Promise.resolve(ctx.invariants.register(PACKAGE_NAME, install))
/* jscpd:ignore-end */

View File

@@ -0,0 +1,84 @@
/**
* ui-commands browser half on a real cordis Context with fake slash/slots
* faces and real session scopes: the plugin body mounts CommandUiRuntime as
* `command`, the popupSelect shell registers into conversation.input.overlay
* through slot declaration injection with a per-session inject (sessionId →
* scope → popupFor; unknown id fails loud), both fold up on fiber disposal
* (HMR safety), and the service satisfies the frozen CommandUiContract.
*/
import { Context } from '@deepseek-ai/cordis'
import { describe, expect, it } from 'vitest'
import { createScope, scopeOf, SlotRegistry } from '@deepseek-ai/dsh-client-runtime/client'
import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
import type { InputTriggerSource } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
import type { CommandUiContract } from '../src/client/contract.ts'
import type { PopupSelectInjected } from '../src/client/PopupSelectView.tsx'
import { LocaleRuntime } from '@deepseek-ai/dsh-client-locale/client'
import { apply, CommandUiRuntime, inject } from '../src/client/index.ts'
const sid = (k: string): SessionId => k as SessionId
async function bench() {
const ctx = new Context()
const sources = new Map<string, InputTriggerSource>()
ctx.provide('inputTriggers', {
registerSource(src: InputTriggerSource) {
sources.set(`${src.trigger} ${src.name}`, src)
return () => { sources.delete(`${src.trigger} ${src.name}`) }
},
})
const scopes = new Map<SessionId, Context>()
ctx.provide('sessions', {
scope: (id: SessionId) => scopes.get(id),
scopeOf: (c: Context) => scopeOf(c),
})
const commandsRemote = { list: () => Promise.resolve([]) }
// The service subscribes its cache-invalidation events on construction, so
// the Remote face needs `$on` even where this spec dispatches none.
ctx.provide('remote', { commands: commandsRemote, $on: () => () => {} })
ctx.provide('remote.commands', commandsRemote)
await ctx.plugin(SlotRegistry).await()
ctx.slots.register({
name: 'root', children: { 'conversation.input.overlay': { kind: 'list', scope: 'session' } },
} as never, (() => null) as never)
ctx.provide('locale', new LocaleRuntime(ctx))
const fiber = ctx.plugin({ inject: [...inject], apply })
await fiber.await()
const mint = (key: string) => {
const handle = createScope(ctx, sid(key))
scopes.set(sid(key), handle.ctx)
return handle
}
return { ctx, fiber, sources, slots: ctx.slots, mint }
}
describe('apply', () => {
it('declares the services it binds', () => {
expect(inject).toEqual(['inputTriggers', 'sessions', 'remote', 'remote.commands', 'locale'])
})
it('mounts ctx.commandUi, registers the source and the overlay entry, and folds up on disposal', async () => {
const { ctx, fiber, sources, slots } = await bench()
const command = ctx.get('commandUi')
expect(command).toBeInstanceOf(CommandUiRuntime)
// Frozen-contract conformance (compile-time check rides the assignment).
const contract: CommandUiContract = command as CommandUiRuntime
expect(typeof contract.register).toBe('function')
expect(typeof contract.popupFor).toBe('function')
expect([...sources.keys()]).toEqual(['/ command'])
expect(slots.entries('conversation.input.overlay').map(entry => entry.options.id)).toEqual(['command-popup'])
await fiber.dispose()
expect(sources.size).toBe(0)
expect(slots.entries('conversation.input.overlay')).toHaveLength(0)
})
it('the overlay inject resolves the per-session popup controller by sessionId and fails loud on an unknown id', async () => {
const { ctx, slots, mint } = await bench()
const command = ctx.get('commandUi') as CommandUiRuntime
const scope = mint('s1')
const entry = slots.entries('conversation.input.overlay')[0]!
const injectEntry = entry.inject as unknown as (sessionId: SessionId) => PopupSelectInjected
expect(injectEntry(sid('s1')).popup).toBe(command.popupFor(scope.ctx))
expect(() => injectEntry(sid('ghost'))).toThrow(/resolved no scope/)
})
})

View File

@@ -0,0 +1,293 @@
/**
* CommandDirectory unit tests over the session-key axis: per-key status
* transitions and epoch guard, key isolation across sessions, soft
* invalidation (invalidateAll), the reconnect hard reset (resetConnected:
* every entry drops its snapshot and prewarms), the warm hook's cold/failed
* gate, and the per-key ensureReady strong-wait policy.
*/
import { describe, expect, it } from 'vitest'
import type { SessionId } from '@deepseek-ai/dsh-api-remotes/client'
import type { CommandDescriptor } from '../src/client/directory.ts'
import { CommandDirectory } from '../src/client/directory.ts'
const sid = (k: string): SessionId => k as SessionId
const S1 = sid('s1')
const S2 = sid('s2')
function deferred<T>() {
let resolve!: (value: T) => void
let reject!: (reason?: unknown) => void
const promise = new Promise<T>((res, rej) => { resolve = res; reject = rej })
return { promise, resolve, reject }
}
const CMDS: CommandDescriptor[] = [
{ name: 'plan', description: 'plan mode' },
{ name: 'goal', description: 'set goal', input: { hint: 'goal text' } },
]
const S2_CMDS: CommandDescriptor[] = [
...CMDS,
{ name: 'attach', description: 'attach a file', input: { hint: 'path' } },
]
/** Directory over per-key pull queues: each fetch appends a hand-settled deferred. */
function bench() {
const pulls = new Map<SessionId, Array<ReturnType<typeof deferred<readonly CommandDescriptor[]>>>>()
const calls: SessionId[] = []
const dir = new CommandDirectory((key) => {
calls.push(key)
const d = deferred<readonly CommandDescriptor[]>()
const queue = pulls.get(key) ?? []
queue.push(d)
pulls.set(key, queue)
return d.promise
})
const pull = (key: SessionId, i: number) => {
const d = pulls.get(key)?.[i]
if (d === undefined) throw new Error(`no pull #${i} for ${key}`)
return d
}
return { dir, pull, calls, countOf: (key: SessionId) => pulls.get(key)?.length ?? 0 }
}
describe('status and resolve (per key)', () => {
it('starts cold and resolves nothing', () => {
const { dir } = bench()
expect(dir.status(S1)).toBe('cold')
expect(dir.resolve(S1, 'plan')).toBeUndefined()
})
it('serves exact-name lookups once ready, undefined for unknown names', async () => {
const { dir, pull } = bench()
const refreshed = dir.refresh(S1)
expect(dir.status(S1)).toBe('pending')
pull(S1, 0).resolve(CMDS)
await refreshed
expect(dir.status(S1)).toBe('ready')
expect(dir.resolve(S1, 'goal')).toEqual(CMDS[1])
expect(dir.resolve(S1, 'nope')).toBeUndefined()
})
it('drops the snapshot and records failure on a failed pull', async () => {
const { dir, pull } = bench()
const refreshed = dir.refresh(S1)
pull(S1, 0).reject(new Error('boom'))
await refreshed
expect(dir.status(S1)).toBe('failed')
expect(dir.resolve(S1, 'plan')).toBeUndefined()
})
it('keys are isolated: one session catalog landing leaves another cold', async () => {
const { dir, pull } = bench()
const refreshed = dir.refresh(S1)
pull(S1, 0).resolve(CMDS)
await refreshed
expect(dir.status(S2)).toBe('cold')
expect(dir.resolve(S2, 'plan')).toBeUndefined()
const other = dir.refresh(S2)
pull(S2, 0).resolve(S2_CMDS)
await other
expect(dir.resolve(S2, 'attach')).toBeDefined()
expect(dir.resolve(S1, 'attach')).toBeUndefined()
})
})
describe('epoch guard (per key)', () => {
it('a superseded pull cannot overwrite the newer one (old resolves after new)', async () => {
const { dir, pull } = bench()
const first = dir.refresh(S1)
const second = dir.refresh(S1)
pull(S1, 1).resolve(CMDS)
await second
expect(dir.resolve(S1, 'plan')).toBeDefined()
pull(S1, 0).resolve([{ name: 'stale', description: 'old world' }])
await first
expect(dir.resolve(S1, 'stale')).toBeUndefined()
expect(dir.resolve(S1, 'plan')).toBeDefined()
})
it('a superseded failure cannot demote the newer success', async () => {
const { dir, pull } = bench()
const first = dir.refresh(S1)
const second = dir.refresh(S1)
pull(S1, 1).resolve(CMDS)
await second
pull(S1, 0).reject(new Error('late failure'))
await first
expect(dir.status(S1)).toBe('ready')
expect(dir.resolve(S1, 'plan')).toBeDefined()
})
it('epochs are per key: one session supersede leaves another session epoch alone', async () => {
const { dir, pull } = bench()
const one = dir.refresh(S1)
void dir.refresh(S2)
void dir.refresh(S2) // supersedes the s2 pull only
pull(S1, 0).resolve(CMDS)
await one
expect(dir.status(S1)).toBe('ready')
})
})
describe('invalidateAll (commands-changed soft)', () => {
it('repulls every touched key in the background while ready snapshots keep serving', async () => {
const { dir, pull, countOf } = bench()
const a = dir.refresh(S1)
const b = dir.refresh(S2)
pull(S1, 0).resolve(CMDS)
pull(S2, 0).resolve(S2_CMDS)
await Promise.all([a, b])
dir.invalidateAll()
expect(countOf(S1)).toBe(2)
expect(countOf(S2)).toBe(2)
expect(dir.status(S1)).toBe('ready')
expect(dir.resolve(S2, 'attach')).toBeDefined()
pull(S1, 1).resolve([{ name: 'fresh', description: 'new world' }])
await Promise.resolve()
await Promise.resolve()
expect(dir.resolve(S1, 'fresh')).toBeDefined()
expect(dir.resolve(S1, 'plan')).toBeUndefined()
})
it('an untouched directory invalidates to nothing (no keys, no pulls)', () => {
const { dir, calls } = bench()
dir.invalidateAll()
expect(calls).toEqual([])
})
})
describe('resetConnected (reconnect hard)', () => {
it('every entry drops its snapshot immediately and prewarms', async () => {
const { dir, pull, countOf } = bench()
const a = dir.refresh(S1)
const b = dir.refresh(S2)
pull(S1, 0).resolve(CMDS)
pull(S2, 0).resolve(S2_CMDS)
await Promise.all([a, b])
dir.resetConnected()
// Hard: the agent world may have changed shape across the generation.
expect(dir.status(S1)).toBe('pending')
expect(dir.resolve(S1, 'plan')).toBeUndefined()
expect(dir.status(S2)).toBe('pending')
expect(dir.resolve(S2, 'attach')).toBeUndefined()
expect(countOf(S1)).toBe(2)
expect(countOf(S2)).toBe(2)
pull(S1, 1).resolve(CMDS)
pull(S2, 1).resolve(S2_CMDS)
await Promise.resolve()
await Promise.resolve()
expect(dir.status(S1)).toBe('ready')
expect(dir.resolve(S2, 'attach')).toBeDefined()
})
})
describe('warm', () => {
it('launches a pull from cold, again after failure, and never over pending/ready', async () => {
const { dir, pull, countOf } = bench()
dir.warm(S1)
expect(countOf(S1)).toBe(1)
dir.warm(S1) // pending → no second pull
expect(countOf(S1)).toBe(1)
pull(S1, 0).reject(new Error('boom'))
await Promise.resolve()
await Promise.resolve()
expect(dir.status(S1)).toBe('failed')
dir.warm(S1) // failed → retry
expect(countOf(S1)).toBe(2)
pull(S1, 1).resolve(CMDS)
await Promise.resolve()
await Promise.resolve()
dir.warm(S1) // ready → no-op
expect(countOf(S1)).toBe(2)
})
it('warms keys independently', () => {
const { dir, countOf } = bench()
dir.warm(S2)
expect(countOf(S2)).toBe(1)
expect(countOf(S1)).toBe(0)
})
})
describe('ensureReady (per key)', () => {
const signal = () => new AbortController().signal
it('returns the hot snapshot at once when ready', async () => {
const { dir, pull, countOf } = bench()
const warm = dir.refresh(S1)
pull(S1, 0).resolve(CMDS)
await warm
await expect(dir.ensureReady(S1, signal())).resolves.toEqual(CMDS)
expect(countOf(S1)).toBe(1)
})
it('launches a pull from cold and resolves on arrival, without touching other keys', async () => {
const { dir, pull, countOf } = bench()
const wait = dir.ensureReady(S2, signal())
expect(dir.status(S2)).toBe('pending')
pull(S2, 0).resolve(S2_CMDS)
await expect(wait).resolves.toEqual(S2_CMDS)
expect(countOf(S1)).toBe(0)
})
it('joins a flying pull instead of starting a second one', async () => {
const { dir, pull, countOf } = bench()
void dir.refresh(S1)
const wait = dir.ensureReady(S1, signal())
expect(countOf(S1)).toBe(1)
pull(S1, 0).resolve(CMDS)
await expect(wait).resolves.toEqual(CMDS)
})
it('rejects when the awaited pull fails (no silent downgrade)', async () => {
const { dir, pull } = bench()
const wait = dir.ensureReady(S1, signal())
pull(S1, 0).reject(new Error('warmup boom'))
await expect(wait).rejects.toThrow('command directory warmup failed: warmup boom')
})
it('retries from failed state with a fresh pull', async () => {
const { dir, pull } = bench()
const first = dir.ensureReady(S1, signal())
pull(S1, 0).reject(new Error('boom'))
await expect(first).rejects.toThrow()
const second = dir.ensureReady(S1, signal())
pull(S1, 1).resolve(CMDS)
await expect(second).resolves.toEqual(CMDS)
})
it('rejects on abort while waiting', async () => {
const { dir } = bench()
const ac = new AbortController()
const wait = dir.ensureReady(S1, ac.signal)
ac.abort(new Error('attempt superseded'))
await expect(wait).rejects.toThrow('attempt superseded')
})
it('rejects immediately on an already-aborted signal', async () => {
const { dir, pull } = bench()
const warm = dir.refresh(S1)
pull(S1, 0).reject(new Error('irrelevant'))
await warm
const ac = new AbortController()
ac.abort() // bare abort: the DOMException reason is itself an Error and travels as-is
await expect(dir.ensureReady(S1, ac.signal)).rejects.toThrow(/aborted/)
})
it('keeps waiting across a superseded pull and settles on the winner', async () => {
const { dir, pull } = bench()
const wait = dir.ensureReady(S1, signal())
void dir.refresh(S1) // supersedes pull #0 with pull #1
pull(S1, 0).resolve([{ name: 'stale', description: 'loser' }])
pull(S1, 1).resolve(CMDS)
await expect(wait).resolves.toEqual(CMDS)
})
})

View File

@@ -0,0 +1,254 @@
// @vitest-environment jsdom
/**
* PopupSelectView interaction spec: the search input takes
* focus on open and plain typing filters locally, ↑↓ move the filtered
* highlight while ←→ stay native to the input, Enter selects single-flight,
* Escape dismisses back through focusComposer, outside pointerdown dismisses
* plainly, the submitting/failed states render pending text and a working
* retry button, the highlighted row scrolls into view, and the card height
* clamps to the space above the composer.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
import type { SelectOption } from '../src/client/contract.ts'
import type { PopupSpec, TokenSegment } from '../src/client/popup.ts'
import { PopupSelectController } from '../src/client/popup.ts'
import { PopupSelectView } from '../src/client/PopupSelectView.tsx'
import { makeTranslate } from '@deepseek-ai/dsh-client-test-runtime'
import { zh as commonZh } from '@deepseek-ai/dsh-client-locale/src/locales/zh.ts'
import { zh } from '../src/client/locales.ts'
// The framework-injected t seat, stubbed over the zh dictionaries (the default locale).
const t: Parameters<typeof PopupSelectView>[0]['t'] = makeTranslate(zh, commonZh)
// jsdom has no scrollIntoView; the view calls it on the highlighted row.
const scrollIntoView = vi.fn()
beforeEach(() => {
Element.prototype.scrollIntoView = scrollIntoView
scrollIntoView.mockClear()
})
afterEach(() => {
cleanup()
vi.restoreAllMocks()
})
const OPTIONS: SelectOption[] = [
{ id: 'dark', label: 'Dark' },
{ id: 'light', label: 'Light', active: true },
{ id: 'sepia', label: 'Sepia', detail: 'warm' },
]
const GATED: SelectOption = {
id: 'full',
label: 'Full access',
confirmation: {
title: 'Enable Full access?',
description: 'Sensitive operations.',
acknowledgeLabel: 'I understand the risks',
cancelLabel: 'Cancel',
confirmLabel: 'Enable Full access',
},
}
const SEGMENT: TokenSegment = { via: 'enter', token: '/theme' }
function spec(overrides: Partial<PopupSpec<string>> = {}): PopupSpec<string> {
return {
options: () => Promise.resolve(OPTIONS),
onSelect: () => undefined,
...overrides,
}
}
async function mountOpen(overrides: Partial<PopupSpec<string>> = {}, consumeResult = true) {
const consume = vi.fn((_segment: TokenSegment) => consumeResult)
const focusComposer = vi.fn()
const popup = new PopupSelectController<string>({ consume, focusComposer })
const view = render(<PopupSelectView popup={popup} t={t} />)
await act(async () => {
popup.open('theme', spec(overrides), 'ctx-A', SEGMENT)
await Promise.resolve()
})
return { popup, view, consume, focusComposer, search: screen.getByRole('textbox', { name: '筛选选项' }) }
}
function rowLabels(): string[] {
return screen.getAllByRole('option').map(o => o.querySelector('span')!.textContent)
}
describe('PopupSelectView', () => {
it('renders null while closed, opens with focus in the search input', async () => {
const popup = new PopupSelectController<string>({ consume: () => true, focusComposer: () => {} })
const view = render(<PopupSelectView popup={popup} t={t} />)
expect(view.container.childElementCount).toBe(0)
await act(async () => {
popup.open('theme', spec(), 'ctx-A', SEGMENT)
await Promise.resolve()
})
const search = screen.getByRole('textbox', { name: '筛选选项' })
expect(document.activeElement).toBe(search)
expect(rowLabels()).toEqual(['Dark', 'Light', 'Sepia'])
})
it('typing filters rows locally and rebases the highlight', async () => {
const options = vi.fn(() => Promise.resolve(OPTIONS))
const { search } = await mountOpen({ options })
act(() => { fireEvent.change(search, { target: { value: 'li' } }) })
expect(rowLabels()).toEqual(['Light'])
expect(screen.getByRole('option').getAttribute('aria-selected')).toBe('true')
expect(options).toHaveBeenCalledTimes(1)
act(() => { fireEvent.change(search, { target: { value: 'zzz' } }) })
expect(screen.queryByRole('option')).toBeNull()
expect(screen.queryByText('无选项')).not.toBeNull()
})
it('ArrowUp/Down move the filtered highlight; ArrowLeft/Right are left to the native caret', async () => {
const { search } = await mountOpen()
act(() => { fireEvent.keyDown(search, { key: 'ArrowDown' }) })
let options = screen.getAllByRole('option')
expect(options[1]!.getAttribute('aria-selected')).toBe('true')
act(() => { fireEvent.keyDown(search, { key: 'ArrowUp' }) })
options = screen.getAllByRole('option')
expect(options[0]!.getAttribute('aria-selected')).toBe('true')
// fireEvent returns false when preventDefault was called: arrow left/right must NOT be intercepted.
expect(fireEvent.keyDown(search, { key: 'ArrowLeft' })).toBe(true)
expect(fireEvent.keyDown(search, { key: 'ArrowRight' })).toBe(true)
})
it('scrolls the highlighted row into view when the highlight moves', async () => {
const { search } = await mountOpen()
scrollIntoView.mockClear()
act(() => { fireEvent.keyDown(search, { key: 'ArrowDown' }) })
const options = screen.getAllByRole('option')
expect(scrollIntoView).toHaveBeenCalledWith({ block: 'nearest' })
expect(scrollIntoView.mock.instances.at(-1)).toBe(options[1])
})
it('caps the card height at the design maximum when the composer sits low enough', async () => {
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({ bottom: 800 } as DOMRect)
await mountOpen()
expect(screen.getByLabelText('/theme 选项').style.maxHeight).toBe('320px')
})
it('clamps the card height to the space above the composer minus the safe margin', async () => {
vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({ bottom: 200 } as DOMRect)
await mountOpen()
expect(screen.getByLabelText('/theme 选项').style.maxHeight).toBe('188px')
})
it('Enter selects the highlighted row: onSelect, consume, close, focusComposer', async () => {
const seen: Array<{ option: SelectOption; context: string }> = []
const { view, search, consume, focusComposer } = await mountOpen({
onSelect: (option, context) => { seen.push({ option, context }) },
})
act(() => { fireEvent.keyDown(search, { key: 'ArrowDown' }) })
await act(async () => { fireEvent.keyDown(search, { key: 'Enter' }) })
expect(seen).toEqual([{ option: OPTIONS[1], context: 'ctx-A' }])
expect(consume).toHaveBeenCalledExactlyOnceWith(SEGMENT)
expect(focusComposer).toHaveBeenCalledTimes(1)
expect(view.container.childElementCount).toBe(0)
})
it('click selects a row; mouseenter moves the highlight', async () => {
const seen: SelectOption[] = []
const { view } = await mountOpen({ onSelect: (option) => { seen.push(option) } })
const options = screen.getAllByRole('option')
act(() => { fireEvent.mouseEnter(options[2]!) })
expect(screen.getAllByRole('option')[2]!.getAttribute('aria-selected')).toBe('true')
await act(async () => { fireEvent.click(options[2]!) })
expect(seen).toEqual([OPTIONS[2]])
expect(view.container.childElementCount).toBe(0)
})
it('renders a gated option as an in-page modal and requires the checkbox before onSelect', async () => {
const onSelect = vi.fn()
const { popup, consume } = await mountOpen({
options: () => Promise.resolve([GATED]),
onSelect,
})
await act(async () => { fireEvent.click(screen.getByRole('option', { name: 'Full access' })) })
expect(screen.queryByLabelText('/theme 选项')).toBeNull()
expect(screen.getByRole('dialog', { name: 'Enable Full access?' })).toBeTruthy()
const enable = screen.getByRole('button', { name: 'Enable Full access' }) as HTMLButtonElement
expect(enable.disabled).toBe(true)
expect(onSelect).not.toHaveBeenCalled()
fireEvent.click(screen.getByRole('checkbox', { name: 'I understand the risks' }))
expect(enable.disabled).toBe(false)
await act(async () => { fireEvent.click(enable) })
expect(onSelect).toHaveBeenCalledExactlyOnceWith(GATED, 'ctx-A')
expect(consume).toHaveBeenCalledExactlyOnceWith(SEGMENT)
expect(popup.state.getSnapshot().open).toBe(false)
})
it('canceling a gated option returns to the picker with acknowledgement reset', async () => {
await mountOpen({ options: () => Promise.resolve([GATED]) })
await act(async () => { fireEvent.click(screen.getByRole('option', { name: 'Full access' })) })
fireEvent.click(screen.getByRole('checkbox'))
fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
expect(screen.getByLabelText('/theme 选项')).toBeTruthy()
await act(async () => { fireEvent.click(screen.getByRole('option', { name: 'Full access' })) })
expect(screen.getByRole<HTMLInputElement>('checkbox').checked).toBe(false)
})
it('submitting shows pending, locks the search input, and further Enter/click no-op', async () => {
let release!: () => void
const onSelect = vi.fn(() => new Promise<void>((resolve) => { release = resolve }))
const { search, consume } = await mountOpen({ onSelect })
await act(async () => { fireEvent.keyDown(search, { key: 'Enter' }) })
expect(screen.queryByText('正在应用…')).not.toBeNull()
expect((search as HTMLInputElement).readOnly).toBe(true)
await act(async () => {
fireEvent.keyDown(search, { key: 'Enter' })
fireEvent.click(screen.getAllByRole('option')[1]!)
})
expect(onSelect).toHaveBeenCalledTimes(1)
await act(async () => {
release()
await Promise.resolve()
})
expect(consume).toHaveBeenCalledTimes(1)
})
it('a failed options load shows the error with a retry button that reloads', async () => {
let attempts = 0
await mountOpen({
options: () => {
attempts += 1
return attempts === 1 ? Promise.reject(new Error('directory down')) : Promise.resolve(OPTIONS)
},
})
expect(screen.getByRole('alert').textContent).toContain('directory down')
await act(async () => {
fireEvent.click(screen.getByRole('button', { name: '重试' }))
await Promise.resolve()
})
expect(attempts).toBe(2)
expect(rowLabels()).toEqual(['Dark', 'Light', 'Sepia'])
})
it('an onSelect failure keeps the shell open with the error strip and no retry button (re-select is the retry)', async () => {
const { search, consume } = await mountOpen({ onSelect: () => Promise.reject(new Error('host rejected')) })
await act(async () => { fireEvent.keyDown(search, { key: 'Enter' }) })
expect(screen.getByRole('alert').textContent).toContain('host rejected')
expect(screen.queryByRole('button', { name: '重试' })).toBeNull()
expect(consume).not.toHaveBeenCalled()
expect(screen.getAllByRole('option').length).toBe(3)
})
it('Escape dismisses and restores composer focus', async () => {
const { view, search, focusComposer } = await mountOpen()
act(() => { fireEvent.keyDown(search, { key: 'Escape' }) })
expect(view.container.childElementCount).toBe(0)
expect(focusComposer).toHaveBeenCalledTimes(1)
})
it('an outside pointerdown dismisses without focusComposer; an inside one does not dismiss', async () => {
const { view, focusComposer } = await mountOpen()
act(() => { fireEvent.pointerDown(screen.getAllByRole('option')[0]!) })
expect(view.container.childElementCount).not.toBe(0)
act(() => { fireEvent.pointerDown(document.body) })
expect(view.container.childElementCount).toBe(0)
expect(focusComposer).not.toHaveBeenCalled()
})
})

View File

@@ -0,0 +1,399 @@
/**
* PopupSelectController behavior: one options load per
* open with local search filtering, filtered highlight movement,
* single-flight select with open-time context, consume-on-success (CAS miss
* benign), failure-keeps-open retry semantics for both options and onSelect,
* and binding-identity revocation of late settlements after
* dismiss/reopen/dispose.
*/
import { describe, expect, it, vi } from 'vitest'
import type { SelectOption } from '../src/client/contract.ts'
import type { PopupSpec, TokenSegment } from '../src/client/popup.ts'
import { filterOptions, PopupSelectController } from '../src/client/popup.ts'
interface Ctx { readonly session: string }
const CTX_A: Ctx = { session: 'A' }
const OPTIONS: SelectOption[] = [
{ id: 'dark', label: 'Dark' },
{ id: 'light', label: 'Light', active: true },
{ id: 'sepia', label: 'Sepia', detail: 'warm' },
]
const GATED: SelectOption = {
id: 'full',
label: 'Full access',
confirmation: {
title: 'Enable Full access?',
description: 'Sensitive operations.',
acknowledgeLabel: 'I understand',
cancelLabel: 'Cancel',
confirmLabel: 'Enable Full access',
},
}
const SEGMENT: TokenSegment = { via: 'enter', token: '/theme' }
function spec(overrides: Partial<PopupSpec<Ctx>> = {}): PopupSpec<Ctx> {
return {
options: () => Promise.resolve(OPTIONS),
onSelect: () => undefined,
...overrides,
}
}
/** Fake session wiring: records consume/focus calls; consume answer is settable per test. */
function makeDeps(consumeResult = true) {
const consume = vi.fn((_segment: TokenSegment) => consumeResult)
const focusComposer = vi.fn()
return { consume, focusComposer }
}
async function readyPopup(overrides: Partial<PopupSpec<Ctx>> = {}, deps = makeDeps()) {
const popup = new PopupSelectController<Ctx>(deps)
popup.open('theme', spec(overrides), CTX_A, SEGMENT)
await Promise.resolve()
return { popup, deps }
}
describe('filterOptions', () => {
it('matches case-insensitively over label and detail; blank keeps all', () => {
expect(filterOptions(OPTIONS, '')).toBe(OPTIONS)
expect(filterOptions(OPTIONS, ' ')).toBe(OPTIONS)
expect(filterOptions(OPTIONS, 'DARK')).toEqual([OPTIONS[0]])
expect(filterOptions(OPTIONS, 'warm')).toEqual([OPTIONS[2]])
expect(filterOptions(OPTIONS, 'nope')).toEqual([])
})
})
describe('open and options load', () => {
it('publishes pending immediately, ready when options land', async () => {
const popup = new PopupSelectController<Ctx>(makeDeps())
let release!: (options: readonly SelectOption[]) => void
popup.open('theme', spec({ options: () => new Promise((resolve) => { release = resolve }) }), CTX_A, SEGMENT)
expect(popup.state.getSnapshot()).toMatchObject({ open: true, command: 'theme', status: 'pending', search: '', submitting: false, error: null })
release(OPTIONS)
await Promise.resolve()
expect(popup.state.getSnapshot()).toMatchObject({ status: 'ready', options: OPTIONS, active: 0 })
})
it('loads options exactly once: search filters locally without re-querying the provider', async () => {
const options = vi.fn(() => Promise.resolve(OPTIONS))
const { popup } = await readyPopup({ options })
popup.setSearch('li')
popup.setSearch('light')
const s = popup.state.getSnapshot()
expect(options).toHaveBeenCalledTimes(1)
expect(s.options).toEqual(OPTIONS) // original array retained; filtering is view-side
expect(s.search).toBe('light')
expect(filterOptions(s.options, s.search)).toEqual([OPTIONS[1]])
})
it('a reopen aborts the old load and drops its late arrival', async () => {
const popup = new PopupSelectController<Ctx>(makeDeps())
let firstSignal!: AbortSignal
let releaseFirst!: (options: readonly SelectOption[]) => void
popup.open('alpha', spec({
options: (_ctx, signal) => {
firstSignal = signal
return new Promise((resolve) => { releaseFirst = resolve })
},
}), CTX_A, SEGMENT)
popup.open('beta', spec(), CTX_A, SEGMENT)
expect(firstSignal.aborted).toBe(true)
releaseFirst([{ id: 'stale', label: 'stale' }])
await Promise.resolve()
const s = popup.state.getSnapshot()
expect(s.command).toBe('beta')
expect(s.options).toEqual(OPTIONS)
})
it('dispose aborts the flying load, clears state, and drops the late arrival', async () => {
const popup = new PopupSelectController<Ctx>(makeDeps())
let signal!: AbortSignal
let release!: (options: readonly SelectOption[]) => void
popup.open('theme', spec({
options: (_ctx, s) => {
signal = s
return new Promise((resolve) => { release = resolve })
},
}), CTX_A, SEGMENT)
popup.dispose()
expect(signal.aborted).toBe(true)
expect(popup.state.getSnapshot().open).toBe(false)
release(OPTIONS)
await Promise.resolve()
expect(popup.state.getSnapshot().open).toBe(false)
})
it('an options failure keeps the shell open with search retained, surfaces the error, and retry reloads', async () => {
let attempts = 0
const { popup } = await readyPopup({
options: () => {
attempts += 1
return attempts === 1 ? Promise.reject(new Error('directory down')) : Promise.resolve(OPTIONS)
},
})
await Promise.resolve()
popup.setSearch('da')
// The failure landed before setSearch (readyPopup awaited); search must survive it and retry.
expect(popup.state.getSnapshot()).toMatchObject({ open: true, status: 'failed', error: 'directory down', search: 'da' })
popup.retry()
expect(popup.state.getSnapshot()).toMatchObject({ status: 'pending', error: null })
await Promise.resolve()
expect(popup.state.getSnapshot()).toMatchObject({ status: 'ready', options: OPTIONS, search: 'da' })
expect(attempts).toBe(2)
})
it('retry is a no-op unless the options load failed', async () => {
const { popup } = await readyPopup()
popup.retry()
expect(popup.state.getSnapshot().status).toBe('ready')
const closed = new PopupSelectController<Ctx>(makeDeps())
closed.retry()
expect(closed.state.getSnapshot().open).toBe(false)
})
})
describe('search / move / highlight over the filtered list', () => {
it('setSearch rebases the highlight to 0 and ignores closed shells and identical text', async () => {
const { popup } = await readyPopup()
popup.move(1)
expect(popup.state.getSnapshot().active).toBe(1)
popup.setSearch('s')
expect(popup.state.getSnapshot()).toMatchObject({ search: 's', active: 0 })
const before = popup.state.getSnapshot()
popup.setSearch('s')
expect(popup.state.getSnapshot()).toBe(before)
const closed = new PopupSelectController<Ctx>(makeDeps())
closed.setSearch('x')
expect(closed.state.getSnapshot().search).toBe('')
})
it('move wraps across the FILTERED rows', async () => {
const { popup } = await readyPopup()
popup.setSearch('a') // Dark, Sepia (detail 'warm' also matches 'a'? label match: Dark, Sepia)
const rows = filterOptions(popup.state.getSnapshot().options, 'a')
expect(rows.length).toBe(2)
popup.move(1)
expect(popup.state.getSnapshot().active).toBe(1)
popup.move(1)
expect(popup.state.getSnapshot().active).toBe(0)
popup.move(-1)
expect(popup.state.getSnapshot().active).toBe(1)
})
it('move is a no-op while pending, closed, or when the filter matches nothing', async () => {
const pending = new PopupSelectController<Ctx>(makeDeps())
pending.open('theme', spec({ options: () => new Promise(() => {}) }), CTX_A, SEGMENT)
pending.move(1)
expect(pending.state.getSnapshot().active).toBe(0)
const closed = new PopupSelectController<Ctx>(makeDeps())
closed.move(1)
expect(closed.state.getSnapshot().active).toBe(0)
const { popup } = await readyPopup()
popup.setSearch('nope')
popup.move(1)
expect(popup.state.getSnapshot().active).toBe(0)
})
it('highlight sets the active filtered row and ignores out-of-range or same-index calls', async () => {
const { popup } = await readyPopup()
popup.highlight(1)
expect(popup.state.getSnapshot().active).toBe(1)
popup.highlight(99)
popup.highlight(-1)
popup.highlight(1)
expect(popup.state.getSnapshot().active).toBe(1)
popup.setSearch('dark') // one filtered row → index 1 now out of range
popup.highlight(1)
expect(popup.state.getSnapshot().active).toBe(0)
})
})
describe('select', () => {
it('gates a confirmed option until acknowledgement, then settles through the original binding', async () => {
const onSelect = vi.fn()
const deps = makeDeps()
const { popup } = await readyPopup({ options: () => Promise.resolve([GATED]), onSelect }, deps)
await popup.select(0)
expect(popup.state.getSnapshot()).toMatchObject({
open: true, confirming: GATED, acknowledged: false, submitting: false,
})
expect(onSelect).not.toHaveBeenCalled()
await popup.confirm()
expect(onSelect).not.toHaveBeenCalled()
popup.acknowledge(true)
await popup.confirm()
expect(onSelect).toHaveBeenCalledExactlyOnceWith(GATED, CTX_A)
expect(deps.consume).toHaveBeenCalledExactlyOnceWith(SEGMENT)
expect(popup.state.getSnapshot().open).toBe(false)
})
it('cancels a confirmation back to the picker without selecting or consuming', async () => {
const onSelect = vi.fn()
const deps = makeDeps()
const { popup } = await readyPopup({ options: () => Promise.resolve([GATED]), onSelect }, deps)
await popup.select(0)
popup.acknowledge(true)
popup.cancelConfirmation()
expect(popup.state.getSnapshot()).toMatchObject({
open: true, confirming: null, acknowledged: false, submitting: false,
})
expect(onSelect).not.toHaveBeenCalled()
expect(deps.consume).not.toHaveBeenCalled()
})
it('runs onSelect with the filtered option and the open-time context, consumes, closes, refocuses', async () => {
const seen: Array<{ option: SelectOption; context: Ctx }> = []
const deps = makeDeps()
const { popup } = await readyPopup({
onSelect: (option, context) => { seen.push({ option, context }) },
}, deps)
popup.setSearch('light')
await popup.select(0)
expect(seen).toEqual([{ option: OPTIONS[1], context: CTX_A }])
expect(deps.consume).toHaveBeenCalledExactlyOnceWith(SEGMENT)
expect(deps.focusComposer).toHaveBeenCalledTimes(1)
expect(popup.state.getSnapshot().open).toBe(false)
})
it('is single-flight: the first call enters submitting, later Enter/click calls no-op', async () => {
let release!: () => void
const onSelect = vi.fn(() => new Promise<void>((resolve) => { release = resolve }))
const deps = makeDeps()
const { popup } = await readyPopup({ onSelect }, deps)
const first = popup.select(0)
expect(popup.state.getSnapshot().submitting).toBe(true)
await popup.select(0)
await popup.select(1)
popup.setSearch('x') // locked while submitting
popup.move(1)
popup.highlight(1)
expect(popup.state.getSnapshot()).toMatchObject({ search: '', active: 0 })
release()
await first
expect(onSelect).toHaveBeenCalledTimes(1)
expect(deps.consume).toHaveBeenCalledTimes(1)
expect(popup.state.getSnapshot().open).toBe(false)
})
it('a consume CAS miss is benign: no retry, still closes and refocuses', async () => {
const deps = makeDeps(false)
const { popup } = await readyPopup({}, deps)
await popup.select(0)
expect(deps.consume).toHaveBeenCalledTimes(1)
expect(deps.focusComposer).toHaveBeenCalledTimes(1)
expect(popup.state.getSnapshot().open).toBe(false)
})
it('an onSelect failure keeps the shell open with search/highlight/token intact, no consumption, and select re-arms', async () => {
let attempts = 0
const deps = makeDeps()
const { popup } = await readyPopup({
onSelect: () => {
attempts += 1
if (attempts === 1) throw new Error('host rejected')
return undefined
},
}, deps)
popup.setSearch('a')
popup.move(1)
await popup.select(1)
expect(popup.state.getSnapshot()).toMatchObject({
open: true, status: 'ready', submitting: false, error: 'host rejected', search: 'a', active: 1,
})
expect(deps.consume).not.toHaveBeenCalled()
await popup.select(1) // retry = selecting again
expect(deps.consume).toHaveBeenCalledExactlyOnceWith(SEGMENT)
expect(popup.state.getSnapshot().open).toBe(false)
})
it('ignores selects while closed, pending, failed, or out of filtered range', async () => {
const closed = new PopupSelectController<Ctx>(makeDeps())
await closed.select(0)
expect(closed.state.getSnapshot().open).toBe(false)
const failedDeps = makeDeps()
const { popup: failed } = await readyPopup({ options: () => Promise.reject(new Error('x')) }, failedDeps)
await failed.select(0)
expect(failedDeps.consume).not.toHaveBeenCalled()
const deps = makeDeps()
const { popup } = await readyPopup({}, deps)
popup.setSearch('dark')
await popup.select(1) // only one filtered row
expect(deps.consume).not.toHaveBeenCalled()
expect(popup.state.getSnapshot().open).toBe(true)
})
it('a dismiss racing a succeeding onSelect revokes it: no consume, no focus, state stays closed', async () => {
let release!: () => void
const deps = makeDeps()
const { popup } = await readyPopup({
onSelect: () => new Promise<void>((resolve) => { release = resolve }),
}, deps)
const selecting = popup.select(0)
popup.dismiss()
release()
await selecting
expect(deps.consume).not.toHaveBeenCalled()
expect(deps.focusComposer).not.toHaveBeenCalled()
expect(popup.state.getSnapshot().open).toBe(false)
})
it('a dispose racing a failing onSelect revokes its error write', async () => {
let reject!: (error: Error) => void
const deps = makeDeps()
const { popup } = await readyPopup({
onSelect: () => new Promise<void>((_resolve, rej) => { reject = rej }),
}, deps)
const selecting = popup.select(0)
popup.dispose()
reject(new Error('late'))
await selecting
expect(popup.state.getSnapshot()).toMatchObject({ open: false, error: null })
expect(deps.consume).not.toHaveBeenCalled()
})
it('a reopen racing a succeeding onSelect keeps the new shell: no consume of the old segment', async () => {
let release!: () => void
const deps = makeDeps()
const { popup } = await readyPopup({
onSelect: () => new Promise<void>((resolve) => { release = resolve }),
}, deps)
const selecting = popup.select(0)
popup.open('other', spec(), CTX_A, { via: 'enter', token: '/other' })
release()
await selecting
await Promise.resolve()
expect(deps.consume).not.toHaveBeenCalled()
expect(popup.state.getSnapshot()).toMatchObject({ open: true, command: 'other' })
})
})
describe('dismiss / dispose', () => {
it('dismiss closes, aborts the flying fetch, and is a no-op when already closed', async () => {
const deps = makeDeps()
const popup = new PopupSelectController<Ctx>(deps)
let signal!: AbortSignal
popup.open('theme', spec({
options: (_ctx, s) => {
signal = s
return new Promise(() => {})
},
}), CTX_A, SEGMENT)
popup.dismiss()
expect(signal.aborted).toBe(true)
expect(popup.state.getSnapshot().open).toBe(false)
expect(deps.focusComposer).not.toHaveBeenCalled() // outside-pointer path: the click's target takes focus
popup.dismiss()
popup.dispose()
expect(popup.state.getSnapshot().open).toBe(false)
})
it('the Escape path restores composer focus explicitly', async () => {
const deps = makeDeps()
const { popup } = await readyPopup({}, deps)
popup.dismiss({ focusComposer: true })
expect(deps.focusComposer).toHaveBeenCalledTimes(1)
expect(popup.state.getSnapshot().open).toBe(false)
})
})

View File

@@ -0,0 +1,749 @@
/**
* CommandUiRuntime tests on a real cordis Context with fake slash/connection
* faces and real session scopes (createScope): session-keyed candidate
* synthesis (host catalog by sessionId + contributions by availability,
* collision fail-loud), the dispatch decision table cell by cell, matchSpace
* hot-key policy, matchEnter strong-wait / reject, the sessionId execute
* payload, the scoped consume-token dispatch, per-session popupFor
* lifecycle, and the directory invalidation event subscriptions.
*/
import { Context } from '@deepseek-ai/cordis'
import { describe, expect, it, vi } from 'vitest'
import type { CommandResult } from '@deepseek-ai/dsh-commands/types'
import { createScope, scopeOf } from '@deepseek-ai/dsh-client-runtime/client'
import type { SessionId } from '@deepseek-ai/dsh-client-runtime/client'
import type { ClientSessionContext, ConsumeTokenRequest, InputTriggerPick, InputTriggerSource } from '@deepseek-ai/dsh-client-ui-input-trigger/client'
import type { CommandContribution, CommandDecoration, CommandUiSpec, SelectOption } from '../src/client/contract.ts'
import type { CommandDescriptor } from '../src/client/directory.ts'
import { CommandUiRuntime } from '../src/client/service.ts'
const sid = (k: string): SessionId => k as SessionId
/** The agent-backed session projection (single state; identity only). */
const proj = (id: string): ClientSessionContext => ({ sessionId: sid(id) })
const S1_CMDS: CommandDescriptor[] = [
{ name: 'plan', description: 'bare kind' },
{ name: 'goal', description: 'leadingInput kind', input: { hint: 'goal text' } },
]
const S2_CMDS: CommandDescriptor[] = [
...S1_CMDS,
{ name: 'attach', description: 'scoped shadow', input: { hint: 'path' } },
]
type ExecuteValue = { matched: boolean; commandId?: string }
interface BenchOptions {
/** Scripted catalog per list payload; default serves the fixed catalogs by session. */
commands?: (payload: { sessionId: SessionId }) => Promise<{ commands: CommandDescriptor[] }>
execute?: (payload: { sessionId: SessionId; line: string }) => Promise<ExecuteValue>
addressed?: SessionId
}
/**
* Fold one programmed answer into the generated Remote face's outcome: a
* resolved value is the ok branch, a rejection is the transport failure the
* carrier reports in the error branch instead of throwing at the caller.
* @param produce - the scripted answer for one Remote method.
* @returns the carried result the service reads.
*/
async function carried<T>(produce: () => Promise<T>) {
try {
return { ok: true as const, value: await produce() }
} catch (error) {
return {
ok: false as const,
error: {
code: 'internal',
message: error instanceof Error ? error.message : String(error),
details: {},
},
}
}
}
async function bench(opts: BenchOptions = {}) {
const ctx = new Context()
const registered = new Map<string, InputTriggerSource>()
const listCalls: Array<{ sessionId: SessionId }> = []
const executeCalls: Array<{ sessionId: SessionId; line: string }> = []
// The service reads the generated commands Remote, which delivers the
// carrier's outcome, so a programmed failure answers the error branch.
const commandsRemote = {
list: async (sessionId: SessionId) => {
listCalls.push({ sessionId })
return await carried(async () => {
const value = await (opts.commands ?? (p => Promise.resolve({
commands: p.sessionId === sid('s2') ? S2_CMDS : S1_CMDS,
})))({ sessionId })
return value.commands
})
},
execute: async (sessionId: SessionId, line: string) => {
executeCalls.push({ sessionId, line })
return await carried(async () => {
const fallback = (): Promise<ExecuteValue> => Promise.resolve({ matched: true })
const value = await (opts.execute ?? fallback)({ sessionId, line })
return value.matched
? { commandId: value.commandId ?? 'fake-command', result: { kind: 'success' as const } }
: undefined
})
},
}
ctx.provide('inputTriggers', {
registerSource(src: InputTriggerSource) {
const key = `${src.trigger} ${src.name}`
registered.set(key, src)
return () => { registered.delete(key) }
},
})
// Real scope tags behind a fake sessions face.
const scopes = new Map<SessionId, { ctx: Context; fiber: { dispose(): Promise<void> } }>()
ctx.provide('sessions', {
scope: (id: SessionId) => scopes.get(id)?.ctx,
scopeOf: (c: Context) => scopeOf(c),
subagentAddress: (id: SessionId) => id === opts.addressed
? { parentSessionId: sid('parent'), childSessionId: id, mode: 'continuable' as const }
: undefined,
})
const forwarded = new Map<string, Array<(...args: never[]) => void>>()
ctx.provide('remote', {
commands: commandsRemote,
$on: (event: string, listener: (...args: never[]) => void) => {
const listeners = forwarded.get(event) ?? []
listeners.push(listener)
forwarded.set(event, listeners)
return () => { forwarded.set(event, listeners.filter(entry => entry !== listener)) }
},
$dispatch: (event: string, args: readonly unknown[]) => {
for (const listener of forwarded.get(event) ?? []) listener(...args as never[])
},
})
ctx.provide('remote.commands', commandsRemote)
const executions: Array<{ sessionId: SessionId; name: string; result: CommandResult }> = []
ctx.on('command/executed', (sessionId, name, result) => {
executions.push({ sessionId, name, result })
})
/** Notices the fake conversation face collected (runDetached routing). */
const notices: Array<{ scope: SessionId | undefined; level: 'info' | 'error'; text: string }> = []
ctx.provide('conversation', {
input: {
for: (actx: Context) => ({
notify: (level: 'info' | 'error', text: string) => {
notices.push({ scope: scopeOf(actx), level, text })
},
}),
},
})
const fiber = ctx.plugin(CommandUiRuntime)
await fiber.await()
const command = ctx.get('commandUi') as CommandUiRuntime
const source = registered.get('/ command')
if (source === undefined) throw new Error('command source not registered')
const mint = (key: string) => {
const handle = createScope(ctx, sid(key))
scopes.set(sid(key), handle)
return handle
}
/** Warm one session's catalog through the source's own candidate pull. */
const warm = async (session: ClientSessionContext) => {
await source.candidates(session, { query: '', position: 'leading', signal: new AbortController().signal })
}
return { ctx, fiber, command, source, mint, warm, listCalls, executeCalls, executions, registered, notices }
}
function menuPick(source: InputTriggerSource, name: string, session: ClientSessionContext, end?: number) {
const pick: InputTriggerPick = {
candidate: { name },
session,
position: 'leading',
via: 'menu',
span: { start: 0, end: end ?? name.length + 1, draftRev: 3 },
}
return source.onPick(pick)
}
const themeUi = (over: Partial<CommandUiSpec> = {}): CommandUiSpec => ({
kind: 'popupSelect',
options: () => Promise.resolve([{ id: 'dark', label: 'Dark' }]),
onSelect: () => undefined,
...over,
})
const themeContribution = (over: Partial<CommandContribution> = {}): CommandContribution => ({
name: 'theme',
description: 'client popup kind',
available: () => true,
ui: themeUi(),
...over,
})
const req = (query: string, position: 'leading' | 'inline' = 'leading') =>
({ query, position, signal: new AbortController().signal })
describe('registration', () => {
it('registers the "/" source with matchSpace/matchEnter/warm hooks and removes it on fiber disposal', async () => {
const { registered, source, fiber } = await bench()
expect(typeof source.matchSpace).toBe('function')
expect(typeof source.matchEnter).toBe('function')
expect(typeof source.warm).toBe('function')
expect([...registered.keys()]).toEqual(['/ command'])
await fiber.dispose()
expect(registered.size).toBe(0)
})
it('the warm hook prewarms the session key: one pull per session, no duplicate over pending', async () => {
const { source, listCalls } = await bench()
source.warm!(proj('s1'))
expect(listCalls).toEqual([{ sessionId: sid('s1') }])
source.warm!(proj('s2'))
expect(listCalls).toEqual([{ sessionId: sid('s1') }, { sessionId: sid('s2') }])
source.warm!(proj('s1')) // s1 already pending → no duplicate pull
expect(listCalls).toHaveLength(2)
})
})
describe('candidates', () => {
it('does not fetch Agent-bound commands for an addressed child', async () => {
const b = await bench({ addressed: sid('child') })
await expect(b.warm(proj('child'))).resolves.toBeUndefined()
expect(b.listCalls).toEqual([])
})
it('pulls the session catalog; fuzzy filter and hint mapping apply', async () => {
const { source, listCalls } = await bench()
const list = await source.candidates(proj('s1'), req('g'))
expect(listCalls).toEqual([{ sessionId: sid('s1') }])
expect(list).toEqual([{ name: 'goal', description: 'leadingInput kind', hint: 'goal text' }])
})
it('matches case-insensitive subsequences and ranks prefixes, boundaries, adjacency, gaps, then source order', async () => {
const commands: CommandDescriptor[] = [
{ name: 'q-xylophone', description: '' },
{ name: 'qx-long', description: '' },
{ name: 'fabulous', description: '' },
{ name: 'foo-bar', description: '' },
{ name: 'zuv', description: '' },
{ name: 'zu1v', description: '' },
{ name: 'yu1v', description: '' },
{ name: 'zu12v', description: '' },
]
const { source } = await bench({ commands: () => Promise.resolve({ commands }) })
const names = async (query: string) => (await source.candidates(proj('s1'), req(query))).map(c => c.name)
await expect(names('QX')).resolves.toEqual(['qx-long', 'q-xylophone'])
await expect(names('fb')).resolves.toEqual(['foo-bar', 'fabulous'])
await expect(names('uv')).resolves.toEqual(['zuv', 'zu1v', 'yu1v', 'zu12v'])
await expect(names('zzz')).resolves.toEqual([])
await expect(names('query-longer-than-every-name')).resolves.toEqual([])
})
it('catalogs are per session: another session pulls its own key', async () => {
const { source, listCalls } = await bench()
const names = (await source.candidates(proj('s2'), req(''))).map(c => c.name)
expect(listCalls).toEqual([{ sessionId: sid('s2') }])
expect(names).toEqual(['plan', 'goal', 'attach'])
})
it('hides leadingInput commands at inline position', async () => {
const { source } = await bench()
const names = (await source.candidates(proj('s1'), req('', 'inline'))).map(c => c.name)
expect(names).toEqual(['plan'])
})
it('merges available contributions and filters unavailable ones with the per-call projection', async () => {
const { command, source } = await bench()
const available = vi.fn((session: ClientSessionContext) => session.sessionId === sid('s1'))
command.register(themeContribution({ available }))
const s1Names = (await source.candidates(proj('s1'), req(''))).map(c => c.name)
expect(s1Names).toEqual(['plan', 'goal', 'theme'])
expect(available).toHaveBeenLastCalledWith(proj('s1'))
const s2Names = (await source.candidates(proj('s2'), req(''))).map(c => c.name)
expect(s2Names).not.toContain('theme')
})
it('contribution rows ride the same fuzzy query filter', async () => {
const { command, source } = await bench()
command.register(themeContribution())
const names = (await source.candidates(proj('s1'), req('tm'))).map(c => c.name)
expect(names).toEqual(['theme'])
})
it('a contribution/host name collision fails loud', async () => {
const { command, source } = await bench()
command.register(themeContribution({ name: 'plan' }))
await expect(source.candidates(proj('s1'), req(''))).rejects.toThrow('collides with a host command')
})
})
describe('decorations (bare-invocation UI on host commands)', () => {
const goalDecoration = (over: Partial<CommandDecoration> = {}): CommandDecoration => ({
name: 'goal',
available: () => true,
ui: themeUi(),
...over,
})
it('adds no catalog row: the host row stands alone', async () => {
const { command, source } = await bench()
command.decorate(goalDecoration())
const names = (await source.candidates(proj('s1'), req(''))).map(c => c.name)
expect(names).toEqual(['plan', 'goal'])
})
it('bare enter opens the popup; an argued line never consults the decoration (host claim)', async () => {
const { command, source, mint, warm } = await bench()
command.decorate(goalDecoration())
const scope = mint('s1')
await warm(proj('s1'))
expect(await source.matchEnter!(proj('s1'), '/goal', new AbortController().signal)).toBe('handled')
expect(command.popupFor(scope.ctx).state.getSnapshot()).toMatchObject({ open: true, command: 'goal' })
const argued = await source.matchEnter!(proj('s1'), '/goal ship it', new AbortController().signal)
if (argued === undefined || argued === 'handled' || !('claim' in argued)) throw new Error('expected the host claim')
expect(argued.claim.token).toBe('/goal ')
})
it('space never consults the decoration (host claim)', async () => {
const { command, source, warm } = await bench()
command.decorate(goalDecoration())
await warm(proj('s1'))
const outcome = source.matchSpace!(proj('s1'), '/goal')
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected the host claim')
expect(outcome.claim.token).toBe('/goal ')
})
it('a decoration with no host row never fires (bare enter misses; menu pick misses)', async () => {
const { command, source, mint, warm } = await bench()
command.decorate(goalDecoration({ name: 'phantom' }))
const scope = mint('s1')
await warm(proj('s1'))
expect(await source.matchEnter!(proj('s1'), '/phantom', new AbortController().signal)).toBeUndefined()
expect(menuPick(source, 'phantom', proj('s1'))).toBeUndefined()
expect(command.popupFor(scope.ctx).state.getSnapshot().open).toBe(false)
})
it('an unavailable decoration falls through to the host bare path (detached execute)', async () => {
const { command, source, warm, executeCalls } = await bench()
command.decorate(goalDecoration({ name: 'plan', available: () => false }))
await warm(proj('s1'))
expect(await source.matchEnter!(proj('s1'), '/plan', new AbortController().signal)).toBe('handled')
expect(executeCalls).toEqual([{ sessionId: sid('s1'), line: '/plan' }])
})
it('duplicate decoration names fail loud', async () => {
const { command } = await bench()
command.decorate(goalDecoration())
expect(() => { command.decorate(goalDecoration()) }).toThrow('duplicate decoration for /goal')
})
})
describe('dispatch (menu column)', () => {
it('contribution → opens the session popup with the open-time projection, no execute', async () => {
const { command, source, mint, warm, executeCalls } = await bench()
const options = vi.fn((_s: ClientSessionContext) => Promise.resolve([{ id: 'dark', label: 'Dark' }]))
command.register(themeContribution({ ui: themeUi({ options }) }))
const scope = mint('s1')
await warm(proj('s1'))
expect(menuPick(source, 'theme', proj('s1'))).toBe('handled')
const popup = command.popupFor(scope.ctx)
expect(popup.state.getSnapshot()).toMatchObject({ open: true, command: 'theme' })
expect(options).toHaveBeenCalledExactlyOnceWith(proj('s1'), expect.any(AbortSignal))
expect(executeCalls).toEqual([])
})
it('an unavailable contribution falls through to the host catalog', async () => {
const { command, source, mint, warm } = await bench()
command.register(themeContribution({ available: () => false }))
const scope = mint('s1')
await warm(proj('s1'))
expect(menuPick(source, 'theme', proj('s1'))).toBeUndefined() // no host 'theme' either
expect(command.popupFor(scope.ctx).state.getSnapshot().open).toBe(false)
})
it('host leadingInput → {claim} with token "/name " and hint; claiming never executes', async () => {
const { source, warm, executeCalls } = await bench()
await warm(proj('s1'))
const outcome = menuPick(source, 'goal', proj('s1'))
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
expect(outcome.claim.token).toBe('/goal ')
expect(outcome.claim.hint).toBe('goal text')
expect(executeCalls).toEqual([])
})
it('host bare → consume-token span guard on the session scope + detached execute', async () => {
const { source, mint, warm, executeCalls, executions } = await bench()
const scope = mint('s1')
const consumes: ConsumeTokenRequest[] = []
scope.ctx.on('slash/input-consume-token', (r) => {
consumes.push(r)
return true
})
await warm(proj('s1'))
expect(menuPick(source, 'plan', proj('s1'), 5)).toBe('handled')
expect(consumes).toEqual([{ guard: { kind: 'span', span: { start: 0, end: 5, draftRev: 3 } } }])
await vi.waitFor(() => {
expect(executeCalls).toEqual([{ sessionId: sid('s1'), line: '/plan' }])
expect(executions).toEqual([{
sessionId: sid('s1'),
name: 'plan',
result: { kind: 'success' },
}])
})
})
it('a name the directory no longer serves → undefined (snapshot swapped between menu and pick)', async () => {
const { source, warm } = await bench()
await warm(proj('s1'))
expect(menuPick(source, 'gone', proj('s1'))).toBeUndefined()
})
})
describe('matchSpace (space column)', () => {
it('answers undefined from a not-ready key (no waiting, no RPC)', async () => {
const { source, listCalls } = await bench()
expect(source.matchSpace!(proj('s1'), '/goal')).toBeUndefined()
expect(listCalls).toEqual([])
})
it('hot leadingInput exact token → {claim}; the key axis is the session', async () => {
const { source, warm } = await bench()
await warm(proj('s2'))
const outcome = source.matchSpace!(proj('s2'), '/attach')
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
expect(outcome.claim.token).toBe('/attach ')
// s1's key is still cold: the same token answers undefined there.
expect(source.matchSpace!(proj('s1'), '/attach')).toBeUndefined()
})
it('bare kind and contribution names stay plain text', async () => {
const { command, source, warm } = await bench()
command.register(themeContribution())
await warm(proj('s1'))
expect(source.matchSpace!(proj('s1'), '/plan')).toBeUndefined()
expect(source.matchSpace!(proj('s1'), '/theme')).toBeUndefined()
})
it('unknown token / non-slash token → undefined', async () => {
const { source, warm } = await bench()
await warm(proj('s1'))
expect(source.matchSpace!(proj('s1'), '/nope')).toBeUndefined()
expect(source.matchSpace!(proj('s1'), 'plan')).toBeUndefined()
})
})
describe('matchEnter (enter column)', () => {
const signal = () => new AbortController().signal
it('strong-waits a cold key before adjudicating', async () => {
let release!: (value: { commands: CommandDescriptor[] }) => void
const { source } = await bench({
commands: () => new Promise((resolve) => { release = resolve }),
})
const wait = source.matchEnter!(proj('s1'), '/goal args', signal())
release({ commands: S1_CMDS })
const outcome = await wait
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
expect(outcome.claim.token).toBe('/goal ')
})
it('rejects when warmup fails (never a silent downgrade)', async () => {
const { source } = await bench({
commands: () => Promise.reject(new Error('warmup boom')),
})
await expect(source.matchEnter!(proj('s1'), '/goal', signal())).rejects.toThrow('warmup boom')
})
it('leadingInput claims args-tolerant (bare and with trailing text)', async () => {
const { source, warm } = await bench()
await warm(proj('s1'))
for (const line of ['/goal', '/goal refactor the loop']) {
const outcome = await source.matchEnter!(proj('s1'), line, signal())
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
expect(outcome.claim.token).toBe('/goal ')
}
})
it('bare host command executes detached with the bare-token consume guard', async () => {
const { source, mint, warm, executeCalls } = await bench()
const scope = mint('s1')
const consumes: ConsumeTokenRequest[] = []
scope.ctx.on('slash/input-consume-token', (r) => {
consumes.push(r)
return true
})
await warm(proj('s1'))
await expect(source.matchEnter!(proj('s1'), '/plan', signal())).resolves.toBe('handled')
expect(consumes).toEqual([{ guard: { kind: 'bare-token', token: '/plan' } }])
await Promise.resolve()
expect(executeCalls).toEqual([{ sessionId: sid('s1'), line: '/plan' }])
})
it('bare kind with trailing text → undefined and no RPC (default sink owns the line)', async () => {
const { source, warm, executeCalls } = await bench()
await warm(proj('s1'))
await expect(source.matchEnter!(proj('s1'), '/plan now', signal())).resolves.toBeUndefined()
expect(executeCalls).toEqual([])
})
it('contribution: bare token opens the popup without touching the directory; args → undefined', async () => {
const { command, source, mint, listCalls } = await bench()
command.register(themeContribution())
const scope = mint('s1')
await expect(source.matchEnter!(proj('s1'), '/theme', signal())).resolves.toBe('handled')
expect(command.popupFor(scope.ctx).state.getSnapshot().open).toBe(true)
expect(listCalls).toEqual([]) // contribution short-circuits ahead of ensureReady
await expect(source.matchEnter!(proj('s1'), '/theme dark', signal())).resolves.toBeUndefined()
})
it('unknown name, bare "/", and non-slash lines → undefined', async () => {
const { source, warm } = await bench()
await warm(proj('s1'))
await expect(source.matchEnter!(proj('s1'), '/nope', signal())).resolves.toBeUndefined()
await expect(source.matchEnter!(proj('s1'), '/', signal())).resolves.toBeUndefined()
await expect(source.matchEnter!(proj('s1'), 'plain text', signal())).resolves.toBeUndefined()
})
})
describe('execute payload', () => {
it('claim.submit addresses the session; admitted outcomes stay off the composer (flow card owns them)', async () => {
const { source, warm, executeCalls, executions } = await bench({
execute: () => Promise.resolve({ matched: true }),
})
await warm(proj('s1'))
const outcome = source.matchSpace!(proj('s1'), '/goal')
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
const settled = await outcome.claim.submit('ship it', new Context())
expect(executeCalls).toEqual([{ sessionId: sid('s1'), line: '/goal ship it' }])
// Pure admission: no outcome text ever rides the submit result — the
// durable command lifecycle events render the outcome in the flow.
expect(settled).toEqual({ kind: 'success' })
expect(executions).toEqual([{
sessionId: sid('s1'),
name: 'goal',
result: { kind: 'success' },
}])
})
it('contains local acknowledgment listeners without changing an admitted result', async () => {
const b = await bench({ execute: () => Promise.resolve({ matched: true }) })
await b.warm(proj('s1'))
const outcome = b.source.matchSpace!(proj('s1'), '/goal')
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
const syncFailure = new Error('sync observer failed')
const asyncFailure = new Error('async observer failed')
const after = vi.fn()
const warn = vi.spyOn(b.ctx.logger, 'warn').mockImplementation(() => undefined)
b.ctx.on('command/executed', () => { throw syncFailure })
const rejectingListener = (() => Promise.reject(asyncFailure)) as unknown as () => void
b.ctx.on('command/executed', rejectingListener)
b.ctx.on('command/executed', after)
await expect(outcome.claim.submit('ship it', new Context())).resolves.toEqual({ kind: 'success' })
expect(after).toHaveBeenCalledOnce()
await Promise.resolve()
await Promise.resolve()
expect(warn).toHaveBeenCalledWith('client command: a command/executed listener for "%s" failed', 'goal')
expect(warn).toHaveBeenCalledWith(syncFailure)
expect(warn).toHaveBeenCalledWith(asyncFailure)
})
it('maps matched:false to an error outcome and a matched bare result to success', async () => {
const claimOf = async (opts: BenchOptions) => {
const b = await bench(opts)
await b.warm(proj('s1'))
const outcome = b.source.matchSpace!(proj('s1'), '/goal')
if (outcome === undefined || outcome === 'handled' || !('claim' in outcome)) throw new Error('expected claim')
return outcome.claim
}
const first = await claimOf({ execute: () => Promise.resolve({ matched: false }) })
const bad = await first.submit('x', new Context())
expect(bad.kind).toBe('error')
const second = await claimOf({ execute: () => Promise.resolve({ matched: true }) })
await expect(second.submit('', new Context())).resolves.toEqual({ kind: 'success' })
})
})
describe('detached admission notices', () => {
const flush = () => new Promise(resolve => setTimeout(resolve, 0))
it('admitted outcomes stay silent; admission miss and transport rejection notice as errors', async () => {
let mode: 'admitted' | 'miss' | 'reject' = 'admitted'
const { source, mint, warm, notices } = await bench({
execute: () => {
if (mode === 'reject') return Promise.reject(new Error('network down'))
return Promise.resolve({ matched: mode === 'admitted' })
},
})
mint('s1')
await warm(proj('s1'))
// Admitted: the durable lifecycle events own the outcome — no notice.
menuPick(source, 'plan', proj('s1'))
await flush()
expect(notices).toEqual([])
// Admission miss (matched:false): immediate composer feedback stays.
mode = 'miss'
await source.matchEnter!(proj('s1'), '/plan', new AbortController().signal)
await flush()
expect(notices).toEqual([{ scope: sid('s1'), level: 'error', text: 'unknown or malformed command: /plan' }])
notices.length = 0
mode = 'reject'
menuPick(source, 'plan', proj('s1'))
await flush()
// A dead Remote call and a rejected one now read alike: both arrive as a
// failed result, so the notice names the endpoint either way.
expect(notices).toEqual([{
scope: sid('s1'),
level: 'error',
text: 'command.execute failed: internal: network down',
}])
})
it('a torn-down scope drops the failure notice', async () => {
const { source, warm, notices } = await bench({
execute: () => Promise.reject(new Error('orphan failure')),
})
await warm(proj('ghost')) // never minted: scopeFor misses
menuPick(source, 'plan', proj('ghost'))
await flush()
expect(notices).toEqual([])
})
})
describe('register (contribution face)', () => {
it('duplicate registration throws; the disposer frees the name', async () => {
const { command } = await bench()
const dispose = command.register(themeContribution())
expect(() => command.register(themeContribution())).toThrow('duplicate contribution')
dispose()
command.register(themeContribution())()
})
})
describe('popupFor', () => {
it('resolves lazily per session; a foreign session gets its own controller; unscoped ctx throws', async () => {
const { ctx, command, mint } = await bench()
const a = mint('s1')
const first = command.popupFor(a.ctx)
expect(command.popupFor(a.ctx)).toBe(first)
expect(command.popupFor(mint('s2').ctx)).not.toBe(first)
expect(() => command.popupFor(ctx)).toThrow('requires a session scope')
})
it('a successful select dispatches the scoped consume-token and fires the bound composer focus', async () => {
const { command, source, mint } = await bench()
const onSelect = vi.fn()
command.register(themeContribution({ ui: themeUi({ onSelect }) }))
const scope = mint('s1')
const consumes: ConsumeTokenRequest[] = []
scope.ctx.on('slash/input-consume-token', (r) => {
consumes.push(r)
return true
})
const focus = vi.fn()
command.bindComposerFocus(sid('s1'), focus)
expect(menuPick(source, 'theme', proj('s1'), 6)).toBe('handled')
const popup = command.popupFor(scope.ctx)
await Promise.resolve() // options land
await popup.select(0)
expect(onSelect).toHaveBeenCalledExactlyOnceWith({ id: 'dark', label: 'Dark' } satisfies SelectOption, proj('s1'))
expect(consumes).toEqual([{ guard: { kind: 'span', span: { start: 0, end: 6, draftRev: 3 } } }])
expect(focus).toHaveBeenCalledTimes(1)
})
it('the enter path opens with the bare-token guard', async () => {
const { command, source, mint } = await bench()
command.register(themeContribution())
const scope = mint('s1')
const consumes: ConsumeTokenRequest[] = []
scope.ctx.on('slash/input-consume-token', (r) => {
consumes.push(r)
return true
})
await source.matchEnter!(proj('s1'), '/theme', new AbortController().signal)
const popup = command.popupFor(scope.ctx)
await Promise.resolve()
await popup.select(0)
expect(consumes).toEqual([{ guard: { kind: 'bare-token', token: '/theme' } }])
})
it('the scope disposer disposes the controller and a re-mint resolves fresh', async () => {
const { command, source, mint } = await bench()
command.register(themeContribution())
const scope = mint('s1')
await source.matchEnter!(proj('s1'), '/theme', new AbortController().signal)
const popup = command.popupFor(scope.ctx)
expect(popup.state.getSnapshot().open).toBe(true)
await scope.fiber.dispose()
expect(popup.state.getSnapshot().open).toBe(false)
expect(command.popupFor(mint('s1').ctx)).not.toBe(popup)
})
})
describe('directory invalidation events', () => {
it('commands/change repulls in the background while the old snapshot serves', async () => {
let round = 0
const { ctx, source, warm } = await bench({
commands: () => {
round += 1
return Promise.resolve({
commands: round === 1
? S1_CMDS
: [{ name: 'fresh', description: '', input: { hint: 'h' } }],
})
},
})
await warm(proj('s1'))
ctx.remote.$dispatch('commands/change', [])
await new Promise(resolve => setTimeout(resolve, 0))
expect(source.matchSpace!(proj('s1'), '/fresh')).not.toBeUndefined()
expect(source.matchSpace!(proj('s1'), '/goal')).toBeUndefined()
})
it('agent-preset/selected repulls the recomposed session and leaves the others served', async () => {
const rounds = new Map<SessionId, number>()
const { ctx, source, warm } = await bench({
commands: (payload) => {
const round = (rounds.get(payload.sessionId) ?? 0) + 1
rounds.set(payload.sessionId, round)
return Promise.resolve({
commands: round === 1
? S1_CMDS
: [{ name: 'fresh', description: '', input: { hint: 'h' } }],
})
},
})
await warm(proj('s1'))
await warm(proj('s2'))
// A preset switch changes which commands one session's agent resolves;
// every other session keeps the catalog its own composition serves.
ctx.remote.$dispatch('agent-preset/selected', [sid('s1'), 'minimal'])
await new Promise(resolve => setTimeout(resolve, 0))
expect(source.matchSpace!(proj('s1'), '/fresh')).not.toBeUndefined()
expect(source.matchSpace!(proj('s1'), '/goal')).toBeUndefined()
expect(source.matchSpace!(proj('s2'), '/goal')).not.toBeUndefined()
})
it('connection/reset hard-drops every session key until its rewarm lands', async () => {
let block = false
let release!: (value: { commands: CommandDescriptor[] }) => void
const { ctx, source, warm } = await bench({
commands: () => (block
? new Promise((resolve) => { release = resolve })
: Promise.resolve({ commands: S2_CMDS })),
})
await warm(proj('s2'))
expect(source.matchSpace!(proj('s2'), '/attach')).not.toBeUndefined()
block = true
ctx.emit('connection/reset')
// Hard reset: silent until the rewarm lands.
expect(source.matchSpace!(proj('s2'), '/attach')).toBeUndefined()
release({ commands: S2_CMDS })
await new Promise(resolve => setTimeout(resolve, 0))
expect(source.matchSpace!(proj('s2'), '/attach')).not.toBeUndefined()
})
})

View File

@@ -0,0 +1,45 @@
{
"extends": "../../../tsconfig.base.client.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib/types"
},
"include": [
"src"
],
"references": [
{
"path": "../../api/remotes/tsconfig.client.json"
},
{
"path": "../../../vendor/cordis"
},
{
"path": "../locale"
},
{
"path": "../runtime"
},
{
"path": "../ui-conversation"
},
{
"path": "../ui-primitives"
},
{
"path": "../ui-input-trigger"
},
{
"path": "../ui-slots"
},
{
"path": "../../interaction/commands"
},
{
"path": "../../runtime-diagnostics/invariants"
},
{
"path": "../../api/remotes/tsconfig.client.json"
}
]
}

View File

@@ -0,0 +1,3 @@
import { clientBundle } from '../tsdown.client.ts'
export default clientBundle('@deepseek-ai/dsh-client-ui-commands', ['lib/types/index.js', 'lib/types/invariant.js'])