@sliday/tamp 0.5.9 → 0.8.0
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 +80 -13
- package/bin/lifecycle.js +16 -4
- package/bin/tamp.js +149 -6
- package/bin/ui/App.js +11 -3
- package/bin/ui/Banner.js +16 -6
- package/bin/ui/LevelPicker.js +107 -0
- package/compress.js +160 -6
- package/config.js +177 -21
- package/detect.js +10 -0
- package/index.js +105 -14
- package/lib/bm25.js +194 -0
- package/lib/br-cache.js +157 -0
- package/lib/disclosure.js +124 -0
- package/lib/path-extract.js +49 -0
- package/lib/read-cache.js +55 -0
- package/lib/rewriters/commands/cargo.js +50 -0
- package/lib/rewriters/commands/docker.js +54 -0
- package/lib/rewriters/commands/git.js +81 -0
- package/lib/rewriters/commands/jest.js +49 -0
- package/lib/rewriters/commands/npm.js +82 -0
- package/lib/rewriters/commands/pip.js +58 -0
- package/lib/rewriters/commands/prisma.js +50 -0
- package/lib/rewriters/commands/pytest.js +55 -0
- package/lib/rewriters/commands/terraform.js +49 -0
- package/lib/rewriters/commands/wget-curl.js +67 -0
- package/lib/rewriters/index.js +77 -0
- package/lib/rules-generator.js +45 -2
- package/metadata.js +90 -5
- package/package.json +5 -3
- package/providers.js +281 -4
- package/session-graph.js +36 -3
- package/stats.js +12 -1
package/providers.js
CHANGED
|
@@ -94,6 +94,117 @@ function appendToLastUserMessageAnthropic(messages, text) {
|
|
|
94
94
|
return false
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
// ---- Disclosure: find and rehydrate <tamp-ref:v1:...> quotes (Phase 5) ----
|
|
98
|
+
|
|
99
|
+
const DISCLOSURE_MARKER = /<tamp-ref:v1:([a-f0-9]{64}):(\d+)>/g
|
|
100
|
+
|
|
101
|
+
function scanStringForRefs(str, pathDesc, out) {
|
|
102
|
+
if (typeof str !== 'string' || str.indexOf('<tamp-ref:v1:') === -1) return
|
|
103
|
+
const re = new RegExp(DISCLOSURE_MARKER.source, DISCLOSURE_MARKER.flags)
|
|
104
|
+
let m
|
|
105
|
+
while ((m = re.exec(str)) !== null) {
|
|
106
|
+
out.push({ hash: m[1], bytes: Number(m[2]), match: m[0], path: pathDesc.slice() })
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function findAnthropicReferences(body) {
|
|
111
|
+
const out = []
|
|
112
|
+
if (!Array.isArray(body?.messages)) return out
|
|
113
|
+
for (let mi = 0; mi < body.messages.length; mi++) {
|
|
114
|
+
const msg = body.messages[mi]
|
|
115
|
+
if (!msg) continue
|
|
116
|
+
if (typeof msg.content === 'string') {
|
|
117
|
+
scanStringForRefs(msg.content, ['messages', mi, 'content'], out)
|
|
118
|
+
continue
|
|
119
|
+
}
|
|
120
|
+
if (!Array.isArray(msg.content)) continue
|
|
121
|
+
for (let bi = 0; bi < msg.content.length; bi++) {
|
|
122
|
+
const block = msg.content[bi]
|
|
123
|
+
if (!block) continue
|
|
124
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
125
|
+
scanStringForRefs(block.text, ['messages', mi, 'content', bi, 'text'], out)
|
|
126
|
+
} else if (block.type === 'tool_use') {
|
|
127
|
+
if (block.input && typeof block.input === 'object') {
|
|
128
|
+
const serialized = JSON.stringify(block.input)
|
|
129
|
+
scanStringForRefs(serialized, ['messages', mi, 'content', bi, 'input'], out)
|
|
130
|
+
} else if (typeof block.input === 'string') {
|
|
131
|
+
scanStringForRefs(block.input, ['messages', mi, 'content', bi, 'input'], out)
|
|
132
|
+
}
|
|
133
|
+
} else if (block.type === 'tool_result') {
|
|
134
|
+
if (typeof block.content === 'string') {
|
|
135
|
+
scanStringForRefs(block.content, ['messages', mi, 'content', bi, 'content'], out)
|
|
136
|
+
} else if (Array.isArray(block.content)) {
|
|
137
|
+
for (let ci = 0; ci < block.content.length; ci++) {
|
|
138
|
+
const sub = block.content[ci]
|
|
139
|
+
if (sub?.type === 'text' && typeof sub.text === 'string') {
|
|
140
|
+
scanStringForRefs(sub.text, ['messages', mi, 'content', bi, 'content', ci, 'text'], out)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return out
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function getAtPath(body, path) {
|
|
151
|
+
let obj = body
|
|
152
|
+
for (let i = 0; i < path.length; i++) obj = obj?.[path[i]]
|
|
153
|
+
return obj
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function setAtPath(body, path, value) {
|
|
157
|
+
let obj = body
|
|
158
|
+
for (let i = 0; i < path.length - 1; i++) obj = obj[path[i]]
|
|
159
|
+
obj[path[path.length - 1]] = value
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Replace marker(s) at each ref's path with the expanded block. For
|
|
163
|
+
// tool_use.input objects we round-trip via JSON. Idempotent: if the marker
|
|
164
|
+
// has already been replaced by an expansion (which contains "expanded -"
|
|
165
|
+
// sentinel for the same hash), we skip further replacement.
|
|
166
|
+
function applyAnthropicRehydration(body, refs) {
|
|
167
|
+
const groups = new Map()
|
|
168
|
+
for (const ref of refs) {
|
|
169
|
+
if (!ref.expansion) continue
|
|
170
|
+
const key = ref.path.join('|')
|
|
171
|
+
if (!groups.has(key)) groups.set(key, { path: ref.path, items: [] })
|
|
172
|
+
groups.get(key).items.push(ref)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
for (const { path, items } of groups.values()) {
|
|
176
|
+
const isToolUseInput = path[path.length - 1] === 'input'
|
|
177
|
+
let current
|
|
178
|
+
if (isToolUseInput) {
|
|
179
|
+
const val = getAtPath(body, path)
|
|
180
|
+
current = typeof val === 'string' ? val : JSON.stringify(val)
|
|
181
|
+
} else {
|
|
182
|
+
current = getAtPath(body, path)
|
|
183
|
+
}
|
|
184
|
+
if (typeof current !== 'string') continue
|
|
185
|
+
|
|
186
|
+
let updated = current
|
|
187
|
+
for (const ref of items) {
|
|
188
|
+
const expandedSentinel = `<tamp-ref:v1:${ref.hash} expanded -`
|
|
189
|
+
if (updated.indexOf(expandedSentinel) !== -1) continue
|
|
190
|
+
const idx = updated.indexOf(ref.match)
|
|
191
|
+
if (idx === -1) continue
|
|
192
|
+
updated = updated.slice(0, idx) + ref.expansion + updated.slice(idx + ref.match.length)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (updated === current) continue
|
|
196
|
+
|
|
197
|
+
if (isToolUseInput) {
|
|
198
|
+
try {
|
|
199
|
+
const parsed = JSON.parse(updated)
|
|
200
|
+
setAtPath(body, path, parsed)
|
|
201
|
+
continue
|
|
202
|
+
} catch { /* fall through to string */ }
|
|
203
|
+
}
|
|
204
|
+
setAtPath(body, path, updated)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
97
208
|
const anthropic = {
|
|
98
209
|
name: 'anthropic',
|
|
99
210
|
match(method, url) {
|
|
@@ -116,10 +227,38 @@ const anthropic = {
|
|
|
116
227
|
injectOutputHint(body, text) {
|
|
117
228
|
return appendToLastUserMessageAnthropic(body?.messages, text)
|
|
118
229
|
},
|
|
230
|
+
findReferences(body) {
|
|
231
|
+
return findAnthropicReferences(body)
|
|
232
|
+
},
|
|
233
|
+
applyRehydration(body, refs) {
|
|
234
|
+
return applyAnthropicRehydration(body, refs)
|
|
235
|
+
},
|
|
119
236
|
}
|
|
120
237
|
|
|
238
|
+
// OpenAI-compatible block types we don't know how to recompress safely.
|
|
239
|
+
// Kimi's "thinking" / "partial" deltas in particular must be left alone —
|
|
240
|
+
// they are not tool_result text and compressing them breaks the wire format.
|
|
241
|
+
const OPENAI_COMPAT_SKIP_BLOCK_TYPES = new Set(['thinking', 'partial'])
|
|
242
|
+
|
|
121
243
|
function extractOpenAIChatTargets(msg, i) {
|
|
122
|
-
if (msg.role !== 'tool'
|
|
244
|
+
if (msg.role !== 'tool') return []
|
|
245
|
+
// Some OpenAI-compat providers (Kimi, etc.) emit non-string tool content
|
|
246
|
+
// with typed blocks like { type: 'thinking' }. Skip those.
|
|
247
|
+
if (typeof msg.content !== 'string') {
|
|
248
|
+
if (Array.isArray(msg.content)) {
|
|
249
|
+
const targets = []
|
|
250
|
+
for (let j = 0; j < msg.content.length; j++) {
|
|
251
|
+
const block = msg.content[j]
|
|
252
|
+
if (!block || typeof block !== 'object') continue
|
|
253
|
+
if (OPENAI_COMPAT_SKIP_BLOCK_TYPES.has(block.type)) continue
|
|
254
|
+
if (block.type === 'text' && typeof block.text === 'string') {
|
|
255
|
+
targets.push({ path: ['messages', i, 'content', j, 'text'], text: block.text, index: i })
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return targets
|
|
259
|
+
}
|
|
260
|
+
return []
|
|
261
|
+
}
|
|
123
262
|
return [{ path: ['messages', i, 'content'], text: msg.content, index: i }]
|
|
124
263
|
}
|
|
125
264
|
|
|
@@ -176,6 +315,54 @@ const openai = {
|
|
|
176
315
|
},
|
|
177
316
|
}
|
|
178
317
|
|
|
318
|
+
// Kimi Code (subscription) + Moonshot (OpenAI-compat API key) share wire
|
|
319
|
+
// format with OpenAI chat-completions aside from extra block types on
|
|
320
|
+
// streamed messages. Treat them as passthrough: same extract/apply contract
|
|
321
|
+
// as the openai adapter, but a different name so index.js can route them to
|
|
322
|
+
// api.kimi.com / api.moonshot.cn upstreams.
|
|
323
|
+
const kimi = {
|
|
324
|
+
name: 'kimi',
|
|
325
|
+
match(method, url) {
|
|
326
|
+
if (method !== 'POST') return false
|
|
327
|
+
return (
|
|
328
|
+
url.startsWith('/coding/v1/chat/completions') ||
|
|
329
|
+
url.startsWith('/kimi/v1/chat/completions') ||
|
|
330
|
+
url.startsWith('/kimi/coding/v1/chat/completions')
|
|
331
|
+
)
|
|
332
|
+
},
|
|
333
|
+
normalizeUrl(url) {
|
|
334
|
+
// Strip the tamp-mount prefix so the upstream receives the canonical
|
|
335
|
+
// Kimi Code path.
|
|
336
|
+
if (url.startsWith('/kimi/coding/v1/')) return url.slice('/kimi'.length)
|
|
337
|
+
if (url.startsWith('/kimi/v1/')) return '/coding' + url.slice('/kimi'.length)
|
|
338
|
+
return url
|
|
339
|
+
},
|
|
340
|
+
extract: openai.extract,
|
|
341
|
+
apply: openai.apply,
|
|
342
|
+
getLastUserText: openai.getLastUserText,
|
|
343
|
+
injectOutputHint: openai.injectOutputHint,
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const moonshot = {
|
|
347
|
+
name: 'moonshot',
|
|
348
|
+
match(method, url) {
|
|
349
|
+
if (method !== 'POST') return false
|
|
350
|
+
return (
|
|
351
|
+
url.startsWith('/moonshot/v1/chat/completions') ||
|
|
352
|
+
url.startsWith('/moonshot/chat/completions')
|
|
353
|
+
)
|
|
354
|
+
},
|
|
355
|
+
normalizeUrl(url) {
|
|
356
|
+
if (url.startsWith('/moonshot/v1/')) return url.slice('/moonshot'.length)
|
|
357
|
+
if (url.startsWith('/moonshot/chat/completions')) return '/v1' + url.slice('/moonshot'.length)
|
|
358
|
+
return url
|
|
359
|
+
},
|
|
360
|
+
extract: openai.extract,
|
|
361
|
+
apply: openai.apply,
|
|
362
|
+
getLastUserText: openai.getLastUserText,
|
|
363
|
+
injectOutputHint: openai.injectOutputHint,
|
|
364
|
+
}
|
|
365
|
+
|
|
179
366
|
function extractGeminiContentTargets(content, ci) {
|
|
180
367
|
const targets = []
|
|
181
368
|
if (!content.parts?.length) return targets
|
|
@@ -325,13 +512,103 @@ const openaiResponses = {
|
|
|
325
512
|
},
|
|
326
513
|
}
|
|
327
514
|
|
|
328
|
-
|
|
515
|
+
// ---- Auth-mode detection for OpenAI-like providers ----
|
|
516
|
+
//
|
|
517
|
+
// Codex CLI running in ChatGPT Plus mode does NOT use a traditional
|
|
518
|
+
// `sk-*` API key. It sends an OAuth JWT as the Bearer token, plus a
|
|
519
|
+
// `chatgpt-account-id` header. The upstream in that mode is
|
|
520
|
+
// https://chatgpt.com/backend-api/codex (NOT api.openai.com). See
|
|
521
|
+
// openai/codex `model-provider-info/src/lib.rs` for the canonical list.
|
|
522
|
+
//
|
|
523
|
+
// Returns:
|
|
524
|
+
// 'api-key' — classic `Authorization: Bearer sk-...`
|
|
525
|
+
// 'chatgpt-oauth' — JWT bearer OR chatgpt-account-id header present
|
|
526
|
+
// 'unknown' — neither
|
|
527
|
+
function headerValue(headers, name) {
|
|
528
|
+
if (!headers) return null
|
|
529
|
+
// Node's IncomingHeaders are already lowercased, but be defensive for
|
|
530
|
+
// plain objects passed from tests.
|
|
531
|
+
if (headers[name] !== undefined) return headers[name]
|
|
532
|
+
const lower = name.toLowerCase()
|
|
533
|
+
if (headers[lower] !== undefined) return headers[lower]
|
|
534
|
+
for (const k of Object.keys(headers)) {
|
|
535
|
+
if (k.toLowerCase() === lower) return headers[k]
|
|
536
|
+
}
|
|
537
|
+
return null
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function extractBearer(auth) {
|
|
541
|
+
if (typeof auth !== 'string') return null
|
|
542
|
+
const m = auth.match(/^\s*Bearer\s+(.+?)\s*$/i)
|
|
543
|
+
return m ? m[1] : null
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function looksLikeJwt(token) {
|
|
547
|
+
if (typeof token !== 'string') return false
|
|
548
|
+
// Compact JWS: three base64url segments separated by dots. Header segment
|
|
549
|
+
// starts with "eyJ" (base64-encoded `{"`). Good enough for detection.
|
|
550
|
+
if (!token.startsWith('eyJ')) return false
|
|
551
|
+
const parts = token.split('.')
|
|
552
|
+
return parts.length === 3 && parts.every(p => p.length > 0)
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export function detectOpenAIAuthMode(headersOrRequest) {
|
|
556
|
+
const headers = headersOrRequest?.headers || headersOrRequest || {}
|
|
557
|
+
const accountId = headerValue(headers, 'chatgpt-account-id')
|
|
558
|
+
const auth = headerValue(headers, 'authorization')
|
|
559
|
+
const token = extractBearer(auth)
|
|
329
560
|
|
|
330
|
-
|
|
561
|
+
if (accountId) return 'chatgpt-oauth'
|
|
562
|
+
if (token && token.startsWith('sk-')) return 'api-key'
|
|
563
|
+
if (token && looksLikeJwt(token)) return 'chatgpt-oauth'
|
|
564
|
+
return 'unknown'
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
// Route selection for OpenAI-like requests. Returns the upstream base URL
|
|
568
|
+
// and a path transformer. Callers are responsible for enforcing
|
|
569
|
+
// TAMP_DISABLE_CHATGPT_ROUTE (handled in index.js).
|
|
570
|
+
export function resolveOpenAIUpstream({ mode, base, providerName }) {
|
|
571
|
+
if (mode === 'chatgpt-oauth') {
|
|
572
|
+
return {
|
|
573
|
+
base: 'https://chatgpt.com',
|
|
574
|
+
transformPath(path) {
|
|
575
|
+
// Codex calls land on /v1/responses or /v1/chat/completions. The
|
|
576
|
+
// ChatGPT backend expects /backend-api/codex<rest>.
|
|
577
|
+
if (path.startsWith('/backend-api/codex')) return path
|
|
578
|
+
return '/backend-api/codex' + path
|
|
579
|
+
},
|
|
580
|
+
mode,
|
|
581
|
+
providerName,
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
return {
|
|
585
|
+
base,
|
|
586
|
+
transformPath(path) { return path },
|
|
587
|
+
mode,
|
|
588
|
+
providerName,
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
const providers = [anthropic, openai, openaiResponses, gemini, kimi, moonshot]
|
|
593
|
+
|
|
594
|
+
// detectProvider(method, url, headers?) — headers is optional for b/c. When
|
|
595
|
+
// the path is OpenAI-like but headers carry a `x-tamp-target: kimi|moonshot`
|
|
596
|
+
// hint we route to the corresponding adapter. Path-based matching runs
|
|
597
|
+
// first so existing mounts keep working.
|
|
598
|
+
export function detectProvider(method, url, headers) {
|
|
599
|
+
const hint = headers ? headerValue(headers, 'x-tamp-target') : null
|
|
600
|
+
if (hint) {
|
|
601
|
+
const hinted = providers.find(p => p.name === hint)
|
|
602
|
+
if (hinted && hinted.match(method, url)) return hinted
|
|
603
|
+
// Hint may point to a kimi/moonshot provider even if path doesn't carry
|
|
604
|
+
// the tamp mount prefix (e.g. user pointed their client at /v1/...).
|
|
605
|
+
if (hint === 'kimi' && openai.match(method, url)) return kimi
|
|
606
|
+
if (hint === 'moonshot' && openai.match(method, url)) return moonshot
|
|
607
|
+
}
|
|
331
608
|
for (const p of providers) {
|
|
332
609
|
if (p.match(method, url)) return p
|
|
333
610
|
}
|
|
334
611
|
return null
|
|
335
612
|
}
|
|
336
613
|
|
|
337
|
-
export { anthropic, openai, openaiResponses, gemini }
|
|
614
|
+
export { anthropic, openai, openaiResponses, gemini, kimi, moonshot }
|
package/session-graph.js
CHANGED
|
@@ -4,7 +4,7 @@ const SESSION_TTL_MS = 2 * 60 * 60 * 1000
|
|
|
4
4
|
const MAX_SESSIONS = 50
|
|
5
5
|
const MIN_BYTES = 512
|
|
6
6
|
|
|
7
|
-
export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_SESSIONS } = {}) {
|
|
7
|
+
export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_SESSIONS, brCache = null } = {}) {
|
|
8
8
|
const store = new Map()
|
|
9
9
|
|
|
10
10
|
function prune() {
|
|
@@ -26,7 +26,7 @@ export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_S
|
|
|
26
26
|
const oldest = store.keys().next().value
|
|
27
27
|
store.delete(oldest)
|
|
28
28
|
}
|
|
29
|
-
bucket = {
|
|
29
|
+
bucket = createSessionBucket({ brCache })
|
|
30
30
|
store.set(key, bucket)
|
|
31
31
|
return bucket
|
|
32
32
|
}
|
|
@@ -34,6 +34,27 @@ export function createSessionStore({ ttlMs = SESSION_TTL_MS, maxSessions = MAX_S
|
|
|
34
34
|
return { getBucket, prune, _store: store }
|
|
35
35
|
}
|
|
36
36
|
|
|
37
|
+
// A bucket tracks per-session graph state. Optionally pipes large target
|
|
38
|
+
// bodies through a Brotli disk cache so the in-memory structure stays lean
|
|
39
|
+
// and content persists across proxy restarts for Phase 5 rehydration.
|
|
40
|
+
export function createSessionBucket({ brCache = null } = {}) {
|
|
41
|
+
const refs = new Map()
|
|
42
|
+
let brotliBytes = 0
|
|
43
|
+
let offloaded = 0
|
|
44
|
+
let hits = 0
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
refs,
|
|
48
|
+
nextId: 0,
|
|
49
|
+
lastSeen: Date.now(),
|
|
50
|
+
brCache,
|
|
51
|
+
// Counters incremented by graphDeduplicateTargets when brCache is attached.
|
|
52
|
+
brStats() { return { brotliBytes, offloaded, hits } },
|
|
53
|
+
_recordOffload(bytes) { brotliBytes += bytes; offloaded += 1 },
|
|
54
|
+
_recordHit() { hits += 1 },
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
37
58
|
export function deriveSessionKey(headers) {
|
|
38
59
|
if (!headers) return null
|
|
39
60
|
const auth = headers.authorization || headers['x-api-key'] || ''
|
|
@@ -43,6 +64,7 @@ export function deriveSessionKey(headers) {
|
|
|
43
64
|
|
|
44
65
|
export function graphDeduplicateTargets(targets, bucket, { minBytes = MIN_BYTES } = {}) {
|
|
45
66
|
if (!bucket) return
|
|
67
|
+
const brCache = bucket.brCache
|
|
46
68
|
for (const target of targets) {
|
|
47
69
|
if (target.skip || target.dedup || target.diffed || target.compressed) continue
|
|
48
70
|
if (typeof target.text !== 'string' || target.text.length < minBytes) continue
|
|
@@ -52,9 +74,20 @@ export function graphDeduplicateTargets(targets, bucket, { minBytes = MIN_BYTES
|
|
|
52
74
|
if (existing) {
|
|
53
75
|
target.compressed = `<tamp-file-ref id="${existing.id}" sha="${hash}" bytes="${target.text.length}"/>`
|
|
54
76
|
target.graphed = true
|
|
77
|
+
if (brCache && existing.brHash) bucket._recordHit()
|
|
55
78
|
continue
|
|
56
79
|
}
|
|
57
80
|
bucket.nextId += 1
|
|
58
|
-
|
|
81
|
+
const entry = { id: bucket.nextId, bytes: target.text.length }
|
|
82
|
+
// Opportunistic offload to the disk store — Phase 3 collects the hash
|
|
83
|
+
// for forward-compat with Phase 5 rehydration; no markers change yet.
|
|
84
|
+
if (brCache && typeof brCache.put === 'function' && target.text.length >= brCache.minSize) {
|
|
85
|
+
const stored = brCache.put(target.text)
|
|
86
|
+
if (stored) {
|
|
87
|
+
entry.brHash = stored.hash
|
|
88
|
+
bucket._recordOffload(stored.brotliBytes)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
bucket.refs.set(hash, entry)
|
|
59
92
|
}
|
|
60
93
|
}
|
package/stats.js
CHANGED
|
@@ -9,7 +9,7 @@ const c = {
|
|
|
9
9
|
red: '\x1b[31m',
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost) {
|
|
12
|
+
export function formatRequestLog(stats, session, providerName, url, bodySize, tokenCost, sessionBucket) {
|
|
13
13
|
const compressed = stats.filter(s => s.method)
|
|
14
14
|
const label = providerName || 'anthropic'
|
|
15
15
|
const path = url || '/v1/messages'
|
|
@@ -58,6 +58,17 @@ export function formatRequestLog(stats, session, providerName, url, bodySize, to
|
|
|
58
58
|
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}`)
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// br-cache counters — only render when the bucket reports activity, so
|
|
62
|
+
// turning the stage off produces no extra rows.
|
|
63
|
+
if (sessionBucket && typeof sessionBucket.brStats === 'function') {
|
|
64
|
+
const br = sessionBucket.brStats()
|
|
65
|
+
if (br && (br.offloaded > 0 || br.hits > 0)) {
|
|
66
|
+
const totalLookups = br.offloaded + br.hits
|
|
67
|
+
const ratio = totalLookups > 0 ? ((br.hits / totalLookups) * 100).toFixed(1) : '0.0'
|
|
68
|
+
lines.push(`[tamp] ${c.cyan}br-cache${c.reset} ${fmtSize(br.brotliBytes)} stored, ${br.offloaded} offloaded, ${br.hits} hits ${c.dim}(${ratio}% hit ratio)${c.reset}`)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
61
72
|
return lines.join('\n')
|
|
62
73
|
}
|
|
63
74
|
|