@ainyc/canonry 2.10.1 → 2.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/assets/assets/{index-PhLDQh1e.js → index-Ckr4V5dK.js} +108 -108
- package/assets/index.html +1 -1
- package/dist/{chunk-SZSWQG3J.js → chunk-FCYNFM4B.js} +1055 -267
- package/dist/{chunk-KWQCQMPY.js → chunk-PLI7EOPM.js} +177 -0
- package/dist/{chunk-PYHANJ3B.js → chunk-UM6RDSRJ.js} +327 -0
- package/dist/cli.js +250 -12
- package/dist/index.js +3 -3
- package/dist/{intelligence-service-2ZABHNR4.js → intelligence-service-54F3NGPM.js} +1 -1
- package/dist/mcp.js +62 -11
- package/package.json +9 -9
|
@@ -1540,6 +1540,148 @@ var ccCachedReleaseSchema = z14.object({
|
|
|
1540
1540
|
lastUsedAt: z14.string().nullable()
|
|
1541
1541
|
});
|
|
1542
1542
|
|
|
1543
|
+
// ../contracts/src/composites.ts
|
|
1544
|
+
import { z as z15 } from "zod";
|
|
1545
|
+
var searchHitKindSchema = z15.enum(["snapshot", "insight"]);
|
|
1546
|
+
var projectSearchSnapshotHitSchema = z15.object({
|
|
1547
|
+
kind: z15.literal("snapshot"),
|
|
1548
|
+
id: z15.string(),
|
|
1549
|
+
runId: z15.string(),
|
|
1550
|
+
keyword: z15.string(),
|
|
1551
|
+
provider: z15.string(),
|
|
1552
|
+
model: z15.string().nullable(),
|
|
1553
|
+
citationState: citationStateSchema,
|
|
1554
|
+
matchedField: z15.enum(["answerText", "citedDomains", "searchQueries", "keyword"]),
|
|
1555
|
+
snippet: z15.string(),
|
|
1556
|
+
createdAt: z15.string()
|
|
1557
|
+
});
|
|
1558
|
+
var projectSearchInsightHitSchema = z15.object({
|
|
1559
|
+
kind: z15.literal("insight"),
|
|
1560
|
+
id: z15.string(),
|
|
1561
|
+
runId: z15.string().nullable(),
|
|
1562
|
+
type: z15.enum(["regression", "gain", "opportunity"]),
|
|
1563
|
+
severity: z15.enum(["critical", "high", "medium", "low"]),
|
|
1564
|
+
title: z15.string(),
|
|
1565
|
+
keyword: z15.string(),
|
|
1566
|
+
provider: z15.string(),
|
|
1567
|
+
matchedField: z15.enum(["title", "keyword", "recommendation", "cause"]),
|
|
1568
|
+
snippet: z15.string(),
|
|
1569
|
+
dismissed: z15.boolean(),
|
|
1570
|
+
createdAt: z15.string()
|
|
1571
|
+
});
|
|
1572
|
+
var projectSearchHitSchema = z15.discriminatedUnion("kind", [
|
|
1573
|
+
projectSearchSnapshotHitSchema,
|
|
1574
|
+
projectSearchInsightHitSchema
|
|
1575
|
+
]);
|
|
1576
|
+
var projectSearchResponseSchema = z15.object({
|
|
1577
|
+
query: z15.string(),
|
|
1578
|
+
totalHits: z15.number().int().nonnegative(),
|
|
1579
|
+
truncated: z15.boolean(),
|
|
1580
|
+
hits: z15.array(projectSearchHitSchema)
|
|
1581
|
+
});
|
|
1582
|
+
|
|
1583
|
+
// ../contracts/src/content.ts
|
|
1584
|
+
import { z as z16 } from "zod";
|
|
1585
|
+
var contentActionSchema = z16.enum(["create", "expand", "refresh", "add-schema"]);
|
|
1586
|
+
var ContentActions = contentActionSchema.enum;
|
|
1587
|
+
var demandSourceSchema = z16.enum(["gsc", "competitor-evidence", "both"]);
|
|
1588
|
+
var DemandSources = demandSourceSchema.enum;
|
|
1589
|
+
var actionConfidenceSchema = z16.enum(["high", "medium", "low"]);
|
|
1590
|
+
var ActionConfidences = actionConfidenceSchema.enum;
|
|
1591
|
+
var pageTypeSchema = z16.enum([
|
|
1592
|
+
"blog-post",
|
|
1593
|
+
"comparison",
|
|
1594
|
+
"listicle",
|
|
1595
|
+
"how-to",
|
|
1596
|
+
"guide",
|
|
1597
|
+
"glossary"
|
|
1598
|
+
]);
|
|
1599
|
+
var PageTypes = pageTypeSchema.enum;
|
|
1600
|
+
var contentActionStateSchema = z16.enum([
|
|
1601
|
+
"proposed",
|
|
1602
|
+
"briefed",
|
|
1603
|
+
"payload-generated",
|
|
1604
|
+
"draft-created",
|
|
1605
|
+
"published",
|
|
1606
|
+
"validated",
|
|
1607
|
+
"dismissed"
|
|
1608
|
+
]);
|
|
1609
|
+
var ContentActionStates = contentActionStateSchema.enum;
|
|
1610
|
+
var ourBestPageSchema = z16.object({
|
|
1611
|
+
url: z16.string(),
|
|
1612
|
+
gscImpressions: z16.number().nonnegative(),
|
|
1613
|
+
gscClicks: z16.number().nonnegative(),
|
|
1614
|
+
// Null when the page came from the inventory fallback (no GSC ranking data).
|
|
1615
|
+
gscAvgPosition: z16.number().nonnegative().nullable(),
|
|
1616
|
+
organicSessions: z16.number().nonnegative()
|
|
1617
|
+
});
|
|
1618
|
+
var winningCompetitorSchema = z16.object({
|
|
1619
|
+
domain: z16.string(),
|
|
1620
|
+
url: z16.string(),
|
|
1621
|
+
title: z16.string(),
|
|
1622
|
+
citationCount: z16.number().int().nonnegative()
|
|
1623
|
+
});
|
|
1624
|
+
var scoreBreakdownSchema = z16.object({
|
|
1625
|
+
demand: z16.number(),
|
|
1626
|
+
competitor: z16.number(),
|
|
1627
|
+
absence: z16.number(),
|
|
1628
|
+
gapSeverity: z16.number()
|
|
1629
|
+
});
|
|
1630
|
+
var existingActionRefSchema = z16.object({
|
|
1631
|
+
actionId: z16.string(),
|
|
1632
|
+
state: contentActionStateSchema,
|
|
1633
|
+
lastUpdated: z16.string()
|
|
1634
|
+
});
|
|
1635
|
+
var contentTargetRowDtoSchema = z16.object({
|
|
1636
|
+
targetRef: z16.string(),
|
|
1637
|
+
query: z16.string(),
|
|
1638
|
+
action: contentActionSchema,
|
|
1639
|
+
ourBestPage: ourBestPageSchema.nullable(),
|
|
1640
|
+
winningCompetitor: winningCompetitorSchema.nullable(),
|
|
1641
|
+
score: z16.number(),
|
|
1642
|
+
scoreBreakdown: scoreBreakdownSchema,
|
|
1643
|
+
drivers: z16.array(z16.string()),
|
|
1644
|
+
demandSource: demandSourceSchema,
|
|
1645
|
+
actionConfidence: actionConfidenceSchema,
|
|
1646
|
+
existingAction: existingActionRefSchema.nullable()
|
|
1647
|
+
});
|
|
1648
|
+
var contentTargetsResponseDtoSchema = z16.object({
|
|
1649
|
+
targets: z16.array(contentTargetRowDtoSchema),
|
|
1650
|
+
contextMetrics: z16.object({
|
|
1651
|
+
totalAiReferralSessions: z16.number().int().nonnegative(),
|
|
1652
|
+
latestRunId: z16.string(),
|
|
1653
|
+
runTimestamp: z16.string()
|
|
1654
|
+
})
|
|
1655
|
+
});
|
|
1656
|
+
var contentGroundingSourceSchema = z16.object({
|
|
1657
|
+
uri: z16.string(),
|
|
1658
|
+
title: z16.string(),
|
|
1659
|
+
domain: z16.string(),
|
|
1660
|
+
isOurDomain: z16.boolean(),
|
|
1661
|
+
isCompetitor: z16.boolean(),
|
|
1662
|
+
citationCount: z16.number().int().nonnegative(),
|
|
1663
|
+
providers: z16.array(providerNameSchema)
|
|
1664
|
+
});
|
|
1665
|
+
var contentSourceRowDtoSchema = z16.object({
|
|
1666
|
+
query: z16.string(),
|
|
1667
|
+
groundingSources: z16.array(contentGroundingSourceSchema)
|
|
1668
|
+
});
|
|
1669
|
+
var contentSourcesResponseDtoSchema = z16.object({
|
|
1670
|
+
sources: z16.array(contentSourceRowDtoSchema),
|
|
1671
|
+
latestRunId: z16.string()
|
|
1672
|
+
});
|
|
1673
|
+
var contentGapRowDtoSchema = z16.object({
|
|
1674
|
+
query: z16.string(),
|
|
1675
|
+
competitorDomains: z16.array(z16.string()),
|
|
1676
|
+
competitorCount: z16.number().int().nonnegative(),
|
|
1677
|
+
missRate: z16.number().min(0).max(1),
|
|
1678
|
+
lastSeenInRunId: z16.string()
|
|
1679
|
+
});
|
|
1680
|
+
var contentGapsResponseDtoSchema = z16.object({
|
|
1681
|
+
gaps: z16.array(contentGapRowDtoSchema),
|
|
1682
|
+
latestRunId: z16.string()
|
|
1683
|
+
});
|
|
1684
|
+
|
|
1543
1685
|
// src/client.ts
|
|
1544
1686
|
function createApiClient() {
|
|
1545
1687
|
const config = loadConfig();
|
|
@@ -2105,9 +2247,43 @@ var ApiClient = class {
|
|
|
2105
2247
|
async dismissInsight(project, id) {
|
|
2106
2248
|
return this.request("POST", `/projects/${encodeURIComponent(project)}/insights/${encodeURIComponent(id)}/dismiss`);
|
|
2107
2249
|
}
|
|
2250
|
+
// ── Content ──────────────────────────────────────────────────────────
|
|
2251
|
+
async getContentTargets(project, opts) {
|
|
2252
|
+
const params = new URLSearchParams();
|
|
2253
|
+
if (opts?.limit !== void 0) params.set("limit", String(opts.limit));
|
|
2254
|
+
if (opts?.includeInProgress) params.set("include-in-progress", "true");
|
|
2255
|
+
const qs = params.toString();
|
|
2256
|
+
return this.request(
|
|
2257
|
+
"GET",
|
|
2258
|
+
`/projects/${encodeURIComponent(project)}/content/targets${qs ? `?${qs}` : ""}`
|
|
2259
|
+
);
|
|
2260
|
+
}
|
|
2261
|
+
async getContentSources(project) {
|
|
2262
|
+
return this.request(
|
|
2263
|
+
"GET",
|
|
2264
|
+
`/projects/${encodeURIComponent(project)}/content/sources`
|
|
2265
|
+
);
|
|
2266
|
+
}
|
|
2267
|
+
async getContentGaps(project) {
|
|
2268
|
+
return this.request(
|
|
2269
|
+
"GET",
|
|
2270
|
+
`/projects/${encodeURIComponent(project)}/content/gaps`
|
|
2271
|
+
);
|
|
2272
|
+
}
|
|
2108
2273
|
async getHealth(project) {
|
|
2109
2274
|
return this.request("GET", `/projects/${encodeURIComponent(project)}/health/latest`);
|
|
2110
2275
|
}
|
|
2276
|
+
async getProjectOverview(project) {
|
|
2277
|
+
return this.request("GET", `/projects/${encodeURIComponent(project)}/overview`);
|
|
2278
|
+
}
|
|
2279
|
+
async searchProject(project, opts) {
|
|
2280
|
+
const params = new URLSearchParams({ q: opts.q });
|
|
2281
|
+
if (opts.limit !== void 0) params.set("limit", String(opts.limit));
|
|
2282
|
+
return this.request(
|
|
2283
|
+
"GET",
|
|
2284
|
+
`/projects/${encodeURIComponent(project)}/search?${params.toString()}`
|
|
2285
|
+
);
|
|
2286
|
+
}
|
|
2111
2287
|
async getHealthHistory(project, limit) {
|
|
2112
2288
|
const qs = limit ? `?limit=${limit}` : "";
|
|
2113
2289
|
return this.request("GET", `/projects/${encodeURIComponent(project)}/health/history${qs}`);
|
|
@@ -2203,6 +2379,7 @@ export {
|
|
|
2203
2379
|
RunStatuses,
|
|
2204
2380
|
RunKinds,
|
|
2205
2381
|
RunTriggers,
|
|
2382
|
+
CitationStates,
|
|
2206
2383
|
runTriggerRequestSchema,
|
|
2207
2384
|
parseRunError,
|
|
2208
2385
|
buildRunErrorFromMessages,
|
|
@@ -1329,6 +1329,328 @@ function analyzeRuns(currentRun, previousRun, allRuns) {
|
|
|
1329
1329
|
};
|
|
1330
1330
|
}
|
|
1331
1331
|
|
|
1332
|
+
// ../intelligence/src/query-shape.ts
|
|
1333
|
+
var TRANSACTIONAL = /\b(buy|price|pricing|cost|cheap|discount|coupon|deal|sale|trial|plan)\b/i;
|
|
1334
|
+
var NAVIGATIONAL = /\b(login|sign[- ]?in|contact|support|help|download|app|homepage)\b|\.(com|io|net|org|app|ai)\b/i;
|
|
1335
|
+
function isBlogShapedQuery(query) {
|
|
1336
|
+
const trimmed = query.trim();
|
|
1337
|
+
if (!trimmed) return false;
|
|
1338
|
+
if (TRANSACTIONAL.test(trimmed)) return false;
|
|
1339
|
+
if (NAVIGATIONAL.test(trimmed)) return false;
|
|
1340
|
+
return true;
|
|
1341
|
+
}
|
|
1342
|
+
|
|
1343
|
+
// ../intelligence/src/site-inventory.ts
|
|
1344
|
+
var BLOG_SHAPED_PATH_PREFIXES = [
|
|
1345
|
+
"/blog/",
|
|
1346
|
+
"/posts/",
|
|
1347
|
+
"/articles/",
|
|
1348
|
+
"/guides/",
|
|
1349
|
+
"/learn/",
|
|
1350
|
+
"/resources/",
|
|
1351
|
+
"/glossary/"
|
|
1352
|
+
];
|
|
1353
|
+
function buildInventory(input) {
|
|
1354
|
+
const map = /* @__PURE__ */ new Map();
|
|
1355
|
+
const addPage = (rawUrl, source) => {
|
|
1356
|
+
const path = extractPath(rawUrl);
|
|
1357
|
+
if (!path) return;
|
|
1358
|
+
if (!isBlogShaped(path)) return;
|
|
1359
|
+
let sources = map.get(path);
|
|
1360
|
+
if (!sources) {
|
|
1361
|
+
sources = /* @__PURE__ */ new Set();
|
|
1362
|
+
map.set(path, sources);
|
|
1363
|
+
}
|
|
1364
|
+
sources.add(source);
|
|
1365
|
+
};
|
|
1366
|
+
for (const url of input.gscPages) addPage(url, "gsc");
|
|
1367
|
+
for (const url of input.ga4LandingPages) addPage(url, "ga4");
|
|
1368
|
+
for (const url of input.sitemapUrls) addPage(url, "sitemap");
|
|
1369
|
+
for (const url of input.wpPosts) addPage(url, "wp");
|
|
1370
|
+
return Array.from(map.entries()).map(([url, sources]) => ({
|
|
1371
|
+
url,
|
|
1372
|
+
sources: Array.from(sources)
|
|
1373
|
+
}));
|
|
1374
|
+
}
|
|
1375
|
+
function extractPath(url) {
|
|
1376
|
+
const trimmed = url.trim();
|
|
1377
|
+
if (!trimmed) return "";
|
|
1378
|
+
const match = /^https?:\/\/[^/]+(.*)$/.exec(trimmed);
|
|
1379
|
+
const path = match ? match[1] : trimmed;
|
|
1380
|
+
const stripped = path.replace(/\/+$/, "");
|
|
1381
|
+
return stripped || "/";
|
|
1382
|
+
}
|
|
1383
|
+
function isBlogShaped(path) {
|
|
1384
|
+
return BLOG_SHAPED_PATH_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
// ../intelligence/src/content-classifier.ts
|
|
1388
|
+
var SEO_STRONG_THRESHOLD = 10;
|
|
1389
|
+
var SEO_WEAK_THRESHOLD = 30;
|
|
1390
|
+
function classifyContentAction(input) {
|
|
1391
|
+
const { ourPage, ourPageInGroundingSources, ourPageHasSchema } = input;
|
|
1392
|
+
if (!ourPage) return "create";
|
|
1393
|
+
if (ourPageInGroundingSources) {
|
|
1394
|
+
if (ourPageHasSchema === false) return "add-schema";
|
|
1395
|
+
return null;
|
|
1396
|
+
}
|
|
1397
|
+
if (ourPage.position <= SEO_STRONG_THRESHOLD) return "refresh";
|
|
1398
|
+
if (ourPage.position <= SEO_WEAK_THRESHOLD) return "expand";
|
|
1399
|
+
return "create";
|
|
1400
|
+
}
|
|
1401
|
+
|
|
1402
|
+
// ../intelligence/src/content-scorer.ts
|
|
1403
|
+
var SEVERITY_BY_ACTION = {
|
|
1404
|
+
create: 1,
|
|
1405
|
+
"add-schema": 0.7,
|
|
1406
|
+
expand: 0.6,
|
|
1407
|
+
refresh: 0.4
|
|
1408
|
+
};
|
|
1409
|
+
function scoreContentTarget(input) {
|
|
1410
|
+
const demand = computeDemandComponent(input.gscImpressions, input.aiReferralFactor);
|
|
1411
|
+
const competitor = computeCompetitorComponent(
|
|
1412
|
+
input.competitorCount,
|
|
1413
|
+
input.recentMissRate,
|
|
1414
|
+
input.citationCount
|
|
1415
|
+
);
|
|
1416
|
+
const absence = clamp01(1 - input.ourCitedRate);
|
|
1417
|
+
const gapSeverity = input.action ? SEVERITY_BY_ACTION[input.action] : 0;
|
|
1418
|
+
const score = (demand + competitor) * absence * gapSeverity;
|
|
1419
|
+
return {
|
|
1420
|
+
score,
|
|
1421
|
+
scoreBreakdown: { demand, competitor, absence, gapSeverity },
|
|
1422
|
+
drivers: buildDrivers(input),
|
|
1423
|
+
demandSource: classifyDemandSource(input.gscImpressions, input.competitorCount)
|
|
1424
|
+
};
|
|
1425
|
+
}
|
|
1426
|
+
function computeDemandComponent(gscImpressions, aiReferralFactor) {
|
|
1427
|
+
const logImpressions = Math.log(Math.max(gscImpressions, 0) + 1);
|
|
1428
|
+
const aiBoost = 1 + Math.max(aiReferralFactor, 0);
|
|
1429
|
+
return logImpressions * aiBoost;
|
|
1430
|
+
}
|
|
1431
|
+
function computeCompetitorComponent(competitorCount, recentMissRate, citationCount) {
|
|
1432
|
+
if (competitorCount <= 0) return 0;
|
|
1433
|
+
const logCompetitors = Math.log(competitorCount + 1);
|
|
1434
|
+
return logCompetitors * clamp01(recentMissRate) * Math.max(citationCount, 0);
|
|
1435
|
+
}
|
|
1436
|
+
function classifyDemandSource(gscImpressions, competitorCount) {
|
|
1437
|
+
const hasGsc = gscImpressions > 0;
|
|
1438
|
+
const hasCompetitor = competitorCount > 0;
|
|
1439
|
+
if (hasGsc && hasCompetitor) return "both";
|
|
1440
|
+
if (hasCompetitor) return "competitor-evidence";
|
|
1441
|
+
return "gsc";
|
|
1442
|
+
}
|
|
1443
|
+
function clamp01(value) {
|
|
1444
|
+
if (value < 0) return 0;
|
|
1445
|
+
if (value > 1) return 1;
|
|
1446
|
+
return value;
|
|
1447
|
+
}
|
|
1448
|
+
function buildDrivers(input) {
|
|
1449
|
+
const drivers = [];
|
|
1450
|
+
if (input.competitorCount > 0) {
|
|
1451
|
+
const noun = input.competitorCount === 1 ? "competitor" : "competitors";
|
|
1452
|
+
drivers.push(`${input.competitorCount} ${noun} cited`);
|
|
1453
|
+
}
|
|
1454
|
+
if (input.gscImpressions > 0) {
|
|
1455
|
+
drivers.push(`${formatImpressions(input.gscImpressions)} GSC impressions`);
|
|
1456
|
+
}
|
|
1457
|
+
if (input.recentMissRate >= 0.5 && input.competitorCount > 0) {
|
|
1458
|
+
const pct = Math.round(clamp01(input.recentMissRate) * 100);
|
|
1459
|
+
drivers.push(`missed in ${pct}% of recent runs`);
|
|
1460
|
+
}
|
|
1461
|
+
if (input.action === "create" && input.position === null) {
|
|
1462
|
+
drivers.push("no existing page");
|
|
1463
|
+
}
|
|
1464
|
+
if (input.position !== null && input.position > 30) {
|
|
1465
|
+
drivers.push(`page ranks #${input.position} (effectively invisible)`);
|
|
1466
|
+
} else if (input.position !== null && input.position > 10) {
|
|
1467
|
+
drivers.push(`page ranks #${input.position}`);
|
|
1468
|
+
}
|
|
1469
|
+
if (input.action === "add-schema") {
|
|
1470
|
+
drivers.push("cited by LLMs but lacks structured data");
|
|
1471
|
+
}
|
|
1472
|
+
return drivers;
|
|
1473
|
+
}
|
|
1474
|
+
function formatImpressions(impressions) {
|
|
1475
|
+
if (impressions >= 1e3) {
|
|
1476
|
+
return `${Math.round(impressions / 100) / 10}k`;
|
|
1477
|
+
}
|
|
1478
|
+
return String(impressions);
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
// ../intelligence/src/content-confidence.ts
|
|
1482
|
+
var GSC_DENSE_IMPRESSIONS_THRESHOLD = 100;
|
|
1483
|
+
var RUN_HISTORY_HIGH_CONFIDENCE_THRESHOLD = 3;
|
|
1484
|
+
function calculateActionConfidence(input) {
|
|
1485
|
+
const gscDense = input.hasGsc && input.gscImpressions >= GSC_DENSE_IMPRESSIONS_THRESHOLD;
|
|
1486
|
+
const historyDeep = input.runsOfHistory >= RUN_HISTORY_HIGH_CONFIDENCE_THRESHOLD;
|
|
1487
|
+
if (gscDense && historyDeep) return "high";
|
|
1488
|
+
if (!input.hasGsc && !input.hasInventoryMatch) {
|
|
1489
|
+
return "low";
|
|
1490
|
+
}
|
|
1491
|
+
return "medium";
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
// ../intelligence/src/content-targets.ts
|
|
1495
|
+
function buildContentTargetRows(input) {
|
|
1496
|
+
const rows = [];
|
|
1497
|
+
for (const cq of input.candidateQueries) {
|
|
1498
|
+
const ourPage = resolveOurPage(cq, input.inventory);
|
|
1499
|
+
const ourPageInGroundingSources = cq.ourCitedInLatestRun;
|
|
1500
|
+
const ourPageHasSchema = ourPage ? input.wpSchemaAudit.get(ourPage.url) ?? null : null;
|
|
1501
|
+
const action = classifyContentAction({
|
|
1502
|
+
ourPage,
|
|
1503
|
+
ourPageInGroundingSources,
|
|
1504
|
+
ourPageHasSchema
|
|
1505
|
+
});
|
|
1506
|
+
if (!action) continue;
|
|
1507
|
+
const hasGsc = cq.gscImpressions > 0;
|
|
1508
|
+
const hasCompetitor = cq.competitorDomains.length > 0;
|
|
1509
|
+
if (!hasGsc && !hasCompetitor && !cq.ourCitedInLatestRun) continue;
|
|
1510
|
+
const aiReferralFactor = computeAiReferralFactor(
|
|
1511
|
+
input.totalAiReferralSessions,
|
|
1512
|
+
cq.competitorCitationCount
|
|
1513
|
+
);
|
|
1514
|
+
const scoring = scoreContentTarget({
|
|
1515
|
+
gscImpressions: cq.gscImpressions,
|
|
1516
|
+
aiReferralFactor,
|
|
1517
|
+
competitorCount: cq.competitorDomains.length,
|
|
1518
|
+
recentMissRate: cq.recentMissRate,
|
|
1519
|
+
citationCount: cq.competitorCitationCount,
|
|
1520
|
+
ourCitedRate: cq.ourCitedRate,
|
|
1521
|
+
action,
|
|
1522
|
+
position: ourPage?.position ?? null
|
|
1523
|
+
});
|
|
1524
|
+
const actionConfidence = calculateActionConfidence({
|
|
1525
|
+
hasGsc: cq.gscPage !== null,
|
|
1526
|
+
gscImpressions: cq.gscImpressions,
|
|
1527
|
+
runsOfHistory: cq.runsOfHistory,
|
|
1528
|
+
hasCompetitorEvidence: cq.competitorDomains.length > 0,
|
|
1529
|
+
hasInventoryMatch: ourPage?.source === "inventory"
|
|
1530
|
+
});
|
|
1531
|
+
const targetRef = computeTargetRef({
|
|
1532
|
+
projectId: input.projectId,
|
|
1533
|
+
query: cq.query,
|
|
1534
|
+
action,
|
|
1535
|
+
targetPage: ourPage?.url ?? null
|
|
1536
|
+
});
|
|
1537
|
+
const winningCompetitor = pickTopCompetitor(cq.competitorGroundingUrls);
|
|
1538
|
+
const ourBestPage = ourPage ? {
|
|
1539
|
+
url: ourPage.url,
|
|
1540
|
+
gscImpressions: cq.gscImpressions,
|
|
1541
|
+
gscClicks: cq.gscClicks,
|
|
1542
|
+
gscAvgPosition: cq.gscPosition,
|
|
1543
|
+
organicSessions: input.gaTrafficByPage.get(ourPage.url) ?? 0
|
|
1544
|
+
} : null;
|
|
1545
|
+
rows.push({
|
|
1546
|
+
targetRef,
|
|
1547
|
+
query: cq.query,
|
|
1548
|
+
action,
|
|
1549
|
+
ourBestPage,
|
|
1550
|
+
winningCompetitor,
|
|
1551
|
+
score: scoring.score,
|
|
1552
|
+
scoreBreakdown: scoring.scoreBreakdown,
|
|
1553
|
+
drivers: scoring.drivers,
|
|
1554
|
+
demandSource: scoring.demandSource,
|
|
1555
|
+
actionConfidence,
|
|
1556
|
+
existingAction: input.inProgressActions.get(targetRef) ?? null
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
1559
|
+
return rows.sort((a, b) => b.score - a.score);
|
|
1560
|
+
}
|
|
1561
|
+
function buildContentSourceRows(input) {
|
|
1562
|
+
return input.candidateQueries.map((cq) => ({
|
|
1563
|
+
query: cq.query,
|
|
1564
|
+
groundingSources: [
|
|
1565
|
+
...cq.ourGroundingUrls.map((g) => ({
|
|
1566
|
+
uri: g.uri,
|
|
1567
|
+
title: g.title,
|
|
1568
|
+
domain: g.domain,
|
|
1569
|
+
isOurDomain: true,
|
|
1570
|
+
isCompetitor: false,
|
|
1571
|
+
citationCount: g.citationCount,
|
|
1572
|
+
providers: g.providers
|
|
1573
|
+
})),
|
|
1574
|
+
...cq.competitorGroundingUrls.map((g) => ({
|
|
1575
|
+
uri: g.uri,
|
|
1576
|
+
title: g.title,
|
|
1577
|
+
domain: g.domain,
|
|
1578
|
+
isOurDomain: false,
|
|
1579
|
+
isCompetitor: true,
|
|
1580
|
+
citationCount: g.citationCount,
|
|
1581
|
+
providers: g.providers
|
|
1582
|
+
}))
|
|
1583
|
+
]
|
|
1584
|
+
}));
|
|
1585
|
+
}
|
|
1586
|
+
function buildContentGapRows(input) {
|
|
1587
|
+
const gaps = [];
|
|
1588
|
+
for (const cq of input.candidateQueries) {
|
|
1589
|
+
if (cq.competitorDomains.length === 0) continue;
|
|
1590
|
+
if (cq.ourCitedRate >= 1) continue;
|
|
1591
|
+
gaps.push({
|
|
1592
|
+
query: cq.query,
|
|
1593
|
+
competitorDomains: cq.competitorDomains,
|
|
1594
|
+
competitorCount: cq.competitorDomains.length,
|
|
1595
|
+
missRate: clamp012(cq.recentMissRate),
|
|
1596
|
+
lastSeenInRunId: input.latestRunId
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
1599
|
+
return gaps.sort((a, b) => {
|
|
1600
|
+
if (b.missRate !== a.missRate) return b.missRate - a.missRate;
|
|
1601
|
+
return b.competitorCount - a.competitorCount;
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
function resolveOurPage(cq, inventory) {
|
|
1605
|
+
if (cq.gscPage && cq.gscPosition !== null) {
|
|
1606
|
+
return { url: cq.gscPage, position: cq.gscPosition, source: "gsc" };
|
|
1607
|
+
}
|
|
1608
|
+
for (const page of inventory) {
|
|
1609
|
+
if (slugMatchesQuery(page.url, cq.query)) {
|
|
1610
|
+
return { url: page.url, position: 100, source: "inventory" };
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
return null;
|
|
1614
|
+
}
|
|
1615
|
+
function slugMatchesQuery(url, query) {
|
|
1616
|
+
const slug = url.toLowerCase();
|
|
1617
|
+
const queryAsSlug = query.toLowerCase().trim().replace(/\s+/g, "-");
|
|
1618
|
+
if (slug.includes(queryAsSlug)) return true;
|
|
1619
|
+
const queryTokens = query.toLowerCase().split(/\s+/).filter((t) => t.length > 2);
|
|
1620
|
+
const slugTokens = new Set(slug.split(/[/\s\-_.]+/));
|
|
1621
|
+
const overlap = queryTokens.filter((t) => slugTokens.has(t)).length;
|
|
1622
|
+
return overlap >= 2;
|
|
1623
|
+
}
|
|
1624
|
+
function computeAiReferralFactor(totalAiReferralSessions, competitorCount) {
|
|
1625
|
+
if (totalAiReferralSessions <= 0) return 0;
|
|
1626
|
+
const baseline = Math.min(totalAiReferralSessions / 1e3, 0.5);
|
|
1627
|
+
const competitorBoost = competitorCount > 0 ? 0.1 : 0;
|
|
1628
|
+
return Math.min(baseline + competitorBoost, 1);
|
|
1629
|
+
}
|
|
1630
|
+
function pickTopCompetitor(competitors2) {
|
|
1631
|
+
if (competitors2.length === 0) return null;
|
|
1632
|
+
const top = [...competitors2].sort((a, b) => b.citationCount - a.citationCount)[0];
|
|
1633
|
+
return {
|
|
1634
|
+
domain: top.domain,
|
|
1635
|
+
url: top.uri,
|
|
1636
|
+
title: top.title,
|
|
1637
|
+
citationCount: top.citationCount
|
|
1638
|
+
};
|
|
1639
|
+
}
|
|
1640
|
+
function computeTargetRef(input) {
|
|
1641
|
+
const key = [input.projectId, input.query, input.action, input.targetPage ?? ""].join("|");
|
|
1642
|
+
let hash = 0;
|
|
1643
|
+
for (let i = 0; i < key.length; i++) {
|
|
1644
|
+
hash = (hash << 5) - hash + key.charCodeAt(i) | 0;
|
|
1645
|
+
}
|
|
1646
|
+
return `tgt_${(hash >>> 0).toString(36)}`;
|
|
1647
|
+
}
|
|
1648
|
+
function clamp012(value) {
|
|
1649
|
+
if (value < 0) return 0;
|
|
1650
|
+
if (value > 1) return 1;
|
|
1651
|
+
return value;
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1332
1654
|
// src/intelligence-service.ts
|
|
1333
1655
|
import crypto from "crypto";
|
|
1334
1656
|
|
|
@@ -1589,6 +1911,11 @@ export {
|
|
|
1589
1911
|
extractLegacyCredentials,
|
|
1590
1912
|
dropLegacyCredentialColumns,
|
|
1591
1913
|
migrate,
|
|
1914
|
+
isBlogShapedQuery,
|
|
1915
|
+
buildInventory,
|
|
1916
|
+
buildContentTargetRows,
|
|
1917
|
+
buildContentSourceRows,
|
|
1918
|
+
buildContentGapRows,
|
|
1592
1919
|
createLogger,
|
|
1593
1920
|
IntelligenceService
|
|
1594
1921
|
};
|