|
1 | 1 | from enum import Enum
|
2 |
| -from typing import Any, AsyncGenerator, Dict, List, Optional |
| 2 | +from typing import AsyncGenerator, List, Optional |
3 | 3 |
|
4 | 4 | from agno.agent import Agent
|
5 |
| -from agno.storage.agent.session import AgentSession |
6 | 5 | from fastapi import APIRouter, HTTPException, status
|
7 | 6 | from fastapi.responses import StreamingResponse
|
8 | 7 | from pydantic import BaseModel
|
@@ -64,7 +63,7 @@ class RunRequest(BaseModel):
|
64 | 63 | session_id: Optional[str] = None
|
65 | 64 |
|
66 | 65 |
|
67 |
| -@agents_router.post("/{agent_id}/run", status_code=status.HTTP_200_OK) |
| 66 | +@agents_router.post("/{agent_id}/runs", status_code=status.HTTP_200_OK) |
68 | 67 | async def run_agent(agent_id: AgentType, body: RunRequest):
|
69 | 68 | """
|
70 | 69 | POST /agents/{agent_id}/run
|
@@ -101,190 +100,3 @@ async def run_agent(agent_id: AgentType, body: RunRequest):
|
101 | 100 | # For advanced use cases, we should yield the entire response
|
102 | 101 | # that contains the tool calls and intermediate steps.
|
103 | 102 | return response.content
|
104 |
| - |
105 |
| - |
106 |
| -@agents_router.get("/{agent_id}/sessions", response_model=List[str]) |
107 |
| -async def get_agent_session_ids(agent_id: AgentType, user_id: Optional[str] = None): |
108 |
| - """ |
109 |
| - GET /agents/{agent_id}/sessions |
110 |
| -
|
111 |
| - Returns all session IDs for a specific agent and user. |
112 |
| -
|
113 |
| - Args: |
114 |
| - agent_id: The agent type |
115 |
| - user_id: Optional user ID |
116 |
| -
|
117 |
| - Returns: |
118 |
| - List[str]: List of session identifiers |
119 |
| - """ |
120 |
| - logger.debug(f"GetAgentSessionsRequest: agent_id={agent_id}, user_id={user_id}") |
121 |
| - return get_agent(user_id=user_id, agent_id=agent_id).storage.get_all_session_ids() |
122 |
| - |
123 |
| - |
124 |
| -@agents_router.get("/{agent_id}/sessions/{session_id}", response_model=Optional[AgentSession]) |
125 |
| -async def get_agent_session(agent_id: AgentType, session_id: str, user_id: Optional[str] = None): |
126 |
| - """ |
127 |
| - GET /agents/{agent_id}/sessions/{session_id} |
128 |
| -
|
129 |
| - Retrieves details about a specific agent session. |
130 |
| -
|
131 |
| - Args: |
132 |
| - agent_id: Agent ID |
133 |
| - session_id: The session ID to retrieve |
134 |
| - user_id: Optional user ID |
135 |
| -
|
136 |
| - Returns: |
137 |
| - AgentSession or None if not found |
138 |
| - """ |
139 |
| - logger.debug(f"GetAgentSessionRequest: agent_id={agent_id}, session_id={session_id}, user_id={user_id}") |
140 |
| - |
141 |
| - try: |
142 |
| - agent: Agent = get_agent(session_id=session_id, user_id=user_id, agent_id=agent_id) |
143 |
| - return agent.read_from_storage() |
144 |
| - except Exception as e: |
145 |
| - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Session not found: {str(e)}") |
146 |
| - |
147 |
| - |
148 |
| -@agents_router.get("/{agent_id}/sessions/{session_id}/messages", response_model=List[Dict[str, Any]]) |
149 |
| -async def get_session_messages(agent_id: AgentType, session_id: str, user_id: Optional[str] = None): |
150 |
| - """ |
151 |
| - GET /agents/{agent_id}/sessions/{session_id}/messages |
152 |
| -
|
153 |
| - Retrieves the messages for a specific agent session. |
154 |
| -
|
155 |
| - Args: |
156 |
| - agent_id: Agent ID |
157 |
| - session_id: The session ID to retrieve history for |
158 |
| - user_id: Optional user ID |
159 |
| -
|
160 |
| - Returns: |
161 |
| - List of message objects representing the conversation history |
162 |
| - """ |
163 |
| - logger.debug(f"GetSessionHistoryRequest: agent_id={agent_id}, session_id={session_id}, user_id={user_id}") |
164 |
| - |
165 |
| - try: |
166 |
| - agent: Agent = get_agent(session_id=session_id, user_id=user_id, agent_id=agent_id) |
167 |
| - # Load the agent from the database |
168 |
| - agent.read_from_storage() |
169 |
| - except Exception as e: |
170 |
| - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Session not found: {str(e)}") |
171 |
| - |
172 |
| - if agent.memory: |
173 |
| - return agent.memory.get_messages() |
174 |
| - else: |
175 |
| - return [] |
176 |
| - |
177 |
| - |
178 |
| -@agents_router.delete("/{agent_id}/sessions/{session_id}", response_model=dict) |
179 |
| -async def delete_session(agent_id: AgentType, session_id: str, user_id: Optional[str] = None): |
180 |
| - """ |
181 |
| - DELETE /agents/{agent_id}/sessions/{session_id} |
182 |
| -
|
183 |
| - Deletes a specific agent session. |
184 |
| - """ |
185 |
| - logger.debug(f"DeleteSessionRequest: agent_id={agent_id}, session_id={session_id}") |
186 |
| - |
187 |
| - try: |
188 |
| - agent: Agent = get_agent(user_id=user_id, agent_id=agent_id, session_id=session_id) |
189 |
| - agent.delete_session(session_id=session_id) |
190 |
| - return {"message": "Session deleted"} |
191 |
| - except Exception as e: |
192 |
| - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Session not found: {str(e)}") |
193 |
| - |
194 |
| - |
195 |
| -class RenameSessionRequest(BaseModel): |
196 |
| - """Request model for renaming a session""" |
197 |
| - |
198 |
| - session_name: str |
199 |
| - |
200 |
| - |
201 |
| -@agents_router.patch("/{agent_id}/sessions/{session_id}/rename", response_model=dict) |
202 |
| -async def rename_session( |
203 |
| - agent_id: AgentType, session_id: str, body: RenameSessionRequest, user_id: Optional[str] = None |
204 |
| -): |
205 |
| - """ |
206 |
| - PATCH /agents/{agent_id}/sessions/{session_id}/rename |
207 |
| -
|
208 |
| - Renames a specific agent session. |
209 |
| -
|
210 |
| - Args: |
211 |
| - agent_id: Agent ID |
212 |
| - session_id: The session ID to rename |
213 |
| - body: Request containing the new session name |
214 |
| - user_id: Optional user ID |
215 |
| -
|
216 |
| - Returns: |
217 |
| - Updated session information |
218 |
| - """ |
219 |
| - logger.debug( |
220 |
| - f"RenameSessionRequest: agent_id={agent_id}, session_id={session_id}, session_name={body.session_name}" |
221 |
| - ) |
222 |
| - |
223 |
| - try: |
224 |
| - agent: Agent = get_agent(user_id=user_id, agent_id=agent_id, session_id=session_id) |
225 |
| - agent.rename_session(session_name=body.session_name) |
226 |
| - |
227 |
| - return { |
228 |
| - "session_id": agent.session_id, |
229 |
| - "session_name": agent.session_name, |
230 |
| - } |
231 |
| - except Exception as e: |
232 |
| - # Use a more appropriate status code for errors that might not be "not found" |
233 |
| - raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=f"Failed to rename session: {str(e)}") |
234 |
| - |
235 |
| - |
236 |
| -@agents_router.post("/{agent_id}/sessions/{session_id}/auto-rename", response_model=dict) |
237 |
| -async def auto_rename_session(agent_id: AgentType, session_id: str, user_id: Optional[str] = None): |
238 |
| - """ |
239 |
| - POST /agents/{agent_id}/sessions/{session_id}/auto-rename |
240 |
| -
|
241 |
| - Automatically renames a session using the LLM based on conversation context. |
242 |
| -
|
243 |
| - Args: |
244 |
| - session_id: The session ID to auto-rename |
245 |
| - user_id: Optional user ID |
246 |
| - agent_id: Optional agent type |
247 |
| -
|
248 |
| - Returns: |
249 |
| - Updated session information with the auto-generated name |
250 |
| - """ |
251 |
| - logger.debug(f"AutoRenameSessionRequest: agent_id={agent_id}, session_id={session_id}") |
252 |
| - |
253 |
| - try: |
254 |
| - agent: Agent = get_agent(user_id=user_id, agent_id=agent_id, session_id=session_id) |
255 |
| - agent.auto_rename_session() |
256 |
| - |
257 |
| - return { |
258 |
| - "session_id": agent.session_id, |
259 |
| - "session_name": agent.session_name, |
260 |
| - } |
261 |
| - except Exception as e: |
262 |
| - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Failed to auto-rename session: {str(e)}") |
263 |
| - |
264 |
| - |
265 |
| -@agents_router.post("/{agent_id}/load-knowledge", status_code=status.HTTP_200_OK) |
266 |
| -async def load_knowledge(agent_id: AgentType, user_id: Optional[str] = None, recreate: bool = False): |
267 |
| - """ |
268 |
| - POST /agents/{agent_id}/load-knowledge |
269 |
| -
|
270 |
| - Loads the knowledge base for a specific agent. Please update the Agent Knowledge Base with the required data before calling this endpoint. Example: PDFUrlKnowledgeBase, CSVKnowledgeBase, etc. |
271 |
| -
|
272 |
| - Args: |
273 |
| - agent_id: The agent type |
274 |
| - user_id: Optional user ID |
275 |
| - recreate: Whether to recreate the knowledge base |
276 |
| - Returns: |
277 |
| - Confirmation message |
278 |
| - """ |
279 |
| - logger.debug(f"LoadKnowledgeRequest: agent_id={agent_id}, user_id={user_id}") |
280 |
| - |
281 |
| - try: |
282 |
| - agent: Agent = get_agent(user_id=user_id, agent_id=agent_id) |
283 |
| - logger.debug(f"Agent: {agent.knowledge}") |
284 |
| - if agent.knowledge is not None: |
285 |
| - agent.knowledge.load(recreate=recreate) |
286 |
| - return {"message": "Knowledge Base Loaded"} |
287 |
| - except Exception as e: |
288 |
| - logger.error(f"Failed to load knowledge base: {str(e)}") |
289 |
| - # Consider more specific error handling here - not all exceptions should be 404 |
290 |
| - raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Failed to load knowledge base: {str(e)}") |
0 commit comments