@sliday/tamp 0.4.0 → 0.4.1

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() }
@@ -103,14 +104,30 @@ function pruneJSON(text) {
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
@@ -238,16 +257,17 @@ async function compressWithLLMLingua(text, config) {
238
257
  try {
239
258
  const controller = new AbortController()
240
259
  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) }
260
+ try {
261
+ const res = await fetch(config.llmLinguaUrl + '/compress', {
262
+ method: 'POST',
263
+ headers: { 'Content-Type': 'application/json' },
264
+ body: JSON.stringify({ text, rate: 0.5 }),
265
+ signal: controller.signal,
266
+ })
267
+ if (!res.ok) return null
268
+ const data = await res.json()
269
+ return { text: data.text, method: 'llmlingua', originalLen: text.length, compressedLen: data.text.length, originalTokens: countTokens(text), compressedTokens: countTokens(data.text) }
270
+ } finally { clearTimeout(timeout) }
251
271
  } catch {
252
272
  return null
253
273
  }
@@ -270,11 +290,13 @@ async function compressBlock(text, config) {
270
290
  result = sync
271
291
  }
272
292
 
273
- if (cache.size >= MAX_CACHE) {
274
- const firstKey = cache.keys().next().value
275
- cache.delete(firstKey)
293
+ if (result) {
294
+ if (cache.size >= MAX_CACHE) {
295
+ const firstKey = cache.keys().next().value
296
+ cache.delete(firstKey)
297
+ }
298
+ cache.set(key, result)
276
299
  }
277
- cache.set(key, result)
278
300
  return result
279
301
  }
280
302
 
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.1",
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) {