scorpiox-bot-plugin-github
←gh.hc
166 lines · 7 KBHEADBlameDownload
1/*
2 * gh.h - shared declarations for plugin_github (SCORPIOX BOT open-source
3 * GitHub MCP connector).
4 *
5 * One binary, five modes, exactly as the plugin_<name> spec requires:
6 *
7 * 1. --meta / SX_PLUGIN_META=1 one line of discovery JSON (unauthenticated)
8 * {"name":"github","desc":"GitHub tools","kind":"mcp","mcp":true,
9 * "daemon":false,"menus":false,"verified":"mock"}
10 * 2. --mcp MCP JSON-RPC 2.0 over stdio with five tools:
11 * github_get_repo, github_list_issues,
12 * github_create_issue, github_get_pull_request,
13 * github_search_code
14 * 3. --schema the tools/list array (compat)
15 * 4. ?action=... | ?format=json the node-auth-gated JSON API the setup page
16 * talks to (status/save/test/tools/clear)
17 * 5. GET (default) the PAT + API-base setup page
18 *
19 * Files:
20 * plugin_github.c identity + tool table + main() + MCP/CGI dispatch
21 * gh_api.c the one outbound HTTP call (verified TLS via the
22 * vendored BearSSL transport in src/sx_http_tls.c) and
23 * the five REST mappings + result shaping
24 * gh_config.c connector dir, config.json at mode 0600, env resolution,
25 * growable buffer + helpers
26 * sx_connector.* the SCORPIOX BOT Connector SDK runtime (vendored)
27 * sx_http_tls.* HTTP/1.1 + TLS client (vendored BearSSL, flat tree in
28 * vendor/bearssl)
29 *
30 * Clean-room rule: zero LAN literals, zero credentials, zero external
31 * dependencies. The API base comes from the environment or the config file,
32 * the token from the environment or config.json - never from source.
33 *
34 * MIT License. Copyright (c) 2026 SCORPIOX INC.
35 */
36#ifndef SX_GH_H
37#define SX_GH_H
38
39#include <stddef.h>
40#include <stdint.h>
41
42#include "yyjson.h"
43#include "sx_connector.h"
44
45#define GH_SLUG "github"
46#define GH_ID "plugin_github"
47#define GH_VER "1.0.2"
48#define GH_NAME "github"
49#define GH_DESC "GitHub tools"
50
51#define GH_DEFAULT_API_BASE "https://api.github.com"
52#define GH_API_VERSION "2022-11-28"
53#define GH_UA "scorpiox-bot-plugin-github/" GH_VER
54
55/* Config file inside the connector directory, written at mode 0600. */
56#define GH_CONFIG_FILE "config.json"
57
58#define GH_HTTP_TIMEOUT_MS 20000
59/* A 429/5xx is retried once, sleeping min(Retry-After, GH_RETRY_MAX_MS). */
60#define GH_RETRY_MAX_MS 5000
61/* Shaped tool output is capped under the SDK's 64 KiB reply buffer. */
62#define GH_OUT_CAP 48000
63/* An issue/PR page is fetched with this many entries per request. */
64#define GH_PER_PAGE 30
65/* github_search_code reports at most this many items. */
66#define GH_SEARCH_MAX_ITEMS 10
67
68/* ------------------------------------------------------------------ */
69/* Growable byte buffer + small helpers (gh_config.c) */
70/* ------------------------------------------------------------------ */
71
72typedef struct {
73 char *p;
74 size_t n;
75 size_t cap;
76} gh_buf;
77
78void gh_buf_init(gh_buf *b);
79int gh_buf_need(gh_buf *b, size_t extra);
80int gh_buf_add(gh_buf *b, const char *s, size_t len);
81int gh_buf_puts(gh_buf *b, const char *s);
82int gh_buf_putc(gh_buf *b, int c);
83int gh_buf_printf(gh_buf *b, const char *fmt, ...);
84const char *gh_buf_str(const gh_buf *b);
85void gh_buf_free(gh_buf *b);
86
87/* JSON string escape into a buffer (quotes, backslash, control chars).
88 * Non-UTF-8 bytes are replaced with U+FFFD so the reply is always valid. */
89int gh_json_str(gh_buf *b, const char *s);
90
91/* Bounded copy that always NUL-terminates. */
92void gh_copy(char *dst, size_t capsz, const char *src);
93
94/* Percent-encode everything but [A-Za-z0-9._~-] into a buffer.
95 * (gh_buf version; sx_url_encode covers the fixed-buffer cases.) */
96void gh_url_encode(const char *in, gh_buf *out);
97
98/* ------------------------------------------------------------------ */
99/* Config (gh_config.c) */
100/* ------------------------------------------------------------------ */
101
102typedef struct {
103 char api_base[256]; /* always resolved, never empty */
104 char token[256]; /* "" == unauthenticated (public reads only) */
105 int has_token;
106} gh_config;
107
108/* Resolve configuration. Precedence for each value:
109 * GITHUB_API_BASE / GITHUB_TOKEN environment > config.json > default.
110 * Returns 0 on success (config is always usable, possibly token-less). */
111int gh_config_load(gh_config *cfg);
112
113/* Persist api_base and/or token. Empty/NULL arguments leave the stored value
114 * unchanged EXCEPT force_clear_token=1 which wipes the token. Writes
115 * config.json atomically at mode 0600. 0 on success, -1 on failure. */
116int gh_config_save(const char *api_base, const char *token,
117 int force_clear_token);
118
119/* Validate + normalise an API base URL: http(s) only, no userinfo, no query,
120 * a trailing slash is dropped. Returns 0 ok, -1 with a message in err. */
121int gh_api_base_valid(const char *base, char *out, size_t outsz,
122 char *err, size_t errsz);
123
124/* ------------------------------------------------------------------ */
125/* HTTP + REST (gh_api.c) */
126/* ------------------------------------------------------------------ */
127
128/* Perform one GitHub API request. `path` starts with "/" and may carry a
129 * query string; it is appended to cfg->api_base. `body` (JSON or NULL).
130 * On 429/5xx the request is retried once after honouring Retry-After
131 * (capped at GH_RETRY_MAX_MS). *status receives the HTTP code. The body is
132 * malloc'd into *resp (caller frees, may be "" but never NULL on success of
133 * the transport). Returns 0 when the transport worked (whatever the HTTP
134 * status), -1 with a transport error message in err. */
135int gh_api_call(const gh_config *cfg, const char *method, const char *path,
136 const char *body, long *status, char **resp,
137 char *err, size_t errsz);
138
139/* Extract {"message": "..."} (and documentation_url when present) from an
140 * error body and format the upstream refusal: the relay rule - the message
141 * GitHub itself answered is shown, never a generic invented one. */
142void gh_upstream_error(const char *resp, long status,
143 char *err, size_t errsz);
144
145/* ---- the five tools: shape human-readable text into out ---- */
146
147int gh_tool_get_repo(const char *args_json, char *out, size_t outsz,
148 char *err, size_t errsz);
149int gh_tool_list_issues(const char *args_json, char *out, size_t outsz,
150 char *err, size_t errsz);
151int gh_tool_create_issue(const char *args_json, char *out, size_t outsz,
152 char *err, size_t errsz);
153int gh_tool_get_pull_request(const char *args_json, char *out, size_t outsz,
154 char *err, size_t errsz);
155int gh_tool_search_code(const char *args_json, char *out, size_t outsz,
156 char *err, size_t errsz);
157
158/* Small arg helper: read a string argument into a fixed buffer.
159 * Returns 1 found, 0 absent (required missing -> error is emitted). */
160int gh_arg_str(const char *args_json, const char *key, char *out, size_t capsz,
161 int required, char *err, size_t errsz);
162/* Read an integer-ish argument (int or numeric string). */
163int gh_arg_int(const char *args_json, const char *key, long *out_val,
164 int required, char *err, size_t errsz);
165
166#endif /* SX_GH_H */