:hammer: [WIP] New AgentStorage: Define database schema, and load from local configs
Browse files- configs/agents.json +10 -0
- server.js +18 -0
- storages/agent_storage.js +79 -0
- storages/agents_storage.js +0 -1
configs/agents.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"name": "Summarizer",
|
4 |
+
"description": "Summarizes text",
|
5 |
+
"temperature": 0.0,
|
6 |
+
"max_output_token": -1,
|
7 |
+
"system_prompt": "Summarize the following text:",
|
8 |
+
"need_protect": false
|
9 |
+
}
|
10 |
+
]
|
server.js
CHANGED
@@ -31,6 +31,24 @@ app.get("/endpoints", async (req, res) => {
|
|
31 |
}
|
32 |
});
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
app.post("/chat/completions", async (req, res) => {
|
35 |
try {
|
36 |
const {
|
|
|
31 |
}
|
32 |
});
|
33 |
|
34 |
+
app.get("/agents", async (req, res) => {
|
35 |
+
try {
|
36 |
+
let agents_configs_path = path.join(
|
37 |
+
__dirname,
|
38 |
+
"configs",
|
39 |
+
"agents.json"
|
40 |
+
);
|
41 |
+
const data = await fs.readFile(agents_configs_path, "utf-8");
|
42 |
+
const local_agents = JSON.parse(data);
|
43 |
+
res.json(local_agents);
|
44 |
+
} catch (error) {
|
45 |
+
console.error(error);
|
46 |
+
res.status(500).json({
|
47 |
+
error: "Failed to get local agents: Maybe configs/agents.json not existed?",
|
48 |
+
});
|
49 |
+
}
|
50 |
+
});
|
51 |
+
|
52 |
app.post("/chat/completions", async (req, res) => {
|
53 |
try {
|
54 |
const {
|
storages/agent_storage.js
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class AgentStorage {
|
2 |
+
constructor() {
|
3 |
+
this.init_database();
|
4 |
+
this.load_local_agents();
|
5 |
+
// this.create_agent_items();
|
6 |
+
}
|
7 |
+
init_database() {
|
8 |
+
this.db = new Dexie("agents");
|
9 |
+
this.db.version(1).stores({
|
10 |
+
agents: "index, name, model, temperature, max_output_tokens, system_prompt, need_protect",
|
11 |
+
});
|
12 |
+
this.db.agents.count((count) => {
|
13 |
+
console.log(`${count} agents loaded.`);
|
14 |
+
});
|
15 |
+
}
|
16 |
+
clear_database() {
|
17 |
+
this.db.agents.count((count) => {
|
18 |
+
console.log(`${count} agents would be cleared.`);
|
19 |
+
});
|
20 |
+
this.db.agents.clear();
|
21 |
+
}
|
22 |
+
async load_local_agents() {
|
23 |
+
fetch("/agents")
|
24 |
+
.then((response) => response.json())
|
25 |
+
.then((data) => {
|
26 |
+
if (data.error) {
|
27 |
+
console.error(data.error);
|
28 |
+
return;
|
29 |
+
}
|
30 |
+
let count = Object.keys(data).length;
|
31 |
+
console.log(`${count} local agents loaded.`);
|
32 |
+
// data is array of agent items, each item has 7 keys:
|
33 |
+
// - name, model, description, temperature, max_output_tokens, system_prompt, need_protect
|
34 |
+
data.forEach((agent) => {
|
35 |
+
this.db.agents.put({
|
36 |
+
index: agent.name,
|
37 |
+
name: agent.name,
|
38 |
+
description: agent.description || "",
|
39 |
+
model: agent.model,
|
40 |
+
temperature: agent.temperature || 0.0,
|
41 |
+
max_output_tokens: agent.max_output_tokens || -1,
|
42 |
+
system_prompt: agent.system_prompt || "",
|
43 |
+
need_protect: agent.need_protect || false,
|
44 |
+
});
|
45 |
+
});
|
46 |
+
});
|
47 |
+
}
|
48 |
+
|
49 |
+
generate_agent_item_html() {
|
50 |
+
let agent_item_html = ``;
|
51 |
+
return agent_item_html;
|
52 |
+
}
|
53 |
+
|
54 |
+
set_default_agent() {
|
55 |
+
let storage_default_agent = localStorage.getItem("default_agent");
|
56 |
+
|
57 |
+
// let select = $("#agent-select");
|
58 |
+
if (
|
59 |
+
storage_default_agent
|
60 |
+
// && select.find(`option[value="${storage_default_agent}"]`).length > 0
|
61 |
+
) {
|
62 |
+
// select.val(storage_default_agent);
|
63 |
+
console.log(
|
64 |
+
"load default agent:",
|
65 |
+
localStorage.getItem("default_agent")
|
66 |
+
);
|
67 |
+
} else {
|
68 |
+
// let new_storage_default_agent = select.find("option:first").val();
|
69 |
+
// select.val(new_storage_default_agent);
|
70 |
+
// localStorage.setItem("default_agent", new_storage_default_agent);
|
71 |
+
console.log(
|
72 |
+
"set new default agent:",
|
73 |
+
localStorage.getItem("default_agent")
|
74 |
+
);
|
75 |
+
}
|
76 |
+
}
|
77 |
+
}
|
78 |
+
|
79 |
+
export let agent_storage = new AgentStorage();
|
storages/agents_storage.js
DELETED
@@ -1 +0,0 @@
|
|
1 |
-
class AgentsStorage {}
|
|
|
|