-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'debug_messages' into 'master'
Bot UI: Display debug messages See merge request postgres-ai/database-lab!867
- Loading branch information
Showing
16 changed files
with
486 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {request} from "../../helpers/request"; | ||
import { DebugMessage } from "../../types/api/entities/bot"; | ||
|
||
type Req = | ||
| { thread_id: string; message_id?: string } | ||
| { thread_id?: string; message_id: string }; | ||
|
||
export const getDebugMessages = async (req: Req): Promise<{ response: DebugMessage[] | null; error: Response | null }> => { | ||
const { thread_id, message_id } = req; | ||
|
||
const params: { [key: string]: string } = {}; | ||
|
||
if (thread_id) { | ||
params['chat_thread_id'] = `eq.${thread_id}`; | ||
} | ||
|
||
if (message_id) { | ||
params['chat_msg_id'] = `eq.${message_id}`; | ||
} | ||
|
||
const queryString = new URLSearchParams(params).toString(); | ||
|
||
const apiServer = process.env.REACT_APP_API_URL_PREFIX || ''; | ||
|
||
try { | ||
const response = await request(`${apiServer}/chat_debug_messages?${queryString}`); | ||
|
||
if (!response.ok) { | ||
return { response: null, error: response }; | ||
} | ||
|
||
const responseData: DebugMessage[] = await response.json(); | ||
|
||
return { response: responseData, error: null }; | ||
|
||
} catch (error) { | ||
return { response: null, error: error as Response }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
ui/packages/platform/src/pages/Bot/DebugConsole/DebugConsole.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import React, { useEffect, useRef, useState } from "react"; | ||
import Dialog from "@material-ui/core/Dialog"; | ||
import { DialogContent, DialogTitle, makeStyles } from "@material-ui/core"; | ||
import { useAiBot } from "../hooks"; | ||
import rehypeRaw from "rehype-raw"; | ||
import remarkGfm from "remark-gfm"; | ||
import ReactMarkdown from "react-markdown"; | ||
import IconButton from "@material-ui/core/IconButton"; | ||
import CloseIcon from "@material-ui/icons/Close"; | ||
import { disallowedHtmlTagsForMarkdown } from "../utils"; | ||
import { DebugLogs } from "../DebugLogs/DebugLogs"; | ||
import { DebugMessage } from "../../../types/api/entities/bot"; | ||
|
||
const useStyles = makeStyles( | ||
(theme) => ({ | ||
dialogStyle: { | ||
top: '5%!important', | ||
right: '10%!important', | ||
left: 'unset!important', | ||
height: 'fit-content', | ||
width: 'fit-content', | ||
}, | ||
paper: { | ||
width: '80vw', | ||
height: '70vh', | ||
opacity: '.5', | ||
transition: '.2s ease', | ||
'&:hover': { | ||
opacity: 1 | ||
} | ||
}, | ||
dialogTitle: { | ||
padding: '0.5rem', | ||
'& h2': { | ||
fontSize: '1rem', | ||
display: 'flex', | ||
justifyContent: 'space-between', | ||
alignItems: 'center' | ||
} | ||
}, | ||
dialogContent: { | ||
padding: 0, | ||
margin: 0, | ||
} | ||
})) | ||
|
||
type DebugConsoleProps = { | ||
isOpen: boolean | ||
onClose: () => void | ||
threadId?: string | ||
} | ||
|
||
export const DebugConsole = (props: DebugConsoleProps) => { | ||
const { isOpen, onClose, threadId } = props; | ||
const { debugMessages, debugMessagesLoading } = useAiBot(); | ||
const classes = useStyles(); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
if (containerRef.current && debugMessages?.length && threadId && !debugMessagesLoading && isOpen) { | ||
let code: HTMLElement = containerRef.current.getElementsByTagName('code')?.[0]; | ||
if (code.hasChildNodes()) { | ||
code.appendChild(document.createTextNode(`[${debugMessages[debugMessages.length - 1].created_at}]: ${debugMessages[debugMessages.length - 1].content}\n`)) | ||
} else { | ||
debugMessages.forEach((item) => { | ||
code.appendChild(document.createTextNode(`[${item.created_at}]: ${item.content}\n`)) | ||
}) | ||
const container = document.getElementById(`logs-container-${threadId}`); | ||
if (container) { | ||
container.appendChild(code) | ||
} | ||
} | ||
} | ||
}, [debugMessages, isOpen, threadId, debugMessagesLoading]); | ||
|
||
return ( | ||
<Dialog | ||
open={isOpen} | ||
disableEnforceFocus | ||
hideBackdrop | ||
className={classes.dialogStyle} | ||
classes={{paper: classes.paper}} | ||
scroll="paper" | ||
fullWidth | ||
maxWidth="xl" | ||
> | ||
<DialogTitle className={classes.dialogTitle}> | ||
Debug console | ||
<IconButton | ||
onClick={onClose} | ||
> | ||
<CloseIcon /> | ||
</IconButton> | ||
</DialogTitle> | ||
<DialogContent | ||
classes={{root: classes.dialogContent}} | ||
ref={containerRef} | ||
> | ||
<DebugLogs | ||
isLoading={debugMessagesLoading} | ||
isEmpty={debugMessages?.length === 0} | ||
id={threadId!} | ||
/> | ||
</DialogContent> | ||
</Dialog> | ||
) | ||
} |
Oops, something went wrong.