fix(agent-runner): stabilize event context and streams

This commit is contained in:
huanghuoguoguo
2026-05-29 21:05:20 +08:00
committed by huanghuoguoguo
parent 9bdebcdc5a
commit a2b38f5bf2
13 changed files with 232 additions and 56 deletions

View File

@@ -7,6 +7,7 @@ only the necessary dependencies.
from __future__ import annotations
import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
import openai # Import real openai package
@@ -140,6 +141,36 @@ class TestInvokeLLMErrorHandling:
assert '频繁' in str(exc.value) or '余额' in str(exc.value)
@pytest.mark.asyncio
async def test_stream_skips_choice_with_none_delta(self, requester_with_mocked_client, mock_model):
"""OpenAI-compatible streams may include choice frames with delta=None."""
async def fake_req_stream(args, extra_body=None):
yield SimpleNamespace(choices=[SimpleNamespace(delta=None, finish_reason=None)])
yield SimpleNamespace(
choices=[
SimpleNamespace(
delta=SimpleNamespace(model_dump=lambda: {'role': 'assistant', 'content': 'OK'}),
finish_reason='stop',
)
]
)
requester_with_mocked_client._req_stream = fake_req_stream
chunks = [
chunk
async for chunk in requester_with_mocked_client._closure_stream(
query=None,
req_messages=[{'role': 'user', 'content': 'hello'}],
use_model=mock_model,
)
]
assert len(chunks) == 1
assert chunks[0].content == 'OK'
assert chunks[0].is_final is True
class TestInvokeEmbeddingErrorHandling:
"""Tests for invoke_embedding error handling."""