Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2026 Conrad Lelubre

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 14 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default function App() {
const [editingConfig, setEditingConfig] = useState(false);
const [editHost, setEditHost] = useState('');
const [editPort, setEditPort] = useState('');
const [editScheme, setEditScheme] = useState<'http' | 'https'>('http');
const [status, setStatus] = useState<ProxyStatus>({ running: false, capturing: false, activeModel: null });
const [sessions, setSessions] = useState<SessionMeta[]>([]);
const [selectedSessionId, setSelectedSessionId] = useState<string | null>(null);
Expand Down Expand Up @@ -169,7 +170,7 @@ export default function App() {
};

const handleSaveConfig = () => {
api.updateConfig({ targetHost: editHost, targetPort: parseInt(editPort, 10) }).then(c => {
api.updateConfig({ targetHost: editHost, targetPort: parseInt(editPort, 10), targetScheme: editScheme }).then(c => {
setConfig(c);
setEditingConfig(false);
}).catch(() => showError('Failed to save config'));
Expand All @@ -179,6 +180,7 @@ export default function App() {
if (config) {
setEditHost(config.targetHost);
setEditPort(String(config.targetPort));
setEditScheme(config.targetScheme);
}
setEditingConfig(true);
};
Expand All @@ -205,12 +207,22 @@ export default function App() {
<div className="header-center">
{config && !editingConfig && (
<span className="target-url" onClick={startEditingConfig} title="Click to edit target">
{config.targetHost}:{config.targetPort}
{config.targetScheme}://{config.targetHost}:{config.targetPort}
<span className="edit-icon">✎</span>
</span>
)}
{config && editingConfig && (
<span className="target-url editing">
<select
value={editScheme}
onChange={e => setEditScheme(e.target.value as 'http' | 'https')}
disabled={status.running}
title={status.running ? 'Stop proxy to change target' : ''}
>
<option value="http">http</option>
<option value="https">https</option>
</select>
<span className="sep">://</span>
<input
className="host-input"
value={editHost}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/hooks/useApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Config {
targetHost: string;
targetPort: number;
proxyPort: number;
targetScheme: 'http' | 'https';
}

export interface SessionMeta {
Expand Down Expand Up @@ -58,7 +59,7 @@ export interface ThreadInfo {

export const api = {
getConfig: () => request<Config>('/config'),
updateConfig: (cfg: { targetHost?: string; targetPort?: number }) =>
updateConfig: (cfg: { targetHost?: string; targetPort?: number; targetScheme?: 'http' | 'https' }) =>
request<Config>('/config', { method: 'PUT', body: JSON.stringify(cfg) }),

proxyStart: () => request<{ running: boolean }>('/proxy/start', { method: 'POST' }),
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"name": "cache-hunter",
"version": "1.0.0",
"description": "Transparent proxy for vLLM with SQLite logging for cache debugging",
"license": "MIT",
"type": "module",
"scripts": {
"start": "concurrently -n backend,frontend -c blue,green \"npm run start:backend\" \"npm run start:frontend\"",
"start:backend": "tsx src/index.ts",
"start:backend": "NODE_EXTRA_CA_CERTS=/etc/ssl/certs/ca-certificates.crt tsx src/index.ts",
"start:frontend": "cd frontend && npm run dev",
"dev": "concurrently -n backend,frontend -c blue,green \"npm run dev:backend\" \"npm run dev:frontend\"",
"dev:backend": "tsx watch src/index.ts",
"dev:backend": "NODE_OPTIONS=--use-system-ca tsx watch src/index.ts",
"dev:frontend": "cd frontend && npm run dev",
"build": "cd frontend && npm run build",
"test": "vitest",
Expand Down
25 changes: 24 additions & 1 deletion src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('API Endpoints', () => {
const { ProxyEngine } = await import('./proxy-engine.js');
const { createApp } = await import('./app.js');

const engine = new ProxyEngine({ targetHost: 'localhost', targetPort: 8765, proxyPort: 0 });
const engine = new ProxyEngine({ targetHost: 'localhost', targetPort: 8765, proxyPort: 0, targetScheme: 'http' });
const app = createApp(engine, TEST_DATA_DIR);

server = await new Promise<any>((resolve) => {
Expand All @@ -51,6 +51,7 @@ describe('API Endpoints', () => {
expect(status).toBe(200);
expect(body.targetHost).toBe('localhost');
expect(body.targetPort).toBe(8765);
expect(body.targetScheme).toBe('http');
});

it('PUT /api/config updates config and persists to disk', async () => {
Expand All @@ -69,6 +70,28 @@ describe('API Endpoints', () => {
expect(saved.targetPort).toBe(8080);
});

it('PUT /api/config with targetScheme https updates correctly', async () => {
const { status, body } = await fetchJson(`${baseUrl}/api/config`, {
method: 'PUT',
body: JSON.stringify({ targetHost: 'api.anthropic.com', targetPort: 443, targetScheme: 'https' }),
});
expect(status).toBe(200);
expect(body.targetScheme).toBe('https');
expect(body.targetHost).toBe('api.anthropic.com');

const saved = JSON.parse(readFileSync(getConfigPath(TEST_DATA_DIR), 'utf-8'));
expect(saved.targetScheme).toBe('https');
});

it('PUT /api/config ignores invalid targetScheme', async () => {
const { status, body } = await fetchJson(`${baseUrl}/api/config`, {
method: 'PUT',
body: JSON.stringify({ targetScheme: 'ftp' }),
});
expect(status).toBe(200);
expect(body.targetScheme).toBe('http');
});

it('GET /api/proxy/status returns stopped initially', async () => {
const { status, body } = await fetchJson(`${baseUrl}/api/proxy/status`);
expect(status).toBe(200);
Expand Down
7 changes: 4 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,23 @@ export function createApp(engine: ProxyEngine, dataDir: string = DATA_DIR, broad
// Config
app.get('/api/config', (_req: Request, res: Response) => {
const cfg = engine.getConfig()
res.json({ targetHost: cfg.targetHost, targetPort: cfg.targetPort, proxyPort: cfg.proxyPort })
res.json({ targetHost: cfg.targetHost, targetPort: cfg.targetPort, proxyPort: cfg.proxyPort, targetScheme: cfg.targetScheme })
})

app.put('/api/config', (req: Request, res: Response) => {
if (engine.running) {
res.status(409).json({ error: 'Cannot change config while proxy is running' })
return
}
const { targetHost, targetPort } = req.body
const { targetHost, targetPort, targetScheme } = req.body
const updates: any = {}
if (targetHost) updates.targetHost = targetHost
if (targetPort) updates.targetPort = parseInt(targetPort, 10)
if (targetScheme === 'http' || targetScheme === 'https') updates.targetScheme = targetScheme
engine.updateConfig(updates)
const cfg = engine.getConfig()
saveProxyConfig(dataDir, cfg)
res.json({ targetHost: cfg.targetHost, targetPort: cfg.targetPort })
res.json({ targetHost: cfg.targetHost, targetPort: cfg.targetPort, targetScheme: cfg.targetScheme })
})

// Proxy
Expand Down
26 changes: 22 additions & 4 deletions src/config-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ describe('config-store', () => {
rmSync(TEST_DIR, { recursive: true, force: true })
})

const envFallback = { targetHost: 'fallback.host', targetPort: 9999, proxyPort: 7777 }
const envFallback = { targetHost: 'fallback.host', targetPort: 9999, proxyPort: 7777, targetScheme: 'http' as const }

it('returns env fallback when no config file exists', () => {
const cfg = loadProxyConfig(TEST_DIR, envFallback)
expect(cfg).toEqual(envFallback)
})

it('reads config from file when it exists', () => {
const saved = { targetHost: '10.0.0.1', targetPort: 8080, proxyPort: 8888 }
const saved = { targetHost: '10.0.0.1', targetPort: 8080, proxyPort: 8888, targetScheme: 'https' as const }
saveProxyConfig(TEST_DIR, saved)

const loaded = loadProxyConfig(TEST_DIR, envFallback)
Expand All @@ -43,7 +43,7 @@ describe('config-store', () => {

it('falls through on corrupt file', () => {
const path = getConfigPath(TEST_DIR)
saveProxyConfig(TEST_DIR, { targetHost: 'x', targetPort: 1, proxyPort: 2 })
saveProxyConfig(TEST_DIR, { targetHost: 'x', targetPort: 1, proxyPort: 2, targetScheme: 'http' })
// corrupt it
const { writeFileSync } = require('fs')
writeFileSync(path, '{invalid json')
Expand All @@ -53,10 +53,28 @@ describe('config-store', () => {
})

it('writes valid JSON to disk', () => {
const cfg = { targetHost: 'a.b.c', targetPort: 1111, proxyPort: 2222 }
const cfg = { targetHost: 'a.b.c', targetPort: 1111, proxyPort: 2222, targetScheme: 'http' as const }
saveProxyConfig(TEST_DIR, cfg)

const raw = readFileSync(getConfigPath(TEST_DIR), 'utf-8')
expect(JSON.parse(raw)).toEqual(cfg)
})

it('falls back to http scheme for legacy config without targetScheme', () => {
const legacy = { targetHost: 'old.host', targetPort: 8000, proxyPort: 8787 } as any
saveProxyConfig(TEST_DIR, legacy)

const loaded = loadProxyConfig(TEST_DIR, envFallback)
expect(loaded.targetScheme).toBe('http')
})

it('saves and loads https scheme correctly', () => {
const cfg = { targetHost: 'api.anthropic.com', targetPort: 443, proxyPort: 8787, targetScheme: 'https' as const }
saveProxyConfig(TEST_DIR, cfg)

const loaded = loadProxyConfig(TEST_DIR, envFallback)
expect(loaded.targetScheme).toBe('https')
expect(loaded.targetHost).toBe('api.anthropic.com')
expect(loaded.targetPort).toBe(443)
})
})
2 changes: 2 additions & 0 deletions src/config-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface StoredProxyConfig {
targetHost: string
targetPort: number
proxyPort: number
targetScheme: 'http' | 'https'
}

const CONFIG_FILENAME = 'proxy-config.json'
Expand All @@ -23,6 +24,7 @@ export function loadProxyConfig(dataDir: string, envFallback: StoredProxyConfig)
targetHost: parsed.targetHost ?? envFallback.targetHost,
targetPort: parsed.targetPort ?? envFallback.targetPort,
proxyPort: parsed.proxyPort ?? envFallback.proxyPort,
targetScheme: parsed.targetScheme ?? envFallback.targetScheme,
}
} catch {
// corrupt file, fall through
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const envDefaults = {
targetHost: process.env.TARGET_HOST || '127.0.0.1',
targetPort: parseInt(process.env.TARGET_PORT || '8000', 10),
proxyPort: PROXY_PORT_ENV,
targetScheme: (process.env.TARGET_SCHEME === 'https' ? 'https' : 'http') as 'http' | 'https',
};

const persisted = loadProxyConfig(DATA_DIR, envDefaults);
Expand All @@ -29,6 +30,7 @@ const engine = new ProxyEngine({
targetHost: persisted.targetHost,
targetPort: persisted.targetPort,
proxyPort: persisted.proxyPort,
targetScheme: persisted.targetScheme,
});

const server = createServer();
Expand All @@ -47,7 +49,7 @@ server.on('request', webApp);
server.listen(WEB_PORT, () => {
console.log(`Cache Hunter Web App running on http://localhost:${WEB_PORT}`);
console.log(`Proxy port: ${persisted.proxyPort}`);
console.log(`Default target: ${persisted.targetHost}:${persisted.targetPort}`);
console.log(`Default target: ${persisted.targetScheme}://${persisted.targetHost}:${persisted.targetPort}`);
console.log(`Data directory: ${DATA_DIR}`);

finalizeStaleSessions()
Expand Down
72 changes: 72 additions & 0 deletions src/parse-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,76 @@ describe('parseRequestBody', () => {
});
});

describe('parseRequestBody — Anthropic /v1/messages', () => {
it('parses basic messages with string system', () => {
const body = JSON.stringify({
model: 'claude-opus-4-5',
system: 'You are helpful',
messages: [{ role: 'user', content: 'Hello' }],
});
const result = parseRequestBody(body, '/v1/messages');
expect(result.messages).toHaveLength(2);
expect(result.messages[0]).toEqual({ role: 'system', content: 'You are helpful' });
expect(result.messages[1]).toEqual({ role: 'user', content: 'Hello' });
});

it('parses messages without system field', () => {
const body = JSON.stringify({
model: 'claude-opus-4-5',
messages: [{ role: 'user', content: 'Hi' }],
});
const result = parseRequestBody(body, '/v1/messages');
expect(result.messages).toHaveLength(1);
expect(result.messages[0]).toEqual({ role: 'user', content: 'Hi' });
});

it('parses system as array of content blocks', () => {
const system = [{ type: 'text', text: 'Be concise' }];
const body = JSON.stringify({
model: 'claude-opus-4-5',
system,
messages: [{ role: 'user', content: 'Hi' }],
});
const result = parseRequestBody(body, '/v1/messages');
expect(result.messages[0]).toEqual({ role: 'system', content: system });
});

it('includes tools as-is (Anthropic format)', () => {
const tools = [{ name: 'search', description: 'Web search', input_schema: { type: 'object', properties: {} } }];
const body = JSON.stringify({
model: 'claude-opus-4-5',
messages: [{ role: 'user', content: 'Search something' }],
tools,
});
const result = parseRequestBody(body, '/v1/messages');
expect(result.tools).toEqual(tools);
});

it('maps thinking budget_tokens to reasoningEffort', () => {
const body = JSON.stringify({
model: 'claude-opus-4-5',
messages: [{ role: 'user', content: 'Think hard' }],
thinking: { type: 'enabled', budget_tokens: 5000 },
});
const result = parseRequestBody(body, '/v1/messages');
expect(result.reasoningEffort).toBe('5000');
});

it('returns no reasoningEffort when thinking is absent', () => {
const body = JSON.stringify({
model: 'claude-opus-4-5',
messages: [{ role: 'user', content: 'Hello' }],
});
const result = parseRequestBody(body, '/v1/messages');
expect(result.reasoningEffort).toBeUndefined();
});

it('handles missing messages field', () => {
const body = JSON.stringify({ model: 'claude-opus-4-5', system: 'Be helpful' });
const result = parseRequestBody(body, '/v1/messages');
expect(result.messages).toHaveLength(1);
expect(result.messages[0]).toEqual({ role: 'system', content: 'Be helpful' });
});
});


19 changes: 17 additions & 2 deletions src/parse-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,29 @@ export function parseRequestBody(body: string, path: string): ParsedRequest {
return { messages: [], tools: [] };
}

if (path === '/v1/responses') {
if (path.startsWith('/v1/responses')) {
const messages = (parsed.input || [])
.filter((item: any) => item.type === 'message')
.map((item: any) => ({ ...item }));
return { messages, tools: parsed.tools || [], reasoningEffort: parsed.reasoning_effort };
}

if (path === '/v1/chat/completions') {
if (path.startsWith('/v1/messages')) {
const sys = parsed.system
const systemMessages: Array<Record<string, unknown>> = sys
? [{ role: 'system', content: sys }]
: []
const userMessages = (parsed.messages || []).map((m: any) => ({ ...m }))
return {
messages: [...systemMessages, ...userMessages],
tools: parsed.tools || [],
reasoningEffort: parsed.thinking?.budget_tokens != null
? String(parsed.thinking.budget_tokens)
: undefined,
}
}

if (path.startsWith('/v1/chat/completions')) {
return {
messages: (parsed.messages || []).map((m: any) => ({ ...m })),
tools: parsed.tools || [],
Expand Down
Loading