mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Allow configuring multiple AI providers
This commit is contained in:
8 files changed
+415
-84
No files matched your search
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { parseProviderModel } from './parseProviderModel';
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
|
||||
describe('parseProviderModel', () => {
|
||||
const mockProviders: Record<string, AIConnection> = {
|
||||
openai: {
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test-key',
|
||||
},
|
||||
openrouter: {
|
||||
baseURL: 'https://openrouter.ai/api/v1',
|
||||
apiKey: 'sk-or-test-key',
|
||||
},
|
||||
anthropic: {
|
||||
baseURL: 'https://api.anthropic.com',
|
||||
apiKey: 'sk-ant-test-key',
|
||||
},
|
||||
unconfigured: {
|
||||
baseURL: 'https://example.com/v1',
|
||||
apiKey: '',
|
||||
},
|
||||
};
|
||||
|
||||
it('should parse simple provider/model format', () => {
|
||||
const result = parseProviderModel('openai/gpt-4o', mockProviders);
|
||||
|
||||
expect(result.provider).toBe('openai');
|
||||
expect(result.model).toBe('gpt-4o');
|
||||
expect(result.connection).toEqual(mockProviders.openai);
|
||||
});
|
||||
|
||||
it('should parse complex model names with slashes', () => {
|
||||
const result = parseProviderModel('openrouter/anthropic/claude-sonnet-4', mockProviders);
|
||||
|
||||
expect(result.provider).toBe('openrouter');
|
||||
expect(result.model).toBe('anthropic/claude-sonnet-4');
|
||||
expect(result.connection).toEqual(mockProviders.openrouter);
|
||||
});
|
||||
|
||||
it('should handle model names with multiple slashes', () => {
|
||||
const result = parseProviderModel('openrouter/meta/llama-3.1-405b-instruct', mockProviders);
|
||||
|
||||
expect(result.provider).toBe('openrouter');
|
||||
expect(result.model).toBe('meta/llama-3.1-405b-instruct');
|
||||
expect(result.connection).toEqual(mockProviders.openrouter);
|
||||
});
|
||||
|
||||
it('should throw error for invalid format without slash', () => {
|
||||
expect(() => {
|
||||
parseProviderModel('invalidformat', mockProviders);
|
||||
}).toThrow('Invalid format. Use provider/model (e.g., "openai/gpt-4o")');
|
||||
});
|
||||
|
||||
it('should throw error for empty provider', () => {
|
||||
expect(() => {
|
||||
parseProviderModel('/gpt-4o', mockProviders);
|
||||
}).toThrow('Invalid format. Both provider and model must be specified');
|
||||
});
|
||||
|
||||
it('should throw error for empty model', () => {
|
||||
expect(() => {
|
||||
parseProviderModel('openai/', mockProviders);
|
||||
}).toThrow('Invalid format. Both provider and model must be specified');
|
||||
});
|
||||
|
||||
it('should throw error for unknown provider', () => {
|
||||
expect(() => {
|
||||
parseProviderModel('unknown/gpt-4o', mockProviders);
|
||||
}).toThrow('Provider "unknown" not found. Available providers: openai, openrouter, anthropic, unconfigured');
|
||||
});
|
||||
|
||||
it('should throw error for provider without API key', () => {
|
||||
expect(() => {
|
||||
parseProviderModel('unconfigured/some-model', mockProviders);
|
||||
}).toThrow('Provider "unconfigured" is not configured with an API key');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { AIConnection } from '@/contexts/AISettingsContext';
|
||||
|
||||
export interface ParsedProviderModel {
|
||||
provider: string;
|
||||
model: string;
|
||||
connection: AIConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a provider/model string and return the provider connection and model name
|
||||
* Format: provider/model (e.g., "openai/gpt-4o", "openrouter/anthropic/claude-sonnet-4")
|
||||
* The first slash separates the provider from the model
|
||||
*/
|
||||
export function parseProviderModel(
|
||||
providerModelString: string,
|
||||
providers: Record<string, AIConnection>
|
||||
): ParsedProviderModel {
|
||||
const firstSlashIndex = providerModelString.indexOf('/');
|
||||
|
||||
if (firstSlashIndex === -1) {
|
||||
throw new Error('Invalid format. Use provider/model (e.g., "openai/gpt-4o")');
|
||||
}
|
||||
|
||||
const provider = providerModelString.substring(0, firstSlashIndex);
|
||||
const model = providerModelString.substring(firstSlashIndex + 1);
|
||||
|
||||
if (!provider || !model) {
|
||||
throw new Error('Invalid format. Both provider and model must be specified');
|
||||
}
|
||||
|
||||
const connection = providers[provider];
|
||||
if (!connection) {
|
||||
throw new Error(`Provider "${provider}" not found. Available providers: ${Object.keys(providers).join(', ')}`);
|
||||
}
|
||||
|
||||
if (!connection.apiKey) {
|
||||
throw new Error(`Provider "${provider}" is not configured with an API key`);
|
||||
}
|
||||
|
||||
return {
|
||||
provider,
|
||||
model,
|
||||
connection,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user