Maintain a generator that is a proxy for self.process_chunk() Usage: - The main POST SSE code that launches the streaming request will call .process_chunk with each incoming stream (as a handler) - NOTE: this interface is SINGLE THREADED, and meant to be used with a singl
| 256 | |
| 257 | |
| 258 | class StreamingServerInterface(AgentChunkStreamingInterface): |
| 259 | """Maintain a generator that is a proxy for self.process_chunk() |
| 260 | |
| 261 | Usage: |
| 262 | - The main POST SSE code that launches the streaming request |
| 263 | will call .process_chunk with each incoming stream (as a handler) |
| 264 | - |
| 265 | |
| 266 | NOTE: this interface is SINGLE THREADED, and meant to be used |
| 267 | with a single agent. A multi-agent implementation of this interface |
| 268 | should maintain multiple generators and index them with the request ID |
| 269 | """ |
| 270 | |
| 271 | def __init__( |
| 272 | self, |
| 273 | multi_step=True, |
| 274 | # Related to if we want to try and pass back the AssistantMessage as a special case function |
| 275 | use_assistant_message=False, |
| 276 | assistant_message_tool_name=DEFAULT_MESSAGE_TOOL, |
| 277 | assistant_message_tool_kwarg=DEFAULT_MESSAGE_TOOL_KWARG, |
| 278 | # Related to if we expect inner_thoughts to be in the kwargs |
| 279 | inner_thoughts_in_kwargs=True, |
| 280 | inner_thoughts_kwarg=INNER_THOUGHTS_KWARG, |
| 281 | ): |
| 282 | # If streaming mode, ignores base interface calls like .assistant_message, etc |
| 283 | self.streaming_mode = False |
| 284 | # NOTE: flag for supporting legacy 'stream' flag where send_message is treated specially |
| 285 | self.nonstreaming_legacy_mode = False |
| 286 | # If chat completion mode, creates a "chatcompletion-style" stream, but with concepts remapped |
| 287 | self.streaming_chat_completion_mode = False |
| 288 | self.streaming_chat_completion_mode_function_name = None # NOTE: sadly need to track state during stream |
| 289 | # If chat completion mode, we need a special stream reader to |
| 290 | # turn function argument to send_message into a normal text stream |
| 291 | self.streaming_chat_completion_json_reader = FunctionArgumentsStreamHandler(json_key=assistant_message_tool_kwarg) |
| 292 | |
| 293 | # @matt's changes here, adopting new optimistic json parser |
| 294 | self.current_function_arguments = [] |
| 295 | self.optimistic_json_parser = OptimisticJSONParser() |
| 296 | self.current_json_parse_result = {} |
| 297 | |
| 298 | # Store metadata passed from server |
| 299 | self.metadata = {} |
| 300 | |
| 301 | self._chunks = deque() |
| 302 | self._event = asyncio.Event() # Use an event to notify when chunks are available |
| 303 | self._active = True # This should be set to False to stop the generator |
| 304 | |
| 305 | # if multi_step = True, the stream ends when the agent yields |
| 306 | # if multi_step = False, the stream ends when the step ends |
| 307 | self.multi_step = multi_step |
| 308 | # self.multi_step_indicator = MessageStreamStatus.done_step |
| 309 | # self.multi_step_gen_indicator = MessageStreamStatus.done_generation |
| 310 | |
| 311 | # Support for AssistantMessage |
| 312 | self.use_assistant_message = use_assistant_message # TODO: Remove this (actually? @charles) |
| 313 | self.assistant_message_tool_name = assistant_message_tool_name |
| 314 | self.assistant_message_tool_kwarg = assistant_message_tool_kwarg |
| 315 | self.prev_assistant_message_id = None # Used to skip tool call response receipts for `send_message` |
no outgoing calls
no test coverage detected