|
15 | 15 |
|
16 | 16 | # Code generated by the Google Gen AI SDK generator DO NOT EDIT. |
17 | 17 |
|
18 | | -import base64 |
19 | 18 | import functools |
20 | 19 | import json |
21 | 20 | import logging |
| 21 | +import mimetypes |
22 | 22 | from typing import Any, Iterator, Optional, Union |
23 | 23 | from urllib.parse import urlencode |
24 | 24 |
|
@@ -618,20 +618,52 @@ def execute_code( |
618 | 618 | Returns: |
619 | 619 | ExecuteSandboxEnvironmentResponse: The response from executing the code. |
620 | 620 | """ |
621 | | - json_string = json.dumps(input_data) |
622 | | - |
623 | | - base64_bytes = base64.b64encode(json_string.encode("utf-8")) |
624 | | - base64_string = base64_bytes.decode("utf-8") |
| 621 | + input_chunks = [] |
| 622 | + |
| 623 | + if input_data.get("code") is not None: |
| 624 | + code = input_data.get("code", "") |
| 625 | + json_code = json.dumps({"code": code}).encode("utf-8") |
| 626 | + input_chunks.append( |
| 627 | + types.Chunk( |
| 628 | + mime_type="application/json", |
| 629 | + data=json_code, |
| 630 | + ) |
| 631 | + ) |
625 | 632 |
|
626 | | - # Only single JSON input is supported for now. |
627 | | - inputs = [{"mime_type": "application/json", "data": base64_string}] |
| 633 | + for file in input_data.get("files", []): |
| 634 | + file_name = file.get("name", "") |
| 635 | + input_chunks.append( |
| 636 | + types.Chunk( |
| 637 | + mime_type=file.get("mimeType", ""), |
| 638 | + data=file.get("content", b""), |
| 639 | + metadata={"attributes": {"file_name": file_name.encode("utf-8")}}, |
| 640 | + ) |
| 641 | + ) |
628 | 642 |
|
629 | 643 | response = self._execute_code( |
630 | 644 | name=name, |
631 | | - inputs=inputs, |
| 645 | + inputs=input_chunks, |
632 | 646 | config=config, |
633 | 647 | ) |
634 | 648 |
|
| 649 | + output_chunks = [] |
| 650 | + for output in response.outputs: |
| 651 | + if output.mime_type is None: |
| 652 | + # if mime_type is not available, try to guess the mime_type from the file_name. |
| 653 | + if ( |
| 654 | + output.metadata is not None |
| 655 | + and output.metadata.attributes is not None |
| 656 | + ): |
| 657 | + file_name = output.metadata.attributes.get("file_name", b"").decode( |
| 658 | + "utf-8" |
| 659 | + ) |
| 660 | + mime_type, _ = mimetypes.guess_type(file_name) |
| 661 | + output.mime_type = mime_type |
| 662 | + |
| 663 | + output_chunks.append(output) |
| 664 | + |
| 665 | + response = types.ExecuteSandboxEnvironmentResponse(outputs=output_chunks) |
| 666 | + |
635 | 667 | return response |
636 | 668 |
|
637 | 669 | def get( |
|
0 commit comments