Hansimov commited on
Commit
8f9b2a4
1 Parent(s): 73813dd

:gem: [Feature] Replace top select from available models to agents, and auto set default agent

Browse files
components/buttons_binder.js CHANGED
@@ -47,6 +47,8 @@ export class ButtonsBinder {
47
  clear_chat_agents_button_binder.bind();
48
  let available_models_select_binder = new AvailableModelsSelectBinder();
49
  available_models_select_binder.bind();
 
 
50
  let dark_theme_toggle_button_binder = new DarkThemeToggleButtonBinder();
51
  dark_theme_toggle_button_binder.bind();
52
  }
@@ -354,6 +356,17 @@ class AvailableModelsSelectBinder {
354
  }
355
  }
356
 
 
 
 
 
 
 
 
 
 
 
 
357
  class DarkThemeToggleButtonBinder {
358
  constructor() {
359
  this.storage_key = "theme";
 
47
  clear_chat_agents_button_binder.bind();
48
  let available_models_select_binder = new AvailableModelsSelectBinder();
49
  available_models_select_binder.bind();
50
+ let agents_select_binder = new AgentsSelectBinder();
51
+ agents_select_binder.bind();
52
  let dark_theme_toggle_button_binder = new DarkThemeToggleButtonBinder();
53
  dark_theme_toggle_button_binder.bind();
54
  }
 
356
  }
357
  }
358
 
359
+ class AgentsSelectBinder {
360
+ constructor() {}
361
+ bind() {
362
+ const select = $("#agents-select");
363
+ select.change(() => {
364
+ localStorage.setItem("default_agent", select.val());
365
+ console.log("set default_agent:", select.val());
366
+ });
367
+ }
368
+ }
369
+
370
  class DarkThemeToggleButtonBinder {
371
  constructor() {
372
  this.storage_key = "theme";
configs/agents.json CHANGED
@@ -1,6 +1,7 @@
1
  [
2
  {
3
  "name": "默认",
 
4
  "description": "默认",
5
  "temperature": 0.5,
6
  "top_p": 0.9,
 
1
  [
2
  {
3
  "name": "默认",
4
+ "index": "0",
5
  "description": "默认",
6
  "temperature": 0.5,
7
  "top_p": 0.9,
css/default.css CHANGED
@@ -113,6 +113,11 @@ body {
113
 
114
  #available-models-select {
115
  max-width: calc(100vw - 200px);
 
 
 
 
 
116
  }
117
 
118
  #temperature-select {
 
113
 
114
  #available-models-select {
115
  max-width: calc(100vw - 200px);
116
+ display: none;
117
+ }
118
+
119
+ #agents-select {
120
+ max-width: calc(100vw - 200px);
121
  }
122
 
123
  #temperature-select {
index.html CHANGED
@@ -98,8 +98,9 @@
98
  <i class="fa fa-key"></i>
99
  </button>
100
  </div>
 
101
  <div class="col px-0">
102
- <select class="form-select" id="available-models-select" title="Available Models"></select>
103
  </div>
104
  <div class="col-auto">
105
  <button id="scroll-to-bottom-button" class="btn px-0">
 
98
  <i class="fa fa-key"></i>
99
  </button>
100
  </div>
101
+ <select class="form-select" id="available-models-select" title="Available Models"></select>
102
  <div class="col px-0">
103
+ <select class="form-select" id="agents-select" title="Agents"></select>
104
  </div>
105
  <div class="col-auto">
106
  <button id="scroll-to-bottom-button" class="btn px-0">
storages/agent_storage.js CHANGED
@@ -8,7 +8,7 @@ class AgentStorage {
8
  constructor() {
9
  this.init_database();
10
  this.load_local_agents().then(() => {
11
- this.create_agent_items();
12
  });
13
  }
14
  init_database() {
@@ -40,7 +40,7 @@ class AgentStorage {
40
  // - name, model, description, temperature, top_p, max_output_tokens, system_prompt, need_protect
41
  data.forEach((agent) => {
42
  this.db.agents.put({
43
- index: agent.name,
44
  name: agent.name,
45
  description: agent.description || "",
46
  model: agent.model,
@@ -54,43 +54,39 @@ class AgentStorage {
54
  });
55
  }
56
 
57
- generate_agent_item_html(agent) {
58
- let agent_item_html = `
59
- <div class="my-2 row no-gutters">
60
- <button id="${agent.name}-agent-button" title="${agent.system_prompt}" class="agent-button" type="button">
61
- <i class="fa fa-arrow-circle-o-right"></i> ${agent.name}
62
- </button>
63
- </div>
64
- `;
65
- return agent_item_html;
66
- }
67
-
68
- create_agent_items() {
69
- let chat_agents_sidebar_items = $("#chat-agents-sidebar-items");
70
- chat_agents_sidebar_items.empty();
71
- this.db.agents.each((agent) => {
72
- let agent_item_html = this.generate_agent_item_html(agent);
73
- chat_agents_sidebar_items.append(agent_item_html);
74
  });
75
  }
76
-
77
  set_default_agent() {
78
  let storage_default_agent = localStorage.getItem("default_agent");
79
 
80
- // let select = $("#agent-select");
81
  if (
82
- storage_default_agent
83
- // && select.find(`option[value="${storage_default_agent}"]`).length > 0
84
  ) {
85
- // select.val(storage_default_agent);
86
  console.log(
87
  "load default agent:",
88
  localStorage.getItem("default_agent")
89
  );
90
  } else {
91
- // let new_storage_default_agent = select.find("option:first").val();
92
- // select.val(new_storage_default_agent);
93
- // localStorage.setItem("default_agent", new_storage_default_agent);
94
  console.log(
95
  "set new default agent:",
96
  localStorage.getItem("default_agent")
 
8
  constructor() {
9
  this.init_database();
10
  this.load_local_agents().then(() => {
11
+ this.fill_agents_select();
12
  });
13
  }
14
  init_database() {
 
40
  // - name, model, description, temperature, top_p, max_output_tokens, system_prompt, need_protect
41
  data.forEach((agent) => {
42
  this.db.agents.put({
43
+ index: agent.index || agent.name,
44
  name: agent.name,
45
  description: agent.description || "",
46
  model: agent.model,
 
54
  });
55
  }
56
 
57
+ fill_agents_select() {
58
+ // fetch agents, and then fill agents_select
59
+ let agents_select = $("#agents-select");
60
+ agents_select.empty();
61
+ this.db.agents.toArray().then((agents) => {
62
+ let promises = agents.map((agent) => {
63
+ let option_name = agent.name;
64
+ let option_value = agent.name;
65
+ let option = new Option(option_name, option_value);
66
+ agents_select.append(option);
67
+ });
68
+ Promise.all(promises).then(() => {
69
+ this.set_default_agent();
70
+ });
 
 
 
71
  });
72
  }
 
73
  set_default_agent() {
74
  let storage_default_agent = localStorage.getItem("default_agent");
75
 
76
+ let select = $("#agents-select");
77
  if (
78
+ storage_default_agent &&
79
+ select.find(`option[value="${storage_default_agent}"]`).length > 0
80
  ) {
81
+ select.val(storage_default_agent);
82
  console.log(
83
  "load default agent:",
84
  localStorage.getItem("default_agent")
85
  );
86
  } else {
87
+ let new_storage_default_agent = select.find("option:first").val();
88
+ select.val(new_storage_default_agent);
89
+ localStorage.setItem("default_agent", new_storage_default_agent);
90
  console.log(
91
  "set new default agent:",
92
  localStorage.getItem("default_agent")