|
11 | 11 | ) |
12 | 12 |
|
13 | 13 | from ... import ExternalToolset, ToolDefinition |
14 | | -from ...messages import ( |
15 | | - BuiltinToolCallPart, |
16 | | - BuiltinToolReturnPart, |
17 | | - ModelMessage, |
18 | | - SystemPromptPart, |
19 | | - TextPart, |
20 | | - ToolCallPart, |
21 | | - ToolReturnPart, |
22 | | - UserPromptPart, |
23 | | -) |
| 14 | +from ...messages import ModelMessage |
24 | 15 | from ...output import OutputDataT |
25 | 16 | from ...tools import AgentDepsT |
26 | 17 | from ...toolsets import AbstractToolset |
27 | 18 |
|
28 | 19 | try: |
29 | 20 | from ag_ui.core import ( |
30 | | - AssistantMessage, |
31 | 21 | BaseEvent, |
32 | | - DeveloperMessage, |
33 | 22 | Message, |
34 | 23 | RunAgentInput, |
35 | | - SystemMessage, |
36 | 24 | Tool as AGUITool, |
37 | | - ToolMessage, |
38 | | - UserMessage, |
39 | 25 | ) |
40 | 26 |
|
41 | | - from .. import MessagesBuilder, UIAdapter, UIEventStream |
42 | | - from ._event_stream import BUILTIN_TOOL_CALL_ID_PREFIX, AGUIEventStream |
| 27 | + from .. import UIAdapter, UIEventStream |
| 28 | + from ._event_stream import AGUIEventStream |
| 29 | + from ._messages import messages_from_ag_ui |
43 | 30 | except ImportError as e: # pragma: no cover |
44 | 31 | raise ImportError( |
45 | 32 | 'Please install the `ag-ui-protocol` package to use AG-UI integration, ' |
@@ -119,77 +106,9 @@ def state(self) -> dict[str, Any] | None: |
119 | 106 |
|
120 | 107 | @classmethod |
121 | 108 | def load_messages(cls, messages: Sequence[Message]) -> list[ModelMessage]: |
122 | | - """Transform AG-UI messages into Pydantic AI messages.""" |
123 | | - builder = MessagesBuilder() |
124 | | - tool_calls: dict[str, str] = {} # Tool call ID to tool name mapping. |
125 | | - |
126 | | - for msg in messages: |
127 | | - if isinstance(msg, UserMessage | SystemMessage | DeveloperMessage) or ( |
128 | | - isinstance(msg, ToolMessage) and not msg.tool_call_id.startswith(BUILTIN_TOOL_CALL_ID_PREFIX) |
129 | | - ): |
130 | | - if isinstance(msg, UserMessage): |
131 | | - builder.add(UserPromptPart(content=msg.content)) |
132 | | - elif isinstance(msg, SystemMessage | DeveloperMessage): |
133 | | - builder.add(SystemPromptPart(content=msg.content)) |
134 | | - else: |
135 | | - tool_call_id = msg.tool_call_id |
136 | | - tool_name = tool_calls.get(tool_call_id) |
137 | | - if tool_name is None: # pragma: no cover |
138 | | - raise ValueError(f'Tool call with ID {tool_call_id} not found in the history.') |
139 | | - |
140 | | - builder.add( |
141 | | - ToolReturnPart( |
142 | | - tool_name=tool_name, |
143 | | - content=msg.content, |
144 | | - tool_call_id=tool_call_id, |
145 | | - ) |
146 | | - ) |
147 | | - |
148 | | - elif isinstance(msg, AssistantMessage) or ( # pragma: no branch |
149 | | - isinstance(msg, ToolMessage) and msg.tool_call_id.startswith(BUILTIN_TOOL_CALL_ID_PREFIX) |
150 | | - ): |
151 | | - if isinstance(msg, AssistantMessage): |
152 | | - if msg.content: |
153 | | - builder.add(TextPart(content=msg.content)) |
154 | | - |
155 | | - if msg.tool_calls: |
156 | | - for tool_call in msg.tool_calls: |
157 | | - tool_call_id = tool_call.id |
158 | | - tool_name = tool_call.function.name |
159 | | - tool_calls[tool_call_id] = tool_name |
160 | | - |
161 | | - if tool_call_id.startswith(BUILTIN_TOOL_CALL_ID_PREFIX): |
162 | | - _, provider_name, tool_call_id = tool_call_id.split('|', 2) |
163 | | - builder.add( |
164 | | - BuiltinToolCallPart( |
165 | | - tool_name=tool_name, |
166 | | - args=tool_call.function.arguments, |
167 | | - tool_call_id=tool_call_id, |
168 | | - provider_name=provider_name, |
169 | | - ) |
170 | | - ) |
171 | | - else: |
172 | | - builder.add( |
173 | | - ToolCallPart( |
174 | | - tool_name=tool_name, |
175 | | - tool_call_id=tool_call_id, |
176 | | - args=tool_call.function.arguments, |
177 | | - ) |
178 | | - ) |
179 | | - else: |
180 | | - tool_call_id = msg.tool_call_id |
181 | | - tool_name = tool_calls.get(tool_call_id) |
182 | | - if tool_name is None: # pragma: no cover |
183 | | - raise ValueError(f'Tool call with ID {tool_call_id} not found in the history.') |
184 | | - _, provider_name, tool_call_id = tool_call_id.split('|', 2) |
185 | | - |
186 | | - builder.add( |
187 | | - BuiltinToolReturnPart( |
188 | | - tool_name=tool_name, |
189 | | - content=msg.content, |
190 | | - tool_call_id=tool_call_id, |
191 | | - provider_name=provider_name, |
192 | | - ) |
193 | | - ) |
194 | | - |
195 | | - return builder.messages |
| 109 | + """Transform AG-UI messages into Pydantic AI messages. |
| 110 | +
|
| 111 | + This is a convenience method that delegates to [`messages_from_ag_ui()`][pydantic_ai.ui.ag_ui.messages_from_ag_ui]. |
| 112 | + You can use that function directly if you need to convert messages without creating an adapter. |
| 113 | + """ |
| 114 | + return messages_from_ag_ui(messages) |
0 commit comments