@sliday/tamp 0.3.12 → 0.3.13

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
@@ -9,7 +9,7 @@ npx @sliday/tamp
9
9
  Or install globally:
10
10
 
11
11
  ```bash
12
- npm i -g @sliday/tamp@0.3.9
12
+ npm i -g @sliday/tamp
13
13
  ```
14
14
 
15
15
  ### 🦞 OpenClaw Quick Setup
@@ -30,7 +30,7 @@ TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune tamp -y
30
30
 
31
31
  ## How It Works
32
32
 
33
- Tamp auto-detects your agent's API format and compresses tool result blocks before forwarding upstream. Source code, error results, and non-JSON content pass through untouched.
33
+ Tamp auto-detects your agent's API format and compresses eligible tool output before forwarding upstream. Error results are skipped, JSON is compacted structurally, and text/code can be normalized or semantically compressed when those stages are enabled.
34
34
 
35
35
  ```
36
36
  Claude Code ──► Tamp (localhost:7778) ──► Anthropic API
@@ -63,8 +63,13 @@ Gemini CLI ────► │ ──► Google AI API
63
63
  | `strip-lines` | Removes line-number prefixes | Read tool output (` 1→...`) |
64
64
  | `whitespace` | Collapses blank lines, trims trailing spaces | CLI output, source code |
65
65
  | `llmlingua` | Neural text compression via [LLMLingua-2](https://github.com/microsoft/LLMLingua) | Natural language text (auto-starts sidecar) |
66
+ | `dedup` | Replaces repeated tool output with a short reference | Duplicate results in the eligible compression window |
67
+ | `diff` | Replaces similar re-reads with unified diffs | Similar text blocks in the eligible compression window |
68
+ | `prune` | Removes low-value lockfile/package metadata before minifying | npm metadata and registry URLs |
69
+ | `strip-comments` | Removes comments from text/code | Opt-in lossy mode |
70
+ | `textpress` | LLM semantic text compression via Ollama/OpenRouter | Opt-in lossy mode |
66
71
 
67
- All 5 stages enabled by default. On first launch, an interactive prompt lets you toggle methods. Use `-y` to skip the prompt.
72
+ All 8 default stages are enabled by default. Two additional lossy stages are available via the interactive prompt or `TAMP_STAGES`. Use `-y` to skip the prompt.
68
73
 
69
74
  ## Quick Start
70
75
 
@@ -83,7 +88,7 @@ npx @sliday/tamp
83
88
  │ ANTHROPIC_BASE_URL=http://localhost:7778
84
89
  │ │
85
90
  │ Aider / Cursor / Cline: │
86
- │ OPENAI_BASE_URL=http://localhost:7778
91
+ │ OPENAI_API_BASE=http://localhost:7778
87
92
  └────────────────────────────────────────┘
88
93
  ```
89
94
 
@@ -116,11 +121,12 @@ All configuration via environment variables:
116
121
  | `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream API URL |
117
122
  | `TAMP_UPSTREAM_OPENAI` | `https://api.openai.com` | Upstream for OpenAI-format requests |
118
123
  | `TAMP_UPSTREAM_GEMINI` | `https://generativelanguage.googleapis.com` | Upstream for Gemini-format requests |
119
- | `TAMP_STAGES` | `minify,toon,strip-lines,whitespace,llmlingua` | Comma-separated compression stages |
124
+ | `TAMP_STAGES` | `minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune` | Comma-separated compression stages |
120
125
  | `TAMP_MIN_SIZE` | `200` | Minimum content size (chars) to attempt compression |
121
126
  | `TAMP_LOG` | `true` | Enable request logging to stderr |
122
- | `TAMP_LOG_FILE` | _(none)_ | Write logs to file |
127
+ | `TAMP_LOG_FILE` | _(none)_ | Append request logs to a file when `TAMP_LOG=true` |
123
128
  | `TAMP_MAX_BODY` | `10485760` | Max request body size (bytes) before passthrough |
129
+ | `TAMP_CACHE_SAFE` | `true` | Only compress the newest eligible provider payload to preserve prompt-cache stability |
124
130
  | `TAMP_LLMLINGUA_URL` | _(none)_ | LLMLingua sidecar URL for text compression |
125
131
 
126
132
  ### Recommended setup
@@ -132,6 +138,9 @@ npx @sliday/tamp
132
138
  # Skip interactive prompt (CI/scripts):
133
139
  npx @sliday/tamp -y
134
140
 
141
+ # Re-compress eligible history too:
142
+ TAMP_CACHE_SAFE=false npx @sliday/tamp -y
143
+
135
144
  # Without LLMLingua (no Python needed):
136
145
  TAMP_STAGES=minify,toon,strip-lines,whitespace npx @sliday/tamp -y
137
146
  ```
@@ -169,7 +178,7 @@ The installer clones to `~/.tamp`, adds `ANTHROPIC_BASE_URL` to your shell profi
169
178
 
170
179
  ## What Gets Compressed
171
180
 
172
- Tamp compresses `tool_result` blocks in **all messages** — not just the latest. Since each API call re-sends the full conversation history uncompressed, this compounds savings with every turn. An in-memory cache ensures identical content is only compressed once per session.
181
+ With the default `TAMP_CACHE_SAFE=true`, Tamp compresses only the newest eligible tool output or function response for the detected provider so prompt-cache prefixes stay byte-stable. Set `TAMP_CACHE_SAFE=false` to also compress eligible historical blocks across the full request. An in-memory cache ensures identical content is only compressed once per session.
173
182
 
174
183
  | Content Type | Action | Example |
175
184
  |-------------|--------|---------|
package/bin/tamp.js CHANGED
@@ -1,30 +1,14 @@
1
1
  #!/usr/bin/env node
2
2
  import { createProxy } from '../index.js'
3
- import { existsSync, readFileSync } from 'node:fs'
3
+ import { existsSync } from 'node:fs'
4
4
  import { spawn, execFileSync } from 'node:child_process'
5
5
  import { fileURLToPath } from 'node:url'
6
6
  import { dirname, join } from 'node:path'
7
7
  import http from 'node:http'
8
+ import { DEFAULT_STAGES, EXTRA_STAGES, STAGE_DESCRIPTIONS, VERSION } from '../metadata.js'
8
9
 
9
10
  const __dirname = dirname(fileURLToPath(import.meta.url))
10
11
  const root = join(__dirname, '..')
11
- const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'))
12
-
13
- const DEFAULT_STAGES = ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune']
14
- const EXTRA_STAGES = ['strip-comments', 'textpress']
15
-
16
- const STAGE_DESC = {
17
- minify: 'Strip JSON whitespace (lossless)',
18
- toon: 'Columnar array encoding (lossless)',
19
- 'strip-lines': 'Remove line-number prefixes',
20
- whitespace: 'Collapse blank lines, trim trailing',
21
- llmlingua: 'Neural compression via LLMLingua-2',
22
- dedup: 'Deduplicate identical tool_results',
23
- diff: 'Replace similar re-reads with diffs',
24
- prune: 'Strip lockfile hashes & npm metadata',
25
- 'strip-comments': 'Remove code comments (lossy)',
26
- textpress: 'LLM semantic compression (Ollama/OpenRouter)',
27
- }
28
12
 
29
13
  // --- Sidecar helpers (shared by both modes) ---
30
14
 
@@ -130,11 +114,11 @@ if (skipPrompt) {
130
114
  const extraActive = active.filter(s => EXTRA_STAGES.includes(s))
131
115
 
132
116
  console.error('')
133
- console.error(` ${c.cyan}${c.bold}Tamp${c.reset} ${c.dim}v${pkg.version}${c.reset} ${c.bgGreen}${c.bold} READY ${c.reset} ${c.green}${url}${c.reset}`)
117
+ console.error(` ${c.cyan}${c.bold}Tamp${c.reset} ${c.dim}v${VERSION}${c.reset} ${c.bgGreen}${c.bold} READY ${c.reset} ${c.green}${url}${c.reset}`)
134
118
  console.error('')
135
119
  console.error(` ${c.bold}Setup:${c.reset}`)
136
120
  console.error(` ${c.dim}Claude Code:${c.reset} ANTHROPIC_BASE_URL=${c.yellow}${url}${c.reset}`)
137
- console.error(` ${c.dim}Aider/Cursor:${c.reset} OPENAI_BASE_URL=${c.yellow}${url}${c.reset}`)
121
+ console.error(` ${c.dim}Aider/Cursor:${c.reset} OPENAI_API_BASE=${c.yellow}${url}${c.reset}`)
138
122
  console.error('')
139
123
  console.error(` ${c.bold}Stages${c.reset} ${c.dim}(${active.length} active)${c.reset}`)
140
124
  for (const s of defaultActive) {
@@ -167,9 +151,10 @@ if (skipPrompt) {
167
151
 
168
152
  const cleanup = () => { sidecarProc?.kill() }
169
153
  process.on('exit', cleanup)
154
+ process.on('SIGTERM', () => { cleanup(); process.exit() })
170
155
 
171
156
  render(h(App, {
172
- version: pkg.version,
157
+ version: VERSION,
173
158
  envStages,
174
159
  startSidecar,
175
160
  createProxy,
package/bin/ui/App.js CHANGED
@@ -6,8 +6,6 @@ import { Dashboard } from './Dashboard.js'
6
6
 
7
7
  const h = React.createElement
8
8
 
9
- const DEFAULT_STAGES = ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune']
10
-
11
9
  export function App({ version, envStages, startSidecar, createProxy }) {
12
10
  const { exit } = useApp()
13
11
  const [phase, setPhase] = useState('pick')
@@ -17,6 +15,7 @@ export function App({ version, envStages, startSidecar, createProxy }) {
17
15
  const [config, setConfig] = useState(null)
18
16
  const serverRef = useRef(null)
19
17
  const sidecarUrlRef = useRef(null)
18
+ const stagesRef = useRef(null)
20
19
  const eventIdRef = useRef(0)
21
20
 
22
21
  const onCompress = useCallback((stats, newTotals, meta) => {
@@ -33,13 +32,19 @@ export function App({ version, envStages, startSidecar, createProxy }) {
33
32
  serverRef.current = server
34
33
  setConfig(proxyConfig)
35
34
 
35
+ server.on('error', (err) => {
36
+ exit()
37
+ process.stderr.write(`\n[tamp] Failed to start: ${err.message}\n`)
38
+ process.exit(1)
39
+ })
36
40
  server.listen(proxyConfig.port, () => {
37
41
  setPhase('running')
38
42
  })
39
- }, [onCompress, createProxy])
43
+ }, [onCompress, createProxy, exit])
40
44
 
41
45
  const onStagesSelected = useCallback((selected) => {
42
46
  setStages(selected)
47
+ stagesRef.current = selected
43
48
  if (selected.includes('llmlingua')) {
44
49
  setPhase('loading')
45
50
  } else {
@@ -47,16 +52,19 @@ export function App({ version, envStages, startSidecar, createProxy }) {
47
52
  }
48
53
  }, [startProxy])
49
54
 
55
+ // Use stagesRef to avoid stale closure — SidecarLoader's useEffect
56
+ // captures onReady/onFail on first render only
50
57
  const onSidecarReady = useCallback((url) => {
51
58
  sidecarUrlRef.current = url
52
- startProxy(stages, url)
53
- }, [stages, startProxy])
59
+ startProxy(stagesRef.current, url)
60
+ }, [startProxy])
54
61
 
55
62
  const onSidecarFail = useCallback(() => {
56
- const filtered = stages.filter(s => s !== 'llmlingua')
63
+ const filtered = stagesRef.current.filter(s => s !== 'llmlingua')
57
64
  setStages(filtered)
65
+ stagesRef.current = filtered
58
66
  startProxy(filtered, null)
59
- }, [stages, startProxy])
67
+ }, [startProxy])
60
68
 
61
69
  useEffect(() => {
62
70
  const shutdown = () => {
package/bin/ui/Banner.js CHANGED
@@ -1,13 +1,10 @@
1
1
  import React from 'react'
2
2
  import { Box, Text } from 'ink'
3
3
  import { Badge } from '@inkjs/ui'
4
+ import { ALL_STAGES, DEFAULT_STAGES, EXTRA_STAGES } from '../../metadata.js'
4
5
 
5
6
  const h = React.createElement
6
7
 
7
- const DEFAULT_STAGES = ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune']
8
- const EXTRA_STAGES = ['strip-comments', 'textpress']
9
- const ALL_STAGES = [...DEFAULT_STAGES, ...EXTRA_STAGES]
10
-
11
8
  export function Banner({ version, port, stages, llmLinguaUrl }) {
12
9
  const url = `http://localhost:${port}`
13
10
  const active = new Set(stages)
@@ -32,7 +29,7 @@ export function Banner({ version, port, stages, llmLinguaUrl }) {
32
29
  ),
33
30
  h(Text, null,
34
31
  h(Text, { dimColor: true }, 'Aider/Cursor: '),
35
- h(Text, null, 'OPENAI_BASE_URL='),
32
+ h(Text, null, 'OPENAI_API_BASE='),
36
33
  h(Text, { color: 'yellow' }, url),
37
34
  ),
38
35
  ),
@@ -10,19 +10,20 @@ export function SidecarLoader({ startSidecar, onReady, onFail }) {
10
10
 
11
11
  useEffect(() => {
12
12
  let mounted = true
13
+ let tid
13
14
  startSidecar().then(url => {
14
15
  if (!mounted) return
15
16
  if (url) {
16
17
  setStatus('success')
17
18
  setMessage(`LLMLingua-2 ready on ${url}`)
18
- setTimeout(() => onReady(url), 600)
19
+ tid = setTimeout(() => onReady(url), 600)
19
20
  } else {
20
21
  setStatus('warning')
21
22
  setMessage('LLMLingua-2 not available. Continuing without neural compression.')
22
- setTimeout(() => onFail(), 1200)
23
+ tid = setTimeout(() => onFail(), 1200)
23
24
  }
24
25
  })
25
- return () => { mounted = false }
26
+ return () => { mounted = false; clearTimeout(tid) }
26
27
  }, [])
27
28
 
28
29
  if (status === 'loading') {
@@ -1,25 +1,10 @@
1
1
  import React, { useState } from 'react'
2
- import { Box, Text, useInput } from 'ink'
2
+ import { Box, Text } from 'ink'
3
3
  import { MultiSelect } from '@inkjs/ui'
4
+ import { DEFAULT_STAGES, EXTRA_STAGES, STAGE_DESCRIPTIONS } from '../../metadata.js'
4
5
 
5
6
  const h = React.createElement
6
7
 
7
- const DEFAULT_STAGES = ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune']
8
- const EXTRA_STAGES = ['strip-comments', 'textpress']
9
-
10
- const STAGE_DESC = {
11
- minify: 'Strip JSON whitespace (lossless)',
12
- toon: 'Columnar array encoding (lossless)',
13
- 'strip-lines': 'Remove line-number prefixes',
14
- whitespace: 'Collapse blank lines, trim trailing',
15
- llmlingua: 'Neural compression via LLMLingua-2',
16
- dedup: 'Deduplicate identical tool_results',
17
- diff: 'Replace similar re-reads with diffs',
18
- prune: 'Strip lockfile hashes & npm metadata',
19
- 'strip-comments': 'Remove code comments (lossy)',
20
- textpress: 'LLM semantic compression (Ollama/OpenRouter)',
21
- }
22
-
23
8
  export function StagePicker({ version, envStages, onSelect }) {
24
9
  const [error, setError] = useState(null)
25
10
 
@@ -29,11 +14,11 @@ export function StagePicker({ version, envStages, onSelect }) {
29
14
 
30
15
  const options = [
31
16
  ...DEFAULT_STAGES.map(s => ({
32
- label: `${s.padEnd(16)} ${STAGE_DESC[s]}`,
17
+ label: `${s.padEnd(16)} ${STAGE_DESCRIPTIONS[s]}`,
33
18
  value: s,
34
19
  })),
35
20
  ...EXTRA_STAGES.map(s => ({
36
- label: `${s.padEnd(16)} ${STAGE_DESC[s]}`,
21
+ label: `${s.padEnd(16)} ${STAGE_DESCRIPTIONS[s]}`,
37
22
  value: s,
38
23
  })),
39
24
  ]
package/compress.js CHANGED
@@ -279,7 +279,7 @@ async function compressBlock(text, config) {
279
279
  }
280
280
 
281
281
  export async function compressRequest(body, config, provider) {
282
- const targets = provider.extract(body)
282
+ const targets = provider.extract(body, { ...config, cacheSafe: config.cacheSafe !== false })
283
283
 
284
284
  // Dedup: replace identical blocks with reference markers
285
285
  if (config.stages.includes('dedup')) deduplicateTargets(targets)
package/config.js CHANGED
@@ -1,6 +1,16 @@
1
+ import { DEFAULT_STAGES, VERSION } from './metadata.js'
2
+
3
+ function parseBoolean(value, defaultValue) {
4
+ if (value === undefined) return defaultValue
5
+ if (value === 'true') return true
6
+ if (value === 'false') return false
7
+ return defaultValue
8
+ }
9
+
1
10
  export function loadConfig(env = process.env) {
2
- const stages = (env.TAMP_STAGES || 'minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune').split(',').map(s => s.trim()).filter(Boolean)
11
+ const stages = (env.TAMP_STAGES || DEFAULT_STAGES.join(',')).split(',').map(s => s.trim()).filter(Boolean)
3
12
  return Object.freeze({
13
+ version: VERSION,
4
14
  port: parseInt(env.TAMP_PORT, 10) || 7778,
5
15
  upstream: env.TAMP_UPSTREAM || 'https://api.anthropic.com',
6
16
  upstreams: Object.freeze({
@@ -13,7 +23,7 @@ export function loadConfig(env = process.env) {
13
23
  log: env.TAMP_LOG !== 'false',
14
24
  logFile: env.TAMP_LOG_FILE || null,
15
25
  maxBody: parseInt(env.TAMP_MAX_BODY, 10) || 10_485_760,
16
- cacheSafe: true,
26
+ cacheSafe: parseBoolean(env.TAMP_CACHE_SAFE, true),
17
27
  llmLinguaUrl: env.TAMP_LLMLINGUA_URL || null,
18
28
  textpressOllamaUrl: env.TAMP_TEXTPRESS_OLLAMA_URL || 'http://localhost:11434',
19
29
  textpressOllamaModel: env.TAMP_TEXTPRESS_OLLAMA_MODEL || 'qwen3.5:0.8b',
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import http from 'node:http'
2
2
  import https from 'node:https'
3
+ import { appendFileSync } from 'node:fs'
3
4
  import zlib from 'node:zlib'
4
5
  import * as fzstd from 'fzstd'
5
6
  import { loadConfig } from './config.js'
@@ -10,7 +11,15 @@ import { createSession, formatRequestLog } from './stats.js'
10
11
  function buildUpstreamUrl(reqPath, base) {
11
12
  const parsed = new URL(base)
12
13
  const basePath = parsed.pathname.replace(/\/+$/, '')
13
- parsed.pathname = basePath + reqPath
14
+ // Split reqPath into pathname and query string — the WHATWG URL pathname
15
+ // setter encodes '?' as '%3F', which drops the query on the floor.
16
+ const qIdx = reqPath.indexOf('?')
17
+ if (qIdx !== -1) {
18
+ parsed.pathname = basePath + reqPath.substring(0, qIdx)
19
+ parsed.search = reqPath.substring(qIdx)
20
+ } else {
21
+ parsed.pathname = basePath + reqPath
22
+ }
14
23
  return parsed
15
24
  }
16
25
 
@@ -25,6 +34,7 @@ export function createProxy(overrides = {}) {
25
34
  }
26
35
 
27
36
  function _createServer(config, session) {
37
+ const log = createRequestLogger(config)
28
38
 
29
39
  function forwardRequest(method, upstreamUrl, headers, body, res) {
30
40
  const mod = upstreamUrl.protocol === 'https:' ? https : http
@@ -40,13 +50,13 @@ function forwardRequest(method, upstreamUrl, headers, body, res) {
40
50
  res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
41
51
  upstreamRes.pipe(res)
42
52
  upstreamRes.on('error', (err) => {
43
- console.error(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
53
+ log(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
44
54
  res.destroy()
45
55
  })
46
56
  })
47
57
 
48
58
  upstream.on('error', (err) => {
49
- console.error(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
59
+ log(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
50
60
  if (!res.headersSent) {
51
61
  res.writeHead(502, { 'Content-Type': 'application/json' })
52
62
  }
@@ -54,7 +64,7 @@ function forwardRequest(method, upstreamUrl, headers, body, res) {
54
64
  })
55
65
 
56
66
  res.on('error', (err) => {
57
- console.error(`[tamp] client disconnect: ${err.code || ''} ${err.message}`)
67
+ log(`[tamp] client disconnect: ${err.code || ''} ${err.message}`)
58
68
  upstream.destroy()
59
69
  })
60
70
 
@@ -84,13 +94,13 @@ function pipeRequest(req, res, upstreamUrl, prefixChunks) {
84
94
  res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
85
95
  upstreamRes.pipe(res)
86
96
  upstreamRes.on('error', (err) => {
87
- console.error(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
97
+ log(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
88
98
  res.destroy()
89
99
  })
90
100
  })
91
101
 
92
102
  upstream.on('error', (err) => {
93
- console.error(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
103
+ log(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
94
104
  if (!res.headersSent) {
95
105
  res.writeHead(502, { 'Content-Type': 'application/json' })
96
106
  }
@@ -98,7 +108,7 @@ function pipeRequest(req, res, upstreamUrl, prefixChunks) {
98
108
  })
99
109
 
100
110
  res.on('error', (err) => {
101
- console.error(`[tamp] client disconnect: ${err.code || ''} ${err.message}`)
111
+ log(`[tamp] client disconnect: ${err.code || ''} ${err.message}`)
102
112
  upstream.destroy()
103
113
  })
104
114
 
@@ -114,7 +124,7 @@ function pipeRequest(req, res, upstreamUrl, prefixChunks) {
114
124
  return http.createServer(async (req, res) => {
115
125
  // Health check endpoint
116
126
  if (req.url === '/health' && (req.method === 'GET' || req.method === 'HEAD')) {
117
- const body = JSON.stringify({ status: 'ok', version: config.version || '0.3.7', stages: config.stages })
127
+ const body = JSON.stringify({ status: 'ok', version: config.version, stages: config.stages })
118
128
  res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) })
119
129
  return res.end(req.method === 'HEAD' ? undefined : body)
120
130
  }
@@ -122,7 +132,7 @@ return http.createServer(async (req, res) => {
122
132
  const provider = detectProvider(req.method, req.url)
123
133
 
124
134
  if (!provider) {
125
- if (config.log) console.error(`[tamp] ${req.method} ${req.url}`)
135
+ log(`[tamp] ${req.method} ${req.url}`)
126
136
  const upstreamUrl = buildUpstreamUrl(req.url, config.upstream)
127
137
  return pipeRequest(req, res, upstreamUrl)
128
138
  }
@@ -145,7 +155,7 @@ return http.createServer(async (req, res) => {
145
155
  }
146
156
 
147
157
  if (overflow) {
148
- if (config.log) console.error('[tamp] passthrough (body too large)')
158
+ log('[tamp] passthrough (body too large)')
149
159
  return pipeRequest(req, res, upstreamUrl, chunks)
150
160
  }
151
161
 
@@ -173,7 +183,7 @@ return http.createServer(async (req, res) => {
173
183
  decompressed = true
174
184
  } else if (encoding && encoding !== 'identity') {
175
185
  // Unknown encoding — can't decompress, passthrough as-is
176
- if (config.log) console.error(`[tamp] passthrough (unsupported encoding: ${encoding})`)
186
+ log(`[tamp] passthrough (unsupported encoding: ${encoding})`)
177
187
  forwardRequest(req.method, upstreamUrl, headers, rawBody, res)
178
188
  return
179
189
  } else {
@@ -181,7 +191,7 @@ return http.createServer(async (req, res) => {
181
191
  }
182
192
  } catch {
183
193
  // Decompression failed — passthrough original body
184
- if (config.log) console.error(`[tamp] passthrough (decompression failed)`)
194
+ log('[tamp] passthrough (decompression failed)')
185
195
  forwardRequest(req.method, upstreamUrl, headers, rawBody, res)
186
196
  return
187
197
  }
@@ -195,12 +205,10 @@ return http.createServer(async (req, res) => {
195
205
  if (decompressed) delete headers['content-encoding']
196
206
 
197
207
  session.record(stats)
198
- if (config.log) {
199
- console.error(formatRequestLog(stats, session, provider.name, req.url, textBody.length, config.tokenCost))
200
- }
208
+ log(formatRequestLog(stats, session, provider.name, req.url, textBody.length, config.tokenCost))
201
209
  config.onCompress?.(stats, session.getTotals(), { provider: provider.name, url: req.url, bodySize: textBody.length })
202
210
  } catch (err) {
203
- if (config.log) console.error(`[tamp] passthrough (parse error): ${err.message}`)
211
+ log(`[tamp] passthrough (parse error): ${err.message}`)
204
212
  finalBody = rawBody
205
213
  }
206
214
 
@@ -211,6 +219,29 @@ return http.createServer(async (req, res) => {
211
219
  })
212
220
  }
213
221
 
222
+ function createRequestLogger(config) {
223
+ if (!config.log) return () => {}
224
+
225
+ let fileLoggingEnabled = Boolean(config.logFile)
226
+ let warned = false
227
+
228
+ return (message) => {
229
+ console.error(message)
230
+
231
+ if (!fileLoggingEnabled) return
232
+
233
+ try {
234
+ appendFileSync(config.logFile, `${message}\n`, 'utf8')
235
+ } catch (err) {
236
+ fileLoggingEnabled = false
237
+ if (!warned) {
238
+ warned = true
239
+ process.stderr.write(`[tamp] log file disabled: ${err.message}\n`)
240
+ }
241
+ }
242
+ }
243
+ }
244
+
214
245
  const isMain = !process.argv[1]?.includes('node_modules') && process.argv[1] === new URL(import.meta.url).pathname
215
246
 
216
247
  if (isMain) {
package/metadata.js ADDED
@@ -0,0 +1,39 @@
1
+ import { readFileSync } from 'node:fs'
2
+
3
+ const pkg = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf-8'))
4
+
5
+ export const VERSION = pkg.version
6
+
7
+ export const DEFAULT_STAGES = Object.freeze([
8
+ 'minify',
9
+ 'toon',
10
+ 'strip-lines',
11
+ 'whitespace',
12
+ 'llmlingua',
13
+ 'dedup',
14
+ 'diff',
15
+ 'prune',
16
+ ])
17
+
18
+ export const EXTRA_STAGES = Object.freeze([
19
+ 'strip-comments',
20
+ 'textpress',
21
+ ])
22
+
23
+ export const ALL_STAGES = Object.freeze([
24
+ ...DEFAULT_STAGES,
25
+ ...EXTRA_STAGES,
26
+ ])
27
+
28
+ export const STAGE_DESCRIPTIONS = Object.freeze({
29
+ minify: 'Strip JSON whitespace (lossless)',
30
+ toon: 'Columnar array encoding (lossless)',
31
+ 'strip-lines': 'Remove line-number prefixes',
32
+ whitespace: 'Collapse blank lines, trim trailing',
33
+ llmlingua: 'Neural compression via LLMLingua-2',
34
+ dedup: 'Deduplicate identical tool_results',
35
+ diff: 'Replace similar re-reads with diffs',
36
+ prune: 'Strip lockfile hashes & npm metadata',
37
+ 'strip-comments': 'Remove code comments (lossy)',
38
+ textpress: 'LLM semantic compression (Ollama/OpenRouter)',
39
+ })
package/package.json CHANGED
@@ -6,10 +6,11 @@
6
6
  "compress.js",
7
7
  "config.js",
8
8
  "detect.js",
9
+ "metadata.js",
9
10
  "providers.js",
10
11
  "stats.js"
11
12
  ],
12
- "version": "0.3.12",
13
+ "version": "0.3.13",
13
14
  "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 52.6% fewer input tokens.",
14
15
  "type": "module",
15
16
  "main": "index.js",
package/providers.js CHANGED
@@ -1,50 +1,95 @@
1
+ function applyTargets(body, targets) {
2
+ for (const t of targets) {
3
+ if (t.skip || !t.compressed) continue
4
+ let obj = body
5
+ const path = t.path
6
+ for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
7
+ obj[path[path.length - 1]] = t.compressed
8
+ }
9
+ }
10
+
11
+ function findLatestEligibleGroup(items, extractTargets) {
12
+ let fallback = null
13
+ for (let i = items.length - 1; i >= 0; i--) {
14
+ const targets = extractTargets(items[i], i)
15
+ if (!targets.length) continue
16
+ fallback ||= targets
17
+ if (targets.some(target => !target.skip)) return targets
18
+ }
19
+ return fallback || []
20
+ }
21
+
22
+ function extractAnthropicMessageTargets(msg, mi) {
23
+ const targets = []
24
+ if (msg.role !== 'user') return targets
25
+
26
+ if (typeof msg.content === 'string') {
27
+ targets.push({ path: ['messages', mi, 'content'], text: msg.content, index: mi })
28
+ } else if (Array.isArray(msg.content)) {
29
+ for (let i = 0; i < msg.content.length; i++) {
30
+ const block = msg.content[i]
31
+ if (block.type !== 'tool_result') continue
32
+ if (block.is_error) {
33
+ targets.push({ skip: 'error', index: i })
34
+ continue
35
+ }
36
+
37
+ if (typeof block.content === 'string') {
38
+ targets.push({ path: ['messages', mi, 'content', i, 'content'], text: block.content, index: i })
39
+ } else if (Array.isArray(block.content)) {
40
+ for (let j = 0; j < block.content.length; j++) {
41
+ const sub = block.content[j]
42
+ if (sub.type === 'text') {
43
+ targets.push({ path: ['messages', mi, 'content', i, 'content', j, 'text'], text: sub.text, index: i })
44
+ }
45
+ }
46
+ }
47
+ }
48
+ }
49
+
50
+ return targets
51
+ }
52
+
1
53
  const anthropic = {
2
54
  name: 'anthropic',
3
55
  match(method, url) {
4
56
  return method === 'POST' && url.startsWith('/v1/messages')
5
57
  },
6
- extract(body) {
7
- const targets = []
8
- if (!body?.messages?.length) return targets
9
-
10
- for (let mi = 0; mi < body.messages.length; mi++) {
11
- const msg = body.messages[mi]
12
- if (msg.role !== 'user') continue
13
-
14
- if (typeof msg.content === 'string') {
15
- targets.push({ path: ['messages', mi, 'content'], text: msg.content })
16
- } else if (Array.isArray(msg.content)) {
17
- for (let i = 0; i < msg.content.length; i++) {
18
- const block = msg.content[i]
19
- if (block.type !== 'tool_result') continue
20
- if (block.is_error) { targets.push({ skip: 'error', index: i }); continue }
21
-
22
- if (typeof block.content === 'string') {
23
- targets.push({ path: ['messages', mi, 'content', i, 'content'], text: block.content, index: i })
24
- } else if (Array.isArray(block.content)) {
25
- for (let j = 0; j < block.content.length; j++) {
26
- const sub = block.content[j]
27
- if (sub.type === 'text') {
28
- targets.push({ path: ['messages', mi, 'content', i, 'content', j, 'text'], text: sub.text, index: i })
29
- }
30
- }
31
- }
32
- }
33
- }
58
+ extract(body, config = {}) {
59
+ if (!body?.messages?.length) return []
60
+ if (config.cacheSafe) {
61
+ return findLatestEligibleGroup(body.messages, extractAnthropicMessageTargets)
34
62
  }
35
- return targets
63
+
64
+ return body.messages.flatMap(extractAnthropicMessageTargets)
36
65
  },
37
66
  apply(body, targets) {
38
- for (const t of targets) {
39
- if (t.skip || !t.compressed) continue
40
- let obj = body
41
- const path = t.path
42
- for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
43
- obj[path[path.length - 1]] = t.compressed
44
- }
67
+ applyTargets(body, targets)
45
68
  },
46
69
  }
47
70
 
71
+ function extractOpenAIResponsesTargets(item, i) {
72
+ const targets = []
73
+ if (item.type === 'function_call_output' && typeof item.output === 'string') {
74
+ targets.push({ path: ['input', i, 'output'], text: item.output, index: i })
75
+ return targets
76
+ }
77
+ if (item.type === 'message' && Array.isArray(item.content)) {
78
+ for (let j = 0; j < item.content.length; j++) {
79
+ const part = item.content[j]
80
+ if ((part.type === 'input_text' || part.type === 'output_text') && typeof part.text === 'string') {
81
+ targets.push({ path: ['input', i, 'content', j, 'text'], text: part.text, index: i })
82
+ }
83
+ }
84
+ }
85
+ return targets
86
+ }
87
+
88
+ function extractOpenAIChatTargets(msg, i) {
89
+ if (msg.role !== 'tool' || typeof msg.content !== 'string') return []
90
+ return [{ path: ['messages', i, 'content'], text: msg.content, index: i }]
91
+ }
92
+
48
93
  const openai = {
49
94
  name: 'openai',
50
95
  match(method, url) {
@@ -58,78 +103,74 @@ const openai = {
58
103
  if (url.startsWith('/chat/completions')) return '/v1' + url
59
104
  return url
60
105
  },
61
- extract(body) {
62
- const targets = []
63
-
106
+ extract(body, config = {}) {
64
107
  // Responses API format: body.input array (Codex CLI)
65
108
  if (body?.input?.length) {
66
- for (let i = 0; i < body.input.length; i++) {
67
- const item = body.input[i]
68
- if (item.type === 'function_call_output' && typeof item.output === 'string') {
69
- targets.push({ path: ['input', i, 'output'], text: item.output, index: i })
70
- continue
71
- }
72
- if (item.type === 'message' && Array.isArray(item.content)) {
73
- for (let j = 0; j < item.content.length; j++) {
74
- const part = item.content[j]
75
- if ((part.type === 'input_text' || part.type === 'output_text') && typeof part.text === 'string') {
76
- targets.push({ path: ['input', i, 'content', j, 'text'], text: part.text, index: i })
77
- }
78
- }
109
+ if (config.cacheSafe) {
110
+ const targets = []
111
+ for (let i = body.input.length - 1; i >= 0; i--) {
112
+ const itemTargets = extractOpenAIResponsesTargets(body.input[i], i)
113
+ if (!itemTargets.length) break
114
+ targets.unshift(...itemTargets)
79
115
  }
116
+ return targets
80
117
  }
81
- return targets
118
+
119
+ return body.input.flatMap(extractOpenAIResponsesTargets)
82
120
  }
83
121
 
84
122
  // Chat Completions format
85
- if (!body?.messages?.length) return targets
123
+ if (!body?.messages?.length) return []
86
124
 
87
- for (let i = 0; i < body.messages.length; i++) {
88
- const msg = body.messages[i]
89
- if (msg.role !== 'tool') continue
90
- if (typeof msg.content === 'string') {
91
- targets.push({ path: ['messages', i, 'content'], text: msg.content, index: i })
125
+ if (config.cacheSafe) {
126
+ const targets = []
127
+ for (let i = body.messages.length - 1; i >= 0; i--) {
128
+ const msg = body.messages[i]
129
+ if (msg.role !== 'tool') break
130
+ targets.unshift(...extractOpenAIChatTargets(msg, i))
92
131
  }
132
+ return targets
93
133
  }
94
- return targets
134
+
135
+ return body.messages.flatMap(extractOpenAIChatTargets)
95
136
  },
96
137
  apply(body, targets) {
97
- for (const t of targets) {
98
- if (t.skip || !t.compressed) continue
99
- let obj = body
100
- const path = t.path
101
- for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
102
- obj[path[path.length - 1]] = t.compressed
103
- }
138
+ applyTargets(body, targets)
104
139
  },
105
140
  }
106
141
 
142
+ function extractGeminiContentTargets(content, ci) {
143
+ const targets = []
144
+ if (!content.parts?.length) return targets
145
+
146
+ for (let pi = 0; pi < content.parts.length; pi++) {
147
+ const part = content.parts[pi]
148
+ if (!part.functionResponse?.response) continue
149
+ const resp = part.functionResponse.response
150
+ const text = typeof resp === 'string' ? resp : JSON.stringify(resp, null, 2)
151
+ targets.push({
152
+ path: ['contents', ci, 'parts', pi, 'functionResponse', 'response'],
153
+ text,
154
+ index: pi,
155
+ wasObject: typeof resp !== 'string',
156
+ })
157
+ }
158
+
159
+ return targets
160
+ }
161
+
107
162
  const gemini = {
108
163
  name: 'gemini',
109
164
  match(method, url) {
110
165
  return method === 'POST' && url.includes('generateContent')
111
166
  },
112
- extract(body) {
113
- const targets = []
114
- if (!body?.contents?.length) return targets
115
-
116
- for (let ci = 0; ci < body.contents.length; ci++) {
117
- const content = body.contents[ci]
118
- if (!content.parts?.length) continue
119
- for (let pi = 0; pi < content.parts.length; pi++) {
120
- const part = content.parts[pi]
121
- if (!part.functionResponse?.response) continue
122
- const resp = part.functionResponse.response
123
- const text = typeof resp === 'string' ? resp : JSON.stringify(resp, null, 2)
124
- targets.push({
125
- path: ['contents', ci, 'parts', pi, 'functionResponse', 'response'],
126
- text,
127
- index: pi,
128
- wasObject: typeof resp !== 'string',
129
- })
130
- }
167
+ extract(body, config = {}) {
168
+ if (!body?.contents?.length) return []
169
+ if (config.cacheSafe) {
170
+ return findLatestEligibleGroup(body.contents, extractGeminiContentTargets)
131
171
  }
132
- return targets
172
+
173
+ return body.contents.flatMap(extractGeminiContentTargets)
133
174
  },
134
175
  apply(body, targets) {
135
176
  for (const t of targets) {