| 12 | } |
| 13 | |
| 14 | const SchemaDrawer = (props: Props) => { |
| 15 | const { t } = useTranslation(); |
| 16 | const conversationStore = useConversationStore(); |
| 17 | const connectionStore = useConnectionStore(); |
| 18 | const settingStore = useSettingStore(); |
| 19 | const currentConversation = conversationStore.getConversationById(conversationStore.currentConversationId); |
| 20 | const maxToken = getModel(settingStore.setting.openAIApiConfig?.model || "").max_token; |
| 21 | const [prompt, setPrompt] = useState<string>(""); |
| 22 | |
| 23 | const getPrompt = async () => { |
| 24 | if (!currentConversation) return; |
| 25 | const promptGenerator = getPromptGeneratorOfAssistant(getAssistantById(currentConversation.assistantId)!); |
| 26 | let dbPrompt = promptGenerator(); |
| 27 | if (connectionStore.currentConnectionCtx?.database) { |
| 28 | const schemaList = await connectionStore.getOrFetchDatabaseSchema(connectionStore.currentConnectionCtx?.database); |
| 29 | dbPrompt = generateDbPromptFromContext( |
| 30 | promptGenerator, |
| 31 | connectionStore.currentConnectionCtx.connection.engineType, |
| 32 | schemaList, |
| 33 | currentConversation.selectedSchemaName || "", |
| 34 | currentConversation.selectedTableNameList || [], |
| 35 | maxToken |
| 36 | ); |
| 37 | } |
| 38 | setPrompt(dbPrompt); |
| 39 | }; |
| 40 | |
| 41 | useEffect(() => { |
| 42 | // TODO: initial state with current conversation. |
| 43 | }, []); |
| 44 | |
| 45 | useEffect(() => { |
| 46 | getPrompt(); |
| 47 | }, []); |
| 48 | |
| 49 | const close = () => props.close(); |
| 50 | return ( |
| 51 | <Drawer open={true} anchor="right" className="w-full" onClose={close}> |
| 52 | <div className="dark:text-gray-300 w-screen sm:w-[calc(75vw)] max-w-full flex flex-col p-4"> |
| 53 | <button className="w-8 h-8 p-1 bg-zinc-600 text-gray-100 rounded-full hover:opacity-80" onClick={close}> |
| 54 | <Icon.IoMdClose className="w-full h-auto" /> |
| 55 | </button> |
| 56 | <div className="flex justify-between items-center"> |
| 57 | <h3 className="font-bold text-2xl mt-4">{t("prompt.current-conversation")}</h3> |
| 58 | <div className="text-base mt-4"> |
| 59 | {t("connection.total-token")} {encode(prompt).length}/{maxToken} |
| 60 | </div> |
| 61 | </div> |
| 62 | <div> |
| 63 | <CodeBlock language="Prompt" value={prompt} messageId={currentConversation?.id || ""} wrapLongLines={true} /> |
| 64 | </div> |
| 65 | </div> |
| 66 | </Drawer> |
| 67 | ); |
| 68 | }; |
| 69 | |
| 70 | export default SchemaDrawer; |