diff --git a/components/chat.tsx b/components/chat.tsx index 23bf8dd..b7be99f 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -117,13 +117,15 @@ const Chat: React.FC = ({ const handleMessageSent = async (message: string) => { if (!socket || !connected) return; - setMessages((prevMessages) => [ - ...prevMessages, - { type: MessageType.User, message }, - ]); - setLatestAgentMessage({ - type: MessageType.Agent, - message: 'Waiting for model response...', + setMessages((prevMessages) => { + setLatestAgentMessage({ + type: MessageType.Agent, + message: 'Waiting for model response...', + name: prevMessages + ? prevMessages[prevMessages.length - 1].name + : undefined, + }); + return [...prevMessages, { type: MessageType.User, message }]; }); if (hasNoUserMessages() && thread) { renameThread(thread, await generateThreadName(message)); diff --git a/components/chat/messages/confirmForm.tsx b/components/chat/messages/confirmForm.tsx index a744a65..9a57ab0 100644 --- a/components/chat/messages/confirmForm.tsx +++ b/components/chat/messages/confirmForm.tsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React from 'react'; import { GoCheckCircle, GoXCircle, GoCheckCircleFill } from 'react-icons/go'; import type { AuthResponse } from '@gptscript-ai/gptscript'; import { Button, Code, Tooltip } from '@nextui-org/react'; @@ -21,21 +21,10 @@ const ConfirmForm = ({ message, command, }: ConfirmFormProps) => { - const [loading, setLoading] = useState(false); - - useEffect(() => { - setLoading(false); - }, [id]); - const onSubmitForm = (accept: boolean) => { - setLoading(true); onSubmit({ id, message: `denied by user`, accept }); }; - if (loading) { - return null; - } - return (
{ const { register, handleSubmit, getValues } = useForm>(); - const [submitted, setSubmitted] = useState(false); - const noFields = !frame.fields || frame.fields.length === 0; if (noFields) { frame.fields = []; } const onSubmitForm = () => { - setSubmitted(true); onSubmit({ id: frame.id, responses: getValues() }); if (frame.metadata && frame.metadata.authURL) { open(frame.metadata.authURL); @@ -64,9 +60,8 @@ const PromptForm = ({ className="mb-6 w-full" size="lg" color="primary" - isDisabled={submitted} > - {submitted ? 'Submitted' : buttonText} + {buttonText} ); diff --git a/components/chat/useChatSocket.tsx b/components/chat/useChatSocket.tsx index 812ec81..d23e1c9 100644 --- a/components/chat/useChatSocket.tsx +++ b/components/chat/useChatSocket.tsx @@ -17,7 +17,7 @@ import { rootTool, stringify } from '@/actions/gptscript'; import { updateScript } from '@/actions/me/scripts'; import { gatewayTool } from '@/actions/knowledge/util'; -const useChatSocket = (isEmpty?: boolean) => { +const useChatSocket = () => { const initiallyTrustedRepos = [ 'github.com/gptscript-ai/context', 'github.com/gptscript-ai/gateway-provider', @@ -97,7 +97,11 @@ const useChatSocket = (isEmpty?: boolean) => { const processWorkQueue = useCallback(() => { const initialQueueLength = workQueue.current.length; for (let i = 0; i < initialQueueLength; i++) { - if (workQueue.current.length === 0 || waitingForUserResponse) break; // Stop if the queue is empty + if ( + workQueue.current.length === 0 || + latestAgentMessageRef.current.component + ) + break; const { frame, name } = workQueue.current.shift()!; // Remove and process the first message in the queue @@ -112,6 +116,7 @@ const useChatSocket = (isEmpty?: boolean) => { if (frame.type === 'runStart') { latestAgentMessageRef.current.message = 'Waiting for model response...'; + latestAgentMessageRef.current.name = name; setLatestAgentMessage({ ...latestAgentMessageRef.current }); } continue; @@ -162,7 +167,7 @@ const useChatSocket = (isEmpty?: boolean) => { setLatestAgentMessage({ ...latestAgentMessageRef.current }); } } - }, [waitingForUserResponse]); + }, []); // Set up the interval to process the work queue every 50ms useEffect(() => { @@ -190,6 +195,10 @@ const useChatSocket = (isEmpty?: boolean) => { onSubmit={(response: PromptResponse) => { socketRef.current?.emit('promptResponse', response); setWaitingForUserResponse(false); + latestAgentMessageRef.current.component = null; + latestAgentMessageRef.current.message = + 'Waiting for model response...'; + setLatestAgentMessage({ ...latestAgentMessageRef.current }); }} /> ); @@ -232,6 +241,10 @@ const useChatSocket = (isEmpty?: boolean) => { onSubmit={(response: AuthResponse) => { socketRef.current?.emit('confirmResponse', response); setWaitingForUserResponse(false); + latestAgentMessageRef.current.component = null; + latestAgentMessageRef.current.message = + 'Waiting for model response...'; + setLatestAgentMessage({ ...latestAgentMessageRef.current }); }} /> ); @@ -449,18 +462,6 @@ const useChatSocket = (isEmpty?: boolean) => { socket.emit('interrupt'); }, [socket, connected]); - useEffect(() => { - if (running && messages.length === 0) { - if (!isEmpty) { - setLatestAgentMessage({ - calls: {}, - type: MessageType.Agent, - message: 'Waiting for model response...', - }); - } - } - }, [running, messages, isEmpty]); - return { error, socket, diff --git a/contexts/chat.tsx b/contexts/chat.tsx index 6ca663b..0884231 100644 --- a/contexts/chat.tsx +++ b/contexts/chat.tsx @@ -47,8 +47,6 @@ interface ChatContextState { setHasRun: React.Dispatch>; hasParams: boolean; setHasParams: React.Dispatch>; - isEmpty: boolean; - setIsEmpty: React.Dispatch>; notFound: boolean; setNotFound: React.Dispatch>; latestAgentMessage: Message; @@ -93,7 +91,6 @@ const ChatContextProvider: React.FC = ({ const [scriptId, setScriptId] = useState(initialScriptId); const [hasRun, setHasRun] = useState(false); const [hasParams, setHasParams] = useState(false); - const [isEmpty, setIsEmpty] = useState(false); const [notFound, setNotFound] = useState(false); const [thread, setThread] = useState(''); const [threads, setThreads] = useState([]); @@ -119,7 +116,7 @@ const ChatContextProvider: React.FC = ({ forceRun, setForceRun, waitingForUserResponse, - } = useChatSocket(isEmpty); + } = useChatSocket(); const [scriptDisplayName, setScriptDisplayName] = useState(''); const threadInitialized = useRef(false); const [shouldRestart, setShouldRestart] = useState(false); @@ -234,7 +231,6 @@ const ChatContextProvider: React.FC = ({ }, [subTool]); useEffect(() => { - setIsEmpty(!tool.instructions); if ( hasRun || !socket || @@ -365,8 +361,6 @@ const ChatContextProvider: React.FC = ({ setHasRun, hasParams, setHasParams, - isEmpty, - setIsEmpty, notFound, setNotFound, latestAgentMessage,