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
335 lines
10 KiB
Swift
335 lines
10 KiB
Swift
//
|
|
// SettingsPagesView.swift
|
|
// Museion
|
|
//
|
|
// #135 (Profiles S2): the Settings sidebar's detail pages over the
|
|
// in-memory SettingsModel — one page per LLM surface (provider select,
|
|
// registry combobox with first-class free text, per-provider effort menu)
|
|
// and the Provider Keys page (endpoint, z.ai key-type toggle swapping the
|
|
// endpoint family, masked key + reveal). Visual contract: #130 prototype
|
|
// variant C, with variant B's current-model headline folded in.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
/// The quiet field label (prototype `.field-label`, #130 fold-in): mono
|
|
/// 10.5 uppercase, one line, never a wrapping caption.
|
|
struct SettingsFieldLabel: View {
|
|
let text: String
|
|
|
|
init(_ text: String) {
|
|
self.text = text
|
|
}
|
|
|
|
var body: some View {
|
|
Text(text)
|
|
.font(.system(size: 10.5, design: DesignTokens.mono))
|
|
.lineLimit(1)
|
|
.tracking(0.5)
|
|
.textCase(.uppercase)
|
|
.foregroundStyle(DesignTokens.muted)
|
|
}
|
|
}
|
|
|
|
/// One surface's model page (prototype `.vC .detail` surface branch +
|
|
/// variant B's `.current` headline): provider, model combobox, reasoning
|
|
/// effort. Output budget is not a control (#128).
|
|
struct SurfaceSettingsPage: View {
|
|
let model: SettingsModel
|
|
let surface: ProfileSurface
|
|
|
|
var body: some View {
|
|
let config = model.config(for: surface)
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
HStack {
|
|
Text(label)
|
|
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
|
|
Spacer(minLength: 12)
|
|
// macOS convention: Restore Defaults in the page header.
|
|
Button("Restore Defaults") {
|
|
model.resetToDefault(for: surface)
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.controlSize(.small)
|
|
.disabled(config == .surfaceDefault)
|
|
}
|
|
// Variant B's stolen headline: the current model reads first.
|
|
Text(config.model + (model.isCustom(surface) ? " · custom (not in registry)" : ""))
|
|
.font(.system(size: 12.5, design: DesignTokens.mono))
|
|
.foregroundStyle(DesignTokens.accent)
|
|
Text("profile · \(surface.profileName) — \(note)")
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
.foregroundStyle(DesignTokens.muted)
|
|
VStack(alignment: .leading, spacing: 14) {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
SettingsFieldLabel("Provider")
|
|
Picker("Provider", selection: Binding(
|
|
get: { config.provider },
|
|
set: { model.setProvider($0, for: surface) },
|
|
)) {
|
|
ForEach(ModelProvider.allCases, id: \.self) { provider in
|
|
Text(provider.label).tag(provider)
|
|
}
|
|
}
|
|
.pickerStyle(.menu)
|
|
.frame(width: 200)
|
|
}
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
SettingsFieldLabel("Model")
|
|
HStack(spacing: 8) {
|
|
ModelComboBox(
|
|
value: config.model,
|
|
items: model.registry.list(for: config.provider),
|
|
) { model.setModel($0, for: surface) }
|
|
.frame(width: 240)
|
|
if model.isCustom(surface) {
|
|
Text("custom (not in registry)")
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
.foregroundStyle(DesignTokens.muted)
|
|
}
|
|
}
|
|
}
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
SettingsFieldLabel("Reasoning Effort")
|
|
Picker("Reasoning Effort", selection: Binding(
|
|
get: { config.effort },
|
|
set: { model.setEffort($0, for: surface) },
|
|
)) {
|
|
Text("unset · provider default").tag(ReasoningEffort?.none)
|
|
// The provider's ladder only — a provider switch
|
|
// rebuilds this menu (and resets the choice).
|
|
ForEach(config.provider.effortOptions, id: \.self) { effort in
|
|
Text(effort.rawValue).tag(ReasoningEffort?.some(effort))
|
|
}
|
|
}
|
|
.pickerStyle(.menu)
|
|
.frame(width: 200)
|
|
}
|
|
Text("Output budget fixed at \(ProfileSurface.outputBudgetTokens / 1000)k tokens — not a setting.")
|
|
.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 var label: String {
|
|
switch surface {
|
|
case .pass1: "Pass 1"
|
|
case .pass2: "Pass 2"
|
|
case .chat: "Chat"
|
|
}
|
|
}
|
|
|
|
/// The page's posture note (prototype `SURFACES[].note`): passes apply
|
|
/// on the next run (on-demand engines, ADR-0014); Chat is the resident
|
|
/// engine Sessions rides, restarted in place on a model change (#129).
|
|
private var note: String {
|
|
switch surface {
|
|
case .pass1, .pass2: "applies on the next pass"
|
|
case .chat: "Sessions rides this engine · a model change restarts it (after any streaming turn)"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The Provider Keys page (#135): one block per provider — endpoint, z.ai
|
|
/// key-type toggle, masked API key + reveal. One key per provider fans out
|
|
/// to every profile on that provider; no Keychain (#131 verdict).
|
|
struct ProviderKeysPage: View {
|
|
let model: SettingsModel
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text("Provider Keys")
|
|
.font(.system(size: 16, weight: .semibold, design: DesignTokens.serif))
|
|
Text("One key per provider, fanned out to every profile on it. No Keychain.")
|
|
.font(.system(size: 12))
|
|
.foregroundStyle(DesignTokens.muted)
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
ForEach(ModelProvider.allCases, id: \.self) { provider in
|
|
ProviderKeyBlock(model: model, provider: provider)
|
|
}
|
|
}
|
|
.padding(.top, 12)
|
|
}
|
|
.frame(maxWidth: DesignTokens.settingsFormWidth + 60, alignment: .leading)
|
|
.padding(.horizontal, 26)
|
|
.padding(.top, 18)
|
|
.padding(.bottom, 48)
|
|
}
|
|
}
|
|
|
|
/// One provider's key block (prototype `.block`).
|
|
private struct ProviderKeyBlock: View {
|
|
let model: SettingsModel
|
|
let provider: ModelProvider
|
|
@State private var revealed = false
|
|
|
|
var body: some View {
|
|
let config = model.keyConfig(for: provider)
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
HStack {
|
|
Text(provider.label)
|
|
.font(.system(size: 14, weight: .semibold, design: DesignTokens.serif))
|
|
Spacer(minLength: 12)
|
|
if provider == .zai {
|
|
KeyTypeSegment(model: model)
|
|
}
|
|
}
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
SettingsFieldLabel("Endpoint (Base URL)")
|
|
TextField(
|
|
"Endpoint",
|
|
text: Binding(
|
|
get: { config.endpoint },
|
|
set: { model.setEndpoint($0, for: provider) },
|
|
),
|
|
)
|
|
.font(.system(size: 11.5, design: DesignTokens.mono))
|
|
}
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
SettingsFieldLabel("API Key")
|
|
HStack(spacing: 6) {
|
|
if revealed {
|
|
TextField("API key", text: Binding(
|
|
get: { config.apiKey },
|
|
set: { model.setAPIKey($0, for: provider) },
|
|
))
|
|
.font(.system(size: 11.5, design: DesignTokens.mono))
|
|
} else {
|
|
SecureField("API key", text: Binding(
|
|
get: { config.apiKey },
|
|
set: { model.setAPIKey($0, for: provider) },
|
|
))
|
|
.font(.system(size: 11.5, design: DesignTokens.mono))
|
|
}
|
|
Button(revealed ? "hide" : "show") {
|
|
revealed.toggle()
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.controlSize(.small)
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
}
|
|
}
|
|
if provider == .zai {
|
|
Text("Key type picks the endpoint family — coding-plan keys are distribution-restricted; shipped Museion uses standard keys.")
|
|
.font(.system(size: 11.5))
|
|
.foregroundStyle(DesignTokens.muted)
|
|
}
|
|
// #136: Test Connection (factsheet §4) — /models count + a
|
|
// minimal streaming chat; latency + model count on success,
|
|
// the honest decoded verdict otherwise.
|
|
HStack(spacing: 8) {
|
|
Button("Test Connection") {
|
|
Task { await model.testConnection(for: provider) }
|
|
}
|
|
.buttonStyle(.bordered)
|
|
.controlSize(.small)
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
.disabled(model.testStates[provider] == .running)
|
|
switch model.testStates[provider] {
|
|
case .running:
|
|
HStack(spacing: 4) {
|
|
ProgressView()
|
|
.controlSize(.small)
|
|
Text("testing…")
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
.foregroundStyle(DesignTokens.muted)
|
|
}
|
|
case let .done(outcome):
|
|
TestConnectionResult(outcome: outcome)
|
|
case nil:
|
|
EmptyView()
|
|
}
|
|
}
|
|
}
|
|
.padding(EdgeInsets(top: 14, leading: 16, bottom: 14, trailing: 16))
|
|
.background(
|
|
RoundedRectangle(cornerRadius: DesignTokens.radius)
|
|
.fill(DesignTokens.surface)
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: DesignTokens.radius)
|
|
.strokeBorder(DesignTokens.border),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
|
|
/// The probe's one-line verdict (#136): works carries latency + model
|
|
/// count; every failure states its decoded reason (the AC's
|
|
/// wrong-key-type vs unreachable vs works distinction).
|
|
private struct TestConnectionResult: View {
|
|
let outcome: ProviderProbe.Outcome
|
|
|
|
var body: some View {
|
|
switch outcome {
|
|
case let .works(latencyMS, modelCount):
|
|
Text("✓ works · \(latencyMS) ms · \(modelCount) models")
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
.foregroundStyle(DesignTokens.accent)
|
|
case let .authFailed(detail):
|
|
text("key rejected — \(detail)")
|
|
case let .wrongKeyType(detail):
|
|
text("wrong key type — \(detail)")
|
|
case let .unreachable(detail):
|
|
text("unreachable — \(detail)")
|
|
case let .starved(detail):
|
|
text("token cap starved — \(detail)")
|
|
case let .requestFailed(status, detail):
|
|
text("HTTP \(status) — \(detail)")
|
|
}
|
|
}
|
|
|
|
private func text(_ content: String) -> some View {
|
|
Text(content)
|
|
.font(.system(size: 11, design: DesignTokens.mono))
|
|
.foregroundStyle(.orange)
|
|
.lineLimit(2)
|
|
.truncationMode(.middle)
|
|
.help(content)
|
|
}
|
|
}
|
|
|
|
/// The z.ai key-type toggle (prototype `.seg`): a two-segment capsule, the
|
|
/// selected segment filled with the foreground ink. Toggling swaps the
|
|
/// endpoint family (the model guards a custom endpoint).
|
|
private struct KeyTypeSegment: View {
|
|
let model: SettingsModel
|
|
|
|
var body: some View {
|
|
HStack(spacing: 0) {
|
|
ForEach(ZaiKeyType.allCases, id: \.self) { type in
|
|
let selected = model.zaiKeyType == type
|
|
Button {
|
|
model.setZaiKeyType(type)
|
|
} label: {
|
|
Text(type.label)
|
|
.font(.system(size: 11.5))
|
|
.padding(.horizontal, 10)
|
|
.padding(.vertical, 4)
|
|
.foregroundStyle(selected ? DesignTokens.background : DesignTokens.muted)
|
|
.background {
|
|
if selected {
|
|
RoundedRectangle(cornerRadius: 6)
|
|
.fill(DesignTokens.foreground)
|
|
.padding(1)
|
|
}
|
|
}
|
|
}
|
|
.buttonStyle(.plain)
|
|
.accessibilityAddTraits(selected ? [.isSelected] : [])
|
|
}
|
|
}
|
|
.background(
|
|
RoundedRectangle(cornerRadius: 7)
|
|
.fill(DesignTokens.surface)
|
|
.overlay(RoundedRectangle(cornerRadius: 7).strokeBorder(DesignTokens.border)),
|
|
)
|
|
.accessibilityLabel("z.ai key type")
|
|
}
|
|
}
|