# regenrek/codefetch architecture

> Architecture diagram of the GitHub repository regenrek/codefetch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/regenrek/codefetch
- Repository: https://github.com/regenrek/codefetch

## Overview

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: [`packages/sdk/src/fetch.ts`](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/fetch.ts) (Fetch API), [`packages/cli/src/commands/default.ts`](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/commands/default.ts) (Commands), [`packages/cli/src/cli.ts`](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/cli.ts) (CLI entry), [`packages/sdk/src/markdown-content.ts`](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/markdown-content.ts) (Markdown output)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_workspace["Workspace"]
  node_root["Workspace<br/>pnpm monorepo"]
  node_config["Shared config<br/>repo config"]
  node_release["Release flow<br/>release tooling<br/>[release.ts]"]
  node_ci["CI workflows<br/>github actions<br/>[sdk-test.yml]"]
end

subgraph group_sdk["SDK"]
  node_sdk_fetch["Fetch API<br/>core pipeline<br/>[fetch.ts]"]
  node_sdk_files["File rules<br/>selection/filtering<br/>[files.ts]"]
  node_sdk_tree["Tree builder<br/>structure rendering<br/>[tree.ts]"]
  node_sdk_markdown["Markdown output<br/>content assembly"]
  node_sdk_token["Token budget<br/>token accounting<br/>[token-counter.ts]"]
  node_sdk_cache["Cache layer<br/>cache abstraction<br/>[index.ts]"]
  node_sdk_web["Web fetch<br/>remote acquisition<br/>[index.ts]"]
  node_sdk_runtime["Runtime builds<br/>multi-target packaging<br/>[worker.ts]"]
  node_sdk_prompts["Prompt templates<br/>prompt system<br/>[index.ts]"]
end

subgraph group_cli["CLI"]
  node_cli_entry["CLI entry<br/>command router<br/>[cli.ts]"]
  node_cli_config["Config loader<br/>cli config<br/>[config.ts]"]
  node_cli_commands["Commands<br/>[default.ts]"]
  node_cli_prompts["Prompt UX<br/>prompt resolution<br/>[codegen.ts]"]
  node_cli_output["Local UX<br/>output delivery<br/>[browser.ts]"]
end

subgraph group_mcp["MCP"]
  node_mcp_server["MCP server<br/>protocol adapter<br/>[server.ts]"]
end

subgraph group_playground["Playground"]
  node_play_scripts["Demo scripts<br/>playground tooling"]
  node_play_sample["Sample repo<br/>fixture repository<br/>[main.rs]"]
end

node_root -->|"shared config"| node_config
node_root -->|"publish"| node_release
node_root -->|"validate"| node_ci
node_cli_entry -->|"loads"| node_cli_config
node_cli_entry -->|"routes"| node_cli_commands
node_cli_commands -->|"calls"| node_sdk_fetch
node_cli_commands -->|"resolves"| node_cli_prompts
node_cli_commands -->|"hands off"| node_cli_output
node_cli_prompts -->|"shares"| node_sdk_prompts
node_sdk_fetch -->|"filters"| node_sdk_files
node_sdk_fetch -->|"builds"| node_sdk_tree
node_sdk_fetch -->|"formats"| node_sdk_markdown
node_sdk_markdown -->|"limits"| node_sdk_token
node_sdk_fetch -->|"caches"| node_sdk_cache
node_sdk_fetch -->|"acquires"| node_sdk_web
node_sdk_fetch -->|"targets"| node_sdk_runtime
node_sdk_web -->|"adapts to"| node_sdk_runtime
node_mcp_server -->|"exposes"| node_sdk_fetch
node_play_scripts -->|"exercises"| node_sdk_fetch
node_play_sample -->|"fixtures"| node_sdk_files

click node_config "https://github.com/regenrek/codefetch/blob/main/codefetch.config.mjs"
click node_release "https://github.com/regenrek/codefetch/blob/main/scripts/release.ts"
click node_ci "https://github.com/regenrek/codefetch/blob/main/.github/workflows/sdk-test.yml"
click node_sdk_fetch "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/fetch.ts"
click node_sdk_files "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/files.ts"
click node_sdk_tree "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/tree.ts"
click node_sdk_markdown "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/markdown-content.ts"
click node_sdk_token "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/token-counter.ts"
click node_sdk_cache "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/cache/index.ts"
click node_sdk_web "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/web/index.ts"
click node_sdk_runtime "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/worker.ts"
click node_sdk_prompts "https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/prompts/index.ts"
click node_cli_entry "https://github.com/regenrek/codefetch/blob/main/packages/cli/src/cli.ts"
click node_cli_config "https://github.com/regenrek/codefetch/blob/main/packages/cli/src/config.ts"
click node_cli_commands "https://github.com/regenrek/codefetch/blob/main/packages/cli/src/commands/default.ts"
click node_cli_prompts "https://github.com/regenrek/codefetch/blob/main/packages/cli/src/prompts/codegen.ts"
click node_cli_output "https://github.com/regenrek/codefetch/blob/main/packages/cli/src/utils/browser.ts"
click node_mcp_server "https://github.com/regenrek/codefetch/blob/main/packages/mcp/src/server.ts"
click node_play_scripts "https://github.com/regenrek/codefetch/blob/main/playground/analyze-github-repo.js"
click node_play_sample "https://github.com/regenrek/codefetch/blob/main/playground/sample-rust-project/src/main.rs"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_root,node_config,node_release,node_ci toneBlue
class node_sdk_fetch,node_sdk_files,node_sdk_tree,node_sdk_markdown,node_sdk_token,node_sdk_cache,node_sdk_web,node_sdk_runtime,node_sdk_prompts toneAmber
class node_cli_entry,node_cli_config,node_cli_commands,node_cli_prompts,node_cli_output toneMint
class node_mcp_server toneRose
class node_play_scripts,node_play_sample toneIndigo
```

## Components

### Workspace

Top-level pnpm workspace, shared config, release tooling, and CI.

- **Workspace**: pnpm monorepo · Workspace root with shared config, release scripts, and package orchestration.
- [Shared config](https://github.com/regenrek/codefetch/blob/main/codefetch.config.mjs): repo config · Root-level lint, ignore, and codefetch configuration. · `codefetch.config.mjs`
- [Release flow](https://github.com/regenrek/codefetch/blob/main/scripts/release.ts): release tooling · Scripts and guidance for workspace releases and package publication. · `scripts/release.ts`
- [CI workflows](https://github.com/regenrek/codefetch/blob/main/.github/workflows/sdk-test.yml): github actions · 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](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/fetch.ts): core pipeline · Unified entrypoint that turns local, remote, or web sources into AI-ready output. · `packages/sdk/src/fetch.ts`
- [File rules](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/files.ts): selection/filtering · Ignore handling, include/exclude rules, and extension filtering shared by tree and content paths. · `packages/sdk/src/files.ts`
- [Tree builder](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/tree.ts): structure rendering · Builds the codebase tree representation used in snapshots and prompts. · `packages/sdk/src/tree.ts`
- [Markdown output](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/markdown-content.ts): content assembly · Assembles structured Markdown with semantic tags for LLM consumption. · `packages/sdk/src/markdown-content.ts`
- [Token budget](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/token-counter.ts): token accounting · Encoders and truncation logic used to fit content within model budgets. · `packages/sdk/src/token-counter.ts`
- [Cache layer](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/cache/index.ts): cache abstraction · Runtime-aware cache interface and implementations for memory, filesystem, and Cloudflare. · `packages/sdk/src/cache/index.ts`
- [Web fetch](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/web/index.ts): remote acquisition · URL handling, GitHub/GitLab/tarball fetching, and HTML-to-Markdown conversion for remote sources. · `packages/sdk/src/web/index.ts`
- [Runtime builds](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/worker.ts): multi-target packaging · Node, browser, and worker entrypoints and build configs for the shared core. · `packages/sdk/src/worker.ts`
- [Prompt templates](https://github.com/regenrek/codefetch/blob/main/packages/sdk/src/prompts/index.ts): prompt system · 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](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/cli.ts): command router · Main CLI composition layer that wires args, commands, and SDK calls. · `packages/cli/src/cli.ts`
- [Config loader](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/config.ts): cli config · Loads CLI configuration and resolves prompt/model defaults. · `packages/cli/src/config.ts`
- [Commands](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/commands/default.ts): subcommands · Default analysis, init, and open flows exposed by the CLI. · `packages/cli/src/commands/default.ts`
- [Prompt UX](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/prompts/codegen.ts): prompt resolution · CLI-side prompt selection, built-ins, and help prompt handling. · `packages/cli/src/prompts/codegen.ts`
- [Local UX](https://github.com/regenrek/codefetch/blob/main/packages/cli/src/utils/browser.ts): output delivery · Stdout, file output, clipboard, and browser handoff helpers. · `packages/cli/src/utils/browser.ts`

### MCP

Minimal Model Context Protocol service boundary.

- [MCP server](https://github.com/regenrek/codefetch/blob/main/packages/mcp/src/server.ts): protocol adapter · 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](https://github.com/regenrek/codefetch/blob/main/playground/analyze-github-repo.js): playground tooling · Ad hoc scripts for analyzing repos, generating docs, and testing web fetch flows. · `playground/analyze-github-repo.js`
- [Sample repo](https://github.com/regenrek/codefetch/blob/main/playground/sample-rust-project/src/main.rs): fixture repository · Local Rust sample project used to exercise ignore rules and source extraction. · `playground/sample-rust-project/src/main.rs`

## Connections

- 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.
