@sliday/tamp 0.5.1 → 0.5.3

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
@@ -48,7 +48,7 @@ Auto-detects API format, compresses tool output, forwards upstream. Error result
48
48
  | `diff` | Replace similar re-reads with diffs |
49
49
  | `prune` | Remove low-value metadata |
50
50
 
51
- Opt-in stages: `strip-comments`, `textpress` (LLM semantic compression), `graph` (session-scoped dedup across requests — shines on long Codex/Claude plan sessions that re-read the same files across turns, up to -99% per repeat block)
51
+ Opt-in stages: `strip-comments`, `textpress` (LLM semantic compression), `graph` (session-scoped dedup — works on any coding agent: Codex, Claude Code, Aider — anywhere the same file is read twice, up to -99% per repeat block)
52
52
 
53
53
  ## Quick Start
54
54
 
package/package.json CHANGED
@@ -8,10 +8,11 @@
8
8
  "detect.js",
9
9
  "metadata.js",
10
10
  "providers.js",
11
+ "session-graph.js",
11
12
  "stats.js",
12
13
  "lib/"
13
14
  ],
14
- "version": "0.5.1",
15
+ "version": "0.5.3",
15
16
  "description": "Token compression proxy for coding agents. Works with Claude Code, Aider, Cursor, Cline, Windsurf. 60-70% combined input+output savings with Caveman-integrated presets.",
16
17
  "type": "module",
17
18
  "main": "index.js",
@@ -0,0 +1,60 @@
1
+ import { createHash } from 'node:crypto'
2
+
3
+ const SESSION_TTL_MS = 2 * 60 * 60 * 1000
4
+ const MAX_SESSIONS = 50
5
+ const MIN_BYTES = 512
6
+
7
+ export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_SESSIONS } = {}) {
8
+ const store = new Map()
9
+
10
+ function prune() {
11
+ const cutoff = Date.now() - ttlMs
12
+ for (const [k, bucket] of store) {
13
+ if (bucket.lastSeen < cutoff) store.delete(k)
14
+ }
15
+ }
16
+
17
+ function getBucket(key) {
18
+ if (!key) return null
19
+ prune()
20
+ let bucket = store.get(key)
21
+ if (bucket) {
22
+ bucket.lastSeen = Date.now()
23
+ return bucket
24
+ }
25
+ if (store.size >= maxSessions) {
26
+ const oldest = store.keys().next().value
27
+ store.delete(oldest)
28
+ }
29
+ bucket = { refs: new Map(), nextId: 0, lastSeen: Date.now() }
30
+ store.set(key, bucket)
31
+ return bucket
32
+ }
33
+
34
+ return { getBucket, prune, _store: store }
35
+ }
36
+
37
+ export function deriveSessionKey(headers) {
38
+ if (!headers) return null
39
+ const auth = headers.authorization || headers['x-api-key'] || ''
40
+ if (!auth) return null
41
+ return createHash('sha256').update(auth).digest('hex').slice(0, 16)
42
+ }
43
+
44
+ export function graphDeduplicateTargets(targets, bucket, { minBytes = MIN_BYTES } = {}) {
45
+ if (!bucket) return
46
+ for (const target of targets) {
47
+ if (target.skip || target.dedup || target.diffed || target.compressed) continue
48
+ if (typeof target.text !== 'string' || target.text.length < minBytes) continue
49
+
50
+ const hash = createHash('sha256').update(target.text).digest('hex').slice(0, 12)
51
+ const existing = bucket.refs.get(hash)
52
+ if (existing) {
53
+ target.compressed = `<tamp-file-ref id="${existing.id}" sha="${hash}" bytes="${target.text.length}"/>`
54
+ target.graphed = true
55
+ continue
56
+ }
57
+ bucket.nextId += 1
58
+ bucket.refs.set(hash, { id: bucket.nextId, bytes: target.text.length })
59
+ }
60
+ }