// CommonsScholarClient.java — Zero-Dependency Native JVM (Java 11+ / Kotlin / Scala) SDK v0.67.0 package ai.openie.scholar; import java.io.IOException; import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.time.Duration; public final class CommonsScholarClient { public static final String SDK_VERSION = "0.67.0"; private final String baseUrl; private final HttpClient http; public CommonsScholarClient(String baseUrl) { String clean = (baseUrl == null || baseUrl.isBlank()) ? "https://scholar.askdavidc.ai" : baseUrl.trim(); while (clean.endsWith("/")) clean = clean.substring(0, clean.length() - 1); this.baseUrl = clean; this.http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(15)).build(); } public String getRaw(String routeWithQuery) throws IOException, InterruptedException { String p = routeWithQuery.startsWith("/") ? routeWithQuery : "/" + routeWithQuery; HttpRequest req = HttpRequest.newBuilder(URI.create(baseUrl + p)) .header("Accept", "application/json") .header("User-Agent", "commons-scholar-jvm/0.67.0") .GET() .build(); HttpResponse resp = http.send(req, HttpResponse.BodyHandlers.ofString()); if (resp.statusCode() < 200 || resp.statusCode() >= 300) { throw new IOException("Commons Scholar HTTP " + resp.statusCode() + ": " + resp.body()); } return resp.body(); } public String postRaw(String route, String jsonBody) throws IOException, InterruptedException { String p = route.startsWith("/") ? route : "/" + route; HttpRequest req = HttpRequest.newBuilder(URI.create(baseUrl + p)) .header("Accept", "application/json") .header("Content-Type", "application/json") .header("User-Agent", "commons-scholar-jvm/0.67.0") .POST(HttpRequest.BodyPublishers.ofString(jsonBody, StandardCharsets.UTF_8)) .build(); HttpResponse resp = http.send(req, HttpResponse.BodyHandlers.ofString()); if (resp.statusCode() < 200 || resp.statusCode() >= 300) { throw new IOException("Commons Scholar HTTP " + resp.statusCode() + ": " + resp.body()); } return resp.body(); } public String askRaw(String query, int top) throws IOException, InterruptedException { String q = URLEncoder.encode(query, StandardCharsets.UTF_8); return getRaw("/api/ask?q=" + q + "&top=" + top); } public String referenceRaw(String slug, String query) throws IOException, InterruptedException { String cleanSlug = slug.replaceAll("^/+|/+$", ""); String q = URLEncoder.encode(query == null ? "" : query, StandardCharsets.UTF_8); return getRaw("/api/reference/" + cleanSlug + "?q=" + q); } public String scholarBenchRaw(String query, boolean leaderboard) throws IOException, InterruptedException { String q = URLEncoder.encode(query == null ? "" : query, StandardCharsets.UTF_8); if (leaderboard) { return getRaw("/api/reference/scholar-bench?mode=leaderboard&q=" + q); } return referenceRaw("scholar-bench", query); } public String waldenLineageRaw(String query) throws IOException, InterruptedException { return referenceRaw("walden-lineage", query); } public String tufazipCodecRaw(String query) throws IOException, InterruptedException { return referenceRaw("tufazip-codec", query); } public String wai1TufaArchiveRaw(String query) throws IOException, InterruptedException { return referenceRaw("wai1-tufa-archive", query); } public String multimodalMdlSchedulerRaw(String query) throws IOException, InterruptedException { return referenceRaw("multimodal-mdl-scheduler", query); } public String trainingCommonsRaw(String query) throws IOException, InterruptedException { return referenceRaw("training-commons", query); } public String sdkRegistryRaw() throws IOException, InterruptedException { return referenceRaw("sdk-registry", ""); } public String priorArtRaw(String query, int n) throws IOException, InterruptedException { String q = URLEncoder.encode(query == null ? "" : query, StandardCharsets.UTF_8); return getRaw("/api/prior-art?q=" + q + "&n=" + n); } public String channelsRaw(String slug) throws IOException, InterruptedException { String clean = slug == null ? "" : slug.replaceAll("^/+|/+$", ""); return clean.isEmpty() ? getRaw("/api/channels") : getRaw("/api/channels/" + clean); } public String jouleDbDistinctionRaw(String query, int top) throws IOException, InterruptedException { String q = URLEncoder.encode(query == null ? "" : query, StandardCharsets.UTF_8); return getRaw("/api/jouledb/distinction?q=" + q + "&top=" + top); } public String distinctionReceiptRaw(String query, int top) throws IOException, InterruptedException { String q = URLEncoder.encode(query == null ? "" : query, StandardCharsets.UTF_8); int effTop = top > 0 ? top : 8; return getRaw("/api/distinction-receipt?q=" + q + "&top=" + effTop); } public String pricePerDistinctionRaw(String query, int top) throws IOException, InterruptedException { return distinctionReceiptRaw(query, top); } public String capsulesRaw(String ecosystem) throws IOException, InterruptedException { String e = URLEncoder.encode(ecosystem == null ? "" : ecosystem, StandardCharsets.UTF_8); return getRaw("/api/capsules?ecosystem=" + e); } public String codingNucleusRaw(String stratum) throws IOException, InterruptedException { String s = URLEncoder.encode(stratum == null ? "" : stratum, StandardCharsets.UTF_8); return s.isEmpty() ? getRaw("/api/nucleus") : getRaw("/api/nucleus?stratum=" + s); } public String nucleusDecomposeRaw(String code, String lang) throws IOException, InterruptedException { String c = URLEncoder.encode(code == null ? "" : code, StandardCharsets.UTF_8); String l = URLEncoder.encode(lang == null || lang.isEmpty() ? "rust" : lang, StandardCharsets.UTF_8); return getRaw("/api/nucleus/decompose?code=" + c + "&lang=" + l); } public String softwareFactoryRaw() throws IOException, InterruptedException { return getRaw("/api/factory"); } public String legacyLiftRaw(String code, String dialect) throws IOException, InterruptedException { String c = URLEncoder.encode(code == null ? "" : code, StandardCharsets.UTF_8); String d = URLEncoder.encode(dialect == null ? "" : dialect, StandardCharsets.UTF_8); return getRaw("/api/nucleus/legacy-lift?code=" + c + "&dialect=" + d); } public String postGitVcsRaw(String tribe) throws IOException, InterruptedException { String t = URLEncoder.encode(tribe == null ? "" : tribe, StandardCharsets.UTF_8); return t.isEmpty() ? getRaw("/api/vcs") : getRaw("/api/vcs?tribe=" + t); } public String postGitVcsSimulateRaw(String op) throws IOException, InterruptedException { String o = URLEncoder.encode(op == null || op.isEmpty() ? "merge" : op, StandardCharsets.UTF_8); return getRaw("/api/vcs/simulate?op=" + o); } public String keplerZeroLlmRaw(String era) throws IOException, InterruptedException { String e = URLEncoder.encode(era == null ? "" : era, StandardCharsets.UTF_8); return e.isEmpty() ? getRaw("/api/kepler") : getRaw("/api/kepler?era=" + e); } public String keplerSynthesizeRaw(String symbol) throws IOException, InterruptedException { String s = URLEncoder.encode(symbol == null || symbol.isEmpty() ? "rail_pd_500hz" : symbol, StandardCharsets.UTF_8); return getRaw("/api/kepler/synthesize?symbol=" + s); } public String omniCleanroomRaw(String family) throws IOException, InterruptedException { String f = URLEncoder.encode(family == null ? "" : family, StandardCharsets.UTF_8); return f.isEmpty() ? getRaw("/api/omni") : getRaw("/api/omni?family=" + f); } public String omniCompileRaw(String kernel) throws IOException, InterruptedException { String k = URLEncoder.encode(kernel == null || kernel.isEmpty() ? "k0-fma-dot" : kernel, StandardCharsets.UTF_8); return getRaw("/api/omni/compile?kernel=" + k); } public String appBlueprintRaw(String camp) throws IOException, InterruptedException { String c = URLEncoder.encode(camp == null ? "" : camp, StandardCharsets.UTF_8); return c.isEmpty() ? getRaw("/api/blueprint") : getRaw("/api/blueprint?camp=" + c); } public String appBlueprintCompileRaw(String blueprint) throws IOException, InterruptedException { String b = URLEncoder.encode(blueprint == null || blueprint.isEmpty() ? "b0-clinical-trial-ledger" : blueprint, StandardCharsets.UTF_8); return getRaw("/api/blueprint/compile?blueprint=" + b); } public String flightSreRaw(String camp) throws IOException, InterruptedException { String c = URLEncoder.encode(camp == null ? "" : camp, StandardCharsets.UTF_8); return c.isEmpty() ? getRaw("/api/flight") : getRaw("/api/flight?camp=" + c); } public String flightSimulateRaw(String topology) throws IOException, InterruptedException { String t = URLEncoder.encode(topology == null || topology.isEmpty() ? "payment-webhook-saga" : topology, StandardCharsets.UTF_8); return getRaw("/api/flight/simulate?topology=" + t); } public String surfaceSyncRaw(String camp) throws IOException, InterruptedException { String c = URLEncoder.encode(camp == null ? "" : camp, StandardCharsets.UTF_8); return c.isEmpty() ? getRaw("/api/surface") : getRaw("/api/surface?camp=" + c); } public String surfaceCompileRaw(String surface) throws IOException, InterruptedException { String s = URLEncoder.encode(surface == null || surface.isEmpty() ? "s0-evidence-search-desk" : surface, StandardCharsets.UTF_8); return getRaw("/api/surface/compile?surface=" + s); } public String formCleanroomRaw() throws IOException, InterruptedException { return getRaw("/api/form"); } public String formVerifyRaw(String kernel) throws IOException, InterruptedException { String k = URLEncoder.encode(kernel == null || kernel.isEmpty() ? "k0-fma-dot" : kernel, StandardCharsets.UTF_8); return getRaw("/api/form/verify?kernel=" + k); } public String hourglassV1Raw() throws IOException, InterruptedException { return getRaw("/api/hourglass"); } public String hourglassVerifyRaw(String leanKernel) throws IOException, InterruptedException { String k = URLEncoder.encode(leanKernel == null || leanKernel.isEmpty() ? "k1-counter-bounded" : leanKernel, StandardCharsets.UTF_8); return getRaw("/api/hourglass/verify?lean_kernel=" + k); } public String cospanInterfacesRaw() throws IOException, InterruptedException { return getRaw("/api/cospan"); } public String cospanVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "mars-orbiter-lbfs" : preset, StandardCharsets.UTF_8); return getRaw("/api/cospan/verify?preset=" + p); } public String fabricR1R5Raw() throws IOException, InterruptedException { return getRaw("/api/fabric"); } public String fabricVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "rtos-1khz-servo" : preset, StandardCharsets.UTF_8); return getRaw("/api/fabric/verify?preset=" + p); } public String openStandardsRaw() throws IOException, InterruptedException { return getRaw("/api/standards"); } public String standardsVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-jcr1-s5-arl7" : preset, StandardCharsets.UTF_8); return getRaw("/api/standards/verify?preset=" + p); } public String substrate24Raw() throws IOException, InterruptedException { return getRaw("/api/substrate"); } public String substrateVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-24of24-jouleplan-pool-mesh" : preset, StandardCharsets.UTF_8); return getRaw("/api/substrate/verify?preset=" + p); } public String liquidSiliconRaw() throws IOException, InterruptedException { return getRaw("/api/silicon"); } public String liquidSiliconVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-9target-fpos-vault16-wai2" : preset, StandardCharsets.UTF_8); return getRaw("/api/silicon/verify?preset=" + p); } public String quantumSpiceRaw() throws IOException, InterruptedException { return getRaw("/api/quantum"); } public String quantumVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-lrat-qir-spice-trojan-moq" : preset, StandardCharsets.UTF_8); return getRaw("/api/quantum/verify?preset=" + p); } public String formulaicDreamingRaw() throws IOException, InterruptedException { return getRaw("/api/dreaming"); } public String dreamingVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-fdm6tier-pec-pauli-radhard-dma-dbsp" : preset, StandardCharsets.UTF_8); return getRaw("/api/dreaming/verify?preset=" + p); } public String ucbBondingRaw() throws IOException, InterruptedException { return getRaw("/api/bonding"); } public String bondingVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-bondword-arrive334-landauer40-accept24-qec" : preset, StandardCharsets.UTF_8); return getRaw("/api/bonding/verify?preset=" + p); } public String ucbThreebodyRaw() throws IOException, InterruptedException { return getRaw("/api/threebody"); } public String threebodyVerifyRaw(String preset) throws IOException, InterruptedException { String p = URLEncoder.encode(preset == null || preset.isEmpty() ? "clean-threebody-landauer-v3-weyl-perm-dct2-surf-350" : preset, StandardCharsets.UTF_8); return getRaw("/api/threebody/verify?preset=" + p); } }