Skip to content

Commit

Permalink
fix new agent name
Browse files Browse the repository at this point in the history
  • Loading branch information
f1delius committed May 12, 2024
1 parent de57045 commit 6655f3a
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions src/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from api import send_message_as_tool


async def workflow_steps(superagent_url: str, workflow_id: str, api_key: str, session: httpx.AsyncClient):
async def workflow_steps(
superagent_url: str,
workflow_id: str,
api_key: str,
session: httpx.AsyncClient
):
headers = {
'Authorization': f'Bearer {api_key}',
}
Expand Down Expand Up @@ -43,42 +48,26 @@ async def stream_json_response_with_auth(
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
json = {"input": msg_data, "sessionId": thread_id, "enableStreaming": True}
json = {"input": msg_data, "sessionId": thread_id, "enableStreaming": True, "stream_token" : True}
prev_data = ''
prev_event = None
async with aiohttp.ClientSession() as session:
async with session.post(api_url, headers=headers, json=json) as response:
response.raise_for_status()
async for line in response.content:
data = line.decode('utf-8')
# Split the line into event and data parts
if line.startswith(b'id:'):
event = line.decode('utf-8').split(':', 1)[1].strip()
if prev_event is not None and event != prev_event:
# Print the complete message for the previous event
prev_data = prev_data.replace("````", "`\n```")
prev_data = prev_data.replace("```", "\n```")
logging.info(
f'Event: {prev_event}, Data: {prev_data}')
if data.startswith("workflow_agent_name:") or data.startswith("\nworkflow_agent_name:"):
event = data.split("name:")[1]
if prev_event != event:
prev_event = event
await send_agent_message(agent[prev_event], thread_id, reply_id, prev_data, room_id, workflow_bot, msg_limit)
# Reset the previous data
prev_data = ''
# Get the next line which contains data
if line.startswith(b'data:'):
event_data = line[6:-1]
if event_data == b'':
prev_data += '\n'
elif event_data.isspace():
prev_data += '\n' + event_data.decode('utf-8')[1:]
else:
prev_data += event_data.decode('utf-8')

# Update the previous event
prev_event = event
else:
prev_data += line

# Print the complete message for the last event
if prev_event is not None:
prev_data = prev_data.replace("````", "`\n```")
prev_data = prev_data.replace("```", "\n```")
logging.info(f'Event: {prev_event}, Data: {prev_data}')
await send_agent_message(agent[prev_event], thread_id, reply_id, prev_data, room_id, workflow_bot, msg_limit)
else:
Expand Down

0 comments on commit 6655f3a

Please sign in to comment.