shenchucheng
commited on
Commit
·
0a13671
1
Parent(s):
9759100
cancel task if disconnect
Browse files
app.py
CHANGED
|
@@ -19,6 +19,7 @@ import uvicorn
|
|
| 19 |
from fastapi import FastAPI, Request
|
| 20 |
from fastapi.responses import StreamingResponse
|
| 21 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 22 |
from metagpt.actions.action import Action
|
| 23 |
from metagpt.actions.action_output import ActionOutput
|
| 24 |
from metagpt.config import CONFIG
|
|
@@ -174,11 +175,25 @@ async def create_message(req_model: NewMsg, request: Request):
|
|
| 174 |
qa_type=QueryAnswerType.Answer.value,
|
| 175 |
)
|
| 176 |
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
while True:
|
| 180 |
tc_id += 1
|
| 181 |
-
if
|
| 182 |
return
|
| 183 |
think_result: RoleRun = await role.think()
|
| 184 |
if not think_result: # End of conversion
|
|
@@ -210,7 +225,7 @@ async def create_message(req_model: NewMsg, request: Request):
|
|
| 210 |
yield answer.prompt + "\n\n" # Notify the front-end that the message is complete.
|
| 211 |
except Exception as ex:
|
| 212 |
description = str(ex)
|
| 213 |
-
answer =
|
| 214 |
step = ThinkActStep(
|
| 215 |
id=tc_id,
|
| 216 |
status="failed",
|
|
|
|
| 19 |
from fastapi import FastAPI, Request
|
| 20 |
from fastapi.responses import StreamingResponse
|
| 21 |
from fastapi.staticfiles import StaticFiles
|
| 22 |
+
from loguru import logger
|
| 23 |
from metagpt.actions.action import Action
|
| 24 |
from metagpt.actions.action_output import ActionOutput
|
| 25 |
from metagpt.config import CONFIG
|
|
|
|
| 175 |
qa_type=QueryAnswerType.Answer.value,
|
| 176 |
)
|
| 177 |
|
| 178 |
+
task = None
|
| 179 |
+
|
| 180 |
+
async def stop_if_disconnect():
|
| 181 |
+
while not await request.is_disconnected():
|
| 182 |
+
await asyncio.sleep(1)
|
| 183 |
+
|
| 184 |
+
if task is None:
|
| 185 |
+
return
|
| 186 |
|
| 187 |
+
if not task.done():
|
| 188 |
+
task.cancel()
|
| 189 |
+
logger.info(f"cancel task {task}")
|
| 190 |
+
|
| 191 |
+
asyncio.create_task(stop_if_disconnect())
|
| 192 |
+
|
| 193 |
+
tc_id = 0
|
| 194 |
while True:
|
| 195 |
tc_id += 1
|
| 196 |
+
if await request.is_disconnected():
|
| 197 |
return
|
| 198 |
think_result: RoleRun = await role.think()
|
| 199 |
if not think_result: # End of conversion
|
|
|
|
| 225 |
yield answer.prompt + "\n\n" # Notify the front-end that the message is complete.
|
| 226 |
except Exception as ex:
|
| 227 |
description = str(ex)
|
| 228 |
+
answer = traceback.format_exc()
|
| 229 |
step = ThinkActStep(
|
| 230 |
id=tc_id,
|
| 231 |
status="failed",
|