mirror of
https://github.com/tiennm99/ccs.git
synced 2026-10-14 06:17:27 +00:00
feat(api): add Anthropic direct API key support
Support native Anthropic API keys (sk-ant-...) via `ccs api create --preset anthropic`. Profiles use ANTHROPIC_API_KEY env var without BASE_URL, letting Claude CLI authenticate natively with x-api-key header instead of proxy Bearer token. - Add anthropic preset to provider catalog - Branch profile-writer env structure (native vs proxy) - Add validateAnthropicKey() preflight with x-api-key header - Auto-detect api.anthropic.com URL in interactive flow - Fix hasApiKey/isConfigured to recognize ANTHROPIC_API_KEY - Update delegation-validator for native mode profiles - Add Droid target API key fallback (ANTHROPIC_API_KEY) Closes #688 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
0c4d5244c8
commit
fedb4d4cde
11 files changed
+301
-20
No files matched your search
@@ -0,0 +1,104 @@
|
||||
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 { createApiProfile } from '../../../src/api/services/profile-writer';
|
||||
|
||||
describe('profile-writer Anthropic direct', () => {
|
||||
let tempHome = '';
|
||||
let originalCcsHome: string | undefined;
|
||||
|
||||
beforeEach(() => {
|
||||
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ccs-profile-writer-'));
|
||||
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('creates native env structure for sk-ant- API key', () => {
|
||||
const result = createApiProfile(
|
||||
'anthropic-test',
|
||||
'',
|
||||
'sk-ant-api03-testkey123',
|
||||
{ default: 'claude-sonnet-4-5-20250929', opus: 'claude-opus-4-5-20251101', sonnet: 'claude-sonnet-4-5-20250929', haiku: 'claude-haiku-4-5-20251001' }
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const settingsPath = path.join(tempHome, '.ccs', 'anthropic-test.settings.json');
|
||||
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
||||
|
||||
// Native mode: ANTHROPIC_API_KEY present, no BASE_URL or AUTH_TOKEN
|
||||
expect(settings.env.ANTHROPIC_API_KEY).toBe('sk-ant-api03-testkey123');
|
||||
expect(settings.env.ANTHROPIC_BASE_URL).toBeUndefined();
|
||||
expect(settings.env.ANTHROPIC_AUTH_TOKEN).toBeUndefined();
|
||||
expect(settings.env.ANTHROPIC_MODEL).toBe('claude-sonnet-4-5-20250929');
|
||||
});
|
||||
|
||||
it('creates native env structure for api.anthropic.com URL', () => {
|
||||
const result = createApiProfile(
|
||||
'anthropic-url',
|
||||
'https://api.anthropic.com',
|
||||
'some-key-123',
|
||||
{ default: 'claude-sonnet-4-5-20250929', opus: 'claude-sonnet-4-5-20250929', sonnet: 'claude-sonnet-4-5-20250929', haiku: 'claude-sonnet-4-5-20250929' }
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const settingsPath = path.join(tempHome, '.ccs', 'anthropic-url.settings.json');
|
||||
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
||||
|
||||
expect(settings.env.ANTHROPIC_API_KEY).toBe('some-key-123');
|
||||
expect(settings.env.ANTHROPIC_BASE_URL).toBeUndefined();
|
||||
expect(settings.env.ANTHROPIC_AUTH_TOKEN).toBeUndefined();
|
||||
});
|
||||
|
||||
it('creates proxy env structure for non-Anthropic keys', () => {
|
||||
const result = createApiProfile(
|
||||
'proxy-test',
|
||||
'https://api.z.ai/api/anthropic',
|
||||
'ghp_testkey123',
|
||||
{ default: 'glm-5', opus: 'glm-5', sonnet: 'glm-5', haiku: 'glm-5' }
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const settingsPath = path.join(tempHome, '.ccs', 'proxy-test.settings.json');
|
||||
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
||||
|
||||
// Proxy mode: BASE_URL + AUTH_TOKEN, no ANTHROPIC_API_KEY
|
||||
expect(settings.env.ANTHROPIC_BASE_URL).toBe('https://api.z.ai/api/anthropic');
|
||||
expect(settings.env.ANTHROPIC_AUTH_TOKEN).toBe('ghp_testkey123');
|
||||
expect(settings.env.ANTHROPIC_API_KEY).toBeUndefined();
|
||||
});
|
||||
|
||||
it('preserves OpenRouter ANTHROPIC_API_KEY blank behavior', () => {
|
||||
const result = createApiProfile(
|
||||
'openrouter-test',
|
||||
'https://openrouter.ai/api',
|
||||
'sk-or-testkey',
|
||||
{ default: 'anthropic/claude-opus-4.5', opus: 'anthropic/claude-opus-4.5', sonnet: 'anthropic/claude-opus-4.5', haiku: 'anthropic/claude-opus-4.5' }
|
||||
);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
|
||||
const settingsPath = path.join(tempHome, '.ccs', 'openrouter-test.settings.json');
|
||||
const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
|
||||
|
||||
// OpenRouter: proxy mode with ANTHROPIC_API_KEY explicitly blank
|
||||
expect(settings.env.ANTHROPIC_BASE_URL).toBe('https://openrouter.ai/api');
|
||||
expect(settings.env.ANTHROPIC_AUTH_TOKEN).toBe('sk-or-testkey');
|
||||
expect(settings.env.ANTHROPIC_API_KEY).toBe('');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user