Spaces:
Running
Running
File size: 19,428 Bytes
e99df76 f7872b8 e99df76 2e7b8ca e99df76 a9e27c0 2e7b8ca a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 2e7b8ca f7872b8 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 2e7b8ca e99df76 a9e27c0 e99df76 a9e27c0 e99df76 a9e27c0 2e7b8ca e99df76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | from __future__ import annotations
"""Own public projection, tool release, recovery mapping and wire termination."""
import re
from collections.abc import AsyncGenerator, AsyncIterator, Callable, Iterator
from copy import deepcopy
from typing import Any, Literal
try:
import simplejson
except ImportError:
import json as simplejson
from free_claude_code.core.anthropic.streaming.decoder import AnthropicSSEDecoder
from free_claude_code.core.async_iterators import try_close_async_iterator
from free_claude_code.core.continuation_stream import ContinuationStream
from free_claude_code.core.delivered_response import DeliveredResponse
from free_claude_code.core.failures import (
ExecutionFailure,
FailureKind,
find_execution_failure,
)
from free_claude_code.core.gemini.response import gemini_finished, gemini_parts
from free_claude_code.core.replay_finalization import finalize_replay
from free_claude_code.core.stream_delivery import (
StreamDeliveryState,
bind_stream_delivery,
)
from free_claude_code.core.stream_output import StreamOutput, call_references
from .classifier_projection import ClassifierProjection
from .stream_frames import frame_data, replace_frame_data
class DeliveryObservedStream(AsyncIterator[StreamOutput]):
"""Bind observation for each read/close, including prefetch in another task."""
def __init__(
self, body: AsyncIterator[StreamOutput], state: StreamDeliveryState
) -> None:
self._body = body
self._state = state
def __aiter__(self) -> DeliveryObservedStream:
return self
async def __anext__(self) -> StreamOutput:
with bind_stream_delivery(self._state):
return await anext(self._body)
async def aclose(self) -> None:
with bind_stream_delivery(self._state):
error = await try_close_async_iterator(self._body)
if error is not None:
raise error
class PublicResponseStream(AsyncIterator[str]):
"""Publish one response while providers retain their physical request loops."""
def __init__(
self,
body: AsyncIterator[StreamOutput],
state: StreamDeliveryState,
*,
wire_api: Literal["messages", "responses", "gemini", "chat"],
first_chunk: StreamOutput,
terminal_frame: Callable[[str, str, BaseException], str] | None,
terminal_failure_observer: Callable[[BaseException], None] | None,
hide_reasoning: bool = False,
) -> None:
self._body = body
self.state = state
self._wire_api = wire_api
self._revision = state.attempt_revision
self._hide_reasoning = hide_reasoning
self._decoder = AnthropicSSEDecoder()
self._projection = ClassifierProjection() if hide_reasoning else None
self._delivered = DeliveredResponse(wire_api)
self._continuation = ContinuationStream(self._delivered)
state.bind_prefix(self._delivered.snapshot)
self._published_starts: set[str] = set()
self._seen_starts: set[str] = set()
self._replacement = False
self._public_id: str | None = None
self._created_at: object = None
self._sequence = -1
self._pending_starts: list[str] = []
self._replay_carriers: dict[str, str] = {}
self.start_frame = ""
self._initial_chunk = "".join(first_chunk.frames)
self._latest_chunk = ""
self._terminal_frame = terminal_frame
self._terminal_failure_observer = terminal_failure_observer
self._done = False
self._closed = False
self._pending_release = False
self._pending_call_proofs: set[tuple[object, ...]] = set()
self._events = self._iterate()
def __aiter__(self) -> PublicResponseStream:
return self
async def __anext__(self) -> str:
if self._closed or self._done:
raise StopAsyncIteration
try:
while True:
packet, synthetic = await anext(self._events)
value = self._publish_packet(packet, synthetic=synthetic)
if value is not None:
self._latest_chunk = value
return value
except StopAsyncIteration:
self._done = True
raise
except (Exception, BaseExceptionGroup) as exc:
self._done = True
self._pending_starts.clear()
failure = find_execution_failure(exc) or exc
if self._terminal_frame is None:
raise
if self._terminal_failure_observer is not None:
self._terminal_failure_observer(failure)
frame = self._terminal_frame(
self.start_frame or self._initial_chunk, self._latest_chunk, failure
)
return self._publish_packet(StreamOutput((frame,)), synthetic=True) or frame
def _publish_packet(self, packet: StreamOutput, *, synthetic: bool) -> str | None:
excluded = {
"_body",
"state",
"_events",
"_terminal_frame",
"_terminal_failure_observer",
}
saved = deepcopy(
{key: value for key, value in self.__dict__.items() if key not in excluded}
)
try:
self._pending_call_proofs.update(
self._continuation.map_call_reference(call).identity
for call in packet.completed_calls
)
published = []
for chunk in packet.frames:
for raw in self._decoder.feed_frames(chunk):
for frame in (raw,) if synthetic else self._process(raw):
frame = self._finalize_frame(frame)
value = self._publish(frame, synthetic=synthetic)
if value is not None:
published.append(value)
# Replay removal can hold a completed call until a later text packet.
# Retain only its proof identity and consume it at actual publication.
calls = tuple(
call
for call in call_references(published)
if call.identity in self._pending_call_proofs
)
self._delivered.complete_calls(calls)
self._pending_call_proofs.difference_update(call.identity for call in calls)
if self._pending_release:
self.state.release_content(synthetic=synthetic)
self._pending_release = False
return "".join(published) or None
except BaseException:
self.__dict__.update(saved)
self.state.bind_prefix(self._delivered.snapshot)
raise
def _finalize_frame(self, frame: str) -> str:
payload = frame_data(frame)
if payload is None:
return frame
if finalize_replay(payload, self._delivered.snapshot, self._replay_carriers):
if self._continuation.active and not self._continuation.made_progress:
raise ExecutionFailure(
kind=FailureKind.UPSTREAM,
status_code=502,
message="The provider continuation completed without adding output.",
retryable=False,
)
frame = replace_frame_data(frame, payload)
kind = payload.get("type")
if kind == "response.failed":
response = payload.get("response")
if not isinstance(response, dict):
return frame
# Failure snapshots may be based on the provider's empty start frame.
# Use only retained public evidence, independently of a failed mapper.
if self._delivered.retention_exhausted:
response["output"] = []
response["usage"] = None
error = response.get("error")
error = error if isinstance(error, dict) else {}
message = error.get("message") or "Provider response failed."
response["error"] = {
**error,
"message": f"{message}\n\nThe partial answer was already streamed. "
"A complete final snapshot is unavailable.",
}
elif self._continuation.active or self.state.transition is not None:
# A timeout can precede the first frame that activates recovery.
response["output"] = [
deepcopy(item) for _, item in sorted(self._delivered.items.items())
]
response["usage"] = self._delivered.usage(response["output"])
else:
return frame
elif not self._continuation.active or kind in {"error", "response.error"}:
return frame
else:
payload = self._continuation.finalize(payload)
return replace_frame_data(frame, payload)
async def _iterate(self) -> AsyncGenerator[tuple[StreamOutput, bool]]:
while True:
try:
packet = await anext(self._body)
except StopAsyncIteration:
for frame in self._synchronize():
yield StreamOutput((frame,)), True
if self._continuation.handoff:
for payload in self._delivered.handoff_events():
yield StreamOutput((self._format(payload),)), True
if self._wire_api == "chat":
yield StreamOutput(("data: [DONE]\n\n",)), True
return
for frame in self._synchronize():
yield StreamOutput((frame,)), True
yield packet, False
def _synchronize(self) -> Iterator[str]:
transition = self.state.transition
if self._revision == self.state.attempt_revision and transition is None:
return
self._revision = self.state.attempt_revision
self.state.transition = None
self._decoder = AnthropicSSEDecoder()
self._projection = ClassifierProjection() if self._hide_reasoning else None
self._seen_starts.clear()
self._pending_starts.clear()
self._pending_call_proofs.clear()
if self._wire_api == "chat" and not (
self.state.content_released or self._pending_release
):
self._public_id = None
self._created_at = None
if transition is not None:
self._continuation.begin(handoff=transition == "handoff")
elif self._continuation.active:
self._continuation.restart_attempt()
self._replacement = (
bool(self._published_starts) and not self._continuation.active
)
for event in self._continuation.boundary():
yield self._format(event)
def _process(self, frame: str) -> Iterator[str]:
if self._projection is not None:
projected = self._projection.feed(frame)
if projected is None:
return
frame = projected
yield from self._prepare(frame)
def _prepare(self, frame: str) -> Iterator[str]:
if not self._continuation.active:
yield frame
return
payload = frame_data(frame)
if payload is None:
yield frame
return
for item in self._continuation.prepare(payload):
yield (
replace_frame_data(frame, item)
if item.get("type") == payload.get("type")
else self._format(item)
)
@staticmethod
def _format(payload: dict[str, Any]) -> str:
if "type" not in payload:
return f"data: {simplejson.dumps(payload, use_decimal=True)}\n\n"
return f"event: {payload['type']}\ndata: {simplejson.dumps(payload, use_decimal=True)}\n\n"
async def aclose(self) -> None:
if self._closed:
return
self._closed = self._done = True
await self._events.aclose()
self._pending_starts.clear()
self._pending_call_proofs.clear()
self._decoder = AnthropicSSEDecoder()
self._replay_carriers.clear()
self.state.bind_prefix(None)
self._delivered = DeliveredResponse(self._wire_api)
self._continuation = ContinuationStream(self._delivered)
self.start_frame = self._initial_chunk = self._latest_chunk = ""
error = await try_close_async_iterator(self._body)
if error is not None:
raise error
def _release_content(self, frame: str, *, synthetic: bool) -> str:
self._pending_release = True
for start in self._pending_starts:
self._record(start, synthetic=synthetic)
self._record(frame, synthetic=synthetic)
if not self._pending_starts:
return frame
self.start_frame = self._pending_starts[0]
self._published_starts.add("message_start")
prefix = "".join(self._pending_starts)
self._pending_starts.clear()
return prefix + frame
def _record(self, frame: str, *, synthetic: bool) -> None:
if self._wire_api == "chat" and frame.strip() == "data: [DONE]":
return
payload = frame_data(frame)
if payload is not None:
self._delivered.observe(payload, count_progress=not synthetic)
elif any(line.startswith("data:") for line in frame.splitlines()):
self._delivered.unsafe_reason = "unknown_event"
def _publish(self, frame: str, *, synthetic: bool = False) -> str | None:
"""Keep replacement and failure envelopes in the public response lifecycle."""
payload = frame_data(frame)
if payload is None:
if self._wire_api == "chat" and frame.strip() == "data: [DONE]":
return self._release_content(frame, synthetic=synthetic)
if any(
line.strip() and not line.startswith(":")
for line in re.split(r"\r\n|\r|\n", frame)
):
return self._release_content(frame, synthetic=synthetic)
return frame
raw_kind = payload.get("type")
if self._wire_api == "chat":
if self._public_id is None and isinstance(payload.get("id"), str):
self._public_id = payload["id"]
self._created_at = payload.get("created")
elif self._public_id is not None and "id" in payload:
payload["id"] = self._public_id
if self._created_at is not None:
payload["created"] = self._created_at
frame = replace_frame_data(frame, payload)
if "error" in payload:
self._pending_starts.clear()
elif self._metadata("", payload) and not (
self.state.content_released or self._pending_release
):
self._pending_starts.append(frame)
return None
if self._wire_api == "gemini":
if self._public_id is None and isinstance(payload.get("responseId"), str):
self._public_id = payload["responseId"]
self.start_frame = frame
elif self._public_id is not None and "responseId" in payload:
payload["responseId"] = self._public_id
frame = replace_frame_data(frame, payload)
kind = raw_kind if isinstance(raw_kind, str) else ""
metadata = self._metadata(kind, payload)
if self._wire_api == "messages":
if (
kind == "message_start"
and metadata
and not (self.state.content_released or self._pending_release)
):
self._pending_starts.append(frame)
return None
if kind == "error":
self._pending_starts.clear()
start = kind in {"message_start", "response.created", "response.in_progress"}
if start and metadata:
if kind not in self._seen_starts:
self._seen_starts.add(kind)
if self._replacement and kind in self._published_starts:
return None
self._published_starts.add(kind)
if not self.start_frame:
self.start_frame = frame
value = payload.get(
"message" if self._wire_api == "messages" else "response"
)
if isinstance(value, dict):
self._public_id = value.get("id")
self._created_at = value.get("created_at")
changed = False
failure = kind == "response.failed"
if (self._replacement or failure) and self._wire_api == "responses":
response = payload.get("response")
if isinstance(response, dict):
if (
self._public_id is not None
and response.get("id") != self._public_id
):
response["id"] = self._public_id
changed = True
if (
self._created_at is not None
and response.get("created_at") != self._created_at
):
response["created_at"] = self._created_at
changed = True
if (
"response_id" in payload
and self._public_id is not None
and payload["response_id"] != self._public_id
):
payload["response_id"] = self._public_id
changed = True
if self._wire_api == "responses":
self._sequence += 1
payload["sequence_number"] = self._sequence
changed = True
frame = replace_frame_data(frame, payload) if changed else frame
if metadata:
self._record(frame, synthetic=synthetic)
return frame
return self._release_content(frame, synthetic=synthetic)
def _metadata(self, kind: str, payload: dict[str, Any]) -> bool:
if self._wire_api == "chat":
return "error" not in payload and all(
choice.get("finish_reason") is None
and all(
key == "role" or value in (None, "", [], {})
for key, value in (choice.get("delta") or {}).items()
)
for choice in payload.get("choices", [])
)
if self._wire_api == "gemini":
return (
not gemini_parts(payload)
and not gemini_finished(payload)
and "error" not in payload
)
if kind == "ping":
return True
if self._wire_api == "messages" and kind == "message_start":
message = payload.get("message")
return (
isinstance(message, dict)
and message.get("content", []) == []
and message.get("stop_reason") is None
)
if self._wire_api == "responses" and kind in {
"response.created",
"response.in_progress",
}:
response = payload.get("response")
return (
isinstance(response, dict)
and response.get("output", []) == []
and response.get("status") in (None, "queued", "in_progress")
)
return False
|