2023-10-13 06:35:49 +00:00
|
|
|
import React, { useEffect, useState, useRef } from "react";
|
2023-03-26 06:06:10 +00:00
|
|
|
import { useRouter } from "next/router";
|
|
|
|
// react-hook-form
|
|
|
|
import { useForm } from "react-hook-form";
|
|
|
|
// services
|
|
|
|
import aiService from "services/ai.service";
|
2023-03-30 13:57:46 +00:00
|
|
|
import trackEventServices from "services/track-event.service";
|
2023-03-26 06:06:10 +00:00
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-06-06 16:06:00 +00:00
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
2023-03-26 06:06:10 +00:00
|
|
|
// ui
|
|
|
|
import { Input, PrimaryButton, SecondaryButton } from "components/ui";
|
2023-10-13 06:35:49 +00:00
|
|
|
// components
|
|
|
|
import { RichReadOnlyEditor, RichReadOnlyEditorWithRef } from "@plane/rich-text-editor";
|
2023-08-28 10:24:49 +00:00
|
|
|
// types
|
2023-03-30 13:57:46 +00:00
|
|
|
import { IIssue, IPageBlock } from "types";
|
2023-08-28 10:24:49 +00:00
|
|
|
|
2023-03-26 06:06:10 +00:00
|
|
|
type Props = {
|
|
|
|
isOpen: boolean;
|
|
|
|
handleClose: () => void;
|
|
|
|
inset?: string;
|
|
|
|
content: string;
|
2023-03-28 18:50:00 +00:00
|
|
|
htmlContent?: string;
|
2023-03-26 06:06:10 +00:00
|
|
|
onResponse: (response: string) => void;
|
2023-03-29 11:00:40 +00:00
|
|
|
projectId: string;
|
2023-03-30 13:57:46 +00:00
|
|
|
block?: IPageBlock;
|
|
|
|
issue?: IIssue;
|
2023-03-26 06:06:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type FormData = {
|
|
|
|
prompt: string;
|
|
|
|
task: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const GptAssistantModal: React.FC<Props> = ({
|
|
|
|
isOpen,
|
|
|
|
handleClose,
|
|
|
|
inset = "top-0 left-0",
|
|
|
|
content,
|
2023-03-28 18:50:00 +00:00
|
|
|
htmlContent,
|
2023-03-26 06:06:10 +00:00
|
|
|
onResponse,
|
2023-03-29 11:00:40 +00:00
|
|
|
projectId,
|
2023-03-30 13:57:46 +00:00
|
|
|
block,
|
|
|
|
issue,
|
2023-03-26 06:06:10 +00:00
|
|
|
}) => {
|
|
|
|
const [response, setResponse] = useState("");
|
|
|
|
const [invalidResponse, setInvalidResponse] = useState(false);
|
|
|
|
|
|
|
|
const router = useRouter();
|
2023-03-29 11:00:40 +00:00
|
|
|
const { workspaceSlug } = router.query;
|
2023-03-26 06:06:10 +00:00
|
|
|
|
2023-06-06 16:06:00 +00:00
|
|
|
const { user } = useUserAuth();
|
|
|
|
|
2023-05-11 08:11:16 +00:00
|
|
|
const editorRef = useRef<any>(null);
|
|
|
|
|
2023-03-26 06:06:10 +00:00
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const {
|
|
|
|
handleSubmit,
|
|
|
|
register,
|
|
|
|
reset,
|
|
|
|
setFocus,
|
|
|
|
formState: { isSubmitting },
|
|
|
|
} = useForm({
|
|
|
|
defaultValues: {
|
|
|
|
prompt: content,
|
|
|
|
task: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const onClose = () => {
|
|
|
|
handleClose();
|
|
|
|
setResponse("");
|
|
|
|
setInvalidResponse(false);
|
|
|
|
reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleResponse = async (formData: FormData) => {
|
|
|
|
if (!workspaceSlug || !projectId) return;
|
|
|
|
|
|
|
|
if (formData.task === "") {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Please enter some task to get AI assistance.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await aiService
|
2023-06-06 16:06:00 +00:00
|
|
|
.createGptTask(
|
|
|
|
workspaceSlug as string,
|
|
|
|
projectId as string,
|
|
|
|
{
|
|
|
|
prompt: content && content !== "" ? content : htmlContent ?? "",
|
|
|
|
task: formData.task,
|
|
|
|
},
|
|
|
|
user
|
|
|
|
)
|
2023-03-26 06:06:10 +00:00
|
|
|
.then((res) => {
|
2023-03-28 18:50:00 +00:00
|
|
|
setResponse(res.response_html);
|
2023-03-26 06:06:10 +00:00
|
|
|
setFocus("task");
|
|
|
|
|
|
|
|
if (res.response === "") setInvalidResponse(true);
|
|
|
|
else setInvalidResponse(false);
|
2023-03-30 13:15:33 +00:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2023-07-26 18:29:34 +00:00
|
|
|
const error = err?.data?.error;
|
|
|
|
|
2023-03-30 13:15:33 +00:00
|
|
|
if (err.status === 429)
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message:
|
2023-07-26 18:29:34 +00:00
|
|
|
error ||
|
2023-03-30 13:15:33 +00:00
|
|
|
"You have reached the maximum number of requests of 50 requests per month per user.",
|
|
|
|
});
|
|
|
|
else
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
2023-07-26 18:29:34 +00:00
|
|
|
message: error || "Some error occurred. Please try again.",
|
2023-03-30 13:15:33 +00:00
|
|
|
});
|
2023-03-26 06:06:10 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isOpen) setFocus("task");
|
|
|
|
}, [isOpen, setFocus]);
|
|
|
|
|
2023-05-11 08:11:16 +00:00
|
|
|
useEffect(() => {
|
|
|
|
editorRef.current?.setEditorValue(htmlContent ?? `<p>${content}</p>`);
|
|
|
|
}, [htmlContent, editorRef, content]);
|
|
|
|
|
2023-03-26 06:06:10 +00:00
|
|
|
return (
|
|
|
|
<div
|
2023-10-13 06:35:49 +00:00
|
|
|
className={`absolute ${inset} z-20 w-full space-y-4 rounded-[10px] border border-custom-border-200 bg-custom-background-100 p-4 shadow ${isOpen ? "block" : "hidden"
|
|
|
|
}`}
|
2023-03-26 06:06:10 +00:00
|
|
|
>
|
2023-05-10 20:45:49 +00:00
|
|
|
{((content && content !== "") || (htmlContent && htmlContent !== "<p></p>")) && (
|
2023-08-16 12:55:11 +00:00
|
|
|
<div className="text-sm">
|
2023-03-29 11:00:40 +00:00
|
|
|
Content:
|
2023-10-13 06:35:49 +00:00
|
|
|
<RichReadOnlyEditorWithRef
|
2023-08-15 09:34:46 +00:00
|
|
|
value={htmlContent ?? `<p>${content}</p>`}
|
2023-04-04 13:57:13 +00:00
|
|
|
customClassName="-m-3"
|
2023-03-29 11:00:40 +00:00
|
|
|
noBorder
|
|
|
|
borderOnFocus={false}
|
2023-05-11 08:11:16 +00:00
|
|
|
ref={editorRef}
|
2023-03-29 11:00:40 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{response !== "" && (
|
2023-05-05 10:15:10 +00:00
|
|
|
<div className="page-block-section text-sm">
|
2023-03-29 11:00:40 +00:00
|
|
|
Response:
|
2023-10-13 06:35:49 +00:00
|
|
|
<RichReadOnlyEditor
|
2023-03-29 11:00:40 +00:00
|
|
|
value={`<p>${response}</p>`}
|
|
|
|
customClassName="-mx-3 -my-3"
|
|
|
|
noBorder
|
|
|
|
borderOnFocus={false}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{invalidResponse && (
|
|
|
|
<div className="text-sm text-red-500">
|
|
|
|
No response could be generated. This may be due to insufficient content or task
|
|
|
|
information. Please try again.
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<Input
|
|
|
|
type="text"
|
|
|
|
name="task"
|
|
|
|
register={register}
|
2023-10-13 06:35:49 +00:00
|
|
|
placeholder={`${content && content !== ""
|
|
|
|
? "Tell AI what action to perform on this content..."
|
|
|
|
: "Ask AI anything..."
|
|
|
|
}`}
|
2023-03-29 11:00:40 +00:00
|
|
|
autoComplete="off"
|
|
|
|
/>
|
|
|
|
<div className={`flex gap-2 ${response === "" ? "justify-end" : "justify-between"}`}>
|
2023-03-26 06:06:10 +00:00
|
|
|
{response !== "" && (
|
2023-03-29 11:00:40 +00:00
|
|
|
<PrimaryButton
|
|
|
|
onClick={() => {
|
|
|
|
onResponse(response);
|
|
|
|
onClose();
|
2023-03-30 13:57:46 +00:00
|
|
|
if (block)
|
|
|
|
trackEventServices.trackUseGPTResponseEvent(
|
|
|
|
block,
|
2023-06-06 16:06:00 +00:00
|
|
|
"USE_GPT_RESPONSE_IN_PAGE_BLOCK",
|
|
|
|
user
|
2023-03-30 13:57:46 +00:00
|
|
|
);
|
|
|
|
else if (issue)
|
2023-06-06 16:06:00 +00:00
|
|
|
trackEventServices.trackUseGPTResponseEvent(
|
|
|
|
issue,
|
|
|
|
"USE_GPT_RESPONSE_IN_ISSUE",
|
|
|
|
user
|
|
|
|
);
|
2023-03-29 11:00:40 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Use this response
|
|
|
|
</PrimaryButton>
|
2023-03-26 06:06:10 +00:00
|
|
|
)}
|
2023-03-29 11:00:40 +00:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
<SecondaryButton onClick={onClose}>Close</SecondaryButton>
|
|
|
|
<PrimaryButton
|
|
|
|
type="button"
|
|
|
|
onClick={handleSubmit(handleResponse)}
|
|
|
|
loading={isSubmitting}
|
|
|
|
>
|
|
|
|
{isSubmitting
|
|
|
|
? "Generating response..."
|
|
|
|
: response === ""
|
2023-10-13 06:35:49 +00:00
|
|
|
? "Generate response"
|
|
|
|
: "Generate again"}
|
2023-03-29 11:00:40 +00:00
|
|
|
</PrimaryButton>
|
2023-03-26 06:06:10 +00:00
|
|
|
</div>
|
2023-03-29 11:00:40 +00:00
|
|
|
</div>
|
2023-03-26 06:06:10 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|