Skip to content

feat(content): let agents link over the API, to the brand's pages and the draft's sources - #1372

Merged
gkhngyk merged 12 commits into
ansvisor:mainfrom
cihan-gyk:feature/agent-links-mcp
Oct 23, 2026
Merged

gkhngyk merged 12 commits into
ansvisor:mainfrom
cihan-gyk:feature/agent-links-mcp

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Agents working through the editor API (MCP suggest_content_changes) can now link: to the brand's own pages and the draft's sources, as the in-app Content Agent does since #1371.

Stacked on #1371 (which is stacked on #1369 and #1370). This PR's own change is c4a5062. Merge after #1371.

get_content_draft (and its REST twin) returns link_targets:

  • own_pages: the brand's pages from site_pages, the most cited and visited first. Up to 40; failed fetches and the draft's own URL are left out.
  • sources: the draft's sources from the content_draft_sources RPC plus the ones the person added. Up to 20, without the brand's or a competitor's domains, and without the ones left out.
  • These are the only URLs a suggestion may link to.

suggest_content_changes

  • A replace or an insert may link words as <a href="URL">words</a>. A link stays only when its URL matches a target (linkKey: host without www, no tracking parameters, hash or trailing slash), and it keeps the target's clean URL.
    • Any other link is unwrapped to its words.
    • A heading never keeps a link, and a link with no words is dropped.
    • A replace that then changes nothing is dropped as no_change, as before.
  • A replace's links are stored in after_links (00240, from feat(content): let the Content Agent link to the brand's pages and the draft's sources #1371), by offset into its words. An insert keeps them in its HTML. The editor puts them on as link marks, as for the Content Agent.
  • Cost: the targets are read only when a suggestion contains an <a, so most calls don't run the sources RPC.

Code

  • web/src/lib/content/link-targets.ts (new) mirrors server/src/lib/content-links.js: linkKey, linkHref, linkAllowList and pickLinkTargets, with competitors matched through classifyDomain / normalizeDomain.
  • agent-suggestions.ts:
    • cleanInsertHtml(html, links) uses sanitize-html. a is renamed to a dropped span unless allowed, then links inside h2–h4 are stripped.
    • inlineText reads the sanitized inline HTML into words plus link offsets, with the same offsets as the server's inlineText.
    • splitReplace and keepAgentSuggestions pass the allow-list through.
  • lib/mcp/content.ts: linkTargetsOf(draft). A failed read is logged and leaves its list empty. get_content_draft reads targets, locked passages and open suggestions in parallel.
  • The tool and schema descriptions say links go only to link_targets URLs.

No migration.

Related issue

Content Agent rule gaps, item A (part 2), found by testing on prod on 2026-10-22. No issue.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

With an API key (MCP or the REST twin):

  1. get_content_draft on a draft: link_targets.own_pages lists the brand's pages, and link_targets.sources lists the draft's cited sources, with no competitor and no left-out source.
  2. suggest_content_changes with a replace linking words to one of those URLs: the card in the editor underlines the words and lists "Links: …", and accepting it adds the link.
  3. Do the same with an invented URL: the link is unwrapped to its words. If nothing else changed, the suggestion comes back in dropped as no_change.

Not tried end to end locally: that needs an API key (gkhngyk's). Covered by tests:

  • link-targets.test.ts: targets, competitors with a path, left-outs, no sources without their read;
  • agent-suggestions.test.ts: kept and invented links, links in headings, empty links, inline offsets;
  • content.test.ts: link_targets in get_content_draft, after_links written, no sources read when nothing links.

The pre-push review also ran ~15 hostile inputs through the real sanitize-html 2.17 (javascript:, made-up domains, extra attributes, a duplicate href, <a> in an svg, uppercase tags in headings): no link outside the allow-list survived. It also confirmed the offsets match the server's inlineText.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

Also run: web yarn test (324 files, 2,854 tests) and yarn build.

🤖 Generated with Claude Code

cihan-gyk and others added 11 commits October 22, 2026 22:42
A replace on a heading that carried its answer ("What is X? X is …") or a
whole FAQ was flattened into the heading's line. Content Agent and the
editor API now split such a replace: the heading keeps its words and the
answer or blocks become an insert after it, kept together or not at all.
An agent change may not grow an H1 past 70 characters or another heading
past 90 (a long imported heading may still be shortened); both agent
prompts and the MCP description say a heading stays one line, and the
editor won't accept an older suggestion that would glue text into one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A page's H1 is its title. The editor now makes a second H1 an H2 however it
comes (typed, pasted, the HTML view, Add HTML or an agent suggestion),
keeping the page's own; Heading 1 is off while the page has one, and says
why on hover. A version restored from History and older drafts keep theirs,
and a new always-on "One H1 per page" rule flags them. The Content Agent is
never asked to fix it (it doesn't change heading levels) and its inserted
H1s become H2s. URL import keeps the H1 nearest the article's text, not a
logo's or a site name's, and makes the rest H2s.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
…nly a real answer

Review fixes for ansvisor#1369 (gkhngyk):
- the question/answer split runs whenever the passage is a heading, also
  when blocks follow, and the answer goes first under it (inline answer
  plus a list, or <h2>Q? A.</h2><p>…);
- a heading that fits keeps a subtitle: only an answer that ends a
  sentence (. ! 。 !) is split off; past the limit the rest always goes;
- "?" ends a question only before a space; a full-width "?" with or
  without one;
- the regex has no catastrophic case on long text, and the garbled doc
  comment in agent-suggestions.ts reads again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ent, count HTML blocks'

Review fixes for ansvisor#1370 (gkhngyk):
- URL import picks the title's H1 once, before the frame and the root go:
  the last H1 with words before the text's first paragraph (a byline,
  date or breadcrumb line doesn't count as one), else the first; the
  title comes from it, and the root's other H1s become H2s;
- New content keeps one H1 in a pasted page, a file or an imported page
  (oneH1Html in bring(): the first H1 with words stays, others are H2s,
  empty ones unwrapped);
- "One H1 per page" counts H1s with words inside HTML blocks too;
- an empty H1 line never counts as the page's H1 in the editor, so it
  never stays over one with words, and Heading 1 stays on beside it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e draft's sources

The agent could never meet the Internal links, Outbound links or Cite
sources inline rules: a replace's new words were plain text, inserts took
no <a>, and it had nowhere to link to. When one of those rules is asked
for, it now gets the brand's own pages (site_pages, the most cited and
visited first) and the draft's sources (cited pages, not the brand's or a
competitor's, not left out), and may link only to those URLs: any other
link is unwrapped, tracking parameters come off. A replace keeps its links
beside its words (after_links, 00240); an insert keeps them in its HTML.
The editor puts them on as link marks when a suggestion is accepted, takes
them off on undo, and shows on the card where each one goes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	server/src/lib/content-agent.js
… the draft's sources

The editor API's suggest_content_changes took no links, so an agent working
through MCP could not meet the link rules either. get_content_draft now
lists link_targets (the brand's own pages, and the draft's sources without
the brand's or a competitor's), and suggest_content_changes keeps a link
only to one of those URLs, as the Content Agent does on the server: a
replace's links go in after_links, an insert's stay in its HTML, any other
link is unwrapped and a link with no words dropped. The targets are read
only when a suggestion links.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 22, 2026
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Content Agent rule gaps A2, stacked on #1371. MCP agents get the same links as the in-app agent:

  • get_content_draft lists link_targets;
  • suggest_content_changes keeps a link only to one of those URLs.

Testing it end to end needs an API key, so it is yours if you'd like to try it. The tests and a sanitize-html probe cover it.

A review pass before pushing asked for three changes, now in:

  • the reads run in parallel, and the targets are read only when a suggestion links;
  • a failed read is logged;
  • a link with no words is dropped.

gkhngyk
gkhngyk previously approved these changes Oct 23, 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. The same two follow-ups as #1371, on the web side:

  1. Redirecting pages as targets (web/src/lib/mcp/content.ts:259). Keep 2xx only (fetch_status.lt.300), as in #1371, so the API and the in-app agent offer the same list.

  2. A link in a heading's split-off answer is lost (web/src/lib/content/agent-suggestions.ts:188, :201). The answer paragraph is built from escapeHtml(answered[2]), so a listed link inside it is dropped. Fix: carry the answer's links into the paragraph by offset, as in #1371.

Checked and fine:

  • API scope.
    • getContentDraftFor and suggestContentChangesFor both resolve the draft through draftFor, which checks the key's organization (brands!inner). Only after that does linkTargetsOf read site_pages and the sources by that draft's brand_id and id.
    • API keys are scoped to an organization, so there is no brand scope to check as well.
    • The REST twins call the same functions.
  • Only listed URLs survive.
    • In sanitize-html's transformTags.a, a link stays an <a> only when its key is in the allow-list. It then keeps only href, set to the target's URL. Anything else becomes a dropped span and keeps its words.
    • Targets come from linkHref, which is http(s) only, so javascript: and other schemes can't come through.
    • Links in h2–h4 are stripped, and links with no words go through exclusiveFilter.
    • When no suggestion contains <a, links is null and every link is unwrapped. That fails safe.
  • Parity with the server.
    • linkKey, linkHref, the caps (40 and 20) and the filters match content-links.js.
    • The competitor and own-domain checks, classifyDomain/normalizeDomain here and ownDomainMatcher on the server, use the same host-or-subdomain rule.
    • inlineText reads sanitize-html's output, with entities decoded, into the same offsets as cheerio's. It handles nested anchors the same way too.
  • Writes. after_links is sliced to 20 and set on replaces only, within 00240's check.
  • Cost. There is no LLM call. get_content_draft reads the targets alongside the locked passages, so it waits on the slower of the two. The content_draft_sources read takes 0.15 s warm and 1.3 s cold on the largest brand. suggest_content_changes skips it when nothing links. The response grows by at most 60 short entries.

@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.

Re-approving after merging main into the branch. The merge only brings in the squashed parent PR; the tree matches the one tested (web and server tests, typecheck, lint, format).

@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 23, 2026
@gkhngyk
gkhngyk merged commit 03bfd33 into ansvisor:main Oct 23, 2026
4 of 5 checks passed
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