2026-07-14 22:22:22 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-07-14 22:40:15 +08:00
|
|
|
from msgspec import UNSET
|
|
|
|
|
|
2026-07-14 22:22:22 +08:00
|
|
|
from plyngent.lmproto.openai_compatible.client import merge_stream_tool_calls
|
2026-07-14 22:40:15 +08:00
|
|
|
from plyngent.lmproto.openai_compatible.model import StreamFunctionDelta, StreamToolCallDelta
|
2026-07-14 22:22:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_merge_stream_tool_calls_accumulates_arguments() -> None:
|
2026-07-14 22:40:15 +08:00
|
|
|
deltas = [
|
|
|
|
|
StreamToolCallDelta(
|
|
|
|
|
index=0,
|
|
|
|
|
id="c1",
|
|
|
|
|
type="function",
|
|
|
|
|
function=StreamFunctionDelta(name="add", arguments=""),
|
|
|
|
|
),
|
|
|
|
|
StreamToolCallDelta(
|
|
|
|
|
index=0,
|
|
|
|
|
function=StreamFunctionDelta(name=UNSET, arguments='{"a":'),
|
|
|
|
|
),
|
|
|
|
|
StreamToolCallDelta(
|
|
|
|
|
index=0,
|
|
|
|
|
function=StreamFunctionDelta(name=UNSET, arguments="1}"),
|
|
|
|
|
),
|
2026-07-14 22:22:22 +08:00
|
|
|
]
|
2026-07-14 22:40:15 +08:00
|
|
|
calls = merge_stream_tool_calls(deltas)
|
2026-07-14 22:22:22 +08:00
|
|
|
assert len(calls) == 1
|
|
|
|
|
assert calls[0].id == "c1"
|
|
|
|
|
assert calls[0].function.name == "add"
|
|
|
|
|
assert calls[0].function.arguments == '{"a":1}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_merge_stream_tool_calls_multiple_indices() -> None:
|
2026-07-14 22:40:15 +08:00
|
|
|
deltas = [
|
|
|
|
|
StreamToolCallDelta(
|
|
|
|
|
index=0,
|
|
|
|
|
id="a",
|
|
|
|
|
type="function",
|
|
|
|
|
function=StreamFunctionDelta(name="f1", arguments="{}"),
|
|
|
|
|
),
|
|
|
|
|
StreamToolCallDelta(
|
|
|
|
|
index=1,
|
|
|
|
|
id="b",
|
|
|
|
|
type="function",
|
|
|
|
|
function=StreamFunctionDelta(name="f2", arguments="[]"),
|
|
|
|
|
),
|
2026-07-14 22:22:22 +08:00
|
|
|
]
|
2026-07-14 22:40:15 +08:00
|
|
|
calls = merge_stream_tool_calls(deltas)
|
2026-07-14 22:22:22 +08:00
|
|
|
assert {c.function.name for c in calls} == {"f1", "f2"}
|