(self, text: str)
| 6378 | } |
| 6379 | |
| 6380 | def _advance_stream_state(self, text: str) -> Tuple[bool, List[Dict[str, Any]]]: |
| 6381 | deltas: List[Dict[str, Any]] = [] |
| 6382 | if self._stream_plan is None: |
| 6383 | return False, deltas |
| 6384 | if self._direct.deltas: |
| 6385 | return self._advance_direct_stream_state(text) |
| 6386 | if self._stream_state is None: |
| 6387 | return False, deltas |
| 6388 | if self._stream_plan["kind"] == "segment-message": |
| 6389 | state = self._stream_state |
| 6390 | parsed = cast(Dict[str, Any], state.parsed) |
| 6391 | buffer = state.pending + text |
| 6392 | state.pending = "" |
| 6393 | while True: |
| 6394 | if state.mode == "segment-start": |
| 6395 | _, matched_start, remainder, pending = self._consume_until_any_literal( |
| 6396 | buffer, |
| 6397 | cast(Tuple[str, ...], self._stream_plan["segment_starts"]), |
| 6398 | ) |
| 6399 | if matched_start is None: |
| 6400 | state.pending = buffer if not pending else pending |
| 6401 | return True, deltas |
| 6402 | state.current_segment = cast( |
| 6403 | Dict[str, Any], |
| 6404 | self._stream_plan["segments_by_start"][matched_start], |
| 6405 | ) |
| 6406 | buffer = remainder |
| 6407 | state.mode = ( |
| 6408 | "segment-tool-item" |
| 6409 | if state.current_segment["kind"] == "iterator" |
| 6410 | else "segment-field" |
| 6411 | ) |
| 6412 | if state.current_segment["kind"] == "iterator": |
| 6413 | item_state = self._new_tool_call_state(state.current_segment["item"]) |
| 6414 | if item_state["kind"] in {"tagged-parameters", "json-message"}: |
| 6415 | tool_calls = cast( |
| 6416 | List[Dict[str, Any]], |
| 6417 | parsed.setdefault("tool_calls", []), |
| 6418 | ) |
| 6419 | tool_calls.append(item_state["tool_call"]) |
| 6420 | item_state["tool_call_index"] = len(tool_calls) - 1 |
| 6421 | state.current_item = item_state |
| 6422 | state.saw_tool_calls = True |
| 6423 | continue |
| 6424 | if state.mode == "segment-field": |
| 6425 | current_segment = state.current_segment |
| 6426 | if not isinstance(current_segment, dict): |
| 6427 | return False, deltas |
| 6428 | segment_text, matched, remainder, pending = self._consume_until_literal( |
| 6429 | buffer, |
| 6430 | cast(str, current_segment["end"]), |
| 6431 | ) |
| 6432 | field_name = cast(str, current_segment["field"]) |
| 6433 | self._append_parsed_text(parsed, field_name, segment_text) |
| 6434 | if segment_text: |
| 6435 | deltas.append({field_name: segment_text}) |
| 6436 | if not matched: |
| 6437 | state.pending = pending |
no test coverage detected