@sliday/tamp 0.3.10 → 0.3.12
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/bin/tamp.js +90 -116
- package/bin/ui/App.js +87 -0
- package/bin/ui/Banner.js +62 -0
- package/bin/ui/Dashboard.js +105 -0
- package/bin/ui/SidecarLoader.js +43 -0
- package/bin/ui/StagePicker.js +61 -0
- package/bin/ui/theme.js +14 -0
- package/index.js +2 -1
- package/package.json +5 -3
package/bin/tamp.js
CHANGED
|
@@ -4,20 +4,12 @@ import { existsSync, readFileSync } 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 { checkbox, Separator } from '@inquirer/prompts'
|
|
8
7
|
import http from 'node:http'
|
|
9
8
|
|
|
10
9
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
11
10
|
const root = join(__dirname, '..')
|
|
12
11
|
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf-8'))
|
|
13
12
|
|
|
14
|
-
const c = {
|
|
15
|
-
reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m',
|
|
16
|
-
green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m',
|
|
17
|
-
magenta: '\x1b[35m', cyan: '\x1b[36m', bgGreen: '\x1b[42m', red: '\x1b[31m',
|
|
18
|
-
}
|
|
19
|
-
function log(msg = '') { console.error(msg) }
|
|
20
|
-
|
|
21
13
|
const DEFAULT_STAGES = ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune']
|
|
22
14
|
const EXTRA_STAGES = ['strip-comments', 'textpress']
|
|
23
15
|
|
|
@@ -34,55 +26,11 @@ const STAGE_DESC = {
|
|
|
34
26
|
textpress: 'LLM semantic compression (Ollama/OpenRouter)',
|
|
35
27
|
}
|
|
36
28
|
|
|
37
|
-
// ---
|
|
38
|
-
const skipPrompt = process.argv.includes('-y') || process.argv.includes('--no-interactive') || !process.stdin.isTTY
|
|
39
|
-
let selectedStages
|
|
40
|
-
|
|
41
|
-
// Determine pre-checked stages from env var (if set)
|
|
42
|
-
const envStages = process.env.TAMP_STAGES
|
|
43
|
-
? new Set(process.env.TAMP_STAGES.split(',').map(s => s.trim()).filter(Boolean))
|
|
44
|
-
: null
|
|
45
|
-
|
|
46
|
-
if (skipPrompt) {
|
|
47
|
-
selectedStages = envStages ? [...envStages] : [...DEFAULT_STAGES]
|
|
48
|
-
} else {
|
|
49
|
-
log('')
|
|
50
|
-
log(` ${c.bold}${c.cyan}Tamp${c.reset} ${c.dim}v${pkg.version}${c.reset}`)
|
|
51
|
-
log(` ${c.dim}Token compression proxy for coding agents${c.reset}`)
|
|
52
|
-
log('')
|
|
53
|
-
|
|
54
|
-
selectedStages = await checkbox({
|
|
55
|
-
message: 'Select compression stages:',
|
|
56
|
-
choices: [
|
|
57
|
-
new Separator(`${c.dim}── Default (lossless) ──${c.reset}`),
|
|
58
|
-
...DEFAULT_STAGES.map(s => ({
|
|
59
|
-
name: `${c.cyan}${s.padEnd(15)}${c.reset} ${c.dim}${STAGE_DESC[s]}${c.reset}`,
|
|
60
|
-
value: s,
|
|
61
|
-
checked: envStages ? envStages.has(s) : true,
|
|
62
|
-
})),
|
|
63
|
-
new Separator(`${c.dim}── Extra (lossy, opt-in) ──${c.reset}`),
|
|
64
|
-
...EXTRA_STAGES.map(s => ({
|
|
65
|
-
name: `${c.yellow}${s.padEnd(15)}${c.reset} ${c.dim}${STAGE_DESC[s]}${c.reset}`,
|
|
66
|
-
value: s,
|
|
67
|
-
checked: envStages ? envStages.has(s) : false,
|
|
68
|
-
})),
|
|
69
|
-
],
|
|
70
|
-
pageSize: 15,
|
|
71
|
-
loop: false,
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
if (selectedStages.length === 0) {
|
|
75
|
-
log(`\n ${c.red}No stages selected. At least one is required.${c.reset}`)
|
|
76
|
-
process.exit(1)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
29
|
+
// --- Sidecar helpers (shared by both modes) ---
|
|
81
30
|
|
|
82
|
-
// --- Sidecar startup ---
|
|
83
31
|
let sidecarProc = null
|
|
84
32
|
|
|
85
|
-
|
|
33
|
+
function checkPort(port) {
|
|
86
34
|
return new Promise(resolve => {
|
|
87
35
|
const req = http.get(`http://127.0.0.1:${port}/health`, (res) => {
|
|
88
36
|
res.resume(); resolve(res.statusCode === 200)
|
|
@@ -96,19 +44,29 @@ function hasCommand(cmd) {
|
|
|
96
44
|
try { execFileSync('which', [cmd], { stdio: 'ignore' }); return true } catch { return false }
|
|
97
45
|
}
|
|
98
46
|
|
|
99
|
-
|
|
47
|
+
function waitForSidecar(proc, port, timeout = 30000) {
|
|
48
|
+
return new Promise(resolve => {
|
|
49
|
+
const timer = setTimeout(() => resolve(null), timeout)
|
|
50
|
+
proc.stderr.on('data', (d) => {
|
|
51
|
+
if (d.toString().includes('Uvicorn running')) {
|
|
52
|
+
clearTimeout(timer)
|
|
53
|
+
resolve(`http://localhost:${port}`)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
proc.on('exit', () => { clearTimeout(timer); resolve(null) })
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function startSidecar() {
|
|
100
61
|
const sidecarPort = 8788
|
|
101
62
|
const sidecarDir = join(root, 'sidecar')
|
|
102
63
|
const serverPy = join(sidecarDir, 'server.py')
|
|
103
64
|
|
|
104
65
|
if (await checkPort(sidecarPort)) {
|
|
105
|
-
log(` ${c.green}✓${c.reset} LLMLingua-2 sidecar already running on :${sidecarPort}`)
|
|
106
66
|
return `http://localhost:${sidecarPort}`
|
|
107
67
|
}
|
|
108
68
|
if (!existsSync(serverPy)) return null
|
|
109
69
|
|
|
110
|
-
log(` ${c.yellow}→${c.reset} Starting LLMLingua-2 sidecar ...`)
|
|
111
|
-
|
|
112
70
|
if (hasCommand('uv')) {
|
|
113
71
|
try {
|
|
114
72
|
const proc = spawn('uv', [
|
|
@@ -135,69 +93,85 @@ async function startSidecar() {
|
|
|
135
93
|
return null
|
|
136
94
|
}
|
|
137
95
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
const timer = setTimeout(() => resolve(null), timeout)
|
|
141
|
-
proc.stderr.on('data', (d) => {
|
|
142
|
-
if (d.toString().includes('Uvicorn running')) {
|
|
143
|
-
clearTimeout(timer)
|
|
144
|
-
log(` ${c.green}✓${c.reset} LLMLingua-2 ready on :${c.bold}${port}${c.reset}`)
|
|
145
|
-
resolve(`http://localhost:${port}`)
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
proc.on('exit', () => { clearTimeout(timer); resolve(null) })
|
|
149
|
-
})
|
|
150
|
-
}
|
|
96
|
+
// --- Mode detection ---
|
|
97
|
+
const skipPrompt = process.argv.includes('-y') || process.argv.includes('--no-interactive') || !process.stdin.isTTY
|
|
151
98
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
process.env.TAMP_LLMLINGUA_URL = sidecarUrl
|
|
156
|
-
} else {
|
|
157
|
-
log(` ${c.yellow}!${c.reset} LLMLingua-2 not available`)
|
|
158
|
-
if (!hasCommand('uv')) log(` ${c.dim}Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh${c.reset}`)
|
|
159
|
-
log(` ${c.dim}Continuing without neural compression.${c.reset}`)
|
|
160
|
-
selectedStages = selectedStages.filter(s => s !== 'llmlingua')
|
|
161
|
-
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
162
|
-
}
|
|
163
|
-
}
|
|
99
|
+
const envStages = process.env.TAMP_STAGES
|
|
100
|
+
? new Set(process.env.TAMP_STAGES.split(',').map(s => s.trim()).filter(Boolean))
|
|
101
|
+
: null
|
|
164
102
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const extra = s === 'llmlingua' && config.llmLinguaUrl ? ` ${c.dim}(${config.llmLinguaUrl})${c.reset}` : ''
|
|
185
|
-
log(` ${c.green}✓${c.reset} ${c.cyan}${s}${c.reset}${extra}`)
|
|
103
|
+
if (skipPrompt) {
|
|
104
|
+
// --- Non-interactive mode (plain text, no Ink) ---
|
|
105
|
+
const { ANSI: c } = await import('./ui/theme.js')
|
|
106
|
+
|
|
107
|
+
let selectedStages = envStages ? [...envStages] : [...DEFAULT_STAGES]
|
|
108
|
+
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
109
|
+
|
|
110
|
+
if (selectedStages.includes('llmlingua')) {
|
|
111
|
+
const sidecarUrl = await startSidecar()
|
|
112
|
+
if (sidecarUrl) {
|
|
113
|
+
process.env.TAMP_LLMLINGUA_URL = sidecarUrl
|
|
114
|
+
console.error(` ${c.green}\u2713${c.reset} LLMLingua-2 ready on :${c.bold}8788${c.reset}`)
|
|
115
|
+
} else {
|
|
116
|
+
console.error(` ${c.yellow}!${c.reset} LLMLingua-2 not available`)
|
|
117
|
+
if (!hasCommand('uv')) console.error(` ${c.dim}Install uv: curl -LsSf https://astral.sh/uv/install.sh | sh${c.reset}`)
|
|
118
|
+
console.error(` ${c.dim}Continuing without neural compression.${c.reset}`)
|
|
119
|
+
selectedStages = selectedStages.filter(s => s !== 'llmlingua')
|
|
120
|
+
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
121
|
+
}
|
|
186
122
|
}
|
|
187
|
-
|
|
123
|
+
|
|
124
|
+
const { config, server } = createProxy()
|
|
125
|
+
|
|
126
|
+
function printBanner() {
|
|
127
|
+
const url = `http://localhost:${config.port}`
|
|
128
|
+
const active = config.stages
|
|
129
|
+
const defaultActive = active.filter(s => DEFAULT_STAGES.includes(s))
|
|
130
|
+
const extraActive = active.filter(s => EXTRA_STAGES.includes(s))
|
|
131
|
+
|
|
132
|
+
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}`)
|
|
134
|
+
console.error('')
|
|
135
|
+
console.error(` ${c.bold}Setup:${c.reset}`)
|
|
136
|
+
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}`)
|
|
138
|
+
console.error('')
|
|
139
|
+
console.error(` ${c.bold}Stages${c.reset} ${c.dim}(${active.length} active)${c.reset}`)
|
|
140
|
+
for (const s of defaultActive) {
|
|
141
|
+
const extra = s === 'llmlingua' && config.llmLinguaUrl ? ` ${c.dim}(${config.llmLinguaUrl})${c.reset}` : ''
|
|
142
|
+
console.error(` ${c.green}\u2713${c.reset} ${c.cyan}${s}${c.reset}${extra}`)
|
|
143
|
+
}
|
|
188
144
|
for (const s of extraActive) {
|
|
189
|
-
|
|
145
|
+
console.error(` ${c.yellow}\u2713${c.reset} ${c.yellow}${s}${c.reset} ${c.dim}(lossy)${c.reset}`)
|
|
190
146
|
}
|
|
147
|
+
const disabled = [...DEFAULT_STAGES, ...EXTRA_STAGES].filter(s => !active.includes(s))
|
|
148
|
+
if (disabled.length && disabled.length <= 4) {
|
|
149
|
+
console.error(` ${c.dim}\u2717 ${disabled.join(', ')}${c.reset}`)
|
|
150
|
+
}
|
|
151
|
+
console.error('')
|
|
191
152
|
}
|
|
192
|
-
const disabled = [...DEFAULT_STAGES, ...EXTRA_STAGES].filter(s => !active.includes(s))
|
|
193
|
-
if (disabled.length && disabled.length <= 4) {
|
|
194
|
-
log(` ${c.dim}✗ ${disabled.join(', ')}${c.reset}`)
|
|
195
|
-
}
|
|
196
|
-
log('')
|
|
197
|
-
}
|
|
198
153
|
|
|
199
|
-
server.listen(config.port, () => { printBanner() })
|
|
154
|
+
server.listen(config.port, () => { printBanner() })
|
|
200
155
|
|
|
201
|
-
process.on('exit', () => sidecarProc?.kill())
|
|
202
|
-
process.on('SIGINT', () => { sidecarProc?.kill(); process.exit() })
|
|
203
|
-
process.on('SIGTERM', () => { sidecarProc?.kill(); process.exit() })
|
|
156
|
+
process.on('exit', () => sidecarProc?.kill())
|
|
157
|
+
process.on('SIGINT', () => { sidecarProc?.kill(); process.exit() })
|
|
158
|
+
process.on('SIGTERM', () => { sidecarProc?.kill(); process.exit() })
|
|
159
|
+
|
|
160
|
+
} else {
|
|
161
|
+
// --- Interactive mode (Ink TUI) ---
|
|
162
|
+
const { render } = await import('ink')
|
|
163
|
+
const React = await import('react')
|
|
164
|
+
const { App } = await import('./ui/App.js')
|
|
165
|
+
|
|
166
|
+
const h = React.createElement
|
|
167
|
+
|
|
168
|
+
const cleanup = () => { sidecarProc?.kill() }
|
|
169
|
+
process.on('exit', cleanup)
|
|
170
|
+
|
|
171
|
+
render(h(App, {
|
|
172
|
+
version: pkg.version,
|
|
173
|
+
envStages,
|
|
174
|
+
startSidecar,
|
|
175
|
+
createProxy,
|
|
176
|
+
}))
|
|
177
|
+
}
|
package/bin/ui/App.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import React, { useState, useEffect, useCallback, useRef } from 'react'
|
|
2
|
+
import { Box, Text, useApp } from 'ink'
|
|
3
|
+
import { StagePicker } from './StagePicker.js'
|
|
4
|
+
import { SidecarLoader } from './SidecarLoader.js'
|
|
5
|
+
import { Dashboard } from './Dashboard.js'
|
|
6
|
+
|
|
7
|
+
const h = React.createElement
|
|
8
|
+
|
|
9
|
+
const DEFAULT_STAGES = ['minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'prune']
|
|
10
|
+
|
|
11
|
+
export function App({ version, envStages, startSidecar, createProxy }) {
|
|
12
|
+
const { exit } = useApp()
|
|
13
|
+
const [phase, setPhase] = useState('pick')
|
|
14
|
+
const [stages, setStages] = useState(null)
|
|
15
|
+
const [events, setEvents] = useState([])
|
|
16
|
+
const [totals, setTotals] = useState({ totalSaved: 0, totalOriginal: 0, totalTokensSaved: 0, compressionCount: 0 })
|
|
17
|
+
const [config, setConfig] = useState(null)
|
|
18
|
+
const serverRef = useRef(null)
|
|
19
|
+
const sidecarUrlRef = useRef(null)
|
|
20
|
+
const eventIdRef = useRef(0)
|
|
21
|
+
|
|
22
|
+
const onCompress = useCallback((stats, newTotals, meta) => {
|
|
23
|
+
const id = eventIdRef.current++
|
|
24
|
+
setEvents(prev => [...prev, { id, stats, totals: newTotals, meta }])
|
|
25
|
+
setTotals(newTotals)
|
|
26
|
+
}, [])
|
|
27
|
+
|
|
28
|
+
const startProxy = useCallback((selectedStages, sidecarUrl) => {
|
|
29
|
+
process.env.TAMP_STAGES = selectedStages.join(',')
|
|
30
|
+
if (sidecarUrl) process.env.TAMP_LLMLINGUA_URL = sidecarUrl
|
|
31
|
+
|
|
32
|
+
const { config: proxyConfig, server } = createProxy({ onCompress, log: false })
|
|
33
|
+
serverRef.current = server
|
|
34
|
+
setConfig(proxyConfig)
|
|
35
|
+
|
|
36
|
+
server.listen(proxyConfig.port, () => {
|
|
37
|
+
setPhase('running')
|
|
38
|
+
})
|
|
39
|
+
}, [onCompress, createProxy])
|
|
40
|
+
|
|
41
|
+
const onStagesSelected = useCallback((selected) => {
|
|
42
|
+
setStages(selected)
|
|
43
|
+
if (selected.includes('llmlingua')) {
|
|
44
|
+
setPhase('loading')
|
|
45
|
+
} else {
|
|
46
|
+
startProxy(selected, null)
|
|
47
|
+
}
|
|
48
|
+
}, [startProxy])
|
|
49
|
+
|
|
50
|
+
const onSidecarReady = useCallback((url) => {
|
|
51
|
+
sidecarUrlRef.current = url
|
|
52
|
+
startProxy(stages, url)
|
|
53
|
+
}, [stages, startProxy])
|
|
54
|
+
|
|
55
|
+
const onSidecarFail = useCallback(() => {
|
|
56
|
+
const filtered = stages.filter(s => s !== 'llmlingua')
|
|
57
|
+
setStages(filtered)
|
|
58
|
+
startProxy(filtered, null)
|
|
59
|
+
}, [stages, startProxy])
|
|
60
|
+
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
const shutdown = () => {
|
|
63
|
+
serverRef.current?.close()
|
|
64
|
+
exit()
|
|
65
|
+
}
|
|
66
|
+
process.on('SIGINT', shutdown)
|
|
67
|
+
process.on('SIGTERM', shutdown)
|
|
68
|
+
return () => {
|
|
69
|
+
process.off('SIGINT', shutdown)
|
|
70
|
+
process.off('SIGTERM', shutdown)
|
|
71
|
+
}
|
|
72
|
+
}, [exit])
|
|
73
|
+
|
|
74
|
+
if (phase === 'pick') {
|
|
75
|
+
return h(StagePicker, { version, envStages, onSelect: onStagesSelected })
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (phase === 'loading') {
|
|
79
|
+
return h(SidecarLoader, { startSidecar, onReady: onSidecarReady, onFail: onSidecarFail })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (phase === 'running' && config) {
|
|
83
|
+
return h(Dashboard, { version, config, events, totals })
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return h(Box, { paddingX: 1 }, h(Text, { dimColor: true }, 'Starting...'))
|
|
87
|
+
}
|
package/bin/ui/Banner.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Box, Text } from 'ink'
|
|
3
|
+
import { Badge } from '@inkjs/ui'
|
|
4
|
+
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
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
|
+
export function Banner({ version, port, stages, llmLinguaUrl }) {
|
|
12
|
+
const url = `http://localhost:${port}`
|
|
13
|
+
const active = new Set(stages)
|
|
14
|
+
const defaultActive = DEFAULT_STAGES.filter(s => active.has(s))
|
|
15
|
+
const extraActive = EXTRA_STAGES.filter(s => active.has(s))
|
|
16
|
+
const disabled = ALL_STAGES.filter(s => !active.has(s))
|
|
17
|
+
|
|
18
|
+
return h(Box, { flexDirection: 'column', paddingX: 1 },
|
|
19
|
+
h(Box, { gap: 2 },
|
|
20
|
+
h(Text, { bold: true, color: 'cyan' }, 'Tamp'),
|
|
21
|
+
h(Text, { dimColor: true }, `v${version}`),
|
|
22
|
+
h(Badge, { color: 'green' }, 'READY'),
|
|
23
|
+
h(Text, { color: 'green' }, url),
|
|
24
|
+
),
|
|
25
|
+
h(Text, null, ''),
|
|
26
|
+
h(Text, { bold: true }, 'Setup'),
|
|
27
|
+
h(Box, { paddingLeft: 2, flexDirection: 'column' },
|
|
28
|
+
h(Text, null,
|
|
29
|
+
h(Text, { dimColor: true }, 'Claude Code: '),
|
|
30
|
+
h(Text, null, 'ANTHROPIC_BASE_URL='),
|
|
31
|
+
h(Text, { color: 'yellow' }, url),
|
|
32
|
+
),
|
|
33
|
+
h(Text, null,
|
|
34
|
+
h(Text, { dimColor: true }, 'Aider/Cursor: '),
|
|
35
|
+
h(Text, null, 'OPENAI_BASE_URL='),
|
|
36
|
+
h(Text, { color: 'yellow' }, url),
|
|
37
|
+
),
|
|
38
|
+
),
|
|
39
|
+
h(Text, null, ''),
|
|
40
|
+
h(Text, { bold: true }, `Stages `, h(Text, { dimColor: true }, `(${stages.length} active)`)),
|
|
41
|
+
h(Box, { paddingLeft: 2, flexDirection: 'column' },
|
|
42
|
+
...defaultActive.map(s =>
|
|
43
|
+
h(Text, { key: s },
|
|
44
|
+
h(Text, { color: 'green' }, '\u2713 '),
|
|
45
|
+
h(Text, { color: 'cyan' }, s),
|
|
46
|
+
s === 'llmlingua' && llmLinguaUrl ? h(Text, { dimColor: true }, ` (${llmLinguaUrl})`) : null,
|
|
47
|
+
)
|
|
48
|
+
),
|
|
49
|
+
...extraActive.map(s =>
|
|
50
|
+
h(Text, { key: s },
|
|
51
|
+
h(Text, { color: 'yellow' }, '\u2713 '),
|
|
52
|
+
h(Text, { color: 'yellow' }, s),
|
|
53
|
+
h(Text, { dimColor: true }, ' (lossy)'),
|
|
54
|
+
)
|
|
55
|
+
),
|
|
56
|
+
...(disabled.length && disabled.length <= 4
|
|
57
|
+
? [h(Text, { key: 'disabled', dimColor: true }, `\u2717 ${disabled.join(', ')}`)]
|
|
58
|
+
: []
|
|
59
|
+
),
|
|
60
|
+
),
|
|
61
|
+
)
|
|
62
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Box, Text, Static } from 'ink'
|
|
3
|
+
import { Banner } from './Banner.js'
|
|
4
|
+
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
7
|
+
function fmtSize(n) {
|
|
8
|
+
if (n >= 1048576) return (n / 1048576).toFixed(1) + 'M'
|
|
9
|
+
if (n >= 1024) return (n / 1024).toFixed(1) + 'k'
|
|
10
|
+
return n + ''
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function fmtTokens(n) {
|
|
14
|
+
if (n >= 1000000) return (n / 1000000).toFixed(1) + 'M'
|
|
15
|
+
if (n >= 1000) return (n / 1000).toFixed(1) + 'k'
|
|
16
|
+
return n + ''
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function LogEntry({ event }) {
|
|
20
|
+
const { stats, meta } = event
|
|
21
|
+
const compressed = stats.filter(s => s.method)
|
|
22
|
+
const skipped = stats.filter(s => s.skipped)
|
|
23
|
+
|
|
24
|
+
if (!compressed.length) {
|
|
25
|
+
const reason = skipped.length ? `${skipped.length} skipped` : 'passthrough'
|
|
26
|
+
return h(Text, { dimColor: true },
|
|
27
|
+
` ${meta.provider} ${meta.url} \u2014 ${stats.length} block${stats.length !== 1 ? 's' : ''}, ${reason}`
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const totalOrig = compressed.reduce((a, s) => a + s.originalLen, 0)
|
|
32
|
+
const totalComp = compressed.reduce((a, s) => a + s.compressedLen, 0)
|
|
33
|
+
const pct = (((totalOrig - totalComp) / totalOrig) * 100).toFixed(1)
|
|
34
|
+
const tokSaved = compressed.reduce((a, s) => a + (s.originalTokens || 0) - (s.compressedTokens || 0), 0)
|
|
35
|
+
const n = compressed.length
|
|
36
|
+
|
|
37
|
+
return h(Text, null,
|
|
38
|
+
h(Text, { color: 'cyan' }, ` ${meta.provider}`),
|
|
39
|
+
h(Text, null, ` ${meta.url} `),
|
|
40
|
+
h(Text, { dimColor: true }, `${fmtSize(meta.bodySize)} `),
|
|
41
|
+
h(Text, { color: 'green', bold: true }, `-${pct}%`),
|
|
42
|
+
h(Text, null, ` ${n} block${n !== 1 ? 's' : ''}`),
|
|
43
|
+
tokSaved > 0 ? h(Text, { dimColor: true }, ` (${fmtTokens(tokSaved)} tok saved)`) : null,
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function StatsBar({ totals, tokenCost }) {
|
|
48
|
+
const { totalSaved, totalOriginal, totalTokensSaved, compressionCount } = totals
|
|
49
|
+
const pct = totalOriginal > 0 ? (((totalSaved) / totalOriginal) * 100).toFixed(1) : '0.0'
|
|
50
|
+
const costPerM = tokenCost || 3
|
|
51
|
+
const dollars = (totalTokensSaved / 1_000_000) * costPerM
|
|
52
|
+
|
|
53
|
+
if (compressionCount === 0) {
|
|
54
|
+
return h(Box, { paddingX: 1, marginTop: 1 },
|
|
55
|
+
h(Text, { dimColor: true }, 'Waiting for requests...'),
|
|
56
|
+
)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return h(Box, { paddingX: 1, marginTop: 1, gap: 2 },
|
|
60
|
+
h(Text, null,
|
|
61
|
+
h(Text, { dimColor: true }, 'Session '),
|
|
62
|
+
h(Text, { bold: true, color: 'magenta' }, `${compressionCount}`),
|
|
63
|
+
h(Text, { dimColor: true }, ` block${compressionCount !== 1 ? 's' : ''}`),
|
|
64
|
+
),
|
|
65
|
+
h(Text, null,
|
|
66
|
+
h(Text, { color: 'green', bold: true }, `-${pct}%`),
|
|
67
|
+
h(Text, { dimColor: true }, ' avg'),
|
|
68
|
+
),
|
|
69
|
+
h(Text, null,
|
|
70
|
+
h(Text, { dimColor: true }, 'saved '),
|
|
71
|
+
h(Text, { bold: true }, `${fmtSize(totalSaved)} chars`),
|
|
72
|
+
totalTokensSaved > 0
|
|
73
|
+
? h(Text, null,
|
|
74
|
+
h(Text, { dimColor: true }, ', '),
|
|
75
|
+
h(Text, { bold: true }, `${fmtTokens(totalTokensSaved)} tok`),
|
|
76
|
+
)
|
|
77
|
+
: null,
|
|
78
|
+
),
|
|
79
|
+
dollars > 0.001
|
|
80
|
+
? h(Text, null,
|
|
81
|
+
h(Text, { color: 'green', bold: true }, `$${dollars.toFixed(4)}`),
|
|
82
|
+
h(Text, { dimColor: true }, ` @ $${costPerM}/Mtok`),
|
|
83
|
+
)
|
|
84
|
+
: null,
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function Dashboard({ version, config, events, totals }) {
|
|
89
|
+
return h(Box, { flexDirection: 'column' },
|
|
90
|
+
h(Banner, {
|
|
91
|
+
version,
|
|
92
|
+
port: config.port,
|
|
93
|
+
stages: config.stages,
|
|
94
|
+
llmLinguaUrl: config.llmLinguaUrl,
|
|
95
|
+
}),
|
|
96
|
+
h(StatsBar, { totals, tokenCost: config.tokenCost }),
|
|
97
|
+
events.length > 0
|
|
98
|
+
? h(Box, { flexDirection: 'column', marginTop: 1 },
|
|
99
|
+
h(Static, { items: events },
|
|
100
|
+
(event) => h(LogEntry, { key: event.id, event }),
|
|
101
|
+
),
|
|
102
|
+
)
|
|
103
|
+
: null,
|
|
104
|
+
)
|
|
105
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react'
|
|
2
|
+
import { Box, Text } from 'ink'
|
|
3
|
+
import { Spinner, StatusMessage } from '@inkjs/ui'
|
|
4
|
+
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
7
|
+
export function SidecarLoader({ startSidecar, onReady, onFail }) {
|
|
8
|
+
const [status, setStatus] = useState('loading')
|
|
9
|
+
const [message, setMessage] = useState(null)
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
let mounted = true
|
|
13
|
+
startSidecar().then(url => {
|
|
14
|
+
if (!mounted) return
|
|
15
|
+
if (url) {
|
|
16
|
+
setStatus('success')
|
|
17
|
+
setMessage(`LLMLingua-2 ready on ${url}`)
|
|
18
|
+
setTimeout(() => onReady(url), 600)
|
|
19
|
+
} else {
|
|
20
|
+
setStatus('warning')
|
|
21
|
+
setMessage('LLMLingua-2 not available. Continuing without neural compression.')
|
|
22
|
+
setTimeout(() => onFail(), 1200)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
return () => { mounted = false }
|
|
26
|
+
}, [])
|
|
27
|
+
|
|
28
|
+
if (status === 'loading') {
|
|
29
|
+
return h(Box, { paddingX: 1 },
|
|
30
|
+
h(Spinner, { label: 'Starting LLMLingua-2 sidecar...' }),
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (status === 'success') {
|
|
35
|
+
return h(Box, { paddingX: 1 },
|
|
36
|
+
h(StatusMessage, { variant: 'success' }, message),
|
|
37
|
+
)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return h(Box, { paddingX: 1, flexDirection: 'column' },
|
|
41
|
+
h(StatusMessage, { variant: 'warning' }, message),
|
|
42
|
+
)
|
|
43
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import React, { useState } from 'react'
|
|
2
|
+
import { Box, Text, useInput } from 'ink'
|
|
3
|
+
import { MultiSelect } from '@inkjs/ui'
|
|
4
|
+
|
|
5
|
+
const h = React.createElement
|
|
6
|
+
|
|
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
|
+
export function StagePicker({ version, envStages, onSelect }) {
|
|
24
|
+
const [error, setError] = useState(null)
|
|
25
|
+
|
|
26
|
+
const preSelected = envStages
|
|
27
|
+
? [...envStages]
|
|
28
|
+
: [...DEFAULT_STAGES]
|
|
29
|
+
|
|
30
|
+
const options = [
|
|
31
|
+
...DEFAULT_STAGES.map(s => ({
|
|
32
|
+
label: `${s.padEnd(16)} ${STAGE_DESC[s]}`,
|
|
33
|
+
value: s,
|
|
34
|
+
})),
|
|
35
|
+
...EXTRA_STAGES.map(s => ({
|
|
36
|
+
label: `${s.padEnd(16)} ${STAGE_DESC[s]}`,
|
|
37
|
+
value: s,
|
|
38
|
+
})),
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
return h(Box, { flexDirection: 'column', paddingX: 1 },
|
|
42
|
+
h(Box, { gap: 1, marginBottom: 1 },
|
|
43
|
+
h(Text, { bold: true, color: 'cyan' }, 'Tamp'),
|
|
44
|
+
h(Text, { dimColor: true }, `v${version}`),
|
|
45
|
+
),
|
|
46
|
+
h(Text, { dimColor: true, marginBottom: 1 }, 'Select compression stages (space to toggle, enter to confirm):'),
|
|
47
|
+
h(MultiSelect, {
|
|
48
|
+
options,
|
|
49
|
+
defaultValue: preSelected,
|
|
50
|
+
visibleOptionCount: 12,
|
|
51
|
+
onSubmit: (values) => {
|
|
52
|
+
if (values.length === 0) {
|
|
53
|
+
setError('At least one stage required.')
|
|
54
|
+
return
|
|
55
|
+
}
|
|
56
|
+
onSelect(values)
|
|
57
|
+
},
|
|
58
|
+
}),
|
|
59
|
+
error ? h(Text, { color: 'red' }, error) : null,
|
|
60
|
+
)
|
|
61
|
+
}
|
package/bin/ui/theme.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const colors = {
|
|
2
|
+
brand: 'cyan',
|
|
3
|
+
success: 'green',
|
|
4
|
+
warning: 'yellow',
|
|
5
|
+
error: 'red',
|
|
6
|
+
info: 'blue',
|
|
7
|
+
accent: 'magenta',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const ANSI = {
|
|
11
|
+
reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m',
|
|
12
|
+
green: '\x1b[32m', yellow: '\x1b[33m', blue: '\x1b[34m',
|
|
13
|
+
magenta: '\x1b[35m', cyan: '\x1b[36m', bgGreen: '\x1b[42m', red: '\x1b[31m',
|
|
14
|
+
}
|
package/index.js
CHANGED
|
@@ -194,10 +194,11 @@ return http.createServer(async (req, res) => {
|
|
|
194
194
|
// Send uncompressed — simpler and content-length is accurate
|
|
195
195
|
if (decompressed) delete headers['content-encoding']
|
|
196
196
|
|
|
197
|
+
session.record(stats)
|
|
197
198
|
if (config.log) {
|
|
198
|
-
session.record(stats)
|
|
199
199
|
console.error(formatRequestLog(stats, session, provider.name, req.url, textBody.length, config.tokenCost))
|
|
200
200
|
}
|
|
201
|
+
config.onCompress?.(stats, session.getTotals(), { provider: provider.name, url: req.url, bodySize: textBody.length })
|
|
201
202
|
} catch (err) {
|
|
202
203
|
if (config.log) console.error(`[tamp] passthrough (parse error): ${err.message}`)
|
|
203
204
|
finalBody = rawBody
|
package/package.json
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"providers.js",
|
|
10
10
|
"stats.js"
|
|
11
11
|
],
|
|
12
|
-
"version": "0.3.
|
|
12
|
+
"version": "0.3.12",
|
|
13
13
|
"description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 52.6% fewer input tokens.",
|
|
14
14
|
"type": "module",
|
|
15
15
|
"main": "index.js",
|
|
@@ -40,9 +40,11 @@
|
|
|
40
40
|
"homepage": "https://github.com/sliday/tamp",
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@anthropic-ai/tokenizer": "^0.0.4",
|
|
43
|
-
"@
|
|
43
|
+
"@inkjs/ui": "^2.0.0",
|
|
44
44
|
"@toon-format/toon": "^2.1.0",
|
|
45
45
|
"diff": "^8.0.4",
|
|
46
|
-
"fzstd": "^0.1.1"
|
|
46
|
+
"fzstd": "^0.1.1",
|
|
47
|
+
"ink": "^5.2.0",
|
|
48
|
+
"react": "^18.2.0"
|
|
47
49
|
}
|
|
48
50
|
}
|