-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathadapters.ts
More file actions
267 lines (253 loc) · 6.97 KB
/
adapters.ts
File metadata and controls
267 lines (253 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import {
createDiscordAdapter,
type DiscordAdapter,
} from "@chat-adapter/discord";
import {
createGoogleChatAdapter,
type GoogleChatAdapter,
} from "@chat-adapter/gchat";
import { createGitHubAdapter, type GitHubAdapter } from "@chat-adapter/github";
import { createLinearAdapter, type LinearAdapter } from "@chat-adapter/linear";
import { createSlackAdapter, type SlackAdapter } from "@chat-adapter/slack";
import { createTeamsAdapter, type TeamsAdapter } from "@chat-adapter/teams";
import {
createTelegramAdapter,
type TelegramAdapter,
} from "@chat-adapter/telegram";
import {
createWhatsAppAdapter,
type WhatsAppAdapter,
} from "@chat-adapter/whatsapp";
import { ConsoleLogger } from "chat";
import { recorder, withRecording } from "./recorder";
// Create a shared logger for adapters that need explicit logger overrides
const logger = new ConsoleLogger("info");
export interface Adapters {
discord?: DiscordAdapter;
gchat?: GoogleChatAdapter;
github?: GitHubAdapter;
linear?: LinearAdapter;
slack?: SlackAdapter;
teams?: TeamsAdapter;
telegram?: TelegramAdapter;
whatsapp?: WhatsAppAdapter;
}
// Methods to record for each adapter (outgoing API calls)
const DISCORD_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"startTyping",
"openDM",
"fetchMessages",
];
const SLACK_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"startTyping",
"stream",
"openDM",
"fetchMessages",
];
const TEAMS_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"startTyping",
"openDM",
"fetchMessages",
];
const GCHAT_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"openDM",
"fetchMessages",
];
const GITHUB_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"fetchMessages",
];
const LINEAR_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"fetchMessages",
];
const TELEGRAM_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"startTyping",
"openDM",
"fetchMessages",
];
const WHATSAPP_METHODS = [
"postMessage",
"editMessage",
"deleteMessage",
"addReaction",
"removeReaction",
"startTyping",
"openDM",
"fetchMessages",
];
/**
* Build type-safe adapters based on available environment variables.
* Adapters are only created if their required env vars are present.
*
* Factory functions auto-detect env vars, so only app-specific overrides
* (like userName and appType) need to be provided explicitly.
*/
export function buildAdapters(): Adapters {
// Start fetch recording to capture outgoing adapter API calls
recorder.startFetchRecording();
const adapters: Adapters = {};
// Discord adapter (optional) - env vars: DISCORD_BOT_TOKEN, DISCORD_PUBLIC_KEY, DISCORD_APPLICATION_ID
if (process.env.DISCORD_BOT_TOKEN) {
adapters.discord = withRecording(
createDiscordAdapter({
userName: "Chat SDK Bot",
logger: logger.child("discord"),
}),
"discord",
DISCORD_METHODS
);
}
// Slack adapter (optional) - env vars: SLACK_SIGNING_SECRET + (SLACK_BOT_TOKEN or SLACK_CLIENT_ID/SECRET)
if (process.env.SLACK_SIGNING_SECRET) {
adapters.slack = withRecording(
createSlackAdapter({
userName: "Chat SDK Bot",
logger: logger.child("slack"),
botToken: process.env.SLACK_BOT_TOKEN,
clientSecret: process.env.SLACK_CLIENT_SECRET,
}),
"slack",
SLACK_METHODS
);
}
// Teams adapter (optional) - env vars: TEAMS_APP_ID, TEAMS_APP_PASSWORD
if (process.env.TEAMS_APP_ID) {
adapters.teams = withRecording(
createTeamsAdapter({
appType: "SingleTenant",
userName: "Chat SDK Demo",
logger: logger.child("teams"),
}),
"teams",
TEAMS_METHODS
);
}
// Google Chat adapter (optional) - env vars: GOOGLE_CHAT_CREDENTIALS or GOOGLE_CHAT_USE_ADC
if (
process.env.GOOGLE_CHAT_CREDENTIALS ||
process.env.GOOGLE_CHAT_USE_ADC === "true"
) {
try {
adapters.gchat = withRecording(
createGoogleChatAdapter({
userName: "Chat SDK Demo",
logger: logger.child("gchat"),
}),
"gchat",
GCHAT_METHODS
);
} catch {
console.warn(
"[chat] Failed to create gchat adapter (check GOOGLE_CHAT_CREDENTIALS or GOOGLE_CHAT_USE_ADC)"
);
}
}
// GitHub adapter (optional) - env vars: GITHUB_WEBHOOK_SECRET + (GITHUB_TOKEN or GITHUB_APP_ID/PRIVATE_KEY)
if (process.env.GITHUB_WEBHOOK_SECRET) {
try {
adapters.github = withRecording(
createGitHubAdapter({
logger: logger.child("github"),
userName: "chat-sdk-bot",
}),
"github",
GITHUB_METHODS
);
} catch {
console.warn(
"[chat] Failed to create github adapter (check GITHUB_TOKEN or GITHUB_APP_ID/PRIVATE_KEY)"
);
}
}
// Linear adapter (optional) - env vars: LINEAR_WEBHOOK_SECRET + (LINEAR_API_KEY, LINEAR_ACCESS_TOKEN, LINEAR_CLIENT_CREDENTIALS_*, or LINEAR_CLIENT_ID/SECRET).
// Set LINEAR_MODE=agent-sessions for app-actor installs with Agent session events + app:mentionable.
if (process.env.LINEAR_WEBHOOK_SECRET) {
try {
adapters.linear = withRecording(
createLinearAdapter({
logger: logger.child("linear"),
mode:
process.env.LINEAR_MODE === "agent-sessions"
? "agent-sessions"
: "comments",
}),
"linear",
LINEAR_METHODS
);
} catch {
console.warn(
"[chat] Failed to create linear adapter (check LINEAR_API_KEY, LINEAR_ACCESS_TOKEN, LINEAR_CLIENT_CREDENTIALS_*, or LINEAR_CLIENT_ID/SECRET)"
);
}
}
// Telegram adapter (optional) - env vars: TELEGRAM_BOT_TOKEN
if (process.env.TELEGRAM_BOT_TOKEN) {
adapters.telegram = withRecording(
createTelegramAdapter({
logger: logger.child("telegram"),
}),
"telegram",
TELEGRAM_METHODS
);
}
// WhatsApp adapter (optional) - env vars: WHATSAPP_ACCESS_TOKEN, WHATSAPP_APP_SECRET, WHATSAPP_PHONE_NUMBER_ID, WHATSAPP_VERIFY_TOKEN
console.log("[chat] WhatsApp env check:", {
hasAccessToken: !!process.env.WHATSAPP_ACCESS_TOKEN,
hasAppSecret: !!process.env.WHATSAPP_APP_SECRET,
hasPhoneNumberId: !!process.env.WHATSAPP_PHONE_NUMBER_ID,
hasVerifyToken: !!process.env.WHATSAPP_VERIFY_TOKEN,
});
if (
process.env.WHATSAPP_ACCESS_TOKEN &&
process.env.WHATSAPP_PHONE_NUMBER_ID
) {
try {
adapters.whatsapp = withRecording(
createWhatsAppAdapter({
logger: logger.child("whatsapp"),
}),
"whatsapp",
WHATSAPP_METHODS
);
} catch (err) {
console.warn(
"[chat] Failed to create whatsapp adapter:",
err instanceof Error ? err.message : err
);
}
}
return adapters;
}