Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Orca <help@stably.ai> Co-authored-by: gsxdsm <gsxdsm@users.noreply.github.com>
86 lines
3.0 KiB
TypeScript
86 lines
3.0 KiB
TypeScript
import { useEffect, useMemo, useState } from 'react'
|
|
import { useAppStore } from '../../store'
|
|
import { parseInteractivePrompt } from './native-chat-interactive-prompt'
|
|
import { nativeChatCardDismissKey } from './native-chat-dismiss-key'
|
|
import { NativeChatQuestionCard } from './NativeChatQuestionCard'
|
|
import { NativeChatApprovalCard } from './NativeChatApprovalCard'
|
|
import type { NativeChatInteractiveSend } from './use-native-chat-interactive-send'
|
|
|
|
/**
|
|
* Render the live interactive card for the pane while the agent's
|
|
* `interactivePrompt` is present: a question wizard (precedence) or a tool
|
|
* approval. Cleared by the host once the agent moves on, so it disappears
|
|
* automatically. Sends through the composer's verified runtime path (R8/R6):
|
|
* answers as bracketed-paste + Enter; cancel/deny as ESC. Guarded by `canSend`
|
|
* so a mobile presence-lock blocks desktop sends the same way it guards xterm.
|
|
*
|
|
* Dismiss-on-answer (mobile parity): the live status lingers after answering —
|
|
* the agent emits a post-tool event carrying the same prompt — so we track the
|
|
* answered prompt by content key and hide the card until a genuinely different
|
|
* prompt arrives. The dismissal resets once the prompt clears, so a later
|
|
* (even identical) prompt shows again instead of staying hidden.
|
|
*/
|
|
export function NativeChatInteractiveCard({
|
|
paneKey,
|
|
send,
|
|
canSend
|
|
}: {
|
|
paneKey: string
|
|
send: NativeChatInteractiveSend
|
|
canSend: boolean
|
|
}): React.JSX.Element | null {
|
|
const interactivePrompt = useAppStore(
|
|
(s) => s.agentStatusByPaneKey[paneKey]?.interactivePrompt ?? null
|
|
)
|
|
// Thread the sibling `toolName` from the same status entry so the question
|
|
// parser can dispatch through the tool's registered parser (mobile parity).
|
|
const interactiveToolName = useAppStore(
|
|
(s) => s.agentStatusByPaneKey[paneKey]?.toolName ?? null
|
|
)
|
|
const { sendAnswer, sendRaw, cancel } = send
|
|
|
|
const card = useMemo(
|
|
() => parseInteractivePrompt(interactivePrompt, interactiveToolName ?? undefined),
|
|
[interactivePrompt, interactiveToolName]
|
|
)
|
|
const cardKey = useMemo(() => nativeChatCardDismissKey(card), [card])
|
|
const [dismissedKey, setDismissedKey] = useState<string | null>(null)
|
|
|
|
// Forget the dismissal once the prompt clears so a fresh prompt can show.
|
|
const present = card != null
|
|
useEffect(() => {
|
|
if (!present) {
|
|
setDismissedKey(null)
|
|
}
|
|
}, [present])
|
|
|
|
if (!card || !canSend || cardKey === dismissedKey) {
|
|
return null
|
|
}
|
|
if (card.kind === 'question') {
|
|
return (
|
|
<NativeChatQuestionCard
|
|
key={cardKey ?? 'question'}
|
|
prompt={card.prompt}
|
|
onAnswer={(text) => {
|
|
setDismissedKey(cardKey)
|
|
sendAnswer(text)
|
|
}}
|
|
onCancel={() => {
|
|
setDismissedKey(cardKey)
|
|
cancel()
|
|
}}
|
|
/>
|
|
)
|
|
}
|
|
return (
|
|
<NativeChatApprovalCard
|
|
approval={card.approval}
|
|
onChoose={(raw) => {
|
|
setDismissedKey(cardKey)
|
|
sendRaw(raw)
|
|
}}
|
|
/>
|
|
)
|
|
}
|