mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-02 02:40:50 +08:00
feat: replace AI provider modals with dedicated edit pages
This commit is contained in:
84
src/components/common/SecondaryScreenShell.module.scss
Normal file
84
src/components/common/SecondaryScreenShell.module.scss
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
@use '../../styles/variables' as *;
|
||||||
|
|
||||||
|
.container {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: $spacing-lg;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.topBar {
|
||||||
|
position: sticky;
|
||||||
|
top: 0;
|
||||||
|
z-index: 5;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 1fr auto 1fr;
|
||||||
|
align-items: center;
|
||||||
|
gap: $spacing-md;
|
||||||
|
padding: $spacing-sm $spacing-md;
|
||||||
|
background: var(--bg-secondary);
|
||||||
|
border-bottom: 1px solid var(--border-color);
|
||||||
|
min-height: 44px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.topBarTitle {
|
||||||
|
min-width: 0;
|
||||||
|
text-align: center;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: 650;
|
||||||
|
color: var(--text-primary);
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
justify-self: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.backButton {
|
||||||
|
padding-left: 6px;
|
||||||
|
padding-right: 10px;
|
||||||
|
justify-self: start;
|
||||||
|
gap: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.backButton > span:last-child {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.backIcon {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
|
||||||
|
svg {
|
||||||
|
display: block;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.backText {
|
||||||
|
font-weight: 600;
|
||||||
|
line-height: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.rightSlot {
|
||||||
|
justify-self: end;
|
||||||
|
display: flex;
|
||||||
|
justify-content: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.loadingState {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: $spacing-sm;
|
||||||
|
padding: $spacing-2xl 0;
|
||||||
|
color: var(--text-secondary);
|
||||||
|
}
|
||||||
|
|
||||||
|
.content {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: $spacing-lg;
|
||||||
|
}
|
||||||
|
|
||||||
78
src/components/common/SecondaryScreenShell.tsx
Normal file
78
src/components/common/SecondaryScreenShell.tsx
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import { forwardRef, type ReactNode } from 'react';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
||||||
|
import { IconChevronLeft } from '@/components/ui/icons';
|
||||||
|
import styles from './SecondaryScreenShell.module.scss';
|
||||||
|
|
||||||
|
export type SecondaryScreenShellProps = {
|
||||||
|
title: ReactNode;
|
||||||
|
onBack?: () => void;
|
||||||
|
backLabel?: string;
|
||||||
|
backAriaLabel?: string;
|
||||||
|
rightAction?: ReactNode;
|
||||||
|
isLoading?: boolean;
|
||||||
|
loadingLabel?: ReactNode;
|
||||||
|
className?: string;
|
||||||
|
contentClassName?: string;
|
||||||
|
children?: ReactNode;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const SecondaryScreenShell = forwardRef<HTMLDivElement, SecondaryScreenShellProps>(
|
||||||
|
function SecondaryScreenShell(
|
||||||
|
{
|
||||||
|
title,
|
||||||
|
onBack,
|
||||||
|
backLabel = 'Back',
|
||||||
|
backAriaLabel,
|
||||||
|
rightAction,
|
||||||
|
isLoading = false,
|
||||||
|
loadingLabel = 'Loading...',
|
||||||
|
className = '',
|
||||||
|
contentClassName = '',
|
||||||
|
children,
|
||||||
|
},
|
||||||
|
ref
|
||||||
|
) {
|
||||||
|
const containerClassName = [styles.container, className].filter(Boolean).join(' ');
|
||||||
|
const contentClasses = [styles.content, contentClassName].filter(Boolean).join(' ');
|
||||||
|
const titleTooltip = typeof title === 'string' ? title : undefined;
|
||||||
|
const resolvedBackAriaLabel = backAriaLabel ?? backLabel;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={containerClassName} ref={ref}>
|
||||||
|
<div className={styles.topBar}>
|
||||||
|
{onBack ? (
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={onBack}
|
||||||
|
className={styles.backButton}
|
||||||
|
aria-label={resolvedBackAriaLabel}
|
||||||
|
>
|
||||||
|
<span className={styles.backIcon}>
|
||||||
|
<IconChevronLeft size={18} />
|
||||||
|
</span>
|
||||||
|
<span className={styles.backText}>{backLabel}</span>
|
||||||
|
</Button>
|
||||||
|
) : (
|
||||||
|
<div />
|
||||||
|
)}
|
||||||
|
<div className={styles.topBarTitle} title={titleTooltip}>
|
||||||
|
{title}
|
||||||
|
</div>
|
||||||
|
<div className={styles.rightSlot}>{rightAction}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{isLoading ? (
|
||||||
|
<div className={styles.loadingState}>
|
||||||
|
<LoadingSpinner size={16} />
|
||||||
|
<span>{loadingLabel}</span>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className={contentClasses}>{children}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
@@ -376,6 +376,20 @@ export function MainLayout() {
|
|||||||
pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
|
pathname.length > 1 && pathname.endsWith('/') ? pathname.slice(0, -1) : pathname;
|
||||||
const normalizedPath = trimmedPath === '/dashboard' ? '/' : trimmedPath;
|
const normalizedPath = trimmedPath === '/dashboard' ? '/' : trimmedPath;
|
||||||
|
|
||||||
|
const aiProvidersIndex = navOrder.indexOf('/ai-providers');
|
||||||
|
if (aiProvidersIndex !== -1) {
|
||||||
|
if (normalizedPath === '/ai-providers') return aiProvidersIndex;
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/')) {
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/gemini')) return aiProvidersIndex + 0.1;
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/codex')) return aiProvidersIndex + 0.2;
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/claude')) return aiProvidersIndex + 0.3;
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/vertex')) return aiProvidersIndex + 0.4;
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/ampcode')) return aiProvidersIndex + 0.5;
|
||||||
|
if (normalizedPath.startsWith('/ai-providers/openai')) return aiProvidersIndex + 0.6;
|
||||||
|
return aiProvidersIndex + 0.05;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const authFilesIndex = navOrder.indexOf('/auth-files');
|
const authFilesIndex = navOrder.indexOf('/auth-files');
|
||||||
if (authFilesIndex !== -1) {
|
if (authFilesIndex !== -1) {
|
||||||
if (normalizedPath === '/auth-files') return authFilesIndex;
|
if (normalizedPath === '/auth-files') return authFilesIndex;
|
||||||
@@ -405,7 +419,11 @@ export function MainLayout() {
|
|||||||
const to = normalize(toPathname);
|
const to = normalize(toPathname);
|
||||||
const isAuthFiles = (pathname: string) =>
|
const isAuthFiles = (pathname: string) =>
|
||||||
pathname === '/auth-files' || pathname.startsWith('/auth-files/');
|
pathname === '/auth-files' || pathname.startsWith('/auth-files/');
|
||||||
return isAuthFiles(from) && isAuthFiles(to) ? 'ios' : 'vertical';
|
const isAiProviders = (pathname: string) =>
|
||||||
|
pathname === '/ai-providers' || pathname.startsWith('/ai-providers/');
|
||||||
|
if (isAuthFiles(from) && isAuthFiles(to)) return 'ios';
|
||||||
|
if (isAiProviders(from) && isAiProviders(to)) return 'ios';
|
||||||
|
return 'vertical';
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const handleRefreshAll = async () => {
|
const handleRefreshAll = async () => {
|
||||||
|
|||||||
@@ -5,32 +5,21 @@ import type { AmpcodeConfig } from '@/types';
|
|||||||
import { maskApiKey } from '@/utils/format';
|
import { maskApiKey } from '@/utils/format';
|
||||||
import styles from '@/pages/AiProvidersPage.module.scss';
|
import styles from '@/pages/AiProvidersPage.module.scss';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { AmpcodeModal } from './AmpcodeModal';
|
|
||||||
|
|
||||||
interface AmpcodeSectionProps {
|
interface AmpcodeSectionProps {
|
||||||
config: AmpcodeConfig | null | undefined;
|
config: AmpcodeConfig | null | undefined;
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
disableControls: boolean;
|
disableControls: boolean;
|
||||||
isSaving: boolean;
|
|
||||||
isSwitching: boolean;
|
isSwitching: boolean;
|
||||||
isBusy: boolean;
|
onEdit: () => void;
|
||||||
isModalOpen: boolean;
|
|
||||||
onOpen: () => void;
|
|
||||||
onCloseModal: () => void;
|
|
||||||
onBusyChange: (busy: boolean) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function AmpcodeSection({
|
export function AmpcodeSection({
|
||||||
config,
|
config,
|
||||||
loading,
|
loading,
|
||||||
disableControls,
|
disableControls,
|
||||||
isSaving,
|
|
||||||
isSwitching,
|
isSwitching,
|
||||||
isBusy,
|
onEdit,
|
||||||
isModalOpen,
|
|
||||||
onOpen,
|
|
||||||
onCloseModal,
|
|
||||||
onBusyChange,
|
|
||||||
}: AmpcodeSectionProps) {
|
}: AmpcodeSectionProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
@@ -46,8 +35,8 @@ export function AmpcodeSection({
|
|||||||
extra={
|
extra={
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
onClick={onOpen}
|
onClick={onEdit}
|
||||||
disabled={disableControls || isSaving || isBusy || isSwitching}
|
disabled={disableControls || loading || isSwitching}
|
||||||
>
|
>
|
||||||
{t('common.edit')}
|
{t('common.edit')}
|
||||||
</Button>
|
</Button>
|
||||||
@@ -99,13 +88,6 @@ export function AmpcodeSection({
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<AmpcodeModal
|
|
||||||
isOpen={isModalOpen}
|
|
||||||
disableControls={disableControls}
|
|
||||||
onClose={onCloseModal}
|
|
||||||
onBusyChange={onBusyChange}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,8 +16,6 @@ import styles from '@/pages/AiProvidersPage.module.scss';
|
|||||||
import { ProviderList } from '../ProviderList';
|
import { ProviderList } from '../ProviderList';
|
||||||
import { ProviderStatusBar } from '../ProviderStatusBar';
|
import { ProviderStatusBar } from '../ProviderStatusBar';
|
||||||
import { getStatsBySource, hasDisableAllModelsRule } from '../utils';
|
import { getStatsBySource, hasDisableAllModelsRule } from '../utils';
|
||||||
import type { ProviderFormState } from '../types';
|
|
||||||
import { ClaudeModal } from './ClaudeModal';
|
|
||||||
|
|
||||||
interface ClaudeSectionProps {
|
interface ClaudeSectionProps {
|
||||||
configs: ProviderKeyConfig[];
|
configs: ProviderKeyConfig[];
|
||||||
@@ -25,16 +23,11 @@ interface ClaudeSectionProps {
|
|||||||
usageDetails: UsageDetail[];
|
usageDetails: UsageDetail[];
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
disableControls: boolean;
|
disableControls: boolean;
|
||||||
isSaving: boolean;
|
|
||||||
isSwitching: boolean;
|
isSwitching: boolean;
|
||||||
isModalOpen: boolean;
|
|
||||||
modalIndex: number | null;
|
|
||||||
onAdd: () => void;
|
onAdd: () => void;
|
||||||
onEdit: (index: number) => void;
|
onEdit: (index: number) => void;
|
||||||
onDelete: (index: number) => void;
|
onDelete: (index: number) => void;
|
||||||
onToggle: (index: number, enabled: boolean) => void;
|
onToggle: (index: number, enabled: boolean) => void;
|
||||||
onCloseModal: () => void;
|
|
||||||
onSave: (data: ProviderFormState, index: number | null) => Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ClaudeSection({
|
export function ClaudeSection({
|
||||||
@@ -43,20 +36,15 @@ export function ClaudeSection({
|
|||||||
usageDetails,
|
usageDetails,
|
||||||
loading,
|
loading,
|
||||||
disableControls,
|
disableControls,
|
||||||
isSaving,
|
|
||||||
isSwitching,
|
isSwitching,
|
||||||
isModalOpen,
|
|
||||||
modalIndex,
|
|
||||||
onAdd,
|
onAdd,
|
||||||
onEdit,
|
onEdit,
|
||||||
onDelete,
|
onDelete,
|
||||||
onToggle,
|
onToggle,
|
||||||
onCloseModal,
|
|
||||||
onSave,
|
|
||||||
}: ClaudeSectionProps) {
|
}: ClaudeSectionProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const actionsDisabled = disableControls || isSaving || isSwitching;
|
const actionsDisabled = disableControls || loading || isSwitching;
|
||||||
const toggleDisabled = disableControls || loading || isSaving || isSwitching;
|
const toggleDisabled = disableControls || loading || isSwitching;
|
||||||
|
|
||||||
const statusBarCache = useMemo(() => {
|
const statusBarCache = useMemo(() => {
|
||||||
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
||||||
@@ -76,8 +64,6 @@ export function ClaudeSection({
|
|||||||
return cache;
|
return cache;
|
||||||
}, [configs, usageDetails]);
|
}, [configs, usageDetails]);
|
||||||
|
|
||||||
const initialData = modalIndex !== null ? configs[modalIndex] : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card
|
<Card
|
||||||
@@ -200,15 +186,6 @@ export function ClaudeSection({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<ClaudeModal
|
|
||||||
isOpen={isModalOpen}
|
|
||||||
editIndex={modalIndex}
|
|
||||||
initialData={initialData}
|
|
||||||
onClose={onCloseModal}
|
|
||||||
onSave={onSave}
|
|
||||||
isSaving={isSaving}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import styles from '@/pages/AiProvidersPage.module.scss';
|
|||||||
import { ProviderList } from '../ProviderList';
|
import { ProviderList } from '../ProviderList';
|
||||||
import { ProviderStatusBar } from '../ProviderStatusBar';
|
import { ProviderStatusBar } from '../ProviderStatusBar';
|
||||||
import { getStatsBySource, hasDisableAllModelsRule } from '../utils';
|
import { getStatsBySource, hasDisableAllModelsRule } from '../utils';
|
||||||
import type { ProviderFormState } from '../types';
|
|
||||||
import { CodexModal } from './CodexModal';
|
|
||||||
|
|
||||||
interface CodexSectionProps {
|
interface CodexSectionProps {
|
||||||
configs: ProviderKeyConfig[];
|
configs: ProviderKeyConfig[];
|
||||||
@@ -26,17 +24,12 @@ interface CodexSectionProps {
|
|||||||
usageDetails: UsageDetail[];
|
usageDetails: UsageDetail[];
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
disableControls: boolean;
|
disableControls: boolean;
|
||||||
isSaving: boolean;
|
|
||||||
isSwitching: boolean;
|
isSwitching: boolean;
|
||||||
resolvedTheme: string;
|
resolvedTheme: string;
|
||||||
isModalOpen: boolean;
|
|
||||||
modalIndex: number | null;
|
|
||||||
onAdd: () => void;
|
onAdd: () => void;
|
||||||
onEdit: (index: number) => void;
|
onEdit: (index: number) => void;
|
||||||
onDelete: (index: number) => void;
|
onDelete: (index: number) => void;
|
||||||
onToggle: (index: number, enabled: boolean) => void;
|
onToggle: (index: number, enabled: boolean) => void;
|
||||||
onCloseModal: () => void;
|
|
||||||
onSave: (data: ProviderFormState, index: number | null) => Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CodexSection({
|
export function CodexSection({
|
||||||
@@ -45,21 +38,16 @@ export function CodexSection({
|
|||||||
usageDetails,
|
usageDetails,
|
||||||
loading,
|
loading,
|
||||||
disableControls,
|
disableControls,
|
||||||
isSaving,
|
|
||||||
isSwitching,
|
isSwitching,
|
||||||
resolvedTheme,
|
resolvedTheme,
|
||||||
isModalOpen,
|
|
||||||
modalIndex,
|
|
||||||
onAdd,
|
onAdd,
|
||||||
onEdit,
|
onEdit,
|
||||||
onDelete,
|
onDelete,
|
||||||
onToggle,
|
onToggle,
|
||||||
onCloseModal,
|
|
||||||
onSave,
|
|
||||||
}: CodexSectionProps) {
|
}: CodexSectionProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const actionsDisabled = disableControls || isSaving || isSwitching;
|
const actionsDisabled = disableControls || loading || isSwitching;
|
||||||
const toggleDisabled = disableControls || loading || isSaving || isSwitching;
|
const toggleDisabled = disableControls || loading || isSwitching;
|
||||||
|
|
||||||
const statusBarCache = useMemo(() => {
|
const statusBarCache = useMemo(() => {
|
||||||
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
||||||
@@ -79,8 +67,6 @@ export function CodexSection({
|
|||||||
return cache;
|
return cache;
|
||||||
}, [configs, usageDetails]);
|
}, [configs, usageDetails]);
|
||||||
|
|
||||||
const initialData = modalIndex !== null ? configs[modalIndex] : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card
|
<Card
|
||||||
@@ -192,15 +178,6 @@ export function CodexSection({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<CodexModal
|
|
||||||
isOpen={isModalOpen}
|
|
||||||
editIndex={modalIndex}
|
|
||||||
initialData={initialData}
|
|
||||||
onClose={onCloseModal}
|
|
||||||
onSave={onSave}
|
|
||||||
isSaving={isSaving}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,11 +13,9 @@ import {
|
|||||||
type UsageDetail,
|
type UsageDetail,
|
||||||
} from '@/utils/usage';
|
} from '@/utils/usage';
|
||||||
import styles from '@/pages/AiProvidersPage.module.scss';
|
import styles from '@/pages/AiProvidersPage.module.scss';
|
||||||
import type { GeminiFormState } from '../types';
|
|
||||||
import { ProviderList } from '../ProviderList';
|
import { ProviderList } from '../ProviderList';
|
||||||
import { ProviderStatusBar } from '../ProviderStatusBar';
|
import { ProviderStatusBar } from '../ProviderStatusBar';
|
||||||
import { getStatsBySource, hasDisableAllModelsRule } from '../utils';
|
import { getStatsBySource, hasDisableAllModelsRule } from '../utils';
|
||||||
import { GeminiModal } from './GeminiModal';
|
|
||||||
|
|
||||||
interface GeminiSectionProps {
|
interface GeminiSectionProps {
|
||||||
configs: GeminiKeyConfig[];
|
configs: GeminiKeyConfig[];
|
||||||
@@ -25,16 +23,11 @@ interface GeminiSectionProps {
|
|||||||
usageDetails: UsageDetail[];
|
usageDetails: UsageDetail[];
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
disableControls: boolean;
|
disableControls: boolean;
|
||||||
isSaving: boolean;
|
|
||||||
isSwitching: boolean;
|
isSwitching: boolean;
|
||||||
isModalOpen: boolean;
|
|
||||||
modalIndex: number | null;
|
|
||||||
onAdd: () => void;
|
onAdd: () => void;
|
||||||
onEdit: (index: number) => void;
|
onEdit: (index: number) => void;
|
||||||
onDelete: (index: number) => void;
|
onDelete: (index: number) => void;
|
||||||
onToggle: (index: number, enabled: boolean) => void;
|
onToggle: (index: number, enabled: boolean) => void;
|
||||||
onCloseModal: () => void;
|
|
||||||
onSave: (data: GeminiFormState, index: number | null) => Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function GeminiSection({
|
export function GeminiSection({
|
||||||
@@ -43,20 +36,15 @@ export function GeminiSection({
|
|||||||
usageDetails,
|
usageDetails,
|
||||||
loading,
|
loading,
|
||||||
disableControls,
|
disableControls,
|
||||||
isSaving,
|
|
||||||
isSwitching,
|
isSwitching,
|
||||||
isModalOpen,
|
|
||||||
modalIndex,
|
|
||||||
onAdd,
|
onAdd,
|
||||||
onEdit,
|
onEdit,
|
||||||
onDelete,
|
onDelete,
|
||||||
onToggle,
|
onToggle,
|
||||||
onCloseModal,
|
|
||||||
onSave,
|
|
||||||
}: GeminiSectionProps) {
|
}: GeminiSectionProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const actionsDisabled = disableControls || isSaving || isSwitching;
|
const actionsDisabled = disableControls || loading || isSwitching;
|
||||||
const toggleDisabled = disableControls || loading || isSaving || isSwitching;
|
const toggleDisabled = disableControls || loading || isSwitching;
|
||||||
|
|
||||||
const statusBarCache = useMemo(() => {
|
const statusBarCache = useMemo(() => {
|
||||||
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
||||||
@@ -76,8 +64,6 @@ export function GeminiSection({
|
|||||||
return cache;
|
return cache;
|
||||||
}, [configs, usageDetails]);
|
}, [configs, usageDetails]);
|
||||||
|
|
||||||
const initialData = modalIndex !== null ? configs[modalIndex] : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card
|
<Card
|
||||||
@@ -181,15 +167,6 @@ export function GeminiSection({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<GeminiModal
|
|
||||||
isOpen={isModalOpen}
|
|
||||||
editIndex={modalIndex}
|
|
||||||
initialData={initialData}
|
|
||||||
onClose={onCloseModal}
|
|
||||||
onSave={onSave}
|
|
||||||
isSaving={isSaving}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,8 +17,6 @@ import styles from '@/pages/AiProvidersPage.module.scss';
|
|||||||
import { ProviderList } from '../ProviderList';
|
import { ProviderList } from '../ProviderList';
|
||||||
import { ProviderStatusBar } from '../ProviderStatusBar';
|
import { ProviderStatusBar } from '../ProviderStatusBar';
|
||||||
import { getOpenAIProviderStats, getStatsBySource } from '../utils';
|
import { getOpenAIProviderStats, getStatsBySource } from '../utils';
|
||||||
import type { OpenAIFormState } from '../types';
|
|
||||||
import { OpenAIModal } from './OpenAIModal';
|
|
||||||
|
|
||||||
interface OpenAISectionProps {
|
interface OpenAISectionProps {
|
||||||
configs: OpenAIProviderConfig[];
|
configs: OpenAIProviderConfig[];
|
||||||
@@ -26,16 +24,11 @@ interface OpenAISectionProps {
|
|||||||
usageDetails: UsageDetail[];
|
usageDetails: UsageDetail[];
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
disableControls: boolean;
|
disableControls: boolean;
|
||||||
isSaving: boolean;
|
|
||||||
isSwitching: boolean;
|
isSwitching: boolean;
|
||||||
resolvedTheme: string;
|
resolvedTheme: string;
|
||||||
isModalOpen: boolean;
|
|
||||||
modalIndex: number | null;
|
|
||||||
onAdd: () => void;
|
onAdd: () => void;
|
||||||
onEdit: (index: number) => void;
|
onEdit: (index: number) => void;
|
||||||
onDelete: (index: number) => void;
|
onDelete: (index: number) => void;
|
||||||
onCloseModal: () => void;
|
|
||||||
onSave: (data: OpenAIFormState, index: number | null) => Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function OpenAISection({
|
export function OpenAISection({
|
||||||
@@ -44,19 +37,14 @@ export function OpenAISection({
|
|||||||
usageDetails,
|
usageDetails,
|
||||||
loading,
|
loading,
|
||||||
disableControls,
|
disableControls,
|
||||||
isSaving,
|
|
||||||
isSwitching,
|
isSwitching,
|
||||||
resolvedTheme,
|
resolvedTheme,
|
||||||
isModalOpen,
|
|
||||||
modalIndex,
|
|
||||||
onAdd,
|
onAdd,
|
||||||
onEdit,
|
onEdit,
|
||||||
onDelete,
|
onDelete,
|
||||||
onCloseModal,
|
|
||||||
onSave,
|
|
||||||
}: OpenAISectionProps) {
|
}: OpenAISectionProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const actionsDisabled = disableControls || isSaving || isSwitching;
|
const actionsDisabled = disableControls || loading || isSwitching;
|
||||||
|
|
||||||
const statusBarCache = useMemo(() => {
|
const statusBarCache = useMemo(() => {
|
||||||
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
||||||
@@ -77,8 +65,6 @@ export function OpenAISection({
|
|||||||
return cache;
|
return cache;
|
||||||
}, [configs, usageDetails]);
|
}, [configs, usageDetails]);
|
||||||
|
|
||||||
const initialData = modalIndex !== null ? configs[modalIndex] : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card
|
<Card
|
||||||
@@ -204,15 +190,6 @@ export function OpenAISection({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<OpenAIModal
|
|
||||||
isOpen={isModalOpen}
|
|
||||||
editIndex={modalIndex}
|
|
||||||
initialData={initialData}
|
|
||||||
onClose={onCloseModal}
|
|
||||||
onSave={onSave}
|
|
||||||
isSaving={isSaving}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,8 +15,6 @@ import styles from '@/pages/AiProvidersPage.module.scss';
|
|||||||
import { ProviderList } from '../ProviderList';
|
import { ProviderList } from '../ProviderList';
|
||||||
import { ProviderStatusBar } from '../ProviderStatusBar';
|
import { ProviderStatusBar } from '../ProviderStatusBar';
|
||||||
import { getStatsBySource } from '../utils';
|
import { getStatsBySource } from '../utils';
|
||||||
import type { VertexFormState } from '../types';
|
|
||||||
import { VertexModal } from './VertexModal';
|
|
||||||
|
|
||||||
interface VertexSectionProps {
|
interface VertexSectionProps {
|
||||||
configs: ProviderKeyConfig[];
|
configs: ProviderKeyConfig[];
|
||||||
@@ -24,15 +22,10 @@ interface VertexSectionProps {
|
|||||||
usageDetails: UsageDetail[];
|
usageDetails: UsageDetail[];
|
||||||
loading: boolean;
|
loading: boolean;
|
||||||
disableControls: boolean;
|
disableControls: boolean;
|
||||||
isSaving: boolean;
|
|
||||||
isSwitching: boolean;
|
isSwitching: boolean;
|
||||||
isModalOpen: boolean;
|
|
||||||
modalIndex: number | null;
|
|
||||||
onAdd: () => void;
|
onAdd: () => void;
|
||||||
onEdit: (index: number) => void;
|
onEdit: (index: number) => void;
|
||||||
onDelete: (index: number) => void;
|
onDelete: (index: number) => void;
|
||||||
onCloseModal: () => void;
|
|
||||||
onSave: (data: VertexFormState, index: number | null) => Promise<void>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function VertexSection({
|
export function VertexSection({
|
||||||
@@ -41,18 +34,13 @@ export function VertexSection({
|
|||||||
usageDetails,
|
usageDetails,
|
||||||
loading,
|
loading,
|
||||||
disableControls,
|
disableControls,
|
||||||
isSaving,
|
|
||||||
isSwitching,
|
isSwitching,
|
||||||
isModalOpen,
|
|
||||||
modalIndex,
|
|
||||||
onAdd,
|
onAdd,
|
||||||
onEdit,
|
onEdit,
|
||||||
onDelete,
|
onDelete,
|
||||||
onCloseModal,
|
|
||||||
onSave,
|
|
||||||
}: VertexSectionProps) {
|
}: VertexSectionProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const actionsDisabled = disableControls || isSaving || isSwitching;
|
const actionsDisabled = disableControls || loading || isSwitching;
|
||||||
|
|
||||||
const statusBarCache = useMemo(() => {
|
const statusBarCache = useMemo(() => {
|
||||||
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
const cache = new Map<string, ReturnType<typeof calculateStatusBarData>>();
|
||||||
@@ -72,8 +60,6 @@ export function VertexSection({
|
|||||||
return cache;
|
return cache;
|
||||||
}, [configs, usageDetails]);
|
}, [configs, usageDetails]);
|
||||||
|
|
||||||
const initialData = modalIndex !== null ? configs[modalIndex] : undefined;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Card
|
<Card
|
||||||
@@ -168,15 +154,6 @@ export function VertexSection({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<VertexModal
|
|
||||||
isOpen={isModalOpen}
|
|
||||||
editIndex={modalIndex}
|
|
||||||
initialData={initialData}
|
|
||||||
onClose={onCloseModal}
|
|
||||||
onSave={onSave}
|
|
||||||
isSaving={isSaving}
|
|
||||||
/>
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
306
src/pages/AiProvidersAmpcodeEditPage.tsx
Normal file
306
src/pages/AiProvidersAmpcodeEditPage.tsx
Normal file
@@ -0,0 +1,306 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
import { useLocation, useNavigate } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { ModelInputList } from '@/components/ui/ModelInputList';
|
||||||
|
import { ToggleSwitch } from '@/components/ui/ToggleSwitch';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { ampcodeApi } from '@/services/api';
|
||||||
|
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
|
||||||
|
import type { AmpcodeConfig } from '@/types';
|
||||||
|
import { maskApiKey } from '@/utils/format';
|
||||||
|
import { buildAmpcodeFormState, entriesToAmpcodeMappings } from '@/components/providers/utils';
|
||||||
|
import type { AmpcodeFormState } from '@/components/providers';
|
||||||
|
|
||||||
|
type LocationState = { fromAiProviders?: boolean } | null;
|
||||||
|
|
||||||
|
export function AiProvidersAmpcodeEditPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
const { showNotification, showConfirmation } = useNotificationStore();
|
||||||
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
|
const disableControls = connectionStatus !== 'connected';
|
||||||
|
|
||||||
|
const config = useConfigStore((state) => state.config);
|
||||||
|
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
|
||||||
|
const clearCache = useConfigStore((state) => state.clearCache);
|
||||||
|
|
||||||
|
const [form, setForm] = useState<AmpcodeFormState>(() => buildAmpcodeFormState(null));
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [loaded, setLoaded] = useState(false);
|
||||||
|
const [mappingsDirty, setMappingsDirty] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const initializedRef = useRef(false);
|
||||||
|
|
||||||
|
const title = useMemo(() => t('ai_providers.ampcode_modal_title'), [t]);
|
||||||
|
|
||||||
|
const getErrorMessage = (err: unknown) => {
|
||||||
|
if (err instanceof Error) return err.message;
|
||||||
|
if (typeof err === 'string') return err;
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
const state = location.state as LocationState;
|
||||||
|
if (state?.fromAiProviders) {
|
||||||
|
navigate(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('/ai-providers', { replace: true });
|
||||||
|
}, [location.state, navigate]);
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (initializedRef.current) return;
|
||||||
|
initializedRef.current = true;
|
||||||
|
|
||||||
|
setLoading(true);
|
||||||
|
setLoaded(false);
|
||||||
|
setMappingsDirty(false);
|
||||||
|
setError('');
|
||||||
|
setForm(buildAmpcodeFormState(config?.ampcode ?? null));
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
ampcodeApi
|
||||||
|
.getAmpcode()
|
||||||
|
.then((ampcode) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoaded(true);
|
||||||
|
updateConfigValue('ampcode', ampcode);
|
||||||
|
clearCache('ampcode');
|
||||||
|
setForm(buildAmpcodeFormState(ampcode));
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setError(getErrorMessage(err) || t('notification.refresh_failed'));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [clearCache, config?.ampcode, t, updateConfigValue]);
|
||||||
|
|
||||||
|
const clearAmpcodeUpstreamApiKey = async () => {
|
||||||
|
showConfirmation({
|
||||||
|
title: t('ai_providers.ampcode_clear_upstream_api_key_title', {
|
||||||
|
defaultValue: 'Clear Upstream API Key',
|
||||||
|
}),
|
||||||
|
message: t('ai_providers.ampcode_clear_upstream_api_key_confirm'),
|
||||||
|
variant: 'danger',
|
||||||
|
confirmText: t('common.confirm'),
|
||||||
|
onConfirm: async () => {
|
||||||
|
setSaving(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
await ampcodeApi.clearUpstreamApiKey();
|
||||||
|
const previous = config?.ampcode ?? {};
|
||||||
|
const next: AmpcodeConfig = { ...previous };
|
||||||
|
delete next.upstreamApiKey;
|
||||||
|
updateConfigValue('ampcode', next);
|
||||||
|
clearCache('ampcode');
|
||||||
|
showNotification(t('notification.ampcode_upstream_api_key_cleared'), 'success');
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = getErrorMessage(err);
|
||||||
|
setError(message);
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const performSaveAmpcode = async () => {
|
||||||
|
setSaving(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
const upstreamUrl = form.upstreamUrl.trim();
|
||||||
|
const overrideKey = form.upstreamApiKey.trim();
|
||||||
|
const modelMappings = entriesToAmpcodeMappings(form.mappingEntries);
|
||||||
|
|
||||||
|
if (upstreamUrl) {
|
||||||
|
await ampcodeApi.updateUpstreamUrl(upstreamUrl);
|
||||||
|
} else {
|
||||||
|
await ampcodeApi.clearUpstreamUrl();
|
||||||
|
}
|
||||||
|
|
||||||
|
await ampcodeApi.updateForceModelMappings(form.forceModelMappings);
|
||||||
|
|
||||||
|
if (loaded || mappingsDirty) {
|
||||||
|
if (modelMappings.length) {
|
||||||
|
await ampcodeApi.saveModelMappings(modelMappings);
|
||||||
|
} else {
|
||||||
|
await ampcodeApi.clearModelMappings();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overrideKey) {
|
||||||
|
await ampcodeApi.updateUpstreamApiKey(overrideKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
const previous = config?.ampcode ?? {};
|
||||||
|
const next: AmpcodeConfig = {
|
||||||
|
upstreamUrl: upstreamUrl || undefined,
|
||||||
|
forceModelMappings: form.forceModelMappings,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (previous.upstreamApiKey) {
|
||||||
|
next.upstreamApiKey = previous.upstreamApiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(previous.modelMappings)) {
|
||||||
|
next.modelMappings = previous.modelMappings;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (overrideKey) {
|
||||||
|
next.upstreamApiKey = overrideKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (loaded || mappingsDirty) {
|
||||||
|
if (modelMappings.length) {
|
||||||
|
next.modelMappings = modelMappings;
|
||||||
|
} else {
|
||||||
|
delete next.modelMappings;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
updateConfigValue('ampcode', next);
|
||||||
|
clearCache('ampcode');
|
||||||
|
showNotification(t('notification.ampcode_updated'), 'success');
|
||||||
|
handleBack();
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = getErrorMessage(err);
|
||||||
|
setError(message);
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveAmpcode = async () => {
|
||||||
|
if (!loaded && mappingsDirty) {
|
||||||
|
showConfirmation({
|
||||||
|
title: t('ai_providers.ampcode_mappings_overwrite_title', { defaultValue: 'Overwrite Mappings' }),
|
||||||
|
message: t('ai_providers.ampcode_mappings_overwrite_confirm'),
|
||||||
|
variant: 'secondary',
|
||||||
|
confirmText: t('common.confirm'),
|
||||||
|
onConfirm: performSaveAmpcode,
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await performSaveAmpcode();
|
||||||
|
};
|
||||||
|
|
||||||
|
const canSave = !disableControls && !saving && !loading;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={title}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={() => void saveAmpcode()} loading={saving} disabled={!canSave}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={loading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
{error && <div className="error-box">{error}</div>}
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.ampcode_upstream_url_label')}
|
||||||
|
placeholder={t('ai_providers.ampcode_upstream_url_placeholder')}
|
||||||
|
value={form.upstreamUrl}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, upstreamUrl: e.target.value }))}
|
||||||
|
disabled={loading || saving || disableControls}
|
||||||
|
hint={t('ai_providers.ampcode_upstream_url_hint')}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.ampcode_upstream_api_key_label')}
|
||||||
|
placeholder={t('ai_providers.ampcode_upstream_api_key_placeholder')}
|
||||||
|
type="password"
|
||||||
|
value={form.upstreamApiKey}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, upstreamApiKey: e.target.value }))}
|
||||||
|
disabled={loading || saving || disableControls}
|
||||||
|
hint={t('ai_providers.ampcode_upstream_api_key_hint')}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
style={{
|
||||||
|
display: 'flex',
|
||||||
|
gap: 8,
|
||||||
|
alignItems: 'center',
|
||||||
|
marginTop: -8,
|
||||||
|
marginBottom: 12,
|
||||||
|
flexWrap: 'wrap',
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="hint" style={{ margin: 0 }}>
|
||||||
|
{t('ai_providers.ampcode_upstream_api_key_current', {
|
||||||
|
key: config?.ampcode?.upstreamApiKey
|
||||||
|
? maskApiKey(config.ampcode.upstreamApiKey)
|
||||||
|
: t('common.not_set'),
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
variant="danger"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => void clearAmpcodeUpstreamApiKey()}
|
||||||
|
disabled={loading || saving || disableControls || !config?.ampcode?.upstreamApiKey}
|
||||||
|
>
|
||||||
|
{t('ai_providers.ampcode_clear_upstream_api_key')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group">
|
||||||
|
<ToggleSwitch
|
||||||
|
label={t('ai_providers.ampcode_force_model_mappings_label')}
|
||||||
|
checked={form.forceModelMappings}
|
||||||
|
onChange={(value) => setForm((prev) => ({ ...prev, forceModelMappings: value }))}
|
||||||
|
disabled={loading || saving || disableControls}
|
||||||
|
/>
|
||||||
|
<div className="hint">{t('ai_providers.ampcode_force_model_mappings_hint')}</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.ampcode_model_mappings_label')}</label>
|
||||||
|
<ModelInputList
|
||||||
|
entries={form.mappingEntries}
|
||||||
|
onChange={(entries) => {
|
||||||
|
setMappingsDirty(true);
|
||||||
|
setForm((prev) => ({ ...prev, mappingEntries: entries }));
|
||||||
|
}}
|
||||||
|
addLabel={t('ai_providers.ampcode_model_mappings_add_btn')}
|
||||||
|
namePlaceholder={t('ai_providers.ampcode_model_mappings_from_placeholder')}
|
||||||
|
aliasPlaceholder={t('ai_providers.ampcode_model_mappings_to_placeholder')}
|
||||||
|
disabled={loading || saving || disableControls}
|
||||||
|
/>
|
||||||
|
<div className="hint">{t('ai_providers.ampcode_model_mappings_hint')}</div>
|
||||||
|
</div>
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
275
src/pages/AiProvidersClaudeEditPage.tsx
Normal file
275
src/pages/AiProvidersClaudeEditPage.tsx
Normal file
@@ -0,0 +1,275 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { HeaderInputList } from '@/components/ui/HeaderInputList';
|
||||||
|
import { ModelInputList, modelsToEntries } from '@/components/ui/ModelInputList';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { providersApi } from '@/services/api';
|
||||||
|
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
|
||||||
|
import type { ProviderKeyConfig } from '@/types';
|
||||||
|
import { buildHeaderObject, headersToEntries } from '@/utils/headers';
|
||||||
|
import { excludedModelsToText, parseExcludedModels } from '@/components/providers/utils';
|
||||||
|
import type { ProviderFormState } from '@/components/providers';
|
||||||
|
|
||||||
|
type LocationState = { fromAiProviders?: boolean } | null;
|
||||||
|
|
||||||
|
const buildEmptyForm = (): ProviderFormState => ({
|
||||||
|
apiKey: '',
|
||||||
|
prefix: '',
|
||||||
|
baseUrl: '',
|
||||||
|
proxyUrl: '',
|
||||||
|
headers: [],
|
||||||
|
models: [],
|
||||||
|
excludedModels: [],
|
||||||
|
modelEntries: [{ name: '', alias: '' }],
|
||||||
|
excludedText: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const parseIndexParam = (value: string | undefined) => {
|
||||||
|
if (!value) return null;
|
||||||
|
const parsed = Number.parseInt(value, 10);
|
||||||
|
return Number.isFinite(parsed) ? parsed : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersClaudeEditPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
const params = useParams<{ index?: string }>();
|
||||||
|
|
||||||
|
const { showNotification } = useNotificationStore();
|
||||||
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
|
const disableControls = connectionStatus !== 'connected';
|
||||||
|
|
||||||
|
const fetchConfig = useConfigStore((state) => state.fetchConfig);
|
||||||
|
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
|
||||||
|
const clearCache = useConfigStore((state) => state.clearCache);
|
||||||
|
|
||||||
|
const [configs, setConfigs] = useState<ProviderKeyConfig[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [form, setForm] = useState<ProviderFormState>(() => buildEmptyForm());
|
||||||
|
|
||||||
|
const hasIndexParam = typeof params.index === 'string';
|
||||||
|
const editIndex = useMemo(() => parseIndexParam(params.index), [params.index]);
|
||||||
|
const invalidIndexParam = hasIndexParam && editIndex === null;
|
||||||
|
|
||||||
|
const initialData = useMemo(() => {
|
||||||
|
if (editIndex === null) return undefined;
|
||||||
|
return configs[editIndex];
|
||||||
|
}, [configs, editIndex]);
|
||||||
|
|
||||||
|
const invalidIndex = editIndex !== null && !initialData;
|
||||||
|
|
||||||
|
const title =
|
||||||
|
editIndex !== null
|
||||||
|
? t('ai_providers.claude_edit_modal_title')
|
||||||
|
: t('ai_providers.claude_add_modal_title');
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
const state = location.state as LocationState;
|
||||||
|
if (state?.fromAiProviders) {
|
||||||
|
navigate(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('/ai-providers', { replace: true });
|
||||||
|
}, [location.state, navigate]);
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
fetchConfig('claude-api-key')
|
||||||
|
.then((value) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setConfigs(Array.isArray(value) ? (value as ProviderKeyConfig[]) : []);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message || t('notification.refresh_failed'));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [fetchConfig, t]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading) return;
|
||||||
|
|
||||||
|
if (initialData) {
|
||||||
|
setForm({
|
||||||
|
...initialData,
|
||||||
|
headers: headersToEntries(initialData.headers),
|
||||||
|
modelEntries: modelsToEntries(initialData.models),
|
||||||
|
excludedText: excludedModelsToText(initialData.excludedModels),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setForm(buildEmptyForm());
|
||||||
|
}, [initialData, loading]);
|
||||||
|
|
||||||
|
const canSave = !disableControls && !saving && !loading && !invalidIndexParam && !invalidIndex;
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
if (!canSave) return;
|
||||||
|
|
||||||
|
setSaving(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
const payload: ProviderKeyConfig = {
|
||||||
|
apiKey: form.apiKey.trim(),
|
||||||
|
prefix: form.prefix?.trim() || undefined,
|
||||||
|
baseUrl: (form.baseUrl ?? '').trim() || undefined,
|
||||||
|
proxyUrl: form.proxyUrl?.trim() || undefined,
|
||||||
|
headers: buildHeaderObject(form.headers),
|
||||||
|
models: form.modelEntries
|
||||||
|
.map((entry) => {
|
||||||
|
const name = entry.name.trim();
|
||||||
|
if (!name) return null;
|
||||||
|
const alias = entry.alias.trim();
|
||||||
|
return { name, alias: alias || name };
|
||||||
|
})
|
||||||
|
.filter(Boolean) as ProviderKeyConfig['models'],
|
||||||
|
excludedModels: parseExcludedModels(form.excludedText),
|
||||||
|
};
|
||||||
|
|
||||||
|
const nextList =
|
||||||
|
editIndex !== null
|
||||||
|
? configs.map((item, idx) => (idx === editIndex ? payload : item))
|
||||||
|
: [...configs, payload];
|
||||||
|
|
||||||
|
await providersApi.saveClaudeConfigs(nextList);
|
||||||
|
updateConfigValue('claude-api-key', nextList);
|
||||||
|
clearCache('claude-api-key');
|
||||||
|
showNotification(
|
||||||
|
editIndex !== null ? t('notification.claude_config_updated') : t('notification.claude_config_added'),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
handleBack();
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message);
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
canSave,
|
||||||
|
clearCache,
|
||||||
|
configs,
|
||||||
|
editIndex,
|
||||||
|
form,
|
||||||
|
handleBack,
|
||||||
|
showNotification,
|
||||||
|
t,
|
||||||
|
updateConfigValue,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={title}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={handleSave} loading={saving} disabled={!canSave}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={loading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
{error && <div className="error-box">{error}</div>}
|
||||||
|
{invalidIndexParam || invalidIndex ? (
|
||||||
|
<div className="hint">Invalid provider index.</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.claude_add_modal_key_label')}
|
||||||
|
value={form.apiKey}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, apiKey: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.prefix_label')}
|
||||||
|
placeholder={t('ai_providers.prefix_placeholder')}
|
||||||
|
value={form.prefix ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, prefix: e.target.value }))}
|
||||||
|
hint={t('ai_providers.prefix_hint')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.claude_add_modal_url_label')}
|
||||||
|
value={form.baseUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, baseUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.claude_add_modal_proxy_label')}
|
||||||
|
value={form.proxyUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, proxyUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<HeaderInputList
|
||||||
|
entries={form.headers}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, headers: entries }))}
|
||||||
|
addLabel={t('common.custom_headers_add')}
|
||||||
|
keyPlaceholder={t('common.custom_headers_key_placeholder')}
|
||||||
|
valuePlaceholder={t('common.custom_headers_value_placeholder')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.claude_models_label')}</label>
|
||||||
|
<ModelInputList
|
||||||
|
entries={form.modelEntries}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, modelEntries: entries }))}
|
||||||
|
addLabel={t('ai_providers.claude_models_add_btn')}
|
||||||
|
namePlaceholder={t('common.model_name_placeholder')}
|
||||||
|
aliasPlaceholder={t('common.model_alias_placeholder')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.excluded_models_label')}</label>
|
||||||
|
<textarea
|
||||||
|
className="input"
|
||||||
|
placeholder={t('ai_providers.excluded_models_placeholder')}
|
||||||
|
value={form.excludedText}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, excludedText: e.target.value }))}
|
||||||
|
rows={4}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="hint">{t('ai_providers.excluded_models_hint')}</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
265
src/pages/AiProvidersCodexEditPage.tsx
Normal file
265
src/pages/AiProvidersCodexEditPage.tsx
Normal file
@@ -0,0 +1,265 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { HeaderInputList } from '@/components/ui/HeaderInputList';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { providersApi } from '@/services/api';
|
||||||
|
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
|
||||||
|
import type { ProviderKeyConfig } from '@/types';
|
||||||
|
import { buildHeaderObject, headersToEntries } from '@/utils/headers';
|
||||||
|
import { entriesToModels } from '@/components/ui/ModelInputList';
|
||||||
|
import { excludedModelsToText, parseExcludedModels } from '@/components/providers/utils';
|
||||||
|
import type { ProviderFormState } from '@/components/providers';
|
||||||
|
|
||||||
|
type LocationState = { fromAiProviders?: boolean } | null;
|
||||||
|
|
||||||
|
const buildEmptyForm = (): ProviderFormState => ({
|
||||||
|
apiKey: '',
|
||||||
|
prefix: '',
|
||||||
|
baseUrl: '',
|
||||||
|
proxyUrl: '',
|
||||||
|
headers: [],
|
||||||
|
models: [],
|
||||||
|
excludedModels: [],
|
||||||
|
modelEntries: [{ name: '', alias: '' }],
|
||||||
|
excludedText: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const parseIndexParam = (value: string | undefined) => {
|
||||||
|
if (!value) return null;
|
||||||
|
const parsed = Number.parseInt(value, 10);
|
||||||
|
return Number.isFinite(parsed) ? parsed : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersCodexEditPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
const params = useParams<{ index?: string }>();
|
||||||
|
|
||||||
|
const { showNotification } = useNotificationStore();
|
||||||
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
|
const disableControls = connectionStatus !== 'connected';
|
||||||
|
|
||||||
|
const fetchConfig = useConfigStore((state) => state.fetchConfig);
|
||||||
|
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
|
||||||
|
const clearCache = useConfigStore((state) => state.clearCache);
|
||||||
|
|
||||||
|
const [configs, setConfigs] = useState<ProviderKeyConfig[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [form, setForm] = useState<ProviderFormState>(() => buildEmptyForm());
|
||||||
|
|
||||||
|
const hasIndexParam = typeof params.index === 'string';
|
||||||
|
const editIndex = useMemo(() => parseIndexParam(params.index), [params.index]);
|
||||||
|
const invalidIndexParam = hasIndexParam && editIndex === null;
|
||||||
|
|
||||||
|
const initialData = useMemo(() => {
|
||||||
|
if (editIndex === null) return undefined;
|
||||||
|
return configs[editIndex];
|
||||||
|
}, [configs, editIndex]);
|
||||||
|
|
||||||
|
const invalidIndex = editIndex !== null && !initialData;
|
||||||
|
|
||||||
|
const title =
|
||||||
|
editIndex !== null ? t('ai_providers.codex_edit_modal_title') : t('ai_providers.codex_add_modal_title');
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
const state = location.state as LocationState;
|
||||||
|
if (state?.fromAiProviders) {
|
||||||
|
navigate(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('/ai-providers', { replace: true });
|
||||||
|
}, [location.state, navigate]);
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
fetchConfig('codex-api-key')
|
||||||
|
.then((value) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setConfigs(Array.isArray(value) ? (value as ProviderKeyConfig[]) : []);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message || t('notification.refresh_failed'));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [fetchConfig, t]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading) return;
|
||||||
|
|
||||||
|
if (initialData) {
|
||||||
|
setForm({
|
||||||
|
...initialData,
|
||||||
|
headers: headersToEntries(initialData.headers),
|
||||||
|
modelEntries: (initialData.models || []).map((model) => ({
|
||||||
|
name: model.name,
|
||||||
|
alias: model.alias ?? '',
|
||||||
|
})),
|
||||||
|
excludedText: excludedModelsToText(initialData.excludedModels),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setForm(buildEmptyForm());
|
||||||
|
}, [initialData, loading]);
|
||||||
|
|
||||||
|
const canSave = !disableControls && !saving && !loading && !invalidIndexParam && !invalidIndex;
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
if (!canSave) return;
|
||||||
|
|
||||||
|
const trimmedBaseUrl = (form.baseUrl ?? '').trim();
|
||||||
|
const baseUrl = trimmedBaseUrl || undefined;
|
||||||
|
if (!baseUrl) {
|
||||||
|
showNotification(t('notification.codex_base_url_required'), 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSaving(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
const payload: ProviderKeyConfig = {
|
||||||
|
apiKey: form.apiKey.trim(),
|
||||||
|
prefix: form.prefix?.trim() || undefined,
|
||||||
|
baseUrl,
|
||||||
|
proxyUrl: form.proxyUrl?.trim() || undefined,
|
||||||
|
headers: buildHeaderObject(form.headers),
|
||||||
|
models: entriesToModels(form.modelEntries),
|
||||||
|
excludedModels: parseExcludedModels(form.excludedText),
|
||||||
|
};
|
||||||
|
|
||||||
|
const nextList =
|
||||||
|
editIndex !== null
|
||||||
|
? configs.map((item, idx) => (idx === editIndex ? payload : item))
|
||||||
|
: [...configs, payload];
|
||||||
|
|
||||||
|
await providersApi.saveCodexConfigs(nextList);
|
||||||
|
updateConfigValue('codex-api-key', nextList);
|
||||||
|
clearCache('codex-api-key');
|
||||||
|
showNotification(
|
||||||
|
editIndex !== null ? t('notification.codex_config_updated') : t('notification.codex_config_added'),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
handleBack();
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message);
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
canSave,
|
||||||
|
clearCache,
|
||||||
|
configs,
|
||||||
|
editIndex,
|
||||||
|
form,
|
||||||
|
handleBack,
|
||||||
|
showNotification,
|
||||||
|
t,
|
||||||
|
updateConfigValue,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={title}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={handleSave} loading={saving} disabled={!canSave}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={loading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
{error && <div className="error-box">{error}</div>}
|
||||||
|
{invalidIndexParam || invalidIndex ? (
|
||||||
|
<div className="hint">Invalid provider index.</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.codex_add_modal_key_label')}
|
||||||
|
value={form.apiKey}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, apiKey: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.prefix_label')}
|
||||||
|
placeholder={t('ai_providers.prefix_placeholder')}
|
||||||
|
value={form.prefix ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, prefix: e.target.value }))}
|
||||||
|
hint={t('ai_providers.prefix_hint')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.codex_add_modal_url_label')}
|
||||||
|
value={form.baseUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, baseUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.codex_add_modal_proxy_label')}
|
||||||
|
value={form.proxyUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, proxyUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<HeaderInputList
|
||||||
|
entries={form.headers}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, headers: entries }))}
|
||||||
|
addLabel={t('common.custom_headers_add')}
|
||||||
|
keyPlaceholder={t('common.custom_headers_key_placeholder')}
|
||||||
|
valuePlaceholder={t('common.custom_headers_value_placeholder')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.excluded_models_label')}</label>
|
||||||
|
<textarea
|
||||||
|
className="input"
|
||||||
|
placeholder={t('ai_providers.excluded_models_placeholder')}
|
||||||
|
value={form.excludedText}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, excludedText: e.target.value }))}
|
||||||
|
rows={4}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="hint">{t('ai_providers.excluded_models_hint')}</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
244
src/pages/AiProvidersGeminiEditPage.tsx
Normal file
244
src/pages/AiProvidersGeminiEditPage.tsx
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { HeaderInputList } from '@/components/ui/HeaderInputList';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { providersApi } from '@/services/api';
|
||||||
|
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
|
||||||
|
import type { GeminiKeyConfig } from '@/types';
|
||||||
|
import { buildHeaderObject, headersToEntries } from '@/utils/headers';
|
||||||
|
import { excludedModelsToText, parseExcludedModels } from '@/components/providers/utils';
|
||||||
|
import type { GeminiFormState } from '@/components/providers';
|
||||||
|
|
||||||
|
type LocationState = { fromAiProviders?: boolean } | null;
|
||||||
|
|
||||||
|
const buildEmptyForm = (): GeminiFormState => ({
|
||||||
|
apiKey: '',
|
||||||
|
prefix: '',
|
||||||
|
baseUrl: '',
|
||||||
|
headers: [],
|
||||||
|
excludedModels: [],
|
||||||
|
excludedText: '',
|
||||||
|
});
|
||||||
|
|
||||||
|
const parseIndexParam = (value: string | undefined) => {
|
||||||
|
if (!value) return null;
|
||||||
|
const parsed = Number.parseInt(value, 10);
|
||||||
|
return Number.isFinite(parsed) ? parsed : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersGeminiEditPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
const params = useParams<{ index?: string }>();
|
||||||
|
|
||||||
|
const { showNotification } = useNotificationStore();
|
||||||
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
|
const disableControls = connectionStatus !== 'connected';
|
||||||
|
|
||||||
|
const fetchConfig = useConfigStore((state) => state.fetchConfig);
|
||||||
|
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
|
||||||
|
const clearCache = useConfigStore((state) => state.clearCache);
|
||||||
|
|
||||||
|
const [configs, setConfigs] = useState<GeminiKeyConfig[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [form, setForm] = useState<GeminiFormState>(() => buildEmptyForm());
|
||||||
|
|
||||||
|
const hasIndexParam = typeof params.index === 'string';
|
||||||
|
const editIndex = useMemo(() => parseIndexParam(params.index), [params.index]);
|
||||||
|
const invalidIndexParam = hasIndexParam && editIndex === null;
|
||||||
|
|
||||||
|
const initialData = useMemo(() => {
|
||||||
|
if (editIndex === null) return undefined;
|
||||||
|
return configs[editIndex];
|
||||||
|
}, [configs, editIndex]);
|
||||||
|
|
||||||
|
const invalidIndex = editIndex !== null && !initialData;
|
||||||
|
|
||||||
|
const title =
|
||||||
|
editIndex !== null ? t('ai_providers.gemini_edit_modal_title') : t('ai_providers.gemini_add_modal_title');
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
const state = location.state as LocationState;
|
||||||
|
if (state?.fromAiProviders) {
|
||||||
|
navigate(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('/ai-providers', { replace: true });
|
||||||
|
}, [location.state, navigate]);
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
fetchConfig('gemini-api-key')
|
||||||
|
.then((value) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setConfigs(Array.isArray(value) ? (value as GeminiKeyConfig[]) : []);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message || t('notification.refresh_failed'));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [fetchConfig, t]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading) return;
|
||||||
|
|
||||||
|
if (initialData) {
|
||||||
|
setForm({
|
||||||
|
...initialData,
|
||||||
|
headers: headersToEntries(initialData.headers),
|
||||||
|
excludedText: excludedModelsToText(initialData.excludedModels),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setForm(buildEmptyForm());
|
||||||
|
}, [initialData, loading]);
|
||||||
|
|
||||||
|
const canSave = !disableControls && !saving && !loading && !invalidIndexParam && !invalidIndex;
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
if (!canSave) return;
|
||||||
|
|
||||||
|
setSaving(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
const payload: GeminiKeyConfig = {
|
||||||
|
apiKey: form.apiKey.trim(),
|
||||||
|
prefix: form.prefix?.trim() || undefined,
|
||||||
|
baseUrl: form.baseUrl?.trim() || undefined,
|
||||||
|
headers: buildHeaderObject(form.headers),
|
||||||
|
excludedModels: parseExcludedModels(form.excludedText),
|
||||||
|
};
|
||||||
|
|
||||||
|
const nextList =
|
||||||
|
editIndex !== null
|
||||||
|
? configs.map((item, idx) => (idx === editIndex ? payload : item))
|
||||||
|
: [...configs, payload];
|
||||||
|
|
||||||
|
await providersApi.saveGeminiKeys(nextList);
|
||||||
|
updateConfigValue('gemini-api-key', nextList);
|
||||||
|
clearCache('gemini-api-key');
|
||||||
|
showNotification(
|
||||||
|
editIndex !== null ? t('notification.gemini_key_updated') : t('notification.gemini_key_added'),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
handleBack();
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message);
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
canSave,
|
||||||
|
clearCache,
|
||||||
|
configs,
|
||||||
|
editIndex,
|
||||||
|
form,
|
||||||
|
handleBack,
|
||||||
|
showNotification,
|
||||||
|
t,
|
||||||
|
updateConfigValue,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={title}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={handleSave} loading={saving} disabled={!canSave}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={loading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
{error && <div className="error-box">{error}</div>}
|
||||||
|
{invalidIndexParam || invalidIndex ? (
|
||||||
|
<div className="hint">Invalid provider index.</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.gemini_add_modal_key_label')}
|
||||||
|
placeholder={t('ai_providers.gemini_add_modal_key_placeholder')}
|
||||||
|
value={form.apiKey}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, apiKey: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.prefix_label')}
|
||||||
|
placeholder={t('ai_providers.prefix_placeholder')}
|
||||||
|
value={form.prefix ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, prefix: e.target.value }))}
|
||||||
|
hint={t('ai_providers.prefix_hint')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.gemini_base_url_label')}
|
||||||
|
placeholder={t('ai_providers.gemini_base_url_placeholder')}
|
||||||
|
value={form.baseUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, baseUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<HeaderInputList
|
||||||
|
entries={form.headers}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, headers: entries }))}
|
||||||
|
addLabel={t('common.custom_headers_add')}
|
||||||
|
keyPlaceholder={t('common.custom_headers_key_placeholder')}
|
||||||
|
valuePlaceholder={t('common.custom_headers_value_placeholder')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.excluded_models_label')}</label>
|
||||||
|
<textarea
|
||||||
|
className="input"
|
||||||
|
placeholder={t('ai_providers.excluded_models_placeholder')}
|
||||||
|
value={form.excludedText}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, excludedText: e.target.value }))}
|
||||||
|
rows={4}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="hint">{t('ai_providers.excluded_models_hint')}</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
290
src/pages/AiProvidersOpenAIEditLayout.tsx
Normal file
290
src/pages/AiProvidersOpenAIEditLayout.tsx
Normal file
@@ -0,0 +1,290 @@
|
|||||||
|
import type { Dispatch, SetStateAction } from 'react';
|
||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { Outlet, useLocation, useNavigate, useParams } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { providersApi } from '@/services/api';
|
||||||
|
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
|
||||||
|
import { entriesToModels, modelsToEntries } from '@/components/ui/ModelInputList';
|
||||||
|
import type { ApiKeyEntry, OpenAIProviderConfig } from '@/types';
|
||||||
|
import type { ModelInfo } from '@/utils/models';
|
||||||
|
import { buildHeaderObject, headersToEntries } from '@/utils/headers';
|
||||||
|
import { buildApiKeyEntry } from '@/components/providers/utils';
|
||||||
|
import type { ModelEntry, OpenAIFormState } from '@/components/providers/types';
|
||||||
|
|
||||||
|
type LocationState = { fromAiProviders?: boolean } | null;
|
||||||
|
|
||||||
|
export type OpenAIEditOutletContext = {
|
||||||
|
hasIndexParam: boolean;
|
||||||
|
editIndex: number | null;
|
||||||
|
invalidIndexParam: boolean;
|
||||||
|
invalidIndex: boolean;
|
||||||
|
disableControls: boolean;
|
||||||
|
loading: boolean;
|
||||||
|
saving: boolean;
|
||||||
|
form: OpenAIFormState;
|
||||||
|
setForm: Dispatch<SetStateAction<OpenAIFormState>>;
|
||||||
|
testModel: string;
|
||||||
|
setTestModel: Dispatch<SetStateAction<string>>;
|
||||||
|
testStatus: 'idle' | 'loading' | 'success' | 'error';
|
||||||
|
setTestStatus: Dispatch<SetStateAction<'idle' | 'loading' | 'success' | 'error'>>;
|
||||||
|
testMessage: string;
|
||||||
|
setTestMessage: Dispatch<SetStateAction<string>>;
|
||||||
|
availableModels: string[];
|
||||||
|
handleBack: () => void;
|
||||||
|
handleSave: () => Promise<void>;
|
||||||
|
mergeDiscoveredModels: (selectedModels: ModelInfo[]) => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
const buildEmptyForm = (): OpenAIFormState => ({
|
||||||
|
name: '',
|
||||||
|
prefix: '',
|
||||||
|
baseUrl: '',
|
||||||
|
headers: [],
|
||||||
|
apiKeyEntries: [buildApiKeyEntry()],
|
||||||
|
modelEntries: [{ name: '', alias: '' }],
|
||||||
|
testModel: undefined,
|
||||||
|
});
|
||||||
|
|
||||||
|
const parseIndexParam = (value: string | undefined) => {
|
||||||
|
if (!value) return null;
|
||||||
|
const parsed = Number.parseInt(value, 10);
|
||||||
|
return Number.isFinite(parsed) ? parsed : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const getErrorMessage = (err: unknown) => {
|
||||||
|
if (err instanceof Error) return err.message;
|
||||||
|
if (typeof err === 'string') return err;
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersOpenAIEditLayout() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
const { showNotification } = useNotificationStore();
|
||||||
|
|
||||||
|
const params = useParams<{ index?: string }>();
|
||||||
|
const hasIndexParam = typeof params.index === 'string';
|
||||||
|
const editIndex = useMemo(() => parseIndexParam(params.index), [params.index]);
|
||||||
|
const invalidIndexParam = hasIndexParam && editIndex === null;
|
||||||
|
|
||||||
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
|
const disableControls = connectionStatus !== 'connected';
|
||||||
|
|
||||||
|
const fetchConfig = useConfigStore((state) => state.fetchConfig);
|
||||||
|
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
|
||||||
|
const clearCache = useConfigStore((state) => state.clearCache);
|
||||||
|
|
||||||
|
const [providers, setProviders] = useState<OpenAIProviderConfig[]>([]);
|
||||||
|
const [form, setForm] = useState<OpenAIFormState>(() => buildEmptyForm());
|
||||||
|
const [testModel, setTestModel] = useState('');
|
||||||
|
const [testStatus, setTestStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
|
||||||
|
const [testMessage, setTestMessage] = useState('');
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
|
||||||
|
const initialData = useMemo(() => {
|
||||||
|
if (editIndex === null) return undefined;
|
||||||
|
return providers[editIndex];
|
||||||
|
}, [editIndex, providers]);
|
||||||
|
|
||||||
|
const invalidIndex = editIndex !== null && !initialData;
|
||||||
|
|
||||||
|
const availableModels = useMemo(
|
||||||
|
() => form.modelEntries.map((entry) => entry.name.trim()).filter(Boolean),
|
||||||
|
[form.modelEntries]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
const state = location.state as LocationState;
|
||||||
|
if (state?.fromAiProviders) {
|
||||||
|
navigate(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('/ai-providers', { replace: true });
|
||||||
|
}, [location.state, navigate]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
fetchConfig('openai-compatibility')
|
||||||
|
.then((value) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setProviders(Array.isArray(value) ? (value as OpenAIProviderConfig[]) : []);
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const message = getErrorMessage(err) || t('notification.refresh_failed');
|
||||||
|
showNotification(`${t('notification.load_failed')}: ${message}`, 'error');
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [fetchConfig, showNotification, t]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading) return;
|
||||||
|
|
||||||
|
if (initialData) {
|
||||||
|
const modelEntries = modelsToEntries(initialData.models);
|
||||||
|
setForm({
|
||||||
|
name: initialData.name,
|
||||||
|
prefix: initialData.prefix ?? '',
|
||||||
|
baseUrl: initialData.baseUrl,
|
||||||
|
headers: headersToEntries(initialData.headers),
|
||||||
|
testModel: initialData.testModel,
|
||||||
|
modelEntries,
|
||||||
|
apiKeyEntries: initialData.apiKeyEntries?.length
|
||||||
|
? initialData.apiKeyEntries
|
||||||
|
: [buildApiKeyEntry()],
|
||||||
|
});
|
||||||
|
|
||||||
|
const available = modelEntries.map((entry) => entry.name.trim()).filter(Boolean);
|
||||||
|
const initialTestModel =
|
||||||
|
initialData.testModel && available.includes(initialData.testModel)
|
||||||
|
? initialData.testModel
|
||||||
|
: available[0] || '';
|
||||||
|
setTestModel(initialTestModel);
|
||||||
|
} else {
|
||||||
|
setForm(buildEmptyForm());
|
||||||
|
setTestModel('');
|
||||||
|
}
|
||||||
|
|
||||||
|
setTestStatus('idle');
|
||||||
|
setTestMessage('');
|
||||||
|
}, [initialData, loading]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading) return;
|
||||||
|
|
||||||
|
if (availableModels.length === 0) {
|
||||||
|
if (testModel) {
|
||||||
|
setTestModel('');
|
||||||
|
setTestStatus('idle');
|
||||||
|
setTestMessage('');
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!testModel || !availableModels.includes(testModel)) {
|
||||||
|
setTestModel(availableModels[0]);
|
||||||
|
setTestStatus('idle');
|
||||||
|
setTestMessage('');
|
||||||
|
}
|
||||||
|
}, [availableModels, loading, testModel]);
|
||||||
|
|
||||||
|
const mergeDiscoveredModels = useCallback(
|
||||||
|
(selectedModels: ModelInfo[]) => {
|
||||||
|
if (!selectedModels.length) return;
|
||||||
|
|
||||||
|
const mergedMap = new Map<string, ModelEntry>();
|
||||||
|
form.modelEntries.forEach((entry) => {
|
||||||
|
const name = entry.name.trim();
|
||||||
|
if (!name) return;
|
||||||
|
mergedMap.set(name, { name, alias: entry.alias?.trim() || '' });
|
||||||
|
});
|
||||||
|
|
||||||
|
let addedCount = 0;
|
||||||
|
selectedModels.forEach((model) => {
|
||||||
|
const name = model.name.trim();
|
||||||
|
if (!name || mergedMap.has(name)) return;
|
||||||
|
mergedMap.set(name, { name, alias: model.alias ?? '' });
|
||||||
|
addedCount += 1;
|
||||||
|
});
|
||||||
|
|
||||||
|
const mergedEntries = Array.from(mergedMap.values());
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
modelEntries: mergedEntries.length ? mergedEntries : [{ name: '', alias: '' }],
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (addedCount > 0) {
|
||||||
|
showNotification(t('ai_providers.openai_models_fetch_added', { count: addedCount }), 'success');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[form.modelEntries, showNotification, t]
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
const payload: OpenAIProviderConfig = {
|
||||||
|
name: form.name.trim(),
|
||||||
|
prefix: form.prefix?.trim() || undefined,
|
||||||
|
baseUrl: form.baseUrl.trim(),
|
||||||
|
headers: buildHeaderObject(form.headers),
|
||||||
|
apiKeyEntries: form.apiKeyEntries.map((entry: ApiKeyEntry) => ({
|
||||||
|
apiKey: entry.apiKey.trim(),
|
||||||
|
proxyUrl: entry.proxyUrl?.trim() || undefined,
|
||||||
|
headers: entry.headers,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
if (form.testModel) payload.testModel = form.testModel.trim();
|
||||||
|
const models = entriesToModels(form.modelEntries);
|
||||||
|
if (models.length) payload.models = models;
|
||||||
|
|
||||||
|
const nextList =
|
||||||
|
editIndex !== null
|
||||||
|
? providers.map((item, idx) => (idx === editIndex ? payload : item))
|
||||||
|
: [...providers, payload];
|
||||||
|
|
||||||
|
await providersApi.saveOpenAIProviders(nextList);
|
||||||
|
setProviders(nextList);
|
||||||
|
updateConfigValue('openai-compatibility', nextList);
|
||||||
|
clearCache('openai-compatibility');
|
||||||
|
showNotification(
|
||||||
|
editIndex !== null
|
||||||
|
? t('notification.openai_provider_updated')
|
||||||
|
: t('notification.openai_provider_added'),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
handleBack();
|
||||||
|
} catch (err: unknown) {
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${getErrorMessage(err)}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
clearCache,
|
||||||
|
editIndex,
|
||||||
|
form,
|
||||||
|
handleBack,
|
||||||
|
providers,
|
||||||
|
showNotification,
|
||||||
|
t,
|
||||||
|
updateConfigValue,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Outlet
|
||||||
|
context={{
|
||||||
|
hasIndexParam,
|
||||||
|
editIndex,
|
||||||
|
invalidIndexParam,
|
||||||
|
invalidIndex,
|
||||||
|
disableControls,
|
||||||
|
loading,
|
||||||
|
saving,
|
||||||
|
form,
|
||||||
|
setForm,
|
||||||
|
testModel,
|
||||||
|
setTestModel,
|
||||||
|
testStatus,
|
||||||
|
setTestStatus,
|
||||||
|
testMessage,
|
||||||
|
setTestMessage,
|
||||||
|
availableModels,
|
||||||
|
handleBack,
|
||||||
|
handleSave,
|
||||||
|
mergeDiscoveredModels,
|
||||||
|
} satisfies OpenAIEditOutletContext}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
373
src/pages/AiProvidersOpenAIEditPage.tsx
Normal file
373
src/pages/AiProvidersOpenAIEditPage.tsx
Normal file
@@ -0,0 +1,373 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
import { useNavigate, useOutletContext } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { HeaderInputList } from '@/components/ui/HeaderInputList';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { ModelInputList } from '@/components/ui/ModelInputList';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { useNotificationStore } from '@/stores';
|
||||||
|
import { apiCallApi, getApiCallErrorMessage } from '@/services/api';
|
||||||
|
import type { ApiKeyEntry } from '@/types';
|
||||||
|
import { buildHeaderObject } from '@/utils/headers';
|
||||||
|
import { buildApiKeyEntry, buildOpenAIChatCompletionsEndpoint } from '@/components/providers/utils';
|
||||||
|
import type { OpenAIEditOutletContext } from './AiProvidersOpenAIEditLayout';
|
||||||
|
import styles from './AiProvidersPage.module.scss';
|
||||||
|
|
||||||
|
const OPENAI_TEST_TIMEOUT_MS = 30_000;
|
||||||
|
|
||||||
|
const getErrorMessage = (err: unknown) => {
|
||||||
|
if (err instanceof Error) return err.message;
|
||||||
|
if (typeof err === 'string') return err;
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersOpenAIEditPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const { showNotification } = useNotificationStore();
|
||||||
|
const {
|
||||||
|
hasIndexParam,
|
||||||
|
invalidIndexParam,
|
||||||
|
invalidIndex,
|
||||||
|
disableControls,
|
||||||
|
loading,
|
||||||
|
saving,
|
||||||
|
form,
|
||||||
|
setForm,
|
||||||
|
testModel,
|
||||||
|
setTestModel,
|
||||||
|
testStatus,
|
||||||
|
setTestStatus,
|
||||||
|
testMessage,
|
||||||
|
setTestMessage,
|
||||||
|
availableModels,
|
||||||
|
handleBack,
|
||||||
|
handleSave,
|
||||||
|
} = useOutletContext<OpenAIEditOutletContext>();
|
||||||
|
|
||||||
|
const title = hasIndexParam
|
||||||
|
? t('ai_providers.openai_edit_modal_title')
|
||||||
|
: t('ai_providers.openai_add_modal_title');
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
const canSave = !disableControls && !loading && !saving && !invalidIndexParam && !invalidIndex;
|
||||||
|
|
||||||
|
const renderKeyEntries = (entries: ApiKeyEntry[]) => {
|
||||||
|
const list = entries.length ? entries : [buildApiKeyEntry()];
|
||||||
|
|
||||||
|
const updateEntry = (idx: number, field: keyof ApiKeyEntry, value: string) => {
|
||||||
|
const next = list.map((entry, i) => (i === idx ? { ...entry, [field]: value } : entry));
|
||||||
|
setForm((prev) => ({ ...prev, apiKeyEntries: next }));
|
||||||
|
};
|
||||||
|
|
||||||
|
const removeEntry = (idx: number) => {
|
||||||
|
const next = list.filter((_, i) => i !== idx);
|
||||||
|
setForm((prev) => ({
|
||||||
|
...prev,
|
||||||
|
apiKeyEntries: next.length ? next : [buildApiKeyEntry()],
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const addEntry = () => {
|
||||||
|
setForm((prev) => ({ ...prev, apiKeyEntries: [...list, buildApiKeyEntry()] }));
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="stack">
|
||||||
|
{list.map((entry, index) => (
|
||||||
|
<div key={index} className="item-row">
|
||||||
|
<div className="item-meta">
|
||||||
|
<Input
|
||||||
|
label={`${t('common.api_key')} #${index + 1}`}
|
||||||
|
value={entry.apiKey}
|
||||||
|
onChange={(e) => updateEntry(index, 'apiKey', e.target.value)}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('common.proxy_url')}
|
||||||
|
value={entry.proxyUrl ?? ''}
|
||||||
|
onChange={(e) => updateEntry(index, 'proxyUrl', e.target.value)}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="item-actions">
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => removeEntry(index)}
|
||||||
|
disabled={saving || disableControls || list.length <= 1}
|
||||||
|
>
|
||||||
|
{t('common.delete')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
size="sm"
|
||||||
|
onClick={addEntry}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
>
|
||||||
|
{t('ai_providers.openai_keys_add_btn')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const openOpenaiModelDiscovery = () => {
|
||||||
|
const baseUrl = form.baseUrl.trim();
|
||||||
|
if (!baseUrl) {
|
||||||
|
showNotification(t('ai_providers.openai_models_fetch_invalid_url'), 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('models');
|
||||||
|
};
|
||||||
|
|
||||||
|
const testOpenaiProviderConnection = async () => {
|
||||||
|
const baseUrl = form.baseUrl.trim();
|
||||||
|
if (!baseUrl) {
|
||||||
|
const message = t('notification.openai_test_url_required');
|
||||||
|
setTestStatus('error');
|
||||||
|
setTestMessage(message);
|
||||||
|
showNotification(message, 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const endpoint = buildOpenAIChatCompletionsEndpoint(baseUrl);
|
||||||
|
if (!endpoint) {
|
||||||
|
const message = t('notification.openai_test_url_required');
|
||||||
|
setTestStatus('error');
|
||||||
|
setTestMessage(message);
|
||||||
|
showNotification(message, 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const firstKeyEntry = form.apiKeyEntries.find((entry) => entry.apiKey?.trim());
|
||||||
|
if (!firstKeyEntry) {
|
||||||
|
const message = t('notification.openai_test_key_required');
|
||||||
|
setTestStatus('error');
|
||||||
|
setTestMessage(message);
|
||||||
|
showNotification(message, 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const modelName = testModel.trim() || availableModels[0] || '';
|
||||||
|
if (!modelName) {
|
||||||
|
const message = t('notification.openai_test_model_required');
|
||||||
|
setTestStatus('error');
|
||||||
|
setTestMessage(message);
|
||||||
|
showNotification(message, 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const customHeaders = buildHeaderObject(form.headers);
|
||||||
|
const headers: Record<string, string> = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...customHeaders,
|
||||||
|
};
|
||||||
|
if (!headers.Authorization && !headers['authorization']) {
|
||||||
|
headers.Authorization = `Bearer ${firstKeyEntry.apiKey.trim()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
setTestStatus('loading');
|
||||||
|
setTestMessage(t('ai_providers.openai_test_running'));
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await apiCallApi.request(
|
||||||
|
{
|
||||||
|
method: 'POST',
|
||||||
|
url: endpoint,
|
||||||
|
header: Object.keys(headers).length ? headers : undefined,
|
||||||
|
data: JSON.stringify({
|
||||||
|
model: modelName,
|
||||||
|
messages: [{ role: 'user', content: 'Hi' }],
|
||||||
|
stream: false,
|
||||||
|
max_tokens: 5,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
{ timeout: OPENAI_TEST_TIMEOUT_MS }
|
||||||
|
);
|
||||||
|
|
||||||
|
if (result.statusCode < 200 || result.statusCode >= 300) {
|
||||||
|
throw new Error(getApiCallErrorMessage(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
setTestStatus('success');
|
||||||
|
setTestMessage(t('ai_providers.openai_test_success'));
|
||||||
|
} catch (err: unknown) {
|
||||||
|
setTestStatus('error');
|
||||||
|
const message = getErrorMessage(err);
|
||||||
|
const errorCode =
|
||||||
|
typeof err === 'object' && err !== null && 'code' in err
|
||||||
|
? String((err as { code?: string }).code)
|
||||||
|
: '';
|
||||||
|
const isTimeout = errorCode === 'ECONNABORTED' || message.toLowerCase().includes('timeout');
|
||||||
|
if (isTimeout) {
|
||||||
|
setTestMessage(
|
||||||
|
t('ai_providers.openai_test_timeout', { seconds: OPENAI_TEST_TIMEOUT_MS / 1000 })
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
setTestMessage(`${t('ai_providers.openai_test_failed')}: ${message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={title}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={() => void handleSave()} loading={saving} disabled={!canSave}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={loading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
{invalidIndexParam || invalidIndex ? (
|
||||||
|
<div className="hint">Invalid provider index.</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.openai_add_modal_name_label')}
|
||||||
|
value={form.name}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, name: e.target.value }))}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.prefix_label')}
|
||||||
|
placeholder={t('ai_providers.prefix_placeholder')}
|
||||||
|
value={form.prefix ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, prefix: e.target.value }))}
|
||||||
|
hint={t('ai_providers.prefix_hint')}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.openai_add_modal_url_label')}
|
||||||
|
value={form.baseUrl}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, baseUrl: e.target.value }))}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<HeaderInputList
|
||||||
|
entries={form.headers}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, headers: entries }))}
|
||||||
|
addLabel={t('common.custom_headers_add')}
|
||||||
|
keyPlaceholder={t('common.custom_headers_key_placeholder')}
|
||||||
|
valuePlaceholder={t('common.custom_headers_value_placeholder')}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<div className="form-group">
|
||||||
|
<label>
|
||||||
|
{hasIndexParam
|
||||||
|
? t('ai_providers.openai_edit_modal_models_label')
|
||||||
|
: t('ai_providers.openai_add_modal_models_label')}
|
||||||
|
</label>
|
||||||
|
<div className="hint">{t('ai_providers.openai_models_hint')}</div>
|
||||||
|
<ModelInputList
|
||||||
|
entries={form.modelEntries}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, modelEntries: entries }))}
|
||||||
|
addLabel={t('ai_providers.openai_models_add_btn')}
|
||||||
|
namePlaceholder={t('common.model_name_placeholder')}
|
||||||
|
aliasPlaceholder={t('common.model_alias_placeholder')}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
size="sm"
|
||||||
|
onClick={openOpenaiModelDiscovery}
|
||||||
|
disabled={saving || disableControls}
|
||||||
|
>
|
||||||
|
{t('ai_providers.openai_models_fetch_button')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.openai_test_title')}</label>
|
||||||
|
<div className="hint">{t('ai_providers.openai_test_hint')}</div>
|
||||||
|
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
|
||||||
|
<select
|
||||||
|
className={`input ${styles.openaiTestSelect}`}
|
||||||
|
value={testModel}
|
||||||
|
onChange={(e) => {
|
||||||
|
setTestModel(e.target.value);
|
||||||
|
setTestStatus('idle');
|
||||||
|
setTestMessage('');
|
||||||
|
}}
|
||||||
|
disabled={saving || disableControls || availableModels.length === 0}
|
||||||
|
>
|
||||||
|
<option value="">
|
||||||
|
{availableModels.length
|
||||||
|
? t('ai_providers.openai_test_select_placeholder')
|
||||||
|
: t('ai_providers.openai_test_select_empty')}
|
||||||
|
</option>
|
||||||
|
{form.modelEntries
|
||||||
|
.filter((entry) => entry.name.trim())
|
||||||
|
.map((entry, idx) => {
|
||||||
|
const name = entry.name.trim();
|
||||||
|
const alias = entry.alias.trim();
|
||||||
|
const label = alias && alias !== name ? `${name} (${alias})` : name;
|
||||||
|
return (
|
||||||
|
<option key={`${name}-${idx}`} value={name}>
|
||||||
|
{label}
|
||||||
|
</option>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</select>
|
||||||
|
<Button
|
||||||
|
variant={testStatus === 'error' ? 'danger' : 'secondary'}
|
||||||
|
className={`${styles.openaiTestButton} ${
|
||||||
|
testStatus === 'success' ? styles.openaiTestButtonSuccess : ''
|
||||||
|
}`}
|
||||||
|
onClick={() => void testOpenaiProviderConnection()}
|
||||||
|
loading={testStatus === 'loading'}
|
||||||
|
disabled={saving || disableControls || availableModels.length === 0}
|
||||||
|
>
|
||||||
|
{t('ai_providers.openai_test_action')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
{testMessage && (
|
||||||
|
<div
|
||||||
|
className={`status-badge ${
|
||||||
|
testStatus === 'error'
|
||||||
|
? 'error'
|
||||||
|
: testStatus === 'success'
|
||||||
|
? 'success'
|
||||||
|
: 'muted'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{testMessage}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.openai_add_modal_keys_label')}</label>
|
||||||
|
{renderKeyEntries(form.apiKeyEntries)}
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
222
src/pages/AiProvidersOpenAIModelsPage.tsx
Normal file
222
src/pages/AiProvidersOpenAIModelsPage.tsx
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { useNavigate, useOutletContext } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { modelsApi } from '@/services/api';
|
||||||
|
import type { ModelInfo } from '@/utils/models';
|
||||||
|
import { buildHeaderObject } from '@/utils/headers';
|
||||||
|
import { buildOpenAIModelsEndpoint } from '@/components/providers/utils';
|
||||||
|
import type { OpenAIEditOutletContext } from './AiProvidersOpenAIEditLayout';
|
||||||
|
import styles from './AiProvidersPage.module.scss';
|
||||||
|
|
||||||
|
const getErrorMessage = (err: unknown) => {
|
||||||
|
if (err instanceof Error) return err.message;
|
||||||
|
if (typeof err === 'string') return err;
|
||||||
|
return '';
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersOpenAIModelsPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const {
|
||||||
|
disableControls,
|
||||||
|
loading: initialLoading,
|
||||||
|
saving,
|
||||||
|
form,
|
||||||
|
mergeDiscoveredModels,
|
||||||
|
} = useOutletContext<OpenAIEditOutletContext>();
|
||||||
|
|
||||||
|
const [endpoint, setEndpoint] = useState('');
|
||||||
|
const [models, setModels] = useState<ModelInfo[]>([]);
|
||||||
|
const [fetching, setFetching] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [search, setSearch] = useState('');
|
||||||
|
const [selected, setSelected] = useState<Set<string>>(new Set());
|
||||||
|
|
||||||
|
const filteredModels = useMemo(() => {
|
||||||
|
const filter = search.trim().toLowerCase();
|
||||||
|
if (!filter) return models;
|
||||||
|
return models.filter((model) => {
|
||||||
|
const name = (model.name || '').toLowerCase();
|
||||||
|
const alias = (model.alias || '').toLowerCase();
|
||||||
|
const desc = (model.description || '').toLowerCase();
|
||||||
|
return name.includes(filter) || alias.includes(filter) || desc.includes(filter);
|
||||||
|
});
|
||||||
|
}, [models, search]);
|
||||||
|
|
||||||
|
const fetchOpenaiModelDiscovery = useCallback(
|
||||||
|
async ({ allowFallback = true }: { allowFallback?: boolean } = {}) => {
|
||||||
|
const trimmedBaseUrl = form.baseUrl.trim();
|
||||||
|
if (!trimmedBaseUrl) return;
|
||||||
|
|
||||||
|
setFetching(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
const headerObject = buildHeaderObject(form.headers);
|
||||||
|
const firstKey = form.apiKeyEntries.find((entry) => entry.apiKey?.trim())?.apiKey?.trim();
|
||||||
|
const hasAuthHeader = Boolean(headerObject.Authorization || headerObject['authorization']);
|
||||||
|
const list = await modelsApi.fetchModelsViaApiCall(
|
||||||
|
trimmedBaseUrl,
|
||||||
|
hasAuthHeader ? undefined : firstKey,
|
||||||
|
headerObject
|
||||||
|
);
|
||||||
|
setModels(list);
|
||||||
|
} catch (err: unknown) {
|
||||||
|
if (allowFallback) {
|
||||||
|
try {
|
||||||
|
const list = await modelsApi.fetchModelsViaApiCall(trimmedBaseUrl);
|
||||||
|
setModels(list);
|
||||||
|
return;
|
||||||
|
} catch (fallbackErr: unknown) {
|
||||||
|
const message = getErrorMessage(fallbackErr) || getErrorMessage(err);
|
||||||
|
setModels([]);
|
||||||
|
setError(`${t('ai_providers.openai_models_fetch_error')}: ${message}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
setModels([]);
|
||||||
|
setError(`${t('ai_providers.openai_models_fetch_error')}: ${getErrorMessage(err)}`);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setFetching(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[form.apiKeyEntries, form.baseUrl, form.headers, t]
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (initialLoading) return;
|
||||||
|
setEndpoint(buildOpenAIModelsEndpoint(form.baseUrl));
|
||||||
|
setModels([]);
|
||||||
|
setSearch('');
|
||||||
|
setSelected(new Set());
|
||||||
|
setError('');
|
||||||
|
void fetchOpenaiModelDiscovery();
|
||||||
|
}, [fetchOpenaiModelDiscovery, form.baseUrl, initialLoading]);
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
navigate(-1);
|
||||||
|
}, [navigate]);
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
const toggleSelection = (name: string) => {
|
||||||
|
setSelected((prev) => {
|
||||||
|
const next = new Set(prev);
|
||||||
|
if (next.has(name)) {
|
||||||
|
next.delete(name);
|
||||||
|
} else {
|
||||||
|
next.add(name);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleApply = () => {
|
||||||
|
const selectedModels = models.filter((model) => selected.has(model.name));
|
||||||
|
if (selectedModels.length) {
|
||||||
|
mergeDiscoveredModels(selectedModels);
|
||||||
|
}
|
||||||
|
handleBack();
|
||||||
|
};
|
||||||
|
|
||||||
|
const canApply = !disableControls && !saving && !fetching;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={t('ai_providers.openai_models_fetch_title')}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={handleApply} disabled={!canApply}>
|
||||||
|
{t('ai_providers.openai_models_fetch_apply')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={initialLoading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
<div className="hint" style={{ marginBottom: 8 }}>
|
||||||
|
{t('ai_providers.openai_models_fetch_hint')}
|
||||||
|
</div>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.openai_models_fetch_url_label')}</label>
|
||||||
|
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
|
||||||
|
<input className="input" readOnly value={endpoint} />
|
||||||
|
<Button
|
||||||
|
variant="secondary"
|
||||||
|
size="sm"
|
||||||
|
onClick={() => void fetchOpenaiModelDiscovery({ allowFallback: true })}
|
||||||
|
loading={fetching}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
>
|
||||||
|
{t('ai_providers.openai_models_fetch_refresh')}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.openai_models_search_label')}
|
||||||
|
placeholder={t('ai_providers.openai_models_search_placeholder')}
|
||||||
|
value={search}
|
||||||
|
onChange={(e) => setSearch(e.target.value)}
|
||||||
|
disabled={fetching}
|
||||||
|
/>
|
||||||
|
{error && <div className="error-box">{error}</div>}
|
||||||
|
{fetching ? (
|
||||||
|
<div className="hint">{t('ai_providers.openai_models_fetch_loading')}</div>
|
||||||
|
) : models.length === 0 ? (
|
||||||
|
<div className="hint">{t('ai_providers.openai_models_fetch_empty')}</div>
|
||||||
|
) : filteredModels.length === 0 ? (
|
||||||
|
<div className="hint">{t('ai_providers.openai_models_search_empty')}</div>
|
||||||
|
) : (
|
||||||
|
<div className={styles.modelDiscoveryList}>
|
||||||
|
{filteredModels.map((model) => {
|
||||||
|
const checked = selected.has(model.name);
|
||||||
|
return (
|
||||||
|
<label
|
||||||
|
key={model.name}
|
||||||
|
className={`${styles.modelDiscoveryRow} ${
|
||||||
|
checked ? styles.modelDiscoveryRowSelected : ''
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={checked}
|
||||||
|
onChange={() => toggleSelection(model.name)}
|
||||||
|
/>
|
||||||
|
<div className={styles.modelDiscoveryMeta}>
|
||||||
|
<div className={styles.modelDiscoveryName}>
|
||||||
|
{model.name}
|
||||||
|
{model.alias && (
|
||||||
|
<span className={styles.modelDiscoveryAlias}>{model.alias}</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{model.description && (
|
||||||
|
<div className={styles.modelDiscoveryDesc}>{model.description}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useCallback, useEffect, useState } from 'react';
|
import { useCallback, useEffect, useState } from 'react';
|
||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { entriesToModels } from '@/components/ui/ModelInputList';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
AmpcodeSection,
|
AmpcodeSection,
|
||||||
ClaudeSection,
|
ClaudeSection,
|
||||||
@@ -9,25 +9,19 @@ import {
|
|||||||
OpenAISection,
|
OpenAISection,
|
||||||
VertexSection,
|
VertexSection,
|
||||||
useProviderStats,
|
useProviderStats,
|
||||||
type GeminiFormState,
|
|
||||||
type OpenAIFormState,
|
|
||||||
type ProviderFormState,
|
|
||||||
type ProviderModal,
|
|
||||||
type VertexFormState,
|
|
||||||
} from '@/components/providers';
|
} from '@/components/providers';
|
||||||
import {
|
import {
|
||||||
parseExcludedModels,
|
|
||||||
withDisableAllModelsRule,
|
withDisableAllModelsRule,
|
||||||
withoutDisableAllModelsRule,
|
withoutDisableAllModelsRule,
|
||||||
} from '@/components/providers/utils';
|
} from '@/components/providers/utils';
|
||||||
import { ampcodeApi, providersApi } from '@/services/api';
|
import { ampcodeApi, providersApi } from '@/services/api';
|
||||||
import { useAuthStore, useConfigStore, useNotificationStore, useThemeStore } from '@/stores';
|
import { useAuthStore, useConfigStore, useNotificationStore, useThemeStore } from '@/stores';
|
||||||
import type { GeminiKeyConfig, OpenAIProviderConfig, ProviderKeyConfig } from '@/types';
|
import type { GeminiKeyConfig, OpenAIProviderConfig, ProviderKeyConfig } from '@/types';
|
||||||
import { buildHeaderObject } from '@/utils/headers';
|
|
||||||
import styles from './AiProvidersPage.module.scss';
|
import styles from './AiProvidersPage.module.scss';
|
||||||
|
|
||||||
export function AiProvidersPage() {
|
export function AiProvidersPage() {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
const { showNotification, showConfirmation } = useNotificationStore();
|
const { showNotification, showConfirmation } = useNotificationStore();
|
||||||
const resolvedTheme = useThemeStore((state) => state.resolvedTheme);
|
const resolvedTheme = useThemeStore((state) => state.resolvedTheme);
|
||||||
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
@@ -46,10 +40,7 @@ export function AiProvidersPage() {
|
|||||||
const [vertexConfigs, setVertexConfigs] = useState<ProviderKeyConfig[]>([]);
|
const [vertexConfigs, setVertexConfigs] = useState<ProviderKeyConfig[]>([]);
|
||||||
const [openaiProviders, setOpenaiProviders] = useState<OpenAIProviderConfig[]>([]);
|
const [openaiProviders, setOpenaiProviders] = useState<OpenAIProviderConfig[]>([]);
|
||||||
|
|
||||||
const [saving, setSaving] = useState(false);
|
|
||||||
const [configSwitchingKey, setConfigSwitchingKey] = useState<string | null>(null);
|
const [configSwitchingKey, setConfigSwitchingKey] = useState<string | null>(null);
|
||||||
const [modal, setModal] = useState<ProviderModal | null>(null);
|
|
||||||
const [ampcodeBusy, setAmpcodeBusy] = useState(false);
|
|
||||||
|
|
||||||
const disableControls = connectionStatus !== 'connected';
|
const disableControls = connectionStatus !== 'connected';
|
||||||
const isSwitching = Boolean(configSwitchingKey);
|
const isSwitching = Boolean(configSwitchingKey);
|
||||||
@@ -120,62 +111,12 @@ export function AiProvidersPage() {
|
|||||||
config?.openaiCompatibility,
|
config?.openaiCompatibility,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const closeModal = () => {
|
const openEditor = useCallback(
|
||||||
setModal(null);
|
(path: string) => {
|
||||||
};
|
navigate(path, { state: { fromAiProviders: true } });
|
||||||
|
},
|
||||||
const openGeminiModal = (index: number | null) => {
|
[navigate]
|
||||||
setModal({ type: 'gemini', index });
|
);
|
||||||
};
|
|
||||||
|
|
||||||
const openProviderModal = (type: 'codex' | 'claude', index: number | null) => {
|
|
||||||
setModal({ type, index });
|
|
||||||
};
|
|
||||||
|
|
||||||
const openVertexModal = (index: number | null) => {
|
|
||||||
setModal({ type: 'vertex', index });
|
|
||||||
};
|
|
||||||
|
|
||||||
const openAmpcodeModal = () => {
|
|
||||||
setModal({ type: 'ampcode', index: null });
|
|
||||||
};
|
|
||||||
|
|
||||||
const openOpenaiModal = (index: number | null) => {
|
|
||||||
setModal({ type: 'openai', index });
|
|
||||||
};
|
|
||||||
|
|
||||||
const saveGemini = async (form: GeminiFormState, editIndex: number | null) => {
|
|
||||||
setSaving(true);
|
|
||||||
try {
|
|
||||||
const payload: GeminiKeyConfig = {
|
|
||||||
apiKey: form.apiKey.trim(),
|
|
||||||
prefix: form.prefix?.trim() || undefined,
|
|
||||||
baseUrl: form.baseUrl?.trim() || undefined,
|
|
||||||
headers: buildHeaderObject(form.headers),
|
|
||||||
excludedModels: parseExcludedModels(form.excludedText),
|
|
||||||
};
|
|
||||||
const nextList =
|
|
||||||
editIndex !== null
|
|
||||||
? geminiKeys.map((item, idx) => (idx === editIndex ? payload : item))
|
|
||||||
: [...geminiKeys, payload];
|
|
||||||
|
|
||||||
await providersApi.saveGeminiKeys(nextList);
|
|
||||||
setGeminiKeys(nextList);
|
|
||||||
updateConfigValue('gemini-api-key', nextList);
|
|
||||||
clearCache('gemini-api-key');
|
|
||||||
const message =
|
|
||||||
editIndex !== null
|
|
||||||
? t('notification.gemini_key_updated')
|
|
||||||
: t('notification.gemini_key_added');
|
|
||||||
showNotification(message, 'success');
|
|
||||||
closeModal();
|
|
||||||
} catch (err: unknown) {
|
|
||||||
const message = getErrorMessage(err);
|
|
||||||
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteGemini = async (index: number) => {
|
const deleteGemini = async (index: number) => {
|
||||||
const entry = geminiKeys[index];
|
const entry = geminiKeys[index];
|
||||||
@@ -293,68 +234,6 @@ export function AiProvidersPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveProvider = async (
|
|
||||||
type: 'codex' | 'claude',
|
|
||||||
form: ProviderFormState,
|
|
||||||
editIndex: number | null
|
|
||||||
) => {
|
|
||||||
const trimmedBaseUrl = (form.baseUrl ?? '').trim();
|
|
||||||
const baseUrl = trimmedBaseUrl || undefined;
|
|
||||||
if (type === 'codex' && !baseUrl) {
|
|
||||||
showNotification(t('notification.codex_base_url_required'), 'error');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSaving(true);
|
|
||||||
try {
|
|
||||||
const source = type === 'codex' ? codexConfigs : claudeConfigs;
|
|
||||||
|
|
||||||
const payload: ProviderKeyConfig = {
|
|
||||||
apiKey: form.apiKey.trim(),
|
|
||||||
prefix: form.prefix?.trim() || undefined,
|
|
||||||
baseUrl,
|
|
||||||
proxyUrl: form.proxyUrl?.trim() || undefined,
|
|
||||||
headers: buildHeaderObject(form.headers),
|
|
||||||
models: entriesToModels(form.modelEntries),
|
|
||||||
excludedModels: parseExcludedModels(form.excludedText),
|
|
||||||
};
|
|
||||||
|
|
||||||
const nextList =
|
|
||||||
editIndex !== null
|
|
||||||
? source.map((item, idx) => (idx === editIndex ? payload : item))
|
|
||||||
: [...source, payload];
|
|
||||||
|
|
||||||
if (type === 'codex') {
|
|
||||||
await providersApi.saveCodexConfigs(nextList);
|
|
||||||
setCodexConfigs(nextList);
|
|
||||||
updateConfigValue('codex-api-key', nextList);
|
|
||||||
clearCache('codex-api-key');
|
|
||||||
const message =
|
|
||||||
editIndex !== null
|
|
||||||
? t('notification.codex_config_updated')
|
|
||||||
: t('notification.codex_config_added');
|
|
||||||
showNotification(message, 'success');
|
|
||||||
} else {
|
|
||||||
await providersApi.saveClaudeConfigs(nextList);
|
|
||||||
setClaudeConfigs(nextList);
|
|
||||||
updateConfigValue('claude-api-key', nextList);
|
|
||||||
clearCache('claude-api-key');
|
|
||||||
const message =
|
|
||||||
editIndex !== null
|
|
||||||
? t('notification.claude_config_updated')
|
|
||||||
: t('notification.claude_config_added');
|
|
||||||
showNotification(message, 'success');
|
|
||||||
}
|
|
||||||
|
|
||||||
closeModal();
|
|
||||||
} catch (err: unknown) {
|
|
||||||
const message = getErrorMessage(err);
|
|
||||||
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteProviderEntry = async (type: 'codex' | 'claude', index: number) => {
|
const deleteProviderEntry = async (type: 'codex' | 'claude', index: number) => {
|
||||||
const source = type === 'codex' ? codexConfigs : claudeConfigs;
|
const source = type === 'codex' ? codexConfigs : claudeConfigs;
|
||||||
const entry = source[index];
|
const entry = source[index];
|
||||||
@@ -389,55 +268,6 @@ export function AiProvidersPage() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveVertex = async (form: VertexFormState, editIndex: number | null) => {
|
|
||||||
const trimmedBaseUrl = (form.baseUrl ?? '').trim();
|
|
||||||
const baseUrl = trimmedBaseUrl || undefined;
|
|
||||||
if (!baseUrl) {
|
|
||||||
showNotification(t('notification.vertex_base_url_required'), 'error');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
setSaving(true);
|
|
||||||
try {
|
|
||||||
const payload: ProviderKeyConfig = {
|
|
||||||
apiKey: form.apiKey.trim(),
|
|
||||||
prefix: form.prefix?.trim() || undefined,
|
|
||||||
baseUrl,
|
|
||||||
proxyUrl: form.proxyUrl?.trim() || undefined,
|
|
||||||
headers: buildHeaderObject(form.headers),
|
|
||||||
models: form.modelEntries
|
|
||||||
.map((entry) => {
|
|
||||||
const name = entry.name.trim();
|
|
||||||
const alias = entry.alias.trim();
|
|
||||||
if (!name || !alias) return null;
|
|
||||||
return { name, alias };
|
|
||||||
})
|
|
||||||
.filter(Boolean) as ProviderKeyConfig['models'],
|
|
||||||
};
|
|
||||||
|
|
||||||
const nextList =
|
|
||||||
editIndex !== null
|
|
||||||
? vertexConfigs.map((item, idx) => (idx === editIndex ? payload : item))
|
|
||||||
: [...vertexConfigs, payload];
|
|
||||||
|
|
||||||
await providersApi.saveVertexConfigs(nextList);
|
|
||||||
setVertexConfigs(nextList);
|
|
||||||
updateConfigValue('vertex-api-key', nextList);
|
|
||||||
clearCache('vertex-api-key');
|
|
||||||
const message =
|
|
||||||
editIndex !== null
|
|
||||||
? t('notification.vertex_config_updated')
|
|
||||||
: t('notification.vertex_config_added');
|
|
||||||
showNotification(message, 'success');
|
|
||||||
closeModal();
|
|
||||||
} catch (err: unknown) {
|
|
||||||
const message = getErrorMessage(err);
|
|
||||||
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteVertex = async (index: number) => {
|
const deleteVertex = async (index: number) => {
|
||||||
const entry = vertexConfigs[index];
|
const entry = vertexConfigs[index];
|
||||||
if (!entry) return;
|
if (!entry) return;
|
||||||
@@ -462,47 +292,6 @@ export function AiProvidersPage() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const saveOpenai = async (form: OpenAIFormState, editIndex: number | null) => {
|
|
||||||
setSaving(true);
|
|
||||||
try {
|
|
||||||
const payload: OpenAIProviderConfig = {
|
|
||||||
name: form.name.trim(),
|
|
||||||
prefix: form.prefix?.trim() || undefined,
|
|
||||||
baseUrl: form.baseUrl.trim(),
|
|
||||||
headers: buildHeaderObject(form.headers),
|
|
||||||
apiKeyEntries: form.apiKeyEntries.map((entry) => ({
|
|
||||||
apiKey: entry.apiKey.trim(),
|
|
||||||
proxyUrl: entry.proxyUrl?.trim() || undefined,
|
|
||||||
headers: entry.headers,
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
if (form.testModel) payload.testModel = form.testModel.trim();
|
|
||||||
const models = entriesToModels(form.modelEntries);
|
|
||||||
if (models.length) payload.models = models;
|
|
||||||
|
|
||||||
const nextList =
|
|
||||||
editIndex !== null
|
|
||||||
? openaiProviders.map((item, idx) => (idx === editIndex ? payload : item))
|
|
||||||
: [...openaiProviders, payload];
|
|
||||||
|
|
||||||
await providersApi.saveOpenAIProviders(nextList);
|
|
||||||
setOpenaiProviders(nextList);
|
|
||||||
updateConfigValue('openai-compatibility', nextList);
|
|
||||||
clearCache('openai-compatibility');
|
|
||||||
const message =
|
|
||||||
editIndex !== null
|
|
||||||
? t('notification.openai_provider_updated')
|
|
||||||
: t('notification.openai_provider_added');
|
|
||||||
showNotification(message, 'success');
|
|
||||||
closeModal();
|
|
||||||
} catch (err: unknown) {
|
|
||||||
const message = getErrorMessage(err);
|
|
||||||
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
|
||||||
} finally {
|
|
||||||
setSaving(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const deleteOpenai = async (index: number) => {
|
const deleteOpenai = async (index: number) => {
|
||||||
const entry = openaiProviders[index];
|
const entry = openaiProviders[index];
|
||||||
if (!entry) return;
|
if (!entry) return;
|
||||||
@@ -527,12 +316,6 @@ export function AiProvidersPage() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const geminiModalIndex = modal?.type === 'gemini' ? modal.index : null;
|
|
||||||
const codexModalIndex = modal?.type === 'codex' ? modal.index : null;
|
|
||||||
const claudeModalIndex = modal?.type === 'claude' ? modal.index : null;
|
|
||||||
const vertexModalIndex = modal?.type === 'vertex' ? modal.index : null;
|
|
||||||
const openaiModalIndex = modal?.type === 'openai' ? modal.index : null;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
<h1 className={styles.pageTitle}>{t('ai_providers.title')}</h1>
|
<h1 className={styles.pageTitle}>{t('ai_providers.title')}</h1>
|
||||||
@@ -545,16 +328,11 @@ export function AiProvidersPage() {
|
|||||||
usageDetails={usageDetails}
|
usageDetails={usageDetails}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disableControls={disableControls}
|
disableControls={disableControls}
|
||||||
isSaving={saving}
|
|
||||||
isSwitching={isSwitching}
|
isSwitching={isSwitching}
|
||||||
isModalOpen={modal?.type === 'gemini'}
|
onAdd={() => openEditor('/ai-providers/gemini/new')}
|
||||||
modalIndex={geminiModalIndex}
|
onEdit={(index) => openEditor(`/ai-providers/gemini/${index}`)}
|
||||||
onAdd={() => openGeminiModal(null)}
|
|
||||||
onEdit={(index) => openGeminiModal(index)}
|
|
||||||
onDelete={deleteGemini}
|
onDelete={deleteGemini}
|
||||||
onToggle={(index, enabled) => void setConfigEnabled('gemini', index, enabled)}
|
onToggle={(index, enabled) => void setConfigEnabled('gemini', index, enabled)}
|
||||||
onCloseModal={closeModal}
|
|
||||||
onSave={saveGemini}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<CodexSection
|
<CodexSection
|
||||||
@@ -563,17 +341,12 @@ export function AiProvidersPage() {
|
|||||||
usageDetails={usageDetails}
|
usageDetails={usageDetails}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disableControls={disableControls}
|
disableControls={disableControls}
|
||||||
isSaving={saving}
|
|
||||||
isSwitching={isSwitching}
|
isSwitching={isSwitching}
|
||||||
resolvedTheme={resolvedTheme}
|
resolvedTheme={resolvedTheme}
|
||||||
isModalOpen={modal?.type === 'codex'}
|
onAdd={() => openEditor('/ai-providers/codex/new')}
|
||||||
modalIndex={codexModalIndex}
|
onEdit={(index) => openEditor(`/ai-providers/codex/${index}`)}
|
||||||
onAdd={() => openProviderModal('codex', null)}
|
|
||||||
onEdit={(index) => openProviderModal('codex', index)}
|
|
||||||
onDelete={(index) => void deleteProviderEntry('codex', index)}
|
onDelete={(index) => void deleteProviderEntry('codex', index)}
|
||||||
onToggle={(index, enabled) => void setConfigEnabled('codex', index, enabled)}
|
onToggle={(index, enabled) => void setConfigEnabled('codex', index, enabled)}
|
||||||
onCloseModal={closeModal}
|
|
||||||
onSave={(form, editIndex) => saveProvider('codex', form, editIndex)}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<ClaudeSection
|
<ClaudeSection
|
||||||
@@ -582,16 +355,11 @@ export function AiProvidersPage() {
|
|||||||
usageDetails={usageDetails}
|
usageDetails={usageDetails}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disableControls={disableControls}
|
disableControls={disableControls}
|
||||||
isSaving={saving}
|
|
||||||
isSwitching={isSwitching}
|
isSwitching={isSwitching}
|
||||||
isModalOpen={modal?.type === 'claude'}
|
onAdd={() => openEditor('/ai-providers/claude/new')}
|
||||||
modalIndex={claudeModalIndex}
|
onEdit={(index) => openEditor(`/ai-providers/claude/${index}`)}
|
||||||
onAdd={() => openProviderModal('claude', null)}
|
|
||||||
onEdit={(index) => openProviderModal('claude', index)}
|
|
||||||
onDelete={(index) => void deleteProviderEntry('claude', index)}
|
onDelete={(index) => void deleteProviderEntry('claude', index)}
|
||||||
onToggle={(index, enabled) => void setConfigEnabled('claude', index, enabled)}
|
onToggle={(index, enabled) => void setConfigEnabled('claude', index, enabled)}
|
||||||
onCloseModal={closeModal}
|
|
||||||
onSave={(form, editIndex) => saveProvider('claude', form, editIndex)}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<VertexSection
|
<VertexSection
|
||||||
@@ -600,28 +368,18 @@ export function AiProvidersPage() {
|
|||||||
usageDetails={usageDetails}
|
usageDetails={usageDetails}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disableControls={disableControls}
|
disableControls={disableControls}
|
||||||
isSaving={saving}
|
|
||||||
isSwitching={isSwitching}
|
isSwitching={isSwitching}
|
||||||
isModalOpen={modal?.type === 'vertex'}
|
onAdd={() => openEditor('/ai-providers/vertex/new')}
|
||||||
modalIndex={vertexModalIndex}
|
onEdit={(index) => openEditor(`/ai-providers/vertex/${index}`)}
|
||||||
onAdd={() => openVertexModal(null)}
|
|
||||||
onEdit={(index) => openVertexModal(index)}
|
|
||||||
onDelete={deleteVertex}
|
onDelete={deleteVertex}
|
||||||
onCloseModal={closeModal}
|
|
||||||
onSave={saveVertex}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<AmpcodeSection
|
<AmpcodeSection
|
||||||
config={config?.ampcode}
|
config={config?.ampcode}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disableControls={disableControls}
|
disableControls={disableControls}
|
||||||
isSaving={saving}
|
|
||||||
isSwitching={isSwitching}
|
isSwitching={isSwitching}
|
||||||
isBusy={ampcodeBusy}
|
onEdit={() => openEditor('/ai-providers/ampcode')}
|
||||||
isModalOpen={modal?.type === 'ampcode'}
|
|
||||||
onOpen={openAmpcodeModal}
|
|
||||||
onCloseModal={closeModal}
|
|
||||||
onBusyChange={setAmpcodeBusy}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<OpenAISection
|
<OpenAISection
|
||||||
@@ -630,16 +388,11 @@ export function AiProvidersPage() {
|
|||||||
usageDetails={usageDetails}
|
usageDetails={usageDetails}
|
||||||
loading={loading}
|
loading={loading}
|
||||||
disableControls={disableControls}
|
disableControls={disableControls}
|
||||||
isSaving={saving}
|
|
||||||
isSwitching={isSwitching}
|
isSwitching={isSwitching}
|
||||||
resolvedTheme={resolvedTheme}
|
resolvedTheme={resolvedTheme}
|
||||||
isModalOpen={modal?.type === 'openai'}
|
onAdd={() => openEditor('/ai-providers/openai/new')}
|
||||||
modalIndex={openaiModalIndex}
|
onEdit={(index) => openEditor(`/ai-providers/openai/${index}`)}
|
||||||
onAdd={() => openOpenaiModal(null)}
|
|
||||||
onEdit={(index) => openOpenaiModal(index)}
|
|
||||||
onDelete={deleteOpenai}
|
onDelete={deleteOpenai}
|
||||||
onCloseModal={closeModal}
|
|
||||||
onSave={saveOpenai}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
279
src/pages/AiProvidersVertexEditPage.tsx
Normal file
279
src/pages/AiProvidersVertexEditPage.tsx
Normal file
@@ -0,0 +1,279 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||||
|
import { useLocation, useNavigate, useParams } from 'react-router-dom';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { Card } from '@/components/ui/Card';
|
||||||
|
import { Button } from '@/components/ui/Button';
|
||||||
|
import { Input } from '@/components/ui/Input';
|
||||||
|
import { HeaderInputList } from '@/components/ui/HeaderInputList';
|
||||||
|
import { ModelInputList, modelsToEntries } from '@/components/ui/ModelInputList';
|
||||||
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
|
import { providersApi } from '@/services/api';
|
||||||
|
import { useAuthStore, useConfigStore, useNotificationStore } from '@/stores';
|
||||||
|
import type { ProviderKeyConfig } from '@/types';
|
||||||
|
import { buildHeaderObject, headersToEntries } from '@/utils/headers';
|
||||||
|
import type { VertexFormState } from '@/components/providers';
|
||||||
|
|
||||||
|
type LocationState = { fromAiProviders?: boolean } | null;
|
||||||
|
|
||||||
|
const buildEmptyForm = (): VertexFormState => ({
|
||||||
|
apiKey: '',
|
||||||
|
prefix: '',
|
||||||
|
baseUrl: '',
|
||||||
|
proxyUrl: '',
|
||||||
|
headers: [],
|
||||||
|
models: [],
|
||||||
|
modelEntries: [{ name: '', alias: '' }],
|
||||||
|
});
|
||||||
|
|
||||||
|
const parseIndexParam = (value: string | undefined) => {
|
||||||
|
if (!value) return null;
|
||||||
|
const parsed = Number.parseInt(value, 10);
|
||||||
|
return Number.isFinite(parsed) ? parsed : null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function AiProvidersVertexEditPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
const location = useLocation();
|
||||||
|
const params = useParams<{ index?: string }>();
|
||||||
|
|
||||||
|
const { showNotification } = useNotificationStore();
|
||||||
|
const connectionStatus = useAuthStore((state) => state.connectionStatus);
|
||||||
|
const disableControls = connectionStatus !== 'connected';
|
||||||
|
|
||||||
|
const fetchConfig = useConfigStore((state) => state.fetchConfig);
|
||||||
|
const updateConfigValue = useConfigStore((state) => state.updateConfigValue);
|
||||||
|
const clearCache = useConfigStore((state) => state.clearCache);
|
||||||
|
|
||||||
|
const [configs, setConfigs] = useState<ProviderKeyConfig[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [error, setError] = useState('');
|
||||||
|
const [form, setForm] = useState<VertexFormState>(() => buildEmptyForm());
|
||||||
|
const initializedRef = useRef(false);
|
||||||
|
|
||||||
|
const hasIndexParam = typeof params.index === 'string';
|
||||||
|
const editIndex = useMemo(() => parseIndexParam(params.index), [params.index]);
|
||||||
|
const invalidIndexParam = hasIndexParam && editIndex === null;
|
||||||
|
|
||||||
|
const initialData = useMemo(() => {
|
||||||
|
if (editIndex === null) return undefined;
|
||||||
|
return configs[editIndex];
|
||||||
|
}, [configs, editIndex]);
|
||||||
|
|
||||||
|
const invalidIndex = editIndex !== null && !initialData;
|
||||||
|
|
||||||
|
const title =
|
||||||
|
editIndex !== null ? t('ai_providers.vertex_edit_modal_title') : t('ai_providers.vertex_add_modal_title');
|
||||||
|
|
||||||
|
const handleBack = useCallback(() => {
|
||||||
|
const state = location.state as LocationState;
|
||||||
|
if (state?.fromAiProviders) {
|
||||||
|
navigate(-1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
navigate('/ai-providers', { replace: true });
|
||||||
|
}, [location.state, navigate]);
|
||||||
|
|
||||||
|
const swipeRef = useEdgeSwipeBack({ onBack: handleBack });
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
handleBack();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handleKeyDown);
|
||||||
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
||||||
|
}, [handleBack]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (initializedRef.current) return;
|
||||||
|
initializedRef.current = true;
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
Promise.all([fetchConfig('vertex-api-key'), providersApi.getVertexConfigs()])
|
||||||
|
.then(([configResult, vertexResult]) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
|
||||||
|
const list = Array.isArray(vertexResult)
|
||||||
|
? (vertexResult as ProviderKeyConfig[])
|
||||||
|
: Array.isArray(configResult)
|
||||||
|
? (configResult as ProviderKeyConfig[])
|
||||||
|
: [];
|
||||||
|
setConfigs(list);
|
||||||
|
updateConfigValue('vertex-api-key', list);
|
||||||
|
clearCache('vertex-api-key');
|
||||||
|
})
|
||||||
|
.catch((err: unknown) => {
|
||||||
|
if (cancelled) return;
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message || t('notification.refresh_failed'));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
if (cancelled) return;
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [clearCache, fetchConfig, t, updateConfigValue]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (loading) return;
|
||||||
|
|
||||||
|
if (initialData) {
|
||||||
|
setForm({
|
||||||
|
...initialData,
|
||||||
|
headers: headersToEntries(initialData.headers),
|
||||||
|
modelEntries: modelsToEntries(initialData.models),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setForm(buildEmptyForm());
|
||||||
|
}, [initialData, loading]);
|
||||||
|
|
||||||
|
const canSave = !disableControls && !saving && !loading && !invalidIndexParam && !invalidIndex;
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
if (!canSave) return;
|
||||||
|
|
||||||
|
const trimmedBaseUrl = (form.baseUrl ?? '').trim();
|
||||||
|
const baseUrl = trimmedBaseUrl || undefined;
|
||||||
|
if (!baseUrl) {
|
||||||
|
showNotification(t('notification.vertex_base_url_required'), 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSaving(true);
|
||||||
|
setError('');
|
||||||
|
try {
|
||||||
|
const payload: ProviderKeyConfig = {
|
||||||
|
apiKey: form.apiKey.trim(),
|
||||||
|
prefix: form.prefix?.trim() || undefined,
|
||||||
|
baseUrl,
|
||||||
|
proxyUrl: form.proxyUrl?.trim() || undefined,
|
||||||
|
headers: buildHeaderObject(form.headers),
|
||||||
|
models: form.modelEntries
|
||||||
|
.map((entry) => {
|
||||||
|
const name = entry.name.trim();
|
||||||
|
const alias = entry.alias.trim();
|
||||||
|
if (!name || !alias) return null;
|
||||||
|
return { name, alias };
|
||||||
|
})
|
||||||
|
.filter(Boolean) as ProviderKeyConfig['models'],
|
||||||
|
};
|
||||||
|
|
||||||
|
const nextList =
|
||||||
|
editIndex !== null
|
||||||
|
? configs.map((item, idx) => (idx === editIndex ? payload : item))
|
||||||
|
: [...configs, payload];
|
||||||
|
|
||||||
|
await providersApi.saveVertexConfigs(nextList);
|
||||||
|
updateConfigValue('vertex-api-key', nextList);
|
||||||
|
clearCache('vertex-api-key');
|
||||||
|
showNotification(
|
||||||
|
editIndex !== null ? t('notification.vertex_config_updated') : t('notification.vertex_config_added'),
|
||||||
|
'success'
|
||||||
|
);
|
||||||
|
handleBack();
|
||||||
|
} catch (err: unknown) {
|
||||||
|
const message = err instanceof Error ? err.message : '';
|
||||||
|
setError(message);
|
||||||
|
showNotification(`${t('notification.update_failed')}: ${message}`, 'error');
|
||||||
|
} finally {
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
|
}, [
|
||||||
|
canSave,
|
||||||
|
clearCache,
|
||||||
|
configs,
|
||||||
|
editIndex,
|
||||||
|
form,
|
||||||
|
handleBack,
|
||||||
|
showNotification,
|
||||||
|
t,
|
||||||
|
updateConfigValue,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<SecondaryScreenShell
|
||||||
|
ref={swipeRef}
|
||||||
|
title={title}
|
||||||
|
onBack={handleBack}
|
||||||
|
backLabel={t('common.back')}
|
||||||
|
backAriaLabel={t('common.back')}
|
||||||
|
rightAction={
|
||||||
|
<Button size="sm" onClick={handleSave} loading={saving} disabled={!canSave}>
|
||||||
|
{t('common.save')}
|
||||||
|
</Button>
|
||||||
|
}
|
||||||
|
isLoading={loading}
|
||||||
|
loadingLabel={t('common.loading')}
|
||||||
|
>
|
||||||
|
<Card>
|
||||||
|
{error && <div className="error-box">{error}</div>}
|
||||||
|
{invalidIndexParam || invalidIndex ? (
|
||||||
|
<div className="hint">Invalid provider index.</div>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.vertex_add_modal_key_label')}
|
||||||
|
placeholder={t('ai_providers.vertex_add_modal_key_placeholder')}
|
||||||
|
value={form.apiKey}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, apiKey: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.prefix_label')}
|
||||||
|
placeholder={t('ai_providers.prefix_placeholder')}
|
||||||
|
value={form.prefix ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, prefix: e.target.value }))}
|
||||||
|
hint={t('ai_providers.prefix_hint')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.vertex_add_modal_url_label')}
|
||||||
|
placeholder={t('ai_providers.vertex_add_modal_url_placeholder')}
|
||||||
|
value={form.baseUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, baseUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<Input
|
||||||
|
label={t('ai_providers.vertex_add_modal_proxy_label')}
|
||||||
|
placeholder={t('ai_providers.vertex_add_modal_proxy_placeholder')}
|
||||||
|
value={form.proxyUrl ?? ''}
|
||||||
|
onChange={(e) => setForm((prev) => ({ ...prev, proxyUrl: e.target.value }))}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<HeaderInputList
|
||||||
|
entries={form.headers}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, headers: entries }))}
|
||||||
|
addLabel={t('common.custom_headers_add')}
|
||||||
|
keyPlaceholder={t('common.custom_headers_key_placeholder')}
|
||||||
|
valuePlaceholder={t('common.custom_headers_value_placeholder')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="form-group">
|
||||||
|
<label>{t('ai_providers.vertex_models_label')}</label>
|
||||||
|
<ModelInputList
|
||||||
|
entries={form.modelEntries}
|
||||||
|
onChange={(entries) => setForm((prev) => ({ ...prev, modelEntries: entries }))}
|
||||||
|
addLabel={t('ai_providers.vertex_models_add_btn')}
|
||||||
|
namePlaceholder={t('common.model_name_placeholder')}
|
||||||
|
aliasPlaceholder={t('common.model_alias_placeholder')}
|
||||||
|
disabled={disableControls || saving}
|
||||||
|
/>
|
||||||
|
<div className="hint">{t('ai_providers.vertex_models_hint')}</div>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</SecondaryScreenShell>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,86 +1,6 @@
|
|||||||
@use '../styles/variables' as *;
|
@use '../styles/variables' as *;
|
||||||
@use '../styles/mixins' as *;
|
@use '../styles/mixins' as *;
|
||||||
|
|
||||||
.container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: $spacing-lg;
|
|
||||||
min-height: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.topBar {
|
|
||||||
position: sticky;
|
|
||||||
top: 0;
|
|
||||||
z-index: 5;
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 1fr auto 1fr;
|
|
||||||
align-items: center;
|
|
||||||
gap: $spacing-md;
|
|
||||||
padding: $spacing-sm $spacing-md;
|
|
||||||
background: var(--bg-secondary);
|
|
||||||
border-bottom: 1px solid var(--border-color);
|
|
||||||
min-height: 44px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.topBarTitle {
|
|
||||||
min-width: 0;
|
|
||||||
text-align: center;
|
|
||||||
font-size: 16px;
|
|
||||||
font-weight: 650;
|
|
||||||
color: var(--text-primary);
|
|
||||||
overflow: hidden;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
white-space: nowrap;
|
|
||||||
justify-self: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.backButton {
|
|
||||||
padding-left: 6px;
|
|
||||||
padding-right: 10px;
|
|
||||||
justify-self: start;
|
|
||||||
gap: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.backButton > span:last-child {
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 6px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.backIcon {
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
|
|
||||||
svg {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.backText {
|
|
||||||
font-weight: 600;
|
|
||||||
line-height: 18px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.saveButton {
|
|
||||||
justify-self: end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.loadingState {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
gap: $spacing-sm;
|
|
||||||
padding: $spacing-2xl 0;
|
|
||||||
color: var(--text-secondary);
|
|
||||||
}
|
|
||||||
|
|
||||||
.content {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: $spacing-lg;
|
|
||||||
}
|
|
||||||
|
|
||||||
.settingsCard {
|
.settingsCard {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
overflow: visible;
|
overflow: visible;
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import { Button } from '@/components/ui/Button';
|
|||||||
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
||||||
import { AutocompleteInput } from '@/components/ui/AutocompleteInput';
|
import { AutocompleteInput } from '@/components/ui/AutocompleteInput';
|
||||||
import { EmptyState } from '@/components/ui/EmptyState';
|
import { EmptyState } from '@/components/ui/EmptyState';
|
||||||
import { IconChevronLeft, IconInfo } from '@/components/ui/icons';
|
import { IconInfo } from '@/components/ui/icons';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
import { useAuthStore, useNotificationStore } from '@/stores';
|
import { useAuthStore, useNotificationStore } from '@/stores';
|
||||||
import { authFilesApi } from '@/services/api';
|
import { authFilesApi } from '@/services/api';
|
||||||
@@ -294,40 +295,21 @@ export function AuthFilesOAuthExcludedEditPage() {
|
|||||||
const canSave = !disableControls && !saving && !excludedUnsupported;
|
const canSave = !disableControls && !saving && !excludedUnsupported;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container} ref={swipeRef}>
|
<SecondaryScreenShell
|
||||||
<div className={styles.topBar}>
|
ref={swipeRef}
|
||||||
<Button
|
title={title}
|
||||||
variant="ghost"
|
onBack={handleBack}
|
||||||
size="sm"
|
backLabel={t('common.back')}
|
||||||
onClick={handleBack}
|
backAriaLabel={t('common.back')}
|
||||||
className={styles.backButton}
|
rightAction={
|
||||||
aria-label={t('common.back')}
|
<Button size="sm" onClick={handleSave} loading={saving} disabled={!canSave}>
|
||||||
>
|
|
||||||
<span className={styles.backIcon}>
|
|
||||||
<IconChevronLeft size={18} />
|
|
||||||
</span>
|
|
||||||
<span className={styles.backText}>{t('common.back')}</span>
|
|
||||||
</Button>
|
|
||||||
<div className={styles.topBarTitle} title={title}>
|
|
||||||
{title}
|
|
||||||
</div>
|
|
||||||
<Button
|
|
||||||
size="sm"
|
|
||||||
onClick={handleSave}
|
|
||||||
loading={saving}
|
|
||||||
disabled={!canSave}
|
|
||||||
className={styles.saveButton}
|
|
||||||
>
|
|
||||||
{t('oauth_excluded.save')}
|
{t('oauth_excluded.save')}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
}
|
||||||
|
isLoading={initialLoading}
|
||||||
{initialLoading ? (
|
loadingLabel={t('common.loading')}
|
||||||
<div className={styles.loadingState}>
|
>
|
||||||
<LoadingSpinner size={16} />
|
{excludedUnsupported ? (
|
||||||
<span>{t('common.loading')}</span>
|
|
||||||
</div>
|
|
||||||
) : excludedUnsupported ? (
|
|
||||||
<Card>
|
<Card>
|
||||||
<EmptyState
|
<EmptyState
|
||||||
title={t('oauth_excluded.upgrade_required_title')}
|
title={t('oauth_excluded.upgrade_required_title')}
|
||||||
@@ -335,7 +317,7 @@ export function AuthFilesOAuthExcludedEditPage() {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
) : (
|
) : (
|
||||||
<div className={styles.content}>
|
<>
|
||||||
<Card className={styles.settingsCard}>
|
<Card className={styles.settingsCard}>
|
||||||
<div className={styles.settingsHeader}>
|
<div className={styles.settingsHeader}>
|
||||||
<div className={styles.settingsHeaderTitle}>
|
<div className={styles.settingsHeaderTitle}>
|
||||||
@@ -443,8 +425,8 @@ export function AuthFilesOAuthExcludedEditPage() {
|
|||||||
<div className={styles.emptyModels}>{t('oauth_excluded.provider_required')}</div>
|
<div className={styles.emptyModels}>{t('oauth_excluded.provider_required')}</div>
|
||||||
)}
|
)}
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</SecondaryScreenShell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,86 +1,6 @@
|
|||||||
@use '../styles/variables' as *;
|
@use '../styles/variables' as *;
|
||||||
@use '../styles/mixins' as *;
|
@use '../styles/mixins' as *;
|
||||||
|
|
||||||
.container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: $spacing-lg;
|
|
||||||
min-height: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.topBar {
|
|
||||||
position: sticky;
|
|
||||||
top: 0;
|
|
||||||
z-index: 5;
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 1fr auto 1fr;
|
|
||||||
align-items: center;
|
|
||||||
gap: $spacing-md;
|
|
||||||
padding: $spacing-sm $spacing-md;
|
|
||||||
background: var(--bg-secondary);
|
|
||||||
border-bottom: 1px solid var(--border-color);
|
|
||||||
min-height: 44px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.topBarTitle {
|
|
||||||
min-width: 0;
|
|
||||||
text-align: center;
|
|
||||||
font-size: 16px;
|
|
||||||
font-weight: 650;
|
|
||||||
color: var(--text-primary);
|
|
||||||
overflow: hidden;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
white-space: nowrap;
|
|
||||||
justify-self: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.backButton {
|
|
||||||
padding-left: 6px;
|
|
||||||
padding-right: 10px;
|
|
||||||
justify-self: start;
|
|
||||||
gap: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.backButton > span:last-child {
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 6px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.backIcon {
|
|
||||||
display: inline-flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
|
|
||||||
svg {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.backText {
|
|
||||||
font-weight: 600;
|
|
||||||
line-height: 18px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.saveButton {
|
|
||||||
justify-self: end;
|
|
||||||
}
|
|
||||||
|
|
||||||
.loadingState {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
justify-content: center;
|
|
||||||
gap: $spacing-sm;
|
|
||||||
padding: $spacing-2xl 0;
|
|
||||||
color: var(--text-secondary);
|
|
||||||
}
|
|
||||||
|
|
||||||
.content {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
gap: $spacing-lg;
|
|
||||||
}
|
|
||||||
|
|
||||||
.settingsCard {
|
.settingsCard {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
overflow: visible;
|
overflow: visible;
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
import { Card } from '@/components/ui/Card';
|
import { Card } from '@/components/ui/Card';
|
||||||
import { Button } from '@/components/ui/Button';
|
import { Button } from '@/components/ui/Button';
|
||||||
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
|
|
||||||
import { AutocompleteInput } from '@/components/ui/AutocompleteInput';
|
import { AutocompleteInput } from '@/components/ui/AutocompleteInput';
|
||||||
import { EmptyState } from '@/components/ui/EmptyState';
|
import { EmptyState } from '@/components/ui/EmptyState';
|
||||||
import { ToggleSwitch } from '@/components/ui/ToggleSwitch';
|
import { ToggleSwitch } from '@/components/ui/ToggleSwitch';
|
||||||
import { IconChevronLeft, IconInfo, IconX } from '@/components/ui/icons';
|
import { IconInfo, IconX } from '@/components/ui/icons';
|
||||||
|
import { SecondaryScreenShell } from '@/components/common/SecondaryScreenShell';
|
||||||
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
import { useEdgeSwipeBack } from '@/hooks/useEdgeSwipeBack';
|
||||||
import { useAuthStore, useNotificationStore } from '@/stores';
|
import { useAuthStore, useNotificationStore } from '@/stores';
|
||||||
import { authFilesApi } from '@/services/api';
|
import { authFilesApi } from '@/services/api';
|
||||||
@@ -341,40 +341,21 @@ export function AuthFilesOAuthModelAliasEditPage() {
|
|||||||
const canSave = !disableControls && !saving && !modelAliasUnsupported;
|
const canSave = !disableControls && !saving && !modelAliasUnsupported;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container} ref={swipeRef}>
|
<SecondaryScreenShell
|
||||||
<div className={styles.topBar}>
|
ref={swipeRef}
|
||||||
<Button
|
title={title}
|
||||||
variant="ghost"
|
onBack={handleBack}
|
||||||
size="sm"
|
backLabel={t('common.back')}
|
||||||
onClick={handleBack}
|
backAriaLabel={t('common.back')}
|
||||||
className={styles.backButton}
|
rightAction={
|
||||||
aria-label={t('common.back')}
|
<Button size="sm" onClick={handleSave} loading={saving} disabled={!canSave}>
|
||||||
>
|
|
||||||
<span className={styles.backIcon}>
|
|
||||||
<IconChevronLeft size={18} />
|
|
||||||
</span>
|
|
||||||
<span className={styles.backText}>{t('common.back')}</span>
|
|
||||||
</Button>
|
|
||||||
<div className={styles.topBarTitle} title={title}>
|
|
||||||
{title}
|
|
||||||
</div>
|
|
||||||
<Button
|
|
||||||
size="sm"
|
|
||||||
onClick={handleSave}
|
|
||||||
loading={saving}
|
|
||||||
disabled={!canSave}
|
|
||||||
className={styles.saveButton}
|
|
||||||
>
|
|
||||||
{t('oauth_model_alias.save')}
|
{t('oauth_model_alias.save')}
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
}
|
||||||
|
isLoading={initialLoading}
|
||||||
{initialLoading ? (
|
loadingLabel={t('common.loading')}
|
||||||
<div className={styles.loadingState}>
|
>
|
||||||
<LoadingSpinner size={16} />
|
{modelAliasUnsupported ? (
|
||||||
<span>{t('common.loading')}</span>
|
|
||||||
</div>
|
|
||||||
) : modelAliasUnsupported ? (
|
|
||||||
<Card>
|
<Card>
|
||||||
<EmptyState
|
<EmptyState
|
||||||
title={t('oauth_model_alias.upgrade_required_title')}
|
title={t('oauth_model_alias.upgrade_required_title')}
|
||||||
@@ -382,7 +363,7 @@ export function AuthFilesOAuthModelAliasEditPage() {
|
|||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
) : (
|
) : (
|
||||||
<div className={styles.content}>
|
<>
|
||||||
<Card className={styles.settingsCard}>
|
<Card className={styles.settingsCard}>
|
||||||
<div className={styles.settingsHeader}>
|
<div className={styles.settingsHeader}>
|
||||||
<div className={styles.settingsHeaderTitle}>
|
<div className={styles.settingsHeaderTitle}>
|
||||||
@@ -493,8 +474,8 @@ export function AuthFilesOAuthModelAliasEditPage() {
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</SecondaryScreenShell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,14 @@ import { DashboardPage } from '@/pages/DashboardPage';
|
|||||||
import { SettingsPage } from '@/pages/SettingsPage';
|
import { SettingsPage } from '@/pages/SettingsPage';
|
||||||
import { ApiKeysPage } from '@/pages/ApiKeysPage';
|
import { ApiKeysPage } from '@/pages/ApiKeysPage';
|
||||||
import { AiProvidersPage } from '@/pages/AiProvidersPage';
|
import { AiProvidersPage } from '@/pages/AiProvidersPage';
|
||||||
|
import { AiProvidersAmpcodeEditPage } from '@/pages/AiProvidersAmpcodeEditPage';
|
||||||
|
import { AiProvidersClaudeEditPage } from '@/pages/AiProvidersClaudeEditPage';
|
||||||
|
import { AiProvidersCodexEditPage } from '@/pages/AiProvidersCodexEditPage';
|
||||||
|
import { AiProvidersGeminiEditPage } from '@/pages/AiProvidersGeminiEditPage';
|
||||||
|
import { AiProvidersOpenAIEditLayout } from '@/pages/AiProvidersOpenAIEditLayout';
|
||||||
|
import { AiProvidersOpenAIEditPage } from '@/pages/AiProvidersOpenAIEditPage';
|
||||||
|
import { AiProvidersOpenAIModelsPage } from '@/pages/AiProvidersOpenAIModelsPage';
|
||||||
|
import { AiProvidersVertexEditPage } from '@/pages/AiProvidersVertexEditPage';
|
||||||
import { AuthFilesPage } from '@/pages/AuthFilesPage';
|
import { AuthFilesPage } from '@/pages/AuthFilesPage';
|
||||||
import { AuthFilesOAuthExcludedEditPage } from '@/pages/AuthFilesOAuthExcludedEditPage';
|
import { AuthFilesOAuthExcludedEditPage } from '@/pages/AuthFilesOAuthExcludedEditPage';
|
||||||
import { AuthFilesOAuthModelAliasEditPage } from '@/pages/AuthFilesOAuthModelAliasEditPage';
|
import { AuthFilesOAuthModelAliasEditPage } from '@/pages/AuthFilesOAuthModelAliasEditPage';
|
||||||
@@ -18,7 +26,33 @@ const mainRoutes = [
|
|||||||
{ path: '/dashboard', element: <DashboardPage /> },
|
{ path: '/dashboard', element: <DashboardPage /> },
|
||||||
{ path: '/settings', element: <SettingsPage /> },
|
{ path: '/settings', element: <SettingsPage /> },
|
||||||
{ path: '/api-keys', element: <ApiKeysPage /> },
|
{ path: '/api-keys', element: <ApiKeysPage /> },
|
||||||
|
{ path: '/ai-providers/gemini/new', element: <AiProvidersGeminiEditPage /> },
|
||||||
|
{ path: '/ai-providers/gemini/:index', element: <AiProvidersGeminiEditPage /> },
|
||||||
|
{ path: '/ai-providers/codex/new', element: <AiProvidersCodexEditPage /> },
|
||||||
|
{ path: '/ai-providers/codex/:index', element: <AiProvidersCodexEditPage /> },
|
||||||
|
{ path: '/ai-providers/claude/new', element: <AiProvidersClaudeEditPage /> },
|
||||||
|
{ path: '/ai-providers/claude/:index', element: <AiProvidersClaudeEditPage /> },
|
||||||
|
{ path: '/ai-providers/vertex/new', element: <AiProvidersVertexEditPage /> },
|
||||||
|
{ path: '/ai-providers/vertex/:index', element: <AiProvidersVertexEditPage /> },
|
||||||
|
{
|
||||||
|
path: '/ai-providers/openai/new',
|
||||||
|
element: <AiProvidersOpenAIEditLayout />,
|
||||||
|
children: [
|
||||||
|
{ index: true, element: <AiProvidersOpenAIEditPage /> },
|
||||||
|
{ path: 'models', element: <AiProvidersOpenAIModelsPage /> },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '/ai-providers/openai/:index',
|
||||||
|
element: <AiProvidersOpenAIEditLayout />,
|
||||||
|
children: [
|
||||||
|
{ index: true, element: <AiProvidersOpenAIEditPage /> },
|
||||||
|
{ path: 'models', element: <AiProvidersOpenAIModelsPage /> },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{ path: '/ai-providers/ampcode', element: <AiProvidersAmpcodeEditPage /> },
|
||||||
{ path: '/ai-providers', element: <AiProvidersPage /> },
|
{ path: '/ai-providers', element: <AiProvidersPage /> },
|
||||||
|
{ path: '/ai-providers/*', element: <AiProvidersPage /> },
|
||||||
{ path: '/auth-files', element: <AuthFilesPage /> },
|
{ path: '/auth-files', element: <AuthFilesPage /> },
|
||||||
{ path: '/auth-files/oauth-excluded', element: <AuthFilesOAuthExcludedEditPage /> },
|
{ path: '/auth-files/oauth-excluded', element: <AuthFilesOAuthExcludedEditPage /> },
|
||||||
{ path: '/auth-files/oauth-model-alias', element: <AuthFilesOAuthModelAliasEditPage /> },
|
{ path: '/auth-files/oauth-model-alias', element: <AuthFilesOAuthModelAliasEditPage /> },
|
||||||
|
|||||||
Reference in New Issue
Block a user