|
4 | 4 | import pytest |
5 | 5 | from google.genai import types |
6 | 6 |
|
| 7 | +import astrbot.core.message.components as Comp |
| 8 | +import astrbot.core.provider.sources.gemini_source as gemini_source |
7 | 9 | import astrbot.core.provider.sources.request_retry as request_retry |
8 | 10 | from astrbot.core.exceptions import EmptyModelOutputError |
9 | 11 | from astrbot.core.provider.entities import LLMResponse |
@@ -236,3 +238,135 @@ async def list(self): |
236 | 238 |
|
237 | 239 | assert await provider.get_models() == ["gemini-a"] |
238 | 240 | assert models.calls == 2 |
| 241 | + |
| 242 | + |
| 243 | +def _gemini_part(*, text=None, thought=None, function_call=None): |
| 244 | + return SimpleNamespace( |
| 245 | + text=text, |
| 246 | + thought=thought, |
| 247 | + function_call=function_call, |
| 248 | + inline_data=None, |
| 249 | + thought_signature=None, |
| 250 | + ) |
| 251 | + |
| 252 | + |
| 253 | +def _gemini_stream_chunk( |
| 254 | + *, |
| 255 | + text=None, |
| 256 | + thought=None, |
| 257 | + function_call=None, |
| 258 | + finish_reason=None, |
| 259 | + response_id="resp-1", |
| 260 | +): |
| 261 | + """Build a minimal stand-in for a google-genai streaming chunk.""" |
| 262 | + parts = [] |
| 263 | + if text is not None: |
| 264 | + parts.append(_gemini_part(text=text)) |
| 265 | + if thought is not None: |
| 266 | + parts.append(_gemini_part(text=thought, thought=True)) |
| 267 | + if function_call is not None: |
| 268 | + parts.append(_gemini_part(function_call=function_call)) |
| 269 | + return SimpleNamespace( |
| 270 | + candidates=[ |
| 271 | + SimpleNamespace( |
| 272 | + content=SimpleNamespace(parts=parts), |
| 273 | + finish_reason=finish_reason, |
| 274 | + ) |
| 275 | + ], |
| 276 | + text=text, |
| 277 | + response_id=response_id, |
| 278 | + usage_metadata=None, |
| 279 | + ) |
| 280 | + |
| 281 | + |
| 282 | +def _gemini_stream_provider(): |
| 283 | + provider = ProviderGoogleGenAI.__new__(ProviderGoogleGenAI) |
| 284 | + provider.provider_config = {} |
| 285 | + provider.provider_settings = {} |
| 286 | + provider.model_name = "gemini-3.7-flash" |
| 287 | + provider.safety_settings = [] |
| 288 | + provider.client = SimpleNamespace( |
| 289 | + models=SimpleNamespace(generate_content_stream=lambda **kwargs: None) |
| 290 | + ) |
| 291 | + return provider |
| 292 | + |
| 293 | + |
| 294 | +async def _drain_gemini_stream(provider, monkeypatch, chunks): |
| 295 | + async def fake_stream(): |
| 296 | + for chunk in chunks: |
| 297 | + yield chunk |
| 298 | + |
| 299 | + async def fake_retry(provider_name, request_factory, max_attempts=None): |
| 300 | + return fake_stream() |
| 301 | + |
| 302 | + monkeypatch.setattr(gemini_source, "retry_provider_request", fake_retry) |
| 303 | + return [ |
| 304 | + response |
| 305 | + async for response in provider._query_stream( |
| 306 | + payloads={ |
| 307 | + "messages": [{"role": "user", "content": "what's the weather?"}], |
| 308 | + "model": "gemini-3.7-flash", |
| 309 | + }, |
| 310 | + tools=None, |
| 311 | + ) |
| 312 | + ] |
| 313 | + |
| 314 | + |
| 315 | +@pytest.mark.asyncio |
| 316 | +async def test_gemini_stream_keeps_narration_emitted_before_tool_call(monkeypatch): |
| 317 | + """Narration streamed before a tool call must reach the final response. |
| 318 | +
|
| 319 | + Regression test for the tool-call branch that used to build the final |
| 320 | + LLMResponse from the tool-call chunk alone and return immediately, leaving |
| 321 | + text the user had already seen out of the conversation history. |
| 322 | + """ |
| 323 | + provider = _gemini_stream_provider() |
| 324 | + tool_call = SimpleNamespace( |
| 325 | + name="get_weather", |
| 326 | + args={"city": "Shenyang"}, |
| 327 | + id="call-1", |
| 328 | + thought_signature=None, |
| 329 | + ) |
| 330 | + |
| 331 | + responses = await _drain_gemini_stream( |
| 332 | + provider, |
| 333 | + monkeypatch, |
| 334 | + [ |
| 335 | + _gemini_stream_chunk(text="Sure, let me check that for you."), |
| 336 | + _gemini_stream_chunk(function_call=tool_call), |
| 337 | + ], |
| 338 | + ) |
| 339 | + |
| 340 | + final = responses[-1] |
| 341 | + assert final.is_chunk is False |
| 342 | + assert final.tools_call_name == ["get_weather"] |
| 343 | + plain_text = "".join( |
| 344 | + part.text |
| 345 | + for part in (final.result_chain.chain or []) |
| 346 | + if isinstance(part, Comp.Plain) |
| 347 | + ) |
| 348 | + assert "Sure, let me check that for you." in plain_text |
| 349 | + |
| 350 | + |
| 351 | +@pytest.mark.asyncio |
| 352 | +async def test_gemini_stream_keeps_reasoning_from_tool_call_chunk(monkeypatch): |
| 353 | + """Reasoning on the tool-call chunk itself must not be overwritten.""" |
| 354 | + provider = _gemini_stream_provider() |
| 355 | + tool_call = SimpleNamespace( |
| 356 | + name="get_weather", |
| 357 | + args={"city": "Shenyang"}, |
| 358 | + id="call-1", |
| 359 | + thought_signature=None, |
| 360 | + ) |
| 361 | + |
| 362 | + responses = await _drain_gemini_stream( |
| 363 | + provider, |
| 364 | + monkeypatch, |
| 365 | + [ |
| 366 | + _gemini_stream_chunk(thought="weighing options"), |
| 367 | + _gemini_stream_chunk(function_call=tool_call, thought="deciding to call"), |
| 368 | + ], |
| 369 | + ) |
| 370 | + |
| 371 | + final = responses[-1] |
| 372 | + assert final.reasoning_content == "weighing optionsdeciding to call" |
0 commit comments