@sliday/tamp 0.4.6 → 0.4.7
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 +50 -248
- package/compress.js +82 -1
- package/config.js +6 -0
- package/metadata.js +2 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,320 +6,122 @@
|
|
|
6
6
|
npx @sliday/tamp
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
Or install globally:
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
npm i -g @sliday/tamp
|
|
13
|
-
```
|
|
14
|
-
|
|
15
9
|
### Claude Code plugin
|
|
16
10
|
|
|
17
|
-
Auto-start
|
|
11
|
+
Auto-start on every session:
|
|
18
12
|
|
|
19
13
|
```bash
|
|
20
14
|
claude plugin marketplace add sliday/claude-plugins
|
|
21
15
|
claude plugin install tamp@sliday
|
|
22
16
|
```
|
|
23
17
|
|
|
24
|
-
|
|
18
|
+
Adds `/tamp:status` and `/tamp:config` commands.
|
|
25
19
|
|
|
26
|
-
### 🦞 OpenClaw
|
|
20
|
+
### 🦞 OpenClaw
|
|
27
21
|
|
|
28
22
|
```bash
|
|
29
|
-
# 1. Start Tamp
|
|
30
23
|
TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune tamp -y
|
|
31
|
-
|
|
32
|
-
# 2. Add provider to OpenClaw config
|
|
33
|
-
# baseUrl: "http://localhost:7778"
|
|
34
|
-
# apiKey: "${ANTHROPIC_API_KEY}"
|
|
35
|
-
# api: "anthropic-messages"
|
|
36
|
-
|
|
37
|
-
# 3. Set as primary model → done
|
|
38
24
|
```
|
|
39
25
|
|
|
40
|
-
|
|
26
|
+
Set provider to `http://localhost:7778` → done. Full guide: [docs/openclaw-setup.md](docs/openclaw-setup.md)
|
|
41
27
|
|
|
42
28
|
## How It Works
|
|
43
29
|
|
|
44
|
-
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.
|
|
45
|
-
|
|
46
30
|
```
|
|
47
31
|
Claude Code ──► Tamp (localhost:7778) ──► Anthropic API
|
|
48
32
|
Aider/Cursor ──► │ ──► OpenAI API
|
|
49
33
|
Gemini CLI ────► │ ──► Google AI API
|
|
50
|
-
│
|
|
51
|
-
├─ JSON → minify whitespace
|
|
52
|
-
├─ Arrays → TOON columnar encoding
|
|
53
|
-
├─ Line-numbered → strip prefixes + minify
|
|
54
|
-
├─ Source code → passthrough
|
|
55
|
-
└─ Errors → skip
|
|
56
34
|
```
|
|
57
35
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
| Format | Endpoint | Agents |
|
|
61
|
-
|--------|----------|--------|
|
|
62
|
-
| Anthropic Messages | `POST /v1/messages` | Claude Code |
|
|
63
|
-
| OpenAI Chat Completions | `POST /v1/chat/completions` | Aider, Cursor, Cline, Windsurf, OpenCode |
|
|
64
|
-
| Google Gemini | `POST .../generateContent` | Gemini CLI |
|
|
36
|
+
Auto-detects API format, compresses tool output, forwards upstream. Error results skipped. JSON minified, arrays encoded columnarly, text/code normalized or semantically compressed.
|
|
65
37
|
|
|
66
|
-
|
|
38
|
+
**Compression Stages** (all enabled by default):
|
|
67
39
|
|
|
68
|
-
|
|
40
|
+
| Stage | What |
|
|
41
|
+
|-------|------|
|
|
42
|
+
| `minify` | Strip JSON whitespace |
|
|
43
|
+
| `toon` | Columnar array encoding |
|
|
44
|
+
| `strip-lines` | Remove line-number prefixes |
|
|
45
|
+
| `whitespace` | Collapse blank lines |
|
|
46
|
+
| `llmlingua` | Neural text compression |
|
|
47
|
+
| `dedup` | Replace duplicates with refs |
|
|
48
|
+
| `diff` | Replace similar re-reads with diffs |
|
|
49
|
+
| `prune` | Remove low-value metadata |
|
|
69
50
|
|
|
70
|
-
|
|
71
|
-
|-------|-------------|-----------------|
|
|
72
|
-
| `minify` | Strips JSON whitespace | Pretty-printed JSON objects/arrays |
|
|
73
|
-
| `toon` | Columnar [TOON encoding](https://github.com/nicholasgasior/toon-format) | Homogeneous arrays (file listings, routes, deps) |
|
|
74
|
-
| `strip-lines` | Removes line-number prefixes | Read tool output (` 1→...`) |
|
|
75
|
-
| `whitespace` | Collapses blank lines, trims trailing spaces | CLI output, source code |
|
|
76
|
-
| `llmlingua` | Neural text compression via [LLMLingua-2](https://github.com/microsoft/LLMLingua) | Natural language text (auto-starts sidecar) |
|
|
77
|
-
| `dedup` | Replaces repeated tool output with a short reference | Duplicate results in the eligible compression window |
|
|
78
|
-
| `diff` | Replaces similar re-reads with unified diffs | Similar text blocks in the eligible compression window |
|
|
79
|
-
| `prune` | Removes low-value lockfile/package metadata before minifying | npm metadata and registry URLs |
|
|
80
|
-
| `strip-comments` | Removes comments from text/code | Opt-in lossy mode |
|
|
81
|
-
| `textpress` | LLM semantic text compression via Ollama/OpenRouter | Opt-in lossy mode |
|
|
82
|
-
|
|
83
|
-
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.
|
|
51
|
+
Opt-in lossy stages: `strip-comments`, `textpress` (LLM semantic compression)
|
|
84
52
|
|
|
85
53
|
## Quick Start
|
|
86
54
|
|
|
87
|
-
### Option A: One-line installer
|
|
88
|
-
|
|
89
55
|
```bash
|
|
56
|
+
# Option A: One-line installer
|
|
90
57
|
curl -fsSL https://tamp.dev/setup.sh | bash
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
Installs via npm, sets `ANTHROPIC_BASE_URL` and `OPENAI_API_BASE` in your shell profile. Then just run `tamp` in one terminal and your agent in another.
|
|
94
|
-
|
|
95
|
-
### Option B: Manual
|
|
96
58
|
|
|
97
|
-
|
|
98
|
-
npx @sliday/tamp
|
|
99
|
-
export ANTHROPIC_BASE_URL=http://localhost:7778
|
|
100
|
-
export OPENAI_API_BASE=http://localhost:7778 # Aider, Cursor, Cline
|
|
59
|
+
# Option B: Manual
|
|
60
|
+
npx @sliday/tamp
|
|
61
|
+
export ANTHROPIC_BASE_URL=http://localhost:7778
|
|
101
62
|
```
|
|
102
63
|
|
|
103
|
-
Use your agent as normal — Tamp compresses silently
|
|
64
|
+
Use your agent as normal — Tamp compresses silently.
|
|
104
65
|
|
|
105
66
|
## Configuration
|
|
106
67
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
Run `tamp init` to create a persistent config file:
|
|
110
|
-
|
|
111
|
-
```bash
|
|
112
|
-
tamp init
|
|
113
|
-
# Creates ~/.config/tamp/config
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
Edit the file to set your defaults. All `TAMP_*` variables are supported. Environment variables still override the config file.
|
|
117
|
-
|
|
118
|
-
```bash
|
|
119
|
-
# ~/.config/tamp/config
|
|
120
|
-
TAMP_PORT=7778
|
|
121
|
-
TAMP_UPSTREAM=https://api.anthropic.com
|
|
122
|
-
TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune
|
|
123
|
-
```
|
|
124
|
-
|
|
125
|
-
### Environment variables
|
|
126
|
-
|
|
127
|
-
All configuration also works via environment variables (override config file):
|
|
68
|
+
Run `tamp init` to create `~/.config/tamp/config`. All variables work via env or config file.
|
|
128
69
|
|
|
129
70
|
| Variable | Default | Description |
|
|
130
71
|
|----------|---------|-------------|
|
|
131
|
-
| `TAMP_PORT` | `7778` |
|
|
132
|
-
| `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream
|
|
133
|
-
| `
|
|
134
|
-
| `
|
|
135
|
-
| `
|
|
136
|
-
| `
|
|
137
|
-
| `
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
| `TAMP_CACHE_SAFE` | `true` | Only compress the newest eligible provider payload to preserve prompt-cache stability |
|
|
141
|
-
| `TAMP_LLMLINGUA_URL` | _(none)_ | LLMLingua sidecar URL for text compression |
|
|
142
|
-
|
|
143
|
-
### Recommended setup
|
|
72
|
+
| `TAMP_PORT` | `7778` | Listen port |
|
|
73
|
+
| `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream |
|
|
74
|
+
| `TAMP_STAGES` | *(all default)* | Comma-separated stages |
|
|
75
|
+
| `TAMP_MIN_SIZE` | `200` | Min content size (chars) |
|
|
76
|
+
| `TAMP_LOG` | `true` | Enable logging |
|
|
77
|
+
| `TAMP_CACHE_SAFE` | `true` | Compress newest only (prompt-cache safe) |
|
|
78
|
+
| `TAMP_LLMLINGUA_URL` | *(none)* | LLMLingua sidecar URL |
|
|
79
|
+
|
|
80
|
+
**Recommended setups:**
|
|
144
81
|
|
|
145
82
|
```bash
|
|
146
|
-
#
|
|
83
|
+
# Default (all stages)
|
|
147
84
|
npx @sliday/tamp
|
|
148
85
|
|
|
149
|
-
#
|
|
150
|
-
npx @sliday/tamp -y
|
|
151
|
-
|
|
152
|
-
# Re-compress eligible history too:
|
|
153
|
-
TAMP_CACHE_SAFE=false npx @sliday/tamp -y
|
|
86
|
+
# No Python (skip LLMLingua)
|
|
87
|
+
TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune npx @sliday/tamp -y
|
|
154
88
|
|
|
155
|
-
#
|
|
156
|
-
|
|
89
|
+
# Re-compress history too
|
|
90
|
+
TAMP_CACHE_SAFE=false npx @sliday/tamp
|
|
157
91
|
```
|
|
158
92
|
|
|
159
|
-
## Installation
|
|
160
|
-
|
|
161
|
-
### npx (no install)
|
|
93
|
+
## Installation
|
|
162
94
|
|
|
163
95
|
```bash
|
|
96
|
+
# npx (no install)
|
|
164
97
|
npx @sliday/tamp
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
### npm global
|
|
168
98
|
|
|
169
|
-
|
|
99
|
+
# npm global
|
|
170
100
|
npm install -g @sliday/tamp
|
|
171
|
-
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### Git clone
|
|
175
|
-
|
|
176
|
-
```bash
|
|
177
|
-
git clone https://github.com/sliday/tamp.git
|
|
178
|
-
cd tamp && npm install
|
|
179
|
-
node bin/tamp.js
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
### Systemd service (Ubuntu/Linux)
|
|
183
|
-
|
|
184
|
-
```bash
|
|
185
|
-
npm i -g @sliday/tamp
|
|
186
|
-
tamp init # create config file
|
|
187
|
-
tamp install-service # install + start systemd user service
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
Manages itself via `systemctl --user`. Starts on login, restarts on crash.
|
|
191
|
-
|
|
192
|
-
```bash
|
|
193
|
-
tamp status # check if running
|
|
194
|
-
systemctl --user status tamp
|
|
195
|
-
journalctl --user -u tamp -f
|
|
196
|
-
tamp uninstall-service # remove
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
### One-line installer
|
|
200
|
-
|
|
201
|
-
```bash
|
|
202
|
-
curl -fsSL https://tamp.dev/setup.sh | bash
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
Installs `@sliday/tamp` globally via npm and adds `ANTHROPIC_BASE_URL` + `OPENAI_API_BASE` to your shell profile.
|
|
206
|
-
|
|
207
|
-
## What Gets Compressed
|
|
208
|
-
|
|
209
|
-
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.
|
|
210
|
-
|
|
211
|
-
| Content Type | Action | Example |
|
|
212
|
-
|-------------|--------|---------|
|
|
213
|
-
| Pretty-printed JSON | Minify whitespace | `package.json`, config files |
|
|
214
|
-
| JSON with line numbers | Strip prefixes + minify | Read tool output (` 1→{...}`) |
|
|
215
|
-
| Homogeneous JSON arrays | TOON encode | File listings, route tables, dependencies |
|
|
216
|
-
| Already-minified JSON | Skip | Single-line JSON |
|
|
217
|
-
| Source code (text) | Strip line numbers + normalize whitespace | `.ts`, `.py`, `.rs` files |
|
|
218
|
-
| `is_error: true` results | Skip entirely | Error tool results |
|
|
219
|
-
| TOON-encoded content | Skip | Already compressed |
|
|
220
|
-
|
|
221
|
-
## Architecture
|
|
101
|
+
tamp
|
|
222
102
|
|
|
103
|
+
# systemd service (Linux)
|
|
104
|
+
tamp install-service
|
|
105
|
+
tamp status
|
|
223
106
|
```
|
|
224
|
-
bin/tamp.js CLI entry point
|
|
225
|
-
index.js HTTP proxy server
|
|
226
|
-
providers.js API format adapters (Anthropic, OpenAI, Gemini) + auto-detection
|
|
227
|
-
compress.js Compression pipeline (compressRequest, compressText)
|
|
228
|
-
detect.js Content classification (classifyContent, tryParseJSON, stripLineNumbers)
|
|
229
|
-
config.js Configuration (env vars + ~/.config/tamp/config)
|
|
230
|
-
stats.js Session statistics and request logging
|
|
231
|
-
setup.sh One-line installer script
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
### How the proxy works
|
|
235
|
-
|
|
236
|
-
1. `detectProvider()` auto-detects the API format from the request path
|
|
237
|
-
2. Unrecognized requests are piped through unmodified
|
|
238
|
-
3. Matched requests are buffered, parsed, and tool results are extracted via the provider adapter
|
|
239
|
-
4. Extracted blocks are classified and compressed
|
|
240
|
-
5. The modified body is forwarded to the correct upstream with updated `Content-Length`
|
|
241
|
-
6. The upstream response is streamed back to the client unmodified
|
|
242
107
|
|
|
243
|
-
|
|
108
|
+
## Token Savings
|
|
244
109
|
|
|
245
|
-
|
|
110
|
+
Claude Code sends full conversation history on every API call. Tool results accumulate — files, listings, outputs — all re-sent as input tokens.
|
|
246
111
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
```bash
|
|
250
|
-
OPENROUTER_API_KEY=... node bench/runner.js # 70 API calls, ~2 min
|
|
251
|
-
node bench/analyze.js # Statistical analysis
|
|
252
|
-
node bench/render.js # White paper (HTML + PDF)
|
|
253
|
-
```
|
|
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.
|
|
254
113
|
|
|
255
|
-
|
|
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).
|
|
256
115
|
|
|
257
|
-
|
|
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).
|
|
258
117
|
|
|
259
118
|
## Development
|
|
260
119
|
|
|
261
120
|
```bash
|
|
262
|
-
# Run tests
|
|
263
121
|
npm test
|
|
264
|
-
|
|
265
|
-
# Smoke test (spins up proxy + echo server, validates compression)
|
|
266
122
|
node smoke.js
|
|
267
|
-
|
|
268
|
-
# Run specific test file
|
|
269
|
-
node --test test/compress.test.js
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### Test files
|
|
273
|
-
|
|
274
|
-
```
|
|
275
|
-
test/compress.test.js Compression pipeline tests (Anthropic + OpenAI formats)
|
|
276
|
-
test/providers.test.js Provider adapter + auto-detection tests
|
|
277
|
-
test/detect.test.js Content classification tests
|
|
278
|
-
test/config.test.js Configuration loading tests
|
|
279
|
-
test/proxy.test.js HTTP proxy integration tests
|
|
280
|
-
test/stats.test.js Statistics and logging tests
|
|
281
|
-
test/fixtures/ Sample API payloads
|
|
282
123
|
```
|
|
283
124
|
|
|
284
|
-
## How Token Savings Work
|
|
285
|
-
|
|
286
|
-
Claude Code sends the full conversation history on every API call. As a session progresses, tool results accumulate — file contents, directory listings, command outputs — all re-sent as input tokens on each request.
|
|
287
|
-
|
|
288
|
-
At $3/million input tokens (Sonnet 4.6), a 200-request session consuming 3M input tokens costs $9. With 32% compression on 60% compressible traffic, that's ~63K tokens and $0.19 saved per session. For Opus 4.6 ($5/MTok), savings are $0.32/session.
|
|
289
|
-
|
|
290
|
-
### Claude Max subscribers
|
|
291
|
-
|
|
292
|
-
Max plans have fixed token budgets. With 32% fewer input tokens per request, you get **47% more requests** from the same budget:
|
|
293
|
-
|
|
294
|
-
| Plan | Without Tamp | With Tamp |
|
|
295
|
-
|------|-------------|-----------|
|
|
296
|
-
| Max 5× ($100/mo) | 5× Pro | **7.4×** Pro |
|
|
297
|
-
| Max 20× ($200/mo) | 20× Pro | **29.4×** Pro |
|
|
298
|
-
|
|
299
|
-
### API cost savings
|
|
300
|
-
|
|
301
|
-
| Model | Per dev/month | 10-person team/year |
|
|
302
|
-
|-------|-------------|-------------------|
|
|
303
|
-
| Sonnet 4.6 ($3/MTok) | $28 | $3,400 |
|
|
304
|
-
| Opus 4.6 ($5/MTok) | $48 | $5,760 |
|
|
305
|
-
| Opus 4.6 extended ($10/MTok) | $96 | $11,520 |
|
|
306
|
-
|
|
307
|
-
Based on 5 sessions/day, 200 requests/session, 60% compressible traffic.
|
|
308
|
-
|
|
309
|
-
## Star History
|
|
310
|
-
|
|
311
|
-
<a href="https://www.star-history.com/?repos=sliday%2Ftamp&type=date&legend=top-left">
|
|
312
|
-
<picture>
|
|
313
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/image?repos=sliday/tamp&type=date&theme=dark&legend=top-left" />
|
|
314
|
-
<source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/image?repos=sliday/tamp&type=date&legend=top-left" />
|
|
315
|
-
<img alt="Star History Chart" src="https://api.star-history.com/image?repos=sliday/tamp&type=date&legend=top-left" />
|
|
316
|
-
</picture>
|
|
317
|
-
</a>
|
|
318
|
-
|
|
319
125
|
## License
|
|
320
126
|
|
|
321
|
-
MIT
|
|
322
|
-
|
|
323
|
-
## Author
|
|
324
|
-
|
|
325
|
-
[Stas Kulesh](mailto:stas@sliday.com) — [sliday.com](https://sliday.com)
|
|
127
|
+
MIT © [Stas Kulesh](https://sliday.com)
|
package/compress.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto'
|
|
2
|
+
import { spawn } from 'node:child_process'
|
|
2
3
|
import { encode } from '@toon-format/toon'
|
|
3
4
|
import { countTokens } from '@anthropic-ai/tokenizer'
|
|
4
5
|
import { createPatch } from 'diff'
|
|
@@ -217,6 +218,9 @@ export function compressText(text, config) {
|
|
|
217
218
|
if (config.stages.includes('llmlingua') && config.llmLinguaUrl) {
|
|
218
219
|
return { async: true, asyncMethod: 'llmlingua', text: processed, cls }
|
|
219
220
|
}
|
|
221
|
+
if (config.stages.includes('foundation-models') && config.foundationModelsPath) {
|
|
222
|
+
return { async: true, asyncMethod: 'foundation-models', text: processed, cls }
|
|
223
|
+
}
|
|
220
224
|
if (config.stages.includes('textpress')) {
|
|
221
225
|
return { async: true, asyncMethod: 'textpress', text: processed, cls }
|
|
222
226
|
}
|
|
@@ -263,10 +267,22 @@ export function compressText(text, config) {
|
|
|
263
267
|
|
|
264
268
|
async function compressWithLLMLingua(text, config) {
|
|
265
269
|
try {
|
|
270
|
+
// SSRF protection: only allow localhost URLs
|
|
271
|
+
let url
|
|
272
|
+
try {
|
|
273
|
+
url = new URL(config.llmLinguaUrl + '/compress')
|
|
274
|
+
const hostname = url.hostname.toLowerCase()
|
|
275
|
+
if (hostname !== 'localhost' && hostname !== '127.0.0.1' && hostname !== '::1' && !hostname.startsWith('127.')) {
|
|
276
|
+
return null
|
|
277
|
+
}
|
|
278
|
+
} catch {
|
|
279
|
+
return null
|
|
280
|
+
}
|
|
281
|
+
|
|
266
282
|
const controller = new AbortController()
|
|
267
283
|
const timeout = setTimeout(() => controller.abort(), 5000)
|
|
268
284
|
try {
|
|
269
|
-
const res = await fetch(
|
|
285
|
+
const res = await fetch(url, {
|
|
270
286
|
method: 'POST',
|
|
271
287
|
headers: { 'Content-Type': 'application/json' },
|
|
272
288
|
body: JSON.stringify({ text, rate: config.llmLinguaRate || 0.7 }),
|
|
@@ -281,6 +297,69 @@ async function compressWithLLMLingua(text, config) {
|
|
|
281
297
|
}
|
|
282
298
|
}
|
|
283
299
|
|
|
300
|
+
async function compressWithFoundationModels(text, config) {
|
|
301
|
+
if (!config.foundationModelsPath) return null
|
|
302
|
+
|
|
303
|
+
const SYSTEM_PROMPT = config.foundationModelsSystemPrompt ||
|
|
304
|
+
'Compress this text to 50% length while preserving all key information and meaning. Return only the compressed text without explanation.'
|
|
305
|
+
|
|
306
|
+
try {
|
|
307
|
+
const args = [
|
|
308
|
+
'-o', 'json',
|
|
309
|
+
'-s', SYSTEM_PROMPT,
|
|
310
|
+
'--max-tokens', String(Math.floor(text.length * 0.7)),
|
|
311
|
+
'--quiet',
|
|
312
|
+
text
|
|
313
|
+
]
|
|
314
|
+
|
|
315
|
+
const proc = spawn(config.foundationModelsPath, args, {
|
|
316
|
+
stdio: ['pipe', 'pipe', 'pipe']
|
|
317
|
+
})
|
|
318
|
+
|
|
319
|
+
let responseData = ''
|
|
320
|
+
proc.stdout.on('data', (chunk) => {
|
|
321
|
+
responseData += chunk.toString()
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
const response = await new Promise((resolve, reject) => {
|
|
325
|
+
proc.on('close', (code) => {
|
|
326
|
+
if (code === 5) {
|
|
327
|
+
reject(new Error('FoundationModels not available (requires macOS 15+, Apple Silicon)'))
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
if (code !== 0) {
|
|
331
|
+
reject(new Error(`apfel exited with code ${code}`))
|
|
332
|
+
return
|
|
333
|
+
}
|
|
334
|
+
try {
|
|
335
|
+
const data = JSON.parse(responseData)
|
|
336
|
+
resolve(data)
|
|
337
|
+
} catch (err) {
|
|
338
|
+
reject(new Error(`Failed to parse apfel JSON output: ${err.message}`))
|
|
339
|
+
}
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
proc.on('error', (err) => {
|
|
343
|
+
reject(err)
|
|
344
|
+
})
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
const compressedText = response.content || response.compressedText
|
|
348
|
+
if (!compressedText || compressedText.length >= text.length * 0.9) return null
|
|
349
|
+
|
|
350
|
+
return {
|
|
351
|
+
text: compressedText,
|
|
352
|
+
method: 'foundation-models',
|
|
353
|
+
originalLen: text.length,
|
|
354
|
+
compressedLen: compressedText.length,
|
|
355
|
+
originalTokens: countTokens(text),
|
|
356
|
+
compressedTokens: countTokens(compressedText),
|
|
357
|
+
}
|
|
358
|
+
} catch {
|
|
359
|
+
return null
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
284
363
|
async function compressBlock(text, config) {
|
|
285
364
|
const key = cacheKey(text)
|
|
286
365
|
if (cache.has(key)) return cache.get(key)
|
|
@@ -291,6 +370,8 @@ async function compressBlock(text, config) {
|
|
|
291
370
|
if (sync.asyncMethod === 'textpress') {
|
|
292
371
|
const compressed = await textpressCompress(sync.text, config)
|
|
293
372
|
result = compressed ? { text: compressed, method: 'textpress', originalLen: text.length, compressedLen: compressed.length, originalTokens: countTokens(text), compressedTokens: countTokens(compressed) } : null
|
|
373
|
+
} else if (sync.asyncMethod === 'foundation-models') {
|
|
374
|
+
result = await compressWithFoundationModels(sync.text, config)
|
|
294
375
|
} else {
|
|
295
376
|
result = await compressWithLLMLingua(sync.text, config)
|
|
296
377
|
}
|
package/config.js
CHANGED
|
@@ -58,6 +58,9 @@ export function loadConfig(env = process.env) {
|
|
|
58
58
|
textpressOllamaModel: get('TAMP_TEXTPRESS_OLLAMA_MODEL') || 'qwen3.5:0.8b',
|
|
59
59
|
textpressModel: get('TAMP_TEXTPRESS_MODEL') || 'google/gemini-3.1-flash-lite-preview',
|
|
60
60
|
textpressApiKey: get('TAMP_TEXTPRESS_API_KEY') || get('OPENROUTER_API_KEY') || null,
|
|
61
|
+
foundationModelsPath: get('TAMP_FOUNDATION_MODELS_PATH') || 'apfel',
|
|
62
|
+
foundationModelsTimeout: parseInt(get('TAMP_FOUNDATION_MODELS_TIMEOUT'), 10) || 10000,
|
|
63
|
+
foundationModelsSystemPrompt: get('TAMP_FOUNDATION_MODELS_SYSTEM_PROMPT') || 'Compress this text to 50% length while preserving all key information and meaning. Return only the compressed text without explanation.',
|
|
61
64
|
tokenCost: parseFloat(get('TAMP_TOKEN_COST')) || 3,
|
|
62
65
|
})
|
|
63
66
|
}
|
|
@@ -77,4 +80,7 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
|
|
|
77
80
|
# TAMP_MAX_BODY=10485760
|
|
78
81
|
# TAMP_CACHE_SAFE=true
|
|
79
82
|
# TAMP_TOKEN_COST=3
|
|
83
|
+
# TAMP_FOUNDATION_MODELS_PATH=apfel
|
|
84
|
+
# TAMP_FOUNDATION_MODELS_TIMEOUT=10000
|
|
85
|
+
# TAMP_FOUNDATION_MODELS_SYSTEM_PROMPT=
|
|
80
86
|
`
|
package/metadata.js
CHANGED
|
@@ -18,6 +18,7 @@ export const DEFAULT_STAGES = Object.freeze([
|
|
|
18
18
|
export const EXTRA_STAGES = Object.freeze([
|
|
19
19
|
'strip-comments',
|
|
20
20
|
'textpress',
|
|
21
|
+
'foundation-models',
|
|
21
22
|
])
|
|
22
23
|
|
|
23
24
|
export const ALL_STAGES = Object.freeze([
|
|
@@ -31,6 +32,7 @@ export const STAGE_DESCRIPTIONS = Object.freeze({
|
|
|
31
32
|
'strip-lines': 'Remove line-number prefixes',
|
|
32
33
|
whitespace: 'Collapse blank lines, trim trailing',
|
|
33
34
|
llmlingua: 'Neural compression via LLMLingua-2',
|
|
35
|
+
'foundation-models': 'Apple Intelligence neural compression (macOS 15+, Apple Silicon, 100% local)',
|
|
34
36
|
dedup: 'Deduplicate identical tool_results',
|
|
35
37
|
diff: 'Replace similar re-reads with diffs',
|
|
36
38
|
prune: 'Strip lockfile hashes & npm metadata',
|
package/package.json
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"providers.js",
|
|
11
11
|
"stats.js"
|
|
12
12
|
],
|
|
13
|
-
"version": "0.4.
|
|
13
|
+
"version": "0.4.7",
|
|
14
14
|
"description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 52.6% fewer input tokens.",
|
|
15
15
|
"type": "module",
|
|
16
16
|
"main": "index.js",
|