orbital-discussion 0.1.1

Composable discussion and reply-thread components for Orbital
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
558
559
560
use chrono::{Duration, TimeZone, Utc};

use crate::{
    DiscussionAuthor, DiscussionAuthorRole, DiscussionCitation, DiscussionComposerSubmit,
    DiscussionLabel, DiscussionMetadata, DiscussionPart, DiscussionReply, DiscussionReplyStatus,
    DiscussionStepPart,
};

fn reply_at(
    id: &str,
    parent_id: Option<&str>,
    author_name: &str,
    text: &str,
    labels: Vec<DiscussionLabel>,
    hidden_child_count: u32,
    role: DiscussionAuthorRole,
    created_at: chrono::DateTime<Utc>,
    edited: bool,
) -> DiscussionReply {
    DiscussionReply {
        id: id.to_string(),
        parent_id: parent_id.map(str::to_string),
        author: DiscussionAuthor::new(id, author_name).with_role(role),
        metadata: DiscussionMetadata {
            created_at,
            edited_at: edited.then(|| created_at + Duration::minutes(15)),
            labels,
        },
        parts: vec![DiscussionPart::text(text)],
        citations: vec![],
        status: DiscussionReplyStatus::Ready,
        hidden_child_count,
    }
}

fn reply(
    id: &str,
    parent_id: Option<&str>,
    author_name: &str,
    text: &str,
    labels: Vec<DiscussionLabel>,
    hidden_child_count: u32,
    role: DiscussionAuthorRole,
    hours_ago: i64,
    edited: bool,
) -> DiscussionReply {
    let created_at = Utc::now() - Duration::hours(hours_ago);
    reply_at(
        id,
        parent_id,
        author_name,
        text,
        labels,
        hidden_child_count,
        role,
        created_at,
        edited,
    )
}

/// Logged-in viewer author id used in previews for accent card tinting.
pub const PREVIEW_VIEWER_AUTHOR_ID: &str = "r-jordan";

/// Build an in-memory reply from a composer submit (preview / demo only).
pub fn reply_from_submit(
    payload: &DiscussionComposerSubmit,
    author_id: &str,
    author_name: &str,
) -> DiscussionReply {
    let id = format!("preview-{}", Utc::now().timestamp_millis());
    let mut parts = vec![DiscussionPart::text(&payload.body_markdown)];

    for attachment in &payload.attachments {
        let url = attachment
            .uploaded_url
            .clone()
            .unwrap_or_else(|| format!("https://orbital.dev/files/{}", attachment.name));
        parts.push(DiscussionPart::File {
            name: attachment.name.clone(),
            url,
            mime: attachment.mime.clone(),
            size_bytes: attachment.size_bytes,
        });
    }

    DiscussionReply {
        id,
        parent_id: payload.parent_id.clone(),
        author: DiscussionAuthor::new(author_id, author_name),
        metadata: DiscussionMetadata {
            created_at: Utc::now(),
            edited_at: None,
            labels: vec![],
        },
        parts,
        citations: payload.citations.clone(),
        status: DiscussionReplyStatus::Ready,
        hidden_child_count: 0,
    }
}

/// Append an optimistic in-memory reply from a composer submit (preview / demo only).
pub fn append_composer_reply(
    replies: &mut Vec<DiscussionReply>,
    payload: &DiscussionComposerSubmit,
    author_id: &str,
    author_name: &str,
) {
    replies.push(reply_from_submit(payload, author_id, author_name));
}

/// Sample nested thread for previews and tests.
pub fn sample_thread() -> Vec<DiscussionReply> {
    vec![
        reply(
            "r-root",
            None,
            "Alex Chen",
            "Main message with **markdown** and a [design link](https://orbital.dev).",
            vec![DiscussionLabel::Op],
            0,
            DiscussionAuthorRole::User,
            2,
            false,
        ),
        reply(
            "r-sam",
            Some("r-root"),
            "Sam Rivera",
            "Agree — let's track open questions here.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            1,
            true,
        ),
        reply(
            "r-jordan",
            Some("r-sam"),
            "Jordan Lee",
            "I'll draft the migration checklist.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
        reply(
            "r-pat",
            Some("r-jordan"),
            "Pat Okonkwo",
            "Depth-3 reply for tree testing.",
            vec![],
            3,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
        reply(
            "r-morgan",
            Some("r-root"),
            "Morgan Kim",
            "Separate branch at L1.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
        reply(
            "r-agent",
            Some("r-morgan"),
            "Orbital Agent",
            "Based on the thread, the default depth is **4**.",
            vec![],
            0,
            DiscussionAuthorRole::Agent,
            0,
            false,
        ),
    ]
}

/// Empty thread for overlay previews (Phase 4).
pub fn empty_thread() -> Vec<DiscussionReply> {
    vec![]
}

/// Short thread for slots composition preview (OP + one reply).
pub fn slots_thread() -> Vec<DiscussionReply> {
    vec![
        reply(
            "slots-root",
            None,
            "Alex Chen",
            "Welcome to the discussion — customize the toolbar slot above.",
            vec![DiscussionLabel::Op],
            0,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
        reply(
            "slots-reply",
            Some("slots-root"),
            "Sam Rivera",
            "Looks good. The host owns this toolbar region.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
    ]
}

/// Deep nested thread (5 levels) for tree navigation and show-more previews.
pub fn deep_thread() -> Vec<DiscussionReply> {
    vec![
        reply(
            "d-root",
            None,
            "Alex Chen",
            "Root post for depth testing.",
            vec![DiscussionLabel::Op],
            0,
            DiscussionAuthorRole::User,
            5,
            false,
        ),
        reply(
            "d-l1",
            Some("d-root"),
            "Sam Rivera",
            "Level 1 reply.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            4,
            false,
        ),
        reply(
            "d-l2",
            Some("d-l1"),
            "Jordan Lee",
            "Level 2 reply.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            3,
            false,
        ),
        reply(
            "d-l3",
            Some("d-l2"),
            "Pat Okonkwo",
            "Level 3 reply.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            2,
            false,
        ),
        reply(
            "d-l4",
            Some("d-l3"),
            "Casey Ng",
            "Level 4 reply at depth cap.",
            vec![],
            4,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
    ]
}

/// Flat thread with replies spanning two calendar days for date divider previews.
pub fn flat_thread_with_dates() -> Vec<DiscussionReply> {
    let yesterday = Utc::now().date_naive() - Duration::days(1);
    let today = Utc::now().date_naive();
    let yesterday_noon =
        Utc.from_utc_datetime(&yesterday.and_hms_opt(12, 0, 0).expect("valid time"));
    let today_noon = Utc.from_utc_datetime(&today.and_hms_opt(12, 0, 0).expect("valid time"));

    vec![
        reply_at(
            "fd-root",
            None,
            "Alex Chen",
            "Yesterday's root post.",
            vec![DiscussionLabel::Op],
            0,
            DiscussionAuthorRole::User,
            yesterday_noon,
            false,
        ),
        reply_at(
            "fd-y1",
            Some("fd-root"),
            "Sam Rivera",
            "Reply from yesterday.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            yesterday_noon + Duration::hours(1),
            false,
        ),
        reply_at(
            "fd-t1",
            Some("fd-root"),
            "Jordan Lee",
            "Reply from today.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            today_noon,
            false,
        ),
        reply_at(
            "fd-t2",
            Some("fd-t1"),
            "Pat Okonkwo",
            "Nested reply from today.",
            vec![],
            0,
            DiscussionAuthorRole::User,
            today_noon + Duration::hours(1),
            false,
        ),
    ]
}

/// Thread with a fenced code block in the root reply.
pub fn thread_with_code_block() -> Vec<DiscussionReply> {
    vec![reply(
        "cb-root",
        None,
        "Alex Chen",
        "Example usage:\n\n```rust\nfn main() {\n    println!(\"Hello\");\n}\n```",
        vec![DiscussionLabel::Op],
        0,
        DiscussionAuthorRole::User,
        1,
        false,
    )]
}

/// Thread with structured citations on the root reply (includes inline ref in body).
pub fn thread_with_citations() -> Vec<DiscussionReply> {
    thread_with_custom_citations()
}

/// Citations with custom kind/data and markdown `[^id]` reference in body.
pub fn thread_with_custom_citations() -> Vec<DiscussionReply> {
    vec![DiscussionReply {
        id: "cit-root".to_string(),
        parent_id: None,
        author: DiscussionAuthor::new("cit-root", "Alex Chen"),
        metadata: DiscussionMetadata {
            created_at: Utc::now() - Duration::hours(2),
            edited_at: None,
            labels: vec![DiscussionLabel::Op],
        },
        parts: vec![DiscussionPart::text(
            "Reply referencing external sources — see [^cit-1] and [^cit-2].",
        )],
        citations: vec![
            DiscussionCitation {
                id: "cit-1".to_string(),
                title: "Design doc §4.2".to_string(),
                url: Some("https://orbital.dev/discussion/design".to_string()),
                excerpt: Some(
                    "Depth cap defaults to four visible levels before show-more drill-in."
                        .to_string(),
                ),
                kind: Some("agreement_vote".to_string()),
                data: Some(serde_json::json!({
                    "score": 3,
                    "viewer_vote": "agree"
                })),
            },
            DiscussionCitation {
                id: "cit-2".to_string(),
                title: "Research worksheet DISC-19".to_string(),
                url: Some("https://orbital.dev/discussion/research".to_string()),
                excerpt: None,
                kind: Some("agreement_vote".to_string()),
                data: Some(serde_json::json!({ "score": 1, "viewer_vote": null })),
            },
        ],
        status: DiscussionReplyStatus::Ready,
        hidden_child_count: 0,
    }]
}

/// Thread with image and download file parts.
pub fn thread_with_file_parts() -> Vec<DiscussionReply> {
    vec![DiscussionReply {
        id: "fp-root".to_string(),
        parent_id: None,
        author: DiscussionAuthor::new("fp-root", "Alex Chen"),
        metadata: DiscussionMetadata {
            created_at: Utc::now() - Duration::hours(1),
            edited_at: None,
            labels: vec![DiscussionLabel::Op],
        },
        parts: vec![
            DiscussionPart::text("Screenshot and spec attached below."),
            DiscussionPart::File {
                name: "architecture.png".to_string(),
                url: "https://picsum.photos/seed/orbital-discussion/640/360".to_string(),
                mime: Some("image/png".to_string()),
                size_bytes: Some(245_760),
            },
            DiscussionPart::File {
                name: "specification.pdf".to_string(),
                url: "https://orbital.dev/files/specification.pdf".to_string(),
                mime: Some("application/pdf".to_string()),
                size_bytes: Some(1_048_576),
            },
        ],
        citations: vec![],
        status: DiscussionReplyStatus::Ready,
        hidden_child_count: 0,
    }]
}

/// Thread demonstrating non-ready reply status indicators.
pub fn thread_with_statuses() -> Vec<DiscussionReply> {
    vec![
        reply(
            "st-sending",
            None,
            "Alex Chen",
            "Optimistic reply while the host persists.",
            vec![DiscussionLabel::Op],
            0,
            DiscussionAuthorRole::User,
            0,
            false,
        ),
        DiscussionReply {
            id: "st-streaming".to_string(),
            parent_id: Some("st-sending".to_string()),
            author: DiscussionAuthor::new("st-streaming", "Orbital Agent")
                .with_role(DiscussionAuthorRole::Agent),
            metadata: DiscussionMetadata {
                created_at: Utc::now(),
                edited_at: None,
                labels: vec![],
            },
            parts: vec![DiscussionPart::text("Streaming response in progress…")],
            citations: vec![],
            status: DiscussionReplyStatus::Streaming,
            hidden_child_count: 0,
        },
        DiscussionReply {
            id: "st-error".to_string(),
            parent_id: Some("st-sending".to_string()),
            author: DiscussionAuthor::new("st-error", "Sam Rivera"),
            metadata: DiscussionMetadata {
                created_at: Utc::now(),
                edited_at: None,
                labels: vec![],
            },
            parts: vec![DiscussionPart::text("Failed to deliver.")],
            citations: vec![],
            status: DiscussionReplyStatus::Error("Network timeout".to_string()),
            hidden_child_count: 0,
        },
    ]
    .into_iter()
    .map(|mut reply| {
        if reply.id == "st-sending" {
            reply.status = DiscussionReplyStatus::Sending;
        }
        reply
    })
    .collect()
}

/// Thread with a custom part for part_view previews.
pub fn thread_with_custom_part() -> Vec<DiscussionReply> {
    vec![DiscussionReply {
        id: "cp-root".to_string(),
        parent_id: None,
        author: DiscussionAuthor::new("cp-root", "Alex Chen"),
        metadata: DiscussionMetadata {
            created_at: Utc::now() - Duration::hours(1),
            edited_at: None,
            labels: vec![DiscussionLabel::Op],
        },
        parts: vec![DiscussionPart::Custom {
            kind: "poll".to_string(),
            data: serde_json::json!({ "question": "Ship Phase 2?" }),
        }],
        citations: vec![],
        status: DiscussionReplyStatus::Ready,
        hidden_child_count: 0,
    }]
}

/// Mock H — agent reply with reasoning, tool approval, and streaming text (Phase 6).
pub fn agent_thread_mock_h() -> Vec<DiscussionReply> {
    use crate::{
        DiscussionReasoningStatus, DiscussionStepStatus, DiscussionToolPart,
        DiscussionToolRunStatus,
    };

    vec![
        reply(
            "r-agent-root",
            None,
            "Alex Chen",
            "What is the default tree depth limit?",
            vec![DiscussionLabel::Op],
            0,
            DiscussionAuthorRole::User,
            1,
            false,
        ),
        DiscussionReply {
            id: "r-agent-h".to_string(),
            parent_id: Some("r-agent-root".to_string()),
            author: DiscussionAuthor::new("r-agent-h", "Orbital Agent")
                .with_role(DiscussionAuthorRole::Agent),
            metadata: DiscussionMetadata {
                created_at: Utc::now(),
                edited_at: None,
                labels: vec![],
            },
            parts: vec![
                DiscussionPart::reasoning(
                    "Checking design doc for depth configuration…",
                    DiscussionReasoningStatus::Done,
                ),
                DiscussionPart::Step(DiscussionStepPart::new(
                    1,
                    "Search documentation",
                    DiscussionStepStatus::Active,
                )),
                DiscussionPart::tool(DiscussionToolPart {
                    tool_call_id: "tc-search-docs".to_string(),
                    tool_name: "search_docs".to_string(),
                    input: serde_json::json!({ "query": "depth limit" }),
                    output: None,
                    status: DiscussionToolRunStatus::AwaitingApproval,
                    error_message: None,
                }),
                DiscussionPart::streaming_text(
                    "Based on the design doc, the default depth is **4**.",
                ),
            ],
            citations: vec![],
            status: DiscussionReplyStatus::Streaming,
            hidden_child_count: 0,
        },
    ]
}