-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Uniformize message prompt into <MessageWrapper/>
- Loading branch information
Showing
4 changed files
with
141 additions
and
137 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
125 changes: 125 additions & 0 deletions
125
src/chainlit/frontend/src/components/organisms/playground/editor/MessageWrapper.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,125 @@ | ||
import React from 'react'; | ||
import { useSetRecoilState } from 'recoil'; | ||
import { useToggle } from 'usehooks-ts'; | ||
|
||
import { | ||
Box, | ||
SelectChangeEvent, | ||
Stack, | ||
Theme, | ||
Typography | ||
} from '@mui/material'; | ||
|
||
import SelectInput from 'components/organisms/inputs/selectInput'; | ||
|
||
import { IPromptMessage, PromptMessageRole } from 'state/chat'; | ||
import { playgroundState } from 'state/playground'; | ||
|
||
const roles = ['Assistant', 'System', 'User']; | ||
|
||
interface MessageWrapperProps { | ||
canSelectRole?: boolean; | ||
children: React.ReactElement; | ||
index?: number; | ||
message?: IPromptMessage; | ||
role?: string; | ||
} | ||
|
||
const MessageWrapper = ({ | ||
canSelectRole, | ||
children, | ||
index, | ||
message, | ||
role | ||
}: MessageWrapperProps): JSX.Element => { | ||
const setPlayground = useSetRecoilState(playgroundState); | ||
const [isSelectRoleOpen, toggleSelectRole] = useToggle(false); | ||
|
||
const onRoleSelected = (event: SelectChangeEvent) => { | ||
const role = event.target.value as PromptMessageRole; | ||
|
||
if (role) { | ||
setPlayground((old) => ({ | ||
...old, | ||
prompt: { | ||
...old.prompt!, | ||
messages: old.prompt?.messages?.map((message, mIndex) => ({ | ||
...message, | ||
...(mIndex === index ? { role } : {}) // Update role if it's the selected message | ||
})) | ||
} | ||
})); | ||
} | ||
|
||
toggleSelectRole(); | ||
}; | ||
|
||
return ( | ||
<Box key={index}> | ||
<Box | ||
sx={{ | ||
border: (theme) => `1px solid ${theme.palette.divider}`, | ||
borderRadius: 1 | ||
}} | ||
/> | ||
<Stack | ||
direction="row" | ||
width="100%" | ||
sx={{ | ||
gap: 2, | ||
flex: 10, | ||
paddingY: 1, | ||
'&:hover': { | ||
background: (theme: Theme) => theme.palette.background.paper | ||
} | ||
}} | ||
> | ||
<Box sx={{ flex: 1, paddingLeft: 2 }}> | ||
{!isSelectRoleOpen ? ( | ||
<Typography | ||
onClick={() => canSelectRole && toggleSelectRole()} | ||
color="text.primary" | ||
sx={{ | ||
p: 1, | ||
borderRadius: 0.5, | ||
marginTop: 1, | ||
cursor: canSelectRole ? 'pointer' : 'default', | ||
fontSize: '12px', | ||
fontWeight: 700, | ||
width: 'fit-content', | ||
...(canSelectRole && { | ||
'&:hover': { | ||
backgroundColor: (theme) => theme.palette.divider | ||
} | ||
}) | ||
}} | ||
> | ||
{role} | ||
</Typography> | ||
) : ( | ||
<SelectInput | ||
defaultOpen | ||
items={roles.map((role) => ({ | ||
label: role, | ||
value: role.toLowerCase() | ||
}))} | ||
id="role-select" | ||
value={message?.role} | ||
onChange={onRoleSelected} | ||
sx={{ width: 'fit-content' }} | ||
iconSx={{ | ||
px: 0, | ||
marginRight: '2px !important' | ||
}} | ||
/> | ||
)} | ||
</Box> | ||
<Box width="100%" flex={8}> | ||
{children} | ||
</Box> | ||
</Stack> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default MessageWrapper; |
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