: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
feat(aeo): suggest prompts from the prompts tab (#130536)
Lucas Ricoy9388b115e172
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex f8309ed96716..22cf388f6b35 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -8303,9 +8303,9 @@ snapshots:products-web-analytics-content-autopilot-workspace--onboarding--light:hash: v1.k794b7964.43db8fae97d0c1d904bbc77258a41cbf9ce99992a214721db5869aca949828bd.2dCjwtWR3d2g3FGZx9hQhvz4NCBI2AmaSEAUEJHAj-Iproducts-web-analytics-content-autopilot-workspace--prompts-tab--dark:- hash: v1.k794b7964.909e5f4181aa2889553d2ea8cce00b04a8c17c49319d2e9efd9bc2351cdd0990.cw9K4gnJp37GgFGnYzcm3vqcVCjgm-i6s_gCG8_sbRQ+ hash: v1.k794b7964.e9f2aeae0fac687c709d73ca884854ef59beba38fd3c6a8f38df28b7651d435b._SGqY4TrfBGE2d1iELr9tFx_0UsyKGG7VmK_xa6zZaMproducts-web-analytics-content-autopilot-workspace--prompts-tab--light:- hash: v1.k794b7964.2452cd81a3dca6efde3a7b75a11bfe8ac27e8f082f477191d7286b7c79c2b6e9.5uu6tAr2c2x0mnee3lXuNF6k5EC08hoRcydQDPHZje8+ hash: v1.k794b7964.f0ce9a7f6541bfff8882d8ae8a6c71acb0d2359ae789951979584d5d6edf77c9.8xTwKDr--t7chmYN7ZVlqxn-5ToQE-vts6RkJP4hPJMproducts-web-analytics-content-autopilot-workspace--proposal-preview--dark:hash: v1.k794b7964.3c03dc669ecd9ed2784e3cfd07da89fe99930de0dbead14eb969c3c0bfd7f7e4.4Lo8dPZZhSvHN4znzANzryiq8f-jEw18Vs6hUFgzMakproducts-web-analytics-content-autopilot-workspace--proposal-preview--light:diff --git a/products/web_analytics/frontend/contentAutopilot/ContentAutopilot.tsx b/products/web_analytics/frontend/contentAutopilot/ContentAutopilot.tsxindex 68f07814b348..a9e802614580 100644--- a/products/web_analytics/frontend/contentAutopilot/ContentAutopilot.tsx+++ b/products/web_analytics/frontend/contentAutopilot/ContentAutopilot.tsx@@ -9,6 +9,7 @@ import { ContentAutopilotOpportunities } from './ContentAutopilotOpportunities'import { ContentAutopilotPromptAddModal } from './ContentAutopilotPromptAddModal'import { ContentAutopilotPrompts } from './ContentAutopilotPrompts'import { ContentAutopilotPromptsOverview } from './ContentAutopilotPromptsOverview'+import { ContentAutopilotPromptSuggestModal } from './ContentAutopilotPromptSuggestModal'import { ContentAutopilotProposalDetail } from './ContentAutopilotProposalDetail'import { ContentAutopilotSetup } from './ContentAutopilotSetup'import { ContentAutopilotSiteGate } from './ContentAutopilotSiteGate'@@ -184,6 +185,7 @@ export const ContentAutopilot = ({ bots }: { bots?: ContentAutopilotBotsTab }):)}<ContentAutopilotProposalDetail /><ContentAutopilotPromptAddModal />+ <ContentAutopilotPromptSuggestModal /></div>)}diff --git a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestButton.tsx b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestButton.tsxnew file mode 100644index 000000000000..a774daa4cd4b--- /dev/null+++ b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestButton.tsx@@ -0,0 +1,33 @@+import { useActions, useValues } from 'kea'++import { IconSparkles } from '@posthog/icons'+import { LemonButton, Spinner } from '@posthog/lemon-ui'++import { contentAutopilotPromptSuggestionsLogic } from './contentAutopilotPromptSuggestionsLogic'++export const ContentAutopilotPromptSuggestButton = ({+ type = 'secondary',+ size,+}: {+ type?: 'primary' | 'secondary'+ size?: 'small'+}): JSX.Element => {+ const { job } = useValues(contentAutopilotPromptSuggestionsLogic)+ const { openSuggest } = useActions(contentAutopilotPromptSuggestionsLogic)++ return (+ <LemonButton+ type={type}+ size={size}+ icon={job?.status === 'pending' ? <Spinner /> : <IconSparkles />}+ onClick={openSuggest}+ data-attr="content-autopilot-prompts-suggest"+ >+ {job?.status === 'pending'+ ? 'Suggesting prompts'+ : job?.status === 'completed' && job.suggestions.length > 0+ ? `Review suggestions (${job.suggestions.length})`+ : 'Suggest prompts'}+ </LemonButton>+ )+}diff --git a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestModal.stories.tsx b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestModal.stories.tsxnew file mode 100644index 000000000000..7345492ab854--- /dev/null+++ b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestModal.stories.tsx@@ -0,0 +1,110 @@+import type { Meta, StoryFn } from '@storybook/react'+import { useActions } from 'kea'+import { useEffect } from 'react'++import { FEATURE_FLAGS } from 'lib/constants'++import { mswDecorator } from '~/mocks/browser'++import type { AEOPromptSuggestionJobApi } from '../generated/api.schemas'+import { contentAutopilotPromptSuggestionsLogic } from './contentAutopilotPromptSuggestionsLogic'+import { ContentAutopilotPromptSuggestModal } from './ContentAutopilotPromptSuggestModal'++const JOB_ID = '00000000-0000-4000-8000-000000000901'++const PENDING_JOB: AEOPromptSuggestionJobApi = { id: JOB_ID, status: 'pending', suggestions: [], error: '' }++const COMPLETED_JOB: AEOPromptSuggestionJobApi = {+ id: JOB_ID,+ status: 'completed',+ error: '',+ suggestions: [+ {+ prompt: 'What is the best file sharing app for a small design agency?',+ topic: 'File sharing',+ circle: 'battleground',+ funnel_stage: 'bottom',+ branded: false,+ reason: 'Agencies compare tools before they buy seats for the team.',+ },+ {+ prompt: 'How can I send large video files to clients without them making an account?',+ topic: 'Client delivery',+ circle: 'differentiation',+ funnel_stage: 'middle',+ branded: false,+ reason: 'Guest links are a common reason to switch tools.',+ },+ {+ prompt: 'Which cloud storage tools have the best version history for documents?',+ topic: 'File sharing',+ circle: 'vulnerability',+ funnel_stage: 'middle',+ branded: false,+ reason: 'Buyers who need audit trails often pick a competitor here.',+ },+ {+ prompt: 'Is there a free alternative to paid cloud storage for students?',+ topic: 'Pricing',+ circle: 'table_stakes',+ funnel_stage: 'top',+ branded: false,+ reason: 'Price-sensitive buyers start with free plans.',+ },+ {+ prompt: 'What is Hedgebox and how does it compare to Dropbox?',+ topic: 'Brand',+ circle: 'branded',+ funnel_stage: 'middle',+ branded: true,+ reason: 'Checks how assistants describe the brand next to a competitor.',+ },+ ],+}++const meta: Meta<typeof ContentAutopilotPromptSuggestModal> = {+ title: 'Products/Web Analytics/Content autopilot/Suggest prompts',+ component: ContentAutopilotPromptSuggestModal,+ parameters: {+ layout: 'fullscreen',+ featureFlags: [FEATURE_FLAGS.WEB_ANALYTICS_CONTENT_AUTOPILOT],+ testOptions: { snapshotBrowsers: [] },+ },+ decorators: [+ mswDecorator({+ get: {+ '/api/projects/:team_id/web_analytics_content_autopilot_profiles/': () => [+ 200,+ { count: 0, next: null, previous: null, results: [] },+ ],+ '/api/projects/:team_id/aeo_prompts/': () => [+ 200,+ { count: 0, next: null, previous: null, results: [] },+ ],+ '/api/projects/:team_id/aeo_prompts/topics/': () => [200, []],+ '/api/projects/:team_id/aeo_prompts/suggest/:job_id/': () => [200, PENDING_JOB],+ },+ }),+ ],+}++export default meta++const SuggestStory = ({ job }: { job?: AEOPromptSuggestionJobApi }): JSX.Element => {+ const { openSuggest, setForm, setJob } = useActions(contentAutopilotPromptSuggestionsLogic)+ useEffect(() => {+ setForm({ domain: 'hedgebox.net', brand: 'Hedgebox', competitors: ['Dropbox', 'Box'] })+ openSuggest()+ if (job) {+ setJob(job)+ }+ }, [openSuggest, setForm, setJob, job])+ return <ContentAutopilotPromptSuggestModal />+}++export const Form: StoryFn = () => <SuggestStory />++export const Working: StoryFn = () => <SuggestStory job={PENDING_JOB} />+Working.parameters = { testOptions: { snapshotBrowsers: [], waitForLoadersToDisappear: false } }++export const Review: StoryFn = () => <SuggestStory job={COMPLETED_JOB} />diff --git a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestModal.tsx b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestModal.tsxnew file mode 100644index 000000000000..0dae2a62d7d3--- /dev/null+++ b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptSuggestModal.tsx@@ -0,0 +1,303 @@+import { useActions, useValues } from 'kea'++import {+ LemonBanner,+ LemonButton,+ LemonCheckbox,+ LemonInput,+ LemonInputSelect,+ LemonModal,+ LemonSelect,+ LemonTag,+ LemonTextArea,+ Spinner,+ Tooltip,+} from '@posthog/lemon-ui'++import { LemonField } from 'lib/lemon-ui/LemonField'+import { pluralize } from 'lib/utils/strings'++import type {+ AEOPromptSuggestionApi,+ FunnelStageEnumApi,+ PromptCircleEnumApi,+} from 'products/web_analytics/frontend/generated/api.schemas'+import {+ aeoPromptsSuggestBodyBrandMax,+ aeoPromptsSuggestBodyCompetitorsItemMax,+ aeoPromptsSuggestBodyCompetitorsMax,+ aeoPromptsSuggestBodyDescriptionMax,+ aeoPromptsSuggestBodyDomainMax,+} from 'products/web_analytics/frontend/generated/api.zod'++import {+ SUGGESTION_COUNTS,+ contentAutopilotPromptSuggestionsLogic,+ siteDomain,+} from './contentAutopilotPromptSuggestionsLogic'++const CIRCLES: Record<PromptCircleEnumApi, { label: string; description: string }> = {+ differentiation: {+ label: 'Differentiation',+ description: 'A need your buyers have that you meet and competitors do not.',+ },+ battleground: { label: 'Battleground', description: 'A need you and your competitors all meet.' },+ vulnerability: { label: 'Vulnerability', description: 'A need competitors meet better than you do.' },+ table_stakes: { label: 'Table stakes', description: 'Something every tool in the category does.' },+ branded: { label: 'Branded', description: 'Names your brand. Checks how assistants describe you.' },+}++const STAGES: Record<FunnelStageEnumApi, string> = {+ top: 'Learning',+ middle: 'Comparing',+ bottom: 'Choosing',+}++const SuggestionRow = ({ suggestion }: { suggestion: AEOPromptSuggestionApi }): JSX.Element => {+ const { selectedPrompts } = useValues(contentAutopilotPromptSuggestionsLogic)+ const { toggleSuggestion } = useActions(contentAutopilotPromptSuggestionsLogic)+ const circle = CIRCLES[suggestion.circle]++ return (+ <div className="flex gap-2 py-2 border-b last:border-b-0">+ <LemonCheckbox+ checked={selectedPrompts.includes(suggestion.prompt)}+ onChange={() => toggleSuggestion(suggestion.prompt)}+ aria-label={`Select ${suggestion.prompt}`}+ />+ <div className="flex flex-col gap-1 min-w-0">+ <span className="font-semibold break-words">{suggestion.prompt}</span>+ <div className="flex flex-wrap items-center gap-1">+ {suggestion.topic ? <LemonTag>{suggestion.topic}</LemonTag> : null}+ <Tooltip title={circle.description}>+ <LemonTag type={suggestion.branded ? 'muted' : 'highlight'}>{circle.label}</LemonTag>+ </Tooltip>+ <LemonTag type="muted">{STAGES[suggestion.funnel_stage]}</LemonTag>+ </div>+ {suggestion.reason ? <span className="text-xs text-muted">{suggestion.reason}</span> : null}+ </div>+ </div>+ )+}++// The API takes at most 20 entries of 200 characters each.+const cappedList = (values: string[]): string[] =>+ values+ .slice(0, aeoPromptsSuggestBodyCompetitorsMax)+ .map((value) => value.slice(0, aeoPromptsSuggestBodyCompetitorsItemMax))++const SuggestForm = (): JSX.Element => {+ const { form, startError } = useValues(contentAutopilotPromptSuggestionsLogic)+ const { setForm } = useActions(contentAutopilotPromptSuggestionsLogic)++ return (+ <div className="flex flex-col gap-4">+ {startError ? <LemonBanner type="error">{startError}</LemonBanner> : null}+ <LemonField.Pure label="Site" help="Claude reads this site to understand what you do and who it is for.">+ <LemonInput+ value={form.domain}+ onChange={(domain) => setForm({ domain })}+ maxLength={aeoPromptsSuggestBodyDomainMax}+ placeholder="example.com"+ autoFocus+ data-attr="content-autopilot-suggest-domain"+ />+ </LemonField.Pure>+ <div className="grid grid-cols-1 @md/suggest:grid-cols-2 gap-4">+ <LemonField.Pure label="Brand" help="Optional. Read from the site when empty.">+ <LemonInput+ value={form.brand}+ onChange={(brand) => setForm({ brand })}+ maxLength={aeoPromptsSuggestBodyBrandMax}+ />+ </LemonField.Pure>+ <LemonField.Pure label="Number of prompts">+ <LemonSelect+ value={form.count}+ onChange={(count) => setForm({ count })}+ options={SUGGESTION_COUNTS.map((count) => ({ value: count, label: String(count) }))}+ />+ </LemonField.Pure>+ </div>+ <LemonField.Pure label="What you do and who it is for" help="Optional. Read from the site when empty.">+ <LemonTextArea+ value={form.description}+ onChange={(description) => setForm({ description })}+ maxLength={aeoPromptsSuggestBodyDescriptionMax}+ minRows={2}+ placeholder="Open source product analytics for engineers at startups"+ />+ </LemonField.Pure>+ <LemonField.Pure label="Competitors" help="Optional. Claude picks the main ones when empty.">+ <LemonInputSelect+ mode="multiple"+ allowCustomValues+ value={form.competitors}+ onChange={(competitors) => setForm({ competitors: cappedList(competitors) })}+ placeholder="Add a name or domain"+ />+ </LemonField.Pure>+ <LemonField.Pure label="Topics" help="Optional. Claude picks 3 to 6 when empty.">+ <LemonInputSelect+ mode="multiple"+ allowCustomValues+ value={form.topics}+ onChange={(topics) => setForm({ topics: cappedList(topics) })}+ placeholder="Add a topic"+ />+ </LemonField.Pure>+ </div>+ )+}++const SuggestReview = (): JSX.Element => {+ const { job, selectedSuggestions } = useValues(contentAutopilotPromptSuggestionsLogic)+ const { setSelectedPrompts } = useActions(contentAutopilotPromptSuggestionsLogic)+ const suggestions = job?.suggestions ?? []+ const allSelected = selectedSuggestions.length === suggestions.length++ if (suggestions.length === 0) {+ return (+ <p className="m-0 text-muted">+ No new prompts came back. Claude's ideas may already be tracked, or its answer had none to keep. Change+ the topics or competitors and try again.+ </p>+ )+ }++ return (+ <div className="flex flex-col gap-2">+ <div className="flex items-center justify-between gap-2">+ <span className="text-muted">+ {/* translate="no" keeps the count updating when the browser translates the page. */}+ <span translate="no">{selectedSuggestions.length}</span> of{' '}+ {pluralize(suggestions.length, 'suggestion')} selected. Nothing is tracked until you add them.+ </span>+ <LemonButton+ size="small"+ type="tertiary"+ onClick={() => setSelectedPrompts(allSelected ? [] : suggestions.map(({ prompt }) => prompt))}+ >+ {allSelected ? 'Select none' : 'Select all'}+ </LemonButton>+ </div>+ <div className="max-h-[50vh] overflow-y-auto">+ {suggestions.map((suggestion) => (+ <SuggestionRow key={suggestion.prompt} suggestion={suggestion} />+ ))}+ </div>+ </div>+ )+}++export const ContentAutopilotPromptSuggestModal = (): JSX.Element => {+ const { suggestOpen, form, job, jobRef, starting, adding, selectedSuggestions, editingInputs } = useValues(+ contentAutopilotPromptSuggestionsLogic+ )+ const { closeSuggest, startSuggestions, editRequest, backToSuggestions, addSelected } = useActions(+ contentAutopilotPromptSuggestionsLogic+ )+ const view = editingInputs || !job ? 'form' : job.status+ const domain = jobRef?.domain || siteDomain(form.domain) || 'your site'+ const startButton = (+ <LemonButton+ type="primary"+ onClick={startSuggestions}+ loading={starting}+ disabledReason={!siteDomain(form.domain) ? 'Enter your site' : undefined}+ data-attr="content-autopilot-suggest-start"+ >+ Suggest prompts+ </LemonButton>+ )++ const footer =+ view === 'completed' ? (+ <>+ <LemonButton+ type="secondary"+ onClick={editRequest}+ disabledReason={adding ? 'Wait for the prompts to save' : undefined}+ >+ Change inputs+ </LemonButton>+ <LemonButton+ type="primary"+ onClick={addSelected}+ loading={adding}+ disabledReason={selectedSuggestions.length === 0 ? 'Select at least one prompt' : undefined}+ data-attr="content-autopilot-suggest-add"+ >+ {selectedSuggestions.length > 0+ ? `Add ${pluralize(selectedSuggestions.length, 'prompt')}`+ : 'Add prompts'}+ </LemonButton>+ </>+ ) : view === 'pending' ? (+ <LemonButton type="secondary" onClick={closeSuggest}>+ Close and keep working+ </LemonButton>+ ) : view === 'failed' ? (+ <>+ <LemonButton type="secondary" onClick={editRequest}>+ Change inputs+ </LemonButton>+ {siteDomain(form.domain) ? (+ <LemonButton type="primary" onClick={startSuggestions} loading={starting}>+ Try again+ </LemonButton>+ ) : null}+ </>+ ) : (+ <>+ {job && job.status !== 'failed' ? (+ <LemonButton type="secondary" onClick={backToSuggestions}>+ {job.status === 'pending' ? 'Back to progress' : 'Back to suggestions'}+ </LemonButton>+ ) : (+ <LemonButton type="secondary" onClick={closeSuggest}>+ Cancel+ </LemonButton>+ )}+ {startButton}+ </>+ )++ return (+ <LemonModal+ isOpen={suggestOpen}+ onClose={adding ? undefined : closeSuggest}+ title="Suggest prompts"+ description="Claude reads your site and writes questions your buyers would ask an AI assistant. You pick which ones to track."+ width={720}+ footer={footer}+ >+ <div className="@container/suggest">+ {view === 'pending' ? (+ <div className="flex flex-col items-center gap-3 py-10 text-center">+ <Spinner className="text-2xl" />+ <span className="font-semibold">Reading {domain} and writing prompts</span>+ <span className="text-muted max-w-md">+ This usually takes a minute or two. You can close this and come back. The suggestions keep+ loading in the background.+ </span>+ </div>+ ) : view === 'failed' ? (+ <LemonBanner type="error">{job?.error || "Couldn't suggest prompts. Try again."}</LemonBanner>+ ) : view === 'completed' ? (+ <SuggestReview />+ ) : (+ <div className="flex flex-col gap-3">+ {editingInputs && job?.status === 'completed' ? (+ <LemonBanner type="info">+ Suggesting again replaces the {pluralize(job.suggestions.length, 'suggestion')} you have+ now.+ </LemonBanner>+ ) : null}+ <SuggestForm />+ </div>+ )}+ </div>+ </LemonModal>+ )+}diff --git a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPrompts.tsx b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPrompts.tsxindex a46a91c81707..6692e78cc404 100644--- a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPrompts.tsx+++ b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPrompts.tsx@@ -26,6 +26,7 @@ import { ContentAutopilotPromptDetail } from './ContentAutopilotPromptDetail'import { contentAutopilotPromptDetailLogic } from './contentAutopilotPromptDetailLogic'import { ContentAutopilotPromptsDisabled } from './ContentAutopilotPromptsDisabled'import { PROMPTS_PAGE_SIZE, contentAutopilotPromptsLogic } from './contentAutopilotPromptsLogic'+import { ContentAutopilotPromptSuggestButton } from './ContentAutopilotPromptSuggestButton'import { draftableOpportunity, engineLabel } from './utils'const checkTag = (prompt: AEOPromptApi, engine: string): JSX.Element => {@@ -125,16 +126,18 @@ export const ContentAutopilotPrompts = (): JSX.Element => {size="small"data-attr="content-autopilot-prompts-show-archived"/>- <LemonButton- type="primary"- size="small"- icon={<IconPlusSmall />}- className="ml-auto"- onClick={openAddPrompts}- data-attr="content-autopilot-prompts-add"- >- Add prompts- </LemonButton>+ <div className="ml-auto flex gap-2">+ <ContentAutopilotPromptSuggestButton size="small" />+ <LemonButton+ type="primary"+ size="small"+ icon={<IconPlusSmall />}+ onClick={openAddPrompts}+ data-attr="content-autopilot-prompts-add"+ >+ Add prompts+ </LemonButton>+ </div></div>{promptsError === 'failed' ? (@@ -154,9 +157,13 @@ export const ContentAutopilotPrompts = (): JSX.Element => {<LemonCard hoverEffect={false} className="p-6 text-center"><h3 className="m-0">No prompts yet</h3><p className="m-0 mt-2 text-muted max-w-xl mx-auto">- Add the questions your customers ask AI assistants. You can also import prompts with the API (+ Add the questions your buyers ask AI assistants. Claude can suggest them from your site, or you+ can write your own. You can also import prompts with the API (<code>POST /api/projects/:project_id/aeo_prompts/bulk_import/</code>) or the PostHog MCP tools.</p>+ <div className="mt-4 flex justify-center">+ <ContentAutopilotPromptSuggestButton type="primary" />+ </div></LemonCard>) : (<LemonTablediff --git a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptsOverview.tsx b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptsOverview.tsxindex 269638a80de7..61818713d18b 100644--- a/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptsOverview.tsx+++ b/products/web_analytics/frontend/contentAutopilot/ContentAutopilotPromptsOverview.tsx@@ -14,6 +14,7 @@ import { ContentAutopilotPromptsDisabled } from './ContentAutopilotPromptsDisablimport { contentAutopilotPromptsLogic } from './contentAutopilotPromptsLogic'import { OVERVIEW_DAYS, contentAutopilotPromptsOverviewLogic } from './contentAutopilotPromptsOverviewLogic'import { ContentAutopilotPromptsTopicTable } from './ContentAutopilotPromptsTopicTable'+import { ContentAutopilotPromptSuggestButton } from './ContentAutopilotPromptSuggestButton'import { promptSourcesContext } from './promptReportColumns'import { engineLabel } from './utils'@@ -49,18 +50,20 @@ export const ContentAutopilotPromptsOverview = (): JSX.Element => {><h3 className="m-0">No prompts yet</h3><p className="m-0 mt-2 text-muted max-w-xl mx-auto">- Add the questions your customers ask AI assistants. PostHog checks them once a day and shows the- results here.+ Add the questions your buyers ask AI assistants. Claude can suggest them from your site, or you can+ write your own. PostHog checks them once a day and shows the results here.</p>- <LemonButton- type="primary"- icon={<IconPlusSmall />}- className="mt-4 mx-auto"- onClick={openAddPrompts}- data-attr="content-autopilot-prompts-overview-add"- >- Add prompts- </LemonButton>+ <div className="mt-4 flex justify-center gap-2">+ <ContentAutopilotPromptSuggestButton type="primary" />+ <LemonButton+ type="secondary"+ icon={<IconPlusSmall />}+ onClick={openAddPrompts}+ data-attr="content-autopilot-prompts-overview-add"+ >+ Add my own+ </LemonButton>+ </div></LemonCard>) : (<LemonCard hoverEffect={false} className="p-4" data-attr="content-autopilot-prompts-overview-empty">diff --git a/products/web_analytics/frontend/contentAutopilot/contentAutopilotPromptSuggestionsLogic.test.ts b/products/web_analytics/frontend/contentAutopilot/contentAutopilotPromptSuggestionsLogic.test.tsnew file mode 100644index 000000000000..43756c408a85--- /dev/null+++ b/products/web_analytics/frontend/contentAutopilot/contentAutopilotPromptSuggestionsLogic.test.ts@@ -0,0 +1,138 @@+import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'++import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'++import { aeoPromptsBulkImport, aeoPromptsSuggest, aeoPromptsSuggestionRetrieve } from '../generated/api'+import type { AEOPromptSuggestionApi, AEOPromptSuggestionJobApi } from '../generated/api.schemas'+import { contentAutopilotPromptSuggestionsLogic, siteDomain } from './contentAutopilotPromptSuggestionsLogic'++jest.mock('../generated/api', () => ({+ ...jest.requireActual('../generated/api'),+ aeoPromptsSuggest: jest.fn(),+ aeoPromptsSuggestionRetrieve: jest.fn(),+ aeoPromptsBulkImport: jest.fn(),+}))++const suggestion = (prompt: string, topic: string): AEOPromptSuggestionApi => ({+ prompt,+ topic,+ circle: 'battleground',+ funnel_stage: 'bottom',+ branded: false,+ reason: 'Buyers compare tools.',+})++const JOB_ID = '00000000-0000-4000-8000-000000000901'+const pending: AEOPromptSuggestionJobApi = { id: JOB_ID, status: 'pending', suggestions: [], error: '' }+const completed: AEOPromptSuggestionJobApi = {+ ...pending,+ status: 'completed',+ suggestions: [+ suggestion('Best session replay tool?', 'Replay'),+ suggestion('Cheapest analytics tool?', 'Analytics'),+ ],+}++describe('contentAutopilotPromptSuggestionsLogic', () => {+ let logic: ReturnType<typeof contentAutopilotPromptSuggestionsLogic.build>++ beforeEach(() => {+ jest.clearAllMocks()+ // The job reference persists, so one test's job must not resume in the next.+ localStorage.clear()+ initKeaTests()+ jest.mocked(aeoPromptsSuggest).mockResolvedValue(pending)+ jest.mocked(aeoPromptsBulkImport).mockResolvedValue({ created: 1, updated: 0, skipped: 0 })+ logic = contentAutopilotPromptSuggestionsLogic()+ logic.mount()+ })++ afterEach(() => {+ logic.unmount()+ })++ it('polls a job until it completes, then imports only the selected suggestions', async () => {+ logic.actions.openSuggest()+ logic.actions.setForm({ domain: 'https://example.com/pricing' })+ await expectLogic(logic, () => logic.actions.startSuggestions()).toFinishAllListeners()++ expect(aeoPromptsSuggest).toHaveBeenCalledWith(+ String(MOCK_DEFAULT_TEAM.id),+ expect.objectContaining({ domain: 'example.com', count: 20 })+ )+ expect(logic.values.job?.status).toBe('pending')++ // A poll that still finds the job running leaves it pending.+ jest.mocked(aeoPromptsSuggestionRetrieve).mockResolvedValueOnce(pending).mockResolvedValueOnce(completed)+ await expectLogic(logic, () => logic.actions.loadJob()).toFinishAllListeners()+ expect(logic.values.job?.status).toBe('pending')+ await expectLogic(logic, () => logic.actions.loadJob()).toFinishAllListeners()++ expect(logic.values.selectedPrompts).toEqual(['Best session replay tool?', 'Cheapest analytics tool?'])+ logic.actions.toggleSuggestion('Cheapest analytics tool?')+ await expectLogic(logic, () => logic.actions.addSelected()).toFinishAllListeners()++ expect(aeoPromptsBulkImport).toHaveBeenCalledWith(String(MOCK_DEFAULT_TEAM.id), {+ prompts: [{ prompt: 'Best session replay tool?', topic: 'Replay', origin: 'suggestion' }],+ })+ expect(logic.values).toMatchObject({ suggestOpen: false, job: null, adding: false, selectedPrompts: [] })++ // A second run starts with all of its suggestions selected, not the previous run's choices.+ jest.mocked(aeoPromptsSuggestionRetrieve).mockResolvedValueOnce(completed)+ await expectLogic(logic, () => logic.actions.startSuggestions()).toFinishAllListeners()+ await expectLogic(logic, () => logic.actions.loadJob()).toFinishAllListeners()+ expect(logic.values.selectedSuggestions).toHaveLength(2)+ })++ it('resumes a job after the page unmounts and ignores a poll that would overlap another', async () => {+ logic.actions.setForm({ domain: 'example.com' })+ await expectLogic(logic, () => logic.actions.startSuggestions()).toFinishAllListeners()+ logic.unmount()++ let finishPoll: (job: AEOPromptSuggestionJobApi) => void = () => {}+ jest.mocked(aeoPromptsSuggestionRetrieve).mockReturnValueOnce(+ new Promise((resolve) => {+ finishPoll = resolve+ })+ )+ logic = contentAutopilotPromptSuggestionsLogic()+ logic.mount()+ // The remount polls the stored job; a second poll while it runs sends no request.+ logic.actions.loadJob()+ expect(aeoPromptsSuggestionRetrieve).toHaveBeenCalledTimes(1)++ finishPoll(completed)+ await expectLogic(logic).toFinishAllListeners()+ expect(logic.values.job?.status).toBe('completed')+ expect(logic.values.selectedPrompts).toHaveLength(2)+ })++ it('shows the API reason when a job cannot start', async () => {+ const { ApiError } = jest.requireActual('lib/api-error')+ jest.mocked(aeoPromptsSuggest).mockRejectedValue(+ new ApiError('Forbidden', 403, undefined, {+ detail: 'Prompt suggestions need AI data processing approved.',+ })+ )+ logic.actions.setForm({ domain: 'example.com' })+ await expectLogic(logic, () => logic.actions.startSuggestions()).toFinishAllListeners()++ expect(logic.values).toMatchObject({+ job: null,+ starting: false,+ startError: 'Prompt suggestions need AI data processing approved.',+ })+ })++ it.each([+ ['example.com', 'example.com'],+ ['https://example.com/pricing?utm=x#plans', 'example.com'],+ ['Example.com:8080', 'example.com'],+ ['example.com.', 'example.com'],+ [' ', ''],+ ])('reads the site from %p', (input, expected) => {+ expect(siteDomain(input)).toBe(expected)+ })+})diff --git a/products/web_analytics/frontend/contentAutopilot/contentAutopilotPromptSuggestionsLogic.ts b/products/web_analytics/frontend/contentAutopilot/contentAutopilotPromptSuggestionsLogic.tsnew file mode 100644index 000000000000..acccdcc5ea67--- /dev/null+++ b/products/web_analytics/frontend/contentAutopilot/contentAutopilotPromptSuggestionsLogic.ts@@ -0,0 +1,431 @@+import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'+import posthog from 'posthog-js'++import { ApiError } from 'lib/api-error'+import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'+import { teamLogic } from 'scenes/teamLogic'++import {+ aeoPromptsBulkImport,+ aeoPromptsSuggest,+ aeoPromptsSuggestionRetrieve,+} from 'products/web_analytics/frontend/generated/api'+import type {+ AEOPromptImportResultApi,+ AEOPromptSuggestionApi,+ AEOPromptSuggestionJobApi,+ ContentAutopilotSiteProfileApi,+} from 'products/web_analytics/frontend/generated/api.schemas'++import { contentAutopilotLogic } from './contentAutopilotLogic'+import { contentAutopilotPromptsLogic } from './contentAutopilotPromptsLogic'++export const SUGGESTION_POLL_INTERVAL_MS = 3000+export const SUGGESTION_COUNTS = [10, 20, 30, 50]+const MAX_POLL_FAILURES = 5++export interface PromptSuggestionJobRef {+ id: string+ teamId: number | string+ // Optional because a reference stored before these fields existed lacks them.+ domain?: string+ startedAt?: number+}++// The server marks a lost job failed after 8 minutes; this is the fallback if that answer never arrives.+const CLIENT_JOB_DEADLINE_MS = 10 * 60 * 1000++export interface PromptSuggestionForm {+ domain: string+ brand: string+ description: string+ competitors: string[]+ topics: string[]+ count: number+}++const EMPTY_FORM: PromptSuggestionForm = {+ domain: '',+ brand: '',+ description: '',+ competitors: [],+ topics: [],+ count: 20,+}++export const siteDomain = (input: string): string => {+ const value = input.trim()+ if (!value) {+ return ''+ }+ try {+ return new URL(/^[a-z][a-z\d+.-]*:\/\//i.test(value) ? value : `https://${value}`).hostname.replace(/\.$/, '')+ } catch {+ return value+ }+}++const startErrorMessage = (error: unknown): string =>+ error instanceof ApiError && error.status && error.status < 500 && error.detail+ ? error.detail+ : "Couldn't start the suggestions. Try again."++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface contentAutopilotPromptSuggestionsLogicValues {+ profile: ContentAutopilotSiteProfileApi | null // contentAutopilotLogic+ currentTeamIdStrict: number | string // teamLogic+ adding: boolean+ editingInputs: boolean+ form: PromptSuggestionForm+ job: AEOPromptSuggestionJobApi | null+ jobRef: PromptSuggestionJobRef | null+ selectedPrompts: string[]+ selectedSuggestions: AEOPromptSuggestionApi[]+ startError: string | null+ starting: boolean+ suggestOpen: boolean+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface contentAutopilotPromptSuggestionsLogicActions {+ addPromptsSuccess: (result: AEOPromptImportResultApi) => {+ result: AEOPromptImportResultApi+ } // contentAutopilotPromptsLogic+ addSelected: () => {+ value: true+ }+ addSelectedFailure: () => {+ value: true+ }+ addSelectedSuccess: () => {+ value: true+ }+ backToSuggestions: () => {+ value: true+ }+ closeSuggest: () => {+ value: true+ }+ editRequest: () => {+ value: true+ }+ loadJob: () => {+ value: true+ }+ openSuggest: () => {+ value: true+ }+ resumeJob: () => {+ value: true+ }+ setForm: (form: Partial<PromptSuggestionForm>) => {+ form: Partial<PromptSuggestionForm>+ }+ setJob: (job: AEOPromptSuggestionJobApi) => {+ job: AEOPromptSuggestionJobApi+ }+ setJobRef: (jobRef: PromptSuggestionJobRef | null) => {+ jobRef: PromptSuggestionJobRef | null+ }+ setSelectedPrompts: (prompts: string[]) => {+ prompts: string[]+ }+ startSuggestions: () => {+ value: true+ }+ startSuggestionsFailure: (error: string) => {+ error: string+ }+ toggleSuggestion: (prompt: string) => {+ prompt: string+ }+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface contentAutopilotPromptSuggestionsLogicMeta {+ __keaTypeGenInternalSelectorTypes: {+ selectedSuggestions: (+ job: AEOPromptSuggestionJobApi | null,+ selectedPrompts: string[]+ ) => AEOPromptSuggestionApi[]+ }+}++export type contentAutopilotPromptSuggestionsLogicType = MakeLogicType<+ contentAutopilotPromptSuggestionsLogicValues,+ contentAutopilotPromptSuggestionsLogicActions,+ Record<string, any>,+ contentAutopilotPromptSuggestionsLogicMeta+>++export const contentAutopilotPromptSuggestionsLogic = kea<contentAutopilotPromptSuggestionsLogicType>([+ path(['products', 'webAnalytics', 'contentAutopilot', 'contentAutopilotPromptSuggestionsLogic']),+ connect(() => ({+ values: [teamLogic, ['currentTeamIdStrict'], contentAutopilotLogic, ['profile']],+ actions: [contentAutopilotPromptsLogic, ['addPromptsSuccess']],+ })),+ actions({+ openSuggest: true,+ closeSuggest: true,+ setForm: (form: Partial<PromptSuggestionForm>) => ({ form }),+ startSuggestions: true,+ startSuggestionsFailure: (error: string) => ({ error }),+ setJobRef: (jobRef: PromptSuggestionJobRef | null) => ({ jobRef }),+ setJob: (job: AEOPromptSuggestionJobApi) => ({ job }),+ loadJob: true,+ editRequest: true,+ backToSuggestions: true,+ resumeJob: true,+ toggleSuggestion: (prompt: string) => ({ prompt }),+ setSelectedPrompts: (prompts: string[]) => ({ prompts }),+ addSelected: true,+ addSelectedSuccess: true,+ addSelectedFailure: true,+ }),+ reducers({+ suggestOpen: [+ false,+ {+ openSuggest: () => true,+ closeSuggest: () => false,+ addSelectedSuccess: () => false,+ },+ ],+ form: [+ EMPTY_FORM,+ {+ setForm: (state, { form }) => ({ ...state, ...form }),+ },+ ],+ // Only a reference to the job is kept, so a job started before leaving the page can be read again.+ jobRef: [+ null as PromptSuggestionJobRef | null,+ { persist: true },+ {+ setJobRef: (_, { jobRef }) => jobRef,+ addSelectedSuccess: () => null,+ },+ ],+ job: [+ null as AEOPromptSuggestionJobApi | null,+ {+ setJob: (_, { job }) => job,+ addSelectedSuccess: () => null,+ },+ ],+ // Editing the inputs keeps the current suggestions until a new request replaces them.+ editingInputs: [+ false,+ {+ editRequest: () => true,+ backToSuggestions: () => false,+ setJobRef: () => false,+ addSelectedSuccess: () => false,+ },+ ],+ starting: [+ false,+ {+ startSuggestions: () => true,+ setJob: () => false,+ startSuggestionsFailure: () => false,+ },+ ],+ startError: [+ null as string | null,+ {+ startSuggestions: () => null,+ startSuggestionsFailure: (_, { error }) => error,+ backToSuggestions: () => null,+ },+ ],+ selectedPrompts: [+ [] as string[],+ {+ setSelectedPrompts: (_, { prompts }) => prompts,+ toggleSuggestion: (state, { prompt }) =>+ state.includes(prompt) ? state.filter((p) => p !== prompt) : [...state, prompt],+ startSuggestions: () => [],+ addSelectedSuccess: () => [],+ },+ ],+ adding: [+ false,+ {+ addSelected: () => true,+ addSelectedSuccess: () => false,+ addSelectedFailure: () => false,+ },+ ],+ }),+ selectors({+ selectedSuggestions: [+ (s) => [s.job, s.selectedPrompts],+ (job: AEOPromptSuggestionJobApi | null, selectedPrompts: string[]): AEOPromptSuggestionApi[] =>+ (job?.suggestions ?? []).filter(({ prompt }) => selectedPrompts.includes(prompt)),+ ],+ }),+ listeners(({ actions, values, cache }) => ({+ openSuggest: () => {+ // pinned: analytics event name, renaming it breaks dashboards+ posthog.capture('web analytics content autopilot prompt suggestions opened')+ if (!values.form.domain && values.profile) {+ actions.setForm({ domain: siteDomain(values.profile.domain), brand: values.profile.name ?? '' })+ }+ },+ startSuggestions: async () => {+ const { form } = values+ const domain = siteDomain(form.domain)+ try {+ const job = await aeoPromptsSuggest(String(values.currentTeamIdStrict), {+ domain,+ brand: form.brand.trim(),+ description: form.description.trim(),+ competitors: form.competitors,+ topics: form.topics,+ count: form.count,+ })+ // pinned: analytics event name, renaming it breaks dashboards+ posthog.capture('web analytics content autopilot prompt suggestions started', { count: form.count })+ cache.pollFailures = 0+ actions.setJobRef({ id: job.id, teamId: values.currentTeamIdStrict, domain, startedAt: Date.now() })+ actions.setJob(job)+ } catch (error) {+ actions.startSuggestionsFailure(startErrorMessage(error))+ }+ },+ setJob: ({ job }) => {+ if (job.status === 'pending') {+ cache.pollingJobId = job.id+ actions.resumeJob()+ return+ }+ cache.disposables.dispose('suggestionPoll')+ if (job.status === 'completed' && values.selectedPrompts.length === 0) {+ actions.setSelectedPrompts(job.suggestions.map(({ prompt }) => prompt))+ }+ // Only a job this page watched finish counts, so restoring a finished job reports nothing.+ if (cache.pollingJobId !== job.id) {+ return+ }+ cache.pollingJobId = null+ if (job.status === 'completed') {+ // pinned: analytics event name, renaming it breaks dashboards+ posthog.capture('web analytics content autopilot prompt suggestions completed', {+ suggestions: job.suggestions.length,+ })+ }+ // With the dialog closed, the result would otherwise go unnoticed.+ if (!values.suggestOpen) {+ if (job.status === 'completed') {+ lemonToast.success(+ job.suggestions.length+ ? `${job.suggestions.length} prompt suggestions are ready.`+ : 'Prompt suggestions finished with no new prompts.',+ { button: { label: 'Review', action: () => actions.openSuggest() } }+ )+ } else {+ lemonToast.error(job.error || "Couldn't suggest prompts. Try again.")+ }+ }+ },+ resumeJob: () => {+ cache.disposables.add(() => {+ const id = setInterval(() => actions.loadJob(), SUGGESTION_POLL_INTERVAL_MS)+ return () => clearInterval(id)+ }, 'suggestionPoll')+ },+ loadJob: async () => {+ const jobRef = values.jobRef+ const jobId = jobRef?.id+ // Polls never overlap, so a slow response cannot be overtaken and miscounted as a failure.+ if (!jobId || cache.pollInFlight) {+ return+ }+ if (jobRef?.startedAt && Date.now() - jobRef.startedAt > CLIENT_JOB_DEADLINE_MS) {+ actions.setJob({+ id: jobId,+ status: 'failed',+ suggestions: [],+ error: 'Prompt suggestions stopped before they finished. Try again.',+ })+ return+ }+ cache.pollInFlight = true+ try {+ const job = await aeoPromptsSuggestionRetrieve(String(values.currentTeamIdStrict), jobId)+ cache.pollFailures = 0+ // A job the user replaced with a new request must not overwrite it.+ if (+ !cache.disposables.isDisposed &&+ job.id === values.jobRef?.id &&+ job.status !== values.job?.status+ ) {+ actions.setJob(job)+ }+ } catch (error) {+ if (cache.disposables.isDisposed || values.jobRef?.id !== jobId) {+ return+ }+ const expired = error instanceof ApiError && error.status === 404+ cache.pollFailures = (cache.pollFailures ?? 0) + 1+ if (expired || cache.pollFailures >= MAX_POLL_FAILURES) {+ cache.pollFailures = 0+ actions.setJob({+ id: jobId,+ status: 'failed',+ suggestions: [],+ error: expired+ ? 'These suggestions expired. Start again to get new ones.'+ : "Couldn't load the suggestions.",+ })+ }+ } finally {+ cache.pollInFlight = false+ }+ },+ addSelected: async () => {+ const selected = values.selectedSuggestions+ const suggested = values.job?.suggestions.length ?? 0+ const teamId = String(values.currentTeamIdStrict)+ let result: AEOPromptImportResultApi+ try {+ result = await aeoPromptsBulkImport(teamId, {+ prompts: selected.map(({ prompt, topic }) => ({ prompt, topic, origin: 'suggestion' })),+ })+ } catch (error) {+ if (!cache.disposables.isDisposed) {+ actions.addSelectedFailure()+ // A 400 explains why the import was refused, so show it rather than asking for a retry.+ const detail = error instanceof ApiError && error.status === 400 ? error.detail : null+ lemonToast.error(detail || "Couldn't add the prompts. Try again.")+ }+ return+ }+ // pinned: analytics event name, renaming it breaks dashboards+ posthog.capture('web analytics content autopilot prompt suggestions added', {+ added: selected.length,+ suggested,+ })+ if (!cache.disposables.isDisposed) {+ actions.addSelectedSuccess()+ // Reuses the import toast, and reloads the list, topics and overview.+ actions.addPromptsSuccess(result)+ }+ },+ })),+ afterMount(({ actions, values }) => {+ const { jobRef } = values+ if (!jobRef) {+ return+ }+ // A stored job from another project cannot be read here.+ if (String(jobRef.teamId) !== String(values.currentTeamIdStrict)) {+ actions.setJobRef(null)+ return+ }+ // Polling, not a single read, so a failed first read is retried like any other poll.+ actions.loadJob()+ actions.resumeJob()+ }),+])