-
Notifications
You must be signed in to change notification settings - Fork 11
feat: 提升自定义提供商克隆体验 #261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SurviveM
merged 1 commit into
awsl-project:main
from
ymkiux:feat/disable-error-cooldown
Feb 25, 2026
+122
−8
Merged
feat: 提升自定义提供商克隆体验 #261
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { | |
| Key, | ||
| Check, | ||
| Trash2, | ||
| Copy, | ||
| Plus, | ||
| ArrowRight, | ||
| Zap, | ||
|
|
@@ -20,6 +21,7 @@ import { | |
| DialogFooter, | ||
| } from '@/components/ui/dialog'; | ||
| import { | ||
| useCreateProvider, | ||
| useUpdateProvider, | ||
| useDeleteProvider, | ||
| useModelMappings, | ||
|
|
@@ -280,11 +282,16 @@ type EditFormData = { | |
| export function ProviderEditFlow({ provider, onClose }: ProviderEditFlowProps) { | ||
| const { t } = useTranslation(); | ||
| const [saving, setSaving] = useState(false); | ||
| const [cloning, setCloning] = useState(false); | ||
| const [cloneToastMessage, setCloneToastMessage] = useState<string | null>(null); | ||
| const [deleting, setDeleting] = useState(false); | ||
| const [saveStatus, setSaveStatus] = useState<'idle' | 'success' | 'error'>('idle'); | ||
| const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); | ||
| const createProvider = useCreateProvider(); | ||
| const updateProvider = useUpdateProvider(); | ||
| const deleteProvider = useDeleteProvider(); | ||
| const createModelMapping = useCreateModelMapping(); | ||
| const { data: allMappings } = useModelMappings(); | ||
|
|
||
| const initClients = (): ClientConfig[] => { | ||
| const supportedTypes = provider.supportedClientTypes || []; | ||
|
|
@@ -323,20 +330,20 @@ export function ProviderEditFlow({ provider, onClose }: ProviderEditFlowProps) { | |
| return hasEnabledClient && hasUrl; | ||
| }; | ||
|
|
||
| const parseSensitiveWords = (value: string): string[] => { | ||
| return value | ||
| .split(/[\n,]/) | ||
| .map((item) => item.trim()) | ||
| .filter(Boolean); | ||
| }; | ||
|
|
||
| const handleSave = async () => { | ||
| if (!isValid()) return; | ||
|
|
||
| setSaving(true); | ||
| setSaveStatus('idle'); | ||
|
|
||
| try { | ||
| const parseSensitiveWords = (value: string): string[] => { | ||
| return value | ||
| .split(/[\n,]/) | ||
| .map((item) => item.trim()) | ||
| .filter(Boolean); | ||
| }; | ||
|
|
||
| const supportedClientTypes = formData.clients.filter((c) => c.enabled).map((c) => c.id); | ||
| const clientBaseURL: Partial<Record<ClientType, string>> = {}; | ||
| const clientMultiplier: Partial<Record<ClientType, number>> = {}; | ||
|
|
@@ -387,6 +394,89 @@ export function ProviderEditFlow({ provider, onClose }: ProviderEditFlowProps) { | |
| } | ||
| }; | ||
|
|
||
| const handleClone = async () => { | ||
| if (!isValid() || cloning || cloneToastMessage) return; | ||
|
|
||
| setCloning(true); | ||
|
|
||
| try { | ||
| const supportedClientTypes = formData.clients.filter((c) => c.enabled).map((c) => c.id); | ||
| const clientBaseURL: Partial<Record<ClientType, string>> = {}; | ||
| const clientMultiplier: Partial<Record<ClientType, number>> = {}; | ||
| formData.clients.forEach((c) => { | ||
| if (c.enabled && c.urlOverride) { | ||
| clientBaseURL[c.id] = c.urlOverride; | ||
| } | ||
| if (c.enabled && c.multiplier !== 10000) { | ||
| clientMultiplier[c.id] = c.multiplier; | ||
| } | ||
| }); | ||
|
|
||
| const baseName = formData.name.trim() || provider.name; | ||
| const suffix = t('provider.cloneSuffix'); | ||
| const cloneName = baseName.endsWith(suffix) ? baseName : `${baseName}${suffix}`; | ||
|
|
||
| const data: CreateProviderData = { | ||
| type: provider.type || 'custom', | ||
| name: cloneName, | ||
| logo: provider.logo, | ||
| config: { | ||
| disableErrorCooldown: !!formData.disableErrorCooldown, | ||
| custom: { | ||
| baseURL: formData.baseURL, | ||
| apiKey: formData.apiKey || provider.config?.custom?.apiKey || '', | ||
| clientBaseURL: Object.keys(clientBaseURL).length > 0 ? clientBaseURL : undefined, | ||
| clientMultiplier: | ||
| Object.keys(clientMultiplier).length > 0 ? clientMultiplier : undefined, | ||
| cloak: | ||
| formData.cloakMode !== 'auto' || | ||
| formData.cloakStrictMode || | ||
| parseSensitiveWords(formData.cloakSensitiveWords || '').length > 0 | ||
| ? { | ||
| mode: formData.cloakMode, | ||
| strictMode: formData.cloakStrictMode, | ||
| sensitiveWords: parseSensitiveWords(formData.cloakSensitiveWords || ''), | ||
| } | ||
| : undefined, | ||
| }, | ||
| }, | ||
| supportedClientTypes, | ||
| supportModels: formData.supportModels.length > 0 ? formData.supportModels : undefined, | ||
| }; | ||
|
|
||
| const newProvider = await createProvider.mutateAsync(data); | ||
|
|
||
| const providerMappings = (allMappings || []).filter( | ||
| (mapping) => mapping.scope === 'provider' && mapping.providerID === provider.id, | ||
| ); | ||
|
|
||
| if (providerMappings.length > 0) { | ||
| for (const mapping of providerMappings) { | ||
| await createModelMapping.mutateAsync({ | ||
| scope: mapping.scope, | ||
| clientType: mapping.clientType, | ||
| providerType: mapping.providerType, | ||
| providerID: newProvider.id, | ||
| projectID: mapping.projectID, | ||
| routeID: mapping.routeID, | ||
| apiTokenID: mapping.apiTokenID, | ||
| pattern: mapping.pattern, | ||
| target: mapping.target, | ||
| priority: mapping.priority, | ||
| isEnabled: mapping.isEnabled, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| setCloneToastMessage(t('provider.cloneSuccess', { name: cloneName })); | ||
| setTimeout(() => onClose(), 800); | ||
| } catch (error) { | ||
| console.error('Failed to clone provider:', error); | ||
| } finally { | ||
| setCloning(false); | ||
| } | ||
|
Comment on lines
+453
to
+477
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 映射复制失败会产生“部分克隆”且缺少明确失败反馈。 当前实现中,provider 创建成功后若某条映射复制失败,会进入 🔧 建议修复- if (providerMappings.length > 0) {
- for (const mapping of providerMappings) {
- await createModelMapping.mutateAsync({
- ...
- });
- }
- }
-
- setCloneToastMessage(t('provider.cloneSuccess', { name: cloneName }));
+ let failedCount = 0;
+ for (const mapping of providerMappings) {
+ try {
+ await createModelMapping.mutateAsync({
+ ...
+ });
+ } catch (err) {
+ failedCount += 1;
+ }
+ }
+
+ setCloneToastMessage(
+ failedCount === 0
+ ? t('provider.cloneSuccess', { name: cloneName })
+ : t('provider.clonePartialSuccess', { name: cloneName, failed: failedCount }),
+ );
...
} catch (error) {
console.error('Failed to clone provider:', error);
+ setCloneToastMessage(t('provider.cloneError'));
}Also applies to: 717-721 🤖 Prompt for AI Agents |
||
| }; | ||
|
|
||
| const handleDelete = async () => { | ||
| setDeleting(true); | ||
| try { | ||
|
|
@@ -472,6 +562,14 @@ export function ProviderEditFlow({ provider, onClose }: ProviderEditFlowProps) { | |
| <Trash2 size={14} /> | ||
| {t('provider.delete')} | ||
| </Button> | ||
| <Button | ||
| onClick={handleClone} | ||
| disabled={cloning || saving || !isValid() || !!cloneToastMessage} | ||
| variant={'outline'} | ||
| > | ||
| <Copy size={14} /> | ||
| {cloning ? t('provider.cloning') : t('provider.clone')} | ||
| </Button> | ||
| <Button onClick={onClose} variant={'secondary'}> | ||
| {t('provider.cancel')} | ||
| </Button> | ||
|
|
@@ -616,6 +714,12 @@ export function ProviderEditFlow({ provider, onClose }: ProviderEditFlowProps) { | |
| </div> | ||
| </div> | ||
|
|
||
| {cloneToastMessage && ( | ||
| <div className="fixed bottom-6 right-6 bg-card border border-border rounded-lg shadow-lg p-4 z-50"> | ||
| <div className="text-sm font-medium text-foreground">{cloneToastMessage}</div> | ||
| </div> | ||
| )} | ||
|
|
||
| <DeleteConfirmModal | ||
| providerName={provider.name} | ||
| deleting={deleting} | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
克隆时存在“映射未加载即被当成空集合”的正确性风险。
当
allMappings还在加载时,当前实现会直接继续克隆并提示成功,导致新 provider 可能没有复制到任何映射。建议在映射加载完成前禁用克隆,或在克隆前显式拉取一次映射。🔧 建议修复
Also applies to: 449-451, 565-568
🤖 Prompt for AI Agents