|
class AgentStorage { |
|
constructor() { |
|
this.init_database(); |
|
this.load_local_agents(); |
|
|
|
} |
|
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.forEach((agent) => { |
|
this.db.agents.put({ |
|
index: agent.name, |
|
name: agent.name, |
|
description: agent.description || "", |
|
model: agent.model, |
|
temperature: agent.temperature || 0.0, |
|
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"); |
|
|
|
|
|
if ( |
|
storage_default_agent |
|
|
|
) { |
|
|
|
console.log( |
|
"load default agent:", |
|
localStorage.getItem("default_agent") |
|
); |
|
} else { |
|
|
|
|
|
|
|
console.log( |
|
"set new default agent:", |
|
localStorage.getItem("default_agent") |
|
); |
|
} |
|
} |
|
} |
|
|
|
export let agent_storage = new AgentStorage(); |
|
|