sapiopycommons 2025.8.14a703__py3-none-any.whl → 2025.8.15a704__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.

Files changed (46) hide show
  1. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.py +43 -0
  2. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.pyi +31 -0
  3. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2_grpc.py +24 -0
  4. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.py +123 -0
  5. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.pyi +598 -0
  6. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2_grpc.py +24 -0
  7. sapiopycommons/ai/api/plan/converter/proto/converter_pb2.py +50 -0
  8. sapiopycommons/ai/api/plan/converter/proto/converter_pb2.pyi +61 -0
  9. sapiopycommons/ai/api/plan/converter/proto/converter_pb2_grpc.py +149 -0
  10. sapiopycommons/ai/api/plan/item/proto/item_container_pb2.py +55 -0
  11. sapiopycommons/ai/api/plan/item/proto/item_container_pb2.pyi +88 -0
  12. sapiopycommons/ai/api/plan/item/proto/item_container_pb2_grpc.py +24 -0
  13. sapiopycommons/ai/api/plan/proto/step_output_pb2.py +45 -0
  14. sapiopycommons/ai/api/plan/proto/step_output_pb2.pyi +42 -0
  15. sapiopycommons/ai/api/plan/proto/step_output_pb2_grpc.py +24 -0
  16. sapiopycommons/ai/api/plan/proto/step_pb2.py +43 -0
  17. sapiopycommons/ai/api/plan/proto/step_pb2.pyi +43 -0
  18. sapiopycommons/ai/api/plan/proto/step_pb2_grpc.py +24 -0
  19. sapiopycommons/ai/api/plan/script/proto/script_pb2.py +59 -0
  20. sapiopycommons/ai/api/plan/script/proto/script_pb2.pyi +102 -0
  21. sapiopycommons/ai/api/plan/script/proto/script_pb2_grpc.py +153 -0
  22. sapiopycommons/ai/api/plan/tool/proto/entry_pb2.py +41 -0
  23. sapiopycommons/ai/api/plan/tool/proto/entry_pb2.pyi +35 -0
  24. sapiopycommons/ai/api/plan/tool/proto/entry_pb2_grpc.py +24 -0
  25. sapiopycommons/ai/api/plan/tool/proto/tool_pb2.py +75 -0
  26. sapiopycommons/ai/api/plan/tool/proto/tool_pb2.pyi +237 -0
  27. sapiopycommons/ai/api/plan/tool/proto/tool_pb2_grpc.py +154 -0
  28. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.py +39 -0
  29. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.pyi +32 -0
  30. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2_grpc.py +24 -0
  31. sapiopycommons/ai/converter_service_base.py +116 -0
  32. sapiopycommons/ai/protobuf_utils.py +504 -0
  33. sapiopycommons/ai/test_client.py +334 -0
  34. sapiopycommons/ai/tool_service_base.py +917 -0
  35. sapiopycommons/callbacks/callback_util.py +16 -26
  36. sapiopycommons/flowcyto/flow_cyto.py +24 -2
  37. sapiopycommons/general/accession_service.py +28 -2
  38. sapiopycommons/multimodal/multimodal.py +24 -2
  39. sapiopycommons/webhook/webservice_handlers.py +1 -1
  40. {sapiopycommons-2025.8.14a703.dist-info → sapiopycommons-2025.8.15a704.dist-info}/METADATA +2 -2
  41. {sapiopycommons-2025.8.14a703.dist-info → sapiopycommons-2025.8.15a704.dist-info}/RECORD +43 -12
  42. sapiopycommons/ai/tool_of_tools.py +0 -917
  43. sapiopycommons/files/assay_plate_reader.py +0 -93
  44. sapiopycommons/files/file_text_converter.py +0 -207
  45. {sapiopycommons-2025.8.14a703.dist-info → sapiopycommons-2025.8.15a704.dist-info}/WHEEL +0 -0
  46. {sapiopycommons-2025.8.14a703.dist-info → sapiopycommons-2025.8.15a704.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,334 @@
1
+ import base64
2
+ import json
3
+ from enum import Enum
4
+ from typing import Any
5
+
6
+ import grpc
7
+ from sapiopylib.rest.User import SapioUser
8
+
9
+ from sapiopycommons.ai.api.plan.converter.proto.converter_pb2 import ConverterDetailsRequestPbo, \
10
+ ConverterDetailsResponsePbo, ConvertResponsePbo, ConvertRequestPbo
11
+ from sapiopycommons.ai.api.plan.converter.proto.converter_pb2_grpc import ConverterServiceStub
12
+ from sapiopycommons.ai.api.plan.item.proto.item_container_pb2 import ContentTypePbo
13
+ from sapiopycommons.ai.api.plan.tool.proto.entry_pb2 import StepBinaryContainerPbo, StepCsvRowPbo, \
14
+ StepCsvHeaderRowPbo, StepCsvContainerPbo, StepJsonContainerPbo, StepTextContainerPbo, \
15
+ StepItemContainerPbo, StepInputBatchPbo
16
+ from sapiopycommons.ai.api.plan.tool.proto.tool_pb2 import ProcessStepResponsePbo, ProcessStepRequestPbo, \
17
+ ToolDetailsRequestPbo, ToolDetailsResponsePbo, ProcessStepResponseStatusPbo
18
+ from sapiopycommons.ai.api.plan.tool.proto.tool_pb2_grpc import ToolServiceStub
19
+ from sapiopycommons.ai.api.session.proto.sapio_conn_info_pb2 import SapioConnectionInfoPbo, SapioUserSecretTypePbo
20
+ from sapiopycommons.ai.protobuf_utils import ProtobufUtils
21
+ from sapiopycommons.general.aliases import FieldValue
22
+
23
+
24
+ class ContainerType(Enum):
25
+ """
26
+ An enum of the different container contents of a StepItemContainerPbo.
27
+ """
28
+ BINARY = "binary"
29
+ CSV = "csv"
30
+ JSON = "json"
31
+ TEXT = "text"
32
+
33
+
34
+ # FR-47422: Created class.
35
+ class ToolOutput:
36
+ """
37
+ A class for holding the output of a TestClient that calls a ToolService. ToolOutput objects an be
38
+ printed to show the output of the tool in a human-readable format.
39
+ """
40
+ tool_name: str
41
+
42
+ status: str
43
+ message: str
44
+
45
+ binary_output: list[bytes]
46
+ csv_output: list[dict[str, Any]]
47
+ json_output: list[Any]
48
+ text_output: list[str]
49
+
50
+ new_records: list[dict[str, FieldValue]]
51
+
52
+ logs: list[str]
53
+
54
+ def __init__(self, tool_name: str):
55
+ self.tool_name = tool_name
56
+ self.binary_output = []
57
+ self.csv_output = []
58
+ self.json_output = []
59
+ self.text_output = []
60
+ self.new_records = []
61
+ self.logs = []
62
+
63
+ def __str__(self):
64
+ ret_val: str = f"{self.tool_name} Output:\n"
65
+ ret_val += f"\tStatus: {self.status}\n"
66
+ ret_val += f"\tMessage: {self.message}\n"
67
+ ret_val += "-" * 25 + "\n"
68
+
69
+ ret_val += f"Binary Output: {len(self.binary_output)} item(s)\n"
70
+ for binary in self.binary_output:
71
+ ret_val += f"\t{len(binary)} byte(s)\n"
72
+ ret_val += f"\t{binary[:50]}...\n"
73
+
74
+ ret_val += f"CSV Output: {len(self.csv_output)} item(s)\n"
75
+ if self.csv_output:
76
+ ret_val += f"\tHeaders: {', '.join(self.csv_output[0].keys())}\n"
77
+ for i, csv_row in enumerate(self.csv_output):
78
+ ret_val += f"\t{i}: {', '.join(f'{v}' for k, v in csv_row.items())}\n"
79
+
80
+ ret_val += f"JSON Output: {len(self.json_output)} item(s)\n"
81
+ if self.json_output:
82
+ ret_val += f"{json.dumps(self.json_output, indent=2)}\n"
83
+
84
+ ret_val += f"Text Output: {len(self.text_output)} item(s)\n"
85
+ for text in self.text_output:
86
+ ret_val += f"\t{text}\n"
87
+
88
+ ret_val += f"New Records: {len(self.new_records)} item(s)\n"
89
+ for record in self.new_records:
90
+ ret_val += f"{json.dumps(record, indent=2)}\n"
91
+
92
+ ret_val += f"Logs: {len(self.logs)} item(s)\n"
93
+ for log in self.logs:
94
+ ret_val += f"\t{log}\n"
95
+ return ret_val
96
+
97
+
98
+ class TestClient:
99
+ """
100
+ A client for testing a ToolService.
101
+ """
102
+ grpc_server_url: str
103
+ options: list[tuple[str, Any]] | None
104
+ connection: SapioConnectionInfoPbo
105
+ _request_inputs: list[StepItemContainerPbo]
106
+ _config_fields: dict[str, Any]
107
+
108
+ def __init__(self, grpc_server_url: str, user: SapioUser | None = None,
109
+ options: list[tuple[str, Any]] | None = None):
110
+ """
111
+ :param grpc_server_url: The URL of the gRPC server to connect to.
112
+ :param user: Optional SapioUser object to use for the connection. If not provided, a default connection
113
+ will be created with test credentials.
114
+ :param options: Optional list of gRPC channel options.
115
+ """
116
+ self.grpc_server_url = grpc_server_url
117
+ self.options = options
118
+ self._create_connection(user)
119
+ self._request_inputs = []
120
+ self._config_fields = {}
121
+
122
+ def _create_connection(self, user: SapioUser | None = None):
123
+ """
124
+ Create a SapioConnectionInfoPbo object with test credentials. This method can be overridden to
125
+ create a user with specific credentials for testing.
126
+ """
127
+ self.connection = SapioConnectionInfoPbo()
128
+ self.connection.username = user.username if user else "Testing"
129
+ self.connection.webservice_url = user.url if user else "https://localhost:8080/webservice/api"
130
+ self.connection.app_guid = user.guid if user else "1234567890"
131
+ self.connection.rmi_host.append("Testing")
132
+ self.connection.rmi_port = 9001
133
+ if user and user.password:
134
+ self.connection.secret_type = SapioUserSecretTypePbo.PASSWORD
135
+ self.connection.secret = "Basic " + base64.b64encode(f'{user.username}:{user.password}'.encode()).decode()
136
+ else:
137
+ self.connection.secret_type = SapioUserSecretTypePbo.SESSION_TOKEN
138
+ self.connection.secret = user.api_token if user and user.api_token else "test_api_token"
139
+
140
+ def add_binary_input(self, input_data: list[bytes]) -> None:
141
+ """
142
+ Add a binary input to the the next request.
143
+ """
144
+ self._add_input(ContainerType.BINARY, StepBinaryContainerPbo(items=input_data))
145
+
146
+ def add_csv_input(self, input_data: list[dict[str, Any]]) -> None:
147
+ """
148
+ Add a CSV input to the next request.
149
+ """
150
+ csv_items = []
151
+ for row in input_data:
152
+ csv_items.append(StepCsvRowPbo(cells=[str(value) for value in row.values()]))
153
+ header = StepCsvHeaderRowPbo(cells=list(input_data[0].keys()))
154
+ self._add_input(ContainerType.CSV, StepCsvContainerPbo(header=header, items=csv_items))
155
+
156
+ def add_json_input(self, input_data: list[dict[str, Any]]) -> None:
157
+ """
158
+ Add a JSON input to the next request.
159
+ """
160
+ self._add_input(ContainerType.JSON, StepJsonContainerPbo(items=[json.dumps(x) for x in input_data]))
161
+
162
+ def add_text_input(self, input_data: list[str]) -> None:
163
+ """
164
+ Add a text input to the next request.
165
+ """
166
+ self._add_input(ContainerType.TEXT, StepTextContainerPbo(items=input_data))
167
+
168
+ def clear_inputs(self) -> None:
169
+ """
170
+ Clear all inputs that have been added to the next request.
171
+ This is useful if you want to start a new request without the previous inputs.
172
+ """
173
+ self._request_inputs.clear()
174
+
175
+ def add_config_field(self, field_name: str, value: Any) -> None:
176
+ """
177
+ Add a configuration field value to the next request.
178
+
179
+ :param field_name: The name of the configuration field.
180
+ :param value: The value to set for the configuration field.
181
+ """
182
+ self._config_fields[field_name] = value
183
+
184
+ def add_config_fields(self, config_fields: dict[str, Any]) -> None:
185
+ """
186
+ Add multiple configuration field values to the next request.
187
+
188
+ :param config_fields: A dictionary of configuration field names and their corresponding values.
189
+ """
190
+ self._config_fields.update(config_fields)
191
+
192
+ def clear_configs(self) -> None:
193
+ """
194
+ Clear all configuration field values that have been added to the next request.
195
+ This is useful if you want to start a new request without the previous configurations.
196
+ """
197
+ self._config_fields.clear()
198
+
199
+ def clear_request(self) -> None:
200
+ """
201
+ Clear all inputs and configuration fields that have been added to the next request.
202
+ This is useful if you want to start a new request without the previous inputs and configurations.
203
+ """
204
+ self.clear_inputs()
205
+ self.clear_configs()
206
+
207
+ def _add_input(self, container_type: ContainerType, items: Any) -> None:
208
+ """
209
+ Helper method for adding inputs to the next request.
210
+ """
211
+ match container_type:
212
+ # The content type doesn't matter when we're just testing.
213
+ case ContainerType.BINARY:
214
+ container = StepItemContainerPbo(content_type=ContentTypePbo(), binary_container=items)
215
+ case ContainerType.CSV:
216
+ container = StepItemContainerPbo(content_type=ContentTypePbo(), csv_container=items)
217
+ case ContainerType.JSON:
218
+ container = StepItemContainerPbo(content_type=ContentTypePbo(), json_container=items)
219
+ case ContainerType.TEXT:
220
+ container = StepItemContainerPbo(content_type=ContentTypePbo(), text_container=items)
221
+ case _:
222
+ raise ValueError(f"Unsupported data type: {container_type}")
223
+ self._request_inputs.append(container)
224
+
225
+ def get_service_details(self) -> ToolDetailsResponsePbo:
226
+ """
227
+ Get the details of the tools from the server.
228
+
229
+ :return: A ToolDetailsResponsePbo object containing the details of the tool service.
230
+ """
231
+ with grpc.insecure_channel(self.grpc_server_url, options=self.options) as channel:
232
+ stub = ToolServiceStub(channel)
233
+ return stub.GetToolDetails(ToolDetailsRequestPbo(sapio_conn_info=self.connection))
234
+
235
+ def call_tool(self, tool_name: str, is_dry_run: bool = False) -> ToolOutput:
236
+ """
237
+ Send the request to the tool service for a particular tool name. This will send all the inputs that have been
238
+ added using the add_X_input functions.
239
+
240
+ :param tool_name: The name of the tool to call on the server.
241
+ :param is_dry_run: If True, the tool will not be executed, but the request will be validated.
242
+ :return: A ToolOutput object containing the results of the tool service call.
243
+ """
244
+ with grpc.insecure_channel(self.grpc_server_url, options=self.options) as channel:
245
+ stub = ToolServiceStub(channel)
246
+
247
+ response: ProcessStepResponsePbo = stub.ProcessData(
248
+ ProcessStepRequestPbo(
249
+ sapio_user=self.connection,
250
+ tool_name=tool_name,
251
+ config_field_values={x: ProtobufUtils.value_to_field_pbo(y) for x, y in self._config_fields.items()},
252
+ dry_run=is_dry_run,
253
+ verbose_logging=True,
254
+ input=[
255
+ StepInputBatchPbo(is_partial=False, item_container=item)
256
+ for item in self._request_inputs
257
+ ]
258
+ )
259
+ )
260
+
261
+ results = ToolOutput(tool_name)
262
+
263
+ match response.status:
264
+ case ProcessStepResponseStatusPbo.SUCCESS:
265
+ results.status = "Success"
266
+ case ProcessStepResponseStatusPbo.FAILURE:
267
+ results.status = "Failure"
268
+ case _:
269
+ results.status = "Unknown"
270
+ results.message = response.status_message
271
+
272
+ for item in response.output:
273
+ container = item.item_container
274
+
275
+ results.binary_output.extend(container.binary_container.items)
276
+ for header in container.csv_container.header.cells:
277
+ output_row: dict[str, Any] = {}
278
+ for i, row in enumerate(container.csv_container.items):
279
+ output_row[header] = row.cells[i]
280
+ results.csv_output.append(output_row)
281
+ results.json_output.extend([json.loads(x) for x in container.json_container.items])
282
+ results.text_output.extend(container.text_container.items)
283
+
284
+ for record in response.new_records:
285
+ field_map: dict[str, Any] = {x: ProtobufUtils.field_pbo_to_value(y) for x, y in record.fields.items()}
286
+ results.new_records.append(field_map)
287
+
288
+ results.logs.extend(response.log)
289
+
290
+ return results
291
+
292
+
293
+ class TestConverterClient:
294
+ """
295
+ A client for testing a ConverterService.
296
+ """
297
+ grpc_server_url: str
298
+ options: list[tuple[str, Any]] | None
299
+
300
+ def __init__(self, grpc_server_url: str, options: list[tuple[str, Any]] | None = None):
301
+ """
302
+ :param grpc_server_url: The URL of the gRPC server to connect to.
303
+ :param options: Optional list of gRPC channel options.
304
+ """
305
+ self.grpc_server_url = grpc_server_url
306
+ self.options = options
307
+
308
+ def get_converter_details(self) -> ConverterDetailsResponsePbo:
309
+ """
310
+ Get the details of the converters from the server.
311
+
312
+ :return: A ToolDetailsResponsePbo object containing the details of the converter service.
313
+ """
314
+ with grpc.insecure_channel(self.grpc_server_url, options=self.options) as channel:
315
+ stub = ConverterServiceStub(channel)
316
+ return stub.GetConverterDetails(ConverterDetailsRequestPbo())
317
+
318
+ def convert_content(self, input_container: StepItemContainerPbo, target_type: ContentTypePbo) \
319
+ -> StepItemContainerPbo:
320
+ """
321
+ Convert the content of the input container to the target content type.
322
+
323
+ :param input_container: The input container to convert. This container must have a ContentTypePbo set that
324
+ matches one of the input types that the converter service supports.
325
+ :param target_type: The target content type to convert to. This must match one of the target types that the
326
+ converter service supports.
327
+ :return: A StepItemContainerPbo object containing the converted content.
328
+ """
329
+ with grpc.insecure_channel(self.grpc_server_url, options=self.options) as channel:
330
+ stub = ConverterServiceStub(channel)
331
+ response: ConvertResponsePbo = stub.ConvertContent(
332
+ ConvertRequestPbo(item_container=input_container, target_content_type=target_type)
333
+ )
334
+ return response.item_container