a4-cli 0.46.0

CLI tool for generating TypeScript SDKs from Arete stream specifications
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
//! Self-serve protocol endpoints: protocols, releases, resources, fee
//! policies, deployments, webhook endpoints and registry listings.
//!
//! Every route needs a secret key, except the public listing search and
//! lookup (`/api/registry/protocols`). Response types are lenient (unknown
//! fields are ignored) so a newer server stays compatible with this CLI.

use anyhow::{Context, Result};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use super::ApiClient;

/// A protocol the caller owns.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolRecord {
    pub id: String,
    pub slug: String,
    pub name: String,
    #[serde(default)]
    pub created_at: Option<String>,
}

/// The result of uploading a spec (and handler bundle).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolReleaseUpload {
    #[serde(default)]
    pub schema: Option<String>,
    pub protocol_id: String,
    pub release_hash: String,
    #[serde(default)]
    pub spec_hash: Option<String>,
    #[serde(default)]
    pub stack_source_hash: Option<String>,
    #[serde(default)]
    pub handler_bundle_hash: Option<String>,
    pub has_handlers: bool,
    /// `pending`, `passed` or `failed`.
    pub template_check: String,
    pub reviewed: bool,
    /// `false` when the identical release was already registered.
    pub created: bool,
}

/// A draft or published resource version.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolResourceVersion {
    #[serde(default)]
    pub id: Option<String>,
    pub name: String,
    pub version: i64,
    pub status: String,
    #[serde(default)]
    pub resource_hash: Option<String>,
}

/// A stored fee policy.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolFeePolicy {
    pub id: String,
    #[serde(default)]
    pub version: Option<i64>,
    #[serde(default)]
    pub document_sha256: Option<String>,
}

/// A protocol deployment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolDeployment {
    pub id: String,
    #[serde(default)]
    pub protocol_id: Option<String>,
    /// Cloud dev environment binding; absent for production deployments.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub environment_id: Option<String>,
    pub short_id: String,
    /// `disabled`, `activating` or `active`.
    pub status: String,
    #[serde(default)]
    pub revision: Option<i64>,
    pub release_hash: String,
    pub access_mode: String,
    #[serde(default)]
    pub attestation_key_id: Option<String>,
    /// `reviewed` or `unreviewed`.
    #[serde(default)]
    pub trust_tier: Option<String>,
    #[serde(default)]
    pub runtime_kind: Option<String>,
}

/// The result of activating a deployment.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolDeploymentTransition {
    pub deployment: ProtocolDeployment,
    /// What activation still waits for, when it has not finished.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pending: Option<Value>,
    /// The owner-private registry package the activation published (or
    /// found, or skipped), when the server reports one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub registry_package: Option<Value>,
}

/// A deployment's registry listing.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolListingStatus {
    pub deployment_id: String,
    /// `requested`, `listed`, `rejected` or `delisted`.
    pub status: String,
    /// `reviewed` or `unreviewed`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub trust_tier: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub updated_at: Option<String>,
}

/// A webhook endpoint subscribed to a deployment's emitted events.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolWebhook {
    pub id: String,
    #[serde(default)]
    pub deployment_id: Option<String>,
    pub url: String,
    /// `publisher` or `integrator`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub scope: Option<Value>,
    #[serde(default)]
    pub events: Vec<String>,
    pub status: String,
    /// The end of the signing secret, to tell endpoints apart.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub secret_hint: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub created_at: Option<String>,
    /// The signing secret: returned once, when the endpoint is created.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub secret: Option<String>,
}

/// The result of sending a test delivery to a webhook endpoint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProtocolWebhookTest {
    pub delivery_id: String,
    pub delivered: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub response_code: Option<u16>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Largest page of `GET /api/registry/protocols`.
pub const MAX_PROTOCOL_LISTING_LIMIT: usize = 50;

/// Readable guidance for the listing refusal and lookup codes.
pub fn protocol_listing_message(code: &str, details: Option<&Value>) -> Option<String> {
    Some(match code {
        "protocol-listing-not-public" => "Only public deployments can be listed, and this one is private. Self-serve deployments are private: ask Arete to make the deployment public, then request the listing again".to_string(),
        "protocol-listing-edges-private" => {
            let edges: Vec<String> = details
                .and_then(|details| details["edges"].as_array())
                .into_iter()
                .flatten()
                .map(|edge| match edge {
                    Value::String(edge) => edge.clone(),
                    other => other.to_string(),
                })
                .collect();
            format!(
                "A listed protocol may only use public stacks and programs, and this one uses private ones{}. Publish them (or switch to public packages), deploy again and request the listing for the new deployment",
                if edges.is_empty() {
                    String::new()
                } else {
                    format!(": {}", edges.join(", "))
                }
            )
        }
        "protocol-listing-fee-policy" => "The deployment's fee policy does not meet the listing rules (the minimum platform fee or the fee destination). Deploy with a fee policy that meets them, then request the listing for the new deployment".to_string(),
        "protocol-listing-not-owner" => "This deployment belongs to another account. Use a secret key of the account that deployed it".to_string(),
        "protocol-listing-account-standing" => "This account cannot list protocols right now because of its standing. Contact Arete support".to_string(),
        "protocol-listing-not-found" => "No listed protocol has that slug. Search the listings with `a4 protocol search`".to_string(),
        _ => return None,
    })
}

impl ApiClient {
    /// Rewrite a listing refusal's message with [`protocol_listing_message`].
    fn listing_result<T>(result: Result<T>) -> Result<T> {
        result.map_err(|error| match error.downcast::<super::ApiClientError>() {
            Ok(mut api_error) => {
                if let Some(message) = api_error.problem.code.as_deref().and_then(|code| {
                    protocol_listing_message(code, api_error.problem.extra.get("details"))
                }) {
                    api_error.problem.error = message;
                }
                api_error.into()
            }
            Err(error) => error,
        })
    }

    /// Search the listed protocol packages (`GET /api/registry/protocols`).
    /// The raw page is returned so `--json` prints what the registry sent.
    pub fn search_protocol_listings(
        &self,
        query: Option<&str>,
        cursor: Option<&str>,
        limit: Option<usize>,
    ) -> Result<Value> {
        let mut params: Vec<(&str, String)> = Vec::new();
        if let Some(query) = query.map(str::trim).filter(|query| !query.is_empty()) {
            params.push(("q", query.to_string()));
        }
        if let Some(cursor) = cursor.map(str::trim).filter(|cursor| !cursor.is_empty()) {
            params.push(("cursor", cursor.to_string()));
        }
        if let Some(limit) = limit {
            params.push((
                "limit",
                limit.clamp(1, MAX_PROTOCOL_LISTING_LIMIT).to_string(),
            ));
        }
        let response = self
            .with_optional_auth(
                self.client
                    .get(format!("{}/api/registry/protocols", self.base_url))
                    .query(&params),
            )
            .send()
            .context("Failed to send protocol listing search request")?;
        Self::handle_response(response)
    }

    /// One listed protocol package by slug.
    pub fn get_protocol_listing(&self, slug: &str) -> Result<Value> {
        let response = self
            .with_optional_auth(self.client.get(format!(
                "{}/api/registry/protocols/{}",
                self.base_url,
                super::registry_path_segment(slug)?
            )))
            .send()
            .context("Failed to send protocol listing request")?;
        Self::listing_result(Self::handle_response(response))
    }

    /// Ask for a deployment to be listed in the public registry.
    pub fn request_protocol_listing(
        &self,
        deployment_id: &str,
        note: Option<&str>,
    ) -> Result<ProtocolListingStatus> {
        let mut body = json!({});
        if let Some(note) = note.map(str::trim).filter(|note| !note.is_empty()) {
            body["note"] = Value::String(note.to_string());
        }
        Self::listing_result(self.protocol_post(
            &format!("/api/protocol-deployments/{deployment_id}/listing"),
            &body,
            "request protocol listing",
        ))
    }

    /// A deployment's listing state.
    pub fn get_protocol_listing_status(
        &self,
        deployment_id: &str,
    ) -> Result<ProtocolListingStatus> {
        Self::listing_result(self.protocol_get(
            &format!("/api/protocol-deployments/{deployment_id}/listing"),
            "read protocol listing",
        ))
    }

    /// Whether the configured key is a publishable (browser) key, which the
    /// protocol routes refuse.
    pub(crate) fn api_key_is_publishable(&self) -> bool {
        self.api_key
            .as_deref()
            .is_some_and(|key| key.starts_with("a4_pk_"))
    }

    fn protocol_get<T: DeserializeOwned>(&self, path: &str, what: &str) -> Result<T> {
        let api_key = self.require_api_key()?;
        let response = self
            .client
            .get(format!("{}{path}", self.base_url))
            .bearer_auth(api_key)
            .send()
            .with_context(|| format!("Failed to send {what} request"))?;
        Self::handle_response(response)
    }

    fn protocol_delete<T: DeserializeOwned>(&self, path: &str, what: &str) -> Result<T> {
        let api_key = self.require_api_key()?;
        let response = self
            .client
            .delete(format!("{}{path}", self.base_url))
            .bearer_auth(api_key)
            .send()
            .with_context(|| format!("Failed to send {what} request"))?;
        Self::handle_response(response)
    }

    fn protocol_post<T: DeserializeOwned>(
        &self,
        path: &str,
        body: &Value,
        what: &str,
    ) -> Result<T> {
        let api_key = self.require_api_key()?;
        let response = self
            .client
            .post(format!("{}{path}", self.base_url))
            .bearer_auth(api_key)
            .json(body)
            .send()
            .with_context(|| format!("Failed to send {what} request"))?;
        Self::handle_response(response)
    }

    pub fn list_protocols(&self) -> Result<Vec<ProtocolRecord>> {
        self.protocol_get("/api/protocols", "list protocols")
    }

    pub fn create_protocol(&self, slug: &str, name: &str) -> Result<ProtocolRecord> {
        self.protocol_post(
            "/api/protocols",
            &json!({ "slug": slug, "name": name }),
            "create protocol",
        )
    }

    /// Upload a spec and, exactly when a step has a handler, its bundle as
    /// padded standard base64.
    pub fn upload_protocol_release(
        &self,
        protocol_id: &str,
        spec: &Value,
        bundle_base64: Option<&str>,
    ) -> Result<ProtocolReleaseUpload> {
        let mut body = json!({ "spec": spec });
        if let Some(bundle) = bundle_base64 {
            body["bundleBase64"] = Value::String(bundle.to_string());
        }
        self.protocol_post(
            &format!("/api/protocols/{protocol_id}/releases"),
            &body,
            "upload protocol release",
        )
    }

    pub fn create_protocol_resource_version(
        &self,
        protocol_id: &str,
        name: &str,
        value: &Value,
        release_hash: &str,
    ) -> Result<ProtocolResourceVersion> {
        self.protocol_post(
            &format!("/api/protocols/{protocol_id}/resources/{name}/versions"),
            &json!({ "value": value, "releaseHash": release_hash }),
            "create protocol resource version",
        )
    }

    pub fn publish_protocol_resource_version(
        &self,
        protocol_id: &str,
        name: &str,
        version: i64,
        release_hash: &str,
    ) -> Result<ProtocolResourceVersion> {
        self.protocol_post(
            &format!("/api/protocols/{protocol_id}/resources/{name}/versions/{version}/publish"),
            &json!({ "releaseHash": release_hash }),
            "publish protocol resource version",
        )
    }

    pub fn create_protocol_fee_policy(
        &self,
        protocol_id: &str,
        document: &Value,
    ) -> Result<ProtocolFeePolicy> {
        self.protocol_post(
            &format!("/api/protocols/{protocol_id}/fee-policies"),
            &json!({ "document": document }),
            "create protocol fee policy",
        )
    }

    pub fn list_protocol_deployments(&self, protocol_id: &str) -> Result<Vec<ProtocolDeployment>> {
        self.protocol_get(
            &format!("/api/protocols/{protocol_id}/deployments"),
            "list protocol deployments",
        )
    }

    /// Create a private deployment. The server derives the attestation key.
    pub fn create_protocol_deployment(
        &self,
        protocol_id: &str,
        release_hash: &str,
        resources: &std::collections::BTreeMap<String, i64>,
        fee_policy_id: &str,
    ) -> Result<ProtocolDeployment> {
        self.protocol_post(
            &format!("/api/protocols/{protocol_id}/deployments"),
            &json!({
                "releaseHash": release_hash,
                "resources": resources,
                "feePolicyId": fee_policy_id,
                "accessMode": "private",
            }),
            "create protocol deployment",
        )
    }

    pub fn activate_protocol_deployment(
        &self,
        deployment_id: &str,
    ) -> Result<ProtocolDeploymentTransition> {
        self.protocol_post(
            &format!("/api/protocol-deployments/{deployment_id}/activate"),
            &json!({}),
            "activate protocol deployment",
        )
    }

    /// Subscribe `url` to `events` of a deployment. The response carries the
    /// signing secret, which the server never returns again.
    pub fn create_protocol_webhook(
        &self,
        deployment_id: &str,
        url: &str,
        events: &[String],
    ) -> Result<ProtocolWebhook> {
        self.protocol_post(
            &format!("/api/protocol-deployments/{deployment_id}/webhooks"),
            &json!({ "url": url, "events": events }),
            "create protocol webhook",
        )
    }

    /// The caller's webhook endpoints on a deployment.
    pub fn list_protocol_webhooks(&self, deployment_id: &str) -> Result<Vec<ProtocolWebhook>> {
        self.protocol_get(
            &format!("/api/protocol-deployments/{deployment_id}/webhooks"),
            "list protocol webhooks",
        )
    }

    /// Disable a webhook endpoint: it receives no further deliveries.
    pub fn disable_protocol_webhook(
        &self,
        deployment_id: &str,
        webhook_id: &str,
    ) -> Result<ProtocolWebhook> {
        self.protocol_delete(
            &format!("/api/protocol-deployments/{deployment_id}/webhooks/{webhook_id}"),
            "disable protocol webhook",
        )
    }

    /// Send a signed test delivery to a webhook endpoint now.
    pub fn test_protocol_webhook(
        &self,
        deployment_id: &str,
        webhook_id: &str,
    ) -> Result<ProtocolWebhookTest> {
        self.protocol_post(
            &format!("/api/protocol-deployments/{deployment_id}/webhooks/{webhook_id}/test"),
            &json!({}),
            "test protocol webhook",
        )
    }
}