Repository Public
1// SP Notes Core2// File: Sources/SPNotesFeatures/Assistant/ModelLogo.swift3// Description: The logos of model creators and API routes, bundled from the web registry (Scripts/generate-model-logos.sh →4// Resources/ModelLogos.xcassets + ModelLogosRegistry.json). Single-colour vector marks drawn as template5// images in the text colour; a creator without a shipped mark gets the neutral generic glyph with its6// monogram. The marks are trademarks of their owners (registry: simple-icons, CC0-1.0).7//8// Author: Simon-Pierre Boucher9// Contact: contact@spboucher.ai10import Foundation11import SPNotesDesign12import SwiftUI13#if canImport(AppKit)14import AppKit15#elseif canImport(UIKit)16import UIKit17#endif1819/// The bundled copy of `public/model-logos/registry.json`.20public struct ModelLogoRegistry: Sendable {21 public struct Entry: Decodable, Sendable, Equatable, Identifiable {22 public var id: String23 /// `creator` or `route`.24 public var kind: String25 public var name: String26 /// The SVG file (`anthropic.svg`); `generic.svg` when no mark is shipped.27 public var file: String28 public var generic: Bool29 /// Missing for routes.30 public var monogram: String?31 public var licence: String?32 public var note: String?33 }3435 private struct File: Decodable {36 var fallback: String37 var template: Bool?38 var generatedFrom: String?39 var entries: [Entry]40 }4142 public let entries: [Entry]43 /// The image name of the neutral fallback.44 public let fallbackImage: String45 /// Where the marks come from (`simple-icons@…`).46 public let source: String?47 private let byKey: [String: Entry]48 private let stems: Set<String>4950 /// The registry bundled with the library (empty when the resource is missing: everything falls back).51 public static let shared: ModelLogoRegistry = {52 guard let url = Bundle.module.url(forResource: "ModelLogosRegistry", withExtension: "json"), let data = try? Data(contentsOf: url) else {53 return ModelLogoRegistry(entries: [], fallback: "generic.svg", source: nil)54 }55 return (try? ModelLogoRegistry(data: data)) ?? ModelLogoRegistry(entries: [], fallback: "generic.svg", source: nil)56 }()5758 public init(data: Data) throws {59 let file = try JSONDecoder().decode(File.self, from: data)60 self.init(entries: file.entries, fallback: file.fallback, source: file.generatedFrom)61 }6263 init(entries: [Entry], fallback: String, source: String?) {64 self.entries = entries65 fallbackImage = Self.imageName(file: fallback)66 self.source = source67 byKey = Dictionary(entries.map { ($0.kind + ":" + $0.id.lowercased(), $0) }, uniquingKeysWith: { a, _ in a })68 stems = Set(entries.map { Self.stem($0.file) } + [Self.stem(fallback)])69 }7071 static func stem(_ file: String) -> String {72 let name = file.split(separator: "/").last.map(String.init) ?? file73 return name.hasSuffix(".svg") ? String(name.dropLast(4)) : name74 }7576 /// The asset-catalogue name of an SVG file of the registry.77 public static func imageName(file: String) -> String { "ModelLogo-" + stem(file) }7879 public func entry(id: String?, kind: String) -> Entry? {80 guard let id, !id.isEmpty else { return nil }81 return byKey[kind + ":" + id.lowercased()]82 }8384 /// What to draw for a creator or a route.85 /// - Parameters:86 /// - id: the creator / route id of the catalogue (`anthropic`, `openrouter`).87 /// - kind: `creator` or `route`.88 /// - logoPath: the server's `logo` path (`/model-logos/<file>.svg`): used when the id is not in the bundled registry89 /// but its file is (a creator added on the server that reuses a shipped mark).90 /// - monogram: the server's monogram, preferred over the registry's.91 public func resolve(id: String?, kind: String = "creator", logoPath: String? = nil, monogram: String? = nil) -> ModelLogo {92 let known = entry(id: id, kind: kind)93 let letters = monogram ?? known?.monogram ?? ModelLogo.monogram(from: known?.name ?? id)94 if let known, !known.generic { return ModelLogo(imageName: Self.imageName(file: known.file), isGeneric: false, monogram: letters, name: known.name) }95 if known == nil, let logoPath {96 let stem = Self.stem(logoPath)97 if stems.contains(stem), Self.imageName(file: stem) != fallbackImage { return ModelLogo(imageName: Self.imageName(file: stem), isGeneric: false, monogram: letters, name: id ?? "") }98 }99 return ModelLogo(imageName: fallbackImage, isGeneric: true, monogram: letters, name: known?.name ?? id ?? "")100 }101}102103/// A resolved logo: a bundled template image, and the monogram shown with the generic glyph.104public struct ModelLogo: Sendable, Equatable {105 public var imageName: String106 /// True = the neutral fallback (no mark shipped for this creator).107 public var isGeneric: Bool108 public var monogram: String109 public var name: String110111 static func monogram(from name: String?) -> String {112 let letters = (name ?? "").filter { $0.isLetter || $0.isNumber }113 return letters.isEmpty ? "AI" : String(letters.prefix(2)).uppercased()114 }115116 /// Whether the asset catalogue really contains the image (tests; a missing image would draw nothing).117 public var isBundled: Bool {118 #if canImport(AppKit)119 return Bundle.module.image(forResource: NSImage.Name(imageName)) != nil120 #elseif canImport(UIKit)121 return UIImage(named: imageName, in: .module, compatibleWith: nil) != nil122 #else123 return false124 #endif125 }126}127128/// The mark of a creator (or of a route) in a rounded tile. Decorative by default: the name is always written next to it.129public struct ModelLogoView: View {130 private let logo: ModelLogo131 private let size: CGFloat132 private let tint: Color133134 public init(_ logo: ModelLogo, size: CGFloat = 28, tint: Color = NVColor.fg) {135 self.logo = logo136 self.size = size137 self.tint = tint138 }139140 public init(creator: String?, logoPath: String? = nil, monogram: String? = nil, size: CGFloat = 28) {141 self.init(ModelLogoRegistry.shared.resolve(id: creator, kind: "creator", logoPath: logoPath, monogram: monogram), size: size)142 }143144 public init(route: String?, logoPath: String? = nil, size: CGFloat = 16) {145 self.init(ModelLogoRegistry.shared.resolve(id: route, kind: "route", logoPath: logoPath), size: size, tint: NVColor.fgMuted)146 }147148 public var body: some View {149 Group {150 if logo.isGeneric, size >= 24 {151 // Neutral fallback: the monogram, never somebody else's mark.152 Text(logo.monogram)153 .font(.system(size: size * 0.38, weight: .semibold, design: .rounded))154 .foregroundStyle(NVColor.fgMuted)155 } else {156 Image(logo.imageName, bundle: .module)157 .renderingMode(.template)158 .resizable()159 .scaledToFit()160 .foregroundStyle(logo.isGeneric ? NVColor.fgMuted : tint)161 .padding(size * 0.2)162 }163 }164 .frame(width: size, height: size)165 .background(NVColor.bgSunken, in: NVRadius.shape(size * 0.26))166 .overlay(NVRadius.shape(size * 0.26).strokeBorder(NVColor.line, lineWidth: 1))167 .accessibilityHidden(true)168 }169}170