|
| 1 | +from .llms.types import ( |
| 2 | + ResponseChunkToolCallFailed, |
| 3 | + ResponseChunkToolCallFinished, |
| 4 | + ResponseChunkToolCallStarted, |
| 5 | + ResponseControlChunkStreamBegin, |
| 6 | + ResponseControlChunkStreamEnd, |
| 7 | + ResponseStream, |
| 8 | + ResponseTextChunk, |
| 9 | + ToolCall, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +class EventHandlerBase: |
| 14 | + """Base class for event handlers that process messages and events from the assistant and tools. |
| 15 | +
|
| 16 | + This can be used to implement User Interface classes, custom logic, etc. |
| 17 | + """ |
| 18 | + |
| 19 | + def info(self, message: str): |
| 20 | + """Print an informational message.""" |
| 21 | + pass |
| 22 | + |
| 23 | + def debug(self, message: str): |
| 24 | + """Print a debug message.""" |
| 25 | + pass |
| 26 | + |
| 27 | + def assistant_message_stream_start(self, stream: ResponseStream): |
| 28 | + """Called when the assistant starts sending a message stream.""" |
| 29 | + pass |
| 30 | + |
| 31 | + def assistant_message_stream_chunk(self, stream: ResponseStream, chunk: ResponseTextChunk): |
| 32 | + """Called for each chunk of the assistant's response.""" |
| 33 | + pass |
| 34 | + |
| 35 | + def assistant_message_stream_end(self, stream: ResponseStream): |
| 36 | + """Called when the assistant finishes sending a message stream.""" |
| 37 | + pass |
| 38 | + |
| 39 | + def tool_call_start(self, tool_call: ToolCall): |
| 40 | + """Called when a tool call is about to be executed.""" |
| 41 | + pass |
| 42 | + |
| 43 | + def tool_call_result(self, tool_call: ToolCall, result: str): |
| 44 | + """Called when a tool call has been executed successfully.""" |
| 45 | + pass |
| 46 | + |
| 47 | + def tool_call_error(self, tool_call: ToolCall, error: str): |
| 48 | + """Called when a tool call has failed.""" |
| 49 | + pass |
| 50 | + |
| 51 | + def loop_end(self, cost: float): |
| 52 | + pass |
| 53 | + |
| 54 | + |
| 55 | +async def use_event_handler(stream: ResponseStream, handler: EventHandlerBase): |
| 56 | + """Uses the event handler for the stream's messages. |
| 57 | +
|
| 58 | + This is basically an adapter that directs the stream's messages to the event handler. |
| 59 | + """ |
| 60 | + |
| 61 | + async for chunk in stream.all_chunks(): |
| 62 | + if isinstance(chunk, ResponseControlChunkStreamBegin): |
| 63 | + handler.assistant_message_stream_start(stream) |
| 64 | + elif isinstance(chunk, ResponseControlChunkStreamEnd): |
| 65 | + handler.assistant_message_stream_end(stream) |
| 66 | + elif isinstance(chunk, ResponseTextChunk): |
| 67 | + handler.assistant_message_stream_chunk(stream, chunk) |
| 68 | + elif isinstance(chunk, ResponseChunkToolCallStarted): |
| 69 | + handler.tool_call_start(chunk.tool_call) |
| 70 | + elif isinstance(chunk, ResponseChunkToolCallFinished): |
| 71 | + handler.tool_call_result(chunk.tool_call, chunk.result) |
| 72 | + elif isinstance(chunk, ResponseChunkToolCallFailed): |
| 73 | + handler.tool_call_error(chunk.tool_call, chunk.error) |
0 commit comments