Skip to content

Commit

Permalink
Handle stop event in app consumer
Browse files Browse the repository at this point in the history
  • Loading branch information
ajhai committed Oct 22, 2024
1 parent 805d678 commit 35d9587
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions llmstack/server/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,25 @@ async def _run_app(self, request_uuid, request, **kwargs):
async def _respond_to_event(self, text_data):
json_data = json.loads(text_data)
client_request_id = json_data.get("id", None)
app_runner_request = AppRunnerRequest(
client_request_id=client_request_id,
session_id=self._session_id,
input=json_data.get("input", {}),
)
try:
response_iterator = self._app_runner.run(app_runner_request)
async for response in response_iterator:
if response.type == AppRunnerStreamingResponseType.OUTPUT_STREAM_CHUNK:
await self.send(text_data=json.dumps(response.model_dump()))
elif response.type == AppRunnerStreamingResponseType.OUTPUT_STREAM_END:
await self.send(text_data=json.dumps({"event": "done", "request_id": client_request_id}))
except Exception as e:
logger.exception(f"Failed to run app: {e}")
event = json_data.get("event", None)

if event == "run":
app_runner_request = AppRunnerRequest(
client_request_id=client_request_id,
session_id=self._session_id,
input=json_data.get("input", {}),
)
try:
response_iterator = self._app_runner.run(app_runner_request)
async for response in response_iterator:
if response.type == AppRunnerStreamingResponseType.OUTPUT_STREAM_CHUNK:
await self.send(text_data=json.dumps(response.model_dump()))
elif response.type == AppRunnerStreamingResponseType.OUTPUT_STREAM_END:
await self.send(text_data=json.dumps({"event": "done", "request_id": client_request_id}))
except Exception as e:
logger.exception(f"Failed to run app: {e}")
elif event == "stop":
self.disconnect()

async def _respond_to_event_old(self, text_data):
from llmstack.apps.apis import AppViewSet
Expand Down

0 comments on commit 35d9587

Please sign in to comment.