sapiopycommons 2025.4.25a497__py3-none-any.whl → 2025.4.25a498__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sapiopycommons might be problematic. Click here for more details.
- sapiopycommons/callbacks/callback_util.py +116 -64
- sapiopycommons/callbacks/field_builder.py +2 -0
- sapiopycommons/customreport/auto_pagers.py +2 -1
- sapiopycommons/customreport/term_builder.py +1 -1
- sapiopycommons/datatype/pseudo_data_types.py +349 -326
- sapiopycommons/eln/experiment_cache.py +188 -0
- sapiopycommons/eln/experiment_handler.py +336 -719
- sapiopycommons/eln/experiment_step_factory.py +476 -0
- sapiopycommons/eln/plate_designer.py +7 -2
- sapiopycommons/eln/step_creation.py +236 -0
- sapiopycommons/files/file_util.py +4 -4
- sapiopycommons/general/accession_service.py +2 -2
- sapiopycommons/general/aliases.py +4 -1
- sapiopycommons/general/data_structure_util.py +115 -0
- sapiopycommons/general/sapio_links.py +4 -12
- sapiopycommons/processtracking/custom_workflow_handler.py +2 -1
- sapiopycommons/recordmodel/record_handler.py +357 -27
- sapiopycommons/rules/eln_rule_handler.py +8 -1
- sapiopycommons/rules/on_save_rule_handler.py +8 -1
- sapiopycommons/webhook/webhook_handlers.py +3 -0
- sapiopycommons/webhook/webservice_handlers.py +2 -2
- {sapiopycommons-2025.4.25a497.dist-info → sapiopycommons-2025.4.25a498.dist-info}/METADATA +2 -2
- sapiopycommons-2025.4.25a498.dist-info/RECORD +67 -0
- sapiopycommons/ai/__init__.py +0 -0
- sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.py +0 -123
- sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.pyi +0 -598
- sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/proto/step_output_pb2.py +0 -45
- sapiopycommons/ai/api/plan/proto/step_output_pb2.pyi +0 -42
- sapiopycommons/ai/api/plan/proto/step_output_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/proto/step_pb2.py +0 -43
- sapiopycommons/ai/api/plan/proto/step_pb2.pyi +0 -43
- sapiopycommons/ai/api/plan/proto/step_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/script/proto/script_pb2.py +0 -53
- sapiopycommons/ai/api/plan/script/proto/script_pb2.pyi +0 -99
- sapiopycommons/ai/api/plan/script/proto/script_pb2_grpc.py +0 -153
- sapiopycommons/ai/api/plan/tool/proto/entry_pb2.py +0 -57
- sapiopycommons/ai/api/plan/tool/proto/entry_pb2.pyi +0 -96
- sapiopycommons/ai/api/plan/tool/proto/entry_pb2_grpc.py +0 -24
- sapiopycommons/ai/api/plan/tool/proto/tool_pb2.py +0 -73
- sapiopycommons/ai/api/plan/tool/proto/tool_pb2.pyi +0 -242
- sapiopycommons/ai/api/plan/tool/proto/tool_pb2_grpc.py +0 -154
- sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.py +0 -39
- sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.pyi +0 -32
- sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2_grpc.py +0 -24
- sapiopycommons/ai/tool_of_tools.py +0 -917
- sapiopycommons/ai/tool_service_base.py +0 -556
- sapiopycommons/general/html_formatter.py +0 -456
- sapiopycommons-2025.4.25a497.dist-info/RECORD +0 -88
- {sapiopycommons-2025.4.25a497.dist-info → sapiopycommons-2025.4.25a498.dist-info}/WHEEL +0 -0
- {sapiopycommons-2025.4.25a497.dist-info → sapiopycommons-2025.4.25a498.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,556 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import json
|
|
4
|
-
import traceback
|
|
5
|
-
from abc import abstractmethod, ABC
|
|
6
|
-
from typing import Any
|
|
7
|
-
|
|
8
|
-
from grpc import ServicerContext
|
|
9
|
-
|
|
10
|
-
from sapiopycommons.ai.api.fielddefinitions.proto.velox_field_def_pb2 import VeloxFieldDefProto, FieldTypeProto, \
|
|
11
|
-
IntegerProperties, DoubleProperties, SelectionProperties, BooleanProperties
|
|
12
|
-
from sapiopycommons.general.aliases import FieldMap
|
|
13
|
-
from sapiopylib.rest.User import SapioUser
|
|
14
|
-
|
|
15
|
-
from sapiopycommons.ai.api.plan.tool.proto.entry_pb2 import StepEntryOutputData, StepEntryData, StepJsonData, DataType, \
|
|
16
|
-
StepImageData, StepTextData, StepCsvData, StepBinaryData, StepCsvHeaderRow, StepCsvRow
|
|
17
|
-
from sapiopycommons.ai.api.plan.tool.proto.tool_pb2 import ProcessStepRequest, ToolDetailsRequest, ToolDetailsResponse, \
|
|
18
|
-
ProcessStepResponse, ToolDetails, StepRecord, StepRecordFieldValue, ToolInputDetails, ToolOutputDetails
|
|
19
|
-
from sapiopycommons.ai.api.plan.tool.proto.tool_pb2_grpc import ToolServiceServicer
|
|
20
|
-
from sapiopycommons.ai.api.session.proto.sapio_conn_info_pb2 import SapioConnectionInfo, SapioUserSecretType
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
class SapioToolResult(ABC):
|
|
24
|
-
"""
|
|
25
|
-
A class representing a result from a Sapio tool. Instantiate one of the subclasses to create a result object.
|
|
26
|
-
"""
|
|
27
|
-
|
|
28
|
-
@abstractmethod
|
|
29
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
30
|
-
"""
|
|
31
|
-
Convert this SapioToolResult object to a StepEntryOutputData or list of StepRecord proto objects.
|
|
32
|
-
"""
|
|
33
|
-
pass
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
class BinaryResult(SapioToolResult):
|
|
37
|
-
"""
|
|
38
|
-
A class representing binary results from a Sapio tool.
|
|
39
|
-
"""
|
|
40
|
-
binary_data: list[bytes]
|
|
41
|
-
|
|
42
|
-
def __init__(self, binary_data: list[bytes]):
|
|
43
|
-
"""
|
|
44
|
-
:param binary_data: The binary data as a list of bytes. Each entry in the list represents a separate binary
|
|
45
|
-
entry.
|
|
46
|
-
"""
|
|
47
|
-
self.binary_data = binary_data
|
|
48
|
-
|
|
49
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
50
|
-
return StepEntryOutputData(
|
|
51
|
-
entry_data=StepEntryData(
|
|
52
|
-
dataType=DataType.BINARY,
|
|
53
|
-
binary_data=StepBinaryData(entries=self.binary_data)
|
|
54
|
-
)
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
class CsvResult(SapioToolResult):
|
|
59
|
-
"""
|
|
60
|
-
A class representing CSV results from a Sapio tool.
|
|
61
|
-
"""
|
|
62
|
-
csv_data: list[dict[str, Any]]
|
|
63
|
-
|
|
64
|
-
def __init__(self, csv_data: list[dict[str, Any]]):
|
|
65
|
-
"""
|
|
66
|
-
:param csv_data: The list of CSV data results, provided as a list of dictionaries of column name to value.
|
|
67
|
-
Each entry in the list represents a separate row in the CSV.
|
|
68
|
-
"""
|
|
69
|
-
self.csv_data = csv_data
|
|
70
|
-
|
|
71
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
72
|
-
return StepEntryOutputData(
|
|
73
|
-
entry_data=StepEntryData(
|
|
74
|
-
dataType=DataType.CSV,
|
|
75
|
-
csv_data=StepCsvData(
|
|
76
|
-
header=StepCsvHeaderRow(cells=self.csv_data[0].keys()),
|
|
77
|
-
entries=[StepCsvRow(cells=[str(x) for x in row.values()]) for row in self.csv_data]
|
|
78
|
-
)
|
|
79
|
-
) if self.csv_data else None
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
class FieldMapResult(SapioToolResult):
|
|
84
|
-
"""
|
|
85
|
-
A class representing field map results from a Sapio tool.
|
|
86
|
-
"""
|
|
87
|
-
field_maps: list[FieldMap]
|
|
88
|
-
|
|
89
|
-
def __init__(self, field_maps: list[FieldMap]):
|
|
90
|
-
"""
|
|
91
|
-
:param field_maps: A list of field maps, where each map is a dictionary of field names to values. Each entry
|
|
92
|
-
will create a new data record in the system.
|
|
93
|
-
"""
|
|
94
|
-
self.field_maps = field_maps
|
|
95
|
-
|
|
96
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
97
|
-
new_records: list[StepRecord] = []
|
|
98
|
-
for field_map in self.field_maps:
|
|
99
|
-
fields: dict[str, StepRecordFieldValue] = {}
|
|
100
|
-
for field, value in field_map.items():
|
|
101
|
-
field_value = StepRecordFieldValue()
|
|
102
|
-
if isinstance(value, str):
|
|
103
|
-
field_value.string_value = value
|
|
104
|
-
elif isinstance(value, int):
|
|
105
|
-
field_value.int_value = value
|
|
106
|
-
elif isinstance(value, float):
|
|
107
|
-
field_value.double_value = value
|
|
108
|
-
elif isinstance(value, bool):
|
|
109
|
-
field_value.bool_value = value
|
|
110
|
-
fields[field] = field_value
|
|
111
|
-
new_records.append(StepRecord(fields=fields))
|
|
112
|
-
return new_records
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
class ImageResult(SapioToolResult):
|
|
116
|
-
"""
|
|
117
|
-
A class representing image results from a Sapio tool.
|
|
118
|
-
"""
|
|
119
|
-
image_format: str
|
|
120
|
-
image_data: list[bytes]
|
|
121
|
-
|
|
122
|
-
def __init__(self, image_format: str, image_data: list[bytes]):
|
|
123
|
-
"""
|
|
124
|
-
:param image_format: The format of the image (e.g., PNG, JPEG).
|
|
125
|
-
:param image_data: The image data as a list of bytes. Each entry in the list represents a separate image.
|
|
126
|
-
"""
|
|
127
|
-
self.image_format = image_format
|
|
128
|
-
self.image_data = image_data
|
|
129
|
-
|
|
130
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
131
|
-
return StepEntryOutputData(
|
|
132
|
-
entry_data=StepEntryData(
|
|
133
|
-
dataType=DataType.IMAGE,
|
|
134
|
-
image_data=StepImageData(
|
|
135
|
-
image_format=self.image_format,
|
|
136
|
-
entries=self.image_data)
|
|
137
|
-
)
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
class JsonResult(SapioToolResult):
|
|
142
|
-
"""
|
|
143
|
-
A class representing JSON results from a Sapio tool.
|
|
144
|
-
"""
|
|
145
|
-
json_data: list[Any]
|
|
146
|
-
|
|
147
|
-
def __init__(self, json_data: list[Any]):
|
|
148
|
-
"""
|
|
149
|
-
:param json_data: The list of JSON data results. Each entry in the list represents a separate JSON object.
|
|
150
|
-
These entries must be able to be serialized to JSON using json.dumps(). A common JSON data type is a
|
|
151
|
-
dictionary of strings to strings, integers, doubles, or booleans.
|
|
152
|
-
"""
|
|
153
|
-
self.json_data = json_data
|
|
154
|
-
|
|
155
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
156
|
-
return StepEntryOutputData(
|
|
157
|
-
entry_data=StepEntryData(
|
|
158
|
-
dataType=DataType.JSON,
|
|
159
|
-
json_data=StepJsonData(entries=[json.dumps(x) for x in self.json_data])
|
|
160
|
-
)
|
|
161
|
-
)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
class TextResult(SapioToolResult):
|
|
165
|
-
"""
|
|
166
|
-
A class representing text results from a Sapio tool.
|
|
167
|
-
"""
|
|
168
|
-
text_data: list[str]
|
|
169
|
-
|
|
170
|
-
def __init__(self, text_data: list[str]):
|
|
171
|
-
"""
|
|
172
|
-
:param text_data: The text data as a list of strings. Each entry in the list represents a separate text entry.
|
|
173
|
-
"""
|
|
174
|
-
self.text_data = text_data
|
|
175
|
-
|
|
176
|
-
def to_proto(self) -> StepEntryOutputData | list[StepRecord]:
|
|
177
|
-
return StepEntryOutputData(
|
|
178
|
-
entry_data=StepEntryData(
|
|
179
|
-
dataType=DataType.TEXT,
|
|
180
|
-
text_data=StepTextData(entries=self.text_data)
|
|
181
|
-
)
|
|
182
|
-
)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
class ToolServiceBase(ToolServiceServicer, ABC):
|
|
186
|
-
"""
|
|
187
|
-
A base class for implementing a tool service. Subclasses should implement the register_tools method to register
|
|
188
|
-
their tools with the service.
|
|
189
|
-
"""
|
|
190
|
-
def GetToolDetails(self, request: ToolDetailsRequest, context: ServicerContext) -> ToolDetailsResponse:
|
|
191
|
-
try:
|
|
192
|
-
# Get the tool details from the registered tools.
|
|
193
|
-
details: list[ToolDetails] = self.get_details()
|
|
194
|
-
return ToolDetailsResponse(tool_framework_version=self.tool_version(), tool_details=details)
|
|
195
|
-
except Exception:
|
|
196
|
-
# TODO: This response doesn't even allow logs. What should we do if an exception occurs in this case?
|
|
197
|
-
return ToolDetailsResponse()
|
|
198
|
-
|
|
199
|
-
def ProcessData(self, request: ProcessStepRequest, context: ServicerContext) -> ProcessStepResponse:
|
|
200
|
-
try:
|
|
201
|
-
# Convert the SapioConnectionInfo proto object to a SapioUser object.
|
|
202
|
-
user = self.create_user(request.sapio_user)
|
|
203
|
-
# Get the tool results from the registered tool matching the request and convert them to proto objects.
|
|
204
|
-
entry_data: list[StepEntryOutputData] = []
|
|
205
|
-
new_records: list[StepRecord] = []
|
|
206
|
-
# TODO: Make use of the success value after the response object has a field for it.
|
|
207
|
-
success, results, logs = self.run(user, request, context)
|
|
208
|
-
for result in results:
|
|
209
|
-
data: StepEntryOutputData | list[StepRecord] = result.to_proto()
|
|
210
|
-
if isinstance(data, StepEntryOutputData):
|
|
211
|
-
entry_data.append(data)
|
|
212
|
-
else:
|
|
213
|
-
new_records.extend(data)
|
|
214
|
-
# Return a ProcessStepResponse proto object containing the output data and new records to the caller.
|
|
215
|
-
return ProcessStepResponse(entry_data=entry_data, log=logs, new_records=new_records)
|
|
216
|
-
except Exception:
|
|
217
|
-
# TODO: Return a False success result after the response object has a field for it.
|
|
218
|
-
return ProcessStepResponse(log=[traceback.format_exc()])
|
|
219
|
-
|
|
220
|
-
@staticmethod
|
|
221
|
-
def create_user(info: SapioConnectionInfo, timeout_seconds: int = 60) -> SapioUser:
|
|
222
|
-
"""
|
|
223
|
-
Create a SapioUser object from the given SapioConnectionInfo proto object.
|
|
224
|
-
|
|
225
|
-
:param info: The SapioConnectionInfo proto object.
|
|
226
|
-
:param timeout_seconds: The request timeout for calls made from this user object.
|
|
227
|
-
"""
|
|
228
|
-
# TODO: Have a customizable request timeout? Would need to be added to the request object.
|
|
229
|
-
# TODO: How should the RMI hosts and port be used in the connection info?
|
|
230
|
-
user = SapioUser(info.webservice_url, True, timeout_seconds, guid=info.app_guid)
|
|
231
|
-
if info.secret_type == SapioUserSecretType.SESSION_TOKEN:
|
|
232
|
-
user.api_token = info.secret
|
|
233
|
-
elif info.secret_type == SapioUserSecretType.PASSWORD:
|
|
234
|
-
# TODO: Will the secret be base64 encoded if it's a password? That's how basic auth is normally handled.
|
|
235
|
-
user.password = info.secret
|
|
236
|
-
else:
|
|
237
|
-
raise Exception(f"Unexpected secret type: {info.secret_type}")
|
|
238
|
-
return user
|
|
239
|
-
|
|
240
|
-
@staticmethod
|
|
241
|
-
def tool_version() -> int:
|
|
242
|
-
"""
|
|
243
|
-
:return: The version of this tool.
|
|
244
|
-
"""
|
|
245
|
-
return 1
|
|
246
|
-
|
|
247
|
-
def _get_tools(self) -> list[ToolBase]:
|
|
248
|
-
"""
|
|
249
|
-
return: Get the tools registered with this service.
|
|
250
|
-
"""
|
|
251
|
-
tools: list[ToolBase] = self.register_tools()
|
|
252
|
-
if not tools:
|
|
253
|
-
raise Exception("No tools registered with this service.")
|
|
254
|
-
return tools
|
|
255
|
-
|
|
256
|
-
def _get_tool(self, name: str) -> ToolBase:
|
|
257
|
-
"""
|
|
258
|
-
Get a specific tool by its name.
|
|
259
|
-
|
|
260
|
-
:param name: The name of the tool to retrieve.
|
|
261
|
-
:return: The tool object corresponding to the given name.
|
|
262
|
-
"""
|
|
263
|
-
tools: dict[str, ToolBase] = {x.name: x for x in self.register_tools()}
|
|
264
|
-
if not tools:
|
|
265
|
-
raise Exception("No tools registered with this service.")
|
|
266
|
-
if name not in tools:
|
|
267
|
-
raise Exception(f"Tool \"{name}\" not found in registered tools.")
|
|
268
|
-
return tools[name]
|
|
269
|
-
|
|
270
|
-
@abstractmethod
|
|
271
|
-
def register_tools(self) -> list[ToolBase]:
|
|
272
|
-
"""
|
|
273
|
-
Register the tools with this service. Create and instantiate ToolBase subclasses to register them.
|
|
274
|
-
|
|
275
|
-
:return: A list of tools to register to this service.
|
|
276
|
-
"""
|
|
277
|
-
pass
|
|
278
|
-
|
|
279
|
-
def get_details(self) -> list[ToolDetails]:
|
|
280
|
-
"""
|
|
281
|
-
Get the details of the tool.
|
|
282
|
-
|
|
283
|
-
:return: A ToolDetailsResponse object containing the tool details.
|
|
284
|
-
"""
|
|
285
|
-
tool_details: list[ToolDetails] = []
|
|
286
|
-
for tool in self._get_tools():
|
|
287
|
-
tool_details.append(tool.to_proto())
|
|
288
|
-
return tool_details
|
|
289
|
-
|
|
290
|
-
def run(self, user: SapioUser, request: ProcessStepRequest, context: ServicerContext) \
|
|
291
|
-
-> tuple[bool, list[SapioToolResult], list[str]]:
|
|
292
|
-
"""
|
|
293
|
-
Execute a tool from this service.
|
|
294
|
-
|
|
295
|
-
:param user: A user object that can be used to initialize manager classes using DataMgmtServer to query the
|
|
296
|
-
system.
|
|
297
|
-
:param request: The request object containing the input data.
|
|
298
|
-
:param context: The gRPC context.
|
|
299
|
-
:return: Whether or not the tool succeeded, the results of the tool, and any logs generated by the tool.
|
|
300
|
-
"""
|
|
301
|
-
tool = self._get_tool(request.tool_name)
|
|
302
|
-
try:
|
|
303
|
-
results = tool.run(user, request, context)
|
|
304
|
-
return True, results, tool.logs
|
|
305
|
-
except Exception:
|
|
306
|
-
tool.log_message(traceback.format_exc())
|
|
307
|
-
return False, [], tool.logs
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
class ToolBase(ABC):
|
|
311
|
-
"""
|
|
312
|
-
A base class for implementing a tool.
|
|
313
|
-
"""
|
|
314
|
-
name: str
|
|
315
|
-
description: str
|
|
316
|
-
data_type_name: str | None
|
|
317
|
-
inputs: list[ToolInputDetails]
|
|
318
|
-
outputs: list[ToolOutputDetails]
|
|
319
|
-
configs: list[VeloxFieldDefProto]
|
|
320
|
-
logs: list[str]
|
|
321
|
-
|
|
322
|
-
def __init__(self, name: str, description: str, data_type_name: str | None = None):
|
|
323
|
-
"""
|
|
324
|
-
:param name: The name of the tool.
|
|
325
|
-
:param description: A description of the tool.
|
|
326
|
-
:param data_type_name: The name of the output data type of this tool, if applicable. When this tool returns
|
|
327
|
-
FieldMapResult objects in its run method, this name will be used to set the data type of the output data.
|
|
328
|
-
"""
|
|
329
|
-
self.name = name
|
|
330
|
-
self.description = description
|
|
331
|
-
self.data_type_name = data_type_name
|
|
332
|
-
self.inputs = []
|
|
333
|
-
self.outputs = []
|
|
334
|
-
self.configs = []
|
|
335
|
-
self.logs = []
|
|
336
|
-
|
|
337
|
-
def add_input(self, details: ToolInputDetails) -> None:
|
|
338
|
-
"""
|
|
339
|
-
Add an input configuration to the tool. This determines how many inputs this tool will accept in the plan
|
|
340
|
-
manager, as well as what those inputs are.
|
|
341
|
-
|
|
342
|
-
:param details: The input configuration details.
|
|
343
|
-
"""
|
|
344
|
-
self.inputs.append(details)
|
|
345
|
-
|
|
346
|
-
def add_output(self, details: ToolOutputDetails) -> None:
|
|
347
|
-
"""
|
|
348
|
-
Add an output configuration to the tool. This determines how many outputs this tool will return in the plan
|
|
349
|
-
manager, as well as what those outputs are.
|
|
350
|
-
|
|
351
|
-
:param details: The output configuration details.
|
|
352
|
-
"""
|
|
353
|
-
self.outputs.append(details)
|
|
354
|
-
|
|
355
|
-
def add_config_field(self, field: VeloxFieldDefProto) -> None:
|
|
356
|
-
"""
|
|
357
|
-
Add a configuration field to the tool. This field will be used to configure the tool in the plan manager.
|
|
358
|
-
|
|
359
|
-
:param field: The configuration field details.
|
|
360
|
-
"""
|
|
361
|
-
self.configs.append(field)
|
|
362
|
-
|
|
363
|
-
def add_integer_config_field(self, field_name: str, display_name: str, description: str,
|
|
364
|
-
default_value: int, min_value: int = -2**31, max_value: int = 2**31-1) -> None:
|
|
365
|
-
"""
|
|
366
|
-
Add an integer configuration field to the tool. This field will be used to configure the tool in the plan
|
|
367
|
-
manager.
|
|
368
|
-
|
|
369
|
-
:param field_name: The name of the field.
|
|
370
|
-
:param display_name: The display name of the field.
|
|
371
|
-
:param description: The description of the field.
|
|
372
|
-
:param default_value: The default value of the field.
|
|
373
|
-
:param min_value: The minimum value of the field.
|
|
374
|
-
:param max_value: The maximum value of the field.
|
|
375
|
-
"""
|
|
376
|
-
self.configs.append(VeloxFieldDefProto(
|
|
377
|
-
data_field_type=FieldTypeProto.INTEGER,
|
|
378
|
-
data_field_name=field_name,
|
|
379
|
-
display_name=display_name,
|
|
380
|
-
description=description,
|
|
381
|
-
required=True,
|
|
382
|
-
editable=True,
|
|
383
|
-
integer_properties=IntegerProperties(
|
|
384
|
-
default_value=default_value,
|
|
385
|
-
min_value=min_value,
|
|
386
|
-
max_value=max_value
|
|
387
|
-
)
|
|
388
|
-
))
|
|
389
|
-
|
|
390
|
-
def add_double_config_field(self, field_name: str, display_name: str, description: str, default_value: float,
|
|
391
|
-
min_value: float = -10.**120, max_value: float = 10.**120, precision: int = 2) -> None:
|
|
392
|
-
"""
|
|
393
|
-
Add a double configuration field to the tool. This field will be used to configure the tool in the plan
|
|
394
|
-
manager.
|
|
395
|
-
|
|
396
|
-
:param field_name: The name of the field.
|
|
397
|
-
:param display_name: The display name of the field.
|
|
398
|
-
:param description: The description of the field.
|
|
399
|
-
:param default_value: The default value of the field.
|
|
400
|
-
:param min_value: The minimum value of the field.
|
|
401
|
-
:param max_value: The maximum value of the field.
|
|
402
|
-
:param precision: The precision of the field.
|
|
403
|
-
"""
|
|
404
|
-
self.configs.append(VeloxFieldDefProto(
|
|
405
|
-
data_field_type=FieldTypeProto.DOUBLE,
|
|
406
|
-
data_field_name=field_name,
|
|
407
|
-
display_name=display_name,
|
|
408
|
-
description=description,
|
|
409
|
-
required=True,
|
|
410
|
-
editable=True,
|
|
411
|
-
double_properties=DoubleProperties(
|
|
412
|
-
default_value=default_value,
|
|
413
|
-
min_value=min_value,
|
|
414
|
-
max_value=max_value,
|
|
415
|
-
precision=precision
|
|
416
|
-
)
|
|
417
|
-
))
|
|
418
|
-
|
|
419
|
-
def add_list_config_field(self, field_name: str, display_name: str, description: str, default_value: str,
|
|
420
|
-
allowed_values: list[str]) -> None:
|
|
421
|
-
"""
|
|
422
|
-
Add a list configuration field to the tool. This field will be used to configure the tool in the plan
|
|
423
|
-
manager.
|
|
424
|
-
|
|
425
|
-
:param field_name: The name of the field.
|
|
426
|
-
:param display_name: The display name of the field.
|
|
427
|
-
:param description: The description of the field.
|
|
428
|
-
:param default_value: The default value of the field.
|
|
429
|
-
:param allowed_values: The list of allowed values for the field.
|
|
430
|
-
"""
|
|
431
|
-
self.configs.append(VeloxFieldDefProto(
|
|
432
|
-
data_field_type=FieldTypeProto.SELECTION,
|
|
433
|
-
data_field_name=field_name,
|
|
434
|
-
display_name=display_name,
|
|
435
|
-
description=description,
|
|
436
|
-
required=True,
|
|
437
|
-
editable=True,
|
|
438
|
-
selection_properties=SelectionProperties(
|
|
439
|
-
default_value=default_value,
|
|
440
|
-
static_list_values=allowed_values,
|
|
441
|
-
)
|
|
442
|
-
))
|
|
443
|
-
|
|
444
|
-
def add_boolean_config_field(self, field_name: str, display_name: str, description: str, default_value: bool) \
|
|
445
|
-
-> None:
|
|
446
|
-
"""
|
|
447
|
-
Add a boolean configuration field to the tool. This field will be used to configure the tool in the plan
|
|
448
|
-
manager.
|
|
449
|
-
|
|
450
|
-
:param field_name: The name of the field.
|
|
451
|
-
:param display_name: The display name of the field.
|
|
452
|
-
:param description: The description of the field.
|
|
453
|
-
:param default_value: The default value of the field.
|
|
454
|
-
"""
|
|
455
|
-
self.configs.append(VeloxFieldDefProto(
|
|
456
|
-
data_field_type=FieldTypeProto.BOOLEAN,
|
|
457
|
-
data_field_name=field_name,
|
|
458
|
-
display_name=display_name,
|
|
459
|
-
description=description,
|
|
460
|
-
required=True,
|
|
461
|
-
editable=True,
|
|
462
|
-
boolean_properties=BooleanProperties(
|
|
463
|
-
default_value=default_value
|
|
464
|
-
)
|
|
465
|
-
))
|
|
466
|
-
|
|
467
|
-
def to_proto(self) -> ToolDetails:
|
|
468
|
-
"""
|
|
469
|
-
:return: The ToolDetails proto object representing this tool.
|
|
470
|
-
"""
|
|
471
|
-
return ToolDetails(
|
|
472
|
-
name=self.name,
|
|
473
|
-
description=self.description,
|
|
474
|
-
input_configs=self.inputs,
|
|
475
|
-
output_configs=self.outputs,
|
|
476
|
-
output_data_type_name=self.data_type_name,
|
|
477
|
-
config_fields=self.configs
|
|
478
|
-
)
|
|
479
|
-
|
|
480
|
-
def log_message(self, message: str) -> None:
|
|
481
|
-
"""
|
|
482
|
-
Log a message for this tool. This message will be included in the logs returned to the caller.
|
|
483
|
-
|
|
484
|
-
:param message: The message to log.
|
|
485
|
-
"""
|
|
486
|
-
self.logs.append(f"{self.name}: {message}")
|
|
487
|
-
|
|
488
|
-
@abstractmethod
|
|
489
|
-
def run(self, user: SapioUser, request: ProcessStepRequest, context: ServicerContext) -> list[SapioToolResult]:
|
|
490
|
-
"""
|
|
491
|
-
Execute this tool.
|
|
492
|
-
|
|
493
|
-
:param user: A user object that can be used to initialize manager classes using DataMgmtServer to query the
|
|
494
|
-
system.
|
|
495
|
-
:param request: The request object containing the input data.
|
|
496
|
-
:param context: The gRPC context.
|
|
497
|
-
:return: A SapioToolResults object containing the response data.
|
|
498
|
-
"""
|
|
499
|
-
pass
|
|
500
|
-
|
|
501
|
-
@staticmethod
|
|
502
|
-
def get_request_json(request: ProcessStepRequest, index: int = 0) -> list[Any]:
|
|
503
|
-
"""
|
|
504
|
-
Parse the JSON data from the request object.
|
|
505
|
-
|
|
506
|
-
:param request: The request object containing the input data.
|
|
507
|
-
:param index: The index of the input to parse. Defaults to 0.
|
|
508
|
-
:return: A list of parsed JSON objects. Each entry in the list represents a separate JSON entry from the input.
|
|
509
|
-
"""
|
|
510
|
-
entry_data = request.entry_data
|
|
511
|
-
input_data: list[Any] = [json.loads(x) for x in entry_data[index].entry_data.json_data.entries]
|
|
512
|
-
return input_data
|
|
513
|
-
|
|
514
|
-
@staticmethod
|
|
515
|
-
def read_from_json(json_data: list[dict[str, Any]], key: str) -> list[Any]:
|
|
516
|
-
"""
|
|
517
|
-
From a list of dictionaries, return a list of values for the given key from each dictionary. Skips null values.
|
|
518
|
-
|
|
519
|
-
:param json_data: The JSON data to read from.
|
|
520
|
-
:param key: The key to read the values from.
|
|
521
|
-
:return: A list of values corresponding to the given key in the JSON data.
|
|
522
|
-
"""
|
|
523
|
-
ret_val: list[Any] = []
|
|
524
|
-
for entry in json_data:
|
|
525
|
-
if key in entry:
|
|
526
|
-
value = entry[key]
|
|
527
|
-
if isinstance(value, list):
|
|
528
|
-
ret_val.extend(value)
|
|
529
|
-
elif value is not None:
|
|
530
|
-
ret_val.append(value)
|
|
531
|
-
return ret_val
|
|
532
|
-
|
|
533
|
-
def get_config_fields(self, request: ProcessStepRequest) -> dict[str, Any]:
|
|
534
|
-
"""
|
|
535
|
-
Get the configuration fields from the request object.
|
|
536
|
-
|
|
537
|
-
:param request: The request object containing the input data.
|
|
538
|
-
:return: A dictionary of configuration field names and their values.
|
|
539
|
-
"""
|
|
540
|
-
config_fields: dict[str, Any] = {}
|
|
541
|
-
# noinspection PyUnresolvedReferences,PyTypeChecker
|
|
542
|
-
field_defs: dict[str, VeloxFieldDefProto] = {x.data_field_name: x for x in self.to_proto().config_fields}
|
|
543
|
-
for field, value in request.config_field_values.items():
|
|
544
|
-
if field not in field_defs:
|
|
545
|
-
continue
|
|
546
|
-
field_type: FieldTypeProto = field_defs[field].data_field_type
|
|
547
|
-
if field_type == FieldTypeProto.BOOLEAN:
|
|
548
|
-
value = value.bool_value
|
|
549
|
-
elif field_type == FieldTypeProto.DOUBLE:
|
|
550
|
-
value = value.double_value
|
|
551
|
-
elif field_type in [FieldTypeProto.INTEGER, FieldTypeProto.SHORT, FieldTypeProto.LONG, FieldTypeProto.ENUM]:
|
|
552
|
-
value = value.int_value
|
|
553
|
-
else:
|
|
554
|
-
value = value.string_value
|
|
555
|
-
config_fields[field] = value
|
|
556
|
-
return config_fields
|