|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Utility functions for handling artifact URIs.""" |
| 15 | + |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import re |
| 19 | +from typing import NamedTuple |
| 20 | +from typing import Optional |
| 21 | + |
| 22 | +from google.genai import types |
| 23 | + |
| 24 | + |
| 25 | +class ParsedArtifactUri(NamedTuple): |
| 26 | + """The result of parsing an artifact URI.""" |
| 27 | + |
| 28 | + app_name: str |
| 29 | + user_id: str |
| 30 | + session_id: Optional[str] |
| 31 | + filename: str |
| 32 | + version: int |
| 33 | + |
| 34 | + |
| 35 | +_SESSION_SCOPED_ARTIFACT_URI_RE = re.compile( |
| 36 | + r"artifact://apps/([^/]+)/users/([^/]+)/sessions/([^/]+)/artifacts/([^/]+)/versions/(\d+)" |
| 37 | +) |
| 38 | +_USER_SCOPED_ARTIFACT_URI_RE = re.compile( |
| 39 | + r"artifact://apps/([^/]+)/users/([^/]+)/artifacts/([^/]+)/versions/(\d+)" |
| 40 | +) |
| 41 | + |
| 42 | + |
| 43 | +def parse_artifact_uri(uri: str) -> Optional[ParsedArtifactUri]: |
| 44 | + """Parses an artifact URI. |
| 45 | +
|
| 46 | + Args: |
| 47 | + uri: The artifact URI to parse. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + A ParsedArtifactUri if parsing is successful, None otherwise. |
| 51 | + """ |
| 52 | + if not uri or not uri.startswith("artifact://"): |
| 53 | + return None |
| 54 | + |
| 55 | + match = _SESSION_SCOPED_ARTIFACT_URI_RE.match(uri) |
| 56 | + if match: |
| 57 | + return ParsedArtifactUri( |
| 58 | + app_name=match.group(1), |
| 59 | + user_id=match.group(2), |
| 60 | + session_id=match.group(3), |
| 61 | + filename=match.group(4), |
| 62 | + version=int(match.group(5)), |
| 63 | + ) |
| 64 | + |
| 65 | + match = _USER_SCOPED_ARTIFACT_URI_RE.match(uri) |
| 66 | + if match: |
| 67 | + return ParsedArtifactUri( |
| 68 | + app_name=match.group(1), |
| 69 | + user_id=match.group(2), |
| 70 | + session_id=None, |
| 71 | + filename=match.group(3), |
| 72 | + version=int(match.group(4)), |
| 73 | + ) |
| 74 | + |
| 75 | + return None |
| 76 | + |
| 77 | + |
| 78 | +def get_artifact_uri( |
| 79 | + app_name: str, |
| 80 | + user_id: str, |
| 81 | + filename: str, |
| 82 | + version: int, |
| 83 | + session_id: Optional[str] = None, |
| 84 | +) -> str: |
| 85 | + """Constructs an artifact URI. |
| 86 | +
|
| 87 | + Args: |
| 88 | + app_name: The name of the application. |
| 89 | + user_id: The ID of the user. |
| 90 | + filename: The name of the artifact file. |
| 91 | + version: The version of the artifact. |
| 92 | + session_id: The ID of the session. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + The constructed artifact URI. |
| 96 | + """ |
| 97 | + if session_id: |
| 98 | + return f"artifact://apps/{app_name}/users/{user_id}/sessions/{session_id}/artifacts/{filename}/versions/{version}" |
| 99 | + else: |
| 100 | + return f"artifact://apps/{app_name}/users/{user_id}/artifacts/{filename}/versions/{version}" |
| 101 | + |
| 102 | + |
| 103 | +def is_artifact_ref(artifact: types.Part) -> bool: |
| 104 | + """Checks if an artifact part is an artifact reference. |
| 105 | +
|
| 106 | + Args: |
| 107 | + artifact: The artifact part to check. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + True if the artifact part is an artifact reference, False otherwise. |
| 111 | + """ |
| 112 | + return bool( |
| 113 | + artifact.file_data |
| 114 | + and artifact.file_data.file_uri |
| 115 | + and artifact.file_data.file_uri.startswith("artifact://") |
| 116 | + ) |
0 commit comments