Files
museion/Museion/SettingsPaneView.swift
T
AlexKucera 5ad605eb3c feat(settings): profiles write path + Test Connection (#136)
Profiles S3 over the S2 shell: every Settings change now persists.

Model/effort writes ride the engine's own CLI — `hermes -p <profile>
config set/unset` for model.provider / model.default /
model.reasoning_effort / model.base_url (zai endpoint family written
LAST: setting model.provider clears a stale base_url, live-probed;
unset-when-absent is idempotent success). Provider keys upsert
VAR=value into ALL THREE profiles' .env — a key entered before any
surface is on its provider still persists and pre-lands for any later
switch (maintainer ruling 2026-10-28; no switch re-fire). 0600,
foreign lines preserved byte-for-byte, line-broken values refused at
the shared sink (env-file injection guard), NSLock serializes the
read-upsert-write across concurrent provider writes.

Hydration reads model blocks and keys back at pane open — a restart
neither blanks the key fields nor refuses Test Connection; an edit
made mid-read owns the truth (no stomp), and a custom z.ai base_url
seeds the endpoint field instead of being clobbered to a family
default. Per-target writes coalesce AND chain past the debounce
guard: cancellation can't stop a past-the-guard CLI write, so the
newer write queues — the last edit is always the last write to land
(finish-audit race fix, pinned).

Test Connection = two-step probe (GET /models, then a minimal
streaming chat) with honest decode per the factsheet: zai 1113 →
wrong key type, 401s → auth failed (Anthropic request_id),
finish=length → starved, empty 200 → request failed, errors riding a
2xx decoded as the provider error.

Live-probed wire corrections (dogfood, 2026-10-28): Content-Type
application/json on every request — URLSession defaults to
form-urlencoded and providers diverge (zai 200+envelope, OpenAI 400,
Anthropic tolerates); OpenAI speaks the Responses API (/responses,
input + max_output_tokens, flagship gpt-6.1-sol); probe token cap 512
(64 starved reasoning models into "no content"); OpenAI's
"error": null event snapshots don't decode as failures.

Finish-audit (ponytail + security + general reviewers): write-ordering
race + .env lost update + hydrate stomp + custom-endpoint clobber
fixed and pinned (one-shot hold / latched read knobs in the test
double); ~30 lines of dead flexibility cut (keyType protocol param,
works(reply:) payload, unused timeout knob, double JSON round trips).

Suite 468/468 xcresult-verified; swiftlint clean; factsheet §6
correction logged. Closes #136
2026-10-27 18:32:30 +01:00

230 lines
6.3 KiB
Swift

//
// SettingsPaneView.swift
// Museion
//
// #17 (S1.1): Settings — the Museion Home path, repoint-only (ADR-0004).
// #135 (Profiles S2): reworked to the #130 prototype's variant C verdict —
// a Settings sidebar + detail column (System Settings shape): destinations
// Content · Pass 1 · Pass 2 · Chat · Provider Keys. Content keeps the #17
// Home control unchanged; the surface and keys pages live in
// SettingsPagesView.swift.
// #136 (Profiles S3): the model runs over HermesSettingsWriter (the real
// CLI/.env rail) and hydrates from the profiles at pane open.
//
import AppKit
import SwiftUI
/// Settings pane — sidebar + detail per prototype variant C.
struct SettingsPaneView: View {
@Binding var home: URL
/// #136: the gate's binary winner — HermesSettingsWriter's CLI rail and
/// the hydration reads both need it (nil before a gate pass: writes
/// surface their failure, reads leave the seed).
let binaryPath: String?
@State private var page: SettingsPage = .content
/// #135/#136: the model over the REAL write path; hydrated from the
/// profiles at pane open.
@State private var model: SettingsModel
init(home: Binding<URL>, binaryPath: String?) {
_home = home
self.binaryPath = binaryPath
_model = State(initialValue: SettingsModel(writer: HermesSettingsWriter(hermesBinary: binaryPath)))
}
var body: some View {
VStack(spacing: 0) {
PaneHeader(title: "Settings")
if let error = model.lastWriteError {
Text(error)
.font(.system(size: 11, design: DesignTokens.mono))
.foregroundStyle(.orange)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.horizontal, 14)
.padding(.vertical, 4)
}
HStack(spacing: 0) {
sidebar
Rectangle()
.fill(DesignTokens.border)
.frame(width: 1)
detail
}
}
.background(DesignTokens.background)
.task { await model.hydrate() }
}
// MARK: Sidebar (prototype `.vC .side`)
private var sidebar: some View {
VStack(alignment: .leading, spacing: 2) {
ForEach(SettingsPage.allCases) { candidate in
SettingsSidebarButton(
page: candidate,
selection: $page,
)
}
Spacer(minLength: 0)
}
.padding(.horizontal, 8)
.padding(.vertical, 14)
.frame(width: DesignTokens.settingsSidebarWidth)
}
// MARK: Detail (prototype `.vC .detail`)
private var detail: some View {
ScrollView {
switch page {
case .content:
ContentSettingsPage(home: $home)
case .pass1:
SurfaceSettingsPage(model: model, surface: .pass1)
case .pass2:
SurfaceSettingsPage(model: model, surface: .pass2)
case .chat:
SurfaceSettingsPage(model: model, surface: .chat)
case .keys:
ProviderKeysPage(model: model)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
/// The sidebar's destinations (#135): the #17 Content control, one page per
/// LLM surface (Sessions rides Chat — #126), and the provider keys.
enum SettingsPage: String, CaseIterable, Identifiable, Hashable {
case content
case pass1
case pass2
case chat
case keys
var id: String {
rawValue
}
var label: String {
switch self {
case .content: "Content"
case .pass1: "Pass 1"
case .pass2: "Pass 2"
case .chat: "Chat"
case .keys: "Provider Keys"
}
}
/// The prototype's trailing mono annotation: pass chips + the Sessions
/// note on Chat (the chat row notes "Sessions rides this engine").
var annotation: String? {
switch self {
case .pass1: "P1"
case .pass2: "P2"
case .chat: "· Sessions"
default: nil
}
}
}
/// One sidebar row (prototype `.vC .side-button`): quiet label, trailing
/// mono annotation, surface card + inset accent bar when current.
private struct SettingsSidebarButton: View {
let page: SettingsPage
@Binding var selection: SettingsPage
@State private var hover = false
var body: some View {
let selected = selection == page
Button {
selection = page
} label: {
HStack(spacing: 8) {
Text(page.label)
.foregroundStyle(DesignTokens.foreground)
Spacer(minLength: 0)
if let annotation = page.annotation {
Text(annotation)
.font(.system(size: 10, design: DesignTokens.mono))
.foregroundStyle(DesignTokens.muted)
}
}
.padding(.horizontal, 10)
.padding(.vertical, 7)
.frame(maxWidth: .infinity, alignment: .leading)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.background {
if selected || hover {
RoundedRectangle(cornerRadius: 8)
.fill(selected ? DesignTokens.surface : DesignTokens.foreground.opacity(0.06))
}
}
.overlay(alignment: .leading) {
// `.side-button.current` inset accent bar.
if selected {
RoundedRectangle(cornerRadius: 1)
.fill(DesignTokens.accent)
.frame(width: 2)
.padding(.vertical, 4)
}
}
.onHover { hover = $0 }
.accessibilityLabel(page.label)
.accessibilityAddTraits(selected ? [.isSelected] : [])
}
}
/// The Content page: the existing #17 Home control, repoint-only, in the
/// variant C form (quiet label, mono path, Choose…).
private struct ContentSettingsPage: View {
@Binding var home: URL
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Content")
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
Text("Where the corpus lives.")
.font(.system(size: 12))
.foregroundStyle(DesignTokens.muted)
VStack(alignment: .leading, spacing: 14) {
VStack(alignment: .leading, spacing: 4) {
SettingsFieldLabel("Museion Home")
HStack(spacing: 8) {
Text(home.path(percentEncoded: false))
.font(.system(size: 12, design: DesignTokens.mono))
.lineLimit(1)
.truncationMode(.middle)
.textSelection(.enabled)
Spacer(minLength: 12)
Button("Choose…") { chooseHome() }
.buttonStyle(.bordered)
}
}
Text("Repoint-only: Museion never moves content.")
.font(.system(size: 11.5))
.foregroundStyle(DesignTokens.muted)
}
.padding(.top, 12)
}
.frame(maxWidth: DesignTokens.settingsFormWidth, alignment: .leading)
.padding(.horizontal, 26)
.padding(.top, 18)
.padding(.bottom, 48)
}
private func chooseHome() {
let panel = NSOpenPanel()
panel.canChooseDirectories = true
panel.canChooseFiles = false
panel.canCreateDirectories = true
panel.message = "Choose the folder holding your posts. Museion never moves content."
if panel.runModal() == .OK, let url = panel.url {
home = url
}
}
}