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