@relyon/kit 0.0.0-stage → 1.0.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/AGENTS.md +75 -0
- package/CHANGELOG.md +1053 -0
- package/LICENSE +21 -0
- package/QUICKSTART.md +239 -0
- package/README.md +117 -2
- package/dist/adapters/express.d.ts +39 -0
- package/dist/adapters/express.js +106 -0
- package/dist/adapters/hono.d.ts +16 -0
- package/dist/adapters/hono.js +15 -0
- package/dist/adapters/next.d.ts +29 -0
- package/dist/adapters/next.js +38 -0
- package/dist/agent-card.d.ts +14 -0
- package/dist/agent-card.js +58 -0
- package/dist/agent-docs.d.ts +36 -0
- package/dist/agent-docs.js +78 -0
- package/dist/agent-hooks.d.ts +66 -0
- package/dist/agent-hooks.js +280 -0
- package/dist/agents.d.ts +24 -0
- package/dist/agents.js +120 -0
- package/dist/api-key.d.ts +6 -0
- package/dist/api-key.js +33 -0
- package/dist/cache.d.ts +46 -0
- package/dist/cache.js +86 -0
- package/dist/cart-tokens.d.ts +31 -0
- package/dist/cart-tokens.js +58 -0
- package/dist/cart-transfer.d.ts +95 -0
- package/dist/cart-transfer.js +387 -0
- package/dist/catalog-enrichment.d.ts +6 -0
- package/dist/catalog-enrichment.js +71 -0
- package/dist/catalog-reads.d.ts +126 -0
- package/dist/catalog-reads.js +104 -0
- package/dist/catalog.d.ts +78 -0
- package/dist/catalog.js +288 -0
- package/dist/charge-claims.d.ts +11 -0
- package/dist/charge-claims.js +47 -0
- package/dist/check.d.ts +44 -0
- package/dist/check.js +152 -0
- package/dist/checklist-manifest.d.ts +72 -0
- package/dist/checklist-manifest.js +341 -0
- package/dist/checkout.d.ts +222 -0
- package/dist/checkout.js +353 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +727 -0
- package/dist/confirmation.d.ts +78 -0
- package/dist/confirmation.js +124 -0
- package/dist/context.d.ts +81 -0
- package/dist/context.js +10 -0
- package/dist/discovery.d.ts +151 -0
- package/dist/discovery.js +346 -0
- package/dist/doctor-cart-transfer.d.ts +33 -0
- package/dist/doctor-cart-transfer.js +186 -0
- package/dist/doctor-catalog.d.ts +8 -0
- package/dist/doctor-catalog.js +47 -0
- package/dist/doctor-evidence.d.ts +22 -0
- package/dist/doctor-evidence.js +57 -0
- package/dist/doctor-merchant.d.ts +48 -0
- package/dist/doctor-merchant.js +74 -0
- package/dist/doctor-source.d.ts +5 -0
- package/dist/doctor-source.js +142 -0
- package/dist/doctor-variants.d.ts +70 -0
- package/dist/doctor-variants.js +102 -0
- package/dist/doctor.d.ts +215 -0
- package/dist/doctor.js +1689 -0
- package/dist/events.d.ts +95 -0
- package/dist/events.js +200 -0
- package/dist/feed-mapping.d.ts +114 -0
- package/dist/feed-mapping.js +152 -0
- package/dist/feed-values.d.ts +13 -0
- package/dist/feed-values.js +40 -0
- package/dist/feeds.d.ts +104 -0
- package/dist/feeds.js +366 -0
- package/dist/identify.d.ts +102 -0
- package/dist/identify.js +124 -0
- package/dist/identity.d.ts +37 -0
- package/dist/identity.js +54 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +41 -0
- package/dist/init.d.ts +157 -0
- package/dist/init.js +912 -0
- package/dist/kit-config.d.ts +19 -0
- package/dist/kit-config.js +43 -0
- package/dist/kit-environment.d.ts +20 -0
- package/dist/kit-environment.js +32 -0
- package/dist/kit-updates.d.ts +47 -0
- package/dist/kit-updates.js +77 -0
- package/dist/kit-version-project.d.ts +18 -0
- package/dist/kit-version-project.js +214 -0
- package/dist/kit-version.d.ts +37 -0
- package/dist/kit-version.js +162 -0
- package/dist/kit.d.ts +516 -0
- package/dist/kit.js +1309 -0
- package/dist/layers/pages.d.ts +3 -0
- package/dist/layers/pages.js +5 -0
- package/dist/layers/payments.d.ts +7 -0
- package/dist/layers/payments.js +9 -0
- package/dist/layers/products.d.ts +10 -0
- package/dist/layers/products.js +13 -0
- package/dist/live.d.ts +13 -0
- package/dist/live.js +52 -0
- package/dist/mcp.d.ts +270 -0
- package/dist/mcp.js +312 -0
- package/dist/next-steps.d.ts +101 -0
- package/dist/next-steps.js +257 -0
- package/dist/order-pending.d.ts +57 -0
- package/dist/order-pending.js +127 -0
- package/dist/orders.d.ts +12 -0
- package/dist/orders.js +21 -0
- package/dist/payments-init.d.ts +19 -0
- package/dist/payments-init.js +70 -0
- package/dist/payments-routes.d.ts +54 -0
- package/dist/payments-routes.js +123 -0
- package/dist/payments.d.ts +165 -0
- package/dist/payments.js +192 -0
- package/dist/pnpm-workspace.d.ts +58 -0
- package/dist/pnpm-workspace.js +250 -0
- package/dist/policies.d.ts +111 -0
- package/dist/policies.js +317 -0
- package/dist/preview-hosts.d.ts +64 -0
- package/dist/preview-hosts.js +175 -0
- package/dist/private-config.d.ts +169 -0
- package/dist/private-config.js +820 -0
- package/dist/progress.d.ts +54 -0
- package/dist/progress.js +153 -0
- package/dist/protocol-inputs.d.ts +22 -0
- package/dist/protocol-inputs.js +151 -0
- package/dist/public-origin.d.ts +50 -0
- package/dist/public-origin.js +180 -0
- package/dist/refusals.d.ts +86 -0
- package/dist/refusals.js +157 -0
- package/dist/relay.d.ts +107 -0
- package/dist/relay.js +655 -0
- package/dist/release-notes.d.ts +2 -0
- package/dist/release-notes.js +290 -0
- package/dist/relyon-cart-tokens.d.ts +101 -0
- package/dist/relyon-cart-tokens.js +521 -0
- package/dist/relyon.d.ts +141 -0
- package/dist/relyon.js +238 -0
- package/dist/robots.d.ts +27 -0
- package/dist/robots.js +127 -0
- package/dist/rules.d.ts +144 -0
- package/dist/rules.js +347 -0
- package/dist/sdk/generated/public-api.d.ts +4816 -0
- package/dist/sdk/generated/public-api.js +1 -0
- package/dist/sdk/index.d.ts +24 -0
- package/dist/sdk/index.js +34 -0
- package/dist/sdk/signatures.d.ts +17 -0
- package/dist/sdk/signatures.js +33 -0
- package/dist/setup.d.ts +37 -0
- package/dist/setup.js +94 -0
- package/dist/signatures.d.ts +112 -0
- package/dist/signatures.js +443 -0
- package/dist/state.d.ts +185 -0
- package/dist/state.js +96 -0
- package/dist/store-payments.d.ts +48 -0
- package/dist/store-payments.js +143 -0
- package/dist/structured-fields.d.ts +26 -0
- package/dist/structured-fields.js +200 -0
- package/dist/templates.d.ts +19 -0
- package/dist/templates.js +193 -0
- package/dist/test-checkout-command.d.ts +32 -0
- package/dist/test-checkout-command.js +161 -0
- package/dist/test-checkout.d.ts +88 -0
- package/dist/test-checkout.js +292 -0
- package/dist/types.d.ts +103 -0
- package/dist/types.js +18 -0
- package/dist/ucp-versions.d.ts +57 -0
- package/dist/ucp-versions.js +129 -0
- package/dist/util.d.ts +23 -0
- package/dist/util.js +68 -0
- package/dist/variants.d.ts +67 -0
- package/dist/variants.js +146 -0
- package/dist/version.d.ts +9 -0
- package/dist/version.js +10 -0
- package/dist/waiting.d.ts +43 -0
- package/dist/waiting.js +57 -0
- package/dist/webhook-command.d.ts +48 -0
- package/dist/webhook-command.js +367 -0
- package/dist/webhook-registration.d.ts +47 -0
- package/dist/webhook-registration.js +69 -0
- package/dist/webmcp-schema.d.ts +14 -0
- package/dist/webmcp-schema.js +140 -0
- package/dist/webmcp-script.d.ts +3 -0
- package/dist/webmcp-script.js +176 -0
- package/dist/webmcp-store.d.ts +102 -0
- package/dist/webmcp-store.js +116 -0
- package/dist/webmcp.d.ts +242 -0
- package/dist/webmcp.js +866 -0
- package/dist/wire-names.d.ts +85 -0
- package/dist/wire-names.js +109 -0
- package/package.json +118 -4
- package/release-notes.json +290 -0
- package/scripts/install-notice.cjs +7 -0
- package/skills/relyon-agents/SKILL.md +327 -0
- package/skills/relyon-catalog/SKILL.md +499 -0
- package/skills/relyon-catalog/references/example-store.md +218 -0
- package/skills/relyon-checkout/SKILL.md +603 -0
- package/skills/relyon-checkout/references/checkout-flow.md +257 -0
- package/skills/relyon-checkout/references/checkout-page.md +315 -0
- package/skills/relyon-go-live/SKILL.md +344 -0
- package/skills/relyon-orders/SKILL.md +191 -0
- package/skills/relyon-orders/references/webhook-receiver.md +113 -0
- package/skills/relyon-payments/SKILL.md +117 -0
- package/skills/relyon-setup/SKILL.md +1061 -0
- package/skills/relyon-setup/references/QUICKSTART.md +239 -0
- package/skills/relyon-setup/references/durable-state.md +209 -0
- package/skills/relyon-setup/references/express-server.md +388 -0
- package/skills/relyon-setup/references/next-app-router.md +251 -0
- package/templates/common/STORE-README.md +67 -0
- package/templates/common/gitignore +8 -0
- package/templates/common/scripts/check-pricing.ts +22 -0
- package/templates/common/scripts/serve.mjs +13 -0
- package/templates/common/server/catalog.ts +79 -0
- package/templates/common/server/checkout-flow.d.ts +34 -0
- package/templates/common/server/pages.ts +184 -0
- package/templates/common/server/storage.ts +114 -0
- package/templates/common/server/store.ts +67 -0
- package/templates/express/server/express.ts +20 -0
- package/templates/express/tsconfig.build.json +6 -0
- package/templates/express/tsconfig.json +9 -0
- package/templates/next/app/[[...path]]/route.ts +24 -0
- package/templates/next/next.config.mjs +19 -0
- package/templates/next/tsconfig.json +12 -0
- package/templates/payments/express.ts +39 -0
- package/templates/payments/next.ts +38 -0
- package/templates/payments/node.ts +39 -0
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { DOMAINS_PATH, UCP_AGENT_PROFILE_PATH } from "./wire-names.js";
|
|
2
|
+
import { merchantReturnPolicy, policyLlmsLines } from "./policies.js";
|
|
3
|
+
import { maxAgeSeconds, StaleIfErrorCache } from "./cache.js";
|
|
4
|
+
import { RelyonProblemError, RelyonUnavailableError } from "./relyon.js";
|
|
5
|
+
import { refusal } from "./refusals.js";
|
|
6
|
+
import { escapeHtml, escapeXml, isRecord, textResponse } from "./util.js";
|
|
7
|
+
import { profileHostname } from "./preview-hosts.js";
|
|
8
|
+
import { bindAddressOf, onPublicOrigin, publishedUrl } from "./public-origin.js";
|
|
9
|
+
const DEFAULT_PROFILE_TTL_SECONDS = 300;
|
|
10
|
+
const DOMAINS_TTL_MS = 60_000;
|
|
11
|
+
/** A hostname missing from the cached list re-reads it, at most this often (a domain added on another server). */
|
|
12
|
+
const DOMAINS_MISS_REFRESH_MS = 5_000;
|
|
13
|
+
/** After a failed domain-list read, no new read is sent for this long (a 429: DOMAINS_TTL_MS). */
|
|
14
|
+
const DOMAINS_FAILURE_BACKOFF_MS = 5_000;
|
|
15
|
+
function isUcpProfile(data) {
|
|
16
|
+
return isRecord(data) && isRecord(data.ucp) && typeof data.ucp.version === "string"
|
|
17
|
+
&& /^\d{4}-\d{2}-\d{2}$/.test(data.ucp.version) && isRecord(data.ucp.services) && isRecord(data.ucp.capabilities)
|
|
18
|
+
&& (data.keys === undefined || (Array.isArray(data.keys) && data.keys.every((key) => isRecord(key) && typeof key.kid === "string" && typeof key.kty === "string")));
|
|
19
|
+
}
|
|
20
|
+
function isDomainList(data) {
|
|
21
|
+
return isRecord(data) && Array.isArray(data.data) && typeof data.has_more === "boolean" && data.data.every(isRecord);
|
|
22
|
+
}
|
|
23
|
+
/** `/.well-known/ucp` and `/.well-known/relyon-verification`, from cached Relyon reads. */
|
|
24
|
+
export class DiscoveryReads {
|
|
25
|
+
now;
|
|
26
|
+
profiles;
|
|
27
|
+
domains;
|
|
28
|
+
lastMissRefresh = Number.NEGATIVE_INFINITY;
|
|
29
|
+
domainsFailure;
|
|
30
|
+
constructor(gateway, now) {
|
|
31
|
+
this.now = now;
|
|
32
|
+
this.profiles = new StaleIfErrorCache(async (hostname) => {
|
|
33
|
+
const { data, response } = await gateway.call((client) => client.GET(UCP_AGENT_PROFILE_PATH, { params: { query: { hostname } } }), isUcpProfile);
|
|
34
|
+
const maxAge = maxAgeSeconds(response.headers.get("cache-control")) ?? DEFAULT_PROFILE_TTL_SECONDS;
|
|
35
|
+
return { value: data, ttlMs: maxAge * 1000 };
|
|
36
|
+
}, (hostname) => hostname.toLowerCase(), now);
|
|
37
|
+
// Every list read is gated after a failure (Relyon down, a refused key,
|
|
38
|
+
// 429), so public verification-file requests can't turn into one Relyon
|
|
39
|
+
// request each; the cache still serves its last good copy meanwhile.
|
|
40
|
+
this.domains = new StaleIfErrorCache(async () => {
|
|
41
|
+
const failure = this.domainsFailure;
|
|
42
|
+
if (failure && now() - failure.at < failure.backoffMs)
|
|
43
|
+
throw failure.error;
|
|
44
|
+
try {
|
|
45
|
+
const loaded = await loadDomains();
|
|
46
|
+
this.domainsFailure = undefined;
|
|
47
|
+
return loaded;
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
const status = error instanceof RelyonProblemError || error instanceof RelyonUnavailableError ? error.status : undefined;
|
|
51
|
+
this.domainsFailure = { at: now(), backoffMs: status === 429 ? DOMAINS_TTL_MS : DOMAINS_FAILURE_BACKOFF_MS, error };
|
|
52
|
+
throw error;
|
|
53
|
+
}
|
|
54
|
+
}, () => "all", now);
|
|
55
|
+
const loadDomains = async () => {
|
|
56
|
+
const all = [];
|
|
57
|
+
let startingAfter;
|
|
58
|
+
for (let page = 0; page < 50; page += 1) {
|
|
59
|
+
const { data } = await gateway.call((client) => client.GET(DOMAINS_PATH, { params: { query: { limit: 100, ...(startingAfter ? { starting_after: startingAfter } : {}) } } }), isDomainList);
|
|
60
|
+
for (const domain of data.data) {
|
|
61
|
+
const verification = isRecord(domain.verification) ? domain.verification : {};
|
|
62
|
+
if (typeof domain.hostname !== "string" || typeof domain.status !== "string")
|
|
63
|
+
throw new RelyonUnavailableError("malformed", "A domain in Relyon's list is malformed.");
|
|
64
|
+
all.push({
|
|
65
|
+
hostname: domain.hostname.toLowerCase(), status: domain.status,
|
|
66
|
+
record_value: typeof verification.record_value === "string" ? verification.record_value : null,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
const last = data.data[data.data.length - 1];
|
|
70
|
+
if (!data.has_more || !last || typeof last.id !== "string")
|
|
71
|
+
break;
|
|
72
|
+
startingAfter = last.id;
|
|
73
|
+
}
|
|
74
|
+
return { value: all, ttlMs: DOMAINS_TTL_MS };
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** This store just changed its domains through Relyon: the next read fetches the list, even during a failure backoff. */
|
|
78
|
+
domainsChanged() {
|
|
79
|
+
this.domainsFailure = undefined;
|
|
80
|
+
this.domains.expire();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The store's UCP profile for one of its hostnames. `origin` (the store's
|
|
84
|
+
* public address for the request) replaces the profile's
|
|
85
|
+
* `https://<hostname>` endpoints when it differs, such as a Test loopback
|
|
86
|
+
* store on plain http with a port; otherwise the profile is served unchanged.
|
|
87
|
+
*/
|
|
88
|
+
async ucpProfile(hostname, origin) {
|
|
89
|
+
try {
|
|
90
|
+
const queried = profileHostname(hostname);
|
|
91
|
+
const read = await this.profiles.read(queried);
|
|
92
|
+
const value = origin && origin !== `https://${queried}` ? withOrigin(read.value, `https://${queried}`, origin) : read.value;
|
|
93
|
+
return new Response(JSON.stringify(value), {
|
|
94
|
+
status: 200,
|
|
95
|
+
headers: {
|
|
96
|
+
"content-type": "application/json",
|
|
97
|
+
"cache-control": `public, max-age=${read.freshForSeconds}`,
|
|
98
|
+
"access-control-allow-origin": "*",
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
if (error instanceof RelyonUnavailableError)
|
|
104
|
+
return refusal("ucp", "service_unavailable", { message: "The store profile is temporarily unavailable." });
|
|
105
|
+
if (error instanceof RelyonProblemError) {
|
|
106
|
+
if (error.status === 404 || error.code === "domain_not_verified" || error.code === "not_found") {
|
|
107
|
+
return refusal("ucp", error.code === "domain_not_verified" ? "domain_not_verified" : "not_found", {
|
|
108
|
+
status: 404, message: "This store has no verified UCP profile on this hostname.",
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
return refusal("ucp", "internal_error", { status: 500 });
|
|
112
|
+
}
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The verification value of the matching domain (pending, verified or
|
|
118
|
+
* failed: a failed domain can be verified again), as text/plain with no
|
|
119
|
+
* trailing newline. This is the body Relyon's file check accepts
|
|
120
|
+
* (`relyon-domain-verification=<token>`). The contract itself still defines
|
|
121
|
+
* no file format. `hostname` is always one of the store's own hostnames.
|
|
122
|
+
*/
|
|
123
|
+
async verification(hostname) {
|
|
124
|
+
const wanted = hostname.toLowerCase();
|
|
125
|
+
const find = (domains) => domains.find((domain) => domain.hostname === wanted
|
|
126
|
+
&& (domain.status === "verified" || domain.status === "pending" || domain.status === "failed") && domain.record_value);
|
|
127
|
+
try {
|
|
128
|
+
let match = find((await this.domains.read("all")).value);
|
|
129
|
+
// A domain added since the list was cached (for example by another server) is picked up at once.
|
|
130
|
+
if (!match && this.now() - this.lastMissRefresh >= DOMAINS_MISS_REFRESH_MS) {
|
|
131
|
+
this.lastMissRefresh = this.now();
|
|
132
|
+
this.domains.expire();
|
|
133
|
+
match = find((await this.domains.read("all")).value);
|
|
134
|
+
}
|
|
135
|
+
if (!match)
|
|
136
|
+
return textResponse(404, "Not found\n", "text/plain; charset=utf-8", { "cache-control": "no-store" });
|
|
137
|
+
return textResponse(200, match.record_value, "text/plain; charset=utf-8", { "cache-control": "no-store" });
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
if (error instanceof RelyonUnavailableError)
|
|
141
|
+
return refusal("plain", "service_unavailable");
|
|
142
|
+
if (error instanceof RelyonProblemError)
|
|
143
|
+
return refusal("plain", "internal_error", { status: 500 });
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const CURRENCY_DIGITS = new Map();
|
|
149
|
+
function currencyDigits(currency) {
|
|
150
|
+
let digits = CURRENCY_DIGITS.get(currency);
|
|
151
|
+
if (digits === undefined) {
|
|
152
|
+
try {
|
|
153
|
+
digits = new Intl.NumberFormat("en", { style: "currency", currency }).resolvedOptions().maximumFractionDigits ?? 2;
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
digits = 2;
|
|
157
|
+
}
|
|
158
|
+
CURRENCY_DIGITS.set(currency, digits);
|
|
159
|
+
}
|
|
160
|
+
return digits;
|
|
161
|
+
}
|
|
162
|
+
/** Minor units → a decimal string, e.g. 2400 USD → "24.00", 2400 JPY → "2400". */
|
|
163
|
+
export function decimalAmount(amount, currency) {
|
|
164
|
+
const digits = currencyDigits(currency);
|
|
165
|
+
return (amount / 10 ** digits).toFixed(digits);
|
|
166
|
+
}
|
|
167
|
+
const AVAILABILITY = {
|
|
168
|
+
in_stock: "https://schema.org/InStock",
|
|
169
|
+
out_of_stock: "https://schema.org/OutOfStock",
|
|
170
|
+
pre_order: "https://schema.org/PreOrder",
|
|
171
|
+
backorder: "https://schema.org/BackOrder",
|
|
172
|
+
};
|
|
173
|
+
const AVAILABILITY_TEXT = {
|
|
174
|
+
in_stock: "In stock", out_of_stock: "Out of stock", pre_order: "Pre-order", backorder: "Backorder",
|
|
175
|
+
};
|
|
176
|
+
/** What a feed URL is, as llms.txt and the homepage head name it. */
|
|
177
|
+
export function feedLabel(url) {
|
|
178
|
+
const path = (() => { try {
|
|
179
|
+
return new URL(url).pathname;
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return url;
|
|
183
|
+
} })();
|
|
184
|
+
if (/\.jsonl(?:\.gz)?$/i.test(path))
|
|
185
|
+
return `ChatGPT product feed (OpenAI format${path.endsWith(".gz") ? ", gzip" : ""})`;
|
|
186
|
+
if (/google/i.test(path))
|
|
187
|
+
return "Google product feed (Merchant Center RSS)";
|
|
188
|
+
if (/microsoft/i.test(path))
|
|
189
|
+
return "Microsoft product feed (Merchant Center RSS)";
|
|
190
|
+
return "Product feed";
|
|
191
|
+
}
|
|
192
|
+
export function llmsTxt(store, origin, catalog, paths, feedUrls = [], policies, extras = {}) {
|
|
193
|
+
const lines = [`# ${store.name}`, ""];
|
|
194
|
+
if (store.description)
|
|
195
|
+
lines.push(`> ${store.description}`, "");
|
|
196
|
+
lines.push(...(extras.waitingForSandboxKey
|
|
197
|
+
? ["Shopping agents can browse this store. Checkout status: waiting_for_sandbox_key (checkout is still being set up)."]
|
|
198
|
+
: [
|
|
199
|
+
"Shopping agents can browse and check out on this store directly.",
|
|
200
|
+
"Checkout needs this store's agent credential (Authorization: Bearer agt_…). The merchant issues one in the Relyon dashboard under AI agents → Credentials.",
|
|
201
|
+
]), "", `- UCP business profile: ${origin}/.well-known/ucp`, `- MCP server: ${origin}${paths.mcp}`, ...(extras.acp && !extras.waitingForSandboxKey ? [`- ACP checkout (Agentic Commerce Protocol): ${origin}${extras.acp}/checkout_sessions`] : []), ...(extras.agentCard ? [`- Agent card: ${origin}/.well-known/agent-card.json`] : []), `- Product sitemap: ${origin}${paths.sitemap}`, ...feedUrls.map(url => `- ${feedLabel(url)}: ${url}`), "", "## Products", "");
|
|
202
|
+
const rebase = extras.rebase;
|
|
203
|
+
for (const listed of catalog.all()) {
|
|
204
|
+
const product = rebase ? onPublicOrigin(listed, origin, rebase.hostnames) : listed;
|
|
205
|
+
const label = product.url ? `[${product.title}](${product.url})` : product.title;
|
|
206
|
+
const facts = [];
|
|
207
|
+
if (product.from_price)
|
|
208
|
+
facts.push(`from ${decimalAmount(product.from_price.amount, product.from_price.currency)} ${product.from_price.currency}`);
|
|
209
|
+
if (product.availability)
|
|
210
|
+
facts.push(AVAILABILITY_TEXT[product.availability] ?? product.availability);
|
|
211
|
+
lines.push(`- ${label}${facts.length ? `: ${facts.join(", ")}` : ""}`);
|
|
212
|
+
}
|
|
213
|
+
if (catalog.all().length === 0)
|
|
214
|
+
lines.push("No products are listed yet.");
|
|
215
|
+
lines.push(...policyLlmsLines(rebase ? policiesOnOrigin(policies, origin, rebase.hostnames) : policies));
|
|
216
|
+
return `${lines.join("\n")}\n`;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* The store's root sitemap index (`/sitemap.xml`): it lists the kit's product
|
|
220
|
+
* sitemap, so a crawler that only knows `/sitemap.xml` finds the products.
|
|
221
|
+
*/
|
|
222
|
+
export function sitemapIndex(origin, sitemapPaths) {
|
|
223
|
+
const entries = sitemapPaths.map((path) => ` <sitemap><loc>${escapeXml(new URL(path, origin).href)}</loc></sitemap>`);
|
|
224
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${entries.join("\n")}${entries.length ? "\n" : ""}</sitemapindex>\n`;
|
|
225
|
+
}
|
|
226
|
+
/** A copy of a JSON value with every string that starts with `from` (then `/` or nothing) moved onto `to`. */
|
|
227
|
+
function withOrigin(value, from, to, depth = 0) {
|
|
228
|
+
if (depth > 32)
|
|
229
|
+
return value;
|
|
230
|
+
if (typeof value === "string")
|
|
231
|
+
return value === from || value.startsWith(`${from}/`) ? to + value.slice(from.length) : value;
|
|
232
|
+
if (Array.isArray(value))
|
|
233
|
+
return value.map((entry) => withOrigin(entry, from, to, depth + 1));
|
|
234
|
+
if (isRecord(value))
|
|
235
|
+
return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, withOrigin(entry, from, to, depth + 1)]));
|
|
236
|
+
return value;
|
|
237
|
+
}
|
|
238
|
+
/** Policy page links on the store's own hostnames, moved onto `origin`. */
|
|
239
|
+
export function policiesOnOrigin(policies, origin, hostnames) {
|
|
240
|
+
if (!policies)
|
|
241
|
+
return policies;
|
|
242
|
+
const move = (url) => url === undefined ? undefined : publishedUrl(url, origin, hostnames);
|
|
243
|
+
const out = {};
|
|
244
|
+
for (const field of ["shippingUrl", "privacyUrl", "termsUrl", "contactUrl"]) {
|
|
245
|
+
const url = move(policies[field]);
|
|
246
|
+
if (url !== undefined)
|
|
247
|
+
out[field] = url;
|
|
248
|
+
}
|
|
249
|
+
if (policies.returns) {
|
|
250
|
+
const policyUrl = move(policies.returns.policyUrl);
|
|
251
|
+
out.returns = { ...policies.returns, ...(policyUrl !== undefined ? { policyUrl } : {}) };
|
|
252
|
+
}
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* The product pages as a sitemap `urlset`. With `rebase`, links on the
|
|
257
|
+
* store's own hostnames (and dev addresses) are moved onto `rebase.origin`.
|
|
258
|
+
*/
|
|
259
|
+
export function productSitemap(catalog, rebase) {
|
|
260
|
+
const lastmod = catalog.syncedAt ? `<lastmod>${new Date(catalog.syncedAt).toISOString().slice(0, 10)}</lastmod>` : "";
|
|
261
|
+
const seen = new Set();
|
|
262
|
+
const urls = catalog.all()
|
|
263
|
+
.map((product) => typeof product.url === "string" && product.url
|
|
264
|
+
? rebase ? publishedUrl(product.url, rebase.origin, rebase.hostnames) : bindAddressOf(product.url) ? undefined : product.url
|
|
265
|
+
: undefined)
|
|
266
|
+
.filter((url) => url !== undefined && !seen.has(url) && Boolean(seen.add(url)))
|
|
267
|
+
.map((url) => ` <url><loc>${escapeXml(url)}</loc>${lastmod}</url>`);
|
|
268
|
+
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls.join("\n")}${urls.length ? "\n" : ""}</urlset>\n`;
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* schema.org `Product` JSON-LD for a product page. Prices are display only
|
|
272
|
+
* ("from"). `gtin` and `mpn` appear only when the product has them. With
|
|
273
|
+
* `rebase`, page and image links on the store's own hostnames are moved onto
|
|
274
|
+
* `rebase.origin` (the store's public address).
|
|
275
|
+
*/
|
|
276
|
+
export function productJsonLd(product, policies, rebase) {
|
|
277
|
+
if (rebase) {
|
|
278
|
+
product = onPublicOrigin(product, rebase.origin, rebase.hostnames);
|
|
279
|
+
policies = policiesOnOrigin(policies, rebase.origin, rebase.hostnames);
|
|
280
|
+
}
|
|
281
|
+
const data = {
|
|
282
|
+
"@context": "https://schema.org",
|
|
283
|
+
"@type": "Product",
|
|
284
|
+
name: product.title,
|
|
285
|
+
sku: product.merchant_product_id,
|
|
286
|
+
};
|
|
287
|
+
const returnPolicy = merchantReturnPolicy(policies?.returns);
|
|
288
|
+
if (product.description)
|
|
289
|
+
data.description = product.description;
|
|
290
|
+
if (product.images?.length)
|
|
291
|
+
data.image = product.images;
|
|
292
|
+
if (product.url)
|
|
293
|
+
data.url = product.url;
|
|
294
|
+
if (product.brand)
|
|
295
|
+
data.brand = { "@type": "Brand", name: product.brand };
|
|
296
|
+
if (product.gtin)
|
|
297
|
+
data.gtin = product.gtin;
|
|
298
|
+
if (product.mpn)
|
|
299
|
+
data.mpn = product.mpn;
|
|
300
|
+
if (product.category)
|
|
301
|
+
data.category = product.category;
|
|
302
|
+
if (product.group_id)
|
|
303
|
+
data.inProductGroupWithID = product.group_id;
|
|
304
|
+
if (product.from_price) {
|
|
305
|
+
data.offers = {
|
|
306
|
+
"@type": "Offer",
|
|
307
|
+
price: decimalAmount(product.from_price.amount, product.from_price.currency),
|
|
308
|
+
priceCurrency: product.from_price.currency,
|
|
309
|
+
...(product.availability ? { availability: AVAILABILITY[product.availability] } : {}),
|
|
310
|
+
...(product.url ? { url: product.url } : {}),
|
|
311
|
+
...(returnPolicy ? { hasMerchantReturnPolicy: returnPolicy } : {}),
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
return data;
|
|
315
|
+
}
|
|
316
|
+
/** A JSON value as a `<script type="application/ld+json">` tag, safe to place in HTML. */
|
|
317
|
+
export function jsonLdScript(value) {
|
|
318
|
+
const json = JSON.stringify(value).replace(/</g, "\\u003c").replace(/>/g, "\\u003e").replace(/&/g, "\\u0026")
|
|
319
|
+
.replace(/\u2028/g, "\\u2028").replace(/\u2029/g, "\\u2029");
|
|
320
|
+
return `<script type="application/ld+json">${json}</script>`;
|
|
321
|
+
}
|
|
322
|
+
/** The JSON-LD as a `<script type="application/ld+json">` tag, safe to place in HTML. */
|
|
323
|
+
export function renderProductJsonLd(product, policies, rebase) {
|
|
324
|
+
return jsonLdScript(productJsonLd(product, policies, rebase));
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* schema.org `ItemList` JSON-LD for a page that lists products (the
|
|
328
|
+
* homepage): each entry is the product's full `Product` data, with name,
|
|
329
|
+
* price, currency, availability, brand and `sku` (and `gtin` / `mpn` only
|
|
330
|
+
* when supplied). At most 100 products.
|
|
331
|
+
*/
|
|
332
|
+
export function productListJsonLd(products, policies, rebase) {
|
|
333
|
+
return {
|
|
334
|
+
"@context": "https://schema.org",
|
|
335
|
+
"@type": "ItemList",
|
|
336
|
+
itemListElement: products.slice(0, 100).map((product, index) => {
|
|
337
|
+
const { "@context": _context, ...item } = productJsonLd(product, policies, rebase);
|
|
338
|
+
return { "@type": "ListItem", position: index + 1, item };
|
|
339
|
+
}),
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
/** The homepage product list as a `<script type="application/ld+json">` tag. */
|
|
343
|
+
export function renderProductListJsonLd(products, policies, rebase) {
|
|
344
|
+
return jsonLdScript(productListJsonLd(products, policies, rebase));
|
|
345
|
+
}
|
|
346
|
+
export { escapeHtml };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type McpProductSummary } from "./doctor-variants.js";
|
|
2
|
+
/** What the doctor's request helper returns (a subset of its own type). */
|
|
3
|
+
export interface CartTransferFetched {
|
|
4
|
+
status: number;
|
|
5
|
+
type: string;
|
|
6
|
+
text: string;
|
|
7
|
+
location: string | null;
|
|
8
|
+
}
|
|
9
|
+
export type CartTransferGet = (path: string, init?: {
|
|
10
|
+
method?: string;
|
|
11
|
+
body?: string;
|
|
12
|
+
headers?: Record<string, string>;
|
|
13
|
+
}) => Promise<CartTransferFetched | Error>;
|
|
14
|
+
export interface CartTransferRow {
|
|
15
|
+
status: "PASS" | "FAIL" | "N/A" | "NOT DONE";
|
|
16
|
+
detail: string;
|
|
17
|
+
}
|
|
18
|
+
/** Test only: what doctor needs to transfer a real item and open the continue_url. */
|
|
19
|
+
export interface CartTransferLanding {
|
|
20
|
+
/** The store's agent-visible products (doctor reads them from its MCP search_products). */
|
|
21
|
+
products: () => Promise<readonly McpProductSummary[] | Error>;
|
|
22
|
+
/** The store's cart-transfer caller key (RELYON_CART_TRANSFER_KEY), sent only to the store's own /ucp/carts. */
|
|
23
|
+
callerKey?: string | undefined;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The cart-transfer row: whether the served profile advertises
|
|
27
|
+
* `dev.ucp.shopping.cart`, whether `POST /ucp/carts` is the kit's answer (a
|
|
28
|
+
* harmless empty cart the kit refuses before pricing anything; a caller-key
|
|
29
|
+
* refusal also counts as reachable) and whether the advertised UCP 2026-04-08
|
|
30
|
+
* profile is served and valid. Every request stays on the store's origin.
|
|
31
|
+
* `profile` is the store's served /.well-known/ucp body.
|
|
32
|
+
*/
|
|
33
|
+
export declare function cartTransferCheck(get: CartTransferGet, origin: string, profile: unknown, live: boolean, landing?: CartTransferLanding): Promise<CartTransferRow>;
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { CART_CAPABILITY, UCP_2026_04_08, UCP_2026_04_08_PROFILE_PATH, UCP_CURRENT_VERSION } from "./ucp-versions.js";
|
|
2
|
+
import { isRecord } from "./util.js";
|
|
3
|
+
import { printableUrl } from "./doctor.js";
|
|
4
|
+
import { CHECKOUT_MARKER_FIX, TRANSFER_ATTEMPTS, checkoutMarker, transferCandidates } from "./doctor-variants.js";
|
|
5
|
+
import { CHECKOUT_CART_ITEMS_ATTRIBUTE } from "./checkout.js";
|
|
6
|
+
const LANDING_FIX = "On the store's checkout page call kit.landing() with the page's request, URL or searchParams and show the landed cart (landing.inputs / landing.cart). See docs/cart-transfer.md in the store kit's source.";
|
|
7
|
+
const CART_TRANSFER_KEY_HEADER_NAME = "x-api-key";
|
|
8
|
+
const MOUNT_FIX = "Mount storeKitMiddleware (every kit route, including /ucp/carts and /.well-known/ucp/2026-04-08) before the app's own routes.";
|
|
9
|
+
const DOCS = "See docs/cart-transfer.md in the store kit's source.";
|
|
10
|
+
const jsonOf = (fetched) => { try {
|
|
11
|
+
return JSON.parse(fetched.text);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
return undefined;
|
|
15
|
+
} };
|
|
16
|
+
const sameOrigin = (value, origin) => { try {
|
|
17
|
+
return new URL(value).origin === origin;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return false;
|
|
21
|
+
} };
|
|
22
|
+
/** The kit's own cart-transfer answer to the doctor's empty probe: its invalid-request or caller-key refusal. */
|
|
23
|
+
function kitAnswer(fetched) {
|
|
24
|
+
const body = jsonOf(fetched);
|
|
25
|
+
if (!isRecord(body) || typeof body.code !== "string" || typeof body.content !== "string")
|
|
26
|
+
return null;
|
|
27
|
+
if (fetched.status === 400 && body.code === "invalid_request")
|
|
28
|
+
return "open";
|
|
29
|
+
if ((fetched.status === 401 && body.code === "unauthorized") || (fetched.status === 403 && body.code === "forbidden"))
|
|
30
|
+
return "key";
|
|
31
|
+
if (fetched.status === 404 && body.code === "not_found")
|
|
32
|
+
return "off";
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The cart-transfer row: whether the served profile advertises
|
|
37
|
+
* `dev.ucp.shopping.cart`, whether `POST /ucp/carts` is the kit's answer (a
|
|
38
|
+
* harmless empty cart the kit refuses before pricing anything; a caller-key
|
|
39
|
+
* refusal also counts as reachable) and whether the advertised UCP 2026-04-08
|
|
40
|
+
* profile is served and valid. Every request stays on the store's origin.
|
|
41
|
+
* `profile` is the store's served /.well-known/ucp body.
|
|
42
|
+
*/
|
|
43
|
+
export async function cartTransferCheck(get, origin, profile, live, landing) {
|
|
44
|
+
const ucp = isRecord(profile) && isRecord(profile.ucp) ? profile.ucp : undefined;
|
|
45
|
+
const capabilities = ucp && isRecord(ucp.capabilities) ? ucp.capabilities : {};
|
|
46
|
+
const advertised = Array.isArray(capabilities[CART_CAPABILITY]) && capabilities[CART_CAPABILITY].length > 0;
|
|
47
|
+
if (!advertised) {
|
|
48
|
+
if (ucp && ucp.version !== UCP_CURRENT_VERSION) {
|
|
49
|
+
return { status: "N/A", detail: `Not advertised: the store's profile is UCP ${typeof ucp.version === "string" ? ucp.version.slice(0, 20) : "(no version)"}; the kit advertises ${CART_CAPABILITY} only on Relyon's UCP ${UCP_CURRENT_VERSION} profile. ${DOCS}` };
|
|
50
|
+
}
|
|
51
|
+
// Nothing advertised, nothing probed: the doctor sends no POST a store didn't offer.
|
|
52
|
+
return {
|
|
53
|
+
status: "N/A",
|
|
54
|
+
detail: live
|
|
55
|
+
? `Not advertised. Live cart transfer needs a caller key: set RELYON_CART_TRANSFER_KEY (16+ characters) on the store's server, restart it, then give Google the same key for the X-API-Key header. ${DOCS}`
|
|
56
|
+
: `Not advertised: the kit's cartTransfer option is false, its key is shorter than 16 characters, or the profile's REST endpoint isn't ${origin}/ucp. Remove cartTransfer: false or fix the key to let Google transfer carts. ${DOCS}`,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// A harmless empty cart: the kit refuses it before reading or pricing anything.
|
|
60
|
+
const probe = await get("/ucp/carts", { method: "POST", headers: { "content-type": "application/json" }, body: "{\"line_items\":[]}" });
|
|
61
|
+
const answer = probe instanceof Error ? null : kitAnswer(probe);
|
|
62
|
+
if (probe instanceof Error)
|
|
63
|
+
return { status: "FAIL", detail: `/ucp/carts: ${probe.message}.` };
|
|
64
|
+
if (probe.status >= 300 && probe.status < 400)
|
|
65
|
+
return { status: "FAIL", detail: `/ucp/carts redirects (${probe.status}); Google doesn't follow redirects. ${MOUNT_FIX}` };
|
|
66
|
+
if (answer !== "open" && answer !== "key") {
|
|
67
|
+
return { status: "FAIL", detail: `The profile advertises cart transfer but POST /ucp/carts answered ${probe.status}, not the kit's answer. ${MOUNT_FIX}` };
|
|
68
|
+
}
|
|
69
|
+
const versions = ucp && isRecord(ucp.supported_versions) ? ucp.supported_versions : {};
|
|
70
|
+
const leafUrl = versions[UCP_2026_04_08];
|
|
71
|
+
if (typeof leafUrl !== "string" || !sameOrigin(leafUrl, origin) || new URL(leafUrl).pathname !== UCP_2026_04_08_PROFILE_PATH) {
|
|
72
|
+
return { status: "FAIL", detail: `The profile doesn't name ${origin}${UCP_2026_04_08_PROFILE_PATH} as its UCP ${UCP_2026_04_08} profile in supported_versions. Serve the kit's /.well-known/ucp unchanged.` };
|
|
73
|
+
}
|
|
74
|
+
const leaf = await get(UCP_2026_04_08_PROFILE_PATH);
|
|
75
|
+
if (leaf instanceof Error)
|
|
76
|
+
return { status: "FAIL", detail: `${UCP_2026_04_08_PROFILE_PATH}: ${leaf.message}.` };
|
|
77
|
+
const body = leaf.status === 200 ? jsonOf(leaf) : undefined;
|
|
78
|
+
const leafUcp = isRecord(body) && isRecord(body.ucp) ? body.ucp : undefined;
|
|
79
|
+
const services = leafUcp && isRecord(leafUcp.services) && Array.isArray(leafUcp.services["dev.ucp.shopping"]) ? leafUcp.services["dev.ucp.shopping"] : [];
|
|
80
|
+
const rest = services.find((service) => isRecord(service) && service.transport === "rest");
|
|
81
|
+
const leafCart = leafUcp && isRecord(leafUcp.capabilities) && Array.isArray(leafUcp.capabilities[CART_CAPABILITY]);
|
|
82
|
+
if (!leafUcp || leafUcp.version !== UCP_2026_04_08 || !rest || typeof rest.endpoint !== "string"
|
|
83
|
+
|| !sameOrigin(rest.endpoint, origin) || new URL(rest.endpoint).pathname !== `/ucp/${UCP_2026_04_08}` || !leafCart
|
|
84
|
+
|| !Array.isArray(body.signing_keys)) {
|
|
85
|
+
return { status: "FAIL", detail: `${UCP_2026_04_08_PROFILE_PATH} answered ${leaf.status} without a valid UCP ${UCP_2026_04_08} profile (version, REST endpoint ${origin}/ucp/${UCP_2026_04_08}, ${CART_CAPABILITY}, signing_keys). ${MOUNT_FIX}` };
|
|
86
|
+
}
|
|
87
|
+
const reachable = `Advertised (${CART_CAPABILITY}); POST /ucp/carts answers${answer === "key" ? " and requires the X-API-Key caller key" : ", open without a key (Test)"}; the UCP ${UCP_2026_04_08} profile is served.`;
|
|
88
|
+
// Live keeps the reachability check; Test also transfers a real item and opens its continue_url.
|
|
89
|
+
if (live || !landing)
|
|
90
|
+
return { status: "PASS", detail: reachable };
|
|
91
|
+
return transferAndLand(get, origin, answer === "key", landing, reachable);
|
|
92
|
+
}
|
|
93
|
+
/** Test: transfer one real catalog item (a variant when one exists), then open the continue_url and require a non-empty cart. */
|
|
94
|
+
async function transferAndLand(get, origin, keyed, landing, reachable) {
|
|
95
|
+
const notDone = (detail) => ({ status: "NOT DONE", detail });
|
|
96
|
+
if (keyed && !landing.callerKey) {
|
|
97
|
+
return notDone(`${reachable} Doctor could not transfer a cart: the store requires its caller key and doctor has none. Rerun doctor with RELYON_CART_TRANSFER_KEY set in the environment to the store's cart-transfer key (never on the command line).`);
|
|
98
|
+
}
|
|
99
|
+
const products = await landing.products();
|
|
100
|
+
const candidates = products instanceof Error ? [] : transferCandidates(products).slice(0, TRANSFER_ATTEMPTS);
|
|
101
|
+
if (!candidates.length) {
|
|
102
|
+
return notDone(products instanceof Error
|
|
103
|
+
? `Doctor could not pick a real item to transfer (${products.message}). Mount the kit's /mcp so search_products lists the agent-visible products, then rerun doctor.`
|
|
104
|
+
: "Doctor found no item agents can buy to transfer (search_products on /mcp lists none that is purchasable and in stock, with a variant for products with options). Mark a product agent.visible and agent.purchasable (relyon-catalog skill), then rerun doctor.");
|
|
105
|
+
}
|
|
106
|
+
// Try each eligible item in turn: a store refusing one item (out of stock
|
|
107
|
+
// since the listing, an option its priceCart rejects) is not yet a broken
|
|
108
|
+
// cart transfer. Every item tried is named when all are refused.
|
|
109
|
+
const refused = [];
|
|
110
|
+
let item;
|
|
111
|
+
let body;
|
|
112
|
+
let continueUrl;
|
|
113
|
+
for (const candidate of candidates) {
|
|
114
|
+
const created = await get("/ucp/carts", {
|
|
115
|
+
method: "POST",
|
|
116
|
+
headers: { "content-type": "application/json", ...(landing.callerKey ? { [CART_TRANSFER_KEY_HEADER_NAME]: landing.callerKey } : {}) },
|
|
117
|
+
body: JSON.stringify({ line_items: [{ item: { id: candidate.id }, quantity: 1 }] }),
|
|
118
|
+
});
|
|
119
|
+
if (created instanceof Error) {
|
|
120
|
+
refused.push({ label: candidate.label, reason: `/ucp/carts ${created.message}` });
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const answer = jsonOf(created);
|
|
124
|
+
const url = isRecord(answer) && typeof answer.continue_url === "string" ? answer.continue_url : undefined;
|
|
125
|
+
if (created.status === 201 && url) {
|
|
126
|
+
item = candidate;
|
|
127
|
+
body = answer;
|
|
128
|
+
continueUrl = url;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
const messages = isRecord(answer) && Array.isArray(answer.messages) ? answer.messages.filter(isRecord) : [];
|
|
132
|
+
const reason = messages.length && typeof messages[0].content === "string" ? messages[0].content
|
|
133
|
+
: isRecord(answer) && typeof answer.content === "string" ? answer.content : `HTTP ${created.status}`;
|
|
134
|
+
refused.push({ label: candidate.label, reason: reason.slice(0, 200) });
|
|
135
|
+
}
|
|
136
|
+
if (!item || !continueUrl) {
|
|
137
|
+
const fix = "The store's priceCart must price the items it lists (a variant arrives as its product_id plus that variant's options). Fix it, then rerun doctor.";
|
|
138
|
+
return notDone(refused.length === 1
|
|
139
|
+
? `Transferring ${refused[0].label} was refused: ${refused[0].reason}. ${fix}`
|
|
140
|
+
: `Transferring was refused for every item doctor tried (${refused.length}): ${refused.map((entry) => `${entry.label} (${entry.reason})`).join("; ")}. ${fix}`);
|
|
141
|
+
}
|
|
142
|
+
const lines = isRecord(body) && Array.isArray(body.line_items) ? body.line_items : [];
|
|
143
|
+
const echoed = isRecord(lines[0]) && isRecord(lines[0].item) ? lines[0].item.id : undefined;
|
|
144
|
+
if (echoed !== item.id)
|
|
145
|
+
return notDone(`Transferring ${item.label}: the answer names item ${JSON.stringify(String(echoed).slice(0, 128))}, not the item sent. Serve the kit's /ucp/carts unchanged.`);
|
|
146
|
+
if (!sameOrigin(continueUrl, origin))
|
|
147
|
+
return notDone(`Transferring ${item.label}: the continue_url is not on ${origin}. Set STORE_PUBLIC_URL (store.publicUrl) to the store's origin.`);
|
|
148
|
+
// Open the continue_url on the store's origin (same-origin redirects only, at most 3).
|
|
149
|
+
let path = `${new URL(continueUrl).pathname}${new URL(continueUrl).search}`;
|
|
150
|
+
// Printed without the query (the transfer token) and with token-like path parts hidden.
|
|
151
|
+
const shown = (value) => printableUrl(`${origin}${value}`);
|
|
152
|
+
let page = new Error("not opened");
|
|
153
|
+
for (let hop = 0; hop <= 3; hop++) {
|
|
154
|
+
page = await get(path);
|
|
155
|
+
if (page instanceof Error || page.status < 300 || page.status >= 400 || !page.location)
|
|
156
|
+
break;
|
|
157
|
+
let next;
|
|
158
|
+
try {
|
|
159
|
+
next = new URL(page.location, `${origin}${path}`);
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
return notDone(`The continue_url redirects to something that is not a URL. ${LANDING_FIX}`);
|
|
163
|
+
}
|
|
164
|
+
if (next.origin !== origin)
|
|
165
|
+
return notDone(`The continue_url (${shown(path)}) redirects off the store's origin. ${LANDING_FIX}`);
|
|
166
|
+
path = `${next.pathname}${next.search}`;
|
|
167
|
+
}
|
|
168
|
+
if (page instanceof Error)
|
|
169
|
+
return notDone(`Opening the continue_url (${shown(path)}): ${page.message}. ${LANDING_FIX}`);
|
|
170
|
+
if (page.status !== 200)
|
|
171
|
+
return notDone(`The continue_url landed at ${shown(path)} with ${page.status}, not the checkout page. ${LANDING_FIX}`);
|
|
172
|
+
const marker = checkoutMarker(page.text);
|
|
173
|
+
// Only a positive item count proves the transferred item reached the cart:
|
|
174
|
+
// card fields alone show a checkout page, not the transferred cart.
|
|
175
|
+
if (marker.items === null) {
|
|
176
|
+
const where = `${shown(path)}${path.split("?")[0] === "/" ? " (the home page)" : ""}`;
|
|
177
|
+
return notDone(marker.cardFields
|
|
178
|
+
? `The continue_url landed at ${where}, which shows card fields but no cart item count, so the transferred item can't be confirmed in the cart: render ${CHECKOUT_CART_ITEMS_ATTRIBUTE}="<number of items in the cart>" on the checkout page. ${LANDING_FIX}`
|
|
179
|
+
: `The continue_url landed at ${where}, which shows no checkout marker: ${CHECKOUT_MARKER_FIX} ${LANDING_FIX}`);
|
|
180
|
+
}
|
|
181
|
+
if (marker.items === 0) {
|
|
182
|
+
return notDone(`The continue_url landed at ${shown(path)} with an empty cart (0 items). ${LANDING_FIX}`);
|
|
183
|
+
}
|
|
184
|
+
const shownCart = `${marker.items} ${marker.items === 1 ? "item" : "items"} in the cart`;
|
|
185
|
+
return { status: "PASS", detail: `${reachable} Transferred ${item.label}; the continue_url opens ${shown(path)} with ${shownCart}.` };
|
|
186
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RelyonGateway } from "./relyon.js";
|
|
2
|
+
import type { StorePolicies } from "./setup.js";
|
|
3
|
+
export declare const RETURN_WINDOW_PENDING = "Waiting on the owner: set your return window";
|
|
4
|
+
/** Reads the shared validator's recorded issues; does not rescore products or infer eligibility. */
|
|
5
|
+
export declare function catalogReadiness(gateway: RelyonGateway, policies: StorePolicies | undefined): Promise<{
|
|
6
|
+
agent: string[];
|
|
7
|
+
owner: string[];
|
|
8
|
+
}>;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { LIST_OBJECT, PRODUCTS_PATH } from "./wire-names.js";
|
|
2
|
+
import { categoryGuidance, formatCatalogIssue } from "./catalog-enrichment.js";
|
|
3
|
+
import { isRecord } from "./util.js";
|
|
4
|
+
export const RETURN_WINDOW_PENDING = "Waiting on the owner: set your return window";
|
|
5
|
+
function isPage(data) {
|
|
6
|
+
return isRecord(data) && data.object === LIST_OBJECT && typeof data.has_more === "boolean" && Array.isArray(data.data)
|
|
7
|
+
&& data.data.every(product => isRecord(product) && typeof product.id === "string"
|
|
8
|
+
&& typeof product.merchant_product_id === "string" && typeof product.title === "string"
|
|
9
|
+
&& typeof product.livemode === "boolean" && Array.isArray(product.issues)
|
|
10
|
+
&& product.issues.every(issue => isRecord(issue) && typeof issue.field === "string"
|
|
11
|
+
&& typeof issue.severity === "string" && typeof issue.code === "string" && typeof issue.message === "string"));
|
|
12
|
+
}
|
|
13
|
+
/** Reads the shared validator's recorded issues; does not rescore products or infer eligibility. */
|
|
14
|
+
export async function catalogReadiness(gateway, policies) {
|
|
15
|
+
const agent = [];
|
|
16
|
+
const owner = [];
|
|
17
|
+
let cursor;
|
|
18
|
+
const seen = new Set();
|
|
19
|
+
for (let page = 0; page < 100; page++) {
|
|
20
|
+
const { data } = await gateway.call(client => client.GET(PRODUCTS_PATH, {
|
|
21
|
+
params: { query: { limit: 100, ...(cursor ? { starting_after: cursor } : {}) } },
|
|
22
|
+
}), isPage);
|
|
23
|
+
for (const product of data.data) {
|
|
24
|
+
if (product.livemode !== gateway.livemode)
|
|
25
|
+
throw new Error("Catalog readiness returned products for the wrong Test/Live mode.");
|
|
26
|
+
if (seen.has(product.id))
|
|
27
|
+
throw new Error("Catalog readiness repeated a product while paging.");
|
|
28
|
+
seen.add(product.id);
|
|
29
|
+
for (const issue of product.issues) {
|
|
30
|
+
if (issue.field === "returns" && issue.severity === "warning" && issue.code === "recommended_field_missing"
|
|
31
|
+
&& policies?.livemode === gateway.livemode && policies.missing.some(field => field === "returns" || field === "returns.policy_url")) {
|
|
32
|
+
owner.push(`${product.title}: ${RETURN_WINDOW_PENDING}`);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
agent.push(issue.field === "category" && issue.code === "recommended_field_missing"
|
|
36
|
+
? categoryGuidance(product) : formatCatalogIssue(product.title, issue));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (!data.has_more)
|
|
41
|
+
return { agent, owner };
|
|
42
|
+
cursor = data.data.at(-1)?.id;
|
|
43
|
+
if (!cursor)
|
|
44
|
+
throw new Error("Catalog readiness returned an empty page with more products remaining.");
|
|
45
|
+
}
|
|
46
|
+
throw new Error("Catalog readiness exceeded 100 pages; the complete catalog could not be checked.");
|
|
47
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { StoreDoctorItem } from "./doctor.js";
|
|
2
|
+
/** Outcome-only advisory metadata. Never includes details, URLs or secrets. */
|
|
3
|
+
export { KIT_OUTCOMES_HEADER } from "./wire-names.js";
|
|
4
|
+
/**
|
|
5
|
+
* The wire vocabulary of a kit-reported outcome. Server-verified checks are
|
|
6
|
+
* excluded: Relyon observes those itself, and the evidence header is sent
|
|
7
|
+
* before the remote doctor's answer exists, so a kit-side verdict for them
|
|
8
|
+
* would be provisional — with one exception: the domain-gated rows the kit
|
|
9
|
+
* probes locally on the store (UCP profile, MCP, llms.txt, sitemap, feeds,
|
|
10
|
+
* robots.txt). On a preview address or a pending domain the server never
|
|
11
|
+
* probes the store, so its only verdict is not-applicable or absent; the
|
|
12
|
+
* kit's local verdict fills exactly that gap (server-observed outcomes stay
|
|
13
|
+
* authoritative whenever they exist). Keep in step with the server's
|
|
14
|
+
* fallback allowlist (kit-evidence.ts in the API server).
|
|
15
|
+
*/
|
|
16
|
+
export declare const KIT_FALLBACK_CHECK_IDS: readonly ["ucp-profile", "mcp-initialize", "llms-txt", "product-sitemap", "product-feeds", "robots-txt"];
|
|
17
|
+
/** The evidence caps: at most this many entries and this many bytes. */
|
|
18
|
+
export declare const KIT_EVIDENCE_LIMITS: {
|
|
19
|
+
readonly entries: 40;
|
|
20
|
+
readonly bytes: 4096;
|
|
21
|
+
};
|
|
22
|
+
export declare function kitOutcomeMetadata(items: readonly StoreDoctorItem[]): string | undefined;
|