mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Provider name is a required field - backfill it when parsing config
This commit is contained in:
9 files changed
+57
-50
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { AI_PROVIDER_PRESETS, getPresetProvider } from './aiProviderPresets';
|
||||
import { AI_PROVIDER_PRESETS } from './aiProviderPresets';
|
||||
|
||||
describe('AI Provider Presets', () => {
|
||||
it('should export preset providers array', () => {
|
||||
@@ -22,11 +22,11 @@ describe('AI Provider Presets', () => {
|
||||
});
|
||||
|
||||
it('should get preset provider by id', () => {
|
||||
const openai = getPresetProvider('openai');
|
||||
const openai = AI_PROVIDER_PRESETS.find(p => p.id === 'openai');
|
||||
expect(openai).toBeDefined();
|
||||
expect(openai?.name).toBe('OpenAI');
|
||||
|
||||
const nonExistent = getPresetProvider('non-existent');
|
||||
|
||||
const nonExistent = AI_PROVIDER_PRESETS.find(p => p.id === 'non-existent');
|
||||
expect(nonExistent).toBeUndefined();
|
||||
});
|
||||
|
||||
@@ -39,7 +39,7 @@ describe('AI Provider Presets', () => {
|
||||
expect(preset.baseURL).toBeDefined();
|
||||
expect(typeof preset.baseURL).toBe('string');
|
||||
expect(preset.baseURL).toMatch(/^https?:\/\//);
|
||||
|
||||
|
||||
// Optional fields
|
||||
if (preset.apiKeysURL) {
|
||||
expect(typeof preset.apiKeysURL).toBe('string');
|
||||
|
||||
@@ -89,26 +89,4 @@ export const AI_PROVIDER_PRESETS: PresetProvider[] = [
|
||||
tosURL: "https://vercel.com/legal/terms",
|
||||
proxy: true,
|
||||
},
|
||||
];
|
||||
|
||||
// Helper function to get a specific preset by ID
|
||||
export function getPresetProvider(id: string): PresetProvider | undefined {
|
||||
return AI_PROVIDER_PRESETS.find(preset => preset.id === id);
|
||||
}
|
||||
|
||||
// Helper function to get display name for a provider
|
||||
export function getProviderDisplayName(provider: { id: string; name?: string }): string {
|
||||
// If name is provided, use it
|
||||
if (provider.name) {
|
||||
return provider.name;
|
||||
}
|
||||
|
||||
// Try to find a matching preset by ID
|
||||
const preset = getPresetProvider(provider.id);
|
||||
if (preset) {
|
||||
return preset.name;
|
||||
}
|
||||
|
||||
// Fallback to ID
|
||||
return provider.id;
|
||||
}
|
||||
];
|
||||
@@ -76,6 +76,14 @@ describe('configUtils', () => {
|
||||
expect(mockFS.readFile).toHaveBeenCalledWith('/config/ai.json', 'utf8');
|
||||
expect(result).toEqual({
|
||||
...sampleAISettings,
|
||||
providers: [
|
||||
{
|
||||
id: 'openai',
|
||||
baseURL: 'https://api.openai.com/v1',
|
||||
apiKey: 'sk-test123',
|
||||
name: 'OpenAI', // Name is now filled in from presets
|
||||
},
|
||||
],
|
||||
mcpServers: {},
|
||||
});
|
||||
});
|
||||
|
||||
+25
-3
@@ -1,7 +1,8 @@
|
||||
import z from 'zod';
|
||||
import { filteredArray } from '@/lib/schema';
|
||||
import { AI_PROVIDER_PRESETS } from '@/lib/aiProviderPresets';
|
||||
import type { JSRuntimeFS } from '@/lib/JSRuntime';
|
||||
import type { AIProvider, AISettings, MCPServer } from '@/contexts/AISettingsContext';
|
||||
import type { AISettings, MCPServer } from '@/contexts/AISettingsContext';
|
||||
import type { GitSettings } from '@/contexts/GitSettingsContext';
|
||||
import type { DeployProvider, DeploySettings } from '@/contexts/DeploySettingsContext';
|
||||
|
||||
@@ -29,7 +30,7 @@ function getDeployConfigPath(configPath = '/config'): string {
|
||||
return `${configPath}/deploy.json`;
|
||||
}
|
||||
|
||||
const aiProviderSchema: z.ZodType<AIProvider> = z.object({
|
||||
const aiProviderSchema = z.object({
|
||||
id: z.string(),
|
||||
name: z.string().optional(),
|
||||
baseURL: z.string().url(),
|
||||
@@ -146,9 +147,30 @@ export async function readAISettings(fs: JSRuntimeFS, configPath = '/config'): P
|
||||
const content = await fs.readFile(aiConfigPath, 'utf8');
|
||||
const data = JSON.parse(content);
|
||||
const parsed = aiSettingsSchema.parse(data);
|
||||
// Ensure mcpServers exists even if not in parsed data
|
||||
|
||||
return {
|
||||
...parsed,
|
||||
providers: parsed.providers.map((provider) => {
|
||||
let name = provider.name;
|
||||
|
||||
// Fill in name from presets if missing
|
||||
if (typeof name !== 'string') {
|
||||
const presetProvider = AI_PROVIDER_PRESETS.find(preset => preset.id === provider.id);
|
||||
if (presetProvider) {
|
||||
name = presetProvider.name;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to id if name is still missing
|
||||
if (typeof name !== 'string') {
|
||||
name = provider.id;
|
||||
}
|
||||
|
||||
return {
|
||||
...provider,
|
||||
name,
|
||||
}
|
||||
}),
|
||||
mcpServers: parsed.mcpServers || {},
|
||||
};
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user