#include "ctxstream.hpp" #include #include #include #include #include #include #include #include namespace fs = std::filesystem; using namespace ctxstream; static void usage() { std::fprintf(stderr, "ctxstream -- stream an oversized context past a small model, reduce in code.\n\n" "Usage:\n" " ctxstream --input --extract [options]\n\n" "A directory input builds a code graph first and segments along it.\n\n" "Options:\n" " --input PATH file, or directory for codebase mode\n" " --extract PROMPT what each fragment should emit as \\t\n" " --answer MODE least | most | total | list (default: list)\n" " --label TEXT answer label (default: Answer)\n" " --model NAME ollama model (default: gemma4:e4b)\n" " --host H --port P model server (default: 127.0.0.1:11434)\n" " --num-ctx N per-call window (default: 32768)\n" " --segment-chars N fragment size (default: 60000)\n" " --concurrency N fragments in flight (default: 2)\n" " --plan-only print the plan and exit, no model calls\n" " --graph-only print the code graph and exit\n" " --quiet suppress progress\n"); } static std::string slurp(const fs::path& p) { std::ifstream in(p, std::ios::binary); std::ostringstream ss; ss << in.rdbuf(); return ss.str(); } int main(int argc, char** argv) { std::string input, extract, answer_mode = "list", label = "Answer"; BackendOptions backend; ManifestOptions manifest; PipelineOptions pipe; bool plan_only = false, graph_only = false; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; auto next = [&]() -> std::string { return (i + 1 < argc) ? argv[++i] : std::string(); }; if (a == "--input") input = next(); else if (a == "--extract") extract = next(); else if (a == "--answer") answer_mode = next(); else if (a == "--label") label = next(); else if (a == "--model") backend.model = next(); else if (a == "--host") backend.host = next(); else if (a == "--port") backend.port = std::stoi(next()); else if (a == "--num-ctx") backend.num_ctx = std::stoi(next()); else if (a == "--segment-chars") manifest.segment_chars = std::stoul(next()); else if (a == "--concurrency") pipe.concurrency = std::stoi(next()); else if (a == "--plan-only") plan_only = true; else if (a == "--graph-only") graph_only = true; else if (a == "--quiet") pipe.verbose = false; else if (a == "-h" || a == "--help") { usage(); return 0; } else { std::fprintf(stderr, "unknown option: %s\n", a.c_str()); usage(); return 2; } } if (input.empty()) { usage(); return 2; } std::error_code ec; if (!fs::exists(input, ec)) { std::fprintf(stderr, "no such input: %s\n", input.c_str()); return 2; } std::string text; std::vector segments; const bool codebase = fs::is_directory(input, ec); if (codebase) { // Default path for a repo: graph first, then segment along it. const CodeGraph g = scan_repo(input, ScanOptions{}); std::fprintf(stderr, "[ctxstream] graph %s\n", graph_summary(g).c_str()); for (const auto& [p, why] : g.skipped) { std::fprintf(stderr, "[ctxstream] skipped %s (%s)\n", p.c_str(), why.c_str()); } if (graph_only) { for (const auto& f : g.files) { std::printf("FILE\t%s\t%s\t%zu\t%d\n", f.path.c_str(), f.language.c_str(), f.bytes, f.symbols); } for (const auto& s : g.symbols) { std::printf("SYM\t%s\t%s:%d\n", s.name.c_str(), g.files[s.file].path.c_str(), s.line); } for (const auto& e : g.edges) { std::printf("EDGE\t%s\t->\t%s\n", g.files[e.from].path.c_str(), g.files[e.to].path.c_str()); } return 0; } if (g.files.empty()) { std::fprintf(stderr, "no source files found under %s\n", input.c_str()); return 1; } segments = plan_codebase(g, manifest, &text); } else { text = slurp(input); if (text.empty()) { std::fprintf(stderr, "empty input\n"); return 1; } segments = plan(text, manifest); } std::fprintf(stderr, "[ctxstream] %zu chars -> %zu segments (<=%zu chars each)\n", text.size(), segments.size(), manifest.segment_chars); if (plan_only) { for (const auto& s : segments) { std::printf("SEG\t%d\t%zu\t%zu\n", s.index, s.offset, s.length); } return 0; } if (extract.empty()) { std::fprintf(stderr, "--extract is required unless --plan-only/--graph-only\n"); return 2; } const auto t0 = std::chrono::steady_clock::now(); const std::vector results = stream(text, segments, extract, backend, pipe); int failed = 0; for (const auto& r : results) { if (!r.ok) ++failed; } const Tally t = tally(results); Answer how = Answer::List; if (answer_mode == "least") how = Answer::Least; else if (answer_mode == "most") how = Answer::Most; else if (answer_mode == "total") how = Answer::Total; const double secs = std::chrono::duration( std::chrono::steady_clock::now() - t0).count(); std::fprintf(stderr, "[ctxstream] failed=%d/%zu records=%d unparsed_lines=%d keys=%zu %.1fs\n", failed, results.size(), t.parsed_records, t.unparsed_lines, t.sorted.size(), secs); // A partial sweep must never be reported as a clean answer. if (failed) { std::fprintf(stderr, "[ctxstream] REFUSING a headline answer: %d segment(s) failed, " "so every key is undercounted. Fix and rerun, or use --answer list.\n", failed); if (how != Answer::List) return 1; } std::cout << construct(t, how, label) << std::endl; return 0; }