@kya-os/mcp-i-cloudflare 1.13.0 → 1.13.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent.d.ts +7 -0
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +43 -7
- package/dist/agent.js.map +1 -1
- package/dist/helpers/env-mapper.d.ts.map +1 -1
- package/dist/helpers/env-mapper.js +6 -0
- package/dist/helpers/env-mapper.js.map +1 -1
- package/dist/runtime/oauth-handler.d.ts.map +1 -1
- package/dist/runtime/oauth-handler.js +71 -5
- package/dist/runtime/oauth-handler.js.map +1 -1
- package/dist/runtime/oidc/idp-scopes.d.ts +26 -0
- package/dist/runtime/oidc/idp-scopes.d.ts.map +1 -0
- package/dist/runtime/oidc/idp-scopes.js +24 -0
- package/dist/runtime/oidc/idp-scopes.js.map +1 -0
- package/dist/services/batch-queue.d.ts +103 -0
- package/dist/services/batch-queue.d.ts.map +1 -0
- package/dist/services/batch-queue.js +180 -0
- package/dist/services/batch-queue.js.map +1 -0
- package/dist/services/consent.service.d.ts +124 -0
- package/dist/services/consent.service.d.ts.map +1 -1
- package/dist/services/consent.service.js +542 -101
- package/dist/services/consent.service.js.map +1 -1
- package/dist/services/kya-os-events.service.d.ts +131 -0
- package/dist/services/kya-os-events.service.d.ts.map +1 -0
- package/dist/services/kya-os-events.service.js +135 -0
- package/dist/services/kya-os-events.service.js.map +1 -0
- package/dist/services/proof-batch-queue.d.ts +15 -67
- package/dist/services/proof-batch-queue.d.ts.map +1 -1
- package/dist/services/proof-batch-queue.js +12 -147
- package/dist/services/proof-batch-queue.js.map +1 -1
- package/dist/services/proof.service.d.ts +7 -0
- package/dist/services/proof.service.d.ts.map +1 -1
- package/dist/services/proof.service.js +35 -0
- package/dist/services/proof.service.js.map +1 -1
- package/dist/types.d.ts +2 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +4 -3
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic Batch Queue (Cloudflare Workers Implementation)
|
|
3
|
+
*
|
|
4
|
+
* Platform-agnostic, type-generic batching for Cloudflare Workers. Collects
|
|
5
|
+
* items in memory and submits them in batches to one or more destinations,
|
|
6
|
+
* without blocking request handling.
|
|
7
|
+
*
|
|
8
|
+
* This is the shared engine behind both the proof batch queue
|
|
9
|
+
* (`proof-batch-queue.ts`) and the KYA-OS activity events emitter
|
|
10
|
+
* (`kya-os-events.service.ts`) so the batching, retry, and flush semantics
|
|
11
|
+
* live in exactly one place.
|
|
12
|
+
*
|
|
13
|
+
* Performance:
|
|
14
|
+
* - Batch size: 10 items (configurable)
|
|
15
|
+
* - Fire-and-forget submission (does not block the caller)
|
|
16
|
+
*
|
|
17
|
+
* Retry Strategy:
|
|
18
|
+
* - Exponential backoff: 1s, 2s, 4s, 8s, 16s
|
|
19
|
+
* - Max retries: 5 (configurable)
|
|
20
|
+
* - Failed items logged and dropped after max retries
|
|
21
|
+
*
|
|
22
|
+
* Note: In Cloudflare Workers, timers do not persist across requests, so
|
|
23
|
+
* automatic flush timers are disabled. Flush is driven by two triggers:
|
|
24
|
+
* within a request when the batch size is reached, and across requests via
|
|
25
|
+
* a scheduled (cron) `flush()` call.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Generic Batch Queue.
|
|
29
|
+
*
|
|
30
|
+
* Collects items and submits them in batches to multiple destinations.
|
|
31
|
+
*
|
|
32
|
+
* Note: In Cloudflare Workers, timers don't persist across requests, so
|
|
33
|
+
* automatic flush timers are disabled. Use manual `flush()` calls via cron.
|
|
34
|
+
*/
|
|
35
|
+
export class BatchQueue {
|
|
36
|
+
queue = [];
|
|
37
|
+
pendingBatches = [];
|
|
38
|
+
config;
|
|
39
|
+
closed = false;
|
|
40
|
+
// Stats
|
|
41
|
+
stats = {
|
|
42
|
+
queued: 0,
|
|
43
|
+
submitted: 0,
|
|
44
|
+
failed: 0,
|
|
45
|
+
batchesSubmitted: 0,
|
|
46
|
+
};
|
|
47
|
+
constructor(config) {
|
|
48
|
+
this.config = {
|
|
49
|
+
destinations: config.destinations,
|
|
50
|
+
maxBatchSize: config.maxBatchSize || 10,
|
|
51
|
+
flushIntervalMs: config.flushIntervalMs || 5000,
|
|
52
|
+
maxRetries: config.maxRetries || 5,
|
|
53
|
+
debug: config.debug || false,
|
|
54
|
+
logPrefix: config.logPrefix || "BatchQueue",
|
|
55
|
+
noun: config.noun || "item",
|
|
56
|
+
};
|
|
57
|
+
// Note: Timers are disabled in Cloudflare Workers.
|
|
58
|
+
// Flush must be called manually via cron jobs.
|
|
59
|
+
}
|
|
60
|
+
/** The log prefix, e.g. "[ProofBatchQueue]". */
|
|
61
|
+
get tag() {
|
|
62
|
+
return `[${this.config.logPrefix}]`;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Add an item to the queue.
|
|
66
|
+
*/
|
|
67
|
+
enqueue(item) {
|
|
68
|
+
if (this.closed) {
|
|
69
|
+
console.warn(`${this.tag} Queue is closed, dropping ${this.config.noun}`);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
this.queue.push(item);
|
|
73
|
+
this.stats.queued++;
|
|
74
|
+
if (this.config.debug) {
|
|
75
|
+
console.error(`${this.tag} Enqueued ${this.config.noun} (queue size: ${this.queue.length})`);
|
|
76
|
+
}
|
|
77
|
+
// Flush immediately if batch size reached (Cloudflare Workers compatible)
|
|
78
|
+
if (this.queue.length >= this.config.maxBatchSize) {
|
|
79
|
+
this.flush();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Flush queue immediately (submit all queued items).
|
|
84
|
+
*/
|
|
85
|
+
async flush() {
|
|
86
|
+
if (this.queue.length === 0) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const items = this.queue.splice(0, this.config.maxBatchSize);
|
|
90
|
+
if (this.config.debug) {
|
|
91
|
+
console.error(`${this.tag} Flushing ${items.length} ${this.config.noun}s to ${this.config.destinations.length} destinations`);
|
|
92
|
+
}
|
|
93
|
+
// Submit to all destinations
|
|
94
|
+
for (const destination of this.config.destinations) {
|
|
95
|
+
const batch = {
|
|
96
|
+
items: [...items], // Copy items for each destination
|
|
97
|
+
destination,
|
|
98
|
+
retryCount: 0,
|
|
99
|
+
};
|
|
100
|
+
this.submitBatch(batch); // Fire-and-forget
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Submit a batch to a destination (with retries).
|
|
105
|
+
*/
|
|
106
|
+
async submitBatch(batch) {
|
|
107
|
+
try {
|
|
108
|
+
await batch.destination.submit(batch.items);
|
|
109
|
+
this.stats.submitted += batch.items.length;
|
|
110
|
+
this.stats.batchesSubmitted++;
|
|
111
|
+
if (this.config.debug) {
|
|
112
|
+
console.error(`${this.tag} Successfully submitted ${batch.items.length} ${this.config.noun}s to ${batch.destination.name}`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
catch (error) {
|
|
116
|
+
console.error(`${this.tag} Failed to submit to ${batch.destination.name}:`, error);
|
|
117
|
+
// Retry with exponential backoff
|
|
118
|
+
if (batch.retryCount < this.config.maxRetries) {
|
|
119
|
+
batch.retryCount++;
|
|
120
|
+
const backoffMs = Math.min(1000 * Math.pow(2, batch.retryCount - 1), 16000);
|
|
121
|
+
batch.nextRetryAt = Date.now() + backoffMs;
|
|
122
|
+
this.pendingBatches.push(batch);
|
|
123
|
+
if (this.config.debug) {
|
|
124
|
+
console.error(`${this.tag} Scheduling retry ${batch.retryCount}/${this.config.maxRetries} in ${backoffMs}ms`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
// Max retries exceeded, drop batch
|
|
129
|
+
this.stats.failed += batch.items.length;
|
|
130
|
+
console.error(`${this.tag} Max retries exceeded for ${batch.destination.name}, dropping ${batch.items.length} ${this.config.noun}s`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Retry pending batches that are ready.
|
|
136
|
+
* Called by cron job or manually.
|
|
137
|
+
*/
|
|
138
|
+
async retryPending() {
|
|
139
|
+
const now = Date.now();
|
|
140
|
+
const retryBatches = this.pendingBatches.filter((batch) => batch.nextRetryAt && batch.nextRetryAt <= now);
|
|
141
|
+
if (retryBatches.length > 0) {
|
|
142
|
+
// Remove from pending
|
|
143
|
+
this.pendingBatches = this.pendingBatches.filter((batch) => !retryBatches.includes(batch));
|
|
144
|
+
// Retry each batch
|
|
145
|
+
for (const batch of retryBatches) {
|
|
146
|
+
this.submitBatch(batch); // Fire-and-forget
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Close queue and flush remaining items.
|
|
152
|
+
*/
|
|
153
|
+
async close() {
|
|
154
|
+
if (this.closed) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
this.closed = true;
|
|
158
|
+
// Flush remaining items
|
|
159
|
+
if (this.queue.length > 0) {
|
|
160
|
+
await this.flush();
|
|
161
|
+
}
|
|
162
|
+
// Wait for pending batches (with timeout)
|
|
163
|
+
const timeout = 5000; // 5 seconds
|
|
164
|
+
const startTime = Date.now();
|
|
165
|
+
while (this.pendingBatches.length > 0 && Date.now() - startTime < timeout) {
|
|
166
|
+
await this.retryPending();
|
|
167
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
168
|
+
}
|
|
169
|
+
if (this.pendingBatches.length > 0) {
|
|
170
|
+
console.warn(`${this.tag} Closing with ${this.pendingBatches.length} pending batches (timed out)`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Get queue statistics.
|
|
175
|
+
*/
|
|
176
|
+
getStats() {
|
|
177
|
+
return { ...this.stats };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=batch-queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"batch-queue.js","sourceRoot":"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,KAAK,EAAE,CAAC;QACrB,CAAC;QAED,0CAA0C;QAC1C,MAAM,OAAO,GAAG,IAAI,CAAC,CAAC,YAAY;QAClC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAE7B,OAAO,IAAI,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,GAAG,OAAO,EAAE,CAAC;YAC1E,MAAM,IAAI,CAAC,YAAY,EAAE,CAAC;YAC1B,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;QAC3D,CAAC;QAED,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACnC,OAAO,CAAC,IAAI,CACV,GAAG,IAAI,CAAC,GAAG,iBAAiB,IAAI,CAAC,cAAc,CAAC,MAAM,8BAA8B,CACrF,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;OAEG;IACH,QAAQ;QACN,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC;CACF"}
|
|
@@ -83,6 +83,7 @@ export declare class ConsentService {
|
|
|
83
83
|
delegationToken: string;
|
|
84
84
|
delegationId: string;
|
|
85
85
|
userDid?: string;
|
|
86
|
+
authorizationType?: string;
|
|
86
87
|
}, serverOrigin?: string): Promise<boolean>;
|
|
87
88
|
/**
|
|
88
89
|
* Initialize audit service - fetches config from remote API
|
|
@@ -384,6 +385,21 @@ export declare class ConsentService {
|
|
|
384
385
|
* @returns Parsed body object
|
|
385
386
|
*/
|
|
386
387
|
private parseRequestBody;
|
|
388
|
+
/**
|
|
389
|
+
* Interpret the `CONSENT_REQUIRE_IDENTITY` env override that gates the
|
|
390
|
+
* identity-required refusal in `handleApproval`. Defaults to enabled - only
|
|
391
|
+
* an explicit `"false"`/`"0"` disables it, mirroring the escape-hatch
|
|
392
|
+
* convention used for `MCPI_HTTP_CHALLENGE` in config.ts.
|
|
393
|
+
*/
|
|
394
|
+
private isIdentityRequired;
|
|
395
|
+
/**
|
|
396
|
+
* 403 returned by `handleApproval` when a credential/OAuth clickwrap mint is
|
|
397
|
+
* attempted without a valid, grant-bound identity assertion (A3, spec §3.2).
|
|
398
|
+
* A distinct `error_code` from the plain `identity_required` gate so callers
|
|
399
|
+
* and tests can tell "no server-signed proof of authentication" apart from
|
|
400
|
+
* "no identity resolved at all".
|
|
401
|
+
*/
|
|
402
|
+
private identityAssertionRequiredResponse;
|
|
387
403
|
/**
|
|
388
404
|
* Handle consent approval
|
|
389
405
|
*
|
|
@@ -434,6 +450,11 @@ export declare class ConsentService {
|
|
|
434
450
|
* @param delegationId - Delegation ID
|
|
435
451
|
* @param providedUserDid - Optional userDid passed directly (bypasses KV read for consistency)
|
|
436
452
|
* @param serverOrigin - Server origin for DO URL construction
|
|
453
|
+
* @param authorizationType - The delegation's authorization type as recorded
|
|
454
|
+
* at mint (raw provider_type / authorization.type). Normalized here before
|
|
455
|
+
* persisting so the call-time enforcement gate (Task B2/B3) reads a
|
|
456
|
+
* consistent value ("password" | "oauth2" | "none" | …). Undefined → the
|
|
457
|
+
* field is omitted and the gate fails safe (never satisfies a typed tool).
|
|
437
458
|
*/
|
|
438
459
|
private storeDelegationToken;
|
|
439
460
|
/**
|
|
@@ -546,6 +567,109 @@ export declare class ConsentService {
|
|
|
546
567
|
* Constant-time string comparison to prevent timing attacks
|
|
547
568
|
*/
|
|
548
569
|
private constantTimeCompare;
|
|
570
|
+
/**
|
|
571
|
+
* Whether local/miniflare testing may relax consent security checks.
|
|
572
|
+
*
|
|
573
|
+
* Mirrors `consent-anchor-bridge.ts`'s `isTestConsentMode` verbatim: requires
|
|
574
|
+
* BOTH `MCPI_TEST_CONSENT === "true"` AND an explicit `development`
|
|
575
|
+
* environment. The flag alone must NEVER relax a check on a
|
|
576
|
+
* production/staging/unset deployment.
|
|
577
|
+
*
|
|
578
|
+
* NOT used by `mintIdentityAssertion`/`verifyIdentityAssertion` — those are
|
|
579
|
+
* strictly fail-closed with no dev relaxation of any kind (a review found
|
|
580
|
+
* that signing/verifying under an attacker-known empty key when this flag
|
|
581
|
+
* is active was a latent total-forgery bypass if the two dev flags ever
|
|
582
|
+
* co-occurred in production). If a dev bypass for the identity-assertion
|
|
583
|
+
* requirement is ever needed, it belongs at the CALL SITE (mirroring how
|
|
584
|
+
* the anchor bridge bypasses CSRF via this flag at the handler, before
|
|
585
|
+
* `validateCsrfToken` is even invoked — never inside the crypto itself).
|
|
586
|
+
* Public so the OAuth callback path (runtime/oauth-handler.ts) can gate its
|
|
587
|
+
* own redirect mint on the identical dev/test relaxation used by the
|
|
588
|
+
* credential redirect above, rather than re-deriving the two-flag check.
|
|
589
|
+
*/
|
|
590
|
+
isDevConsentTestMode(): boolean;
|
|
591
|
+
/**
|
|
592
|
+
* SHA-256 hex digest of the given scopes, sorted ascending and
|
|
593
|
+
* JSON-array-encoded. Used as the identity assertion's `scopesHash` so the
|
|
594
|
+
* signed grant binds to the scope *set* regardless of the order scopes
|
|
595
|
+
* arrive in the request body. Mirrors the digest-to-hex shape already used
|
|
596
|
+
* elsewhere in this codebase (e.g. `DelegationService.hashToken`).
|
|
597
|
+
*
|
|
598
|
+
* `JSON.stringify` (not a `\n`-joined string) is required for injectivity:
|
|
599
|
+
* a plain join collides distinct scope sets that share the same joined
|
|
600
|
+
* bytes — e.g. `["a\nb"]` and `["a", "b"]` both join to `"a\nb"`. JSON
|
|
601
|
+
* array encoding preserves each element's boundary (quoting + `,`
|
|
602
|
+
* separators), so distinct scope sets can never hash identically.
|
|
603
|
+
*/
|
|
604
|
+
private hashScopes;
|
|
605
|
+
/**
|
|
606
|
+
* Compute HMAC-SHA256 for identity-assertion generation/validation.
|
|
607
|
+
*
|
|
608
|
+
* Structurally identical to `computeCsrfHmac`, but `computeCsrfHmac`
|
|
609
|
+
* hardcodes the `csrf:` key prefix internally so it cannot be reused for a
|
|
610
|
+
* different domain — this mirrors its shape with an `identity:` prefix
|
|
611
|
+
* instead, so a CSRF token and an identity assertion are signed under
|
|
612
|
+
* disjoint keys and can never validate as one another.
|
|
613
|
+
*/
|
|
614
|
+
private computeIdentityHmac;
|
|
615
|
+
/**
|
|
616
|
+
* Mint a grant-bound, domain-separated HMAC identity assertion.
|
|
617
|
+
*
|
|
618
|
+
* Carries the `userDid` resolved by a completed authentication step
|
|
619
|
+
* (credential or OAuth) across the consent redirect, replacing the
|
|
620
|
+
* caller-forgeable `user_did` body field (see
|
|
621
|
+
* docs/superpowers/specs/2026-07-29-consent-approve-auth-gate-design.md
|
|
622
|
+
* §3.1). Format mirrors `storeCredentialCsrfToken`:
|
|
623
|
+
* `${hmacB64url}.${payloadB64url}`.
|
|
624
|
+
*
|
|
625
|
+
* Binds the grant, not just the identity: `agentDid`, `tool`, `scopesHash`
|
|
626
|
+
* (SHA-256 of the sorted scope set), and `projectId` are signed in and must
|
|
627
|
+
* be re-checked against the request body at mint time by the caller — a
|
|
628
|
+
* leaked assertion must not be replayable to mint an arbitrary grant under
|
|
629
|
+
* the victim's `userDid` within the 10-minute window.
|
|
630
|
+
*
|
|
631
|
+
* FAIL-CLOSED, ALWAYS: throws if `OAUTH_ENCRYPTION_SECRET` is unset or
|
|
632
|
+
* empty. There is deliberately NO dev/test relaxation inside this helper —
|
|
633
|
+
* signing under a predictable key (e.g. an empty-string fallback) would be
|
|
634
|
+
* an attacker-known key and a latent total-forgery bypass if a dev flag
|
|
635
|
+
* were ever misconfigured in production. A mint must never silently
|
|
636
|
+
* produce an unsigned/unverifiable token — unlike
|
|
637
|
+
* `storeCredentialCsrfToken`'s legacy "graceful degradation," which this
|
|
638
|
+
* assertion deliberately does not inherit. Any future dev/test bypass
|
|
639
|
+
* belongs at the call site (see `isDevConsentTestMode`'s docstring).
|
|
640
|
+
*
|
|
641
|
+
* Public so both authenticated redirect sites can mint under this service's
|
|
642
|
+
* `identity:`-keyed secret: the credential redirect (in-class) and the OAuth
|
|
643
|
+
* callback redirect (runtime/oauth-handler.ts, a separate module).
|
|
644
|
+
*/
|
|
645
|
+
mintIdentityAssertion(params: {
|
|
646
|
+
sid: string;
|
|
647
|
+
userDid: string;
|
|
648
|
+
provider: string;
|
|
649
|
+
agentDid: string;
|
|
650
|
+
tool: string;
|
|
651
|
+
scopes: string[];
|
|
652
|
+
projectId: string;
|
|
653
|
+
}): Promise<string>;
|
|
654
|
+
/**
|
|
655
|
+
* Verify a grant-bound identity assertion minted by `mintIdentityAssertion`.
|
|
656
|
+
*
|
|
657
|
+
* Returns the payload's `userDid` iff: the HMAC matches (constant-time),
|
|
658
|
+
* the assertion has not expired, and every bound field (`sid`, `agentDid`,
|
|
659
|
+
* `tool`, `projectId`, and `scopesHash` recomputed from `expected.scopes`)
|
|
660
|
+
* equals the caller-supplied `expected` values. Any mismatch, tamper, or
|
|
661
|
+
* expiry returns `null` — never throws, so callers can treat `null`
|
|
662
|
+
* uniformly as "no verified identity."
|
|
663
|
+
*
|
|
664
|
+
* FAIL-CLOSED, ALWAYS: returns `null` if `OAUTH_ENCRYPTION_SECRET` is unset
|
|
665
|
+
* or empty. There is deliberately NO dev/test relaxation inside this
|
|
666
|
+
* helper — mirrors `consent-anchor-bridge.ts`'s `validateCsrfToken`, NOT
|
|
667
|
+
* `validateCredentialCsrfToken`'s legacy "return true to allow flow to
|
|
668
|
+
* continue" behavior (the fail-open this PR closes). Any future dev/test
|
|
669
|
+
* bypass belongs at the call site (see `isDevConsentTestMode`'s
|
|
670
|
+
* docstring), never as a predictable/empty signing key inside this helper.
|
|
671
|
+
*/
|
|
672
|
+
private verifyIdentityAssertion;
|
|
549
673
|
/**
|
|
550
674
|
* Build full DelegationRecord format request (future format)
|
|
551
675
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"consent.service.d.ts","sourceRoot":"","sources":["../../src/services/consent.service.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,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