-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathgateway.js
More file actions
407 lines (350 loc) · 14 KB
/
gateway.js
File metadata and controls
407 lines (350 loc) · 14 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import 'dotenv/config'
import http from 'http'
import QRCode from 'qrcode'
import config from './config.js'
import WhatsAppAdapter from './adapters/whatsapp.js'
import iMessageAdapter from './adapters/imessage.js'
import TelegramAdapter from './adapters/telegram.js'
import SignalAdapter from './adapters/signal.js'
import SessionManager from './sessions/manager.js'
import AgentRunner from './agent/runner.js'
import CommandHandler from './commands/handler.js'
import { Composio } from '@composio/core'
/**
* Secure OpenClaw Gateway - Routes messages between messaging platforms and Claude agent
*/
class Gateway {
constructor() {
this.sessionManager = new SessionManager()
this.agentRunner = new AgentRunner(this.sessionManager, {
allowedTools: config.agent?.allowedTools || ['Read', 'Write', 'Edit', 'Bash', 'Glob', 'Grep'],
maxTurns: config.agent?.maxTurns || 50,
provider: config.agent?.provider || 'claude',
permissionMode: 'bypassPermissions',
opencode: config.agent?.opencode || {}
})
this.commandHandler = new CommandHandler(this)
this.adapters = new Map()
this.pendingApprovals = new Map() // chatId -> { resolve, timeout }
this.composio = new Composio()
this.composioSession = null
this.mcpServers = {}
this.setupQueueMonitoring()
this.setupAgentMonitoring()
this.setupCronExecution()
}
async initMcpServers() {
const userId = config.agentId || 'secure-openclaw-user'
console.log('[Composio] Initializing session for:', userId)
try {
this.composioSession = await this.composio.create(userId)
this.mcpServers.composio = {
type: 'http',
url: this.composioSession.mcp.url,
headers: this.composioSession.mcp.headers
}
console.log('[Composio] Session ready')
} catch (err) {
console.error('[Composio] Failed to initialize:', err.message)
}
}
setupQueueMonitoring() {
this.agentRunner.on('queued', ({ runId, sessionKey, position, queueLength }) => {
if (position > 0) {
console.log(`[Queue] 📥 Queued: position ${position + 1}, ${queueLength} pending`)
}
})
this.agentRunner.on('processing', ({ runId, waitTimeMs, remainingInQueue }) => {
if (waitTimeMs > 100) {
console.log(`[Queue] ⚙️ Processing (waited ${Math.round(waitTimeMs)}ms, ${remainingInQueue} remaining)`)
}
})
this.agentRunner.on('completed', ({ runId, processingTimeMs }) => {
console.log(`[Queue] ✓ Completed in ${Math.round(processingTimeMs)}ms`)
})
this.agentRunner.on('failed', ({ runId, error }) => {
console.log(`[Queue] ✗ Failed: ${error}`)
})
}
setupAgentMonitoring() {
this.agentRunner.on('agent:tool', ({ sessionKey, name }) => {
console.log(`[Agent] 🔧 Using tool: ${name}`)
})
}
setupCronExecution() {
// Handle cron job execution - send scheduled messages or invoke agent
this.agentRunner.agent.cronScheduler.on('execute', async ({ jobId, platform, chatId, sessionKey, message, invokeAgent }) => {
console.log(`[Cron] ⏰ Executing job ${jobId}${invokeAgent ? ' (invoking agent)' : ''}`)
const adapter = this.adapters.get(platform)
if (!adapter) {
console.error(`[Cron] No adapter for platform: ${platform}`)
return
}
try {
if (invokeAgent) {
// Run the agent with the message and send the response
console.log(`[Cron] Invoking agent with: ${message}`)
const response = await this.agentRunner.agent.runAndCollect({
message,
sessionKey: sessionKey || `cron:${jobId}`,
platform,
chatId,
mcpServers: this.mcpServers
})
if (response) {
await adapter.sendMessage(chatId, response)
console.log(`[Cron] Agent response sent for job ${jobId}`)
}
} else {
// Just send the message directly
await adapter.sendMessage(chatId, message)
console.log(`[Cron] Message sent for job ${jobId}`)
}
} catch (err) {
console.error(`[Cron] Failed to execute job:`, err.message)
}
})
}
/**
* Send a message and wait for the user's reply.
* Used for tool approval prompts and clarifying questions.
*/
waitForApproval(chatId, adapter, message, timeoutMs = 120000) {
// Clear any existing pending approval for this chat
const existing = this.pendingApprovals.get(chatId)
if (existing) {
clearTimeout(existing.timeout)
existing.resolve(null)
}
return new Promise(async (resolve) => {
const timeout = setTimeout(() => {
this.pendingApprovals.delete(chatId)
resolve(null) // Timeout = no response
}, timeoutMs)
this.pendingApprovals.set(chatId, { resolve, timeout })
try {
await adapter.sendMessage(chatId, message)
} catch (err) {
console.error('[Gateway] Failed to send approval prompt:', err.message)
clearTimeout(timeout)
this.pendingApprovals.delete(chatId)
resolve(null)
}
})
}
async start() {
console.log('='.repeat(50))
console.log('Secure OpenClaw Gateway Starting')
console.log('='.repeat(50))
console.log(`Agent ID: ${config.agentId}`)
console.log(`Workspace: ~/secure-openclaw/`)
console.log('')
const platforms = ['whatsapp', 'imessage', 'telegram', 'signal']
for (const p of platforms) {
const pc = config[p]
if (!pc?.enabled) continue
const dms = pc.allowedDMs?.length ? pc.allowedDMs.join(', ') : 'NONE (all blocked)'
const groups = pc.allowedGroups?.length ? pc.allowedGroups.join(', ') : 'NONE (all blocked)'
console.log(`[Security] ${p}: DMs=${dms} | Groups=${groups}`)
}
await this.initMcpServers()
this.agentRunner.setMcpServers(this.mcpServers)
// Pre-initialize provider (connect/start server before messages arrive)
if (this.agentRunner.agent.provider.initialize) {
try {
await this.agentRunner.agent.provider.initialize()
console.log('[Provider] Ready')
} catch (err) {
console.error('[Provider] Init failed:', err.message)
}
}
this.agentRunner.agent.gateway = this
// Initialize WhatsApp adapter
if (config.whatsapp.enabled) {
console.log('[Gateway] Initializing WhatsApp adapter...')
const whatsapp = new WhatsAppAdapter(config.whatsapp)
this.setupAdapter(whatsapp, 'whatsapp', config.whatsapp)
this.adapters.set('whatsapp', whatsapp)
try {
await whatsapp.start()
} catch (err) {
console.error('[Gateway] WhatsApp adapter failed to start:', err.message)
}
}
// Initialize iMessage adapter
if (config.imessage.enabled) {
console.log('[Gateway] Initializing iMessage adapter...')
const imessage = new iMessageAdapter(config.imessage)
this.setupAdapter(imessage, 'imessage', config.imessage)
this.adapters.set('imessage', imessage)
try {
await imessage.start()
} catch (err) {
console.error('[Gateway] iMessage adapter failed to start:', err.message)
}
}
if (config.telegram?.enabled) {
console.log('[Gateway] Initializing Telegram adapter...')
const telegram = new TelegramAdapter(config.telegram)
this.setupAdapter(telegram, 'telegram', config.telegram)
this.adapters.set('telegram', telegram)
try {
await telegram.start()
} catch (err) {
console.error('[Gateway] Telegram adapter failed to start:', err.message)
}
}
// Initialize Signal adapter
if (config.signal?.enabled) {
console.log('[Gateway] Initializing Signal adapter...')
const signal = new SignalAdapter(config.signal)
this.setupAdapter(signal, 'signal', config.signal)
this.adapters.set('signal', signal)
try {
await signal.start()
} catch (err) {
console.error('[Gateway] Signal adapter failed to start:', err.message)
}
}
// Handle shutdown
process.on('SIGINT', () => this.stop())
process.on('SIGTERM', () => this.stop())
// Start HTTP server for health checks and WhatsApp QR code
this.startHttpServer()
console.log('')
console.log('[Gateway] Ready and listening for messages')
console.log('[Gateway] Using Claude Agent SDK with memory + cron + Composio')
console.log('[Gateway] Commands: /help, /new, /status, /memory, /stop')
}
setupAdapter(adapter, platform, platformConfig) {
adapter.onMessage(async (message) => {
const sessionKey = adapter.generateSessionKey(config.agentId, platform, message)
console.log('')
console.log(`[${platform.toUpperCase()}] Incoming message:`)
console.log(` Session: ${sessionKey}`)
console.log(` From: ${message.sender}`)
console.log(` Group: ${message.isGroup}`)
console.log(` Text: ${message.text.substring(0, 100)}${message.text.length > 100 ? '...' : ''}`)
if (message.image) {
console.log(` Image: ${Math.round(message.image.data.length / 1024)}KB`)
}
// Check for pending approval — if one exists, resolve it with the user's reply
const pending = this.pendingApprovals.get(message.chatId)
if (pending) {
console.log(`[${platform.toUpperCase()}] Resolving pending approval with: ${message.text}`)
clearTimeout(pending.timeout)
this.pendingApprovals.delete(message.chatId)
pending.resolve(message.text)
return
}
// Check for pending /model or /provider selection
if (this.commandHandler.handlePendingReply(message.text, message.chatId)) {
console.log(`[${platform.toUpperCase()}] Resolved pending command selection: ${message.text}`)
return
}
try {
// Check for slash commands first
const commandResult = await this.commandHandler.execute(
message.text,
sessionKey,
adapter,
message.chatId
)
if (commandResult.handled) {
console.log(`[${platform.toUpperCase()}] Command handled: ${message.text.split(' ')[0]}`)
if (commandResult.response) {
await adapter.sendMessage(message.chatId, commandResult.response)
}
return
}
// Check queue status and show typing indicator
const queueStatus = this.agentRunner.getQueueStatus(sessionKey)
if (adapter.sendTyping) {
await adapter.sendTyping(message.chatId)
}
if (queueStatus.pending > 0 && adapter.react && message.raw?.key?.id) {
await adapter.react(message.chatId, message.raw.key.id, '⏳')
}
// Enqueue agent run with optional image
console.log(`[${platform.toUpperCase()}] Processing...`)
const response = await this.agentRunner.enqueueRun(
sessionKey,
message.text,
adapter,
message.chatId,
message.image // Pass image if present
)
if (adapter.stopTyping) {
await adapter.stopTyping(message.chatId)
}
console.log(`[${platform.toUpperCase()}] Done`)
} catch (error) {
console.error(`[${platform.toUpperCase()}] Error:`, error.message)
if (adapter.stopTyping) {
await adapter.stopTyping(message.chatId)
}
try {
await adapter.sendMessage(
message.chatId,
"Sorry, I encountered an error. Please try again."
)
} catch (sendErr) {
console.error(`[${platform.toUpperCase()}] Failed to send error message:`, sendErr.message)
}
}
})
}
startHttpServer() {
const port = process.env.PORT || 4096
this.httpServer = http.createServer(async (req, res) => {
if (req.url === '/qr') {
const wa = this.adapters.get('whatsapp')
if (!wa || !wa.latestQr) {
res.writeHead(200, { 'Content-Type': 'text/html' })
const status = wa?.myJid ? 'WhatsApp is connected.' : 'No QR code available. Waiting for WhatsApp...'
res.end(`<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="5"><title>WhatsApp QR</title><style>body{font-family:system-ui;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0;background:#111;color:#fff}</style></head><body><p>${status}</p></body></html>`)
return
}
try {
const qrDataUrl = await QRCode.toDataURL(wa.latestQr, { width: 400, margin: 2 })
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(`<!DOCTYPE html><html><head><meta charset="utf-8"><meta http-equiv="refresh" content="10"><title>WhatsApp QR</title><style>body{font-family:system-ui;display:flex;flex-direction:column;align-items:center;justify-content:center;min-height:100vh;margin:0;background:#111;color:#fff}img{border-radius:12px}</style></head><body><h2>Scan with WhatsApp</h2><img src="${qrDataUrl}" alt="QR Code"/><p>Page refreshes automatically.</p></body></html>`)
} catch (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' })
res.end('Failed to generate QR')
}
return
}
// Health check / status
res.writeHead(200, { 'Content-Type': 'application/json' })
const adaptersStatus = {}
for (const [name, adapter] of this.adapters) {
adaptersStatus[name] = { connected: !!adapter.sock || !!adapter.bot }
}
res.end(JSON.stringify({ status: 'ok', adapters: adaptersStatus }))
})
this.httpServer.listen(port, () => {
console.log(`[HTTP] Listening on port ${port} (QR code at /qr)`)
})
}
async stop() {
console.log('\n[Gateway] Shutting down...')
// Stop cron scheduler
this.agentRunner.agent.stopCron()
for (const adapter of this.adapters.values()) {
try {
await adapter.stop()
} catch (err) {
console.error('[Gateway] Error stopping adapter:', err.message)
}
}
console.log('[Gateway] Goodbye!')
process.exit(0)
}
}
// Start the gateway
const gateway = new Gateway()
gateway.start().catch((err) => {
console.error('[Gateway] Fatal error:', err)
process.exit(1)
})