| 1 |
import { recordAgentActivity } from '@agent/api'; |
| 2 |
import { AnimateChunks } from '@agent/components/messages/AnimateChunks'; |
| 3 |
import { magic } from '@agent/icons'; |
| 4 |
import pageTours from '@agent/lib/page-tours'; |
| 5 |
import tours from '@agent/tours/tours'; |
| 6 |
import { SingleTour } from '@agent/workflows/misc/components/ToursList'; |
| 7 |
import { decodeEntities } from '@wordpress/html-entities'; |
| 8 |
import { __ } from '@wordpress/i18n'; |
| 9 |
import { cog, Icon, lifesaver, pencil, styles } from '@wordpress/icons'; |
| 10 |
import ReactMarkdown from 'react-markdown'; |
| 11 |
|
| 12 |
const availableTours = Object.values(tours); |
| 13 |
const adminPages = window.extAgentData.agentContext?.availableAdminPages || []; |
| 14 |
const agentSuggestions = |
| 15 |
Object.fromEntries( |
| 16 |
adminPages.map((page) => [ |
| 17 |
page, |
| 18 |
{ |
| 19 |
label: __('Take me there', 'extendify-local'), |
| 20 |
tour: |
| 21 |
availableTours.find((tour) => tour.id === pageTours[page]) ?? null, |
| 22 |
}, |
| 23 |
]), |
| 24 |
) || {}; |
| 25 |
|
| 26 |
const agentIcons = { |
| 27 |
agent1: lifesaver, |
| 28 |
agent2: styles, |
| 29 |
agent3: pencil, |
| 30 |
agent4: cog, |
| 31 |
}; |
| 32 |
|
| 33 |
export const AgentMessage = ({ message, animate }) => { |
| 34 |
const { |
| 35 |
content, |
| 36 |
role, |
| 37 |
pageSuggestion, |
| 38 |
agent, |
| 39 |
sessionId = 'not-set', |
| 40 |
} = message.details; |
| 41 |
const containsCodeBlock = /```[\s\S]*?```/.test(content); |
| 42 |
const blocks = containsCodeBlock |
| 43 |
? [decodeEntities(content ?? '')] |
| 44 |
: decodeEntities(content ?? '').split(/\n{2,}/); |
| 45 |
|
| 46 |
// Check if the pageSuggestion matches any key in agentSuggestions |
| 47 |
const agentSuggestionKey = Object.keys(agentSuggestions).find((k) => |
| 48 |
pageSuggestion?.startsWith(k), |
| 49 |
); |
| 50 |
const agentSuggestion = agentSuggestions[agentSuggestionKey] || {}; |
| 51 |
|
| 52 |
return ( |
| 53 |
<div |
| 54 |
data-agent-message-role={role} |
| 55 |
className="flex w-full items-start gap-2.5 p-2" |
| 56 |
> |
| 57 |
<div className="w-7 shrink-0"> |
| 58 |
{agent?.avatar ? ( |
| 59 |
<img className="mt-px" src={agent.avatar} alt={agent.name} /> |
| 60 |
) : ( |
| 61 |
<Icon |
| 62 |
className="-mt-0.5 fill-gray-900" |
| 63 |
icon={agentIcons[agent?.id] ?? magic} |
| 64 |
size={28} |
| 65 |
/> |
| 66 |
)} |
| 67 |
</div> |
| 68 |
<div className="flex min-w-0 flex-1 flex-col gap-4"> |
| 69 |
<div className="extendify-agent-markdown w-full"> |
| 70 |
{animate ? ( |
| 71 |
<AnimateChunks words={blocks} delay={0.1} duration={0.35} /> |
| 72 |
) : ( |
| 73 |
<ReactMarkdown>{decodeEntities(content)}</ReactMarkdown> |
| 74 |
)} |
| 75 |
</div> |
| 76 |
{agentSuggestion?.label ? ( |
| 77 |
<div> |
| 78 |
<a |
| 79 |
onClick={() => { |
| 80 |
recordAgentActivity({ |
| 81 |
sessionId, |
| 82 |
action: 'take_me_there_click', |
| 83 |
value: { pageSuggestion }, |
| 84 |
}); |
| 85 |
}} |
| 86 |
href={`${window.extSharedData.adminUrl}${pageSuggestion}`} |
| 87 |
className="rounded-sm border border-design-main bg-design-main p-2 text-sm text-white no-underline hover:opacity-90" |
| 88 |
> |
| 89 |
{agentSuggestion.label} |
| 90 |
</a> |
| 91 |
</div> |
| 92 |
) : null} |
| 93 |
{agentSuggestion?.tour && ( |
| 94 |
<div> |
| 95 |
<SingleTour tour={agentSuggestion.tour} /> |
| 96 |
</div> |
| 97 |
)} |
| 98 |
</div> |
| 99 |
</div> |
| 100 |
); |
| 101 |
}; |
| 102 |
|