sse_done.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """SSE ``[DONE]`` sentinel normalization for OpenAI-compatible proxies.
  2. Strict OpenAI clients treat a stream without ``data: [DONE]`` as truncated. The tracker watches the
  3. forwarded SSE bytes and says whether to append ONE ``[DONE]`` after a *clean* upstream EOF: only after
  4. a terminal choice (``finish_reason`` non-null) or ``lastOne: true``; never after an error event, an
  5. interrupted stream, or when the upstream already sent ``[DONE]``.
  6. """
  7. from __future__ import annotations
  8. import json
  9. from dataclasses import dataclass, field
  10. DONE_SSE_FRAME = b"data: [DONE]\n\n"
  11. @dataclass
  12. class SseDoneTracker:
  13. """Incremental scanner over forwarded SSE chunks."""
  14. saw_done: bool = False
  15. saw_terminal_finish: bool = False
  16. saw_last_one: bool = False
  17. saw_error_event: bool = False
  18. saw_malformed_event: bool = False
  19. interrupted: bool = False
  20. _buf: bytearray = field(default_factory=bytearray, repr=False)
  21. _data_lines: list = field(default_factory=list, repr=False)
  22. def feed(self, chunk: bytes) -> None:
  23. """Observe a forwarded chunk (bytes are not modified)."""
  24. if not chunk:
  25. return
  26. self._buf.extend(chunk)
  27. while (nl := self._buf.find(b"\n")) >= 0:
  28. line = bytes(self._buf[:nl])
  29. del self._buf[: nl + 1]
  30. self._consume_line(line)
  31. def mark_interrupted(self) -> None:
  32. """Upstream stream ended via error/cancel — do not synthesize DONE."""
  33. self.interrupted = True
  34. def _blocked(self) -> bool:
  35. return self.saw_done or self.saw_error_event or self.saw_malformed_event
  36. def should_append_done(self) -> bool:
  37. """True when a single terminal ``[DONE]`` should be appended."""
  38. if self.interrupted or self._blocked():
  39. return False
  40. # Flush a trailing line without a final newline (rare but valid), then dispatch a final
  41. # event that never saw its blank-line boundary.
  42. if self._buf:
  43. self._consume_line(bytes(self._buf))
  44. self._buf.clear()
  45. self._dispatch_event()
  46. if self._blocked():
  47. return False
  48. return self.saw_terminal_finish or self.saw_last_one
  49. def _consume_line(self, line: bytes) -> None:
  50. if line.endswith(b"\r"): # CRLF-delimited SSE
  51. line = line[:-1]
  52. if not line: # blank line = event boundary
  53. self._dispatch_event()
  54. return
  55. if not line.startswith(b"data:"):
  56. return
  57. # One event may span several ``data:`` lines joined with "\n" at dispatch; parsing each
  58. # line alone would misread a split JSON event as two malformed fragments.
  59. self._data_lines.append(line[5:].strip())
  60. def _dispatch_event(self) -> None:
  61. if not self._data_lines:
  62. return
  63. payload = b"\n".join(self._data_lines).strip()
  64. self._data_lines = []
  65. if payload == b"[DONE]":
  66. self.saw_done = True
  67. return
  68. if not payload:
  69. return
  70. try:
  71. event = json.loads(payload.decode("utf-8"))
  72. except (UnicodeDecodeError, json.JSONDecodeError):
  73. self.saw_malformed_event = True
  74. return
  75. if not isinstance(event, dict):
  76. return
  77. if event.get("error") is not None:
  78. self.saw_error_event = True
  79. return
  80. # Relabelled upstreams have been observed sending ``"lastOne": 1`` / ``"true"``.
  81. if event.get("lastOne") in (True, 1, "true"):
  82. self.saw_last_one = True
  83. for choice in event.get("choices") or []:
  84. if not isinstance(choice, dict):
  85. continue
  86. fr = choice.get("finish_reason")
  87. if fr is not None:
  88. self.saw_terminal_finish = True
  89. # OpenAI error-shaped finish reasons should not unlock DONE.
  90. if isinstance(fr, str) and fr.lower() in {"error", "provider_error"}:
  91. self.saw_error_event = True
  92. def content_type_is_sse(headers) -> bool:
  93. """True when response headers advertise an SSE body."""
  94. try:
  95. value = headers.get("Content-Type") or headers.get("content-type") or ""
  96. except Exception:
  97. value = ""
  98. return "text/event-stream" in str(value).lower()
  99. __all__ = ["DONE_SSE_FRAME", "SseDoneTracker", "content_type_is_sse"]