Skip to content

fix(prompts): ask for a topic before adding prompts or generating suggestions - #1808

Merged
gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:fix/topic-first
Oct 30, 2026
Merged

gkhngyk merged 6 commits into
ansvisor:mainfrom
cihan-gyk:fix/topic-first

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Prompts are grouped under topics, but a brand without any topic could still get prompts, with none of them under a topic:

  • Add prompt said "Add topics in brand settings" and saved the prompt topicless anyway.
  • Fan-out Track added prompts without a topic.
  • Generating suggestions created topics from the AI's own labels.

Now a brand needs a topic first, everywhere a prompt can be added or suggested. The product owner's request: "Topic eklemek olmazsa olmaz."

Where it applies. Each place shows the same Add a topic first notice or dialog instead of going ahead (en.json topicFirst):

  • Prompts › Add prompt, Single and Bulk import. Topics are read again each time it opens, so coming back after adding one works.
  • Prompt Suggestions: Generate, New, + and Add All.
  • Track buttons: Insights opportunities, Query Fan-out, and the prompt detail's + Track.
  • AI Traffic: the Add prompts dialog and Track on Google queries.
  • Brand › Manage prompts: this replaces the old "brand settings" text.

Add a topic

  • Admins and managers:
    • On the Prompts page, a button closes the dialog, switches to Topics and opens Add Topic there.
    • Elsewhere, a link goes to /dashboard/prompts/topics?new=1, which opens Add Topic once.
    • Manage prompts links to that brand's own topics (/dashboard/brands/{id}/topics#add-topic).
  • Other roles: topic writes are admins' and managers' since fix(db): keep topic changes to admins and managers #1790. They see "Ask an admin or a manager to add a topic first." and no link.

Server (the client isn't trusted)

  • Server actions: addPromptToSet and importPrompts return { error, code: 'no_topics' } and write nothing. Every single-prompt path goes through addPromptToSet. trackPagePrompts checks before creating anything.
  • API: the suggestion refresh and AI Traffic's page suggestions return 409 { code: 'no_topics' } before any AI call or reservation.
  • MCP: accept_prompt_suggestion says to accept a topic suggestion first.
  • refreshPromptSuggestions no longer throws (fix(prompts): show suggestions' load failures plainly and retry once, instead of React #441 #1777 pattern). Budget (429), read-only/demo (402), role (403) and 5xx come back as typed failures. The card shows them with useFailureToast, which opens the upgrade dialog for subscription.
  • "Has a topic" means an active topic, the same as the Add Prompt dialog's list. A failed read counts as yes, so nobody is sent to add a topic they already have.

Kept as is: onboarding and brand setup (they save topics before prompts), topic suggestions (a way to add a topic), and roles.

Help: help/prompts/add-prompts.md gets "Add a topic first", and help/prompts/prompt-suggestions.md gets "A brand without topics".

Related issue

None. Product owner's request.

Type of change

  • fix — Bug fix

How to test

  1. On a brand with no topics, open Prompts › Tracked Prompts › Add prompt. The dialog says Add a topic first.
    • Add a topic switches to Topics and opens Add Topic.
    • Add one, go back, and Add prompt works.
  2. On Prompt Suggestions, Generate, New and Add All show the same.
    • No AI is used: the server answers 409 no_topics before generating.
  3. As an analyst on that brand, the notice asks an admin or a manager, with no link.
  4. On a brand with topics, every flow works as before.
  5. Unit tests:
    • new: topic-first.test.tsx, prompt-topics-required.test.ts, accept-prompt-suggestion.test.ts;
    • updated: Prompts page, Topics, Suggestions, Fan-out, AI Traffic, the demand card, Manage prompts, prompt-suggestions.test.ts;
    • server: prompts-suggestions-refresh.test.js and the AI Traffic page-suggestion route.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • Server: format, lint, tests pass (full suite 4,414; content-people.test.js timed out once under load, passes alone 32/32)
  • Web: format, lint, typecheck, tests (635 files / 6,300 tests before the review round; 22 files / 251 related tests after merging main); build: see below
  • Tests added
  • Help article updated (approved: false)

Local build: typecheck OK after merging main; the local yarn build ran out of disk and memory on this 8 GB machine (ENOSPC, then heap). CI runs the same production build on this PR.

Follow-ups, not in this PR:

  • On a brand that has topics, Fan-out Track and Google-query Track can still add a topicless prompt.
  • Refresh may still create topics from the AI's labels on a brand that has some.
  • The MCP coach skill (skills/ansvisor-aeo-coach/SKILL.md) doesn't mention the rule yet; a separate docs PR will follow.

🤖 Generated with Claude Code

cihan-gyk and others added 5 commits October 29, 2026 20:41
…gestions

Prompts are grouped under topics, but a brand with none could still add
them: the Add Prompt dialog said "No topics defined yet. Add topics in brand
settings." and saved the prompt without a topic, Track buttons did the same,
and generating suggestions made topics the person never chose from the AI's
labels (findOrCreateTopic).

Server (API):
- POST /api/prompts/suggestions/:brandId/refresh and /:brandId/page answer
  409 { code: 'no_topics' } for a brand with no active topic, after the
  role check and before anything is generated, reserved or written.

Server actions and MCP:
- addPromptToSet (every single add: suggestions, Add All, Insights Track,
  fan-out Track, AI Traffic, People Also Ask) and importPrompts (Add Prompt
  dialog, bulk import) return { error, code: 'no_topics' } and write
  nothing. trackPagePrompts checks before it makes a topic.
- refreshPromptSuggestions returns { ok: false, failure: 'no_topics' } as a
  value; other failures still throw, as before.
- accept_prompt_suggestion says to add a topic first (accept_topic_suggestion
  or Prompts > Topics), and its description says so.
- A failed topics read never counts as "no topics".

UI:
- One TopicFirstDialog / TopicFirstNotice (en.json "topicFirst") with an
  "Add a topic" link to /dashboard/prompts/topics?new=1, which opens Add
  Topic once and clears the param. Buttons stay visible; a click that hits
  no_topics shows it.
- Add Prompt shows the notice instead of the form when the brand has no
  topics; it reads topics on every open, so a topic added meanwhile counts.
- Brand > Manage prompts replaces the old "brand settings" text in both
  cards and asks for a topic instead of adding one without.
- Onboarding and brand setup (savePromptSet) are unchanged: they create the
  chosen topics first. Role gating (ansvisor#1763, ansvisor#1769) is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…uggestions failed

Review follow-ups on the topic-first flow.

- Prompts page: "Add a topic" in Add prompt, Suggestions and Query Fan-out
  is a button that closes the dialog, switches to the Topics tab with
  showSection and asks TopicsTab to open Add Topic (openAddTopic /
  onAddTopicOpened). The page reads its tab from the route only when it
  opens, so the ?new=1 link changed the address alone after a tab switch.
  Insights, AI Traffic and the prompt page keep the link.
- Roles: AddTopicLink, TopicFirstNotice and TopicFirstDialog read
  canManage. Since ansvisor#1790 only admins and managers write topics; anyone else
  gets managersOnly.addTopic and no link, and the dialog's button says Close.
- refreshPromptSuggestions returns every failure as a value, as ansvisor#1777's
  reads do (apiFailure + isTransientStatus on the body read once, so
  no_topics stays its own): a used-up budget, a read-only workspace or the
  demo brand, a role refusal, an outage, no answer. The card shows them with
  useFailureToast (the upgrade dialog for subscription) instead of a
  message production hides.
- Brand > Manage prompts links to that brand's own topics
  (/dashboard/brands/:id/topics#add-topic, as the brand card does), not the
  active brand's.
- trackPagePrompts reads active topics only; the AI Traffic page dialog
  forgets a Track's no_topics on the next open; the Insights test has its
  own describe block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	server/src/routes/prompts-suggestions-refresh.test.js
#	web/src/lib/actions/prompt-suggestions.test.ts
#	web/src/lib/actions/prompt-suggestions.ts
gkhngyk
gkhngyk previously approved these changes Oct 30, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Approving.

I followed every way a prompt gets added or suggested and checked that each one now stops on a brand with no topic. The refusal happens on the server as well as in the UI, and nothing that worked before breaks.

Entry points checked

  • Server actions: addPromptToSet and importPrompts return no_topics before they write anything. addPromptToSet covers single adds from Prompts, Manage prompts, suggestion accept, Add All and Insights Track (trackSuggestions), and Fan-out Track. trackPagePrompts checks before it creates any topic, which covers AI Traffic page prompts, Google-query Track and PAA Track.
  • API: suggestion refresh and page suggestions answer 409 no_topics after the brand and role checks. That's before any AI call. Refresh is only ever started by a click, so a brand without topics never gets the dialog unless someone asks for suggestions.
  • MCP: accept_prompt_suggestion refuses before it reads the prompt set, and the message points to list_topic_suggestions / accept_topic_suggestion, which both exist. Chat's registry has no prompt-adding tool, and the REST v1 routes are read-only.
  • Topic scoping: every topic lookup is by brand_id of the prompt set's own brand (or the brand already checked against the organization in MCP), so a topic from another brand can't be used.
  • Not affected: onboarding and Add Brand save the selected topics before savePromptSet, and both need at least one topic to continue. The demo brand and read-only workspaces still hit assertWritable / the 402 first. Analysts get the "ask an admin or a manager" wording with no link, which matches the server's role checks.
  • Embeds: none added. The new reads are topics.select('id') / select('name') with no embed.
  • refreshPromptSuggestions no longer throws, and its only caller (the suggestions card) handles every result.

Tests: server refresh and page-suggestion route tests pass (18). The 23 related web test files pass (252 tests), and so do plan-row.test.ts, subscription-guard-coverage.test.ts and the demo gate tests. tsc is clean, and next build passes on this head. The PR already contains the latest main.

Merging with other open PRs: two small conflicts, both easy.

  • web/src/lib/actions/fanout.ts (with #1800 and the parts built on it): only the import lines. Keep both: dbClient plus addPromptToSet, type PromptAddFailureCode.
  • prompts/page.test.tsx (with #1814): two neighbouring vi.mock blocks. Keep both.

Follow-ups (not blocking)

  1. savePromptSet has no topic check. Today the UI can't reach it without topics: onboarding and Add Brand save topics first, and Manage prompts checks topics.length before a manual add. A one-line brandHasTopics guard (when prompts isn't empty) would make "the client isn't trusted" true for that path too.
  2. help/prompts/prompt-suggestions.md: "Generate and New ask you to add a topic first and open Prompts › Topics". They show the Add a topic first dialog, and its Add a topic button (admins and managers) opens Topics. Something like "…ask you to add a topic first, with an Add a topic button for admins and managers" would match.
  3. Bulk import on a brand that has topics still saves prompts with no topic (topic: null), the same as the Fan-out and Google-query Track cases already listed as follow-ups.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging in order with the other approved PRs. This head merges current main and keeps both sides of the conflicts with #1800 (fanout.ts imports) and #1814 (prompts/page.test.tsx mocks).

@gkhngyk
gkhngyk merged commit 5b10730 into ansvisor:main Oct 30, 2026
4 of 5 checks passed
gkhngyk pushed a commit that referenced this pull request Nov 6, 2026
…and choose_plan (#2214)

The three coach-skill fixes #1808, #1694 and #1671 left for a docs PR,
in the MCP skill and its standalone copy:

- A brand needs a topic before any prompt: accept_prompt_suggestion is
  refused on a brand without one (#1808). The skill says so, and offers
  a topic suggestion first.
- list_action_history reads an action 7, 14 and 30 days after it
  closed (#1694): reading_day and final are documented, and an outcome
  before day 30 is reported as a reading so far. The outcome list gains
  mixed.
- get_prompt_usage marks an ended plan with plan.ended and the
  choose_plan cta (#1671): no slot is left until a plan is chosen.


Claude-Session: https://claude.ai/code/session_01SvzkMf5TZg7yqwaSeMFba9
(cherry picked from commit 17b7ea8)

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants