({
session,
initialPrompt,
}: {
session: SessionData,
initialPrompt?: { message: string; mode: ModeType; model: SupportedChatModelId };
})
| 57 | }; |
| 58 | |
| 59 | function SessionChat({ |
| 60 | session, |
| 61 | initialPrompt, |
| 62 | }: { |
| 63 | session: SessionData, |
| 64 | initialPrompt?: { message: string; mode: ModeType; model: SupportedChatModelId }; |
| 65 | }) { |
| 66 | const [initialMessages] = useState(() => session.messages as unknown as Message[]); |
| 67 | const { mode, model } = usePromptConfig(); |
| 68 | const { isTopLayer } = useKeyboardLayer(); |
| 69 | const { messages, status, submit, abort, interrupt, error } = useChat( |
| 70 | session.id, |
| 71 | initialMessages |
| 72 | ); |
| 73 | const hasSubmittedInitialPromptRef = useRef(false); |
| 74 | |
| 75 | // Stop the pending reply when the user leaves this session. |
| 76 | useEffect(() => { |
| 77 | return () => { |
| 78 | void abort(); |
| 79 | }; |
| 80 | }, [abort]); |
| 81 | |
| 82 | // Let the user cancel a reply even before the first streamed chunk arrives. |
| 83 | useKeyboard((key) => { |
| 84 | if (key.name === "escape" && isTopLayer("base") && status === "streaming") { |
| 85 | key.preventDefault(); |
| 86 | interrupt(); |
| 87 | } |
| 88 | }); |
| 89 | |
| 90 | useEffect(() => { |
| 91 | if (!initialPrompt || hasSubmittedInitialPromptRef.current) return; |
| 92 | hasSubmittedInitialPromptRef.current = true; |
| 93 | void submit({ |
| 94 | userText: initialPrompt.message, |
| 95 | mode: initialPrompt.mode, |
| 96 | model: initialPrompt.model, |
| 97 | }); |
| 98 | }, [initialPrompt, submit]); |
| 99 | |
| 100 | return ( |
| 101 | <SessionShell |
| 102 | onSubmit={(text) => submit({ userText: text, mode, model })} |
| 103 | loading={status === "submitted" || status === "streaming"} |
| 104 | interruptible={status === "submitted" || status === "streaming"} |
| 105 | > |
| 106 | {messages.map((msg) => ( |
| 107 | <ChatMessage key={msg.id} msg={msg} /> |
| 108 | ))} |
| 109 | {error && <ErrorMessage message={error.message} />} |
| 110 | </SessionShell> |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | export function Session() { |
| 115 | const { id } = useParams(); |
nothing calls this directly
no test coverage detected