: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(tasks): hide injected blocks and render mentions in web user messages (#109686)
Shy Alter66b274f430d1
diff --git a/frontend/src/lib/components/AgentObjectTags/rewriteAgentObjectTags.ts b/frontend/src/lib/components/AgentObjectTags/rewriteAgentObjectTags.tsindex 545d2cf41ac0..0a204bd2abef 100644--- a/frontend/src/lib/components/AgentObjectTags/rewriteAgentObjectTags.ts+++ b/frontend/src/lib/components/AgentObjectTags/rewriteAgentObjectTags.ts@@ -47,7 +47,7 @@ const XML_ENTITIES: Record<string, string> = {}const RE_XML_ENTITY = /&(?:quot|apos|lt|gt|amp);/g-function unescapeXml(value: string): string {+export function unescapeXml(value: string): string {return value.replace(RE_XML_ENTITY, (entity) => XML_ENTITIES[entity])}diff --git a/products/posthog_ai/frontend/components/ThreadRow.tsx b/products/posthog_ai/frontend/components/ThreadRow.tsxindex 37a5f6611d36..bd041f1b2306 100644--- a/products/posthog_ai/frontend/components/ThreadRow.tsx+++ b/products/posthog_ai/frontend/components/ThreadRow.tsx@@ -17,6 +17,7 @@ import { MessageTemplate } from '../messages/MessageTemplate'import { ReasoningAnswer } from '../messages/ReasoningAnswer'import type { ProgressStep, ThreadItem } from '../types/streamTypes'import { resolveToolCall } from '../utils/toolResolver'+import { userMessageDisplayText } from '../utils/userMessageDisplay'import { Activity } from './ActivityPrimitives'import { QuillAssistantMessage, QuillHumanMessage } from './quill/QuillMessages'import { useQuillThread } from './quill/quillThreadContext'@@ -152,13 +153,14 @@ export const ThreadRow = memo(function ThreadRow({if (quill) {return <QuillHumanMessage item={item} />}+ const text = userMessageDisplayText(item.text ?? '')return (<MessageTemplatetype="human"className="group"- action={<HumanMessageFooter startedAt={item.startedAt} text={item.text} />}+ action={<HumanMessageFooter startedAt={item.startedAt} text={text} />}>- <MarkdownMessage content={item.text || '*No text.*'} id={item.id} />+ <MarkdownMessage content={text || '*No text.*'} id={item.id} />{item.attachments && <ThreadAttachments attachments={item.attachments} />}</MessageTemplate>)diff --git a/products/posthog_ai/frontend/components/quill/QuillMessages.tsx b/products/posthog_ai/frontend/components/quill/QuillMessages.tsxindex 37f233fbe126..a79a552d192e 100644--- a/products/posthog_ai/frontend/components/quill/QuillMessages.tsx+++ b/products/posthog_ai/frontend/components/quill/QuillMessages.tsx@@ -14,6 +14,7 @@ import { TZLabel } from 'lib/components/TZLabel'import { MarkdownMessage } from '../../messages/MarkdownMessage'import type { ThreadItem } from '../../types/streamTypes'+import { userMessageDisplayText } from '../../utils/userMessageDisplay'import { ThreadAttachments } from '../ThreadAttachments'import { QuillCopyButton } from './QuillFooterButton'@@ -64,7 +65,7 @@ function ClampedContent({ children }: { children: ReactNode }): JSX.Element {}export const QuillHumanMessage = memo(function QuillHumanMessage({ item }: { item: ThreadItem }): JSX.Element {- const text = item.text ?? ''+ const text = userMessageDisplayText(item.text ?? '')return (<ChatMessage align="end" className="group" data-attr="posthog-ai-human-message"><ChatMessageContent className="gap-1">diff --git a/products/posthog_ai/frontend/utils/userMessageDisplay.test.ts b/products/posthog_ai/frontend/utils/userMessageDisplay.test.tsnew file mode 100644index 000000000000..cf40190cbd39--- /dev/null+++ b/products/posthog_ai/frontend/utils/userMessageDisplay.test.ts@@ -0,0 +1,22 @@+import { userMessageDisplayText } from './userMessageDisplay'++describe('userMessageDisplayText', () => {+ it.each([+ ['plain text', 'Fix the flaky test', 'Fix the flaky test'],+ [+ 'injected blocks',+ 'Ship it\n<user_custom_instructions>The user has saved custom instructions that apply to all of their tasks. Follow them.\nUse pnpm</user_custom_instructions>\n<channel_context channel="web">Notes</channel_context>',+ 'Ship it',+ ],+ [+ 'a pull request mention',+ '<github_pr number="12" title="Fix [login] & logout" url="https://github.com/example/repo/pull/12" />\nReview this',+ '[#12 - Fix login & logout](https://github.com/example/repo/pull/12)\nReview this',+ ],+ ['an issue mention without a url', 'See <github_issue number="7" />', 'See #7'],+ ['a file mention', 'Read <file path="src/app/main.ts" />', 'Read `app/main.ts`'],+ ['a folder mention', 'Open <folder path="src/app" />', 'Open `app/`'],+ ])('renders %s', (_, text, expected) => {+ expect(userMessageDisplayText(text)).toEqual(expected)+ })+})diff --git a/products/posthog_ai/frontend/utils/userMessageDisplay.ts b/products/posthog_ai/frontend/utils/userMessageDisplay.tsnew file mode 100644index 000000000000..0594a5677a1d--- /dev/null+++ b/products/posthog_ai/frontend/utils/userMessageDisplay.ts@@ -0,0 +1,39 @@+import { unescapeXml } from 'lib/components/AgentObjectTags/rewriteAgentObjectTags'++import { stripInjectedBlocks } from 'products/tasks/frontend/spaces/spaceFeedPreview'++const MENTION_TAG =+ /<file\s+path="([^"]+)"\s*\/>|<(github_issue|github_pr)\s+number="([^"]+)"(?:\s+title="([^"]*)")?(?:\s+url="([^"]*)")?\s*\/>|<error_context\s+label="([^"]*)">[\s\S]*?<\/error_context>|<folder\s+path="([^"]+)"\s*\/>|<comment_context\s+label="([^"]*)"(?:\s+screenshot="[^"]*")?>[\s\S]*?<\/comment_context>/g++function lastSegments(path: string, count: number): string {+ return path.split('/').filter(Boolean).slice(-count).join('/') || path+}++function codeSpan(text: string): string {+ return `\`${text.replace(/`/g, "'")}\``+}++function mentionToMarkdown(match: string[]): string {+ const [, file, githubKind, number, title, url, errorLabel, folder, commentLabel] = match+ if (file) {+ return codeSpan(lastSegments(unescapeXml(file), 2))+ }+ if (githubKind) {+ const label = `#${number}${title ? ` - ${unescapeXml(title)}` : ''}`.replace(/[[\]]/g, '')+ return url ? `[${label}](${unescapeXml(url)})` : label+ }+ if (errorLabel) {+ return codeSpan(unescapeXml(errorLabel))+ }+ if (folder) {+ return codeSpan(`${lastSegments(unescapeXml(folder), 1)}/`)+ }+ if (commentLabel !== undefined) {+ return codeSpan(unescapeXml(commentLabel) || 'Comment')+ }+ return match[0]+}++export function userMessageDisplayText(text: string): string {+ return stripInjectedBlocks(text).replace(MENTION_TAG, (...match: string[]) => mentionToMarkdown(match))+}diff --git a/products/tasks/frontend/spaces/spaceFeedPreview.ts b/products/tasks/frontend/spaces/spaceFeedPreview.tsindex 310dece14bc5..f44c6752d69a 100644--- a/products/tasks/frontend/spaces/spaceFeedPreview.ts+++ b/products/tasks/frontend/spaces/spaceFeedPreview.ts@@ -110,15 +110,21 @@ function mentionLabel(tag: string, attrs: Record<string, string>, body: string):return attrs.id ? unescapeXml(body).trim() || attrs.id : ''}-export function spaceFeedPreview(text: string | null | undefined): string {- if (!text) {- return ''- }+export function stripInjectedBlocks(text: string): string {return text.replace(COMPLETE_BLOCK, '').replace(CUSTOM_INSTRUCTIONS_BLOCK, (block: string, inner: string) =>inner.trimStart().startsWith(CUSTOM_INSTRUCTIONS_PREAMBLE) ? '' : block)+ .replace(/\n{3,}/g, '\n\n')+ .trim()+}++export function spaceFeedPreview(text: string | null | undefined): string {+ if (!text) {+ return ''+ }+ return stripInjectedBlocks(text).replace(TRUNCATED_BLOCK, '').replace(MENTION_TAG,