rtk icon indicating copy to clipboard operation
rtk copied to clipboard

Expose rtk as a library crate

Open Sewer56 opened this issue 8 months ago โ€ข 5 comments

Summary

RTK's filtering engine and command classifier are valuable beyond CLI proxy use.

Several modules (filter.rs, discover/registry.rs, utils.rs) already have clean public APIs that return values; they just can't be consumed externally because there's no [lib] target and all modules are private. i.e. they use mod not pub mod.

Proposal:

  • Add a [lib] target to Cargo.toml
  • Create src/lib.rs with curated public re-exports.

This will allow downstream crates to depend on rtk as a library (git dependency).

Motivation

Consuming rtk as a library enables:

  • In-process output filtering: no subprocess overhead, call filter.filter(content, lang) directly
  • Embedding in other tools: LLM agent frameworks, coding tools, etc. without complexity of downloading rtk.

Current State (Why It Doesn't Work Today)

Check Result
[lib] section in Cargo.toml Missing
src/lib.rs Does not exist
Module visibility All mod (not pub mod)
crates.io package Name collision ("Rust Type Kit")

Proposed Change

Modules ready for public exposure (no refactoring needed)

These already return values and avoid println! / std::process::exit():

  • filter: FilterStrategy trait, get_filter(level), Language::from_extension(), smart_truncate()
  • discover::registry: classify_command(), rewrite_command()
  • discover::rules: static pattern/rule data (PATTERNS, RULES, IGNORED_PREFIXES)
  • utils: strip_ansi(), truncate(), format_tokens()

Changes needed

  1. Add [lib] section to Cargo.toml (name = "rtk", path = "src/lib.rs")
  2. Create src/lib.rs with pub mod for the modules listed above
  3. Change mod โ†’ pub mod in src/main.rs for those modules
  4. Ensure src/discover/mod.rs submodules are pub where needed

The [lib] target coexists with the existing [bin]. cargo build produces both, no breaking change.

Out of scope for this issue

The 57+ *_cmd.rs modules (git.rs, cargo_cmd.rs, etc.) use println! and std::process::exit(). Refactoring those to return String is a separate effort that can be tracked in a follow-up issue.

Alternatives Considered

  • Fork with lib support: works but fragments the ecosystem; you don't want forks if you can avoid it.
  • Separate rtk-lib crate: splits the codebase; inlining the [lib] target in the existing repo is simpler

Also a fork would require publishing under a different name to crates.io , which is undesireable.

Sewer56 avatar Mar 20 '26 22:03 Sewer56

wshm ยท Automated triage by AI

๐Ÿ” Automated Triage

โœจ Category feature
๐ŸŸก Priority medium
๐ŸŽฏ Confidence 93%

Summary

Well-scoped feature request to add a [lib] target so RTK's filter/discovery modules can be consumed as a Rust library; author has identified exact files and mechanical changes needed.

This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

๐Ÿ“ Relevant files
  • Cargo.toml
  • src/lib.rs
  • src/main.rs
  • src/filter.rs
  • src/utils.rs
  • src/discover/mod.rs
  • src/discover/registry.rs
  • src/discover/rules.rs

Triaged automatically by wshm ยท This is an automated analysis, not a human review.

pszymkowiak avatar Mar 20 '26 22:03 pszymkowiak

Great idea ๐Ÿ‘

This would allow Rust apps like https://github.com/farion1231/cc-switch to integrate it into their own proxies easier!

reneleonhardt avatar May 01 '26 15:05 reneleonhardt

The relevant PR: https://github.com/rtk-ai/rtk/pull/1693

photino avatar Jun 24 '26 10:06 photino

Adding a concrete use case plus a finding that may make this smaller and lower-risk than the full lib/bin refactor in #1693.

Use case: embedding rtk's output filtering directly in an in-process code/agent harness โ€” string in โ†’ compressed string out โ€” without spawning the binary per call, and without maintaining a fork.

Finding (verified against #1693): most of what an in-process consumer needs is already shaped as a clean public API; the only blocker is the missing [lib] target. Two surfaces are enough:

  1. core::filter โ€” FilterStrategy, get_filter(level), Language::from_extension, smart_truncate โ€” all already pub (generic language-aware code filtering).
  2. cmds::system::pipe_cmd::resolve_filter(name) -> Option<fn(&str) -> String> โ€” already pub, and it returns function pointers to the per-command filters (the same ~17 rtk pipe exposes: cargo-test, pytest, tsc, vitest, git-log, git-diff, ruff-check, mypy, prettier, โ€ฆ).

I checked out #1693 and built an external consumer crate against its lib target. All three paths work today:

// generic code filter
let lang = rtk::filter::Language::from_extension("rs");
let out  = rtk::filter::smart_truncate(src, 200, &lang);

// per-command output filter, in-process (no subprocess)
let out = match rtk::pipe_cmd::resolve_filter("cargo-test") {
    Some(f) => f(&raw),
    None    => raw, // fallback to raw on unknown filter
};

The per-command filter_* functions stay pub(crate) โ€” a direct call fails with E0603 โ€” which is fine: resolve_filter is the intended stable entry point and keeps the internal functions free to change.

Suggestion: this doesn't require the full main.rs โ†’ lib.rs + dispatch.rs restructure from #1693 (large diff, now labeled invalid/postponed). A minimal [lib] target + src/lib.rs re-exporting just core::filter and pipe_cmd::resolve_filter (plus the curated list already in this issue) would unblock embedding with a small, reviewable surface and zero behavioral change to the binary โ€” [lib] and [[bin]] coexist, so cargo build still produces the same rtk.

Happy to open a PR scoped to exactly that minimal surface if maintainers are open to it. (Related: #2037 wants the same foundation for Python bindings.)

dredozubov avatar Jun 24 '26 16:06 dredozubov

For people who need a library distribution before upstream provides one, I have published a public fork here:

https://github.com/dredozubov/rtk

The fork keeps the original rtk-ai/rtk work and upstream contributors clearly credited in FORK.md, preserves the Apache-2.0 license text, and identifies itself as an unofficial fork. It adds a minimal library target to the existing crate/package with the surface we needed:

  • rtk::core::filter
  • rtk::pipe_cmd::resolve_filter

Until the upstream maintainers decide to provide a clear official library build target, consumers who need this can use the fork as a git dependency, for example:

rtk = { git = "https://github.com/dredozubov/rtk", branch = "develop" }

This is intended as an interim community fork for library consumers, not an official rtk-ai/rtk distribution.

dredozubov avatar Jul 01 '26 08:07 dredozubov