@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
package/dist/state.js
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { canonicalJson } from "./util.js";
|
|
2
|
+
export class MemoryCheckoutStateStore {
|
|
3
|
+
maxRecords;
|
|
4
|
+
records = new Map();
|
|
5
|
+
byAgent = new Map();
|
|
6
|
+
claims = new Map();
|
|
7
|
+
storePayments = new Map();
|
|
8
|
+
handoffClaims = new Map();
|
|
9
|
+
constructor(maxRecords = 10_000) {
|
|
10
|
+
this.maxRecords = maxRecords;
|
|
11
|
+
}
|
|
12
|
+
get(protocol, sessionId) {
|
|
13
|
+
return this.records.get(`${protocol}:${sessionId}`) ?? null;
|
|
14
|
+
}
|
|
15
|
+
byAgentCheckoutId(agentCheckoutId) {
|
|
16
|
+
const key = this.byAgent.get(agentCheckoutId);
|
|
17
|
+
return key ? this.records.get(key) ?? null : null;
|
|
18
|
+
}
|
|
19
|
+
put(record) {
|
|
20
|
+
const key = `${record.protocol}:${record.sessionId}`;
|
|
21
|
+
this.records.delete(key);
|
|
22
|
+
this.records.set(key, record);
|
|
23
|
+
if (record.agentCheckoutId)
|
|
24
|
+
this.byAgent.set(record.agentCheckoutId, key);
|
|
25
|
+
while (this.records.size > this.maxRecords) {
|
|
26
|
+
const oldest = this.records.keys().next().value;
|
|
27
|
+
const gone = this.records.get(oldest);
|
|
28
|
+
this.records.delete(oldest);
|
|
29
|
+
if (gone?.agentCheckoutId)
|
|
30
|
+
this.byAgent.delete(gone.agentCheckoutId);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
claimOrder(paymentId, options) {
|
|
34
|
+
const current = this.claims.get(paymentId);
|
|
35
|
+
if (current && (current.created || !expired(current, options?.now)))
|
|
36
|
+
return false;
|
|
37
|
+
this.claims.set(paymentId, {
|
|
38
|
+
claimId: options?.claimId ?? null, created: false, expiresAt: options ? options.now + options.leaseMs : null,
|
|
39
|
+
});
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
releaseOrder(paymentId, claimId) {
|
|
43
|
+
const current = this.claims.get(paymentId);
|
|
44
|
+
if (!current || current.created)
|
|
45
|
+
return;
|
|
46
|
+
if (claimId !== undefined && current.claimId !== null && current.claimId !== claimId)
|
|
47
|
+
return;
|
|
48
|
+
this.claims.delete(paymentId);
|
|
49
|
+
}
|
|
50
|
+
completeOrder(paymentId) {
|
|
51
|
+
this.claims.set(paymentId, { claimId: this.claims.get(paymentId)?.claimId ?? null, created: true, expiresAt: null });
|
|
52
|
+
}
|
|
53
|
+
orderClaimStatus(paymentId, now) {
|
|
54
|
+
const current = this.claims.get(paymentId);
|
|
55
|
+
if (!current)
|
|
56
|
+
return null;
|
|
57
|
+
if (current.created)
|
|
58
|
+
return "created";
|
|
59
|
+
return expired(current, now) ? null : "creating";
|
|
60
|
+
}
|
|
61
|
+
getStorePayment(idempotencyKey) {
|
|
62
|
+
return this.storePayments.get(idempotencyKey) ?? null;
|
|
63
|
+
}
|
|
64
|
+
putStorePayment(idempotencyKey, record) {
|
|
65
|
+
this.storePayments.delete(idempotencyKey);
|
|
66
|
+
this.storePayments.set(idempotencyKey, record);
|
|
67
|
+
while (this.storePayments.size > this.maxRecords)
|
|
68
|
+
this.storePayments.delete(this.storePayments.keys().next().value);
|
|
69
|
+
}
|
|
70
|
+
setHandoffToken(protocol, sessionId, token, options) {
|
|
71
|
+
const current = this.get(protocol, sessionId);
|
|
72
|
+
if (!current || (current.handoffToken ?? null) !== options.expectedToken)
|
|
73
|
+
return false;
|
|
74
|
+
if (canonicalJson(current.inputs) !== canonicalJson(options.expectedInputs))
|
|
75
|
+
return false;
|
|
76
|
+
this.put({ ...current, handoffToken: token, handoffTokenAt: options.now, updatedAt: options.now });
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
claimHandoffToken(tokenDigest, options) {
|
|
80
|
+
const until = this.handoffClaims.get(tokenDigest);
|
|
81
|
+
if (until !== undefined && options.now < until)
|
|
82
|
+
return false;
|
|
83
|
+
this.handoffClaims.delete(tokenDigest);
|
|
84
|
+
this.handoffClaims.set(tokenDigest, options.expiresAt);
|
|
85
|
+
if (this.handoffClaims.size > this.maxRecords) {
|
|
86
|
+
// Expired claims go first; a token is refused once it expires anyway.
|
|
87
|
+
for (const [key, expiresAt] of this.handoffClaims)
|
|
88
|
+
if (expiresAt <= options.now)
|
|
89
|
+
this.handoffClaims.delete(key);
|
|
90
|
+
while (this.handoffClaims.size > this.maxRecords)
|
|
91
|
+
this.handoffClaims.delete(this.handoffClaims.keys().next().value);
|
|
92
|
+
}
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const expired = (claim, now) => claim.expiresAt !== null && now !== undefined && now >= claim.expiresAt;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { KitContext } from "./context.js";
|
|
2
|
+
import type { StorePaymentRecord } from "./state.js";
|
|
3
|
+
/** Records where a `payAndConfirm` payment stands. Never throws. */
|
|
4
|
+
export declare function rememberStorePayment(ctx: KitContext, idempotencyKey: string, record: StorePaymentRecord): Promise<void>;
|
|
5
|
+
/** The record a key has now (shared or this process's, whichever says more); never throws. */
|
|
6
|
+
export declare function storePaymentRecord(ctx: KitContext, idempotencyKey: string): Promise<StorePaymentRecord | null>;
|
|
7
|
+
/**
|
|
8
|
+
* Records a state unless the key already has one that says more (a decline
|
|
9
|
+
* never overwrites a charge; a pending order never overwrites a created one;
|
|
10
|
+
* another attempt's `charging` is kept until a payment succeeds).
|
|
11
|
+
* Used for the order-reference and hand-off records several attempts share.
|
|
12
|
+
* Never throws.
|
|
13
|
+
*/
|
|
14
|
+
export declare function rememberStorePaymentUnlessStronger(ctx: KitContext, idempotencyKey: string, record: StorePaymentRecord): Promise<void>;
|
|
15
|
+
/** The record several attempts of one order reference share. */
|
|
16
|
+
export declare const orderReferencePaymentKey: (orderReference: string) => string;
|
|
17
|
+
/** The record every store payment for one agent checkout (a hand-off cart) shares. */
|
|
18
|
+
export declare const handoffPaymentKey: (agentCheckoutId: string) => string;
|
|
19
|
+
/**
|
|
20
|
+
* Before the payment request is sent: records `charging` unless the key
|
|
21
|
+
* already has a record that says more (a succeeded payment is never
|
|
22
|
+
* downgraded). When the shared store can't be read, `charging` is kept in
|
|
23
|
+
* this process only, so it can never overwrite a shared `order_pending`.
|
|
24
|
+
* Returns the record the key had before. Never throws.
|
|
25
|
+
*/
|
|
26
|
+
export declare function noteStorePaymentCharging(ctx: KitContext, idempotencyKey: string): Promise<StorePaymentRecord | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Where a payment Idempotency-Key stands before the kit answers a rule
|
|
29
|
+
* refusal in `payAndConfirm`:
|
|
30
|
+
* - `order_pending`: its payment succeeded and the store's order isn't created;
|
|
31
|
+
* - `charged`: its payment succeeded and the order exists;
|
|
32
|
+
* - `none`: established unpaid: the key's only recorded outcome is a decline;
|
|
33
|
+
* - `unknown`: the kit can't tell. A missing record is never proof of no
|
|
34
|
+
* charge: the memory may be per-process, restarted, or have evicted it.
|
|
35
|
+
* Never throws.
|
|
36
|
+
*/
|
|
37
|
+
export type StorePaymentStanding = {
|
|
38
|
+
kind: "order_pending";
|
|
39
|
+
paymentId: string;
|
|
40
|
+
record: StorePaymentRecord;
|
|
41
|
+
} | {
|
|
42
|
+
kind: "charged";
|
|
43
|
+
} | {
|
|
44
|
+
kind: "none";
|
|
45
|
+
} | {
|
|
46
|
+
kind: "unknown";
|
|
47
|
+
};
|
|
48
|
+
export declare function storePaymentStanding(ctx: KitContext, idempotencyKey: string): Promise<StorePaymentStanding>;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { describeError } from "./relyon-cart-tokens.js";
|
|
2
|
+
/**
|
|
3
|
+
* What `payAndConfirm` charged, by payment Idempotency-Key, when the state
|
|
4
|
+
* store has no `getStorePayment`/`putStorePayment` or refused to save: kept
|
|
5
|
+
* in this process (per state store), like the relay's unsaved order-pending
|
|
6
|
+
* markers. Bounded; the oldest entries go first.
|
|
7
|
+
*/
|
|
8
|
+
const inProcess = new WeakMap();
|
|
9
|
+
const MAX_IN_PROCESS = 10_000;
|
|
10
|
+
function local(state) {
|
|
11
|
+
let payments = inProcess.get(state);
|
|
12
|
+
if (!payments) {
|
|
13
|
+
payments = new Map();
|
|
14
|
+
inProcess.set(state, payments);
|
|
15
|
+
}
|
|
16
|
+
return payments;
|
|
17
|
+
}
|
|
18
|
+
const remembers = (state) => typeof state.getStorePayment === "function" && typeof state.putStorePayment === "function";
|
|
19
|
+
/** Records where a `payAndConfirm` payment stands. Never throws. */
|
|
20
|
+
export async function rememberStorePayment(ctx, idempotencyKey, record) {
|
|
21
|
+
const payments = local(ctx.state);
|
|
22
|
+
if (remembers(ctx.state)) {
|
|
23
|
+
try {
|
|
24
|
+
await ctx.state.putStorePayment(idempotencyKey, record);
|
|
25
|
+
payments.delete(idempotencyKey);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
ctx.logger.error("The checkout state store failed to save a store payment; this server keeps it", {
|
|
30
|
+
payment: record.paymentId, state: record.state, error: describeError(error),
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
keepLocally(ctx, idempotencyKey, record);
|
|
35
|
+
}
|
|
36
|
+
/** How much a record says: a later state never yields to an earlier one. */
|
|
37
|
+
const RANK = { declined: 0, charging: 1, order_pending: 2, order_created: 3 };
|
|
38
|
+
/** The record that says more (a charge over none, a created order over a pending one); the newer on a tie. */
|
|
39
|
+
function stronger(a, b) {
|
|
40
|
+
if (!a || !b)
|
|
41
|
+
return a ?? b;
|
|
42
|
+
if (RANK[a.state] !== RANK[b.state])
|
|
43
|
+
return RANK[a.state] > RANK[b.state] ? a : b;
|
|
44
|
+
return a.updatedAt >= b.updatedAt ? a : b;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* The record for a key: the shared store's or this process's, whichever says
|
|
48
|
+
* more (a save that failed here may have kept a newer state than the shared
|
|
49
|
+
* store holds). `known` is false when the shared store can't say.
|
|
50
|
+
*/
|
|
51
|
+
async function load(ctx, idempotencyKey) {
|
|
52
|
+
const kept = local(ctx.state).get(idempotencyKey) ?? null;
|
|
53
|
+
if (!remembers(ctx.state))
|
|
54
|
+
return { record: kept, known: false };
|
|
55
|
+
try {
|
|
56
|
+
return { record: stronger((await ctx.state.getStorePayment(idempotencyKey)) ?? null, kept), known: true };
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
ctx.logger.error("The checkout state store failed to load a store payment", { error: describeError(error) });
|
|
60
|
+
return { record: kept, known: false };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/** The record a key has now (shared or this process's, whichever says more); never throws. */
|
|
64
|
+
export async function storePaymentRecord(ctx, idempotencyKey) {
|
|
65
|
+
return (await load(ctx, idempotencyKey)).record;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Records a state unless the key already has one that says more (a decline
|
|
69
|
+
* never overwrites a charge; a pending order never overwrites a created one;
|
|
70
|
+
* another attempt's `charging` is kept until a payment succeeds).
|
|
71
|
+
* Used for the order-reference and hand-off records several attempts share.
|
|
72
|
+
* Never throws.
|
|
73
|
+
*/
|
|
74
|
+
export async function rememberStorePaymentUnlessStronger(ctx, idempotencyKey, record) {
|
|
75
|
+
const { record: current, known } = await load(ctx, idempotencyKey);
|
|
76
|
+
const sameAttempt = current?.orderReference === record.orderReference && current?.attempt === record.attempt;
|
|
77
|
+
if (current?.state === "charging") {
|
|
78
|
+
// Another attempt's unfinished charge stays: this attempt's decline (or its
|
|
79
|
+
// own charge) never hides that one may have gone through. The same
|
|
80
|
+
// attempt's `charging` gives way to its outcome.
|
|
81
|
+
if (!sameAttempt && RANK[record.state] <= RANK.charging)
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
else if (current && RANK[current.state] > RANK[record.state]) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// The shared store can't be read: it may hold more than this process knows.
|
|
88
|
+
if (!known && remembers(ctx.state) && RANK[record.state] < RANK.order_pending) {
|
|
89
|
+
keepLocally(ctx, idempotencyKey, stronger(current, record));
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
await rememberStorePayment(ctx, idempotencyKey, record);
|
|
93
|
+
}
|
|
94
|
+
function keepLocally(ctx, idempotencyKey, record) {
|
|
95
|
+
const payments = local(ctx.state);
|
|
96
|
+
payments.delete(idempotencyKey);
|
|
97
|
+
payments.set(idempotencyKey, record);
|
|
98
|
+
while (payments.size > MAX_IN_PROCESS)
|
|
99
|
+
payments.delete(payments.keys().next().value);
|
|
100
|
+
}
|
|
101
|
+
/** The record several attempts of one order reference share. */
|
|
102
|
+
export const orderReferencePaymentKey = (orderReference) => `order_reference:${orderReference}`;
|
|
103
|
+
/** The record every store payment for one agent checkout (a hand-off cart) shares. */
|
|
104
|
+
export const handoffPaymentKey = (agentCheckoutId) => `agent_checkout:${agentCheckoutId}`;
|
|
105
|
+
/**
|
|
106
|
+
* Before the payment request is sent: records `charging` unless the key
|
|
107
|
+
* already has a record that says more (a succeeded payment is never
|
|
108
|
+
* downgraded). When the shared store can't be read, `charging` is kept in
|
|
109
|
+
* this process only, so it can never overwrite a shared `order_pending`.
|
|
110
|
+
* Returns the record the key had before. Never throws.
|
|
111
|
+
*/
|
|
112
|
+
export async function noteStorePaymentCharging(ctx, idempotencyKey) {
|
|
113
|
+
const { record, known } = await load(ctx, idempotencyKey);
|
|
114
|
+
if (record && record.state !== "declined")
|
|
115
|
+
return record;
|
|
116
|
+
const charging = { state: "charging", paymentId: null, updatedAt: ctx.now() };
|
|
117
|
+
if (!known && remembers(ctx.state))
|
|
118
|
+
keepLocally(ctx, idempotencyKey, charging);
|
|
119
|
+
else
|
|
120
|
+
await rememberStorePayment(ctx, idempotencyKey, charging);
|
|
121
|
+
return record;
|
|
122
|
+
}
|
|
123
|
+
export async function storePaymentStanding(ctx, idempotencyKey) {
|
|
124
|
+
const { record } = await load(ctx, idempotencyKey);
|
|
125
|
+
if (!record)
|
|
126
|
+
return { kind: "unknown" };
|
|
127
|
+
if (record.state === "declined")
|
|
128
|
+
return { kind: "none" };
|
|
129
|
+
if (record.state === "charging" || !record.paymentId)
|
|
130
|
+
return { kind: "unknown" };
|
|
131
|
+
if (record.state === "order_created")
|
|
132
|
+
return { kind: "charged" };
|
|
133
|
+
if (typeof ctx.state.orderClaimStatus === "function") {
|
|
134
|
+
try {
|
|
135
|
+
if (await ctx.state.orderClaimStatus(record.paymentId, ctx.now()) === "created")
|
|
136
|
+
return { kind: "charged" };
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// The claim can't be read: the order stays pending.
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return { kind: "order_pending", paymentId: record.paymentId, record };
|
|
143
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export type BareItem = string | number | boolean | Uint8Array | {
|
|
2
|
+
token: string;
|
|
3
|
+
};
|
|
4
|
+
export type Parameters = Map<string, BareItem>;
|
|
5
|
+
export interface Item {
|
|
6
|
+
value: BareItem;
|
|
7
|
+
params: Parameters;
|
|
8
|
+
}
|
|
9
|
+
export interface InnerList {
|
|
10
|
+
items: Item[];
|
|
11
|
+
params: Parameters;
|
|
12
|
+
}
|
|
13
|
+
export type Member = Item | InnerList;
|
|
14
|
+
export declare class StructuredFieldError extends Error {
|
|
15
|
+
constructor(message: string);
|
|
16
|
+
}
|
|
17
|
+
export declare const isInnerList: (member: Member) => member is InnerList;
|
|
18
|
+
export declare const isToken: (value: BareItem) => value is {
|
|
19
|
+
token: string;
|
|
20
|
+
};
|
|
21
|
+
/** Parses an RFC 8941 dictionary. */
|
|
22
|
+
export declare function parseDictionary(text: string): Map<string, Member>;
|
|
23
|
+
/** Parses an RFC 8941 item (for a Signature-Agent that is a single string). */
|
|
24
|
+
export declare function parseItem(text: string): Item;
|
|
25
|
+
export declare function serializeParameters(params: Parameters): string;
|
|
26
|
+
export declare function serializeInnerList(list: InnerList): string;
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// The small part of RFC 8941 (Structured Field Values) that HTTP Message
|
|
2
|
+
// Signatures need: dictionaries whose members are items or inner lists, with
|
|
3
|
+
// parameters. Strict: anything malformed throws `StructuredFieldError`.
|
|
4
|
+
export class StructuredFieldError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "StructuredFieldError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
export const isInnerList = (member) => "items" in member;
|
|
11
|
+
export const isToken = (value) => typeof value === "object" && value !== null && "token" in value;
|
|
12
|
+
class Parser {
|
|
13
|
+
text;
|
|
14
|
+
pos = 0;
|
|
15
|
+
constructor(text) {
|
|
16
|
+
this.text = text;
|
|
17
|
+
}
|
|
18
|
+
peek() { return this.text[this.pos]; }
|
|
19
|
+
done() { return this.pos >= this.text.length; }
|
|
20
|
+
skipSp() { while (this.peek() === " ")
|
|
21
|
+
this.pos += 1; }
|
|
22
|
+
skipOws() { while (this.peek() === " " || this.peek() === "\t")
|
|
23
|
+
this.pos += 1; }
|
|
24
|
+
fail(what) { throw new StructuredFieldError(`${what} at position ${this.pos}`); }
|
|
25
|
+
key() {
|
|
26
|
+
const start = this.pos;
|
|
27
|
+
if (!/[a-z*]/.test(this.peek() ?? ""))
|
|
28
|
+
this.fail("expected a key");
|
|
29
|
+
while (/[a-z0-9_\-.*]/.test(this.peek() ?? ""))
|
|
30
|
+
this.pos += 1;
|
|
31
|
+
return this.text.slice(start, this.pos);
|
|
32
|
+
}
|
|
33
|
+
bareItem() {
|
|
34
|
+
const c = this.peek();
|
|
35
|
+
if (c === undefined)
|
|
36
|
+
this.fail("expected a value");
|
|
37
|
+
if (c === '"')
|
|
38
|
+
return this.string();
|
|
39
|
+
if (c === ":")
|
|
40
|
+
return this.bytes();
|
|
41
|
+
if (c === "?")
|
|
42
|
+
return this.boolean();
|
|
43
|
+
if (c === "-" || /[0-9]/.test(c))
|
|
44
|
+
return this.number();
|
|
45
|
+
if (/[A-Za-z*]/.test(c))
|
|
46
|
+
return this.token();
|
|
47
|
+
return this.fail("unexpected character");
|
|
48
|
+
}
|
|
49
|
+
string() {
|
|
50
|
+
this.pos += 1;
|
|
51
|
+
let out = "";
|
|
52
|
+
for (;;) {
|
|
53
|
+
const c = this.peek();
|
|
54
|
+
if (c === undefined)
|
|
55
|
+
this.fail("unterminated string");
|
|
56
|
+
this.pos += 1;
|
|
57
|
+
if (c === "\\") {
|
|
58
|
+
const next = this.peek();
|
|
59
|
+
if (next !== '"' && next !== "\\")
|
|
60
|
+
this.fail("bad escape");
|
|
61
|
+
out += next;
|
|
62
|
+
this.pos += 1;
|
|
63
|
+
}
|
|
64
|
+
else if (c === '"') {
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const code = c.charCodeAt(0);
|
|
69
|
+
if (code < 0x20 || code > 0x7e)
|
|
70
|
+
this.fail("non-printable character in string");
|
|
71
|
+
out += c;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
bytes() {
|
|
76
|
+
this.pos += 1;
|
|
77
|
+
const end = this.text.indexOf(":", this.pos);
|
|
78
|
+
if (end === -1)
|
|
79
|
+
this.fail("unterminated byte sequence");
|
|
80
|
+
const b64 = this.text.slice(this.pos, end);
|
|
81
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(b64))
|
|
82
|
+
this.fail("bad byte sequence");
|
|
83
|
+
this.pos = end + 1;
|
|
84
|
+
return new Uint8Array(Buffer.from(b64, "base64"));
|
|
85
|
+
}
|
|
86
|
+
boolean() {
|
|
87
|
+
this.pos += 1;
|
|
88
|
+
const c = this.peek();
|
|
89
|
+
if (c !== "0" && c !== "1")
|
|
90
|
+
this.fail("bad boolean");
|
|
91
|
+
this.pos += 1;
|
|
92
|
+
return c === "1";
|
|
93
|
+
}
|
|
94
|
+
number() {
|
|
95
|
+
const match = /^-?[0-9]{1,15}(\.[0-9]{1,3})?/.exec(this.text.slice(this.pos));
|
|
96
|
+
if (!match)
|
|
97
|
+
this.fail("bad number");
|
|
98
|
+
this.pos += match[0].length;
|
|
99
|
+
return Number(match[0]);
|
|
100
|
+
}
|
|
101
|
+
token() {
|
|
102
|
+
const start = this.pos;
|
|
103
|
+
this.pos += 1;
|
|
104
|
+
while (/[!#$%&'*+\-.^_`|~0-9A-Za-z:/]/.test(this.peek() ?? ""))
|
|
105
|
+
this.pos += 1;
|
|
106
|
+
return { token: this.text.slice(start, this.pos) };
|
|
107
|
+
}
|
|
108
|
+
params() {
|
|
109
|
+
const params = new Map();
|
|
110
|
+
while (this.peek() === ";") {
|
|
111
|
+
this.pos += 1;
|
|
112
|
+
this.skipSp();
|
|
113
|
+
const key = this.key();
|
|
114
|
+
let value = true;
|
|
115
|
+
if (this.peek() === "=") {
|
|
116
|
+
this.pos += 1;
|
|
117
|
+
value = this.bareItem();
|
|
118
|
+
}
|
|
119
|
+
params.set(key, value);
|
|
120
|
+
}
|
|
121
|
+
return params;
|
|
122
|
+
}
|
|
123
|
+
member() {
|
|
124
|
+
if (this.peek() === "(") {
|
|
125
|
+
this.pos += 1;
|
|
126
|
+
const items = [];
|
|
127
|
+
for (;;) {
|
|
128
|
+
this.skipSp();
|
|
129
|
+
if (this.peek() === ")") {
|
|
130
|
+
this.pos += 1;
|
|
131
|
+
return { items, params: this.params() };
|
|
132
|
+
}
|
|
133
|
+
const value = this.bareItem();
|
|
134
|
+
items.push({ value, params: this.params() });
|
|
135
|
+
const next = this.peek();
|
|
136
|
+
if (next !== " " && next !== ")")
|
|
137
|
+
this.fail("bad inner list");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
const value = this.bareItem();
|
|
141
|
+
return { value, params: this.params() };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/** Parses an RFC 8941 dictionary. */
|
|
145
|
+
export function parseDictionary(text) {
|
|
146
|
+
const parser = new Parser(text.trim());
|
|
147
|
+
const out = new Map();
|
|
148
|
+
if (parser.done())
|
|
149
|
+
return out;
|
|
150
|
+
for (;;) {
|
|
151
|
+
const key = parser.key();
|
|
152
|
+
let member;
|
|
153
|
+
if (parser.peek() === "=") {
|
|
154
|
+
parser.pos += 1;
|
|
155
|
+
member = parser.member();
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
member = { value: true, params: parser.params() };
|
|
159
|
+
}
|
|
160
|
+
out.set(key, member);
|
|
161
|
+
parser.skipOws();
|
|
162
|
+
if (parser.done())
|
|
163
|
+
return out;
|
|
164
|
+
if (parser.peek() !== ",")
|
|
165
|
+
parser.fail("expected a comma");
|
|
166
|
+
parser.pos += 1;
|
|
167
|
+
parser.skipOws();
|
|
168
|
+
if (parser.done())
|
|
169
|
+
parser.fail("trailing comma");
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** Parses an RFC 8941 item (for a Signature-Agent that is a single string). */
|
|
173
|
+
export function parseItem(text) {
|
|
174
|
+
const parser = new Parser(text.trim());
|
|
175
|
+
const value = parser.bareItem();
|
|
176
|
+
const params = parser.params();
|
|
177
|
+
if (!parser.done())
|
|
178
|
+
parser.fail("trailing characters");
|
|
179
|
+
return { value, params };
|
|
180
|
+
}
|
|
181
|
+
function serializeBare(value) {
|
|
182
|
+
if (typeof value === "string")
|
|
183
|
+
return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
|
|
184
|
+
if (typeof value === "number")
|
|
185
|
+
return String(value);
|
|
186
|
+
if (typeof value === "boolean")
|
|
187
|
+
return value ? "?1" : "?0";
|
|
188
|
+
if (value instanceof Uint8Array)
|
|
189
|
+
return `:${Buffer.from(value).toString("base64")}:`;
|
|
190
|
+
return value.token;
|
|
191
|
+
}
|
|
192
|
+
export function serializeParameters(params) {
|
|
193
|
+
let out = "";
|
|
194
|
+
for (const [key, value] of params)
|
|
195
|
+
out += value === true ? `;${key}` : `;${key}=${serializeBare(value)}`;
|
|
196
|
+
return out;
|
|
197
|
+
}
|
|
198
|
+
export function serializeInnerList(list) {
|
|
199
|
+
return `(${list.items.map((item) => `${serializeBare(item.value)}${serializeParameters(item.params)}`).join(" ")})${serializeParameters(list.params)}`;
|
|
200
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** All direct runtime and development dependency pins for generated stores. */
|
|
2
|
+
export declare const TEMPLATE_VERSIONS: {
|
|
3
|
+
readonly next: "15.5.28";
|
|
4
|
+
readonly express: "5.2.1";
|
|
5
|
+
readonly react: "19.1.10";
|
|
6
|
+
readonly "react-dom": "19.1.10";
|
|
7
|
+
readonly typescript: "5.9.3";
|
|
8
|
+
readonly tsx: "4.20.6";
|
|
9
|
+
readonly "@types/node": "25.3.5";
|
|
10
|
+
readonly "@types/react": "19.1.16";
|
|
11
|
+
readonly "@types/react-dom": "19.1.9";
|
|
12
|
+
readonly "@types/express": "5.0.6";
|
|
13
|
+
};
|
|
14
|
+
export type StoreTemplate = "next" | "express";
|
|
15
|
+
export declare const TEMPLATE_QUICKSTART = "relyon/QUICKSTART.md";
|
|
16
|
+
export declare function templateInstructions(template: StoreTemplate): string;
|
|
17
|
+
/** Only explicit structured facts are interpreted. Free-form ideas stay in the brief. */
|
|
18
|
+
export declare function templateCatalog(brief?: string): object;
|
|
19
|
+
export declare function renderTemplate(template: StoreTemplate, name: string, brief?: string, skillsDir?: string, assetsDir?: string): Promise<Map<string, Buffer>>;
|