@sliday/tamp 0.2.6 → 0.2.8

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/config.js CHANGED
@@ -15,5 +15,6 @@ export function loadConfig(env = process.env) {
15
15
  maxBody: parseInt(env.TAMP_MAX_BODY, 10) || 10_485_760,
16
16
  cacheSafe: true,
17
17
  llmLinguaUrl: env.TAMP_LLMLINGUA_URL || null,
18
+ tokenCost: parseFloat(env.TAMP_TOKEN_COST) || 3,
18
19
  })
19
20
  }
package/index.js CHANGED
@@ -179,9 +179,9 @@ return http.createServer(async (req, res) => {
179
179
  // Send uncompressed — simpler and content-length is accurate
180
180
  if (decompressed) delete headers['content-encoding']
181
181
 
182
- if (config.log && stats.length) {
182
+ if (config.log) {
183
183
  session.record(stats)
184
- console.error(formatRequestLog(stats, session, provider.name, req.url))
184
+ console.error(formatRequestLog(stats, session, provider.name, req.url, textBody.length, config.tokenCost))
185
185
  }
186
186
  } catch (err) {
187
187
  if (config.log) console.error(`[tamp] passthrough (parse error): ${err.message}`)
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "providers.js",
10
10
  "stats.js"
11
11
  ],
12
- "version": "0.2.6",
12
+ "version": "0.2.8",
13
13
  "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 33.9% fewer input tokens.",
14
14
  "type": "module",
15
15
  "main": "index.js",
package/stats.js CHANGED
@@ -1,40 +1,71 @@
1
- export function formatRequestLog(stats, session, providerName, url) {
1
+ const c = {
2
+ reset: '\x1b[0m',
3
+ bold: '\x1b[1m',
4
+ dim: '\x1b[2m',
5
+ green: '\x1b[32m',
6
+ yellow: '\x1b[33m',
7
+ cyan: '\x1b[36m',
8
+ magenta: '\x1b[35m',
9
+ red: '\x1b[31m',
10
+ }
11
+
12
+ export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost) {
2
13
  const compressed = stats.filter(s => s.method)
3
- const skipped = stats.filter(s => s.skipped)
4
14
  const label = providerName || 'anthropic'
5
15
  const path = url || '/v1/messages'
6
- const lines = [`[tamp] ${label} ${path}${stats.length} blocks, ${compressed.length} compressed`]
7
-
8
- for (const s of stats) {
9
- if (s.skipped) {
10
- lines.push(`[tamp] block[${s.index}]: skipped (${s.skipped})`)
11
- } else if (s.method) {
12
- const pct = (((s.originalLen - s.compressedLen) / s.originalLen) * 100).toFixed(1)
13
- const tokInfo = s.originalTokens ? ` ${s.originalTokens}->${s.compressedTokens} tok` : ''
14
- lines.push(`[tamp] block[${s.index}]: ${s.originalLen}->${s.compressedLen} chars (-${pct}%)${tokInfo} [${s.method}]`)
15
- }
16
+ const sizeInfo = bodySize ? ` ${c.dim}${fmtSize(bodySize)}${c.reset}` : ''
17
+
18
+ if (!compressed.length && !stats.length) {
19
+ return `[tamp] ${c.cyan}${label}${c.reset} ${path}${sizeInfo} ${c.dim}— no tool blocks${c.reset}`
20
+ }
21
+
22
+ if (!compressed.length) {
23
+ const skipCount = stats.filter(s => s.skipped).length
24
+ const reason = skipCount ? `${skipCount} skipped` : 'nothing to compress'
25
+ return `[tamp] ${c.cyan}${label}${c.reset} ${path}${sizeInfo} ${c.dim}— ${stats.length} blocks, ${reason}${c.reset}`
16
26
  }
17
27
 
18
28
  const totalOrig = compressed.reduce((a, s) => a + s.originalLen, 0)
19
29
  const totalComp = compressed.reduce((a, s) => a + s.compressedLen, 0)
20
30
  const totalOrigTok = compressed.reduce((a, s) => a + (s.originalTokens || 0), 0)
21
31
  const totalCompTok = compressed.reduce((a, s) => a + (s.compressedTokens || 0), 0)
22
- if (compressed.length > 0) {
23
- const pct = (((totalOrig - totalComp) / totalOrig) * 100).toFixed(1)
24
- const tokPct = totalOrigTok > 0 ? (((totalOrigTok - totalCompTok) / totalOrigTok) * 100).toFixed(1) : '0.0'
25
- lines.push(`[tamp] total: ${totalOrig}->${totalComp} chars (-${pct}%), ${totalOrigTok}->${totalCompTok} tokens (-${tokPct}%)`)
32
+ const pct = (((totalOrig - totalComp) / totalOrig) * 100).toFixed(1)
33
+ const saved = totalOrig - totalComp
34
+ const tokSaved = totalOrigTok - totalCompTok
35
+
36
+ const lines = []
37
+ lines.push(`[tamp] ${c.cyan}${label}${c.reset} ${path}${sizeInfo} ${c.green}— ${compressed.length} compressed, -${pct}%${c.reset}`)
38
+
39
+ for (const s of compressed) {
40
+ const sPct = (((s.originalLen - s.compressedLen) / s.originalLen) * 100).toFixed(1)
41
+ const tokInfo = s.originalTokens ? ` ${c.dim}${s.originalTokens}→${s.compressedTokens} tok${c.reset}` : ''
42
+ lines.push(`[tamp] ${c.dim}block[${s.index}]${c.reset} ${fmtSize(s.originalLen)}→${fmtSize(s.compressedLen)} ${c.green}-${sPct}%${c.reset}${tokInfo} ${c.dim}[${s.method}]${c.reset}`)
43
+ }
44
+
45
+ for (const s of stats.filter(s => s.skipped)) {
46
+ lines.push(`[tamp] ${c.dim}block[${s.index}] skipped (${s.skipped})${c.reset}`)
26
47
  }
27
48
 
28
49
  if (session) {
29
- const totals = session.getTotals()
30
- lines.push(`[tamp] session: ${totals.totalSaved} chars, ${totals.totalTokensSaved} tokens saved across ${totals.compressionCount} compressions`)
50
+ const t = session.getTotals()
51
+ const sessionPct = t.totalOriginal > 0 ? (((t.totalSaved) / t.totalOriginal) * 100).toFixed(1) : '0.0'
52
+ const costPerM = tokenCost || 3
53
+ const dollarsSaved = (t.totalTokensSaved / 1_000_000) * costPerM
54
+ const moneyInfo = t.totalTokensSaved > 0 ? ` ${c.green}$${dollarsSaved.toFixed(4)} saved${c.reset} ${c.dim}@ $${costPerM}/Mtok${c.reset}` : ''
55
+ lines.push(`[tamp] ${c.magenta}session${c.reset} ${fmtSize(t.totalSaved)} chars, ${t.totalTokensSaved} tokens saved across ${t.compressionCount} blocks ${c.dim}(${sessionPct}% avg)${c.reset}${moneyInfo}`)
31
56
  }
32
57
 
33
58
  return lines.join('\n')
34
59
  }
35
60
 
61
+ function fmtSize(n) {
62
+ if (n >= 1024) return (n / 1024).toFixed(1) + 'k'
63
+ return n + ''
64
+ }
65
+
36
66
  export function createSession() {
37
67
  let totalSaved = 0
68
+ let totalOriginal = 0
38
69
  let totalTokensSaved = 0
39
70
  let compressionCount = 0
40
71
 
@@ -43,13 +74,14 @@ export function createSession() {
43
74
  for (const s of stats) {
44
75
  if (s.method && s.originalLen && s.compressedLen) {
45
76
  totalSaved += s.originalLen - s.compressedLen
77
+ totalOriginal += s.originalLen
46
78
  totalTokensSaved += (s.originalTokens || 0) - (s.compressedTokens || 0)
47
79
  compressionCount++
48
80
  }
49
81
  }
50
82
  },
51
83
  getTotals() {
52
- return { totalSaved, totalTokensSaved, compressionCount }
84
+ return { totalSaved, totalOriginal, totalTokensSaved, compressionCount }
53
85
  },
54
86
  }
55
87
  }