@@ -67,13 +67,11 @@ async def idempotency_start(key: str, ttl_seconds: Optional[int] = None) -> None
6767 async with _IDEMPOTENCY_LOCK :
6868 # purge expired
6969 now = _now_seconds ()
70- expired = [k for k , v in _IDEMPOTENCY_RUNNING .items (
71- ) if now - v > (ttl_seconds or _IDEMPOTENCY_TTL_SECONDS_DEFAULT )]
70+ expired = [k for k , v in _IDEMPOTENCY_RUNNING .items () if now - v > (ttl_seconds or _IDEMPOTENCY_TTL_SECONDS_DEFAULT )]
7271 for k in expired :
7372 _IDEMPOTENCY_RUNNING .pop (k , None )
7473 if key in _IDEMPOTENCY_RUNNING :
75- raise LimitExceededError (
76- "Duplicate request is still running, please wait." )
74+ raise LimitExceededError ("Duplicate request is still running, please wait." )
7775 _IDEMPOTENCY_RUNNING [key ] = now
7876
7977
@@ -93,8 +91,7 @@ async def check_and_consume_rate_limit(tenant_id: str) -> None:
9391 state = _RATE_STATE .setdefault (tenant_id , {})
9492 count = state .get (bucket , 0 )
9593 if count >= _RATE_LIMIT_PER_MINUTE :
96- raise LimitExceededError (
97- "Query rate exceeded limit. Please try again later" )
94+ raise LimitExceededError ("Query rate exceeded limit. Please try again later" )
9895 state [bucket ] = count + 1
9996 # cleanup old buckets, keep only current
10097 for b in list (state .keys ()):
@@ -123,20 +120,17 @@ def _build_idempotency_key(*parts: Any) -> str:
123120async def to_external_conversation_id (internal_id : int ) -> str :
124121 if not internal_id :
125122 raise Exception ("invalid internal conversation id" )
126- external_id = get_external_id_by_internal (
127- internal_id = internal_id , mapping_type = "CONVERSATION" )
123+ external_id = get_external_id_by_internal (internal_id = internal_id , mapping_type = "CONVERSATION" )
128124 if not external_id :
129- logger .error (
130- f"cannot find external id for conversation_id: { internal_id } " )
125+ logger .error (f"cannot find external id for conversation_id: { internal_id } " )
131126 raise Exception ("cannot find external id" )
132127 return external_id
133128
134129
135130async def to_internal_conversation_id (external_id : str ) -> int :
136131 if not external_id :
137132 raise Exception ("invalid external conversation id" )
138- internal_id = get_internal_id_by_external (
139- external_id = external_id , mapping_type = "CONVERSATION" )
133+ internal_id = get_internal_id_by_external (external_id = external_id , mapping_type = "CONVERSATION" )
140134 return internal_id
141135
142136
@@ -147,8 +141,7 @@ async def get_agent_info_by_name(agent_name: str, tenant_id: str) -> int:
147141 try :
148142 return await get_agent_id_by_name (agent_name = agent_name , tenant_id = tenant_id )
149143 except Exception as _ :
150- raise Exception (
151- f"Failed to get agent id for agent_name: { agent_name } in tenant_id: { tenant_id } " )
144+ raise Exception (f"Failed to get agent id for agent_name: { agent_name } in tenant_id: { tenant_id } " )
152145
153146
154147async def start_streaming_chat (
@@ -166,22 +159,18 @@ async def start_streaming_chat(
166159 internal_conversation_id = await to_internal_conversation_id (external_conversation_id )
167160 # Add mapping to postgres database
168161 if internal_conversation_id is None :
169- logging .info (
170- f"Conversation { external_conversation_id } not found, creating a new conversation" )
162+ logging .info (f"Conversation { external_conversation_id } not found, creating a new conversation" )
171163 # Create a new conversation and get its internal ID
172- new_conversation = create_new_conversation (
173- title = "New Conversation" , user_id = ctx .user_id )
164+ new_conversation = create_new_conversation (title = "New Conversation" , user_id = ctx .user_id )
174165 internal_conversation_id = new_conversation ["conversation_id" ]
175166 # Add the new mapping to the database
176- add_mapping_id (internal_id = internal_conversation_id ,
177- external_id = external_conversation_id , tenant_id = ctx .tenant_id , user_id = ctx .user_id )
167+ add_mapping_id (internal_id = internal_conversation_id , external_id = external_conversation_id , tenant_id = ctx .tenant_id , user_id = ctx .user_id )
178168
179169 # Get history according to internal_conversation_id
180170 history = await get_conversation_history (ctx , external_conversation_id )
181171 agent_id = await get_agent_id_by_name (agent_name = agent_name , tenant_id = ctx .tenant_id )
182172 # Idempotency: only prevent concurrent duplicate starts
183- composed_key = idempotency_key or _build_idempotency_key (
184- ctx .tenant_id , external_conversation_id , agent_id , query )
173+ composed_key = idempotency_key or _build_idempotency_key (ctx .tenant_id , external_conversation_id , agent_id , query )
185174 await idempotency_start (composed_key )
186175 agent_request = AgentRequest (
187176 conversation_id = internal_conversation_id ,
@@ -193,13 +182,11 @@ async def start_streaming_chat(
193182 )
194183
195184 except LimitExceededError as _ :
196- raise LimitExceededError (
197- "Query rate exceeded limit. Please try again later." )
185+ raise LimitExceededError ("Query rate exceeded limit. Please try again later." )
198186 except UnauthorizedError as _ :
199187 raise UnauthorizedError ("Cannot authenticate." )
200188 except Exception as e :
201- raise Exception (
202- f"Failed to start streaming chat for external conversation id { external_conversation_id } : { str (e )} " )
189+ raise Exception (f"Failed to start streaming chat for external conversation id { external_conversation_id } : { str (e )} " )
203190
204191 try :
205192 response = await run_agent_stream (
@@ -226,8 +213,7 @@ async def stop_chat(ctx: NorthboundContext, external_conversation_id: str) -> Di
226213 stop_result = stop_agent_tasks (internal_id )
227214 return {"message" : stop_result .get ("message" , "success" ), "data" : external_conversation_id , "requestId" : ctx .request_id }
228215 except Exception as e :
229- raise Exception (
230- f"Failed to stop chat for external conversation id { external_conversation_id } : { str (e )} " )
216+ raise Exception (f"Failed to stop chat for external conversation id { external_conversation_id } : { str (e )} " )
231217
232218
233219async def list_conversations (ctx : NorthboundContext ) -> Dict [str , Any ]:
@@ -266,8 +252,7 @@ async def get_agent_info_list(ctx: NorthboundContext) -> Dict[str, Any]:
266252 agent_info .pop ("agent_id" , None )
267253 return {"message" : "success" , "data" : agent_info_list , "requestId" : ctx .request_id }
268254 except Exception as e :
269- raise Exception (
270- f"Failed to get agent info list for tenant { ctx .tenant_id } : { str (e )} " )
255+ raise Exception (f"Failed to get agent info list for tenant { ctx .tenant_id } : { str (e )} " )
271256
272257
273258async def update_conversation_title (ctx : NorthboundContext , external_conversation_id : str , title : str , idempotency_key : Optional [str ] = None ) -> Dict [str , Any ]:
@@ -276,8 +261,7 @@ async def update_conversation_title(ctx: NorthboundContext, external_conversatio
276261 internal_id = await to_internal_conversation_id (external_conversation_id )
277262
278263 # Idempotency: avoid concurrent duplicate title update for same conversation
279- composed_key = idempotency_key or _build_idempotency_key (
280- ctx .tenant_id , external_conversation_id , title )
264+ composed_key = idempotency_key or _build_idempotency_key (ctx .tenant_id , external_conversation_id , title )
281265 await idempotency_start (composed_key )
282266
283267 update_conversation_title_service (internal_id , title , ctx .user_id )
@@ -288,11 +272,9 @@ async def update_conversation_title(ctx: NorthboundContext, external_conversatio
288272 "idempotency_key" : composed_key ,
289273 }
290274 except LimitExceededError as _ :
291- raise LimitExceededError (
292- "Duplicate request is still running, please wait." )
275+ raise LimitExceededError ("Duplicate request is still running, please wait." )
293276 except Exception as e :
294- raise Exception (
295- f"Failed to update conversation title for external conversation id { external_conversation_id } : { str (e )} " )
277+ raise Exception (f"Failed to update conversation title for external conversation id { external_conversation_id } : { str (e )} " )
296278 finally :
297279 if composed_key :
298280 asyncio .create_task (_release_idempotency_after_delay (composed_key ))
0 commit comments