From 45488283548606caff6457f2ef7b6dfba7882fc2 Mon Sep 17 00:00:00 2001 From: worldmozara Date: Tue, 14 Jul 2026 22:40:15 +0800 Subject: [PATCH] core/lmproto+agent: use normal stream API for chat loop Accept async-def returning async iterator; stream via chat_completions(..., stream=True); typed StreamToolCallDelta merge; update scripted test clients to emit real chunks. --- tests/test_agent/test_stream_merge.py | 42 +++++++++++++++++++++------ 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/tests/test_agent/test_stream_merge.py b/tests/test_agent/test_stream_merge.py index f902324..09bc232 100644 --- a/tests/test_agent/test_stream_merge.py +++ b/tests/test_agent/test_stream_merge.py @@ -1,15 +1,29 @@ from __future__ import annotations +from msgspec import UNSET + from plyngent.lmproto.openai_compatible.client import merge_stream_tool_calls +from plyngent.lmproto.openai_compatible.model import StreamFunctionDelta, StreamToolCallDelta def test_merge_stream_tool_calls_accumulates_arguments() -> None: - lines = [ - b'{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"c1","type":"function","function":{"name":"add","arguments":""}}]}}]}', - b'{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"{\\"a\\":"}}]}}]}', - b'{"choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"function":{"arguments":"1}"}}]},"finish_reason":"tool_calls"}]}', + 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}"), + ), ] - calls = merge_stream_tool_calls(lines) + calls = merge_stream_tool_calls(deltas) assert len(calls) == 1 assert calls[0].id == "c1" assert calls[0].function.name == "add" @@ -17,9 +31,19 @@ def test_merge_stream_tool_calls_accumulates_arguments() -> None: def test_merge_stream_tool_calls_multiple_indices() -> None: - lines = [ - b'{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"a","type":"function","function":{"name":"f1","arguments":"{}"}}]}}]}', - b'{"choices":[{"delta":{"tool_calls":[{"index":1,"id":"b","type":"function","function":{"name":"f2","arguments":"[]"}}]}}]}', + 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="[]"), + ), ] - calls = merge_stream_tool_calls(lines) + calls = merge_stream_tool_calls(deltas) assert {c.function.name for c in calls} == {"f1", "f2"}