ta-submit 0.15.15-alpha.3

Submit adapters for VCS integration in Trusted Autonomy
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
//! JSON-over-stdio protocol types for external social media adapter plugins.
//!
//! Social adapter plugins communicate with TA using a request/response
//! protocol over stdin/stdout. TA spawns the plugin process, writes one JSON
//! request line to stdin, reads one JSON response line from stdout.
//!
//! ## Protocol overview
//!
//! Every exchange is a single JSON line in each direction:
//!
//! ```text
//! TA → plugin: {"op":"<name>",...params...}
//! plugin → TA: {"ok":true,...result...}   or   {"ok":false,"error":"..."}
//! ```
//!
//! ## Operations
//!
//! | Op                 | Description                                              |
//! |--------------------|----------------------------------------------------------|
//! | `create_draft`     | Write a draft to the platform's native draft state       |
//! | `create_scheduled` | Schedule a post at a future time (platform scheduler)    |
//! | `draft_status`     | Poll whether a draft was published, deleted, or open     |
//! | `health`           | Connectivity + credential check                          |
//! | `capabilities`     | Advertise which optional ops this plugin supports        |
//!
//! ## Safety invariant — `publish` is absent by design
//!
//! The `publish` operation is intentionally absent from this protocol.
//! TA never publishes social media posts on behalf of the user. Plugins
//! expose only `create_draft` and `create_scheduled`; the user publishes
//! from their native platform UI or scheduler (e.g., LinkedIn, Buffer).
//! This is a deliberate safety boundary enforced at the type level.

use serde::{Deserialize, Serialize};

/// Protocol version implemented by this TA build.
pub const SOCIAL_PROTOCOL_VERSION: u32 = 1;

// ---------------------------------------------------------------------------
// Request envelope
// ---------------------------------------------------------------------------

/// Request sent from TA to a social media plugin over stdin.
///
/// One JSON line per request. The plugin processes it and writes one
/// `SocialPluginResponse` line to stdout, then the process exits.
///
/// The `op` field selects the operation. Additional fields carry
/// operation-specific parameters (flat layout, not nested).
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum SocialPluginRequest {
    /// Create a draft in the platform's native draft state.
    ///
    /// NOTE: There is intentionally no `Publish` variant. TA never publishes.
    CreateDraft(CreateSocialDraftParams),

    /// Schedule a post at a future time via the platform's native scheduler.
    ///
    /// NOTE: This schedules a post but does not publish it immediately.
    /// The platform (or its scheduler, e.g., Buffer) controls the actual send.
    CreateScheduled(CreateScheduledParams),

    /// Poll the current state of a previously created draft or scheduled post.
    DraftStatus(SocialDraftStatusParams),

    /// Connectivity and credential health check.
    Health(SocialHealthParams),

    /// Advertise optional capabilities supported by this plugin.
    Capabilities(SocialCapabilitiesParams),
}

// ---------------------------------------------------------------------------
// Response envelope
// ---------------------------------------------------------------------------

/// Response sent from a social media plugin to TA over stdout.
///
/// One JSON line per response. Always contains `ok`; on success contains
/// the operation result fields; on failure contains `error`.
#[derive(Debug, Serialize, Deserialize)]
pub struct SocialPluginResponse {
    /// Whether the operation succeeded.
    pub ok: bool,

    /// Human-readable error message (only set when ok=false).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,

    /// Native draft ID assigned by the platform (only for create_draft op).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub draft_id: Option<String>,

    /// Native scheduled post ID (only for create_scheduled op).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheduled_id: Option<String>,

    /// ISO-8601 timestamp when the post is scheduled to go out (create_scheduled op).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scheduled_at: Option<String>,

    /// Current state of a draft or scheduled post (only for draft_status op).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<SocialPostState>,

    /// Connected handle / username (only for health op, e.g., "@username").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub handle: Option<String>,

    /// Provider name reported by the plugin (only for health op).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,

    /// Capabilities declared by the plugin (only for capabilities op).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub capabilities: Option<Vec<String>>,
}

impl SocialPluginResponse {
    /// Construct a success response with no result fields.
    pub fn ok() -> Self {
        Self {
            ok: true,
            error: None,
            draft_id: None,
            scheduled_id: None,
            scheduled_at: None,
            state: None,
            handle: None,
            provider: None,
            capabilities: None,
        }
    }

    /// Construct an error response.
    pub fn error(msg: impl Into<String>) -> Self {
        Self {
            ok: false,
            error: Some(msg.into()),
            draft_id: None,
            scheduled_id: None,
            scheduled_at: None,
            state: None,
            handle: None,
            provider: None,
            capabilities: None,
        }
    }
}

// ---------------------------------------------------------------------------
// create_draft
// ---------------------------------------------------------------------------

/// Parameters for the `create_draft` operation.
///
/// The plugin writes this draft to the platform's native draft state.
/// For LinkedIn: Draft Share API. For X: draft tweet endpoint.
/// For Buffer: Buffer Draft queue.
///
/// The user sees the draft in their platform UI and publishes when ready.
/// TA never publishes directly.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct CreateSocialDraftParams {
    /// Post content to draft.
    pub post: SocialPostContent,
}

/// The content of a social media post to be drafted or scheduled.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct SocialPostContent {
    /// Main post body text.
    pub body: String,

    /// URLs to media attachments (images, videos). May be empty.
    #[serde(default)]
    pub media_urls: Vec<String>,

    /// Post ID or URL being replied to (for threaded replies). None for new posts.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_to_id: Option<String>,
}

// ---------------------------------------------------------------------------
// create_scheduled
// ---------------------------------------------------------------------------

/// Parameters for the `create_scheduled` operation.
///
/// The plugin queues this post in the platform's native scheduler.
/// The post goes live at `scheduled_at` without further TA involvement.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct CreateScheduledParams {
    /// Post content to schedule.
    pub post: SocialPostContent,

    /// ISO-8601 timestamp when the post should go live.
    pub scheduled_at: String,
}

// ---------------------------------------------------------------------------
// draft_status
// ---------------------------------------------------------------------------

/// Parameters for the `draft_status` operation.
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct SocialDraftStatusParams {
    /// Platform-specific draft ID returned by `create_draft` or `create_scheduled`.
    pub draft_id: String,
}

/// Current state of a social post as reported by the platform.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "snake_case")]
pub enum SocialPostState {
    /// Draft exists and has not been published.
    Draft,
    /// The post has been published (by the user or scheduler).
    Published,
    /// The draft or scheduled post was deleted.
    Deleted,
    /// State cannot be determined (e.g., platform API limitations).
    Unknown,
}

impl std::fmt::Display for SocialPostState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SocialPostState::Draft => write!(f, "draft"),
            SocialPostState::Published => write!(f, "published"),
            SocialPostState::Deleted => write!(f, "deleted"),
            SocialPostState::Unknown => write!(f, "unknown"),
        }
    }
}

// ---------------------------------------------------------------------------
// health
// ---------------------------------------------------------------------------

/// Parameters for the `health` operation.
///
/// No parameters required — plugins use their configured credentials.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct SocialHealthParams {}

// ---------------------------------------------------------------------------
// capabilities
// ---------------------------------------------------------------------------

/// Parameters for the `capabilities` operation.
///
/// No parameters required.
#[derive(Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct SocialCapabilitiesParams {}

// ---------------------------------------------------------------------------
// Error type
// ---------------------------------------------------------------------------

/// Errors from social media plugin operations.
#[derive(Debug, thiserror::Error)]
pub enum SocialPluginError {
    #[error("social plugin not found: {name}. Install with: ta adapter setup social/{name}")]
    PluginNotFound { name: String },

    #[error("social plugin '{name}' op '{op}' failed: {reason}")]
    OpFailed {
        name: String,
        op: String,
        reason: String,
    },

    #[error("social plugin '{name}' produced invalid response for op '{op}': {reason}")]
    InvalidResponse {
        name: String,
        op: String,
        reason: String,
    },

    #[error("failed to spawn social plugin '{command}': {reason}. Ensure the plugin is on PATH.")]
    SpawnFailed { command: String, reason: String },

    #[error("social plugin '{name}' timed out after {timeout_secs}s for op '{op}'. Increase timeout in plugin.toml.")]
    Timeout {
        name: String,
        op: String,
        timeout_secs: u64,
    },

    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),

    #[error("JSON error: {0}")]
    Json(#[from] serde_json::Error),
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_draft_request_roundtrip() {
        let req = SocialPluginRequest::CreateDraft(CreateSocialDraftParams {
            post: SocialPostContent {
                body: "Excited to announce the cinepipe launch! 🎬".to_string(),
                media_urls: vec![],
                reply_to_id: None,
            },
        });
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"op\":\"create_draft\""));
        let parsed: SocialPluginRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, req);
    }

    #[test]
    fn create_scheduled_request_roundtrip() {
        let req = SocialPluginRequest::CreateScheduled(CreateScheduledParams {
            post: SocialPostContent {
                body: "Week 1 of our public alpha is live!".to_string(),
                media_urls: vec!["https://example.com/screenshot.png".to_string()],
                reply_to_id: None,
            },
            scheduled_at: "2026-04-07T14:00:00Z".to_string(),
        });
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"op\":\"create_scheduled\""));
        assert!(json.contains("2026-04-07T14:00:00Z"));
        let parsed: SocialPluginRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, req);
    }

    #[test]
    fn no_publish_op_variant() {
        // The protocol MUST NOT have a Publish variant.
        let req = SocialPluginRequest::Health(SocialHealthParams {});
        let json = serde_json::to_string(&req).unwrap();
        assert!(
            !json.contains("\"publish\""),
            "Publish op must not exist in the social protocol"
        );
    }

    #[test]
    fn draft_status_request_roundtrip() {
        let req = SocialPluginRequest::DraftStatus(SocialDraftStatusParams {
            draft_id: "linkedin-draft-xyz".to_string(),
        });
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"op\":\"draft_status\""));
        let parsed: SocialPluginRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, req);
    }

    #[test]
    fn health_request_roundtrip() {
        let req = SocialPluginRequest::Health(SocialHealthParams {});
        let json = serde_json::to_string(&req).unwrap();
        assert!(json.contains("\"op\":\"health\""));
        let parsed: SocialPluginRequest = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, req);
    }

    #[test]
    fn response_ok_roundtrip() {
        let resp = SocialPluginResponse::ok();
        let json = serde_json::to_string(&resp).unwrap();
        let parsed: SocialPluginResponse = serde_json::from_str(&json).unwrap();
        assert!(parsed.ok);
        assert!(parsed.error.is_none());
    }

    #[test]
    fn response_error_roundtrip() {
        let resp = SocialPluginResponse::error("credentials not found");
        let json = serde_json::to_string(&resp).unwrap();
        let parsed: SocialPluginResponse = serde_json::from_str(&json).unwrap();
        assert!(!parsed.ok);
        assert_eq!(parsed.error.as_deref(), Some("credentials not found"));
    }

    #[test]
    fn response_with_draft_id() {
        let mut resp = SocialPluginResponse::ok();
        resp.draft_id = Some("linkedin-draft-abc123".to_string());
        let json = serde_json::to_string(&resp).unwrap();
        let parsed: SocialPluginResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.draft_id.as_deref(), Some("linkedin-draft-abc123"));
    }

    #[test]
    fn response_with_scheduled_id() {
        let mut resp = SocialPluginResponse::ok();
        resp.scheduled_id = Some("buffer-post-xyz".to_string());
        resp.scheduled_at = Some("2026-04-07T14:00:00Z".to_string());
        let json = serde_json::to_string(&resp).unwrap();
        let parsed: SocialPluginResponse = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.scheduled_id.as_deref(), Some("buffer-post-xyz"));
        assert_eq!(parsed.scheduled_at.as_deref(), Some("2026-04-07T14:00:00Z"));
    }

    #[test]
    fn post_state_display() {
        assert_eq!(SocialPostState::Draft.to_string(), "draft");
        assert_eq!(SocialPostState::Published.to_string(), "published");
        assert_eq!(SocialPostState::Deleted.to_string(), "deleted");
        assert_eq!(SocialPostState::Unknown.to_string(), "unknown");
    }

    #[test]
    fn post_state_roundtrip() {
        for state in [
            SocialPostState::Draft,
            SocialPostState::Published,
            SocialPostState::Deleted,
            SocialPostState::Unknown,
        ] {
            let json = serde_json::to_string(&state).unwrap();
            let parsed: SocialPostState = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, state);
        }
    }

    #[test]
    fn social_protocol_version_is_one() {
        assert_eq!(SOCIAL_PROTOCOL_VERSION, 1);
    }

    #[test]
    fn post_content_with_media_urls() {
        let post = SocialPostContent {
            body: "Check out our new feature!".to_string(),
            media_urls: vec![
                "https://example.com/img1.png".to_string(),
                "https://example.com/img2.png".to_string(),
            ],
            reply_to_id: None,
        };
        let json = serde_json::to_string(&post).unwrap();
        let parsed: SocialPostContent = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.media_urls.len(), 2);
    }

    #[test]
    fn post_content_reply_to_id() {
        let post = SocialPostContent {
            body: "Replying to this!".to_string(),
            media_urls: vec![],
            reply_to_id: Some("tweet-12345".to_string()),
        };
        let json = serde_json::to_string(&post).unwrap();
        assert!(json.contains("reply_to_id"));
        let parsed: SocialPostContent = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.reply_to_id.as_deref(), Some("tweet-12345"));
    }
}