@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/compress.js
CHANGED
|
@@ -4,9 +4,13 @@ import { encode } from '@toon-format/toon'
|
|
|
4
4
|
import { countTokens } from '@anthropic-ai/tokenizer'
|
|
5
5
|
import { createPatch } from 'diff'
|
|
6
6
|
import { tryParseJSON, classifyContent, stripLineNumbers } from './detect.js'
|
|
7
|
+
import { rewriteCommandOutput } from './lib/rewriters/index.js'
|
|
7
8
|
import { anthropic } from './providers.js'
|
|
8
9
|
import { graphDeduplicateTargets } from './session-graph.js'
|
|
9
|
-
import { detectTaskType, generateOutputRules } from './lib/rules-generator.js'
|
|
10
|
+
import { detectTaskType, generateOutputRules, isDangerousTask } from './lib/rules-generator.js'
|
|
11
|
+
import { extractTargetPaths } from './lib/path-extract.js'
|
|
12
|
+
import { summarize, rehydrateReferences } from './lib/disclosure.js'
|
|
13
|
+
import { trimLinesByRelevance } from './lib/bm25.js'
|
|
10
14
|
|
|
11
15
|
const cache = new Map()
|
|
12
16
|
const MAX_CACHE = 500
|
|
@@ -77,6 +81,101 @@ function quickSimilarity(a, b) {
|
|
|
77
81
|
return union > 0 ? intersection / union : 0
|
|
78
82
|
}
|
|
79
83
|
|
|
84
|
+
// --- Stage: read-diff ---
|
|
85
|
+
// Session-scoped unified diff for re-reads. For each target whose file path
|
|
86
|
+
// can be confidently attributed (via the preceding tool_use), compare against
|
|
87
|
+
// the last-seen body for the same (session, path) pair. If the patch is small
|
|
88
|
+
// enough, substitute it for the body. Always cache the fresh text afterwards.
|
|
89
|
+
function readDiffTargets(targets, body, sessionKey, readCache) {
|
|
90
|
+
if (!readCache || !sessionKey) return
|
|
91
|
+
const paths = extractTargetPaths(body, targets)
|
|
92
|
+
for (let i = 0; i < targets.length; i++) {
|
|
93
|
+
const target = targets[i]
|
|
94
|
+
const path = paths[i]
|
|
95
|
+
if (!path) continue
|
|
96
|
+
if (target.skip || target.dedup || target.diffed || target.compressed) {
|
|
97
|
+
// Even for already-compressed targets, refresh the cache with the
|
|
98
|
+
// original text so the next request can diff against the latest copy.
|
|
99
|
+
if (typeof target.text === 'string') readCache.put(sessionKey, path, target.text)
|
|
100
|
+
continue
|
|
101
|
+
}
|
|
102
|
+
if (typeof target.text !== 'string') continue
|
|
103
|
+
|
|
104
|
+
const prior = readCache.get(sessionKey, path)
|
|
105
|
+
if (prior && prior !== target.text) {
|
|
106
|
+
const patch = createPatch(path, prior, target.text, '', '')
|
|
107
|
+
if (patch.length < target.text.length * 0.5) {
|
|
108
|
+
target.compressed = `[read-diff from prior read of ${path}]:\n${patch}`
|
|
109
|
+
target.readDiffed = true
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
readCache.put(sessionKey, path, target.text)
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// --- Stage: disclosure (Phase 5) ---
|
|
117
|
+
// For tool_result bodies >= 32 KB that haven't been handled by earlier stages,
|
|
118
|
+
// store the full body in br-cache and replace it with a 3-tier summary block.
|
|
119
|
+
// Bypassed when the latest user message classifies as a dangerous task.
|
|
120
|
+
function discloseTargets(targets, body, provider, brCache) {
|
|
121
|
+
if (!brCache || typeof brCache.put !== 'function') return 0
|
|
122
|
+
if (!provider || typeof provider.getLastUserText !== 'function') return 0
|
|
123
|
+
const userText = provider.getLastUserText(body) || ''
|
|
124
|
+
if (isDangerousTask(userText)) return 0
|
|
125
|
+
|
|
126
|
+
let disclosed = 0
|
|
127
|
+
for (const target of targets) {
|
|
128
|
+
if (target.skip || target.dedup || target.diffed || target.readDiffed || target.graphed || target.compressed) continue
|
|
129
|
+
if (typeof target.text !== 'string') continue
|
|
130
|
+
const byteLen = Buffer.byteLength(target.text, 'utf8')
|
|
131
|
+
if (byteLen < 32 * 1024) continue
|
|
132
|
+
|
|
133
|
+
const put = brCache.put(target.text)
|
|
134
|
+
if (!put) continue
|
|
135
|
+
const result = summarize(target.text, { hash: put.hash })
|
|
136
|
+
if (!result) continue
|
|
137
|
+
target.compressed = result.summary
|
|
138
|
+
target.disclosed = true
|
|
139
|
+
target.disclosedHash = result.hash
|
|
140
|
+
target.disclosedOriginalBytes = result.originalBytes
|
|
141
|
+
disclosed += 1
|
|
142
|
+
}
|
|
143
|
+
return disclosed
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// --- Stage: bm25-trim (Phase 6) ---
|
|
147
|
+
// For tool_result bodies > 64 KB that survived earlier stages (including
|
|
148
|
+
// disclosure), rank lines by BM25 against the latest user message and drop
|
|
149
|
+
// low-relevance lines past a token budget. Bypassed when the latest user
|
|
150
|
+
// message classifies as a dangerous task — full fidelity is non-negotiable
|
|
151
|
+
// for security/debug/refactor/test flows.
|
|
152
|
+
function bm25TrimTargets(targets, body, provider) {
|
|
153
|
+
if (!provider || typeof provider.getLastUserText !== 'function') return 0
|
|
154
|
+
const userText = provider.getLastUserText(body) || ''
|
|
155
|
+
if (!userText) return 0
|
|
156
|
+
if (isDangerousTask(userText)) return 0
|
|
157
|
+
|
|
158
|
+
let trimmed = 0
|
|
159
|
+
for (const target of targets) {
|
|
160
|
+
if (target.skip || target.dedup || target.diffed || target.readDiffed || target.graphed || target.disclosed || target.compressed) continue
|
|
161
|
+
if (typeof target.text !== 'string') continue
|
|
162
|
+
const byteLen = Buffer.byteLength(target.text, 'utf8')
|
|
163
|
+
if (byteLen <= 64 * 1024) continue
|
|
164
|
+
|
|
165
|
+
const result = trimLinesByRelevance(target.text, userText)
|
|
166
|
+
if (!result) continue
|
|
167
|
+
if (result.text.length >= target.text.length) continue
|
|
168
|
+
target.compressed = result.text
|
|
169
|
+
target.bm25Trimmed = true
|
|
170
|
+
target.bm25KeptLines = result.keptLines
|
|
171
|
+
target.bm25DroppedLines = result.droppedLines
|
|
172
|
+
target.bm25OriginalTokens = result.originalTokens
|
|
173
|
+
target.bm25TrimmedTokens = result.trimmedTokens
|
|
174
|
+
trimmed += 1
|
|
175
|
+
}
|
|
176
|
+
return trimmed
|
|
177
|
+
}
|
|
178
|
+
|
|
80
179
|
// --- Stage: prune ---
|
|
81
180
|
const PRUNE_KEYS = new Set(['integrity', 'shasum', '_id', '_from', '_resolved', '_integrity', '_nodeVersion', '_npmVersion', '_phantomChildren', '_requiredBy'])
|
|
82
181
|
|
|
@@ -218,9 +317,19 @@ export function compressText(text, config) {
|
|
|
218
317
|
|
|
219
318
|
if (cls === 'text') {
|
|
220
319
|
let processed = text
|
|
320
|
+
let cmdStripRewriter = null
|
|
321
|
+
let cmdStripLen = text.length
|
|
322
|
+
if (config.stages.includes('cmd-strip')) {
|
|
323
|
+
const out = rewriteCommandOutput(processed)
|
|
324
|
+
if (out.rewriter && out.text.length < processed.length) {
|
|
325
|
+
processed = out.text
|
|
326
|
+
cmdStripRewriter = out.rewriter
|
|
327
|
+
cmdStripLen = processed.length
|
|
328
|
+
}
|
|
329
|
+
}
|
|
221
330
|
if (config.stages.includes('strip-lines')) {
|
|
222
|
-
const stripped = stripLineNumbers(
|
|
223
|
-
if (stripped !==
|
|
331
|
+
const stripped = stripLineNumbers(processed)
|
|
332
|
+
if (stripped !== processed) processed = stripped
|
|
224
333
|
}
|
|
225
334
|
if (config.stages.includes('whitespace')) {
|
|
226
335
|
processed = normalizeWhitespace(processed)
|
|
@@ -229,7 +338,12 @@ export function compressText(text, config) {
|
|
|
229
338
|
processed = stripComments(processed)
|
|
230
339
|
}
|
|
231
340
|
if (processed.length < text.length * 0.9) {
|
|
232
|
-
|
|
341
|
+
// If cmd-strip produced the savings and downstream stages didn't improve it further,
|
|
342
|
+
// surface the rewriter identifier in the method name so stats are legible.
|
|
343
|
+
const method = (cmdStripRewriter && processed.length === cmdStripLen)
|
|
344
|
+
? `cmd-strip:${cmdStripRewriter}`
|
|
345
|
+
: 'normalize'
|
|
346
|
+
return { text: processed, method, originalLen: text.length, compressedLen: processed.length, originalTokens: countTokens(text), compressedTokens: countTokens(processed) }
|
|
233
347
|
}
|
|
234
348
|
if (config.stages.includes('llmlingua') && config.llmLinguaUrl) {
|
|
235
349
|
return { async: true, asyncMethod: 'llmlingua', text: processed, cls }
|
|
@@ -440,7 +554,7 @@ function maybeInjectOutputHint(body, config, provider) {
|
|
|
440
554
|
if (!userText) return null
|
|
441
555
|
|
|
442
556
|
const taskType = config.autoDetectTaskType === false ? 'complex' : detectTaskType(userText)
|
|
443
|
-
const rules = generateOutputRules(mode, taskType)
|
|
557
|
+
const rules = generateOutputRules(mode, taskType, config.agent)
|
|
444
558
|
if (!rules) return null
|
|
445
559
|
|
|
446
560
|
// Keep the injected hint compact and clearly marked so the model knows
|
|
@@ -451,10 +565,24 @@ function maybeInjectOutputHint(body, config, provider) {
|
|
|
451
565
|
}
|
|
452
566
|
|
|
453
567
|
export async function compressRequest(body, config, provider) {
|
|
568
|
+
// Phase 5: before any stage runs, rehydrate <tamp-ref:v1:...> markers the
|
|
569
|
+
// model quoted back from a prior turn. Must happen first so downstream
|
|
570
|
+
// stages see the expanded body if relevant.
|
|
571
|
+
let disclosureStats = null
|
|
572
|
+
if (config.stages?.includes('disclosure') && config.brCache) {
|
|
573
|
+
const rh = rehydrateReferences(body, provider, config.brCache)
|
|
574
|
+
if (rh.rehydrated || rh.missed) disclosureStats = { rehydrated: rh.rehydrated, missed: rh.missed, disclosed: 0 }
|
|
575
|
+
}
|
|
576
|
+
|
|
454
577
|
const outputHint = maybeInjectOutputHint(body, config, provider)
|
|
455
578
|
|
|
456
579
|
const targets = provider.extract(body, { ...config, cacheSafe: config.cacheSafe !== false })
|
|
457
580
|
|
|
581
|
+
// Read-diff: session-scoped unified diff for re-reads (lossless, opt-in)
|
|
582
|
+
if (config.stages.includes('read-diff') && config.readCache && config.sessionKey && provider.name === 'anthropic') {
|
|
583
|
+
readDiffTargets(targets, body, config.sessionKey, config.readCache)
|
|
584
|
+
}
|
|
585
|
+
|
|
458
586
|
// Dedup: replace identical blocks with reference markers
|
|
459
587
|
if (config.stages.includes('dedup')) deduplicateTargets(targets)
|
|
460
588
|
|
|
@@ -466,6 +594,20 @@ export async function compressRequest(body, config, provider) {
|
|
|
466
594
|
graphDeduplicateTargets(targets, config.sessionBucket)
|
|
467
595
|
}
|
|
468
596
|
|
|
597
|
+
// Disclosure: progressive 3-tier summary for huge tool_results (opt-in, lossy)
|
|
598
|
+
if (config.stages.includes('disclosure') && config.brCache && provider.name === 'anthropic') {
|
|
599
|
+
const n = discloseTargets(targets, body, provider, config.brCache)
|
|
600
|
+
if (n > 0) {
|
|
601
|
+
disclosureStats = disclosureStats || { rehydrated: 0, missed: 0, disclosed: 0 }
|
|
602
|
+
disclosureStats.disclosed += n
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// BM25-trim: query-aware line drop for huge tool_results (opt-in, lossy)
|
|
607
|
+
if (config.stages.includes('bm25-trim')) {
|
|
608
|
+
bm25TrimTargets(targets, body, provider)
|
|
609
|
+
}
|
|
610
|
+
|
|
469
611
|
const stats = []
|
|
470
612
|
for (const target of targets) {
|
|
471
613
|
if (target.skip) { stats.push({ index: target.index, skipped: target.skip }); continue }
|
|
@@ -479,10 +621,22 @@ export async function compressRequest(body, config, provider) {
|
|
|
479
621
|
stats.push({ index: target.index, method: 'diff', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
|
|
480
622
|
continue
|
|
481
623
|
}
|
|
624
|
+
if (target.readDiffed) {
|
|
625
|
+
stats.push({ index: target.index, method: 'read-diff', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
|
|
626
|
+
continue
|
|
627
|
+
}
|
|
482
628
|
if (target.graphed) {
|
|
483
629
|
stats.push({ index: target.index, method: 'graph', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
|
|
484
630
|
continue
|
|
485
631
|
}
|
|
632
|
+
if (target.disclosed) {
|
|
633
|
+
stats.push({ index: target.index, method: 'disclosure', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
|
|
634
|
+
continue
|
|
635
|
+
}
|
|
636
|
+
if (target.bm25Trimmed) {
|
|
637
|
+
stats.push({ index: target.index, method: 'bm25-trim', originalLen: target.text.length, compressedLen: target.compressed.length, originalTokens: countTokens(target.text), compressedTokens: countTokens(target.compressed) })
|
|
638
|
+
continue
|
|
639
|
+
}
|
|
486
640
|
|
|
487
641
|
const result = await compressBlock(target.text, config)
|
|
488
642
|
if (result) {
|
|
@@ -493,7 +647,7 @@ export async function compressRequest(body, config, provider) {
|
|
|
493
647
|
}
|
|
494
648
|
}
|
|
495
649
|
provider.apply(body, targets)
|
|
496
|
-
return { body, stats, targetCount: targets.length, outputHint }
|
|
650
|
+
return { body, stats, targetCount: targets.length, outputHint, disclosure: disclosureStats }
|
|
497
651
|
}
|
|
498
652
|
|
|
499
653
|
export async function compressMessages(body, config) {
|
package/config.js
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { readFileSync } from 'node:fs'
|
|
2
2
|
import { homedir } from 'node:os'
|
|
3
3
|
import { join } from 'node:path'
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
DEFAULT_STAGES,
|
|
6
|
+
VERSION,
|
|
7
|
+
COMPRESSION_PRESETS,
|
|
8
|
+
COMPRESSION_LEVELS,
|
|
9
|
+
DEFAULT_LEVEL,
|
|
10
|
+
LEVEL_ALIASES,
|
|
11
|
+
resolveLevel,
|
|
12
|
+
} from './metadata.js'
|
|
13
|
+
|
|
14
|
+
const VALID_OUTPUT_MODES = new Set(['off', 'conservative', 'balanced', 'aggressive'])
|
|
5
15
|
|
|
6
16
|
function parseBoolean(value, defaultValue) {
|
|
7
17
|
if (value === undefined) return defaultValue
|
|
@@ -10,20 +20,47 @@ function parseBoolean(value, defaultValue) {
|
|
|
10
20
|
return defaultValue
|
|
11
21
|
}
|
|
12
22
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
23
|
+
// Coerce numeric strings to integers; leave alias strings untouched.
|
|
24
|
+
// Returns { value, valid } — value is the coerced input for resolveLevel(),
|
|
25
|
+
// valid is whether the input is recognisable as a level at all.
|
|
26
|
+
function coerceLevelInput(raw) {
|
|
27
|
+
if (raw === undefined || raw === null || raw === '') return { value: null, valid: false }
|
|
28
|
+
if (typeof raw === 'number') {
|
|
29
|
+
const v = resolveLevel(raw) ? raw : null
|
|
30
|
+
return { value: v, valid: v !== null }
|
|
17
31
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
32
|
+
const s = String(raw).trim()
|
|
33
|
+
if (s === '') return { value: null, valid: false }
|
|
34
|
+
if (/^-?\d+$/.test(s)) {
|
|
35
|
+
const n = parseInt(s, 10)
|
|
36
|
+
const v = resolveLevel(n) ? n : null
|
|
37
|
+
return { value: v, valid: v !== null }
|
|
23
38
|
}
|
|
39
|
+
if (s in LEVEL_ALIASES) return { value: s, valid: true }
|
|
40
|
+
return { value: null, valid: false }
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Convert a level input (number or alias) to its canonical integer.
|
|
44
|
+
function levelInputToInt(input) {
|
|
45
|
+
if (typeof input === 'number') return input
|
|
46
|
+
if (typeof input === 'string' && input in LEVEL_ALIASES) return LEVEL_ALIASES[input]
|
|
47
|
+
return null
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Preset levels are the "anchor" rungs of the ladder — when a resolved level
|
|
51
|
+
// matches one of these, prefer the preset's stage ordering. This preserves
|
|
52
|
+
// the L4 === conservative / L5 === balanced / L8 === aggressive identity
|
|
53
|
+
// promise and keeps pre-Phase-C stage ordering stable for downstream code.
|
|
54
|
+
const LEVEL_TO_PRESET = Object.freeze({
|
|
55
|
+
4: 'conservative',
|
|
56
|
+
5: 'balanced',
|
|
57
|
+
8: 'aggressive',
|
|
58
|
+
})
|
|
24
59
|
|
|
25
|
-
|
|
26
|
-
|
|
60
|
+
function stagesForLevel(level) {
|
|
61
|
+
const presetName = LEVEL_TO_PRESET[level]
|
|
62
|
+
if (presetName) return [...COMPRESSION_PRESETS[presetName].stages]
|
|
63
|
+
return [...COMPRESSION_LEVELS[level].stages]
|
|
27
64
|
}
|
|
28
65
|
|
|
29
66
|
export const CONFIG_PATH = join(homedir(), '.config', 'tamp', 'config')
|
|
@@ -49,19 +86,106 @@ export function loadConfigFile(path) {
|
|
|
49
86
|
} catch { return {} }
|
|
50
87
|
}
|
|
51
88
|
|
|
52
|
-
export function loadConfig(env = process.env) {
|
|
89
|
+
export function loadConfig(env = process.env, options = {}) {
|
|
53
90
|
const fileVars = (env === process.env) ? loadConfigFile() : {}
|
|
54
91
|
const get = (key) => env[key] !== undefined ? env[key] : fileVars[key]
|
|
55
92
|
|
|
56
|
-
//
|
|
93
|
+
// --- Stage/level resolution with explicit precedence ---
|
|
94
|
+
// 1. TAMP_STAGES (explicit stage list) — power-user override
|
|
95
|
+
// 2. --level CLI flag (options.levelOverride)
|
|
96
|
+
// 3. TAMP_LEVEL env
|
|
97
|
+
// 4. TAMP_COMPRESSION_PRESET env
|
|
98
|
+
// 5. config-file `level` field
|
|
99
|
+
// 6. DEFAULT_LEVEL (5)
|
|
100
|
+
|
|
57
101
|
const explicitStagesStr = get('TAMP_STAGES')
|
|
58
|
-
const explicitStages = explicitStagesStr
|
|
102
|
+
const explicitStages = explicitStagesStr
|
|
103
|
+
? explicitStagesStr.split(',').map(s => s.trim()).filter(Boolean)
|
|
104
|
+
: []
|
|
105
|
+
|
|
106
|
+
const presetNameRaw = get('TAMP_COMPRESSION_PRESET')
|
|
107
|
+
const presetExplicit = typeof presetNameRaw === 'string' && presetNameRaw.length > 0
|
|
108
|
+
|
|
109
|
+
const fileLevelRaw = fileVars.level
|
|
110
|
+
const levelFlagRaw = options.levelOverride
|
|
111
|
+
const levelEnvRaw = get('TAMP_LEVEL')
|
|
59
112
|
|
|
60
|
-
|
|
61
|
-
|
|
113
|
+
let stages = null
|
|
114
|
+
let level = null
|
|
115
|
+
let levelSource = null
|
|
116
|
+
let presetName = presetExplicit ? presetNameRaw : 'balanced'
|
|
62
117
|
|
|
63
|
-
|
|
64
|
-
|
|
118
|
+
if (explicitStages.length > 0) {
|
|
119
|
+
stages = explicitStages
|
|
120
|
+
level = null
|
|
121
|
+
levelSource = 'stages-env'
|
|
122
|
+
} else {
|
|
123
|
+
// Try level inputs in precedence order: CLI flag > env
|
|
124
|
+
const candidates = [
|
|
125
|
+
{ raw: levelFlagRaw, source: 'level-flag' },
|
|
126
|
+
{ raw: levelEnvRaw, source: 'level-env' },
|
|
127
|
+
]
|
|
128
|
+
let chosen = null
|
|
129
|
+
for (const c of candidates) {
|
|
130
|
+
if (c.raw === undefined || c.raw === null || c.raw === '') continue
|
|
131
|
+
const { value, valid } = coerceLevelInput(c.raw)
|
|
132
|
+
if (valid) {
|
|
133
|
+
chosen = { value, source: c.source }
|
|
134
|
+
break
|
|
135
|
+
} else {
|
|
136
|
+
const label = c.source === 'level-flag' ? '--level' : 'TAMP_LEVEL'
|
|
137
|
+
process.stderr.write(
|
|
138
|
+
`[tamp] invalid ${label}=${JSON.stringify(c.raw)} — expected 1..9 or one of: ${Object.keys(LEVEL_ALIASES).join(', ')}. Ignoring.\n`
|
|
139
|
+
)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (chosen) {
|
|
144
|
+
level = levelInputToInt(chosen.value)
|
|
145
|
+
stages = stagesForLevel(level)
|
|
146
|
+
levelSource = chosen.source
|
|
147
|
+
} else if (presetExplicit) {
|
|
148
|
+
const preset = COMPRESSION_PRESETS[presetNameRaw]
|
|
149
|
+
if (preset) {
|
|
150
|
+
stages = [...preset.stages]
|
|
151
|
+
level = typeof preset.level === 'number' ? preset.level : null
|
|
152
|
+
levelSource = 'preset-env'
|
|
153
|
+
} else {
|
|
154
|
+
// Unknown preset name — fall through to default balanced
|
|
155
|
+
stages = [...COMPRESSION_PRESETS.balanced.stages]
|
|
156
|
+
level = COMPRESSION_PRESETS.balanced.level
|
|
157
|
+
levelSource = 'default'
|
|
158
|
+
presetName = 'balanced'
|
|
159
|
+
}
|
|
160
|
+
} else if (fileLevelRaw !== undefined && fileLevelRaw !== '') {
|
|
161
|
+
const { value, valid } = coerceLevelInput(fileLevelRaw)
|
|
162
|
+
if (valid) {
|
|
163
|
+
level = levelInputToInt(value)
|
|
164
|
+
stages = stagesForLevel(level)
|
|
165
|
+
levelSource = 'config-file'
|
|
166
|
+
} else {
|
|
167
|
+
process.stderr.write(
|
|
168
|
+
`[tamp] invalid level=${JSON.stringify(fileLevelRaw)} in config file — expected 1..9 or one of: ${Object.keys(LEVEL_ALIASES).join(', ')}. Ignoring.\n`
|
|
169
|
+
)
|
|
170
|
+
level = DEFAULT_LEVEL
|
|
171
|
+
stages = stagesForLevel(DEFAULT_LEVEL)
|
|
172
|
+
levelSource = 'default'
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
level = DEFAULT_LEVEL
|
|
176
|
+
stages = stagesForLevel(DEFAULT_LEVEL)
|
|
177
|
+
levelSource = 'default'
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Output mode precedence (highest wins):
|
|
182
|
+
// 1. TAMP_OUTPUT_MODE — explicit per-session override
|
|
183
|
+
// 2. TAMP_OUTPUT_DEFAULT — env-level default seed (v1.5.0 parity)
|
|
184
|
+
// 3. 'off' — Caveman disabled unless opted in
|
|
185
|
+
const explicitOutputMode = get('TAMP_OUTPUT_MODE')
|
|
186
|
+
const defaultOutputMode = get('TAMP_OUTPUT_DEFAULT')
|
|
187
|
+
let outputMode = explicitOutputMode || defaultOutputMode || 'off'
|
|
188
|
+
if (!VALID_OUTPUT_MODES.has(outputMode)) outputMode = 'off'
|
|
65
189
|
|
|
66
190
|
return Object.freeze({
|
|
67
191
|
version: VERSION,
|
|
@@ -72,11 +196,26 @@ export function loadConfig(env = process.env) {
|
|
|
72
196
|
openai: get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
|
|
73
197
|
'openai-responses': get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
|
|
74
198
|
gemini: get('TAMP_UPSTREAM_GEMINI') || 'https://generativelanguage.googleapis.com',
|
|
199
|
+
// Kimi Code (subscription CLI) — proprietary path /coding/v1/*
|
|
200
|
+
kimi: get('TAMP_UPSTREAM_KIMI') || 'https://api.kimi.com',
|
|
201
|
+
// Moonshot public OpenAI-compat API — strictly /v1/*
|
|
202
|
+
moonshot: get('TAMP_UPSTREAM_MOONSHOT') || 'https://api.moonshot.cn',
|
|
75
203
|
}),
|
|
204
|
+
// Codex CLI ChatGPT Plus routes to https://chatgpt.com/backend-api/codex
|
|
205
|
+
// by default (not api.openai.com). Set TAMP_DISABLE_CHATGPT_ROUTE=1 to
|
|
206
|
+
// force legacy api.openai.com routing even when a JWT bearer / account
|
|
207
|
+
// id is detected.
|
|
208
|
+
disableChatgptRoute: parseBoolean(get('TAMP_DISABLE_CHATGPT_ROUTE'), false) ||
|
|
209
|
+
get('TAMP_DISABLE_CHATGPT_ROUTE') === '1',
|
|
210
|
+
chatgptUpstream: get('TAMP_UPSTREAM_CHATGPT') || 'https://chatgpt.com',
|
|
76
211
|
minSize: parseInt(get('TAMP_MIN_SIZE'), 10) || 200,
|
|
77
212
|
stages,
|
|
78
213
|
preset: presetName,
|
|
79
|
-
|
|
214
|
+
level,
|
|
215
|
+
levelSource,
|
|
216
|
+
outputMode,
|
|
217
|
+
outputModeDefault: defaultOutputMode && VALID_OUTPUT_MODES.has(defaultOutputMode) ? defaultOutputMode : null,
|
|
218
|
+
agent: get('TAMP_AGENT') || null,
|
|
80
219
|
autoDetectTaskType: parseBoolean(get('TAMP_AUTO_DETECT_TASK_TYPE'), true),
|
|
81
220
|
log: get('TAMP_LOG') !== 'false',
|
|
82
221
|
logFile: get('TAMP_LOG_FILE') || null,
|
|
@@ -102,6 +241,15 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
|
|
|
102
241
|
# TAMP_UPSTREAM=https://api.anthropic.com
|
|
103
242
|
# TAMP_UPSTREAM_OPENAI=https://api.openai.com
|
|
104
243
|
# TAMP_UPSTREAM_GEMINI=https://generativelanguage.googleapis.com
|
|
244
|
+
# TAMP_UPSTREAM_KIMI=https://api.kimi.com
|
|
245
|
+
# TAMP_UPSTREAM_MOONSHOT=https://api.moonshot.cn
|
|
246
|
+
|
|
247
|
+
# Codex CLI ChatGPT Plus routing (OAuth/JWT bearers land on chatgpt.com)
|
|
248
|
+
# Set to 1 to force legacy api.openai.com routing even when JWT is detected
|
|
249
|
+
# TAMP_DISABLE_CHATGPT_ROUTE=0
|
|
250
|
+
|
|
251
|
+
# Compression level 1..9 (or: conservative, balanced, aggressive, max)
|
|
252
|
+
# level=5
|
|
105
253
|
|
|
106
254
|
# Compression preset (conservative | balanced | aggressive)
|
|
107
255
|
# TAMP_COMPRESSION_PRESET=balanced
|
|
@@ -109,12 +257,20 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
|
|
|
109
257
|
# Explicit stages (overrides preset if set)
|
|
110
258
|
# TAMP_STAGES=minify,toon,strip-lines,whitespace,llmlingua,dedup,diff,prune
|
|
111
259
|
|
|
112
|
-
# Output compression mode (conservative | balanced | aggressive)
|
|
260
|
+
# Output compression mode (conservative | balanced | aggressive | off)
|
|
113
261
|
# TAMP_OUTPUT_MODE=balanced
|
|
114
262
|
|
|
263
|
+
# Env-level default for output mode (used when TAMP_OUTPUT_MODE is unset).
|
|
264
|
+
# Valid: off | conservative | balanced | aggressive
|
|
265
|
+
# TAMP_OUTPUT_DEFAULT=off
|
|
266
|
+
|
|
115
267
|
# Auto-detect task type for output compression (true | false)
|
|
116
268
|
# TAMP_AUTO_DETECT_TASK_TYPE=true
|
|
117
269
|
|
|
270
|
+
# Agent identifier for per-agent output rule overrides
|
|
271
|
+
# (e.g. codex, cursor, cline, aider, claude-code)
|
|
272
|
+
# TAMP_AGENT=
|
|
273
|
+
|
|
118
274
|
# TAMP_MIN_SIZE=200
|
|
119
275
|
# TAMP_LOG=true
|
|
120
276
|
# TAMP_LOG_FILE=
|
package/detect.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { rewriteCommandOutput } from './lib/rewriters/index.js'
|
|
2
|
+
|
|
1
3
|
const LINE_NUM_RE = /^ *\d+[\t→]/
|
|
2
4
|
|
|
3
5
|
export function stripLineNumbers(str) {
|
|
@@ -41,3 +43,11 @@ export function classifyContent(str) {
|
|
|
41
43
|
if (str.length > 0) return 'text'
|
|
42
44
|
return 'unknown'
|
|
43
45
|
}
|
|
46
|
+
|
|
47
|
+
// Thin wrapper around the rewriters module. Returns the rewriter name
|
|
48
|
+
// that would fire for this text, or null if no rewriter matches.
|
|
49
|
+
// Side-effect-free: discards the rewritten text and savedBytes.
|
|
50
|
+
export function detectCommandOutput(text) {
|
|
51
|
+
const out = rewriteCommandOutput(text)
|
|
52
|
+
return out.rewriter || null
|
|
53
|
+
}
|