Commit 3e321e59 authored by jaden's avatar jaden

improve: improve

parent 2b091cd3
......@@ -2,6 +2,7 @@ import getConfig from 'next/config';
import { backendApi } from '.';
import { getModel } from '@/utils/gpt';
import { fetchEventSource, EventStreamContentType } from '@microsoft/fetch-event-source';
import { MutableRefObject, SetStateAction } from 'react';
export interface View {
type: 'schema' | 'table';
......@@ -35,6 +36,7 @@ export default class OpenAI {
onFinish: (responseText: string) => any,
onUpdate: (responseText: string, delta: string) => any,
onError: (e: Error) => any,
closeFn?: MutableRefObject<any>,
stream: boolean = true
) {
const requestPayload = {
......@@ -59,6 +61,12 @@ export default class OpenAI {
};
const requestTimeoutId = setTimeout(() => controller.abort(), 1000 * 120);
const chatPath = '/openai/v1/chat/completions';
if (closeFn) {
closeFn.current = () => {
clearTimeout(requestTimeoutId);
controller.abort();
};
}
if (stream) {
let responseText = '';
let finished = false;
......
......@@ -49,6 +49,7 @@ type AIProps = {
searchFlag?: RegExp;
inputProps?: Record<string, any>;
SendButton?: ({ inputRef }: { inputRef: any }) => JSX.Element;
noHistory?: boolean;
};
export function AIWrapper({
......@@ -66,6 +67,7 @@ export function AIWrapper({
searchFlag = /^\//,
inputProps = {},
SendButton,
noHistory,
}: AIProps) {
const input = useRef<any>();
const scrollContainer = useRef<any>();
......@@ -87,16 +89,30 @@ export function AIWrapper({
},
]);
setCurrentAssistantMessage('');
toView();
// return clearContext;
}, [currentAssistantMessage, loading]);
const closeRef = useRef<any>();
const close = () => {
closeRef.current && closeRef.current();
// setCurrentAssistantMessage('');
// requestAnimationFrame(() => {
// setLoading(false);
// });
};
const toView = useCallback(
debounce(() => {
scrollContainer &&
scrollContainer?.current?.scrollIntoView({
behavior: 'smooth',
block: 'end',
});
}, 50),
debounce(
() => {
scrollContainer &&
scrollContainer?.current?.scrollIntoView({
behavior: 'smooth',
block: 'end',
});
},
50,
{ maxWait: 500 }
),
[]
);
const handleButtonClick = useCallback(
......@@ -119,14 +135,20 @@ export function AIWrapper({
setLoading(true);
toView();
OpenAI.request(
initMessageList,
noHistory
? initMessageList.slice(0, startView).concat({
role: 'user',
content: inputValue,
})
: initMessageList,
currentAssistantMessageStr => {
setTinking(true);
setTimeout(() => {
setLoading(false);
setTinking(false);
setTimeout(toView, 100);
}, 1000);
// setTinking(true);
// setTimeout(() => {
setLoading(false);
// setTinking(false);
setTimeout(toView, 100);
// }, 100);
setCurrentAssistantMessage(currentAssistantMessageStr);
callBack && callBack(currentAssistantMessageStr);
doneFx && doneFx(currentAssistantMessageStr);
......@@ -141,7 +163,8 @@ export function AIWrapper({
title: 'No Response',
content: undefined,
});
}
},
closeRef
);
},
[
......@@ -466,6 +489,9 @@ export function AIWrapper({
? `${t('Analyzing requirements')}...`
: `${t('Creating query')}...`}
</div>
<Button className="shadow" onClick={close} size="mini" shape="round">
取消
</Button>
</div>
) : (
<Comment
......
......@@ -351,8 +351,13 @@ export function ChatView({
text: line,
};
const op2 = {
range: new monaco.Range(nextLineNumber, 1, nextLineNumber, 1),
text: insertText.trim() + '\n\n',
range: new monaco.Range(
lineNumber,
column + insertText.length,
nextLineNumber,
1
),
text: '\n' + insertText.trim() + '\n\n',
};
editor.executeEdits('insertSnippet', [op1, op2]);
......@@ -397,7 +402,7 @@ export function ChatView({
)}
</div>
<div className="w-full mb-[20px]">
<Collapse style={{ maxWidth: 1180 }}>
<Collapse>
<CollapseItem
header={
<div>
......
......@@ -805,6 +805,7 @@ INSERT INTO users (email, name) VALUES ($email$, $name$);
>
<Content>
<AI
noHistory
quickTip={queryTip}
welcome={
<>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment