feat(proxy): add adaptive local port selection for OpenAI-compatible profiles

This commit is contained in:
Tam Nhu Tran committed 2026-04-23 14:47:01 -04:00
1 parent 5ddf130223
commit 15751e2db5
11 files changed
+337 -103

No files matched your search

+83 -63
View File
@@ -21,6 +21,27 @@ function runCli(args: string[], extraEnv: Record<string, string> = {}) {
});
}
function writeJson(filePath: string, data: unknown) {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
}
function createProfileConfig(profiles: Record<string, Record<string, string>>) {
const ccsDir = path.join(tempDir, '.ccs');
fs.mkdirSync(ccsDir, { recursive: true });
const configProfiles = Object.fromEntries(
Object.keys(profiles).map((name) => [name, path.join(ccsDir, `${name}.settings.json`)])
);
writeJson(path.join(ccsDir, 'config.json'), { profiles: configProfiles });
for (const [name, env] of Object.entries(profiles)) {
writeJson(configProfiles[name], { env });
}
}
function getRunningPort(statusOutput: string) {
const match = statusOutput.match(/Local URL: http:\/\/127\.0\.0\.1:(\d+)/);
expect(match).not.toBeNull();
return Number(match?.[1]);
}
beforeAll(() => {
if (process.env.CCS_E2E_SKIP_BUILD === '1') {
expect(fs.existsSync(DIST_ENTRY)).toBe(true);
@@ -54,6 +75,8 @@ describe('proxy command e2e', () => {
expect(help.status).toBe(0);
expect(help.stdout).toContain('Usage: ccs proxy <start|stop|status|activate> [profile] [options]');
expect(help.stdout).toContain('stop [profile] Stop the running proxy (or all proxies when omitted)');
expect(help.stdout).toContain('Pin an exact local proxy port');
expect(help.stdout).not.toContain('default: 3456');
});
it('shows the last-known proxy state when no proxy is currently running', async () => {
@@ -84,45 +107,29 @@ describe('proxy command e2e', () => {
expect(status.stdout).toContain('Profile: stale');
});
it('starts, reports status, activates, and stops via the built CLI', async () => {
const port = await getPort();
const ccsDir = path.join(tempDir, '.ccs');
fs.mkdirSync(ccsDir, { recursive: true });
const settingsPath = path.join(ccsDir, 'hf.settings.json');
fs.writeFileSync(
path.join(ccsDir, 'config.json'),
JSON.stringify({ profiles: { hf: settingsPath } }, null, 2),
'utf8'
);
fs.writeFileSync(
settingsPath,
JSON.stringify({
env: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434',
ANTHROPIC_AUTH_TOKEN: 'ollama',
ANTHROPIC_MODEL: 'qwen3-coder',
CCS_DROID_PROVIDER: 'generic-chat-completion-api',
},
}),
'utf8'
);
const started = runCli(['proxy', 'start', 'hf', '--port', String(port), '--host', '127.0.0.1']);
expect(started.status).toBe(0);
const status = runCli(['proxy', 'status']);
it('surfaces the actual adaptive port in status and activation output', async () => {
createProfileConfig({
hf: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434',
ANTHROPIC_AUTH_TOKEN: 'ollama',
ANTHROPIC_MODEL: 'qwen3-coder',
CCS_DROID_PROVIDER: 'generic-chat-completion-api',
},
});
expect(runCli(['proxy', 'start', 'hf', '--host', '127.0.0.1']).status).toBe(0);
const status = runCli(['proxy', 'status', 'hf']);
expect(status.status).toBe(0);
const port = getRunningPort(status.stdout);
expect(status.stdout).toContain(`Proxy running on port ${port}`);
expect(status.stdout).toContain('Host: 127.0.0.1');
expect(status.stdout).toContain('Profile: hf');
const activate = runCli(['proxy', 'activate', '--shell', 'bash']);
const activate = runCli(['proxy', 'activate', 'hf', '--shell', 'bash']);
expect(activate.stdout).toContain(`export ANTHROPIC_BASE_URL='http://127.0.0.1:${port}'`);
expect(activate.stdout).toMatch(/export ANTHROPIC_AUTH_TOKEN='[a-f0-9]{48}'/);
expect(activate.stdout).toContain("export DISABLE_TELEMETRY='1'");
expect(activate.stdout).toContain("export DISABLE_COST_WARNINGS='1'");
expect(activate.stdout).toContain("export API_TIMEOUT_MS='600000'");
expect(activate.stdout).toContain("export NO_PROXY='127.0.0.1,localhost'");
const activateFish = runCli(['proxy', 'activate', '--fish']);
expect(activateFish.stdout).toContain(`set -gx ANTHROPIC_BASE_URL 'http://127.0.0.1:${port}'`);
@@ -147,41 +154,54 @@ describe('proxy command e2e', () => {
expect(stopped.status).toBe(0);
}, 35000);
it('respects an explicit --port override in status output', async () => {
const port = await getPort();
createProfileConfig({
hf: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434',
ANTHROPIC_AUTH_TOKEN: 'ollama',
ANTHROPIC_MODEL: 'qwen3-coder',
CCS_DROID_PROVIDER: 'generic-chat-completion-api',
},
});
const started = runCli(['proxy', 'start', 'hf', '--port', String(port), '--host', '127.0.0.1']);
expect(started.status).toBe(0);
const status = runCli(['proxy', 'status', 'hf']);
expect(status.status).toBe(0);
expect(status.stdout).toContain(`Proxy running on port ${port}`);
expect(status.stdout).toContain(`Local URL: http://127.0.0.1:${port}`);
}, 35000);
it('rejects malformed explicit port values instead of coercing numeric prefixes', () => {
createProfileConfig({
hf: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434',
ANTHROPIC_AUTH_TOKEN: 'ollama',
ANTHROPIC_MODEL: 'qwen3-coder',
CCS_DROID_PROVIDER: 'generic-chat-completion-api',
},
});
const invalid = runCli(['proxy', 'start', 'hf', '--port', '3456junk']);
expect(invalid.status).toBe(1);
expect(invalid.stderr).toContain('Invalid port: 3456junk');
});
it('requires an explicit profile when activating with multiple running proxies', async () => {
const firstPort = await getPort();
const ccsDir = path.join(tempDir, '.ccs');
fs.mkdirSync(ccsDir, { recursive: true });
const firstSettingsPath = path.join(ccsDir, 'ccg.settings.json');
const secondSettingsPath = path.join(ccsDir, 'ccgm.settings.json');
fs.writeFileSync(
path.join(ccsDir, 'config.json'),
JSON.stringify({ profiles: { ccg: firstSettingsPath, ccgm: secondSettingsPath } }, null, 2),
'utf8'
);
fs.writeFileSync(
firstSettingsPath,
JSON.stringify({
env: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434',
ANTHROPIC_AUTH_TOKEN: 'ollama-ccg',
ANTHROPIC_MODEL: 'qwen3-coder',
CCS_DROID_PROVIDER: 'generic-chat-completion-api',
},
}),
'utf8'
);
fs.writeFileSync(
secondSettingsPath,
JSON.stringify({
env: {
ANTHROPIC_BASE_URL: 'https://api.openai.com/v1',
ANTHROPIC_AUTH_TOKEN: 'sk-ccgm',
ANTHROPIC_MODEL: 'gpt-4.1',
},
}),
'utf8'
);
createProfileConfig({
ccg: {
ANTHROPIC_BASE_URL: 'http://127.0.0.1:11434',
ANTHROPIC_AUTH_TOKEN: 'ollama-ccg',
ANTHROPIC_MODEL: 'qwen3-coder',
CCS_DROID_PROVIDER: 'generic-chat-completion-api',
},
ccgm: {
ANTHROPIC_BASE_URL: 'https://api.openai.com/v1',
ANTHROPIC_AUTH_TOKEN: 'sk-ccgm',
ANTHROPIC_MODEL: 'gpt-4.1',
},
});
expect(runCli(['proxy', 'start', 'ccg', '--port', String(firstPort)]).status).toBe(0);
const secondPort = await getPort();
expect(runCli(['proxy', 'start', 'ccgm', '--port', String(secondPort)]).status).toBe(0);