Spaces:
Running
Running
abrakjamson
commited on
Commit
·
2ddfb98
1
Parent(s):
ff80e2f
conversion to message/send and new protocol version
Browse files- app.py +3 -3
- task_management.py +12 -6
app.py
CHANGED
@@ -94,7 +94,8 @@ def load_session_history(session_id: str) -> list:
|
|
94 |
|
95 |
def respond(message: str, history: list) -> tuple[str, list]:
|
96 |
server_url = get_selected_server()
|
97 |
-
|
|
|
98 |
|
99 |
def create_chat_interface():
|
100 |
with gr.Blocks() as demo:
|
@@ -153,11 +154,10 @@ def create_chat_interface():
|
|
153 |
with gr.Column(scale=2):
|
154 |
chatbot = gr.Chatbot(label="Chat", type="messages")
|
155 |
chat_input = gr.Textbox(label="Message", placeholder="Type a message...")
|
156 |
-
chat_state = gr.State([])
|
157 |
|
158 |
chat_input.submit(
|
159 |
respond,
|
160 |
-
inputs=[chat_input
|
161 |
outputs=[chat_input, chatbot],
|
162 |
queue=False,
|
163 |
)
|
|
|
94 |
|
95 |
def respond(message: str, history: list) -> tuple[str, list]:
|
96 |
server_url = get_selected_server()
|
97 |
+
history = send_message(server_url, message)
|
98 |
+
return "", history
|
99 |
|
100 |
def create_chat_interface():
|
101 |
with gr.Blocks() as demo:
|
|
|
154 |
with gr.Column(scale=2):
|
155 |
chatbot = gr.Chatbot(label="Chat", type="messages")
|
156 |
chat_input = gr.Textbox(label="Message", placeholder="Type a message...")
|
|
|
157 |
|
158 |
chat_input.submit(
|
159 |
respond,
|
160 |
+
inputs=[chat_input],
|
161 |
outputs=[chat_input, chatbot],
|
162 |
queue=False,
|
163 |
)
|
task_management.py
CHANGED
@@ -2,6 +2,7 @@ import uuid
|
|
2 |
import json
|
3 |
import requests
|
4 |
import time
|
|
|
5 |
|
6 |
# Data class of a session with ID and task history
|
7 |
class Session:
|
@@ -101,14 +102,14 @@ def send_message(server_url, content):
|
|
101 |
if "error" in result:
|
102 |
error_message = result["error"].get("message", "Unknown error")
|
103 |
sessions[current_session_id].add_task({"role": "assistant", "content": f"Error: {error_message}"})
|
104 |
-
|
105 |
return
|
106 |
|
107 |
# If it's a task, we need to poll for updates.
|
108 |
task_id = result.get("id")
|
109 |
if not task_id:
|
110 |
sessions[current_session_id].add_task({"role": "assistant", "content": "No task ID returned."})
|
111 |
-
|
112 |
return
|
113 |
# TODO allow for multiple concurrent tasks in the client
|
114 |
# should store task IDs in the session and add a view into interface
|
@@ -116,13 +117,18 @@ def send_message(server_url, content):
|
|
116 |
# Poll for task updates
|
117 |
return poll_for_task_completion(server_url, task_id, current_session_id)
|
118 |
|
119 |
-
# If the response contains a result, it should be a direct message
|
120 |
-
|
121 |
-
|
|
|
|
|
|
|
122 |
if agent_reply:
|
123 |
sessions[current_session_id].add_task({"role": "assistant", "content": agent_reply})
|
124 |
|
125 |
-
|
|
|
|
|
126 |
|
127 |
# Poll for task updates
|
128 |
def poll_for_task_completion(server_url, task_id, current_session_id):
|
|
|
2 |
import json
|
3 |
import requests
|
4 |
import time
|
5 |
+
import gradio as gr
|
6 |
|
7 |
# Data class of a session with ID and task history
|
8 |
class Session:
|
|
|
102 |
if "error" in result:
|
103 |
error_message = result["error"].get("message", "Unknown error")
|
104 |
sessions[current_session_id].add_task({"role": "assistant", "content": f"Error: {error_message}"})
|
105 |
+
return sessions[current_session_id].get_history()
|
106 |
return
|
107 |
|
108 |
# If it's a task, we need to poll for updates.
|
109 |
task_id = result.get("id")
|
110 |
if not task_id:
|
111 |
sessions[current_session_id].add_task({"role": "assistant", "content": "No task ID returned."})
|
112 |
+
return sessions[current_session_id].get_history()
|
113 |
return
|
114 |
# TODO allow for multiple concurrent tasks in the client
|
115 |
# should store task IDs in the session and add a view into interface
|
|
|
117 |
# Poll for task updates
|
118 |
return poll_for_task_completion(server_url, task_id, current_session_id)
|
119 |
|
120 |
+
# If the response contains a result, it should be a direct message object
|
121 |
+
# Get the parts where the kind is "text", then get the text value
|
122 |
+
message_parts = result.get("result", {}).get("parts", [])
|
123 |
+
agent_reply = next((part["text"] for part in message_parts if part.get("kind") == "text"), "")
|
124 |
+
# convert agent_reply_object from a string of json into a Python object to get the text field
|
125 |
+
|
126 |
if agent_reply:
|
127 |
sessions[current_session_id].add_task({"role": "assistant", "content": agent_reply})
|
128 |
|
129 |
+
history = sessions[current_session_id].get_history()
|
130 |
+
return history
|
131 |
+
|
132 |
|
133 |
# Poll for task updates
|
134 |
def poll_for_task_completion(server_url, task_id, current_session_id):
|