Datasets:
File size: 6,495 Bytes
829dbce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | #include "ctxstream.hpp"
#include <chrono>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
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 <file|dir> --extract <prompt> [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 <key>\\t<number>\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<Segment> 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<SegmentResult> 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<double>(
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;
}
|