@sliday/tamp 0.4.8 → 0.5.0

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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Tamp
2
2
 
3
- **Token compression proxy for coding agents.** 52.6% fewer tokens, zero code changes. Works with Claude Code, Aider, Cursor, Cline, Windsurf, 🦞 [OpenClaw](https://openclaw.app), and any OpenAI-compatible agent.
3
+ **Token compression proxy for coding agents.** 52.6% fewer input tokens, 60-70% combined with output compression. Zero code changes. Works with Claude Code, Codex CLI, Aider, Cursor, Cline, Windsurf, 🦞 [OpenClaw](https://openclaw.app), and any OpenAI-compatible agent.
4
4
 
5
5
  ```
6
6
  npx @sliday/tamp
@@ -58,7 +58,8 @@ curl -fsSL https://tamp.dev/setup.sh | bash
58
58
 
59
59
  # Option B: Manual
60
60
  npx @sliday/tamp
61
- export ANTHROPIC_BASE_URL=http://localhost:7778
61
+ export ANTHROPIC_BASE_URL=http://localhost:7778 # Claude Code
62
+ export OPENAI_API_BASE=http://localhost:7778/v1 # Codex CLI, Aider, Cursor
62
63
  ```
63
64
 
64
65
  Use your agent as normal — Tamp compresses silently.
@@ -67,11 +68,45 @@ Use your agent as normal — Tamp compresses silently.
67
68
 
68
69
  Run `tamp init` to create `~/.config/tamp/config`. All variables work via env or config file.
69
70
 
71
+ ### Compression Presets (New!)
72
+
73
+ Simplify configuration with three intensity levels:
74
+
75
+ | Preset | Savings | Description |
76
+ |--------|---------|-------------|
77
+ | `conservative` | 45-50% | Lossless only (no neural) |
78
+ | `balanced` (default) | 52-58% | Recommended, includes LLMLingua |
79
+ | `aggressive` | 60-68% | Maximum, lossy stages enabled |
80
+
81
+ ```bash
82
+ # Use a preset
83
+ export TAMP_COMPRESSION_PRESET=balanced
84
+
85
+ # Or override specific stages
86
+ TAMP_COMPRESSION_PRESET=balanced
87
+ TAMP_STAGES=minify,toon # Override preset
88
+ ```
89
+
90
+ ### Output Compression (New!)
91
+
92
+ Task-type-aware output compression via CLAUDE.md injection:
93
+
94
+ ```bash
95
+ export TAMP_OUTPUT_MODE=balanced # conservative | balanced | aggressive
96
+ export TAMP_AUTO_DETECT_TASK_TYPE=true # Auto-detect safe vs dangerous tasks
97
+ ```
98
+
99
+ **Mode behavior:**
100
+ - **Conservative**: Professional but concise (40-50% output savings)
101
+ - **Balanced**: Terse on safe tasks, full output on dangerous (65-75% on safe)
102
+ - **Aggressive**: Minimal caveman-style (75-85% on safe, partial on dangerous)
103
+
104
+ ### Other Settings
105
+
70
106
  | Variable | Default | Description |
71
107
  |----------|---------|-------------|
72
108
  | `TAMP_PORT` | `7778` | Listen port |
73
109
  | `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream |
74
- | `TAMP_STAGES` | *(all default)* | Comma-separated stages |
75
110
  | `TAMP_MIN_SIZE` | `200` | Min content size (chars) |
76
111
  | `TAMP_LOG` | `true` | Enable logging |
77
112
  | `TAMP_CACHE_SAFE` | `true` | Compress newest only (prompt-cache safe) |
@@ -80,16 +115,35 @@ Run `tamp init` to create `~/.config/tamp/config`. All variables work via env or
80
115
  **Recommended setups:**
81
116
 
82
117
  ```bash
83
- # Default (all stages)
118
+ # Default (balanced preset)
84
119
  npx @sliday/tamp
85
120
 
86
- # No Python (skip LLMLingua)
87
- TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune npx @sliday/tamp -y
121
+ # Conservative (no Python, lossless only)
122
+ TAMP_COMPRESSION_PRESET=conservative npx @sliday/tamp -y
88
123
 
89
- # Re-compress history too
90
- TAMP_CACHE_SAFE=false npx @sliday/tamp
124
+ # Aggressive (maximum compression)
125
+ TAMP_COMPRESSION_PRESET=aggressive npx @sliday/tamp -y
91
126
  ```
92
127
 
128
+ ## CLI Tools
129
+
130
+ ### compress-config (New!)
131
+
132
+ Compress CLAUDE.md and config files by 40-45%:
133
+
134
+ ```bash
135
+ # Dry run (preview savings)
136
+ tamp compress-config --dry-run ~/.claude/CLAUDE.md
137
+
138
+ # Compress with backup
139
+ tamp compress-config ~/.claude/CLAUDE.md
140
+
141
+ # Compress multiple files
142
+ tamp compress-config ~/.config/tamp/config ~/.claude/CLAUDE.md
143
+ ```
144
+
145
+ Inspired by [JuliusBrussee/caveman-compress](https://github.com/JuliusBrussee/caveman).
146
+
93
147
  ## Installation
94
148
 
95
149
  ```bash
@@ -109,11 +163,11 @@ tamp status
109
163
 
110
164
  Claude Code sends full conversation history on every API call. Tool results accumulate — files, listings, outputs — all re-sent as input tokens.
111
165
 
112
- **With 52.6% average compression:** Save $0.19–$0.32 per 200-request session (Sonnet/Opus 4.6). Max subscribers get 47% more requests from fixed budgets. See [whitepaper PDF](site/whitepaper.pdf) for full benchmarks.
166
+ **With 52.6% average input compression:** Save $0.19–$0.32 per 200-request session (Sonnet/Opus 4.6). Max subscribers get 47% more requests from fixed budgets. See [whitepaper PDF](site/whitepaper.pdf) for full benchmarks.
113
167
 
114
- **Output savings:** Tamp injects token-efficient rules into your `CLAUDE.md`, reducing output tokens by 66.2%. Inspired by [drona23/claude-token-efficient](https://github.com/drona23/claude-token-efficient).
168
+ **Output compression (new):** Task-type-aware rules reduce output tokens by 65-75% on safe tasks (env vars, typos, docs) while preserving full output for dangerous tasks (security, debugging). Inspired by [JuliusBrussee/caveman](https://github.com/JuliusBrussee/caveman).
115
169
 
116
- **Caveman Mode evaluation:** Tested extreme compression (40-70% additional savings) but found unsafe for production. Recommended: task-type-aware compression (78% savings on safe tasks like env vars/typos/docs). Full evaluation: [`bench/caveman-mode-evaluation.md`](bench/caveman-mode-evaluation.md).
170
+ **Combined impact:** With new Caveman-integrated features, Tamp achieves 60-70% total token savings (input + output) in balanced mode.
117
171
 
118
172
  ## Development
119
173
 
package/compress.js CHANGED
@@ -5,6 +5,7 @@ import { countTokens } from '@anthropic-ai/tokenizer'
5
5
  import { createPatch } from 'diff'
6
6
  import { tryParseJSON, classifyContent, stripLineNumbers } from './detect.js'
7
7
  import { anthropic } from './providers.js'
8
+ import { graphDeduplicateTargets } from './session-graph.js'
8
9
 
9
10
  const cache = new Map()
10
11
  const MAX_CACHE = 500
@@ -316,12 +317,24 @@ async function compressWithFoundationModels(text, config) {
316
317
 
317
318
  // Validate foundationModelsPath to prevent command injection
318
319
  const cmdPath = config.foundationModelsPath
320
+
321
+ // Reject shell metacharacters and command injection attempts
322
+ const dangerousChars = /[;&|`$()<>]/
323
+ if (dangerousChars.test(cmdPath)) {
324
+ return null
325
+ }
326
+
319
327
  // Allow only simple command names (no path separators) or absolute paths
320
328
  if (cmdPath.includes('/') || cmdPath.includes('\\')) {
321
329
  // If it contains path separators, must be an absolute path
322
330
  if (!cmdPath.startsWith('/')) {
323
331
  return null
324
332
  }
333
+ // Additional check: prevent path traversal in absolute paths
334
+ const normalizedPath = cmdPath.replace(/\.\./g, '')
335
+ if (normalizedPath !== cmdPath) {
336
+ return null
337
+ }
325
338
  }
326
339
 
327
340
  const SYSTEM_PROMPT = config.foundationModelsSystemPrompt ||
@@ -422,11 +435,16 @@ export async function compressRequest(body, config, provider) {
422
435
  // Diff: replace similar blocks with unified diffs
423
436
  if (config.stages.includes('diff')) diffTargets(targets)
424
437
 
438
+ // Graph: session-scoped dedup across requests (opt-in)
439
+ if (config.stages.includes('graph') && config.sessionBucket) {
440
+ graphDeduplicateTargets(targets, config.sessionBucket)
441
+ }
442
+
425
443
  const stats = []
426
444
  for (const target of targets) {
427
445
  if (target.skip) { stats.push({ index: target.index, skipped: target.skip }); continue }
428
446
 
429
- // Dedup/diff already set .compressed — record stats and skip compression
447
+ // Dedup/diff/graph already set .compressed — record stats and skip compression
430
448
  if (target.dedup) {
431
449
  stats.push({ index: target.index, method: 'dedup', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
432
450
  continue
@@ -435,6 +453,10 @@ export async function compressRequest(body, config, provider) {
435
453
  stats.push({ index: target.index, method: 'diff', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
436
454
  continue
437
455
  }
456
+ if (target.graphed) {
457
+ stats.push({ index: target.index, method: 'graph', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
458
+ continue
459
+ }
438
460
 
439
461
  const result = await compressBlock(target.text, config)
440
462
  if (result) {
package/config.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { readFileSync } from 'node:fs'
2
2
  import { homedir } from 'node:os'
3
3
  import { join } from 'node:path'
4
- import { DEFAULT_STAGES, VERSION } from './metadata.js'
4
+ import { DEFAULT_STAGES, VERSION, COMPRESSION_PRESETS } from './metadata.js'
5
5
 
6
6
  function parseBoolean(value, defaultValue) {
7
7
  if (value === undefined) return defaultValue
@@ -10,6 +10,22 @@ function parseBoolean(value, defaultValue) {
10
10
  return defaultValue
11
11
  }
12
12
 
13
+ function resolvePreset(presetName, explicitStages) {
14
+ // If user explicitly set TAMP_STAGES, ignore preset
15
+ if (explicitStages && explicitStages.length > 0) {
16
+ return explicitStages
17
+ }
18
+
19
+ // Resolve preset name to stages
20
+ const preset = COMPRESSION_PRESETS[presetName]
21
+ if (preset) {
22
+ return preset.stages
23
+ }
24
+
25
+ // Default to balanced preset if preset name not recognized
26
+ return COMPRESSION_PRESETS.balanced.stages
27
+ }
28
+
13
29
  export const CONFIG_PATH = join(homedir(), '.config', 'tamp', 'config')
14
30
 
15
31
  export function loadConfigFile(path) {
@@ -37,7 +53,16 @@ export function loadConfig(env = process.env) {
37
53
  const fileVars = (env === process.env) ? loadConfigFile() : {}
38
54
  const get = (key) => env[key] !== undefined ? env[key] : fileVars[key]
39
55
 
40
- const stages = (get('TAMP_STAGES') || DEFAULT_STAGES.join(',')).split(',').map(s => s.trim()).filter(Boolean)
56
+ // Get explicit stages (if user set TAMP_STAGES directly)
57
+ const explicitStagesStr = get('TAMP_STAGES')
58
+ const explicitStages = explicitStagesStr ? explicitStagesStr.split(',').map(s => s.trim()).filter(Boolean) : []
59
+
60
+ // Get compression preset
61
+ const presetName = get('TAMP_COMPRESSION_PRESET') || 'balanced'
62
+
63
+ // Resolve preset to stages (or use explicit stages if provided)
64
+ const stages = resolvePreset(presetName, explicitStages)
65
+
41
66
  return Object.freeze({
42
67
  version: VERSION,
43
68
  port: parseInt(get('TAMP_PORT'), 10) || 7778,
@@ -45,10 +70,14 @@ export function loadConfig(env = process.env) {
45
70
  upstreams: Object.freeze({
46
71
  anthropic: get('TAMP_UPSTREAM') || 'https://api.anthropic.com',
47
72
  openai: get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
73
+ 'openai-responses': get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
48
74
  gemini: get('TAMP_UPSTREAM_GEMINI') || 'https://generativelanguage.googleapis.com',
49
75
  }),
50
76
  minSize: parseInt(get('TAMP_MIN_SIZE'), 10) || 200,
51
77
  stages,
78
+ preset: presetName,
79
+ outputMode: get('TAMP_OUTPUT_MODE') || 'balanced',
80
+ autoDetectTaskType: parseBoolean(get('TAMP_AUTO_DETECT_TASK_TYPE'), true),
52
81
  log: get('TAMP_LOG') !== 'false',
53
82
  logFile: get('TAMP_LOG_FILE') || null,
54
83
  maxBody: parseInt(get('TAMP_MAX_BODY'), 10) || 10_485_760,
@@ -73,7 +102,19 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
73
102
  # TAMP_UPSTREAM=https://api.anthropic.com
74
103
  # TAMP_UPSTREAM_OPENAI=https://api.openai.com
75
104
  # TAMP_UPSTREAM_GEMINI=https://generativelanguage.googleapis.com
105
+
106
+ # Compression preset (conservative | balanced | aggressive)
107
+ # TAMP_COMPRESSION_PRESET=balanced
108
+
109
+ # Explicit stages (overrides preset if set)
76
110
  # TAMP_STAGES=minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune
111
+
112
+ # Output compression mode (conservative | balanced | aggressive)
113
+ # TAMP_OUTPUT_MODE=balanced
114
+
115
+ # Auto-detect task type for output compression (true | false)
116
+ # TAMP_AUTO_DETECT_TASK_TYPE=true
117
+
77
118
  # TAMP_MIN_SIZE=200
78
119
  # TAMP_LOG=true
79
120
  # TAMP_LOG_FILE=
package/index.js CHANGED
@@ -7,6 +7,7 @@ import { loadConfig } from './config.js'
7
7
  import { compressRequest } from './compress.js'
8
8
  import { detectProvider } from './providers.js'
9
9
  import { createSession, formatRequestLog } from './stats.js'
10
+ import { createSessionStore, deriveSessionKey } from './session-graph.js'
10
11
 
11
12
  function buildUpstreamUrl(reqPath, base) {
12
13
  const parsed = new URL(base)
@@ -27,13 +28,14 @@ export function createProxy(overrides = {}) {
27
28
  const base = loadConfig()
28
29
  const config = { ...base, ...overrides }
29
30
  if (overrides.upstream && !overrides.upstreams) {
30
- config.upstreams = { anthropic: overrides.upstream, openai: overrides.upstream, gemini: overrides.upstream }
31
+ config.upstreams = { anthropic: overrides.upstream, openai: overrides.upstream, 'openai-responses': overrides.upstream, gemini: overrides.upstream }
31
32
  }
32
33
  const session = createSession()
33
- return { config, session, server: _createServer(config, session) }
34
+ const sessionStore = createSessionStore()
35
+ return { config, session, sessionStore, server: _createServer(config, session, sessionStore) }
34
36
  }
35
37
 
36
- function _createServer(config, session) {
38
+ function _createServer(config, session, sessionStore) {
37
39
  const log = createRequestLogger(config)
38
40
 
39
41
  function openUpstream(method, upstreamUrl, headers, res) {
@@ -209,7 +211,10 @@ return http.createServer(async (req, res) => {
209
211
  try {
210
212
  const parsed = JSON.parse(textBody.toString('utf-8'))
211
213
 
212
- const { body, stats } = await compressRequest(parsed, config, provider)
214
+ const sessionBucket = config.stages?.includes('graph')
215
+ ? sessionStore.getBucket(deriveSessionKey(req.headers))
216
+ : null
217
+ const { body, stats } = await compressRequest(parsed, { ...config, sessionBucket }, provider)
213
218
  finalBody = Buffer.from(JSON.stringify(body), 'utf-8')
214
219
  // Send uncompressed — simpler and content-length is accurate
215
220
  if (decompressed) delete headers['content-encoding']
@@ -0,0 +1,241 @@
1
+ /**
2
+ * File compression utilities for Tamp config and CLAUDE.md files
3
+ * Inspired by JuliusBrussee/caveman-compress
4
+ */
5
+
6
+ import { readFile, writeFile, copyFile } from 'node:fs/promises'
7
+ import { existsSync } from 'node:fs'
8
+ import { basename, extname } from 'node:path'
9
+ import { compressText } from '../compress.js'
10
+
11
+ /**
12
+ * Compression strategies for different file types
13
+ */
14
+ const STRATEGIES = Object.freeze({
15
+ 'claude-rules': {
16
+ stages: ['textpress', 'strip-comments', 'whitespace'],
17
+ preserveStructure: true,
18
+ minSize: 100,
19
+ },
20
+ markdown: {
21
+ stages: ['textpress', 'whitespace'],
22
+ preserveStructure: true,
23
+ minSize: 200,
24
+ },
25
+ json: {
26
+ stages: ['minify', 'toon', 'prune'],
27
+ preserveStructure: false,
28
+ minSize: 100,
29
+ },
30
+ text: {
31
+ stages: ['textpress', 'whitespace'],
32
+ preserveStructure: true,
33
+ minSize: 200,
34
+ },
35
+ })
36
+
37
+ /**
38
+ * Detect file strategy based on path and content
39
+ * @param {string} filePath - Path to file
40
+ * @returns {string} - Strategy name
41
+ */
42
+ export function detectFileStrategy(filePath) {
43
+ const filename = basename(filePath).toLowerCase()
44
+
45
+ // CLAUDE.md files get special treatment
46
+ if (filename.includes('claude.md')) {
47
+ return 'claude-rules'
48
+ }
49
+
50
+ const ext = extname(filePath).toLowerCase()
51
+
52
+ if (ext === '.md') return 'markdown'
53
+ if (ext === '.json') return 'json'
54
+ return 'text'
55
+ }
56
+
57
+ /**
58
+ * Compress a file's content
59
+ * @param {string} content - File content
60
+ * @param {string} strategy - Strategy name
61
+ * @param {object} options - Compression options
62
+ * @returns {Promise<{success: boolean, compressed?: string, originalLen?: number, compressedLen?: number, method?: string, skipped?: string}>}
63
+ */
64
+ export async function compressContent(content, strategy, options = {}) {
65
+ const strategyConfig = STRATEGIES[strategy] || STRATEGIES.text
66
+
67
+ // Build compression config
68
+ const compressConfig = {
69
+ minSize: strategyConfig.minSize,
70
+ stages: strategyConfig.stages,
71
+ log: options.log || false,
72
+ ...options.compressionConfig,
73
+ }
74
+
75
+ try {
76
+ const result = compressText(content, compressConfig)
77
+
78
+ if (!result) {
79
+ return {
80
+ success: false,
81
+ skipped: 'not-compressible',
82
+ originalLen: content.length,
83
+ }
84
+ }
85
+
86
+ return {
87
+ success: true,
88
+ compressed: result.text,
89
+ originalLen: content.length,
90
+ compressedLen: result.text.length,
91
+ method: result.method || 'unknown',
92
+ savings: ((content.length - result.text.length) / content.length * 100).toFixed(1) + '%',
93
+ }
94
+ } catch (error) {
95
+ return {
96
+ success: false,
97
+ skipped: error.message,
98
+ originalLen: content.length,
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * Compress a file with backup
105
+ * @param {string} filePath - Path to file
106
+ * @param {object} options - Options
107
+ * @returns {Promise<object>} - Result object
108
+ */
109
+ export async function compressFile(filePath, options = {}) {
110
+ // Validate file exists
111
+ if (!existsSync(filePath)) {
112
+ return {
113
+ success: false,
114
+ error: `File not found: ${filePath}`,
115
+ }
116
+ }
117
+
118
+ // Read file
119
+ const content = await readFile(filePath, 'utf8')
120
+ const originalLen = content.length
121
+
122
+ // Detect strategy
123
+ const strategy = options.strategy || detectFileStrategy(filePath)
124
+
125
+ // Compress content
126
+ const result = await compressContent(content, strategy, options)
127
+
128
+ if (!result.success) {
129
+ return {
130
+ ...result,
131
+ file: filePath,
132
+ strategy,
133
+ originalLen,
134
+ }
135
+ }
136
+
137
+ // Dry run mode - don't write file
138
+ if (options.dryRun) {
139
+ return {
140
+ ...result,
141
+ file: filePath,
142
+ strategy,
143
+ dryRun: true,
144
+ }
145
+ }
146
+
147
+ // Create backup
148
+ const backupPath = filePath + '.bak'
149
+ if (!options.noBackup) {
150
+ try {
151
+ await copyFile(filePath, backupPath)
152
+ } catch (error) {
153
+ return {
154
+ success: false,
155
+ error: `Failed to create backup: ${error.message}`,
156
+ file: filePath,
157
+ }
158
+ }
159
+ }
160
+
161
+ // Write compressed content
162
+ const outputPath = options.output || filePath
163
+ try {
164
+ await writeFile(outputPath, result.compressed, 'utf8')
165
+ } catch (error) {
166
+ return {
167
+ success: false,
168
+ error: `Failed to write file: ${error.message}`,
169
+ file: filePath,
170
+ }
171
+ }
172
+
173
+ return {
174
+ ...result,
175
+ file: filePath,
176
+ output: outputPath,
177
+ strategy,
178
+ backup: options.noBackup ? null : backupPath,
179
+ dryRun: false,
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Compress multiple files
185
+ * @param {string[]} filePaths - Array of file paths
186
+ * @param {object} options - Options
187
+ * @returns {Promise<object[]>} - Array of results
188
+ */
189
+ export async function compressFiles(filePaths, options = {}) {
190
+ const results = []
191
+
192
+ for (const filePath of filePaths) {
193
+ const result = await compressFile(filePath, options)
194
+ results.push(result)
195
+ }
196
+
197
+ return results
198
+ }
199
+
200
+ /**
201
+ * Format compression result for CLI output
202
+ * @param {object} result - Result object
203
+ * @returns {string} - Formatted output
204
+ */
205
+ export function formatResult(result) {
206
+ if (result.dryRun) {
207
+ return `DRY RUN: ${result.file}\n` +
208
+ ` Strategy: ${result.strategy}\n` +
209
+ ` Original: ${result.originalLen} chars\n` +
210
+ ` Compressed: ${result.compressedLen} chars\n` +
211
+ ` Savings: ${result.savings}\n` +
212
+ ` Method: ${result.method}`
213
+ }
214
+
215
+ if (result.success) {
216
+ let output = `✓ ${result.file}\n` +
217
+ ` Strategy: ${result.strategy}\n` +
218
+ ` Original: ${result.originalLen} chars\n` +
219
+ ` Compressed: ${result.compressedLen} chars\n` +
220
+ ` Savings: ${result.savings}\n` +
221
+ ` Method: ${result.method}`
222
+
223
+ if (result.backup) {
224
+ output += `\n Backup: ${result.backup}`
225
+ }
226
+
227
+ if (result.output !== result.file) {
228
+ output += `\n Output: ${result.output}`
229
+ }
230
+
231
+ return output
232
+ }
233
+
234
+ if (result.skipped) {
235
+ return `⊘ ${result.file || 'N/A'}\n` +
236
+ ` Skipped: ${result.skipped}`
237
+ }
238
+
239
+ return `✗ ${result.file || 'N/A'}\n` +
240
+ ` Error: ${result.error || 'Unknown error'}`
241
+ }
@@ -0,0 +1,215 @@
1
+ /**
2
+ * Task-type-aware output compression rules generator
3
+ * Inspired by JuliusBrussee/caveman evaluation findings
4
+ */
5
+
6
+ // Safe task patterns: simple, well-defined changes
7
+ const SAFE_TASK_PATTERNS = [
8
+ /^(add|remove|update|set|unset)\s+(env\s+var|environment variable|config|configuration|\w+=)/i,
9
+ /^fix\s+typo/i,
10
+ /^update\s+(README|readme|documentation|docs)/i,
11
+ /^(install|uninstall).*package/i,
12
+ /^add\s+\w+\s+(as\s+)?dependency/i,
13
+ /^update\s+version/i,
14
+ /^(format|lint|fmt)|run linter/i,
15
+ ]
16
+
17
+ // Dangerous task patterns: require detailed output
18
+ const DANGEROUS_TASK_PATTERNS = [
19
+ /security|vulnerability|exploit|attack/i,
20
+ /debug|investigate|diagnose|troubleshoot/i,
21
+ /memory leak|performance|optimization|optimize/i,
22
+ /^refactor|^architecture|^design/i,
23
+ /^fix\s+bug/i,
24
+ /^explain|^why|^how\s+(does|work)/i,
25
+ /test|spec|coverage/i,
26
+ ]
27
+
28
+ /**
29
+ * Detect task type from user message
30
+ * @param {string} userMessage - User's input message
31
+ * @returns {'safe' | 'dangerous' | 'complex'} - Task type classification
32
+ */
33
+ export function detectTaskType(userMessage) {
34
+ if (!userMessage || typeof userMessage !== 'string') return 'complex'
35
+
36
+ const task = userMessage.toLowerCase().trim()
37
+
38
+ // Check dangerous patterns first (safety priority)
39
+ if (DANGEROUS_TASK_PATTERNS.some(p => p.test(task))) {
40
+ return 'dangerous'
41
+ }
42
+
43
+ // Then check safe patterns
44
+ if (SAFE_TASK_PATTERNS.some(p => p.test(task))) {
45
+ return 'safe'
46
+ }
47
+
48
+ // Default to complex (ambiguous tasks)
49
+ return 'complex'
50
+ }
51
+
52
+ /**
53
+ * Output compression intensity levels
54
+ */
55
+ export const OUTPUT_MODES = Object.freeze({
56
+ conservative: {
57
+ name: 'Conservative',
58
+ description: 'Professional but concise',
59
+ outputStyle: 'professional-concise',
60
+ safeTaskCompression: 'none', // Full output
61
+ dangerousTaskCompression: 'none', // Full output
62
+ expectedSavings: '40-50%',
63
+ },
64
+ balanced: {
65
+ name: 'Balanced',
66
+ description: 'Terse, bullet points, task-type-aware',
67
+ outputStyle: 'terse-bullets',
68
+ safeTaskCompression: 'aggressive', // Compressed
69
+ dangerousTaskCompression: 'none', // Full output
70
+ expectedSavings: '65-75% on safe tasks',
71
+ },
72
+ aggressive: {
73
+ name: 'Aggressive',
74
+ description: 'Minimal caveman-style',
75
+ outputStyle: 'minimal-caveman',
76
+ safeTaskCompression: 'maximum', // Highly compressed
77
+ dangerousTaskCompression: 'partial', // Some compression
78
+ expectedSavings: '75-85% on safe tasks',
79
+ },
80
+ })
81
+
82
+ /**
83
+ * Generate optimized CLAUDE.md rules for output compression
84
+ * @param {string} mode - Output mode (conservative | balanced | aggressive)
85
+ * @param {string} taskType - Task type (safe | dangerous | complex)
86
+ * @returns {string} - Optimized CLAUDE.md rules text
87
+ */
88
+ export function generateOutputRules(mode = 'balanced', taskType = 'complex') {
89
+ const modeConfig = OUTPUT_MODES[mode] || OUTPUT_MODES.balanced
90
+
91
+ // Conservative mode always generates minimal rules (for all task types)
92
+ if (mode === 'conservative') {
93
+ return generateConservativeRules()
94
+ }
95
+
96
+ // Determine if we should compress based on mode + task type
97
+ const shouldCompress = {
98
+ safe: modeConfig.safeTaskCompression !== 'none',
99
+ dangerous: modeConfig.dangerousTaskCompression === 'partial',
100
+ complex: mode === 'aggressive', // Only compress complex in aggressive mode
101
+ }[taskType]
102
+
103
+ // If compression disabled for this task type, return empty
104
+ if (!shouldCompress) {
105
+ return ''
106
+ }
107
+
108
+ // Generate rules based on mode
109
+ const rules = {
110
+ balanced: generateBalancedRules(taskType),
111
+ aggressive: generateAggressiveRules(taskType),
112
+ }
113
+
114
+ return rules[mode] || generateBalancedRules(taskType)
115
+ }
116
+
117
+ function generateConservativeRules() {
118
+ return `## Token-Efficient Output (Conservative Mode)
119
+
120
+ Be concise but professional. Skip fluff, pleasantries, and meta-commentary.
121
+ - Use bullet points for lists
122
+ - Prefer active voice
123
+ - Avoid "Here's the summary" or "Let me explain" intros
124
+ - Return code first, explanation after if non-obvious
125
+ `
126
+ }
127
+
128
+ function generateBalancedRules(taskType) {
129
+ const baseRules = `## Token-Efficient Output (Balanced Mode)
130
+
131
+ **Output Style:** Terse, actionable, task-appropriate.
132
+
133
+ ### Core Rules
134
+ - Skip ALL pleasantries ("Sure!", "Great question!", "I hope this helps!")
135
+ - No sycophantic openers or closing fluff
136
+ - Return code first. Explanation after, only if non-obvious
137
+ - Use bullet points for multi-step explanations
138
+ - Single-sentence responses when possible
139
+ - No "Here's what I'll do" preambles — just do it
140
+ `
141
+
142
+ if (taskType === 'safe') {
143
+ return baseRules + `
144
+ ### Safe Task Optimizations (Active)
145
+ This task classified as SAFE — maximum compression enabled:
146
+ - One-line responses when possible
147
+ - Omit obvious explanations
148
+ - Minimal context, focus on change
149
+ - Code-only if change is self-explanatory
150
+ `
151
+ }
152
+
153
+ return baseRules
154
+ }
155
+
156
+ function generateAggressiveRules(taskType) {
157
+ const baseRules = `## Token-Efficient Output (Aggressive Mode)
158
+
159
+ **Output Style:** Minimal. Caveman-like but functional.
160
+
161
+ ### Core Rules
162
+ - NEVER use pleasantries, intros, or summaries
163
+ - Code ONLY if change is self-explanatory
164
+ - One-word responses when possible (done, fixed, added, removed)
165
+ - Single bullet point per issue
166
+ - Omit ALL non-essential context
167
+ - No "## Section" headers in responses
168
+ - No markdown formatting unless code block
169
+ `
170
+
171
+ if (taskType === 'safe') {
172
+ return baseRules + `
173
+ ### Safe Task Maximum Compression
174
+ - Name of file changed + status. That's it.
175
+ - No explanation unless ambiguous
176
+ - One character response: ✓ or ✗ if clear
177
+ `
178
+ } else if (taskType === 'dangerous') {
179
+ return baseRules + `
180
+ ### Dangerous Task Partial Compression
181
+ - Bullet-point findings only
182
+ - Skip "investigating" status updates
183
+ - Final result + minimal reproduction steps
184
+ `
185
+ }
186
+
187
+ return baseRules
188
+ }
189
+
190
+ /**
191
+ * Generate full CLAUDE.md content with injected rules
192
+ * @param {string} existingContent - Current CLAUDE.md content
193
+ * @param {string} mode - Output mode
194
+ * @param {string} taskType - Task type
195
+ * @returns {string} - Updated CLAUDE.md content
196
+ */
197
+ export function injectOutputRules(existingContent, mode, taskType) {
198
+ const rules = generateOutputRules(mode, taskType)
199
+ if (!rules) return existingContent
200
+
201
+ // Check if rules already exist, replace if so
202
+ const markerStart = '## Token-Efficient Output'
203
+ const existingStart = existingContent.indexOf(markerStart)
204
+
205
+ if (existingStart !== -1) {
206
+ // Replace existing rules section
207
+ const before = existingContent.slice(0, existingStart)
208
+ const afterMatch = existingContent.slice(existingStart).match(/\n\n(?:##|$)/)
209
+ const after = afterMatch ? afterMatch[0] : '\n\n'
210
+ return before + rules.trim() + after
211
+ }
212
+
213
+ // Append new rules
214
+ return existingContent.trimEnd() + '\n\n' + rules.trim() + '\n'
215
+ }
package/metadata.js CHANGED
@@ -19,6 +19,7 @@ export const EXTRA_STAGES = Object.freeze([
19
19
  'strip-comments',
20
20
  'textpress',
21
21
  'foundation-models',
22
+ 'graph',
22
23
  ])
23
24
 
24
25
  export const ALL_STAGES = Object.freeze([
@@ -38,4 +39,29 @@ export const STAGE_DESCRIPTIONS = Object.freeze({
38
39
  prune: 'Strip lockfile hashes & npm metadata',
39
40
  'strip-comments': 'Remove code comments (lossy)',
40
41
  textpress: 'LLM semantic compression (Ollama/OpenRouter)',
42
+ graph: 'Session-scoped dedup across requests (lossless, opt-in)',
43
+ })
44
+
45
+ export const COMPRESSION_PRESETS = Object.freeze({
46
+ conservative: {
47
+ name: 'Conservative',
48
+ description: 'Safe, lossless compression only',
49
+ stages: ['minify', 'toon', 'strip-lines', 'whitespace', 'dedup', 'diff'],
50
+ expectedSavings: '45-50%',
51
+ risk: 'None',
52
+ },
53
+ balanced: {
54
+ name: 'Balanced',
55
+ description: 'Default, includes semantic compression',
56
+ stages: ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune'],
57
+ expectedSavings: '52-58%',
58
+ risk: 'Low',
59
+ },
60
+ aggressive: {
61
+ name: 'Aggressive',
62
+ description: 'Maximum compression, lossy stages enabled',
63
+ stages: ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune', 'strip-comments', 'textpress'],
64
+ expectedSavings: '60-68%',
65
+ risk: 'Medium (may lose comments, verbose text)',
66
+ },
41
67
  })
package/package.json CHANGED
@@ -8,10 +8,11 @@
8
8
  "detect.js",
9
9
  "metadata.js",
10
10
  "providers.js",
11
- "stats.js"
11
+ "stats.js",
12
+ "lib/"
12
13
  ],
13
- "version": "0.4.8",
14
- "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 52.6% fewer input tokens.",
14
+ "version": "0.5.0",
15
+ "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 60-70% combined input+output savings with Caveman-integrated presets.",
15
16
  "type": "module",
16
17
  "main": "index.js",
17
18
  "bin": {
package/providers.js CHANGED
@@ -155,7 +155,45 @@ const gemini = {
155
155
  },
156
156
  }
157
157
 
158
- const providers = [anthropic, openai, gemini]
158
+ function extractOpenAIResponsesTargets(item, i) {
159
+ if (item?.type !== 'function_call_output') return []
160
+ if (typeof item.output !== 'string') return []
161
+ return [{ path: ['input', i, 'output'], text: item.output, index: i }]
162
+ }
163
+
164
+ const openaiResponses = {
165
+ name: 'openai-responses',
166
+ match(method, url) {
167
+ return method === 'POST' && (
168
+ url.startsWith('/v1/responses') ||
169
+ url.startsWith('/responses')
170
+ )
171
+ },
172
+ normalizeUrl(url) {
173
+ if (url.startsWith('/responses')) return '/v1' + url
174
+ return url
175
+ },
176
+ extract(body, config = {}) {
177
+ if (!Array.isArray(body?.input) || !body.input.length) return []
178
+
179
+ if (config.cacheSafe) {
180
+ const targets = []
181
+ for (let i = body.input.length - 1; i >= 0; i--) {
182
+ const item = body.input[i]
183
+ if (item?.type !== 'function_call_output') break
184
+ targets.unshift(...extractOpenAIResponsesTargets(item, i))
185
+ }
186
+ return targets
187
+ }
188
+
189
+ return body.input.flatMap(extractOpenAIResponsesTargets)
190
+ },
191
+ apply(body, targets) {
192
+ applyTargets(body, targets)
193
+ },
194
+ }
195
+
196
+ const providers = [anthropic, openai, openaiResponses, gemini]
159
197
 
160
198
  export function detectProvider(method, url) {
161
199
  for (const p of providers) {
@@ -164,4 +202,4 @@ export function detectProvider(method, url) {
164
202
  return null
165
203
  }
166
204
 
167
- export { anthropic, openai, gemini }
205
+ export { anthropic, openai, openaiResponses, gemini }