MCPcopy Index your code
hub / github.com/abetlen/llama-cpp-python / _advance_stream_state

Method _advance_stream_state

examples/server/server.py:6380–6670  ·  view source on GitHub ↗
(self, text: str)

Source from the content-addressed store, hash-verified

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

Callers 2

__init__Method · 0.95

Calls 12

_new_tool_call_stateMethod · 0.95
_append_parsed_textMethod · 0.95
_tool_call_deltaMethod · 0.95
parse_chat_responseMethod · 0.95
appendMethod · 0.80
extendMethod · 0.45

Tested by

no test coverage detected