|
1 | 1 | import csv
|
| 2 | +import json |
2 | 3 | import logging
|
3 | 4 | import os
|
4 | 5 | import shutil
|
@@ -86,7 +87,7 @@ class Component(ComponentBase):
|
86 | 87 | def __init__(self):
|
87 | 88 | super().__init__()
|
88 | 89 |
|
89 |
| - def run(self, return_data=False): |
| 90 | + def run(self, return_json=False): |
90 | 91 | self.validate_configuration_parameters(REQUIRED_PARAMETERS)
|
91 | 92 | self.validate_image_parameters(REQUIRED_IMAGE_PARS)
|
92 | 93 |
|
@@ -130,7 +131,15 @@ def run(self, return_data=False):
|
130 | 131 | logging.debug([result for result in results])
|
131 | 132 | logging.info(f"Downloaded {total_records} records in total")
|
132 | 133 |
|
133 |
| - if return_data: |
| 134 | + if return_json: |
| 135 | + for result in results: |
| 136 | + if "file" not in result: |
| 137 | + result["status"] = "fileError" |
| 138 | + continue |
| 139 | + result["records"] = list(self._csv_result_to_dict(result.pop("file"))) |
| 140 | + result["status"] = "ok" |
| 141 | + with open("response.json", "w") as f: |
| 142 | + json.dump(results, f) |
134 | 143 | return results
|
135 | 144 |
|
136 | 145 | # remove headers and get columns
|
@@ -180,6 +189,12 @@ def _fix_header_from_csv(results: list[dict]) -> list[str]:
|
180 | 189 | os.replace(temp_file_path, result_file_path)
|
181 | 190 | return expected_header
|
182 | 191 |
|
| 192 | + def _csv_result_to_dict(self, filename: str): |
| 193 | + with open(filename) as f: |
| 194 | + reader = csv.DictReader(f) |
| 195 | + for row in reader: |
| 196 | + yield row |
| 197 | + |
183 | 198 | def set_proxy(self) -> None:
|
184 | 199 | """Sets proxy if defined"""
|
185 | 200 | proxy_config = self.configuration.parameters.get(KEY_PROXY, {})
|
@@ -622,12 +637,105 @@ def load_possible_primary_keys(self) -> list[SelectElement]:
|
622 | 637 | else:
|
623 | 638 | raise UserException(f"Invalid {KEY_QUERY_TYPE}")
|
624 | 639 |
|
625 |
| - # @sync_action("runComponent") |
626 |
| - # def sync_run_component(self): |
627 |
| - # """ |
628 |
| - # Run the component as a sync action |
629 |
| - # """ |
630 |
| - # return self.run(return_data=True) |
| 640 | + @sync_action("runSkill") |
| 641 | + def sync_run_component(self): |
| 642 | + """ |
| 643 | + Run the component as a skill for an AI agent |
| 644 | + """ |
| 645 | + return self.run(return_json=True) |
| 646 | + |
| 647 | + @sync_action("listSkills") |
| 648 | + def list_skills(self): |
| 649 | + return [ |
| 650 | + { |
| 651 | + "name": "Get Salesforce contacts", |
| 652 | + "description": "Returns list of Salesforce contacts.", |
| 653 | + "parameters": [ |
| 654 | + { |
| 655 | + "name": "action", |
| 656 | + "type": "string", |
| 657 | + "required": True, |
| 658 | + "enum": ["runSkill"], |
| 659 | + }, |
| 660 | + { |
| 661 | + "name": "configData", |
| 662 | + "type": "object", |
| 663 | + "required": True, |
| 664 | + "parameters": [ |
| 665 | + { |
| 666 | + "name": "object", |
| 667 | + "type": "string", |
| 668 | + "required": True, |
| 669 | + "enum": ["contacts"], |
| 670 | + "description": "String identifer of the agent skill to run.", |
| 671 | + }, |
| 672 | + { |
| 673 | + "name": "username", |
| 674 | + "type": "string", |
| 675 | + "required": True, |
| 676 | + "description": "The Salesforce account username.", |
| 677 | + }, |
| 678 | + { |
| 679 | + "name": "#password", |
| 680 | + "type": "string", |
| 681 | + "required": True, |
| 682 | + "description": "The Salesforce account password.", |
| 683 | + }, |
| 684 | + { |
| 685 | + "name": "#securityToken", |
| 686 | + "type": "string", |
| 687 | + "required": True, |
| 688 | + "description": "Keboola project security token.", |
| 689 | + }, |
| 690 | + ], |
| 691 | + }, |
| 692 | + ], |
| 693 | + }, |
| 694 | + { |
| 695 | + "name": "Get Salesforce reports", |
| 696 | + "description": "Returns list of Salesforce reports.", |
| 697 | + "parameters": [ |
| 698 | + { |
| 699 | + "name": "action", |
| 700 | + "type": "string", |
| 701 | + "required": True, |
| 702 | + "enum": ["runSkill"], |
| 703 | + }, |
| 704 | + { |
| 705 | + "name": "configData", |
| 706 | + "type": "object", |
| 707 | + "required": True, |
| 708 | + "parameters": [ |
| 709 | + { |
| 710 | + "name": "object", |
| 711 | + "type": "string", |
| 712 | + "required": True, |
| 713 | + "enum": ["reports"], |
| 714 | + "description": "String identifer of the agent skill to run.", |
| 715 | + }, |
| 716 | + { |
| 717 | + "name": "username", |
| 718 | + "type": "string", |
| 719 | + "required": True, |
| 720 | + "description": "The Salesforce account username.", |
| 721 | + }, |
| 722 | + { |
| 723 | + "name": "#password", |
| 724 | + "type": "string", |
| 725 | + "required": True, |
| 726 | + "description": "The Salesforce account password.", |
| 727 | + }, |
| 728 | + { |
| 729 | + "name": "#securityToken", |
| 730 | + "type": "string", |
| 731 | + "required": True, |
| 732 | + "description": "Keboola project security token.", |
| 733 | + }, |
| 734 | + ], |
| 735 | + }, |
| 736 | + ], |
| 737 | + }, |
| 738 | + ] |
631 | 739 |
|
632 | 740 | def _get_object_name_from_custom_query(self) -> str:
|
633 | 741 | params = self.configuration.parameters
|
|
0 commit comments