@open-everywhere/provider-google-chat 0.0.0-stage → 0.8.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.
Files changed (76) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +51 -0
  3. package/README.md +40 -2
  4. package/agent/channels/google-chat-auth.ts +145 -0
  5. package/agent/channels/google-chat-front.ts +338 -0
  6. package/agent/channels/google-chat-identity.ts +97 -0
  7. package/agent/channels/google-chat-message-ops.ts +30 -0
  8. package/agent/channels/google-chat-origin-codec.ts +32 -0
  9. package/agent/channels/google-chat-post-arm.ts +36 -0
  10. package/agent/channels/google-chat-rest.ts +87 -0
  11. package/agent/channels/google-chat.ts +194 -0
  12. package/agent/contributions.ts +16 -0
  13. package/agent/docs-generated.ts +120 -0
  14. package/agent/policy.ts +101 -0
  15. package/agent/provider-events.ts +18 -0
  16. package/agent/register.ts +48 -0
  17. package/agent/surface-parity.ts +64 -0
  18. package/dist/agent/channels/google-chat-auth.d.ts +39 -0
  19. package/dist/agent/channels/google-chat-auth.d.ts.map +1 -0
  20. package/dist/agent/channels/google-chat-auth.js +118 -0
  21. package/dist/agent/channels/google-chat-auth.js.map +1 -0
  22. package/dist/agent/channels/google-chat-front.d.ts +102 -0
  23. package/dist/agent/channels/google-chat-front.d.ts.map +1 -0
  24. package/dist/agent/channels/google-chat-front.js +258 -0
  25. package/dist/agent/channels/google-chat-front.js.map +1 -0
  26. package/dist/agent/channels/google-chat-identity.d.ts +28 -0
  27. package/dist/agent/channels/google-chat-identity.d.ts.map +1 -0
  28. package/dist/agent/channels/google-chat-identity.js +70 -0
  29. package/dist/agent/channels/google-chat-identity.js.map +1 -0
  30. package/dist/agent/channels/google-chat-message-ops.d.ts +10 -0
  31. package/dist/agent/channels/google-chat-message-ops.d.ts.map +1 -0
  32. package/dist/agent/channels/google-chat-message-ops.js +25 -0
  33. package/dist/agent/channels/google-chat-message-ops.js.map +1 -0
  34. package/dist/agent/channels/google-chat-origin-codec.d.ts +6 -0
  35. package/dist/agent/channels/google-chat-origin-codec.d.ts.map +1 -0
  36. package/dist/agent/channels/google-chat-origin-codec.js +31 -0
  37. package/dist/agent/channels/google-chat-origin-codec.js.map +1 -0
  38. package/dist/agent/channels/google-chat-post-arm.d.ts +11 -0
  39. package/dist/agent/channels/google-chat-post-arm.d.ts.map +1 -0
  40. package/dist/agent/channels/google-chat-post-arm.js +31 -0
  41. package/dist/agent/channels/google-chat-post-arm.js.map +1 -0
  42. package/dist/agent/channels/google-chat-rest.d.ts +21 -0
  43. package/dist/agent/channels/google-chat-rest.d.ts.map +1 -0
  44. package/dist/agent/channels/google-chat-rest.js +81 -0
  45. package/dist/agent/channels/google-chat-rest.js.map +1 -0
  46. package/dist/agent/channels/google-chat.d.ts +3 -0
  47. package/dist/agent/channels/google-chat.d.ts.map +1 -0
  48. package/dist/agent/channels/google-chat.js +157 -0
  49. package/dist/agent/channels/google-chat.js.map +1 -0
  50. package/dist/agent/contributions.d.ts +12 -0
  51. package/dist/agent/contributions.d.ts.map +1 -0
  52. package/dist/agent/contributions.js +16 -0
  53. package/dist/agent/contributions.js.map +1 -0
  54. package/dist/agent/docs-generated.d.ts +3 -0
  55. package/dist/agent/docs-generated.d.ts.map +1 -0
  56. package/dist/agent/docs-generated.js +118 -0
  57. package/dist/agent/docs-generated.js.map +1 -0
  58. package/dist/agent/policy.d.ts +38 -0
  59. package/dist/agent/policy.d.ts.map +1 -0
  60. package/dist/agent/policy.js +78 -0
  61. package/dist/agent/policy.js.map +1 -0
  62. package/dist/agent/provider-events.d.ts +3 -0
  63. package/dist/agent/provider-events.d.ts.map +1 -0
  64. package/dist/agent/provider-events.js +16 -0
  65. package/dist/agent/provider-events.js.map +1 -0
  66. package/dist/agent/register.d.ts +3 -0
  67. package/dist/agent/register.d.ts.map +1 -0
  68. package/dist/agent/register.js +37 -0
  69. package/dist/agent/register.js.map +1 -0
  70. package/dist/agent/surface-parity.d.ts +3 -0
  71. package/dist/agent/surface-parity.d.ts.map +1 -0
  72. package/dist/agent/surface-parity.js +60 -0
  73. package/dist/agent/surface-parity.js.map +1 -0
  74. package/package.json +116 -4
  75. package/tsconfig.build.json +22 -0
  76. package/tsconfig.json +15 -0
@@ -0,0 +1,118 @@
1
+ // google-chat-auth — the two credentials the Google Chat surface handles, and nothing else:
2
+ //
3
+ // 1. INBOUND: every event Google Chat POSTs to the app carries `Authorization: Bearer <JWT>`. The token is
4
+ // checked fully — signature against Google's published keys, issuer, audience and expiry — before the
5
+ // body is read as an event. The audience is the policy's (agent/google-chat/policy.ts in the app), never
6
+ // the request's own Host header, so a token minted for another app cannot open this route.
7
+ // 2. OUTBOUND: the app's service-account key (an operator secret, `GOOGLE_CHAT_SERVICE_ACCOUNT_KEY`) signs a
8
+ // JWT-bearer grant for a short-lived access token with the `chat.bot` scope. The token endpoint is PINNED
9
+ // here: the key file's own `token_uri` is ignored, so an edited key cannot send the signed grant elsewhere.
10
+ //
11
+ // Error messages name the failed check only. They never echo a token, a claim value or the key.
12
+ import { createRemoteJWKSet, importPKCS8, jwtVerify, SignJWT } from "jose";
13
+ import { getOperatorSecret } from "@open-everywhere/core/agent/adapter-api.js";
14
+ /** The service account Google Chat signs its events as. */
15
+ export const CHAT_ISSUER_ACCOUNT = "chat@system.gserviceaccount.com";
16
+ /** The JWKS of {@link CHAT_ISSUER_ACCOUNT} — the keys of a "project number" audience token. */
17
+ const CHAT_SYSTEM_JWKS_URL = "https://www.googleapis.com/service_accounts/v1/jwk/chat@system.gserviceaccount.com";
18
+ /** Google's OIDC keys — the keys of an "HTTP endpoint URL" audience token. */
19
+ const GOOGLE_OIDC_JWKS_URL = "https://www.googleapis.com/oauth2/v3/certs";
20
+ const GOOGLE_OIDC_ISSUERS = ["https://accounts.google.com", "accounts.google.com"];
21
+ /** The pinned token endpoint of the JWT-bearer grant. */
22
+ const GOOGLE_TOKEN_URL = "https://oauth2.googleapis.com/token";
23
+ /** The one scope the app asks for: act as the Chat app (post into spaces it is a member of). */
24
+ const CHAT_BOT_SCOPE = "https://www.googleapis.com/auth/chat.bot";
25
+ /** The operator secret holding the service-account key JSON. */
26
+ const SERVICE_ACCOUNT_SECRET = "GOOGLE_CHAT_SERVICE_ACCOUNT_KEY";
27
+ let liveChatKeys;
28
+ let liveOidcKeys;
29
+ function bearer(authorization) {
30
+ const m = /^Bearer\s+([A-Za-z0-9._-]+)$/u.exec(authorization?.trim() ?? "");
31
+ return m?.[1];
32
+ }
33
+ /**
34
+ * Verify one event's bearer token against the policy's audience. Fail closed: no header, a malformed token,
35
+ * a bad signature, a wrong issuer or audience, an expired token, or (endpoint-URL mode) a token for any
36
+ * account but Google Chat's own is refused.
37
+ */
38
+ export async function verifyGoogleChatToken(authorization, audience, deps = {}) {
39
+ if (audience === null)
40
+ return { ok: false, reason: "no audience is configured" };
41
+ const token = bearer(authorization);
42
+ if (token === undefined)
43
+ return { ok: false, reason: "no bearer token" };
44
+ try {
45
+ if (audience.kind === "project-number") {
46
+ const keys = deps.chatSystemKeys ?? (liveChatKeys ??= createRemoteJWKSet(new URL(CHAT_SYSTEM_JWKS_URL)));
47
+ await jwtVerify(token, keys, { issuer: CHAT_ISSUER_ACCOUNT, audience: audience.projectNumber, algorithms: ["RS256"] });
48
+ return { ok: true };
49
+ }
50
+ const keys = deps.googleOidcKeys ?? (liveOidcKeys ??= createRemoteJWKSet(new URL(GOOGLE_OIDC_JWKS_URL)));
51
+ const { payload } = await jwtVerify(token, keys, { issuer: GOOGLE_OIDC_ISSUERS, audience: audience.url, algorithms: ["RS256"] });
52
+ // Any Google account can mint an id token for any audience URL, so the token must name Chat's own account.
53
+ if (payload["email"] !== CHAT_ISSUER_ACCOUNT || payload["email_verified"] !== true) {
54
+ return { ok: false, reason: "the token is not Google Chat's" };
55
+ }
56
+ return { ok: true };
57
+ }
58
+ catch (e) {
59
+ const code = e.code;
60
+ return { ok: false, reason: `the token did not verify${typeof code === "string" ? ` (${code})` : ""}` };
61
+ }
62
+ }
63
+ /** Parse the key JSON. Throws a key-free error on anything that is not a service-account key. */
64
+ export function parseServiceAccountKey(raw) {
65
+ let j;
66
+ try {
67
+ j = JSON.parse(raw);
68
+ }
69
+ catch {
70
+ throw new Error(`${SERVICE_ACCOUNT_SECRET} is not JSON.`);
71
+ }
72
+ const o = (typeof j === "object" && j !== null ? j : {});
73
+ if (o["type"] !== "service_account" || typeof o["client_email"] !== "string" || typeof o["private_key"] !== "string") {
74
+ throw new Error(`${SERVICE_ACCOUNT_SECRET} is not a Google service-account key.`);
75
+ }
76
+ return { clientEmail: o["client_email"], privateKey: o["private_key"] };
77
+ }
78
+ let cached;
79
+ /** Forget the cached access token (tests). */
80
+ export function resetGoogleChatTokenCache() {
81
+ cached = undefined;
82
+ }
83
+ /** A `chat.bot` access token for the app's service account, cached until a minute before it expires. */
84
+ export async function googleChatAccessToken(deps = {}) {
85
+ const now = (deps.now ?? Date.now)();
86
+ const key = parseServiceAccountKey(await (deps.serviceAccountKey ?? (async () => (await getOperatorSecret(SERVICE_ACCOUNT_SECRET)).value))());
87
+ if (cached && cached.account === key.clientEmail && cached.expiresAt - 60_000 > now)
88
+ return cached.token;
89
+ let signingKey;
90
+ try {
91
+ signingKey = await importPKCS8(key.privateKey, "RS256");
92
+ }
93
+ catch {
94
+ throw new Error(`${SERVICE_ACCOUNT_SECRET} holds a private key that does not import.`);
95
+ }
96
+ const iat = Math.floor(now / 1000);
97
+ const assertion = await new SignJWT({ scope: CHAT_BOT_SCOPE })
98
+ .setProtectedHeader({ alg: "RS256", typ: "JWT" })
99
+ .setIssuer(key.clientEmail)
100
+ .setAudience(GOOGLE_TOKEN_URL)
101
+ .setIssuedAt(iat)
102
+ .setExpirationTime(iat + 3600)
103
+ .sign(signingKey);
104
+ const res = await (deps.fetch ?? fetch)(GOOGLE_TOKEN_URL, {
105
+ method: "POST",
106
+ headers: { "content-type": "application/x-www-form-urlencoded" },
107
+ body: new URLSearchParams({ grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer", assertion }).toString(),
108
+ });
109
+ if (!res.ok)
110
+ throw new Error(`the Google token endpoint refused the service-account grant (HTTP ${res.status}).`);
111
+ const body = (await res.json().catch(() => ({})));
112
+ if (typeof body.access_token !== "string" || body.access_token === "")
113
+ throw new Error("the Google token endpoint returned no access token.");
114
+ const ttl = typeof body.expires_in === "number" && body.expires_in > 0 ? body.expires_in : 3600;
115
+ cached = { token: body.access_token, expiresAt: now + ttl * 1000, account: key.clientEmail };
116
+ return body.access_token;
117
+ }
118
+ //# sourceMappingURL=google-chat-auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-chat-auth.js","sourceRoot":"","sources":["../../../agent/channels/google-chat-auth.ts"],"names":[],"mappings":"AAAA,4FAA4F;AAC5F,EAAE;AACF,6GAA6G;AAC7G,2GAA2G;AAC3G,8GAA8G;AAC9G,gGAAgG;AAChG,+GAA+G;AAC/G,+GAA+G;AAC/G,iHAAiH;AACjH,EAAE;AACF,gGAAgG;AAEhG,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAwB,MAAM,MAAM,CAAC;AACjG,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAG/E,2DAA2D;AAC3D,MAAM,CAAC,MAAM,mBAAmB,GAAG,iCAAiC,CAAC;AACrE,+FAA+F;AAC/F,MAAM,oBAAoB,GAAG,oFAAoF,CAAC;AAClH,8EAA8E;AAC9E,MAAM,oBAAoB,GAAG,4CAA4C,CAAC;AAC1E,MAAM,mBAAmB,GAAG,CAAC,6BAA6B,EAAE,qBAAqB,CAAC,CAAC;AACnF,yDAAyD;AACzD,MAAM,gBAAgB,GAAG,qCAAqC,CAAC;AAC/D,gGAAgG;AAChG,MAAM,cAAc,GAAG,0CAA0C,CAAC;AAClE,gEAAgE;AAChE,MAAM,sBAAsB,GAAG,iCAAiC,CAAC;AAUjE,IAAI,YAAyC,CAAC;AAC9C,IAAI,YAAyC,CAAC;AAE9C,SAAS,MAAM,CAAC,aA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@
1
+ import { type IngestedChatMessage, type UnifiedWakeOrigin, type WakeMessageInput, type WakeTrust } from "@open-everywhere/core/agent/adapter-api.js";
2
+ import { type GoogleChatPolicy } from "../policy.js";
3
+ import { type GoogleChatVerifyDeps } from "./google-chat-auth.js";
4
+ import { type OpenEverywhereGoogleChatAuth } from "./google-chat-identity.js";
5
+ import type { AuditDetail } from "@open-everywhere/core/agent/adapter-api.js";
6
+ type GoogleChatOrigin = Extract<UnifiedWakeOrigin, {
7
+ platform: "google-chat";
8
+ }>;
9
+ /** Why the front refused a verified message. */
10
+ export type GoogleChatRefusal = "group-chat" | "dm-off" | "external-dm" | "no-domain";
11
+ export type GoogleChatFrontOutcome = {
12
+ readonly kind: "not-configured";
13
+ } | {
14
+ readonly kind: "unauthorized";
15
+ readonly reason: string;
16
+ } | {
17
+ readonly kind: "ignored";
18
+ readonly why: string;
19
+ } | {
20
+ readonly kind: "duplicate";
21
+ } | {
22
+ readonly kind: "refused";
23
+ readonly reason: GoogleChatRefusal;
24
+ } | {
25
+ readonly kind: "admitted";
26
+ readonly route: "room" | "dm";
27
+ readonly surfaceKey: string;
28
+ };
29
+ /** The parsed facts of one MESSAGE event — only what the gate and the wake read. */
30
+ export interface GoogleChatMessageEvent {
31
+ readonly space: string;
32
+ readonly spaceKind: "space" | "dm" | "group";
33
+ readonly messageName: string;
34
+ readonly thread?: string;
35
+ readonly threadReply: boolean;
36
+ readonly sender: string;
37
+ readonly senderName?: string;
38
+ readonly domainId?: string;
39
+ /** The text with the app's mention removed (`argumentText`), else the whole text. */
40
+ readonly text: string;
41
+ /** Did the message @-mention the app, or invoke one of its slash commands? */
42
+ readonly addressed: boolean;
43
+ readonly createTime: string;
44
+ }
45
+ /** Everything one admitted message's dispatch needs. */
46
+ export interface GoogleChatWakePlan {
47
+ readonly surfaceKey: string;
48
+ readonly route: "room" | "dm";
49
+ readonly auth: OpenEverywhereGoogleChatAuth;
50
+ readonly origin: GoogleChatOrigin;
51
+ readonly trigger: WakeMessageInput;
52
+ readonly trust: "internal" | "external";
53
+ readonly ingested: readonly IngestedChatMessage[];
54
+ readonly event: GoogleChatMessageEvent;
55
+ }
56
+ export interface GoogleChatFrontDeps extends GoogleChatVerifyDeps {
57
+ readonly policy: () => GoogleChatPolicy;
58
+ /** Durable first-sighting claim (true = first time). */
59
+ readonly markSeen: (key: string) => Promise<boolean>;
60
+ /** Does a windowed domain own this space's surface key? (supervisorGrainFor + grainUsesUnifiedWindow.) */
61
+ readonly roomHasSeat: (surfaceKey: string) => boolean;
62
+ /** The per-message trust tier, resolved through the shared resolver over this auth. */
63
+ readonly wakeTrust: (auth: OpenEverywhereGoogleChatAuth, sender: string) => WakeTrust;
64
+ readonly dispatchRoom: (plan: GoogleChatWakePlan) => Promise<void>;
65
+ readonly dispatchDm: (plan: GoogleChatWakePlan) => Promise<void>;
66
+ readonly audit: (row: {
67
+ readonly action: string;
68
+ readonly workspace: string;
69
+ readonly surface?: string;
70
+ readonly principalId?: string;
71
+ readonly detail: AuditDetail;
72
+ }) => Promise<void>;
73
+ }
74
+ /**
75
+ * Parse one event body. Undefined (with a reason) for anything this surface does not take: not JSON, not a
76
+ * MESSAGE, a malformed name, or a sender that is not a human. Pure.
77
+ */
78
+ export declare function parseGoogleChatEvent(rawBody: string): {
79
+ readonly event: GoogleChatMessageEvent;
80
+ } | {
81
+ readonly ignored: string;
82
+ };
83
+ /** Render a plan's wake. */
84
+ export declare function renderGoogleChatWake(plan: GoogleChatWakePlan, wakeId: string, tail?: readonly WakeMessageInput[]): {
85
+ message: string;
86
+ context: readonly string[];
87
+ };
88
+ /** The whole path in one call (the tests' entry): verify, then process. */
89
+ export declare function handleGoogleChatDelivery(authorization: string | undefined, rawBody: string, deps: GoogleChatFrontDeps): Promise<GoogleChatFrontOutcome>;
90
+ /** The largest event body the route reads. A Chat message is at most 32 KB; the event wraps it. */
91
+ export declare const GOOGLE_CHAT_INGRESS_MAX_BODY_BYTES: number;
92
+ /**
93
+ * The whole HTTP route, eve-free so a test drives it. 🔴 THE ORDER IS THE DEFENCE: the policy (503) and the token
94
+ * (401) are checked BEFORE the body is read, so an unconfigured or unauthenticated caller cannot make the route
95
+ * read a byte. Then the body cap (413), then `{}` with the rest in the background (`waitUntil`).
96
+ */
97
+ export declare function handleGoogleChatRequest(req: Request, waitUntil: (p: Promise<unknown>) => void, deps: GoogleChatFrontDeps, hooks?: {
98
+ readonly log?: (msg: string) => void;
99
+ readonly onFailure?: (err: unknown, policy: GoogleChatPolicy) => Promise<void>;
100
+ }): Promise<Response>;
101
+ export {};
102
+ //# sourceMappingURL=google-chat-front.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-chat-front.d.ts","sourceRoot":"","sources":["../../../agent/channels/google-chat-front.ts"],"names":[],"mappings":"AAoBA,OAAO,EAKL,KAAK,mBAAmB,EAExB,KAAK,iBAAiB,EACtB,KAAK,gBAAgB,EACrB,KAAK,SAAS,EACf,MAAM,4CAA4C,CAAC;AACpD,OAAO,EAAwB,KAAK,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3E,OAAO,EAAyB,KAAK,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AACzF,OAAO,EAAuB,KAAK,4BAA4B,EAAE,MAAM,2BAA2B,CAAC;AAEnG,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4CAA4C,CAAC;AAE9E,KAAK,gBAAgB,GAAG,OAAO,CAAC,iBAAiB,EAAE;IAAE,QAAQ,EAAE,aAAa,CAAA;CAAE,CAAC,CAAC;AAKhF,gDAAgD;AAChD,MAAM,MAAM,iBAAiB,GAAG,YAAY,GAAG,QAAQ,GAAG,aAAa,GAAG,WAAW,CAAC;AAEtF,MAAM,MAAM,sBAAsB,GAC9B;IAAE,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAA;CAAE,GACnC;IAAE,QAAQ,CAAC,IAAI,EAAE,cAAc,CAAC;IAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAC1D;IAAE,Q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EAAE,sBAAsB,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAiD/H;AA2ED,4BAA4B;AAC5B,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,SAAS,gBAAgB,EAAE,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAA;CAAE,CAGlK;AAwDD,2EAA2E;AAC3E,wBAAsB,wBAAwB,CAAC,aAAa,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,mBAAmB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAI7J;AAED,mGAAmG;AACnG,eAAO,MAAM,kCAAkC,QAAa,CAAC;AAE7D;;;;GAIG;AACH,wBAAsB,uBAAuB,CAC3C,GAAG,EAAE,OAAO,EACZ,SAAS,EAAE,CAAC,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,EACxC,IAAI,EAAE,mBAAmB,EACzB,KAAK,GAAE;IAAE,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;IAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,gBAAgB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;CAAO,GACnI,OAAO,CAAC,QAAQ,CAAC,CA0BnB"}
@@ -0,0 +1,258 @@
1
+ // google-chat-front — the eve-free core of the Google Chat surface's inbound path. One delivery in, one outcome
2
+ // out: not configured, unauthorized, acknowledged and ignored, a duplicate, refused (audited, no model turn), or
3
+ // admitted and delivered into a seat. The live wiring (google-chat.ts) supplies the store, the config and the two
4
+ // seat dispatches.
5
+ //
6
+ // THE ORDER IS LOAD-BEARING:
7
+ // 1. policy — the surface SHIPS OFF. A disabled policy, or one with no audience, answers "not configured"
8
+ // before the body is read: an external ingress nobody configured is not reachable.
9
+ // 2. verify — the bearer token is checked against Google's keys and the policy's audience (fail closed).
10
+ // Nothing before it reads the body, and an unverified delivery writes no audit row.
11
+ // 3. parse — anything that is not a well-formed MESSAGE event from a HUMAN sender is acknowledged and
12
+ // dropped (an ADDED_TO_SPACE, a bot's own message, a CARD_CLICKED this surface does not use).
13
+ // 4. dedupe — the message's resource name is claimed once, so a retried event never wakes twice.
14
+ // 5. route — the person's own DM with the app wakes their DM seat (internal people only, and only while
15
+ // the policy allows direct messages); a group chat is refused; a space wakes the standing seat
16
+ // that owns `google-chat/<workspace>/<space id>`, only when the message @-mentions the app or
17
+ // is a slash command. With no standing-seat window for the space it is refused `no-domain`.
18
+ // 6. wake — the message's own text (the mention removed) renders as the graded trigger. An outside sender
19
+ // stamps the non-member carriage, exactly as a guest's message does on every chat surface.
20
+ import { mkMessageTrigger, readCappedBody, renderWake, stampNonMemberChatAuth, } from "@open-everywhere/core/agent/adapter-api.js";
21
+ import { googleChatSurfaceKey } from "../policy.js";
22
+ import { verifyGoogleChatToken } from "./google-chat-auth.js";
23
+ import { buildGoogleChatAuth } from "./google-chat-identity.js";
24
+ import { isMessageNameIn, isSpaceName, isThreadNameIn } from "./google-chat-rest.js";
25
+ /** The largest message text that reaches the model; the rest is cut and the cut is said. */
26
+ const GOOGLE_CHAT_MAX_INBOUND_CHARS = 20_000;
27
+ function rec(v) {
28
+ return typeof v === "object" && v !== null && !Array.isArray(v) ? v : undefined;
29
+ }
30
+ function str(v) {
31
+ return typeof v === "string" && v.length > 0 ? v : undefined;
32
+ }
33
+ /**
34
+ * Parse one event body. Undefined (with a reason) for anything this surface does not take: not JSON, not a
35
+ * MESSAGE, a malformed name, or a sender that is not a human. Pure.
36
+ */
37
+ export function parseGoogleChatEvent(rawBody) {
38
+ let j;
39
+ try {
40
+ j = JSON.parse(rawBody);
41
+ }
42
+ catch {
43
+ return { ignored: "not-json" };
44
+ }
45
+ const e = rec(j);
46
+ if (!e || e["type"] !== "MESSAGE")
47
+ return { ignored: "not-a-message" };
48
+ const space = rec(e["space"]);
49
+ const message = rec(e["message"]);
50
+ const sender = rec(message?.["sender"]);
51
+ const spaceName = space?.["name"];
52
+ if (!space || !isSpaceName(spaceName) || !message)
53
+ return { ignored: "malformed" };
54
+ const messageName = message["name"];
55
+ if (!isMessageNameIn(messageName, spaceName))
56
+ return { ignored: "malformed" };
57
+ const threadName = rec(message["thread"])?.["name"];
58
+ if (threadName !== undefined && !isThreadNameIn(threadName, spaceName))
59
+ return { ignored: "malformed" };
60
+ // A bot's message (including this app's own) never wakes a seat: that is the loop guard.
61
+ if (sender?.["type"] !== "HUMAN")
62
+ return { ignored: "not-a-human" };
63
+ const senderName = str(sender["name"]);
64
+ if (senderName === undefined || !/^users\/[A-Za-z0-9_-]{1,128}$/u.test(senderName))
65
+ return { ignored: "malformed" };
66
+ const spaceType = str(space["spaceType"]) ?? (space["type"] === "DM" ? "DIRECT_MESSAGE" : "SPACE");
67
+ const spaceKind = spaceType === "DIRECT_MESSAGE" ? (space["singleUserBotDm"] === true ? "dm" : "group") : spaceType === "GROUP_CHAT" ? "group" : "space";
68
+ const annotations = Array.isArray(message["annotations"]) ? message["annotations"] : [];
69
+ const mentionsApp = annotations.some((a) => {
70
+ const an = rec(a);
71
+ return an?.["type"] === "USER_MENTION" && rec(rec(an["userMention"])?.["user"])?.["type"] === "BOT";
72
+ });
73
+ const slash = rec(message["slashCommand"]) !== undefined;
74
+ const text = (str(message["argumentText"]) ?? str(message["text"]) ?? "").trim();
75
+ const domainId = str(sender["domainId"]);
76
+ const displayName = str(sender["displayName"]);
77
+ return {
78
+ event: {
79
+ space: spaceName,
80
+ spaceKind,
81
+ messageName,
82
+ ...(threadName !== undefined ? { thread: threadName } : {}),
83
+ threadReply: message["threadReply"] === true && threadName !== undefined,
84
+ sender: senderName,
85
+ ...(displayName !== undefined ? { senderName: displayName } : {}),
86
+ ...(domainId !== undefined ? { domainId } : {}),
87
+ text,
88
+ addressed: mentionsApp || slash,
89
+ createTime: str(message["createTime"]) ?? str(e["eventTime"]) ?? new Date(0).toISOString(),
90
+ },
91
+ };
92
+ }
93
+ /** The sender's trust: internal only for a domain id the policy lists. */
94
+ function googleChatSenderTrust(policy, domainId) {
95
+ return domainId !== undefined && policy.internalDomainIds.includes(domainId) ? "internal" : "external";
96
+ }
97
+ /** Build the wake plan for a message the gate admitted. Pure apart from the trust resolver dep. */
98
+ function planGoogleChatWake(input, wakeTrust) {
99
+ const { event, policy, route } = input;
100
+ const trustTier = googleChatSenderTrust(policy, event.domainId);
101
+ const surfaceKey = googleChatSurfaceKey(policy, event.space);
102
+ const base = buildGoogleChatAuth({
103
+ workspace: policy.workspace,
104
+ space: event.space,
105
+ mode: route === "dm" ? "dm" : "channel",
106
+ trust: trustTier,
107
+ sender: event.sender,
108
+ ...(event.thread !== undefined ? { thread: event.thread } : {}),
109
+ messageName: event.messageName,
110
+ });
111
+ const trust = wakeTrust(base, event.sender);
112
+ const ingested = [{ trust, refuted: trustTier === "external" }];
113
+ const auth = stampNonMemberChatAuth(base, ingested, route === "dm" ? { dm: true } : { surfaceKey });
114
+ const body = event.text === ""
115
+ ? "(the message had no text)"
116
+ : event.text.length > GOOGLE_CHAT_MAX_INBOUND_CHARS
117
+ ? `${event.text.slice(0, GOOGLE_CHAT_MAX_INBOUND_CHARS)}\n[… the message was cut at ${GOOGLE_CHAT_MAX_INBOUND_CHARS} characters]`
118
+ : event.text;
119
+ return {
120
+ surfaceKey,
121
+ route,
122
+ auth,
123
+ origin: {
124
+ platform: "google-chat",
125
+ workspace: policy.workspace,
126
+ space: event.space,
127
+ ...(event.thread !== undefined ? { thread: event.thread } : {}),
128
+ messageName: event.messageName,
129
+ ...(event.threadReply ? { threadReply: true } : {}),
130
+ },
131
+ trigger: {
132
+ from: "human",
133
+ author: event.senderName ?? event.sender,
134
+ authorId: event.sender,
135
+ id: event.messageName,
136
+ sentAt: event.createTime,
137
+ trust,
138
+ mention: true,
139
+ body,
140
+ },
141
+ trust: trustTier,
142
+ ingested,
143
+ event,
144
+ };
145
+ }
146
+ /** The NormalizedWake for an admitted message. */
147
+ function buildGoogleChatWake(plan, wakeId, tail) {
148
+ return {
149
+ platform: "google-chat",
150
+ reason: plan.route === "dm" ? "dm" : "mention",
151
+ addressing: "direct-mention",
152
+ containerNoun: "space",
153
+ wakeId,
154
+ locus: { channel: { id: plan.origin.space }, ...(plan.origin.thread !== undefined ? { thread: { ts: plan.origin.thread } } : {}) },
155
+ backscroll: tail && tail.length > 0 ? tail : undefined,
156
+ trigger: mkMessageTrigger(plan.trigger),
157
+ };
158
+ }
159
+ /** Render a plan's wake. */
160
+ export function renderGoogleChatWake(plan, wakeId, tail) {
161
+ const r = renderWake(buildGoogleChatWake(plan, wakeId, tail));
162
+ return { message: r.message, context: r.context ?? [] };
163
+ }
164
+ /** Steps 1 and 2: is the surface configured, and is this delivery Google Chat's? */
165
+ async function verifyGoogleChatDelivery(authorization, deps) {
166
+ const policy = deps.policy();
167
+ if (!policy.enabled || policy.audience === null)
168
+ return { ok: false, outcome: { kind: "not-configured" } };
169
+ const verdict = await verifyGoogleChatToken(authorization, policy.audience, deps);
170
+ if (!verdict.ok)
171
+ return { ok: false, outcome: { kind: "unauthorized", reason: verdict.reason } };
172
+ return { ok: true, policy };
173
+ }
174
+ /** Steps 3 to 6 for a delivery already VERIFIED. Never throws for a bad event; a dispatch failure propagates. */
175
+ async function processVerifiedGoogleChatEvent(policy, rawBody, deps) {
176
+ const parsed = parseGoogleChatEvent(rawBody);
177
+ if ("ignored" in parsed)
178
+ return { kind: "ignored", why: parsed.ignored };
179
+ const event = parsed.event;
180
+ if (event.spaceKind === "space" && !event.addressed)
181
+ return { kind: "ignored", why: "not-addressed" };
182
+ if (!(await deps.markSeen(`google-chat:${event.messageName}`)))
183
+ return { kind: "duplicate" };
184
+ const surfaceKey = googleChatSurfaceKey(policy, event.space);
185
+ const refuse = async (reason) => {
186
+ await deps.audit({
187
+ action: "google-chat.refused",
188
+ workspace: policy.workspace,
189
+ surface: surfaceKey,
190
+ principalId: event.sender,
191
+ detail: { reason, space: event.space, domainId: event.domainId ?? null },
192
+ });
193
+ return { kind: "refused", reason };
194
+ };
195
+ if (event.spaceKind === "group")
196
+ return refuse("group-chat");
197
+ const route = event.spaceKind === "dm" ? "dm" : "room";
198
+ if (route === "dm") {
199
+ if (!policy.directMessages)
200
+ return refuse("dm-off");
201
+ // A DM seat reads its person's private layer; an outside person never gets one.
202
+ if (googleChatSenderTrust(policy, event.domainId) !== "internal")
203
+ return refuse("external-dm");
204
+ }
205
+ else if (!deps.roomHasSeat(surfaceKey)) {
206
+ return refuse("no-domain");
207
+ }
208
+ const plan = planGoogleChatWake({ event, policy, route }, deps.wakeTrust);
209
+ if (route === "room")
210
+ await deps.dispatchRoom(plan);
211
+ else
212
+ await deps.dispatchDm(plan);
213
+ await deps.audit({
214
+ action: "google-chat.admitted",
215
+ workspace: policy.workspace,
216
+ surface: plan.surfaceKey,
217
+ principalId: event.sender,
218
+ detail: { space: event.space, route, trust: plan.trust, domainId: event.domainId ?? null, threadReply: event.threadReply },
219
+ });
220
+ return { kind: "admitted", route, surfaceKey: plan.surfaceKey };
221
+ }
222
+ /** The whole path in one call (the tests' entry): verify, then process. */
223
+ export async function handleGoogleChatDelivery(authorization, rawBody, deps) {
224
+ const v = await verifyGoogleChatDelivery(authorization, deps);
225
+ if (!v.ok)
226
+ return v.outcome;
227
+ return processVerifiedGoogleChatEvent(v.policy, rawBody, deps);
228
+ }
229
+ /** The largest event body the route reads. A Chat message is at most 32 KB; the event wraps it. */
230
+ export const GOOGLE_CHAT_INGRESS_MAX_BODY_BYTES = 512 * 1024;
231
+ /**
232
+ * The whole HTTP route, eve-free so a test drives it. 🔴 THE ORDER IS THE DEFENCE: the policy (503) and the token
233
+ * (401) are checked BEFORE the body is read, so an unconfigured or unauthenticated caller cannot make the route
234
+ * read a byte. Then the body cap (413), then `{}` with the rest in the background (`waitUntil`).
235
+ */
236
+ export async function handleGoogleChatRequest(req, waitUntil, deps, hooks = {}) {
237
+ const log = hooks.log ?? (() => undefined);
238
+ const verified = await verifyGoogleChatDelivery(req.headers.get("authorization") ?? undefined, deps);
239
+ if (!verified.ok) {
240
+ if (verified.outcome.kind === "unauthorized") {
241
+ log(`delivery refused (401): ${verified.outcome.reason}`);
242
+ return new Response("unauthorized", { status: 401 });
243
+ }
244
+ return new Response("google chat surface not configured", { status: 503 });
245
+ }
246
+ const read = await readCappedBody(req, GOOGLE_CHAT_INGRESS_MAX_BODY_BYTES);
247
+ if ("tooLarge" in read)
248
+ return new Response(`payload exceeds ${GOOGLE_CHAT_INGRESS_MAX_BODY_BYTES} bytes`, { status: 413 });
249
+ waitUntil(processVerifiedGoogleChatEvent(verified.policy, read.body, deps)
250
+ .then((outcome) => log(`event ${outcome.kind}${outcome.kind === "refused" ? ` (${outcome.reason})` : outcome.kind === "admitted" ? ` → ${outcome.route} ${outcome.surfaceKey}` : outcome.kind === "ignored" ? ` (${outcome.why})` : ""}`))
251
+ .catch(async (err) => {
252
+ log(`event FAILED after the ack: ${err instanceof Error ? err.name : "error"}`);
253
+ await hooks.onFailure?.(err, verified.policy);
254
+ }));
255
+ // `{}` = no synchronous message. The seat answers through the post arm.
256
+ return new Response("{}", { status: 200, headers: { "content-type": "application/json" } });
257
+ }
258
+ //# sourceMappingURL=google-chat-front.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-chat-front.js","sourceRoot":"","sources":["../../../agent/channels/google-chat-front.ts"],"names":[],"mappings":"AAAA,gHAAgH;AAChH,iHAAiH;AACjH,kHAAkH;AAClH,mBAAmB;AACnB,EAAE;AACF,6BAA6B;AAC7B,6GAA6G;AAC7G,kGAAkG;AAClG,4GAA4G;AAC5G,mGAAmG;AACnG,0GAA0G;AAC1G,6GAA6G;AAC7G,oGAAoG;AACpG,4GAA4G;AAC5G,8GAA8G;AAC9G,6GAA6G;AAC7G,2GAA2G;AAC3G,+GAA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AI,CAAC;SAC7D,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAChB,GAAG,CACD,SAAS,OAAO,CAAC,IAAI,GAAG,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAClN,CACF;SACA,KAAK,CAAC,KAAK,EAAE,GAAY,EAAE,EAAE;QAC5B,GAAG,CAAC,+BAA+B,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC;QAChF,MAAM,KAAK,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IAChD,CAAC,CAAC,CACL,CAAC;IACF,wEAAwE;IACxE,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,EAAE,EAAE,cAAc,EAAE,kBAAkB,EAAE,EAAE,CAAC,CAAC;AAC9F,CAAC"}
@@ -0,0 +1,28 @@
1
+ import type { IdentityMapper } from "@open-everywhere/core/agent/adapter-api.js";
2
+ /** The structural match for eve's SessionAuthContext. */
3
+ export interface OpenEverywhereGoogleChatAuth {
4
+ readonly authenticator: string;
5
+ readonly principalId: string;
6
+ readonly principalType: string;
7
+ readonly attributes: Record<string, string>;
8
+ }
9
+ export interface GoogleChatIdentityInput {
10
+ readonly workspace: string;
11
+ /** The space resource name (`spaces/<id>`). */
12
+ readonly space: string;
13
+ readonly mode: "channel" | "dm";
14
+ readonly trust: "internal" | "external";
15
+ /** The sender's user resource name (`users/<id>`). */
16
+ readonly sender: string;
17
+ readonly thread?: string;
18
+ readonly messageName: string;
19
+ }
20
+ /** Build the auth context the Google Chat surface dispatches with. */
21
+ export declare function buildGoogleChatAuth(input: GoogleChatIdentityInput): OpenEverywhereGoogleChatAuth;
22
+ /**
23
+ * The Google Chat identity mapper. FAIL CLOSED on every field it cannot read: no mode reads `channel`, no trust
24
+ * attribute reads `external`, and visibility is always `private`. `trustVerified` is set for a DM only when the
25
+ * front verified the sender internal, so a DM never lights the internal `dm` layer for an outsider.
26
+ */
27
+ export declare const bindGoogleChat: IdentityMapper;
28
+ //# sourceMappingURL=google-chat-identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-chat-identity.d.ts","sourceRoot":"","sources":["../../../agent/channels/google-chat-identity.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAmB,cAAc,EAAe,MAAM,4CAA4C,CAAC;AAE/G,yDAAyD;AACzD,MAAM,WAAW,4BAA4B;IAC3C,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAC7C;AAED,MAAM,WAAW,uBAAuB;IACtC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,+CAA+C;IAC/C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,IAAI,CAAC;IAChC,QAAQ,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAAC;IACxC,sDAAsD;IACtD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;CAC9B;AAED,sEAAsE;AACtE,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,uBAAuB,GAAG,4BAA4B,CAehG;AAcD;;;;GAIG;AACH,eAAO,MAAM,cAAc,EAAE,cAsB5B,CAAC"}
@@ -0,0 +1,70 @@
1
+ // Pure Google Chat → Open Everywhere identity capture. The front (google-chat-front.ts) stows the HOST-DECIDED
2
+ // facts of one admitted event into the wake's auth `attributes`, and the mapper below interprets them into the
3
+ // binding. The security-load-bearing reading lives here, in one tested place.
4
+ //
5
+ // THE GRAIN:
6
+ // • workspace = the policy's `workspace` segment — one Google Workspace per deployment;
7
+ // • surface = the space id (`spaces/<id>` → `<id>`), so a domain's `reach` lists `google-chat/<ws>/<id>`;
8
+ // • mode = `dm` for the person's own direct message with the app, else `channel`;
9
+ // • visibility = ALWAYS `private`. A Chat space may be discoverable inside the org, but the event does not
10
+ // prove it, so nothing said in a space flows up to a shared tier by visibility alone;
11
+ // • trust = `internal` ONLY when the sender is a human whose Workspace domain id is one of the policy's
12
+ // `internalDomainIds`. Everyone else is `external`;
13
+ // • principal = the sender's Chat user resource name (`users/<id>`), never a display name or an email.
14
+ //
15
+ // §AUTH-ATTRIBUTE PROVENANCE: every attribute below is written by the front from the token-verified event and
16
+ // the policy, never from the message text and never by a model.
17
+ /** Build the auth context the Google Chat surface dispatches with. */
18
+ export function buildGoogleChatAuth(input) {
19
+ return {
20
+ authenticator: "google-chat",
21
+ principalId: input.sender,
22
+ principalType: "google-chat-user",
23
+ attributes: {
24
+ gchat_workspace: input.workspace,
25
+ gchat_space: input.space,
26
+ gchat_surface: input.space.slice(input.space.indexOf("/") + 1),
27
+ gchat_mode: input.mode,
28
+ gchat_trust: input.trust,
29
+ gchat_message: input.messageName,
30
+ ...(input.thread !== undefined ? { gchat_thread: input.thread } : {}),
31
+ },
32
+ };
33
+ }
34
+ function authOf(ctx) {
35
+ return ctx.session?.auth?.current ?? ctx.session?.auth?.initiator ?? null;
36
+ }
37
+ function attr(a, key) {
38
+ const v = a?.[key];
39
+ const first = Array.isArray(v) ? v[0] : v;
40
+ return typeof first === "string" && first.length > 0 ? first : undefined;
41
+ }
42
+ /**
43
+ * The Google Chat identity mapper. FAIL CLOSED on every field it cannot read: no mode reads `channel`, no trust
44
+ * attribute reads `external`, and visibility is always `private`. `trustVerified` is set for a DM only when the
45
+ * front verified the sender internal, so a DM never lights the internal `dm` layer for an outsider.
46
+ */
47
+ export const bindGoogleChat = (ctx) => {
48
+ const auth = authOf(ctx);
49
+ const a = auth?.attributes;
50
+ const mode = attr(a, "gchat_mode") === "dm" ? "dm" : "channel";
51
+ const internal = attr(a, "gchat_trust") === "internal";
52
+ const space = attr(a, "gchat_space");
53
+ const thread = attr(a, "gchat_thread");
54
+ const messageName = attr(a, "gchat_message");
55
+ const principalId = typeof auth?.principalId === "string" && auth.principalId.length > 0 ? auth.principalId : undefined;
56
+ return {
57
+ workspace: attr(a, "gchat_workspace"),
58
+ surface: attr(a, "gchat_surface"),
59
+ mode,
60
+ visibility: "private",
61
+ ...(internal ? { trust: "internal", ...(mode === "dm" ? { trustVerified: true } : {}) } : { trust: "external" }),
62
+ ...(principalId ? { principalId } : {}),
63
+ // `channelId` is the space: the one address every shared room-notice path (an approval outcome note, a
64
+ // judge-lift notice) posts to through the surface message ops.
65
+ ...(space
66
+ ? { target: { space, channelId: space, ...(thread ? { thread } : {}), ...(messageName ? { messageName } : {}) } }
67
+ : {}),
68
+ };
69
+ };
70
+ //# sourceMappingURL=google-chat-identity.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"google-chat-identity.js","sourceRoot":"","sources":["../../../agent/channels/google-chat-identity.ts"],"names":[],"mappings":"AAAA,+GAA+G;AAC/G,+GAA+G;AAC/G,8EAA8E;AAC9E,EAAE;AACF,aAAa;AACb,2FAA2F;AAC3F,+GAA+G;AAC/G,0FAA0F;AAC1F,6GAA6G;AAC7G,uGAAuG;AACvG,+GAA+G;AAC/G,qEAAqE;AACrE,0GAA0G;AAC1G,EAAE;AACF,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