Repository navigation
feat(mentions): read Mentions & Sentiment and correct a sentiment from the API and MCP - #1126
Conversation
|
@gkhngyk the last Mentions piece: the page's reads over the API and MCP (summary, list, one answer, competitors, perception) and "Sentiment is wrong" for admin/manager keys, plus the five reads in the in-app agent. On deploy, after 00157–00161, please apply 00162 (override_mention_sentiment gains p_user_id for service-role calls). The in-app agent's new tools could use one live question with your model key, e.g. "why did sentiment drop on Gemini?". Stacked on #1125 → … → #1115. |
|
Two things block this from landing on its own:
If you'd rather land the API now, move the Perception route and tools (with their tests and the docs lines) into a small follow-up stacked on #1124, and rebase the rest on main after #1125. Otherwise this one, and #1127 with it, waits for #1123/#1124. |
gkhngyk
left a comment
There was a problem hiding this comment.
@cihan-gyk this needs a rebase on main (after #1123). The branch carries an older copy of the stack, and merging it as it stands would undo two fixes that are already on main. Main has no Mentions MCP tools or routes yet, so the API/MCP work itself is still new.
Drop from the branch. Main or #1123 has the current version:
00158_mentions_list.sql: this is the copy from before the read-first rewrite. It would remove the partial index onmention_count > 0and themention_rowsCTE that brought the 30-day table from 8.8 s to under 2 s.00161_mentions_perception.sql: this older copy restatesrefresh_mentions_dailywith the terms inside it.00160_mention_sentiment_override.sql: identical to main.server/src/lib/mentions-rollups.jsand its test: take #1123's version.action-center/from-mentions.js,validate.js,_mention-parts.tsx,_response-drawer.tsxand the other add/add conflicts: these come from the old #1117/#1118 copies. Take main's.
Renumber: 00162_mention_sentiment_override_api.sql collides with main's 00162_refresh_insights_daily_range.sql.
- It depends on 00160 (
sentiment_overrides,sentiment_visibility_bonus), so it can't go in the 00159 gap, and #1123 keeps 00161. - Use the next free number when you rebase. The open content stack (#1256 … #1307) takes 00204–00219, so that's 00220 for now.
- The content is fine: it's 00160's function plus
p_user_id, it only calls therefresh_*functions, and nothing later on main redefines it.
After the rebase:
- Regenerate
types/supabase.tsfor the 4-argumentoverride_mention_sentiment, re-runbuild-schema.sh, and resolve thedocs/guides/mcp-server.mdxconflict. Main added tools in #1156/#1157. - If #1226 / #1230 land first, follow them:
get_mentions_summaryshould report "mentions" as the dashboard's Total mentions does (occurrences).- Its
changesshould come from the same-prompt basis (changeBasis), so the MCP numbers match the page.
|
Update: #1308 takes 00220 as well, so the next free number is 00221. Please check |
458276b to
39f9aad
Compare
|
@gkhngyk Rebased on
Checked: server 1,920 tests, lint; web 1,963 tests, tsc, lint, Prettier and |
Preview approval requiredThis pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit |
gkhngyk
left a comment
There was a problem hiding this comment.
Needs one more rebase on the current main, then it's ready. The code is right. GitHub reports it CONFLICTING because the Content stack landed after this branch was cut.
CI "failure"
- The failing check is Mintlify Preview (fork), with conclusion
action_required. Mintlify won't build a docs preview for a fork PR until someone with write access approves the commit. It runs because the PR editsdocs/guides/mcp-server.mdx. - All four code checks pass: schema, migrations flag, web and server. Vercel was still pending.
- Approve the preview from the PR's checks page, or ignore it. Nothing in the code needs to change for it.
Rebase
- The branch is #1124 plus
39f9aad5. - The old copies of
00158,00160,00161,mentions-rollups.jsand the #1117/#1118 files are gone. What's left is identical to main or to #1123.
Conflicts on the current main
web/src/lib/mcp/server.ts: main's Content tools (#1305) and the Mentions tools both add imports andregisterToolblocks at the same spots. Keep both. The tool names don't overlap.supabase/schema.sql: re-runbash supabase/build-schema.sh, so 00223 follows 00221.en.json,types/supabase.tsand the docs merge cleanly.
Migration 00223_mention_sentiment_override_api.sql
- It's a pure renumber: only the header line changed from the old 00162.
- 00223 is free. Open PRs hold 00222 and 00224–00227.
- It sorts after 00160, which it depends on. Nothing on main up to 00221 redefines
override_mention_sentiment,refresh_mentions_dailyorrefresh_insights_daily. types/supabase.tschanges only the function'sArgs, addingp_user_id?.- Prod still has the 3-argument function. Apply 00223 before merging: the MCP and REST write passes
p_user_id, and the dashboard's 3-argument call keeps working through the default.
Nits (optional)
- The filters'
platformdescription gives"perplexity"as an example. That's the legacy API id; tracked answers useperplexity-web. get_mentions_perception's description still says "share of its mentions". #1124 now labels these "share of the responses naming".- As you said,
get_mentions_summaryshould switch to occurrences andchangeBasisonce #1226 / #1230 land.
…m the API and MCP The Mentions page's figures over the API and MCP (MS-13): summary, the mentions list, one answer, competitors and perception, under the page's filters, from the page's own database functions after a brand check; and "Sentiment is wrong" as the API key's owner. Migration 00162 lets override_mention_sentiment correct for that person when the service role calls, still only for an admin or manager of the answer's organization. The in-app agent gets the five reads. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The tool description said null only when no competitor leads by 20+ points; it is also null when the brand or that competitor has fewer than 20 mentions (ansvisor#1124's low-sample rule), as the skills now say (ansvisor#1127). Co-Authored-By: Claude Opus 5.5 <[email protected]>
39f9aad to
bd20194
Compare
|
@gkhngyk Rebased onto the current main (58007fc, with #1123, #1124, #1316 and #1318 in), force-pushed with lease from 39f9aad.
00223 is on the dev DB; as you said, it goes on production before this merges. #1127 follows with its two wording fixes. |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. All the earlier review items are done:
- Rebase.
git merge-treeonto the current main (c66906ff) is clean. Inserver.tsthe Content tools and the Mentions tools are both registered, and nothing from main was dropped.schema.sqlmatches a freshbuild-schema.sh, with 00223 between 00221 and 00224. - Nits. The platform example is now
perplexity-web. The perception descriptions say "share of the responses naming it". bd20194 adds thebiggestGaplow-sample rule. - 00223. The number is still free.
p_user_idcounts only for the service role, and a signed-in caller is alwaysauth.uid(). Either way the person must be an admin or manager of the answer's organization.anonandpubliccan't execute the function. We'll apply it to the hosted database before merging.
One small thing before merge: please update the PR description, since it becomes the squash commit message.
# Conflicts: # supabase/schema.sql
…s do Total mentions' change is now on times named (occurrences), and every change is taken on the prompts both windows answered (changeBasis, 00195), with the counts behind it in changes.basis. The mention rate's change is null when either window has under 20 responses, as on the card. Adds promptsWithMentionsPct, and timesNamed per platform and day. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@gkhngyk switched as planned, in 416facf (on top of a merge of main, 2a14dbf):
Checks: web format, lint (0 errors), typecheck, all 2622 tests and build pass. Ready for 00223 and the merge with #1127 when you are. |
gkhngyk
left a comment
There was a problem hiding this comment.
Looks good. The switch is in (416facf), and get_mentions_summary now takes its changes as main's Overview cards do (_overview-kpis.tsx):
changes.mentionsPctis the change in occurrences, taken onchangeBasis(web/src/lib/mcp/mentions.ts:266).- The rate's change is null when the current window or either shared window has under 20 responses. This is the card's
rateChangeLow. promptsWithMentionsPctuses the same basis.netSentimentPointsstill comes frommentionsHeadline, which useschangeBasis.- The field names match main's
mentions-overview.ts:changes.basisreadssame.prompts,newPromptsanddroppedPrompts;timesNamedreadsoccurrenceson the totals, the platform rows and the days;- production's
mentions_overviewreturnssame,new_prompts,dropped_promptsandoccurrences.
- The tests cover the shared-prompt basis and the stricter rate rule.
- The merge of main (2a14dbf) re-merges to the same tree and carries nothing else.
Two small things. Neither blocks the merge:
- Optional rename, cheaper before the API ships.
current.mentions,platforms[].mentionsanddaily[].mentionsare the responses naming the brand.changes.mentionsPctsits beside them but is the change intimesNamed(mentions.ts:188,:266). A client that works out the change fromcurrent.mentionsandprevious.mentionsgets a different number.- Renaming it
changes.timesNamedPctties each change to the figure it measures. The MCP and agent descriptions, themcp-server.mdxrow and #1127's two lines would follow.
- PR description. It becomes the squash message. Its
get_mentions_summaryrow still reads "mentions, mention rate, …changeswith the page's low-sample rules". Please add times named, and that every change is taken on the prompts both windows answered (changes.basis).
00223
- It isn't on production yet, and the 3-argument function is still the live one.
- The file hasn't changed since the last review. Its body is the live function's plus the
v_personline, and its grants match. refresh_insights_dailyandrefresh_mentions_dailystill take(uuid, date, date).- 00231's Perception rollups hold no sentiment, so a correction doesn't need to refresh them.
- The number is still free on main. We'll apply it before merging.
This merges cleanly on main (12846db) and with #1127, and schema.sql keeps 00223 between 00221 and 00224. With this PR the server registers 50 tools. mcp-server.mdx:223 still says 36 and is left for the follow-up docs PR.
00223 is applied on production. The old 3-argument function is replaced, execute is granted to authenticated and service_role only, and an outsider passing a member's id as p_user_id is refused (42501). Tested on top of main together with #1333: web and server tests, tsc, lint and Prettier pass, and schema.sql matches a rebuild.
* docs(skills): teach the AEO coach the Mentions & Sentiment tools Both skills learn the six Mentions tools and REST endpoints from #1126: the numbers to quote (net sentiment, low sample, brand only), a workflow for "why did sentiment drop on Gemini?", and the correction as a write. The sentiment reference explains net sentiment and the README lists the new questions. Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(skills): say which Mentions changes are null, and when biggestGap is - Only changes.netSentimentPoints is null on a low sample (and the mention rate's change under 20 tracked responses); mentionsPct isn't gated. - biggestGap is also null when the brand or the leading competitor has fewer than 20 mentions (#1124's low-sample rule). Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(skills): say Mentions changes are on the prompts both windows answered, and mentionsPct is times named Co-Authored-By: Claude Opus 5.5 <[email protected]> * docs(skills): lead the Mentions glossary with times named, the page's Total mentions As the Overview card counts them, "mentions" are every time AI names the brand (timesNamed): the coach quotes that for "how many mentions", and mentions (responses naming the brand) as the base of the rate, net sentiment and lowSample, whose 20 is responses naming the brand. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Cihan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
Summary
Mentions & Sentiment over the API and MCP (MS-13, spec §14). The page's figures are now readable with an
ans_key, so Claude Desktop, Cursor and the in-app AI Agent Chat can answer "why did sentiment drop on Gemini?". "Sentiment is wrong" is available as a write for admins and managers. The numbers are the dashboard's: every read goes through the page's own database functions, and no LLM is called.Reads (
web/src/lib/mcp/mentions.ts)Each read first checks the brand belongs to the key's organization, then calls
mentions_overview(00157),mentions_list(00158) ormentions_perception(00161) as the service role, which those functions already let through.date_from/date_to(the last 30 days when left out, as the page; adate_toofYYYY-MM-DDcovers that day),platform,region,topic_idandsearchover the prompts' text.get_mentions_summaryGET /api/mcp/mentions/summarylowSample), now and before,changeswith the page's low-sample rules, the summary line's facts (the platform that moved net sentiment, companions), per platform and per day.list_mentionsGET /api/mcp/mentionssentiment,competitor_id(nonetoo),sortandpage; each answer cut to its excerpt on the server.get_mentionGET /api/mcp/mentions/{id}get_mention_competitorsGET /api/mcp/mentions/competitorsget_mentions_perceptionGET /api/mcp/mentions/perceptionWrite: "Sentiment is wrong"
override_mention_sentimentPOST /api/mcp/mentions/{id}/sentiment-overridewith{ "sentiment", "note"? }00223_mention_sentiment_override_api.sql(renumbered from 00162, which main's00162_refresh_insights_daily_range.sqltook; 00204–00222 are the open Content and Billing PRs):override_mention_sentiment(00160) took the person fromauth.uid(), which is null for API keys. It is restated withp_user_id:auth.uid(), whatever it passes;42501), 400 (22023) and 404 (P0002).In-app AI Agent Chat
lib/agent/tools.tsgets the five reads, as thin wrappers like its other tools. The system prompt lists them and says how to read net sentiment and a low sample. The write stays out of the chat.Docs
docs/guides/mcp-server.mdxhas a Mentions & sentiment section with the tools, the REST paths and a curl example. The server registers 50 tools with this PR; the guide's count (mcp-server.mdx:223) is updated to 50 in a one-line docs PR right after this merges, so this approved branch doesn't move. The skills (skills/*/SKILL.md) follow in a separate docs PR.Deploy
Apply 00223 after 00160 and 00161 (it restates 00160's function with
p_user_id; nothing later redefines it).Related issue
None.
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Apply 00223 (00157–00161 are on the hosted DB) and run
node src/scripts/backfill-mentions-daily.js(server), as in feat(mentions): add the per-answer terms and the Perception read #1123.Create a key in Settings → API Keys, then call:
&platform=gemini-web,&date_from=…or&search=…: the figures follow the page's filters.GET /api/mcp/mentions?brand_id=…&sentiment=negative: the total matches the Mentions tab with Sentiment = Negative. Take anidandGET /api/mcp/mentions/<id>.As an admin's key:
In an MCP client,
tools/listshows 50 tools, the six*mention*ones among them.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/): no errors; the 7 warnings are in files this PR does not touchyarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)Also run:
web:yarn test(1,963) andyarn buildpass.lib/mcp/mentions.test.ts:date_to;mentions/route.test.tsand[id]/sentiment-override/route.test.tscover query parsing, body checks and status codes.lib/agent/system-prompt.test.ts: the tools and the low-sample rule.bash supabase/build-schema.sh: regeneratedschema.sql.ans_key (deleted afterwards):🤖 Generated with Claude Code