refactor(gui): route the composer chain on PendingWait currency
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { useState, type KeyboardEvent } from 'react'
|
||||
import { useMemo, useState, type KeyboardEvent } from 'react'
|
||||
import clsx from 'clsx'
|
||||
import {
|
||||
Button, IconCheckOutline16, IconChevronLeftOutline14, IconChevronRightOutline14,
|
||||
IconCloseOutline16, IconEditOutline16,
|
||||
} from '@deepseek-ai/dsh-client-ui-primitives'
|
||||
import type { QuestionAnswer, QuestionComposerProps } from './contract/slots.ts'
|
||||
import { PendingQuestion, type QuestionAnswer, type QuestionComposerProps } from './contract/slots.ts'
|
||||
import css from './QuestionComposer.module.css'
|
||||
|
||||
interface DraftAnswer {
|
||||
@@ -41,16 +41,20 @@ function isComposing(event: KeyboardEvent<HTMLTextAreaElement>): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Composer takeover boundary; rpcId keys local drafts while same-id replay preserves them.
|
||||
* @param props - Pending interaction and scoped answer/cancel actions.
|
||||
* Composer takeover boundary; the carrier key keys local drafts, so a
|
||||
* same-request replay (same key, new carrier object) preserves them.
|
||||
* @param props - the selector-matched pending question carrier plus the framework standard kit.
|
||||
* @returns The question flow for this request.
|
||||
*/
|
||||
export function QuestionComposer(props: QuestionComposerProps) {
|
||||
return <QuestionFlow key={props.interaction.rpcId} {...props} />
|
||||
// Domain-face mint rides the carrier's stable identity (never minted in a
|
||||
// select/render dispatch — per-dispatch minting would churn memo identity).
|
||||
const question = useMemo(() => new PendingQuestion(props.matched), [props.matched])
|
||||
return <QuestionFlow key={question.key} pending={question} />
|
||||
}
|
||||
|
||||
function QuestionFlow({ interaction, answer: submitAnswer, cancel }: QuestionComposerProps) {
|
||||
const questions = interaction.questions
|
||||
function QuestionFlow({ pending }: { pending: PendingQuestion }) {
|
||||
const questions = pending.questions
|
||||
const [index, setIndex] = useState(0)
|
||||
const [drafts, setDrafts] = useState<DraftAnswer[]>(() => questions.map(question => ({
|
||||
selected: [], custom: '', customOpen: (question.options?.length ?? 0) === 0, skipped: false,
|
||||
@@ -64,7 +68,7 @@ function QuestionFlow({ interaction, answer: submitAnswer, cancel }: QuestionCom
|
||||
const cancelFlow = (): void => {
|
||||
setBusy('cancel')
|
||||
setError(null)
|
||||
void cancel(interaction).catch((cause: unknown) => {
|
||||
void pending.cancel().catch((cause: unknown) => {
|
||||
setBusy(null)
|
||||
setError(cause instanceof Error ? cause.message : String(cause))
|
||||
})
|
||||
@@ -119,7 +123,7 @@ function QuestionFlow({ interaction, answer: submitAnswer, cancel }: QuestionCom
|
||||
}
|
||||
setBusy('answer')
|
||||
setError(null)
|
||||
void submitAnswer(interaction, answer).catch((cause: unknown) => {
|
||||
void pending.answer(answer).catch((cause: unknown) => {
|
||||
setBusy(null)
|
||||
setError(cause instanceof Error ? cause.message : String(cause))
|
||||
})
|
||||
@@ -156,12 +160,12 @@ function QuestionFlow({ interaction, answer: submitAnswer, cancel }: QuestionCom
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={css.frame} data-question-rpc-id={interaction.rpcId}>
|
||||
<section className={css.card} aria-labelledby={`question-${interaction.rpcId}-${String(index)}`}>
|
||||
<div className={css.frame} data-question-key={pending.key}>
|
||||
<section className={css.card} aria-labelledby={`question-${pending.key}-${String(index)}`}>
|
||||
<header className={css.header}>
|
||||
<div className={css.headingBlock}>
|
||||
{question.header !== undefined && <div className={css.eyebrow}>{question.header}</div>}
|
||||
<h2 className={css.title} id={`question-${interaction.rpcId}-${String(index)}`}>
|
||||
<h2 className={css.title} id={`question-${pending.key}-${String(index)}`}>
|
||||
<span>{question.multiSelect === true
|
||||
? parseQuestionTitle(question.question)
|
||||
: question.question}</span>
|
||||
|
||||
@@ -1,42 +1,77 @@
|
||||
/**
|
||||
* Question-composer slot contract: the registrant-side props composition for
|
||||
* the conversation-owned `conversation.composer` keyed slot. The own injected
|
||||
* share is declared here (a share's type lives with whoever wires it); the
|
||||
* runtime share — the owner-dispatched `interaction` plus the framework
|
||||
* session/global standard kit — is PropsRuntime<'conversation.composer'>,
|
||||
* resolved off ui-conversation's SlotMap declaration and never re-stated.
|
||||
* Single domain — this is the package's whole contract surface.
|
||||
* the conversation-owned `conversation.composer` slot, plus the question
|
||||
* domain face over the runtime's carrier object. The carrier (PendingWait)
|
||||
* owns envelope transport only; the question protocol — answer value shape,
|
||||
* cancelled error encoding, receipt checks — lives HERE, with the package
|
||||
* that consumes it.
|
||||
*/
|
||||
import type { PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots'
|
||||
// Also pulls ui-conversation's SlotMap merge (the 'conversation.composer'
|
||||
// entry) into every program that sees this contract, so PropsRuntime resolves.
|
||||
import type { QuestionComposerOwnerProps } from '@deepseek-ai/dsh-client-ui-conversation/client'
|
||||
import type {} from '@deepseek-ai/dsh-client-ui-conversation/client'
|
||||
import type { PendingWait } from '@deepseek-ai/dsh-client-runtime/client'
|
||||
import type { QuestionResponsePayload } from '@deepseek-ai/dsh-client-connection/client'
|
||||
|
||||
/** The pending question interaction the owner dispatches into the keyed slot. */
|
||||
export type QuestionInteraction = QuestionComposerOwnerProps['interaction']
|
||||
/** The pending question carrier the owner dispatches into the composer slot. */
|
||||
export type QuestionWait = PendingWait<'question'>
|
||||
|
||||
/** One structured answer batch covering every question of the request. */
|
||||
export type QuestionAnswer = QuestionResponsePayload['answer']
|
||||
|
||||
/**
|
||||
* Registrant-private injected share (arrives via the register inject
|
||||
* factory): plain session-scoped callbacks only — the question data rides the
|
||||
* owner share and drafts are component-local. A type alias, not an interface:
|
||||
* the alias carries an implicit index signature, so the factory's return
|
||||
* crosses the registry's `Record<string, unknown>` boundary uncast.
|
||||
* Question domain face over the carrier: render identity and questions
|
||||
* transparently forwarded; answer/cancel own the wire encoding (the ok value
|
||||
* shape and the cancelled error) and turn a rejected carrier receipt into a
|
||||
* thrown error. Components mint one per carrier via useMemo (never inside a
|
||||
* select — a per-dispatch mint would churn identity and break memoization).
|
||||
*/
|
||||
export type QuestionComposerInjected = {
|
||||
/** Deliver the whole answer batch; a rejected receipt surfaces as a thrown error. */
|
||||
answer: (interaction: QuestionInteraction, answer: QuestionAnswer) => Promise<void>
|
||||
/** Reject the whole wait (the host resolves the tool call as cancelled). */
|
||||
cancel: (interaction: QuestionInteraction) => Promise<void>
|
||||
export class PendingQuestion {
|
||||
/**
|
||||
* @param wait - the runtime carrier for one pending question request.
|
||||
*/
|
||||
constructor(private readonly wait: QuestionWait) {}
|
||||
|
||||
/** Opaque render identity (React key / draft remount axis), forwarded from the carrier. */
|
||||
get key(): string {
|
||||
return this.wait.key
|
||||
}
|
||||
|
||||
/** The request's question list, forwarded from the carrier payload. */
|
||||
get questions(): QuestionWait['payload']['questions'] {
|
||||
return this.wait.payload.questions
|
||||
}
|
||||
|
||||
/**
|
||||
* Deliver the whole answer batch; a rejected carrier receipt throws.
|
||||
* @param answer - complete structured answer batch.
|
||||
*/
|
||||
async answer(answer: QuestionAnswer): Promise<void> {
|
||||
const receipt = await this.wait.respond({
|
||||
ok: true, value: { sessionId: this.wait.sessionId, answer },
|
||||
})
|
||||
if (!receipt.accepted) {
|
||||
throw new Error(`question response rejected: ${receipt.reason}`)
|
||||
}
|
||||
}
|
||||
|
||||
/** Reject the whole wait (the host resolves the tool call as cancelled); a rejected receipt throws. */
|
||||
async cancel(): Promise<void> {
|
||||
const receipt = await this.wait.respond({
|
||||
ok: false,
|
||||
error: { code: 'cancelled', message: 'the user closed this question request', details: {} },
|
||||
})
|
||||
if (!receipt.accepted) {
|
||||
throw new Error(`question cancellation rejected: ${receipt.reason}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Full component props: the framework runtime share (owner `interaction` +
|
||||
* session/global standard kit) plus the own injected share. No children are
|
||||
* declared and no store is registered, so no PropsRenderSlots/PropsStore
|
||||
* term appears.
|
||||
* Full component props: the framework runtime share (chain currency +
|
||||
* session/global standard kit) plus the chain `matched` share — the entry's
|
||||
* selector result, already narrowed to the question carrier. No injected
|
||||
* share: the carrier plus the domain face above carry the whole behavior
|
||||
* surface.
|
||||
*/
|
||||
export type QuestionComposerProps = PropsRuntime<'conversation.composer'> & QuestionComposerInjected
|
||||
export type QuestionComposerProps = PropsRuntime<'conversation.composer'> & { matched: QuestionWait }
|
||||
|
||||
@@ -1,66 +1,37 @@
|
||||
/**
|
||||
* Web question plugin, browser half: QuestionComposer registered as the
|
||||
* `question` entry of the conversation-declared keyed `conversation.composer`
|
||||
* slot. Pure consumer — the pending interaction arrives through the owner
|
||||
* share at the dispatch site, drafts are component-local, and the inject
|
||||
* surface is plain session-scoped callbacks closed over the plugin's own ctx
|
||||
* (slot design sections 5 and 6); props composition in contract/slots.ts.
|
||||
* Export discipline: packages/client/AGENTS.md.
|
||||
* Web question plugin, browser half: QuestionComposer registered as a
|
||||
* selector-routed entry of the conversation-declared composer chain. Pure
|
||||
* consumer — the selector narrows the owner's currency to the question
|
||||
* carrier (matched prop), and the whole behavior surface rides the carrier
|
||||
* (domain encoding in contract/slots.ts PendingQuestion); no inject face, no
|
||||
* service dependency beyond slots. Export discipline: packages/client/AGENTS.md.
|
||||
*/
|
||||
import type { ClientContext, SessionId, SessionsService, SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
|
||||
import type { QuestionComposerInjected } from './contract/slots.ts'
|
||||
import type { ClientContext } from '@deepseek-ai/dsh-client-runtime/client'
|
||||
import type { ComposerChainProps } from '@deepseek-ai/dsh-client-ui-conversation/client'
|
||||
import type { QuestionWait } from './contract/slots.ts'
|
||||
import { QuestionComposer } from './QuestionComposer.tsx'
|
||||
|
||||
export type {
|
||||
QuestionAnswer, QuestionComposerInjected, QuestionComposerProps, QuestionInteraction,
|
||||
} from './contract/slots.ts'
|
||||
export { PendingQuestion } from './contract/slots.ts'
|
||||
export type { QuestionAnswer, QuestionComposerProps, QuestionWait } from './contract/slots.ts'
|
||||
|
||||
/** Required services (cordis fiber inject — the loader passes the whole export surface as an object plugin). */
|
||||
export const inject = ['slots', 'sessions']
|
||||
export const inject = ['slots']
|
||||
|
||||
/** Resolve a service via ctx.get, failing loud. This package's program holds
|
||||
* the node half's host-side Context merges too (tool-ask-user), so property
|
||||
* access would resolve the colliding host `sessions` seat — same budgeted
|
||||
* cast as ui-conversation's need(). */
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-parameters -- caller-named cast target
|
||||
function need<T>(ctx: ClientContext, name: string): T {
|
||||
const value = ctx.get(name) as T | undefined
|
||||
if (value === undefined) throw new Error(`ui-question: ${name} service unavailable`)
|
||||
return value
|
||||
/** Chain routing: claim the composer while a question wait is pending (pure — owner props only). */
|
||||
function selectQuestion({ interactions }: ComposerChainProps): QuestionWait | null {
|
||||
return interactions.find((i): i is QuestionWait => i.kind === 'question') ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* Client plugin body: register the question composer into the keyed composer
|
||||
* slot. The inject factory returns receipt-checked answer/cancel callbacks
|
||||
* only (no hooks, no store lines) — the framework resolves the sessionId, and
|
||||
* the question payload rides the owner share.
|
||||
* Client plugin body: register the question composer into the composer chain.
|
||||
* Zero business face — data and verbs both live on the matched carrier.
|
||||
* @param ctx - client root context.
|
||||
*/
|
||||
export function apply(ctx: ClientContext): void {
|
||||
const slots = need<SlotsService>(ctx, 'slots')
|
||||
const sessions = need<SessionsService>(ctx, 'sessions')
|
||||
const injectProps = (sessionId: SessionId): QuestionComposerInjected => {
|
||||
const session = sessions.manager.get(sessionId)
|
||||
return {
|
||||
answer: async (interaction, answer) => {
|
||||
const receipt = await session.answerQuestion(interaction.rpcId, answer)
|
||||
if (!receipt.accepted) {
|
||||
throw new Error(`question response rejected: ${receipt.reason}`)
|
||||
}
|
||||
},
|
||||
cancel: async (interaction) => {
|
||||
const receipt = await session.cancelQuestion(interaction.rpcId)
|
||||
if (!receipt.accepted) {
|
||||
throw new Error(`question cancellation rejected: ${receipt.reason}`)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
const slots = ctx.slots
|
||||
if (slots === undefined) throw new Error('ui-question: slots service unavailable')
|
||||
ctx.effect(
|
||||
() => slots.register(
|
||||
{ name: 'conversation.composer', key: 'question', inject: injectProps },
|
||||
QuestionComposer,
|
||||
),
|
||||
'ui-question: composer slot registration',
|
||||
() => slots.register({ name: 'conversation.composer', select: selectQuestion }, QuestionComposer),
|
||||
'ui-question: composer chain registration',
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user