faculties 0.21.0

An office suite for AI agents: kanban, wiki, files, messaging, and a Lissajous-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
//! 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.
//!
//! It consumes the exact native Headspace snapshot and the aggregate of locally
//! readable configured Secrets collections. The shared projector preserves missing,
//! agreed, and forked DAG heads; this widget never chooses a timestamp winner
//! and never decrypts a credential.
//!
//! ```ignore
//! let mut panel = HeadspaceViewer::default();
//! panel.render(ctx, headspace_view, secrets_view);
//! ```

use std::collections::{BTreeMap, HashMap};

use GORBIE::prelude::CardCtx;
use GORBIE::themes::{blend, colorhash};

use crate::headspace::{self, ConfigValue, ProfileValue, Resolution};
use triblespace::core::id::Id;

use super::storage::{DatasetRevision, DatasetView, SecretsView};

// ── 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())
}

// ── 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,
    resolution: Option<String>,
}

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,
            resolution: None,
        }
    }

    fn from_value(value: &ProfileValue, resolution: Option<String>) -> Self {
        Self {
            id: value.anchor,
            name: value.name.clone(),
            model_name: Some(value.model.clone()),
            base_url: Some(value.base_url.clone()),
            reasoning_effort: value.reasoning_effort.clone(),
            stream: Some(value.stream),
            context_window_tokens: Some(value.context_window_tokens),
            max_output_tokens: Some(value.max_output_tokens),
            context_safety_margin_tokens: Some(value.context_safety_margin_tokens),
            chars_per_token: Some(value.chars_per_token),
            has_api_key: value.model_secret_version.is_some(),
            resolution,
        }
    }
}

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

struct HeadspaceLive {
    headspace_revision: DatasetRevision,
    secrets_revision: DatasetRevision,
    active: ActiveConfig,
    profiles: HashMap<Id, ModelProfile>,
    diagnostic: Option<String>,
}

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

impl HeadspaceLive {
    fn refresh(headspace_view: DatasetView<'_>, secrets_view: SecretsView<'_>) -> Self {
        let result = (|| {
            let config = headspace::current_config(headspace_view.reader, headspace_view.facts)
                .map_err(|error| format!("Headspace config: {error:#}"))?;
            let profiles = headspace::current_profiles(headspace_view.reader, headspace_view.facts)
                .map_err(|error| format!("Headspace profiles: {error:#}"))?;
            headspace::validate_current_secret_references(
                &config,
                profiles.values(),
                secrets_view.snapshot,
            )
            .map_err(|error| format!("Headspace secret references: {error:#}"))?;
            Ok::<_, String>((load_active_config(&config), load_profiles(&profiles)))
        })();

        let (active, profiles, diagnostic) = match result {
            Ok((active, profiles)) => (active, profiles, None),
            Err(error) => (ActiveConfig::default(), HashMap::new(), Some(error)),
        };
        Self {
            headspace_revision: headspace_view.revision,
            secrets_revision: secrets_view.revision,
            active,
            profiles,
            diagnostic,
        }
    }
}

fn load_active_config(config: &Resolution<ConfigValue>) -> ActiveConfig {
    match config {
        Resolution::Missing => ActiveConfig {
            resolution: Some("NO NATIVE CONFIG".to_owned()),
            ..ActiveConfig::default()
        },
        Resolution::Unique(snapshot) => ActiveConfig {
            persona_id: snapshot.value.persona,
            active_profile_id: Some(snapshot.value.active_profile),
            resolution: None,
        },
        Resolution::Agreed(snapshots) => snapshots
            .first()
            .map(|snapshot| ActiveConfig {
                persona_id: snapshot.value.persona,
                active_profile_id: Some(snapshot.value.active_profile),
                resolution: Some(format!("{} CONFIG HEADS AGREE", snapshots.len())),
            })
            .unwrap_or_default(),
        Resolution::Forked(snapshots) => ActiveConfig {
            resolution: Some(format!("CONFIG FORK · {} HEADS", snapshots.len())),
            ..ActiveConfig::default()
        },
        Resolution::Invalid(error) => ActiveConfig {
            resolution: Some(format!("INVALID CONFIG · {error}")),
            ..ActiveConfig::default()
        },
    }
}

fn load_profiles(profiles: &BTreeMap<Id, Resolution<ProfileValue>>) -> HashMap<Id, ModelProfile> {
    let mut out = HashMap::new();
    for (anchor, resolution) in profiles {
        let profile = match resolution {
            Resolution::Unique(snapshot) => ModelProfile::from_value(&snapshot.value, None),
            Resolution::Agreed(snapshots) => snapshots
                .first()
                .map(|snapshot| {
                    ModelProfile::from_value(
                        &snapshot.value,
                        Some(format!("{} HEADS AGREE", snapshots.len())),
                    )
                })
                .unwrap_or_else(|| ModelProfile::empty(*anchor)),
            Resolution::Forked(snapshots) => {
                let mut profile = ModelProfile::empty(*anchor);
                profile.name = format!("forked-{}", short_hex(*anchor));
                profile.resolution = Some(format!("FORK · {} HEADS", snapshots.len()));
                profile
            }
            Resolution::Missing => {
                let mut profile = ModelProfile::empty(*anchor);
                profile.name = format!("missing-{}", short_hex(*anchor));
                profile.resolution = Some("MISSING".to_owned());
                profile
            }
            Resolution::Invalid(error) => {
                let mut profile = ModelProfile::empty(*anchor);
                profile.name = format!("invalid-{}", short_hex(*anchor));
                profile.resolution = Some(format!("INVALID · {error}"));
                profile
            }
        };
        out.insert(*anchor, profile);
    }
    out
}

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<'_>,
        headspace_view: DatasetView<'_>,
        secrets_view: SecretsView<'_>,
    ) {
        let need_refresh = match self.live.as_ref() {
            None => true,
            Some(live) => {
                live.headspace_revision != headspace_view.revision
                    || live.secrets_revision != secrets_view.revision
            }
        };
        if need_refresh {
            self.live = Some(HeadspaceLive::refresh(headspace_view, secrets_view));
        }

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

            if let Some(diagnostic) = live.diagnostic.as_deref() {
                render_diagnostic(ctx.ui_mut(), diagnostic);
                return;
            }

            ctx.grid(|g| {
                if let Some(resolution) = live.active.resolution.as_deref() {
                    g.full(|ctx| render_diagnostic(ctx.ui_mut(), resolution));
                }

                // 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 = blend(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(resolution) = p.resolution.as_deref() {
                            render_chip(ui, resolution);
                        }
                        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,
        blend(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,
        blend(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 = blend(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));
                }
                if let Some(resolution) = p.resolution.as_deref() {
                    render_chip(ui, resolution);
                }
            });
            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),
            );
        });
}

fn render_diagnostic(ui: &mut egui::Ui, message: &str) {
    let color = egui::Color32::from_rgb(0xd1, 0x83, 0x16);
    let background = egui::Color32::from_rgba_unmultiplied(color.r(), color.g(), color.b(), 36);
    egui::Frame::NONE
        .fill(background)
        .stroke(egui::Stroke::new(1.0, color))
        .corner_radius(egui::CornerRadius::ZERO)
        .inner_margin(egui::Margin::symmetric(8, 5))
        .show(ui, |ui| {
            ui.set_min_width(ui.available_width());
            ui.label(
                egui::RichText::new(message)
                    .monospace()
                    .small()
                    .strong()
                    .color(color),
            );
        });
}