File size: 1,803 Bytes
b24a0b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use server';

/**
 * Server-side API validation utilities.
 * These functions check which API keys are present and valid.
 */

import { type ImageProvider, PROVIDER_CONFIGS } from './api-config';

/**
 * Check if a specific provider's API key is present and non-empty.
 */
export async function isProviderAvailable(provider: ImageProvider): Promise<boolean> {
    const config = PROVIDER_CONFIGS[provider];
    const apiKey = process.env[config.envKey];

    // Check if the API key exists and is not a placeholder
    if (!apiKey || apiKey.trim() === '') {
        return false;
    }

    // Check if it's a placeholder value (common patterns)
    const placeholderPatterns = [
        'your-',
        'YOUR_',
        'xxx',
        'placeholder',
        'insert-',
        'api-key-here',
    ];

    const lowerKey = apiKey.toLowerCase();
    for (const pattern of placeholderPatterns) {
        if (lowerKey.includes(pattern.toLowerCase())) {
            return false;
        }
    }

    return true;
}

/**
 * Get all available providers that have valid API keys configured.
 */
export async function getAvailableProviders(): Promise<ImageProvider[]> {
    const providers: ImageProvider[] = ['openai', 'google', 'qwen'];
    const availableProviders: ImageProvider[] = [];

    for (const provider of providers) {
        if (await isProviderAvailable(provider)) {
            availableProviders.push(provider);
        }
    }

    return availableProviders;
}

/**
 * Get detailed information about provider availability.
 */
export async function getProviderStatus(): Promise<Record<ImageProvider, boolean>> {
    return {
        openai: await isProviderAvailable('openai'),
        google: await isProviderAvailable('google'),
        qwen: await isProviderAvailable('qwen'),
    };
}