// 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.52.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) }