@sliday/tamp 0.5.7 → 0.5.9
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 +12 -7
- package/bin/tamp.js +64 -4
- package/bin/ui/Banner.js +27 -7
- package/bin/ui/Dashboard.js +1 -0
- package/compress.js +27 -1
- package/config.js +1 -1
- package/metadata.js +26 -0
- package/package.json +1 -1
- package/providers.js +132 -0
package/README.md
CHANGED
|
@@ -152,19 +152,24 @@ TAMP_COMPRESSION_PRESET=balanced
|
|
|
152
152
|
TAMP_STAGES=minify,toon # Override preset
|
|
153
153
|
```
|
|
154
154
|
|
|
155
|
-
### Output Compression
|
|
155
|
+
### Output Compression — Caveman Mode
|
|
156
156
|
|
|
157
|
-
Task-type-aware output compression
|
|
157
|
+
Task-type-aware output compression. Tamp classifies each request as `safe` (typo fixes, env var changes, doc updates) or `dangerous` (security, debug, refactor) and injects matching rules into the last user message before forwarding. Cache-safe — the prefix stays untouched so prompt caching keeps working.
|
|
158
|
+
|
|
159
|
+
**Opt in** (default is `off` — zero behavior change unless you flip the switch):
|
|
158
160
|
|
|
159
161
|
```bash
|
|
160
|
-
export TAMP_OUTPUT_MODE=balanced # conservative | balanced | aggressive
|
|
161
|
-
export TAMP_AUTO_DETECT_TASK_TYPE=true #
|
|
162
|
+
export TAMP_OUTPUT_MODE=balanced # off | conservative | balanced | aggressive
|
|
163
|
+
export TAMP_AUTO_DETECT_TASK_TYPE=true # default; set to false to force 'complex'
|
|
162
164
|
```
|
|
163
165
|
|
|
164
166
|
**Mode behavior:**
|
|
165
|
-
- **
|
|
166
|
-
- **
|
|
167
|
-
- **
|
|
167
|
+
- **off** *(default)*: No injection. Pass-through.
|
|
168
|
+
- **conservative**: Professional but concise for all tasks (40-50% output savings).
|
|
169
|
+
- **balanced**: Terse on safe tasks, full output on dangerous (65-75% on safe).
|
|
170
|
+
- **aggressive**: Minimal caveman-style (75-85% on safe, partial on dangerous).
|
|
171
|
+
|
|
172
|
+
Supported on all providers: Anthropic, OpenAI Chat, OpenAI Responses (Codex), Gemini.
|
|
168
173
|
|
|
169
174
|
### Other Settings
|
|
170
175
|
|
package/bin/tamp.js
CHANGED
|
@@ -5,7 +5,7 @@ import { spawn, execFileSync } from 'node:child_process'
|
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
6
|
import { dirname, join } from 'node:path'
|
|
7
7
|
import { homedir } from 'node:os'
|
|
8
|
-
import { DEFAULT_STAGES, EXTRA_STAGES, STAGE_DESCRIPTIONS, VERSION, isLossy } from '../metadata.js'
|
|
8
|
+
import { DEFAULT_STAGES, EXTRA_STAGES, STAGE_DESCRIPTIONS, STAGE_HINTS, VERSION, isLossy } from '../metadata.js'
|
|
9
9
|
import { CONFIG_PATH, CONFIG_TEMPLATE } from '../config.js'
|
|
10
10
|
import {
|
|
11
11
|
checkPort,
|
|
@@ -162,6 +162,47 @@ if (subcommand === 'status') {
|
|
|
162
162
|
process.exit(healthy ? 0 : 1)
|
|
163
163
|
}
|
|
164
164
|
|
|
165
|
+
if (subcommand === 'compress-config') {
|
|
166
|
+
const { compressFiles, formatResult } = await import('../lib/compress-file.js')
|
|
167
|
+
const args = process.argv.slice(3)
|
|
168
|
+
const flags = new Set(args.filter(a => a.startsWith('--')))
|
|
169
|
+
const files = args.filter(a => !a.startsWith('--'))
|
|
170
|
+
|
|
171
|
+
if (!files.length) {
|
|
172
|
+
console.error('Usage: tamp compress-config [--dry-run] [--no-backup] <file> [file...]')
|
|
173
|
+
console.error('')
|
|
174
|
+
console.error('Compress CLAUDE.md, config, or markdown files using lossy strategies')
|
|
175
|
+
console.error('inspired by JuliusBrussee/caveman-compress.')
|
|
176
|
+
console.error('')
|
|
177
|
+
console.error('Examples:')
|
|
178
|
+
console.error(' tamp compress-config --dry-run ~/.claude/CLAUDE.md')
|
|
179
|
+
console.error(' tamp compress-config ~/.claude/CLAUDE.md ~/.config/tamp/config')
|
|
180
|
+
process.exit(1)
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const opts = {
|
|
184
|
+
dryRun: flags.has('--dry-run'),
|
|
185
|
+
noBackup: flags.has('--no-backup'),
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const results = await compressFiles(files, opts)
|
|
189
|
+
let failures = 0
|
|
190
|
+
for (const result of results) {
|
|
191
|
+
console.log(formatResult(result))
|
|
192
|
+
console.log('')
|
|
193
|
+
if (!result.success && !result.skipped && !result.dryRun) failures++
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const totalOrig = results.reduce((a, r) => a + (r.originalLen || 0), 0)
|
|
197
|
+
const totalComp = results.reduce((a, r) => a + (r.compressedLen || r.originalLen || 0), 0)
|
|
198
|
+
if (totalOrig > 0) {
|
|
199
|
+
const pct = ((totalOrig - totalComp) / totalOrig * 100).toFixed(1)
|
|
200
|
+
console.log(`Total: ${totalOrig} → ${totalComp} chars (${pct}% saved across ${results.length} file${results.length !== 1 ? 's' : ''})`)
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
process.exit(failures > 0 ? 1 : 0)
|
|
204
|
+
}
|
|
205
|
+
|
|
165
206
|
if (subcommand === 'stop') {
|
|
166
207
|
const port = Number(process.env.TAMP_PORT) || 7778
|
|
167
208
|
await reconcileStalePidFile(port)
|
|
@@ -212,6 +253,7 @@ if (subcommand === 'help' || subcommand === '--help' || subcommand === '-h') {
|
|
|
212
253
|
console.log(' -y Start proxy (non-interactive, use defaults)')
|
|
213
254
|
console.log(' -y --force Replace any existing Tamp on the same port')
|
|
214
255
|
console.log(' stop Stop a running Tamp on TAMP_PORT (default 7778)')
|
|
256
|
+
console.log(' compress-config Compress CLAUDE.md or config files (caveman mode)')
|
|
215
257
|
console.log(' init Create config file (~/.config/tamp/config)')
|
|
216
258
|
console.log(' status Check if proxy is running')
|
|
217
259
|
console.log(' install-service Install systemd user service (Linux)')
|
|
@@ -295,7 +337,12 @@ if (skipPrompt) {
|
|
|
295
337
|
console.error(` ${c.dim}Claude Code:${c.reset} ANTHROPIC_BASE_URL=${c.yellow}${url}${c.reset}`)
|
|
296
338
|
console.error(` ${c.dim}Aider/Cursor:${c.reset} OPENAI_API_BASE=${c.yellow}${url}${c.reset}`)
|
|
297
339
|
console.error('')
|
|
298
|
-
|
|
340
|
+
const total = DEFAULT_STAGES.length + EXTRA_STAGES.length
|
|
341
|
+
if (config.outputMode && config.outputMode !== 'off') {
|
|
342
|
+
console.error(` ${c.bold}Output mode${c.reset} ${c.yellow}${config.outputMode}${c.reset} ${c.dim}(caveman, task-aware)${c.reset}`)
|
|
343
|
+
console.error('')
|
|
344
|
+
}
|
|
345
|
+
console.error(` ${c.bold}Stages${c.reset} ${c.dim}(${active.length} of ${total} active)${c.reset}`)
|
|
299
346
|
for (const s of defaultActive) {
|
|
300
347
|
const extra = s === 'llmlingua' && config.llmLinguaUrl ? ` ${c.dim}(${config.llmLinguaUrl})${c.reset}` : ''
|
|
301
348
|
console.error(` ${c.green}\u2713${c.reset} ${c.cyan}${s}${c.reset}${extra}`)
|
|
@@ -304,9 +351,22 @@ if (skipPrompt) {
|
|
|
304
351
|
const tag = isLossy(s) ? '(lossy)' : '(opt-in)'
|
|
305
352
|
console.error(` ${c.yellow}\u2713${c.reset} ${c.yellow}${s}${c.reset} ${c.dim}${tag}${c.reset}`)
|
|
306
353
|
}
|
|
354
|
+
|
|
307
355
|
const disabled = [...DEFAULT_STAGES, ...EXTRA_STAGES].filter(s => !active.includes(s))
|
|
308
|
-
if (disabled.length
|
|
309
|
-
console.error(
|
|
356
|
+
if (disabled.length) {
|
|
357
|
+
console.error('')
|
|
358
|
+
console.error(` ${c.bold}Available${c.reset} ${c.dim}(not active — opt in for more savings)${c.reset}`)
|
|
359
|
+
for (const s of disabled) {
|
|
360
|
+
const hint = STAGE_HINTS[s]
|
|
361
|
+
if (hint) {
|
|
362
|
+
console.error(` ${c.dim}\u25EF${c.reset} ${c.cyan}${s}${c.reset} ${c.dim}— ${hint.summary}${c.reset}`)
|
|
363
|
+
if (hint.setup) {
|
|
364
|
+
console.error(` ${c.dim}\u2192 ${hint.setup}${c.reset}`)
|
|
365
|
+
}
|
|
366
|
+
} else {
|
|
367
|
+
console.error(` ${c.dim}\u25EF ${s}${c.reset}`)
|
|
368
|
+
}
|
|
369
|
+
}
|
|
310
370
|
}
|
|
311
371
|
console.error('')
|
|
312
372
|
}
|
package/bin/ui/Banner.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import { Box, Text } from 'ink'
|
|
3
3
|
import { Badge } from '@inkjs/ui'
|
|
4
|
-
import { ALL_STAGES, DEFAULT_STAGES, EXTRA_STAGES, isLossy } from '../../metadata.js'
|
|
4
|
+
import { ALL_STAGES, DEFAULT_STAGES, EXTRA_STAGES, STAGE_HINTS, isLossy } from '../../metadata.js'
|
|
5
5
|
|
|
6
6
|
const h = React.createElement
|
|
7
7
|
|
|
8
|
-
export function Banner({ version, port, stages, llmLinguaUrl }) {
|
|
8
|
+
export function Banner({ version, port, stages, llmLinguaUrl, outputMode }) {
|
|
9
9
|
const url = `http://localhost:${port}`
|
|
10
10
|
const active = new Set(stages)
|
|
11
11
|
const defaultActive = DEFAULT_STAGES.filter(s => active.has(s))
|
|
12
12
|
const extraActive = EXTRA_STAGES.filter(s => active.has(s))
|
|
13
13
|
const disabled = ALL_STAGES.filter(s => !active.has(s))
|
|
14
|
+
const showOutput = outputMode && outputMode !== 'off'
|
|
14
15
|
|
|
15
16
|
return h(Box, { flexDirection: 'column', paddingX: 1 },
|
|
16
17
|
h(Box, { gap: 2 },
|
|
@@ -34,7 +35,12 @@ export function Banner({ version, port, stages, llmLinguaUrl }) {
|
|
|
34
35
|
),
|
|
35
36
|
),
|
|
36
37
|
h(Text, null, ''),
|
|
37
|
-
h(Text, { bold: true },
|
|
38
|
+
showOutput ? h(Text, { bold: true }, 'Output mode ',
|
|
39
|
+
h(Text, { color: 'yellow' }, outputMode),
|
|
40
|
+
h(Text, { dimColor: true }, ' (caveman, task-aware)')
|
|
41
|
+
) : null,
|
|
42
|
+
showOutput ? h(Text, null, '') : null,
|
|
43
|
+
h(Text, { bold: true }, `Stages `, h(Text, { dimColor: true }, `(${stages.length} of ${ALL_STAGES.length} active)`)),
|
|
38
44
|
h(Box, { paddingLeft: 2, flexDirection: 'column' },
|
|
39
45
|
...defaultActive.map(s =>
|
|
40
46
|
h(Text, { key: s },
|
|
@@ -50,10 +56,24 @@ export function Banner({ version, port, stages, llmLinguaUrl }) {
|
|
|
50
56
|
h(Text, { dimColor: true }, isLossy(s) ? ' (lossy)' : ' (opt-in)'),
|
|
51
57
|
)
|
|
52
58
|
),
|
|
53
|
-
...(disabled.length && disabled.length <= 4
|
|
54
|
-
? [h(Text, { key: 'disabled', dimColor: true }, `\u2717 ${disabled.join(', ')}`)]
|
|
55
|
-
: []
|
|
56
|
-
),
|
|
57
59
|
),
|
|
60
|
+
disabled.length ? h(Text, null, '') : null,
|
|
61
|
+
disabled.length ? h(Text, { bold: true }, 'Available ', h(Text, { dimColor: true }, '(not active — opt in for more savings)')) : null,
|
|
62
|
+
disabled.length ? h(Box, { paddingLeft: 2, flexDirection: 'column' },
|
|
63
|
+
...disabled.flatMap(s => {
|
|
64
|
+
const hint = STAGE_HINTS[s]
|
|
65
|
+
const lines = [
|
|
66
|
+
h(Text, { key: s },
|
|
67
|
+
h(Text, { dimColor: true }, '\u25EF '),
|
|
68
|
+
h(Text, { color: 'cyan' }, s),
|
|
69
|
+
hint ? h(Text, { dimColor: true }, ` \u2014 ${hint.summary}`) : null,
|
|
70
|
+
)
|
|
71
|
+
]
|
|
72
|
+
if (hint?.setup) {
|
|
73
|
+
lines.push(h(Text, { key: s + '-setup', dimColor: true }, ` \u2192 ${hint.setup}`))
|
|
74
|
+
}
|
|
75
|
+
return lines
|
|
76
|
+
})
|
|
77
|
+
) : null,
|
|
58
78
|
)
|
|
59
79
|
}
|
package/bin/ui/Dashboard.js
CHANGED
|
@@ -92,6 +92,7 @@ export function Dashboard({ version, config, events, totals }) {
|
|
|
92
92
|
port: config.port,
|
|
93
93
|
stages: config.stages,
|
|
94
94
|
llmLinguaUrl: config.llmLinguaUrl,
|
|
95
|
+
outputMode: config.outputMode,
|
|
95
96
|
}),
|
|
96
97
|
h(StatsBar, { totals, tokenCost: config.tokenCost }),
|
|
97
98
|
events.length > 0
|
package/compress.js
CHANGED
|
@@ -6,6 +6,7 @@ import { createPatch } from 'diff'
|
|
|
6
6
|
import { tryParseJSON, classifyContent, stripLineNumbers } from './detect.js'
|
|
7
7
|
import { anthropic } from './providers.js'
|
|
8
8
|
import { graphDeduplicateTargets } from './session-graph.js'
|
|
9
|
+
import { detectTaskType, generateOutputRules } from './lib/rules-generator.js'
|
|
9
10
|
|
|
10
11
|
const cache = new Map()
|
|
11
12
|
const MAX_CACHE = 500
|
|
@@ -426,7 +427,32 @@ async function compressBlock(text, config) {
|
|
|
426
427
|
return result
|
|
427
428
|
}
|
|
428
429
|
|
|
430
|
+
// Caveman-style output compression: append task-type-aware rules to the
|
|
431
|
+
// LATEST user message. Cache-safe — only the freshest message is touched,
|
|
432
|
+
// so the prefix (system prompt + earlier turns) stays cacheable.
|
|
433
|
+
function maybeInjectOutputHint(body, config, provider) {
|
|
434
|
+
const mode = config.outputMode
|
|
435
|
+
if (!mode || mode === 'off') return null
|
|
436
|
+
if (typeof provider.injectOutputHint !== 'function') return null
|
|
437
|
+
if (typeof provider.getLastUserText !== 'function') return null
|
|
438
|
+
|
|
439
|
+
const userText = provider.getLastUserText(body)
|
|
440
|
+
if (!userText) return null
|
|
441
|
+
|
|
442
|
+
const taskType = config.autoDetectTaskType === false ? 'complex' : detectTaskType(userText)
|
|
443
|
+
const rules = generateOutputRules(mode, taskType)
|
|
444
|
+
if (!rules) return null
|
|
445
|
+
|
|
446
|
+
// Keep the injected hint compact and clearly marked so the model knows
|
|
447
|
+
// it's a system-style instruction and not part of the user's prose.
|
|
448
|
+
const wrapped = `[tamp output rules — ${mode} mode, ${taskType} task]\n${rules.trim()}`
|
|
449
|
+
const ok = provider.injectOutputHint(body, wrapped)
|
|
450
|
+
return ok ? { mode, taskType, bytes: wrapped.length } : null
|
|
451
|
+
}
|
|
452
|
+
|
|
429
453
|
export async function compressRequest(body, config, provider) {
|
|
454
|
+
const outputHint = maybeInjectOutputHint(body, config, provider)
|
|
455
|
+
|
|
430
456
|
const targets = provider.extract(body, { ...config, cacheSafe: config.cacheSafe !== false })
|
|
431
457
|
|
|
432
458
|
// Dedup: replace identical blocks with reference markers
|
|
@@ -467,7 +493,7 @@ export async function compressRequest(body, config, provider) {
|
|
|
467
493
|
}
|
|
468
494
|
}
|
|
469
495
|
provider.apply(body, targets)
|
|
470
|
-
return { body, stats, targetCount: targets.length }
|
|
496
|
+
return { body, stats, targetCount: targets.length, outputHint }
|
|
471
497
|
}
|
|
472
498
|
|
|
473
499
|
export async function compressMessages(body, config) {
|
package/config.js
CHANGED
|
@@ -76,7 +76,7 @@ export function loadConfig(env = process.env) {
|
|
|
76
76
|
minSize: parseInt(get('TAMP_MIN_SIZE'), 10) || 200,
|
|
77
77
|
stages,
|
|
78
78
|
preset: presetName,
|
|
79
|
-
outputMode: get('TAMP_OUTPUT_MODE') || '
|
|
79
|
+
outputMode: get('TAMP_OUTPUT_MODE') || 'off',
|
|
80
80
|
autoDetectTaskType: parseBoolean(get('TAMP_AUTO_DETECT_TASK_TYPE'), true),
|
|
81
81
|
log: get('TAMP_LOG') !== 'false',
|
|
82
82
|
logFile: get('TAMP_LOG_FILE') || null,
|
package/metadata.js
CHANGED
|
@@ -41,6 +41,32 @@ export function isLossy(stage) {
|
|
|
41
41
|
return LOSSY_STAGES.has(stage)
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
// Hints shown in the banner when a stage is NOT active. summary describes
|
|
45
|
+
// the value the user is missing; setup is a copy-pasteable enable command
|
|
46
|
+
// (or null if the stage just needs TAMP_STAGES=...,<name>).
|
|
47
|
+
export const STAGE_HINTS = Object.freeze({
|
|
48
|
+
llmlingua: {
|
|
49
|
+
summary: 'neural text compression, +7-12% savings',
|
|
50
|
+
setup: 'install uv: curl -LsSf https://astral.sh/uv/install.sh | sh',
|
|
51
|
+
},
|
|
52
|
+
graph: {
|
|
53
|
+
summary: 'cross-request dedup, lossless, -99% on re-reads',
|
|
54
|
+
setup: 'TAMP_STAGES=...,graph',
|
|
55
|
+
},
|
|
56
|
+
'strip-comments': {
|
|
57
|
+
summary: 'remove code comments (lossy)',
|
|
58
|
+
setup: null,
|
|
59
|
+
},
|
|
60
|
+
textpress: {
|
|
61
|
+
summary: 'LLM semantic compression via Ollama/OpenRouter (lossy)',
|
|
62
|
+
setup: null,
|
|
63
|
+
},
|
|
64
|
+
'foundation-models': {
|
|
65
|
+
summary: 'Apple Intelligence neural (macOS 15+, lossy)',
|
|
66
|
+
setup: null,
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
|
|
44
70
|
export const STAGE_DESCRIPTIONS = Object.freeze({
|
|
45
71
|
minify: 'Strip JSON whitespace (lossless)',
|
|
46
72
|
toon: 'Columnar array encoding (lossless)',
|
package/package.json
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"stats.js",
|
|
13
13
|
"lib/"
|
|
14
14
|
],
|
|
15
|
-
"version": "0.5.
|
|
15
|
+
"version": "0.5.9",
|
|
16
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.",
|
|
17
17
|
"type": "module",
|
|
18
18
|
"main": "index.js",
|
package/providers.js
CHANGED
|
@@ -50,6 +50,50 @@ function extractAnthropicMessageTargets(msg, mi) {
|
|
|
50
50
|
return targets
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
function getLastUserTextFromAnthropicMessages(messages) {
|
|
54
|
+
if (!Array.isArray(messages)) return null
|
|
55
|
+
// Walk back across user messages until we find one with actual text
|
|
56
|
+
// content. Latest user messages in a Claude Code session are often
|
|
57
|
+
// pure tool_result blocks; the human's intent text is in an earlier turn.
|
|
58
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
59
|
+
const msg = messages[i]
|
|
60
|
+
if (msg?.role !== 'user') continue
|
|
61
|
+
if (typeof msg.content === 'string') return msg.content
|
|
62
|
+
if (Array.isArray(msg.content)) {
|
|
63
|
+
for (const b of msg.content) {
|
|
64
|
+
if (b?.type === 'text' && typeof b.text === 'string') return b.text
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// No text in this user message — keep walking back
|
|
68
|
+
}
|
|
69
|
+
return null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function appendToLastUserMessageAnthropic(messages, text) {
|
|
73
|
+
if (!Array.isArray(messages) || !text) return false
|
|
74
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
75
|
+
const msg = messages[i]
|
|
76
|
+
if (msg?.role !== 'user') continue
|
|
77
|
+
if (typeof msg.content === 'string') {
|
|
78
|
+
msg.content = msg.content + '\n\n' + text
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
if (Array.isArray(msg.content)) {
|
|
82
|
+
for (let j = msg.content.length - 1; j >= 0; j--) {
|
|
83
|
+
const b = msg.content[j]
|
|
84
|
+
if (b?.type === 'text' && typeof b.text === 'string') {
|
|
85
|
+
b.text = b.text + '\n\n' + text
|
|
86
|
+
return true
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
msg.content.push({ type: 'text', text })
|
|
90
|
+
return true
|
|
91
|
+
}
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
return false
|
|
95
|
+
}
|
|
96
|
+
|
|
53
97
|
const anthropic = {
|
|
54
98
|
name: 'anthropic',
|
|
55
99
|
match(method, url) {
|
|
@@ -66,6 +110,12 @@ const anthropic = {
|
|
|
66
110
|
apply(body, targets) {
|
|
67
111
|
applyTargets(body, targets)
|
|
68
112
|
},
|
|
113
|
+
getLastUserText(body) {
|
|
114
|
+
return getLastUserTextFromAnthropicMessages(body?.messages)
|
|
115
|
+
},
|
|
116
|
+
injectOutputHint(body, text) {
|
|
117
|
+
return appendToLastUserMessageAnthropic(body?.messages, text)
|
|
118
|
+
},
|
|
69
119
|
}
|
|
70
120
|
|
|
71
121
|
function extractOpenAIChatTargets(msg, i) {
|
|
@@ -103,6 +153,27 @@ const openai = {
|
|
|
103
153
|
apply(body, targets) {
|
|
104
154
|
applyTargets(body, targets)
|
|
105
155
|
},
|
|
156
|
+
getLastUserText(body) {
|
|
157
|
+
if (!body?.messages?.length) return null
|
|
158
|
+
for (let i = body.messages.length - 1; i >= 0; i--) {
|
|
159
|
+
const msg = body.messages[i]
|
|
160
|
+
if (msg?.role === 'user' && typeof msg.content === 'string') return msg.content
|
|
161
|
+
}
|
|
162
|
+
return null
|
|
163
|
+
},
|
|
164
|
+
injectOutputHint(body, text) {
|
|
165
|
+
if (!body?.messages?.length || !text) return false
|
|
166
|
+
for (let i = body.messages.length - 1; i >= 0; i--) {
|
|
167
|
+
const msg = body.messages[i]
|
|
168
|
+
if (msg?.role !== 'user') continue
|
|
169
|
+
if (typeof msg.content === 'string') {
|
|
170
|
+
msg.content = msg.content + '\n\n' + text
|
|
171
|
+
return true
|
|
172
|
+
}
|
|
173
|
+
return false
|
|
174
|
+
}
|
|
175
|
+
return false
|
|
176
|
+
},
|
|
106
177
|
}
|
|
107
178
|
|
|
108
179
|
function extractGeminiContentTargets(content, ci) {
|
|
@@ -138,6 +209,36 @@ const gemini = {
|
|
|
138
209
|
|
|
139
210
|
return body.contents.flatMap(extractGeminiContentTargets)
|
|
140
211
|
},
|
|
212
|
+
getLastUserText(body) {
|
|
213
|
+
if (!body?.contents?.length) return null
|
|
214
|
+
for (let i = body.contents.length - 1; i >= 0; i--) {
|
|
215
|
+
const c = body.contents[i]
|
|
216
|
+
if (c?.role && c.role !== 'user') continue
|
|
217
|
+
if (!Array.isArray(c?.parts)) continue
|
|
218
|
+
for (const p of c.parts) {
|
|
219
|
+
if (typeof p?.text === 'string') return p.text
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return null
|
|
223
|
+
},
|
|
224
|
+
injectOutputHint(body, text) {
|
|
225
|
+
if (!body?.contents?.length || !text) return false
|
|
226
|
+
for (let i = body.contents.length - 1; i >= 0; i--) {
|
|
227
|
+
const c = body.contents[i]
|
|
228
|
+
if (c?.role && c.role !== 'user') continue
|
|
229
|
+
if (!Array.isArray(c?.parts)) continue
|
|
230
|
+
for (let j = c.parts.length - 1; j >= 0; j--) {
|
|
231
|
+
const p = c.parts[j]
|
|
232
|
+
if (typeof p?.text === 'string') {
|
|
233
|
+
p.text = p.text + '\n\n' + text
|
|
234
|
+
return true
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
c.parts.push({ text })
|
|
238
|
+
return true
|
|
239
|
+
}
|
|
240
|
+
return false
|
|
241
|
+
},
|
|
141
242
|
apply(body, targets) {
|
|
142
243
|
for (const t of targets) {
|
|
143
244
|
if (t.skip || !t.compressed) continue
|
|
@@ -191,6 +292,37 @@ const openaiResponses = {
|
|
|
191
292
|
apply(body, targets) {
|
|
192
293
|
applyTargets(body, targets)
|
|
193
294
|
},
|
|
295
|
+
getLastUserText(body) {
|
|
296
|
+
if (!Array.isArray(body?.input) || !body.input.length) return null
|
|
297
|
+
// Walk back across user items until we find one with actual text content
|
|
298
|
+
for (let i = body.input.length - 1; i >= 0; i--) {
|
|
299
|
+
const item = body.input[i]
|
|
300
|
+
if (item?.role !== 'user' || !Array.isArray(item.content)) continue
|
|
301
|
+
for (const block of item.content) {
|
|
302
|
+
if (block?.type === 'input_text' && typeof block.text === 'string') return block.text
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return null
|
|
306
|
+
},
|
|
307
|
+
injectOutputHint(body, text) {
|
|
308
|
+
if (!Array.isArray(body?.input) || !body.input.length || !text) return false
|
|
309
|
+
// Target the LATEST user item (cache-safe). If it has no input_text
|
|
310
|
+
// block we push one, so injection always succeeds for any user item.
|
|
311
|
+
for (let i = body.input.length - 1; i >= 0; i--) {
|
|
312
|
+
const item = body.input[i]
|
|
313
|
+
if (item?.role !== 'user' || !Array.isArray(item.content)) continue
|
|
314
|
+
for (let j = item.content.length - 1; j >= 0; j--) {
|
|
315
|
+
const block = item.content[j]
|
|
316
|
+
if (block?.type === 'input_text' && typeof block.text === 'string') {
|
|
317
|
+
block.text = block.text + '\n\n' + text
|
|
318
|
+
return true
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
item.content.push({ type: 'input_text', text })
|
|
322
|
+
return true
|
|
323
|
+
}
|
|
324
|
+
return false
|
|
325
|
+
},
|
|
194
326
|
}
|
|
195
327
|
|
|
196
328
|
const providers = [anthropic, openai, openaiResponses, gemini]
|