File size: 2,891 Bytes
cb0dedb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf83c0e
cb0dedb
 
 
 
 
 
cf83c0e
 
cb0dedb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
class AgentStorage {
    constructor() {
        this.init_database();
        this.load_local_agents();
        // this.create_agent_items();
    }
    init_database() {
        this.db = new Dexie("agents");
        this.db.version(1).stores({
            agents: "index, name, model, temperature, max_output_tokens, system_prompt, need_protect",
        });
        this.db.agents.count((count) => {
            console.log(`${count} agents loaded.`);
        });
    }
    clear_database() {
        this.db.agents.count((count) => {
            console.log(`${count} agents would be cleared.`);
        });
        this.db.agents.clear();
    }
    async load_local_agents() {
        fetch("/agents")
            .then((response) => response.json())
            .then((data) => {
                if (data.error) {
                    console.error(data.error);
                    return;
                }
                let count = Object.keys(data).length;
                console.log(`${count} local agents loaded.`);
                // data is array of agent items, each item has 7 keys:
                // - name, model, description, temperature, top_p, max_output_tokens, system_prompt, need_protect
                data.forEach((agent) => {
                    this.db.agents.put({
                        index: agent.name,
                        name: agent.name,
                        description: agent.description || "",
                        model: agent.model,
                        temperature: agent.temperature || 0.5,
                        top_p: agent.top_p || 0.9,
                        max_output_tokens: agent.max_output_tokens || -1,
                        system_prompt: agent.system_prompt || "",
                        need_protect: agent.need_protect || false,
                    });
                });
            });
    }

    generate_agent_item_html() {
        let agent_item_html = ``;
        return agent_item_html;
    }

    set_default_agent() {
        let storage_default_agent = localStorage.getItem("default_agent");

        // let select = $("#agent-select");
        if (
            storage_default_agent
            // && select.find(`option[value="${storage_default_agent}"]`).length > 0
        ) {
            // select.val(storage_default_agent);
            console.log(
                "load default agent:",
                localStorage.getItem("default_agent")
            );
        } else {
            // let new_storage_default_agent = select.find("option:first").val();
            // select.val(new_storage_default_agent);
            // localStorage.setItem("default_agent", new_storage_default_agent);
            console.log(
                "set new default agent:",
                localStorage.getItem("default_agent")
            );
        }
    }
}

export let agent_storage = new AgentStorage();