|
38 | 38 | ResponseContentPartAddedEvent, |
39 | 39 | ) |
40 | 40 | from openai.types.responses.response_file_search_tool_call import Result |
| 41 | +from openai.types.responses.response_input_item_param import Message |
41 | 42 | from openai.types.responses.response_output_text import ( |
42 | 43 | AnnotationContainerFileCitation as ResponsesAnnotationContainerFileCitation, |
43 | 44 | ) |
|
77 | 78 | CustomTask, |
78 | 79 | DurationSummary, |
79 | 80 | FileSource, |
| 81 | + HiddenContextItem, |
80 | 82 | InferenceOptions, |
81 | 83 | Page, |
82 | 84 | TaskItem, |
@@ -542,6 +544,72 @@ async def test_input_item_converter_user_input_with_tags_throws_by_default(): |
542 | 544 | await simple_to_agent_input(items) |
543 | 545 |
|
544 | 546 |
|
| 547 | +async def test_input_item_converter_for_hidden_context_with_string_content(): |
| 548 | + items = [ |
| 549 | + HiddenContextItem( |
| 550 | + id="123", |
| 551 | + content="User pressed the red button", |
| 552 | + thread_id=thread.id, |
| 553 | + created_at=datetime.now(), |
| 554 | + ) |
| 555 | + ] |
| 556 | + |
| 557 | + # The default converter works for string content |
| 558 | + items = await simple_to_agent_input(items) |
| 559 | + assert len(items) == 1 |
| 560 | + assert items[0] == { |
| 561 | + "content": [ |
| 562 | + { |
| 563 | + "text": "Hidden context for the agent (not shown to the user):\n<HiddenContext>\nUser pressed the red button\n</HiddenContext>", |
| 564 | + "type": "input_text", |
| 565 | + }, |
| 566 | + ], |
| 567 | + "role": "user", |
| 568 | + "type": "message", |
| 569 | + } |
| 570 | + |
| 571 | + |
| 572 | +async def test_input_item_converter_for_hidden_context_with_non_string_content(): |
| 573 | + items = [ |
| 574 | + HiddenContextItem( |
| 575 | + id="123", |
| 576 | + content={"harry": "potter", "hermione": "granger"}, |
| 577 | + thread_id=thread.id, |
| 578 | + created_at=datetime.now(), |
| 579 | + ) |
| 580 | + ] |
| 581 | + |
| 582 | + # Default converter should throw |
| 583 | + with pytest.raises(NotImplementedError): |
| 584 | + await simple_to_agent_input(items) |
| 585 | + |
| 586 | + class MyThreadItemConverter(ThreadItemConverter): |
| 587 | + async def hidden_context_to_input(self, item: HiddenContextItem): |
| 588 | + content = ",".join(item.content.keys()) |
| 589 | + return Message( |
| 590 | + type="message", |
| 591 | + content=[ |
| 592 | + ResponseInputTextParam( |
| 593 | + type="input_text", text=f"<HIDDEN>{content}</HIDDEN>" |
| 594 | + ) |
| 595 | + ], |
| 596 | + role="user", |
| 597 | + ) |
| 598 | + |
| 599 | + items = await MyThreadItemConverter().to_agent_input(items) |
| 600 | + assert len(items) == 1 |
| 601 | + assert items[0] == { |
| 602 | + "content": [ |
| 603 | + { |
| 604 | + "text": "<HIDDEN>harry,hermione</HIDDEN>", |
| 605 | + "type": "input_text", |
| 606 | + }, |
| 607 | + ], |
| 608 | + "role": "user", |
| 609 | + "type": "message", |
| 610 | + } |
| 611 | + |
| 612 | + |
545 | 613 | async def test_input_item_converter_with_client_tool_call(): |
546 | 614 | items = [ |
547 | 615 | UserMessageItem( |
|
0 commit comments