anda_core 0.13.7

Core types and traits for Anda -- an AI agent framework built with Rust, powered by ICP and TEEs.
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
//! HTTP utilities for CBOR and Candid RPC calls.
//!
//! This module provides functionality for:
//! - Making CBOR-encoded RPC calls;
//! - Making Candid-encoded canister calls;
//! - Handling HTTP requests and responses;
//! - Error handling for RPC operations.
//!
//! The main types are:
//! - [`RPCRequest`]: Represents a generic RPC request with CBOR-encoded parameters;
//! - [`CanisterRequestRef`]: Represents a canister-specific request with Candid-encoded parameters;
//! - [`RPCResponse`]: Represents a response from an RPC call;
//! - [`HttpRPCError`]: Represents possible errors during RPC operations.
//!
//! The main functions are:
//! - [`http_rpc`]: Makes a generic CBOR-encoded RPC call;
//! - [`canister_rpc`]: Makes a canister-specific RPC call with Candid encoding;
//! - [`cbor_rpc`]: Internal function for making CBOR-encoded HTTP requests.

use candid::{CandidType, Principal, decode_args, encode_args, utils::ArgumentEncoder};
use cbor2::{from_slice, to_canonical_vec};
use http::header;
use ic_auth_types::ByteBufB64;
use reqwest::Client;
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::fmt::Display;

/// MIME type used for CBOR-encoded RPC request and response bodies.
pub const CONTENT_TYPE_CBOR: &str = "application/cbor";
/// MIME type used for JSON HTTP request and response bodies.
pub const CONTENT_TYPE_JSON: &str = "application/json";
/// MIME type used for plain text HTTP response bodies.
pub const CONTENT_TYPE_TEXT: &str = "text/plain";

/// Owned RPC request with a method name and CBOR-encoded parameters.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RPCRequest {
    /// The method name to call.
    pub method: String,

    /// CBOR-encoded parameters for the RPC call.
    /// Parameters should be provided as a tuple, where each element represents a single argument.
    /// Examples:
    /// - `()`: No arguments;
    /// - `(1,)`: Single argument;
    /// - `(1, "hello", 3.14)`: Three arguments.
    pub params: ByteBufB64,
}

/// Borrowed RPC request with a method name and CBOR-encoded parameters.
#[derive(Clone, Debug, Serialize)]
pub struct RPCRequestRef<'a> {
    /// The method name to call.
    pub method: &'a str,
    /// CBOR-encoded parameters for the RPC call.
    /// Parameters should be provided as a tuple, where each element represents a single argument.
    /// Examples:
    /// - `()`: No arguments;
    /// - `(1,)`: Single argument;
    /// - `(1, "hello", 3.14)`: Three arguments.
    pub params: &'a ByteBufB64,
}

/// Borrowed request to an ICP canister with Candid-encoded parameters.
#[derive(Clone, Debug, Serialize)]
pub struct CanisterRequestRef<'a> {
    /// The target canister's principal ID
    pub canister: &'a Principal,
    /// The method name to call on the canister
    pub method: &'a str,
    /// Candid-encoded parameters for the canister call.
    /// Parameters should be provided as a tuple, where each element represents a single argument.
    /// Examples:
    /// - `()`: No arguments;
    /// - `(1,)`: Single argument;
    /// - `(1, "hello", 3.14)`: Three arguments.
    pub params: &'a ByteBufB64,
}

/// RPC response payload returned by remote engines.
///
/// `Ok` contains the CBOR or Candid encoded success payload. `Err` contains a
/// remote error message.
pub type RPCResponse = Result<ByteBufB64, String>;

// #[derive(Debug, Deserialize, Serialize)]
// pub struct ListPagination {
//     pub id: String,
//     pub page_token: Option<String>,
//     pub page_size: Option<u16>,
// }

/// Paginated list response.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ListObject<T> {
    /// Items returned on this page.
    pub data: Vec<T>,

    /// Total number of matching items when the backend can report it.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_size: Option<u64>,

    /// Opaque token to request the next page.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_page_token: Option<String>,
}

/// Errors returned by [`http_rpc`], [`canister_rpc`], and [`cbor_rpc`].
#[derive(Debug, thiserror::Error)]
pub enum HttpRPCError {
    /// The request body could not be encoded or the HTTP request failed.
    #[error("http_rpc({endpoint:?}, {path:?}): send error: {error}")]
    RequestError {
        /// Remote endpoint URL used for the RPC request.
        endpoint: String,
        /// RPC path, method, or canister identifier associated with the request.
        path: String,
        /// Underlying request or encoding error.
        error: String,
    },

    /// The remote endpoint returned a non-success status code.
    #[error("http_rpc({endpoint:?}, {path:?}): response status {status}, error: {error}")]
    ResponseError {
        /// Remote endpoint URL used for the RPC request.
        endpoint: String,
        /// RPC path, method, or canister identifier associated with the response.
        path: String,
        /// HTTP status code returned by the remote endpoint.
        status: u16,
        /// Response body or status parsing error.
        error: String,
    },

    /// The response payload could not be decoded into the expected type.
    #[error("http_rpc({endpoint:?}, {path:?}): parse result error: {error}")]
    ResultError {
        /// Remote endpoint URL used for the RPC request.
        endpoint: String,
        /// RPC path, method, or canister identifier associated with the result.
        path: String,
        /// Underlying payload decoding error.
        error: String,
    },
}

/// Calls a remote CBOR RPC method and decodes its CBOR response payload.
///
/// # Arguments
/// * `client` - HTTP client to use for the request.
/// * `endpoint` - URL endpoint to send the request to.
/// * `method` - RPC method name to call.
/// * `args` - Arguments to serialize as CBOR and send with the request.
///
/// # Returns
/// Result with either the deserialized response or an [`HttpRPCError`].
pub async fn http_rpc<T>(
    client: &Client,
    endpoint: &str,
    method: &str,
    args: &impl Serialize,
) -> Result<T, HttpRPCError>
where
    T: DeserializeOwned,
{
    let args = to_canonical_vec(args).map_err(|e| HttpRPCError::RequestError {
        endpoint: endpoint.to_string(),
        path: method.to_string(),
        error: format!("{e:?}"),
    })?;
    let req = RPCRequestRef {
        method,
        params: &args.into(),
    };
    let req = to_canonical_vec(&req).map_err(|e| HttpRPCError::RequestError {
        endpoint: endpoint.to_string(),
        path: method.to_string(),
        error: format!("{e:?}"),
    })?;

    let res = cbor_rpc(client, endpoint, method, None, req).await?;
    from_slice(&res[..]).map_err(|e| HttpRPCError::ResultError {
        endpoint: endpoint.to_string(),
        path: method.to_string(),
        error: format!("{e:?}"),
    })
}

/// Calls a canister method through a remote endpoint using Candid-encoded arguments.
///
/// # Arguments
/// * `client` - HTTP client to use for the request.
/// * `endpoint` - URL endpoint to send the request to.
/// * `canister` - Target canister's principal ID.
/// * `method` - Method name to call on the canister.
/// * `args` - Arguments to encode using Candid.
///
/// # Returns
/// Result with either the deserialized response or an [`HttpRPCError`].
pub async fn canister_rpc<In, Out>(
    client: &Client,
    endpoint: &str,
    canister: &Principal,
    method: &str,
    args: In,
) -> Result<Out, HttpRPCError>
where
    In: ArgumentEncoder,
    Out: CandidType + for<'a> candid::Deserialize<'a>,
{
    let args = encode_args(args).map_err(|e| HttpRPCError::RequestError {
        endpoint: format!("{endpoint}/{canister}"),
        path: method.to_string(),
        error: format!("{e:?}"),
    })?;
    let req = to_canonical_vec(&CanisterRequestRef {
        canister,
        method,
        params: &ByteBufB64::from(args),
    })
    .map_err(|e| HttpRPCError::RequestError {
        endpoint: endpoint.to_string(),
        path: method.to_string(),
        error: format!("{e:?}"),
    })?;
    let res = cbor_rpc(client, endpoint, canister, None, req).await?;
    let res: (Out,) = decode_args(&res).map_err(|e| HttpRPCError::ResultError {
        endpoint: format!("{endpoint}/{canister}"),
        path: method.to_string(),
        error: format!("{e:?}"),
    })?;
    Ok(res.0)
}

/// Sends a raw CBOR RPC request and returns the remote payload.
///
/// # Arguments
/// * `client` - HTTP client to use for the request.
/// * `endpoint` - URL endpoint to send the request to.
/// * `path` - Path or identifier for the request.
/// * `headers` - Optional headers to include in the request.
/// * `body` - CBOR-encoded request body.
///
/// # Returns
/// Result with either the raw ByteBuf response or an [`HttpRPCError`].
pub async fn cbor_rpc(
    client: &Client,
    endpoint: &str,
    path: impl Display,
    headers: Option<http::HeaderMap>,
    body: Vec<u8>,
) -> Result<ByteBufB64, HttpRPCError> {
    let mut headers = headers.unwrap_or_default();
    let ct: http::HeaderValue = http::HeaderValue::from_static(CONTENT_TYPE_CBOR);
    headers.insert(header::CONTENT_TYPE, ct.clone());
    headers.insert(header::ACCEPT, ct);
    let res = client
        .post(endpoint)
        .headers(headers)
        .body(body)
        .send()
        .await
        .map_err(|e| HttpRPCError::RequestError {
            endpoint: endpoint.to_string(),
            path: path.to_string(),
            error: format!("{e:?}"),
        })?;
    let status = res.status().as_u16();
    if status != 200 {
        return Err(HttpRPCError::ResponseError {
            endpoint: endpoint.to_string(),
            path: path.to_string(),
            status,
            error: res.text().await.unwrap_or_default(),
        });
    }

    let data = res.bytes().await.map_err(|e| HttpRPCError::ResultError {
        endpoint: endpoint.to_string(),
        path: path.to_string(),
        error: format!("{e:?}"),
    })?;
    let res: RPCResponse = from_slice(&data[..]).map_err(|e| HttpRPCError::ResultError {
        endpoint: endpoint.to_string(),
        path: path.to_string(),
        error: format!("{e:?}"),
    })?;
    res.map_err(|e| HttpRPCError::ResultError {
        endpoint: endpoint.to_string(),
        path: path.to_string(),
        error: format!("{e:?}"),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{Router, body::Bytes, extract::State, response::IntoResponse, routing::post};
    use http::{HeaderMap, StatusCode};
    use std::sync::{Arc, Mutex};
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    #[derive(Clone)]
    struct ResponseSpec {
        status: StatusCode,
        body: Vec<u8>,
    }

    #[derive(Clone, Debug)]
    struct RecordedRequest {
        headers: HeaderMap,
        body: Vec<u8>,
    }

    type SharedState = Arc<Mutex<(ResponseSpec, Option<RecordedRequest>)>>;

    struct FailingSerialize;

    impl Serialize for FailingSerialize {
        fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
        where
            S: serde::Serializer,
        {
            Err(serde::ser::Error::custom("serialize failed"))
        }
    }

    struct FailingArgs;

    impl ArgumentEncoder for FailingArgs {
        fn encode(self, _ser: &mut candid::ser::IDLBuilder) -> candid::Result<()> {
            Err(candid::Error::msg("encode failed"))
        }

        fn encode_ref(&self, _ser: &mut candid::ser::IDLBuilder) -> candid::Result<()> {
            Err(candid::Error::msg("encode failed"))
        }
    }

    async fn handler(
        State(state): State<SharedState>,
        headers: HeaderMap,
        body: Bytes,
    ) -> impl IntoResponse {
        let mut state = state.lock().unwrap();
        state.1 = Some(RecordedRequest {
            headers,
            body: body.to_vec(),
        });
        (state.0.status, state.0.body.clone())
    }

    async fn spawn_server(status: StatusCode, body: Vec<u8>) -> (String, SharedState) {
        let state = Arc::new(Mutex::new((
            ResponseSpec { status, body },
            None::<RecordedRequest>,
        )));
        let app = Router::new()
            .route("/", post(handler))
            .with_state(state.clone());
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(std::future::IntoFuture::into_future(axum::serve(
            listener, app,
        )));
        (format!("http://{addr}"), state)
    }

    async fn spawn_truncated_body_server() -> String {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            let (mut stream, _) = listener.accept().await.unwrap();
            let mut req = [0_u8; 1024];
            let _ = stream.read(&mut req).await;
            stream
                .write_all(
                    b"HTTP/1.1 200 OK\r\ncontent-type: application/cbor\r\ncontent-length: 64\r\n\r\npartial",
                )
                .await
                .unwrap();
            stream.shutdown().await.unwrap();
        });
        format!("http://{addr}")
    }

    fn rpc_response(result: RPCResponse) -> Vec<u8> {
        to_canonical_vec(&result).unwrap()
    }

    fn rpc_success<T: Serialize>(value: &T) -> Vec<u8> {
        let payload = to_canonical_vec(value).unwrap();
        rpc_response(Ok(ByteBufB64::from(payload)))
    }

    fn client() -> Client {
        Client::builder().no_proxy().build().unwrap()
    }

    fn recorded(state: &SharedState) -> RecordedRequest {
        state.lock().unwrap().1.clone().unwrap()
    }

    #[tokio::test]
    async fn http_rpc_sends_cbor_request_and_decodes_response() {
        let (endpoint, state) =
            spawn_server(StatusCode::OK, rpc_success(&"pong".to_string())).await;
        let output: String = http_rpc(&client(), &endpoint, "ping", &("arg", 7_u8))
            .await
            .unwrap();
        assert_eq!(output, "pong");

        let req = recorded(&state);
        assert_eq!(
            req.headers.get(header::CONTENT_TYPE).unwrap(),
            CONTENT_TYPE_CBOR
        );
        assert_eq!(req.headers.get(header::ACCEPT).unwrap(), CONTENT_TYPE_CBOR);
        let decoded: RPCRequest = from_slice(&req.body[..]).unwrap();
        assert_eq!(decoded.method, "ping");
        let args: (String, u8) = from_slice(&decoded.params.0[..]).unwrap();
        assert_eq!(args, ("arg".to_string(), 7));
    }

    #[tokio::test]
    async fn canister_rpc_sends_cbor_wrapped_candid_and_decodes_response() {
        let encoded = encode_args(("hello".to_string(),)).unwrap();
        let (endpoint, state) =
            spawn_server(StatusCode::OK, rpc_response(Ok(ByteBufB64::from(encoded)))).await;
        let canister = Principal::anonymous();

        let output: String = canister_rpc(&client(), &endpoint, &canister, "greet", ("anda",))
            .await
            .unwrap();
        assert_eq!(output, "hello");

        let req = recorded(&state);
        let value: cbor2::Value = from_slice(&req.body[..]).unwrap();
        let text = format!("{value:?}");
        assert!(text.contains("greet"));
    }

    #[tokio::test]
    async fn cbor_rpc_reports_http_remote_and_decode_errors() {
        let (endpoint, _) = spawn_server(StatusCode::BAD_REQUEST, b"bad request".to_vec()).await;
        let err = cbor_rpc(&client(), &endpoint, "path", None, Vec::new())
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            HttpRPCError::ResponseError {
                status: 400,
                error,
                ..
            } if error == "bad request"
        ));

        let (endpoint, _) = spawn_server(
            StatusCode::OK,
            rpc_response(Err("remote failed".to_string())),
        )
        .await;
        let err = cbor_rpc(&client(), &endpoint, "path", None, Vec::new())
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            HttpRPCError::ResultError { error, .. } if error.contains("remote failed")
        ));

        let (endpoint, _) = spawn_server(StatusCode::OK, b"not cbor".to_vec()).await;
        let err = cbor_rpc(&client(), &endpoint, "path", None, Vec::new())
            .await
            .unwrap_err();
        assert!(matches!(err, HttpRPCError::ResultError { .. }));
    }

    #[tokio::test]
    async fn http_and_canister_rpc_report_payload_decode_errors() {
        let (endpoint, _) = spawn_server(StatusCode::OK, rpc_success(&"not a number")).await;
        let err = http_rpc::<u64>(&client(), &endpoint, "number", &()).await;
        assert!(matches!(err, Err(HttpRPCError::ResultError { .. })));

        let encoded = encode_args(("not a number".to_string(),)).unwrap();
        let (endpoint, _) =
            spawn_server(StatusCode::OK, rpc_response(Ok(ByteBufB64::from(encoded)))).await;
        let err =
            canister_rpc::<_, u64>(&client(), &endpoint, &Principal::anonymous(), "number", ())
                .await;
        assert!(matches!(err, Err(HttpRPCError::ResultError { .. })));
    }

    #[tokio::test]
    async fn request_encoding_errors_are_reported_before_sending() {
        let err = http_rpc::<String>(
            &client(),
            "http://127.0.0.1:1",
            "serialize",
            &FailingSerialize,
        )
        .await
        .unwrap_err();
        assert!(matches!(
            err,
            HttpRPCError::RequestError {
                path,
                error,
                ..
            } if path == "serialize" && error.contains("serialize failed")
        ));

        let err = canister_rpc::<_, String>(
            &client(),
            "http://127.0.0.1:1",
            &Principal::anonymous(),
            "encode",
            FailingArgs,
        )
        .await
        .unwrap_err();
        assert!(matches!(
            err,
            HttpRPCError::RequestError {
                path,
                error,
                ..
            } if path == "encode" && error.contains("encode failed")
        ));
    }

    #[tokio::test]
    async fn cbor_rpc_reports_body_read_errors() {
        let endpoint = spawn_truncated_body_server().await;
        let err = cbor_rpc(&client(), &endpoint, "body", None, Vec::new())
            .await
            .unwrap_err();
        assert!(matches!(
            err,
            HttpRPCError::ResultError { ref path, .. } if path == "body"
        ));
    }

    #[tokio::test]
    async fn cbor_rpc_reports_send_errors() {
        let err = cbor_rpc(
            &client(),
            "http://127.0.0.1:1",
            "unreachable",
            None,
            Vec::new(),
        )
        .await
        .unwrap_err();
        assert!(matches!(err, HttpRPCError::RequestError { .. }));
        assert!(err.to_string().contains("unreachable"));
    }
}