Commit 3e321e59 authored by jaden's avatar jaden

improve: improve

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