feat(models): add models configuration page with provider management and token usage

- Replace "Task Center" with "Models" in sidebar and routing
- Add new models configuration page with AI provider account management
- Implement token usage history with filtering, grouping, and visualization
- Create provider store with Pinia for state management
- Add internationalization support for models feature
- Include development analysis documentation for feature implementation
This commit is contained in:
duanshuwen
2026-04-09 21:49:52 +08:00
parent 6d2991e595
commit 5f542715cb
18 changed files with 2040 additions and 25 deletions

46
src/lib/host-api.ts Normal file
View File

@@ -0,0 +1,46 @@
import { IPC_EVENTS } from '@lib/constants';
export async function hostApiFetch<T>(path: string, init?: RequestInit): Promise<T> {
const method = init?.method || 'GET';
try {
// Attempt to call via IPC if window.api exists
if ((window as any).api && (window as any).api.invoke) {
const response = await (window as any).api.invoke('hostapi:fetch', {
path,
method,
headers: init?.headers || {},
body: init?.body ?? null,
});
if (response && typeof response === 'object') {
if (response.success || response.ok) {
return (response.json ?? response.data?.json ?? response.data ?? []) as T;
} else {
throw new Error(response.error || response.text || 'Request failed');
}
}
return response as T;
}
} catch (error) {
console.warn(`[hostApiFetch] IPC request failed for ${path}:`, error);
}
// Dummy fallback for UI development if backend is not ready
console.warn(`[hostApiFetch] Using dummy data for ${path}`);
if (path.includes('recent-token-history')) {
return [] as any as T;
}
if (path.includes('provider-accounts')) {
return (method === 'GET' ? [] : { success: true }) as any as T;
}
if (path.includes('providers')) {
return (method === 'GET' ? [] : { success: true }) as any as T;
}
if (path.includes('provider-vendors')) {
return [] as any as T;
}
return {} as T;
}

View File

@@ -0,0 +1,126 @@
import { hostApiFetch } from '@lib/host-api';
import type {
ProviderAccount,
ProviderType,
ProviderVendorInfo,
ProviderWithKeyInfo,
} from '@lib/providers';
export interface ProviderSnapshot {
accounts: ProviderAccount[];
statuses: ProviderWithKeyInfo[];
vendors: ProviderVendorInfo[];
defaultAccountId: string | null;
}
export interface ProviderListItem {
account: ProviderAccount;
vendor?: ProviderVendorInfo;
status?: ProviderWithKeyInfo;
}
export async function fetchProviderSnapshot(): Promise<ProviderSnapshot> {
const [accounts, statuses, vendors, defaultInfo] = await Promise.all([
hostApiFetch<ProviderAccount[]>('/api/provider-accounts'),
hostApiFetch<ProviderWithKeyInfo[]>('/api/providers'),
hostApiFetch<ProviderVendorInfo[]>('/api/provider-vendors'),
hostApiFetch<{ accountId: string | null }>('/api/provider-accounts/default'),
]);
return {
accounts,
statuses,
vendors,
defaultAccountId: defaultInfo.accountId,
};
}
export function hasConfiguredCredentials(
account: ProviderAccount,
status?: ProviderWithKeyInfo,
): boolean {
if (account.authMode === 'oauth_device' || account.authMode === 'oauth_browser' || account.authMode === 'local') {
return true;
}
return status?.hasKey ?? false;
}
export function pickPreferredAccount(
accounts: ProviderAccount[],
defaultAccountId: string | null,
vendorId: ProviderType | string,
statusMap: Map<string, ProviderWithKeyInfo>,
): ProviderAccount | null {
const sameVendor = accounts.filter((account) => account.vendorId === vendorId);
if (sameVendor.length === 0) return null;
return (
(defaultAccountId ? sameVendor.find((account) => account.id === defaultAccountId) : undefined)
|| sameVendor.find((account) => hasConfiguredCredentials(account, statusMap.get(account.id)))
|| sameVendor[0]
);
}
export function buildProviderAccountId(
vendorId: ProviderType,
existingAccountId: string | null,
vendors: ProviderVendorInfo[],
): string {
if (existingAccountId) {
return existingAccountId;
}
const vendor = vendors.find((candidate) => candidate.id === vendorId);
return vendor?.supportsMultipleAccounts ? `${vendorId}-${crypto.randomUUID()}` : vendorId;
}
export function legacyProviderToAccount(provider: ProviderWithKeyInfo): ProviderAccount {
return {
id: provider.id,
vendorId: provider.type,
label: provider.name,
authMode: provider.type === 'ollama' ? 'local' : 'api_key',
baseUrl: provider.baseUrl,
headers: provider.headers,
model: provider.model,
fallbackModels: provider.fallbackModels,
fallbackAccountIds: provider.fallbackProviderIds,
enabled: provider.enabled,
isDefault: false,
createdAt: provider.createdAt,
updatedAt: provider.updatedAt,
};
}
export function buildProviderListItems(
accounts: ProviderAccount[],
statuses: ProviderWithKeyInfo[],
vendors: ProviderVendorInfo[],
defaultAccountId: string | null,
): ProviderListItem[] {
const safeAccounts = accounts ?? [];
const safeStatuses = statuses ?? [];
const safeVendors = vendors ?? [];
const vendorMap = new Map(safeVendors.map((vendor) => [vendor.id, vendor]));
const statusMap = new Map(safeStatuses.map((status) => [status.id, status]));
if (safeAccounts.length > 0) {
return safeAccounts
.map((account) => ({
account,
vendor: vendorMap.get(account.vendorId),
status: statusMap.get(account.id),
}))
.sort((left, right) => {
if (left.account.id === defaultAccountId) return -1;
if (right.account.id === defaultAccountId) return 1;
return right.account.updatedAt.localeCompare(left.account.updatedAt);
});
}
return safeStatuses.map((status) => ({
account: legacyProviderToAccount(status),
vendor: vendorMap.get(status.type),
status,
}));
}

236
src/lib/providers.ts Normal file
View File

@@ -0,0 +1,236 @@
export const PROVIDER_TYPES = [
'anthropic',
'openai',
'google',
'openrouter',
'ark',
'moonshot',
'siliconflow',
'minimax-portal',
'minimax-portal-cn',
'modelstudio',
'ollama',
'custom',
] as const;
export type ProviderType = (typeof PROVIDER_TYPES)[number];
export const BUILTIN_PROVIDER_TYPES = [
'anthropic',
'openai',
'google',
'openrouter',
'ark',
'moonshot',
'siliconflow',
'minimax-portal',
'minimax-portal-cn',
'modelstudio',
'ollama',
] as const;
export const OLLAMA_PLACEHOLDER_API_KEY = 'ollama-local';
export interface ProviderConfig {
id: string;
name: string;
type: ProviderType;
baseUrl?: string;
apiProtocol?: 'openai-completions' | 'openai-responses' | 'anthropic-messages';
headers?: Record<string, string>;
model?: string;
fallbackModels?: string[];
fallbackProviderIds?: string[];
enabled: boolean;
createdAt: string;
updatedAt: string;
}
export interface ProviderWithKeyInfo extends ProviderConfig {
hasKey: boolean;
keyMasked: string | null;
}
export interface ProviderTypeInfo {
id: ProviderType;
name: string;
icon: string;
placeholder: string;
model?: string;
requiresApiKey: boolean;
defaultBaseUrl?: string;
showBaseUrl?: boolean;
showModelId?: boolean;
showModelIdInDevModeOnly?: boolean;
modelIdPlaceholder?: string;
defaultModelId?: string;
isOAuth?: boolean;
supportsApiKey?: boolean;
apiKeyUrl?: string;
docsUrl?: string;
docsUrlZh?: string;
codePlanPresetBaseUrl?: string;
codePlanPresetModelId?: string;
codePlanDocsUrl?: string;
hidden?: boolean;
}
export type ProviderAuthMode =
| 'api_key'
| 'oauth_device'
| 'oauth_browser'
| 'local';
export type ProviderVendorCategory =
| 'official'
| 'compatible'
| 'local'
| 'custom';
export interface ProviderVendorInfo extends ProviderTypeInfo {
category: ProviderVendorCategory;
envVar?: string;
supportedAuthModes: ProviderAuthMode[];
defaultAuthMode: ProviderAuthMode;
supportsMultipleAccounts: boolean;
}
export interface ProviderAccount {
id: string;
vendorId: ProviderType;
label: string;
authMode: ProviderAuthMode;
baseUrl?: string;
apiProtocol?: 'openai-completions' | 'openai-responses' | 'anthropic-messages';
headers?: Record<string, string>;
model?: string;
fallbackModels?: string[];
fallbackAccountIds?: string[];
enabled: boolean;
isDefault: boolean;
metadata?: {
region?: string;
email?: string;
resourceUrl?: string;
customModels?: string[];
};
createdAt: string;
updatedAt: string;
}
export const PROVIDER_TYPE_INFO: ProviderTypeInfo[] = [
{
id: 'anthropic',
name: 'Anthropic',
icon: '🤖',
placeholder: 'sk-ant-api03-...',
model: 'Claude',
requiresApiKey: true,
docsUrl: 'https://platform.claude.com/docs/en/api/overview',
},
{
id: 'openai',
name: 'OpenAI',
icon: '💚',
placeholder: 'sk-proj-...',
model: 'GPT',
requiresApiKey: true,
isOAuth: true,
supportsApiKey: true,
defaultModelId: 'gpt-5.4',
showModelId: true,
showModelIdInDevModeOnly: true,
modelIdPlaceholder: 'gpt-5.4',
apiKeyUrl: 'https://platform.openai.com/api-keys',
},
{
id: 'google',
name: 'Google',
icon: '🔷',
placeholder: 'AIza...',
model: 'Gemini',
requiresApiKey: true,
isOAuth: true,
supportsApiKey: true,
defaultModelId: 'gemini-3-pro-preview',
showModelId: true,
showModelIdInDevModeOnly: true,
modelIdPlaceholder: 'gemini-3-pro-preview',
apiKeyUrl: 'https://aistudio.google.com/app/apikey',
},
{ id: 'openrouter', name: 'OpenRouter', icon: '🌐', placeholder: 'sk-or-v1-...', model: 'Multi-Model', requiresApiKey: true, showModelId: true, modelIdPlaceholder: 'openai/gpt-5.4', defaultModelId: 'openai/gpt-5.4', docsUrl: 'https://openrouter.ai/models' },
{ id: 'minimax-portal-cn', name: 'MiniMax (CN)', icon: '☁️', placeholder: 'sk-...', model: 'MiniMax', requiresApiKey: false, isOAuth: true, supportsApiKey: true, defaultModelId: 'MiniMax-M2.7', showModelId: true, showModelIdInDevModeOnly: true, modelIdPlaceholder: 'MiniMax-M2.7', apiKeyUrl: 'https://platform.minimaxi.com/' },
{ id: 'moonshot', name: 'Moonshot (CN)', icon: '🌙', placeholder: 'sk-...', model: 'Kimi', requiresApiKey: true, defaultBaseUrl: 'https://api.moonshot.cn/v1', defaultModelId: 'kimi-k2.5', docsUrl: 'https://platform.moonshot.cn/' },
{ id: 'siliconflow', name: 'SiliconFlow (CN)', icon: '🌊', placeholder: 'sk-...', model: 'Multi-Model', requiresApiKey: true, defaultBaseUrl: 'https://api.siliconflow.cn/v1', showModelId: true, showModelIdInDevModeOnly: true, modelIdPlaceholder: 'deepseek-ai/DeepSeek-V3', defaultModelId: 'deepseek-ai/DeepSeek-V3', docsUrl: 'https://docs.siliconflow.cn/cn/userguide/introduction' },
{ id: 'minimax-portal', name: 'MiniMax (Global)', icon: '☁️', placeholder: 'sk-...', model: 'MiniMax', requiresApiKey: false, isOAuth: true, supportsApiKey: true, defaultModelId: 'MiniMax-M2.7', showModelId: true, showModelIdInDevModeOnly: true, modelIdPlaceholder: 'MiniMax-M2.7', apiKeyUrl: 'https://platform.minimax.io' },
{ id: 'modelstudio', name: 'Model Studio', icon: '☁️', placeholder: 'sk-...', model: 'Qwen', requiresApiKey: true, defaultBaseUrl: 'https://coding.dashscope.aliyuncs.com/v1', showBaseUrl: true, defaultModelId: 'qwen3.5-plus', showModelId: true, showModelIdInDevModeOnly: true, modelIdPlaceholder: 'qwen3.5-plus', apiKeyUrl: 'https://bailian.console.aliyun.com/', hidden: true },
{ id: 'ark', name: 'ByteDance Ark', icon: 'A', placeholder: 'your-ark-api-key', model: 'Doubao', requiresApiKey: true, defaultBaseUrl: 'https://ark.cn-beijing.volces.com/api/v3', showBaseUrl: true, showModelId: true, modelIdPlaceholder: 'ep-20260228000000-xxxxx', docsUrl: 'https://www.volcengine.com/', codePlanPresetBaseUrl: 'https://ark.cn-beijing.volces.com/api/coding/v3', codePlanPresetModelId: 'ark-code-latest', codePlanDocsUrl: 'https://www.volcengine.com/docs/82379/1928261?lang=zh' },
{ id: 'ollama', name: 'Ollama', icon: '🦙', placeholder: 'Not required', requiresApiKey: false, defaultBaseUrl: 'http://localhost:11434/v1', showBaseUrl: true, showModelId: true, modelIdPlaceholder: 'qwen3:latest' },
{
id: 'custom',
name: 'Custom',
icon: '⚙️',
placeholder: 'API key...',
requiresApiKey: true,
showBaseUrl: true,
showModelId: true,
modelIdPlaceholder: 'your-provider/model-id',
docsUrl: 'https://icnnp7d0dymg.feishu.cn/wiki/BmiLwGBcEiloZDkdYnGc8RWnn6d#Ee1ldfvKJoVGvfxc32mcILwenth',
docsUrlZh: 'https://icnnp7d0dymg.feishu.cn/wiki/BmiLwGBcEiloZDkdYnGc8RWnn6d#IWQCdfe5fobGU3xf3UGcgbLynGh',
},
];
export function getProviderIconUrl(type: ProviderType | string): string | undefined {
return undefined;
}
export function shouldInvertInDark(_type: ProviderType | string): boolean {
return true;
}
export const SETUP_PROVIDERS = PROVIDER_TYPE_INFO;
export function getProviderTypeInfo(type: ProviderType): ProviderTypeInfo | undefined {
return PROVIDER_TYPE_INFO.find((t) => t.id === type);
}
export function getProviderDocsUrl(
provider: Pick<ProviderTypeInfo, 'docsUrl' | 'docsUrlZh'> | undefined,
language: string
): string | undefined {
if (!provider?.docsUrl) {
return undefined;
}
if (language.startsWith('zh') && provider.docsUrlZh) {
return provider.docsUrlZh;
}
return provider.docsUrl;
}
export function shouldShowProviderModelId(
provider: Pick<ProviderTypeInfo, 'showModelId' | 'showModelIdInDevModeOnly'> | undefined,
devModeUnlocked: boolean
): boolean {
if (!provider?.showModelId) return false;
if (provider.showModelIdInDevModeOnly && !devModeUnlocked) return false;
return true;
}
export function resolveProviderModelForSave(
provider: Pick<ProviderTypeInfo, 'defaultModelId' | 'showModelId' | 'showModelIdInDevModeOnly'> | undefined,
modelId: string,
devModeUnlocked: boolean
): string | undefined {
if (!shouldShowProviderModelId(provider, devModeUnlocked)) {
return undefined;
}
const trimmedModelId = modelId.trim();
return trimmedModelId || provider?.defaultModelId || undefined;
}
export function resolveProviderApiKeyForSave(type: ProviderType | string, apiKey: string): string | undefined {
const trimmed = apiKey.trim();
if (type === 'ollama') {
return trimmed || OLLAMA_PLACEHOLDER_API_KEY;
}
return trimmed || undefined;
}