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

fix: only handle the query generation keydown event when not streaming #1287

Merged
merged 2 commits into from
Jun 27, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "querybook",
"version": "3.25.2",
"version": "3.25.3",
"description": "A Big Data Webapp",
"private": true,
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions querybook/webapp/components/AIAssistant/AutoFixButton.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.AutoFixModal {
.Modal-box {
.Modal-content {
min-height: 40vh;
}
}
}
70 changes: 45 additions & 25 deletions querybook/webapp/components/AIAssistant/AutoFixButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Message } from 'ui/Message/Message';
import { Modal } from 'ui/Modal/Modal';
import { AccentText } from 'ui/StyledText/StyledText';

import './AutoFixButton.scss';

interface IProps {
query: string;
queryExecutionId: number;
Expand All @@ -22,7 +24,7 @@ export const AutoFixButton = ({
}: IProps) => {
const [show, setShow] = useState(false);

const { streamStatus, startStream, streamData } = useStream(
const { streamStatus, startStream, streamData, cancelStream } = useStream(
'/ds/ai/query_auto_fix/',
{
query_execution_id: queryExecutionId,
Expand All @@ -35,37 +37,53 @@ export const AutoFixButton = ({
fixed_query: fixedQuery,
} = streamData;

const bottomDOM = fixedQuery && (
<div className="right-align mb16">
<Button
title="Reject"
color="cancel"
onClick={() => {
setShow(false);
}}
/>
<Button
title="Apply"
color="confirm"
onClick={() => {
onUpdateQuery?.(fixedQuery);
trackClick({
component: ComponentType.AI_ASSISTANT,
element: ElementType.QUERY_ERROR_AUTO_FIX_APPLY_BUTTON,
});
setShow(false);
}}
/>
</div>
);
const bottomDOM =
streamStatus === StreamStatus.STREAMING ? (
<div className="right-align mb16">
<Button
title="Stop Generating"
color="light"
onClick={cancelStream}
className="mr8"
/>
</div>
) : (
fixedQuery && (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace with streamStatus === StreamStatus.DONE && ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't always have the fixedQuery when the streaming is done. for those cases, we dont want to show the apply/cancel buttons

<div className="right-align mb16">
<Button
title="Reject"
color="cancel"
onClick={() => {
setShow(false);
}}
/>
<Button
title="Apply"
color="confirm"
onClick={() => {
onUpdateQuery?.(fixedQuery);
trackClick({
component: ComponentType.AI_ASSISTANT,
element:
ElementType.QUERY_ERROR_AUTO_FIX_APPLY_BUTTON,
});
setShow(false);
}}
/>
</div>
)
);
return (
<>
<Button
icon="Bug"
title="Auto fix"
onClick={() => {
setShow(true);
if (streamStatus === StreamStatus.NOT_STARTED) {
if (
streamStatus === StreamStatus.NOT_STARTED ||
streamStatus === StreamStatus.CANCELLED
) {
startStream();
trackClick({
component: ComponentType.AI_ASSISTANT,
Expand All @@ -77,9 +95,11 @@ export const AutoFixButton = ({
{show && (
<Modal
onHide={() => {
cancelStream();
setShow(false);
}}
bottomDOM={bottomDOM}
className="AutoFixModal"
>
<Message
message="Note: This AI-powered auto fix may not be 100% accurate. Please use your own judgement and verify the result."
Expand Down
22 changes: 18 additions & 4 deletions querybook/webapp/components/AIAssistant/QueryGenerationModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const QueryGenerationModal = ({
setTables(uniq([...tablesInQuery, ...tables]));
}, [tablesInQuery]);

const { streamStatus, startStream, streamData } = useStream(
const { streamStatus, startStream, streamData, cancelStream } = useStream(
'/ds/ai/generate_query/',
{
query_engine_id: engineId,
Expand All @@ -91,7 +91,10 @@ export const QueryGenerationModal = ({

const onKeyDown = useCallback(
(event: React.KeyboardEvent) => {
if (matchKeyPress(event, 'Enter')) {
if (
streamStatus !== StreamStatus.STREAMING &&
matchKeyPress(event, 'Enter')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also make the textbox not editable while streaming?

) {
startStream();
inputRef.current.blur();
trackClick({
Expand All @@ -105,7 +108,7 @@ export const QueryGenerationModal = ({
});
}
},
[startStream]
[streamStatus, startStream]
);

const questionBarDOM = (
Expand Down Expand Up @@ -148,6 +151,14 @@ export const QueryGenerationModal = ({
autoFocus: true,
}}
/>
{streamStatus === StreamStatus.STREAMING && (
<Button
title="Stop Generating"
color="light"
onClick={cancelStream}
className="mr8"
/>
)}
</div>
);

Expand Down Expand Up @@ -183,7 +194,10 @@ export const QueryGenerationModal = ({

return (
<Modal
onHide={onHide}
onHide={() => {
cancelStream();
onHide();
}}
className="QueryGenerationModal"
bottomDOM={bottomDOM}
>
Expand Down
15 changes: 13 additions & 2 deletions querybook/webapp/hooks/useStream.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useCallback, useState } from 'react';
import { useCallback, useRef, useState } from 'react';

import ds from 'lib/datasource';

export enum StreamStatus {
NOT_STARTED,
STREAMING,
FINISHED,
CANCELLED,
}

export function useStream(
Expand All @@ -16,15 +17,17 @@ export function useStream(
streamData: { [key: string]: string };
startStream: () => void;
resetStream: () => void;
cancelStream: () => void;
} {
const [streamStatus, setSteamStatus] = useState(StreamStatus.NOT_STARTED);
const [data, setData] = useState<{ [key: string]: string }>({});
const streamRef = useRef<EventSource | null>(null);

const startStream = useCallback(() => {
setSteamStatus(StreamStatus.STREAMING);
setData({});

ds.stream(url, params, setData, (data) => {
streamRef.current = ds.stream(url, params, setData, (data) => {
setData(data);
setSteamStatus(StreamStatus.FINISHED);
});
Expand All @@ -35,10 +38,18 @@ export function useStream(
setData({});
}, []);

const cancelStream = useCallback(() => {
if (streamStatus === StreamStatus.STREAMING) {
streamRef.current?.close();
setSteamStatus(StreamStatus.CANCELLED);
}
}, [streamStatus]);

return {
streamStatus,
streamData: data,
startStream,
resetStream,
cancelStream,
};
}
2 changes: 2 additions & 0 deletions querybook/webapp/lib/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ function streamDatasource(
parser.close();
onStreamingEnd?.(parser.result);
});

return eventSource;
}

export default {
Expand Down