@gkoreli/ghx 0.1.4 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +44 -22
  2. package/SKILL.md +314 -0
  3. package/ghx +202 -9
  4. package/package.json +3 -3
package/README.md CHANGED
@@ -2,12 +2,13 @@
2
2
 
3
3
  GitHub code exploration for agents and humans. One command does what takes 3-5 API calls with any other tool.
4
4
 
5
+ - **Repos** — search repos with README preview in 1 GraphQL call
5
6
  - **Explore** a repo (tree + README) in 1 API call
6
7
  - **Read** 1-10 files in 1 API call via GraphQL batching
7
8
  - **Map** code structure with ~92% token reduction
8
- - **Search** code with full GitHub search syntax
9
+ - **Search** code with AND matching, matching context, token protection
9
10
 
10
- ~135 lines of bash. One dependency: `gh` CLI.
11
+ Bash script. One dependency: `gh` CLI. Cross-platform (macOS, Linux, Windows via Git Bash/WSL).
11
12
 
12
13
  ## Install
13
14
 
@@ -20,18 +21,29 @@ npx @gkoreli/ghx --help
20
21
 
21
22
  # curl
22
23
  curl -sf https://raw.githubusercontent.com/gkoreli/ghx/main/install.sh | sh
23
-
24
- # Manual — just copy the script
25
- curl -sf https://raw.githubusercontent.com/gkoreli/ghx/main/ghx -o ~/.local/bin/ghx && chmod +x ~/.local/bin/ghx
26
24
  ```
27
25
 
28
26
  Requires [`gh` CLI](https://cli.github.com/) authenticated (`gh auth login`).
29
27
 
30
28
  [![npm](https://img.shields.io/npm/v/@gkoreli/ghx)](https://www.npmjs.com/package/@gkoreli/ghx)
31
29
 
30
+ ### Platform Support
31
+
32
+ | Platform | Status | Notes |
33
+ |----------|--------|-------|
34
+ | macOS | ✅ Native | bash + readlink -f (12.3+) |
35
+ | Linux | ✅ Native | bash + GNU coreutils |
36
+ | Windows | ✅ Git Bash / WSL | Ships with Git for Windows. Raw cmd.exe/PowerShell not supported |
37
+
38
+ If you have `gh` CLI working, ghx works too — same prerequisites.
39
+
32
40
  ## Usage
33
41
 
34
42
  ```bash
43
+ # Search repos — name, stars, language, README preview in 1 call
44
+ ghx repos "react state management"
45
+ ghx repos "playwright mcp" --limit 5
46
+
35
47
  # Explore a repo — branch, file tree, and README in 1 API call
36
48
  ghx explore plausible/analytics
37
49
 
@@ -47,9 +59,9 @@ ghx read plausible/analytics --grep "defmodule" lib/plausible/stats/query.ex
47
59
  # Read specific line range
48
60
  ghx read plausible/analytics --lines 42-80 lib/plausible/stats/query.ex
49
61
 
50
- # Search code (full GitHub search syntax)
62
+ # Search code (AND matching, shows matching lines, token-protected)
51
63
  ghx search "useState repo:facebook/react"
52
- ghx search "path:llms.txt extension:txt"
64
+ ghx search "path:llms.txt extension:txt" --limit 10
53
65
 
54
66
  # Full recursive tree
55
67
  ghx tree plausible/analytics assets/js
@@ -57,24 +69,36 @@ ghx tree plausible/analytics assets/js
57
69
 
58
70
  ## Why
59
71
 
60
- AI agents exploring GitHub repos face a tooling gap:
72
+ AI agents exploring GitHub face a reliability gap: *"Did I find nothing because nothing exists, or because I used the tool wrong?"* ghx eliminates this with smart defaults — AND matching instead of exact phrase, README previews instead of bare names, matching context instead of bare paths. Unknown flags are rejected loudly (not silently absorbed into queries). The right behavior is the default behavior, and the wrong behavior is impossible.
73
+
74
+ | Tool | Files per call | Matching context | Smart defaults | Dependencies |
75
+ |------|---------------|-----------------|---------------|-------------|
76
+ | GitHub MCP | 1 | No | No (~10K token schemas) | Go binary |
77
+ | `gh` CLI | 1 | No | No (exact phrase, base64, no README) | `gh` |
78
+ | **ghx** | **1-10 (batch)** | **Yes** | **Yes** | **`gh`** |
61
79
 
62
- | Tool | Files per API call | Context overhead | Dependencies |
63
- |------|-------------------|-----------------|-------------|
64
- | GitHub MCP | 1 | ~10K tokens (50+ tool schemas) | Go binary |
65
- | Octocode MCP | 1 (parallel) | ~10K tokens | npm + Docker |
66
- | Raw `gh` CLI | 1 | 0 | `gh` |
67
- | Gitingest | N (clones first) | 0 | pip + tiktoken |
68
- | **ghx** | **1-10 (GraphQL batch)** | **0** | **`gh`** |
80
+ ## Agent Skill Integration
69
81
 
70
- `ghx` reads 10 files in 1 API call. No other tool does this.
82
+ `ghx skill` outputs the full [`SKILL.md`](./SKILL.md) to stdout designed for eager context injection via spawn hooks:
83
+
84
+ ```json
85
+ {
86
+ "hooks": {
87
+ "agentSpawn": [
88
+ {"command": "ghx skill"}
89
+ ]
90
+ }
91
+ }
92
+ ```
93
+
94
+ This loads the skill eagerly into every session — not on-demand like a typical skill file. Use this for agent identities where GitHub exploration is a core capability (not occasional). The agent always has the latest ghx knowledge (commands, gotchas, search strategy) without needing to load it mid-conversation.
95
+
96
+ SKILL.md is included in the npm package and resolved via symlink, so this works with all installation methods.
71
97
 
72
98
  ## Code Map (`--map`)
73
99
 
74
100
  The `--map` flag extracts only structural declarations — imports, exports, function signatures, class definitions, type declarations. Implementation bodies are stripped.
75
101
 
76
- Tested on 6 real files across TypeScript, Python, and Go:
77
-
78
102
  | File | Full | Map | Reduction |
79
103
  |------|------|-----|-----------|
80
104
  | repomix/parseFile.ts | 5,599 | 812 | 86% |
@@ -83,13 +107,11 @@ Tested on 6 real files across TypeScript, Python, and Go:
83
107
 
84
108
  Average: **92% reduction**. An agent can map 16 files in the space of reading 1 file fully.
85
109
 
86
- Supported: TypeScript/JavaScript, Python, Go, Rust, Java/Kotlin, Ruby. Falls back to generic pattern for unknown extensions.
110
+ Supported: TypeScript/JavaScript, Python, Go, Rust, Java/Kotlin, Ruby. Generic fallback for unknown extensions.
87
111
 
88
112
  ## How It Works
89
113
 
90
- `ghx` wraps `gh` CLI with GraphQL batching. The `explore` command fetches tree + README in 1 GraphQL call. The `read` command uses GraphQL aliases (`f0:`, `f1:`, ...) to fetch up to 10 files in 1 call. The `search` command hits the REST `/search/code` endpoint directly (GraphQL has no code search).
91
-
92
- The `--map` flag applies per-language regex patterns to extract structural declarations from the fetched content. No Tree-sitter, no AST parsing — just regex on the first line of each declaration. This works because structural declarations in most languages start at the beginning of a line with a keyword (`function`, `class`, `def`, `func`, `export`, `import`, etc.).
114
+ `ghx` wraps `gh` CLI with GraphQL batching. `repos` and `explore` use GraphQL to batch search + metadata + README into 1 call. `read` uses GraphQL aliases to fetch up to 10 files in 1 call. `search` hits the REST `/search/code` endpoint with `text_matches` for matching context and 200-char token protection.
93
115
 
94
116
  ## License
95
117
 
package/SKILL.md ADDED
@@ -0,0 +1,314 @@
1
+ ---
2
+ name: ghx
3
+ description: GitHub code exploration for AI agents. Use for repo exploration, reading remote files, code search, code maps. Wraps gh CLI with GraphQL batching — one command does what takes 3-5 API calls.
4
+ ---
5
+
6
+ # ghx — GitHub Code Exploration for AI Agents
7
+
8
+ Use `ghx` via `execute_bash` for anything on GitHub — repos, files, code search. Authenticated via `gh` CLI, structured output, zero context overhead.
9
+
10
+ ## Why This Exists
11
+
12
+ Agents exploring GitHub face a reliability gap: *"Did I find nothing because nothing exists, or because I used the tool wrong?"* Raw `gh` commands have silent failure modes — `gh search code` wraps in quotes without telling you, `gh api contents/` returns base64, README requires a separate call. The agent can't distinguish "no results" from "wrong flags."
13
+
14
+ ghx eliminates this by encoding the right defaults into every command. One call returns enough context to decide the next action. You opt into the ghx skill and stop worrying about whether you searched correctly — the right behavior is the default behavior.
15
+
16
+ ## Commands
17
+
18
+ ```bash
19
+ ghx explore <owner/repo> # Branch + tree + README in 1 API call
20
+ ghx explore <owner/repo> <path> # Subdirectory listing
21
+ ghx read <owner/repo> <f1> [f2] [f3] # Read 1-10 files in 1 API call (GraphQL batching)
22
+ ghx read <owner/repo> --map <f1> [f2] # Structural map: signatures, imports, types (~92% token reduction)
23
+ ghx read <owner/repo> --grep "pat" <f> # Read file, show only matching lines (2 lines context)
24
+ ghx read <owner/repo> --lines 42-80 <f> # Read specific line range
25
+ ghx repos "<query>" # Search repos with README preview (default: 10 results)
26
+ ghx repos "<query>" --limit 5 # Limit repo results (max: 20)
27
+ ghx search "<query>" # Code search (AND matching, default: 30 results)
28
+ ghx search "<query>" --limit 10 # Limit code search results (max: 100)
29
+ ghx search --full "<query>" # Code search without line truncation (for minified files)
30
+ ghx tree <owner/repo> [path] # Full recursive tree listing
31
+ ```
32
+
33
+ **Exit codes:** 0 = results returned, 1 = no results (query valid), 2 = usage error (bad flags/args).
34
+ **Flag safety:** Unknown flags always error (exit 2). Never silently absorbed into queries.
35
+
36
+ ## Chain of Thought: Progressive Disclosure
37
+
38
+ **Always start surgical, escalate only when needed.** This mirrors how developers work: scan structure → identify interesting files → read specific sections.
39
+
40
+ ```
41
+ 1. ghx explore owner/repo → What's in this repo? (structure + README)
42
+ 2. ghx read owner/repo --map *.ts → What do these files define? (signatures only, 92% fewer tokens)
43
+ 3. ghx read owner/repo --grep "X" f → Where exactly is X in this file? (targeted lines)
44
+ 4. ghx read owner/repo f → Show me the full file (only when needed)
45
+ ```
46
+
47
+ **Why this order matters:** At 92% reduction, `--map` lets you scan 7 files in the space of reading 1 full file. The agent can understand an entire module's structure before committing context to any single file. Aider's docs confirm: *"The LLM can see classes, methods and function signatures from everywhere in the repo. This alone may give it enough context to solve many tasks."*
48
+
49
+ **When to escalate beyond ghx:**
50
+ - "Understand this entire module" → `gitingest https://github.com/owner/repo/tree/branch/path -i "*.ts" -o - 2>/dev/null`
51
+ - "Compressed view of a codebase" → `npx repomix --remote owner/repo --compress --include "src/**" --stdout`
52
+
53
+ ## Search Query Syntax
54
+
55
+ `ghx search` uses the GitHub REST code search API (legacy). Multi-word queries use AND matching — both words must appear in the file but not necessarily adjacent. This is different from `gh search code` which silently wraps in quotes (exact phrase).
56
+
57
+ **Output format:**
58
+ ```
59
+ 201472 results (showing 30) ← stderr (total + page count)
60
+ jquery/jquery src/attributes/classes.js: addClass: function( value ) { ← stdout (repo path: matching line)
61
+ ```
62
+
63
+ Agents get: result count (stderr) + one line per result with matching context (stdout).
64
+
65
+ ```bash
66
+ ghx search "addClass repo:jquery/jquery" # Scoped to repo
67
+ ghx search "useState language:typescript" # Language filter
68
+ ghx search "filename:package.json repo:owner/repo" # Find specific filename
69
+ ghx search "form path:cgi-bin extension:py" # Path + extension filter
70
+ ghx search '"progress_bar" repo:plausible/analytics' # Exact phrase (shell quotes around double quotes)
71
+ ghx search "path:llms.txt" # Find files by name
72
+ ```
73
+
74
+ **Valid REST API qualifiers:** `repo:`, `org:`, `user:`, `path:`, `filename:`, `extension:`, `language:`, `in:file`, `in:path`, `size:`, `fork:true`
75
+
76
+ **Web-only (DO NOT USE — silently treated as literal text):** `OR`, `NOT`, `symbol:`, `content:`, `is:`, regex (`/pattern/`), `enterprise:`, glob in `path:`. ghx warns on stderr if you use these.
77
+
78
+ **Rate limit:** 9 req/min for code search (strictest endpoint). Authentication required — `gh auth login` first.
79
+
80
+ **Special characters:** Dots act as word separators, not wildcards. `console.log` matches files with both `console` and `log` — it does NOT match `consolelog`.
81
+
82
+ ## Search Strategy for Agents
83
+
84
+ **Search is the entry point.** Agents search first, then read. Bad search = wasted follow-up reads = token explosion. ghx search is designed to give you enough context to decide your next action in one call.
85
+
86
+ ### Reading search output
87
+
88
+ ```
89
+ 90 results (showing 30) ← stderr: is this too broad?
90
+ ⚠ Lines truncated to 200 chars (use --full for complete fragments) ← stderr: token protection kicked in
91
+ ⚠ Query too broad — add repo:, language:, or path: to narrow ← stderr: >1000 results
92
+ jquery/jquery src/attributes/classes.js: addClass: function( value ) { ← stdout: repo path: matching line
93
+ ```
94
+
95
+ **Decision tree after seeing results:**
96
+ - `0 results` → query too specific, broaden (remove qualifiers, try synonyms)
97
+ - `1-30 results` → good. Scan matching lines, `ghx read` the relevant files
98
+ - `30-1000 results` → workable but noisy. Add `repo:`, `language:`, or `path:` to narrow
99
+ - `>1000 results` → too broad. MUST add qualifiers before trusting results
100
+ - `⚠ incomplete` → query timed out, results are partial. Narrow the scope
101
+
102
+ ### Token protection (safe by default)
103
+
104
+ ghx truncates each matching line to 200 chars. This prevents minified JS files (10,000+ char lines) from exploding your context window. One untruncated minified result can consume more tokens than the other 29 results combined.
105
+
106
+ - **Default**: 200 char truncation. You see `⚠ Lines truncated` on stderr only when it triggers.
107
+ - **`--full`**: Disables truncation. Use when you specifically need the complete matching line.
108
+ - **When to use `--full`**: Almost never. The truncated line is enough to decide "relevant" or "skip." Use `ghx read` to get the full file context after you've identified the right file.
109
+
110
+ ### Search refinement chain of thought
111
+
112
+ ```
113
+ 1. ghx search "useState" → 201K results. Too broad.
114
+ 2. ghx search "useState language:typescript" → 50K results. Still broad.
115
+ 3. ghx search "useState repo:vercel/next.js" → 89 results. Workable.
116
+ 4. ghx search "useState path:packages/next extension:tsx repo:vercel/next.js" → 12 results. Surgical.
117
+ ```
118
+
119
+ **Refine, don't paginate.** At 9 req/min, pagination burns rate limit on the same broad query. Adding one qualifier is always better than fetching page 2.
120
+
121
+ ### Two search systems (why some things don't work)
122
+
123
+ GitHub has two code search engines. The REST API (what ghx uses) is the legacy one. The web UI uses Blackbird (new). No programmatic tool — ghx, `gh` CLI, GitHub MCP, Octocode — can access Blackbird. This is a platform limitation, not a ghx limitation.
124
+
125
+ **What this means for agents:**
126
+ - `OR`, `NOT`, `symbol:`, regex, `content:`, `is:` → web-only, don't use
127
+ - `repo:`, `path:`, `filename:`, `language:`, `extension:`, `in:`, `size:`, `fork:` → work in REST API
128
+ - ghx warns on stderr if you use web-only qualifiers, but the results will be wrong
129
+
130
+ ### ghx search vs `gh search code`
131
+
132
+ | Behavior | `ghx search` | `gh search code` |
133
+ |---|---|---|
134
+ | Multi-word matching | AND (both words anywhere) | Exact phrase (words must be adjacent) |
135
+ | Matching context | Shows matching line per result | No matching context |
136
+ | Result count | stderr: "90 results (showing 30)" | Not shown |
137
+ | Token protection | 200 char truncation, `--full` opt-out | None |
138
+ | Web-only warnings | Warns on stderr | Silent |
139
+ | Rate limit | Same (9 req/min) | Same |
140
+
141
+ AND matching is almost always what agents want. `gh search code "useState fetchData"` returns zero results if the words aren't adjacent — with no error. `ghx search "useState fetchData"` finds files containing both terms.
142
+
143
+ ## Gotchas
144
+
145
+ 1. **Web-only qualifiers silently degrade.** `symbol:`, `OR`, `NOT`, `content:`, `is:`, regex — these only work in GitHub's new web code search (Blackbird). The REST API treats them as literal text. `symbol:foo` searches for the TEXT "symbol:foo" inside files. ghx warns on stderr, but the results will be wrong. No programmatic tool can use these features — it's a GitHub platform limitation.
146
+
147
+ 2. **`filename:` vs `path:` — both valid, different systems.** `filename:package.json` works in the REST API (legacy) for exact filename match. `path:` also works and is more flexible (matches directories too). In the NEW web code search, only `path:` works — `filename:` is not recognized. Since ghx uses the REST API, both work.
148
+
149
+ 3. **`language:markdown` won't find `.txt` files.** GitHub's linguist detection doesn't classify .txt as markdown. Use `extension:txt` instead. `language:` = linguist detection, `extension:` = literal file extension.
150
+
151
+ 4. **`gh search code` silently wraps queries in quotes.** `gh search code "foo bar"` sends `q="foo bar"` (exact phrase), not `q=foo bar` (AND). If the words aren't adjacent in the file, you get zero results with no error. `ghx search` sends AND queries — both words must appear but in any order. This is almost always what you want. ghx also shows result count on stderr and matching line context — `gh search code` shows neither.
152
+
153
+ 5. **GraphQL returns null for missing paths.** `object(expression: "branch:path")` returns null silently if the path doesn't exist. No error. `ghx` handles this, but if using `gh api graphql` directly, check for null.
154
+
155
+ 6. **Flag ordering in `read` command.** `ghx read owner/repo file --map` works. `ghx read --map owner/repo file` does NOT — repo must be the first positional arg.
156
+
157
+ 7. **Not all repos use `main`.** cli/cli uses `trunk`, others use `master`. `ghx` handles this automatically. For raw `gh api` calls, query the default branch first: `gh repo view owner/repo --json defaultBranchRef --jq '.defaultBranchRef.name'`
158
+
159
+ 8. **`gh` field names are inconsistent.** `stargazersCount` (search) vs `stargazerCount` (repo view). Always check with `--json` (no fields) to see available fields for any command.
160
+
161
+ 9. **`gh api repos/.../contents/` returns base64 by default.** Without `-H "Accept: application/vnd.github.raw+json"`, you get a JSON blob with base64-encoded content. `ghx read` returns plain text via GraphQL — no decoding needed.
162
+
163
+ 10. **`gh search repos` and `gh search code` use different rate limit pools.** Repo search: 30/min (generous). Code search: 10/min (restrictive). Don't assume one rate limit applies to both.
164
+
165
+ 11. **Unknown flags are rejected, not silently absorbed.** `ghx search "query" --json` exits 2 with a clear error. This is intentional — silent flag absorption was the #1 cause of agent failures (flags like `--limit` would get concatenated into the query string, corrupting it). If you get exit 2, check your flags.
166
+
167
+ ## Anti-Patterns
168
+
169
+ - ❌ `web_fetch`/`web_search` on github.com — returns HTML noise, wastes thousands of tokens for zero useful information
170
+ - ❌ `gh api repos/.../contents/<path>` WITHOUT `-H "Accept: application/vnd.github.raw+json"` — returns base64-encoded JSON blob instead of readable text
171
+ - ❌ Reading entire large files when you need 10 lines — use `--grep "pattern"` or `--lines N-M`
172
+ - ❌ Multiple sequential `gh api` calls for explore workflows — use `ghx explore` (1 GraphQL call) or `ghx read` (batch files)
173
+ - ❌ Using web-only qualifiers (`OR`, `NOT`, `symbol:`, regex) in `ghx search` — silently treated as literal text, returns wrong results. ghx warns but can't prevent it
174
+ - ❌ Firing multiple code search requests in parallel — 9 req/min rate limit, you'll get 403s
175
+ - ❌ Dumping entire repos into context for a specific question — use targeted `ghx` commands. Reserve `gitingest`/`repomix` for "understand this whole module" tasks
176
+ - ❌ Relying on `gh search code` for multi-word queries — silently wraps in quotes (exact phrase), returns nothing when words aren't adjacent. Use `ghx search` (AND matching + matching context)
177
+ - ❌ Using `ghx search` to find repos — ghx search is for code. Use `ghx repos "query"` for repo discovery
178
+ - ❌ Using `gh` for batch file reads — 1 API call per file, base64 encoded. Use `ghx read repo f1 f2 f3` (1 GraphQL call, plain text)
179
+ - ❌ Using `gh repo view` to explore a repo — gets metadata but not tree listing or README content in one call. Use `ghx explore` (1 call for all three)
180
+
181
+ ## Best Practices
182
+
183
+ - **Batch file reads.** `ghx read owner/repo f1 f2 f3` = 1 API call. Three separate reads = 3 calls.
184
+ - **Map before reading.** `ghx read --map` first to understand structure, then `--grep` or `--lines` for specifics.
185
+ - **Refine search, don't paginate.** If `ghx search` shows "201472 results (showing 30)", add qualifiers (`repo:`, `language:`, `path:`) — don't try to page through. 9 req/min rate limit makes pagination expensive.
186
+ - **Use `--limit` to control token budget.** `ghx repos "query" --limit 5` for quick checks, `--limit 15` for thorough discovery. `ghx search "query" --limit 10` when you only need top results.
187
+ - **Check exit codes.** 0 = got results, 1 = no results (query was valid, broaden it), 2 = usage error (fix your command).
188
+ - **Use `gh api --cache 1h`** for repeated lookups when using raw `gh` commands.
189
+ - **Use `--json fields --jq 'expr'`** on `gh` commands to get structured output and reduce noise.
190
+ - **Piped output is machine-formatted.** Tab-delimited, no truncation, no color codes — agents always get clean output.
191
+
192
+ ## The `--map` Flag: Why It Matters
193
+
194
+ `--map` extracts only structural declarations (imports, exports, function/class/type signatures) via per-language regex patterns. Tested on 6 real files across TypeScript, Python, Go:
195
+
196
+ | Metric | Result |
197
+ |--------|--------|
198
+ | Average token reduction | 92% |
199
+ | Files scannable per context window | 7x more than full reads |
200
+ | Implementation | ~15 lines of bash, zero dependencies |
201
+
202
+ Output includes line numbers and token stats:
203
+ ```
204
+ === src/core/parseFile.ts (5544 bytes) ===
205
+ 21:import type { RepomixConfigMerged } from '../../config/configSchema.js';
206
+ 35:export const CHUNK_SEPARATOR = '⋮----';
207
+ 38:export const parseFile = async (fileContent: string, filePath: string, config: RepomixConfigMerged) =>
208
+ 107:const getLanguageParserSingleton = async () =>
209
+ # map: 812/5544 chars (~1386 tokens full, ~203 tokens map)
210
+ ```
211
+
212
+ Supported: TypeScript/JavaScript, Python, Go, Rust, Java/Kotlin, Ruby. Generic fallback for unknown extensions.
213
+
214
+ ## Examples
215
+
216
+ ### Simple: Explore a repo and read a file
217
+
218
+ ```bash
219
+ # What's in this repo?
220
+ ghx explore plausible/analytics
221
+
222
+ # Read the main config
223
+ ghx read plausible/analytics config/runtime.exs
224
+ ```
225
+
226
+ ### Advanced: Research a codebase you've never seen
227
+
228
+ ```bash
229
+ # 1. Explore structure
230
+ ghx explore yamadashy/repomix
231
+
232
+ # 2. Map the core module — understand what exists (92% fewer tokens)
233
+ ghx read yamadashy/repomix --map src/core/output/outputGenerate.ts src/core/file/fileProcess.ts src/core/treeSitter/parseFile.ts
234
+
235
+ # 3. Found interesting function in map output — grep for usage details
236
+ ghx read yamadashy/repomix --grep "processFiles" src/core/file/fileProcess.ts
237
+
238
+ # 4. Search across the whole repo for a pattern
239
+ ghx search "CHUNK_SEPARATOR repo:yamadashy/repomix"
240
+ # → stderr: "3 results (showing 3)"
241
+ # → stdout: yamadashy/repomix src/core/output/outputGenerate.ts: const CHUNK_SEPARATOR = '⋮----';
242
+
243
+ # 5. Read specific lines of a file you've narrowed down
244
+ ghx read yamadashy/repomix --lines 38-65 src/core/treeSitter/parseFile.ts
245
+
246
+ # 6. If you need the full picture of a subdirectory, escalate:
247
+ # gitingest https://github.com/yamadashy/repomix/tree/main/src/core -i "*.ts" -o - 2>/dev/null
248
+ ```
249
+
250
+ ## Complementary Tools
251
+
252
+ | Goal | Tool | Why |
253
+ |------|------|-----|
254
+ | Surgical exploration | `ghx` | Batched API calls, zero overhead, targeted extraction |
255
+ | Holistic understanding | `gitingest` / `repomix --compress` | Dump entire module for broad reasoning |
256
+ | PRs, issues, CI | `gh pr view`, `gh issue view`, `gh pr checks` | Purpose-built commands |
257
+
258
+ ## ghx vs gh: When to Use What
259
+
260
+ **ghx is a complement to gh, not a replacement.** Use ghx for code exploration. Use gh for everything else.
261
+
262
+ ### Use ghx (code exploration)
263
+
264
+ | Task | Command | Why ghx wins |
265
+ |------|---------|-------------|
266
+ | Code search | `ghx search "query"` | AND matching (gh uses exact phrase), matching context, 37x token reduction on minified files, result count + warnings on stderr |
267
+ | Repo search | `ghx repos "query"` | 1 GraphQL call gets name + stars + language + README preview. gh needs 1+N calls for same info, returns worse ranking, no README |
268
+ | Repo overview | `ghx explore owner/repo` | 1 GraphQL call gets description + tree + README (gh needs 3 calls) |
269
+ | Read multiple files | `ghx read owner/repo f1 f2 f3` | 1 GraphQL call for N files (gh needs N calls, returns base64) |
270
+ | Targeted extraction | `ghx read --grep "pat" f` | Built-in grep with context lines — no shell piping |
271
+ | Code map | `ghx read --map f1 f2` | ~92% token reduction, no gh equivalent |
272
+
273
+ ### Use gh (everything else)
274
+
275
+ | Task | Command | Why gh wins |
276
+ |------|---------|-------------|
277
+ | Issues | `gh issue list/view -R owner/repo` | ghx doesn't touch issues |
278
+ | Pull requests | `gh pr list/view/diff/checks -R owner/repo` | ghx doesn't touch PRs |
279
+ | Releases | `gh release list -R owner/repo` | ghx doesn't touch releases |
280
+ | Repo metadata | `gh repo view owner/repo --json stargazerCount,forkCount` | Detailed stats beyond what ghx repos shows |
281
+ | Auth | `gh auth login/status` | ghx depends on gh for auth |
282
+ | Create/update | `gh issue create`, `gh pr create` | ghx is read-only |
283
+
284
+ ### Rate limits (from GitHub docs)
285
+
286
+ | Endpoint | Limit | Used by |
287
+ |---|---|---|
288
+ | Core REST | 5,000/hour | gh commands, ghx tree |
289
+ | GraphQL | 5,000/hour | ghx explore, ghx read |
290
+ | Search (repos, issues) | 30/min | `gh search repos/issues` |
291
+ | Code search | 10/min (budget 9) | `ghx search`, `gh search code` |
292
+
293
+ Code search is 50x more restricted than core REST. This is why "refine don't paginate" matters for search but not for explore/read.
294
+
295
+ ## `gh` CLI Quick Reference
296
+
297
+ ```bash
298
+ # Repos
299
+ gh search repos "<query>" -L 10 --json fullName,description,stargazersCount
300
+ gh repo view owner/repo --json defaultBranchRef --jq '.defaultBranchRef.name'
301
+
302
+ # PRs
303
+ gh pr view 123 -R owner/repo # Title, body, status
304
+ gh pr diff 123 -R owner/repo # Full diff
305
+ gh pr checks 123 -R owner/repo # CI status
306
+
307
+ # Issues
308
+ gh issue view 456 -R owner/repo
309
+ gh issue list -R owner/repo -S "query" -L 20
310
+
311
+ # Raw API (always use the raw header for files)
312
+ gh api repos/owner/repo/contents/path -H "Accept: application/vnd.github.raw+json"
313
+ gh api repos/owner/repo/git/trees/main --jq '.tree[].path' # List structure
314
+ ```
package/ghx CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  set -euo pipefail
7
7
 
8
+ VERSION="0.2.1"
9
+
8
10
  cmd="${1:-help}"
9
11
  shift || true
10
12
 
@@ -73,13 +75,14 @@ read)
73
75
  --grep) grep_pattern="$2"; shift 2 ;;
74
76
  --lines) line_range="$2"; shift 2 ;;
75
77
  --map) map_mode=1; shift ;;
78
+ --*) echo "ghx read: unknown flag '$1'" >&2; exit 2 ;;
76
79
  *) files+=("$1"); shift ;;
77
80
  esac
78
81
  done
79
82
 
80
83
  if [[ ${#files[@]} -eq 0 ]]; then
81
84
  echo "Usage: ghx read <owner/repo> <path1> [path2] [--grep pattern] [--lines N-M]" >&2
82
- exit 1
85
+ exit 2
83
86
  fi
84
87
 
85
88
  # Get default branch
@@ -132,16 +135,192 @@ read)
132
135
  done
133
136
  ;;
134
137
 
135
- # ─── search: code search via REST API (supports full query syntax) ───
138
+ # ─── search: code search via REST API ───
136
139
  search)
137
- query="$*"
140
+ # Parse flags
141
+ full_mode=""
142
+ limit=""
143
+ args=()
144
+ while [[ $# -gt 0 ]]; do
145
+ case "$1" in
146
+ --full) full_mode=1; shift ;;
147
+ --limit) [[ $# -lt 2 ]] && { echo "ghx search: --limit requires a value" >&2; exit 2; }
148
+ limit="$2"; shift 2 ;;
149
+ --*) echo "ghx search: unknown flag '$1'" >&2; exit 2 ;;
150
+ *) args+=("$1"); shift ;;
151
+ esac
152
+ done
153
+ query="${args[*]}"
138
154
  if [[ -z "$query" ]]; then
139
- echo "Usage: ghx search <query>" >&2
140
- echo "Query syntax: 'term1 term2 repo:owner/repo' (AND), 'term1 OR term2 repo:owner/repo'" >&2
141
- echo "Filters: path:src/ extension:tsx language:typescript" >&2
155
+ echo "Usage: ghx search <query> [--full] [--limit N]" >&2
156
+ echo "Examples: 'useState repo:owner/repo' | 'path:src/ extension:tsx language:typescript'" >&2
157
+ exit 2
158
+ fi
159
+ # Validate and clamp limit (API max: 100)
160
+ if [[ -n "$limit" ]]; then
161
+ [[ "$limit" =~ ^[0-9]+$ ]] || { echo "ghx search: --limit must be a number, got '$limit'" >&2; exit 2; }
162
+ (( limit > 100 )) && { echo "⚠ --limit clamped to 100 (API max)" >&2; limit=100; }
163
+ fi
164
+
165
+ # Prerequisite checks
166
+ if ! command -v gh &>/dev/null; then
167
+ echo "ghx requires the GitHub CLI (gh). Install: https://cli.github.com" >&2
168
+ exit 2
169
+ fi
170
+ if ! gh auth status &>/dev/null; then
171
+ echo "GitHub code search requires authentication. Run: gh auth login" >&2
172
+ exit 2
173
+ fi
174
+
175
+ # Warn on web-only qualifiers (they silently become literal text in REST API)
176
+ for pat in 'symbol:' 'content:' 'is:' ' OR ' ' NOT ' '/.*/' 'enterprise:'; do
177
+ if [[ "$query" == *$pat* ]]; then
178
+ echo "⚠ '$pat' is web-only (Blackbird) — REST API treats it as literal text" >&2
179
+ fi
180
+ done
181
+
182
+ # Search with text_matches for matching context
183
+ per_page="${limit:-30}"
184
+ response=$(gh api /search/code --method GET \
185
+ -H "Accept: application/vnd.github.text-match+json" \
186
+ -f q="$query" -f per_page="$per_page" 2>&1) || {
187
+ echo "$response" >&2
188
+ exit 1
189
+ }
190
+
191
+ # Result count + incomplete warning to stderr
192
+ total=$(echo "$response" | jq -r '.total_count')
193
+ incomplete=$(echo "$response" | jq -r '.incomplete_results')
194
+ count=$(echo "$response" | jq '.items | length')
195
+ echo "$total results (showing $count)" >&2
196
+ [[ "$incomplete" == "true" ]] && echo "⚠ Results may be incomplete (query timed out)" >&2
197
+ [[ "$total" -gt 1000 ]] 2>/dev/null && echo "⚠ Query too broad — add repo:, language:, or path: to narrow" >&2
198
+ [[ "$count" -eq 0 ]] && exit 1
199
+
200
+ # Output: repo path: matching context from fragment
201
+ # Default: 200 char window centered on the match (prevents minified JS token explosion)
202
+ # Uses matches[0].indices to find the actual match position in the fragment
203
+ if [[ -n "$full_mode" ]]; then
204
+ echo "$response" | jq -r '
205
+ .items[] |
206
+ .repository.full_name as $repo |
207
+ .path as $path |
208
+ (.text_matches // []) as $tm |
209
+ if ($tm | length) > 0 then
210
+ ($tm[0].fragment | gsub("\\n"; " ") | gsub("\\s+"; " ") |
211
+ gsub("^\\s+"; "") | gsub("\\s+$"; "")) as $line |
212
+ "\($repo) \($path): \($line)"
213
+ else
214
+ "\($repo) \($path)"
215
+ end'
216
+ else
217
+ truncated=$(echo "$response" | jq '[.items[] |
218
+ (.text_matches // []) as $tm |
219
+ if ($tm | length) > 0 then
220
+ ($tm[0].fragment | length) > 200
221
+ else false end] | any')
222
+ [[ "$truncated" == "true" ]] && echo "⚠ Lines truncated to 200 chars (use --full for complete fragments)" >&2
223
+ echo "$response" | jq -r '
224
+ .items[] |
225
+ .repository.full_name as $repo |
226
+ .path as $path |
227
+ (.text_matches // []) as $tm |
228
+ if ($tm | length) > 0 then
229
+ $tm[0] as $m |
230
+ ($m.fragment | gsub("\\n"; " ") | gsub("\\s+"; " ")) as $flat |
231
+ (if ($m.matches | length) > 0 then
232
+ $m.matches[0].indices[0] as $start |
233
+ ([$start - 80, 0] | max) as $from |
234
+ ($from + 200) as $to |
235
+ (if $from > 0 then "…" else "" end) as $prefix |
236
+ (if $to < ($flat | length) then "…" else "" end) as $suffix |
237
+ $prefix + ($flat[$from:$to] | gsub("^\\s+"; "") | gsub("\\s+$"; "")) + $suffix
238
+ else
239
+ $flat[:200]
240
+ end) as $line |
241
+ "\($repo) \($path): \($line)"
242
+ else
243
+ "\($repo) \($path)"
244
+ end'
245
+ fi
246
+ ;;
247
+
248
+ # ─── repos: search repos with README preview in 1 GraphQL call ───
249
+ repos)
250
+ limit=""
251
+ args=()
252
+ while [[ $# -gt 0 ]]; do
253
+ case "$1" in
254
+ --limit) [[ $# -lt 2 ]] && { echo "ghx repos: --limit requires a value" >&2; exit 2; }
255
+ limit="$2"; shift 2 ;;
256
+ --*) echo "ghx repos: unknown flag '$1'" >&2; exit 2 ;;
257
+ *) args+=("$1"); shift ;;
258
+ esac
259
+ done
260
+ query="${args[*]}"
261
+ if [[ -z "$query" ]]; then
262
+ echo "Usage: ghx repos <query> [--limit N]" >&2
263
+ exit 2
264
+ fi
265
+ # Validate and clamp limit (GraphQL max: 100, but README fetching makes >20 expensive)
266
+ first="${limit:-10}"
267
+ [[ "$first" =~ ^[0-9]+$ ]] || { echo "ghx repos: --limit must be a number, got '$first'" >&2; exit 2; }
268
+ (( first > 20 )) && { echo "⚠ --limit clamped to 20 (README fetching makes larger values slow)" >&2; first=20; }
269
+
270
+ response=$(gh api graphql -f query='
271
+ {
272
+ search(query: "'"$query"'", type: REPOSITORY, first: '"$first"') {
273
+ repositoryCount
274
+ nodes {
275
+ ... on Repository {
276
+ nameWithOwner
277
+ description
278
+ stargazerCount
279
+ primaryLanguage { name }
280
+ object(expression: "HEAD:README.md") {
281
+ ... on Blob { text }
282
+ }
283
+ }
284
+ }
285
+ }
286
+ }')
287
+
288
+ echo "$response" | jq -r '.data.search.repositoryCount | "\(.) repos found"' >&2
289
+ count=$(echo "$response" | jq '.data.search.nodes | length')
290
+ [[ "$count" -eq 0 ]] && exit 1
291
+ echo "$response" | jq -r '
292
+ .data.search.nodes[] |
293
+ .nameWithOwner as $name |
294
+ (.stargazerCount | tostring) as $stars |
295
+ ((.primaryLanguage.name // "?")) as $lang |
296
+ (.description // "") as $desc |
297
+ ((.object.text // "") | gsub("\\n"; " ") | gsub("\\s+"; " ") | gsub("^\\s+"; "") |
298
+ gsub("\\[!\\[[^]]*\\]\\([^)]*\\)\\]\\([^)]*\\)"; "") |
299
+ gsub("!\\[[^]]*\\]\\([^)]*\\)"; "") |
300
+ gsub("\\[!\\[[^]]*\\]\\([^)]*\\)\\]"; "") |
301
+ gsub("\\[![^]]*\\]\\([^)]*\\)"; "") |
302
+ gsub("<img[^>]*>"; "") |
303
+ gsub("<div[^>]*>"; "") | gsub("</div>"; "") |
304
+ gsub("<br[^>]*/?>"; "") |
305
+ gsub("<p[^>]*>"; "") | gsub("</p>"; "") |
306
+ gsub("<a[^>]*>"; "") | gsub("</a>"; "") |
307
+ gsub("\\s+"; " ") | gsub("^\\s+"; "") |
308
+ .[:300]) as $readme |
309
+ "\($name) (\($stars)★ \($lang)) \($desc)" +
310
+ (if ($readme | length) > 0 then "\n " + $readme + (if ($readme | length) >= 300 then "…" else "" end) else "" end)
311
+ '
312
+ ;;
313
+
314
+ # ─── skill: output SKILL.md for agent context injection ───
315
+ skill)
316
+ script_dir="$(dirname "$(readlink -f "$0")")"
317
+ skill_file="$script_dir/SKILL.md"
318
+ if [[ -f "$skill_file" ]]; then
319
+ cat "$skill_file"
320
+ else
321
+ echo "SKILL.md not found at $skill_file" >&2
142
322
  exit 1
143
323
  fi
144
- gh api /search/code --method GET -f q="$query" --jq '.items[] | "\(.repository.full_name) \(.path):\(.name)"'
145
324
  ;;
146
325
 
147
326
  # ─── tree: recursive tree listing via REST API ───
@@ -164,6 +343,10 @@ tree)
164
343
  ;;
165
344
 
166
345
  # ─── help ───
346
+ version|-v|--version)
347
+ echo "ghx $VERSION"
348
+ ;;
349
+
167
350
  help|*)
168
351
  cat <<'EOF'
169
352
  ghx — GitHub code exploration for agents and humans
@@ -171,20 +354,30 @@ ghx — GitHub code exploration for agents and humans
171
354
  Commands:
172
355
  ghx explore <owner/repo> [path] Branch + tree + README in 1 API call
173
356
  ghx read <owner/repo> <f1> [f2..] Read 1-10 files in 1 API call
174
- ghx search <query> Code search (full syntax: AND, OR, path:, extension:, language:)
357
+ ghx repos <query> [--limit N] Search repos with README preview (default: 10)
358
+ ghx search "<query>" [--limit N] Code search (AND matching, default: 30)
359
+ ghx search --full "<query>" Code search without line truncation
360
+ ghx skill Output SKILL.md (for agent context injection)
175
361
  ghx tree <owner/repo> [path] Full recursive tree listing
176
362
 
177
363
  Read flags:
178
364
  --grep <pattern> Filter output to matching lines (case-insensitive, 2 lines context)
179
365
  --lines <N-M> Extract specific line range
366
+ --map Structural signatures only (~92% token reduction)
367
+
368
+ Exit codes:
369
+ 0 Success with results
370
+ 1 No results (query valid, nothing matched)
371
+ 2 Usage error (bad flags, missing args)
180
372
 
181
373
  Examples:
182
374
  ghx explore plausible/analytics
183
375
  ghx read plausible/analytics mix.exs assets/js/dashboard/stats/bar.js
184
376
  ghx read plausible/analytics src/app.ts --grep "useState"
185
377
  ghx read plausible/analytics src/app.ts --lines 42-80
378
+ ghx repos "react state management"
186
379
  ghx search "useState repo:plausible/analytics"
187
- ghx search "bar OR percentage repo:plausible/analytics"
380
+ ghx search "filename:package.json repo:plausible/analytics"
188
381
  ghx tree plausible/analytics assets/js
189
382
  EOF
190
383
  ;;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkoreli/ghx",
3
- "version": "0.1.4",
3
+ "version": "0.2.1",
4
4
  "description": "GitHub code exploration for agents and humans. Batch file reads, code maps, search — all via gh CLI.",
5
5
  "bin": {
6
6
  "ghx": "./ghx"
@@ -11,8 +11,8 @@
11
11
  "type": "git",
12
12
  "url": "https://github.com/gkoreli/ghx"
13
13
  },
14
- "files": ["ghx", "README.md", "LICENSE"],
15
- "os": ["darwin", "linux"],
14
+ "files": ["ghx", "SKILL.md", "README.md", "LICENSE"],
15
+ "os": ["darwin", "linux", "win32"],
16
16
  "engines": {
17
17
  "node": ">=16"
18
18
  }