mirror of
https://github.com/theroyallab/tabbyAPI.git
synced 2026-03-14 15:57:27 +00:00
sse_starlette kept firing a ping response if it was taking too long to set an event. Rather than using a hacky workaround, switch to FastAPI's inbuilt streaming response and construct SSE requests with a utility function. This helps the API become more robust and removes an extra requirement. Signed-off-by: kingbri <bdashore3@proton.me>
33 lines
847 B
Python
33 lines
847 B
Python
import traceback
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
|
|
# Wrapper callback for load progress
|
|
def load_progress(module, modules):
|
|
yield module, modules
|
|
|
|
# Common error types
|
|
class TabbyGeneratorErrorMessage(BaseModel):
|
|
message: str
|
|
trace: Optional[str] = None
|
|
|
|
class TabbyGeneratorError(BaseModel):
|
|
error: TabbyGeneratorErrorMessage
|
|
|
|
def get_generator_error(exception: Exception):
|
|
error_message = TabbyGeneratorErrorMessage(
|
|
message = str(exception),
|
|
trace = traceback.format_exc()
|
|
)
|
|
|
|
generator_error = TabbyGeneratorError(
|
|
error = error_message
|
|
)
|
|
|
|
# Log and send the exception
|
|
print(f"\n{generator_error.error.trace}")
|
|
return get_sse_packet(generator_error.json(ensure_ascii = False))
|
|
|
|
def get_sse_packet(json_data: str):
|
|
return f"data: {json_data}\n\n"
|