Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: prevent users from sending new messages while message is streaming #266

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion ui/admin/app/components/chat/ChatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ChatContextType {
generatingMessage: Message | null;
invoke: (prompt?: string) => void;
readOnly?: boolean;
isRunning: boolean;
}

const ChatContext = createContext<ChatContextType | undefined>(undefined);
Expand All @@ -57,6 +58,7 @@ export function ChatProvider({
const [generatingMessage, setGeneratingMessage] = useState<string | null>(
null
);
const [isRunning, setIsRunning] = useState(false);
const isRunningToolCall = useRef(false);
// todo(tylerslaton): this is a huge hack to get the generating message and runId to be
// interactable during workflow invokes. take a look at invokeWorkflow to see why this is
Expand Down Expand Up @@ -145,6 +147,8 @@ export function ChatProvider({
onSuccess: ({ reader, threadId: responseThreadId }) => {
clearGeneratingMessage();

setIsRunning(true);

readStream<ChatEvent>({
reader,
onChunk: (chunk) =>
Expand Down Expand Up @@ -206,6 +210,7 @@ export function ChatProvider({

invokeAgent.clear();
generatingRunIdRef.current = null;
setIsRunning(false);
},
});
},
Expand All @@ -215,7 +220,6 @@ export function ChatProvider({
if (invokeAgent.isLoading)
return { sender: "agent", text: "", isLoading: true };

// slice the first character because it is always a newline for some reason
if (!generatingMessage) {
if (invokeAgent.data?.reader && !isRunningToolCall.current) {
return {
Expand Down Expand Up @@ -250,6 +254,7 @@ export function ChatProvider({
threadId,
generatingMessage: outGeneratingMessage,
invoke,
isRunning,
readOnly,
}}
>
Expand Down
7 changes: 5 additions & 2 deletions ui/admin/app/components/chat/Chatbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ type ChatbarProps = {

export function Chatbar({ className }: ChatbarProps) {
const [input, setInput] = useState("");
const { processUserMessage } = useChat();
const { processUserMessage, isRunning } = useChat();

const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();

if (isRunning) return;

if (input.trim()) {
processUserMessage(input, "user");
setInput("");
Expand Down Expand Up @@ -50,7 +53,7 @@ export function Chatbar({ className }: ChatbarProps) {
variant="secondary"
className="rounded-full"
type="submit"
disabled={!input}
disabled={!input || isRunning}
>
<CircleArrowUpIcon />
</Button>
Expand Down