// CommonsScholar.swift — Zero-Dependency Native Swift SDK v0.67.0 (macOS / iOS / visionOS / Linux) import Foundation #if canImport(FoundationNetworking) import FoundationNetworking #endif public typealias ScholarClient = CommonsScholarClient public struct CommonsScholarClient: Sendable { public static let sdkVersion = "0.67.0" public let baseURL: URL public let bearerToken: String? public init(baseURLString: String = "https://scholar.askdavidc.ai", bearerToken: String? = nil) { let trimmed = baseURLString.trimmingCharacters(in: CharacterSet(charactersIn: "/ ")) self.baseURL = URL(string: trimmed.isEmpty ? "https://scholar.askdavidc.ai" : trimmed)! self.bearerToken = bearerToken } public init(baseUrl: String, bearerToken: String? = nil) { self.init(baseURLString: baseUrl, bearerToken: bearerToken) } public func buildURL(route: String, queryItems: [URLQueryItem] = []) -> URL { let clean = route.hasPrefix("/") ? route : "/" + route var comps = URLComponents(url: baseURL.appendingPathComponent(String(clean.dropFirst())), resolvingAgainstBaseURL: false)! let nonEmpty = queryItems.filter { ($0.value ?? "").isEmpty == false } if !nonEmpty.isEmpty { comps.queryItems = nonEmpty } return comps.url! } public func getJSON(route: String, queryItems: [URLQueryItem] = []) async throws -> [String: Any] { var req = URLRequest(url: buildURL(route: route, queryItems: queryItems)) req.httpMethod = "GET" req.setValue("application/json", forHTTPHeaderField: "Accept") req.setValue("commons-scholar-swift/0.67.0", forHTTPHeaderField: "User-Agent") if let tok = bearerToken { req.setValue("Bearer \(tok)", forHTTPHeaderField: "Authorization") } let (data, _) = try await URLSession.shared.data(for: req) return (try JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:] } public func postJSON(route: String, payload: [String: Any]) async throws -> [String: Any] { var req = URLRequest(url: buildURL(route: route)) req.httpMethod = "POST" req.setValue("application/json", forHTTPHeaderField: "Accept") req.setValue("application/json", forHTTPHeaderField: "Content-Type") req.setValue("commons-scholar-swift/0.67.0", forHTTPHeaderField: "User-Agent") if let tok = bearerToken { req.setValue("Bearer \(tok)", forHTTPHeaderField: "Authorization") } req.httpBody = try JSONSerialization.data(withJSONObject: payload) let (data, _) = try await URLSession.shared.data(for: req) return (try JSONSerialization.jsonObject(with: data)) as? [String: Any] ?? [:] } public func ask(_ query: String, top: Int = 6) async throws -> [String: Any] { try await getJSON(route: "/api/ask", queryItems: [ URLQueryItem(name: "q", value: query), URLQueryItem(name: "top", value: String(top)), ]) } public func chatCompletions(_ prompt: String, model: String = "scholar-grounded") async throws -> [String: Any] { try await postJSON(route: "/v1/chat/completions", payload: [ "model": model, "messages": [["role": "user", "content": prompt]], "stream": false, ]) } public func a2aSend(_ text: String, skillID: String = "commons_ask") async throws -> [String: Any] { try await postJSON(route: "/a2a", payload: [ "jsonrpc": "2.0", "id": 1, "method": "message/send", "params": [ "skillId": skillID, "message": ["role": "user", "parts": [["kind": "text", "text": text]]], ], ]) } public func reference(_ slug: String, query: String = "", payload: [String: Any]? = nil) async throws -> [String: Any] { let cleanSlug = slug.trimmingCharacters(in: CharacterSet(charactersIn: "/")) let route = "/api/reference/\(cleanSlug)" if let body = payload { return try await postJSON(route: route, payload: body) } return try await getJSON(route: route, queryItems: [URLQueryItem(name: "q", value: query)]) } public func scholarBench(query: String = "", leaderboard: Bool = false) async throws -> [String: Any] { if leaderboard { return try await getJSON(route: "/api/reference/scholar-bench", queryItems: [ URLQueryItem(name: "mode", value: "leaderboard"), URLQueryItem(name: "q", value: query), ]) } return try await reference("scholar-bench", query: query) } public func waldenLineage(query: String = "") async throws -> [String: Any] { try await reference("walden-lineage", query: query) } public func tufazipCodec(query: String = "") async throws -> [String: Any] { try await reference("tufazip-codec", query: query) } public func wai1TufaArchive(query: String = "") async throws -> [String: Any] { try await reference("wai1-tufa-archive", query: query) } public func multimodalMdlScheduler(query: String = "") async throws -> [String: Any] { try await reference("multimodal-mdl-scheduler", query: query) } public func trainingCommons(query: String = "") async throws -> [String: Any] { try await reference("training-commons", query: query) } public func sdkRegistry() async throws -> [String: Any] { try await reference("sdk-registry") } public func stats() async throws -> [String: Any] { try await getJSON(route: "/api/stats") } public func priorArt(_ query: String, n: Int = 12) async throws -> [String: Any] { try await getJSON(route: "/api/prior-art", queryItems: [ URLQueryItem(name: "q", value: query), URLQueryItem(name: "n", value: String(n)), ]) } public func channels(slug: String = "") async throws -> [String: Any] { let clean = slug.trimmingCharacters(in: CharacterSet(charactersIn: "/")) return try await getJSON(route: clean.isEmpty ? "/api/channels" : "/api/channels/\(clean)") } public func jouledbDistinction(_ query: String, top: Int = 6) async throws -> [String: Any] { try await getJSON(route: "/api/jouledb/distinction", queryItems: [ URLQueryItem(name: "q", value: query), URLQueryItem(name: "top", value: String(top)), ]) } public func distinctionReceipt(_ query: String, top: Int = 8) async throws -> [String: Any] { try await getJSON(route: "/api/distinction-receipt", queryItems: [ URLQueryItem(name: "q", value: query), URLQueryItem(name: "top", value: String(top)), ]) } public func pricePerDistinction(_ query: String, top: Int = 8) async throws -> [String: Any] { try await distinctionReceipt(query, top: top) } public func capsules(ecosystem: String = "") async throws -> [String: Any] { try await getJSON(route: "/api/capsules", queryItems: [ URLQueryItem(name: "ecosystem", value: ecosystem), ]) } public func codingNucleus(stratum: String = "", full: Bool = false) async throws -> [String: Any] { var items: [URLQueryItem] = [] if !stratum.isEmpty { items.append(URLQueryItem(name: "stratum", value: stratum)) } if full { items.append(URLQueryItem(name: "full", value: "1")) } return try await getJSON(route: "/api/nucleus", queryItems: items) } public func nucleusDecompose(code: String, lang: String = "rust") async throws -> [String: Any] { try await postJSON(route: "/api/nucleus/decompose", payload: ["code": code, "lang": lang]) } public func softwareFactory() async throws -> [String: Any] { try await getJSON(route: "/api/factory") } public func legacyLift(code: String, dialect: String = "") async throws -> [String: Any] { try await postJSON(route: "/api/nucleus/legacy-lift", payload: ["code": code, "dialect": dialect]) } public func postGitVcs(tribe: String = "") async throws -> [String: Any] { var items: [URLQueryItem] = [] if !tribe.isEmpty { items.append(URLQueryItem(name: "tribe", value: tribe)) } return try await getJSON(route: "/api/vcs", queryItems: items) } public func postGitVcsSimulate(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/vcs/simulate", payload: payload) } public func keplerZeroLlm(era: String = "") async throws -> [String: Any] { var items: [URLQueryItem] = [] if !era.isEmpty { items.append(URLQueryItem(name: "era", value: era)) } return try await getJSON(route: "/api/kepler", queryItems: items) } public func keplerSynthesize(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/kepler/synthesize", payload: payload) } public func omniCleanroom(family: String = "") async throws -> [String: Any] { var items: [URLQueryItem] = [] if !family.isEmpty { items.append(URLQueryItem(name: "family", value: family)) } return try await getJSON(route: "/api/omni", queryItems: items) } public func omniCompile(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/omni/compile", payload: payload) } public func appBlueprint(camp: String = "") async throws -> [String: Any] { var items: [URLQueryItem] = [] if !camp.isEmpty { items.append(URLQueryItem(name: "camp", value: camp)) } return try await getJSON(route: "/api/blueprint", queryItems: items) } public func appBlueprintCompile(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/blueprint/compile", payload: payload) } public func flightSre(camp: String = "") async throws -> [String: Any] { var items: [URLQueryItem] = [] if !camp.isEmpty { items.append(URLQueryItem(name: "camp", value: camp)) } return try await getJSON(route: "/api/flight", queryItems: items) } public func flightSimulate(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/flight/simulate", payload: payload) } public func surfaceSync(camp: String = "") async throws -> [String: Any] { var items: [URLQueryItem] = [] if !camp.isEmpty { items.append(URLQueryItem(name: "camp", value: camp)) } return try await getJSON(route: "/api/surface", queryItems: items) } public func surfaceCompile(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/surface/compile", payload: payload) } public func formCleanroom() async throws -> [String: Any] { try await getJSON(route: "/api/form", queryItems: []) } public func formVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/form/verify", payload: payload) } public func hourglassV1() async throws -> [String: Any] { try await getJSON(route: "/api/hourglass", queryItems: []) } public func hourglassVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/hourglass/verify", payload: payload) } public func cospanInterfaces() async throws -> [String: Any] { try await getJSON(route: "/api/cospan", queryItems: []) } public func cospanVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/cospan/verify", payload: payload) } public func fabricR1R5() async throws -> [String: Any] { try await getJSON(route: "/api/fabric", queryItems: []) } public func fabricVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/fabric/verify", payload: payload) } public func openStandards() async throws -> [String: Any] { try await getJSON(route: "/api/standards", queryItems: []) } public func standardsVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/standards/verify", payload: payload) } public func substrate24() async throws -> [String: Any] { try await getJSON(route: "/api/substrate", queryItems: []) } public func substrateVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/substrate/verify", payload: payload) } public func liquidSilicon() async throws -> [String: Any] { try await getJSON(route: "/api/silicon", queryItems: []) } public func liquidSiliconVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/silicon/verify", payload: payload) } public func quantumSpice() async throws -> [String: Any] { try await getJSON(route: "/api/quantum", queryItems: []) } public func quantumVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/quantum/verify", payload: payload) } public func formulaicDreaming() async throws -> [String: Any] { try await getJSON(route: "/api/dreaming", queryItems: []) } public func dreamingVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/dreaming/verify", payload: payload) } public func ucbBonding() async throws -> [String: Any] { try await getJSON(route: "/api/bonding", queryItems: []) } public func bondingVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/bonding/verify", payload: payload) } public func ucbThreebody() async throws -> [String: Any] { try await getJSON(route: "/api/threebody", queryItems: []) } public func threebodyVerify(payload: [String: Any]) async throws -> [String: Any] { try await postJSON(route: "/api/threebody/verify", payload: payload) } }