adk-drivers 3.0.0

Service drivers for ADK-Rust agents: DNS and registrar, email, analytics and search console, social and advertising, each with a kernel driver manifest, a conformance suite and reference adapters
Documentation
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Social drivers (spec 3.5 §7.13): mentions and messages read in the public partition,
//! replies in their thread, posts from the back office; Propose by default.
//!
//! | Tool | Effect class | Side | Default cap |
//! |---|---|---|---|
//! | `social_list_mentions` | `read` | public | Read |
//! | `social_list_messages` | `read` | public | Read |
//! | `social_reply_in_thread` | `send_external` | public | Propose |
//! | `social_post` | `publish_public` | back office | Propose |
//!
//! [`SocialOutbox`] keeps the outbound rules over any [`SocialDriver`]: the disclosure line
//! on every reply and post (A-I17), the suppression list before every reply, replies only
//! to the author of an item the inbox read, from a playbook template, at a rate limit.
//!
//! | Adapter | Feature |
//! |---|---|
//! | [`InMemorySocial`] | — |
//! | `MastodonSocial`: the Mastodon client API of an ActivityPub server (Mastodon, GoToSocial, Pleroma, Akkoma) | `mastodon` |

use std::collections::BTreeMap;
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use adk_core::Tool;
use adk_kernel::drivers::EffectClass;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::class::{AutonomyCap, Side, ToolSpec};
use crate::error::{DriverError, Result};
use crate::outbound::{
    Disclosure, InMemorySuppressions, Limiter, RateLimit, ReplyTemplates, SuppressionList,
    SuppressionScope, ThreadReplier, fields_arg, str_arg,
};
use crate::tools::DriverTool;

#[cfg(feature = "mastodon")]
mod mastodon;

#[cfg(feature = "mastodon")]
pub use mastodon::MastodonSocial;

/// The longest text kept from a social item, in characters.
const MAX_ITEM_CHARS: usize = 5_000;

/// A mention or a direct message (spec 3.5 §7.13): untrusted input, read in the public
/// partition only (A-I3).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SocialItem {
    /// The item's id on the service; a reply answers it.
    pub id: String,
    /// The author's handle, such as `ana@social.example`.
    pub author: String,
    /// The text, without markup, at most 5,000 characters.
    pub text: String,
    /// When it was posted.
    pub at: DateTime<Utc>,
    /// Who sees it: `public`, `unlisted`, `private` or `direct`; a reply keeps it.
    pub visibility: String,
    /// Its address, when it has one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

impl SocialItem {
    /// An item `id` by `author` saying `text`, for tests and the in-memory driver.
    pub fn new(
        id: impl Into<String>,
        author: impl Into<String>,
        text: impl Into<String>,
        visibility: &str,
    ) -> Self {
        Self {
            id: id.into(),
            author: author.into(),
            text: text.into(),
            at: Utc::now(),
            visibility: visibility.to_string(),
            url: None,
        }
    }

    /// Whether it is a direct message.
    pub fn is_direct(&self) -> bool {
        self.visibility == "direct"
    }
}

/// A page of items after a cursor.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SocialPage {
    /// The items, oldest first.
    pub items: Vec<SocialItem>,
    /// The cursor after the last item; unchanged when there are none.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
}

/// What a reply or post created.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SocialPosted {
    /// Its id.
    pub id: String,
    /// Its address.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
    /// When.
    pub at: DateTime<Utc>,
}

/// A social account (spec 3.5 §7.13): reads mentions and messages, replies, posts.
///
/// It only moves posts; [`SocialOutbox`] keeps the rules.
#[async_trait]
pub trait SocialDriver: Send + Sync {
    /// The adapter's name.
    fn name(&self) -> &str;

    /// The longest post the service takes, in characters.
    fn max_chars(&self) -> usize;

    /// Public mentions of the account after `cursor`, oldest first, at most `limit`.
    ///
    /// # Errors
    ///
    /// Returns the service's errors.
    async fn mentions(&self, cursor: Option<&str>, limit: usize) -> Result<SocialPage>;

    /// Direct messages to the account after `cursor`, oldest first, at most `limit`.
    ///
    /// # Errors
    ///
    /// Returns the service's errors.
    async fn messages(&self, cursor: Option<&str>, limit: usize) -> Result<SocialPage>;

    /// Replies `text` to `item`, addressed to its author only, with its visibility.
    ///
    /// # Errors
    ///
    /// Returns the service's errors.
    async fn reply(&self, item: &SocialItem, text: &str) -> Result<SocialPosted>;

    /// Posts `text` publicly.
    ///
    /// # Errors
    ///
    /// Returns the service's errors.
    async fn post(&self, text: &str) -> Result<SocialPosted>;
}

/// A social account in process memory (spec 3.5 §7.13), for tests and as the hermetic
/// reference of the [`SocialDriver`] conformance suite.
#[derive(Debug, Default)]
pub struct InMemorySocial {
    received: Mutex<Vec<SocialItem>>,
    published: Mutex<Vec<(Option<String>, SocialItem)>>,
}

impl InMemorySocial {
    /// An account with nothing received.
    pub fn new() -> Self {
        Self::default()
    }

    /// `item` arrives: a mention, or a direct message when its visibility is `direct`.
    pub fn receive(&self, item: SocialItem) {
        self.received.lock().unwrap_or_else(PoisonError::into_inner).push(item);
    }

    /// Everything posted, with the item each reply answers.
    pub fn published(&self) -> Vec<(Option<String>, SocialItem)> {
        self.published.lock().unwrap_or_else(PoisonError::into_inner).clone()
    }

    fn page(&self, direct: bool, cursor: Option<&str>, limit: usize) -> Result<SocialPage> {
        let after = match cursor {
            Some(cursor) => cursor.parse::<usize>().map_err(|_| {
                DriverError::invalid(format!("`{cursor}` is not a cursor this driver gave"))
            })?,
            None => 0,
        };
        let received = self.received.lock().unwrap_or_else(PoisonError::into_inner);
        let mut items = Vec::new();
        let mut last = after;
        for (index, item) in received.iter().enumerate().skip(after) {
            if items.len() == limit {
                break;
            }
            last = index + 1;
            if item.is_direct() == direct {
                items.push(item.clone());
            }
        }
        Ok(SocialPage { items, cursor: Some(last.to_string()) })
    }

    fn publish(&self, answers: Option<&SocialItem>, text: &str, visibility: &str) -> SocialPosted {
        let mut published = self.published.lock().unwrap_or_else(PoisonError::into_inner);
        let id = format!("out-{}", published.len() + 1);
        let item = SocialItem::new(id.clone(), "site", text, visibility);
        published.push((answers.map(|item| item.id.clone()), item));
        SocialPosted { id, url: None, at: Utc::now() }
    }
}

#[async_trait]
impl SocialDriver for InMemorySocial {
    fn name(&self) -> &str {
        "in-memory"
    }

    fn max_chars(&self) -> usize {
        500
    }

    async fn mentions(&self, cursor: Option<&str>, limit: usize) -> Result<SocialPage> {
        self.page(false, cursor, limit)
    }

    async fn messages(&self, cursor: Option<&str>, limit: usize) -> Result<SocialPage> {
        self.page(true, cursor, limit)
    }

    async fn reply(&self, item: &SocialItem, text: &str) -> Result<SocialPosted> {
        Ok(self.publish(Some(item), &format!("@{} {text}", item.author), &item.visibility))
    }

    async fn post(&self, text: &str) -> Result<SocialPosted> {
        Ok(self.publish(None, text, "public"))
    }
}

/// The outbound rules over a [`SocialDriver`] (spec 3.5 §7.10, §7.13, A-I17).
///
/// # Example
///
/// ```rust
/// use std::collections::BTreeMap;
/// use std::sync::Arc;
/// use adk_drivers::social::{InMemorySocial, SocialItem, SocialOutbox};
/// use adk_drivers::{Disclosure, ReplyTemplates};
///
/// # futures::executor::block_on(async {
/// let social = Arc::new(InMemorySocial::new());
/// social.receive(SocialItem::new("1", "ana@social.example", "Open on Sunday?", "public"));
/// let outbox = SocialOutbox::builder(social.clone(), Disclosure::new("Wanjiru's Bakery").unwrap())
///     .templates(ReplyTemplates::new().template("hours", "Yes, {hours} on Sundays."))
///     .build();
/// let page = outbox.read_mentions(None, 10).await.unwrap();
/// let fields = BTreeMap::from([("hours".to_string(), "8–12".to_string())]);
/// outbox.reply(&page.items[0].id, "hours", &fields).await.unwrap();
/// let (answers, reply) = &social.published()[0];
/// assert_eq!(answers.as_deref(), Some("1"));
/// assert!(reply.text.starts_with("@ana@social.example Yes, 8–12 on Sundays."));
/// # });
/// ```
pub struct SocialOutbox {
    driver: Arc<dyn SocialDriver>,
    disclosure: Disclosure,
    suppressions: Arc<dyn SuppressionList>,
    templates: ReplyTemplates,
    per_thread: Limiter,
    replies: Limiter,
    posts: Limiter,
    known: Mutex<BTreeMap<String, SocialItem>>,
}

impl std::fmt::Debug for SocialOutbox {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SocialOutbox").field("driver", &self.driver.name()).finish_non_exhaustive()
    }
}

/// Builds a [`SocialOutbox`].
pub struct SocialOutboxBuilder {
    outbox: SocialOutbox,
}

impl SocialOutboxBuilder {
    /// Where unsubscribes and revocations are kept; share the email outbox's.
    #[must_use]
    pub fn suppressions(mut self, suppressions: Arc<dyn SuppressionList>) -> Self {
        self.outbox.suppressions = suppressions;
        self
    }

    /// The playbook's reply templates.
    #[must_use]
    pub fn templates(mut self, templates: ReplyTemplates) -> Self {
        self.outbox.templates = templates;
        self
    }

    /// The rate limits: replies per thread (2 an hour by default), replies overall (30 an
    /// hour) and posts (6 a day).
    #[must_use]
    pub fn limits(mut self, per_thread: RateLimit, replies: RateLimit, posts: RateLimit) -> Self {
        self.outbox.per_thread = Limiter::new(per_thread);
        self.outbox.replies = Limiter::new(replies);
        self.outbox.posts = Limiter::new(posts);
        self
    }

    /// The outbox.
    pub fn build(self) -> SocialOutbox {
        self.outbox
    }
}

impl SocialOutbox {
    /// An outbox over `driver`, every reply and post ending with `disclosure`.
    pub fn builder(driver: Arc<dyn SocialDriver>, disclosure: Disclosure) -> SocialOutboxBuilder {
        SocialOutboxBuilder {
            outbox: Self {
                driver,
                disclosure,
                suppressions: Arc::new(InMemorySuppressions::new()),
                templates: ReplyTemplates::new(),
                per_thread: Limiter::new(RateLimit::new(2, Duration::from_secs(3_600))),
                replies: Limiter::new(RateLimit::new(30, Duration::from_secs(3_600))),
                posts: Limiter::new(RateLimit::new(6, Duration::from_secs(86_400))),
                known: Mutex::default(),
            },
        }
    }

    /// The driver.
    pub fn driver(&self) -> &Arc<dyn SocialDriver> {
        &self.driver
    }

    /// The suppression list.
    pub fn suppressions(&self) -> &Arc<dyn SuppressionList> {
        &self.suppressions
    }

    fn remember(&self, page: &SocialPage) {
        let mut known = self.known.lock().unwrap_or_else(PoisonError::into_inner);
        for item in &page.items {
            known.insert(item.id.clone(), item.clone());
        }
    }

    /// Reads mentions after `cursor`, remembering each for a reply.
    ///
    /// # Errors
    ///
    /// Returns the driver's errors.
    pub async fn read_mentions(&self, cursor: Option<&str>, limit: usize) -> Result<SocialPage> {
        let page = self.driver.mentions(cursor, limit).await?;
        self.remember(&page);
        Ok(page)
    }

    /// Reads direct messages after `cursor`, remembering each for a reply.
    ///
    /// # Errors
    ///
    /// Returns the driver's errors.
    pub async fn read_messages(&self, cursor: Option<&str>, limit: usize) -> Result<SocialPage> {
        let page = self.driver.messages(cursor, limit).await?;
        self.remember(&page);
        Ok(page)
    }

    /// The text that will go out: `text` with the disclosure, within the service's length.
    fn outgoing(&self, text: &str, prefix: usize) -> Result<String> {
        let text = self.disclosure.apply(text);
        if text.chars().count() + prefix > self.driver.max_chars() {
            return Err(DriverError::invalid(format!(
                "the text with its disclosure is longer than the {} characters {} takes",
                self.driver.max_chars(),
                self.driver.name()
            )));
        }
        Ok(text)
    }

    /// Replies to item `item` with template `template` (spec 3.5 §7.10): to its author
    /// only, ending with the disclosure.
    ///
    /// # Errors
    ///
    /// Returns [`DriverError::Refused`] for an item the inbox did not read, a revoked author
    /// or an unknown template; [`DriverError::RateLimited`] over a limit; and the driver's
    /// errors.
    pub async fn reply(
        &self,
        item: &str,
        template: &str,
        fields: &BTreeMap<String, String>,
    ) -> Result<SocialPosted> {
        let item = self
            .known
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .get(item)
            .cloned()
            .ok_or_else(|| {
                DriverError::refused("a social reply answers only an item the inbox read")
            })?;
        let rendered = self.templates.render(template, fields)?;
        if let Some(suppression) = self.suppressions.lookup(&social_key(&item.author)).await?
            && suppression.scope == SuppressionScope::All
        {
            return Err(DriverError::refused(format!("{} asked not to be contacted", item.author)));
        }
        let text = self.outgoing(&rendered, item.author.chars().count() + 2)?;
        self.per_thread.take(&item.id, "replies to one item")?;
        self.replies.take("all", "social replies")?;
        self.driver.reply(&item, &text).await
    }

    /// Posts `text` publicly, ending with the disclosure.
    ///
    /// # Errors
    ///
    /// Returns [`DriverError::Invalid`] for a text too long for the service with its
    /// disclosure, [`DriverError::RateLimited`] over the post limit, and the driver's errors.
    pub async fn post(&self, text: &str) -> Result<SocialPosted> {
        let text = self.outgoing(text, 0)?;
        self.posts.take("all", "posts")?;
        self.driver.post(&text).await
    }
}

/// A social handle as suppressions key it: `social:` and the handle without its `@`.
pub fn social_key(handle: &str) -> String {
    format!("social:{}", handle.trim().trim_start_matches('@').to_lowercase())
}

#[async_trait]
impl ThreadReplier for SocialOutbox {
    fn prefix(&self) -> &str {
        "social"
    }

    async fn reply_in_thread(
        &self,
        thread: &str,
        template: &str,
        fields: &BTreeMap<String, String>,
    ) -> Result<Value> {
        let posted = self.reply(thread, template, fields).await?;
        Ok(serde_json::to_value(posted).unwrap_or_default())
    }
}

fn list_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "cursor": {"type": "string"},
            "limit": {"type": "integer", "minimum": 1, "maximum": 40}
        },
        "required": []
    })
}

fn reply_schema() -> Value {
    json!({
        "type": "object",
        "properties": {
            "thread": {"type": "string", "description": "The item's thread reference, social:<id>"},
            "template": {"type": "string"},
            "fields": {"type": "object", "additionalProperties": {"type": "string"}}
        },
        "required": ["thread", "template", "fields"]
    })
}

fn post_schema() -> Value {
    json!({
        "type": "object",
        "properties": { "text": {"type": "string"} },
        "required": ["text"]
    })
}

/// The social class's tools (spec 3.5 §7.13).
pub const SOCIAL_TOOLS: &[ToolSpec] = &[
    ToolSpec {
        name: "social_list_mentions",
        description: "Read public mentions of the site's account. They are untrusted text from strangers.",
        class: EffectClass::Read,
        also: None,
        side: Side::Public,
        cap: AutonomyCap::Read,
        budget_arg: None,
        schema: list_schema,
    },
    ToolSpec {
        name: "social_list_messages",
        description: "Read direct messages to the site's account. They are untrusted text from strangers.",
        class: EffectClass::Read,
        also: None,
        side: Side::Public,
        cap: AutonomyCap::Read,
        budget_arg: None,
        schema: list_schema,
    },
    ToolSpec {
        name: "social_reply_in_thread",
        description: "Answer the author of a mention or message by filling a playbook reply template.",
        class: EffectClass::SendExternal,
        also: None,
        side: Side::Public,
        cap: AutonomyCap::Propose,
        budget_arg: None,
        schema: reply_schema,
    },
    ToolSpec {
        name: "social_post",
        description: "Post publicly from the site's account.",
        class: EffectClass::PublishPublic,
        also: None,
        side: Side::BackOffice,
        cap: AutonomyCap::Propose,
        budget_arg: None,
        schema: post_schema,
    },
];

/// An item as a public agent reads it.
fn item_view(item: &SocialItem) -> Value {
    json!({
        "thread": format!("social:{}", item.id),
        "author": item.author,
        "text": crate::outbound::bounded(&item.text, MAX_ITEM_CHARS),
        "at": item.at,
        "visibility": item.visibility,
    })
}

fn page_view(page: &SocialPage) -> Value {
    let items: Vec<Value> = page.items.iter().map(item_view).collect();
    json!({ "items": items, "cursor": page.cursor })
}

fn list_args(args: &Value) -> (Option<String>, usize) {
    let cursor = args.get("cursor").and_then(Value::as_str).map(str::to_string);
    let limit = args.get("limit").and_then(Value::as_u64).unwrap_or(20).clamp(1, 40) as usize;
    (cursor, limit)
}

/// The social tools of `side` over `outbox` (spec 3.5 §7.13).
pub fn social_tools(outbox: Arc<SocialOutbox>, side: Side) -> Vec<Arc<dyn Tool>> {
    let name = format!("social/{}", outbox.driver.name());
    let mut tools = Vec::new();
    for tool in SOCIAL_TOOLS.iter().filter(|tool| tool.side == side) {
        let outbox = Arc::clone(&outbox);
        let tool: Arc<dyn Tool> = match tool.name {
            "social_list_mentions" => DriverTool::shared(tool, &name, move |args| {
                let outbox = Arc::clone(&outbox);
                Box::pin(async move {
                    let (cursor, limit) = list_args(&args);
                    Ok(page_view(&outbox.read_mentions(cursor.as_deref(), limit).await?))
                })
            }),
            "social_list_messages" => DriverTool::shared(tool, &name, move |args| {
                let outbox = Arc::clone(&outbox);
                Box::pin(async move {
                    let (cursor, limit) = list_args(&args);
                    Ok(page_view(&outbox.read_messages(cursor.as_deref(), limit).await?))
                })
            }),
            "social_reply_in_thread" => DriverTool::shared(tool, &name, move |args| {
                let outbox = Arc::clone(&outbox);
                Box::pin(async move {
                    let thread = str_arg(&args, "thread")?;
                    let item = thread.strip_prefix("social:").unwrap_or(&thread).to_string();
                    let posted = outbox
                        .reply(&item, &str_arg(&args, "template")?, &fields_arg(&args)?)
                        .await?;
                    Ok(serde_json::to_value(posted).unwrap_or_default())
                })
            }),
            _ => DriverTool::shared(tool, &name, move |args| {
                let outbox = Arc::clone(&outbox);
                Box::pin(async move {
                    let posted = outbox.post(&str_arg(&args, "text")?).await?;
                    Ok(serde_json::to_value(posted).unwrap_or_default())
                })
            }),
        };
        tools.push(tool);
    }
    tools
}