faculties 0.19.0

An office suite for AI agents: kanban, wiki, files, messaging, and a GORBIE-backed viewer — all persisted in a TribleSpace pile.
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
//! Read-only GORBIE-embeddable viewer for the `headspace` faculty.
//!
//! Headspace is the playground's active-agent config: which model
//! profile is active, what its model name / base URL / reasoning
//! effort / token budgets look like, plus the inactive profiles
//! available to switch to. This widget renders the live state as a
//! single "you are here" card plus a compact roster of other
//! profiles.
//!
//! The data lives on the `config` branch (the faculty's
//! `CONFIG_BRANCH` constant). One `KIND_CONFIG_ID` entity carries
//! the active configuration; the active model profile is referenced
//! by `active_model_profile_id` and resolves to a
//! `KIND_MODEL_PROFILE_ID` entity that holds the model-name,
//! base-url, token-budget, etc. attributes. The latest entry per
//! kind is selected by `metadata::updated_at` — appends are
//! history-preserving so older rows stay readable via timeline.
//!
//! ```ignore
//! let mut panel = HeadspaceViewer::default();
//! panel.render(ctx, config_ws);
//! ```

use std::collections::HashMap;

use GORBIE::prelude::CardCtx;
use GORBIE::themes::colorhash;

use triblespace::core::id::Id;
use triblespace::core::inline::encodings::hash::Handle;
use triblespace::core::inline::Inline;
use triblespace::core::metadata;
use triblespace::core::repo::pile::Pile;
use triblespace::core::repo::{CommitHandle, Workspace};
use triblespace::core::trible::TribleSet;
use triblespace::macros::{find, pattern};
use triblespace::prelude::blobencodings::LongString;
use triblespace::prelude::inlineencodings::{NsTAIInterval, U256BE};
use triblespace::prelude::View;

use crate::schemas::headspace::{
    playground_config, KIND_CONFIG_ID, KIND_MODEL_PROFILE_ID,
};

type TextHandle = Inline<Handle<LongString>>;

// ── Palette ──────────────────────────────────────────────────────────

fn color_muted(ui: &egui::Ui) -> egui::Color32 {
    if ui.visuals().dark_mode {
        egui::Color32::from_rgb(0x9a, 0x9a, 0x9a)
    } else {
        egui::Color32::from_rgb(0x6a, 0x6a, 0x6a)
    }
}

fn color_frame(ui: &egui::Ui) -> egui::Color32 {
    if ui.visuals().dark_mode {
        egui::Color32::from_rgb(0x29, 0x32, 0x36)
    } else {
        egui::Color32::from_rgb(0xec, 0xec, 0xec)
    }
}

fn profile_color(id: Id) -> egui::Color32 {
    colorhash::ral_categorical(id.as_ref())
}

fn mix(a: egui::Color32, b: egui::Color32, t: f32) -> egui::Color32 {
    let t = t.clamp(0.0, 1.0);
    let lerp = |x: u8, y: u8| {
        ((x as f32) * (1.0 - t) + (y as f32) * t).round().clamp(0.0, 255.0) as u8
    };
    egui::Color32::from_rgb(
        lerp(a.r(), b.r()),
        lerp(a.g(), b.g()),
        lerp(a.b(), b.b()),
    )
}

// ── Row structs ──────────────────────────────────────────────────────

#[derive(Clone, Debug)]
struct ModelProfile {
    id: Id,
    name: String,
    model_name: Option<String>,
    base_url: Option<String>,
    reasoning_effort: Option<String>,
    stream: Option<bool>,
    context_window_tokens: Option<u64>,
    max_output_tokens: Option<u64>,
    context_safety_margin_tokens: Option<u64>,
    chars_per_token: Option<u64>,
    has_api_key: bool,
}

impl ModelProfile {
    fn empty(id: Id) -> Self {
        Self {
            id,
            name: String::new(),
            model_name: None,
            base_url: None,
            reasoning_effort: None,
            stream: None,
            context_window_tokens: None,
            max_output_tokens: None,
            context_safety_margin_tokens: None,
            chars_per_token: None,
            has_api_key: false,
        }
    }
}

#[derive(Clone, Debug, Default)]
struct ActiveConfig {
    persona_id: Option<Id>,
    active_profile_id: Option<Id>,
}

struct HeadspaceLive {
    cached_head: Option<CommitHandle>,
    active: ActiveConfig,
    /// All known model profiles, keyed by their profile id (the
    /// `playground_config::model_profile_id` value on the catalog
    /// entry — distinct from the entry's own entity id, which is
    /// the timestamped revision row).
    profiles: HashMap<Id, ModelProfile>,
}

// ── Live snapshot ────────────────────────────────────────────────────

impl HeadspaceLive {
    fn refresh(ws: &mut Workspace<Pile>) -> Self {
        let space = ws
            .checkout(..)
            .map(|co| co.into_facts())
            .unwrap_or_else(|e| {
                eprintln!("[headspace] checkout: {e:?}");
                TribleSet::new()
            });
        let cached_head = ws.head();
        let active = load_active_config(ws, &space);
        let profiles = load_profiles(ws, &space);
        HeadspaceLive {
            cached_head,
            active,
            profiles,
        }
    }
}

/// Pick the most-recently-updated `KIND_CONFIG_ID` entity and read
/// its persona pointer + active-profile pointer. There can be many
/// historical config rows; `metadata::updated_at` orders them and
/// the latest wins.
fn load_active_config(_ws: &mut Workspace<Pile>, space: &TribleSet) -> ActiveConfig {
    let mut best: Option<(Id, i128)> = None;
    for (config_id, updated_at) in find!(
        (config_id: Id, updated_at: Inline<NsTAIInterval>),
        pattern!(space, [{
            ?config_id @
            metadata::tag: KIND_CONFIG_ID,
            metadata::updated_at: ?updated_at,
        }])
    ) {
        let key = interval_key(updated_at);
        match best {
            Some((_, ck)) if ck >= key => {}
            _ => best = Some((config_id, key)),
        }
    }
    let Some((config_id, _)) = best else {
        return ActiveConfig::default();
    };
    let persona_id = find!(
        v: Id,
        pattern!(space, [{ config_id @ playground_config::persona_id: ?v }])
    )
    .next();
    let active_profile_id = find!(
        v: Id,
        pattern!(space, [{
            config_id @ playground_config::active_model_profile_id: ?v
        }])
    )
    .next();
    ActiveConfig {
        persona_id,
        active_profile_id,
    }
}

/// Walk every `KIND_MODEL_PROFILE_ID` catalog entry, keep only the
/// latest per `model_profile_id`, and load its attributes. The
/// catalog stores append-only revisions per profile id; the latest
/// `metadata::updated_at` is the live row.
fn load_profiles(
    ws: &mut Workspace<Pile>,
    space: &TribleSet,
) -> HashMap<Id, ModelProfile> {
    // Map profile_id → (entry_id, updated_at key) — pick the latest.
    let mut latest: HashMap<Id, (Id, i128)> = HashMap::new();
    for (entry_id, profile_id, updated_at) in find!(
        (entry_id: Id, profile_id: Id, updated_at: Inline<NsTAIInterval>),
        pattern!(space, [{
            ?entry_id @
            metadata::tag: KIND_MODEL_PROFILE_ID,
            metadata::updated_at: ?updated_at,
            playground_config::model_profile_id: ?profile_id,
        }])
    ) {
        let key = interval_key(updated_at);
        latest
            .entry(profile_id)
            .and_modify(|slot| {
                if key > slot.1 {
                    *slot = (entry_id, key);
                }
            })
            .or_insert((entry_id, key));
    }

    let mut out: HashMap<Id, ModelProfile> = HashMap::new();
    for (profile_id, (entry_id, _)) in latest {
        let mut p = ModelProfile::empty(profile_id);

        // Friendly name from metadata::name (Handle<LongString>).
        let name_handle = find!(
            h: TextHandle,
            pattern!(space, [{ entry_id @ metadata::name: ?h }])
        )
        .next();
        p.name = name_handle
            .and_then(|h| read_text(ws, h))
            .unwrap_or_else(|| format!("profile-{}", short_hex(profile_id)));

        // Model name (Handle<LongString>).
        let model_handle = find!(
            h: TextHandle,
            pattern!(space, [{ entry_id @ playground_config::model_name: ?h }])
        )
        .next();
        p.model_name = model_handle.and_then(|h| read_text(ws, h));

        // Base URL (Handle<LongString>).
        let url_handle = find!(
            h: TextHandle,
            pattern!(space, [{ entry_id @ playground_config::model_base_url: ?h }])
        )
        .next();
        p.base_url = url_handle.and_then(|h| read_text(ws, h));

        // Reasoning effort (Handle<LongString>).
        let effort_handle = find!(
            h: TextHandle,
            pattern!(space, [{ entry_id @ playground_config::model_reasoning_effort: ?h }])
        )
        .next();
        p.reasoning_effort = effort_handle.and_then(|h| read_text(ws, h));

        // API key presence (Handle<LongString>) — we don't surface
        // the secret, just whether one is configured.
        p.has_api_key = find!(
            h: TextHandle,
            pattern!(space, [{ entry_id @ playground_config::model_api_key: ?h }])
        )
        .next()
        .is_some();

        // U256BE numerics — extracted to u64 when the upper 24 bytes
        // are zero (i.e. the value really fits a u64).
        p.stream =
            find_u64(space, entry_id, |id| {
                find!(
                    v: Inline<U256BE>,
                    pattern!(space, [{ id @ playground_config::model_stream: ?v }])
                )
                .next()
            })
            .map(|n| n != 0);
        p.context_window_tokens = find_u64(space, entry_id, |id| {
            find!(
                v: Inline<U256BE>,
                pattern!(space, [{ id @ playground_config::model_context_window_tokens: ?v }])
            )
            .next()
        });
        p.max_output_tokens = find_u64(space, entry_id, |id| {
            find!(
                v: Inline<U256BE>,
                pattern!(space, [{ id @ playground_config::model_max_output_tokens: ?v }])
            )
            .next()
        });
        p.context_safety_margin_tokens = find_u64(space, entry_id, |id| {
            find!(
                v: Inline<U256BE>,
                pattern!(space, [{
                    id @ playground_config::model_context_safety_margin_tokens: ?v
                }])
            )
            .next()
        });
        p.chars_per_token = find_u64(space, entry_id, |id| {
            find!(
                v: Inline<U256BE>,
                pattern!(space, [{ id @ playground_config::model_chars_per_token: ?v }])
            )
            .next()
        });
        out.insert(profile_id, p);
    }
    out
}

fn interval_key(interval: Inline<NsTAIInterval>) -> i128 {
    // Two i128s packed as big-endian-ordered halves. Use the first
    // (start) bound as the sort key — matches what headspace.rs does.
    let mut bytes = [0u8; 16];
    bytes.copy_from_slice(&interval.raw[..16]);
    i128::from_be_bytes(bytes)
}

fn read_text(ws: &mut Workspace<Pile>, h: TextHandle) -> Option<String> {
    ws.get::<View<str>, LongString>(h).ok().map(|v| {
        let s: &str = v.as_ref();
        s.to_string()
    })
}

/// Decode a 32-byte big-endian U256 to u64 when the value fits.
/// `query` is a tiny closure that does the per-attribute find!() —
/// keeps the call sites readable without generic type plumbing.
fn find_u64<F>(_space: &TribleSet, entity_id: Id, query: F) -> Option<u64>
where
    F: FnOnce(Id) -> Option<Inline<U256BE>>,
{
    let raw = query(entity_id)?;
    if raw.raw[..24].iter().any(|b| *b != 0) {
        return None;
    }
    let bytes: [u8; 8] = raw.raw[24..32].try_into().ok()?;
    Some(u64::from_be_bytes(bytes))
}

fn short_hex(id: Id) -> String {
    let full = format!("{id:x}");
    full.chars().take(8).collect()
}

fn id_hex(id: Id) -> String {
    format!("{id:x}")
}

fn format_tokens(n: u64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f32 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.0}K", n as f32 / 1_000.0)
    } else {
        format!("{n}")
    }
}

// ── Widget ───────────────────────────────────────────────────────────

pub struct HeadspaceViewer {
    live: Option<HeadspaceLive>,
}

impl Default for HeadspaceViewer {
    fn default() -> Self {
        Self { live: None }
    }
}

impl HeadspaceViewer {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn render(
        &mut self,
        ctx: &mut CardCtx<'_>,
        ws: &mut Workspace<Pile>,
    ) {
        let head = ws.head();
        let need_refresh = match self.live.as_ref() {
            None => true,
            Some(l) => l.cached_head != head,
        };
        if need_refresh {
            self.live = Some(HeadspaceLive::refresh(ws));
        }

        ctx.section("Headspace", |ctx| {
            let Some(live) = self.live.as_ref() else { return };

            ctx.grid(|g| {
                // Header line — total profile count + persona summary.
                g.full(|ctx| {
                    let ui = ctx.ui_mut();
                    let label = format!(
                        "{} PROFILE{}{}",
                        live.profiles.len(),
                        if live.profiles.len() == 1 { "" } else { "S" },
                        match live.active.persona_id {
                            Some(pid) => format!(" · PERSONA {}", short_hex(pid).to_uppercase()),
                            None => String::new(),
                        },
                    );
                    ui.label(
                        egui::RichText::new(label)
                            .monospace()
                            .strong()
                            .small()
                            .color(color_muted(ui)),
                    );
                });

                // Active-profile hero card.
                let active = live
                    .active
                    .active_profile_id
                    .and_then(|pid| live.profiles.get(&pid));
                if let Some(p) = active {
                    g.full(|ctx| {
                        render_active_card(ctx.ui_mut(), p, live.active.persona_id);
                    });
                } else {
                    g.full(|ctx| {
                        let ui = ctx.ui_mut();
                        ui.add_space(8.0);
                        ui.label(
                            egui::RichText::new("No active model profile.")
                                .monospace()
                                .small()
                                .color(color_muted(ui)),
                        );
                        ui.add_space(8.0);
                    });
                }

                // Other profiles roster.
                let mut others: Vec<&ModelProfile> = live
                    .profiles
                    .values()
                    .filter(|p| Some(p.id) != live.active.active_profile_id)
                    .collect();
                if !others.is_empty() {
                    others.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));
                    g.full(|ctx| {
                        let ui = ctx.ui_mut();
                        ui.add_space(4.0);
                        ui.label(
                            egui::RichText::new("OTHER PROFILES")
                                .monospace()
                                .strong()
                                .small()
                                .color(color_muted(ui)),
                        );
                    });
                    for p in others {
                        g.full(|ctx| {
                            render_other_profile_card(ctx.ui_mut(), p);
                        });
                    }
                }
            });
        });
    }
}

// ── Active-profile hero card ────────────────────────────────────────

fn render_active_card(
    ui: &mut egui::Ui,
    p: &ModelProfile,
    persona_id: Option<Id>,
) {
    let bubble_fill = ui.visuals().window_fill;
    let accent = profile_color(p.id);
    let text_on_accent = colorhash::text_color_on(accent);
    let body_text = colorhash::text_color_on(bubble_fill);
    let body_muted = mix(body_text, bubble_fill, 0.22);

    egui::Frame::NONE
        .fill(bubble_fill)
        .stroke(egui::Stroke::new(1.0, color_frame(ui)))
        .shadow(egui::epaint::Shadow {
            offset: [2, 2],
            blur: 0,
            spread: 0,
            color: egui::Color32::from_black_alpha(48),
        })
        .corner_radius(egui::CornerRadius::ZERO)
        .inner_margin(egui::Margin::ZERO)
        .show(ui, |ui| {
            ui.set_min_width(ui.available_width());
            ui.spacing_mut().item_spacing.y = 0.0;

            // ── Header: profile name + ACTIVE badge on accent ──
            egui::Frame::NONE
                .fill(accent)
                .corner_radius(egui::CornerRadius::ZERO)
                .inner_margin(egui::Margin {
                    left: 10,
                    right: 10,
                    top: 8,
                    bottom: 8,
                })
                .show(ui, |ui| {
                    // Force the header to span the card's full width
                    // so the accent fill paints edge-to-edge — without
                    // this, the Frame sizes to content and you get a
                    // colour bar shorter than the card.
                    ui.set_min_width(ui.available_width());
                    ui.spacing_mut().item_spacing.y = 2.0;
                    ui.horizontal(|ui| {
                        ui.label(
                            egui::RichText::new("ACTIVE")
                                .monospace()
                                .small()
                                .strong()
                                .color(text_on_accent),
                        );
                        ui.label(
                            egui::RichText::new("·")
                                .monospace()
                                .small()
                                .color(text_on_accent),
                        );
                        ui.label(
                            egui::RichText::new(&p.name)
                                .size(18.0)
                                .color(text_on_accent),
                        );
                    });
                    ui.label(
                        egui::RichText::new(id_hex(p.id))
                            .monospace()
                            .small()
                            .color(text_on_accent),
                    );
                });

            // ── Body: model, URL, reasoning, token budget ──
            egui::Frame::NONE
                .fill(bubble_fill)
                .corner_radius(egui::CornerRadius::ZERO)
                .inner_margin(egui::Margin {
                    left: 10,
                    right: 10,
                    top: 8,
                    bottom: 10,
                })
                .show(ui, |ui| {
                    ui.set_min_width(ui.available_width());
                    ui.spacing_mut().item_spacing.y = 4.0;

                    // Model name as a primary line.
                    if let Some(m) = p.model_name.as_ref() {
                        ui.label(
                            egui::RichText::new(m)
                                .monospace()
                                .strong()
                                .size(14.0)
                                .color(body_text),
                        );
                    }

                    if let Some(url) = p.base_url.as_ref() {
                        ui.label(
                            egui::RichText::new(url)
                                .monospace()
                                .small()
                                .color(body_muted),
                        );
                    }

                    // Pill row: reasoning effort, stream/no-stream, api-key indicator.
                    ui.add_space(2.0);
                    ui.horizontal_wrapped(|ui| {
                        ui.spacing_mut().item_spacing = egui::vec2(4.0, 4.0);
                        if let Some(eff) = p.reasoning_effort.as_ref() {
                            render_chip(ui, &format!("REASONING {}", eff.to_uppercase()));
                        }
                        match p.stream {
                            Some(true) => render_chip(ui, "STREAM"),
                            Some(false) => render_chip(ui, "NO-STREAM"),
                            None => {}
                        }
                        if p.has_api_key {
                            render_chip(ui, "API KEY \u{1F511}"); // 🔑
                        }
                        if let Some(persona) = persona_id {
                            render_chip(
                                ui,
                                &format!("PERSONA {}", short_hex(persona).to_uppercase()),
                            );
                        }
                    });

                    // Token-budget bar — context window split into
                    // (max output) | (safety margin) | (the rest available
                    // for input). Visual proportion at a glance.
                    if let Some(window) = p.context_window_tokens {
                        ui.add_space(6.0);
                        render_token_budget(
                            ui,
                            window,
                            p.max_output_tokens.unwrap_or(0),
                            p.context_safety_margin_tokens.unwrap_or(0),
                            p.chars_per_token.unwrap_or(4),
                            accent,
                            body_text,
                            body_muted,
                        );
                    }
                });
        });
}

fn render_token_budget(
    ui: &mut egui::Ui,
    window: u64,
    max_out: u64,
    safety: u64,
    chars_per_tok: u64,
    accent: egui::Color32,
    body_text: egui::Color32,
    body_muted: egui::Color32,
) {
    // Header line: "CONTEXT 200K · OUT 16K · SAFETY 1K · ~4 ch/tok"
    let parts = format!(
        "CONTEXT {} · OUT {} · SAFETY {} · ~{} CH/TOK",
        format_tokens(window),
        format_tokens(max_out),
        format_tokens(safety),
        chars_per_tok.max(1),
    );
    ui.label(
        egui::RichText::new(parts)
            .monospace()
            .small()
            .color(body_muted),
    );

    // Bar: full width, 8 px tall. Two segments — safety + max-out
    // (carved off the right), the remainder is "available for input"
    // shown in the accent colour. Gives a quick visual read of
    // "how much room do I have for context".
    let bar_height = 8.0;
    let (bar_rect, _) = ui.allocate_exact_size(
        egui::vec2(ui.available_width(), bar_height),
        egui::Sense::hover(),
    );
    let painter = ui.painter();
    // Background: framing colour for "context window" total.
    let frame = color_frame(ui);
    painter.rect_filled(bar_rect, egui::CornerRadius::ZERO, frame);

    let total = window.max(1) as f32;
    let safety_w = (safety as f32 / total) * bar_rect.width();
    let out_w = (max_out as f32 / total) * bar_rect.width();
    let used_w = safety_w + out_w;
    let input_w = (bar_rect.width() - used_w).max(0.0);

    // Input segment (accent — what the agent has to work with).
    let input_rect = egui::Rect::from_min_size(
        bar_rect.min,
        egui::vec2(input_w, bar_height),
    );
    painter.rect_filled(input_rect, egui::CornerRadius::ZERO, accent);

    // Max-output segment (muted accent — reserved for the reply).
    let out_rect = egui::Rect::from_min_size(
        egui::pos2(bar_rect.left() + input_w, bar_rect.top()),
        egui::vec2(out_w, bar_height),
    );
    painter.rect_filled(
        out_rect,
        egui::CornerRadius::ZERO,
        mix(accent, body_text, 0.55),
    );

    // Safety segment (the right edge — the do-not-cross buffer).
    let safety_rect = egui::Rect::from_min_size(
        egui::pos2(bar_rect.left() + input_w + out_w, bar_rect.top()),
        egui::vec2(safety_w, bar_height),
    );
    painter.rect_filled(
        safety_rect,
        egui::CornerRadius::ZERO,
        mix(body_text, frame, 0.40),
    );
}

// ── Inactive-profile card ───────────────────────────────────────────

fn render_other_profile_card(ui: &mut egui::Ui, p: &ModelProfile) {
    let bubble_fill = ui.visuals().window_fill;
    let accent = profile_color(p.id);
    let body_text = colorhash::text_color_on(bubble_fill);
    let body_muted = mix(body_text, bubble_fill, 0.30);

    egui::Frame::NONE
        .fill(bubble_fill)
        .stroke(egui::Stroke::new(1.0, color_frame(ui)))
        .corner_radius(egui::CornerRadius::ZERO)
        .inner_margin(egui::Margin {
            left: 10,
            right: 10,
            top: 6,
            bottom: 6,
        })
        .show(ui, |ui| {
            ui.set_min_width(ui.available_width());
            ui.spacing_mut().item_spacing.y = 2.0;

            ui.horizontal(|ui| {
                // 8-px swatch indicating profile colour identity.
                let (swatch, _) = ui.allocate_exact_size(
                    egui::vec2(10.0, 10.0),
                    egui::Sense::hover(),
                );
                ui.painter()
                    .rect_filled(swatch, egui::CornerRadius::ZERO, accent);
                ui.add_space(4.0);
                ui.label(
                    egui::RichText::new(&p.name)
                        .monospace()
                        .strong()
                        .size(13.0)
                        .color(body_text),
                );
                if let Some(m) = p.model_name.as_ref() {
                    ui.label(
                        egui::RichText::new("·")
                            .monospace()
                            .small()
                            .color(body_muted),
                    );
                    ui.label(
                        egui::RichText::new(m)
                            .monospace()
                            .small()
                            .color(body_muted),
                    );
                }
            });
            ui.label(
                egui::RichText::new(id_hex(p.id))
                    .monospace()
                    .small()
                    .color(body_muted),
            );
        });
}

// ── Small chip used in the active card's pill row ──────────────────

fn render_chip(ui: &mut egui::Ui, label: &str) {
    let fill = colorhash::ral_categorical(label.as_bytes());
    let text = colorhash::text_color_on(fill);
    egui::Frame::NONE
        .fill(fill)
        .corner_radius(egui::CornerRadius::ZERO)
        .inner_margin(egui::Margin::symmetric(5, 1))
        .show(ui, |ui| {
            ui.label(
                egui::RichText::new(label)
                    .monospace()
                    .small()
                    .strong()
                    .color(text),
            );
        });
}