|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | +import { Message } from './utils/message'; |
| 3 | + |
| 4 | +import { getWelcomeMessage, prompt as defaultPrompt, defaultWelcome } from './utils/prompt'; |
| 5 | +import { Input, Button, Flex, Typography, Space, Skeleton, theme } from 'antd'; |
| 6 | +import { useController } from './useController'; |
| 7 | +import { useContext } from '@graphscope/studio-graph'; |
| 8 | +import { extractTextWithPlaceholders } from './utils/extractTextWithPlaceholders'; |
| 9 | +import { PlayCircleOutlined, ClearOutlined, SearchOutlined, BulbOutlined, SettingOutlined } from '@ant-design/icons'; |
| 10 | +import MessageItem from './message'; |
| 11 | +import Setting from './setting'; |
| 12 | +import { useIntl } from 'react-intl'; |
| 13 | +import { Utils } from '@graphscope/studio-components'; |
| 14 | +import { query } from './query'; |
| 15 | +interface IGPTStatementsProps { |
| 16 | + prompt?: string; |
| 17 | + welcome?: string; |
| 18 | +} |
| 19 | + |
| 20 | +const { useToken } = theme; |
| 21 | +const GPTStatements: React.FunctionComponent<IGPTStatementsProps> = props => { |
| 22 | + const { prompt = defaultPrompt } = props; |
| 23 | + const [state, updateState] = useState<{ messages: Message[]; isLoading: boolean; OPENAI_KEY_FOR_GS: string | null }>({ |
| 24 | + messages: [], |
| 25 | + isLoading: false, |
| 26 | + OPENAI_KEY_FOR_GS: localStorage.getItem('OPENAI_KEY_FOR_GS'), |
| 27 | + }); |
| 28 | + const { messages, isLoading, OPENAI_KEY_FOR_GS } = state; |
| 29 | + const InputRef = useRef(null); |
| 30 | + const { token } = useToken(); |
| 31 | + const { store } = useContext(); |
| 32 | + const { schema: schemaData } = store; |
| 33 | + |
| 34 | + const controller = useController(); |
| 35 | + const { updateStore } = useContext(); |
| 36 | + const intl = useIntl(); |
| 37 | + const recommended_messages = [ |
| 38 | + // intl.formatMessage({ |
| 39 | + // id: 'recommend 5 interesting query statements', |
| 40 | + // }), |
| 41 | + // intl.formatMessage({ |
| 42 | + // id: 'query any subgraph', |
| 43 | + // }), |
| 44 | + // intl.formatMessage({ |
| 45 | + // id: 'insight the statistical distribution of vertex labels in the graph', |
| 46 | + // }), |
| 47 | + ]; |
| 48 | + const welcome = intl.formatMessage({ |
| 49 | + id: 'query.copilot.welcome', |
| 50 | + }); |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + if (schemaData) { |
| 54 | + updateState(pre => { |
| 55 | + return { |
| 56 | + ...pre, |
| 57 | + messages: [...getWelcomeMessage(welcome, prompt, schemaData)], |
| 58 | + }; |
| 59 | + }); |
| 60 | + } |
| 61 | + }, []); |
| 62 | + |
| 63 | + const handleSubmit = async (script?: string) => { |
| 64 | + if (InputRef.current) { |
| 65 | + //@ts-ignore |
| 66 | + const { value } = InputRef.current.input; |
| 67 | + const message = new Message({ |
| 68 | + role: 'user', |
| 69 | + content: script || value, |
| 70 | + }); |
| 71 | + |
| 72 | + updateState(pre => { |
| 73 | + return { |
| 74 | + ...pre, |
| 75 | + messages: [...pre.messages, message], |
| 76 | + isLoading: true, |
| 77 | + }; |
| 78 | + }); |
| 79 | + |
| 80 | + const response = await query([...messages, message], OPENAI_KEY_FOR_GS!, controller.signal); |
| 81 | + if (!response) { |
| 82 | + updateState(preState => { |
| 83 | + return { |
| 84 | + ...preState, |
| 85 | + isLoading: false, |
| 86 | + }; |
| 87 | + }); |
| 88 | + return false; |
| 89 | + } |
| 90 | + // addMessage( |
| 91 | + // new Message({ |
| 92 | + // role: response.message.role, |
| 93 | + // content: response.message.content, |
| 94 | + // status: 'success', |
| 95 | + // }), |
| 96 | + // ); |
| 97 | + updateState(pre => { |
| 98 | + return { |
| 99 | + ...pre, |
| 100 | + messages: [ |
| 101 | + ...pre.messages, |
| 102 | + new Message({ |
| 103 | + role: response.message.role, |
| 104 | + content: response.message.content, |
| 105 | + status: 'success', |
| 106 | + }), |
| 107 | + ], |
| 108 | + isLoading: false, |
| 109 | + }; |
| 110 | + }); |
| 111 | + } |
| 112 | + }; |
| 113 | + //@ts-ignore |
| 114 | + window.runAI = handleSubmit; |
| 115 | + const onQuery = (content: string) => { |
| 116 | + console.log('content'); |
| 117 | + updateStore(draft => {}); |
| 118 | + }; |
| 119 | + const handleClear = () => { |
| 120 | + updateState(pre => { |
| 121 | + return { |
| 122 | + ...pre, |
| 123 | + messages: [...getWelcomeMessage(welcome, prompt, schemaData)], |
| 124 | + }; |
| 125 | + }); |
| 126 | + }; |
| 127 | + |
| 128 | + const handleSave = value => { |
| 129 | + updateState(pre => { |
| 130 | + return { |
| 131 | + ...pre, |
| 132 | + OPENAI_KEY_FOR_GS: value, |
| 133 | + }; |
| 134 | + }); |
| 135 | + }; |
| 136 | + |
| 137 | + return ( |
| 138 | + <Flex vertical justify="space-between"> |
| 139 | + <Flex vertical gap={12} flex={1}> |
| 140 | + <Flex justify="space-between" align="center"> |
| 141 | + <Typography.Text type="secondary" italic> |
| 142 | + Think like a bot |
| 143 | + </Typography.Text> |
| 144 | + <Setting onChange={handleSave} /> |
| 145 | + </Flex> |
| 146 | + <Flex vertical gap={12} style={{ overflowY: 'scroll', height: 'calc(100vh - 170px)' }}> |
| 147 | + {messages |
| 148 | + .filter(m => m.role !== 'system') |
| 149 | + .map(item => { |
| 150 | + return <MessageItem key={item.timestamp} {...item} onQuery={onQuery} />; |
| 151 | + })} |
| 152 | + {isLoading && <Skeleton style={{ padding: '0px 6px' }} />} |
| 153 | + </Flex> |
| 154 | + </Flex> |
| 155 | + <Flex vertical> |
| 156 | + <Flex vertical gap={12}> |
| 157 | + {recommended_messages.map((item, index) => { |
| 158 | + return ( |
| 159 | + <div |
| 160 | + key={index} |
| 161 | + style={{ |
| 162 | + cursor: 'pointer', |
| 163 | + background: token.colorBgLayout, |
| 164 | + padding: '4px', |
| 165 | + borderRadius: '6px', |
| 166 | + margin: '6px 0px', |
| 167 | + }} |
| 168 | + > |
| 169 | + <BulbOutlined style={{ fontSize: '12px', marginRight: '4px' }} /> |
| 170 | + <Typography.Text |
| 171 | + key={index} |
| 172 | + onClick={() => { |
| 173 | + handleSubmit(item); |
| 174 | + }} |
| 175 | + style={{ |
| 176 | + fontSize: '12px', |
| 177 | + }} |
| 178 | + > |
| 179 | + {item} |
| 180 | + </Typography.Text> |
| 181 | + </div> |
| 182 | + ); |
| 183 | + })} |
| 184 | + </Flex> |
| 185 | + <Flex gap={8}> |
| 186 | + <Input ref={InputRef}></Input> |
| 187 | + <Button onClick={() => handleSubmit()} icon={<SearchOutlined />}></Button> |
| 188 | + <Button onClick={handleClear} icon={<ClearOutlined />}></Button> |
| 189 | + </Flex> |
| 190 | + </Flex> |
| 191 | + </Flex> |
| 192 | + ); |
| 193 | +}; |
| 194 | + |
| 195 | +export default GPTStatements; |
0 commit comments