File size: 3,555 Bytes
e940bf5
 
 
 
 
 
cb0dedb
 
 
e940bf5
8f9b2a4
e940bf5
cb0dedb
 
 
 
e940bf5
cb0dedb
 
 
 
 
 
 
 
 
 
 
 
e940bf5
cb0dedb
 
 
 
 
 
 
 
 
cf83c0e
cb0dedb
 
8f9b2a4
cb0dedb
 
 
cf83c0e
 
cb0dedb
 
 
 
 
 
 
 
8f9b2a4
 
 
 
 
 
 
 
 
 
 
 
 
 
e940bf5
 
cb0dedb
 
 
8f9b2a4
cb0dedb
8f9b2a4
 
cb0dedb
8f9b2a4
cb0dedb
 
 
 
 
8f9b2a4
 
 
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class AgentStorageItem {
    constructor(agent_storage) {
        this.agent_storage = agent_storage;
    }
}

class AgentStorage {
    constructor() {
        this.init_database();
        this.load_local_agents().then(() => {
            this.fill_agents_select();
        });
    }
    init_database() {
        this.db = new Dexie("agents");
        this.db.version(1).stores({
            agents: "index, name, model, description, temperature, top_p, 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() {
        return 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.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,
                    });
                });
            });
    }

    fill_agents_select() {
        // fetch agents, and then fill agents_select
        let agents_select = $("#agents-select");
        agents_select.empty();
        this.db.agents.toArray().then((agents) => {
            let promises = agents.map((agent) => {
                let option_name = agent.name;
                let option_value = agent.name;
                let option = new Option(option_name, option_value);
                agents_select.append(option);
            });
            Promise.all(promises).then(() => {
                this.set_default_agent();
            });
        });
    }
    set_default_agent() {
        let storage_default_agent = localStorage.getItem("default_agent");

        let select = $("#agents-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();