@kodelyth/googlechat 2026.5.39 → 2026.5.42
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/api.ts +3 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +1 -0
- package/config-api.ts +2 -0
- package/contract-api.ts +5 -0
- package/dist/actions-YK1wn4ed.js +160 -0
- package/dist/api-BkZX4VNX.js +633 -0
- package/dist/api.js +3 -0
- package/dist/channel-DFZdjXD6.js +584 -0
- package/dist/channel-config-api.js +6 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-en3RNg9S.js +998 -0
- package/dist/contract-api.js +3 -0
- package/dist/doctor-contract-8SF6XoKj.js +151 -0
- package/dist/doctor-contract-api.js +2 -0
- package/dist/index.js +22 -0
- package/dist/runtime-api-DUH2Cg-0.js +29 -0
- package/dist/runtime-api.js +2 -0
- package/dist/secret-contract-DWX4ikgT.js +99 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/setup-plugin-api.js +75 -0
- package/dist/setup-surface-B3Fa7XRx.js +321 -0
- package/dist/test-api.js +3 -0
- package/doctor-contract-api.ts +1 -0
- package/index.ts +20 -0
- package/klaw.plugin.json +2 -967
- package/package.json +4 -4
- package/runtime-api.ts +55 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +13 -0
- package/setup-plugin-api.ts +3 -0
- package/src/accounts.ts +181 -0
- package/src/actions.test.ts +289 -0
- package/src/actions.ts +227 -0
- package/src/api.ts +316 -0
- package/src/approval-auth.test.ts +24 -0
- package/src/approval-auth.ts +32 -0
- package/src/auth.ts +218 -0
- package/src/channel-config.test.ts +39 -0
- package/src/channel.adapters.ts +340 -0
- package/src/channel.deps.runtime.ts +29 -0
- package/src/channel.runtime.ts +17 -0
- package/src/channel.setup.ts +98 -0
- package/src/channel.test.ts +784 -0
- package/src/channel.ts +277 -0
- package/src/config-schema.test.ts +31 -0
- package/src/config-schema.ts +3 -0
- package/src/doctor-contract.test.ts +75 -0
- package/src/doctor-contract.ts +182 -0
- package/src/doctor.ts +57 -0
- package/src/gateway.ts +63 -0
- package/src/google-auth.runtime.test.ts +543 -0
- package/src/google-auth.runtime.ts +568 -0
- package/src/group-policy.ts +17 -0
- package/src/monitor-access.test.ts +491 -0
- package/src/monitor-access.ts +465 -0
- package/src/monitor-durable.test.ts +39 -0
- package/src/monitor-durable.ts +23 -0
- package/src/monitor-reply-delivery.ts +156 -0
- package/src/monitor-routing.ts +65 -0
- package/src/monitor-types.ts +33 -0
- package/src/monitor-webhook.test.ts +587 -0
- package/src/monitor-webhook.ts +303 -0
- package/src/monitor.reply-delivery.test.ts +144 -0
- package/src/monitor.test.ts +159 -0
- package/src/monitor.ts +527 -0
- package/src/monitor.webhook-routing.test.ts +257 -0
- package/src/runtime.ts +9 -0
- package/src/secret-contract.test.ts +60 -0
- package/src/secret-contract.ts +161 -0
- package/src/setup-core.ts +40 -0
- package/src/setup-surface.ts +243 -0
- package/src/setup.test.ts +619 -0
- package/src/targets.test.ts +453 -0
- package/src/targets.ts +66 -0
- package/src/types.config.ts +3 -0
- package/src/types.ts +73 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-config-api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/doctor-contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-entry.js +0 -7
- package/setup-plugin-api.js +0 -7
- package/test-api.js +0 -7
package/src/auth.ts
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
import { fetchWithSsrFGuard } from "../runtime-api.js";
|
|
3
|
+
import type { ResolvedGoogleChatAccount } from "./accounts.js";
|
|
4
|
+
import {
|
|
5
|
+
testing as googleAuthRuntimeTesting,
|
|
6
|
+
getGoogleAuthTransport,
|
|
7
|
+
loadGoogleAuthRuntime,
|
|
8
|
+
resolveValidatedGoogleChatCredentials,
|
|
9
|
+
} from "./google-auth.runtime.js";
|
|
10
|
+
|
|
11
|
+
const CHAT_SCOPE = "https://www.googleapis.com/auth/chat.bot";
|
|
12
|
+
const CHAT_ISSUER = "chat@system.gserviceaccount.com";
|
|
13
|
+
// Google Workspace Add-ons use a different service account pattern
|
|
14
|
+
const ADDON_ISSUER_PATTERN = /^service-\d+@gcp-sa-gsuiteaddons\.iam\.gserviceaccount\.com$/;
|
|
15
|
+
const CHAT_CERTS_URL =
|
|
16
|
+
"https://www.googleapis.com/service_accounts/v1/metadata/x509/chat@system.gserviceaccount.com";
|
|
17
|
+
|
|
18
|
+
async function readGoogleChatCertsResponse(response: Response): Promise<Record<string, string>> {
|
|
19
|
+
try {
|
|
20
|
+
return (await response.json()) as Record<string, string>;
|
|
21
|
+
} catch (cause) {
|
|
22
|
+
throw new Error("Google Chat cert fetch failed: malformed JSON response", { cause });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Size-capped to prevent unbounded growth in long-running deployments (#4948)
|
|
27
|
+
const MAX_AUTH_CACHE_SIZE = 32;
|
|
28
|
+
type GoogleAuthModule = typeof import("google-auth-library");
|
|
29
|
+
type GoogleAuthRuntime = {
|
|
30
|
+
GoogleAuth: GoogleAuthModule["GoogleAuth"];
|
|
31
|
+
OAuth2Client: GoogleAuthModule["OAuth2Client"];
|
|
32
|
+
};
|
|
33
|
+
type GoogleAuthInstance = InstanceType<GoogleAuthRuntime["GoogleAuth"]>;
|
|
34
|
+
type GoogleAuthOptions = ConstructorParameters<GoogleAuthRuntime["GoogleAuth"]>[0];
|
|
35
|
+
type GoogleAuthTransport = NonNullable<GoogleAuthOptions>["clientOptions"] extends {
|
|
36
|
+
transporter?: infer T;
|
|
37
|
+
}
|
|
38
|
+
? T
|
|
39
|
+
: never;
|
|
40
|
+
type OAuth2ClientInstance = InstanceType<GoogleAuthRuntime["OAuth2Client"]>;
|
|
41
|
+
|
|
42
|
+
const authCache = new Map<string, { key: string; auth: GoogleAuthInstance }>();
|
|
43
|
+
|
|
44
|
+
let cachedCerts: { fetchedAt: number; certs: Record<string, string> } | null = null;
|
|
45
|
+
let verifyClientPromise: Promise<OAuth2ClientInstance> | null = null;
|
|
46
|
+
|
|
47
|
+
async function getVerifyClient(): Promise<OAuth2ClientInstance> {
|
|
48
|
+
if (!verifyClientPromise) {
|
|
49
|
+
verifyClientPromise = (async () => {
|
|
50
|
+
try {
|
|
51
|
+
const { OAuth2Client } = await loadGoogleAuthRuntime();
|
|
52
|
+
// google-auth-library types its transporter through gaxios' CJS surface,
|
|
53
|
+
// while the plugin imports the ESM entrypoint directly.
|
|
54
|
+
const transporter = (await getGoogleAuthTransport()) as unknown as GoogleAuthTransport;
|
|
55
|
+
return new OAuth2Client({ transporter });
|
|
56
|
+
} catch (error) {
|
|
57
|
+
verifyClientPromise = null;
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
})();
|
|
61
|
+
}
|
|
62
|
+
return await verifyClientPromise;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function buildAuthKey(account: ResolvedGoogleChatAccount): string {
|
|
66
|
+
if (account.credentialsFile) {
|
|
67
|
+
return `file:${account.credentialsFile}`;
|
|
68
|
+
}
|
|
69
|
+
if (account.credentials) {
|
|
70
|
+
return `inline:${JSON.stringify(account.credentials)}`;
|
|
71
|
+
}
|
|
72
|
+
return "none";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function getAuthInstance(account: ResolvedGoogleChatAccount): Promise<GoogleAuthInstance> {
|
|
76
|
+
const key = buildAuthKey(account);
|
|
77
|
+
const cached = authCache.get(account.accountId);
|
|
78
|
+
if (cached && cached.key === key) {
|
|
79
|
+
return cached.auth;
|
|
80
|
+
}
|
|
81
|
+
const [{ GoogleAuth }, rawTransporter, credentials] = await Promise.all([
|
|
82
|
+
loadGoogleAuthRuntime(),
|
|
83
|
+
getGoogleAuthTransport(),
|
|
84
|
+
resolveValidatedGoogleChatCredentials(account),
|
|
85
|
+
]);
|
|
86
|
+
const transporter = rawTransporter as unknown as GoogleAuthTransport;
|
|
87
|
+
|
|
88
|
+
const evictOldest = () => {
|
|
89
|
+
if (authCache.size > MAX_AUTH_CACHE_SIZE) {
|
|
90
|
+
const oldest = authCache.keys().next().value;
|
|
91
|
+
if (oldest !== undefined) {
|
|
92
|
+
authCache.delete(oldest);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const auth = new GoogleAuth({
|
|
98
|
+
...(credentials ? { credentials } : {}),
|
|
99
|
+
clientOptions: { transporter },
|
|
100
|
+
scopes: [CHAT_SCOPE],
|
|
101
|
+
});
|
|
102
|
+
authCache.set(account.accountId, { key, auth });
|
|
103
|
+
evictOldest();
|
|
104
|
+
return auth;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export async function getGoogleChatAccessToken(
|
|
108
|
+
account: ResolvedGoogleChatAccount,
|
|
109
|
+
): Promise<string> {
|
|
110
|
+
const auth = await getAuthInstance(account);
|
|
111
|
+
const client = await auth.getClient();
|
|
112
|
+
const access = await client.getAccessToken();
|
|
113
|
+
const token = typeof access === "string" ? access : access?.token;
|
|
114
|
+
if (!token) {
|
|
115
|
+
throw new Error("Missing Google Chat access token");
|
|
116
|
+
}
|
|
117
|
+
return token;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function fetchChatCerts(): Promise<Record<string, string>> {
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
if (cachedCerts && now - cachedCerts.fetchedAt < 10 * 60 * 1000) {
|
|
123
|
+
return cachedCerts.certs;
|
|
124
|
+
}
|
|
125
|
+
const { response, release } = await fetchWithSsrFGuard({
|
|
126
|
+
url: CHAT_CERTS_URL,
|
|
127
|
+
auditContext: "googlechat.auth.certs",
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
if (!response.ok) {
|
|
131
|
+
throw new Error(`Failed to fetch Chat certs (${response.status})`);
|
|
132
|
+
}
|
|
133
|
+
const certs = await readGoogleChatCertsResponse(response);
|
|
134
|
+
cachedCerts = { fetchedAt: now, certs };
|
|
135
|
+
return certs;
|
|
136
|
+
} finally {
|
|
137
|
+
await release();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
export type GoogleChatAudienceType = "app-url" | "project-number";
|
|
142
|
+
|
|
143
|
+
export async function verifyGoogleChatRequest(params: {
|
|
144
|
+
bearer?: string | null;
|
|
145
|
+
audienceType?: GoogleChatAudienceType | null;
|
|
146
|
+
audience?: string | null;
|
|
147
|
+
expectedAddOnPrincipal?: string | null;
|
|
148
|
+
}): Promise<{ ok: boolean; reason?: string }> {
|
|
149
|
+
const bearer = params.bearer?.trim();
|
|
150
|
+
if (!bearer) {
|
|
151
|
+
return { ok: false, reason: "missing token" };
|
|
152
|
+
}
|
|
153
|
+
const audience = params.audience?.trim();
|
|
154
|
+
if (!audience) {
|
|
155
|
+
return { ok: false, reason: "missing audience" };
|
|
156
|
+
}
|
|
157
|
+
const audienceType = params.audienceType ?? null;
|
|
158
|
+
|
|
159
|
+
if (audienceType === "app-url") {
|
|
160
|
+
try {
|
|
161
|
+
const verifyClient = await getVerifyClient();
|
|
162
|
+
const ticket = await verifyClient.verifyIdToken({
|
|
163
|
+
idToken: bearer,
|
|
164
|
+
audience,
|
|
165
|
+
});
|
|
166
|
+
const payload = ticket.getPayload();
|
|
167
|
+
const email = normalizeLowercaseStringOrEmpty(payload?.email ?? "");
|
|
168
|
+
if (!payload?.email_verified) {
|
|
169
|
+
return { ok: false, reason: "email not verified" };
|
|
170
|
+
}
|
|
171
|
+
if (email === CHAT_ISSUER) {
|
|
172
|
+
return { ok: true };
|
|
173
|
+
}
|
|
174
|
+
if (!ADDON_ISSUER_PATTERN.test(email)) {
|
|
175
|
+
return { ok: false, reason: `invalid issuer: ${email}` };
|
|
176
|
+
}
|
|
177
|
+
const expectedAddOnPrincipal = normalizeLowercaseStringOrEmpty(
|
|
178
|
+
params.expectedAddOnPrincipal ?? "",
|
|
179
|
+
);
|
|
180
|
+
if (!expectedAddOnPrincipal) {
|
|
181
|
+
return { ok: false, reason: "missing add-on principal binding" };
|
|
182
|
+
}
|
|
183
|
+
const tokenPrincipal = normalizeLowercaseStringOrEmpty(payload?.sub ?? "");
|
|
184
|
+
if (!tokenPrincipal || tokenPrincipal !== expectedAddOnPrincipal) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
reason: `unexpected add-on principal: ${tokenPrincipal || "<missing>"}`,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return { ok: true };
|
|
191
|
+
} catch (err) {
|
|
192
|
+
return { ok: false, reason: err instanceof Error ? err.message : "invalid token" };
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (audienceType === "project-number") {
|
|
197
|
+
try {
|
|
198
|
+
const verifyClient = await getVerifyClient();
|
|
199
|
+
const certs = await fetchChatCerts();
|
|
200
|
+
await verifyClient.verifySignedJwtWithCertsAsync(bearer, certs, audience, [CHAT_ISSUER]);
|
|
201
|
+
return { ok: true };
|
|
202
|
+
} catch (err) {
|
|
203
|
+
return { ok: false, reason: err instanceof Error ? err.message : "invalid token" };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return { ok: false, reason: "unsupported audience type" };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export const testing = {
|
|
211
|
+
resetGoogleChatAuthForTests(): void {
|
|
212
|
+
authCache.clear();
|
|
213
|
+
cachedCerts = null;
|
|
214
|
+
verifyClientPromise = null;
|
|
215
|
+
googleAuthRuntimeTesting.resetGoogleAuthRuntimeForTests();
|
|
216
|
+
},
|
|
217
|
+
};
|
|
218
|
+
export { testing as __testing };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { googlechatPlugin } from "./channel.js";
|
|
4
|
+
|
|
5
|
+
describe("googlechatPlugin config adapter", () => {
|
|
6
|
+
it("keeps read-only accessors from resolving service account SecretRefs", () => {
|
|
7
|
+
const cfg = {
|
|
8
|
+
secrets: {
|
|
9
|
+
providers: {
|
|
10
|
+
google_chat_service_account: {
|
|
11
|
+
source: "file",
|
|
12
|
+
path: "/tmp/klaw-missing-google-chat-service-account",
|
|
13
|
+
mode: "singleValue",
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
channels: {
|
|
18
|
+
googlechat: {
|
|
19
|
+
serviceAccount: {
|
|
20
|
+
source: "file",
|
|
21
|
+
provider: "google_chat_service_account",
|
|
22
|
+
id: "value",
|
|
23
|
+
},
|
|
24
|
+
dm: {
|
|
25
|
+
allowFrom: ["users/123"],
|
|
26
|
+
},
|
|
27
|
+
defaultTo: "spaces/AAA",
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
} as KlawConfig;
|
|
31
|
+
|
|
32
|
+
expect(googlechatPlugin.config.resolveAllowFrom?.({ cfg, accountId: "default" })).toEqual([
|
|
33
|
+
"users/123",
|
|
34
|
+
]);
|
|
35
|
+
expect(googlechatPlugin.config.resolveDefaultTo?.({ cfg, accountId: "default" })).toBe(
|
|
36
|
+
"spaces/AAA",
|
|
37
|
+
);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { adaptScopedAccountAccessor } from "klaw/plugin-sdk/channel-config-helpers";
|
|
2
|
+
import {
|
|
3
|
+
createMessageReceiptFromOutboundResults,
|
|
4
|
+
defineChannelMessageAdapter,
|
|
5
|
+
type MessageReceiptPartKind,
|
|
6
|
+
} from "klaw/plugin-sdk/channel-message";
|
|
7
|
+
import {
|
|
8
|
+
composeAccountWarningCollectors,
|
|
9
|
+
createAllowlistProviderOpenWarningCollector,
|
|
10
|
+
} from "klaw/plugin-sdk/channel-policy";
|
|
11
|
+
import {
|
|
12
|
+
createChannelDirectoryAdapter,
|
|
13
|
+
listResolvedDirectoryGroupEntriesFromMapKeys,
|
|
14
|
+
listResolvedDirectoryUserEntriesFromAllowFrom,
|
|
15
|
+
} from "klaw/plugin-sdk/directory-runtime";
|
|
16
|
+
import { createLazyRuntimeNamedExport } from "klaw/plugin-sdk/lazy-runtime";
|
|
17
|
+
import type { OutboundMediaLoadOptions } from "klaw/plugin-sdk/outbound-media";
|
|
18
|
+
import { sanitizeForPlainText } from "klaw/plugin-sdk/outbound-runtime";
|
|
19
|
+
import {
|
|
20
|
+
normalizeLowercaseStringOrEmpty,
|
|
21
|
+
normalizeOptionalString,
|
|
22
|
+
} from "klaw/plugin-sdk/string-coerce-runtime";
|
|
23
|
+
import {
|
|
24
|
+
type ResolvedGoogleChatAccount,
|
|
25
|
+
chunkTextForOutbound,
|
|
26
|
+
readRemoteMediaBuffer,
|
|
27
|
+
isGoogleChatUserTarget,
|
|
28
|
+
loadOutboundMediaFromUrl,
|
|
29
|
+
missingTargetError,
|
|
30
|
+
normalizeGoogleChatTarget,
|
|
31
|
+
PAIRING_APPROVED_MESSAGE,
|
|
32
|
+
resolveChannelMediaMaxBytes,
|
|
33
|
+
resolveGoogleChatAccount,
|
|
34
|
+
resolveGoogleChatOutboundSpace,
|
|
35
|
+
type KlawConfig,
|
|
36
|
+
} from "./channel.deps.runtime.js";
|
|
37
|
+
import { resolveGoogleChatGroupRequireMention } from "./group-policy.js";
|
|
38
|
+
|
|
39
|
+
const loadGoogleChatChannelRuntime = createLazyRuntimeNamedExport(
|
|
40
|
+
() => import("./channel.runtime.js"),
|
|
41
|
+
"googleChatChannelRuntime",
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
function createGoogleChatSendReceipt(params: {
|
|
45
|
+
messageId?: string;
|
|
46
|
+
chatId: string;
|
|
47
|
+
kind: MessageReceiptPartKind;
|
|
48
|
+
}) {
|
|
49
|
+
const messageId = params.messageId?.trim();
|
|
50
|
+
return createMessageReceiptFromOutboundResults({
|
|
51
|
+
results: messageId
|
|
52
|
+
? [
|
|
53
|
+
{
|
|
54
|
+
channel: "googlechat",
|
|
55
|
+
messageId,
|
|
56
|
+
chatId: params.chatId,
|
|
57
|
+
conversationId: params.chatId,
|
|
58
|
+
},
|
|
59
|
+
]
|
|
60
|
+
: [],
|
|
61
|
+
threadId: params.chatId,
|
|
62
|
+
kind: params.kind,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const formatAllowFromEntry = (entry: string) =>
|
|
67
|
+
normalizeLowercaseStringOrEmpty(
|
|
68
|
+
entry
|
|
69
|
+
.trim()
|
|
70
|
+
.replace(/^(googlechat|google-chat|gchat):/i, "")
|
|
71
|
+
.replace(/^user:/i, "")
|
|
72
|
+
.replace(/^users\//i, ""),
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const collectGoogleChatGroupPolicyWarnings =
|
|
76
|
+
createAllowlistProviderOpenWarningCollector<ResolvedGoogleChatAccount>({
|
|
77
|
+
providerConfigPresent: (cfg) => cfg.channels?.googlechat !== undefined,
|
|
78
|
+
resolveGroupPolicy: (account) => account.config.groupPolicy,
|
|
79
|
+
buildOpenWarning: {
|
|
80
|
+
surface: "Google Chat spaces",
|
|
81
|
+
openBehavior: "allows any space to trigger (mention-gated)",
|
|
82
|
+
remediation:
|
|
83
|
+
'Set channels.googlechat.groupPolicy="allowlist" and configure channels.googlechat.groups',
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const collectGoogleChatSecurityWarnings = composeAccountWarningCollectors<
|
|
88
|
+
ResolvedGoogleChatAccount,
|
|
89
|
+
{
|
|
90
|
+
cfg: KlawConfig;
|
|
91
|
+
account: ResolvedGoogleChatAccount;
|
|
92
|
+
}
|
|
93
|
+
>(
|
|
94
|
+
collectGoogleChatGroupPolicyWarnings,
|
|
95
|
+
(account) =>
|
|
96
|
+
account.config.dm?.policy === "open" &&
|
|
97
|
+
'- Google Chat DMs are open to anyone. Set channels.googlechat.dm.policy="pairing" or "allowlist".',
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
export const googlechatGroupsAdapter = {
|
|
101
|
+
resolveRequireMention: resolveGoogleChatGroupRequireMention,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const googlechatDirectoryAdapter = createChannelDirectoryAdapter({
|
|
105
|
+
listPeers: async (params) =>
|
|
106
|
+
listResolvedDirectoryUserEntriesFromAllowFrom<ResolvedGoogleChatAccount>({
|
|
107
|
+
...params,
|
|
108
|
+
resolveAccount: adaptScopedAccountAccessor(resolveGoogleChatAccount),
|
|
109
|
+
resolveAllowFrom: (account) => account.config.dm?.allowFrom,
|
|
110
|
+
normalizeId: (entry) => normalizeGoogleChatTarget(entry) ?? entry,
|
|
111
|
+
}),
|
|
112
|
+
listGroups: async (params) =>
|
|
113
|
+
listResolvedDirectoryGroupEntriesFromMapKeys<ResolvedGoogleChatAccount>({
|
|
114
|
+
...params,
|
|
115
|
+
resolveAccount: adaptScopedAccountAccessor(resolveGoogleChatAccount),
|
|
116
|
+
resolveGroups: (account) => account.config.groups,
|
|
117
|
+
}),
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
export const googlechatSecurityAdapter = {
|
|
121
|
+
dm: {
|
|
122
|
+
channelKey: "googlechat",
|
|
123
|
+
resolvePolicy: (account: ResolvedGoogleChatAccount) => account.config.dm?.policy,
|
|
124
|
+
resolveAllowFrom: (account: ResolvedGoogleChatAccount) => account.config.dm?.allowFrom,
|
|
125
|
+
allowFromPathSuffix: "dm.",
|
|
126
|
+
normalizeEntry: (raw: string) => formatAllowFromEntry(raw),
|
|
127
|
+
},
|
|
128
|
+
collectWarnings: collectGoogleChatSecurityWarnings,
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
export const googlechatThreadingAdapter = {
|
|
132
|
+
scopedAccountReplyToMode: {
|
|
133
|
+
resolveAccount: (cfg: KlawConfig, accountId?: string | null) =>
|
|
134
|
+
resolveGoogleChatAccount({ cfg, accountId }),
|
|
135
|
+
resolveReplyToMode: (account: ResolvedGoogleChatAccount, _chatType?: string | null) =>
|
|
136
|
+
account.config.replyToMode,
|
|
137
|
+
fallback: "off" as const,
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
export const googlechatPairingTextAdapter = {
|
|
142
|
+
idLabel: "googlechatUserId",
|
|
143
|
+
message: PAIRING_APPROVED_MESSAGE,
|
|
144
|
+
normalizeAllowEntry: (entry: string) => formatAllowFromEntry(entry),
|
|
145
|
+
notify: async ({
|
|
146
|
+
cfg,
|
|
147
|
+
id,
|
|
148
|
+
message,
|
|
149
|
+
accountId,
|
|
150
|
+
}: {
|
|
151
|
+
cfg: KlawConfig;
|
|
152
|
+
id: string;
|
|
153
|
+
message: string;
|
|
154
|
+
accountId?: string | null;
|
|
155
|
+
}) => {
|
|
156
|
+
const account = resolveGoogleChatAccount({ cfg: cfg, accountId });
|
|
157
|
+
if (account.credentialSource === "none") {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const user = normalizeGoogleChatTarget(id) ?? id;
|
|
161
|
+
const target = isGoogleChatUserTarget(user) ? user : `users/${user}`;
|
|
162
|
+
const space = await resolveGoogleChatOutboundSpace({ account, target });
|
|
163
|
+
const { sendGoogleChatMessage } = await loadGoogleChatChannelRuntime();
|
|
164
|
+
await sendGoogleChatMessage({
|
|
165
|
+
account,
|
|
166
|
+
space,
|
|
167
|
+
text: message,
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export const googlechatOutboundAdapter = {
|
|
173
|
+
base: {
|
|
174
|
+
deliveryMode: "direct" as const,
|
|
175
|
+
chunker: chunkTextForOutbound,
|
|
176
|
+
chunkerMode: "markdown" as const,
|
|
177
|
+
textChunkLimit: 4000,
|
|
178
|
+
sanitizeText: ({ text }: { text: string }) => sanitizeForPlainText(text),
|
|
179
|
+
resolveTarget: ({ to }: { to?: string }) => {
|
|
180
|
+
const trimmed = normalizeOptionalString(to) ?? "";
|
|
181
|
+
|
|
182
|
+
if (trimmed) {
|
|
183
|
+
const normalized = normalizeGoogleChatTarget(trimmed);
|
|
184
|
+
if (!normalized) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false as const,
|
|
187
|
+
error: missingTargetError("Google Chat", "<spaces/{space}|users/{user}>"),
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return { ok: true as const, to: normalized };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
ok: false as const,
|
|
195
|
+
error: missingTargetError("Google Chat", "<spaces/{space}|users/{user}>"),
|
|
196
|
+
};
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
attachedResults: {
|
|
200
|
+
channel: "googlechat" as const,
|
|
201
|
+
sendText: async ({
|
|
202
|
+
cfg,
|
|
203
|
+
to,
|
|
204
|
+
text,
|
|
205
|
+
accountId,
|
|
206
|
+
replyToId,
|
|
207
|
+
threadId,
|
|
208
|
+
}: {
|
|
209
|
+
cfg: KlawConfig;
|
|
210
|
+
to: string;
|
|
211
|
+
text: string;
|
|
212
|
+
accountId?: string | null;
|
|
213
|
+
replyToId?: string | null;
|
|
214
|
+
threadId?: string | number | null;
|
|
215
|
+
}) => {
|
|
216
|
+
const account = resolveGoogleChatAccount({
|
|
217
|
+
cfg: cfg,
|
|
218
|
+
accountId,
|
|
219
|
+
});
|
|
220
|
+
const space = await resolveGoogleChatOutboundSpace({ account, target: to });
|
|
221
|
+
const thread =
|
|
222
|
+
typeof threadId === "number" ? String(threadId) : (threadId ?? replyToId ?? undefined);
|
|
223
|
+
const { sendGoogleChatMessage } = await loadGoogleChatChannelRuntime();
|
|
224
|
+
const result = await sendGoogleChatMessage({
|
|
225
|
+
account,
|
|
226
|
+
space,
|
|
227
|
+
text,
|
|
228
|
+
thread,
|
|
229
|
+
});
|
|
230
|
+
const messageId = result?.messageName ?? "";
|
|
231
|
+
return {
|
|
232
|
+
messageId,
|
|
233
|
+
chatId: space,
|
|
234
|
+
receipt: createGoogleChatSendReceipt({ messageId, chatId: space, kind: "text" }),
|
|
235
|
+
};
|
|
236
|
+
},
|
|
237
|
+
sendMedia: async ({
|
|
238
|
+
cfg,
|
|
239
|
+
to,
|
|
240
|
+
text,
|
|
241
|
+
mediaUrl,
|
|
242
|
+
mediaAccess,
|
|
243
|
+
mediaLocalRoots,
|
|
244
|
+
mediaReadFile,
|
|
245
|
+
accountId,
|
|
246
|
+
replyToId,
|
|
247
|
+
threadId,
|
|
248
|
+
}: {
|
|
249
|
+
cfg: KlawConfig;
|
|
250
|
+
to: string;
|
|
251
|
+
text?: string;
|
|
252
|
+
mediaUrl?: string;
|
|
253
|
+
mediaAccess?: OutboundMediaLoadOptions["mediaAccess"];
|
|
254
|
+
mediaLocalRoots?: OutboundMediaLoadOptions["mediaLocalRoots"];
|
|
255
|
+
mediaReadFile?: OutboundMediaLoadOptions["mediaReadFile"];
|
|
256
|
+
accountId?: string | null;
|
|
257
|
+
replyToId?: string | null;
|
|
258
|
+
threadId?: string | number | null;
|
|
259
|
+
}) => {
|
|
260
|
+
if (!mediaUrl) {
|
|
261
|
+
throw new Error("Google Chat mediaUrl is required.");
|
|
262
|
+
}
|
|
263
|
+
const account = resolveGoogleChatAccount({
|
|
264
|
+
cfg: cfg,
|
|
265
|
+
accountId,
|
|
266
|
+
});
|
|
267
|
+
const space = await resolveGoogleChatOutboundSpace({ account, target: to });
|
|
268
|
+
const thread =
|
|
269
|
+
typeof threadId === "number" ? String(threadId) : (threadId ?? replyToId ?? undefined);
|
|
270
|
+
const maxBytes = resolveChannelMediaMaxBytes({
|
|
271
|
+
cfg: cfg,
|
|
272
|
+
resolveChannelLimitMb: ({ cfg, accountId }) =>
|
|
273
|
+
(
|
|
274
|
+
cfg.channels?.googlechat as
|
|
275
|
+
| { accounts?: Record<string, { mediaMaxMb?: number }>; mediaMaxMb?: number }
|
|
276
|
+
| undefined
|
|
277
|
+
)?.accounts?.[accountId]?.mediaMaxMb ??
|
|
278
|
+
(cfg.channels?.googlechat as { mediaMaxMb?: number } | undefined)?.mediaMaxMb,
|
|
279
|
+
accountId,
|
|
280
|
+
});
|
|
281
|
+
const effectiveMaxBytes = maxBytes ?? (account.config.mediaMaxMb ?? 20) * 1024 * 1024;
|
|
282
|
+
const loaded = /^https?:\/\//i.test(mediaUrl)
|
|
283
|
+
? await readRemoteMediaBuffer({
|
|
284
|
+
url: mediaUrl,
|
|
285
|
+
maxBytes: effectiveMaxBytes,
|
|
286
|
+
})
|
|
287
|
+
: await loadOutboundMediaFromUrl(mediaUrl, {
|
|
288
|
+
maxBytes: effectiveMaxBytes,
|
|
289
|
+
mediaAccess,
|
|
290
|
+
mediaLocalRoots,
|
|
291
|
+
mediaReadFile,
|
|
292
|
+
});
|
|
293
|
+
const { sendGoogleChatMessage, uploadGoogleChatAttachment } =
|
|
294
|
+
await loadGoogleChatChannelRuntime();
|
|
295
|
+
const upload = await uploadGoogleChatAttachment({
|
|
296
|
+
account,
|
|
297
|
+
space,
|
|
298
|
+
filename: loaded.fileName ?? "attachment",
|
|
299
|
+
buffer: loaded.buffer,
|
|
300
|
+
contentType: loaded.contentType,
|
|
301
|
+
});
|
|
302
|
+
const result = await sendGoogleChatMessage({
|
|
303
|
+
account,
|
|
304
|
+
space,
|
|
305
|
+
text,
|
|
306
|
+
thread,
|
|
307
|
+
attachments: upload.attachmentUploadToken
|
|
308
|
+
? [
|
|
309
|
+
{
|
|
310
|
+
attachmentUploadToken: upload.attachmentUploadToken,
|
|
311
|
+
contentName: loaded.fileName,
|
|
312
|
+
},
|
|
313
|
+
]
|
|
314
|
+
: undefined,
|
|
315
|
+
});
|
|
316
|
+
const messageId = result?.messageName ?? "";
|
|
317
|
+
return {
|
|
318
|
+
messageId,
|
|
319
|
+
chatId: space,
|
|
320
|
+
receipt: createGoogleChatSendReceipt({ messageId, chatId: space, kind: "media" }),
|
|
321
|
+
};
|
|
322
|
+
},
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
export const googlechatMessageAdapter = defineChannelMessageAdapter({
|
|
327
|
+
id: "googlechat",
|
|
328
|
+
durableFinal: {
|
|
329
|
+
capabilities: {
|
|
330
|
+
text: true,
|
|
331
|
+
media: true,
|
|
332
|
+
thread: true,
|
|
333
|
+
messageSendingHooks: true,
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
send: {
|
|
337
|
+
text: googlechatOutboundAdapter.attachedResults.sendText,
|
|
338
|
+
media: googlechatOutboundAdapter.attachedResults.sendMedia,
|
|
339
|
+
},
|
|
340
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export {
|
|
2
|
+
buildChannelConfigSchema,
|
|
3
|
+
chunkTextForOutbound,
|
|
4
|
+
DEFAULT_ACCOUNT_ID,
|
|
5
|
+
readRemoteMediaBuffer,
|
|
6
|
+
GoogleChatConfigSchema,
|
|
7
|
+
loadOutboundMediaFromUrl,
|
|
8
|
+
missingTargetError,
|
|
9
|
+
PAIRING_APPROVED_MESSAGE,
|
|
10
|
+
resolveChannelMediaMaxBytes,
|
|
11
|
+
type ChannelMessageActionAdapter,
|
|
12
|
+
type ChannelMessageActionName,
|
|
13
|
+
type ChannelStatusIssue,
|
|
14
|
+
type KlawConfig,
|
|
15
|
+
} from "../runtime-api.js";
|
|
16
|
+
export {
|
|
17
|
+
type GoogleChatConfigAccessorAccount,
|
|
18
|
+
listGoogleChatAccountIds,
|
|
19
|
+
resolveGoogleChatConfigAccessorAccount,
|
|
20
|
+
resolveDefaultGoogleChatAccountId,
|
|
21
|
+
resolveGoogleChatAccount,
|
|
22
|
+
type ResolvedGoogleChatAccount,
|
|
23
|
+
} from "./accounts.js";
|
|
24
|
+
export {
|
|
25
|
+
isGoogleChatSpaceTarget,
|
|
26
|
+
isGoogleChatUserTarget,
|
|
27
|
+
normalizeGoogleChatTarget,
|
|
28
|
+
resolveGoogleChatOutboundSpace,
|
|
29
|
+
} from "./targets.js";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import {
|
|
2
|
+
probeGoogleChat as probeGoogleChatImpl,
|
|
3
|
+
sendGoogleChatMessage as sendGoogleChatMessageImpl,
|
|
4
|
+
uploadGoogleChatAttachment as uploadGoogleChatAttachmentImpl,
|
|
5
|
+
} from "./api.js";
|
|
6
|
+
import {
|
|
7
|
+
resolveGoogleChatWebhookPath as resolveGoogleChatWebhookPathImpl,
|
|
8
|
+
startGoogleChatMonitor as startGoogleChatMonitorImpl,
|
|
9
|
+
} from "./monitor.js";
|
|
10
|
+
|
|
11
|
+
export const googleChatChannelRuntime = {
|
|
12
|
+
probeGoogleChat: probeGoogleChatImpl,
|
|
13
|
+
sendGoogleChatMessage: sendGoogleChatMessageImpl,
|
|
14
|
+
uploadGoogleChatAttachment: uploadGoogleChatAttachmentImpl,
|
|
15
|
+
resolveGoogleChatWebhookPath: resolveGoogleChatWebhookPathImpl,
|
|
16
|
+
startGoogleChatMonitor: startGoogleChatMonitorImpl,
|
|
17
|
+
};
|