sapiopycommons 2025.8.12a697__py3-none-any.whl → 2025.8.12a698__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/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.12a697.dist-info → sapiopycommons-2025.8.12a698.dist-info}/METADATA +2 -2
  10. {sapiopycommons-2025.8.12a697.dist-info → sapiopycommons-2025.8.12a698.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 -126
  42. sapiopycommons/ai/protobuf_utils.py +0 -483
  43. sapiopycommons/ai/test_client.py +0 -327
  44. sapiopycommons/ai/tool_service_base.py +0 -915
  45. {sapiopycommons-2025.8.12a697.dist-info → sapiopycommons-2025.8.12a698.dist-info}/WHEEL +0 -0
  46. {sapiopycommons-2025.8.12a697.dist-info → sapiopycommons-2025.8.12a698.dist-info}/licenses/LICENSE +0 -0
@@ -1,327 +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.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
- connection: SapioConnectionInfoPbo
104
- _request_inputs: list[StepItemContainerPbo]
105
- _config_fields: dict[str, Any]
106
-
107
- def __init__(self, grpc_server_url: str, user: SapioUser | None = None):
108
- """
109
- :param grpc_server_url: The URL of the gRPC server to connect to.
110
- :param user: Optional SapioUser object to use for the connection. If not provided, a default connection
111
- will be created with test credentials.
112
- """
113
- self.grpc_server_url = grpc_server_url
114
- self._create_connection(user)
115
- self._request_inputs = []
116
- self._config_fields = {}
117
-
118
- def _create_connection(self, user: SapioUser | None = None):
119
- """
120
- Create a SapioConnectionInfoPbo object with test credentials. This method can be overridden to
121
- create a user with specific credentials for testing.
122
- """
123
- self.connection = SapioConnectionInfoPbo()
124
- self.connection.username = user.username if user else "Testing"
125
- self.connection.webservice_url = user.url if user else "https://localhost:8080/webservice/api"
126
- self.connection.app_guid = user.guid if user else "1234567890"
127
- self.connection.rmi_host.append("Testing")
128
- self.connection.rmi_port = 9001
129
- if user and user.password:
130
- self.connection.secret_type = SapioUserSecretTypePbo.PASSWORD
131
- self.connection.secret = "Basic " + base64.b64encode(f'{user.username}:{user.password}'.encode()).decode()
132
- else:
133
- self.connection.secret_type = SapioUserSecretTypePbo.SESSION_TOKEN
134
- self.connection.secret = user.api_token if user and user.api_token else "test_api_token"
135
-
136
- def add_binary_input(self, input_data: list[bytes]) -> None:
137
- """
138
- Add a binary input to the the next request.
139
- """
140
- self._add_input(ContainerType.BINARY, StepBinaryContainerPbo(items=input_data))
141
-
142
- def add_csv_input(self, input_data: list[dict[str, Any]]) -> None:
143
- """
144
- Add a CSV input to the next request.
145
- """
146
- csv_items = []
147
- for row in input_data:
148
- csv_items.append(StepCsvRowPbo(cells=[str(value) for value in row.values()]))
149
- header = StepCsvHeaderRowPbo(cells=list(input_data[0].keys()))
150
- self._add_input(ContainerType.CSV, StepCsvContainerPbo(header=header, items=csv_items))
151
-
152
- def add_json_input(self, input_data: list[dict[str, Any]]) -> None:
153
- """
154
- Add a JSON input to the next request.
155
- """
156
- self._add_input(ContainerType.JSON, StepJsonContainerPbo(items=[json.dumps(x) for x in input_data]))
157
-
158
- def add_text_input(self, input_data: list[str]) -> None:
159
- """
160
- Add a text input to the next request.
161
- """
162
- self._add_input(ContainerType.TEXT, StepTextContainerPbo(items=input_data))
163
-
164
- def clear_inputs(self) -> None:
165
- """
166
- Clear all inputs that have been added to the next request.
167
- This is useful if you want to start a new request without the previous inputs.
168
- """
169
- self._request_inputs.clear()
170
-
171
- def add_config_field(self, field_name: str, value: Any) -> None:
172
- """
173
- Add a configuration field value to the next request.
174
-
175
- :param field_name: The name of the configuration field.
176
- :param value: The value to set for the configuration field.
177
- """
178
- self._config_fields[field_name] = value
179
-
180
- def add_config_fields(self, config_fields: dict[str, Any]) -> None:
181
- """
182
- Add multiple configuration field values to the next request.
183
-
184
- :param config_fields: A dictionary of configuration field names and their corresponding values.
185
- """
186
- self._config_fields.update(config_fields)
187
-
188
- def clear_configs(self) -> None:
189
- """
190
- Clear all configuration field values that have been added to the next request.
191
- This is useful if you want to start a new request without the previous configurations.
192
- """
193
- self._config_fields.clear()
194
-
195
- def clear_request(self) -> None:
196
- """
197
- Clear all inputs and configuration fields that have been added to the next request.
198
- This is useful if you want to start a new request without the previous inputs and configurations.
199
- """
200
- self.clear_inputs()
201
- self.clear_configs()
202
-
203
- def _add_input(self, container_type: ContainerType, items: Any) -> None:
204
- """
205
- Helper method for adding inputs to the next request.
206
- """
207
- match container_type:
208
- # The content type doesn't matter when we're just testing.
209
- case ContainerType.BINARY:
210
- container = StepItemContainerPbo(content_type=ContentTypePbo(), binary_container=items)
211
- case ContainerType.CSV:
212
- container = StepItemContainerPbo(content_type=ContentTypePbo(), csv_container=items)
213
- case ContainerType.JSON:
214
- container = StepItemContainerPbo(content_type=ContentTypePbo(), json_container=items)
215
- case ContainerType.TEXT:
216
- container = StepItemContainerPbo(content_type=ContentTypePbo(), text_container=items)
217
- case _:
218
- raise ValueError(f"Unsupported data type: {container_type}")
219
- self._request_inputs.append(container)
220
-
221
- def get_service_details(self) -> ToolDetailsResponsePbo:
222
- """
223
- Get the details of the tools from the server.
224
-
225
- :return: A ToolDetailsResponsePbo object containing the details of the tool service.
226
- """
227
- with grpc.insecure_channel(self.grpc_server_url) as channel:
228
- stub = ToolServiceStub(channel)
229
- return stub.GetToolDetails(ToolDetailsRequestPbo(sapio_conn_info=self.connection))
230
-
231
- def call_tool(self, tool_name: str, is_dry_run: bool = False) -> ToolOutput:
232
- """
233
- Send the request to the tool service for a particular tool name. This will send all the inputs that have been
234
- added using the add_X_input functions.
235
-
236
- :param tool_name: The name of the tool to call on the server.
237
- :param is_dry_run: If True, the tool will not be executed, but the request will be validated.
238
- :return: A ToolOutput object containing the results of the tool service call.
239
- """
240
- with grpc.insecure_channel(self.grpc_server_url) as channel:
241
- stub = ToolServiceStub(channel)
242
-
243
- response: ProcessStepResponsePbo = stub.ProcessData(
244
- ProcessStepRequestPbo(
245
- sapio_user=self.connection,
246
- tool_name=tool_name,
247
- config_field_values=self._config_fields,
248
- dry_run=is_dry_run,
249
- verbose_logging=True,
250
- input=[
251
- StepInputBatchPbo(is_partial=False, item_container=item)
252
- for item in self._request_inputs
253
- ]
254
- )
255
- )
256
-
257
- results = ToolOutput(tool_name)
258
-
259
- match response.status:
260
- case ProcessStepResponseStatusPbo.SUCCESS:
261
- results.status = "Success"
262
- case ProcessStepResponseStatusPbo.FAILURE:
263
- results.status = "Failure"
264
- case _:
265
- results.status = "Unknown"
266
- results.message = response.status_message
267
-
268
- for item in response.output:
269
- container = item.item_container
270
-
271
- results.binary_output.extend(container.binary_container.items)
272
- for header in container.csv_container.header.cells:
273
- output_row: dict[str, Any] = {}
274
- for i, row in enumerate(container.csv_container.items):
275
- output_row[header] = row.cells[i]
276
- results.csv_output.append(output_row)
277
- results.json_output.extend([json.loads(x) for x in container.json_container.items])
278
- results.text_output.extend(container.text_container.items)
279
-
280
- for record in response.new_records:
281
- field_map: dict[str, Any] = {x: ProtobufUtils.field_pbo_to_value(y) for x, y in record.fields.items()}
282
- results.new_records.append(field_map)
283
-
284
- results.logs.extend(response.log)
285
-
286
- return results
287
-
288
-
289
- class TestConverterClient:
290
- """
291
- A client for testing a ConverterService.
292
- """
293
- grpc_server_url: str
294
-
295
- def __init__(self, grpc_server_url: str):
296
- """
297
- :param grpc_server_url: The URL of the gRPC server to connect to.
298
- """
299
- self.grpc_server_url = grpc_server_url
300
-
301
- def get_converter_details(self) -> ConverterDetailsResponsePbo:
302
- """
303
- Get the details of the converters from the server.
304
-
305
- :return: A ToolDetailsResponsePbo object containing the details of the converter service.
306
- """
307
- with grpc.insecure_channel(self.grpc_server_url) as channel:
308
- stub = ConverterServiceStub(channel)
309
- return stub.GetConverterDetails(ConverterDetailsRequestPbo())
310
-
311
- def convert_content(self, input_container: StepItemContainerPbo, target_type: ContentTypePbo) \
312
- -> StepItemContainerPbo:
313
- """
314
- Convert the content of the input container to the target content type.
315
-
316
- :param input_container: The input container to convert. This container must have a ContentTypePbo set that
317
- matches one of the input types that the converter service supports.
318
- :param target_type: The target content type to convert to. This must match one of the target types that the
319
- converter service supports.
320
- :return: A StepItemContainerPbo object containing the converted content.
321
- """
322
- with grpc.insecure_channel(self.grpc_server_url) as channel:
323
- stub = ConverterServiceStub(channel)
324
- response: ConvertResponsePbo = stub.ConvertContent(
325
- ConvertRequestPbo(item_container=input_container, target_content_type=target_type)
326
- )
327
- return response.item_container