File size: 1,962 Bytes
d36ee3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

export type DrawCommand = {
    trigger: boolean;
    type?: 'image' | 'video'; 
    description: string;
    negativePrompt: string;
    charId?: string; 
    outfitId?: string; 
    videoModel?: string;
    parameters?: any;
};

export const cleanDrawTag = (content: string) => {
    return content
        .replace(/<draw>[\s\S]*?<\/draw>/g, "")
        .replace(/<draw_log>[\s\S]*?<\/draw_log>/g, "")
        .replace(/<video>[\s\S]*?<\/video>/g, "")     
        .replace(/<video_log>[\s\S]*?<\/video_log>/g, "") 
        .trim();
};

export const parseMediaTag = (content: string): DrawCommand | null => {
    const videoMatch = content.match(/<video>([\s\S]*?)<\/video>/);
    if (videoMatch) return parseInnerJson(videoMatch[1], 'video');

    const drawMatch = content.match(/<draw>([\s\S]*?)<\/draw>/);
    if (drawMatch) return parseInnerJson(drawMatch[1], 'image');

    return null;
};

const parseInnerJson = (rawContent: string, type: 'image' | 'video'): DrawCommand | null => {
    try {
        const jsonString = rawContent.replace(/```json/g, "").replace(/```/g, "").trim();
        const data = JSON.parse(jsonString);

        const desc = data.description || data.prompt || "";
        let rawCharId = data.charId || data.character || data.id;
        if (Array.isArray(rawCharId)) rawCharId = rawCharId[0];
        
        return {
            trigger: true,
            type: type, 
            description: desc,
            charId: rawCharId,
            outfitId: data.outfitId || "casual",
            videoModel: data.model, 
            parameters: data.parameters || {}, 
            negativePrompt: data.negativePrompt || data.neg || ""
        };
    } catch (e) {
        return { 
            trigger: true, 
            type: type, 
            description: rawContent, 
            parameters: {},
            negativePrompt: "",
            outfitId: "casual"
        };
    }
}

export const parseDrawTag = parseMediaTag;