Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(aio): mount one message actions menu (#83114)

Paul D'Ambra5407d1488b74
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 12fe30769dfb..b48d4f205d2d 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -1641,9 +1641,9 @@ snapshots:
components-playerinspector-itemdoctor--snapshot-type-counts--light:
hash: v1.k794b7964.0f18d95f7773bbf995f86cf13254ff41ddc6b11d1d3b5c0641f60365355e3145.eUs-pGedPj1ElXgGW6VAnNTrZ0vEr4u1doXrtaTdy6Y
components-playerinspector-itemevent--ai-generation-event--dark:
- hash: v1.k794b7964.f991db692d775d339e8bed8d12c533f02207ac0d3f3b7adbe630fe0c541e42ce.zxpSfaxS2eHjjTXhplStJfs3cAjOZD0ftIkQao3wiv8
+ hash: v1.k794b7964.2db6a7f5781e14fc5a59f069d66b2744c5277538b088174fe726498986759896.rB-LOp-yHTBzmG8AQ2JyqmRpiBFOxW-QbrJClJlSjF8
components-playerinspector-itemevent--ai-generation-event--light:
- hash: v1.k794b7964.a82cde756818d5bfc854eebebf0f5cdba274e2f6f29b5e7382a969b29dc0f80f.qdXT4jN2t39G6YO37Y8td0VDvRQjrxjaTTh7E_N2Nhk
+ hash: v1.k794b7964.61e65ea3eb5fe542f9bd91b7ee9b0b3a16442f13bbdf2e81bf41747fb24e10da.XnbgMKlrsPZqakfdMBa-FZy9sfHOU0LvNH8nz5Z7j6A
components-playerinspector-itemevent--ai-span-event--dark:
hash: v1.k794b7964.c5c56c80712339d4648486695cd7018284ac9bda6b7c7689900e5ec52a56cac5.p_Y5dQcQADN_IzSF_08ADaFMVXDlQsfoTunAuGucO80
components-playerinspector-itemevent--ai-span-event--light:
@@ -6013,37 +6013,37 @@ snapshots:
scenes-app-ai-observability-clustercard--without-metrics--light:
hash: v1.k794b7964.c5ee6757d060a3942b3c811cf3302d5619ed060659e6629503a3128f431ef919.EaDgjcrVCZGQSn5oAcspURsPpm6QZu6rr2LCHmgYNyg
scenes-app-ai-observability-conversation-display--anthropic--dark:
- hash: v1.k794b7964.87f4440b5a5bd8a370d835987f5422f4e4d8d2ea8086bdd85ad1264bc81c85d7.SCSsIud9YJ1GO35Sng0mwALj3XtFJhvs5f8UKgEn5AU
+ hash: v1.k794b7964.6c0c17698de15753642ffae47294cec53db8d7e6e4751830dd8983fd03bfc651.008raY9qkW-UWHLyUXplV3iqN6IKZMJE_-jaFl5QW3M
scenes-app-ai-observability-conversation-display--anthropic--light:
- hash: v1.k794b7964.153d5fe9eafbb67eb746a3ff3f516c67e65a84542325171764b68f6effbc3393.1EGkU0sySBFFoIO7iHUB6iLLycQqtjlgq7wInrBMgLc
+ hash: v1.k794b7964.4c78db14e941e0e41d2394ddf76a39374619c0b659d028a1037083505089d4fb.zgA0ou2ogDTrfur3JSMCifaBvxJvWaV0eJSHD6MuBDI
scenes-app-ai-observability-conversation-display--basic--dark:
- hash: v1.k794b7964.9ea11684de93cf0bf2fabc01d05a7b4fed8d0e540084b01ec27ab8a29d0f3823.vJCRxMCRSHVE9xVry1hOYTeYimJdH1PyeD0guOSQu4k
+ hash: v1.k794b7964.a0ee60944d3781ed6bb692a5f9a52e40684b97c001f5ec6b7d6663650c2d89d7.Kv2RlnDI7VmQ3R_wu0wsIKhtuKAVYT9Kwv7W9TTSfN4
scenes-app-ai-observability-conversation-display--basic--light:
- hash: v1.k794b7964.3cb142d720e0576d3b92310d75f791cf42452f5f32d38674f79d434790589239.7q-Lw22M7R633hrsN-ZnuIgkAWP9l4i9iRbu5jpnOG8
+ hash: v1.k794b7964.7412535b2a344703c43c6c14c8b6cb357cbe479e505f22583260b4e39f670946.mFWCqbQ4T_vl7VHpBAeFg_4SAU36_7dRHU81vjiydrs
scenes-app-ai-observability-conversation-display--empty-output-blocked-by-provider--dark:
- hash: v1.k794b7964.05125a80c55c9ad20c8edcc84bf05e1f58ff673ca1a2abbef34ced9ec3552b9d.mah6RylH_kokmN1fnEjxZQD4Ps3QUaffSekfCz5N6i4
+ hash: v1.k794b7964.e70edbfda151acebc94f9478737f49a74e56f3bb8e99e592d1ff3ab166dc9946.-pqrliCWW3aBo2r1b-JPQ3hXMpglJ-2BWzjzHsM5Obk
scenes-app-ai-observability-conversation-display--empty-output-blocked-by-provider--light:
- hash: v1.k794b7964.1f86f5ba9536b50c817ca0a3c0d55548c14b8f4a8ff13b504c3f7b150e012ada.4ZWD_3VA2AijSiEbq-_jE96K3tcgkw3HHuu_Ux3IFfQ
+ hash: v1.k794b7964.fbd3604d315716fa2ce1bc03915226c5499dad258440bba7a9b4e162627096a4.I_i41lueuqiVrDNStBZQ014EG2kGpHCSOfskOebPhyc
scenes-app-ai-observability-conversation-display--empty-output-hit-token-limit--dark:
- hash: v1.k794b7964.b2548b108edc6f8c958940e1fc0209b999b98438e0a6b6c095f3ac42a4cdd9f1.-411C02o_BaVwQeYWxv1oPT5d_J-6pawteYt4V80BAA
+ hash: v1.k794b7964.8c270f74357a3de5059ce153821191d4c363377304e96731d14ce5cfb2281080.dB9uHWOKK8VmNCrvogjV6XsTjLAVu5Y8bw1hMKQ2riI
scenes-app-ai-observability-conversation-display--empty-output-hit-token-limit--light:
- hash: v1.k794b7964.f8d093d6d02c6f147e06a6fbff6cbac04aa9cc29e7b1780a67c59b26681869c8.j7EwfprpifYVQrbjfTqvFjMSCeqtwfxhgfeLQQ_2lFk
+ hash: v1.k794b7964.05dd49a9933222d72ba4c3dc76754aa51ae9ded46baddf5b5d49dd16dbac54ee.oUruKAZTB5eyGbueGwZUycs1Q0cF7oCnLI2NlLZ4xEE
scenes-app-ai-observability-conversation-display--empty-output-with-billed-tokens--dark:
- hash: v1.k794b7964.42eeaa8552683109e50304840c0fe8907c4c0825b622ecd1d9745bc4f895d955.HGZ_kprIGe14sglkYwj1wNpS3hLSkGbZKw3tuUJTz5s
+ hash: v1.k794b7964.4c57b3a416a77d3df13b7fecb51e0338c1de87b3a2b1ab5cd3e6f54e9a2b0f9d.RSxXFC35N8hE4xQPYnWchQzK-Vfy4rGpmfBs4F7GmUs
scenes-app-ai-observability-conversation-display--empty-output-with-billed-tokens--light:
- hash: v1.k794b7964.b9e3adecf8f05139d76126faf07998b0a915fec481915e6a8bad3a97992dae68.4UFV8PthL3Y5pbeU3axDaPZIep6ixfatWlrzqW4JKm0
+ hash: v1.k794b7964.aeccad60926ee25db8a356c246b3961eecb041cc3a36a48287c577edcef3b1f5.OXuf6QwD27Vm83Jqg12aCxaF88Cm5wVpaJb0XLIHkos
scenes-app-ai-observability-conversation-display--empty-output-with-reasoning-tokens--dark:
- hash: v1.k794b7964.9f1da3403eb752b5499e096ed36633d7c8fff2e69b7ba3696354e664bde897ff.N7wmsys9CZDuETYl1x-hqLouyIimaxUWLRWc9DZYgjY
+ hash: v1.k794b7964.e92d2044b26126a64ad0a5159b8af6626c73d4fcc4a3af281bac883e1f7fe579.QsmfK94pGqhQLZAimaAHTFPvXffZHZGIwevYhkSFBpU
scenes-app-ai-observability-conversation-display--empty-output-with-reasoning-tokens--light:
- hash: v1.k794b7964.34eabfa265ecc45640d754647a5ad836ef95a3d32c1885391c94af7865d2fe18.N2i_3GzO4MpTXxY30ebKa5IiVNEIBVSbDgGepsfm4RI
+ hash: v1.k794b7964.26c00ba06edee3f80a76292d6b2bb2ca6a93c91da32fa23952687215777519da.qHiG7I94fSfEciOeJCTNHht0wHOgiPhTXKIjknRViH0
scenes-app-ai-observability-conversation-display--error--dark:
- hash: v1.k794b7964.477b40a99b407806f5a85839a94004a899be31370ea6861518dd11cf5c883c45.EQEZzP2enfzFprXAhyqSbD8Um6pvTD1cGa3yq6qn4VU
+ hash: v1.k794b7964.d74f381383c879030af8c433477c70041ebbcae9e220ef7af1196622b46cb8fc.lT97xEYZS1_cRfTbid1rmUSDABTVlrQELxeAsHH87BY
scenes-app-ai-observability-conversation-display--error--light:
- hash: v1.k794b7964.ac8507542eecfdaec76482cb232e677a0d0abe4b4c6ecdbac613ec4757cbfacd.jWQdUiUTDBIvtwDA-nQxlWMkAnWRT2mFrGrtmr3q_ys
+ hash: v1.k794b7964.b83393aac4d581ebffa8e67b7e2f7392840aa3a3f0ca3a8e46b885eda87446ec.IzMPDRME8DOyvzCHQvUFf4aawoDNXEf73qmstY2A2ug
scenes-app-ai-observability-conversation-display--markdown--dark:
- hash: v1.k794b7964.479524503d84d5795c027718559d41efcba3103a6a9e11a66854d4bedf300247.dkazWcFlzQhfdiEy_8W9vix5XwB2ROB6Z0k6S2vRKl4
+ hash: v1.k794b7964.ca8fc61f5255194dfe8ae8c3576f075575c81e1ae657aa0cde624a031654c0f3.dYwWpxUmI2YrXzO4XtgVF4tSQOIhqPG3m4Q79GoWDTI
scenes-app-ai-observability-conversation-display--markdown--light:
- hash: v1.k794b7964.031e91a251072760a2430dd98ba5e87374d77adebd27db1ed5ed08242d9b0537.FLdMLi2pEXuK08SP_gNnEB7gWrSV9iAYY6JMCD9xASI
+ hash: v1.k794b7964.fa46745e2339054f72e04d96f984a2d1614e73a017b34a89c74e25ffd6191090.Un38MvMNvx1UqHXKKua7Y9evJTVSgar5LNlKwTsvcg4
scenes-app-ai-observability-conversation-display--tools--dark:
hash: v1.k794b7964.c9cf0f699197f208ec18e3ad326c29d43b8034f37f5fc101a9e25773510895a5.f_qTgpzSlQlCRirQv0iD791ajpL-x8WnVi8T8qNYr1o
scenes-app-ai-observability-conversation-display--tools--light:
@@ -6189,7 +6189,7 @@ snapshots:
scenes-app-ai-observability-skills--skills-list--light:
hash: v1.k794b7964.be983851dd216dffb0f8cdd9df2de912fa9d477a821e66eee7b1f82bd0000ca6.aiGZU3HoH-JsbDcEul_7etKK6iVsTktb0ZxZ2_vmfOk
scenes-app-ai-observability-trace--full--dark:
- hash: v1.k794b7964.3ea0677df8e51f08cfd46ad1957c78cb0e0c14dff003b15959f9dc9434a239ca.GUZTGarCld-XKdmLqsfKRE1ZIWdWvfojoa2vHPGVVOA
+ hash: v1.k794b7964.142fc550717f2be0bc0c72f8b670fb5b70f1be8cf26ae2805e4a6ae6937c06a5.ms9_vuzQ5yFxSSCaQtMJLVHBMBR0fIKrrKJhivb5Zfg
scenes-app-ai-observability-trace--full--light:
hash: v1.k794b7964.57c11ed1fabef66c724244d8f8c10a48048451bfb8c20725e5a7db7e6f3e97fa.ECgSmPCIFu7JDolQEatnDs2tvmkmel9NzTvekgyGgpA
scenes-app-ai-observability-trace--full-specific-event--dark:
diff --git a/frontend/src/scenes/session-recordings/player/inspector/components/AIEventItems.tsx b/frontend/src/scenes/session-recordings/player/inspector/components/AIEventItems.tsx
index 94427a411b9b..ff1ba101e52d 100644
--- a/frontend/src/scenes/session-recordings/player/inspector/components/AIEventItems.tsx
+++ b/frontend/src/scenes/session-recordings/player/inspector/components/AIEventItems.tsx
@@ -12,7 +12,7 @@ import { selectAiValue } from 'products/ai_observability/frontend/utils'
export function AIEventExpanded({ event }: { event: Record<string, any> }): JSX.Element {
const { input, output, tools, isLoading } = useAIData({
- uuid: event.uuid,
+ uuid: event.id,
input: event.properties?.$ai_input,
output: selectAiValue(event.properties?.$ai_output_choices, event.properties?.$ai_output),
tools: event.properties?.$ai_tools,
@@ -41,6 +41,7 @@ export function AIEventExpanded({ event }: { event: Record<string, any> }): JSX.
textOutputTokens={event.properties.$ai_text_output_tokens}
stopReason={event.properties.$ai_stop_reason}
traceId={event.properties.$ai_trace_id}
+ eventId={event.id}
/>
) : (
<LLMInputOutput
diff --git a/products/ai_observability/frontend/AIObservabilityTraceScene.tsx b/products/ai_observability/frontend/AIObservabilityTraceScene.tsx
index ba7bead1c4d3..0f8fe97e6599 100644
--- a/products/ai_observability/frontend/AIObservabilityTraceScene.tsx
+++ b/products/ai_observability/frontend/AIObservabilityTraceScene.tsx
@@ -1444,11 +1444,15 @@ function TreeNodeChildren({
}
function EventContentDisplay({
+ eventId,
+ traceId,
input,
output,
searchQuery,
displayOption,
}: {
+ eventId: string
+ traceId: string
input: unknown
output: unknown
searchQuery?: string
@@ -1469,6 +1473,8 @@ function EventContentDisplay({
errorData={undefined}
searchQuery={searchQuery}
displayOption={displayOption}
+ eventId={eventId}
+ traceId={traceId}
/>
)
}
@@ -1832,6 +1838,8 @@ const EventContent = React.memo(
<>
<TraceMetricsTable />
<EventContentDisplay
+ eventId={event.id}
+ traceId={trace.id}
input={event.inputState}
output={event.outputState}
searchQuery={searchQuery}
diff --git a/products/ai_observability/frontend/ConversationDisplay/ConversationDisplay.tsx b/products/ai_observability/frontend/ConversationDisplay/ConversationDisplay.tsx
index abe886db111a..a00c69b1021d 100644
--- a/products/ai_observability/frontend/ConversationDisplay/ConversationDisplay.tsx
+++ b/products/ai_observability/frontend/ConversationDisplay/ConversationDisplay.tsx
@@ -106,7 +106,7 @@ export function ConversationDisplay({
stopReason={eventProperties.$ai_stop_reason}
bordered
traceId={eventProperties.$ai_trace_id}
- generationEventId={eventId}
+ eventId={eventId}
/>
)}
</>
diff --git a/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.test.tsx b/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.test.tsx
index 8877ff12deac..8e03821a46c2 100644
--- a/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.test.tsx
+++ b/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.test.tsx
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom'
-import { cleanup, render, screen, waitFor } from '@testing-library/react'
+import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { Provider } from 'kea'
import { initKeaTests } from '~/test/init'
@@ -12,6 +12,7 @@ import {
ImageMessageDisplay,
LLMMessageDisplay,
} from './ConversationMessagesDisplay'
+import { messageActionsMenuLogic } from './messageActionsMenuLogic'
// react-json-view is loaded via React.lazy, so the first render suspends on a code-split chunk.
// Under CI contention that resolve can exceed waitFor's 1s default, so give it headroom.
@@ -471,6 +472,82 @@ describe('ConversationMessagesDisplay', () => {
}
})
+ it('mounts one message actions menu only after a trigger is used', async () => {
+ const { container, rerender } = render(
+ <Provider>
+ <ConversationMessagesDisplay
+ inputNormalized={inputNormalized}
+ outputNormalized={outputNormalized}
+ errorData={null}
+ raisedError={false}
+ eventId="event-1"
+ />
+ </Provider>
+ )
+
+ const triggers = container.querySelectorAll<HTMLButtonElement>('[data-attr="llma-message-actions-trigger"]')
+
+ expect(triggers).toHaveLength(5)
+ expect(container.querySelectorAll('[aria-haspopup="true"]')).toHaveLength(5)
+ expect(container.querySelectorAll('[data-menu-mounted="true"]')).toHaveLength(0)
+
+ triggers[0].focus()
+ fireEvent.click(triggers[0])
+
+ expect(container.querySelectorAll('[data-menu-mounted="true"]')).toHaveLength(1)
+ expect(document.activeElement).toBe(container.querySelectorAll('[data-attr="llma-message-actions-trigger"]')[0])
+ expect(await screen.findByText('Translate')).toBeInTheDocument()
+
+ fireEvent.click(container.querySelectorAll('[data-attr="llma-message-actions-trigger"]')[1])
+
+ expect(container.querySelectorAll('[aria-haspopup="true"]')).toHaveLength(5)
+ expect(container.querySelectorAll('[data-menu-mounted="true"]')).toHaveLength(1)
+ expect(document.activeElement).toBe(container.querySelectorAll('[data-attr="llma-message-actions-trigger"]')[1])
+
+ rerender(
+ <Provider>
+ <ConversationMessagesDisplay
+ inputNormalized={inputNormalized}
+ outputNormalized={outputNormalized}
+ errorData={null}
+ raisedError={false}
+ eventId="event-2"
+ />
+ </Provider>
+ )
+
+ // A different conversation must clear the active menu key, or its matching
+ // message would mount with the menu already open and steal focus.
+ expect(container.querySelectorAll('[aria-haspopup="true"]')).toHaveLength(5)
+ expect(container.querySelectorAll('[data-menu-mounted="true"]')).toHaveLength(0)
+ })
+
+ it('keeps a translation when the shared menu moves to another message and back', () => {
+ const { container } = render(
+ <Provider>
+ <ConversationMessagesDisplay
+ inputNormalized={inputNormalized}
+ outputNormalized={outputNormalized}
+ errorData={null}
+ raisedError={false}
+ />
+ </Provider>
+ )
+ const getTriggers = (): NodeListOf<HTMLButtonElement> =>
+ container.querySelectorAll<HTMLButtonElement>('[data-attr="llma-message-actions-trigger"]')
+ const translation = { translation: 'contenido de entrada del sistema', targetLanguage: 'es' as const }
+
+ fireEvent.click(getTriggers()[0])
+ messageActionsMenuLogic.findMounted({ content: 'system input content' })?.actions.translateSuccess(translation)
+
+ fireEvent.click(getTriggers()[1])
+ fireEvent.click(getTriggers()[0])
+
+ expect(messageActionsMenuLogic.findMounted({ content: 'system input content' })?.values.translation).toEqual(
+ translation
+ )
+ })
+
it.each<[string, unknown, unknown, unknown, unknown, string | null]>([
[
'only output tokens were billed',
diff --git a/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.tsx b/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.tsx
index 6c388a3a5cb5..56597765645d 100644
--- a/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.tsx
+++ b/products/ai_observability/frontend/ConversationDisplay/ConversationMessagesDisplay.tsx
@@ -41,6 +41,7 @@ import {
import { HighlightedLemonMarkdown } from './HighlightedLemonMarkdown'
import { HighlightedXMLViewer } from './HighlightedXMLViewer'
import { MessageActionsMenu } from './MessageActionsMenu'
+import { MessageActionsMenuProvider } from './MessageActionsMenuProvider'
import { RedactedMediaPlaceholder } from './RedactedMediaPlaceholder'
import { XMLViewer } from './XMLViewer'
@@ -142,6 +143,7 @@ export function ConversationMessagesDisplay({
searchQuery,
displayOption,
traceId,
+ eventId,
generationSentiment,
highlightMessageIndex,
}: {
@@ -164,7 +166,7 @@ export function ConversationMessagesDisplay({
searchQuery?: string
displayOption?: ConversationDisplayOption
traceId?: string | null
- generationEventId?: string
+ eventId?: string
generationSentiment?: GenerationSentiment | null
/** Original $ai_input index to auto-expand and highlight (e.g. from sentiment tab deep link) */
highlightMessageIndex?: number | null
@@ -326,6 +328,7 @@ export function ConversationMessagesDisplay({
className={isHighlighted ? 'ring-2 ring-primary/30 rounded' : undefined}
>
<LLMMessageDisplay
+ actionMenuKey={`input-${i}`}
message={message}
show={inputMessageShowStates[i] || false}
onToggle={() => toggleMessage('input', i)}
@@ -359,7 +362,9 @@ export function ConversationMessagesDisplay({
: null
return (
- <>
+ <MessageActionsMenuProvider
+ resetKey={`${eventId ?? ''}:${traceId ?? ''}:${inputRolesSignature}:${outputRolesSignature}`}
+ >
<LLMInputOutput
inputDisplay={inputDisplay}
outputDisplay={
@@ -368,6 +373,7 @@ export function ConversationMessagesDisplay({
outputNormalized.map((message, i) => (
<LLMMessageDisplay
key={i}
+ actionMenuKey={`output-${i}`}
message={message}
show={outputMessageShowStates[i] || false}
isOutput
@@ -437,7 +443,7 @@ export function ConversationMessagesDisplay({
</div>
</div>
)}
- </>
+ </MessageActionsMenuProvider>
)
}
@@ -741,6 +747,7 @@ export const LLMMessageDisplay = React.memo(
onToggleMarkdownRendering,
onToggleXmlRendering,
messageSentiment,
+ actionMenuKey,
}: {
message: CompatMessage
isOutput?: boolean
@@ -756,6 +763,7 @@ export const LLMMessageDisplay = React.memo(
onToggleMarkdownRendering?: () => void
onToggleXmlRendering?: () => void
messageSentiment?: { label: string; score: number }
+ actionMenuKey?: string
}): JSX.Element => {
const { currentTeamId } = useValues(teamLogic)
const { role, content, ...additionalKwargs } = message
@@ -977,6 +985,7 @@ export const LLMMessageDisplay = React.memo(
explicitValue={typeof content === 'string' ? content : JSON.stringify(content)}
/>
<MessageActionsMenu
+ menuKey={actionMenuKey}
content={
typeof content === 'string' ? content : (JSON.stringify(content, null, 2) ?? '')
}
diff --git a/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenu.tsx b/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenu.tsx
index 505787c8fef2..c58ca886a944 100644
--- a/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenu.tsx
+++ b/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenu.tsx
@@ -1,4 +1,6 @@
-import { useActions, useValues } from 'kea'
+import { useActions, useMountedLogic, useValues } from 'kea'
+import { useEffect, useRef } from 'react'
+import type { MutableRefObject } from 'react'
import { IconEllipsis } from '@posthog/icons'
import { LemonMenu, LemonMenuItems } from '@posthog/lemon-ui'
@@ -12,6 +14,7 @@ import { sidePanelStateLogic } from '~/layout/navigation-3000/sidepanel/sidePane
import { AccessControlLevel, AccessControlResourceType, ActivityScope, SidePanelTab } from '~/types'
import { messageActionsMenuLogic } from './messageActionsMenuLogic'
+import { useMessageActionsMenuContext } from './MessageActionsMenuProvider'
import { TranslatePopover } from './TranslatePopover'
const MAX_EDITOR_RETRIES = 10
@@ -21,9 +24,18 @@ const MAX_QUOTE_LENGTH = 500
export interface MessageActionsMenuProps {
content: string
traceId?: string | null
+ menuKey?: string
}
-export const MessageActionsMenu = ({ content, traceId }: MessageActionsMenuProps): JSX.Element | null => {
+const ActiveMessageActionsMenu = ({
+ content,
+ traceId,
+ startVisible,
+ triggerRef,
+}: MessageActionsMenuProps & {
+ startVisible: boolean
+ triggerRef?: MutableRefObject<HTMLButtonElement | null>
+}): JSX.Element | null => {
const { openSidePanel } = useActions(sidePanelStateLogic)
const commentsLogicProps = {
scope: ActivityScope.LLM_TRACE,
@@ -36,10 +48,6 @@ export const MessageActionsMenu = ({ content, traceId }: MessageActionsMenuProps
const { showConsentPopover, dataProcessingAccepted } = useValues(logic)
const { setShowTranslatePopover, setShowConsentPopover } = useActions(logic)
- if (!content || content.trim().length === 0) {
- return null
- }
-
const insertQuoteIntoEditor = (quotedContent: string, retries = 0): void => {
// The logic can be unmounted before this deferred callback runs (e.g. the user navigates
// away or switches traces), so bail out rather than reading `.values` off a torn-down store.
@@ -115,10 +123,19 @@ export const MessageActionsMenu = ({ content, traceId }: MessageActionsMenuProps
setShowTranslatePopover(true)
}
+ // The popover anchors below are empty elements. Wrap them with the trigger so they do not become
+ // gap-separated items of the toolbar row, which would shift this button relative to inactive ones.
return (
- <>
- <LemonMenu items={menuItems} placement="bottom-end">
- <LemonButton size="small" noPadding icon={<IconEllipsis />} tooltip="More actions" />
+ <div className="flex">
+ <LemonMenu ref={triggerRef} items={menuItems} placement="bottom-end" startVisible={startVisible}>
+ <LemonButton
+ size="small"
+ noPadding
+ icon={<IconEllipsis />}
+ tooltip="More actions"
+ data-attr="llma-message-actions-trigger"
+ data-menu-mounted="true"
+ />
</LemonMenu>
{/* AI consent popover - shown first if user hasn't consented */}
@@ -133,6 +150,54 @@ export const MessageActionsMenu = ({ content, traceId }: MessageActionsMenuProps
{/* Translate popover - only shown after consent */}
<TranslatePopover content={content} title="Translate message" />
- </>
+ </div>
+ )
+}
+
+export function MessageActionsMenu({
+ content,
+ traceId,
+ menuKey = 'standalone',
+}: MessageActionsMenuProps): JSX.Element | null {
+ const sharedMenu = useMessageActionsMenuContext()
+ const triggerRef = useRef<HTMLButtonElement | null>(null)
+ const isActive = !sharedMenu || sharedMenu.activeMenuKey === menuKey
+ const shouldRestoreFocus = !!sharedMenu && isActive
+ // Only the active menu renders the translation UI, but its logic must outlive the menu, or a
+ // translation is lost and fetched again each time the shared menu moves to another message.
+ useMountedLogic(messageActionsMenuLogic({ content }))
+
+ useEffect(() => {
+ if (shouldRestoreFocus) {
+ triggerRef.current?.focus()
+ }
+ }, [shouldRestoreFocus])
+
+ if (!content || content.trim().length === 0) {
+ return null
+ }
+
+ if (!isActive) {
+ return (
+ <LemonButton
+ ref={triggerRef}
+ size="small"
+ noPadding
+ icon={<IconEllipsis />}
+ tooltip="More actions"
+ aria-haspopup="true"
+ data-attr="llma-message-actions-trigger"
+ onClick={() => sharedMenu?.setActiveMenuKey(menuKey)}
+ />
+ )
+ }
+
+ return (
+ <ActiveMessageActionsMenu
+ content={content}
+ traceId={traceId}
+ startVisible={!!sharedMenu}
+ triggerRef={triggerRef}
+ />
)
}
diff --git a/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenuProvider.tsx b/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenuProvider.tsx
new file mode 100644
index 000000000000..cfb229ea8df7
--- /dev/null
+++ b/products/ai_observability/frontend/ConversationDisplay/MessageActionsMenuProvider.tsx
@@ -0,0 +1,37 @@
+import { ReactNode, createContext, useContext, useRef, useState } from 'react'
+
+interface MessageActionsMenuContextValue {
+ activeMenuKey: string | null
+ setActiveMenuKey: (menuKey: string) => void
+}
+
+const MessageActionsMenuContext = createContext<MessageActionsMenuContextValue | null>(null)
+
+export function useMessageActionsMenuContext(): MessageActionsMenuContextValue | null {
+ return useContext(MessageActionsMenuContext)
+}
+
+export function MessageActionsMenuProvider({
+ resetKey,
+ children,
+}: {
+ /** Identity of the displayed conversation. When it changes, the active menu key is cleared. */
+ resetKey?: string
+ children: ReactNode
+}): JSX.Element {
+ const [activeMenuKey, setActiveMenuKey] = useState<string | null>(null)
+ const previousResetKeyRef = useRef(resetKey)
+
+ // Clear during render, not in an effect, so a key left over from the previous
+ // conversation can never reach a newly mounted menu and auto-open it.
+ if (previousResetKeyRef.current !== resetKey) {
+ previousResetKeyRef.current = resetKey
+ setActiveMenuKey(null)
+ }
+
+ return (
+ <MessageActionsMenuContext.Provider value={{ activeMenuKey, setActiveMenuKey }}>
+ {children}
+ </MessageActionsMenuContext.Provider>
+ )
+}
diff --git a/products/ai_observability/frontend/components/EventContentWithAsyncData.tsx b/products/ai_observability/frontend/components/EventContentWithAsyncData.tsx
index d52dfce0b9ac..b0cfe3f1c136 100644
--- a/products/ai_observability/frontend/components/EventContentWithAsyncData.tsx
+++ b/products/ai_observability/frontend/components/EventContentWithAsyncData.tsx
@@ -154,6 +154,7 @@ export function EventContentConversation({
searchQuery={searchQuery}
displayOption={displayOption}
traceId={traceId}
+ eventId={eventId}
generationSentiment={generationSentiment}
highlightMessageIndex={highlightMessageIndex}
/>