nfunc-mcp 0.3.0 → 0.4.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 +84 -376
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/mappers/labFieldComparator.d.ts +62 -0
- package/dist/mappers/labFieldComparator.js +134 -0
- package/dist/mappers/labFieldComparator.js.map +1 -0
- package/dist/mappers/psiAggregator.d.ts +130 -0
- package/dist/mappers/psiAggregator.js +293 -0
- package/dist/mappers/psiAggregator.js.map +1 -0
- package/dist/mappers/webVitalsMapper.d.ts +52 -0
- package/dist/mappers/webVitalsMapper.js +131 -0
- package/dist/mappers/webVitalsMapper.js.map +1 -0
- package/dist/tools/performanceAudit.d.ts +2 -0
- package/dist/tools/performanceAudit.js +446 -0
- package/dist/tools/performanceAudit.js.map +1 -0
- package/dist/tools/performanceAuditPlan.d.ts +2 -0
- package/dist/tools/performanceAuditPlan.js +438 -0
- package/dist/tools/performanceAuditPlan.js.map +1 -0
- package/dist/utils/csvReader.d.ts +20 -0
- package/dist/utils/csvReader.js +172 -0
- package/dist/utils/csvReader.js.map +1 -0
- package/dist/utils/httpClient.d.ts +84 -0
- package/dist/utils/httpClient.js +171 -0
- package/dist/utils/httpClient.js.map +1 -0
- package/dist/utils/psiAuth.d.ts +26 -0
- package/dist/utils/psiAuth.js +36 -0
- package/dist/utils/psiAuth.js.map +1 -0
- package/dist/utils/psiParser.d.ts +124 -0
- package/dist/utils/psiParser.js +200 -0
- package/dist/utils/psiParser.js.map +1 -0
- package/dist/utils/publicUrl.d.ts +17 -0
- package/dist/utils/publicUrl.js +115 -0
- package/dist/utils/publicUrl.js.map +1 -0
- package/dist/utils/sitemapReader.d.ts +27 -0
- package/dist/utils/sitemapReader.js +272 -0
- package/dist/utils/sitemapReader.js.map +1 -0
- package/dist/utils/urlClassifier.d.ts +45 -0
- package/dist/utils/urlClassifier.js +267 -0
- package/dist/utils/urlClassifier.js.map +1 -0
- package/docs/manual.md +558 -0
- package/docs/psi-report-spec.md +174 -0
- package/package.json +13 -3
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP counterpart to shellRunner.
|
|
3
|
+
*
|
|
4
|
+
* Every tool before this one reached the outside world by spawning a process,
|
|
5
|
+
* so `runShell` was the single choke point for timeout, error and duration
|
|
6
|
+
* handling. PSI is an HTTP API, and bending shellRunner around `curl` would
|
|
7
|
+
* trade a typed response for string parsing. This mirrors shellRunner's
|
|
8
|
+
* contract instead: never throws for a reachable-but-unhappy endpoint, always
|
|
9
|
+
* reports duration, and leaves the decision about what counts as failure to
|
|
10
|
+
* the caller.
|
|
11
|
+
*
|
|
12
|
+
* Node 18+ ships global fetch, so this adds no dependency.
|
|
13
|
+
*/
|
|
14
|
+
export interface HttpResult {
|
|
15
|
+
/** True only for a 2xx. A 404 is a result, not an exception. */
|
|
16
|
+
ok: boolean;
|
|
17
|
+
/** 0 when the request never got a response (timeout, DNS, connection refused). */
|
|
18
|
+
status: number;
|
|
19
|
+
body: string;
|
|
20
|
+
/** Raw response bytes. Populated only when `raw` was requested. */
|
|
21
|
+
bytes?: Uint8Array;
|
|
22
|
+
durationMs: number;
|
|
23
|
+
/** Total attempts made, including the successful one. 1 means no retries. */
|
|
24
|
+
attempts: number;
|
|
25
|
+
/** Present only when `ok` is false. Always redacted. */
|
|
26
|
+
error?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface HttpRequestOptions {
|
|
29
|
+
timeoutMs?: number;
|
|
30
|
+
/** Extra attempts after the first. 0 disables retrying. */
|
|
31
|
+
retries?: number;
|
|
32
|
+
/** Base delay for exponential backoff; doubled each attempt. */
|
|
33
|
+
retryDelayMs?: number;
|
|
34
|
+
headers?: Record<string, string>;
|
|
35
|
+
/**
|
|
36
|
+
* Return bytes instead of decoded text. Needed for `.xml.gz` sitemaps, which
|
|
37
|
+
* are served as an opaque gzip payload rather than with Content-Encoding, so
|
|
38
|
+
* fetch does not inflate them and decoding to a string corrupts them.
|
|
39
|
+
*/
|
|
40
|
+
raw?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Absolute ceiling across every attempt, including backoff. Where `timeoutMs`
|
|
43
|
+
* bounds one request, this bounds the whole call: each attempt is shortened
|
|
44
|
+
* to whatever is left, and retrying stops when nothing is. Without it,
|
|
45
|
+
* enabling `retryOnTimeout` silently doubles the worst case, which is exactly
|
|
46
|
+
* how a chunked runner overruns its budget.
|
|
47
|
+
*/
|
|
48
|
+
totalBudgetMs?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Retry after a timeout. Off by default: a timeout means the request already
|
|
51
|
+
* spent its entire budget, so retrying multiplies the wall clock by the
|
|
52
|
+
* attempt count for a request that is unlikely to be faster next time. A 429
|
|
53
|
+
* or 5xx is different — those fail fast and often succeed on retry.
|
|
54
|
+
*/
|
|
55
|
+
retryOnTimeout?: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Literal strings to strip from every error message before it leaves this
|
|
58
|
+
* module — API keys, tokens, anything that would otherwise reach a log, a
|
|
59
|
+
* tool response, or a conversation transcript.
|
|
60
|
+
*/
|
|
61
|
+
redact?: string[];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* A URL safe to put in an error message.
|
|
65
|
+
*
|
|
66
|
+
* The PSI request URL carries the API key in the query string, so any error
|
|
67
|
+
* that interpolates the URL leaks the key into the tool response — and from
|
|
68
|
+
* there into the conversation transcript. Redacting at the point of formatting
|
|
69
|
+
* rather than asking every call site to remember is the only version of this
|
|
70
|
+
* that stays correct.
|
|
71
|
+
*/
|
|
72
|
+
export declare function sanitizeUrl(url: string | URL): string;
|
|
73
|
+
export declare function httpGet(url: string | URL, options?: HttpRequestOptions): Promise<HttpResult>;
|
|
74
|
+
export interface HttpJsonResult<T> extends Omit<HttpResult, "body"> {
|
|
75
|
+
data: T | null;
|
|
76
|
+
/** Kept for error reporting; a caller that has `data` should not need it. */
|
|
77
|
+
body: string;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* `httpGet` plus JSON parsing, with malformed JSON demoted to `ok: false`
|
|
81
|
+
* rather than a thrown SyntaxError. A 200 carrying truncated JSON is a failed
|
|
82
|
+
* request as far as every caller is concerned.
|
|
83
|
+
*/
|
|
84
|
+
export declare function httpGetJson<T>(url: string | URL, options?: HttpRequestOptions): Promise<HttpJsonResult<T>>;
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTTP counterpart to shellRunner.
|
|
3
|
+
*
|
|
4
|
+
* Every tool before this one reached the outside world by spawning a process,
|
|
5
|
+
* so `runShell` was the single choke point for timeout, error and duration
|
|
6
|
+
* handling. PSI is an HTTP API, and bending shellRunner around `curl` would
|
|
7
|
+
* trade a typed response for string parsing. This mirrors shellRunner's
|
|
8
|
+
* contract instead: never throws for a reachable-but-unhappy endpoint, always
|
|
9
|
+
* reports duration, and leaves the decision about what counts as failure to
|
|
10
|
+
* the caller.
|
|
11
|
+
*
|
|
12
|
+
* Node 18+ ships global fetch, so this adds no dependency.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Sites behind a bot wall reject the default Node fetch agent outright, which
|
|
16
|
+
* turns a healthy site into a 403 and a sitemap into a "not found". Naming
|
|
17
|
+
* ourselves honestly and looking like a browser gets through most of them;
|
|
18
|
+
* a caller can override it via `headers`.
|
|
19
|
+
*/
|
|
20
|
+
const DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 " +
|
|
21
|
+
"(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 nfunc-mcp/qa-audit";
|
|
22
|
+
/** Query parameters whose values are stripped before a URL is quoted anywhere. */
|
|
23
|
+
const SENSITIVE_PARAMS = new Set(["key", "api_key", "apikey", "token", "access_token"]);
|
|
24
|
+
/**
|
|
25
|
+
* A URL safe to put in an error message.
|
|
26
|
+
*
|
|
27
|
+
* The PSI request URL carries the API key in the query string, so any error
|
|
28
|
+
* that interpolates the URL leaks the key into the tool response — and from
|
|
29
|
+
* there into the conversation transcript. Redacting at the point of formatting
|
|
30
|
+
* rather than asking every call site to remember is the only version of this
|
|
31
|
+
* that stays correct.
|
|
32
|
+
*/
|
|
33
|
+
export function sanitizeUrl(url) {
|
|
34
|
+
try {
|
|
35
|
+
const u = new URL(url);
|
|
36
|
+
for (const param of u.searchParams.keys()) {
|
|
37
|
+
if (SENSITIVE_PARAMS.has(param.toLowerCase())) {
|
|
38
|
+
u.searchParams.set(param, "REDACTED");
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return u.toString();
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return "[unparseable url]";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function redactAll(message, secrets) {
|
|
48
|
+
let out = message;
|
|
49
|
+
for (const secret of secrets) {
|
|
50
|
+
// A short "secret" would redact half the message; a real key is far longer.
|
|
51
|
+
if (secret && secret.length >= 8)
|
|
52
|
+
out = out.split(secret).join("REDACTED");
|
|
53
|
+
}
|
|
54
|
+
return out;
|
|
55
|
+
}
|
|
56
|
+
/** Retry only what a retry can plausibly fix. A 400 will be a 400 next time too. */
|
|
57
|
+
function isRetryable(status) {
|
|
58
|
+
return status === 408 || status === 429 || status >= 500;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* `Retry-After` is either a delay in seconds or an HTTP date. Google sends
|
|
62
|
+
* seconds on 429s, but the date form is legal and cheap to support.
|
|
63
|
+
*/
|
|
64
|
+
function retryAfterMs(header) {
|
|
65
|
+
if (!header)
|
|
66
|
+
return null;
|
|
67
|
+
const seconds = Number(header);
|
|
68
|
+
if (Number.isFinite(seconds) && seconds >= 0)
|
|
69
|
+
return seconds * 1000;
|
|
70
|
+
const date = Date.parse(header);
|
|
71
|
+
if (!Number.isNaN(date))
|
|
72
|
+
return Math.max(0, date - Date.now());
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
76
|
+
export async function httpGet(url, options = {}) {
|
|
77
|
+
const { timeoutMs = 60_000, retries = 2, retryDelayMs = 2_000, headers, raw = false, totalBudgetMs, retryOnTimeout = false, redact = [], } = options;
|
|
78
|
+
const start = Date.now();
|
|
79
|
+
const safeUrl = sanitizeUrl(url);
|
|
80
|
+
let lastError = "";
|
|
81
|
+
let lastStatus = 0;
|
|
82
|
+
const deadline = totalBudgetMs === undefined ? null : start + totalBudgetMs;
|
|
83
|
+
const remainingMs = () => (deadline === null ? Infinity : deadline - Date.now());
|
|
84
|
+
for (let attempt = 1; attempt <= retries + 1; attempt++) {
|
|
85
|
+
const budgetLeft = remainingMs();
|
|
86
|
+
if (budgetLeft <= 0) {
|
|
87
|
+
lastError = lastError || `Request to ${safeUrl} ran out of time budget`;
|
|
88
|
+
break;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
const response = await fetch(url, {
|
|
92
|
+
headers: { "user-agent": DEFAULT_USER_AGENT, ...headers },
|
|
93
|
+
signal: AbortSignal.timeout(Math.min(timeoutMs, budgetLeft)),
|
|
94
|
+
});
|
|
95
|
+
lastStatus = response.status;
|
|
96
|
+
if (response.ok) {
|
|
97
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
98
|
+
return {
|
|
99
|
+
ok: true,
|
|
100
|
+
status: response.status,
|
|
101
|
+
body: raw ? "" : new TextDecoder().decode(bytes),
|
|
102
|
+
...(raw ? { bytes } : {}),
|
|
103
|
+
durationMs: Date.now() - start,
|
|
104
|
+
attempts: attempt,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
// Read the body before deciding — a non-2xx from PSI carries a JSON
|
|
108
|
+
// error object that says far more than the status code does.
|
|
109
|
+
const body = await response.text().catch(() => "");
|
|
110
|
+
lastError = `HTTP ${response.status} from ${safeUrl}${body ? `: ${body.slice(0, 300)}` : ""}`;
|
|
111
|
+
if (!isRetryable(response.status) || attempt > retries) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false,
|
|
114
|
+
status: response.status,
|
|
115
|
+
body,
|
|
116
|
+
durationMs: Date.now() - start,
|
|
117
|
+
attempts: attempt,
|
|
118
|
+
error: redactAll(lastError, redact),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const serverAsked = retryAfterMs(response.headers.get("retry-after"));
|
|
122
|
+
const backoff = Math.min(serverAsked ?? retryDelayMs * 2 ** (attempt - 1), Math.max(0, remainingMs()));
|
|
123
|
+
await sleep(backoff);
|
|
124
|
+
}
|
|
125
|
+
catch (err) {
|
|
126
|
+
// Timeouts and network failures land here. AbortSignal.timeout raises a
|
|
127
|
+
// TimeoutError, which reads as an unexplained abort unless named.
|
|
128
|
+
const e = err;
|
|
129
|
+
const timedOut = e.name === "TimeoutError" || e.name === "AbortError";
|
|
130
|
+
lastStatus = 0;
|
|
131
|
+
lastError = timedOut
|
|
132
|
+
? `Request to ${safeUrl} timed out after ${timeoutMs} ms`
|
|
133
|
+
: `Request to ${safeUrl} failed: ${e.message ?? "unknown network error"}`;
|
|
134
|
+
if (timedOut && !retryOnTimeout)
|
|
135
|
+
break;
|
|
136
|
+
if (attempt > retries)
|
|
137
|
+
break;
|
|
138
|
+
await sleep(Math.min(retryDelayMs * 2 ** (attempt - 1), Math.max(0, remainingMs())));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
ok: false,
|
|
143
|
+
status: lastStatus,
|
|
144
|
+
body: "",
|
|
145
|
+
durationMs: Date.now() - start,
|
|
146
|
+
attempts: retries + 1,
|
|
147
|
+
error: redactAll(lastError, redact),
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* `httpGet` plus JSON parsing, with malformed JSON demoted to `ok: false`
|
|
152
|
+
* rather than a thrown SyntaxError. A 200 carrying truncated JSON is a failed
|
|
153
|
+
* request as far as every caller is concerned.
|
|
154
|
+
*/
|
|
155
|
+
export async function httpGetJson(url, options = {}) {
|
|
156
|
+
const result = await httpGet(url, options);
|
|
157
|
+
if (!result.ok)
|
|
158
|
+
return { ...result, data: null };
|
|
159
|
+
try {
|
|
160
|
+
return { ...result, data: JSON.parse(result.body) };
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
return {
|
|
164
|
+
...result,
|
|
165
|
+
ok: false,
|
|
166
|
+
data: null,
|
|
167
|
+
error: redactAll(`Response from ${sanitizeUrl(url)} was not valid JSON: ${err.message}`, options.redact ?? []),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=httpClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"httpClient.js","sourceRoot":"","sources":["../../src/utils/httpClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAqDH;;;;;GAKG;AACH,MAAM,kBAAkB,GACtB,qEAAqE;IACrE,uEAAuE,CAAC;AAE1E,kFAAkF;AAClF,MAAM,gBAAgB,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,cAAc,CAAC,CAAC,CAAC;AAExF;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,GAAiB;IAC3C,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;QACvB,KAAK,MAAM,KAAK,IAAI,CAAC,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,CAAC;YAC1C,IAAI,gBAAgB,CAAC,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,CAAC,EAAE,CAAC;gBAC9C,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;YACxC,CAAC;QACH,CAAC;QACD,OAAO,CAAC,CAAC,QAAQ,EAAE,CAAC;IACtB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,mBAAmB,CAAC;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AM,CAAC,KAAK,UAAU,WAAW,CAC/B,GAAiB,EACjB,UAA8B,EAAE;IAEhC,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;IAC3C,IAAI,CAAC,MAAM,CAAC,EAAE;QAAE,OAAO,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAEjD,IAAI,CAAC;QACH,OAAO,EAAE,GAAG,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAM,EAAE,CAAC;IAC3D,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO;YACL,GAAG,MAAM;YACT,EAAE,EAAE,KAAK;YACT,IAAI,EAAE,IAAI;YACV,KAAK,EAAE,SAAS,CACd,iBAAiB,WAAW,CAAC,GAAG,CAAC,wBAAyB,GAAa,CAAC,OAAO,EAAE,EACjF,OAAO,CAAC,MAAM,IAAI,EAAE,CACrB;SACF,CAAC;IACJ,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PSI API key resolution.
|
|
3
|
+
*
|
|
4
|
+
* Precedence: explicit tool input → PAGESPEED_API_KEY → keyless.
|
|
5
|
+
*
|
|
6
|
+
* The preferred setup is the `env` block in the MCP client config, because the
|
|
7
|
+
* client launches this server and we do not control its flags — `--env-file`
|
|
8
|
+
* is not available to us. An explicit input is supported for people who want
|
|
9
|
+
* to paste a key once, but it is the worst option: it lands in the
|
|
10
|
+
* conversation transcript and in any client-side logging, so it is never
|
|
11
|
+
* echoed back and every error that touches a request URL is redacted.
|
|
12
|
+
*/
|
|
13
|
+
export type KeySource = "input" | "env" | "none";
|
|
14
|
+
export interface ResolvedKey {
|
|
15
|
+
key: string | null;
|
|
16
|
+
source: KeySource;
|
|
17
|
+
}
|
|
18
|
+
export declare const KEY_ENV_VAR = "PAGESPEED_API_KEY";
|
|
19
|
+
/**
|
|
20
|
+
* Keyless PSI is rate-limited hard enough that a batch will 429 partway
|
|
21
|
+
* through, leaving a half-finished audit and a confusing error. Refusing up
|
|
22
|
+
* front with instructions is a better failure than discovering it at run 12.
|
|
23
|
+
*/
|
|
24
|
+
export declare const KEYLESS_RUN_CAP = 4;
|
|
25
|
+
export declare function resolveApiKey(explicit?: string): ResolvedKey;
|
|
26
|
+
export declare function keylessWarning(runs: number): string;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PSI API key resolution.
|
|
3
|
+
*
|
|
4
|
+
* Precedence: explicit tool input → PAGESPEED_API_KEY → keyless.
|
|
5
|
+
*
|
|
6
|
+
* The preferred setup is the `env` block in the MCP client config, because the
|
|
7
|
+
* client launches this server and we do not control its flags — `--env-file`
|
|
8
|
+
* is not available to us. An explicit input is supported for people who want
|
|
9
|
+
* to paste a key once, but it is the worst option: it lands in the
|
|
10
|
+
* conversation transcript and in any client-side logging, so it is never
|
|
11
|
+
* echoed back and every error that touches a request URL is redacted.
|
|
12
|
+
*/
|
|
13
|
+
export const KEY_ENV_VAR = "PAGESPEED_API_KEY";
|
|
14
|
+
/**
|
|
15
|
+
* Keyless PSI is rate-limited hard enough that a batch will 429 partway
|
|
16
|
+
* through, leaving a half-finished audit and a confusing error. Refusing up
|
|
17
|
+
* front with instructions is a better failure than discovering it at run 12.
|
|
18
|
+
*/
|
|
19
|
+
export const KEYLESS_RUN_CAP = 4;
|
|
20
|
+
export function resolveApiKey(explicit) {
|
|
21
|
+
const trimmed = explicit?.trim();
|
|
22
|
+
if (trimmed)
|
|
23
|
+
return { key: trimmed, source: "input" };
|
|
24
|
+
const fromEnv = process.env[KEY_ENV_VAR]?.trim();
|
|
25
|
+
if (fromEnv)
|
|
26
|
+
return { key: fromEnv, source: "env" };
|
|
27
|
+
return { key: null, source: "none" };
|
|
28
|
+
}
|
|
29
|
+
export function keylessWarning(runs) {
|
|
30
|
+
return (`No ${KEY_ENV_VAR} set — running unauthenticated against a shared, ` +
|
|
31
|
+
`heavily rate-limited quota. This is fine for ${KEYLESS_RUN_CAP} runs or ` +
|
|
32
|
+
`fewer; ${runs} runs will almost certainly hit HTTP 429 partway through. ` +
|
|
33
|
+
`Get a key from the Google Cloud console (enable the PageSpeed Insights ` +
|
|
34
|
+
`API) and set ${KEY_ENV_VAR} in your MCP client config.`);
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=psiAuth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"psiAuth.js","sourceRoot":"","sources":["../../src/utils/psiAuth.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AASH,MAAM,CAAC,MAAM,WAAW,GAAG,mBAAmB,CAAC;AAE/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,CAAC;AAEjC,MAAM,UAAU,aAAa,CAAC,QAAiB;IAC7C,MAAM,OAAO,GAAG,QAAQ,EAAE,IAAI,EAAE,CAAC;IACjC,IAAI,OAAO;QAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAEtD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,IAAI,OAAO;QAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;IAEpD,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,IAAY;IACzC,OAAO,CACL,MAAM,WAAW,mDAAmD;QACpE,gDAAgD,eAAe,WAAW;QAC1E,UAAU,IAAI,4DAA4D;QAC1E,yEAAyE;QACzE,gBAAgB,WAAW,6BAA6B,CACzD,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PageSpeed Insights response parsing.
|
|
3
|
+
*
|
|
4
|
+
* One PSI call returns two unrelated datasets: `lighthouseResult` (a lab run
|
|
5
|
+
* on Google's hardware) and `loadingExperience` (CrUX field data from real
|
|
6
|
+
* Chrome users). They are measured differently, they disagree routinely, and
|
|
7
|
+
* conflating them produces confidently wrong reports — so they stay separate
|
|
8
|
+
* all the way through this module and only meet in the comparator.
|
|
9
|
+
*
|
|
10
|
+
* The lab half is the same LHR schema `parseLighthouseJSON` already handles
|
|
11
|
+
* and is delegated to it unchanged. Everything here is about the field half
|
|
12
|
+
* and about pulling numeric lab metrics that survive aggregation.
|
|
13
|
+
*/
|
|
14
|
+
import { type ParsedLighthouse } from "./outputParsers.js";
|
|
15
|
+
export type CruxCategory = "FAST" | "AVERAGE" | "SLOW" | "NONE";
|
|
16
|
+
/** The five field metrics PSI reports. `fid` is deliberately absent — see below. */
|
|
17
|
+
export type WebVital = "lcp" | "inp" | "cls" | "fcp" | "ttfb";
|
|
18
|
+
export type FieldSource = "url" | "origin";
|
|
19
|
+
export interface CruxMetric {
|
|
20
|
+
/**
|
|
21
|
+
* 75th percentile across the collection window. Milliseconds for every
|
|
22
|
+
* metric except `cls`, which is unitless.
|
|
23
|
+
*/
|
|
24
|
+
p75: number;
|
|
25
|
+
category: CruxCategory;
|
|
26
|
+
/** Share of real users in each bucket. Sums to ~1. */
|
|
27
|
+
distribution: {
|
|
28
|
+
good: number;
|
|
29
|
+
needsImprovement: number;
|
|
30
|
+
poor: number;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Whether this specific metric came from the URL or from origin-wide data.
|
|
34
|
+
* Per metric, not per response — see the note on `parseCrux`.
|
|
35
|
+
*/
|
|
36
|
+
source: FieldSource;
|
|
37
|
+
}
|
|
38
|
+
export interface ParsedCrux {
|
|
39
|
+
/**
|
|
40
|
+
* Always 28. PSI does not return the collection window, but CrUX in PSI is
|
|
41
|
+
* defined as a trailing 28-day aggregate, so this is a documented constant
|
|
42
|
+
* rather than a parsed value. Stated explicitly because reports must not
|
|
43
|
+
* describe field data as real-time.
|
|
44
|
+
*/
|
|
45
|
+
collectionPeriodDays: 28;
|
|
46
|
+
overallCategory: CruxCategory | null;
|
|
47
|
+
/** Which source the majority of metrics came from, for a one-line summary. */
|
|
48
|
+
primarySource: FieldSource;
|
|
49
|
+
metrics: Partial<Record<WebVital, CruxMetric>>;
|
|
50
|
+
}
|
|
51
|
+
export interface LabMetrics {
|
|
52
|
+
lcpMs: number | null;
|
|
53
|
+
fcpMs: number | null;
|
|
54
|
+
cls: number | null;
|
|
55
|
+
tbtMs: number | null;
|
|
56
|
+
speedIndexMs: number | null;
|
|
57
|
+
ttiMs: number | null;
|
|
58
|
+
ttfbMs: number | null;
|
|
59
|
+
}
|
|
60
|
+
export interface ParsedPsi {
|
|
61
|
+
requestedUrl: string;
|
|
62
|
+
finalUrl: string;
|
|
63
|
+
strategy: string;
|
|
64
|
+
fetchTime: string | null;
|
|
65
|
+
lighthouseVersion: string | null;
|
|
66
|
+
scores: Record<string, number>;
|
|
67
|
+
lab: LabMetrics;
|
|
68
|
+
/** Null when the URL has too little real-user traffic and origin fallback is off or empty. */
|
|
69
|
+
field: ParsedCrux | null;
|
|
70
|
+
lighthouse: ParsedLighthouse;
|
|
71
|
+
/** Lighthouse audits that failed, for the existing defect formatter. */
|
|
72
|
+
runWarnings: string[];
|
|
73
|
+
}
|
|
74
|
+
/** PSI returned a 200 whose Lighthouse run did not actually complete. */
|
|
75
|
+
export declare class PsiRuntimeError extends Error {
|
|
76
|
+
readonly code: string;
|
|
77
|
+
constructor(code: string, message: string);
|
|
78
|
+
}
|
|
79
|
+
interface RawCruxMetric {
|
|
80
|
+
percentile?: number;
|
|
81
|
+
category?: string;
|
|
82
|
+
distributions?: Array<{
|
|
83
|
+
min?: number;
|
|
84
|
+
max?: number;
|
|
85
|
+
proportion?: number;
|
|
86
|
+
}>;
|
|
87
|
+
}
|
|
88
|
+
interface RawLoadingExperience {
|
|
89
|
+
id?: string;
|
|
90
|
+
overall_category?: string;
|
|
91
|
+
metrics?: Record<string, RawCruxMetric>;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Merge URL-level and origin-level CrUX into one metric set.
|
|
95
|
+
*
|
|
96
|
+
* Field availability is **per metric, not per URL** — a page can have enough
|
|
97
|
+
* traffic for CrUX to report CLS but not LCP. A real response in the Five
|
|
98
|
+
* Below batch carried two of the five metrics at URL level while the origin
|
|
99
|
+
* carried all five, which is why the manual audit shows "No field data" for
|
|
100
|
+
* one metric and a category for another on the same row.
|
|
101
|
+
*
|
|
102
|
+
* So the fallback runs per metric, and each metric records where it came from.
|
|
103
|
+
* A single response-level `field_source` flag would have to either discard the
|
|
104
|
+
* URL-level metrics that do exist or silently label origin data as page data,
|
|
105
|
+
* and origin CrUX for a homepage says nothing about a checkout page.
|
|
106
|
+
*/
|
|
107
|
+
export declare function parseCrux(urlLevel: RawLoadingExperience | undefined, originLevel: RawLoadingExperience | undefined, originFallback: boolean): ParsedCrux | null;
|
|
108
|
+
export interface ParsePsiOptions {
|
|
109
|
+
strategy: string;
|
|
110
|
+
/** Fall back to origin-wide CrUX per metric when URL-level data is absent. */
|
|
111
|
+
originFallback?: boolean;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Parse a full PSI v5 response.
|
|
115
|
+
*
|
|
116
|
+
* Throws `PsiRuntimeError` when Lighthouse itself failed inside a 200
|
|
117
|
+
* response. PSI does this routinely — a page that times out or refuses the
|
|
118
|
+
* fetch comes back as HTTP 200 with `lighthouseResult.runtimeError` set and
|
|
119
|
+
* every score null. Parsing it anyway records a real page as scoring zero
|
|
120
|
+
* across the board, which is worse than a failed run, because a zero looks
|
|
121
|
+
* like data. Callers should treat this as retryable.
|
|
122
|
+
*/
|
|
123
|
+
export declare function parsePsiResponse(rawJson: string, options: ParsePsiOptions): ParsedPsi;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PageSpeed Insights response parsing.
|
|
3
|
+
*
|
|
4
|
+
* One PSI call returns two unrelated datasets: `lighthouseResult` (a lab run
|
|
5
|
+
* on Google's hardware) and `loadingExperience` (CrUX field data from real
|
|
6
|
+
* Chrome users). They are measured differently, they disagree routinely, and
|
|
7
|
+
* conflating them produces confidently wrong reports — so they stay separate
|
|
8
|
+
* all the way through this module and only meet in the comparator.
|
|
9
|
+
*
|
|
10
|
+
* The lab half is the same LHR schema `parseLighthouseJSON` already handles
|
|
11
|
+
* and is delegated to it unchanged. Everything here is about the field half
|
|
12
|
+
* and about pulling numeric lab metrics that survive aggregation.
|
|
13
|
+
*/
|
|
14
|
+
import { parseLighthouseJSON } from "./outputParsers.js";
|
|
15
|
+
/** PSI returned a 200 whose Lighthouse run did not actually complete. */
|
|
16
|
+
export class PsiRuntimeError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
constructor(code, message) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.code = code;
|
|
21
|
+
this.name = "PsiRuntimeError";
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* CrUX metric key → our vital name.
|
|
26
|
+
*
|
|
27
|
+
* FIRST_INPUT_DELAY_MS is intentionally not mapped. FID was retired in March
|
|
28
|
+
* 2024 and replaced by INP; it still appears in some responses, and treating
|
|
29
|
+
* it as a current metric would report a defect against a standard Google no
|
|
30
|
+
* longer measures. Read it only if a caller ever needs historical data.
|
|
31
|
+
*/
|
|
32
|
+
const CRUX_KEYS = {
|
|
33
|
+
LARGEST_CONTENTFUL_PAINT_MS: "lcp",
|
|
34
|
+
INTERACTION_TO_NEXT_PAINT: "inp",
|
|
35
|
+
CUMULATIVE_LAYOUT_SHIFT_SCORE: "cls",
|
|
36
|
+
FIRST_CONTENTFUL_PAINT_MS: "fcp",
|
|
37
|
+
EXPERIMENTAL_TIME_TO_FIRST_BYTE: "ttfb",
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* CLS is the one field metric PSI returns scaled by 100: a `percentile` of 55
|
|
41
|
+
* means a CLS of 0.55, and the distribution bucket boundaries (0-10, 10-25)
|
|
42
|
+
* are scaled the same way. Comparing the raw integer against the 0.25 "poor"
|
|
43
|
+
* threshold marks every site on earth as catastrophic; forgetting the scaling
|
|
44
|
+
* entirely turns a 0.55 into a 55. Verified against real responses.
|
|
45
|
+
*/
|
|
46
|
+
function scaleCruxValue(vital, percentile) {
|
|
47
|
+
return vital === "cls" ? percentile / 100 : percentile;
|
|
48
|
+
}
|
|
49
|
+
function toCategory(raw) {
|
|
50
|
+
return raw === "FAST" || raw === "AVERAGE" || raw === "SLOW" ? raw : "NONE";
|
|
51
|
+
}
|
|
52
|
+
function toDistribution(raw) {
|
|
53
|
+
const share = (i) => {
|
|
54
|
+
const p = raw?.[i]?.proportion;
|
|
55
|
+
return typeof p === "number" ? Number(p.toFixed(4)) : 0;
|
|
56
|
+
};
|
|
57
|
+
// PSI always emits exactly three buckets, ordered good → poor.
|
|
58
|
+
return { good: share(0), needsImprovement: share(1), poor: share(2) };
|
|
59
|
+
}
|
|
60
|
+
function readMetric(raw, vital, source) {
|
|
61
|
+
if (!raw || typeof raw.percentile !== "number")
|
|
62
|
+
return null;
|
|
63
|
+
return {
|
|
64
|
+
p75: scaleCruxValue(vital, raw.percentile),
|
|
65
|
+
category: toCategory(raw.category),
|
|
66
|
+
distribution: toDistribution(raw.distributions),
|
|
67
|
+
source,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Merge URL-level and origin-level CrUX into one metric set.
|
|
72
|
+
*
|
|
73
|
+
* Field availability is **per metric, not per URL** — a page can have enough
|
|
74
|
+
* traffic for CrUX to report CLS but not LCP. A real response in the Five
|
|
75
|
+
* Below batch carried two of the five metrics at URL level while the origin
|
|
76
|
+
* carried all five, which is why the manual audit shows "No field data" for
|
|
77
|
+
* one metric and a category for another on the same row.
|
|
78
|
+
*
|
|
79
|
+
* So the fallback runs per metric, and each metric records where it came from.
|
|
80
|
+
* A single response-level `field_source` flag would have to either discard the
|
|
81
|
+
* URL-level metrics that do exist or silently label origin data as page data,
|
|
82
|
+
* and origin CrUX for a homepage says nothing about a checkout page.
|
|
83
|
+
*/
|
|
84
|
+
export function parseCrux(urlLevel, originLevel, originFallback) {
|
|
85
|
+
const metrics = {};
|
|
86
|
+
let fromUrl = 0;
|
|
87
|
+
let fromOrigin = 0;
|
|
88
|
+
/**
|
|
89
|
+
* PSI performs its own origin fallback, and does it silently.
|
|
90
|
+
*
|
|
91
|
+
* When a URL has too little traffic, `loadingExperience` comes back populated
|
|
92
|
+
* with **origin-level** numbers and the only signal is its `id`, which holds
|
|
93
|
+
* the origin instead of the URL. Trusting the block's position in the
|
|
94
|
+
* response therefore labels origin data as page data: two different paginated
|
|
95
|
+
* URLs came back with byte-identical p75 values, both marked `source: "url"`.
|
|
96
|
+
*
|
|
97
|
+
* Comparing the two blocks' ids catches it without needing the requested URL.
|
|
98
|
+
*/
|
|
99
|
+
const psiSubstitutedOrigin = Boolean(urlLevel?.id) && Boolean(originLevel?.id) && urlLevel?.id === originLevel?.id;
|
|
100
|
+
const urlLevelSource = psiSubstitutedOrigin ? "origin" : "url";
|
|
101
|
+
for (const [cruxKey, vital] of Object.entries(CRUX_KEYS)) {
|
|
102
|
+
const atUrl = readMetric(urlLevel?.metrics?.[cruxKey], vital, urlLevelSource);
|
|
103
|
+
if (atUrl) {
|
|
104
|
+
metrics[vital] = atUrl;
|
|
105
|
+
if (urlLevelSource === "url")
|
|
106
|
+
fromUrl++;
|
|
107
|
+
else
|
|
108
|
+
fromOrigin++;
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (!originFallback)
|
|
112
|
+
continue;
|
|
113
|
+
const atOrigin = readMetric(originLevel?.metrics?.[cruxKey], vital, "origin");
|
|
114
|
+
if (atOrigin) {
|
|
115
|
+
metrics[vital] = atOrigin;
|
|
116
|
+
fromOrigin++;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (fromUrl === 0 && fromOrigin === 0)
|
|
120
|
+
return null;
|
|
121
|
+
// Only trust the URL-level verdict when the URL actually supplied data;
|
|
122
|
+
// otherwise the origin's verdict is the one describing these numbers.
|
|
123
|
+
const overallRaw = fromUrl > 0 ? urlLevel?.overall_category : originLevel?.overall_category;
|
|
124
|
+
return {
|
|
125
|
+
collectionPeriodDays: 28,
|
|
126
|
+
overallCategory: overallRaw ? toCategory(overallRaw) : null,
|
|
127
|
+
primarySource: fromUrl >= fromOrigin ? "url" : "origin",
|
|
128
|
+
metrics,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Lab metrics as numbers.
|
|
133
|
+
*
|
|
134
|
+
* `numericValue`, never `displayValue`. The Five Below CSV recorded TTFB as
|
|
135
|
+
* "Root document took 930 ms" — a localised string containing a non-breaking
|
|
136
|
+
* space — because it read displayValue, which made the whole column
|
|
137
|
+
* unaggregatable while `numericValue: 929` sat in the same audit object.
|
|
138
|
+
* Formatting is a report-time concern; nothing upstream of the report should
|
|
139
|
+
* hold a number as text.
|
|
140
|
+
*/
|
|
141
|
+
function parseLabMetrics(auditsJson) {
|
|
142
|
+
const audits = JSON.parse(auditsJson);
|
|
143
|
+
const num = (id) => {
|
|
144
|
+
const v = audits.audits?.[id]?.numericValue;
|
|
145
|
+
return typeof v === "number" ? v : null;
|
|
146
|
+
};
|
|
147
|
+
const ms = (id) => {
|
|
148
|
+
const v = num(id);
|
|
149
|
+
return v === null ? null : Math.round(v);
|
|
150
|
+
};
|
|
151
|
+
return {
|
|
152
|
+
lcpMs: ms("largest-contentful-paint"),
|
|
153
|
+
fcpMs: ms("first-contentful-paint"),
|
|
154
|
+
// CLS is unitless and small; rounding it to an integer would destroy it.
|
|
155
|
+
cls: num("cumulative-layout-shift"),
|
|
156
|
+
tbtMs: ms("total-blocking-time"),
|
|
157
|
+
speedIndexMs: ms("speed-index"),
|
|
158
|
+
ttiMs: ms("interactive"),
|
|
159
|
+
ttfbMs: ms("server-response-time"),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Parse a full PSI v5 response.
|
|
164
|
+
*
|
|
165
|
+
* Throws `PsiRuntimeError` when Lighthouse itself failed inside a 200
|
|
166
|
+
* response. PSI does this routinely — a page that times out or refuses the
|
|
167
|
+
* fetch comes back as HTTP 200 with `lighthouseResult.runtimeError` set and
|
|
168
|
+
* every score null. Parsing it anyway records a real page as scoring zero
|
|
169
|
+
* across the board, which is worse than a failed run, because a zero looks
|
|
170
|
+
* like data. Callers should treat this as retryable.
|
|
171
|
+
*/
|
|
172
|
+
export function parsePsiResponse(rawJson, options) {
|
|
173
|
+
const { strategy, originFallback = true } = options;
|
|
174
|
+
const response = JSON.parse(rawJson);
|
|
175
|
+
const lhr = response.lighthouseResult;
|
|
176
|
+
if (!lhr) {
|
|
177
|
+
throw new PsiRuntimeError("NO_LIGHTHOUSE_RESULT", "PSI response contained no lighthouseResult.");
|
|
178
|
+
}
|
|
179
|
+
if (lhr.runtimeError?.code) {
|
|
180
|
+
throw new PsiRuntimeError(lhr.runtimeError.code, lhr.runtimeError.message ?? "Lighthouse reported a runtime error.");
|
|
181
|
+
}
|
|
182
|
+
// parseLighthouseJSON takes the serialised LHR. Re-serialising the object we
|
|
183
|
+
// already hold is a little wasteful, but it keeps the Phase 2-13 parser and
|
|
184
|
+
// its priority mapping untouched, which is worth more than the milliseconds.
|
|
185
|
+
const lhrJson = JSON.stringify(lhr);
|
|
186
|
+
const lighthouse = parseLighthouseJSON(lhrJson);
|
|
187
|
+
return {
|
|
188
|
+
requestedUrl: lhr.requestedUrl ?? "",
|
|
189
|
+
finalUrl: lhr.finalDisplayedUrl ?? lhr.finalUrl ?? lhr.requestedUrl ?? "",
|
|
190
|
+
strategy,
|
|
191
|
+
fetchTime: lhr.fetchTime ?? null,
|
|
192
|
+
lighthouseVersion: lhr.lighthouseVersion ?? null,
|
|
193
|
+
scores: lighthouse.categoryScores,
|
|
194
|
+
lab: parseLabMetrics(lhrJson),
|
|
195
|
+
field: parseCrux(response.loadingExperience, response.originLoadingExperience, originFallback),
|
|
196
|
+
lighthouse,
|
|
197
|
+
runWarnings: lhr.runWarnings ?? [],
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
//# sourceMappingURL=psiParser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"psiParser.js","sourceRoot":"","sources":["../../src/utils/psiParser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,mBAAmB,EAAyB,MAAM,oBAAoB,CAAC;AAgEhF,yEAAyE;AACzE,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAE7B;IADX,YACW,IAAY,EACrB,OAAe;QAEf,KAAK,CAAC,OAAO,CAAC,CAAC;QAHN,SAAI,GAAJ,IAAI,CAAQ;QAIrB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AA+BD;;;;;;;GAOG;AACH,MAAM,SAAS,GAA6B;IAC1C,2BAA2B,EAAE,KAAK;IAClC,yBAAyB,EAAE,KAAK;IAChC,6BAA6B,EAAE,KAAK;IACpC,yBAAyB,EAAE,KAAK;IAChC,+BAA+B,EAAE,MAAM;CACxC,CAAC;AAEF;;;;;;GAMG;AACH,SAAS,cAAc,CAAC,KAAe,EAAE,UAAkB;IACzD,OAAO,KAAK,KAAK,KAAK,CAAC,CAAC,CAAC,UAAU,GAAG,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC;AACzD,CAAC;AAED,SAAS,UAAU,CAAC,GAAuB;IACzC,OAAO,GAAG,KAAK,MAAM,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AAC9E,CAAC;AAED,SAAS,cAAc,CACrB,GAAmC;IAEnC,MAAM,KAAK,GAAG,CAAC,CAAS,EAAU,EAAE;QAClC,MAAM,CAAC,GA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