Files
MyGoNavi/frontend/src/utils/aiProviderEditorState.ts
Syngnat fdcbadf918 🐛 fix(connection-modal): 支持编辑态回填已保存密码并保持默认遮罩
- 编辑连接前主动拉取可编辑配置,恢复主密码与 SSH 等已保存密钥
- 支持 AI 供应商编辑态回填 API Key,并保持默认遮罩展示
- 修正 AI 设置长错误提示换行展示,避免测试连接报错被裁切

Refs #489
2026-05-30 17:25:58 +08:00

83 lines
2.0 KiB
TypeScript

import type { AIProviderConfig, AIProviderType } from '../types';
type ProviderEditorStatus = 'idle' | 'success' | 'error';
type ProviderEditorConfig = Partial<AIProviderConfig> & Pick<AIProviderConfig, 'id' | 'type' | 'name' | 'apiKey'> & { presetKey?: string };
export interface ProviderEditorSession {
editingProvider: ProviderEditorConfig | null;
formValues: Record<string, unknown> | null;
isEditing: boolean;
testStatus: ProviderEditorStatus;
}
interface BuildAddProviderEditorSessionInput {
presetKey?: string;
presetBackendType: AIProviderType;
presetBaseUrl: string;
presetModel: string;
presetModels?: string[];
apiFormat?: string;
}
interface BuildEditProviderEditorSessionInput {
provider: ProviderEditorConfig;
formValues?: Record<string, unknown>;
}
export const buildAddProviderEditorSession = ({
presetKey = 'openai',
presetBackendType,
presetBaseUrl,
presetModel,
presetModels = [],
apiFormat = 'openai',
}: BuildAddProviderEditorSessionInput): ProviderEditorSession => {
const editingProvider: ProviderEditorConfig = {
id: '',
type: presetBackendType,
name: '',
apiKey: '',
baseUrl: presetBaseUrl,
model: presetModel,
models: [...presetModels],
maxTokens: 4096,
temperature: 0.7,
presetKey,
};
return {
editingProvider,
formValues: {
...editingProvider,
presetKey,
apiFormat,
},
isEditing: true,
testStatus: 'idle',
};
};
export const buildEditProviderEditorSession = ({
provider,
formValues,
}: BuildEditProviderEditorSessionInput): ProviderEditorSession => ({
editingProvider: provider,
formValues: formValues || {
...provider,
models: provider.models || [],
presetKey: provider.presetKey,
apiFormat: provider.apiFormat || 'openai',
},
isEditing: true,
testStatus: 'idle',
});
export const buildClosedProviderEditorSession = (): ProviderEditorSession => ({
editingProvider: null,
formValues: null,
isEditing: false,
testStatus: 'idle',
});