@@ -62,7 +62,13 @@ def validate_conversation_ownership(
6262
6363
6464def check_configuration_loaded (config : AppConfig ) -> None :
65- """Check that configuration is loaded and raise exception when it is not."""
65+ """
66+ Ensure the application configuration object is present.
67+
68+ Raises:
69+ HTTPException: HTTP 500 Internal Server Error with detail `{"response":
70+ "Configuration is not loaded"}` when `config` is None.
71+ """
6672 if config is None :
6773 raise HTTPException (
6874 status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
@@ -71,7 +77,31 @@ def check_configuration_loaded(config: AppConfig) -> None:
7177
7278
7379def get_system_prompt (query_request : QueryRequest , config : AppConfig ) -> str :
74- """Get the system prompt: the provided one, configured one, or default one."""
80+ """
81+ Resolve which system prompt to use for a query.
82+
83+ Precedence:
84+ 1. If the request includes `system_prompt`, that value is returned (highest
85+ precedence).
86+ 2. Else if the application configuration provides a customization
87+ `system_prompt`, that value is returned.
88+ 3. Otherwise the module default `constants.DEFAULT_SYSTEM_PROMPT` is
89+ returned (lowest precedence).
90+
91+ If configuration disables per-request system prompts
92+ (config.customization.disable_query_system_prompt) and the incoming
93+ `query_request` contains a `system_prompt`, an HTTP 422 Unprocessable
94+ Entity is raised instructing the client to remove the field.
95+
96+ Parameters:
97+ query_request (QueryRequest): The incoming query payload; may contain a
98+ per-request `system_prompt`.
99+ config (AppConfig): Application configuration which may include
100+ customization flags and a default `system_prompt`.
101+
102+ Returns:
103+ str: The resolved system prompt to apply to the request.
104+ """
75105 system_prompt_disabled = (
76106 config .customization is not None
77107 and config .customization .disable_query_system_prompt
@@ -171,7 +201,46 @@ async def get_agent(
171201 conversation_id : str | None ,
172202 no_tools : bool = False ,
173203) -> tuple [AsyncAgent , str , str ]:
174- """Get existing agent or create a new one with session persistence."""
204+ """
205+ Create or reuse an AsyncAgent with session persistence.
206+
207+ Return the agent, conversation and session IDs.
208+
209+ If a conversation_id is provided, the function attempts to retrieve the
210+ existing agent and, on success, rebinds a newly created agent instance to
211+ that conversation (deleting the temporary/orphan agent) and returns the
212+ first existing session_id for the conversation. If no conversation_id is
213+ provided or the existing agent cannot be retrieved, a new agent and session
214+ are created.
215+
216+ Parameters:
217+ model_id (str): Identifier of the model to instantiate the agent with.
218+ system_prompt (str): Instructions/system prompt to initialize the agent with.
219+
220+ available_input_shields (list[str]): Input shields to apply to the
221+ agent; empty list used if None/empty.
222+
223+ available_output_shields (list[str]): Output shields to apply to the
224+ agent; empty list used if None/empty.
225+
226+ conversation_id (str | None): If provided, attempt to reuse the agent
227+ for this conversation; otherwise a new conversation_id is created.
228+
229+ no_tools (bool): When True, disables tool parsing for the agent (uses no tool parser).
230+
231+ Returns:
232+ tuple[AsyncAgent, str, str]: A tuple of (agent, conversation_id, session_id).
233+
234+ Raises:
235+ HTTPException: Raises HTTP 404 Not Found if an attempt to reuse a
236+ conversation succeeds in retrieving the agent but no sessions are found
237+ for that conversation.
238+
239+ Side effects:
240+ - May delete an orphan agent when rebinding a newly created agent to an
241+ existing conversation_id.
242+ - Initializes the agent and may create a new session.
243+ """
175244 existing_agent_id = None
176245 if conversation_id :
177246 with suppress (ValueError ):
0 commit comments