feat(settings): add Nous Portal provider (#175)
CI / ci (push) Canceled after 0s

Settings gains hermes' `nous` slug as a fourth provider on every
surface and the Provider Keys page — endpoint
https://inference-api.nousresearch.com/v1, NOUS_API_KEY, the
5-level OpenAI-compatible effort ladder, switch flagship
anthropic/claude-sonnet-5 (live-probed: the portal's docs-listed
Hermes-4 ids 404 — the inference API serves the OpenRouter catalog,
so the switch keeps the app's sonnet posture billed through Nous).
The probe speaks plain OpenAI chat-completions; an unkeyed test
borrows the shared Portal session's agent_key (the engine's own
credential — hermes' nous runtime is OAuth-only, and a portal key
and subscription bill the same account credits), gated to the
official https endpoint so the implicit credential never rides a
user-edited URL. Pass readiness rides the same fallback, so a
logged-in machine runs passes with no key. Suite 635.

Closes #175
This commit is contained in:
AlexKucera committed 2026-11-05 15:59:41 +01:00
1 parent d305f822aa
commit b2db859887
15 files changed
+410 -30

No files matched your search

+82 -11
View File
@@ -23,19 +23,21 @@
import Foundation
import Observation
/// The three providers Settings offers (#135). Raw values are Hermes' own
/// provider slugs (`CANONICAL_PROVIDERS`) — the same ids key the registry
/// cache; OpenAI's slug is `openai-api`.
/// The providers Settings offers (#135; Nous Portal #175). Raw values are
/// Hermes' own provider slugs (`CANONICAL_PROVIDERS`) — the same ids key
/// the registry cache; OpenAI's slug is `openai-api`.
nonisolated enum ModelProvider: String, CaseIterable, Sendable {
case zai
case anthropic
case openaiAPI = "openai-api"
case nous
var label: String {
switch self {
case .zai: "z.ai"
case .anthropic: "Anthropic"
case .openaiAPI: "OpenAI"
case .nous: "Nous Portal"
}
}
@@ -44,39 +46,47 @@ nonisolated enum ModelProvider: String, CaseIterable, Sendable {
var effortOptions: [ReasoningEffort] {
switch self {
case .zai: [.low, .high, .max]
case .anthropic, .openaiAPI: [.low, .medium, .high, .xhigh, .max]
case .anthropic, .openaiAPI, .nous: [.low, .medium, .high, .xhigh, .max]
}
}
/// The model a provider switch lands on (prototype #130's pinned
/// flagships — registry order is capability-sorted, not default-sorted;
/// #136 dogfood: OpenAI's flagship is gpt-6.1-sol, the current
/// Responses-era default).
/// Responses-era default). #175: the portal's catalog is the
/// vendor-prefixed router list — the switch keeps the app's sonnet
/// posture (live-probed: `anthropic/claude-sonnet-5` generates).
var switchDefaultModel: String {
switch self {
case .zai: "glm-5.3"
case .anthropic: "claude-sonnet-5"
case .openaiAPI: "gpt-6.1-sol"
case .nous: "anthropic/claude-sonnet-5"
}
}
/// Default endpoint families (factsheet §1; z.ai's is key-type keyed).
/// #175: the Nous Portal inference API (portal.nousresearch.com/api-docs).
var defaultEndpoint: String {
switch self {
case .zai: ZaiKeyType.standard.endpoint
case .anthropic: "https://api.anthropic.com"
case .openaiAPI: "https://api.openai.com/v1"
case .nous: "https://inference-api.nousresearch.com/v1"
}
}
/// #136: the `.env` var Hermes' own provider registry maps each slug to
/// (`hermes_cli/auth.py` live-read): zai→GLM_API_KEY (ZAI_/Z_AI_ are
/// aliases), anthropic→ANTHROPIC_API_KEY, openai-api→OPENAI_API_KEY.
/// aliases), anthropic→ANTHROPIC_API_KEY, openai-api→OPENAI_API_KEY,
/// nous→NOUS_API_KEY (the plugin profile's env var — the engine's model
/// auth rides the Portal login, the key bills the same account credits).
var envVarName: String {
switch self {
case .zai: "GLM_API_KEY"
case .anthropic: "ANTHROPIC_API_KEY"
case .openaiAPI: "OPENAI_API_KEY"
case .nous: "NOUS_API_KEY"
}
}
}
@@ -134,6 +144,31 @@ nonisolated struct ProviderKeyConfig: Equatable, Sendable {
var apiKey: String = ""
}
/// #175: the shared Portal session's inference key from the Hermes auth
/// store (`~/.hermes/auth.json` — where the device-code login's agent_key
/// lives; profiles inherit it via the shared nous state). This is the
/// credential the engine itself runs nous on (API key and subscription
/// bill the same account credits). Read-only and lenient: a missing or
/// foreign-shaped store is nil, never an error. Nonisolated so the model's
/// fallback can read it off-main.
/// ponytail: agent_key_expires_at is not checked — a stale key probes
/// honestly (the portal's 401 decodes authFailed); parse the expiry if a
/// stale-key verdict ever misleads.
nonisolated enum PortalAuth {
static let defaultAuthPath = ("~/.hermes/auth.json" as NSString).expandingTildeInPath
static func sessionKey(authJSONPath: String = defaultAuthPath) -> String? {
guard let data = try? Data(contentsOf: URL(fileURLWithPath: authJSONPath)),
let parsed = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
let providers = parsed["providers"] as? [String: Any],
let nous = providers["nous"] as? [String: Any],
let agentKey = (nous["agent_key"] as? String)?.trimmingCharacters(in: .whitespacesAndNewlines),
!agentKey.isEmpty
else { return nil }
return agentKey
}
}
/// The Settings page's state (#135): per-surface model configs, per-provider
/// keys, the registry the model combobox reads, and the write path every
/// committed change flows through. #136: hydrated from the profiles at pane
@@ -152,6 +187,10 @@ final class SettingsModel {
private(set) var testStates: [ModelProvider: TestState] = [:]
private let writer: any SettingsWriting
/// #175: injected so the nous Test Connection tests run offline.
private let probeTransport: ProviderProbe.Transport
/// #175: the shared Hermes auth store — the nous fallback's agent_key.
private let portalAuthPath: String
/// #137 (S4): fired after a SUCCESSFUL chat-surface write ONLY — the
/// resident chat engine restarts to read the new model block
/// (WorkspaceView wires it to MentorSessionModel.requestRestart).
@@ -170,10 +209,14 @@ final class SettingsModel {
writer: any SettingsWriting = NoopSettingsWriter(),
surfaces: [ProfileSurface: SurfaceModelConfig]? = nil,
writeDebounce: TimeInterval = 0.35,
probeTransport: @escaping ProviderProbe.Transport = ProviderProbe.urlSessionTransport,
portalAuthPath: String = PortalAuth.defaultAuthPath,
) {
self.registry = registry
self.writer = writer
self.writeDebounce = writeDebounce
self.probeTransport = probeTransport
self.portalAuthPath = portalAuthPath
self.surfaces = surfaces
?? Dictionary(uniqueKeysWithValues: ProfileSurface.allCases.map { ($0, .surfaceDefault) })
keys = Dictionary(
@@ -324,18 +367,46 @@ final class SettingsModel {
/// One probe per provider over the CURRENT key/endpoint state. Guarded
/// against stacking; the empty-key precondition makes an unkeyed probe
/// fail fast without touching the network.
/// fail fast without touching the network. #175: an unkeyed NOUS probe
/// falls back to the shared Portal session key — the engine's own
/// credential — and names the login path when neither exists.
func testConnection(for provider: ModelProvider) async {
guard testStates[provider] != .running else { return }
testStates[provider] = .running
let keyConfig = keyConfig(for: provider)
var key = keyConfig(for: provider).apiKey
if provider == .nous, key.isEmpty {
// Finish-audit #175: the session agent_key is an IMPLICIT
// credential (never pasted, field reads empty) — it only ever
// rides the official https endpoint, never a hand-edited one
// (cleartext http / foreign host = silent exfiltration with no
// on-screen cue). Custom endpoints get the paste-a-key refusal.
let endpoint = keyConfig(for: .nous).endpoint
let trimmed = endpoint.hasSuffix("/") ? String(endpoint.dropLast()) : endpoint
guard trimmed == ModelProvider.nous.defaultEndpoint else {
testStates[provider] = .done(.authFailed(
"custom endpoint — paste an API key to test it (the Portal session key stays on the official endpoint)",
))
return
}
let path = portalAuthPath
key = await Task.detached(priority: .utility) {
PortalAuth.sessionKey(authJSONPath: path) ?? ""
}.value
if key.isEmpty {
testStates[provider] = .done(.authFailed(
"no key set and no Hermes Portal login — paste a key or run `hermes auth add nous`",
))
return
}
}
let config = ProviderProbe.Config(
provider: provider,
endpoint: keyConfig.endpoint,
apiKey: keyConfig.apiKey,
endpoint: keyConfig(for: provider).endpoint,
apiKey: key,
model: probeModel(for: provider),
)
testStates[provider] = await .done(ProviderProbe.probe(config))
let transport = probeTransport
testStates[provider] = await .done(ProviderProbe.probe(config, transport: transport))
}
/// The model the probe proves generation on: the first surface on this