chat_completion_helpers_tool_calls.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """Streamed tool-call delta assembly for the chat-completions stream (and Relay's accumulator)."""
  2. from typing import Optional
  3. from agent.reasoning_carriers import field
  4. class _ToolCallAccumulator:
  5. """Assemble streamed tool-call deltas into complete ``tool_calls`` entries
  6. (``acc``: slot index -> entry dict). Ollama-compatible endpoints reuse index 0
  7. for every call in a parallel batch, distinguishing them only by id, so a new
  8. id at an already-seen raw index is redirected to a fresh slot."""
  9. def __init__(self):
  10. self.acc: dict = {}
  11. self._notified: set = set()
  12. self._last_id_at_idx: dict = {} # raw_index -> last seen non-empty id
  13. self._active_slot_by_idx: dict = {} # raw_index -> current slot in acc
  14. # Argument deltas are collected per slot and joined once in ``materialize`` —
  15. # ``+=`` per chunk rebuilds the whole string every delta (quadratic on big args).
  16. self._argument_parts: dict[int, list[str]] = {}
  17. # slot -> (parts scanned, depth, in_string, escaped, started); see ``_arguments_closed``.
  18. self._arg_scan: dict[int, tuple] = {}
  19. def materialize(self) -> dict:
  20. """Join buffered argument deltas into each entry's ``arguments``; idempotent. Returns ``acc``."""
  21. for idx, parts in self._argument_parts.items():
  22. self.acc[idx]["function"]["arguments"] = "".join(parts)
  23. return self.acc
  24. def _starts_new_call(self, tc_delta, slot: int) -> bool:
  25. """Whether a fresh id at a reused raw index opens a new call (Ollama parallel batch) or
  26. continues the current one. LM Studio and Kimi send a fresh id on every argument fragment
  27. with no name, and some MiniMax routes resend the same name on each fragment too: only a
  28. named delta can start a call, and a same-name one only once the current call's arguments
  29. have closed (two parallel calls of one tool must still split)."""
  30. name = getattr(getattr(tc_delta, "function", None), "name", None)
  31. if not name:
  32. return False
  33. if name != self.acc.get(slot, {}).get("function", {}).get("name"):
  34. return True
  35. return self._arguments_closed(slot)
  36. def _arguments_closed(self, slot: int) -> bool:
  37. """Structural completion (brackets balanced outside strings), not strict JSON validity, so a
  38. repairable call with a trailing comma still ends its slot. Scans only fragments added since
  39. the last check: linear over the call, and free for providers that never reach here."""
  40. parts = self._argument_parts.get(slot, [])
  41. done, depth, in_string, escaped, started = self._arg_scan.get(slot, (0, 0, False, False, False))
  42. for ch in "".join(parts[done:]):
  43. if in_string:
  44. if escaped:
  45. escaped = False
  46. elif ch == "\\":
  47. escaped = True
  48. elif ch == '"':
  49. in_string = False
  50. elif ch == '"':
  51. in_string = True
  52. elif ch in "{[":
  53. depth, started = depth + 1, True
  54. elif ch in "}]":
  55. depth -= 1
  56. self._arg_scan[slot] = (len(parts), depth, in_string, escaped, started)
  57. return started and depth == 0 and not in_string
  58. def feed(self, tc_delta) -> Optional[str]:
  59. """Merge one delta; return the tool name the first time it is complete."""
  60. raw_idx = getattr(tc_delta, "index", None)
  61. if raw_idx is None:
  62. raw_idx = 0
  63. tc_id = getattr(tc_delta, "id", None)
  64. delta_id = tc_id or ""
  65. if isinstance(tc_id, int): # Poolside sends integer ids
  66. tc_id = str(tc_id)
  67. self._active_slot_by_idx.setdefault(raw_idx, raw_idx)
  68. if (
  69. delta_id and raw_idx in self._last_id_at_idx and delta_id != self._last_id_at_idx[raw_idx]
  70. and self._starts_new_call(tc_delta, self._active_slot_by_idx[raw_idx])
  71. ):
  72. self._active_slot_by_idx[raw_idx] = max(self.acc, default=-1) + 1
  73. if delta_id:
  74. self._last_id_at_idx[raw_idx] = delta_id
  75. idx = self._active_slot_by_idx[raw_idx]
  76. entry = self.acc.setdefault(
  77. idx, {"id": tc_id or "", "type": "function", "function": {"name": "", "arguments": ""}, "extra_content": None},
  78. )
  79. parts = self._argument_parts.setdefault(idx, [])
  80. if tc_id:
  81. entry["id"] = tc_id
  82. tc_function = getattr(tc_delta, "function", None)
  83. if tc_function:
  84. if getattr(tc_function, "name", None):
  85. # Assignment, not +=: names arrive complete and some providers (MiniMax via
  86. # NVIDIA NIM) resend the full name every chunk — += gives "read_fileread_file".
  87. entry["function"]["name"] = tc_function.name
  88. if getattr(tc_function, "arguments", None):
  89. parts.append(tc_function.arguments)
  90. # Some gateways sign Gemini 3 calls inside ``function``.
  91. if isinstance(fn_sig := field(tc_function, "thought_signature"), str) and fn_sig:
  92. entry["thought_signature"] = fn_sig
  93. extra = getattr(tc_delta, "extra_content", None)
  94. if extra is None and hasattr(tc_delta, "model_extra"):
  95. extra = (tc_delta.model_extra if isinstance(tc_delta.model_extra, dict) else {}).get("extra_content")
  96. if extra is not None:
  97. from agent.chat_completion_helpers import _dump_if_model
  98. entry["extra_content"] = _dump_if_model(extra)
  99. name = entry["function"]["name"]
  100. if name and idx not in self._notified:
  101. self._notified.add(idx)
  102. return name
  103. return None