Sets up the context for a resumed invocation. Args: session: The session to set up the invocation context for. new_message: The new message to process and append to the session. invocation_id: The invocation id to resume. run_config: The run config of the agent. st
(
self,
*,
session: Session,
new_message: Optional[types.Content],
invocation_id: Optional[str],
run_config: RunConfig,
state_delta: Optional[dict[str, Any]],
)
| 1867 | return invocation_context |
| 1868 | |
| 1869 | async def _setup_context_for_resumed_invocation( |
| 1870 | self, |
| 1871 | *, |
| 1872 | session: Session, |
| 1873 | new_message: Optional[types.Content], |
| 1874 | invocation_id: Optional[str], |
| 1875 | run_config: RunConfig, |
| 1876 | state_delta: Optional[dict[str, Any]], |
| 1877 | ) -> InvocationContext: |
| 1878 | """Sets up the context for a resumed invocation. |
| 1879 | |
| 1880 | Args: |
| 1881 | session: The session to set up the invocation context for. |
| 1882 | new_message: The new message to process and append to the session. |
| 1883 | invocation_id: The invocation id to resume. |
| 1884 | run_config: The run config of the agent. |
| 1885 | state_delta: Optional state changes to apply to the session. |
| 1886 | |
| 1887 | Returns: |
| 1888 | The invocation context for the resumed invocation. |
| 1889 | |
| 1890 | Raises: |
| 1891 | ValueError: If the session has no events to resume; If no user message is |
| 1892 | available for resuming the invocation; Or if the app is not resumable. |
| 1893 | """ |
| 1894 | if not session.events: |
| 1895 | raise ValueError(f'Session {session.id} has no events to resume.') |
| 1896 | |
| 1897 | # Step 1: Maybe retrieve a previous user message for the invocation. |
| 1898 | user_message = new_message or self._find_user_message_for_invocation( |
| 1899 | session.events, invocation_id |
| 1900 | ) |
| 1901 | if not user_message: |
| 1902 | raise ValueError( |
| 1903 | f'No user message available for resuming invocation: {invocation_id}' |
| 1904 | ) |
| 1905 | # Step 2: Create invocation context. |
| 1906 | invocation_context = self._new_invocation_context( |
| 1907 | session, |
| 1908 | new_message=user_message, |
| 1909 | run_config=run_config, |
| 1910 | invocation_id=invocation_id, |
| 1911 | ) |
| 1912 | # Step 3: Maybe handle new message. |
| 1913 | if new_message: |
| 1914 | await self._handle_new_message( |
| 1915 | session=invocation_context.session, |
| 1916 | new_message=user_message, |
| 1917 | invocation_context=invocation_context, |
| 1918 | run_config=run_config, |
| 1919 | state_delta=state_delta, |
| 1920 | ) |
| 1921 | # Step 4: Populate agent states for the current invocation. |
| 1922 | invocation_context.populate_invocation_agent_states() |
| 1923 | # Step 5: Set agent to run for the invocation. |
| 1924 | # |
| 1925 | # If the root agent is not found in end_of_agents, it means the invocation |
| 1926 | # started from a sub-agent and paused on a sub-agent. |
no test coverage detected