refactor(translation): extract _tool_event_step from the stream fold

The merged step() carries both branches' new event arms (wire_chunk +
chunk), tipping C901 to 11. Pull the two tool arms into one helper via an
or-pattern case; the helper narrows on the two-literal tag so no
placeholder arm is needed.
This commit is contained in:
mateo-berri
2026-06-12 06:00:47 +00:00
parent fc79c698d4
commit 773bdff8a8
@@ -73,43 +73,8 @@ def step(state: StreamState, event: StreamEvent) -> _StepResult:
return replace(state, model=state.model or event.start.model), ()
case "text_delta":
return _emit(state, {"content": event.text_delta.text})
case "tool_use_start":
started = replace(state, tool_index=state.tool_index + 1)
return _emit(
started,
{
"content": "",
"tool_calls": [
{
"id": event.tool_use_start.id,
"type": "function",
"function": {
"name": event.tool_use_start.name,
"arguments": "",
},
"index": started.tool_index,
}
],
},
)
case "tool_args_delta":
return _emit(
state,
{
"content": "",
"tool_calls": [
{
"id": None,
"type": "function",
"function": {
"name": None,
"arguments": event.tool_args_delta.partial_json,
},
"index": max(state.tool_index, 0),
}
],
},
)
case "tool_use_start" | "tool_args_delta":
return _tool_event_step(state, event)
case "thinking_delta":
return _emit(
state,
@@ -135,6 +100,45 @@ def step(state: StreamState, event: StreamEvent) -> _StepResult:
assert_never(event.tag)
def _tool_event_step(state: StreamState, event: StreamEvent) -> _StepResult:
if event.tag == "tool_use_start":
started = replace(state, tool_index=state.tool_index + 1)
return _emit(
started,
{
"content": "",
"tool_calls": [
{
"id": event.tool_use_start.id,
"type": "function",
"function": {
"name": event.tool_use_start.name,
"arguments": "",
},
"index": started.tool_index,
}
],
},
)
return _emit(
state,
{
"content": "",
"tool_calls": [
{
"id": None,
"type": "function",
"function": {
"name": None,
"arguments": event.tool_args_delta.partial_json,
},
"index": max(state.tool_index, 0),
}
],
},
)
def _finish_chunk(state: StreamState, finish: str) -> Body:
return {
"model": state.model,