Skip to content

Commit ec7d868

Browse files
committed
RDBC-934 Unhandled actions
1 parent bf32c7a commit ec7d868

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

ravendb/documents/ai/ai_conversation.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class AiHandleErrorStrategy:
2222
SEND_ERRORS_TO_MODEL = "SendErrorsToModel"
2323
RAISE_IMMEDIATELY = "RaiseImmediately"
2424

25+
class UnhandledActionEventArgs:
26+
def __init__(self, sender: AiConversation, action: AiAgentActionRequest):
27+
self.sender = sender
28+
self.action = action
29+
2530

2631
class AiConversation:
2732
"""
@@ -54,6 +59,8 @@ def __init__(
5459
# Action handlers
5560
self._invocations: Dict[str, Callable[[AiAgentActionRequest], None]] = {}
5661

62+
self.on_unhandled_action: Optional[Callable[[UnhandledActionEventArgs], None]] = None
63+
5764
def __enter__(self) -> AiConversation:
5865
"""Context manager entry."""
5966
return self
@@ -224,12 +231,14 @@ def _handle_server_reply(self, answer: AiAnswer) -> bool:
224231
# Invoke the registered handler
225232
# Error handling is done by the invocation based on the error strategy
226233
self._invocations[action.name](action)
234+
elif self.on_unhandled_action is not None:
235+
self.on_unhandled_action(UnhandledActionEventArgs(self, action))
227236
else:
228237
# No handler registered for this action
229238
raise RuntimeError(
230239
f"There is no action defined for action '{action.name}' on agent '{self._agent_id}' "
231240
f"({self._conversation_id}), but it was invoked by the model with: {action.arguments}. "
232-
f"Did you forget to call receive() or handle()?"
241+
f"Did you forget to call {self.receive.__name__}() or {self.handle.__name__}()? You can also handle unexpected action invocations using the {self.on_unhandled_action.__name__} event."
233242
)
234243

235244
# If we have nothing to tell the server (no action responses), we're done

0 commit comments

Comments
 (0)