acp-runtime 0.1.2

Rust SDK for the Agent Communication Protocol (ACP)
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
// Copyright 2026 ACP Project
// Licensed under the Apache License, Version 2.0
// See LICENSE file for details.

use std::collections::HashMap;

use regex::Regex;
use serde_json::{Map, Value};

use crate::errors::{AcpError, AcpResult};
use crate::json_support;

pub const DEFAULT_AMQP_EXCHANGE: &str = "acp.exchange";
pub const DEFAULT_AMQP_EXCHANGE_TYPE: &str = "direct";

pub type AmqpMessageHandler = dyn FnMut(&Map<String, Value>) -> bool + Send;

#[derive(Debug, Clone)]
pub struct AmqpTransportClient {
    pub broker_url: String,
    pub exchange: String,
    pub exchange_type: String,
    pub timeout_seconds: u64,
}

impl AmqpTransportClient {
    pub fn new(
        broker_url: impl Into<String>,
        exchange: Option<String>,
        exchange_type: Option<String>,
        timeout_seconds: u64,
    ) -> AcpResult<Self> {
        let broker_url = broker_url.into();
        if broker_url.trim().is_empty() {
            return Err(AcpError::InvalidArgument(
                "broker_url must be provided".to_string(),
            ));
        }
        Ok(Self {
            broker_url,
            exchange: exchange
                .as_deref()
                .map(str::trim)
                .filter(|v| !v.is_empty())
                .unwrap_or(DEFAULT_AMQP_EXCHANGE)
                .to_string(),
            exchange_type: exchange_type
                .as_deref()
                .map(str::trim)
                .filter(|v| !v.is_empty())
                .unwrap_or(DEFAULT_AMQP_EXCHANGE_TYPE)
                .to_string(),
            timeout_seconds: timeout_seconds.max(1),
        })
    }

    pub fn agent_identifier_token(agent_id: &str) -> AcpResult<String> {
        let regex = Regex::new(r"^agent:(?P<name>[^@]+)(?:@(?P<domain>.+))?$")
            .map_err(|e| AcpError::Validation(format!("invalid agent id regex: {e}")))?;
        let captures = regex
            .captures(agent_id)
            .ok_or_else(|| AcpError::Validation(format!("Invalid agent identifier: {agent_id}")))?;
        let name = captures
            .name("name")
            .map(|m| m.as_str().to_string())
            .ok_or_else(|| AcpError::Validation("agent id missing name".to_string()))?;
        let domain = captures.name("domain").map(|m| m.as_str().to_string());
        let base = if let Some(domain) = domain {
            format!("{name}.{domain}")
        } else {
            name
        };
        let normalized = base
            .chars()
            .map(|ch| {
                if ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-') {
                    ch
                } else {
                    '.'
                }
            })
            .collect::<String>()
            .split('.')
            .filter(|part| !part.is_empty())
            .collect::<Vec<_>>()
            .join(".");
        Ok(if normalized.is_empty() {
            "unknown".to_string()
        } else {
            normalized
        })
    }

    pub fn queue_name_for_agent(agent_id: &str) -> AcpResult<String> {
        Ok(format!(
            "acp.agent.{}",
            Self::agent_identifier_token(agent_id)?
        ))
    }

    pub fn routing_key_for_agent(agent_id: &str) -> AcpResult<String> {
        Ok(format!("agent.{}", Self::agent_identifier_token(agent_id)?))
    }

    pub fn build_service_hint(
        agent_id: &str,
        broker_url: &str,
        exchange: Option<&str>,
    ) -> AcpResult<Map<String, Value>> {
        let mut hint = Map::new();
        hint.insert(
            "broker_url".to_string(),
            Value::String(broker_url.to_string()),
        );
        hint.insert(
            "exchange".to_string(),
            Value::String(exchange.unwrap_or(DEFAULT_AMQP_EXCHANGE).to_string()),
        );
        hint.insert(
            "queue".to_string(),
            Value::String(Self::queue_name_for_agent(agent_id)?),
        );
        hint.insert(
            "routing_key".to_string(),
            Value::String(Self::routing_key_for_agent(agent_id)?),
        );
        Ok(hint)
    }

    pub fn publish(
        &self,
        message: &Map<String, Value>,
        recipient_agent_id: &str,
        amqp_service: Option<&Map<String, Value>>,
    ) -> AcpResult<()> {
        let broker_url = pick_string(amqp_service, "broker_url", &self.broker_url);
        let exchange = pick_string(amqp_service, "exchange", &self.exchange);
        let queue = pick_string(
            amqp_service,
            "queue",
            &Self::queue_name_for_agent(recipient_agent_id)?,
        );
        let routing_key = pick_string(
            amqp_service,
            "routing_key",
            &Self::routing_key_for_agent(recipient_agent_id)?,
        );
        let headers = metadata_headers(message);
        let body = json_support::canonical_json_string(&Value::Object(message.clone()))?;
        let exchange_type = self.exchange_type.clone();
        let timeout = self.timeout_seconds;

        run_async(timeout, async move {
            use lapin::options::{
                BasicPublishOptions, ExchangeDeclareOptions, QueueBindOptions, QueueDeclareOptions,
            };
            use lapin::types::{AMQPValue, FieldTable, LongString, ShortString};
            use lapin::{BasicProperties, Connection, ConnectionProperties, ExchangeKind};

            let connection = Connection::connect(&broker_url, ConnectionProperties::default())
                .await
                .map_err(|e| AcpError::Transport(format!("amqp connect failed: {e}")))?;
            let channel = connection
                .create_channel()
                .await
                .map_err(|e| AcpError::Transport(format!("amqp channel failed: {e}")))?;
            let kind = match exchange_type.to_lowercase().as_str() {
                "fanout" => ExchangeKind::Fanout,
                "topic" => ExchangeKind::Topic,
                "headers" => ExchangeKind::Headers,
                _ => ExchangeKind::Direct,
            };
            channel
                .exchange_declare(
                    &exchange,
                    kind,
                    ExchangeDeclareOptions {
                        durable: true,
                        ..Default::default()
                    },
                    FieldTable::default(),
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp exchange declare failed: {e}")))?;
            channel
                .queue_declare(
                    &queue,
                    QueueDeclareOptions {
                        durable: true,
                        ..Default::default()
                    },
                    FieldTable::default(),
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp queue declare failed: {e}")))?;
            channel
                .queue_bind(
                    &queue,
                    &exchange,
                    &routing_key,
                    QueueBindOptions::default(),
                    FieldTable::default(),
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp queue bind failed: {e}")))?;
            let mut table = FieldTable::default();
            for (key, value) in headers {
                table.insert(
                    ShortString::from(key),
                    AMQPValue::LongString(LongString::from(value)),
                );
            }
            let properties = BasicProperties::default()
                .with_content_type(ShortString::from("application/json"))
                .with_delivery_mode(2)
                .with_headers(table);
            channel
                .basic_publish(
                    &exchange,
                    &routing_key,
                    BasicPublishOptions::default(),
                    body.as_bytes(),
                    properties,
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp publish failed: {e}")))?
                .await
                .map_err(|e| AcpError::Transport(format!("amqp publisher confirm failed: {e}")))?;
            connection
                .close(200, "ok")
                .await
                .map_err(|e| AcpError::Transport(format!("amqp close failed: {e}")))?;
            Ok(())
        })
    }

    pub fn consume<F>(
        &self,
        agent_id: &str,
        mut handler: F,
        amqp_service: Option<&Map<String, Value>>,
        max_messages: usize,
    ) -> AcpResult<usize>
    where
        F: FnMut(&Map<String, Value>) -> bool + Send + 'static,
    {
        let broker_url = pick_string(amqp_service, "broker_url", &self.broker_url);
        let exchange = pick_string(amqp_service, "exchange", &self.exchange);
        let queue = pick_string(
            amqp_service,
            "queue",
            &Self::queue_name_for_agent(agent_id)?,
        );
        let routing_key = pick_string(
            amqp_service,
            "routing_key",
            &Self::routing_key_for_agent(agent_id)?,
        );
        let limit = if max_messages == 0 {
            usize::MAX
        } else {
            max_messages
        };
        let exchange_type = self.exchange_type.clone();
        let timeout = self.timeout_seconds;

        run_async(timeout, async move {
            use lapin::options::{
                BasicAckOptions, BasicGetOptions, BasicNackOptions, ExchangeDeclareOptions,
                QueueBindOptions, QueueDeclareOptions,
            };
            use lapin::types::FieldTable;
            use lapin::{Connection, ConnectionProperties, ExchangeKind};
            let connection = Connection::connect(&broker_url, ConnectionProperties::default())
                .await
                .map_err(|e| AcpError::Transport(format!("amqp connect failed: {e}")))?;
            let channel = connection
                .create_channel()
                .await
                .map_err(|e| AcpError::Transport(format!("amqp channel failed: {e}")))?;
            let kind = match exchange_type.to_lowercase().as_str() {
                "fanout" => ExchangeKind::Fanout,
                "topic" => ExchangeKind::Topic,
                "headers" => ExchangeKind::Headers,
                _ => ExchangeKind::Direct,
            };
            channel
                .exchange_declare(
                    &exchange,
                    kind,
                    ExchangeDeclareOptions {
                        durable: true,
                        ..Default::default()
                    },
                    FieldTable::default(),
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp exchange declare failed: {e}")))?;
            channel
                .queue_declare(
                    &queue,
                    QueueDeclareOptions {
                        durable: true,
                        ..Default::default()
                    },
                    FieldTable::default(),
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp queue declare failed: {e}")))?;
            channel
                .queue_bind(
                    &queue,
                    &exchange,
                    &routing_key,
                    QueueBindOptions::default(),
                    FieldTable::default(),
                )
                .await
                .map_err(|e| AcpError::Transport(format!("amqp queue bind failed: {e}")))?;

            let mut processed = 0usize;
            while processed < limit {
                let delivery = channel
                    .basic_get(&queue, BasicGetOptions { no_ack: false })
                    .await
                    .map_err(|e| AcpError::Transport(format!("amqp basic_get failed: {e}")))?;
                let Some(delivery) = delivery else {
                    break;
                };
                let should_ack = std::str::from_utf8(&delivery.data)
                    .ok()
                    .and_then(|body| json_support::map_from_json(body).ok())
                    .map(|message| handler(&message))
                    .unwrap_or(false);
                if should_ack {
                    delivery
                        .ack(BasicAckOptions::default())
                        .await
                        .map_err(|e| AcpError::Transport(format!("amqp ack failed: {e}")))?;
                } else {
                    delivery
                        .nack(BasicNackOptions {
                            requeue: true,
                            ..Default::default()
                        })
                        .await
                        .map_err(|e| AcpError::Transport(format!("amqp nack failed: {e}")))?;
                }
                processed += 1;
            }
            connection
                .close(200, "ok")
                .await
                .map_err(|e| AcpError::Transport(format!("amqp close failed: {e}")))?;
            Ok(processed)
        })
    }
}

fn metadata_headers(message: &Map<String, Value>) -> HashMap<String, String> {
    let mut headers = HashMap::new();
    if let Some(envelope) = message.get("envelope").and_then(Value::as_object) {
        for (src, dest) in [
            ("acp_version", "acp_version"),
            ("message_class", "acp_message_class"),
            ("message_id", "acp_message_id"),
            ("operation_id", "acp_operation_id"),
            ("sender", "acp_sender"),
        ] {
            if let Some(value) = envelope
                .get(src)
                .and_then(Value::as_str)
                .map(str::trim)
                .filter(|v| !v.is_empty())
            {
                headers.insert(dest.to_string(), value.to_string());
            }
        }
    }
    headers
}

fn pick_string(service: Option<&Map<String, Value>>, key: &str, fallback: &str) -> String {
    service
        .and_then(|map| map.get(key))
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|v| !v.is_empty())
        .unwrap_or(fallback)
        .to_string()
}

fn run_async<T>(
    timeout_seconds: u64,
    fut: impl std::future::Future<Output = AcpResult<T>> + Send + 'static,
) -> AcpResult<T>
where
    T: Send + 'static,
{
    let runtime = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .map_err(|e| AcpError::Transport(format!("failed to create tokio runtime: {e}")))?;
    runtime.block_on(async move {
        match tokio::time::timeout(std::time::Duration::from_secs(timeout_seconds.max(1)), fut)
            .await
        {
            Ok(result) => result,
            Err(_) => Err(AcpError::Transport("amqp operation timed out".to_string())),
        }
    })
}