From a84c70eaecaffa726298f7bdac17e08b884f4110 Mon Sep 17 00:00:00 2001 From: samlhuillier Date: Thu, 14 Nov 2024 21:58:16 +0000 Subject: [PATCH] show response --- src/components/Editor/AIEdit.tsx | 71 ++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/src/components/Editor/AIEdit.tsx b/src/components/Editor/AIEdit.tsx index 2ce36d0d..3042f113 100644 --- a/src/components/Editor/AIEdit.tsx +++ b/src/components/Editor/AIEdit.tsx @@ -12,34 +12,61 @@ interface AiEditMenuProps { const AiEditMenu = ({ selectedText, onEdit }: AiEditMenuProps) => { const [response, setResponse] = useState('') + const [isLoading, setIsLoading] = useState(false) const handleEdit = async () => { - const defaultLLMName = await window.llm.getDefaultLLMName() - const llmClient = await resolveLLMClient(defaultLLMName) - const { textStream } = await streamText({ - model: llmClient, - messages: [{ role: 'user', content: `Edit the following text: ${selectedText}` }], - }) + try { + setIsLoading(true) + setResponse('') + const defaultLLMName = await window.llm.getDefaultLLMName() + const llmClient = await resolveLLMClient(defaultLLMName) + const { textStream } = await streamText({ + model: llmClient, + messages: [{ role: 'user', content: `Edit the following text: ${selectedText}` }], + }) - // eslint-disable-next-line no-restricted-syntax - for await (const textPart of textStream) { - setResponse((prev) => prev + textPart) + // eslint-disable-next-line no-restricted-syntax + for await (const textPart of textStream) { + setResponse((prev) => prev + textPart) + } + } finally { + setIsLoading(false) } } + return ( -
- e.stopPropagation()} - onMouseDown={(e) => e.stopPropagation()} - onKeyDown={(e) => e.stopPropagation()} - className="z-50 flex w-full flex-col overflow-hidden rounded border-2 border-solid border-border bg-background text-white focus-within:ring-1 focus-within:ring-ring" - // eslint-disable-next-line jsx-a11y/no-autofocus - autoFocus - /> - +
+ {(response || isLoading) && ( +
+ {isLoading && !response &&
Generating response...
} + {response && ( +
+

{response}

+
+ )} +
+ )} + +
+ e.stopPropagation()} + onMouseDown={(e) => e.stopPropagation()} + onKeyDown={(e) => e.stopPropagation()} + className="z-50 flex w-full flex-col overflow-hidden rounded border-2 border-solid border-border bg-background text-white focus-within:ring-1 focus-within:ring-ring" + // eslint-disable-next-line jsx-a11y/no-autofocus + autoFocus + /> + +
) }