floopfloop 0.1.0-alpha.2

Official Rust SDK for the FloopFloop API (https://www.floopfloop.com)
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
use crate::error::{FloopError, FloopErrorCode};
use crate::Client;
use serde::{Deserialize, Serialize};
use std::time::{Duration, Instant};

#[derive(Debug, Clone, Deserialize)]
pub struct Project {
    pub id: String,
    pub name: String,
    pub subdomain: Option<String>,
    pub status: String,
    #[serde(rename = "botType")]
    pub bot_type: Option<String>,
    pub url: Option<String>,
    #[serde(rename = "amplifyAppUrl")]
    pub amplify_app_url: Option<String>,
    #[serde(rename = "isPublic")]
    pub is_public: bool,
    #[serde(rename = "isAuthProtected")]
    pub is_auth_protected: bool,
    #[serde(rename = "teamId")]
    pub team_id: Option<String>,
    #[serde(rename = "createdAt")]
    pub created_at: String,
    #[serde(rename = "updatedAt")]
    pub updated_at: String,
    #[serde(rename = "thumbnailUrl")]
    pub thumbnail_url: Option<String>,
}

#[derive(Debug, Default, Clone, Serialize)]
pub struct CreateProjectInput {
    pub prompt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subdomain: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "botType")]
    pub bot_type: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "isAuthProtected")]
    pub is_auth_protected: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "teamId")]
    pub team_id: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct Deployment {
    pub id: String,
    pub status: String,
    pub version: i64,
}

#[derive(Debug, Deserialize)]
pub struct CreatedProject {
    pub project: Project,
    pub deployment: Deployment,
}

#[derive(Debug, Default, Clone)]
pub struct ListProjectsOptions {
    pub team_id: Option<String>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct StatusEvent {
    pub step: i64,
    #[serde(rename = "totalSteps")]
    pub total_steps: i64,
    pub status: String,
    pub message: String,
    #[serde(default)]
    pub progress: Option<f64>,
    #[serde(default, rename = "queuePosition")]
    pub queue_position: Option<i64>,
}

#[derive(Debug, Default, Clone, Serialize)]
pub struct RefineInput {
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub attachments: Option<Vec<RefineAttachment>>,
    #[serde(skip_serializing_if = "Option::is_none", rename = "codeEditOnly")]
    pub code_edit_only: Option<bool>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefineAttachment {
    pub key: String,
    #[serde(rename = "fileName")]
    pub file_name: String,
    #[serde(rename = "fileType")]
    pub file_type: String,
    #[serde(rename = "fileSize")]
    pub file_size: i64,
}

/// Three-shape discriminated result from `Projects::refine`. Exactly one
/// of `queued`, `saved_only`, or `processing` is `Some` — pattern-match
/// to branch.
#[derive(Debug, Default, Clone)]
pub struct RefineResult {
    pub queued: Option<RefineQueued>,
    pub saved_only: Option<RefineSavedOnly>,
    pub processing: Option<RefineProcessing>,
}

#[derive(Debug, Clone)]
pub struct RefineQueued {
    pub message_id: String,
}

#[derive(Debug, Clone)]
pub struct RefineSavedOnly;

#[derive(Debug, Clone)]
pub struct RefineProcessing {
    pub deployment_id: String,
    pub queue_priority: i64,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ConversationMessage {
    pub id: String,
    #[serde(rename = "projectId")]
    pub project_id: String,
    pub role: String,
    pub content: String,
    pub metadata: Option<serde_json::Value>,
    pub status: String,
    pub position: Option<i64>,
    #[serde(rename = "createdAt")]
    pub created_at: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct ConversationsResult {
    pub messages: Vec<ConversationMessage>,
    pub queued: Vec<ConversationMessage>,
    #[serde(rename = "latestVersion")]
    pub latest_version: i64,
}

#[derive(Debug, Default, Clone)]
pub struct ConversationsOptions {
    /// 0 means "server default".
    pub limit: u32,
}

/// Configures `Projects::stream` and `Projects::wait_for_live`.
#[derive(Debug, Clone, Copy)]
pub struct StreamOptions {
    pub interval: Duration,
    pub max_wait: Duration,
}

impl Default for StreamOptions {
    fn default() -> Self {
        Self {
            interval: Duration::from_secs(2),
            max_wait: Duration::from_secs(600),
        }
    }
}

/// Alias kept for parity with the Go SDK's `WaitForLiveOptions` name.
pub type WaitForLiveOptions = StreamOptions;

/// Callback invoked on every de-duplicated status snapshot — including
/// the terminal event. Return `Ok(())` to continue polling, or any
/// `Err` to stop early (the error propagates from `stream`).
pub type StreamHandler<E> = Box<dyn FnMut(&StatusEvent) -> Result<(), E> + Send>;

pub struct Projects<'c> {
    pub(crate) client: &'c Client,
}

impl<'c> Projects<'c> {
    pub async fn create(&self, input: CreateProjectInput) -> Result<CreatedProject, FloopError> {
        let body = serde_json::to_value(&input).unwrap();
        self.client
            .request_json(reqwest::Method::POST, "/api/v1/projects", Some(&body))
            .await
    }

    pub async fn list(&self, opts: ListProjectsOptions) -> Result<Vec<Project>, FloopError> {
        let path = match opts.team_id {
            Some(t) => format!("/api/v1/projects?teamId={}", urlencoding_encode(&t)),
            None => "/api/v1/projects".to_owned(),
        };
        self.client
            .request_json(reqwest::Method::GET, &path, None)
            .await
    }

    /// Fetch a single project by id or subdomain. No dedicated backend
    /// route — filters the full list locally, matching the other SDKs.
    pub async fn get(
        &self,
        reference: &str,
        opts: ListProjectsOptions,
    ) -> Result<Project, FloopError> {
        let all = self.list(opts).await?;
        all.into_iter()
            .find(|p| p.id == reference || p.subdomain.as_deref() == Some(reference))
            .ok_or_else(|| {
                FloopError::new(
                    FloopErrorCode::NotFound,
                    404,
                    format!("project not found: {reference}"),
                )
            })
    }

    pub async fn status(&self, reference: &str) -> Result<StatusEvent, FloopError> {
        let path = format!("/api/v1/projects/{}/status", urlencoding_encode(reference));
        self.client
            .request_json(reqwest::Method::GET, &path, None)
            .await
    }

    pub async fn cancel(&self, reference: &str) -> Result<(), FloopError> {
        let path = format!("/api/v1/projects/{}/cancel", urlencoding_encode(reference));
        self.client
            .request_empty(reqwest::Method::POST, &path, None)
            .await
    }

    pub async fn reactivate(&self, reference: &str) -> Result<(), FloopError> {
        let path = format!(
            "/api/v1/projects/{}/reactivate",
            urlencoding_encode(reference)
        );
        self.client
            .request_empty(reqwest::Method::POST, &path, None)
            .await
    }

    pub async fn refine(
        &self,
        reference: &str,
        input: RefineInput,
    ) -> Result<RefineResult, FloopError> {
        let path = format!("/api/v1/projects/{}/refine", urlencoding_encode(reference));
        let body = serde_json::to_value(&input).unwrap();
        let raw: serde_json::Value = self
            .client
            .request_json(reqwest::Method::POST, &path, Some(&body))
            .await?;
        let mut out = RefineResult::default();
        if let Some(q) = raw.get("queued").and_then(|v| v.as_bool()) {
            if q {
                let msg_id = raw
                    .get("messageId")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
                    .to_owned();
                out.queued = Some(RefineQueued { message_id: msg_id });
            } else {
                out.saved_only = Some(RefineSavedOnly);
            }
            return Ok(out);
        }
        if let Some(p) = raw.get("processing").and_then(|v| v.as_bool()) {
            if p {
                let dep_id = raw
                    .get("deploymentId")
                    .and_then(|v| v.as_str())
                    .unwrap_or_default()
                    .to_owned();
                let prio = raw
                    .get("queuePriority")
                    .and_then(|v| v.as_i64())
                    .unwrap_or(0);
                out.processing = Some(RefineProcessing {
                    deployment_id: dep_id,
                    queue_priority: prio,
                });
                return Ok(out);
            }
        }
        Err(FloopError::new(
            FloopErrorCode::Unknown,
            0,
            "refine: unrecognised response shape",
        ))
    }

    pub async fn conversations(
        &self,
        reference: &str,
        opts: ConversationsOptions,
    ) -> Result<ConversationsResult, FloopError> {
        let mut path = format!(
            "/api/v1/projects/{}/conversations",
            urlencoding_encode(reference)
        );
        if opts.limit > 0 {
            path.push_str(&format!("?limit={}", opts.limit));
        }
        self.client
            .request_json(reqwest::Method::GET, &path, None)
            .await
    }

    /// Poll the project's status endpoint, invoking `handler` on each
    /// de-duplicated event, until a terminal state (live / failed /
    /// cancelled), `opts.max_wait` elapses, or the handler returns an
    /// error.
    ///
    /// Return values:
    ///   - `Ok(())` — reached `live`
    ///   - `Err(FloopError{code: BuildFailed})` — terminal failure
    ///   - `Err(FloopError{code: BuildCancelled})` — user cancelled
    ///   - `Err(FloopError{code: Timeout})` — max_wait exceeded
    ///   - `Err(...)` — the handler's error (wrapped in FloopError::new(Unknown, ...))
    ///
    /// Events are de-duplicated on `(status, step, progress, queue_position)`
    /// so callers don't see dozens of identical "queued" snapshots.
    pub async fn stream<F>(
        &self,
        reference: &str,
        opts: Option<StreamOptions>,
        mut handler: F,
    ) -> Result<(), FloopError>
    where
        F: FnMut(&StatusEvent) -> Result<(), FloopError> + Send,
    {
        let o = opts.unwrap_or_default();
        let deadline = Instant::now() + o.max_wait;
        let mut last_key = String::new();
        loop {
            if Instant::now() >= deadline {
                return Err(FloopError::new(
                    FloopErrorCode::Timeout,
                    0,
                    format!(
                        "stream: project {reference} did not reach a terminal state within {:?}",
                        o.max_wait
                    ),
                ));
            }

            let ev = self.status(reference).await?;
            let key = stream_event_key(&ev);
            if key != last_key {
                last_key = key;
                handler(&ev)?;
            }

            match ev.status.as_str() {
                "live" => return Ok(()),
                "failed" => {
                    return Err(FloopError::new(
                        FloopErrorCode::BuildFailed,
                        0,
                        non_empty_or(&ev.message, "build failed"),
                    ))
                }
                "cancelled" => {
                    return Err(FloopError::new(
                        FloopErrorCode::BuildCancelled,
                        0,
                        non_empty_or(&ev.message, "build cancelled"),
                    ))
                }
                _ => {}
            }

            let remaining = deadline.saturating_duration_since(Instant::now());
            let sleep_for = o.interval.min(remaining);
            if sleep_for.is_zero() {
                // Loop one more time to hit the deadline branch cleanly.
                continue;
            }
            tokio_sleep(sleep_for).await;
        }
    }

    /// Block until the project reaches `live` and return the hydrated
    /// `Project`. Wraps `stream` internally.
    pub async fn wait_for_live(
        &self,
        reference: &str,
        opts: Option<WaitForLiveOptions>,
    ) -> Result<Project, FloopError> {
        self.stream(reference, opts, |_| Ok(())).await?;
        self.get(reference, ListProjectsOptions::default()).await
    }
}

fn stream_event_key(ev: &StatusEvent) -> String {
    let progress = ev.progress.map_or(String::new(), |p| format!("{p}"));
    let queue = ev.queue_position.map_or(String::new(), |q| format!("{q}"));
    format!("{}|{}|{progress}|{queue}", ev.status, ev.step)
}

fn non_empty_or(s: &str, fallback: &str) -> String {
    if s.is_empty() {
        fallback.to_owned()
    } else {
        s.to_owned()
    }
}

/// Minimal URL-encoder — path and query values are the only thing we
/// encode and none of the characters our refs contain need the fancy
/// stuff.  Keeps us off the `urlencoding` dep for a 5-line helper.
pub(crate) fn urlencoding_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
                out.push(b as char);
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

// Tokio is a dev-dep; in production consumers bring their own runtime.
// We need a way to sleep inside `stream`, so wire to `tokio::time::sleep`
// through a tiny indirection that works with either full or rt-only
// tokio builds — matches how reqwest itself handles this.
async fn tokio_sleep(d: Duration) {
    // `reqwest`'s default stack pulls in tokio already — we re-use it.
    #[allow(clippy::module_name_repetitions)]
    use std::future::Future;
    use std::pin::Pin;
    use std::task::{Context, Poll};

    struct Sleep {
        until: Instant,
    }
    impl Future for Sleep {
        type Output = ();
        fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
            if Instant::now() >= self.until {
                Poll::Ready(())
            } else {
                // Crude polling loop — good enough for test doubles, and
                // downstream consumers will override via a custom HTTP
                // client anyway.  Tokio's real sleep kicks in on any
                // standard reqwest build; this fallback is only here so
                // the crate compiles without tokio on the default path.
                let waker = cx.waker().clone();
                let until = self.until;
                std::thread::spawn(move || {
                    let now = Instant::now();
                    if until > now {
                        std::thread::sleep(until - now);
                    }
                    waker.wake();
                });
                Poll::Pending
            }
        }
    }
    Sleep {
        until: Instant::now() + d,
    }
    .await;
}