11
11
ThreadResponse ,
12
12
)
13
13
from template_fastapi .repositories .agents import AgentRepository
14
+ from template_fastapi .settings .logging import get_logger
14
15
16
+ logger = get_logger (__name__ )
15
17
router = APIRouter ()
16
18
agent_repo = AgentRepository ()
17
19
@@ -25,9 +27,13 @@ async def create_agent(request: AgentRequest) -> AgentResponse:
25
27
"""
26
28
新しいエージェントを作成する
27
29
"""
30
+ logger .info (f"Creating new agent with model: { request .model } " )
28
31
try :
29
- return agent_repo .create_agent (request )
32
+ result = agent_repo .create_agent (request )
33
+ logger .info (f"Successfully created agent with ID: { result .id } " )
34
+ return result
30
35
except Exception as e :
36
+ logger .error (f"Failed to create agent: { str (e )} " , exc_info = True )
31
37
raise HTTPException (status_code = 500 , detail = f"エージェントの作成に失敗しました: { str (e )} " )
32
38
33
39
@@ -40,9 +46,13 @@ async def get_agent(agent_id: str) -> AgentResponse:
40
46
"""
41
47
エージェントの情報を取得する
42
48
"""
49
+ logger .info (f"Getting agent: { agent_id } " )
43
50
try :
44
- return agent_repo .get_agent (agent_id )
51
+ result = agent_repo .get_agent (agent_id )
52
+ logger .debug (f"Successfully retrieved agent: { agent_id } " )
53
+ return result
45
54
except Exception as e :
55
+ logger .warning (f"Agent not found: { agent_id } - { str (e )} " )
46
56
raise HTTPException (status_code = 404 , detail = f"エージェントが見つかりません: { str (e )} " )
47
57
48
58
@@ -57,9 +67,13 @@ async def list_agents(
57
67
"""
58
68
エージェントの一覧を取得する
59
69
"""
70
+ logger .info (f"Listing agents with limit: { limit } " )
60
71
try :
61
- return agent_repo .list_agents (limit = limit )
72
+ result = agent_repo .list_agents (limit = limit )
73
+ logger .info (f"Retrieved { len (result .agents )} agents" )
74
+ return result
62
75
except Exception as e :
76
+ logger .error (f"Failed to list agents: { str (e )} " , exc_info = True )
63
77
raise HTTPException (status_code = 500 , detail = f"エージェント一覧の取得に失敗しました: { str (e )} " )
64
78
65
79
@@ -71,13 +85,17 @@ async def delete_agent(agent_id: str) -> dict:
71
85
"""
72
86
エージェントを削除する
73
87
"""
88
+ logger .info (f"Deleting agent: { agent_id } " )
74
89
try :
75
90
success = agent_repo .delete_agent (agent_id )
76
91
if success :
92
+ logger .info (f"Successfully deleted agent: { agent_id } " )
77
93
return {"message" : "エージェントが正常に削除されました" }
78
94
else :
95
+ logger .error (f"Failed to delete agent (no error): { agent_id } " )
79
96
raise HTTPException (status_code = 500 , detail = "エージェントの削除に失敗しました" )
80
97
except Exception as e :
98
+ logger .error (f"Failed to delete agent { agent_id } : { str (e )} " , exc_info = True )
81
99
raise HTTPException (status_code = 500 , detail = f"エージェントの削除に失敗しました: { str (e )} " )
82
100
83
101
@@ -90,9 +108,14 @@ async def chat_with_agent(agent_id: str, request: ChatRequest) -> ChatResponse:
90
108
"""
91
109
エージェントとチャットする
92
110
"""
111
+ logger .info (f"Starting chat with agent { agent_id } on thread { request .thread_id } " )
112
+ logger .debug (f"Chat message: { request .message [:100 ]} ..." ) # Log first 100 chars
93
113
try :
94
- return agent_repo .chat_with_agent (agent_id , request )
114
+ result = agent_repo .chat_with_agent (agent_id , request )
115
+ logger .info (f"Chat completed successfully with agent { agent_id } " )
116
+ return result
95
117
except Exception as e :
118
+ logger .error (f"Failed to chat with agent { agent_id } : { str (e )} " , exc_info = True )
96
119
raise HTTPException (status_code = 500 , detail = f"エージェントとのチャットに失敗しました: { str (e )} " )
97
120
98
121
@@ -105,9 +128,13 @@ async def create_thread(request: ThreadRequest) -> ThreadResponse:
105
128
"""
106
129
新しいスレッドを作成する
107
130
"""
131
+ logger .info ("Creating new thread" )
108
132
try :
109
- return agent_repo .create_thread (request )
133
+ result = agent_repo .create_thread (request )
134
+ logger .info (f"Successfully created thread with ID: { result .id } " )
135
+ return result
110
136
except Exception as e :
137
+ logger .error (f"Failed to create thread: { str (e )} " , exc_info = True )
111
138
raise HTTPException (status_code = 500 , detail = f"スレッドの作成に失敗しました: { str (e )} " )
112
139
113
140
@@ -120,9 +147,13 @@ async def get_thread(thread_id: str) -> ThreadResponse:
120
147
"""
121
148
スレッドの情報を取得する
122
149
"""
150
+ logger .info (f"Getting thread: { thread_id } " )
123
151
try :
124
- return agent_repo .get_thread (thread_id )
152
+ result = agent_repo .get_thread (thread_id )
153
+ logger .debug (f"Successfully retrieved thread: { thread_id } " )
154
+ return result
125
155
except Exception as e :
156
+ logger .warning (f"Thread not found: { thread_id } - { str (e )} " )
126
157
raise HTTPException (status_code = 404 , detail = f"スレッドが見つかりません: { str (e )} " )
127
158
128
159
@@ -134,13 +165,17 @@ async def delete_thread(thread_id: str) -> dict:
134
165
"""
135
166
スレッドを削除する
136
167
"""
168
+ logger .info (f"Deleting thread: { thread_id } " )
137
169
try :
138
170
success = agent_repo .delete_thread (thread_id )
139
171
if success :
172
+ logger .info (f"Successfully deleted thread: { thread_id } " )
140
173
return {"message" : "スレッドが正常に削除されました" }
141
174
else :
175
+ logger .error (f"Failed to delete thread (no error): { thread_id } " )
142
176
raise HTTPException (status_code = 500 , detail = "スレッドの削除に失敗しました" )
143
177
except Exception as e :
178
+ logger .error (f"Failed to delete thread { thread_id } : { str (e )} " , exc_info = True )
144
179
raise HTTPException (status_code = 500 , detail = f"スレッドの削除に失敗しました: { str (e )} " )
145
180
146
181
@@ -155,7 +190,11 @@ async def list_threads(
155
190
"""
156
191
エージェントのスレッド一覧を取得する
157
192
"""
193
+ logger .info (f"Listing threads with limit: { limit } " )
158
194
try :
159
- return agent_repo .list_threads (limit = limit )
195
+ result = agent_repo .list_threads (limit = limit )
196
+ logger .info (f"Retrieved { len (result .threads )} threads" )
197
+ return result
160
198
except Exception as e :
199
+ logger .error (f"Failed to list threads: { str (e )} " , exc_info = True )
161
200
raise HTTPException (status_code = 500 , detail = f"スレッド一覧の取得に失敗しました: { str (e )} " )
0 commit comments