mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-17 02:13:53 +00:00
feat(profiles): add cliproxy api profile bridge
- add backend and CLI flows for creating routed API profiles from CLIProxy providers - add dashboard bridge CTAs, metadata, and guided create dialog mode - cover bridge routes and dialog behavior with focused tests Refs #649
This commit is contained in:
1 parent
1f284132ce
commit
287691fa04
26 files changed
+1804
-465
No files matched your search
@@ -0,0 +1,63 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'bun:test';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import { getEffectiveApiKey } from '../../../src/cliproxy/auth-token-manager';
|
||||
import {
|
||||
resolveCliproxyBridgeMetadata,
|
||||
resolveCliproxyBridgeProfile,
|
||||
suggestCliproxyBridgeName,
|
||||
} from '../../../src/api/services/cliproxy-profile-bridge';
|
||||
|
||||
describe('cliproxy-profile-bridge', () => {
|
||||
let tempHome = '';
|
||||
let originalCcsHome: string | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-cliproxy-bridge-'));
|
||||
originalCcsHome = process.env.CCS_HOME;
|
||||
process.env.CCS_HOME = tempHome;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (originalCcsHome === undefined) {
|
||||
delete process.env.CCS_HOME;
|
||||
} else {
|
||||
process.env.CCS_HOME = originalCcsHome;
|
||||
}
|
||||
|
||||
if (tempHome && fs.existsSync(tempHome)) {
|
||||
fs.rmSync(tempHome, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('resolves routed profile payload for a local CLIProxy provider', () => {
|
||||
const bridge = resolveCliproxyBridgeProfile('gemini');
|
||||
|
||||
expect(bridge.name).toBe('gemini-api');
|
||||
expect(bridge.baseUrl).toBe('http://127.0.0.1:8317/api/provider/gemini');
|
||||
expect(bridge.routePath).toBe('/api/provider/gemini');
|
||||
expect(bridge.models.default.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it('suggests a unique name when the default bridge settings file already exists', () => {
|
||||
const ccsDir = path.join(tempHome, '.ccs');
|
||||
fs.mkdirSync(ccsDir, { recursive: true });
|
||||
fs.writeFileSync(path.join(ccsDir, 'gemini-api.settings.json'), '{}\n');
|
||||
|
||||
expect(suggestCliproxyBridgeName('gemini')).toBe('gemini-api-2');
|
||||
});
|
||||
|
||||
it('detects CLIProxy-backed profile metadata and normalizes localhost loopback URLs', () => {
|
||||
const metadata = resolveCliproxyBridgeMetadata({
|
||||
env: {
|
||||
ANTHROPIC_BASE_URL: 'http://localhost:8317/api/provider/gemini',
|
||||
ANTHROPIC_AUTH_TOKEN: getEffectiveApiKey(),
|
||||
},
|
||||
});
|
||||
|
||||
expect(metadata?.provider).toBe('gemini');
|
||||
expect(metadata?.usesCurrentTarget).toBe(true);
|
||||
expect(metadata?.usesCurrentAuthToken).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -55,6 +55,14 @@ describe('api-command arg parser', () => {
|
||||
expect(parsed.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('parses --cliproxy-provider for routed API profile creation', () => {
|
||||
const parsed = parseApiCommandArgs(['my-api', '--cliproxy-provider', 'Gemini']);
|
||||
|
||||
expect(parsed.name).toBe('my-api');
|
||||
expect(parsed.cliproxyProvider).toBe('gemini');
|
||||
expect(parsed.errors).toEqual([]);
|
||||
});
|
||||
|
||||
test('validates invalid --target values', () => {
|
||||
const parsed = parseApiCommandArgs(['my-api', '--target', 'invalid-target']);
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from 'bun:test';
|
||||
import express from 'express';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
import type { Server } from 'http';
|
||||
import profileRoutes from '../../../src/web-server/routes/profile-routes';
|
||||
|
||||
describe('profile-routes cliproxy bridge', () => {
|
||||
let server: Server;
|
||||
let baseUrl = '';
|
||||
let tempHome = '';
|
||||
let originalCcsHome: string | undefined;
|
||||
|
||||
beforeAll(async () => {
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use('/api/profiles', profileRoutes);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server = app.listen(0, '127.0.0.1');
|
||||
const onError = (error: Error) => reject(error);
|
||||
server.once('error', onError);
|
||||
server.once('listening', () => {
|
||||
server.off('error', onError);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
const address = server.address();
|
||||
if (!address || typeof address === 'string') {
|
||||
throw new Error('Unable to resolve test server port');
|
||||
}
|
||||
baseUrl = `http://127.0.0.1:${address.port}`;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()));
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-profile-routes-cliproxy-bridge-'));
|
||||
originalCcsHome = process.env.CCS_HOME;
|
||||
process.env.CCS_HOME = tempHome;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (originalCcsHome !== undefined) {
|
||||
process.env.CCS_HOME = originalCcsHome;
|
||||
} else {
|
||||
delete process.env.CCS_HOME;
|
||||
}
|
||||
|
||||
if (tempHome && fs.existsSync(tempHome)) {
|
||||
fs.rmSync(tempHome, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
it('creates a routed CLIProxy-backed API profile and returns bridge metadata', async () => {
|
||||
const response = await fetch(`${baseUrl}/api/profiles/cliproxy-bridge`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ provider: 'gemini' }),
|
||||
});
|
||||
|
||||
expect(response.status).toBe(201);
|
||||
const body = (await response.json()) as {
|
||||
name: string;
|
||||
settingsPath: string;
|
||||
cliproxyBridge: { provider: string; usesCurrentTarget: boolean };
|
||||
};
|
||||
expect(body.name).toBe('gemini-api');
|
||||
expect(body.settingsPath).toBe('~/.ccs/gemini-api.settings.json');
|
||||
expect(body.cliproxyBridge.provider).toBe('gemini');
|
||||
expect(body.cliproxyBridge.usesCurrentTarget).toBe(true);
|
||||
|
||||
const settingsPath = path.join(tempHome, '.ccs', 'gemini-api.settings.json');
|
||||
expect(fs.existsSync(settingsPath)).toBe(true);
|
||||
});
|
||||
|
||||
it('auto-suggests the next routed profile name when the default bridge name is taken', async () => {
|
||||
const firstResponse = await fetch(`${baseUrl}/api/profiles/cliproxy-bridge`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ provider: 'gemini' }),
|
||||
});
|
||||
expect(firstResponse.status).toBe(201);
|
||||
|
||||
const secondResponse = await fetch(`${baseUrl}/api/profiles/cliproxy-bridge`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ provider: 'gemini' }),
|
||||
});
|
||||
|
||||
expect(secondResponse.status).toBe(201);
|
||||
const body = (await secondResponse.json()) as { name: string };
|
||||
expect(body.name).toBe('gemini-api-2');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user