@cowsea2012/distill 0.1.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +7 -0
  2. package/bin/distill.js +179 -0
  3. package/package.json +28 -0
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # @cowsea2012/distill
2
+
3
+ Install with:
4
+
5
+ ```bash
6
+ npm i -g @cowsea2012/distill
7
+ ```
package/bin/distill.js ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { spawn } = require("node:child_process");
4
+ const path = require("node:path");
5
+ const { createRequire } = require("node:module");
6
+
7
+ const requireFromHere = createRequire(__filename);
8
+ const cliPackage = require(path.join(__dirname, "..", "package.json"));
9
+
10
+ function getPlatformPackageName(target) {
11
+ const optionalDependencies = Object.keys(cliPackage.optionalDependencies ?? {});
12
+ return optionalDependencies.find((packageName) => packageName.endsWith(`-${target}`));
13
+ }
14
+
15
+ function resolveBinaryPath() {
16
+ const target = `${process.platform}-${process.arch}`;
17
+ const packageName = getPlatformPackageName(target);
18
+
19
+ if (!packageName) {
20
+ console.error(
21
+ `[distill] Unsupported platform: ${process.platform}/${process.arch}.`
22
+ );
23
+ process.exit(1);
24
+ }
25
+
26
+ try {
27
+ const packageJsonPath = requireFromHere.resolve(`${packageName}/package.json`);
28
+ return path.join(path.dirname(packageJsonPath), "bin", "distill");
29
+ } catch (error) {
30
+ console.error(
31
+ `[distill] Missing platform package ${packageName}. Reinstall ${cliPackage.name} for this platform.`
32
+ );
33
+ process.exit(1);
34
+ }
35
+ }
36
+
37
+ const PROGRESS_PREFIX = "__DISTILL_PROGRESS__:";
38
+ const PROGRESS_FRAMES = ["-", "\\", "|", "/"];
39
+ const PROGRESS_DOT_FRAMES = ["", ".", "..", "...", "..", "."];
40
+ const PROGRESS_LABELS = {
41
+ collecting: "distill: waiting",
42
+ summarizing: "distill: summarizing"
43
+ };
44
+
45
+ const binPath = resolveBinaryPath();
46
+ const progressWriter = process.stderr.isTTY ? process.stderr : process.stdout.isTTY ? process.stdout : null;
47
+ let progressPhase = "collecting";
48
+ let progressFrame = 0;
49
+ let progressTimer = null;
50
+ let progressVisible = false;
51
+ let childStderrBuffer = "";
52
+
53
+ function renderProgress() {
54
+ if (!progressWriter) {
55
+ return;
56
+ }
57
+
58
+ const frame = PROGRESS_FRAMES[progressFrame % PROGRESS_FRAMES.length];
59
+ const dots =
60
+ PROGRESS_DOT_FRAMES[
61
+ Math.floor(progressFrame / PROGRESS_FRAMES.length) % PROGRESS_DOT_FRAMES.length
62
+ ];
63
+ progressFrame += 1;
64
+ progressWriter.write(
65
+ `\r\u001b[2K${frame} ${PROGRESS_LABELS[progressPhase] || PROGRESS_LABELS.collecting}${dots}`
66
+ );
67
+ progressVisible = true;
68
+ }
69
+
70
+ function startProgress() {
71
+ if (!progressWriter || progressTimer) {
72
+ return;
73
+ }
74
+
75
+ renderProgress();
76
+ progressTimer = setInterval(renderProgress, 120);
77
+ }
78
+
79
+ function stopProgress() {
80
+ if (progressTimer) {
81
+ clearInterval(progressTimer);
82
+ progressTimer = null;
83
+ }
84
+
85
+ if (progressVisible && progressWriter) {
86
+ progressWriter.write("\r\u001b[2K");
87
+ progressVisible = false;
88
+ }
89
+ }
90
+
91
+ function handleChildStderrLine(line) {
92
+ if (!line) {
93
+ return;
94
+ }
95
+
96
+ if (!line.startsWith(PROGRESS_PREFIX)) {
97
+ stopProgress();
98
+ process.stderr.write(`${line}\n`);
99
+ return;
100
+ }
101
+
102
+ if (line === `${PROGRESS_PREFIX}stop`) {
103
+ stopProgress();
104
+ return;
105
+ }
106
+
107
+ if (line.startsWith(`${PROGRESS_PREFIX}phase:`)) {
108
+ progressPhase = line.slice(`${PROGRESS_PREFIX}phase:`.length) || "collecting";
109
+ progressFrame = 0;
110
+ renderProgress();
111
+ }
112
+ }
113
+
114
+ function flushChildStderr(force = false) {
115
+ if (!force && !childStderrBuffer.includes("\n")) {
116
+ return;
117
+ }
118
+
119
+ const parts = childStderrBuffer.split("\n");
120
+ childStderrBuffer = force ? "" : parts.pop() || "";
121
+
122
+ for (const line of parts) {
123
+ handleChildStderrLine(line);
124
+ }
125
+
126
+ if (force && childStderrBuffer) {
127
+ handleChildStderrLine(childStderrBuffer);
128
+ childStderrBuffer = "";
129
+ }
130
+ }
131
+
132
+ const child = spawn(binPath, process.argv.slice(2), {
133
+ stdio: ["inherit", "pipe", "pipe"],
134
+ env: {
135
+ ...process.env,
136
+ DISTILL_PROGRESS_PROTOCOL: "stderr"
137
+ }
138
+ });
139
+
140
+ startProgress();
141
+
142
+ child.stdout.on("data", (chunk) => {
143
+ stopProgress();
144
+ process.stdout.write(chunk);
145
+ });
146
+
147
+ child.stderr.on("data", (chunk) => {
148
+ childStderrBuffer += chunk.toString("utf8");
149
+ flushChildStderr();
150
+ });
151
+
152
+ const forwardSignal = (signal) => {
153
+ if (!child.killed) {
154
+ child.kill(signal);
155
+ }
156
+ };
157
+
158
+ ["SIGINT", "SIGTERM", "SIGHUP"].forEach((signal) => {
159
+ process.on(signal, () => forwardSignal(signal));
160
+ });
161
+
162
+ child.on("error", (error) => {
163
+ stopProgress();
164
+ console.error(`[distill] Failed to launch native binary: ${error.message}`);
165
+ process.exit(1);
166
+ });
167
+
168
+ child.on("exit", (code, signal) => {
169
+ flushChildStderr(true);
170
+ stopProgress();
171
+
172
+ if (signal) {
173
+ process.removeAllListeners(signal);
174
+ process.kill(process.pid, signal);
175
+ return;
176
+ }
177
+
178
+ process.exit(code ?? 1);
179
+ });
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@cowsea2012/distill",
3
+ "version": "0.1.31",
4
+ "description": "Compress command output for downstream LLMs.",
5
+ "license": "MIT",
6
+ "bin": {
7
+ "distill": "bin/distill.js"
8
+ },
9
+ "scripts": {
10
+ "prepack": "node ../../scripts/prepack-check.js"
11
+ },
12
+ "files": [
13
+ "bin",
14
+ "README.md"
15
+ ],
16
+ "engines": {
17
+ "node": ">=18"
18
+ },
19
+ "optionalDependencies": {
20
+ "@cowsea2012/distill-darwin-arm64": "0.1.31",
21
+ "@cowsea2012/distill-darwin-x64": "0.1.31",
22
+ "@cowsea2012/distill-linux-arm64": "0.1.31",
23
+ "@cowsea2012/distill-linux-x64": "0.1.31"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ }
28
+ }