AI ad writer: "Write my ad from my link" on the ad form (Marty 2026-11-17: "okay, let's do it.")

adwriter.js: the server reads the member's page (or follows their LinkSpin spin link, max 5 hops),
SSRF-safe (http/https, ports 80/443, every hop's DNS answer checked and the connection pinned to it,
no private/loopback/link-local/metadata address, 8 s, 1.5 MB, text/html only), extracts title, meta
and og description, h1-h3 and the first paragraphs (about 2,500 tokens), and asks OpenRouter
deepseek/deepseek-v4-flash (reasoning off; fallback anthropic/claude-haiku-5.5) for 3 options
{headline <= 60, line <= 90} written only from the page. Every option goes through the new
safety.checkAdCopy (no dashes, income promises, guarantees, links, shouting; every number must be
on the page) plus the length limits; failing ones are dropped, one retry when none survive. The
member picks one, it fills the form, nothing is posted.
Limits: AI_AD_DAILY (5) per member per Central day, AI_AD_GLOBAL_DAILY (500), 12 an hour per IP,
one at a time, a 24 h cache by page (a repeat is free). Off and hidden without AI_AD_KEY (a dedicated
OpenRouter key, $5 a month). Admin status: writes today, tokens, estimated spend.
safety.privateIp: IPv4-mapped IPv6 in hex form (::ffff:7f00:1) and all of fe80::/10 now count as private.
Chatbot fact added (KNOWLEDGE_DATE 2026-11-17). Server wiring + form markup landed in 828abce.
qa/adwriter.test.mjs 82/82, qa/e2e.mjs 63/63, qa/chatbot.test.mjs 108/108.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-17 07:12:12 -06:00
parent 828abce0fd
commit def20f8ffe
8 changed files with 574 additions and 5 deletions
+2 -2
View File
@@ -20,7 +20,7 @@
<main id="main" class="wrap" style="padding-top:30px;padding-bottom:56px">
<h1 style="font-size:36px">Admin</h1>
<div class="row" style="margin-top:12px"><input type="text" id="tok" placeholder="admin token" style="max-width:320px"><button class="btn sm ink" id="go" type="button">Load</button></div>
<section class="panel" style="margin-top:18px"><h2 style="font-size:22px">Status</h2><pre id="statusOut" style="white-space:pre-wrap;font-size:12px;max-height:420px;overflow:auto"></pre></section>
<section class="panel" style="margin-top:18px"><h2 style="font-size:22px">Status</h2><p id="aiOut" class="muted" style="margin:6px 0 8px;font-size:14px"></p><pre id="statusOut" style="white-space:pre-wrap;font-size:12px;max-height:420px;overflow:auto"></pre></section>
<section class="panel" style="margin-top:18px"><h2 style="font-size:22px">Sign-ups (L2 checks)</h2><div class="tbl"><table><thead><tr><th>Id</th><th>Status</th><th>Email</th><th>Resharer</th><th>Checks</th><th>Action</th></tr></thead><tbody id="suRows"></tbody></table></div></section>
<section class="panel" style="margin-top:18px"><h2 style="font-size:22px">Pending-rewards ledger</h2><div class="tbl"><table><thead><tr><th>Id</th><th>To</th><th>Kind</th><th>Cents</th><th>Status</th><th>Voucher</th></tr></thead><tbody id="rwRows"></tbody></table></div></section>
<section class="panel" style="margin-top:18px" id="testBox"><h2 style="font-size:22px">Staging: mark a test member</h2><p class="muted">Staging only. Lets a wallet post and reshare without a real network account.</p>
@@ -28,6 +28,6 @@
</main>
<script src="/assets/common.js?v=2"></script>
<script src="/assets/chat.js?v=1" defer></script>
<script src="/assets/admin.js?v=2"></script>
<script src="/assets/admin.js?v=20261117a"></script>
</body>
</html>
+1
View File
@@ -8,6 +8,7 @@
async function load() {
if (!tok) return;
const s = await api('/api/admin/status'); $('statusOut').textContent = JSON.stringify(s, null, 2); $('testBox').hidden = !s.testSwitch;
{ const w = s.adWriter || {}; $('aiOut').textContent = 'AI ad writer: ' + (w.on ? 'on' : 'off (no AI_AD_KEY)') + '. Today (' + (w.day || '') + ' Central): ' + (w.writes || 0) + ' writes by ' + (w.members || 0) + ' members, ' + (w.cacheHits || 0) + ' free repeats, ' + (w.failed || 0) + ' failed, ' + (w.tokensIn || 0) + ' + ' + (w.tokensOut || 0) + ' tokens, estimated spend $' + Number(w.costUsd || 0).toFixed(4) + ' (key cap ' + (w.keyLimit || '') + ').'; }
const su = await api('/api/admin/signups');
$('suRows').innerHTML = (su.signups || []).map(x => '<tr><td class="mono">' + esc(x.id) + '</td><td>' + esc(x.status) + '</td><td>' + esc(x.email) + (x.confirmLink ? '<br><a href="' + esc(x.confirmLink) + '">staging confirm link</a>' : '') + '</td><td>' + esc(x.resharer || '')
+ '</td><td style="font-size:12px">' + esc(JSON.stringify(x.checks || {})) + (x.fail.length ? '<br><b>fail:</b> ' + esc(x.fail.join(', ')) : '') + (x.hold.length ? '<br><b>hold:</b> ' + esc(x.hold.join(', ')) : '')
+17
View File
@@ -32,6 +32,23 @@
box.innerHTML = spins.map((x, i) => '<button type="button" data-i="' + i + '"' + (x.paused || !x.destinations ? ' disabled' : '') + (x.url === $('link').value ? ' class="on"' : '') + '>' + esc(x.name || x.code) + ' <span>' + (x.paused ? 'paused' : x.destinations + ' page' + (x.destinations === 1 ? '' : 's')) + '</span></button>').join('');
box.querySelectorAll('button[data-i]').forEach(b => { b.onclick = () => { const x = spins[Number(b.dataset.i)]; $('link').value = x.url; box.querySelectorAll('button').forEach(y => y.classList.remove('on')); b.classList.add('on'); $('track').checked = false; VAS.status('Spin link picked. Save your ad to use it.', 'ok'); }; });
};
// the AI ad writer (2026-11-17): 3 suggestions written only from the member's own page; picking one fills the form, nothing is posted
if (c.aiWriter) {
$('aiBox').hidden = false;
let opts = [];
$('aiWrite').onclick = async () => {
const box = $('aiList'), url = $('link').value.trim();
if (!/^https?:\/\/\S+\.\S+/i.test(url)) { box.innerHTML = '<p class="note err" style="margin:8px">Put your link in the Your link box first (starting with https://), then tap the button again.</p>'; $('link').focus(); return; }
$('aiWrite').disabled = true; box.innerHTML = '<p class="muted" style="margin:0;padding:10px 12px;font-size:14px">Reading your page and writing 3 ads. This takes a few seconds...</p>';
const r = await VAS.post('/api/my/adwriter', { url }).catch(() => ({ error: 'The ad writer did not answer. Try again in a minute.' }));
$('aiWrite').disabled = false;
if (r.error) { box.innerHTML = '<p class="note err" style="margin:8px">' + esc(r.error) + '</p>'; return; }
opts = r.options || [];
box.innerHTML = opts.map((o, i) => '<button type="button" data-ai="' + i + '" style="flex-direction:column;align-items:flex-start;padding:10px 12px;gap:2px"><b>' + esc(o.headline) + '</b><span>' + esc(o.line) + '</span></button>').join('')
+ '<p class="muted" style="margin:0;padding:8px 12px;font-size:13px;border-top:1px solid var(--line)">Tap one to use it. Check every word is true for your offer before you post.' + (typeof r.left === 'number' ? ' Ad writes left today: ' + r.left + '.' : '') + '</p>';
box.querySelectorAll('button[data-ai]').forEach(b => { b.onclick = () => { const o = opts[Number(b.dataset.ai)]; $('headline').value = o.headline; $('line1').value = o.line; box.querySelectorAll('button[data-ai]').forEach(y => y.classList.remove('on')); b.classList.add('on'); VAS.status('Filled in. Edit it as you like, then tap the button below to save.', 'ok'); }; });
};
}
$('adForm').addEventListener('submit', async e => {
e.preventDefault(); $('saveAd').disabled = true;
const r = await VAS.post('/api/my/ad', { headline: $('headline').value, line1: $('line1').value, line2: $('line2').value, link: $('link').value.trim(), track: $('track').checked });