literally-aeo 0.4.1 → 0.4.3
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 +16 -0
- package/dist/cli.mjs +1505 -549
- package/dist/lib/agents/copy-validation.js +3 -1
- package/dist/lib/agents/methodology.js +3 -3
- package/dist/lib/agents/verifier.js +2 -2
- package/dist/lib/bundle/contracts.js +1 -1
- package/dist/lib/bundle/export.js +13 -13
- package/dist/lib/bundle/journal.js +2 -2
- package/dist/lib/bundle/project-journal.js +5 -5
- package/dist/lib/bundle/project.js +11 -11
- package/dist/lib/bundle/read.js +14 -14
- package/dist/lib/bundle/recorder.js +3 -3
- package/dist/lib/bundle/redact.js +2 -2
- package/dist/lib/bundle/trace-coverage.js +2 -2
- package/dist/lib/bundle/verify-integrity.js +4 -4
- package/dist/lib/bundle/verify-measurement.js +2 -2
- package/dist/lib/bundle/verify-report.js +3 -3
- package/dist/lib/bundle/verify.js +8 -8
- package/dist/lib/checks/run-checks.js +4 -4
- package/dist/lib/chunks/{chunk-4KF5EVLI.js → chunk-2ANSPIUT.js} +2 -2
- package/dist/lib/chunks/{chunk-G22MXBBM.js → chunk-54GMIPWV.js} +1 -1
- package/dist/lib/chunks/{chunk-PI7PZKHP.js → chunk-65OIYOUH.js} +2 -2
- package/dist/lib/chunks/{chunk-2XMVEGQG.js → chunk-6MGXILUJ.js} +1 -1
- package/dist/lib/chunks/chunk-6OURIXQZ.js +472 -0
- package/dist/lib/chunks/{chunk-OBSW2WKF.js → chunk-746643QV.js} +1 -1
- package/dist/lib/chunks/{chunk-7DR2X7SF.js → chunk-74K4E3ZS.js} +1 -1
- package/dist/lib/chunks/{chunk-JVIOWZPG.js → chunk-7HCVSLVG.js} +1 -1
- package/dist/lib/chunks/{chunk-XX2TK6FI.js → chunk-7K2XT3WI.js} +5 -5
- package/dist/lib/chunks/{chunk-ABS5TK73.js → chunk-7K6CXSTL.js} +3 -1
- package/dist/lib/chunks/{chunk-CBOETT4C.js → chunk-7L4Y6N3V.js} +1 -1
- package/dist/lib/chunks/{chunk-FDCM7554.js → chunk-7QWT7LFM.js} +2 -2
- package/dist/lib/chunks/{chunk-4UN3YD5Z.js → chunk-7SA2CRSS.js} +1 -1
- package/dist/lib/chunks/{chunk-P7LGJGDL.js → chunk-ARKZ5PJN.js} +1 -1
- package/dist/lib/chunks/{chunk-4WPG2O2S.js → chunk-B6XGNJUU.js} +1 -1
- package/dist/lib/chunks/{chunk-5OL67GMN.js → chunk-BNX7S4GK.js} +4 -4
- package/dist/lib/chunks/chunk-BWBPEXEU.js +83 -0
- package/dist/lib/chunks/{chunk-U53GT2R7.js → chunk-BZAPAGXM.js} +1 -1
- package/dist/lib/chunks/{chunk-BNPWAQQG.js → chunk-C2Q7YE7Y.js} +6 -6
- package/dist/lib/chunks/{chunk-LYOCWN3R.js → chunk-D2LJMNH3.js} +2 -2
- package/dist/lib/chunks/{chunk-32HYNVPR.js → chunk-EVEJ3MFU.js} +3 -3
- package/dist/lib/chunks/{chunk-DGOFCRZM.js → chunk-GC7DISQO.js} +1 -1
- package/dist/lib/chunks/{chunk-BTRBLG6S.js → chunk-I6PQUKKF.js} +1 -1
- package/dist/lib/chunks/{chunk-6FGVCN4H.js → chunk-J5RYDPGP.js} +1 -1
- package/dist/lib/chunks/{chunk-JIUBLXR7.js → chunk-JHUI2K5X.js} +3 -3
- package/dist/lib/chunks/chunk-JQFLWUCA.js +123 -0
- package/dist/lib/chunks/{chunk-4RB3AOPT.js → chunk-JREYIUNG.js} +5 -5
- package/dist/lib/chunks/{chunk-42QARLUQ.js → chunk-JXQOWHWB.js} +3 -3
- package/dist/lib/chunks/{chunk-66BTHIL3.js → chunk-KFJ5O5CL.js} +2 -2
- package/dist/lib/chunks/{chunk-6P7EZJI7.js → chunk-KSJ3NZIJ.js} +4 -2
- package/dist/lib/chunks/{chunk-3RGUIUKU.js → chunk-L5HFTFFG.js} +2 -2
- package/dist/lib/chunks/{chunk-WBSJVDS2.js → chunk-LJD37WAV.js} +2 -2
- package/dist/lib/chunks/{chunk-QT5GTQGD.js → chunk-MKA2EP7M.js} +1 -1
- package/dist/lib/chunks/{chunk-CZMQ2DVJ.js → chunk-MMJ5UR2E.js} +1 -1
- package/dist/lib/chunks/{chunk-BWUOUP4J.js → chunk-MQFFMD4Q.js} +1 -1
- package/dist/lib/chunks/{chunk-7D7EOET3.js → chunk-NM2LUMQT.js} +4 -4
- package/dist/lib/chunks/{chunk-IJQEQL5Q.js → chunk-P5VVHTZU.js} +1 -1
- package/dist/lib/chunks/{chunk-OJQ66533.js → chunk-PFG3ESJE.js} +9 -9
- package/dist/lib/chunks/{chunk-WSATQQJI.js → chunk-Q7CWM6LZ.js} +6 -6
- package/dist/lib/chunks/{chunk-F6CC4WLO.js → chunk-R4HYBSQB.js} +1 -1
- package/dist/lib/chunks/{chunk-FWH3LYHG.js → chunk-R5NLULQW.js} +1 -1
- package/dist/lib/chunks/{chunk-WUDXMAJS.js → chunk-RM7U5M5L.js} +1 -1
- package/dist/lib/chunks/{chunk-3G6COT6E.js → chunk-RQ6GUJI5.js} +1 -1
- package/dist/lib/chunks/{chunk-TNTTIT2R.js → chunk-RUSMX6JH.js} +16 -7
- package/dist/lib/chunks/{chunk-G4GAGKSG.js → chunk-UNNRLD2U.js} +4 -4
- package/dist/lib/chunks/chunk-VFN2J4Z3.js +626 -0
- package/dist/lib/chunks/{chunk-BLDOQAEW.js → chunk-VPJN2GNC.js} +1 -1
- package/dist/lib/chunks/{chunk-3GDCPD5P.js → chunk-XSOTCSD3.js} +3 -3
- package/dist/lib/chunks/{chunk-WHW3NOG5.js → chunk-YX4EH6FR.js} +1 -1
- package/dist/lib/chunks/{chunk-TXGVQ6CN.js → chunk-ZC4MKS3J.js} +12 -12
- package/dist/lib/chunks/{chunk-VXO5RAR4.js → chunk-ZM4RHMBI.js} +1 -1
- package/dist/lib/cli/bundle-commands.js +60 -35
- package/dist/lib/cli/guide-command.js +1 -1
- package/dist/lib/cli/run-commands.js +40 -40
- package/dist/lib/cli/status-command.js +21 -21
- package/dist/lib/codex-workflow/aggregate.js +2 -2
- package/dist/lib/codex-workflow/author.js +5 -5
- package/dist/lib/codex-workflow/capture.js +5 -5
- package/dist/lib/codex-workflow/copy-editor.js +5 -5
- package/dist/lib/codex-workflow/copy.js +4 -4
- package/dist/lib/codex-workflow/finish.js +5 -5
- package/dist/lib/codex-workflow/loop.js +15 -15
- package/dist/lib/codex-workflow/run-source.js +5 -5
- package/dist/lib/codex-workflow/stage.js +6 -6
- package/dist/lib/engine/audit-file.js +3 -3
- package/dist/lib/engine/author.js +12 -12
- package/dist/lib/engine/cost-estimate.js +3 -3
- package/dist/lib/engine/local-run.js +20 -20
- package/dist/lib/engine/preflight.js +3 -3
- package/dist/lib/engine/rejudge.js +27 -27
- package/dist/lib/engine/resources.js +1 -1
- package/dist/lib/engine/run-bindings.js +3 -3
- package/dist/lib/engine/run-method.js +3 -3
- package/dist/lib/engine/run-models.js +4 -4
- package/dist/lib/engine/run-status.js +21 -21
- package/dist/lib/engine/templates.js +4 -4
- package/dist/lib/grounding/grounding-runtime.js +5 -5
- package/dist/lib/grounding/recorded-tools.js +4 -4
- package/dist/lib/pipeline.js +4 -4
- package/dist/lib/probes/bindings.js +2 -2
- package/dist/lib/probes/charged-trace.js +3 -3
- package/dist/lib/probes/probes.js +3 -3
- package/dist/lib/stores/files.js +3 -3
- package/dist/lib/stores/memory.js +2 -2
- package/dist/lib/stores/publication.js +2 -2
- package/dist/lib/viewer/alternatives.js +8 -0
- package/dist/lib/viewer/cited-domains.js +8 -0
- package/dist/lib/viewer/page.js +3 -1
- package/dist/lib/viewer/presentation.js +41 -0
- package/dist/lib/viewer/server.js +15 -10
- package/dist/types/agents/copy-validation.d.ts +2 -1
- package/dist/types/bundle/contracts.d.ts +1 -1
- package/dist/types/engine/resources.d.ts +2 -0
- package/dist/types/viewer/alternatives.d.ts +16 -0
- package/dist/types/viewer/cited-domains.d.ts +22 -0
- package/dist/types/viewer/page.d.ts +9 -2
- package/dist/types/viewer/presentation.d.ts +112 -0
- package/dist/types/viewer/server.d.ts +7 -3
- package/package.json +2 -1
- package/resources/fonts/Mulish-OFL.txt +93 -0
- package/resources/fonts/Mulish-VariableFont_wght.ttf +0 -0
- package/resources/fonts/SourceCodePro-OFL.txt +93 -0
- package/resources/fonts/SourceCodePro-VariableFont_wght.ttf +0 -0
- package/src/agents/copy-validation.ts +8 -2
- package/src/bundle/contracts.ts +1 -1
- package/src/cli/bundle-commands.ts +21 -0
- package/src/engine/resources.ts +9 -5
- package/src/report/core/rate-interval.ts +1 -2
- package/src/viewer/alternatives.ts +202 -0
- package/src/viewer/cited-domains.ts +127 -0
- package/src/viewer/page.ts +549 -236
- package/src/viewer/presentation.ts +759 -0
- package/src/viewer/server.ts +25 -4
- package/dist/lib/chunks/chunk-3TF4JGQA.js +0 -323
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
1
2
|
import path from 'node:path';
|
|
2
3
|
import {
|
|
3
4
|
ExportBlockedError,
|
|
@@ -17,6 +18,8 @@ import { BundleRefusedError } from '../bundle/zip';
|
|
|
17
18
|
import { readSettings } from '../engine/env-file';
|
|
18
19
|
import { runDirectoryState } from '../engine/local-run';
|
|
19
20
|
import { credentialSettingNames } from '../engine/preflight';
|
|
21
|
+
import { engineResources } from '../engine/resources';
|
|
22
|
+
import { viewerFontFiles } from '../viewer/page';
|
|
20
23
|
import { startViewer } from '../viewer/server';
|
|
21
24
|
import { formatLine, writeErr, writeOut, type Flags } from './io';
|
|
22
25
|
|
|
@@ -204,6 +207,23 @@ export const runVerify = async (flags: Flags, usage: string) => {
|
|
|
204
207
|
return verification.exitCode;
|
|
205
208
|
};
|
|
206
209
|
|
|
210
|
+
/** The viewer's fonts, from the engine's own resources. */
|
|
211
|
+
const readViewerFonts = async (): Promise<Map<string, Buffer>> => {
|
|
212
|
+
const { fonts } = await engineResources();
|
|
213
|
+
const entries = await Promise.all(
|
|
214
|
+
Object.values(viewerFontFiles).map(
|
|
215
|
+
async (file) => [file, await readFile(path.join(fonts, file))] as const,
|
|
216
|
+
),
|
|
217
|
+
).catch((err: unknown) => {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`The viewer's fonts are missing from ${fonts}. In a checkout, run pnpm build to refresh resources/; otherwise reinstall literally-aeo.`,
|
|
220
|
+
{ cause: err },
|
|
221
|
+
);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
return new Map(entries);
|
|
225
|
+
};
|
|
226
|
+
|
|
207
227
|
/** view serves a bundle or a run directory on loopback until Ctrl-C. */
|
|
208
228
|
export const runView = async (flags: Flags, usage: string) => {
|
|
209
229
|
const target = flags.positional[0];
|
|
@@ -231,6 +251,7 @@ export const runView = async (flags: Flags, usage: string) => {
|
|
|
231
251
|
const portOption = flags.options.get('--port');
|
|
232
252
|
const viewer = await startViewer(bundle, {
|
|
233
253
|
port: portOption === undefined ? 0 : Number(portOption),
|
|
254
|
+
fonts: await readViewerFonts(),
|
|
234
255
|
});
|
|
235
256
|
|
|
236
257
|
writeOut(
|
package/src/engine/resources.ts
CHANGED
|
@@ -4,11 +4,11 @@ import { enginePackageRoot } from './package-root';
|
|
|
4
4
|
|
|
5
5
|
// ============================================================================
|
|
6
6
|
// The engine's own files (specs/aeo-open-source-cli-spec.md §4.1): the copy
|
|
7
|
-
// and author rules, the banlist, the neutral editorial context
|
|
8
|
-
//
|
|
9
|
-
// reads them in place under src/. Never the caller's
|
|
10
|
-
// never a publisher's private editorial files unless
|
|
11
|
-
// them.
|
|
7
|
+
// and author rules, the banlist, the neutral editorial context, the operator
|
|
8
|
+
// guide and the report viewer's fonts. A packed install carries them in
|
|
9
|
+
// resources/; a checkout reads them in place under src/. Never the caller's
|
|
10
|
+
// working directory, and never a publisher's private editorial files unless
|
|
11
|
+
// AEO_EDITORIAL_DIR names them.
|
|
12
12
|
// ============================================================================
|
|
13
13
|
|
|
14
14
|
export type EngineResources = {
|
|
@@ -18,6 +18,8 @@ export type EngineResources = {
|
|
|
18
18
|
editorial: string;
|
|
19
19
|
/** The operator guide `literally-aeo guide` prints. */
|
|
20
20
|
guide: string;
|
|
21
|
+
/** The report viewer's fonts, served on loopback beside the page. */
|
|
22
|
+
fonts: string;
|
|
21
23
|
};
|
|
22
24
|
|
|
23
25
|
const exists = async (file: string) => {
|
|
@@ -38,6 +40,7 @@ export const engineResources = async (): Promise<EngineResources> => {
|
|
|
38
40
|
author: path.join(packed, 'author'),
|
|
39
41
|
editorial: path.join(packed, 'editorial'),
|
|
40
42
|
guide: path.join(packed, 'guide', 'OPERATOR.md'),
|
|
43
|
+
fonts: path.join(packed, 'fonts'),
|
|
41
44
|
};
|
|
42
45
|
}
|
|
43
46
|
|
|
@@ -46,5 +49,6 @@ export const engineResources = async (): Promise<EngineResources> => {
|
|
|
46
49
|
author: path.join(root, 'src', 'author'),
|
|
47
50
|
editorial: path.join(root, 'src', 'engine', 'editorial'),
|
|
48
51
|
guide: path.join(root, 'src', 'engine', 'guide', 'OPERATOR.md'),
|
|
52
|
+
fonts: path.join(root, 'src', 'viewer', 'fonts'),
|
|
49
53
|
};
|
|
50
54
|
};
|
|
@@ -3,8 +3,7 @@
|
|
|
3
3
|
// percents, so the range a reader sees always contains the computed one.
|
|
4
4
|
// Zod-free: the report page's client island imports it, and the fact
|
|
5
5
|
// sentences use it, so every place that states an interval rounds it the
|
|
6
|
-
// same way
|
|
7
|
-
// its own copy of this rule (viewer/page.ts, `intervalText`).
|
|
6
|
+
// same way, the local viewer included (viewer/presentation.ts).
|
|
8
7
|
// ============================================================================
|
|
9
8
|
|
|
10
9
|
/** The whole-percent range that contains [low, high], two shares in 0..1.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { normalizeDomain } from '../report/core/normalize-domain';
|
|
2
|
+
import { qualifiedCompetitorsForRow } from '../report/core/qualified-competitors';
|
|
3
|
+
import type { AeoMatrixRow } from '../report/core/schema';
|
|
4
|
+
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// "Who buyers hear about instead": a section's qualified alternatives,
|
|
7
|
+
// grouped as the hosted report groups them (its `groupAlternatives`), so a
|
|
8
|
+
// business carries one name and one count on both pages. Entries on one
|
|
9
|
+
// domain are one alternative; a name given without a domain joins the one
|
|
10
|
+
// domain whose answers used that name.
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
export type RankedAlternative = {
|
|
14
|
+
name: string;
|
|
15
|
+
domain?: string;
|
|
16
|
+
/** Answers that name the alternative, each counted once. */
|
|
17
|
+
mentioning: number;
|
|
18
|
+
captured: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type Sighting = {
|
|
22
|
+
key: string;
|
|
23
|
+
domain?: string;
|
|
24
|
+
// Every spelling the answers used, with the rows that used it.
|
|
25
|
+
names: Map<string, Set<number>>;
|
|
26
|
+
rowIndices: Set<number>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type Prospect = { name: string; domain: string };
|
|
30
|
+
|
|
31
|
+
/** rankAlternatives orders a section's alternatives by the answers that
|
|
32
|
+
* name them, most first. */
|
|
33
|
+
export const rankAlternatives = (
|
|
34
|
+
matrix: ReadonlyArray<
|
|
35
|
+
Pick<AeoMatrixRow, 'qualifiedCompetitors' | 'competitors'>
|
|
36
|
+
>,
|
|
37
|
+
prospect: Prospect,
|
|
38
|
+
): RankedAlternative[] => {
|
|
39
|
+
const isProspect = matchesProspect(prospect);
|
|
40
|
+
const ranked = [...foldNameOnlySightings(collectSightings(matrix)).values()]
|
|
41
|
+
.filter((sighting) => !isProspect(sighting))
|
|
42
|
+
.map((sighting) => ({
|
|
43
|
+
name: displayName(sighting),
|
|
44
|
+
...(sighting.domain !== undefined && { domain: sighting.domain }),
|
|
45
|
+
mentioning: sighting.rowIndices.size,
|
|
46
|
+
captured: matrix.length,
|
|
47
|
+
}))
|
|
48
|
+
.sort(
|
|
49
|
+
(a, b) => b.mentioning - a.mentioning || a.name.localeCompare(b.name),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return ranked;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const collectSightings = (
|
|
56
|
+
matrix: ReadonlyArray<
|
|
57
|
+
Pick<AeoMatrixRow, 'qualifiedCompetitors' | 'competitors'>
|
|
58
|
+
>,
|
|
59
|
+
): Map<string, Sighting> => {
|
|
60
|
+
const sightings = new Map<string, Sighting>();
|
|
61
|
+
|
|
62
|
+
matrix.forEach((row, rowIndex) => {
|
|
63
|
+
for (const brand of qualifiedCompetitorsForRow(row)) {
|
|
64
|
+
const name = brand.name.trim();
|
|
65
|
+
const domain = brand.domain ? normalizeDomain(brand.domain) : undefined;
|
|
66
|
+
const key = domain ?? name.toLowerCase();
|
|
67
|
+
const sighting = sightings.get(key) ?? {
|
|
68
|
+
key,
|
|
69
|
+
names: new Map<string, Set<number>>(),
|
|
70
|
+
rowIndices: new Set<number>(),
|
|
71
|
+
};
|
|
72
|
+
const nameRows = sighting.names.get(name) ?? new Set<number>();
|
|
73
|
+
|
|
74
|
+
// A name-only first sighting takes the domain a later entry supplies.
|
|
75
|
+
sighting.domain ??= domain;
|
|
76
|
+
nameRows.add(rowIndex);
|
|
77
|
+
sighting.names.set(name, nameRows);
|
|
78
|
+
sighting.rowIndices.add(rowIndex);
|
|
79
|
+
sightings.set(key, sighting);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return sightings;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// The analyzer sometimes returns one brand with and without a domain, which
|
|
87
|
+
// lands under two keys.
|
|
88
|
+
const foldNameOnlySightings = (
|
|
89
|
+
sightings: ReadonlyMap<string, Sighting>,
|
|
90
|
+
): Map<string, Sighting> => {
|
|
91
|
+
const domainKeysByName = new Map<string, Set<string>>();
|
|
92
|
+
|
|
93
|
+
for (const sighting of sightings.values()) {
|
|
94
|
+
if (sighting.domain === undefined) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
for (const name of sighting.names.keys()) {
|
|
99
|
+
const lowered = name.toLowerCase();
|
|
100
|
+
const keys = domainKeysByName.get(lowered) ?? new Set<string>();
|
|
101
|
+
|
|
102
|
+
keys.add(sighting.key);
|
|
103
|
+
domainKeysByName.set(lowered, keys);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const folded = new Map(sightings);
|
|
108
|
+
|
|
109
|
+
for (const sighting of sightings.values()) {
|
|
110
|
+
if (sighting.domain !== undefined) {
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const [ownerKey, ...otherKeys] = [
|
|
115
|
+
...(domainKeysByName.get(sighting.key) ?? []),
|
|
116
|
+
];
|
|
117
|
+
const owner = ownerKey === undefined ? undefined : folded.get(ownerKey);
|
|
118
|
+
|
|
119
|
+
if (!owner || otherKeys.length > 0) {
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
folded.set(owner.key, mergeSightings(owner, sighting));
|
|
124
|
+
folded.delete(sighting.key);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return folded;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const mergeSightings = (owner: Sighting, nameOnly: Sighting): Sighting => {
|
|
131
|
+
const names = new Map(
|
|
132
|
+
[...owner.names].map(([name, rows]) => [name, new Set(rows)]),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
for (const [name, rows] of nameOnly.names) {
|
|
136
|
+
names.set(name, new Set([...(names.get(name) ?? []), ...rows]));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const merged = {
|
|
140
|
+
...owner,
|
|
141
|
+
names,
|
|
142
|
+
rowIndices: new Set([...owner.rowIndices, ...nameOnly.rowIndices]),
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
return merged;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* displayName labels an alternative by what all its mentions share. When
|
|
150
|
+
* every name the answers used starts with the shortest one ("Rivalstack",
|
|
151
|
+
* "Rivalstack Edge"), that name, in its most-used spelling. Otherwise the
|
|
152
|
+
* entries are different products of one vendor, and the domain names the
|
|
153
|
+
* alternative, so no product is credited with its siblings' mentions.
|
|
154
|
+
*/
|
|
155
|
+
const displayName = (sighting: Sighting): string => {
|
|
156
|
+
const spellings = [...sighting.names].map(([name, rows]) => ({
|
|
157
|
+
name,
|
|
158
|
+
words: name.toLowerCase().split(/\s+/),
|
|
159
|
+
answers: rows.size,
|
|
160
|
+
}));
|
|
161
|
+
const [base] = [...spellings].sort(
|
|
162
|
+
(a, b) =>
|
|
163
|
+
a.words.length - b.words.length ||
|
|
164
|
+
b.answers - a.answers ||
|
|
165
|
+
a.name.localeCompare(b.name),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
if (!base) {
|
|
169
|
+
throw new Error(`Alternative has no recorded name: ${sighting.key}.`);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const isSharedBase = spellings.every((spelling) =>
|
|
173
|
+
base.words.every((word, index) => spelling.words[index] === word),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
if (!isSharedBase && sighting.domain !== undefined) {
|
|
177
|
+
return sighting.domain;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// The base name's spellings ("RivalStack", "Rivalstack"): the one most answers used.
|
|
181
|
+
const [label] = spellings
|
|
182
|
+
.filter((spelling) => spelling.words.length === base.words.length)
|
|
183
|
+
.sort((a, b) => b.answers - a.answers || a.name.localeCompare(b.name));
|
|
184
|
+
|
|
185
|
+
return (label ?? base).name;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const matchesProspect = (prospect: Prospect) => {
|
|
189
|
+
const prospectName = prospect.name.trim().toLowerCase();
|
|
190
|
+
const prospectDomain = normalizeDomain(prospect.domain);
|
|
191
|
+
|
|
192
|
+
return (sighting: Sighting): boolean => {
|
|
193
|
+
const identity = sighting.domain ?? sighting.key;
|
|
194
|
+
const isProspectDomain =
|
|
195
|
+
identity === prospectDomain || identity.endsWith(`.${prospectDomain}`);
|
|
196
|
+
const isProspectName = [...sighting.names.keys()].some(
|
|
197
|
+
(name) => name.toLowerCase() === prospectName,
|
|
198
|
+
);
|
|
199
|
+
|
|
200
|
+
return isProspectDomain || isProspectName;
|
|
201
|
+
};
|
|
202
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { isGroundingRedirectUrl } from '../report/core/grounding';
|
|
2
|
+
import { normalizeDomain } from '../report/core/normalize-domain';
|
|
3
|
+
import type { AeoVisibility } from '../report/core/schema';
|
|
4
|
+
|
|
5
|
+
// ============================================================================
|
|
6
|
+
// "What gets cited": every URL a section's answers cited, grouped by domain
|
|
7
|
+
// as the hosted report groups them. The brand's own domain leads, even at
|
|
8
|
+
// zero (an answer that never cites the brand is the finding), with its
|
|
9
|
+
// citations split by host (site, blog, docs); every other host groups under
|
|
10
|
+
// its last two labels.
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
export type CitedUrl = {
|
|
14
|
+
url: string;
|
|
15
|
+
count: number;
|
|
16
|
+
/** Distinct surfaces and question texts that cited the URL, first seen first. */
|
|
17
|
+
surfaces: string[];
|
|
18
|
+
prompts: string[];
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type CitedDomain = {
|
|
22
|
+
domain: string;
|
|
23
|
+
count: number;
|
|
24
|
+
isBrand: boolean;
|
|
25
|
+
/** The brand's citations by host; present on the brand's row only. */
|
|
26
|
+
hosts?: Array<{ label: string; count: number }>;
|
|
27
|
+
urls: CitedUrl[];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** groupCitedDomains lists the brand's row first, then every other domain
|
|
31
|
+
* by citations, most first. */
|
|
32
|
+
export const groupCitedDomains = (
|
|
33
|
+
visibility: Pick<AeoVisibility, 'matrix' | 'prompts'>,
|
|
34
|
+
brandDomain: string,
|
|
35
|
+
): CitedDomain[] => {
|
|
36
|
+
const brand = normalizeDomain(brandDomain);
|
|
37
|
+
const urlsByDomain = new Map<string, Map<string, CitedUrl>>();
|
|
38
|
+
const brandHostCounts = new Map<string, number>();
|
|
39
|
+
|
|
40
|
+
for (const row of visibility.matrix) {
|
|
41
|
+
for (const url of row.citedUrls ?? []) {
|
|
42
|
+
// An unresolved grounding redirect is an opaque wrapper, not a
|
|
43
|
+
// citation of Google.
|
|
44
|
+
const host = isGroundingRedirectUrl(url) ? undefined : hostOf(url);
|
|
45
|
+
|
|
46
|
+
if (host === undefined) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const isBrandHost = host === brand || host.endsWith(`.${brand}`);
|
|
51
|
+
const domain = isBrandHost ? brand : apexOf(host);
|
|
52
|
+
const urls = urlsByDomain.get(domain) ?? new Map<string, CitedUrl>();
|
|
53
|
+
const cited = urls.get(url) ?? {
|
|
54
|
+
url,
|
|
55
|
+
count: 0,
|
|
56
|
+
surfaces: [],
|
|
57
|
+
prompts: [],
|
|
58
|
+
};
|
|
59
|
+
const promptText = visibility.prompts[row.promptIdx]?.text;
|
|
60
|
+
|
|
61
|
+
cited.count += 1;
|
|
62
|
+
if (!cited.surfaces.includes(row.surface)) {
|
|
63
|
+
cited.surfaces.push(row.surface);
|
|
64
|
+
}
|
|
65
|
+
if (promptText !== undefined && !cited.prompts.includes(promptText)) {
|
|
66
|
+
cited.prompts.push(promptText);
|
|
67
|
+
}
|
|
68
|
+
urls.set(url, cited);
|
|
69
|
+
urlsByDomain.set(domain, urls);
|
|
70
|
+
if (isBrandHost) {
|
|
71
|
+
const label = brandHostLabel(host, brand);
|
|
72
|
+
|
|
73
|
+
brandHostCounts.set(label, (brandHostCounts.get(label) ?? 0) + 1);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const domainOf = (domain: string): CitedDomain => {
|
|
79
|
+
const urls = [...(urlsByDomain.get(domain)?.values() ?? [])].sort(
|
|
80
|
+
(a, b) => b.count - a.count || a.url.localeCompare(b.url),
|
|
81
|
+
);
|
|
82
|
+
const cited = {
|
|
83
|
+
domain,
|
|
84
|
+
count: urls.reduce((sum, url) => sum + url.count, 0),
|
|
85
|
+
isBrand: domain === brand,
|
|
86
|
+
urls,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
return cited;
|
|
90
|
+
};
|
|
91
|
+
const brandRow: CitedDomain = {
|
|
92
|
+
...domainOf(brand),
|
|
93
|
+
...(brandHostCounts.size > 0 && {
|
|
94
|
+
hosts: [...brandHostCounts]
|
|
95
|
+
.map(([label, count]) => ({ label, count }))
|
|
96
|
+
.sort((a, b) => b.count - a.count),
|
|
97
|
+
}),
|
|
98
|
+
};
|
|
99
|
+
const others = [...urlsByDomain.keys()]
|
|
100
|
+
.filter((domain) => domain !== brand)
|
|
101
|
+
.map(domainOf)
|
|
102
|
+
.sort((a, b) => b.count - a.count || a.domain.localeCompare(b.domain));
|
|
103
|
+
const domains = [brandRow, ...others];
|
|
104
|
+
|
|
105
|
+
return domains;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const hostOf = (url: string): string | undefined => {
|
|
109
|
+
try {
|
|
110
|
+
return normalizeDomain(new URL(url).hostname);
|
|
111
|
+
} catch {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// The last two labels approximate the registrable domain (reddit.com). A
|
|
117
|
+
// multi-part public suffix (co.uk) collapses a level: the table is a
|
|
118
|
+
// grouping aid, not a registry.
|
|
119
|
+
const apexOf = (host: string): string => {
|
|
120
|
+
const labels = host.split('.');
|
|
121
|
+
|
|
122
|
+
return labels.length <= 2 ? host : labels.slice(-2).join('.');
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const brandHostLabel = (host: string, brand: string): string => {
|
|
126
|
+
return host === brand ? 'site' : host.slice(0, -(brand.length + 1));
|
|
127
|
+
};
|