Skip to content

Commit

Permalink
update type to align with the be return type
Browse files Browse the repository at this point in the history
  • Loading branch information
koji committed May 15, 2024
1 parent 4db6477 commit 0c560d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
6 changes: 3 additions & 3 deletions opentrons-ai-client/src/molecules/ChatDisplay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ChatDisplayProps {
export function ChatDisplay({ chat, chatId }: ChatDisplayProps): JSX.Element {
const { t } = useTranslation('protocol_generator')
const [isCopied, setIsCopied] = React.useState<boolean>(false)
const { role, content } = chat
const { role, reply } = chat
const isUser = role === 'user'

const handleClickCopy = async (): Promise<void> => {
Expand Down Expand Up @@ -62,7 +62,7 @@ export function ChatDisplay({ chat, chatId }: ChatDisplayProps): JSX.Element {
>
<Markdown
components={{
div: undefined,
div: ParagraphText,
ul: UnnumberedListText,
h2: HeaderText,
li: ListItemText,
Expand All @@ -71,7 +71,7 @@ export function ChatDisplay({ chat, chatId }: ChatDisplayProps): JSX.Element {
code: CodeText,
}}
>
{content}
{reply}
</Markdown>
{role === 'assistant' ? (
<PrimaryButton
Expand Down
36 changes: 15 additions & 21 deletions opentrons-ai-client/src/molecules/InputPrompt/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export function InputPrompt(): JSX.Element {

const [data, setData] = React.useState<any>(null)
const [loading, setLoading] = React.useState<boolean>(false)
const [error, setError] = React.useState<string>('')
// ToDo (kk:05/15/2024) this will be used in the future
// const [error, setError] = React.useState<string>('')

const { getAccessTokenWithPopup } = useAuth0()
const { getAccessTokenSilently } = useAuth0()

const userPrompt = watch('userPrompt') ?? ''

Expand All @@ -52,35 +53,28 @@ export function InputPrompt(): JSX.Element {
return rowsNum
}

// const isLocalhost = (): boolean => {
// const { hostname } = window.location
// return hostname === 'localhost' || hostname === '127.0.0.1'
// }

const fetchData = async (prompt: string): Promise<void> => {
if (prompt !== '') {
setLoading(true)
try {
const token = await getAccessTokenWithPopup({
const accessToken = await getAccessTokenSilently({
authorizationParams: {
audience: 'sandbox-ai-api',
},
})
console.log('token', token)
const postData = {
message: prompt,
fake: false,
}
const response = await axios.post(url, {
postData,
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
})
const headers = {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
}
const response = await axios.post(url, postData, { headers })
setData(response.data)
} catch (err) {
setError('Error fetching data from the API.')
// setError('Error fetching data from the API.')
console.error(`error: ${err}`)
} finally {
setLoading(false)
}
Expand All @@ -90,7 +84,7 @@ export function InputPrompt(): JSX.Element {
const handleClick = (): void => {
const userInput: ChatData = {
role: 'user',
content: userPrompt,
reply: userPrompt,
}
setChatData(chatData => [...chatData, userInput])
void fetchData(userPrompt)
Expand All @@ -104,18 +98,18 @@ export function InputPrompt(): JSX.Element {

React.useEffect(() => {
if (submitted && data && !loading) {
const { role, content } = data.data
const { role, reply } = data
const assistantResponse: ChatData = {
role,
content,
reply,
}
setChatData(chatData => [...chatData, assistantResponse])
setSubmitted(false)
}
}, [data, loading, submitted])

// ToDo (kk:05/02/2024) This is also temp. Asking the design about error.
console.error('error', error)
// console.error('error', error)

return (
<StyledForm id="User_Prompt">
Expand Down
4 changes: 3 additions & 1 deletion opentrons-ai-client/src/resources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@ export interface ChatData {
/** assistant: ChatGPT API, user: user */
role: 'assistant' | 'user'
/** content ChatGPT API return or user prompt */
content: string
// content: string
reply: string
fake?: boolean
}

0 comments on commit 0c560d1

Please sign in to comment.