forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.rs
More file actions
440 lines (384 loc) · 14.4 KB
/
channel.rs
File metadata and controls
440 lines (384 loc) · 14.4 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::ambient_runner::AmbientRunnerHandle;
use crate::config::SafetyConfig;
use crate::logging;
use async_trait::async_trait;
use std::sync::Arc;
#[async_trait]
pub trait MessageChannel: Send + Sync {
fn name(&self) -> &str;
fn is_send_enabled(&self) -> bool;
fn is_reply_enabled(&self) -> bool;
async fn send(&self, text: &str) -> anyhow::Result<()>;
async fn reply_loop(&self, runner: AmbientRunnerHandle);
}
#[derive(Clone)]
pub struct ChannelRegistry {
channels: Vec<Arc<dyn MessageChannel>>,
}
impl ChannelRegistry {
pub fn from_config(config: &SafetyConfig) -> Self {
let mut channels: Vec<Arc<dyn MessageChannel>> = Vec::new();
if config.telegram_enabled
&& let (Some(token), Some(chat_id)) = (
config.telegram_bot_token.clone(),
config.telegram_chat_id.clone(),
)
{
channels.push(Arc::new(TelegramChannel::new(
token,
chat_id,
config.telegram_reply_enabled,
)));
}
if config.discord_enabled
&& let (Some(token), Some(channel_id)) = (
config.discord_bot_token.clone(),
config.discord_channel_id.clone(),
)
{
channels.push(Arc::new(DiscordChannel::new(
token,
channel_id,
config.discord_reply_enabled,
config.discord_bot_user_id.clone(),
)));
}
Self { channels }
}
pub fn send_all(&self, text: &str) {
if tokio::runtime::Handle::try_current().is_err() {
return;
}
for ch in self.channels.iter().filter(|c| c.is_send_enabled()) {
let ch = Arc::clone(ch);
let text = text.to_string();
tokio::spawn(async move {
if let Err(e) = ch.send(&text).await {
logging::error(&format!("{} notification failed: {}", ch.name(), e));
}
});
}
}
pub fn spawn_reply_loops(&self, runner: &AmbientRunnerHandle) {
for ch in self.channels.iter().filter(|c| c.is_reply_enabled()) {
let ch = Arc::clone(ch);
let runner = runner.clone();
tokio::spawn(async move {
logging::info(&format!("{} reply loop spawned", ch.name()));
ch.reply_loop(runner).await;
});
}
}
pub fn channel_names(&self) -> Vec<String> {
self.channels.iter().map(|c| c.name().to_string()).collect()
}
pub fn find_by_name(&self, name: &str) -> Option<Arc<dyn MessageChannel>> {
self.channels.iter().find(|c| c.name() == name).cloned()
}
pub fn send_enabled(&self) -> Vec<Arc<dyn MessageChannel>> {
self.channels
.iter()
.filter(|c| c.is_send_enabled())
.cloned()
.collect()
}
}
// ---------------------------------------------------------------------------
// Telegram channel
// ---------------------------------------------------------------------------
pub struct TelegramChannel {
token: String,
chat_id: String,
reply_enabled: bool,
client: reqwest::Client,
}
impl TelegramChannel {
pub fn new(token: String, chat_id: String, reply_enabled: bool) -> Self {
Self {
token,
chat_id,
reply_enabled,
client: crate::provider::shared_http_client(),
}
}
}
#[async_trait]
impl MessageChannel for TelegramChannel {
fn name(&self) -> &str {
"telegram"
}
fn is_send_enabled(&self) -> bool {
true
}
fn is_reply_enabled(&self) -> bool {
self.reply_enabled
}
async fn send(&self, text: &str) -> anyhow::Result<()> {
crate::telegram::send_message(&self.client, &self.token, &self.chat_id, text).await
}
async fn reply_loop(&self, runner: AmbientRunnerHandle) {
let mut offset: Option<i64> = None;
loop {
match crate::telegram::get_updates(&self.client, &self.token, offset, 30).await {
Ok(updates) => {
for update in updates {
offset = Some(update.update_id + 1);
let msg = match update.message {
Some(m) => m,
None => continue,
};
if msg.chat.id.to_string() != self.chat_id {
continue;
}
let text = match msg.text {
Some(t) => t,
None => continue,
};
let trimmed = text.trim();
if trimmed.is_empty() {
continue;
}
if let Some(req_id) = crate::notifications::extract_permission_id(trimmed) {
let (approved, message) =
crate::notifications::parse_permission_reply(trimmed);
if let Err(e) = crate::safety::record_permission_via_file(
&req_id,
approved,
"telegram_reply",
message,
) {
logging::error(&format!(
"Failed to record permission from Telegram for {}: {}",
req_id, e
));
} else {
logging::info(&format!(
"Permission {} via Telegram: {}",
if approved { "approved" } else { "denied" },
req_id
));
let _ = self
.send(&format!(
"✅ Permission {} for `{}`",
if approved { "approved" } else { "denied" },
req_id
))
.await;
}
} else {
let injected = runner.inject_message(trimmed, "telegram").await;
let ack = if injected {
format!("💬 Message sent to active session: _{}_", trimmed)
} else {
format!("📋 Message queued, waking agent: _{}_", trimmed)
};
let _ = self.send(&ack).await;
}
}
}
Err(e) => {
logging::error(&format!("Telegram poll error: {}", e));
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
}
}
}
}
// ---------------------------------------------------------------------------
// Discord channel
// ---------------------------------------------------------------------------
pub struct DiscordChannel {
token: String,
channel_id: String,
reply_enabled: bool,
bot_user_id: Option<String>,
client: reqwest::Client,
}
impl DiscordChannel {
pub fn new(
token: String,
channel_id: String,
reply_enabled: bool,
bot_user_id: Option<String>,
) -> Self {
Self {
token,
channel_id,
reply_enabled,
bot_user_id,
client: crate::provider::shared_http_client(),
}
}
async fn poll_messages(&self, after: Option<&str>) -> anyhow::Result<Vec<DiscordMessage>> {
let mut url = format!(
"https://discord.com/api/v10/channels/{}/messages?limit=10",
self.channel_id
);
if let Some(after_id) = after {
url.push_str(&format!("&after={}", after_id));
}
let resp = self
.client
.get(&url)
.header("Authorization", format!("Bot {}", self.token))
.send()
.await?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
anyhow::bail!("Discord messages error ({}): {}", status, body);
}
let messages: Vec<DiscordMessage> = resp.json().await?;
Ok(messages)
}
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct DiscordMessage {
pub id: String,
pub content: String,
pub author: DiscordAuthor,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct DiscordAuthor {
pub id: String,
pub bot: Option<bool>,
}
#[async_trait]
impl MessageChannel for DiscordChannel {
fn name(&self) -> &str {
"discord"
}
fn is_send_enabled(&self) -> bool {
true
}
fn is_reply_enabled(&self) -> bool {
self.reply_enabled
}
async fn send(&self, text: &str) -> anyhow::Result<()> {
let url = format!(
"https://discord.com/api/v10/channels/{}/messages",
self.channel_id
);
let resp = self
.client
.post(&url)
.header("Authorization", format!("Bot {}", self.token))
.json(&serde_json::json!({ "content": text }))
.send()
.await?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
anyhow::bail!("Discord API error ({}): {}", status, body);
}
logging::info("Discord notification sent");
Ok(())
}
async fn reply_loop(&self, runner: AmbientRunnerHandle) {
let mut last_seen_id: Option<String> = None;
// Get the latest message ID on startup so we don't replay old messages
match self.poll_messages(None).await {
Ok(msgs) => {
if let Some(latest) = msgs.first() {
last_seen_id = Some(latest.id.clone());
}
}
Err(e) => {
logging::error(&format!("Discord initial poll error: {}", e));
}
}
loop {
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
match self.poll_messages(last_seen_id.as_deref()).await {
Ok(msgs) => {
// Discord returns newest first, reverse for chronological order
let mut msgs = msgs;
msgs.reverse();
for msg in msgs {
last_seen_id = Some(msg.id.clone());
// Skip messages from bots (including ourselves)
if msg.author.bot.unwrap_or(false) {
continue;
}
// If we know our bot user ID, also skip our own messages
if let Some(ref bot_id) = self.bot_user_id
&& msg.author.id == *bot_id
{
continue;
}
let trimmed = msg.content.trim();
if trimmed.is_empty() {
continue;
}
if let Some(req_id) = crate::notifications::extract_permission_id(trimmed) {
let (approved, message) =
crate::notifications::parse_permission_reply(trimmed);
if let Err(e) = crate::safety::record_permission_via_file(
&req_id,
approved,
"discord_reply",
message,
) {
logging::error(&format!(
"Failed to record permission from Discord for {}: {}",
req_id, e
));
} else {
logging::info(&format!(
"Permission {} via Discord: {}",
if approved { "approved" } else { "denied" },
req_id
));
let _ = self
.send(&format!(
"✅ Permission {} for `{}`",
if approved { "approved" } else { "denied" },
req_id
))
.await;
}
} else {
let injected = runner.inject_message(trimmed, "discord").await;
let ack = if injected {
format!("💬 Message sent to active session: *{}*", trimmed)
} else {
format!("📋 Message queued, waking agent: *{}*", trimmed)
};
let _ = self.send(&ack).await;
}
}
}
Err(e) => {
logging::error(&format!("Discord poll error: {}", e));
tokio::time::sleep(std::time::Duration::from_secs(10)).await;
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_discord_message_parse() {
let json = r#"{
"id": "123456",
"content": "hello agent",
"author": {"id": "789", "bot": false}
}"#;
let msg: DiscordMessage = serde_json::from_str(json).unwrap();
assert_eq!(msg.id, "123456");
assert_eq!(msg.content, "hello agent");
assert!(!msg.author.bot.unwrap());
}
#[test]
fn test_discord_bot_message_parse() {
let json = r#"{
"id": "999",
"content": "bot response",
"author": {"id": "111", "bot": true}
}"#;
let msg: DiscordMessage = serde_json::from_str(json).unwrap();
assert!(msg.author.bot.unwrap());
}
}