// Package scholar provides a zero-dependency Go SDK for Commons Scholar // (https://scholar.askdavidc.ai): 7-Axis Ask, L0 Competence, OpenAI /v1 shim, // MCP/Toolpacks, Google/LF A2A 1.0, ACP, Forge, Ontology-Taxonomy-KG, and // Post-Transcript Multimodal Video Moat endpoints. package scholar import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "net/url" "strings" "time" ) const DefaultBaseURL = "https://scholar.askdavidc.ai" type Client struct { BaseURL string BearerToken string HTTPClient *http.Client } func NewClient(baseURL string) *Client { if strings.TrimSpace(baseURL) == "" { baseURL = DefaultBaseURL } return &Client{ BaseURL: strings.TrimRight(baseURL, "/"), HTTPClient: &http.Client{Timeout: 30 * time.Second}, } } func (c *Client) GetJSON(ctx context.Context, route string, params map[string]string, out any) error { if !strings.HasPrefix(route, "/") { route = "/" + route } u, err := url.Parse(c.BaseURL + route) if err != nil { return err } q := u.Query() for k, v := range params { if v != "" { q.Set(k, v) } } u.RawQuery = q.Encode() req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) if err != nil { return err } return c.doJSON(req, out) } func (c *Client) PostJSON(ctx context.Context, route string, body any, out any) error { if !strings.HasPrefix(route, "/") { route = "/" + route } payload, err := json.Marshal(body) if err != nil { return err } req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.BaseURL+route, bytes.NewReader(payload)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") return c.doJSON(req, out) } func (c *Client) doJSON(req *http.Request, out any) error { req.Header.Set("Accept", "application/json") req.Header.Set("User-Agent", "commons-scholar-go/0.67.0") if c.BearerToken != "" { req.Header.Set("Authorization", "Bearer "+c.BearerToken) } hc := c.HTTPClient if hc == nil { hc = http.DefaultClient } resp, err := hc.Do(req) if err != nil { return err } defer resp.Body.Close() raw, err := io.ReadAll(resp.Body) if err != nil { return err } if resp.StatusCode < 200 || resp.StatusCode >= 300 { snippet := string(raw) if len(snippet) > 200 { snippet = snippet[:200] } return fmt.Errorf("commons scholar HTTP %d: %s", resp.StatusCode, snippet) } if out != nil && len(raw) > 0 { return json.Unmarshal(raw, out) } return nil } func (c *Client) Ask(ctx context.Context, query string, top int) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/ask", map[string]string{"q": query, "top": fmt.Sprintf("%d", top)}, &res) return res, err } func (c *Client) L0Context(ctx context.Context, task string) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/ecosystem/agent-context", map[string]string{"task": task}, &res) return res, err } func (c *Client) ChatCompletions(ctx context.Context, model, prompt string) (map[string]any, error) { if model == "" { model = "scholar-grounded" } body := map[string]any{ "model": model, "messages": []map[string]string{{"role": "user", "content": prompt}}, "stream": false, } var res map[string]any err := c.PostJSON(ctx, "/v1/chat/completions", body, &res) return res, err } func (c *Client) A2ASend(ctx context.Context, text, skillID string) (map[string]any, error) { if skillID == "" { skillID = "commons_ask" } body := map[string]any{ "jsonrpc": "2.0", "id": 1, "method": "message/send", "params": map[string]any{ "skillId": skillID, "message": map[string]any{ "role": "user", "parts": []map[string]string{{"kind": "text", "text": text}}, }, }, } var res map[string]any err := c.PostJSON(ctx, "/a2a", body, &res) return res, err } func (c *Client) ACPRun(ctx context.Context, prompt string) (map[string]any, error) { body := map[string]any{ "agent_name": "commons-scholar", "input": []map[string]any{ {"role": "user", "parts": []map[string]string{{"content_type": "text/plain", "content": prompt}}}, }, } var res map[string]any err := c.PostJSON(ctx, "/acp/runs", body, &res) return res, err } func (c *Client) InvokeTool(ctx context.Context, name string, arguments map[string]any) (map[string]any, error) { if arguments == nil { arguments = map[string]any{} } var res map[string]any err := c.PostJSON(ctx, "/api/tools/invoke", map[string]any{"name": name, "arguments": arguments}, &res) return res, err } func (c *Client) Reference(ctx context.Context, slug, query string, customBody any) (map[string]any, error) { var res map[string]any route := "/api/reference/" + strings.Trim(slug, "/") if customBody != nil { err := c.PostJSON(ctx, route, customBody, &res) return res, err } err := c.GetJSON(ctx, route, map[string]string{"q": query}, &res) return res, err } func (c *Client) ScholarBench(ctx context.Context, query string, customBody any) (map[string]any, error) { return c.Reference(ctx, "scholar-bench", query, customBody) } func (c *Client) ScholarBenchLeaderboard(ctx context.Context, query string) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/reference/scholar-bench", map[string]string{"mode": "leaderboard", "q": query}, &res) return res, err } func (c *Client) WaldenLineage(ctx context.Context, query string, customBody any) (map[string]any, error) { return c.Reference(ctx, "walden-lineage", query, customBody) } func (c *Client) SdkRegistry(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/reference/sdk-registry", nil, &res) return res, err } func (c *Client) TufazipCodec(ctx context.Context, query string, customBody any) (map[string]any, error) { return c.Reference(ctx, "tufazip-codec", query, customBody) } func (c *Client) TrainingCommons(ctx context.Context, query string, customBody any) (map[string]any, error) { return c.Reference(ctx, "training-commons", query, customBody) } func (c *Client) PriorArt(ctx context.Context, query string, n int) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/prior-art", map[string]string{"q": query, "n": fmt.Sprintf("%d", n)}, &res) return res, err } func (c *Client) TruthLockAudit(ctx context.Context, refs string) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/truth-lock/audit", map[string]any{"refs": refs}, &res) return res, err } func (c *Client) Channels(ctx context.Context, query string) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/channels", map[string]string{"q": query}, &res) return res, err } func (c *Client) Channel(ctx context.Context, slug, query string, limit int) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/channels/"+strings.Trim(slug, "/"), map[string]string{"q": query, "limit": fmt.Sprintf("%d", limit)}, &res) return res, err } func (c *Client) Watch(ctx context.Context, mediaID, channelSlug string) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/watch", map[string]string{"v": mediaID, "ch": channelSlug}, &res) return res, err } func (c *Client) JouleDbDistinction(ctx context.Context, query string, top int) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/jouledb/distinction", map[string]string{"q": query, "top": fmt.Sprintf("%d", top)}, &res) return res, err } func (c *Client) DistinctionReceipt(ctx context.Context, query string, top int) (map[string]any, error) { if top <= 0 { top = 8 } var res map[string]any err := c.GetJSON(ctx, "/api/distinction-receipt", map[string]string{"q": query, "top": fmt.Sprintf("%d", top)}, &res) return res, err } func (c *Client) PricePerDistinction(ctx context.Context, query string, top int) (map[string]any, error) { return c.DistinctionReceipt(ctx, query, top) } func (c *Client) Capsules(ctx context.Context, ecosystem string) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/capsules", map[string]string{"ecosystem": ecosystem}, &res) return res, err } func (c *Client) Identity(ctx context.Context, handle string) (map[string]any, error) { var res map[string]any clean := strings.TrimLeft(strings.TrimSpace(handle), "@") err := c.GetJSON(ctx, "/api/identity/"+clean, nil, &res) return res, err } func (c *Client) PublishCapsule(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/publish/capsule", payload, &res) return res, err } func (c *Client) PublishDisclosure(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/publish/disclosure", payload, &res) return res, err } func (c *Client) PublishFeed(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/publish/feed", payload, &res) return res, err } func (c *Client) CodingNucleus(ctx context.Context, stratum string, full bool) (map[string]any, error) { var res map[string]any params := map[string]string{} if stratum != "" { params["stratum"] = stratum } if full { params["full"] = "1" } err := c.GetJSON(ctx, "/api/nucleus", params, &res) return res, err } func (c *Client) NucleusDecompose(ctx context.Context, code, lang string) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/nucleus/decompose", map[string]any{"code": code, "lang": lang}, &res) return res, err } func (c *Client) NucleusSweep(ctx context.Context, files []map[string]string) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/nucleus/sweep", map[string]any{"files": files}, &res) return res, err } func (c *Client) NucleusRepair(ctx context.Context, files []map[string]string) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/nucleus/repair", map[string]any{"files": files}, &res) return res, err } func (c *Client) LegacyLift(ctx context.Context, code, dialect string) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/nucleus/legacy-lift", map[string]any{"code": code, "dialect": dialect}, &res) return res, err } func (c *Client) SoftwareFactory(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/factory", nil, &res) return res, err } func (c *Client) SoftwareFactoryRun(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/factory/run", payload, &res) return res, err } func (c *Client) PostGitVcs(ctx context.Context, tribe string) (map[string]any, error) { var res map[string]any var params map[string]string if tribe != "" { params = map[string]string{"tribe": tribe} } err := c.GetJSON(ctx, "/api/vcs", params, &res) return res, err } func (c *Client) PostGitVcsSimulate(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/vcs/simulate", payload, &res) return res, err } func (c *Client) KeplerZeroLlm(ctx context.Context, era string) (map[string]any, error) { var res map[string]any var params map[string]string if era != "" { params = map[string]string{"era": era} } err := c.GetJSON(ctx, "/api/kepler", params, &res) return res, err } func (c *Client) KeplerSynthesize(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/kepler/synthesize", payload, &res) return res, err } func (c *Client) OmniCleanroom(ctx context.Context, family string) (map[string]any, error) { var res map[string]any var params map[string]string if family != "" { params = map[string]string{"family": family} } err := c.GetJSON(ctx, "/api/omni", params, &res) return res, err } func (c *Client) OmniCompile(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/omni/compile", payload, &res) return res, err } func (c *Client) AppBlueprint(ctx context.Context, camp string) (map[string]any, error) { var res map[string]any var params map[string]string if camp != "" { params = map[string]string{"camp": camp} } err := c.GetJSON(ctx, "/api/blueprint", params, &res) return res, err } func (c *Client) AppBlueprintCompile(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/blueprint/compile", payload, &res) return res, err } func (c *Client) FlightSre(ctx context.Context, camp string) (map[string]any, error) { var res map[string]any var params map[string]string if camp != "" { params = map[string]string{"camp": camp} } err := c.GetJSON(ctx, "/api/flight", params, &res) return res, err } func (c *Client) FlightSimulate(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/flight/simulate", payload, &res) return res, err } func (c *Client) SurfaceSync(ctx context.Context, camp string) (map[string]any, error) { var res map[string]any var params map[string]string if camp != "" { params = map[string]string{"camp": camp} } err := c.GetJSON(ctx, "/api/surface", params, &res) return res, err } func (c *Client) SurfaceCompile(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/surface/compile", payload, &res) return res, err } func (c *Client) FormCleanroom(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/form", nil, &res) return res, err } func (c *Client) FormVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/form/verify", payload, &res) return res, err } func (c *Client) HourglassV1(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/hourglass", nil, &res) return res, err } func (c *Client) HourglassVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/hourglass/verify", payload, &res) return res, err } func (c *Client) CospanInterfaces(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/cospan", nil, &res) return res, err } func (c *Client) CospanVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/cospan/verify", payload, &res) return res, err } func (c *Client) FabricR1R5(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/fabric", nil, &res) return res, err } func (c *Client) FabricVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/fabric/verify", payload, &res) return res, err } func (c *Client) OpenStandards(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/standards", nil, &res) return res, err } func (c *Client) StandardsVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/standards/verify", payload, &res) return res, err } func (c *Client) Substrate24(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/substrate", nil, &res) return res, err } func (c *Client) SubstrateVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/substrate/verify", payload, &res) return res, err } func (c *Client) LiquidSilicon(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/silicon", nil, &res) return res, err } func (c *Client) LiquidSiliconVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/silicon/verify", payload, &res) return res, err } func (c *Client) QuantumSpice(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/quantum", nil, &res) return res, err } func (c *Client) QuantumVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/quantum/verify", payload, &res) return res, err } func (c *Client) FormulaicDreaming(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/dreaming", nil, &res) return res, err } func (c *Client) DreamingVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/dreaming/verify", payload, &res) return res, err } func (c *Client) UcbBonding(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/bonding", nil, &res) return res, err } func (c *Client) BondingVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/bonding/verify", payload, &res) return res, err } func (c *Client) UcbThreebody(ctx context.Context) (map[string]any, error) { var res map[string]any err := c.GetJSON(ctx, "/api/threebody", nil, &res) return res, err } func (c *Client) ThreebodyVerify(ctx context.Context, payload map[string]any) (map[string]any, error) { var res map[string]any err := c.PostJSON(ctx, "/api/threebody/verify", payload, &res) return res, err }