@sliday/tamp 0.2.13 → 0.3.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 +18 -16
- package/bin/tamp.js +150 -62
- package/compress.js +44 -3
- package/config.js +1 -1
- package/package.json +3 -2
- package/providers.js +24 -40
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Tamp
|
|
2
2
|
|
|
3
|
-
**Token compression proxy for coding agents.**
|
|
3
|
+
**Token compression proxy for coding agents.** 32.0% fewer input tokens, zero code changes. Works with Claude Code, Aider, Cursor, Cline, Windsurf, and any OpenAI-compatible agent.
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
npx @sliday/tamp
|
|
@@ -19,7 +19,6 @@ Tamp auto-detects your agent's API format and compresses tool result blocks befo
|
|
|
19
19
|
```
|
|
20
20
|
Claude Code ──► Tamp (localhost:7778) ──► Anthropic API
|
|
21
21
|
Aider/Cursor ──► │ ──► OpenAI API
|
|
22
|
-
Codex CLI ─────► │ ──► OpenAI API
|
|
23
22
|
Gemini CLI ────► │ ──► Google AI API
|
|
24
23
|
│
|
|
25
24
|
├─ JSON → minify whitespace
|
|
@@ -35,18 +34,21 @@ Gemini CLI ────► │ ──► Google AI API
|
|
|
35
34
|
|--------|----------|--------|
|
|
36
35
|
| Anthropic Messages | `POST /v1/messages` | Claude Code |
|
|
37
36
|
| OpenAI Chat Completions | `POST /v1/chat/completions` | Aider, Cursor, Cline, Windsurf, OpenCode |
|
|
38
|
-
| OpenAI Responses | `POST /v1/responses` | Codex CLI |
|
|
39
37
|
| Google Gemini | `POST .../generateContent` | Gemini CLI |
|
|
40
38
|
|
|
39
|
+
> **Why not Codex?** Codex CLI uses OpenAI's Responses API (`POST /v1/responses`), which has a different request shape than Chat Completions — `input[]` with `function_call_output` items instead of `messages[]` with `role: tool`. We had early support but pulled it because the Responses API is still evolving and Codex sends zstd-compressed bodies that add another layer of complexity. We'll revisit once the format stabilizes.
|
|
40
|
+
|
|
41
41
|
### Compression Stages
|
|
42
42
|
|
|
43
43
|
| Stage | What it does | When it applies |
|
|
44
44
|
|-------|-------------|-----------------|
|
|
45
45
|
| `minify` | Strips JSON whitespace | Pretty-printed JSON objects/arrays |
|
|
46
46
|
| `toon` | Columnar [TOON encoding](https://github.com/nicholasgasior/toon-format) | Homogeneous arrays (file listings, routes, deps) |
|
|
47
|
-
| `
|
|
47
|
+
| `strip-lines` | Removes line-number prefixes | Read tool output (` 1→...`) |
|
|
48
|
+
| `whitespace` | Collapses blank lines, trims trailing spaces | CLI output, source code |
|
|
49
|
+
| `llmlingua` | Neural text compression via [LLMLingua-2](https://github.com/microsoft/LLMLingua) | Natural language text (auto-starts sidecar) |
|
|
48
50
|
|
|
49
|
-
|
|
51
|
+
All 5 stages enabled by default. On first launch, an interactive prompt lets you toggle methods. Use `-y` to skip the prompt.
|
|
50
52
|
|
|
51
53
|
## Quick Start
|
|
52
54
|
|
|
@@ -83,12 +85,6 @@ export OPENAI_API_BASE=http://localhost:7778
|
|
|
83
85
|
aider
|
|
84
86
|
```
|
|
85
87
|
|
|
86
|
-
**Codex CLI:**
|
|
87
|
-
Add to `~/.codex/config.toml`:
|
|
88
|
-
```toml
|
|
89
|
-
openai_base_url = "http://localhost:7778"
|
|
90
|
-
```
|
|
91
|
-
|
|
92
88
|
**Cursor / Cline / Windsurf:**
|
|
93
89
|
Set the API base URL to `http://localhost:7778` in your editor's settings.
|
|
94
90
|
|
|
@@ -104,7 +100,7 @@ All configuration via environment variables:
|
|
|
104
100
|
| `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream API URL |
|
|
105
101
|
| `TAMP_UPSTREAM_OPENAI` | `https://api.openai.com` | Upstream for OpenAI-format requests |
|
|
106
102
|
| `TAMP_UPSTREAM_GEMINI` | `https://generativelanguage.googleapis.com` | Upstream for Gemini-format requests |
|
|
107
|
-
| `TAMP_STAGES` | `minify,toon` | Comma-separated compression stages |
|
|
103
|
+
| `TAMP_STAGES` | `minify,toon,strip-lines,whitespace,llmlingua` | Comma-separated compression stages |
|
|
108
104
|
| `TAMP_MIN_SIZE` | `200` | Minimum content size (chars) to attempt compression |
|
|
109
105
|
| `TAMP_LOG` | `true` | Enable request logging to stderr |
|
|
110
106
|
| `TAMP_LOG_FILE` | _(none)_ | Write logs to file |
|
|
@@ -114,8 +110,14 @@ All configuration via environment variables:
|
|
|
114
110
|
### Recommended setup
|
|
115
111
|
|
|
116
112
|
```bash
|
|
117
|
-
#
|
|
118
|
-
|
|
113
|
+
# All stages enabled by default — just run:
|
|
114
|
+
npx @sliday/tamp
|
|
115
|
+
|
|
116
|
+
# Skip interactive prompt (CI/scripts):
|
|
117
|
+
npx @sliday/tamp -y
|
|
118
|
+
|
|
119
|
+
# Without LLMLingua (no Python needed):
|
|
120
|
+
TAMP_STAGES=minify,toon,strip-lines,whitespace npx @sliday/tamp -y
|
|
119
121
|
```
|
|
120
122
|
|
|
121
123
|
## Installation Methods
|
|
@@ -151,7 +153,7 @@ The installer clones to `~/.tamp`, adds `ANTHROPIC_BASE_URL` to your shell profi
|
|
|
151
153
|
|
|
152
154
|
## What Gets Compressed
|
|
153
155
|
|
|
154
|
-
Tamp
|
|
156
|
+
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.
|
|
155
157
|
|
|
156
158
|
| Content Type | Action | Example |
|
|
157
159
|
|-------------|--------|---------|
|
|
@@ -159,7 +161,7 @@ Tamp only compresses the **last user message** in each request (the most recent
|
|
|
159
161
|
| JSON with line numbers | Strip prefixes + minify | Read tool output (` 1→{...}`) |
|
|
160
162
|
| Homogeneous JSON arrays | TOON encode | File listings, route tables, dependencies |
|
|
161
163
|
| Already-minified JSON | Skip | Single-line JSON |
|
|
162
|
-
| Source code (text) |
|
|
164
|
+
| Source code (text) | Strip line numbers + normalize whitespace | `.ts`, `.py`, `.rs` files |
|
|
163
165
|
| `is_error: true` results | Skip entirely | Error tool results |
|
|
164
166
|
| TOON-encoded content | Skip | Already compressed |
|
|
165
167
|
|
package/bin/tamp.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createProxy } from '../index.js'
|
|
3
3
|
import { existsSync, readFileSync } from 'node:fs'
|
|
4
|
-
import { spawn } from 'node:child_process'
|
|
4
|
+
import { spawn, execFileSync } from 'node:child_process'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
6
|
import { dirname, join } from 'node:path'
|
|
7
|
+
import { checkbox } from '@inquirer/prompts'
|
|
8
|
+
import http from 'node:http'
|
|
7
9
|
|
|
8
10
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
9
11
|
const root = join(__dirname, '..')
|
|
10
12
|
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'))
|
|
11
13
|
|
|
12
|
-
// ANSI colors
|
|
13
14
|
const c = {
|
|
14
15
|
reset: '\x1b[0m',
|
|
15
16
|
bold: '\x1b[1m',
|
|
@@ -20,12 +21,146 @@ const c = {
|
|
|
20
21
|
magenta: '\x1b[35m',
|
|
21
22
|
cyan: '\x1b[36m',
|
|
22
23
|
bgGreen: '\x1b[42m',
|
|
23
|
-
bgYellow: '\x1b[43m',
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
function log(msg = '') { console.error(msg) }
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
const STAGE_INFO = {
|
|
29
|
+
minify: 'Strip JSON whitespace (lossless)',
|
|
30
|
+
toon: 'Columnar array encoding (lossless)',
|
|
31
|
+
'strip-lines':'Remove line-number prefixes from Read output',
|
|
32
|
+
whitespace: 'Collapse blank lines, trim trailing spaces',
|
|
33
|
+
llmlingua: 'Neural text compression via LLMLingua-2',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// --- Determine stages ---
|
|
37
|
+
const skipPrompt = process.argv.includes('-y') || process.argv.includes('--no-interactive') || !process.stdin.isTTY
|
|
38
|
+
let selectedStages
|
|
39
|
+
|
|
40
|
+
if (process.env.TAMP_STAGES) {
|
|
41
|
+
selectedStages = process.env.TAMP_STAGES.split(',').map(s => s.trim()).filter(Boolean)
|
|
42
|
+
} else if (skipPrompt) {
|
|
43
|
+
selectedStages = Object.keys(STAGE_INFO)
|
|
44
|
+
} else {
|
|
45
|
+
log('')
|
|
46
|
+
log(` ${c.bold}${c.cyan}Tamp${c.reset} ${c.dim}v${pkg.version}${c.reset} — Token compression proxy`)
|
|
47
|
+
log('')
|
|
48
|
+
selectedStages = await checkbox({
|
|
49
|
+
message: 'Compression methods (space to toggle, enter to confirm):',
|
|
50
|
+
choices: Object.entries(STAGE_INFO).map(([value, desc]) => ({
|
|
51
|
+
name: `${value.padEnd(12)} ${c.dim}— ${desc}${c.reset}`,
|
|
52
|
+
value,
|
|
53
|
+
checked: true,
|
|
54
|
+
})),
|
|
55
|
+
})
|
|
56
|
+
if (selectedStages.length === 0) {
|
|
57
|
+
log(` ${c.yellow}No methods selected — running as passthrough proxy.${c.reset}`)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
62
|
+
|
|
63
|
+
// --- Sidecar startup ---
|
|
64
|
+
let sidecarProc = null
|
|
65
|
+
|
|
66
|
+
async function checkPort(port) {
|
|
67
|
+
return new Promise(resolve => {
|
|
68
|
+
const req = http.get(`http://127.0.0.1:${port}/health`, (res) => {
|
|
69
|
+
res.resume()
|
|
70
|
+
resolve(res.statusCode === 200)
|
|
71
|
+
})
|
|
72
|
+
req.on('error', () => resolve(false))
|
|
73
|
+
req.setTimeout(1000, () => { req.destroy(); resolve(false) })
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function hasCommand(cmd) {
|
|
78
|
+
try { execFileSync('which', [cmd], { stdio: 'ignore' }); return true } catch { return false }
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function startSidecar() {
|
|
82
|
+
const sidecarPort = 8788
|
|
83
|
+
const sidecarDir = join(root, 'sidecar')
|
|
84
|
+
const serverPy = join(sidecarDir, 'server.py')
|
|
85
|
+
|
|
86
|
+
// 1. Already running?
|
|
87
|
+
if (await checkPort(sidecarPort)) {
|
|
88
|
+
log(` ${c.green}✓${c.reset} LLMLingua-2 sidecar already running on port ${sidecarPort}`)
|
|
89
|
+
return `http://localhost:${sidecarPort}`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!existsSync(serverPy)) {
|
|
93
|
+
return null
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
log(` ${c.yellow}→${c.reset} Starting LLMLingua-2 sidecar ...`)
|
|
97
|
+
|
|
98
|
+
// 2. Try uv run (no venv needed)
|
|
99
|
+
if (hasCommand('uv')) {
|
|
100
|
+
try {
|
|
101
|
+
const proc = spawn('uv', [
|
|
102
|
+
'run', '--with', 'fastapi', '--with', 'uvicorn', '--with', 'llmlingua', '--with', 'mlx',
|
|
103
|
+
'uvicorn', 'server:app', '--host', '127.0.0.1', '--port', String(sidecarPort),
|
|
104
|
+
], { cwd: sidecarDir, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
105
|
+
|
|
106
|
+
const url = await waitForSidecar(proc, sidecarPort)
|
|
107
|
+
if (url) { sidecarProc = proc; return url }
|
|
108
|
+
proc.kill()
|
|
109
|
+
} catch { /* try next */ }
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 3. Try existing venv
|
|
113
|
+
const venvPython = join(sidecarDir, '.venv', 'bin', 'python')
|
|
114
|
+
if (existsSync(venvPython)) {
|
|
115
|
+
try {
|
|
116
|
+
const proc = spawn(venvPython, [
|
|
117
|
+
'-m', 'uvicorn', 'server:app', '--host', '127.0.0.1', '--port', String(sidecarPort),
|
|
118
|
+
], { cwd: sidecarDir, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
119
|
+
|
|
120
|
+
const url = await waitForSidecar(proc, sidecarPort)
|
|
121
|
+
if (url) { sidecarProc = proc; return url }
|
|
122
|
+
proc.kill()
|
|
123
|
+
} catch { /* fall through */ }
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return null
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function waitForSidecar(proc, port, timeout = 30000) {
|
|
130
|
+
return new Promise(resolve => {
|
|
131
|
+
const timer = setTimeout(() => resolve(null), timeout)
|
|
132
|
+
proc.stderr.on('data', (d) => {
|
|
133
|
+
if (d.toString().includes('Uvicorn running')) {
|
|
134
|
+
clearTimeout(timer)
|
|
135
|
+
log(` ${c.green}✓${c.reset} LLMLingua-2 sidecar ready on port ${c.bold}${port}${c.reset}`)
|
|
136
|
+
resolve(`http://localhost:${port}`)
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
proc.on('exit', () => { clearTimeout(timer); resolve(null) })
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Start sidecar if llmlingua is in selected stages
|
|
144
|
+
if (selectedStages.includes('llmlingua')) {
|
|
145
|
+
const sidecarUrl = await startSidecar()
|
|
146
|
+
if (sidecarUrl) {
|
|
147
|
+
process.env.TAMP_LLMLINGUA_URL = sidecarUrl
|
|
148
|
+
} else {
|
|
149
|
+
log(` ${c.yellow}!${c.reset} LLMLingua-2 sidecar not available`)
|
|
150
|
+
if (!hasCommand('uv')) {
|
|
151
|
+
log(` Install uv: ${c.cyan}curl -LsSf https://astral.sh/uv/install.sh | sh${c.reset}`)
|
|
152
|
+
}
|
|
153
|
+
log(` Continuing without neural compression.`)
|
|
154
|
+
log('')
|
|
155
|
+
selectedStages = selectedStages.filter(s => s !== 'llmlingua')
|
|
156
|
+
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// --- Start proxy ---
|
|
161
|
+
const { config, server } = createProxy()
|
|
162
|
+
|
|
163
|
+
function printBanner() {
|
|
29
164
|
const url = `http://localhost:${config.port}`
|
|
30
165
|
|
|
31
166
|
log('')
|
|
@@ -48,66 +183,19 @@ function printBanner(config) {
|
|
|
48
183
|
log('')
|
|
49
184
|
|
|
50
185
|
log(` ${c.bold}Compression:${c.reset}`)
|
|
51
|
-
for (const stage of
|
|
52
|
-
const
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
: stage
|
|
57
|
-
log(` ${icon} ${c.cyan}${stage}${c.reset} — ${label}`)
|
|
186
|
+
for (const [stage, desc] of Object.entries(STAGE_INFO)) {
|
|
187
|
+
const active = config.stages.includes(stage)
|
|
188
|
+
const icon = active ? `${c.green}✓${c.reset}` : `${c.dim}✗${c.reset}`
|
|
189
|
+
const extra = stage === 'llmlingua' && active && config.llmLinguaUrl ? ` ${c.dim}(${config.llmLinguaUrl})${c.reset}` : ''
|
|
190
|
+
log(` ${icon} ${active ? c.cyan : c.dim}${stage}${c.reset} — ${active ? desc : c.dim + desc + c.reset}${extra}`)
|
|
58
191
|
}
|
|
59
192
|
log('')
|
|
60
193
|
}
|
|
61
194
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const needsSidecar = finalConfig.stages.includes('llmlingua') && !finalConfig.llmLinguaUrl
|
|
66
|
-
const venvPython = join(root, 'sidecar', '.venv', 'bin', 'python')
|
|
67
|
-
const serverPy = join(root, 'sidecar', 'server.py')
|
|
68
|
-
const hasSidecar = existsSync(venvPython) && existsSync(serverPy)
|
|
69
|
-
|
|
70
|
-
if (needsSidecar && hasSidecar) {
|
|
71
|
-
const sidecarPort = 8788
|
|
72
|
-
process.env.TAMP_LLMLINGUA_URL = `http://localhost:${sidecarPort}`
|
|
73
|
-
const refreshed = createProxy()
|
|
74
|
-
finalConfig = refreshed.config
|
|
75
|
-
finalServer = refreshed.server
|
|
76
|
-
|
|
77
|
-
log('')
|
|
78
|
-
log(` ${c.yellow}→${c.reset} Starting LLMLingua-2 sidecar ...`)
|
|
79
|
-
|
|
80
|
-
const sidecar = spawn(venvPython, ['-m', 'uvicorn', 'server:app', '--host', '127.0.0.1', '--port', String(sidecarPort)], {
|
|
81
|
-
cwd: join(root, 'sidecar'),
|
|
82
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
let ready = false
|
|
86
|
-
sidecar.stderr.on('data', (d) => {
|
|
87
|
-
const line = d.toString()
|
|
88
|
-
if (!ready && line.includes('Uvicorn running')) {
|
|
89
|
-
ready = true
|
|
90
|
-
log(` ${c.green}✓${c.reset} LLMLingua-2 sidecar ready on ${c.bold}port ${sidecarPort}${c.reset}`)
|
|
91
|
-
}
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
sidecar.on('exit', (code) => {
|
|
95
|
-
if (code !== null && code !== 0) {
|
|
96
|
-
log(` ${c.yellow}✗${c.reset} LLMLingua-2 sidecar exited (code ${code})`)
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
|
|
100
|
-
process.on('exit', () => { sidecar?.kill() })
|
|
101
|
-
process.on('SIGINT', () => { sidecar?.kill(); process.exit() })
|
|
102
|
-
process.on('SIGTERM', () => { sidecar?.kill(); process.exit() })
|
|
103
|
-
} else if (needsSidecar && !hasSidecar) {
|
|
104
|
-
log('')
|
|
105
|
-
log(` ${c.yellow}✗${c.reset} LLMLingua-2 sidecar not installed`)
|
|
106
|
-
log(` Run: ${c.cyan}curl -fsSL tamp.dev/setup.sh | bash${c.reset}`)
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
const { config: cfg, server: srv } = { config: finalConfig, server: finalServer }
|
|
110
|
-
|
|
111
|
-
srv.listen(cfg.port, () => {
|
|
112
|
-
printBanner(cfg)
|
|
195
|
+
server.listen(config.port, () => {
|
|
196
|
+
printBanner()
|
|
113
197
|
})
|
|
198
|
+
|
|
199
|
+
process.on('exit', () => sidecarProc?.kill())
|
|
200
|
+
process.on('SIGINT', () => { sidecarProc?.kill(); process.exit() })
|
|
201
|
+
process.on('SIGTERM', () => { sidecarProc?.kill(); process.exit() })
|
package/compress.js
CHANGED
|
@@ -3,16 +3,45 @@ import { countTokens } from '@anthropic-ai/tokenizer'
|
|
|
3
3
|
import { tryParseJSON, classifyContent, stripLineNumbers } from './detect.js'
|
|
4
4
|
import { anthropic } from './providers.js'
|
|
5
5
|
|
|
6
|
+
const cache = new Map()
|
|
7
|
+
const MAX_CACHE = 500
|
|
8
|
+
|
|
9
|
+
function cacheKey(text) {
|
|
10
|
+
if (text.length < 128) return text
|
|
11
|
+
return `${text.length}:${text.slice(0, 64)}:${text.slice(-64)}`
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function clearCache() { cache.clear() }
|
|
15
|
+
|
|
16
|
+
function normalizeWhitespace(text) {
|
|
17
|
+
return text
|
|
18
|
+
.replace(/[ \t]+$/gm, '')
|
|
19
|
+
.replace(/\n{3,}/g, '\n\n')
|
|
20
|
+
}
|
|
21
|
+
|
|
6
22
|
export function compressText(text, config) {
|
|
7
23
|
if (text.length < config.minSize) return null
|
|
8
24
|
const cls = classifyContent(text)
|
|
9
25
|
if (cls === 'toon') return null
|
|
26
|
+
|
|
10
27
|
if (cls === 'text') {
|
|
28
|
+
let processed = text
|
|
29
|
+
if (config.stages.includes('strip-lines')) {
|
|
30
|
+
const stripped = stripLineNumbers(text)
|
|
31
|
+
if (stripped !== text) processed = stripped
|
|
32
|
+
}
|
|
33
|
+
if (config.stages.includes('whitespace')) {
|
|
34
|
+
processed = normalizeWhitespace(processed)
|
|
35
|
+
}
|
|
36
|
+
if (processed.length < text.length * 0.9) {
|
|
37
|
+
return { text: processed, method: 'normalize', originalLen: text.length, compressedLen: processed.length, originalTokens: countTokens(text), compressedTokens: countTokens(processed) }
|
|
38
|
+
}
|
|
11
39
|
if (config.stages.includes('llmlingua') && config.llmLinguaUrl) {
|
|
12
|
-
return { async: true, text, cls }
|
|
40
|
+
return { async: true, text: processed, cls }
|
|
13
41
|
}
|
|
14
42
|
return null
|
|
15
43
|
}
|
|
44
|
+
|
|
16
45
|
if (cls !== 'json' && cls !== 'json-lined') return null
|
|
17
46
|
|
|
18
47
|
const raw = cls === 'json-lined' ? stripLineNumbers(text) : text
|
|
@@ -56,11 +85,23 @@ async function compressWithLLMLingua(text, config) {
|
|
|
56
85
|
}
|
|
57
86
|
|
|
58
87
|
async function compressBlock(text, config) {
|
|
88
|
+
const key = cacheKey(text)
|
|
89
|
+
if (cache.has(key)) return cache.get(key)
|
|
90
|
+
|
|
59
91
|
const sync = compressText(text, config)
|
|
92
|
+
let result
|
|
60
93
|
if (sync && sync.async) {
|
|
61
|
-
|
|
94
|
+
result = await compressWithLLMLingua(sync.text, config)
|
|
95
|
+
} else {
|
|
96
|
+
result = sync
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
if (cache.size >= MAX_CACHE) {
|
|
100
|
+
const firstKey = cache.keys().next().value
|
|
101
|
+
cache.delete(firstKey)
|
|
62
102
|
}
|
|
63
|
-
|
|
103
|
+
cache.set(key, result)
|
|
104
|
+
return result
|
|
64
105
|
}
|
|
65
106
|
|
|
66
107
|
export async function compressRequest(body, config, provider) {
|
package/config.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function loadConfig(env = process.env) {
|
|
2
|
-
const stages = (env.TAMP_STAGES || 'minify,toon').split(',').map(s => s.trim()).filter(Boolean)
|
|
2
|
+
const stages = (env.TAMP_STAGES || 'minify,toon,strip-lines,whitespace,llmlingua').split(',').map(s => s.trim()).filter(Boolean)
|
|
3
3
|
return Object.freeze({
|
|
4
4
|
port: parseInt(env.TAMP_PORT, 10) || 7778,
|
|
5
5
|
upstream: env.TAMP_UPSTREAM || 'https://api.anthropic.com',
|
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"providers.js",
|
|
10
10
|
"stats.js"
|
|
11
11
|
],
|
|
12
|
-
"version": "0.
|
|
13
|
-
"description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf.
|
|
12
|
+
"version": "0.3.0",
|
|
13
|
+
"description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 32.0% fewer input tokens.",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"main": "index.js",
|
|
16
16
|
"bin": {
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"homepage": "https://github.com/sliday/tamp",
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"@anthropic-ai/tokenizer": "^0.0.4",
|
|
42
|
+
"@inquirer/prompts": "^8.3.2",
|
|
42
43
|
"@toon-format/toon": "^2.1.0",
|
|
43
44
|
"fzstd": "^0.1.1"
|
|
44
45
|
}
|
package/providers.js
CHANGED
|
@@ -7,29 +7,26 @@ const anthropic = {
|
|
|
7
7
|
const targets = []
|
|
8
8
|
if (!body?.messages?.length) return targets
|
|
9
9
|
|
|
10
|
-
let
|
|
11
|
-
|
|
12
|
-
if (
|
|
13
|
-
}
|
|
14
|
-
if (lastUserIdx === -1) return targets
|
|
15
|
-
|
|
16
|
-
const msg = body.messages[lastUserIdx]
|
|
10
|
+
for (let mi = 0; mi < body.messages.length; mi++) {
|
|
11
|
+
const msg = body.messages[mi]
|
|
12
|
+
if (msg.role !== 'user') continue
|
|
17
13
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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 }
|
|
25
21
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
+
}
|
|
33
30
|
}
|
|
34
31
|
}
|
|
35
32
|
}
|
|
@@ -53,7 +50,8 @@ const openai = {
|
|
|
53
50
|
match(method, url) {
|
|
54
51
|
return method === 'POST' && (
|
|
55
52
|
url.startsWith('/v1/chat/completions') ||
|
|
56
|
-
url.startsWith('/chat/completions')
|
|
53
|
+
url.startsWith('/chat/completions') ||
|
|
54
|
+
url.startsWith('/v1/responses')
|
|
57
55
|
)
|
|
58
56
|
},
|
|
59
57
|
normalizeUrl(url) {
|
|
@@ -63,7 +61,7 @@ const openai = {
|
|
|
63
61
|
extract(body) {
|
|
64
62
|
const targets = []
|
|
65
63
|
|
|
66
|
-
// Responses API format: body.input array
|
|
64
|
+
// Responses API format: body.input array (Codex CLI)
|
|
67
65
|
if (body?.input?.length) {
|
|
68
66
|
for (let i = 0; i < body.input.length; i++) {
|
|
69
67
|
const item = body.input[i]
|
|
@@ -86,20 +84,9 @@ const openai = {
|
|
|
86
84
|
// Chat Completions format
|
|
87
85
|
if (!body?.messages?.length) return targets
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
let lastAssistantIdx = -1
|
|
91
|
-
for (let i = body.messages.length - 1; i >= 0; i--) {
|
|
92
|
-
if (body.messages[i].role === 'assistant' && body.messages[i].tool_calls?.length) {
|
|
93
|
-
lastAssistantIdx = i
|
|
94
|
-
break
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
if (lastAssistantIdx === -1) return targets
|
|
98
|
-
|
|
99
|
-
// Collect all subsequent role:tool messages
|
|
100
|
-
for (let i = lastAssistantIdx + 1; i < body.messages.length; i++) {
|
|
87
|
+
for (let i = 0; i < body.messages.length; i++) {
|
|
101
88
|
const msg = body.messages[i]
|
|
102
|
-
if (msg.role !== 'tool')
|
|
89
|
+
if (msg.role !== 'tool') continue
|
|
103
90
|
if (typeof msg.content === 'string') {
|
|
104
91
|
targets.push({ path: ['messages', i, 'content'], text: msg.content, index: i })
|
|
105
92
|
}
|
|
@@ -126,8 +113,7 @@ const gemini = {
|
|
|
126
113
|
const targets = []
|
|
127
114
|
if (!body?.contents?.length) return targets
|
|
128
115
|
|
|
129
|
-
|
|
130
|
-
for (let ci = body.contents.length - 1; ci >= 0; ci--) {
|
|
116
|
+
for (let ci = 0; ci < body.contents.length; ci++) {
|
|
131
117
|
const content = body.contents[ci]
|
|
132
118
|
if (!content.parts?.length) continue
|
|
133
119
|
for (let pi = 0; pi < content.parts.length; pi++) {
|
|
@@ -142,7 +128,6 @@ const gemini = {
|
|
|
142
128
|
wasObject: typeof resp !== 'string',
|
|
143
129
|
})
|
|
144
130
|
}
|
|
145
|
-
if (targets.length) break
|
|
146
131
|
}
|
|
147
132
|
return targets
|
|
148
133
|
},
|
|
@@ -152,7 +137,6 @@ const gemini = {
|
|
|
152
137
|
let obj = body
|
|
153
138
|
const path = t.path
|
|
154
139
|
for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
|
|
155
|
-
// If original was object, try to parse compressed back to object
|
|
156
140
|
if (t.wasObject) {
|
|
157
141
|
try {
|
|
158
142
|
obj[path[path.length - 1]] = JSON.parse(t.compressed)
|