File size: 939 Bytes
7324743
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// utils.js
export function getRandomIDPro(size) {
    const customDict = '0123456789';
    return Array.from({ length: size }, () =>
        customDict[Math.floor(Math.random() * customDict.length)]
    ).join('');
}

export function validateRequest(req) {
    const { messages, model, stream } = req;

    if (!Array.isArray(messages) || messages.length === 0) {
        throw new ValidationError('Messages array is required and cannot be empty');
    }

    if (!messages.every(msg =>
        msg.role && msg.content &&
        ['system', 'user', 'assistant'].includes(msg.role)
    )) {
        throw new ValidationError('Invalid message format');
    }

    return { messages, model, stream: Boolean(stream) };
}

export async function withTimeout(promise, ms) {
    const timeout = new Promise((_, reject) => {
        setTimeout(() => reject(new Error('Request timeout')), ms);
    });
    return Promise.race([promise, timeout]);
}