@sliday/tamp 0.3.12 → 0.4.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
@@ -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
 
@@ -108,7 +113,27 @@ That's it. Use your agent as normal — Tamp compresses silently in the backgrou
108
113
 
109
114
  ## Configuration
110
115
 
111
- All configuration via environment variables:
116
+ ### Config file
117
+
118
+ Run `tamp init` to create a persistent config file:
119
+
120
+ ```bash
121
+ tamp init
122
+ # Creates ~/.config/tamp/config
123
+ ```
124
+
125
+ Edit the file to set your defaults. All `TAMP_*` variables are supported. Environment variables still override the config file.
126
+
127
+ ```bash
128
+ # ~/.config/tamp/config
129
+ TAMP_PORT=7778
130
+ TAMP_UPSTREAM=https://api.anthropic.com
131
+ TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune
132
+ ```
133
+
134
+ ### Environment variables
135
+
136
+ All configuration also works via environment variables (override config file):
112
137
 
113
138
  | Variable | Default | Description |
114
139
  |----------|---------|-------------|
@@ -116,11 +141,12 @@ All configuration via environment variables:
116
141
  | `TAMP_UPSTREAM` | `https://api.anthropic.com` | Default upstream API URL |
117
142
  | `TAMP_UPSTREAM_OPENAI` | `https://api.openai.com` | Upstream for OpenAI-format requests |
118
143
  | `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 |
144
+ | `TAMP_STAGES` | `minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune` | Comma-separated compression stages |
120
145
  | `TAMP_MIN_SIZE` | `200` | Minimum content size (chars) to attempt compression |
121
146
  | `TAMP_LOG` | `true` | Enable request logging to stderr |
122
- | `TAMP_LOG_FILE` | _(none)_ | Write logs to file |
147
+ | `TAMP_LOG_FILE` | _(none)_ | Append request logs to a file when `TAMP_LOG=true` |
123
148
  | `TAMP_MAX_BODY` | `10485760` | Max request body size (bytes) before passthrough |
149
+ | `TAMP_CACHE_SAFE` | `true` | Only compress the newest eligible provider payload to preserve prompt-cache stability |
124
150
  | `TAMP_LLMLINGUA_URL` | _(none)_ | LLMLingua sidecar URL for text compression |
125
151
 
126
152
  ### Recommended setup
@@ -132,6 +158,9 @@ npx @sliday/tamp
132
158
  # Skip interactive prompt (CI/scripts):
133
159
  npx @sliday/tamp -y
134
160
 
161
+ # Re-compress eligible history too:
162
+ TAMP_CACHE_SAFE=false npx @sliday/tamp -y
163
+
135
164
  # Without LLMLingua (no Python needed):
136
165
  TAMP_STAGES=minify,toon,strip-lines,whitespace npx @sliday/tamp -y
137
166
  ```
@@ -159,6 +188,23 @@ cd tamp && npm install
159
188
  node bin/tamp.js
160
189
  ```
161
190
 
191
+ ### Systemd service (Ubuntu/Linux)
192
+
193
+ ```bash
194
+ npm i -g @sliday/tamp
195
+ tamp init # create config file
196
+ tamp install-service # install + start systemd user service
197
+ ```
198
+
199
+ Manages itself via `systemctl --user`. Starts on login, restarts on crash.
200
+
201
+ ```bash
202
+ tamp status # check if running
203
+ systemctl --user status tamp
204
+ journalctl --user -u tamp -f
205
+ tamp uninstall-service # remove
206
+ ```
207
+
162
208
  ### One-line installer
163
209
 
164
210
  ```bash
@@ -169,7 +215,7 @@ The installer clones to `~/.tamp`, adds `ANTHROPIC_BASE_URL` to your shell profi
169
215
 
170
216
  ## What Gets Compressed
171
217
 
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.
218
+ 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
219
 
174
220
  | Content Type | Action | Example |
175
221
  |-------------|--------|---------|
@@ -189,7 +235,7 @@ index.js HTTP proxy server
189
235
  providers.js API format adapters (Anthropic, OpenAI, Gemini) + auto-detection
190
236
  compress.js Compression pipeline (compressRequest, compressText)
191
237
  detect.js Content classification (classifyContent, tryParseJSON, stripLineNumbers)
192
- config.js Environment-based configuration
238
+ config.js Configuration (env vars + ~/.config/tamp/config)
193
239
  stats.js Session statistics and request logging
194
240
  setup.sh One-line installer script
195
241
  ```
package/bin/tamp.js CHANGED
@@ -1,30 +1,16 @@
1
1
  #!/usr/bin/env node
2
2
  import { createProxy } from '../index.js'
3
- import { existsSync, readFileSync } from 'node:fs'
3
+ import { existsSync, mkdirSync, writeFileSync, readFileSync, unlinkSync } 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
+ import { homedir } from 'node:os'
7
8
  import http from 'node:http'
9
+ import { DEFAULT_STAGES, EXTRA_STAGES, STAGE_DESCRIPTIONS, VERSION } from '../metadata.js'
10
+ import { CONFIG_PATH, CONFIG_TEMPLATE } from '../config.js'
8
11
 
9
12
  const __dirname = dirname(fileURLToPath(import.meta.url))
10
13
  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
14
 
29
15
  // --- Sidecar helpers (shared by both modes) ---
30
16
 
@@ -93,6 +79,104 @@ export async function startSidecar() {
93
79
  return null
94
80
  }
95
81
 
82
+ // --- Subcommands ---
83
+ const subcommand = process.argv[2]
84
+
85
+ if (subcommand === 'init') {
86
+ const dir = dirname(CONFIG_PATH)
87
+ if (existsSync(CONFIG_PATH)) {
88
+ console.log(`Config file exists: ${CONFIG_PATH}\n`)
89
+ console.log(readFileSync(CONFIG_PATH, 'utf8'))
90
+ } else {
91
+ mkdirSync(dir, { recursive: true })
92
+ writeFileSync(CONFIG_PATH, CONFIG_TEMPLATE)
93
+ console.log(`Created config file: ${CONFIG_PATH}`)
94
+ console.log('Edit it to set your defaults. Environment variables still override.\n')
95
+ console.log(CONFIG_TEMPLATE)
96
+ }
97
+ process.exit(0)
98
+ }
99
+
100
+ if (subcommand === 'install-service') {
101
+ if (process.platform !== 'linux') {
102
+ console.error('Service installation requires Linux (systemd).')
103
+ process.exit(1)
104
+ }
105
+ const nodeBin = process.execPath
106
+ const tampBin = join(__dirname, 'tamp.js')
107
+ const unitDir = join(homedir(), '.config', 'systemd', 'user')
108
+ const unitPath = join(unitDir, 'tamp.service')
109
+ const unit = `[Unit]
110
+ Description=Tamp token compression proxy
111
+ After=network.target
112
+
113
+ [Service]
114
+ Type=simple
115
+ ExecStart=${nodeBin} ${tampBin} -y
116
+ Restart=always
117
+ RestartSec=5
118
+ EnvironmentFile=-${CONFIG_PATH}
119
+
120
+ [Install]
121
+ WantedBy=default.target
122
+ `
123
+ mkdirSync(unitDir, { recursive: true })
124
+ writeFileSync(unitPath, unit)
125
+ try {
126
+ execFileSync('systemctl', ['--user', 'daemon-reload'], { stdio: 'inherit' })
127
+ execFileSync('systemctl', ['--user', 'enable', '--now', 'tamp.service'], { stdio: 'inherit' })
128
+ console.log(`\nService installed: ${unitPath}`)
129
+ console.log(' Status: systemctl --user status tamp')
130
+ console.log(' Logs: journalctl --user -u tamp -f')
131
+ console.log(' Stop: systemctl --user stop tamp')
132
+ console.log(' Remove: tamp uninstall-service')
133
+ } catch (e) {
134
+ console.error('systemctl failed:', e.message)
135
+ process.exit(1)
136
+ }
137
+ process.exit(0)
138
+ }
139
+
140
+ if (subcommand === 'uninstall-service') {
141
+ if (process.platform !== 'linux') {
142
+ console.error('Service management requires Linux (systemd).')
143
+ process.exit(1)
144
+ }
145
+ const unitPath = join(homedir(), '.config', 'systemd', 'user', 'tamp.service')
146
+ try { execFileSync('systemctl', ['--user', 'stop', 'tamp.service'], { stdio: 'inherit' }) } catch {}
147
+ try { execFileSync('systemctl', ['--user', 'disable', 'tamp.service'], { stdio: 'inherit' }) } catch {}
148
+ try { unlinkSync(unitPath) } catch {}
149
+ try { execFileSync('systemctl', ['--user', 'daemon-reload'], { stdio: 'inherit' }) } catch {}
150
+ console.log('Service removed.')
151
+ process.exit(0)
152
+ }
153
+
154
+ if (subcommand === 'status') {
155
+ const port = process.env.TAMP_PORT || 7778
156
+ const healthy = await checkPort(port)
157
+ console.log(`Tamp v${VERSION}`)
158
+ console.log(` Health: ${healthy ? 'running' : 'not running'} (port ${port})`)
159
+ if (process.platform === 'linux') {
160
+ try { execFileSync('systemctl', ['--user', 'status', 'tamp.service', '--no-pager'], { stdio: 'inherit' }) } catch {}
161
+ }
162
+ process.exit(healthy ? 0 : 1)
163
+ }
164
+
165
+ if (subcommand === 'help' || subcommand === '--help' || subcommand === '-h') {
166
+ console.log(`Tamp v${VERSION} — Token compression proxy for coding agents\n`)
167
+ console.log('Usage: tamp [command] [options]\n')
168
+ console.log('Commands:')
169
+ console.log(' (default) Start proxy (interactive stage picker)')
170
+ console.log(' -y Start proxy (non-interactive, use defaults)')
171
+ console.log(' init Create config file (~/.config/tamp/config)')
172
+ console.log(' status Check if proxy is running')
173
+ console.log(' install-service Install systemd user service (Linux)')
174
+ console.log(' uninstall-service Remove systemd service')
175
+ console.log(' help Show this help')
176
+ console.log(`\nConfig: ${CONFIG_PATH}`)
177
+ process.exit(0)
178
+ }
179
+
96
180
  // --- Mode detection ---
97
181
  const skipPrompt = process.argv.includes('-y') || process.argv.includes('--no-interactive') || !process.stdin.isTTY
98
182
 
@@ -130,11 +214,11 @@ if (skipPrompt) {
130
214
  const extraActive = active.filter(s => EXTRA_STAGES.includes(s))
131
215
 
132
216
  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}`)
217
+ 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
218
  console.error('')
135
219
  console.error(` ${c.bold}Setup:${c.reset}`)
136
220
  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}`)
221
+ console.error(` ${c.dim}Aider/Cursor:${c.reset} OPENAI_API_BASE=${c.yellow}${url}${c.reset}`)
138
222
  console.error('')
139
223
  console.error(` ${c.bold}Stages${c.reset} ${c.dim}(${active.length} active)${c.reset}`)
140
224
  for (const s of defaultActive) {
@@ -167,9 +251,10 @@ if (skipPrompt) {
167
251
 
168
252
  const cleanup = () => { sidecarProc?.kill() }
169
253
  process.on('exit', cleanup)
254
+ process.on('SIGTERM', () => { cleanup(); process.exit() })
170
255
 
171
256
  render(h(App, {
172
- version: pkg.version,
257
+ version: VERSION,
173
258
  envStages,
174
259
  startSidecar,
175
260
  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,24 +1,80 @@
1
+ import { readFileSync } from 'node:fs'
2
+ import { homedir } from 'node:os'
3
+ import { join } from 'node:path'
4
+ import { DEFAULT_STAGES, VERSION } from './metadata.js'
5
+
6
+ function parseBoolean(value, defaultValue) {
7
+ if (value === undefined) return defaultValue
8
+ if (value === 'true') return true
9
+ if (value === 'false') return false
10
+ return defaultValue
11
+ }
12
+
13
+ export const CONFIG_PATH = join(homedir(), '.config', 'tamp', 'config')
14
+
15
+ export function loadConfigFile(path) {
16
+ const filePath = path || process.env.TAMP_CONFIG || CONFIG_PATH
17
+ try {
18
+ const lines = readFileSync(filePath, 'utf8').split('\n')
19
+ const vars = {}
20
+ for (const raw of lines) {
21
+ const line = raw.trim()
22
+ if (!line || line.startsWith('#')) continue
23
+ const eq = line.indexOf('=')
24
+ if (eq < 1) continue
25
+ const key = line.slice(0, eq).trim()
26
+ let val = line.slice(eq + 1).trim()
27
+ if ((val.startsWith('"') && val.endsWith('"')) || (val.startsWith("'") && val.endsWith("'"))) {
28
+ val = val.slice(1, -1)
29
+ }
30
+ vars[key] = val
31
+ }
32
+ return vars
33
+ } catch { return {} }
34
+ }
35
+
1
36
  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)
37
+ const fileVars = (env === process.env) ? loadConfigFile() : {}
38
+ const get = (key) => env[key] !== undefined ? env[key] : fileVars[key]
39
+
40
+ const stages = (get('TAMP_STAGES') || DEFAULT_STAGES.join(',')).split(',').map(s => s.trim()).filter(Boolean)
3
41
  return Object.freeze({
4
- port: parseInt(env.TAMP_PORT, 10) || 7778,
5
- upstream: env.TAMP_UPSTREAM || 'https://api.anthropic.com',
42
+ version: VERSION,
43
+ port: parseInt(get('TAMP_PORT'), 10) || 7778,
44
+ upstream: get('TAMP_UPSTREAM') || 'https://api.anthropic.com',
6
45
  upstreams: Object.freeze({
7
- anthropic: env.TAMP_UPSTREAM || 'https://api.anthropic.com',
8
- openai: env.TAMP_UPSTREAM_OPENAI || 'https://api.openai.com',
9
- gemini: env.TAMP_UPSTREAM_GEMINI || 'https://generativelanguage.googleapis.com',
46
+ anthropic: get('TAMP_UPSTREAM') || 'https://api.anthropic.com',
47
+ openai: get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
48
+ gemini: get('TAMP_UPSTREAM_GEMINI') || 'https://generativelanguage.googleapis.com',
10
49
  }),
11
- minSize: parseInt(env.TAMP_MIN_SIZE, 10) || 200,
50
+ minSize: parseInt(get('TAMP_MIN_SIZE'), 10) || 200,
12
51
  stages,
13
- log: env.TAMP_LOG !== 'false',
14
- logFile: env.TAMP_LOG_FILE || null,
15
- maxBody: parseInt(env.TAMP_MAX_BODY, 10) || 10_485_760,
16
- cacheSafe: true,
17
- llmLinguaUrl: env.TAMP_LLMLINGUA_URL || null,
18
- textpressOllamaUrl: env.TAMP_TEXTPRESS_OLLAMA_URL || 'http://localhost:11434',
19
- textpressOllamaModel: env.TAMP_TEXTPRESS_OLLAMA_MODEL || 'qwen3.5:0.8b',
20
- textpressModel: env.TAMP_TEXTPRESS_MODEL || 'google/gemini-3.1-flash-lite-preview',
21
- textpressApiKey: env.TAMP_TEXTPRESS_API_KEY || env.OPENROUTER_API_KEY || null,
22
- tokenCost: parseFloat(env.TAMP_TOKEN_COST) || 3,
52
+ log: get('TAMP_LOG') !== 'false',
53
+ logFile: get('TAMP_LOG_FILE') || null,
54
+ maxBody: parseInt(get('TAMP_MAX_BODY'), 10) || 10_485_760,
55
+ cacheSafe: parseBoolean(get('TAMP_CACHE_SAFE'), true),
56
+ llmLinguaUrl: get('TAMP_LLMLINGUA_URL') || null,
57
+ textpressOllamaUrl: get('TAMP_TEXTPRESS_OLLAMA_URL') || 'http://localhost:11434',
58
+ textpressOllamaModel: get('TAMP_TEXTPRESS_OLLAMA_MODEL') || 'qwen3.5:0.8b',
59
+ textpressModel: get('TAMP_TEXTPRESS_MODEL') || 'google/gemini-3.1-flash-lite-preview',
60
+ textpressApiKey: get('TAMP_TEXTPRESS_API_KEY') || get('OPENROUTER_API_KEY') || null,
61
+ tokenCost: parseFloat(get('TAMP_TOKEN_COST')) || 3,
23
62
  })
24
63
  }
64
+
65
+ export const CONFIG_TEMPLATE = `# Tamp configuration
66
+ # Environment variables override these values
67
+ # https://github.com/sliday/tamp
68
+
69
+ # TAMP_PORT=7778
70
+ # TAMP_UPSTREAM=https://api.anthropic.com
71
+ # TAMP_UPSTREAM_OPENAI=https://api.openai.com
72
+ # TAMP_UPSTREAM_GEMINI=https://generativelanguage.googleapis.com
73
+ # TAMP_STAGES=minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune
74
+ # TAMP_MIN_SIZE=200
75
+ # TAMP_LOG=true
76
+ # TAMP_LOG_FILE=
77
+ # TAMP_MAX_BODY=10485760
78
+ # TAMP_CACHE_SAFE=true
79
+ # TAMP_TOKEN_COST=3
80
+ `
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.4.0",
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) {