-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(chat): add draft action buttons flow
- Loading branch information
Magomed-Elbi Dzhukalaev
committed
Dec 19, 2024
1 parent
e6803f4
commit bf1f7a5
Showing
6 changed files
with
253 additions
and
36 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
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
133 changes: 110 additions & 23 deletions
133
apps/chat/src/components/Chat/ChatMessage/MessageFormSchema/MessageFormSchema.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
105 changes: 105 additions & 0 deletions
105
apps/chat/src/components/Chat/ChatMessage/UserMessageContent.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,105 @@ | ||
import classNames from 'classnames'; | ||
|
||
import { isSmallScreen } from '@/src/utils/app/mobile'; | ||
|
||
import { useAppSelector } from '@/src/store/hooks'; | ||
import { SettingsSelectors } from '@/src/store/settings/settings.reducers'; | ||
import { UISelectors } from '@/src/store/ui/ui.reducers'; | ||
|
||
import { | ||
DialMessageFormSchema, | ||
DialWidgets, | ||
FormSchemaPropertyValue, | ||
Message, | ||
} from '@epam/ai-dial-shared'; | ||
|
||
const isFormActionReply = ( | ||
message: Message, | ||
index: number, | ||
allMessages: Message[], | ||
) => { | ||
if (index === 0 || !allMessages[index - 1]?.custom_content?.form_schema) | ||
return false; | ||
|
||
try { | ||
if (JSON.parse(message.content)) return true; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
const getFormActionReplyWidgets = ( | ||
message: Message, | ||
index: number, | ||
allMessages: Message[], | ||
) => { | ||
if (!isFormActionReply(message, index, allMessages)) return []; | ||
const schema = allMessages[index - 1]?.custom_content | ||
?.form_schema as DialMessageFormSchema; | ||
const parsedReply: Record<string, FormSchemaPropertyValue> = JSON.parse( | ||
message.content, | ||
); | ||
|
||
return Object.entries(schema.properties).map(([key, value]) => ({ | ||
property: key, | ||
widget: value['dial:widget'], | ||
value: parsedReply[key], | ||
label: value.oneOf?.find((option) => option.const === parsedReply[key]) | ||
?.title, | ||
})); | ||
}; | ||
|
||
interface UserMessageContentProps { | ||
message: Message; | ||
messageIndex: number; | ||
allMessages: Message[]; | ||
} | ||
|
||
export const UserMessageContent = ({ | ||
message, | ||
messageIndex, | ||
allMessages, | ||
}: UserMessageContentProps) => { | ||
const isChatFullWidth = useAppSelector(UISelectors.selectIsChatFullWidth); | ||
const isOverlay = useAppSelector(SettingsSelectors.selectIsOverlay); | ||
const isMobileOrOverlay = isSmallScreen() || isOverlay; | ||
|
||
const isFormActionReplyMsg = isFormActionReply( | ||
message, | ||
messageIndex, | ||
allMessages, | ||
); | ||
const userReplyProperties = getFormActionReplyWidgets( | ||
message, | ||
messageIndex, | ||
allMessages, | ||
); | ||
|
||
return ( | ||
<div | ||
className={classNames('prose min-w-full flex-1 whitespace-pre-wrap', { | ||
'max-w-none': isChatFullWidth, | ||
'text-sm': isOverlay, | ||
'leading-[150%]': isMobileOrOverlay, | ||
})} | ||
> | ||
{isFormActionReplyMsg ? ( | ||
<div className="flex items-center gap-2"> | ||
{userReplyProperties | ||
.filter(({ widget }) => widget === DialWidgets.buttons) | ||
.map((property) => ( | ||
<button | ||
key={property.property} | ||
className="button button-secondary" | ||
disabled | ||
> | ||
{property.label ?? property.value} | ||
</button> | ||
))} | ||
</div> | ||
) : ( | ||
message.content | ||
)} | ||
</div> | ||
); | ||
}; |
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