File size: 6,827 Bytes
9d28b0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

saved_prompts_refresh_btn__click_js = """(global_state_json, saved_prompts)=>{
    try {
        if(global_state_json=="") {global_state_json=null;};
        console.log('global_state_json:\\n', global_state_json);
        const global_state = JSON.parse(global_state_json??"{ }")??{ };

        const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
        console.log('saved:\\n', saved);
        global_state['saved_prompts'] = saved;
        global_state['selected_saved_prompt_title'] = saved.map(it=>it?.title??"[untitled]")[0];

        const results = [JSON.stringify(global_state), global_state['selected_saved_prompt_title']];
        console.log(results);
        return results;
    } catch(error) {
        console.log(error);
        return ["{ }", ""];
    };
}"""


selected_saved_prompt_title__change_js = """(global_state_json, selected_saved_prompt_title)=>{
    if(global_state_json=="") {global_state_json=null;};
    const global_state = JSON.parse(global_state_json??"{ }")??{ };
    const found = (global_state?.['saved_prompts']??[]).find(it=>it?.title==selected_saved_prompt_title);
    return [JSON.stringify(global_state), found?.title??'', found?.content??{data:[], headers:["role", "content"]}];
}"""


saved_prompts_delete_btn__click_js = """(global_state_json, saved_prompts, prompt_title, prompt_table)=>{
    if(prompt_title==""||!prompt_title){
        return [global_state_json, selected_saved_prompt_title, prompt_title, prompt_table];
    };
    console.log('global_state_json:\\n', global_state_json);

    if(global_state_json=="") {global_state_json=null;};
    const global_state = JSON.parse(global_state_json??"{ }")??{ };
    console.log(global_state);

    const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
    console.log('saved:\\n', saved);


    global_state['saved_prompts'] = saved?.filter?.(it=>it.title!=prompt_title)??[];

    global_state['selected_saved_prompt_title'] = "";

    console.log(global_state);

    localStorage?.setItem?.('[gradio][chat-gpt-ui][prompts]', JSON.stringify(global_state['saved_prompts']));

    return [JSON.stringify(global_state), "", "", {data: [], headers: ['role', 'content']}];
}"""


saved_prompts_save_btn__click_js = """(global_state_json, saved_prompts, prompt_title, prompt_table)=>{
    if(prompt_title==""||!prompt_title){
        return [global_state_json, selected_saved_prompt_title, prompt_title, prompt_table];
    };
    console.log('global_state_json:\\n', global_state_json);

    if(global_state_json=="") {global_state_json=null;};
    const global_state = JSON.parse(global_state_json??"{ }")??{ };
    console.log(global_state);

    const saved = (JSON.parse(localStorage?.getItem?.('[gradio][chat-gpt-ui][prompts]') ?? '[]'));
    console.log('saved:\\n', saved);


    const new_prompt_obj = {
        title: prompt_title, content: prompt_table,
    };

    global_state['saved_prompts'] = saved?.filter?.(it=>it.title!=prompt_title)??[];

    global_state['saved_prompts'].unshift(new_prompt_obj);

    global_state['selected_saved_prompt_title'] = prompt_title;

    console.log(global_state);

    localStorage?.setItem?.('[gradio][chat-gpt-ui][prompts]', JSON.stringify(global_state['saved_prompts']));

    return [JSON.stringify(global_state), prompt_title, prompt_title, prompt_table];
}"""


copy_prompt__click_js = """(prompt_title, prompt_table)=>{
    try {
        const txt = JSON.stringify({
            title: prompt_title,
            content: prompt_table,
        }, null, 2);
        console.log(txt);
        const promise = navigator?.clipboard?.writeText?.(txt);
    } catch(error) {console?.log?.(error);};
    return [prompt_title, prompt_table];
}"""


paste_prompt__click_js = """async (prompt_title, prompt_table)=>{
    console.log("flag1");
    try {
        const promise = navigator?.clipboard?.readText?.();
        console.log(promise);
        console.log("flag1 p");
        const result = await promise?.then?.((txt)=>{
            console.log("flag1 t");
            const json = JSON.parse(txt);
            const title = json?.title ?? "";
            console.log("flag1 0");
            console.log(title);
            const content = json?.content ?? {data: [], headers: ['role', 'content']};
            console.log(content);
            const result = [title, content];
            console.log("flag1 1");
            console.log(result);
            console.log("flag1 2");
            return result;
        });
        console.log("flag1 3");
        if (result!=null) {
            return result;
        };
    } catch(error) {console?.log?.(error);};
    console.log("flag2");
    try {
        const promise = navigator?.clipboard?.read?.();
        console.log(promise);
        promise?.then?.((data)=>{
            console.log(data);
        });
    } catch(error) {console?.log?.(error);};
    console.log("flag3");
    return [prompt_title, prompt_table];
}"""


chat_copy_history_btn__click_js = """(txt)=>{
    console.log(txt);
    try {let promise = navigator?.clipboard?.writeText?.(txt);}
    catch(error) {console?.log?.(error);};
}"""


chat_copy_history_md_btn__click_js = """(txt)=>{
    console.log(txt);
    try {let promise = navigator?.clipboard?.writeText?.(txt);}
    catch(error) {console?.log?.(error);};
}"""


# api_key_refresh_btn__click_js = """()=>{
#     const the_api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]') ?? '';
#     return the_api_key;
# }"""


# api_key_save_btn__click_js = """(api_key_text)=>{
#     localStorage.setItem('[gradio][chat-gpt-ui][api_key_text]', api_key_text);
#     return api_key_text;
# }"""


api_key_refresh_btn__click_js = """()=>{
    const the_api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key_text]') ?? '';
    return the_api_key;
}"""


api_key_save_btn__click_js = """(api_key_text)=>{
    localStorage.setItem('[gradio][chat-gpt-ui][api_key_text]', api_key_text);
    return api_key_text;
}"""


api_key__get_from_browser = """()=>{
    const api_key = localStorage?.getItem?.('[gradio][chat-gpt-ui][api_key]') ?? '';
    const token = localStorage?.getItem?.('[gradio][chat-gpt-ui][token]') ?? '';
    return [api_key, token];
}"""

api_key__save_to_browser = """(api_key, token)=>{
    localStorage?.setItem?.('[gradio][chat-gpt-ui][api_key]', api_key);
    token = localStorage?.getItem?.('[gradio][chat-gpt-ui][token]') ?? token ?? '';
    if (!token?.length) {
        const temp_url = URL.createObjectURL(new Blob());
        const uuid = temp_url.toString();
        URL.revokeObjectURL(temp_url);
        token = uuid.substr(uuid.lastIndexOf("/") + 1);
    };
    localStorage.setItem('[gradio][chat-gpt-ui][token]', token);
    return [api_key, token];
}"""