From 89db642e7b72ab68bfbd2329bd17111931493378 Mon Sep 17 00:00:00 2001 From: bwmac Date: Fri, 17 Jan 2025 10:02:59 -0500 Subject: [PATCH] split too long lines --- synapseclient/models/agent.py | 61 ++++++++++++------- .../models/mixins/asynchronous_job.py | 17 +++--- .../models/protocols/agent_protocol.py | 37 +++++++---- .../models/async/test_agent_async.py | 3 +- .../async/unit_test_asynchronous_job.py | 5 +- .../models/async/unit_test_agent_async.py | 21 ++++--- 6 files changed, 92 insertions(+), 52 deletions(-) diff --git a/synapseclient/models/agent.py b/synapseclient/models/agent.py index be5cf2f75..2fabd5a3f 100644 --- a/synapseclient/models/agent.py +++ b/synapseclient/models/agent.py @@ -131,13 +131,16 @@ class AgentSession(AgentSessionSynchronousProtocol): """Represents a [Synapse Agent Session](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/agent/AgentSession.html) Attributes: - id: The unique ID of the agent session. Can only be used by the user that created it. + id: The unique ID of the agent session. + Can only be used by the user that created it. access_level: The access level of the agent session. - One of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, or WRITE_YOUR_PRIVATE_DATA. + One of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, + or WRITE_YOUR_PRIVATE_DATA. started_on: The date the agent session was started. started_by: The ID of the user who started the agent session. modified_on: The date the agent session was last modified. - agent_registration_id: The registration ID of the agent that will be used for this session. + agent_registration_id: The registration ID of the agent that will + be used for this session. etag: The etag of the agent session. Note: It is recommended to use the `Agent` class to start and get sessions, @@ -191,14 +194,15 @@ class AgentSession(AgentSessionSynchronousProtocol): """ id: Optional[str] = None - """The unique ID of the agent session. Can only be used by the user that created it.""" + """The unique ID of the agent session. + Can only be used by the user that created it.""" access_level: Optional[ AgentSessionAccessLevel ] = AgentSessionAccessLevel.PUBLICLY_ACCESSIBLE """The access level of the agent session. - One of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, or WRITE_YOUR_PRIVATE_DATA. - Defaults to PUBLICLY_ACCESSIBLE. + One of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, or + WRITE_YOUR_PRIVATE_DATA. Defaults to PUBLICLY_ACCESSIBLE. """ started_on: Optional[datetime] = None @@ -319,14 +323,15 @@ async def prompt_async( *, synapse_client: Optional[Synapse] = None, ) -> None: - """Sends a prompt to the agent and adds the response to the AgentSession's chat history. - A session must be started before sending a prompt. + """Sends a prompt to the agent and adds the response to the AgentSession's + chat history. A session must be started before sending a prompt. Arguments: prompt: The prompt to send to the agent. enable_trace: Whether to enable trace for the prompt. print_response: Whether to print the response to the console. - newer_than: The timestamp to get trace results newer than. Defaults to None (all results). + newer_than: The timestamp to get trace results newer than. + Defaults to None (all results). synapse_client: If not passed in and caching was not disabled by `Synapse.allow_client_caching(False)` this will use the last created instance from the Synapse class constructor. @@ -362,9 +367,11 @@ class Agent(AgentSynchronousProtocol): Example: Chat with the baseline Synapse Agent - You can chat with the same agent which is available in the Synapse UI at https://www.synapse.org/Chat:default. - By default, this "baseline" agent is used when a registration ID is not provided. In the background, - the Agent class will start a session and set that new session as the current session if one is not already set. + You can chat with the same agent which is available in the Synapse UI + at https://www.synapse.org/Chat:default. By default, this "baseline" agent + is used when a registration ID is not provided. In the background, + the Agent class will start a session and set that new session as the + current session if one is not already set. syn = Synapse() syn.login() @@ -376,9 +383,11 @@ class Agent(AgentSynchronousProtocol): print_response=True, ) - Example: Register and chat with a custom agent **Only available for internal users (Sage Bionetworks employees)** + Example: Register and chat with a custom agent + **Only available for internal users (Sage Bionetworks employees)** - Alternatively, you can register a custom agent and chat with it provided you have already created it. + Alternatively, you can register a custom agent and chat with it provided + you have already created it. syn = Synapse() syn.login(silent=True) @@ -395,11 +404,13 @@ class Agent(AgentSynchronousProtocol): Advanced Example: Start and prompt multiple sessions - Here, we connect to a custom agent and start one session with the prompt "Hello". In the background, - this first session is being set as the current session and future prompts will be sent to this session - by default. If we want to send a prompt to a different session, we can do so by starting it and calling - prompt again, but with our new session as an argument. We now have two sessions, both stored in the - my_agent.sessions dictionary. After the second prompt, my_second_session is now the current session. + Here, we connect to a custom agent and start one session with the prompt "Hello". + In the background, this first session is being set as the current session + and future prompts will be sent to this session by default. If we want to send a + prompt to a different session, we can do so by starting it and calling prompt again, + but with our new session as an argument. We now have two sessions, both stored in the + my_agent.sessions dictionary. After the second prompt, my_second_session is now + the current session. syn = Synapse() syn.login() @@ -469,7 +480,8 @@ def fill_from_dict(self, agent_registration: Dict[str, str]) -> "Agent": async def register_async( self, *, synapse_client: Optional[Synapse] = None ) -> "Agent": - """Registers an agent with the Synapse API. If agent already exists, it will be retrieved. + """Registers an agent with the Synapse API. + If agent already exists, it will be retrieved. Arguments: synapse_client: If not passed in and caching was not disabled by @@ -524,7 +536,8 @@ async def start_session_async( Arguments: access_level: The access level of the agent session. - Must be one of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, or WRITE_YOUR_PRIVATE_DATA. + Must be one of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, + or WRITE_YOUR_PRIVATE_DATA. Defaults to PUBLICLY_ACCESSIBLE. synapse_client: If not passed in and caching was not disabled by `Synapse.allow_client_caching(False)` this will use the last created @@ -548,7 +561,8 @@ async def get_session_async( self, session_id: str, *, synapse_client: Optional[Synapse] = None ) -> "AgentSession": """Gets an existing agent session. - Adds the session to the Agent's sessions dictionary and sets it as the current session. + Adds the session to the Agent's sessions dictionary and + sets it as the current session. Arguments: session_id: The ID of the session to get. @@ -587,7 +601,8 @@ async def prompt_async( prompt: The prompt to send to the agent. enable_trace: Whether to enable trace for the prompt. print_response: Whether to print the response to the console. - session_id: The ID of the session to send the prompt to. If None, the current session will be used. + session_id: The ID of the session to send the prompt to. + If None, the current session will be used. newer_than: The timestamp to get trace results newer than. Defaults to None (all results). synapse_client: If not passed in and caching was not disabled by `Synapse.allow_client_caching(False)` this will use the last created diff --git a/synapseclient/models/mixins/asynchronous_job.py b/synapseclient/models/mixins/asynchronous_job.py index a759cfe7d..64840cc64 100644 --- a/synapseclient/models/mixins/asynchronous_job.py +++ b/synapseclient/models/mixins/asynchronous_job.py @@ -91,9 +91,10 @@ class CallersContext(str, Enum): """Enum representing information about a web service call: - - SESSION_ID: Each web service request is issued a unique session ID (UUID) that is included in the call's access record. - Events that are triggered by a web service request should include the session ID so that they can be linked to - each other and the call's access record. + - SESSION_ID: Each web service request is issued a unique session ID (UUID) + that is included in the call's access record. + Events that are triggered by a web service request should include the session ID + so that they can be linked to each other and the call's access record. """ SESSION_ID = "SESSION_ID" @@ -107,9 +108,11 @@ class AsynchronousJobStatus: Attributes: state: The state of the job. Either PROCESSING, FAILED, or COMPLETE. canceling: Whether the job has been requested to be cancelled. - request_body: The body of an Asynchronous job request. Will be one of the models described here: + request_body: The body of an Asynchronous job request. + Will be one of the models described here: - response_body: The body of an Asynchronous job response. Will be one of the models described here: + response_body: The body of an Asynchronous job response. + Will be one of the models described here: etag: The etag of the job status. Changes whenever the status changes. id: The ID if the job issued when this job was started. @@ -118,8 +121,8 @@ class AsynchronousJobStatus: changed_on: The date-time when the status of this job was last changed. progress_message: The current message of the progress tracker. progress_current: A value indicating how much progress has been made. - I.e. a value of 50 indicates that 50% of the work has been - completed if progress_total is 100. + I.e. a value of 50 indicates that 50% of the work has been + completed if progress_total is 100. progress_total: A value indicating the total amount of work to complete. exception: The exception that needs to be thrown if the job fails. error_message: A one-line error message when the job fails. diff --git a/synapseclient/models/protocols/agent_protocol.py b/synapseclient/models/protocols/agent_protocol.py index 6c12a03aa..b2235cb7d 100644 --- a/synapseclient/models/protocols/agent_protocol.py +++ b/synapseclient/models/protocols/agent_protocol.py @@ -17,7 +17,8 @@ def start(self, *, synapse_client: Optional[Synapse] = None) -> "AgentSession": """Starts an agent session. Arguments: - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. Returns: The new AgentSession object. @@ -28,7 +29,8 @@ def get(self, *, synapse_client: Optional[Synapse] = None) -> "AgentSession": """Gets an existing agent session. Arguments: - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. Returns: The existing AgentSession object. @@ -39,7 +41,8 @@ def update(self, *, synapse_client: Optional[Synapse] = None) -> "AgentSession": """Updates an existing agent session. Arguments: - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. Returns: The updated AgentSession object. @@ -61,8 +64,10 @@ def prompt( prompt: The prompt to send to the agent. enable_trace: Whether to enable trace for the prompt. print_response: Whether to print the response to the console. - newer_than: The timestamp to get trace results newer than. Defaults to None (all results). - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + newer_than: The timestamp to get trace results newer than. + Defaults to None (all results). + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. """ return None @@ -75,7 +80,8 @@ def register(self, *, synapse_client: Optional[Synapse] = None) -> "Agent": """Registers an agent with the Synapse API. If agent exists, it will be retrieved. Arguments: - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. Returns: The registered or existing Agent object. @@ -86,7 +92,8 @@ def get(self, *, synapse_client: Optional[Synapse] = None) -> "Agent": """Gets an existing agent. Arguments: - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. Returns: The existing Agent object. @@ -103,9 +110,10 @@ def start_session( Adds the session to the Agent's sessions dictionary and sets it as the current session. Arguments: access_level: The access level of the agent session. - Must be one of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, or WRITE_YOUR_PRIVATE_DATA. - Defaults to PUBLICLY_ACCESSIBLE. - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + Must be one of PUBLICLY_ACCESSIBLE, READ_YOUR_PRIVATE_DATA, + or WRITE_YOUR_PRIVATE_DATA. Defaults to PUBLICLY_ACCESSIBLE. + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. Returns: The new AgentSession object. @@ -145,8 +153,11 @@ def prompt( prompt: The prompt to send to the agent. enable_trace: Whether to enable trace for the prompt. print_response: Whether to print the response to the console. - session_id: The ID of the session to send the prompt to. If None, the current session will be used. - newer_than: The timestamp to get trace results newer than. Defaults to None (all results). - synapse_client: The Synapse client to use for the request. If None, the default client will be used. + session_id: The ID of the session to send the prompt to. + If None, the current session will be used. + newer_than: The timestamp to get trace results newer than. + Defaults to None (all results). + synapse_client: The Synapse client to use for the request. + If None, the default client will be used. """ return None diff --git a/tests/integration/synapseclient/models/async/test_agent_async.py b/tests/integration/synapseclient/models/async/test_agent_async.py index cc47214a2..21745aec3 100644 --- a/tests/integration/synapseclient/models/async/test_agent_async.py +++ b/tests/integration/synapseclient/models/async/test_agent_async.py @@ -83,7 +83,8 @@ async def test_get(self) -> None: assert new_session == agent_session async def test_update(self) -> None: - # GIVEN an agent session with a valid agent registration id and access level set + # GIVEN an agent session with a valid agent + # registration id and access level set agent_session = AgentSession( agent_registration_id=AGENT_REGISTRATION_ID, access_level=AgentSessionAccessLevel.PUBLICLY_ACCESSIBLE, diff --git a/tests/unit/synapseclient/mixins/async/unit_test_asynchronous_job.py b/tests/unit/synapseclient/mixins/async/unit_test_asynchronous_job.py index a7fccb436..056976dcc 100644 --- a/tests/unit/synapseclient/mixins/async/unit_test_asynchronous_job.py +++ b/tests/unit/synapseclient/mixins/async/unit_test_asynchronous_job.py @@ -120,7 +120,10 @@ async def test_get_job_async_when_job_fails(self) -> None: ): with pytest.raises( SynapseError, - match=f"{self.failed_job_status.error_message}\n{self.failed_job_status.error_details}", + match=( + f"{self.failed_job_status.error_message}\n" + f"{self.failed_job_status.error_details}" + ), ): # WHEN I call get_job_async # AND the job fails in the Synapse API diff --git a/tests/unit/synapseclient/models/async/unit_test_agent_async.py b/tests/unit/synapseclient/models/async/unit_test_agent_async.py index 8765e8d5c..cb0f405d0 100644 --- a/tests/unit/synapseclient/models/async/unit_test_agent_async.py +++ b/tests/unit/synapseclient/models/async/unit_test_agent_async.py @@ -73,7 +73,8 @@ async def test_post_exchange_async_trace_enabled(self): new_callable=AsyncMock, return_value=self.trace_response, ) as mock_get_trace: - # WHEN I call _post_exchange_async on an initialized AgentPrompt with enable_trace=True + # WHEN I call _post_exchange_async on an + # initialized AgentPrompt with enable_trace=True await self.agent_prompt._post_exchange_async(synapse_client=self.syn) # THEN the mock_get_trace should have been called with the correct arguments mock_get_trace.assert_called_once_with( @@ -91,7 +92,8 @@ async def test_post_exchange_async_trace_disabled(self): return_value=self.trace_response, ) as mock_get_trace: self.agent_prompt.enable_trace = False - # WHEN I call _post_exchange_async on an initialized AgentPrompt with enable_trace=False + # WHEN I call _post_exchange_async on an + # initialized AgentPrompt with enable_trace=False await self.agent_prompt._post_exchange_async(synapse_client=self.syn) # THEN the mock_get_trace should not have been called mock_get_trace.assert_not_called() @@ -122,7 +124,8 @@ async def test_send_job_and_wait_async(self): await self.agent_prompt.send_job_and_wait_async( post_exchange_args={"foo": "bar"}, synapse_client=self.syn ) - # THEN the mock_send_job_and_wait_async should have been called with the correct arguments + # THEN the mock_send_job_and_wait_async should + # have been called with the correct arguments mock_send_job_and_wait_async.assert_called_once_with( request=mock_to_synapse_request.return_value, request_type=self.agent_prompt.concrete_type, @@ -325,9 +328,11 @@ async def test_prompt_trace_enabled_print_response(self) -> None: newer_than=0, synapse_client=self.syn, ) - # THEN the result should be an AgentPrompt with the correct values appended to the chat history + # THEN the result should be an AgentPrompt with the correct + # values appended to the chat history assert self.test_prompt_trace_enabled in self.test_session.chat_history - # AND send_job_and_wait_async should have been called once with the correct arguments + # AND send_job_and_wait_async should have + # been called once with the correct arguments mock_send_job_and_wait_async.assert_called_once_with( synapse_client=self.syn, post_exchange_args={"newer_than": 0} ) @@ -356,9 +361,11 @@ async def test_prompt_trace_disabled_no_print(self) -> None: newer_than=0, synapse_client=self.syn, ) - # THEN the result should be an AgentPrompt with the correct values appended to the chat history + # THEN the result should be an AgentPrompt with the + # correct values appended to the chat history assert self.test_prompt_trace_disabled in self.test_session.chat_history - # AND send_job_and_wait_async should have been called once with the correct arguments + # AND send_job_and_wait_async should have been + # called once with the correct arguments mock_send_job_and_wait_async.assert_called_once_with( synapse_client=self.syn, post_exchange_args={"newer_than": 0} )