Generates a valid media artifact of specified type. Args: media_type: One of 'image', 'audio', 'video'. ctx: The tool context for saving artifacts.
(media_type: str, ctx: Context)
| 157 | |
| 158 | |
| 159 | async def generate_media_artifact(media_type: str, ctx: Context) -> dict: |
| 160 | """Generates a valid media artifact of specified type. |
| 161 | |
| 162 | Args: |
| 163 | media_type: One of 'image', 'audio', 'video'. |
| 164 | ctx: The tool context for saving artifacts. |
| 165 | """ |
| 166 | |
| 167 | with tempfile.TemporaryDirectory() as tmpdir: |
| 168 | if media_type == "image": |
| 169 | mime_type = "image/bmp" |
| 170 | file_path = os.path.join(tmpdir, "sample.bmp") |
| 171 | generate_bmp(file_path) |
| 172 | filename = "sample_image.bmp" |
| 173 | elif media_type == "audio": |
| 174 | mime_type = "audio/wav" |
| 175 | file_path = os.path.join(tmpdir, "sample.wav") |
| 176 | generate_wav(file_path) |
| 177 | filename = "sample_audio.wav" |
| 178 | elif media_type == "video": |
| 179 | mime_type = "video/mp4" |
| 180 | file_path = os.path.join(tmpdir, "sample.mp4") |
| 181 | try: |
| 182 | generate_video(file_path) |
| 183 | except (ImportError, RuntimeError) as e: |
| 184 | return {"error": str(e)} |
| 185 | filename = "sample_video.mp4" |
| 186 | else: |
| 187 | return {"error": f"Unsupported media type: {media_type}"} |
| 188 | |
| 189 | with open(file_path, "rb") as f: |
| 190 | data = f.read() |
| 191 | |
| 192 | version = await ctx.save_artifact( |
| 193 | filename, |
| 194 | types.Part.from_bytes(data=data, mime_type=mime_type), |
| 195 | ) |
| 196 | |
| 197 | return { |
| 198 | "message": ( |
| 199 | f"Media artifact '{filename}' generated and saved (version" |
| 200 | f" {version})." |
| 201 | ), |
| 202 | "filename": filename, |
| 203 | "version": version, |
| 204 | } |
| 205 | |
| 206 | |
| 207 | root_agent = Agent( |
nothing calls this directly
no test coverage detected