|
57 | 57 | MessageDeltaEvent, |
58 | 58 | MessageStartEvent, |
59 | 59 | MessageStopEvent, |
| 60 | + TextBlock, |
| 61 | + ToolUseBlock, |
60 | 62 | ) |
61 | 63 |
|
62 | 64 | if TYPE_CHECKING: |
|
65 | 67 | raise DidNotEnable("Anthropic not installed") |
66 | 68 |
|
67 | 69 | try: |
68 | | - from anthropic.types import ThinkingBlock |
| 70 | + from anthropic.types import ThinkingBlock, ThinkingDelta |
69 | 71 | except ImportError: |
70 | 72 | ThinkingBlock = None # type: ignore[assignment,misc] |
71 | | - |
| 73 | + ThinkingDelta = None # type: ignore[assignment,misc] |
72 | 74 |
|
73 | 75 | if TYPE_CHECKING: |
74 | 76 | from typing import ( |
@@ -385,18 +387,16 @@ def _collect_ai_data( |
385 | 387 | event.content_block, ThinkingBlock |
386 | 388 | ): |
387 | 389 | content_blocks.append({"type": "thinking", "thinking": ""}) |
388 | | - else: |
| 390 | + elif isinstance(event.content_block, (TextBlock, ToolUseBlock)): |
389 | 391 | content_blocks.append({"type": "text", "text": ""}) |
390 | 392 | elif event.type == "content_block_delta" and content_blocks: |
391 | | - block = content_blocks[-1] |
392 | | - if block["type"] == "thinking": |
393 | | - block["thinking"] += event.delta.thinking |
394 | | - else: |
395 | | - chunk = getattr(event.delta, "text", None) or getattr( |
396 | | - event.delta, "partial_json", None |
397 | | - ) |
398 | | - if chunk: |
399 | | - block["text"] += chunk |
| 393 | + delta = event.delta |
| 394 | + if ThinkingDelta is not None and isinstance(delta, ThinkingDelta): |
| 395 | + content_blocks[-1]["thinking"] += delta.thinking |
| 396 | + elif hasattr(event.delta, "text"): |
| 397 | + content_blocks[-1]["text"] += event.delta.text |
| 398 | + elif hasattr(event.delta, "partial_json"): |
| 399 | + content_blocks[-1]["text"] += event.delta.partial_json |
400 | 400 | elif event.type == "content_block_stop": |
401 | 401 | pass |
402 | 402 |
|
@@ -754,9 +754,7 @@ def _set_output_data( |
754 | 754 | if output["type"] == "text": |
755 | 755 | parts.append({"type": "text", "content": output["text"]}) |
756 | 756 | elif output["type"] == "thinking": |
757 | | - parts.append( |
758 | | - {"type": "reasoning", "content": output.get("thinking", "")} |
759 | | - ) |
| 757 | + parts.append({"type": "reasoning", "content": output.get("thinking")}) |
760 | 758 | elif output["type"] == "tool_use": |
761 | 759 | tool_calls.append(output) |
762 | 760 |
|
|
0 commit comments