token-goat 2.6.36 → 2.8.1

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 (34) hide show
  1. package/README.md +36 -8
  2. package/SECURITY.md +46 -17
  3. package/dist/{token-goat-chunk-UFOVM7ZN.mjs → token-goat-chunk-2G6RAB4G.mjs} +1391 -378
  4. package/dist/token-goat-chunk-324QOJYZ.mjs +91 -0
  5. package/dist/token-goat-chunk-5CVKO3DA.mjs +185 -0
  6. package/dist/{token-goat-chunk-65BKISIS.mjs → token-goat-chunk-73MF6YWW.mjs} +4 -4
  7. package/dist/{token-goat-chunk-MGOUYAA2.mjs → token-goat-chunk-7JDXDERZ.mjs} +1 -1
  8. package/dist/{token-goat-hook-chunk-BDR6C6IE.mjs → token-goat-chunk-AOBZUFNJ.mjs} +318 -51
  9. package/dist/{token-goat-chunk-V465YKOR.mjs → token-goat-chunk-ELDJRLHZ.mjs} +492 -28
  10. package/dist/{token-goat-chunk-CNDOJ3ZP.mjs → token-goat-chunk-I6TOUPLP.mjs} +2 -2
  11. package/dist/token-goat-chunk-R4SR7MQY.mjs +486 -0
  12. package/dist/{token-goat-chunk-IYTVE6KN.mjs → token-goat-chunk-RMDQFTQD.mjs} +226 -150
  13. package/dist/{token-goat-chunk-KYFJC37X.mjs → token-goat-chunk-VBXBLGTO.mjs} +792 -144
  14. package/dist/{token-goat-chunk-DG53MVNJ.mjs → token-goat-chunk-WN5T5EW5.mjs} +212 -212
  15. package/dist/{token-goat-chunk-VYMGEVZS.mjs → token-goat-chunk-XEDQH5DA.mjs} +317 -11
  16. package/dist/{token-goat-chunk-LN6OUHTV.mjs → token-goat-chunk-Y2SYNH3S.mjs} +5 -5
  17. package/dist/token-goat-chunk-YUGNM3KL.mjs +23 -0
  18. package/dist/token-goat-hook.mjs +7 -7
  19. package/dist/token-goat.core.mjs +5 -5
  20. package/package.json +12 -8
  21. package/dist/token-goat-chunk-FRTBMRP7.mjs +0 -10048
  22. package/dist/token-goat-hook-chunk-3QYSN4QV.mjs +0 -14764
  23. package/dist/token-goat-hook-chunk-3ZDBWJDF.mjs +0 -13659
  24. package/dist/token-goat-hook-chunk-5UH54CW6.mjs +0 -912
  25. package/dist/token-goat-hook-chunk-6ODM3MP7.mjs +0 -706
  26. package/dist/token-goat-hook-chunk-A77A26A7.mjs +0 -18184
  27. package/dist/token-goat-hook-chunk-BUOCULAM.mjs +0 -29
  28. package/dist/token-goat-hook-chunk-C6GIABOX.mjs +0 -15971
  29. package/dist/token-goat-hook-chunk-E257IGSN.mjs +0 -153
  30. package/dist/token-goat-hook-chunk-MW5HPEGD.mjs +0 -10411
  31. package/dist/token-goat-hook-chunk-QSCYNJ2B.mjs +0 -23
  32. package/dist/token-goat-hook-chunk-RFRLWOQH.mjs +0 -11
  33. package/dist/token-goat-hook-chunk-XUMIVYEN.mjs +0 -109
  34. package/dist/token-goat-hook-chunk-Y2WHX2P3.mjs +0 -6463
@@ -1,153 +0,0 @@
1
- import { createRequire as __cjsRequire } from 'node:module';
2
- const require = __cjsRequire(import.meta.url);
3
- import {
4
- ToolFilter,
5
- capTokens,
6
- combineStreams,
7
- compressOutput,
8
- filterByName,
9
- resolveMinNetSavingsBytes,
10
- selectFilter,
11
- shlexSplit,
12
- wrappedShell
13
- } from "./token-goat-hook-chunk-3ZDBWJDF.mjs";
14
- import {
15
- loadConfig,
16
- recordStat
17
- } from "./token-goat-hook-chunk-Y2WHX2P3.mjs";
18
- import "./token-goat-hook-chunk-BUOCULAM.mjs";
19
- import "./token-goat-hook-chunk-RFRLWOQH.mjs";
20
-
21
- // src/bash_runner.ts
22
- import { spawnSync } from "node:child_process";
23
- import * as os from "node:os";
24
- var IdentityFilter = class extends ToolFilter {
25
- name = "passthrough";
26
- };
27
- var DEFAULT_TIMEOUT_SECONDS = 600;
28
- var MAX_CAPTURE_BYTES = 32 * 1024 * 1024;
29
- var MIN_RECORD_STAT_BYTES = 32;
30
- function resolveFilter(command, filterName, cwd) {
31
- if (filterName) {
32
- const named = filterByName(filterName);
33
- if (named !== null) return named;
34
- }
35
- let argv;
36
- try {
37
- argv = shlexSplit(command);
38
- } catch {
39
- return null;
40
- }
41
- return selectFilter(argv, cwd);
42
- }
43
- function baseSpawnOptions(timeout, cwd, env) {
44
- return { shell: wrappedShell(), timeout: timeout * 1e3, cwd, env };
45
- }
46
- function signalExitCode(signal) {
47
- const num = os.constants.signals[signal];
48
- return 128 + (num ?? 0);
49
- }
50
- function resolveProfile(explicit) {
51
- if (explicit) return explicit;
52
- try {
53
- const p = loadConfig().compression.profile;
54
- return p && p !== "auto" ? p : "balanced";
55
- } catch {
56
- return "balanced";
57
- }
58
- }
59
- function resolveCompressLimits() {
60
- try {
61
- const bc = loadConfig().bash_compress;
62
- return { maxLines: bc.max_lines, maxBytes: bc.max_bytes };
63
- } catch {
64
- return { maxLines: 1e3, maxBytes: 64 * 1024 };
65
- }
66
- }
67
- function run(command, opts = {}) {
68
- const timeout = opts.timeout ?? DEFAULT_TIMEOUT_SECONDS;
69
- const filter = resolveFilter(command, opts.filterName, opts.cwd);
70
- if (filter === null) {
71
- if ((opts.maxTokens ?? 0) > 0) {
72
- return wrapAndCompress(command, new IdentityFilter(), timeout, resolveProfile(opts.compressionProfile), opts);
73
- }
74
- return passthrough(command, timeout, opts.cwd, opts.env);
75
- }
76
- return wrapAndCompress(command, filter, timeout, resolveProfile(opts.compressionProfile), opts);
77
- }
78
- function runRaw(command, timeout = DEFAULT_TIMEOUT_SECONDS) {
79
- return passthrough(command, timeout, void 0, void 0);
80
- }
81
- function passthrough(command, timeout, cwd, env) {
82
- const result = spawnSync(command, { ...baseSpawnOptions(timeout, cwd, env), stdio: "inherit" });
83
- if (isTimeout(result.error)) return 124;
84
- if (result.status !== null) return result.status;
85
- if (result.signal) return signalExitCode(result.signal);
86
- return 0;
87
- }
88
- function isTimeout(error) {
89
- return !!error && error.code === "ETIMEDOUT";
90
- }
91
- function decode(buf) {
92
- if (buf == null) return "";
93
- return typeof buf === "string" ? buf : buf.toString("utf8");
94
- }
95
- function wrapAndCompress(command, filter, timeout, profile, opts) {
96
- const writeStdout = opts.writeStdout ?? ((s) => process.stdout.write(s));
97
- const result = spawnSync(command, {
98
- ...baseSpawnOptions(timeout, opts.cwd, opts.env),
99
- stdio: ["ignore", "pipe", "pipe"],
100
- maxBuffer: MAX_CAPTURE_BYTES
101
- });
102
- const timedOut = isTimeout(result.error);
103
- const overflowed = !!result.error && result.error.code === "ENOBUFS";
104
- let stdoutText = decode(result.stdout);
105
- let stderrText = decode(result.stderr);
106
- if (overflowed) {
107
- stdoutText += `
108
- [token-goat: capture capped at ${MAX_CAPTURE_BYTES / (1024 * 1024)} MiB]`;
109
- }
110
- let exitCode;
111
- if (timedOut) {
112
- exitCode = 124;
113
- stderrText = (stderrText ? stderrText + "\n" : "") + `[token-goat: command exceeded ${timeout}s timeout and was killed]`;
114
- } else if (result.status !== null) {
115
- exitCode = result.status;
116
- } else if (result.signal) {
117
- exitCode = signalExitCode(result.signal);
118
- } else {
119
- exitCode = 0;
120
- }
121
- let argv;
122
- try {
123
- argv = shlexSplit(command);
124
- } catch {
125
- argv = [command];
126
- }
127
- const limits = resolveCompressLimits();
128
- const compressed = compressOutput(filter, stdoutText, stderrText, exitCode, argv, {
129
- compressionProfile: profile,
130
- maxLines: limits.maxLines,
131
- maxBytes: limits.maxBytes
132
- });
133
- const minNetSavingsBytes = resolveMinNetSavingsBytes();
134
- const applied = compressed.worthApplying(minNetSavingsBytes);
135
- let text = applied ? compressed.text : combineStreams(stdoutText, stderrText);
136
- const maxTokens = opts.maxTokens ?? 0;
137
- if (maxTokens > 0) text = capTokens(text, maxTokens);
138
- const marker = applied ? compressed.withMarker(minNetSavingsBytes).slice(compressed.text.length) : "";
139
- const body = text + marker;
140
- writeStdout(body.endsWith("\n") ? body : body + "\n");
141
- if (applied) recordSavings(compressed);
142
- return exitCode;
143
- }
144
- function recordSavings(result) {
145
- if (result.bytesSaved < MIN_RECORD_STAT_BYTES) return;
146
- recordStat(`bash_compress:${result.filterName}`, result.bytesSaved, result.tokensSaved);
147
- }
148
- export {
149
- DEFAULT_TIMEOUT_SECONDS,
150
- MAX_CAPTURE_BYTES,
151
- run,
152
- runRaw
153
- };