1
1
import csv
2
+ import json
2
3
import logging
3
4
import os
4
5
import shutil
60
61
61
62
DEFAULT_LOGIN_METHOD = "security_token"
62
63
64
+ # the listSkills response shall be generated based on user config in a very near future,
65
+ # so I just hardcoded it here for now
66
+ LIST_SKILLS_STATIC_RESPONSE = [
67
+ {
68
+ "name" : "Get Salesforce contacts" ,
69
+ "description" : "Returns list of Salesforce contacts." ,
70
+ "parameters" : [
71
+ {
72
+ "name" : "action" ,
73
+ "type" : "string" ,
74
+ "required" : True ,
75
+ "enum" : ["runSkill" ],
76
+ },
77
+ {
78
+ "name" : "parameters" ,
79
+ "type" : "object" ,
80
+ "required" : True ,
81
+ "parameters" : {
82
+ "name" : "configData" ,
83
+ "type" : "object" ,
84
+ "required" : True ,
85
+ "parameters" : [
86
+ {
87
+ "name" : "object" ,
88
+ "type" : "string" ,
89
+ "required" : True ,
90
+ "enum" : ["Contact" ],
91
+ "description" : "String identifer of the agent skill to run." ,
92
+ },
93
+ ],
94
+ },
95
+ },
96
+ ],
97
+ "response" : {
98
+ "type" : "object" ,
99
+ "properties" : {
100
+ "status" : {
101
+ "title" : "Response status" ,
102
+ "type" : "string" ,
103
+ "enum" : ["ok" , "error" ],
104
+ },
105
+ "number_of_records" : {
106
+ "title" : "Number of records in the response" ,
107
+ "type" : "integer" ,
108
+ },
109
+ "records" : {
110
+ "title" : "Salesforce records" ,
111
+ "type" : "array" ,
112
+ "items" : {
113
+ "type" : "object" ,
114
+ },
115
+ },
116
+ },
117
+ },
118
+ },
119
+ {
120
+ "name" : "Get Salesforce reports" ,
121
+ "description" : "Returns list of Salesforce reports." ,
122
+ "parameters" : [
123
+ {
124
+ "name" : "action" ,
125
+ "type" : "string" ,
126
+ "required" : True ,
127
+ "enum" : ["runSkill" ],
128
+ },
129
+ {
130
+ "name" : "parameters" ,
131
+ "type" : "object" ,
132
+ "required" : True ,
133
+ "parameters" : {
134
+ "name" : "configData" ,
135
+ "type" : "object" ,
136
+ "required" : True ,
137
+ "parameters" : [
138
+ {
139
+ "name" : "object" ,
140
+ "type" : "string" ,
141
+ "required" : True ,
142
+ "enum" : ["Report" ],
143
+ "description" : "String identifer of the agent skill to run." ,
144
+ },
145
+ ],
146
+ },
147
+ },
148
+ ],
149
+ "response" : {
150
+ "type" : "object" ,
151
+ "properties" : {
152
+ "status" : {
153
+ "title" : "Response status" ,
154
+ "type" : "string" ,
155
+ "enum" : ["ok" , "error" ],
156
+ },
157
+ "number_of_records" : {
158
+ "title" : "Number of records in the response" ,
159
+ "type" : "integer" ,
160
+ },
161
+ "records" : {
162
+ "title" : "Salesforce records" ,
163
+ "type" : "array" ,
164
+ "items" : {
165
+ "type" : "object" ,
166
+ },
167
+ },
168
+ },
169
+ },
170
+ },
171
+ ]
172
+
63
173
64
174
class LoginType (str , Enum ):
65
175
SECURITY_TOKEN_LOGIN = "security_token"
@@ -86,13 +196,16 @@ class Component(ComponentBase):
86
196
def __init__ (self ):
87
197
super ().__init__ ()
88
198
89
- def run (self , return_data = False ):
199
+ def run (self , return_json = False ):
90
200
self .validate_configuration_parameters (REQUIRED_PARAMETERS )
91
201
self .validate_image_parameters (REQUIRED_IMAGE_PARS )
92
202
93
203
start_run_time = str (datetime .now (timezone .utc ).strftime ("%Y-%m-%dT%H:%M:%S.000Z" ))
94
204
95
205
params = self .configuration .parameters
206
+
207
+ logging .info ("%s" , json .dumps (params , indent = 4 ))
208
+
96
209
loading_options = params .get (KEY_LOADING_OPTIONS , {})
97
210
98
211
bucket_name = params .get (KEY_BUCKET_NAME , self .get_bucket_name ())
@@ -130,7 +243,15 @@ def run(self, return_data=False):
130
243
logging .debug ([result for result in results ])
131
244
logging .info (f"Downloaded { total_records } records in total" )
132
245
133
- if return_data :
246
+ if return_json :
247
+ for result in results :
248
+ if "file" not in result :
249
+ result ["status" ] = "fileError"
250
+ continue
251
+ result ["records" ] = list (self ._csv_result_to_dict (result .pop ("file" )))
252
+ result ["status" ] = "ok"
253
+ with open ("response.json" , "w" ) as f :
254
+ json .dump (results , f )
134
255
return results
135
256
136
257
# remove headers and get columns
@@ -180,6 +301,12 @@ def _fix_header_from_csv(results: list[dict]) -> list[str]:
180
301
os .replace (temp_file_path , result_file_path )
181
302
return expected_header
182
303
304
+ def _csv_result_to_dict (self , filename : str ):
305
+ with open (filename ) as f :
306
+ reader = csv .DictReader (f )
307
+ for row in reader :
308
+ yield row
309
+
183
310
def set_proxy (self ) -> None :
184
311
"""Sets proxy if defined"""
185
312
proxy_config = self .configuration .parameters .get (KEY_PROXY , {})
@@ -622,12 +749,16 @@ def load_possible_primary_keys(self) -> list[SelectElement]:
622
749
else :
623
750
raise UserException (f"Invalid { KEY_QUERY_TYPE } " )
624
751
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)
752
+ @sync_action ("runSkill" )
753
+ def sync_run_component (self ):
754
+ """
755
+ Run the component as a skill for an AI agent
756
+ """
757
+ return self .run (return_json = True )
758
+
759
+ @sync_action ("listSkills" )
760
+ def list_skills (self ):
761
+ return LIST_SKILLS_STATIC_RESPONSE
631
762
632
763
def _get_object_name_from_custom_query (self ) -> str :
633
764
params = self .configuration .parameters
0 commit comments