Skip to content

Commit 9f1b1c0

Browse files
pontemontiJohan Broberg
andauthored
Implement API to send chat history for OpenAI orchestrator (#127)
* Add PRD for Open AI chat history API * Rename `send_chat_history_async` to `send_chat_history_messages_async` and update PRD to reflect new method naming conventions and functionality. * Add tasks for OpenAI `send_chat_history_messages_async` API implementation * Implement API for OpenAI chat history * Add comments to clarify error handling in namespace package finder --------- Co-authored-by: Johan Broberg <johanb@microsoft.com>
1 parent e8b27e2 commit 9f1b1c0

12 files changed

Lines changed: 2196 additions & 20 deletions

File tree

CLAUDE.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,45 @@ Place it before imports with one blank line after.
168168

169169
### Python Conventions
170170

171-
- Type hints preferred (Pydantic models heavily used)
171+
- Type hints required on all function parameters and return types
172172
- Async/await patterns for I/O operations
173173
- Use explicit `None` checks: `if x is not None:` not `if x:`
174174
- Local imports should be moved to top of file
175175
- Return defensive copies of mutable data to protect singletons
176176

177+
### Type Hints - NEVER Use `Any`
178+
179+
**CRITICAL: Never use `typing.Any` in this codebase.** Using `Any` defeats the purpose of type checking and can hide bugs. Instead:
180+
181+
1. **Use actual types from external SDKs** - When integrating with external libraries (OpenAI, LangChain, etc.), import and use their actual types:
182+
```python
183+
from agents.memory import Session
184+
from agents.items import TResponseInputItem
185+
186+
async def send_chat_history(self, session: Session) -> OperationResult:
187+
...
188+
```
189+
190+
2. **Use `Union` for known possible types**:
191+
```python
192+
from typing import Union
193+
MessageType = Union[UserMessage, AssistantMessage, SystemMessage, Dict[str, object]]
194+
```
195+
196+
3. **Use `object` for truly unknown types** that you only pass through:
197+
```python
198+
def log_item(item: object) -> None: ...
199+
```
200+
201+
4. **Use `Protocol` only as a last resort** - If external types cannot be found or imported, define a Protocol. However, **confirm with the developer first** before proceeding with this approach, as it may indicate a missing dependency or incorrect understanding of the external API.
202+
203+
**Why this matters:**
204+
- `Any` disables all type checking for that variable
205+
- Bugs that type checkers would catch go unnoticed
206+
- Code readability suffers - developers don't know what types to expect
207+
- Using actual SDK types provides better IDE support and ensures compatibility
208+
- This applies to both production code AND test files
209+
177210
## CI/CD
178211

179212
The `.github/workflows/ci.yml` pipeline:

docs/design.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,51 @@ Framework-specific adapters for MCP tool integration:
227227
|---------|---------|------------|
228228
| `extensions-agentframework` | Adapt MCP tools to Microsoft Agents SDK | [design.md](../libraries/microsoft-agents-a365-tooling-extensions-agentframework/docs/design.md) |
229229
| `extensions-azureaifoundry` | Azure AI Foundry tool integration | [design.md](../libraries/microsoft-agents-a365-tooling-extensions-azureaifoundry/docs/design.md) |
230-
| `extensions-openai` | OpenAI function calling integration | [design.md](../libraries/microsoft-agents-a365-tooling-extensions-openai/docs/design.md) |
230+
| `extensions-openai` | OpenAI function calling integration and chat history | [design.md](../libraries/microsoft-agents-a365-tooling-extensions-openai/docs/design.md) |
231231
| `extensions-semantickernel` | Semantic Kernel plugin integration | [design.md](../libraries/microsoft-agents-a365-tooling-extensions-semantickernel/docs/design.md) |
232232

233+
#### OpenAI Extension: Chat History API
234+
235+
The OpenAI tooling extension provides methods to send chat history to the MCP platform for real-time threat protection:
236+
237+
**Key Classes:**
238+
239+
| Class | Purpose |
240+
|-------|---------|
241+
| `McpToolRegistrationService` | MCP tool registration and chat history management |
242+
243+
**Methods:**
244+
245+
| Method | Purpose |
246+
|--------|---------|
247+
| `send_chat_history(turn_context, session, limit, options)` | Extract messages from OpenAI Session and send to MCP platform |
248+
| `send_chat_history_messages(turn_context, messages, options)` | Send a list of OpenAI TResponseInputItem messages to MCP platform |
249+
250+
**Usage Example:**
251+
252+
```python
253+
from agents import Agent, Runner
254+
from microsoft_agents_a365.tooling.extensions.openai import McpToolRegistrationService
255+
256+
service = McpToolRegistrationService()
257+
agent = Agent(name="my-agent", model="gpt-4")
258+
259+
# In your agent handler:
260+
async with Runner.run(agent, messages) as result:
261+
session = result.session
262+
263+
# Option 1: Send from Session object
264+
op_result = await service.send_chat_history(turn_context, session)
265+
266+
# Option 2: Send from message list
267+
op_result = await service.send_chat_history_messages(turn_context, messages)
268+
269+
if op_result.succeeded:
270+
print("Chat history sent successfully")
271+
```
272+
273+
The methods convert OpenAI message types to `ChatHistoryMessage` format and delegate to the core `McpToolServerConfigurationService.send_chat_history()` method.
274+
233275
### 6. Notifications (`microsoft-agents-a365-notifications`)
234276

235277
> **Detailed documentation**: [libraries/microsoft-agents-a365-notifications/docs/design.md](../libraries/microsoft-agents-a365-notifications/docs/design.md)

libraries/microsoft-agents-a365-tooling-extensions-openai/microsoft_agents_a365/tooling/extensions/openai/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22
# Licensed under the MIT License.
33

44
"""
5-
OpenAI extensions for Microsoft Agent 365 Tooling SDK
5+
OpenAI extensions for Microsoft Agent 365 Tooling SDK.
66
77
Tooling and utilities specifically for OpenAI framework integration.
8-
Provides OpenAI-specific helper utilities.
8+
Provides OpenAI-specific helper utilities including:
9+
- McpToolRegistrationService: Service for MCP tool registration and chat history management
10+
11+
For type hints, use the types directly from the OpenAI Agents SDK:
12+
- agents.memory.Session: Protocol for session objects
13+
- agents.items.TResponseInputItem: Type for input message items
914
"""
1015

16+
from .mcp_tool_registration_service import McpToolRegistrationService
17+
1118
__version__ = "1.0.0"
19+
20+
__all__ = [
21+
"McpToolRegistrationService",
22+
]

0 commit comments

Comments
 (0)