How regenrek/codefetch works
1. Monorepo structure
- This repository is a TypeScript pnpm workspace with three product packages: packages/sdk (core library), packages/cli (command-line app), and packages/mcp (MCP server stub/early package).
- playground/ contains ad hoc demo scripts and sample repositories used to exercise the SDK against real local and remote codebases.
- The root also holds release tooling, shared config, and GitHub Actions workflows for package validation and publication.
2. Core architectural center: packages/sdk
- The SDK is the main engine. It owns the unified fetch() API that turns a source into AI-ready Markdown.
- Most higher-level features hang off this pipeline: local filesystem analysis, remote repository fetching, web crawling, ignore handling, token counting, tree rendering, and prompt injection.
- The CLI is mostly a thin orchestration layer over this SDK.
3. Input sources and acquisition paths
- The SDK supports three broad source types:
- local directories / codebases,
- git repositories via GitHub/GitLab/Bitbucket URLs,
- arbitrary web pages for documentation-style crawling.
- Remote repo fetching has multiple transport strategies: GitHub API first, git-clone fallback, and tarball/web fetch helpers for platform-specific cases.
- The packages/sdk/src/web/ area isolates URL handling, GitHub API/tarball logic, GitLab tarball support, and HTML-to-Markdown conversion for website inputs.
4. Filtering and project boundary control
- Before rendering, the SDK applies layered file selection rules: .gitignore, .codefetchignore, default ignore patterns, config-based include/exclude lists, and extension filters.
- This filtering is central to the architecture because it determines the shape of the codebase snapshot and prevents re-including generated output.
- The tree and source extraction paths share these same inclusion rules so the visual structure matches the actual content selection.
5. Content shaping and output format
- The primary output is structured Markdown with semantic XML tags like <task>, <filetree>, and <source_code>.
- The system has separate modules for tree generation, markdown formatting, and markdown content assembly, which keeps the presentation layer distinct from file acquisition.
- This is designed for LLM consumption, so the output is optimized for readability and prompt injection rather than human documentation aesthetics alone.
6. Token accounting and output truncation
- The SDK includes token counting utilities with multiple encoders (simple, p50k, o200k, cl100k) to match different model families.
- Token limiting is part of the rendering pipeline, not just a post-process: the system can either stop sequentially or truncate content across files to fit a budget.
- This makes token management a first-class boundary between “all source collected” and “content safe to send to a model.”
7. Caching and runtime-specific storage
- Caching is abstracted behind a cache interface with implementations for memory, filesystem, and Cloudflare-compatible cache layers.
- There is a factory layer and validation layer, which indicates cache selection is runtime-dependent and guarded by consistent rules.
- The repo separates Node-oriented caching from Worker-oriented caching so the same SDK logic can run in local CLI, browser, and edge environments.
8. CLI package as orchestration and UX layer
- packages/cli provides argument parsing, config loading, prompt selection, browser opening, clipboard integration, and command routing.
- The CLI has subcommands like the default analysis flow, init for bootstrapping config/ignore files, and open for generating content and launching an AI chat in the browser.
- Its role is to transform user intent into SDK calls and then handle local UX concerns such as stdout, file output, clipboard copy, and opening ChatGPT/Gemini/Claude URLs.
9. Prompt system and AI-assistant workflows
- Prompt templates are shared between CLI and SDK, with built-ins such as codegen, fix, improve, and testgen.
- Prompts can come from inline text, named built-ins, config defaults, or external files; the CLI resolves these before invoking the SDK.
- The open flow combines generated code, prompt text, clipboard output, and browser navigation, making it a specialized delivery pipeline for interactive LLM usage.
10. Runtime targets: Node, browser, and Cloudflare Workers
- The repository clearly supports multiple compilation targets: standard Node.js, browser bundles, and a Worker build optimized for edge runtimes.
- packages/sdk/src/browser.ts, worker.ts, build.browser.config.ts, and build.worker.config.ts show that the same core logic is packaged differently per runtime.
- The Worker-specific code avoids filesystem assumptions and uses web-native APIs, which is important for deployment architecture.
11. MCP package
- packages/mcp is a small separate service boundary intended for Model Context Protocol integration.
- The current tree suggests it is minimal compared with the SDK and CLI, acting as an adapter layer that would expose codefetch capabilities to assistant clients rather than implementing the pipeline itself.
- This package is architecturally downstream of the SDK, not a competing core.
12. Verification, fixtures, and release flow
- Tests are extensive and split by package plus integration/regression suites, with fixtures for codebase scanning, prompt handling, web fetching, and worker behavior.
- The presence of mock servers and worker build verification indicates the repo validates both functionality and packaging across runtimes.
- GitHub Actions workflows and release scripts suggest the packages are published independently but coordinated through the workspace.
13. Practical architecture summary
- The main flow is: user input or URL → config and ignore resolution → source acquisition → file selection/tree building → token-aware Markdown assembly → optional caching/output/clipboard/browser handoff.
- The SDK is the system of record for that flow; the CLI, Worker build, and future MCP server are delivery surfaces around it.
Main entry points: fetch.ts Fetch API · default.ts Commands · cli.ts CLI entry · markdown-content.ts Markdown output
Components 21
Workspace
Top-level pnpm workspace, shared config, release tooling, and CI.
- Workspace · Workspace root with shared config, release scripts, and package orchestration.
- Shared config · Root-level lint, ignore, and codefetch configuration.
codefetch.config.mjs - Release flow · Scripts and guidance for workspace releases and package publication.
scripts/release.ts - CI workflows · Package validation and release workflows for the workspace.
.github/workflows/sdk-test.yml
SDK
Core engine for fetching sources, filtering files, shaping Markdown, and supporting multiple runtimes.
- Fetch API · Unified entrypoint that turns local, remote, or web sources into AI-ready output.
packages/sdk/src/fetch.ts - File rules · Ignore handling, include/exclude rules, and extension filtering shared by tree and content paths.
packages/sdk/src/files.ts - Tree builder · Builds the codebase tree representation used in snapshots and prompts.
packages/sdk/src/tree.ts - Markdown output · Assembles structured Markdown with semantic tags for LLM consumption.
packages/sdk/src/markdown-content.ts - Token budget · Encoders and truncation logic used to fit content within model budgets.
packages/sdk/src/token-counter.ts - Cache layer · Runtime-aware cache interface and implementations for memory, filesystem, and Cloudflare.
packages/sdk/src/cache/index.ts - Web fetch · URL handling, GitHub/GitLab/tarball fetching, and HTML-to-Markdown conversion for remote sources.
packages/sdk/src/web/index.ts - Runtime builds · Node, browser, and worker entrypoints and build configs for the shared core.
packages/sdk/src/worker.ts - Prompt templates · Built-in prompts and shared prompt definitions used by SDK and CLI.
packages/sdk/src/prompts/index.ts
CLI
Command-line orchestration layer over the SDK.
- CLI entry · Main CLI composition layer that wires args, commands, and SDK calls.
packages/cli/src/cli.ts - Config loader · Loads CLI configuration and resolves prompt/model defaults.
packages/cli/src/config.ts - Commands · Default analysis, init, and open flows exposed by the CLI.
packages/cli/src/commands/default.ts - Prompt UX · CLI-side prompt selection, built-ins, and help prompt handling.
packages/cli/src/prompts/codegen.ts - Local UX · Stdout, file output, clipboard, and browser handoff helpers.
packages/cli/src/utils/browser.ts
MCP
Minimal Model Context Protocol service boundary.
- MCP server · Minimal server boundary intended to expose codefetch capabilities to MCP clients.
packages/mcp/src/server.ts
Playground
Ad hoc demo scripts and sample repositories used to exercise the SDK.
- Demo scripts · Ad hoc scripts for analyzing repos, generating docs, and testing web fetch flows.
playground/analyze-github-repo.js - Sample repo · Local Rust sample project used to exercise ignore rules and source extraction.
playground/sample-rust-project/src/main.rs
Connections 20
- Workspace → Shared config: shared config · Workspace-level configuration is consumed across packages.
- Workspace → Release flow: publish · Release scripts coordinate workspace publication.
- Workspace → CI workflows: validate · GitHub workflows verify packages and release behavior.
- CLI entry → Config loader: loads · CLI resolves config before command execution.
- CLI entry → Commands: routes · Command router dispatches into default, init, and open flows.
- Commands → Fetch API: calls · CLI commands invoke the SDK fetch pipeline.
- Commands → Prompt UX: resolves · Commands select built-in or configured prompts.
- Commands → Local UX: hands off · Commands manage clipboard, stdout, file, and browser delivery.
- Prompt UX → Prompt templates: shares · CLI and SDK use the same prompt templates.
- Fetch API → File rules: filters · Fetch flow applies ignore and include/exclude rules.
- Fetch API → Tree builder: builds · Fetch flow can produce a file tree snapshot.
- Fetch API → Markdown output: formats · Fetch flow assembles structured Markdown output.
- Markdown output → Token budget: limits · Rendering is constrained by token accounting and truncation.
- Fetch API → Cache layer: caches · Fetch pipeline can reuse cached results across runtimes.
- Fetch API → Web fetch: acquires · Remote repository and web sources are fetched through web adapters.
- Fetch API → Runtime builds: targets · Core logic is packaged for Node, browser, and worker runtimes.
- Web fetch → Runtime builds: adapts to · Web fetch helpers support worker-safe and browser-safe execution.
- MCP server → Fetch API: exposes · MCP server is a downstream adapter over the SDK engine.
- Demo scripts → Fetch API: exercises · Playground scripts run the SDK against real repositories and web pages.
- Sample repo → File rules: fixtures · Sample repository validates ignore and boundary behavior.