@sliday/tamp 0.7.0 → 0.8.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/config.js CHANGED
@@ -1,7 +1,15 @@
1
1
  import { readFileSync } from 'node:fs'
2
2
  import { homedir } from 'node:os'
3
3
  import { join } from 'node:path'
4
- import { DEFAULT_STAGES, VERSION, COMPRESSION_PRESETS } from './metadata.js'
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'
5
13
 
6
14
  const VALID_OUTPUT_MODES = new Set(['off', 'conservative', 'balanced', 'aggressive'])
7
15
 
@@ -12,20 +20,47 @@ function parseBoolean(value, defaultValue) {
12
20
  return defaultValue
13
21
  }
14
22
 
15
- function resolvePreset(presetName, explicitStages) {
16
- // If user explicitly set TAMP_STAGES, ignore preset
17
- if (explicitStages && explicitStages.length > 0) {
18
- return explicitStages
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 }
19
31
  }
20
-
21
- // Resolve preset name to stages
22
- const preset = COMPRESSION_PRESETS[presetName]
23
- if (preset) {
24
- return preset.stages
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 }
25
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
+ })
26
59
 
27
- // Default to balanced preset if preset name not recognized
28
- return COMPRESSION_PRESETS.balanced.stages
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]
29
64
  }
30
65
 
31
66
  export const CONFIG_PATH = join(homedir(), '.config', 'tamp', 'config')
@@ -51,19 +86,97 @@ export function loadConfigFile(path) {
51
86
  } catch { return {} }
52
87
  }
53
88
 
54
- export function loadConfig(env = process.env) {
89
+ export function loadConfig(env = process.env, options = {}) {
55
90
  const fileVars = (env === process.env) ? loadConfigFile() : {}
56
91
  const get = (key) => env[key] !== undefined ? env[key] : fileVars[key]
57
92
 
58
- // Get explicit stages (if user set TAMP_STAGES directly)
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
+
59
101
  const explicitStagesStr = get('TAMP_STAGES')
60
- const explicitStages = explicitStagesStr ? explicitStagesStr.split(',').map(s => s.trim()).filter(Boolean) : []
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
61
108
 
62
- // Get compression preset
63
- const presetName = get('TAMP_COMPRESSION_PRESET') || 'balanced'
109
+ const fileLevelRaw = fileVars.level
110
+ const levelFlagRaw = options.levelOverride
111
+ const levelEnvRaw = get('TAMP_LEVEL')
64
112
 
65
- // Resolve preset to stages (or use explicit stages if provided)
66
- const stages = resolvePreset(presetName, explicitStages)
113
+ let stages = null
114
+ let level = null
115
+ let levelSource = null
116
+ let presetName = presetExplicit ? presetNameRaw : 'balanced'
117
+
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
+ }
67
180
 
68
181
  // Output mode precedence (highest wins):
69
182
  // 1. TAMP_OUTPUT_MODE — explicit per-session override
@@ -83,10 +196,23 @@ export function loadConfig(env = process.env) {
83
196
  openai: get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
84
197
  'openai-responses': get('TAMP_UPSTREAM_OPENAI') || 'https://api.openai.com',
85
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',
86
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',
87
211
  minSize: parseInt(get('TAMP_MIN_SIZE'), 10) || 200,
88
212
  stages,
89
213
  preset: presetName,
214
+ level,
215
+ levelSource,
90
216
  outputMode,
91
217
  outputModeDefault: defaultOutputMode && VALID_OUTPUT_MODES.has(defaultOutputMode) ? defaultOutputMode : null,
92
218
  agent: get('TAMP_AGENT') || null,
@@ -115,6 +241,15 @@ export const CONFIG_TEMPLATE = `# Tamp configuration
115
241
  # TAMP_UPSTREAM=https://api.anthropic.com
116
242
  # TAMP_UPSTREAM_OPENAI=https://api.openai.com
117
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
118
253
 
119
254
  # Compression preset (conservative | balanced | aggressive)
120
255
  # TAMP_COMPRESSION_PRESET=balanced
package/index.js CHANGED
@@ -5,7 +5,7 @@ import zlib from 'node:zlib'
5
5
  import * as fzstd from 'fzstd'
6
6
  import { loadConfig } from './config.js'
7
7
  import { compressRequest } from './compress.js'
8
- import { detectProvider } from './providers.js'
8
+ import { detectProvider, detectOpenAIAuthMode, resolveOpenAIUpstream } from './providers.js'
9
9
  import { createSession, formatRequestLog } from './stats.js'
10
10
  import { createSessionStore, deriveSessionKey } from './session-graph.js'
11
11
  import { createReadCache } from './lib/read-cache.js'
@@ -51,11 +51,40 @@ function buildUpstreamUrl(reqPath, base) {
51
51
  return parsed
52
52
  }
53
53
 
54
- export function createProxy(overrides = {}) {
55
- const base = loadConfig()
54
+ // Pick the upstream base + path transform for a resolved provider. For
55
+ // OpenAI-like providers we inspect auth headers to detect Codex CLI ChatGPT
56
+ // Plus mode (OAuth JWT + chatgpt-account-id), which must be routed to
57
+ // chatgpt.com/backend-api/codex instead of api.openai.com.
58
+ export function resolveUpstream(provider, headers, config) {
59
+ const defaultBase = config.upstreams?.[provider.name] || config.upstream
60
+ if (config.disableChatgptRoute) {
61
+ return { base: defaultBase, transformPath: (p) => p, mode: 'api-key' }
62
+ }
63
+ if (provider.name === 'openai' || provider.name === 'openai-responses') {
64
+ const mode = detectOpenAIAuthMode(headers)
65
+ if (mode === 'chatgpt-oauth') {
66
+ return resolveOpenAIUpstream({
67
+ mode,
68
+ base: config.chatgptUpstream || 'https://chatgpt.com',
69
+ providerName: provider.name,
70
+ })
71
+ }
72
+ }
73
+ return { base: defaultBase, transformPath: (p) => p, mode: 'api-key' }
74
+ }
75
+
76
+ export function createProxy(overrides = {}, loadOpts = {}) {
77
+ const base = loadConfig(process.env, loadOpts)
56
78
  const config = { ...base, ...overrides }
57
79
  if (overrides.upstream && !overrides.upstreams) {
58
- config.upstreams = { anthropic: overrides.upstream, openai: overrides.upstream, 'openai-responses': overrides.upstream, gemini: overrides.upstream }
80
+ config.upstreams = {
81
+ anthropic: overrides.upstream,
82
+ openai: overrides.upstream,
83
+ 'openai-responses': overrides.upstream,
84
+ gemini: overrides.upstream,
85
+ kimi: overrides.upstream,
86
+ moonshot: overrides.upstream,
87
+ }
59
88
  }
60
89
  const session = createSession()
61
90
  const brCache = (config.stages?.includes('graph') || config.stages?.includes('br-cache') || config.stages?.includes('disclosure'))
@@ -177,7 +206,7 @@ return http.createServer(async (req, res) => {
177
206
  return res.end(req.method === 'HEAD' ? undefined : body)
178
207
  }
179
208
 
180
- const provider = detectProvider(req.method, req.url)
209
+ const provider = detectProvider(req.method, req.url, req.headers)
181
210
 
182
211
  if (!provider) {
183
212
  log(`[tamp] ${req.method} ${req.url}`)
@@ -185,8 +214,10 @@ return http.createServer(async (req, res) => {
185
214
  return pipeRequest(req, res, upstreamUrl)
186
215
  }
187
216
 
188
- const upstream = config.upstreams?.[provider.name] || config.upstream
189
- const reqUrl = provider.normalizeUrl ? provider.normalizeUrl(req.url) : req.url
217
+ const route = resolveUpstream(provider, req.headers, config)
218
+ const upstream = route.base
219
+ const normalizedUrl = provider.normalizeUrl ? provider.normalizeUrl(req.url) : req.url
220
+ const reqUrl = route.transformPath(normalizedUrl)
190
221
  const upstreamUrl = buildUpstreamUrl(reqUrl, upstream)
191
222
 
192
223
  const chunks = []
package/metadata.js CHANGED
@@ -121,6 +121,7 @@ export const COMPRESSION_PRESETS = Object.freeze({
121
121
  stages: ['cmd-strip', 'minify', 'toon', 'strip-lines', 'whitespace', 'dedup', 'diff'],
122
122
  expectedSavings: '45-50%',
123
123
  risk: 'None',
124
+ level: 4,
124
125
  },
125
126
  balanced: {
126
127
  name: 'Balanced',
@@ -128,6 +129,7 @@ export const COMPRESSION_PRESETS = Object.freeze({
128
129
  stages: ['cmd-strip', 'minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'read-diff', 'prune'],
129
130
  expectedSavings: '52-58%',
130
131
  risk: 'Low',
132
+ level: 5,
131
133
  },
132
134
  aggressive: {
133
135
  name: 'Aggressive',
@@ -135,5 +137,56 @@ export const COMPRESSION_PRESETS = Object.freeze({
135
137
  stages: ['cmd-strip', 'minify', 'toon', 'strip-lines', 'whitespace', 'llmlingua', 'dedup', 'diff', 'read-diff', 'prune', 'strip-comments', 'textpress', 'br-cache', 'disclosure', 'bm25-trim'],
136
138
  expectedSavings: '65-72%',
137
139
  risk: 'Medium (may lose comments, verbose text)',
140
+ level: 8,
138
141
  },
139
142
  })
143
+
144
+ // Flat 1..9 compression ladder, parallel to the named presets. Each level
145
+ // extends the previous level's stage list (prefix-preserving). L4/L5/L8 are
146
+ // set-equal to conservative/balanced/aggressive respectively; L6-L7 interpolate
147
+ // between balanced and aggressive; L9 extends past aggressive (max).
148
+ //
149
+ // NOTE: `lossy` goes true at L5 because `llmlingua` lives in the balanced
150
+ // preset (= L5). The ladder is aligned to preset semantics per spec.
151
+ function extend(base, adds) { return Object.freeze([...base, ...adds]) }
152
+
153
+ const L1_STAGES = extend([], ['minify'])
154
+ const L2_STAGES = extend(L1_STAGES, ['whitespace', 'strip-lines'])
155
+ const L3_STAGES = extend(L2_STAGES, ['cmd-strip'])
156
+ const L4_STAGES = extend(L3_STAGES, ['toon', 'dedup', 'diff'])
157
+ const L5_STAGES = extend(L4_STAGES, ['llmlingua', 'read-diff', 'prune'])
158
+ const L6_STAGES = extend(L5_STAGES, ['strip-comments'])
159
+ const L7_STAGES = extend(L6_STAGES, ['textpress', 'br-cache'])
160
+ const L8_STAGES = extend(L7_STAGES, ['disclosure', 'bm25-trim'])
161
+ const L9_STAGES = extend(L8_STAGES, ['graph', 'foundation-models'])
162
+
163
+ export const COMPRESSION_LEVELS = Object.freeze({
164
+ 1: Object.freeze({ stages: L1_STAGES, lossy: false, savings: '~15%' }),
165
+ 2: Object.freeze({ stages: L2_STAGES, lossy: false, savings: '~25%' }),
166
+ 3: Object.freeze({ stages: L3_STAGES, lossy: false, savings: '~35%' }),
167
+ 4: Object.freeze({ stages: L4_STAGES, lossy: false, savings: '~45%' }),
168
+ 5: Object.freeze({ stages: L5_STAGES, lossy: true, savings: '~53%' }),
169
+ 6: Object.freeze({ stages: L6_STAGES, lossy: true, savings: '~58%' }),
170
+ 7: Object.freeze({ stages: L7_STAGES, lossy: true, savings: '~62%' }),
171
+ 8: Object.freeze({ stages: L8_STAGES, lossy: true, savings: '~67%' }),
172
+ 9: Object.freeze({ stages: L9_STAGES, lossy: true, savings: '~72%' }),
173
+ })
174
+
175
+ export const DEFAULT_LEVEL = 5
176
+
177
+ export const LEVEL_ALIASES = Object.freeze({
178
+ conservative: 4,
179
+ balanced: 5,
180
+ aggressive: 8,
181
+ max: 9,
182
+ })
183
+
184
+ export function resolveLevel(input) {
185
+ if (typeof input === 'number' && Number.isInteger(input) && input >= 1 && input <= 9) {
186
+ return COMPRESSION_LEVELS[input]
187
+ }
188
+ if (typeof input === 'string' && input in LEVEL_ALIASES) {
189
+ return COMPRESSION_LEVELS[LEVEL_ALIASES[input]]
190
+ }
191
+ return null
192
+ }
package/package.json CHANGED
@@ -10,9 +10,11 @@
10
10
  "providers.js",
11
11
  "session-graph.js",
12
12
  "stats.js",
13
- "lib/"
13
+ "lib/",
14
+ "sidecar/server.py",
15
+ "sidecar/requirements.txt"
14
16
  ],
15
- "version": "0.7.0",
17
+ "version": "0.8.1",
16
18
  "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 and per-command rewriters.",
17
19
  "type": "module",
18
20
  "main": "index.js",
@@ -24,7 +26,9 @@
24
26
  "test": "node --test test/*.test.js",
25
27
  "test:sidecar": "node test/capture-golden.js --verify",
26
28
  "bench:semantic": "node bench/semantic-eval.js",
27
- "test:quality": "node bench/quality-eval.js"
29
+ "test:quality": "node bench/quality-eval.js",
30
+ "compat:probe": "node test/compat/probe-proxy.js",
31
+ "compat:run": "node test/compat/run.mjs"
28
32
  },
29
33
  "keywords": [
30
34
  "claude",
package/providers.js CHANGED
@@ -184,12 +184,20 @@ function applyAnthropicRehydration(body, refs) {
184
184
  if (typeof current !== 'string') continue
185
185
 
186
186
  let updated = current
187
+ const seenMatch = new Set()
187
188
  for (const ref of items) {
189
+ if (seenMatch.has(ref.match)) continue
190
+ seenMatch.add(ref.match)
188
191
  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)
192
+ // Idempotency across passes: skip if the INCOMING body already carried
193
+ // this expansion. Check `current` (the original), not `updated`, so an
194
+ // expansion we insert this pass cannot suppress a sibling marker that
195
+ // shares the same hash.
196
+ if (current.indexOf(expandedSentinel) !== -1) continue
197
+ if (current.indexOf(ref.match) === -1) continue
198
+ // Replace every occurrence: the model may quote the same marker more
199
+ // than once in one block.
200
+ updated = updated.split(ref.match).join(ref.expansion)
193
201
  }
194
202
 
195
203
  if (updated === current) continue
@@ -235,8 +243,30 @@ const anthropic = {
235
243
  },
236
244
  }
237
245
 
246
+ // OpenAI-compatible block types we don't know how to recompress safely.
247
+ // Kimi's "thinking" / "partial" deltas in particular must be left alone —
248
+ // they are not tool_result text and compressing them breaks the wire format.
249
+ const OPENAI_COMPAT_SKIP_BLOCK_TYPES = new Set(['thinking', 'partial'])
250
+
238
251
  function extractOpenAIChatTargets(msg, i) {
239
- if (msg.role !== 'tool' || typeof msg.content !== 'string') return []
252
+ if (msg.role !== 'tool') return []
253
+ // Some OpenAI-compat providers (Kimi, etc.) emit non-string tool content
254
+ // with typed blocks like { type: 'thinking' }. Skip those.
255
+ if (typeof msg.content !== 'string') {
256
+ if (Array.isArray(msg.content)) {
257
+ const targets = []
258
+ for (let j = 0; j < msg.content.length; j++) {
259
+ const block = msg.content[j]
260
+ if (!block || typeof block !== 'object') continue
261
+ if (OPENAI_COMPAT_SKIP_BLOCK_TYPES.has(block.type)) continue
262
+ if (block.type === 'text' && typeof block.text === 'string') {
263
+ targets.push({ path: ['messages', i, 'content', j, 'text'], text: block.text, index: i })
264
+ }
265
+ }
266
+ return targets
267
+ }
268
+ return []
269
+ }
240
270
  return [{ path: ['messages', i, 'content'], text: msg.content, index: i }]
241
271
  }
242
272
 
@@ -274,7 +304,14 @@ const openai = {
274
304
  if (!body?.messages?.length) return null
275
305
  for (let i = body.messages.length - 1; i >= 0; i--) {
276
306
  const msg = body.messages[i]
277
- if (msg?.role === 'user' && typeof msg.content === 'string') return msg.content
307
+ if (msg?.role !== 'user') continue
308
+ if (typeof msg.content === 'string') return msg.content
309
+ // OpenAI chat allows array content parts (vision, structured input).
310
+ if (Array.isArray(msg.content)) {
311
+ for (const b of msg.content) {
312
+ if (b?.type === 'text' && typeof b.text === 'string') return b.text
313
+ }
314
+ }
278
315
  }
279
316
  return null
280
317
  },
@@ -287,12 +324,71 @@ const openai = {
287
324
  msg.content = msg.content + '\n\n' + text
288
325
  return true
289
326
  }
327
+ if (Array.isArray(msg.content)) {
328
+ for (let j = msg.content.length - 1; j >= 0; j--) {
329
+ const b = msg.content[j]
330
+ if (b?.type === 'text' && typeof b.text === 'string') {
331
+ b.text = b.text + '\n\n' + text
332
+ return true
333
+ }
334
+ }
335
+ msg.content.push({ type: 'text', text })
336
+ return true
337
+ }
290
338
  return false
291
339
  }
292
340
  return false
293
341
  },
294
342
  }
295
343
 
344
+ // Kimi Code (subscription) + Moonshot (OpenAI-compat API key) share wire
345
+ // format with OpenAI chat-completions aside from extra block types on
346
+ // streamed messages. Treat them as passthrough: same extract/apply contract
347
+ // as the openai adapter, but a different name so index.js can route them to
348
+ // api.kimi.com / api.moonshot.cn upstreams.
349
+ const kimi = {
350
+ name: 'kimi',
351
+ match(method, url) {
352
+ if (method !== 'POST') return false
353
+ return (
354
+ url.startsWith('/coding/v1/chat/completions') ||
355
+ url.startsWith('/kimi/v1/chat/completions') ||
356
+ url.startsWith('/kimi/coding/v1/chat/completions')
357
+ )
358
+ },
359
+ normalizeUrl(url) {
360
+ // Strip the tamp-mount prefix so the upstream receives the canonical
361
+ // Kimi Code path.
362
+ if (url.startsWith('/kimi/coding/v1/')) return url.slice('/kimi'.length)
363
+ if (url.startsWith('/kimi/v1/')) return '/coding' + url.slice('/kimi'.length)
364
+ return url
365
+ },
366
+ extract: openai.extract,
367
+ apply: openai.apply,
368
+ getLastUserText: openai.getLastUserText,
369
+ injectOutputHint: openai.injectOutputHint,
370
+ }
371
+
372
+ const moonshot = {
373
+ name: 'moonshot',
374
+ match(method, url) {
375
+ if (method !== 'POST') return false
376
+ return (
377
+ url.startsWith('/moonshot/v1/chat/completions') ||
378
+ url.startsWith('/moonshot/chat/completions')
379
+ )
380
+ },
381
+ normalizeUrl(url) {
382
+ if (url.startsWith('/moonshot/v1/')) return url.slice('/moonshot'.length)
383
+ if (url.startsWith('/moonshot/chat/completions')) return '/v1' + url.slice('/moonshot'.length)
384
+ return url
385
+ },
386
+ extract: openai.extract,
387
+ apply: openai.apply,
388
+ getLastUserText: openai.getLastUserText,
389
+ injectOutputHint: openai.injectOutputHint,
390
+ }
391
+
296
392
  function extractGeminiContentTargets(content, ci) {
297
393
  const targets = []
298
394
  if (!content.parts?.length) return targets
@@ -442,13 +538,103 @@ const openaiResponses = {
442
538
  },
443
539
  }
444
540
 
445
- const providers = [anthropic, openai, openaiResponses, gemini]
541
+ // ---- Auth-mode detection for OpenAI-like providers ----
542
+ //
543
+ // Codex CLI running in ChatGPT Plus mode does NOT use a traditional
544
+ // `sk-*` API key. It sends an OAuth JWT as the Bearer token, plus a
545
+ // `chatgpt-account-id` header. The upstream in that mode is
546
+ // https://chatgpt.com/backend-api/codex (NOT api.openai.com). See
547
+ // openai/codex `model-provider-info/src/lib.rs` for the canonical list.
548
+ //
549
+ // Returns:
550
+ // 'api-key' — classic `Authorization: Bearer sk-...`
551
+ // 'chatgpt-oauth' — JWT bearer OR chatgpt-account-id header present
552
+ // 'unknown' — neither
553
+ function headerValue(headers, name) {
554
+ if (!headers) return null
555
+ // Node's IncomingHeaders are already lowercased, but be defensive for
556
+ // plain objects passed from tests.
557
+ if (headers[name] !== undefined) return headers[name]
558
+ const lower = name.toLowerCase()
559
+ if (headers[lower] !== undefined) return headers[lower]
560
+ for (const k of Object.keys(headers)) {
561
+ if (k.toLowerCase() === lower) return headers[k]
562
+ }
563
+ return null
564
+ }
565
+
566
+ function extractBearer(auth) {
567
+ if (typeof auth !== 'string') return null
568
+ const m = auth.match(/^\s*Bearer\s+(.+?)\s*$/i)
569
+ return m ? m[1] : null
570
+ }
446
571
 
447
- export function detectProvider(method, url) {
572
+ function looksLikeJwt(token) {
573
+ if (typeof token !== 'string') return false
574
+ // Compact JWS: three base64url segments separated by dots. Header segment
575
+ // starts with "eyJ" (base64-encoded `{"`). Good enough for detection.
576
+ if (!token.startsWith('eyJ')) return false
577
+ const parts = token.split('.')
578
+ return parts.length === 3 && parts.every(p => p.length > 0)
579
+ }
580
+
581
+ export function detectOpenAIAuthMode(headersOrRequest) {
582
+ const headers = headersOrRequest?.headers || headersOrRequest || {}
583
+ const accountId = headerValue(headers, 'chatgpt-account-id')
584
+ const auth = headerValue(headers, 'authorization')
585
+ const token = extractBearer(auth)
586
+
587
+ if (accountId) return 'chatgpt-oauth'
588
+ if (token && token.startsWith('sk-')) return 'api-key'
589
+ if (token && looksLikeJwt(token)) return 'chatgpt-oauth'
590
+ return 'unknown'
591
+ }
592
+
593
+ // Route selection for OpenAI-like requests. Returns the upstream base URL
594
+ // and a path transformer. Callers are responsible for enforcing
595
+ // TAMP_DISABLE_CHATGPT_ROUTE (handled in index.js).
596
+ export function resolveOpenAIUpstream({ mode, base, providerName }) {
597
+ if (mode === 'chatgpt-oauth') {
598
+ return {
599
+ base: 'https://chatgpt.com',
600
+ transformPath(path) {
601
+ // Codex calls land on /v1/responses or /v1/chat/completions. The
602
+ // ChatGPT backend expects /backend-api/codex<rest>.
603
+ if (path.startsWith('/backend-api/codex')) return path
604
+ return '/backend-api/codex' + path
605
+ },
606
+ mode,
607
+ providerName,
608
+ }
609
+ }
610
+ return {
611
+ base,
612
+ transformPath(path) { return path },
613
+ mode,
614
+ providerName,
615
+ }
616
+ }
617
+
618
+ const providers = [anthropic, openai, openaiResponses, gemini, kimi, moonshot]
619
+
620
+ // detectProvider(method, url, headers?) — headers is optional for b/c. When
621
+ // the path is OpenAI-like but headers carry a `x-tamp-target: kimi|moonshot`
622
+ // hint we route to the corresponding adapter. Path-based matching runs
623
+ // first so existing mounts keep working.
624
+ export function detectProvider(method, url, headers) {
625
+ const hint = headers ? headerValue(headers, 'x-tamp-target') : null
626
+ if (hint) {
627
+ const hinted = providers.find(p => p.name === hint)
628
+ if (hinted && hinted.match(method, url)) return hinted
629
+ // Hint may point to a kimi/moonshot provider even if path doesn't carry
630
+ // the tamp mount prefix (e.g. user pointed their client at /v1/...).
631
+ if (hint === 'kimi' && openai.match(method, url)) return kimi
632
+ if (hint === 'moonshot' && openai.match(method, url)) return moonshot
633
+ }
448
634
  for (const p of providers) {
449
635
  if (p.match(method, url)) return p
450
636
  }
451
637
  return null
452
638
  }
453
639
 
454
- export { anthropic, openai, openaiResponses, gemini }
640
+ export { anthropic, openai, openaiResponses, gemini, kimi, moonshot }
@@ -0,0 +1,4 @@
1
+ llmlingua
2
+ mlx
3
+ uvicorn
4
+ fastapi