Skip to content

Commit c392f66

Browse files
committed
add synthetic child calls
1 parent df6c268 commit c392f66

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

weave/integrations/claude_agent_sdk/claude_agent_sdk_integration.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from typing import Any
1010

1111
from weave.integrations.claude_agent_sdk.display_utils import (
12+
text_display_name,
13+
thinking_display_name,
1214
tool_use_display_name,
1315
turn_display_name,
1416
)
@@ -66,6 +68,7 @@ def _process_message_inline(
6668
from claude_agent_sdk import (
6769
AssistantMessage,
6870
SystemMessage,
71+
TextBlock,
6972
ThinkingBlock,
7073
ToolResultBlock,
7174
ToolUseBlock,
@@ -104,6 +107,34 @@ def _is_thinking_only(m: Any) -> bool:
104107
history = list(accumulated)
105108
accumulated.append(serialized)
106109

110+
# Create child calls for thinking blocks
111+
thinking_blocks = [b for b in msg.content if isinstance(b, ThinkingBlock)]
112+
if thinking_blocks:
113+
thinking_text = "\n".join(b.thinking for b in thinking_blocks)
114+
thinking_call = wc.create_call(
115+
op="claude_agent_sdk.thinking",
116+
inputs={},
117+
display_name=thinking_display_name(thinking_text),
118+
parent=root_call,
119+
attributes={"kind": "llm"},
120+
use_stack=False,
121+
)
122+
wc.finish_call(thinking_call, output={"thinking": thinking_text})
123+
124+
# Create child calls for text blocks
125+
text_blocks = [b for b in msg.content if isinstance(b, TextBlock)]
126+
if text_blocks:
127+
text_content = "\n".join(b.text for b in text_blocks)
128+
text_call = wc.create_call(
129+
op="claude_agent_sdk.text",
130+
inputs={},
131+
display_name=text_display_name(text_content),
132+
parent=root_call,
133+
attributes={"kind": "llm"},
134+
use_stack=False,
135+
)
136+
wc.finish_call(text_call, output={"text": text_content, "model": msg.model})
137+
107138
tool_uses = [b for b in msg.content if isinstance(b, ToolUseBlock)]
108139

109140
# Open tool calls

weave/integrations/claude_agent_sdk/display_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@ def tool_use_display_name(tool_name: str, tool_input: dict[str, Any]) -> str:
5454
return f"{tool_name}({params})"
5555

5656

57+
def _abbreviate(text: str, max_words: int = 8) -> str:
58+
"""Abbreviate text to the first few words."""
59+
words = text.split()[:max_words]
60+
name = " ".join(words)
61+
if len(text.split()) > max_words:
62+
name += "..."
63+
return name
64+
65+
66+
def thinking_display_name(thinking: str) -> str:
67+
"""Generate display name for a thinking block child call."""
68+
return f"Thinking: {_abbreviate(thinking)}"
69+
70+
71+
def text_display_name(text: str) -> str:
72+
"""Generate display name for a text block child call."""
73+
return f"Text: {_abbreviate(text)}"
74+
75+
5776
def response_display_name(model: str | None) -> str:
5877
"""Generate display name for a response call."""
5978
if model:

0 commit comments

Comments
 (0)