File size: 3,194 Bytes
3abb4df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
 * mock-api.js
 * 模拟图片生成API服务器,用于测试和演示
 */

import express from 'express';
import { randomBytes } from 'crypto';

const app = express();
const port = 8000;

app.use(express.json());

// 生成简单的SVG图片作为响应
function generateMockImage(prompt) {
    const colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7', '#DDA0DD'];
    const color = colors[Math.floor(Math.random() * colors.length)];
    const seed = randomBytes(4).toString('hex');
    
    const svg = `
<svg xmlns="http://www.w3.org/2000/svg" width="512" height="512" viewBox="0 0 512 512">
    <defs>
        <linearGradient id="grad${seed}" x1="0%" y1="0%" x2="100%" y2="100%">
            <stop offset="0%" style="stop-color:${color};stop-opacity:1" />
            <stop offset="100%" style="stop-color:#FFF;stop-opacity:1" />
        </linearGradient>
    </defs>
    <rect width="512" height="512" fill="url(#grad${seed})"/>
    <circle cx="256" cy="256" r="100" fill="${color}" opacity="0.8"/>
    <text x="256" y="280" text-anchor="middle" fill="white" font-family="Arial" font-size="16" font-weight="bold">
        AI Generated
    </text>
    <text x="256" y="300" text-anchor="middle" fill="white" font-family="Arial" font-size="12">
        ${prompt.substring(0, 20)}${prompt.length > 20 ? '...' : ''}
    </text>
</svg>`;

    return `data:image/svg+xml;base64,${Buffer.from(svg).toString('base64')}`;
}

// OpenAI兼容的聊天完成端点
app.post('/v1/chat/completions', (req, res) => {
    const { messages, model } = req.body;
    
    console.log(`[Mock API] 收到生成请求:`, {
        model,
        messageCount: messages?.length,
        lastMessage: messages?.[messages.length - 1]?.content?.substring(0, 100) + '...'
    });

    const userPrompt = messages?.[messages.length - 1]?.content || '';
    
    // 模拟处理延迟
    setTimeout(() => {
        const mockImage = generateMockImage(userPrompt);
        
        const response = {
            id: `chatcmpl-${randomBytes(8).toString('hex')}`,
            object: 'chat.completion',
            created: Math.floor(Date.now() / 1000),
            model: model || 'banana-pro',
            choices: [{
                index: 0,
                message: {
                    role: 'assistant',
                    content: `![Generated Image](${mockImage})`
                },
                finish_reason: 'stop'
            }],
            usage: {
                prompt_tokens: 50,
                completion_tokens: 10,
                total_tokens: 60
            }
        };

        res.json(response);
    }, 2000 + Math.random() * 3000); // 2-5秒随机延迟
});

// 健康检查
app.get('/health', (req, res) => {
    res.json({ status: 'ok', message: 'Mock API is running' });
});

app.listen(port, () => {
    console.log('==========================================');
    console.log('🤖 Mock API 服务器已启动');
    console.log('==========================================');
    console.log(`📡 服务地址: http://localhost:${port}`);
    console.log('🎨 模拟图片生成功能');
    console.log('==========================================');
});