periplon 0.2.0

Rust SDK for building multi-agent AI workflows and automation
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
// Organization and Team handlers

#[cfg(feature = "server")]
use axum::{
    extract::{Extension, Path, Query},
    http::StatusCode,
    response::IntoResponse,
    Json,
};
#[cfg(feature = "server")]
use chrono::Utc;
#[cfg(feature = "server")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "server")]
use serde_json::json;
#[cfg(feature = "server")]
use std::sync::Arc;
#[cfg(feature = "server")]
use uuid::Uuid;

#[cfg(feature = "server")]
use crate::server::auth::jwt::Claims;
#[cfg(feature = "server")]
use crate::server::{
    storage::{Organization, OrganizationFilter, Team, TeamFilter, TeamMember},
    Storage,
};

// Organization Request/Response types
#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct ListOrganizationsQuery {
    pub name: Option<String>,
    pub slug: Option<String>,
    pub is_active: Option<bool>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

#[cfg(feature = "server")]
#[derive(Debug, Serialize)]
pub struct OrganizationResponse {
    pub id: Uuid,
    pub name: String,
    pub slug: String,
    pub description: Option<String>,
    pub logo_url: Option<String>,
    pub plan: String,
    pub settings: serde_json::Value,
    pub created_at: String,
    pub updated_at: String,
    pub is_active: bool,
}

#[cfg(feature = "server")]
impl From<Organization> for OrganizationResponse {
    fn from(org: Organization) -> Self {
        Self {
            id: org.id,
            name: org.name,
            slug: org.slug,
            description: org.description,
            logo_url: org.logo_url,
            plan: org.plan,
            settings: org.settings,
            created_at: org.created_at.to_rfc3339(),
            updated_at: org.updated_at.to_rfc3339(),
            is_active: org.is_active,
        }
    }
}

#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct CreateOrganizationRequest {
    pub name: String,
    pub slug: String,
    pub description: Option<String>,
    pub logo_url: Option<String>,
}

#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct UpdateOrganizationRequest {
    pub name: Option<String>,
    pub description: Option<String>,
    pub logo_url: Option<String>,
    pub plan: Option<String>,
    pub is_active: Option<bool>,
}

// Team Request/Response types
#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct ListTeamsQuery {
    pub organization_id: Option<Uuid>,
    pub name: Option<String>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

#[cfg(feature = "server")]
#[derive(Debug, Serialize)]
pub struct TeamResponse {
    pub id: Uuid,
    pub organization_id: Uuid,
    pub name: String,
    pub description: Option<String>,
    pub created_at: String,
    pub updated_at: String,
}

#[cfg(feature = "server")]
impl From<Team> for TeamResponse {
    fn from(team: Team) -> Self {
        Self {
            id: team.id,
            organization_id: team.organization_id,
            name: team.name,
            description: team.description,
            created_at: team.created_at.to_rfc3339(),
            updated_at: team.updated_at.to_rfc3339(),
        }
    }
}

#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct CreateTeamRequest {
    pub organization_id: Uuid,
    pub name: String,
    pub description: Option<String>,
}

#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct UpdateTeamRequest {
    pub name: Option<String>,
    pub description: Option<String>,
}

#[cfg(feature = "server")]
#[derive(Debug, Deserialize)]
pub struct AddTeamMemberRequest {
    pub user_id: Uuid,
    pub role: Option<String>,
}

// Organization Handlers
#[cfg(feature = "server")]
pub async fn list_organizations(
    Query(query): Query<ListOrganizationsQuery>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    let filter = OrganizationFilter {
        name: query.name,
        slug: query.slug,
        is_active: query.is_active,
        limit: query.limit,
        offset: query.offset,
    };

    match storage.list_organizations(&filter).await {
        Ok(orgs) => {
            let responses: Vec<OrganizationResponse> = orgs.into_iter().map(Into::into).collect();
            (
                StatusCode::OK,
                Json(json!({
                    "organizations": responses,
                    "total": responses.len(),
                })),
            )
        }
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to list organizations", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn create_organization(
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
    Json(payload): Json<CreateOrganizationRequest>,
) -> impl IntoResponse {
    let organization = Organization {
        id: Uuid::new_v4(),
        name: payload.name,
        slug: payload.slug,
        description: payload.description,
        logo_url: payload.logo_url,
        plan: "free".to_string(),
        settings: json!({}),
        created_at: Utc::now(),
        updated_at: Utc::now(),
        is_active: true,
    };

    match storage.store_organization(&organization).await {
        Ok(id) => (
            StatusCode::CREATED,
            Json(json!({"id": id, "message": "Organization created"})),
        ),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to create organization", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn get_organization(
    Path(id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    match storage.get_organization(id).await {
        Ok(Some(org)) => (StatusCode::OK, Json(json!(OrganizationResponse::from(org)))),
        Ok(None) => (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "Organization not found"})),
        ),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to get organization", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn update_organization(
    Path(id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
    Json(payload): Json<UpdateOrganizationRequest>,
) -> impl IntoResponse {
    let mut org = match storage.get_organization(id).await {
        Ok(Some(org)) => org,
        Ok(None) => {
            return (
                StatusCode::NOT_FOUND,
                Json(json!({"error": "Organization not found"})),
            )
        }
        Err(e) => {
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": "Failed to get organization", "message": e.to_string()})),
            )
        }
    };

    if let Some(name) = payload.name {
        org.name = name;
    }
    if let Some(desc) = payload.description {
        org.description = Some(desc);
    }
    if let Some(logo) = payload.logo_url {
        org.logo_url = Some(logo);
    }
    if let Some(plan) = payload.plan {
        org.plan = plan;
    }
    if let Some(active) = payload.is_active {
        org.is_active = active;
    }
    org.updated_at = Utc::now();

    match storage.update_organization(id, &org).await {
        Ok(_) => (
            StatusCode::OK,
            Json(json!({"message": "Organization updated"})),
        ),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to update organization", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn delete_organization(
    Path(id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    match storage.delete_organization(id).await {
        Ok(_) => (StatusCode::NO_CONTENT, Json(json!({}))),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to delete organization", "message": e.to_string()})),
        ),
    }
}

// Team Handlers
#[cfg(feature = "server")]
pub async fn list_teams(
    Query(query): Query<ListTeamsQuery>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    let filter = TeamFilter {
        organization_id: query.organization_id,
        name: query.name,
        limit: query.limit,
        offset: query.offset,
    };

    match storage.list_teams(&filter).await {
        Ok(teams) => {
            let responses: Vec<TeamResponse> = teams.into_iter().map(Into::into).collect();
            (
                StatusCode::OK,
                Json(json!({"teams": responses, "total": responses.len()})),
            )
        }
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to list teams", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn create_team(
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
    Json(payload): Json<CreateTeamRequest>,
) -> impl IntoResponse {
    let team = Team {
        id: Uuid::new_v4(),
        organization_id: payload.organization_id,
        name: payload.name,
        description: payload.description,
        created_at: Utc::now(),
        updated_at: Utc::now(),
    };

    match storage.store_team(&team).await {
        Ok(id) => (
            StatusCode::CREATED,
            Json(json!({"id": id, "message": "Team created"})),
        ),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to create team", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn get_team(
    Path(id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    match storage.get_team(id).await {
        Ok(Some(team)) => (StatusCode::OK, Json(json!(TeamResponse::from(team)))),
        Ok(None) => (
            StatusCode::NOT_FOUND,
            Json(json!({"error": "Team not found"})),
        ),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to get team", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn update_team(
    Path(id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
    Json(payload): Json<UpdateTeamRequest>,
) -> impl IntoResponse {
    let mut team = match storage.get_team(id).await {
        Ok(Some(team)) => team,
        Ok(None) => {
            return (
                StatusCode::NOT_FOUND,
                Json(json!({"error": "Team not found"})),
            )
        }
        Err(e) => {
            return (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(json!({"error": "Failed to get team", "message": e.to_string()})),
            )
        }
    };

    if let Some(name) = payload.name {
        team.name = name;
    }
    if let Some(desc) = payload.description {
        team.description = Some(desc);
    }
    team.updated_at = Utc::now();

    match storage.update_team(id, &team).await {
        Ok(_) => (StatusCode::OK, Json(json!({"message": "Team updated"}))),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to update team", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn delete_team(
    Path(id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    match storage.delete_team(id).await {
        Ok(_) => (StatusCode::NO_CONTENT, Json(json!({}))),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to delete team", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn add_team_member(
    Path(team_id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(claims): Extension<Claims>,
    Json(payload): Json<AddTeamMemberRequest>,
) -> impl IntoResponse {
    let member = TeamMember {
        id: Uuid::new_v4(),
        team_id,
        user_id: payload.user_id,
        role: payload.role.unwrap_or_else(|| "member".to_string()),
        added_at: Utc::now(),
        added_by: Some(
            Uuid::parse_str(&claims.sub)
                .ok()
                .unwrap_or_else(Uuid::new_v4),
        ),
    };

    match storage.add_team_member(&member).await {
        Ok(id) => (
            StatusCode::CREATED,
            Json(json!({"id": id, "message": "Member added"})),
        ),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to add member", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn remove_team_member(
    Path((team_id, user_id)): Path<(Uuid, Uuid)>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    match storage.remove_team_member(team_id, user_id).await {
        Ok(_) => (StatusCode::NO_CONTENT, Json(json!({}))),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to remove member", "message": e.to_string()})),
        ),
    }
}

#[cfg(feature = "server")]
pub async fn get_team_members(
    Path(team_id): Path<Uuid>,
    Extension(storage): Extension<Arc<dyn Storage>>,
    Extension(_claims): Extension<Claims>,
) -> impl IntoResponse {
    match storage.get_team_members(team_id).await {
        Ok(members) => (StatusCode::OK, Json(json!({"members": members}))),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(json!({"error": "Failed to get members", "message": e.to_string()})),
        ),
    }
}