-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add WebSocket conversation.chat.cancel
support
#191
Conversation
WalkthroughThe changes introduce new event classes and methods to facilitate conversation chat cancellation. Specifically, two new event types, Changes
Sequence Diagram(s)sequenceDiagram
participant C as Chat Client (Sync/Async)
participant E as Event Enqueuer
participant L as Event Loader (_load_event)
participant H as Chat Event Handler
C->>E: conversation_chat_cancel()
E->>L: Enqueue ConversationChatCancelEvent
L->>H: Identify "conversation.chat.canceled" event
H->>H: on_conversation_chat_canceled(cli, event)
Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 3
🧹 Nitpick comments (4)
examples/websockets_chat.py (1)
19-19
: Unused import detected.The
ConversationChatCancelEvent
is imported but not used in this file. Consider removing it if not needed for future implementations.-from cozepy import ( - COZE_CN_BASE_URL, - AsyncCoze, - AsyncWebsocketsChatClient, - AsyncWebsocketsChatEventHandler, - AudioFormat, - ConversationAudioDeltaEvent, - ConversationChatCompletedEvent, - ConversationChatCanceledEvent, - ConversationChatCreatedEvent, - ConversationChatRequiresActionEvent, - ConversationChatSubmitToolOutputsEvent, - ConversationChatCancelEvent, - ConversationMessageDeltaEvent, +from cozepy import ( + COZE_CN_BASE_URL, + AsyncCoze, + AsyncWebsocketsChatClient, + AsyncWebsocketsChatEventHandler, + AudioFormat, + ConversationAudioDeltaEvent, + ConversationChatCompletedEvent, + ConversationChatCanceledEvent, + ConversationChatCreatedEvent, + ConversationChatRequiresActionEvent, + ConversationChatSubmitToolOutputsEvent, + ConversationMessageDeltaEvent,🧰 Tools
🪛 Ruff (0.8.2)
19-19:
cozepy.ConversationChatCancelEvent
imported but unusedRemove unused import:
cozepy.ConversationChatCancelEvent
(F401)
examples/websockets_chat_realtime_gui.py (1)
299-306
: Handler for chat cancellation implemented with proper error handling.The handler follows the established pattern in the class with appropriate error handling to ensure the application remains stable even if an error occurs during event processing.
Consider adding a more descriptive message or updating the UI to reflect the canceled state, especially since this is a GUI application. For example:
async def on_conversation_chat_canceled( self, cli: AsyncWebsocketsChatClient, event: ConversationChatCanceledEvent ): try: print("打断") + # Update GUI to show chat was canceled + self.gui.status_label.config(text="对话已打断") + self.gui.update_chat_display("对话已被打断", is_user=False) + # Reset recording state + self.gui.root.after(1000, self.gui.resume_recording) except Exception as e: print(f"对话打断错误: {e}")cozepy/websockets/chat/__init__.py (2)
216-216
: Consider adding CONVERSATION_CHAT_CANCELED to wait_eventsThe current implementation only waits for CONVERSATION_CHAT_COMPLETED events. Since you're adding support for cancellation, you might want to also wait for CONVERSATION_CHAT_CANCELED events to ensure the client doesn't block indefinitely when a chat is canceled instead of completed.
- wait_events=[WebsocketsEventType.CONVERSATION_CHAT_COMPLETED], + wait_events=[WebsocketsEventType.CONVERSATION_CHAT_COMPLETED, WebsocketsEventType.CONVERSATION_CHAT_CANCELED],
467-467
: Consider adding CONVERSATION_CHAT_CANCELED to async wait_eventsSimilar to the sync client, the async client only waits for CONVERSATION_CHAT_COMPLETED events. Since you're adding support for cancellation, you might want to also wait for CONVERSATION_CHAT_CANCELED events here as well.
- wait_events=[WebsocketsEventType.CONVERSATION_CHAT_COMPLETED], + wait_events=[WebsocketsEventType.CONVERSATION_CHAT_COMPLETED, WebsocketsEventType.CONVERSATION_CHAT_CANCELED],
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
cozepy/__init__.py
(2 hunks)cozepy/websockets/chat/__init__.py
(8 hunks)cozepy/websockets/ws.py
(2 hunks)examples/websockets_chat.py
(2 hunks)examples/websockets_chat_realtime_gui.py
(2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
examples/websockets_chat.py
19-19: cozepy.ConversationChatCancelEvent
imported but unused
Remove unused import: cozepy.ConversationChatCancelEvent
(F401)
🔇 Additional comments (15)
cozepy/websockets/ws.py (2)
94-94
: New event type for chat cancellation added correctly.This new enum value
CONVERSATION_CHAT_CANCEL
appropriately defines the event type for canceling a chat conversation with a descriptive comment.
108-108
: New event type for chat canceled notification added correctly.This new enum value
CONVERSATION_CHAT_CANCELED
properly defines the event type that notifies when a chat has been canceled with a descriptive comment.examples/websockets_chat.py (2)
15-15
: Event import added correctly.The
ConversationChatCanceledEvent
import is correctly added to support the new event handler implementation.
118-122
: Event handler for chat cancellation implemented correctly.The handler follows the established pattern in the class, taking the client and event as parameters and logging an appropriate message when a chat is canceled.
cozepy/__init__.py (4)
128-128
: New event class properly exposed in module imports.The
ConversationChatCanceledEvent
is correctly added to the module imports, maintaining alphabetical ordering.
132-132
: New event class properly exposed in module imports.The
ConversationChatCancelEvent
is correctly added to the module imports.
262-262
: Export properly added to__all__
list.The
ConversationChatCancelEvent
is correctly added to the__all__
list, making it part of the public API.
269-269
: Export properly added to__all__
list.The
ConversationChatCanceledEvent
is correctly added to the__all__
list, making it part of the public API.examples/websockets_chat_realtime_gui.py (1)
22-22
: Event import properly added.The
ConversationChatCanceledEvent
import is correctly added to support the new handler implementation.cozepy/websockets/chat/__init__.py (6)
57-59
: Correctly implemented ConversationChatCancelEventThe new class for the cancel event follows the existing pattern for websocket events, correctly extending WebsocketsEvent with the appropriate event type.
126-128
: Correctly implemented ConversationChatCanceledEventThe new class for the canceled event follows the existing pattern for websocket events, correctly extending WebsocketsEvent with the appropriate event type.
225-227
: Correctly implemented conversation_chat_cancel methodThe implementation follows the existing pattern for enqueueing events to the input queue.
476-478
: Correctly implemented async conversation_chat_cancel methodThe async implementation follows the existing pattern for enqueueing events to the input queue.
581-587
: Correctly implemented _load_event handler for CONVERSATION_CHAT_CANCELEDThe handling of the canceled event in the async client is correctly implemented, properly checking for the right event type and returning the appropriate event object.
57-128
:❓ Verification inconclusive
Verify if the canceled event should include a data field
Many other completed/finished events in this file include a data field (like ConversationChatCompletedEvent includes Chat data). Consider whether ConversationChatCanceledEvent should also include data about the canceled chat to maintain consistency.
Compare with other similar events:
🏁 Script executed:
#!/bin/bash # Find patterns in other *CompletedEvent and *CanceledEvent classes to check for data field patterns echo "Checking for data fields in other event classes:" rg -A 3 'class \w+(?:Completed|Canceled|Created)Event' --no-filename | rg -A 2 'event_type'Length of output: 1284
Action: Verify Consistency of Data Field in Canceled Event
After reviewing the codebase:
- Similar conversation chat events like
ConversationChatCreatedEvent
,ConversationChatCompletedEvent
, andConversationChatRequiresActionEvent
all include adata: Chat
field.- The
ConversationChatCanceledEvent
currently only specifies theevent_type
without any accompanying data.Please verify whether the omission of the
data
field in the canceled event is intentional. If consistency is desired, consider adding the appropriatedata: Chat
field to this event.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
cozepy/__init__.py
(2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
cozepy/__init__.py
130-130: Redefinition of unused ConversationChatCanceledEvent
from line 127
Remove definition: ConversationChatCanceledEvent
(F811)
134-134: Redefinition of unused ConversationChatCancelEvent
from line 128
Remove definition: ConversationChatCancelEvent
(F811)
🔇 Additional comments (2)
cozepy/__init__.py (2)
127-128
: Imported new chat cancellation event classes.The addition of these two new event classes (
ConversationChatCanceledEvent
andConversationChatCancelEvent
) aligns with the PR objective of implementing support for chat cancellation.
264-265
: Added new event classes to the public API.The event classes
ConversationChatCancelEvent
andConversationChatCanceledEvent
are correctly added to the__all__
list, making them part of the public API.This change properly exposes the new event classes to users of the module, allowing them to work with the chat cancellation functionality.
Also applies to: 271-271
conversation.chat.cancel
support
support cancel chat event ‘conversation.chat.cancel’ and callback canceled event ‘conversation.chat.canceled’
Summary by CodeRabbit