Skip to content

Commit b0a665b

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add async artifact management to AdkApp
PiperOrigin-RevId: 923400255
1 parent 47d8fb0 commit b0a665b

6 files changed

Lines changed: 1116 additions & 7 deletions

File tree

agentplatform/agent_engines/templates/adk.py

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@
3737
from google.auth.transport import requests as requests_auth
3838

3939
if TYPE_CHECKING:
40+
try:
41+
from google.genai import types
42+
43+
types = types
44+
except (ImportError, AttributeError):
45+
types = Any
46+
4047
try:
4148
from google.adk.events.event import Event
4249

@@ -1788,6 +1795,253 @@ async def async_search_memory(self, *, user_id: str, query: str):
17881795
query=query,
17891796
)
17901797

1798+
async def async_save_artifact(
1799+
self,
1800+
*,
1801+
user_id: str,
1802+
filename: str,
1803+
artifact: Union["types.Part", Dict[str, Any]],
1804+
session_id: Optional[str] = None,
1805+
custom_metadata: Optional[Dict[str, Any]] = None,
1806+
**kwargs,
1807+
):
1808+
"""Saves an artifact to the artifact service storage.
1809+
1810+
Args:
1811+
user_id (str):
1812+
Required. The ID of the user.
1813+
filename (str):
1814+
Required. The filename of the artifact.
1815+
artifact (Union[types.Part, Dict[str, Any]]):
1816+
Required. The artifact to save.
1817+
session_id (Optional[str]):
1818+
Optional. The ID of the session.
1819+
custom_metadata (Optional[Dict[str, Any]]):
1820+
Optional. Custom metadata to associate with the artifact.
1821+
**kwargs (dict[str, Any]):
1822+
Optional. Additional keyword arguments to pass to the
1823+
artifact service.
1824+
1825+
Returns:
1826+
int: The revision ID.
1827+
"""
1828+
if not self._tmpl_attrs.get("artifact_service"):
1829+
self.set_up()
1830+
return await self._tmpl_attrs.get("artifact_service").save_artifact(
1831+
app_name=self._app_name(),
1832+
user_id=user_id,
1833+
filename=filename,
1834+
artifact=artifact,
1835+
session_id=session_id,
1836+
custom_metadata=custom_metadata,
1837+
**kwargs,
1838+
)
1839+
1840+
async def async_load_artifact(
1841+
self,
1842+
*,
1843+
user_id: str,
1844+
filename: str,
1845+
session_id: Optional[str] = None,
1846+
version: Optional[int] = None,
1847+
**kwargs,
1848+
):
1849+
"""Gets an artifact from the artifact service storage.
1850+
1851+
Args:
1852+
user_id (str):
1853+
Required. The ID of the user.
1854+
filename (str):
1855+
Required. The filename of the artifact.
1856+
session_id (Optional[str]):
1857+
Optional. The ID of the session.
1858+
version (Optional[int]):
1859+
Optional. The version of the artifact.
1860+
**kwargs (dict[str, Any]):
1861+
Optional. Additional keyword arguments to pass to the
1862+
artifact service.
1863+
1864+
Returns:
1865+
Optional[types.Part]: The artifact or None if not found.
1866+
"""
1867+
if not self._tmpl_attrs.get("artifact_service"):
1868+
self.set_up()
1869+
return await self._tmpl_attrs.get("artifact_service").load_artifact(
1870+
app_name=self._app_name(),
1871+
user_id=user_id,
1872+
filename=filename,
1873+
session_id=session_id,
1874+
version=version,
1875+
**kwargs,
1876+
)
1877+
1878+
async def async_list_artifact_keys(
1879+
self,
1880+
*,
1881+
user_id: str,
1882+
session_id: Optional[str] = None,
1883+
**kwargs,
1884+
):
1885+
"""Lists all the artifact filenames within a session.
1886+
1887+
Args:
1888+
user_id (str):
1889+
Required. The ID of the user.
1890+
session_id (Optional[str]):
1891+
Optional. The ID of the session.
1892+
**kwargs (dict[str, Any]):
1893+
Optional. Additional keyword arguments to pass to the
1894+
artifact service.
1895+
1896+
Returns:
1897+
list[str]: A list of artifact filenames.
1898+
"""
1899+
if not self._tmpl_attrs.get("artifact_service"):
1900+
self.set_up()
1901+
return await self._tmpl_attrs.get("artifact_service").list_artifact_keys(
1902+
app_name=self._app_name(),
1903+
user_id=user_id,
1904+
session_id=session_id,
1905+
**kwargs,
1906+
)
1907+
1908+
async def async_delete_artifact(
1909+
self,
1910+
*,
1911+
user_id: str,
1912+
filename: str,
1913+
session_id: Optional[str] = None,
1914+
**kwargs,
1915+
):
1916+
"""Deletes an artifact.
1917+
1918+
Args:
1919+
user_id (str):
1920+
Required. The ID of the user.
1921+
filename (str):
1922+
Required. The filename of the artifact.
1923+
session_id (Optional[str]):
1924+
Optional. The ID of the session.
1925+
**kwargs (dict[str, Any]):
1926+
Optional. Additional keyword arguments to pass to the
1927+
artifact service.
1928+
"""
1929+
if not self._tmpl_attrs.get("artifact_service"):
1930+
self.set_up()
1931+
await self._tmpl_attrs.get("artifact_service").delete_artifact(
1932+
app_name=self._app_name(),
1933+
user_id=user_id,
1934+
filename=filename,
1935+
session_id=session_id,
1936+
**kwargs,
1937+
)
1938+
1939+
async def async_list_versions(
1940+
self,
1941+
*,
1942+
user_id: str,
1943+
filename: str,
1944+
session_id: Optional[str] = None,
1945+
**kwargs,
1946+
):
1947+
"""Lists all versions of an artifact.
1948+
1949+
Args:
1950+
user_id (str):
1951+
Required. The ID of the user.
1952+
filename (str):
1953+
Required. The filename of the artifact.
1954+
session_id (Optional[str]):
1955+
Optional. The ID of the session.
1956+
**kwargs (dict[str, Any]):
1957+
Optional. Additional keyword arguments to pass to the
1958+
artifact service.
1959+
1960+
Returns:
1961+
list[int]: A list of all available versions of the artifact.
1962+
"""
1963+
if not self._tmpl_attrs.get("artifact_service"):
1964+
self.set_up()
1965+
return await self._tmpl_attrs.get("artifact_service").list_versions(
1966+
app_name=self._app_name(),
1967+
user_id=user_id,
1968+
filename=filename,
1969+
session_id=session_id,
1970+
**kwargs,
1971+
)
1972+
1973+
async def async_list_artifact_versions(
1974+
self,
1975+
*,
1976+
user_id: str,
1977+
filename: str,
1978+
session_id: Optional[str] = None,
1979+
**kwargs,
1980+
):
1981+
"""Lists all versions and their metadata for a specific artifact.
1982+
1983+
Args:
1984+
user_id (str):
1985+
Required. The ID of the user.
1986+
filename (str):
1987+
Required. The filename of the artifact.
1988+
session_id (Optional[str]):
1989+
Optional. The ID of the session.
1990+
**kwargs (dict[str, Any]):
1991+
Optional. Additional keyword arguments to pass to the
1992+
artifact service.
1993+
1994+
Returns:
1995+
list[ArtifactVersion]: A list of ArtifactVersion objects.
1996+
"""
1997+
if not self._tmpl_attrs.get("artifact_service"):
1998+
self.set_up()
1999+
return await self._tmpl_attrs.get("artifact_service").list_artifact_versions(
2000+
app_name=self._app_name(),
2001+
user_id=user_id,
2002+
filename=filename,
2003+
session_id=session_id,
2004+
**kwargs,
2005+
)
2006+
2007+
async def async_get_artifact_version(
2008+
self,
2009+
*,
2010+
user_id: str,
2011+
filename: str,
2012+
session_id: Optional[str] = None,
2013+
version: Optional[int] = None,
2014+
**kwargs,
2015+
):
2016+
"""Gets the metadata for a specific version of an artifact.
2017+
2018+
Args:
2019+
user_id (str):
2020+
Required. The ID of the user.
2021+
filename (str):
2022+
Required. The filename of the artifact.
2023+
session_id (Optional[str]):
2024+
Optional. The ID of the session.
2025+
version (Optional[int]):
2026+
Optional. The version number of the artifact.
2027+
**kwargs (dict[str, Any]):
2028+
Optional. Additional keyword arguments to pass to the
2029+
artifact service.
2030+
2031+
Returns:
2032+
Optional[ArtifactVersion]: An ArtifactVersion object or None.
2033+
"""
2034+
if not self._tmpl_attrs.get("artifact_service"):
2035+
self.set_up()
2036+
return await self._tmpl_attrs.get("artifact_service").get_artifact_version(
2037+
app_name=self._app_name(),
2038+
user_id=user_id,
2039+
filename=filename,
2040+
session_id=session_id,
2041+
version=version,
2042+
**kwargs,
2043+
)
2044+
17912045
def register_operations(self) -> Dict[str, List[str]]:
17922046
"""Registers the operations of the ADK application."""
17932047
return {
@@ -1804,6 +2058,13 @@ def register_operations(self) -> Dict[str, List[str]]:
18042058
"async_delete_session",
18052059
"async_add_session_to_memory",
18062060
"async_search_memory",
2061+
"async_save_artifact",
2062+
"async_load_artifact",
2063+
"async_list_artifact_keys",
2064+
"async_delete_artifact",
2065+
"async_list_versions",
2066+
"async_list_artifact_versions",
2067+
"async_get_artifact_version",
18072068
],
18082069
"stream": ["stream_query"],
18092070
"async_stream": [

0 commit comments

Comments
 (0)