@sliday/tamp 0.4.0 → 0.4.2

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
@@ -12,6 +12,17 @@ Or install globally:
12
12
  npm i -g @sliday/tamp
13
13
  ```
14
14
 
15
+ ### Claude Code plugin
16
+
17
+ Auto-start Tamp on every session — no manual setup:
18
+
19
+ ```bash
20
+ claude plugin marketplace add sliday/claude-plugins
21
+ claude plugin install tamp@sliday
22
+ ```
23
+
24
+ That's it. The plugin starts the proxy automatically, tells you if something's wrong, and adds `/tamp:status` and `/tamp:config` commands.
25
+
15
26
  ### 🦞 OpenClaw Quick Setup
16
27
 
17
28
  ```bash
package/compress.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { createHash } from 'node:crypto'
1
2
  import { encode } from '@toon-format/toon'
2
3
  import { countTokens } from '@anthropic-ai/tokenizer'
3
4
  import { createPatch } from 'diff'
@@ -9,7 +10,7 @@ const MAX_CACHE = 500
9
10
 
10
11
  function cacheKey(text) {
11
12
  if (text.length < 128) return text
12
- return `${text.length}:${text.slice(0, 64)}:${text.slice(-64)}`
13
+ return createHash('sha256').update(text).digest('base64')
13
14
  }
14
15
 
15
16
  export function clearCache() { cache.clear() }
@@ -97,20 +98,36 @@ function pruneJSON(text) {
97
98
  const { ok, value } = tryParseJSON(text)
98
99
  if (!ok) return null
99
100
  const pruned = deepPrune(value)
100
- const result = JSON.stringify(pruned, null, 2)
101
- if (result.length >= text.length * 0.95) return null
102
- return result
101
+ const minified = JSON.stringify(pruned)
102
+ if (minified.length >= text.length * 0.95) return null
103
+ return { value: pruned, minified }
103
104
  }
104
105
 
105
106
  // --- Stage: strip-comments (opt-in, not in defaults) ---
107
+ function stripLineComments(line) {
108
+ let inSingle = false, inDouble = false, inTemplate = false
109
+ for (let i = 0; i < line.length; i++) {
110
+ const ch = line[i]
111
+ const prev = i > 0 ? line[i - 1] : ''
112
+ if (prev === '\\') continue
113
+ if (ch === "'" && !inDouble && !inTemplate) { inSingle = !inSingle; continue }
114
+ if (ch === '"' && !inSingle && !inTemplate) { inDouble = !inDouble; continue }
115
+ if (ch === '`' && !inSingle && !inDouble) { inTemplate = !inTemplate; continue }
116
+ if (inSingle || inDouble || inTemplate) continue
117
+ if (ch === '/' && line[i + 1] === '/') return line.slice(0, i).trimEnd()
118
+ if (ch === '#' && !line.startsWith('#!')) return line.slice(0, i).trimEnd()
119
+ }
120
+ return line
121
+ }
122
+
106
123
  function stripComments(text) {
107
- return text
108
- .replace(/\/\*\*[\s\S]*?\*\//g, '') // JSDoc /** ... */
109
- .replace(/\/\*[\s\S]*?\*\//g, '') // block /* ... */
110
- .replace(/\/\/.*$/gm, '') // line // ...
111
- .replace(/^\s*#(?!!).*$/gm, '') // Python/shell # (not #!)
112
- .replace(/<!--[\s\S]*?-->/g, '') // HTML <!-- ... -->
113
- .replace(/\n\s*\n\s*\n/g, '\n\n') // collapse resulting blank lines
124
+ // Block comments: only strip if they start at line-level (not inside strings)
125
+ let result = text
126
+ .replace(/^[ \t]*\/\*\*[\s\S]*?\*\//gm, '')
127
+ .replace(/^[ \t]*\/\*[\s\S]*?\*\//gm, '')
128
+ .replace(/<!--[\s\S]*?-->/g, '')
129
+ result = result.split('\n').map(stripLineComments).join('\n')
130
+ return result.replace(/\n\s*\n\s*\n/g, '\n\n')
114
131
  }
115
132
 
116
133
  // --- Stage: textpress (opt-in, uses Ollama or OpenRouter free model) ---
@@ -125,24 +142,25 @@ async function textpressCompress(text, config) {
125
142
  try {
126
143
  const controller = new AbortController()
127
144
  const timeout = setTimeout(() => controller.abort(), 15000)
128
- const res = await fetch(`${ollamaUrl}/api/chat`, {
129
- method: 'POST',
130
- headers: { 'Content-Type': 'application/json' },
131
- body: JSON.stringify({
132
- model: ollamaModel,
133
- messages: [{ role: 'system', content: TEXTPRESS_PROMPT }, { role: 'user', content: text }],
134
- stream: false,
135
- options: { temperature: 0, num_predict: Math.ceil(text.length * 0.7) },
136
- think: false,
137
- }),
138
- signal: controller.signal,
139
- })
140
- clearTimeout(timeout)
141
- if (res.ok) {
142
- const data = await res.json()
143
- const output = (data.message?.content || '').trim()
144
- if (output.length > 0 && output.length < text.length * 0.9) return output
145
- }
145
+ try {
146
+ const res = await fetch(`${ollamaUrl}/api/chat`, {
147
+ method: 'POST',
148
+ headers: { 'Content-Type': 'application/json' },
149
+ body: JSON.stringify({
150
+ model: ollamaModel,
151
+ messages: [{ role: 'system', content: TEXTPRESS_PROMPT }, { role: 'user', content: text }],
152
+ stream: false,
153
+ options: { temperature: 0, num_predict: Math.ceil(text.length * 0.7) },
154
+ think: false,
155
+ }),
156
+ signal: controller.signal,
157
+ })
158
+ if (res.ok) {
159
+ const data = await res.json()
160
+ const output = (data.message?.content || '').trim()
161
+ if (output.length > 0 && output.length < text.length * 0.9) return output
162
+ }
163
+ } finally { clearTimeout(timeout) }
146
164
  } catch { /* Ollama not available, try OpenRouter */ }
147
165
 
148
166
  // Fallback: OpenRouter free model
@@ -152,23 +170,24 @@ async function textpressCompress(text, config) {
152
170
  try {
153
171
  const controller = new AbortController()
154
172
  const timeout = setTimeout(() => controller.abort(), 10000)
155
- const res = await fetch('https://openrouter.ai/api/v1/chat/completions', {
156
- method: 'POST',
157
- headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${orKey}` },
158
- body: JSON.stringify({
159
- model: orModel,
160
- messages: [{ role: 'system', content: TEXTPRESS_PROMPT }, { role: 'user', content: text }],
161
- max_tokens: Math.ceil(text.length * 0.5),
162
- temperature: 0,
163
- }),
164
- signal: controller.signal,
165
- })
166
- clearTimeout(timeout)
167
- if (res.ok) {
168
- const data = await res.json()
169
- const output = (data.choices?.[0]?.message?.content || '').replace(/<think>[\s\S]*?<\/think>/g, '').trim()
170
- if (output.length > 0 && output.length < text.length * 0.9) return output
171
- }
173
+ try {
174
+ const res = await fetch('https://openrouter.ai/api/v1/chat/completions', {
175
+ method: 'POST',
176
+ headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${orKey}` },
177
+ body: JSON.stringify({
178
+ model: orModel,
179
+ messages: [{ role: 'system', content: TEXTPRESS_PROMPT }, { role: 'user', content: text }],
180
+ max_tokens: Math.ceil(text.length * 0.5),
181
+ temperature: 0,
182
+ }),
183
+ signal: controller.signal,
184
+ })
185
+ if (res.ok) {
186
+ const data = await res.json()
187
+ const output = (data.choices?.[0]?.message?.content || '').replace(/<think>[\s\S]*?<\/think>/g, '').trim()
188
+ if (output.length > 0 && output.length < text.length * 0.9) return output
189
+ }
190
+ } finally { clearTimeout(timeout) }
172
191
  } catch { /* OpenRouter not available */ }
173
192
 
174
193
  return null
@@ -209,22 +228,29 @@ export function compressText(text, config) {
209
228
  let raw = cls === 'json-lined' ? stripLineNumbers(text) : text
210
229
 
211
230
  // Prune low-value fields before minifying
231
+ let parsedValue
212
232
  if (config.stages.includes('prune')) {
213
233
  const pruned = pruneJSON(raw)
214
- if (pruned) raw = pruned
234
+ if (pruned) {
235
+ parsedValue = pruned.value
236
+ raw = pruned.minified
237
+ }
215
238
  }
216
239
 
217
- const { ok, value } = tryParseJSON(raw)
218
- if (!ok) return null
240
+ if (!parsedValue) {
241
+ const { ok, value } = tryParseJSON(raw)
242
+ if (!ok) return null
243
+ parsedValue = value
244
+ }
219
245
 
220
- const minified = JSON.stringify(value)
246
+ const minified = JSON.stringify(parsedValue)
221
247
  if (minified.length >= text.length) return null
222
248
 
223
249
  let best = { text: minified, method: 'minify' }
224
250
 
225
251
  if (config.stages.includes('toon')) {
226
252
  try {
227
- const tooned = encode(value)
253
+ const tooned = encode(parsedValue)
228
254
  if (tooned.length < best.text.length) {
229
255
  best = { text: tooned, method: 'toon' }
230
256
  }
@@ -238,16 +264,17 @@ async function compressWithLLMLingua(text, config) {
238
264
  try {
239
265
  const controller = new AbortController()
240
266
  const timeout = setTimeout(() => controller.abort(), 5000)
241
- const res = await fetch(config.llmLinguaUrl + '/compress', {
242
- method: 'POST',
243
- headers: { 'Content-Type': 'application/json' },
244
- body: JSON.stringify({ text, rate: 0.5 }),
245
- signal: controller.signal,
246
- })
247
- clearTimeout(timeout)
248
- if (!res.ok) return null
249
- const data = await res.json()
250
- return { text: data.text, method: 'llmlingua', originalLen: text.length, compressedLen: data.text.length, originalTokens: countTokens(text), compressedTokens: countTokens(data.text) }
267
+ try {
268
+ const res = await fetch(config.llmLinguaUrl + '/compress', {
269
+ method: 'POST',
270
+ headers: { 'Content-Type': 'application/json' },
271
+ body: JSON.stringify({ text, rate: 0.5 }),
272
+ signal: controller.signal,
273
+ })
274
+ if (!res.ok) return null
275
+ const data = await res.json()
276
+ return { text: data.text, method: 'llmlingua', originalLen: text.length, compressedLen: data.text.length, originalTokens: countTokens(text), compressedTokens: countTokens(data.text) }
277
+ } finally { clearTimeout(timeout) }
251
278
  } catch {
252
279
  return null
253
280
  }
@@ -270,11 +297,13 @@ async function compressBlock(text, config) {
270
297
  result = sync
271
298
  }
272
299
 
273
- if (cache.size >= MAX_CACHE) {
274
- const firstKey = cache.keys().next().value
275
- cache.delete(firstKey)
300
+ if (result) {
301
+ if (cache.size >= MAX_CACHE) {
302
+ const firstKey = cache.keys().next().value
303
+ cache.delete(firstKey)
304
+ }
305
+ cache.set(key, result)
276
306
  }
277
- cache.set(key, result)
278
307
  return result
279
308
  }
280
309
 
package/index.js CHANGED
@@ -36,17 +36,15 @@ export function createProxy(overrides = {}) {
36
36
  function _createServer(config, session) {
37
37
  const log = createRequestLogger(config)
38
38
 
39
- function forwardRequest(method, upstreamUrl, headers, body, res) {
39
+ function openUpstream(method, upstreamUrl, headers, res) {
40
40
  const mod = upstreamUrl.protocol === 'https:' ? https : http
41
- const opts = {
41
+ const upstream = mod.request({
42
42
  hostname: upstreamUrl.hostname,
43
43
  port: upstreamUrl.port,
44
44
  path: upstreamUrl.pathname + upstreamUrl.search,
45
45
  method,
46
46
  headers,
47
- }
48
-
49
- const upstream = mod.request(opts, (upstreamRes) => {
47
+ }, (upstreamRes) => {
50
48
  res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
51
49
  upstreamRes.pipe(res)
52
50
  upstreamRes.on('error', (err) => {
@@ -68,63 +66,40 @@ function forwardRequest(method, upstreamUrl, headers, body, res) {
68
66
  upstream.destroy()
69
67
  })
70
68
 
71
- if (body) {
72
- upstream.end(body)
73
- } else {
74
- upstream.end()
75
- }
69
+ return upstream
70
+ }
76
71
 
72
+ function forwardRequest(method, upstreamUrl, headers, body, res) {
73
+ const upstream = openUpstream(method, upstreamUrl, headers, res)
74
+ if (body) upstream.end(body)
75
+ else upstream.end()
77
76
  return upstream
78
77
  }
79
78
 
80
79
  function pipeRequest(req, res, upstreamUrl, prefixChunks) {
81
- const mod = upstreamUrl.protocol === 'https:' ? https : http
82
80
  const headers = { ...req.headers }
83
81
  delete headers.host
84
-
85
- const opts = {
86
- hostname: upstreamUrl.hostname,
87
- port: upstreamUrl.port,
88
- path: upstreamUrl.pathname + upstreamUrl.search,
89
- method: req.method,
90
- headers,
91
- }
92
-
93
- const upstream = mod.request(opts, (upstreamRes) => {
94
- res.writeHead(upstreamRes.statusCode, upstreamRes.headers)
95
- upstreamRes.pipe(res)
96
- upstreamRes.on('error', (err) => {
97
- log(`[tamp] response stream error: ${err.code || ''} ${err.message}`)
98
- res.destroy()
99
- })
100
- })
101
-
102
- upstream.on('error', (err) => {
103
- log(`[tamp] upstream error: ${err.code || ''} ${err.message}`)
104
- if (!res.headersSent) {
105
- res.writeHead(502, { 'Content-Type': 'application/json' })
106
- }
107
- res.end(JSON.stringify({ error: 'upstream_error', message: err.message }))
108
- })
109
-
110
- res.on('error', (err) => {
111
- log(`[tamp] client disconnect: ${err.code || ''} ${err.message}`)
112
- upstream.destroy()
113
- })
114
-
82
+ const upstream = openUpstream(req.method, upstreamUrl, headers, res)
115
83
  if (prefixChunks) {
116
- for (const chunk of prefixChunks) {
117
- upstream.write(chunk)
118
- }
84
+ for (const chunk of prefixChunks) upstream.write(chunk)
119
85
  }
120
-
121
86
  req.pipe(upstream)
122
87
  }
123
88
 
124
89
  return http.createServer(async (req, res) => {
125
90
  // Health check endpoint
126
91
  if (req.url === '/health' && (req.method === 'GET' || req.method === 'HEAD')) {
127
- const body = JSON.stringify({ status: 'ok', version: config.version, stages: config.stages })
92
+ const totals = session.getTotals()
93
+ const body = JSON.stringify({
94
+ status: 'ok', version: config.version, stages: config.stages,
95
+ session: {
96
+ requests: totals.requestCount,
97
+ tokensSaved: totals.totalTokensSaved,
98
+ charsSaved: totals.totalSaved,
99
+ charsOriginal: totals.totalOriginal,
100
+ blocksCompressed: totals.compressionCount,
101
+ },
102
+ })
128
103
  res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) })
129
104
  return res.end(req.method === 'HEAD' ? undefined : body)
130
105
  }
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "providers.js",
11
11
  "stats.js"
12
12
  ],
13
- "version": "0.4.0",
13
+ "version": "0.4.2",
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",
package/providers.js CHANGED
@@ -68,23 +68,6 @@ const anthropic = {
68
68
  },
69
69
  }
70
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
71
  function extractOpenAIChatTargets(msg, i) {
89
72
  if (msg.role !== 'tool' || typeof msg.content !== 'string') return []
90
73
  return [{ path: ['messages', i, 'content'], text: msg.content, index: i }]
@@ -95,8 +78,7 @@ const openai = {
95
78
  match(method, url) {
96
79
  return method === 'POST' && (
97
80
  url.startsWith('/v1/chat/completions') ||
98
- url.startsWith('/chat/completions') ||
99
- url.startsWith('/v1/responses')
81
+ url.startsWith('/chat/completions')
100
82
  )
101
83
  },
102
84
  normalizeUrl(url) {
@@ -104,22 +86,6 @@ const openai = {
104
86
  return url
105
87
  },
106
88
  extract(body, config = {}) {
107
- // Responses API format: body.input array (Codex CLI)
108
- if (body?.input?.length) {
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)
115
- }
116
- return targets
117
- }
118
-
119
- return body.input.flatMap(extractOpenAIResponsesTargets)
120
- }
121
-
122
- // Chat Completions format
123
89
  if (!body?.messages?.length) return []
124
90
 
125
91
  if (config.cacheSafe) {
package/stats.js CHANGED
@@ -70,9 +70,11 @@ export function createSession() {
70
70
  let totalOriginal = 0
71
71
  let totalTokensSaved = 0
72
72
  let compressionCount = 0
73
+ let requestCount = 0
73
74
 
74
75
  return {
75
76
  record(stats) {
77
+ requestCount++
76
78
  for (const s of stats) {
77
79
  if (s.method && s.originalLen && s.compressedLen) {
78
80
  totalSaved += s.originalLen - s.compressedLen
@@ -83,7 +85,7 @@ export function createSession() {
83
85
  }
84
86
  },
85
87
  getTotals() {
86
- return { totalSaved, totalOriginal, totalTokensSaved, compressionCount }
88
+ return { totalSaved, totalOriginal, totalTokensSaved, compressionCount, requestCount }
87
89
  },
88
90
  }
89
91
  }