61 lines
2.0 KiB
Swift
61 lines
2.0 KiB
Swift
import AppKit
|
|
import CodexBarCore
|
|
import Foundation
|
|
import SwiftUI
|
|
|
|
struct ZaiProviderImplementation: ProviderImplementation {
|
|
let id: UsageProvider = .zai
|
|
|
|
@MainActor
|
|
func presentation(context _: ProviderPresentationContext) -> ProviderPresentation {
|
|
ProviderPresentation { _ in "api" }
|
|
}
|
|
|
|
@MainActor
|
|
func observeSettings(_ settings: SettingsStore) {
|
|
_ = settings.zaiAPIToken
|
|
_ = settings.zaiAPIRegion
|
|
}
|
|
|
|
@MainActor
|
|
func settingsSnapshot(context: ProviderSettingsSnapshotContext) -> ProviderSettingsSnapshotContribution? {
|
|
.zai(context.settings.zaiSettingsSnapshot(tokenOverride: context.tokenOverride))
|
|
}
|
|
|
|
@MainActor
|
|
func isAvailable(context: ProviderAvailabilityContext) -> Bool {
|
|
if ZaiSettingsReader.apiToken(environment: context.environment) != nil {
|
|
return true
|
|
}
|
|
context.settings.ensureZaiAPITokenLoaded()
|
|
return !context.settings.zaiAPIToken.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
}
|
|
|
|
@MainActor
|
|
func settingsPickers(context: ProviderSettingsContext) -> [ProviderSettingsPickerDescriptor] {
|
|
let binding = Binding(
|
|
get: { context.settings.zaiAPIRegion.rawValue },
|
|
set: { raw in
|
|
context.settings.zaiAPIRegion = ZaiAPIRegion(rawValue: raw) ?? .global
|
|
})
|
|
let options = ZaiAPIRegion.allCases.map {
|
|
ProviderSettingsPickerOption(id: $0.rawValue, title: $0.displayName)
|
|
}
|
|
return [
|
|
ProviderSettingsPickerDescriptor(
|
|
id: "zai-api-region",
|
|
title: "API region",
|
|
subtitle: "Use BigModel for the China mainland endpoints (open.bigmodel.cn).",
|
|
binding: binding,
|
|
options: options,
|
|
isVisible: nil,
|
|
onChange: nil),
|
|
]
|
|
}
|
|
|
|
@MainActor
|
|
func settingsFields(context _: ProviderSettingsContext) -> [ProviderSettingsFieldDescriptor] {
|
|
[]
|
|
}
|
|
}
|