Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
refactor(priority): read the web's priority levels from the shared core's twin (#1768)
Cihan686c1140244e
diff --git a/server/src/lib/priority-core.fixtures.json b/server/src/lib/priority-core.fixtures.jsonindex c5bcd3835..67b0447ad 100644--- a/server/src/lib/priority-core.fixtures.json+++ b/server/src/lib/priority-core.fixtures.json@@ -65,6 +65,16 @@"value": 45000,"fullAt": 1000000,"result": 0.7755369713075567+ },+ {+ "value": null,+ "fullAt": 10000,+ "result": 0+ },+ {+ "value": "not a number",+ "fullAt": 10000,+ "result": 0}],"round": [diff --git a/server/src/lib/priority-core.js b/server/src/lib/priority-core.jsindex fd264566e..42b6b0ded 100644--- a/server/src/lib/priority-core.js+++ b/server/src/lib/priority-core.js@@ -30,10 +30,13 @@ export function round(n, digits = 2) {/*** A count on a log scale: 0 → 0, `fullAt` and above → 1. Demand, sessions* and conversions all grow by orders of magnitude, and on a straight scale- * the few largest would flatten the rest to 0.+ * the few largest would flatten the rest to 0. What isn't a number counts as+ * nothing: a score never turns NaN.*/export function logScale(value, fullAt) {- return clamp(Math.log10(1 + Math.max(0, value)) / Math.log10(1 + fullAt));+ const count = Number(value);+ if (Number.isNaN(count)) return 0;+ return clamp(Math.log10(1 + Math.max(0, count)) / Math.log10(1 + fullAt));}/** How commercial a search is, by its intent: comparing and choosing above learning. */diff --git a/server/src/lib/priority-core.test.js b/server/src/lib/priority-core.test.jsindex f3e26cad3..fefc96d89 100644--- a/server/src/lib/priority-core.test.js+++ b/server/src/lib/priority-core.test.js@@ -47,8 +47,12 @@ describe('logScale', () => {expect(logScale(10000, 10000)).toBe(1);expect(logScale(1_000_000, 10000)).toBe(1);expect(logScale(100, 10000)).toBeCloseTo(0.5, 2);- // A negative count is no count.+ // A negative count is no count; so is what isn't a number, and a count+ // past any end is full.expect(logScale(-3, 10000)).toBe(0);+ expect(logScale(Number.NaN, 10000)).toBe(0);+ expect(logScale(undefined, 10000)).toBe(0);+ expect(logScale(Number.POSITIVE_INFINITY, 10000)).toBe(1);});});diff --git a/web/src/lib/action-center/priority.ts b/web/src/lib/action-center/priority.tsindex 17e160144..c9d5dc37e 100644--- a/web/src/lib/action-center/priority.ts+++ b/web/src/lib/action-center/priority.ts@@ -1,5 +1,6 @@import type { ActionItem } from '@/lib/actions/action-center';import { OPEN_ACTION_STATUSES } from '@/lib/action-center/registry';+import { levelOf } from '@/lib/priority-core';/*** An action's priority as the app shows it (AC-006): the 0–100 score and its@@ -49,12 +50,12 @@ export function isPriorityLevel(value: unknown): value is PriorityLevel {return (PRIORITY_LEVELS as readonly unknown[]).includes(value);}-/** A score's level, as the server assigns it: critical 90+, high 75+, medium 50+. */+/** The server's level cut-offs (config/action-engine.js `priority`): critical 90+, high 75+, medium 50+. */+const LEVEL_CUTOFFS = { critical: 90, high: 75, medium: 50 };++/** A score's level, as the server assigns it. */export function priorityLevelOf(score: number): PriorityLevel {- if (score >= 90) return 'critical';- if (score >= 75) return 'high';- if (score >= 50) return 'medium';- return 'low';+ return levelOf(score, LEVEL_CUTOFFS);}/** The stored level, or the score's when the stored one is missing or unknown. */diff --git a/web/src/lib/content/plan.ts b/web/src/lib/content/plan.tsindex 6de70e45e..af2589662 100644--- a/web/src/lib/content/plan.ts+++ b/web/src/lib/content/plan.ts@@ -16,6 +16,7 @@ import { detectPageType, type SiteType } from './page-types';import type { DraftMethod, PageType } from './drafts';import { pageKey } from './sources';import type { PriorityLevel } from '@/lib/action-center/priority';+import { levelOf, logScale, round } from '@/lib/priority-core';/** The Plan's periods: a week, a month, a quarter. */export const PLAN_PERIODS = [7, 30, 90] as const;@@ -152,10 +153,7 @@ export const SCORE_WEIGHTS = { severity: 40, opportunity: 30, pageValue: 20, citexport const PLAN_LEVEL_CUTOFFS = { critical: 75, high: 55, medium: 40 } as const;export function planPriorityLevel(score: number): PriorityLevel {- if (score >= PLAN_LEVEL_CUTOFFS.critical) return 'critical';- if (score >= PLAN_LEVEL_CUTOFFS.high) return 'high';- if (score >= PLAN_LEVEL_CUTOFFS.medium) return 'medium';- return 'low';+ return levelOf(score, PLAN_LEVEL_CUTOFFS);}/** How bad the signal is, by what the row is (a drop is reckoned, 0.6–1). */@@ -185,11 +183,11 @@ export function dropSeverity(pct: number, lost: number): number {}/** A page's AI visits as value: log10(1 + visits) against FULL_VISITS'. */export function visitsValue(visits: number): number {- return Math.min(1, Math.log10(1 + Math.max(0, visits)) / Math.log10(1 + FULL_VISITS));+ return logScale(visits, FULL_VISITS);}/** A page's conversions as value: log10(1 + conversions) against FULL_CONVERSIONS'. */export function conversionsValue(conversions: number): number {- return Math.min(1, Math.log10(1 + Math.max(0, conversions)) / Math.log10(1 + FULL_CONVERSIONS));+ return logScale(conversions, FULL_CONVERSIONS);}/*** A page's value: its AI visits, and with conversions weighed (a count, not@@ -232,7 +230,7 @@ type PartFields = PlanScorePart extends infer P: never: never;/** To one decimal: whole points would tie pages a citation apart. */-const round1 = (n: number) => Math.round(n * 10) / 10;+const round1 = (n: number) => round(n, 1);function part(fields: PartFields): PlanScorePart {const max = SCORE_WEIGHTS[fields.kind];return { ...fields, max, points: round1(fields.value * max) } as PlanScorePart;diff --git a/web/src/lib/content/shopping-plan.ts b/web/src/lib/content/shopping-plan.tsindex 29ec1c6f9..8e58171c6 100644--- a/web/src/lib/content/shopping-plan.ts+++ b/web/src/lib/content/shopping-plan.ts@@ -3,6 +3,7 @@ import type { DraftStatus } from './drafts';import { SCORE_WEIGHTS } from './plan';import type { DraftShopping } from './shopping-check';import { wordsOf } from './sources';+import { round } from '@/lib/priority-core';/*** Plan › Shopping opportunities (shopping path, part 7; the mockup's@@ -97,7 +98,7 @@ function names(textWords: Set<string>, leafWords: string[]): boolean {return leafWords.length > 0 && leafWords.every((w) => textWords.has(w));}-const round1 = (n: number) => Math.round(n * 10) / 10;+const round1 = (n: number) => round(n, 1);/** A new guide's score: a new page's, its opportunity the prompts asking and the products in stock. */export function guideScore(prompts: number, inStock: number): number {diff --git a/web/src/lib/priority-core.test.ts b/web/src/lib/priority-core.test.tsnew file mode 100644index 000000000..8f26cb368--- /dev/null+++ b/web/src/lib/priority-core.test.ts@@ -0,0 +1,38 @@+import { readFileSync } from 'node:fs';+import { join } from 'node:path';+import { describe, expect, it } from 'vitest';+import { levelOf, logScale, round } from './priority-core';++/**+ * The web twin of the shared priority core (learning D4) gives what the+ * server's gives, case for case: the fixtures are the server's.+ */++interface Fixtures {+ logScale: { value: number; fullAt: number; result: number }[];+ round: { n: number; digits: number; result: number }[];+ levelOf: {+ score: number;+ cutoffs: { critical: number; high: number; medium: number };+ result: string;+ }[];+}++const fixtures: Fixtures = JSON.parse(+ readFileSync(join(__dirname, '../../../server/src/lib/priority-core.fixtures.json'), 'utf8'),+);++describe("the server's cases", () => {+ it('logScale', () => {+ expect(fixtures.logScale.length).toBeGreaterThan(10);+ for (const c of fixtures.logScale) expect(logScale(c.value, c.fullAt)).toBe(c.result);+ });++ it('round', () => {+ for (const c of fixtures.round) expect(round(c.n, c.digits)).toBe(c.result);+ });++ it('levelOf', () => {+ for (const c of fixtures.levelOf) expect(levelOf(c.score, c.cutoffs)).toBe(c.result);+ });+});diff --git a/web/src/lib/priority-core.ts b/web/src/lib/priority-core.tsnew file mode 100644index 000000000..7342941db--- /dev/null+++ b/web/src/lib/priority-core.ts@@ -0,0 +1,43 @@+import type { PriorityLevel } from '@/lib/action-center/priority';++/**+ * What every priority score is built from (learning D4): the web app's twin+ * of server/src/lib/priority-core.js, for the score worked out here (the+ * Content Plan's, plan.ts) and the levels shown for the Action Center's+ * (action-center/priority.ts). Only the pieces the web app uses are here;+ * the cases in server/src/lib/priority-core.fixtures.json are checked+ * against both files, so the two can't drift apart.+ */++const clamp = (n: number, min = 0, max = 1) => Math.min(max, Math.max(min, n));++/** `n` to `digits` decimals. */+export function round(n: number, digits = 2): number {+ const f = 10 ** digits;+ return Math.round(n * f) / f;+}++/**+ * A count on a log scale: 0 → 0, `fullAt` and above → 1. What isn't a+ * number counts as nothing: a score never turns NaN.+ */+export function logScale(value: number, fullAt: number): number {+ const count = Number(value);+ if (Number.isNaN(count)) return 0;+ return clamp(Math.log10(1 + Math.max(0, count)) / Math.log10(1 + fullAt));+}++/** A screen's level cut-offs: the Action Center's and the Content Plan's differ. */+export interface LevelCutoffs {+ critical: number;+ high: number;+ medium: number;+}++/** The level a score falls in, at a screen's own cut-offs. */+export function levelOf(score: number, { critical, high, medium }: LevelCutoffs): PriorityLevel {+ if (score >= critical) return 'critical';+ if (score >= high) return 'high';+ if (score >= medium) return 'medium';+ return 'low';+}