mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-05-05 23:41:06 +00:00
initial commit
This commit is contained in:
commit
f6dd426830
1145 changed files with 102834 additions and 0 deletions
48
backend/app/utils/server/sync_step.py
Normal file
48
backend/app/utils/server/sync_step.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import time
|
||||
import httpx
|
||||
import asyncio
|
||||
import os
|
||||
import json
|
||||
from loguru import logger
|
||||
from app.service.chat_service import Chat
|
||||
from app.component.environment import env
|
||||
|
||||
|
||||
def sync_step(func):
|
||||
async def wrapper(*args, **kwargs):
|
||||
server_url = env("SERVER_URL")
|
||||
sync_url = server_url + "/chat/steps" if server_url else None
|
||||
async for value in func(*args, **kwargs):
|
||||
if not server_url:
|
||||
yield value
|
||||
continue
|
||||
|
||||
if isinstance(value, str) and value.startswith("data: "):
|
||||
value_json_str = value[len("data: ") :].strip()
|
||||
else:
|
||||
value_json_str = value
|
||||
json_data = json.loads(value_json_str)
|
||||
chat: Chat = args[0] if args else None
|
||||
if chat is not None:
|
||||
asyncio.create_task(
|
||||
send_to_api(
|
||||
sync_url,
|
||||
{
|
||||
"task_id": chat.task_id,
|
||||
"step": json_data["step"],
|
||||
"data": json_data["data"],
|
||||
},
|
||||
)
|
||||
)
|
||||
yield value
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
async def send_to_api(url, data):
|
||||
async with httpx.AsyncClient() as client:
|
||||
try:
|
||||
res = await client.post(url, json=data)
|
||||
# logger.info(res)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
Loading…
Add table
Add a link
Reference in a new issue