From d6b0de8667a4f748941a17d142d46c57ba9ed245 Mon Sep 17 00:00:00 2001 From: ddharman-quora <162623013+ddharman-quora@users.noreply.github.com> Date: Wed, 4 Dec 2024 14:00:20 -0800 Subject: [PATCH] feat: Add support for report_reaction endpoint (#128) * feat: Add support for report_reaction endpoint - This is to support message reactions launch. This will replace report_feedback which will be marked deprecated but still functional. Will update documentation around launch time - Tested end-to-end with server bot running locally and trying different reactions and verifying endpoint is called - Some formatting changes are in here since I ran all the recommended steps in the documentation - Updated version to 0.0.49 * Fix formatting errors by running pre-commit * Remove on_reaction and only keep on_reaction_with_context --- pyproject.toml | 2 +- src/fastapi_poe/__init__.py | 2 ++ src/fastapi_poe/base.py | 29 +++++++++++++++++++++++++++++ src/fastapi_poe/client.py | 21 +++++++++++++++++++++ src/fastapi_poe/types.py | 22 +++++++++++++++++++++- 5 files changed, 74 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 82a625d..aaf0b92 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "fastapi_poe" -version = "0.0.48" +version = "0.0.49" authors = [ { name="Lida Li", email="lli@quora.com" }, { name="Jelle Zijlstra", email="jelle@quora.com" }, diff --git a/src/fastapi_poe/__init__.py b/src/fastapi_poe/__init__.py index cd0e5a3..3e3b9ba 100644 --- a/src/fastapi_poe/__init__.py +++ b/src/fastapi_poe/__init__.py @@ -12,6 +12,7 @@ "QueryRequest", "SettingsRequest", "ReportFeedbackRequest", + "ReportReactionRequest", "ReportErrorRequest", "SettingsResponse", "PartialResponse", @@ -44,6 +45,7 @@ QueryRequest, ReportErrorRequest, ReportFeedbackRequest, + ReportReactionRequest, RequestContext, SettingsRequest, SettingsResponse, diff --git a/src/fastapi_poe/base.py b/src/fastapi_poe/base.py index 790f726..aae2fc1 100644 --- a/src/fastapi_poe/base.py +++ b/src/fastapi_poe/base.py @@ -46,6 +46,7 @@ QueryRequest, ReportErrorRequest, ReportFeedbackRequest, + ReportReactionRequest, RequestContext, SettingsRequest, SettingsResponse, @@ -246,6 +247,23 @@ async def on_feedback_with_context( """ await self.on_feedback(feedback_request) + async def on_reaction_with_context( + self, reaction_request: ReportReactionRequest, context: RequestContext + ) -> None: + """ + + Override this to record reaction from the user. This also includes the request context + information. + + #### Parameters: + - `reaction_request` (`ReportReactionRequest`): An object representing a reaction request + from Poe. This is sent out when a user provides reaction on a response on your bot. + - `context` (`RequestContext`): an object representing the current HTTP request. + #### Returns: `None` + + """ + pass + async def on_error(self, error_request: ReportErrorRequest) -> None: """ @@ -674,6 +692,12 @@ async def handle_report_feedback( await self.on_feedback_with_context(feedback_request, context) return JSONResponse({}) + async def handle_report_reaction( + self, reaction_request: ReportReactionRequest, context: RequestContext + ) -> JSONResponse: + await self.on_reaction_with_context(reaction_request, context) + return JSONResponse({}) + async def handle_report_error( self, error_request: ReportErrorRequest, context: RequestContext ) -> JSONResponse: @@ -852,6 +876,11 @@ async def poe_post(request: Request, dict: object = Depends(auth_user)) -> Respo ReportFeedbackRequest.parse_obj(request_body), RequestContext(http_request=request), ) + elif request_body["type"] == "report_reaction": + return await bot.handle_report_reaction( + ReportReactionRequest.parse_obj(request_body), + RequestContext(http_request=request), + ) elif request_body["type"] == "report_error": return await bot.handle_report_error( ReportErrorRequest.parse_obj(request_body), diff --git a/src/fastapi_poe/client.py b/src/fastapi_poe/client.py index bbc27ce..c12fcc8 100644 --- a/src/fastapi_poe/client.py +++ b/src/fastapi_poe/client.py @@ -114,6 +114,27 @@ async def report_feedback( }, ) + async def report_reaction( + self, + message_id: Identifier, + user_id: Identifier, + conversation_id: Identifier, + reaction: str, + ) -> None: + """Report message reaction to the bot server.""" + await self.session.post( + self.endpoint, + headers=self.headers, + json={ + "version": PROTOCOL_VERSION, + "type": "report_reaction", + "message_id": message_id, + "user_id": user_id, + "conversation_id": conversation_id, + "reaction": reaction, + }, + ) + async def fetch_settings(self) -> SettingsResponse: """Fetches settings from a Poe server bot endpoint.""" resp = await self.session.post( diff --git a/src/fastapi_poe/types.py b/src/fastapi_poe/types.py index 167660a..1e45eaa 100644 --- a/src/fastapi_poe/types.py +++ b/src/fastapi_poe/types.py @@ -79,7 +79,9 @@ class BaseRequest(BaseModel): """Common data for all requests.""" version: str - type: Literal["query", "settings", "report_feedback", "report_error"] + type: Literal[ + "query", "settings", "report_feedback", "report_reaction", "report_error" + ] class QueryRequest(BaseRequest): @@ -146,6 +148,24 @@ class ReportFeedbackRequest(BaseRequest): feedback_type: FeedbackType +class ReportReactionRequest(BaseRequest): + """ + + Request parameters for a report_reaction request. + #### Fields: + - `message_id` (`Identifier`) + - `user_id` (`Identifier`) + - `conversation_id` (`Identifier`) + - `reaction` (`str`) + + """ + + message_id: Identifier + user_id: Identifier + conversation_id: Identifier + reaction: str + + class ReportErrorRequest(BaseRequest): """