mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Allow setting AI Provider display name
This commit is contained in:
13 files changed
+107
-33
No files matched your search
@@ -5,7 +5,7 @@ import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { useAICredits } from '@/hooks/useAICredits';
|
||||
import { getShakespeareProvider } from '@/lib/aiProviderPresets';
|
||||
import { getPresetProvider, type PresetProvider } from '@/lib/aiProviderPresets';
|
||||
|
||||
interface Act1DialogProps {
|
||||
open: boolean;
|
||||
@@ -14,6 +14,15 @@ interface Act1DialogProps {
|
||||
|
||||
type DialogStep = 'welcome' | 'migration' | 'credits' | 'conclusion';
|
||||
|
||||
// Helper function to get the Shakespeare provider preset
|
||||
function getShakespeareProvider(): PresetProvider {
|
||||
const shakespeare = getPresetProvider("shakespeare");
|
||||
if (!shakespeare) {
|
||||
throw new Error("Shakespeare provider preset not found");
|
||||
}
|
||||
return shakespeare;
|
||||
}
|
||||
|
||||
export function Act1Dialog({ open, onOpenChange }: Act1DialogProps) {
|
||||
const [step, setStep] = useState<DialogStep>('welcome');
|
||||
const { data: creditsData } = useAICredits(getShakespeareProvider());
|
||||
|
||||
@@ -23,6 +23,7 @@ import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { AI_PROVIDER_PRESETS, type PresetProvider } from '@/lib/aiProviderPresets';
|
||||
import { cn } from '@/lib/utils';
|
||||
import { LoginArea } from '@/components/auth/LoginArea';
|
||||
import { AIProvider } from '@/contexts/AISettingsContext';
|
||||
|
||||
interface OnboardingDialogProps {
|
||||
open: boolean;
|
||||
@@ -57,6 +58,7 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
|
||||
selectedProvider && selectedProvider.nostr && user
|
||||
? {
|
||||
id: selectedProvider.id,
|
||||
name: selectedProvider.name,
|
||||
baseURL: selectedProvider.baseURL,
|
||||
nostr: selectedProvider.nostr,
|
||||
}
|
||||
@@ -122,8 +124,9 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
|
||||
}
|
||||
|
||||
// Add selected provider to their config
|
||||
const providerConfig = {
|
||||
const providerConfig: AIProvider = {
|
||||
id: selectedProvider.id,
|
||||
name: selectedProvider.name,
|
||||
baseURL: selectedProvider.baseURL,
|
||||
nostr: selectedProvider.nostr || undefined,
|
||||
apiKey: requiresApiKey ? providerApiKey.trim() : undefined,
|
||||
@@ -151,8 +154,9 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
|
||||
}
|
||||
|
||||
// Add selected provider to their config
|
||||
const providerConfig = {
|
||||
const providerConfig: AIProvider = {
|
||||
id: selectedProvider.id,
|
||||
name: selectedProvider.name,
|
||||
baseURL: selectedProvider.baseURL,
|
||||
nostr: selectedProvider.nostr || undefined,
|
||||
};
|
||||
@@ -726,6 +730,7 @@ export function OnboardingDialog({ open, onOpenChange }: OnboardingDialogProps)
|
||||
}}
|
||||
provider={{
|
||||
id: selectedProvider.id,
|
||||
name: selectedProvider.name,
|
||||
baseURL: selectedProvider.baseURL,
|
||||
nostr: selectedProvider.nostr || undefined,
|
||||
}}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createContext } from 'react';
|
||||
|
||||
export interface AIProvider {
|
||||
id: string;
|
||||
name?: string;
|
||||
baseURL: string;
|
||||
apiKey?: string;
|
||||
nostr?: boolean;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { AI_PROVIDER_PRESETS, getPresetProvider, getShakespeareProvider } from './aiProviderPresets';
|
||||
import { AI_PROVIDER_PRESETS, getPresetProvider } from './aiProviderPresets';
|
||||
|
||||
describe('AI Provider Presets', () => {
|
||||
it('should export preset providers array', () => {
|
||||
@@ -30,14 +30,6 @@ describe('AI Provider Presets', () => {
|
||||
expect(nonExistent).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should get Shakespeare provider', () => {
|
||||
const shakespeare = getShakespeareProvider();
|
||||
expect(shakespeare).toBeDefined();
|
||||
expect(shakespeare.id).toBe('shakespeare');
|
||||
expect(shakespeare.name).toBe('Shakespeare AI');
|
||||
expect(shakespeare.nostr).toBe(true);
|
||||
});
|
||||
|
||||
it('should have valid preset structure', () => {
|
||||
AI_PROVIDER_PRESETS.forEach(preset => {
|
||||
expect(preset.id).toBeDefined();
|
||||
|
||||
@@ -96,11 +96,19 @@ export function getPresetProvider(id: string): PresetProvider | undefined {
|
||||
return AI_PROVIDER_PRESETS.find(preset => preset.id === id);
|
||||
}
|
||||
|
||||
// Helper function to get the Shakespeare provider preset
|
||||
export function getShakespeareProvider(): PresetProvider {
|
||||
const shakespeare = getPresetProvider("shakespeare");
|
||||
if (!shakespeare) {
|
||||
throw new Error("Shakespeare provider preset not found");
|
||||
// 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;
|
||||
}
|
||||
return shakespeare;
|
||||
|
||||
// Try to find a matching preset by ID
|
||||
const preset = getPresetProvider(provider.id);
|
||||
if (preset) {
|
||||
return preset.name;
|
||||
}
|
||||
|
||||
// Fallback to ID
|
||||
return provider.id;
|
||||
}
|
||||
@@ -31,6 +31,7 @@ function getDeployConfigPath(configPath = '/config'): string {
|
||||
|
||||
const aiProviderSchema: z.ZodType<AIProvider> = z.object({
|
||||
id: z.string(),
|
||||
name: z.string().optional(),
|
||||
baseURL: z.string().url(),
|
||||
apiKey: z.string().optional(),
|
||||
nostr: z.boolean().optional(),
|
||||
|
||||
@@ -91,6 +91,7 @@
|
||||
"remove": "Remove",
|
||||
"retry": "Retry",
|
||||
"name": "Name",
|
||||
"id": "ID",
|
||||
"email": "Email",
|
||||
"buildNostrApps": "Build apps with AI",
|
||||
"whatToBuild": "What would you like to build?",
|
||||
|
||||
@@ -87,6 +87,7 @@
|
||||
"remove": "Cire",
|
||||
"retry": "Sake gwadawa",
|
||||
"name": "Suna",
|
||||
"id": "ID",
|
||||
"email": "Imel",
|
||||
"buildNostrApps": "Gina aikace-aikace da AI",
|
||||
"whatToBuild": "Me kuke son ginawa?",
|
||||
|
||||
@@ -87,6 +87,7 @@
|
||||
"remove": "Wepụ",
|
||||
"retry": "Nwaa ọzọ",
|
||||
"name": "Aha",
|
||||
"id": "ID",
|
||||
"email": "Ozi-eletrọniki",
|
||||
"buildNostrApps": "Wuo ngwa na AI",
|
||||
"whatToBuild": "Gịnị ka ị chọrọ iwu?",
|
||||
|
||||
@@ -87,6 +87,7 @@
|
||||
"remove": "Remover",
|
||||
"retry": "Tentar novamente",
|
||||
"name": "Nome",
|
||||
"id": "ID",
|
||||
"email": "Email",
|
||||
"buildNostrApps": "Construa aplicativos com IA",
|
||||
"whatToBuild": "O que você gostaria de construir?",
|
||||
|
||||
@@ -87,6 +87,7 @@
|
||||
"remove": "Yọ kuro",
|
||||
"retry": "Tun gbiyanju",
|
||||
"name": "Orukọ",
|
||||
"id": "ID",
|
||||
"email": "Imeeli",
|
||||
"buildNostrApps": "Kọ awọn ohun elo pẹlu AI",
|
||||
"whatToBuild": "Kini o fẹ kọ?",
|
||||
|
||||
@@ -87,6 +87,7 @@
|
||||
"remove": "移除",
|
||||
"retry": "重试",
|
||||
"name": "名称",
|
||||
"id": "ID",
|
||||
"email": "邮箱",
|
||||
"buildNostrApps": "使用AI构建应用",
|
||||
"whatToBuild": "您想构建什么?",
|
||||
|
||||
+67
-15
@@ -38,7 +38,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser';
|
||||
import { useAppContext } from '@/hooks/useAppContext';
|
||||
import { useNavigate, Link } from 'react-router-dom';
|
||||
import type { AIProvider } from '@/contexts/AISettingsContext';
|
||||
import { AI_PROVIDER_PRESETS, type PresetProvider } from '@/lib/aiProviderPresets';
|
||||
import { AI_PROVIDER_PRESETS, type PresetProvider, getProviderDisplayName } from '@/lib/aiProviderPresets';
|
||||
import { ExternalFavicon } from '@/components/ExternalFavicon';
|
||||
import { MCPServersSection } from '@/components/MCPServersSection';
|
||||
import { PluginsSection } from '@/components/PluginsSection';
|
||||
@@ -99,7 +99,7 @@ function SortableProviderItem({ provider, preset, onRemove, onSetProvider, onOpe
|
||||
<Bot size={16} />
|
||||
)}
|
||||
<span className="font-medium">
|
||||
{preset?.name || provider.id}
|
||||
{getProviderDisplayName(provider)}
|
||||
</span>
|
||||
<div className="ml-auto">
|
||||
<CreditsBadge
|
||||
@@ -111,6 +111,15 @@ function SortableProviderItem({ provider, preset, onRemove, onSetProvider, onOpe
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${provider.id}-name`}>{t('name')}</Label>
|
||||
<Input
|
||||
id={`${provider.id}-name`}
|
||||
placeholder={preset?.name || provider.id}
|
||||
value={provider.name || ''}
|
||||
onChange={(e) => onSetProvider({ ...provider, name: e.target.value || undefined })}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor={`${provider.id}-baseURL`}>{t('baseUrl')}</Label>
|
||||
<Input
|
||||
@@ -177,6 +186,14 @@ function SortableProviderItem({ provider, preset, onRemove, onSetProvider, onOpe
|
||||
);
|
||||
}
|
||||
|
||||
// Helper function to generate ID from name
|
||||
function generateIdFromName(name: string): string {
|
||||
return name
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-') // Replace non-alphanumeric chars with hyphens
|
||||
.replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
|
||||
}
|
||||
|
||||
export function AISettings() {
|
||||
const { t } = useTranslation();
|
||||
const { settings, setProvider, removeProvider, setProviders } = useAISettings();
|
||||
@@ -185,6 +202,8 @@ export function AISettings() {
|
||||
const { user } = useCurrentUser();
|
||||
const { config, defaultConfig, updateConfig } = useAppContext();
|
||||
const [customProviderName, setCustomProviderName] = useState('');
|
||||
const [customProviderId, setCustomProviderId] = useState('');
|
||||
const [customIdManuallyEdited, setCustomIdManuallyEdited] = useState(false);
|
||||
const [customBaseURL, setCustomBaseURL] = useState('');
|
||||
const [customApiKey, setCustomApiKey] = useState('');
|
||||
const [customAuthMethod, setCustomAuthMethod] = useState<'api-key' | 'nostr'>('api-key');
|
||||
@@ -224,6 +243,7 @@ export function AISettings() {
|
||||
|
||||
const newProvider: AIProvider = {
|
||||
id: preset.id,
|
||||
name: preset.name,
|
||||
baseURL: preset.baseURL,
|
||||
};
|
||||
|
||||
@@ -252,10 +272,11 @@ export function AISettings() {
|
||||
};
|
||||
|
||||
const handleAddCustomProvider = () => {
|
||||
if (!customProviderName.trim() || !customBaseURL.trim()) return;
|
||||
if (!customProviderName.trim() || !customProviderId.trim() || !customBaseURL.trim()) return;
|
||||
|
||||
const newProvider: AIProvider = {
|
||||
id: customProviderName.trim(),
|
||||
id: customProviderId.trim(),
|
||||
name: customProviderName.trim(),
|
||||
baseURL: customBaseURL.trim(),
|
||||
apiKey: customAuthMethod === 'nostr' ? undefined : customApiKey.trim(),
|
||||
nostr: customAuthMethod === 'nostr' || undefined,
|
||||
@@ -266,6 +287,8 @@ export function AISettings() {
|
||||
setProvider(newProvider);
|
||||
|
||||
setCustomProviderName('');
|
||||
setCustomProviderId('');
|
||||
setCustomIdManuallyEdited(false);
|
||||
setCustomBaseURL('');
|
||||
setCustomApiKey('');
|
||||
setCustomAuthMethod('api-key');
|
||||
@@ -510,17 +533,45 @@ export function AISettings() {
|
||||
</AccordionTrigger>
|
||||
<AccordionContent className="px-4 pb-4">
|
||||
<div className="space-y-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">{t('providerName')}</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="e.g., my-custom-api"
|
||||
value={customProviderName}
|
||||
onChange={(e) => setCustomProviderName(e.target.value)}
|
||||
/>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-name">
|
||||
{t('name')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-name"
|
||||
placeholder="e.g., My Custom API"
|
||||
value={customProviderName}
|
||||
onChange={(e) => {
|
||||
const newName = e.target.value;
|
||||
setCustomProviderName(newName);
|
||||
|
||||
// Auto-generate ID from name if ID hasn't been manually edited
|
||||
if (!customIdManuallyEdited) {
|
||||
setCustomProviderId(generateIdFromName(newName));
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-id">
|
||||
{t('id')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-id"
|
||||
placeholder="e.g., my-custom-api"
|
||||
value={customProviderId}
|
||||
onChange={(e) => {
|
||||
setCustomProviderId(e.target.value);
|
||||
setCustomIdManuallyEdited(true);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="grid gap-2">
|
||||
<Label htmlFor="custom-baseurl">{t('baseUrl')}</Label>
|
||||
<Label htmlFor="custom-baseurl">
|
||||
{t('baseUrl')} <span className="text-destructive">*</span>
|
||||
</Label>
|
||||
<Input
|
||||
id="custom-baseurl"
|
||||
placeholder="https://api.example.com/v1"
|
||||
@@ -573,15 +624,16 @@ export function AISettings() {
|
||||
onClick={handleAddCustomProvider}
|
||||
disabled={
|
||||
!customProviderName.trim() ||
|
||||
!customProviderId.trim() ||
|
||||
!customBaseURL.trim() ||
|
||||
settings.providers.some(p => p.id === customProviderName.trim())
|
||||
settings.providers.some(p => p.id === customProviderId.trim())
|
||||
}
|
||||
className="gap-2 ml-auto"
|
||||
>
|
||||
<Check className="h-4 w-4" />
|
||||
{t('addCustomProviderButton')}
|
||||
</Button>
|
||||
{settings.providers.some(p => p.id === customProviderName.trim()) && (
|
||||
{settings.providers.some(p => p.id === customProviderId.trim()) && (
|
||||
<p className="text-sm text-destructive">
|
||||
{t('providerExists')}
|
||||
</p>
|
||||
|
||||
Reference in new issue
Block a user