File size: 1,685 Bytes
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
from __future__ import annotations
"""Project classifier content within one public attempt."""

from free_claude_code.core.anthropic.stream_contracts import parse_sse_text
from free_claude_code.core.anthropic.streaming import format_sse_event


class ClassifierProjection:
    """Project one attempt's blocks without interpreting classifier text."""

    def __init__(self) -> None:
        self._indices: dict[int, int | None] = {}
        self._next_index = 0

    def feed(self, frame: str) -> str | None:
        events = parse_sse_text(frame)
        if not events:
            return frame
        event = events[0]
        payload = event.data
        kind = payload.get("type", event.event)
        if kind not in {
            "content_block_start",
            "content_block_delta",
            "content_block_stop",
        }:
            return frame
        index = payload.get("index")
        if not isinstance(index, int):
            return frame
        if kind == "content_block_start":
            block = payload.get("content_block")
            if isinstance(block, dict) and block.get("type") in {
                "thinking",
                "redacted_thinking",
            }:
                self._indices[index] = None
            else:
                self._indices[index] = self._next_index
                self._next_index += 1
        if index not in self._indices:
            return frame
        visible_index = self._indices[index]
        if kind == "content_block_stop":
            del self._indices[index]
        if visible_index is None:
            return None
        return format_sse_event(event.event, {**payload, "index": visible_index})