|
30 | 30 | Session, |
31 | 31 | SessionSettings, |
32 | 32 | ToolApprovalItem, |
| 33 | + ToolCallOutputItem, |
33 | 34 | TResponseInputItem, |
34 | 35 | Usage, |
35 | 36 | tool_namespace, |
@@ -407,6 +408,183 @@ async def extractor(result) -> str: |
407 | 408 | assert output == "custom output" |
408 | 409 |
|
409 | 410 |
|
| 411 | +@pytest.mark.asyncio |
| 412 | +async def test_agent_as_tool_fallback_uses_current_run_items_only( |
| 413 | + monkeypatch: pytest.MonkeyPatch, |
| 414 | +) -> None: |
| 415 | + agent = Agent(name="summarizer") |
| 416 | + |
| 417 | + message = ResponseOutputMessage( |
| 418 | + id="msg_current", |
| 419 | + role="assistant", |
| 420 | + status="completed", |
| 421 | + type="message", |
| 422 | + content=[ |
| 423 | + ResponseOutputText( |
| 424 | + annotations=[], |
| 425 | + text="Current run summary", |
| 426 | + type="output_text", |
| 427 | + logprobs=[], |
| 428 | + ) |
| 429 | + ], |
| 430 | + ) |
| 431 | + |
| 432 | + class DummyResult: |
| 433 | + def __init__(self) -> None: |
| 434 | + self.final_output = "" |
| 435 | + self.new_items = [ |
| 436 | + ToolCallOutputItem( |
| 437 | + agent=agent, |
| 438 | + raw_item={ |
| 439 | + "call_id": "call_current", |
| 440 | + "output": "Current tool output", |
| 441 | + "type": "function_call_output", |
| 442 | + }, |
| 443 | + output="Current tool output", |
| 444 | + ), |
| 445 | + MessageOutputItem(agent=agent, raw_item=message), |
| 446 | + ] |
| 447 | + |
| 448 | + def to_input_list(self) -> list[dict[str, Any]]: |
| 449 | + return [ |
| 450 | + { |
| 451 | + "call_id": "call_old", |
| 452 | + "output": "Old output from prior history", |
| 453 | + "type": "function_call_output", |
| 454 | + } |
| 455 | + ] |
| 456 | + |
| 457 | + run_result = DummyResult() |
| 458 | + |
| 459 | + async def fake_run( |
| 460 | + cls, |
| 461 | + starting_agent, |
| 462 | + input, |
| 463 | + *, |
| 464 | + context, |
| 465 | + max_turns, |
| 466 | + hooks, |
| 467 | + run_config, |
| 468 | + previous_response_id, |
| 469 | + conversation_id, |
| 470 | + session, |
| 471 | + ): |
| 472 | + del ( |
| 473 | + cls, |
| 474 | + starting_agent, |
| 475 | + input, |
| 476 | + context, |
| 477 | + max_turns, |
| 478 | + hooks, |
| 479 | + run_config, |
| 480 | + previous_response_id, |
| 481 | + conversation_id, |
| 482 | + session, |
| 483 | + ) |
| 484 | + return run_result |
| 485 | + |
| 486 | + monkeypatch.setattr(Runner, "run", classmethod(fake_run)) |
| 487 | + |
| 488 | + tool = agent.as_tool( |
| 489 | + tool_name="summary_tool", |
| 490 | + tool_description="Summarize current run output", |
| 491 | + ) |
| 492 | + tool_context = ToolContext( |
| 493 | + context=None, |
| 494 | + tool_name="summary_tool", |
| 495 | + tool_call_id="call_1", |
| 496 | + tool_arguments='{"input": "hello"}', |
| 497 | + ) |
| 498 | + |
| 499 | + output = await tool.on_invoke_tool(tool_context, '{"input": "hello"}') |
| 500 | + |
| 501 | + assert output == "Current run summary" |
| 502 | + |
| 503 | + |
| 504 | +@pytest.mark.asyncio |
| 505 | +async def test_agent_as_tool_fallback_returns_most_recent_current_run_output( |
| 506 | + monkeypatch: pytest.MonkeyPatch, |
| 507 | +) -> None: |
| 508 | + agent = Agent(name="summarizer") |
| 509 | + |
| 510 | + older_message = ResponseOutputMessage( |
| 511 | + id="msg_older", |
| 512 | + role="assistant", |
| 513 | + status="completed", |
| 514 | + type="message", |
| 515 | + content=[ |
| 516 | + ResponseOutputText( |
| 517 | + annotations=[], |
| 518 | + text="Older message output", |
| 519 | + type="output_text", |
| 520 | + logprobs=[], |
| 521 | + ) |
| 522 | + ], |
| 523 | + ) |
| 524 | + |
| 525 | + class DummyResult: |
| 526 | + def __init__(self) -> None: |
| 527 | + self.final_output = "" |
| 528 | + self.new_items = [ |
| 529 | + MessageOutputItem(agent=agent, raw_item=older_message), |
| 530 | + ToolCallOutputItem( |
| 531 | + agent=agent, |
| 532 | + raw_item={ |
| 533 | + "call_id": "call_current", |
| 534 | + "output": "Newest tool output", |
| 535 | + "type": "function_call_output", |
| 536 | + }, |
| 537 | + output="Newest tool output", |
| 538 | + ), |
| 539 | + ] |
| 540 | + |
| 541 | + run_result = DummyResult() |
| 542 | + |
| 543 | + async def fake_run( |
| 544 | + cls, |
| 545 | + starting_agent, |
| 546 | + input, |
| 547 | + *, |
| 548 | + context, |
| 549 | + max_turns, |
| 550 | + hooks, |
| 551 | + run_config, |
| 552 | + previous_response_id, |
| 553 | + conversation_id, |
| 554 | + session, |
| 555 | + ): |
| 556 | + del ( |
| 557 | + cls, |
| 558 | + starting_agent, |
| 559 | + input, |
| 560 | + context, |
| 561 | + max_turns, |
| 562 | + hooks, |
| 563 | + run_config, |
| 564 | + previous_response_id, |
| 565 | + conversation_id, |
| 566 | + session, |
| 567 | + ) |
| 568 | + return run_result |
| 569 | + |
| 570 | + monkeypatch.setattr(Runner, "run", classmethod(fake_run)) |
| 571 | + |
| 572 | + tool = agent.as_tool( |
| 573 | + tool_name="summary_tool", |
| 574 | + tool_description="Summarize current run output", |
| 575 | + ) |
| 576 | + tool_context = ToolContext( |
| 577 | + context=None, |
| 578 | + tool_name="summary_tool", |
| 579 | + tool_call_id="call_1", |
| 580 | + tool_arguments='{"input": "hello"}', |
| 581 | + ) |
| 582 | + |
| 583 | + output = await tool.on_invoke_tool(tool_context, '{"input": "hello"}') |
| 584 | + |
| 585 | + assert output == "Newest tool output" |
| 586 | + |
| 587 | + |
410 | 588 | @pytest.mark.asyncio |
411 | 589 | async def test_agent_as_tool_extractor_can_access_agent_tool_invocation( |
412 | 590 | monkeypatch: pytest.MonkeyPatch, |
|
0 commit comments