|
6 | 6 |
|
7 | 7 | import pytest |
8 | 8 |
|
9 | | -from agents import Agent, Runner, SQLiteSession, TResponseInputItem |
| 9 | +from agents import Agent, RunConfig, Runner, SQLiteSession, TResponseInputItem |
10 | 10 | from agents.exceptions import UserError |
11 | 11 |
|
12 | 12 | from .fake_model import FakeModel |
@@ -394,7 +394,126 @@ async def test_session_memory_rejects_both_session_and_list_input(runner_method) |
394 | 394 | await run_agent_async(runner_method, agent, list_input, session=session) |
395 | 395 |
|
396 | 396 | # Verify the error message explains the issue |
397 | | - assert "Cannot provide both a session and a list of input items" in str(exc_info.value) |
| 397 | + assert "You must specify the `session_input_handling` in" in str(exc_info.value) |
398 | 398 | assert "manually manage conversation history" in str(exc_info.value) |
399 | 399 |
|
400 | 400 | session.close() |
| 401 | + |
| 402 | + |
| 403 | +@pytest.mark.parametrize("runner_method", ["run", "run_sync", "run_streamed"]) |
| 404 | +@pytest.mark.asyncio |
| 405 | +async def test_session_memory_append_list(runner_method): |
| 406 | + """Test if the user passes a list of items and want to append them.""" |
| 407 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 408 | + db_path = Path(temp_dir) / "test_memory.db" |
| 409 | + |
| 410 | + model = FakeModel() |
| 411 | + agent = Agent(name="test", model=model) |
| 412 | + |
| 413 | + # Session |
| 414 | + session_id = "session_1" |
| 415 | + session = SQLiteSession(session_id, db_path) |
| 416 | + |
| 417 | + model.set_next_output([get_text_message("I like cats")]) |
| 418 | + _ = await run_agent_async(runner_method, agent, "I like cats", session=session) |
| 419 | + |
| 420 | + append_input = [ |
| 421 | + {"role": "user", "content": "Some random user text"}, |
| 422 | + {"role": "assistant", "content": "You're right"}, |
| 423 | + {"role": "user", "content": "What did I say I like?"}, |
| 424 | + ] |
| 425 | + second_model_response = {"role": "assistant", "content": "Yes, you mentioned cats"} |
| 426 | + model.set_next_output([get_text_message(second_model_response.get("content", ""))]) |
| 427 | + |
| 428 | + _ = await run_agent_async( |
| 429 | + runner_method, |
| 430 | + agent, |
| 431 | + append_input, |
| 432 | + session=session, |
| 433 | + run_config=RunConfig(session_input_handling="append"), |
| 434 | + ) |
| 435 | + |
| 436 | + session_items = await session.get_items() |
| 437 | + |
| 438 | + # Check the items has been appended |
| 439 | + assert len(session_items) == 6 |
| 440 | + |
| 441 | + # Check the items are the last 4 elements |
| 442 | + append_input.append(second_model_response) |
| 443 | + for sess_item, orig_item in zip(session_items[-4:], append_input): |
| 444 | + assert sess_item.get("role") == orig_item.get("role") |
| 445 | + |
| 446 | + sess_content = sess_item.get("content") |
| 447 | + # Narrow to list or str for mypy |
| 448 | + assert isinstance(sess_content, (list, str)) |
| 449 | + |
| 450 | + if isinstance(sess_content, list): |
| 451 | + # now mypy knows `content: list[Any]` |
| 452 | + assert isinstance(sess_content[0], dict) and "text" in sess_content[0] |
| 453 | + val_sess = sess_content[0]["text"] |
| 454 | + else: |
| 455 | + # here content is str |
| 456 | + val_sess = sess_content |
| 457 | + |
| 458 | + assert val_sess == orig_item["content"] |
| 459 | + |
| 460 | + session.close() |
| 461 | + |
| 462 | + |
| 463 | +@pytest.mark.parametrize("runner_method", ["run", "run_sync", "run_streamed"]) |
| 464 | +@pytest.mark.asyncio |
| 465 | +async def test_session_memory_replace_list(runner_method): |
| 466 | + """Test if the user passes a list of items and want to replace the history.""" |
| 467 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 468 | + db_path = Path(temp_dir) / "test_memory.db" |
| 469 | + |
| 470 | + model = FakeModel() |
| 471 | + agent = Agent(name="test", model=model) |
| 472 | + |
| 473 | + # Session |
| 474 | + session_id = "session_1" |
| 475 | + session = SQLiteSession(session_id, db_path) |
| 476 | + |
| 477 | + model.set_next_output([get_text_message("I like cats")]) |
| 478 | + _ = await run_agent_async(runner_method, agent, "I like cats", session=session) |
| 479 | + |
| 480 | + replace_input = [ |
| 481 | + {"role": "user", "content": "Some random user text"}, |
| 482 | + {"role": "assistant", "content": "You're right"}, |
| 483 | + {"role": "user", "content": "What did I say I like?"}, |
| 484 | + ] |
| 485 | + second_model_response = {"role": "assistant", "content": "Yes, you mentioned cats"} |
| 486 | + model.set_next_output([get_text_message(second_model_response.get("content", ""))]) |
| 487 | + |
| 488 | + _ = await run_agent_async( |
| 489 | + runner_method, |
| 490 | + agent, |
| 491 | + replace_input, |
| 492 | + session=session, |
| 493 | + run_config=RunConfig(session_input_handling="replace"), |
| 494 | + ) |
| 495 | + |
| 496 | + session_items = await session.get_items() |
| 497 | + |
| 498 | + # Check the new items replaced the history |
| 499 | + assert len(session_items) == 4 |
| 500 | + |
| 501 | + # Check the items are the last 4 elements |
| 502 | + replace_input.append(second_model_response) |
| 503 | + for sess_item, orig_item in zip(session_items, replace_input): |
| 504 | + assert sess_item.get("role") == orig_item.get("role") |
| 505 | + sess_content = sess_item.get("content") |
| 506 | + # Narrow to list or str for mypy |
| 507 | + assert isinstance(sess_content, (list, str)) |
| 508 | + |
| 509 | + if isinstance(sess_content, list): |
| 510 | + # now mypy knows `content: list[Any]` |
| 511 | + assert isinstance(sess_content[0], dict) and "text" in sess_content[0] |
| 512 | + val_sess = sess_content[0]["text"] |
| 513 | + else: |
| 514 | + # here content is str |
| 515 | + val_sess = sess_content |
| 516 | + |
| 517 | + assert val_sess == orig_item["content"] |
| 518 | + |
| 519 | + session.close() |
0 commit comments