File size: 5,578 Bytes
dd32c33 e940bf5 0213430 6d9e109 0213430 6d9e109 0213430 6d9e109 0213430 6d9e109 0213430 cb0dedb e940bf5 8f9b2a4 e940bf5 cb0dedb e940bf5 cb0dedb e940bf5 cb0dedb cf83c0e cb0dedb 8f9b2a4 cb0dedb 6d9e109 cf83c0e cb0dedb 8f9b2a4 6d9e109 8f9b2a4 e940bf5 cb0dedb 8f9b2a4 cb0dedb 8f9b2a4 cb0dedb 8f9b2a4 cb0dedb 8f9b2a4 cb0dedb dd32c33 6d9e109 dd32c33 6d9e109 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 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 |
import { AgentInfoWidget } from "../widgets/agent_info_widget.js";
class AgentStorageItem {
constructor(agent_storage) {
this.agent_storage = agent_storage;
}
}
export function get_current_agent_info() {
let id = "agent-info";
let widget = $(`#${id}`);
let name = widget.find(`#${id}-name`).val();
let model = widget.find(`#${id}-model-select`).val();
let system_prompt = widget.find(`#${id}-system-prompt`).val();
let description = widget.find(`#${id}-description`).val();
let temperature = parseFloat(
widget.find(`#${id}-temperature-number`).val()
);
let top_p = parseFloat(widget.find(`#${id}-top-p-number`).val());
let max_output_tokens = parseInt(
widget.find(`#${id}-max-output-tokens-number`).val()
);
return {
name: name,
model: model,
system_prompt: system_prompt,
description: description,
temperature: temperature,
top_p: top_p,
max_output_tokens: max_output_tokens,
};
}
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,
model: agent.model,
system_prompt: agent.system_prompt || "",
description: agent.description || "",
temperature: agent.temperature || 0.5,
top_p: agent.top_p || 0.9,
max_output_tokens: agent.max_output_tokens || -1,
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();
this.set_agent_info_widget();
});
});
}
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")
);
}
}
get_current_agent() {
let current_agent_name = $("#agents-select").val();
return this.db.agents.get(current_agent_name);
}
set_agent_info_widget() {
this.get_current_agent().then((agent) => {
let agent_info_widget = new AgentInfoWidget({
agent: agent,
});
agent_info_widget.spawn();
});
}
get_agent_info(name = "") {
let agent_info = {};
if (name === "") {
name = $("#agents-select").val();
}
this.db.agents.get(name).then((agent) => {
agent_info.name = agent.name;
agent_info.model = agent.model;
agent_info.system_prompt = agent.system_prompt;
agent_info.description = agent.description;
agent_info.temperature = agent.temperature;
agent_info.top_p = agent.top_p;
agent_info.max_output_tokens = agent.max_output_tokens;
agent_info.need_protect = agent.need_protect;
return agent_info;
});
}
}
export let agent_storage = new AgentStorage();
|