Skip to content

Agent Zero AI framework

Persistent chats update

frdeld9b0491d84b5
diff --git a/python/extensions/message_loop_end/_90_save_chat.py b/python/extensions/message_loop_end/_90_save_chat.py
index 91bc6c9cc..0d4cebb8e 100644
--- a/python/extensions/message_loop_end/_90_save_chat.py
+++ b/python/extensions/message_loop_end/_90_save_chat.py
@@ -5,4 +5,4 @@ from python.helpers import persist_chat
class SaveChat(Extension):
async def execute(self, loop_data: LoopData = LoopData(), **kwargs):
- persist_chat.save_chat(self.agent.context)
\ No newline at end of file
+ persist_chat.save_tmp_chat(self.agent.context)
\ No newline at end of file
diff --git a/python/helpers/persist_chat.py b/python/helpers/persist_chat.py
index 86b908fb9..05a0ed2c1 100644
--- a/python/helpers/persist_chat.py
+++ b/python/helpers/persist_chat.py
@@ -12,21 +12,35 @@ CHATS_FOLDER = "tmp/chats"
LOG_SIZE = 1000
-def save_chat(context: AgentContext):
+def save_tmp_chat(context: AgentContext):
relative_path = _get_file_path(context.id)
data = _serialize_context(context)
js = _safe_json_serialize(data, ensure_ascii=False)
files.write_file(relative_path, js)
-
-def load_chats():
+def load_tmp_chats():
json_files = files.list_files("tmp/chats", "*.json")
+ ctxids = []
for file in json_files:
path = files.get_abs_path(CHATS_FOLDER, file)
js = files.read_file(path)
data = json.loads(js)
ctx = _deserialize_context(data)
+ ctxids.append(ctx.id)
+ return ctxids
+def load_json_chats(jsons: list[str]):
+ ctxids = []
+ for js in jsons:
+ data = json.loads(js)
+ ctx = _deserialize_context(data)
+ ctxids.append(ctx.id)
+ return ctxids
+
+def export_json_chat(context: AgentContext):
+ data = _serialize_context(context)
+ js = _safe_json_serialize(data, ensure_ascii=False)
+ return js
def remove_chat(ctxid):
files.delete_file(_get_file_path(ctxid))
@@ -91,20 +105,19 @@ def _deserialize_context(data):
id=data.get("id", None),
name=data.get("name", None),
log=log,
- paused=True,
+ paused=False,
# agent0=agent0,
# streaming_agent=straming_agent,
)
agents = data.get("agents", [])
agent0 = _deserialize_agents(agents, config, context)
- streaming_agent_no = data.get("streaming_agent", 0)
- straming_agent = (
- agents[streaming_agent_no] if streaming_agent_no < len(agents) else None
- )
-
+ streaming_agent = agent0
+ while streaming_agent.number != data.get("streaming_agent", 0):
+ streaming_agent = streaming_agent.data.get("subordinate", None)
+
context.agent0 = agent0
- context.streaming_agent = straming_agent
+ context.streaming_agent = streaming_agent
return context
diff --git a/run_ui.py b/run_ui.py
index 42ccc20a0..2ef00520e 100644
--- a/run_ui.py
+++ b/run_ui.py
@@ -17,7 +17,7 @@ from python.helpers import persist_chat
# initialize the internal Flask server
app = Flask("app", static_folder=get_abs_path("./webui"), static_url_path="/")
-app.config['JSON_SORT_KEYS'] = False # Disable key sorting in jsonify
+app.config["JSON_SORT_KEYS"] = False # Disable key sorting in jsonify
lock = threading.Lock()
@@ -175,6 +175,66 @@ async def pause():
return jsonify(response)
+# load chats from json
[email protected]("/loadChats", methods=["POST"])
+async def load_chats():
+ try:
+ # data sent to the server
+ input = request.get_json()
+ chats = input.get("chats", [])
+ if not chats:
+ raise Exception("No chats provided")
+
+ ctxids = persist_chat.load_json_chats(chats)
+
+ response = {
+ "ok": True,
+ "message": "Chats loaded.",
+ "ctxids": ctxids,
+ }
+
+ except Exception as e:
+ response = {
+ "ok": False,
+ "message": str(e),
+ }
+ PrintStyle.error(str(e))
+
+ # respond with json
+ return jsonify(response)
+
+
+# load chats from json
[email protected]("/exportChat", methods=["POST"])
+async def export_chat():
+ try:
+ # data sent to the server
+ input = request.get_json()
+ ctxid = input.get("ctxid", "")
+ if not ctxid:
+ raise Exception("No context id provided")
+
+ context = get_context(ctxid)
+ content = persist_chat.export_json_chat(context)
+
+ response = {
+ "ok": True,
+ "message": "Chats loaded.",
+ "ctxid": context.id,
+ "content": content,
+ }
+
+ except Exception as e:
+ response = {
+ "ok": False,
+ "message": str(e),
+ }
+ PrintStyle.error(str(e))
+
+ # respond with json
+ return jsonify(response)
+
+
# restarting with new agent0
@app.route("/reset", methods=["POST"])
async def reset():
@@ -187,7 +247,7 @@ async def reset():
# context instance - get or create
context = get_context(ctxid)
context.reset()
- persist_chat.save_chat(context)
+ persist_chat.save_tmp_chat(context)
response = {
"ok": True,
@@ -287,15 +347,16 @@ async def poll():
return Response(response=response_json, status=200, mimetype="application/json")
# return jsonify(response)
+
def run():
- print("Initializing framework...")
+ print("Initializing framework...")
- #load env vars
+ # load env vars
load_dotenv()
# initialize contexts from persisted chats
- persist_chat.load_chats()
-
+ persist_chat.load_tmp_chats()
+
# Suppress only request logs but keep the startup messages
from werkzeug.serving import WSGIRequestHandler
@@ -310,4 +371,4 @@ def run():
# run the internal server
if __name__ == "__main__":
- run()
\ No newline at end of file
+ run()
diff --git a/webui/index.css b/webui/index.css
index 05dd6181f..503cd8ef2 100644
--- a/webui/index.css
+++ b/webui/index.css
@@ -502,6 +502,8 @@ h4 {
font-family: "Rubik", Arial, Helvetica, sans-serif;
font-size: var(--font-size-small);
margin-top: 0;
+ margin-bottom: var(--spacing-xs);
+ /* margin-right: var(--spacing-xs); */
padding: var(--spacing-sm) 0.75rem;
text-wrap: nowrap;
background-color: var(--color-secondary);
diff --git a/webui/index.html b/webui/index.html
index 232f553f4..a350afb57 100644
--- a/webui/index.html
+++ b/webui/index.html
@@ -45,6 +45,8 @@
<h3>Quick Actions</h3>
<button class="config-button" id="resetChat" @click="resetChat()">Reset chat</button>
<button class="config-button" id="newChat" @click="newChat()">New Chat</button>
+ <button class="config-button" id="loadChats" @click="loadChats()">Load Chat</button>
+ <button class="config-button" id="loadChat" @click="saveChat()">Save Chat</button>
</div>
<div class="config-section" id="chats-section" x-data="{ contexts: [], selected: '' }"
diff --git a/webui/index.js b/webui/index.js
index 7e0bc1728..c75b28f55 100644
--- a/webui/index.js
+++ b/webui/index.js
@@ -373,6 +373,115 @@ function toggleCssProperty(selector, property, value) {
}
}
+window.loadChats = async function () {
+ try {
+ const fileContents = await readJsonFiles();
+ const response = await sendJsonData("/loadChats", { chats: fileContents });
+
+ if (!response) {
+ toast("No response returned.", "error")
+ } else if (!response.ok) {
+ if (response.message) {
+ toast(response.message, "error")
+ } else {
+ toast("Undefined error.", "error")
+ }
+ } else {
+ setContext(response.ctxids[0])
+ toast("Chats loaded.", "success")
+ }
+
+ } catch (e) {
+ toast(e.message, "error")
+ }
+}
+
+window.saveChat = async function () {
+ try {
+ const response = await sendJsonData("/exportChat", { ctxid: context });
+
+ if (!response) {
+ toast("No response returned.", "error")
+ } else if (!response.ok) {
+ if (response.message) {
+ toast(response.message, "error")
+ } else {
+ toast("Undefined error.", "error")
+ }
+ } else {
+ downloadFile(response.ctxid + ".json", response.content)
+ toast("Chat file downloaded.", "success")
+ }
+
+ } catch (e) {
+ toast(e.message, "error")
+ }
+}
+
+function downloadFile(filename, content) {
+ // Create a Blob with the content to save
+ const blob = new Blob([content], { type: 'application/json' });
+
+ // Create a link element
+ const link = document.createElement('a');
+
+ // Create a URL for the Blob
+ const url = URL.createObjectURL(blob);
+ link.href = url;
+
+ // Set the file name for download
+ link.download = filename;
+
+ // Programmatically click the link to trigger the download
+ link.click();
+
+ // Clean up by revoking the object URL
+ setTimeout(() => {
+ URL.revokeObjectURL(url);
+ }, 0);
+}
+
+
+function readJsonFiles() {
+ return new Promise((resolve, reject) => {
+ // Create an input element of type 'file'
+ const input = document.createElement('input');
+ input.type = 'file';
+ input.accept = '.json'; // Only accept JSON files
+ input.multiple = true; // Allow multiple file selection
+
+ // Trigger the file dialog
+ input.click();
+
+ // When files are selected
+ input.onchange = async () => {
+ const files = input.files;
+ if (!files.length) {
+ resolve([]); // Return an empty array if no files are selected
+ return;
+ }
+
+ // Read each file as a string and store in an array
+ const filePromises = Array.from(files).map(file => {
+ return new Promise((fileResolve, fileReject) => {
+ const reader = new FileReader();
+ reader.onload = () => fileResolve(reader.result);
+ reader.onerror = fileReject;
+ reader.readAsText(file);
+ });
+ });
+
+ try {
+ const fileContents = await Promise.all(filePromises);
+ resolve(fileContents);
+ } catch (error) {
+ reject(error); // In case of any file reading error
+ }
+ };
+ });
+}
+
+
function toast(text, type = 'info') {
const toast = document.getElementById('toast');