sapiopycommons 2025.7.23a659__py3-none-any.whl → 2025.7.31a664__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 (34) hide show
  1. sapiopycommons/ai/tool_of_tools.py +809 -0
  2. sapiopycommons/callbacks/callback_util.py +5 -2
  3. sapiopycommons/webhook/webservice_handlers.py +1 -1
  4. {sapiopycommons-2025.7.23a659.dist-info → sapiopycommons-2025.7.31a664.dist-info}/METADATA +1 -1
  5. {sapiopycommons-2025.7.23a659.dist-info → sapiopycommons-2025.7.31a664.dist-info}/RECORD +7 -33
  6. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.py +0 -43
  7. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.pyi +0 -31
  8. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2_grpc.py +0 -24
  9. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.py +0 -123
  10. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.pyi +0 -598
  11. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2_grpc.py +0 -24
  12. sapiopycommons/ai/api/plan/proto/step_output_pb2.py +0 -45
  13. sapiopycommons/ai/api/plan/proto/step_output_pb2.pyi +0 -42
  14. sapiopycommons/ai/api/plan/proto/step_output_pb2_grpc.py +0 -24
  15. sapiopycommons/ai/api/plan/proto/step_pb2.py +0 -43
  16. sapiopycommons/ai/api/plan/proto/step_pb2.pyi +0 -43
  17. sapiopycommons/ai/api/plan/proto/step_pb2_grpc.py +0 -24
  18. sapiopycommons/ai/api/plan/script/proto/script_pb2.py +0 -55
  19. sapiopycommons/ai/api/plan/script/proto/script_pb2.pyi +0 -115
  20. sapiopycommons/ai/api/plan/script/proto/script_pb2_grpc.py +0 -153
  21. sapiopycommons/ai/api/plan/tool/proto/entry_pb2.py +0 -57
  22. sapiopycommons/ai/api/plan/tool/proto/entry_pb2.pyi +0 -96
  23. sapiopycommons/ai/api/plan/tool/proto/entry_pb2_grpc.py +0 -24
  24. sapiopycommons/ai/api/plan/tool/proto/tool_pb2.py +0 -71
  25. sapiopycommons/ai/api/plan/tool/proto/tool_pb2.pyi +0 -250
  26. sapiopycommons/ai/api/plan/tool/proto/tool_pb2_grpc.py +0 -154
  27. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.py +0 -39
  28. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.pyi +0 -32
  29. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2_grpc.py +0 -24
  30. sapiopycommons/ai/protobuf_utils.py +0 -508
  31. sapiopycommons/ai/test_client.py +0 -288
  32. sapiopycommons/ai/tool_service_base.py +0 -916
  33. {sapiopycommons-2025.7.23a659.dist-info → sapiopycommons-2025.7.31a664.dist-info}/WHEEL +0 -0
  34. {sapiopycommons-2025.7.23a659.dist-info → sapiopycommons-2025.7.31a664.dist-info}/licenses/LICENSE +0 -0
@@ -1,288 +0,0 @@
1
- import base64
2
- import json
3
- from typing import Mapping, Any
4
-
5
- import grpc
6
- from sapiopylib.rest.User import SapioUser
7
-
8
- from sapiopycommons.ai.api.fielddefinitions.proto.fields_pb2 import FieldValuePbo
9
- from sapiopycommons.ai.api.plan.tool.proto.entry_pb2 import DataTypePbo, StepBinaryContainerPbo, StepCsvRowPbo, \
10
- StepCsvHeaderRowPbo, StepCsvContainerPbo, StepJsonContainerPbo, StepImageContainerPbo, StepTextContainerPbo, \
11
- StepItemContainerPbo, StepInputBatchPbo
12
- from sapiopycommons.ai.api.plan.tool.proto.tool_pb2 import ProcessStepResponsePbo, ProcessStepRequestPbo, \
13
- ToolDetailsRequestPbo, ToolDetailsResponsePbo, ProcessStepResponseStatusPbo
14
- from sapiopycommons.ai.api.plan.tool.proto.tool_pb2_grpc import ToolServiceStub
15
- from sapiopycommons.ai.api.session.proto.sapio_conn_info_pb2 import SapioConnectionInfoPbo, SapioUserSecretTypePbo
16
- from sapiopycommons.ai.protobuf_utils import ProtobufUtils
17
- from sapiopycommons.general.aliases import FieldValue
18
-
19
-
20
- # FR-47422: Created class.
21
- class ToolOutput:
22
- """
23
- A class for holding the output of a TestClient that calls a ToolService. ToolOutput objects an be
24
- printed to show the output of the tool in a human-readable format.
25
- """
26
- tool_name: str
27
-
28
- status: str
29
- message: str
30
-
31
- binary_output: list[bytes]
32
- csv_output: list[dict[str, Any]]
33
- json_output: list[Any]
34
- image_output: list[bytes]
35
- text_output: list[str]
36
-
37
- new_records: list[dict[str, FieldValue]]
38
-
39
- logs: list[str]
40
-
41
- def __init__(self, tool_name: str):
42
- self.tool_name = tool_name
43
- self.binary_output = []
44
- self.csv_output = []
45
- self.json_output = []
46
- self.image_output = []
47
- self.text_output = []
48
- self.new_records = []
49
- self.logs = []
50
-
51
- def __str__(self):
52
- ret_val: str = f"{self.tool_name} Output:\n"
53
- ret_val += f"\tStatus: {self.status}\n"
54
- ret_val += f"\tMessage: {self.message}\n"
55
- ret_val += "-" * 25 + "\n"
56
-
57
- ret_val += f"Binary Output: {len(self.binary_output)} item(s)\n"
58
- for binary in self.binary_output:
59
- ret_val += f"\t{len(binary)} byte(s)\n"
60
- ret_val += f"\t{binary[:50]}...\n"
61
-
62
- ret_val += f"CSV Output: {len(self.csv_output)} item(s)\n"
63
- if self.csv_output:
64
- ret_val += f"\tHeaders: {', '.join(self.csv_output[0].keys())}\n"
65
- for i, csv_row in enumerate(self.csv_output):
66
- ret_val += f"\t{i}: {', '.join(f'{v}' for k, v in csv_row.items())}\n"
67
-
68
- ret_val += f"JSON Output: {len(self.json_output)} item(s)\n"
69
- if self.json_output:
70
- ret_val += f"{json.dumps(self.json_output, indent=2)}\n"
71
-
72
- ret_val += f"Image Output: {len(self.image_output)} item(s)\n"
73
- for image in self.image_output:
74
- ret_val += f"\t{len(image)} bytes\n"
75
- ret_val += f"\t{image[:50]}...\n"
76
-
77
- ret_val += f"Text Output: {len(self.text_output)} item(s)\n"
78
- for text in self.text_output:
79
- ret_val += f"\t{text}\n"
80
-
81
- ret_val += f"New Records: {len(self.new_records)} item(s)\n"
82
- for record in self.new_records:
83
- ret_val += f"{json.dumps(record, indent=2)}\n"
84
-
85
- ret_val += f"Logs: {len(self.logs)} item(s)\n"
86
- for log in self.logs:
87
- ret_val += f"\t{log}\n"
88
- return ret_val
89
-
90
-
91
- class TestClient:
92
- """
93
- A client for testing a ToolService. This client can be used to send requests to a tool and receive
94
- responses.
95
- """
96
- grpc_server_url: str
97
- connection: SapioConnectionInfoPbo
98
- _request_inputs: list[Any]
99
- _config_fields: dict[str, Any]
100
-
101
- def __init__(self, grpc_server_url: str, user: SapioUser | None = None):
102
- """
103
- :param grpc_server_url: The URL of the gRPC server to connect to.
104
- :param user: Optional SapioUser object to use for the connection. If not provided, a default connection
105
- will be created with test credentials.
106
- """
107
- self.grpc_server_url = grpc_server_url
108
- self._create_connection(user)
109
- self._request_inputs = []
110
- self._config_fields = {}
111
-
112
- def _create_connection(self, user: SapioUser | None = None):
113
- """
114
- Create a SapioConnectionInfoPbo object with test credentials. This method can be overridden to
115
- create a user with specific credentials for testing.
116
- """
117
- self.connection = SapioConnectionInfoPbo()
118
- self.connection.username = user.username if user else "Testing"
119
- self.connection.webservice_url = user.url if user else "https://localhost:8080/webservice/api"
120
- self.connection.app_guid = user.guid if user else "1234567890"
121
- self.connection.rmi_host.append("Testing")
122
- self.connection.rmi_port = 9001
123
- if user and user.password:
124
- self.connection.secret_type = SapioUserSecretTypePbo.PASSWORD
125
- self.connection.secret = "Basic " + base64.b64encode(f'{user.username}:{user.password}'.encode()).decode()
126
- else:
127
- self.connection.secret_type = SapioUserSecretTypePbo.SESSION_TOKEN
128
- self.connection.secret = user.api_token if user and user.api_token else "test_api_token"
129
-
130
- def add_binary_input(self, input_data: list[bytes]) -> None:
131
- """
132
- Add a binary input to the the next request.
133
- """
134
- self._add_input(DataTypePbo.BINARY, StepBinaryContainerPbo(items=input_data))
135
-
136
- def add_csv_input(self, input_data: list[dict[str, Any]]) -> None:
137
- """
138
- Add a CSV input to the next request.
139
- """
140
- csv_items = []
141
- for row in input_data:
142
- csv_items.append(StepCsvRowPbo(cells=[str(value) for value in row.values()]))
143
- header = StepCsvHeaderRowPbo(cells=list(input_data[0].keys()))
144
- self._add_input(DataTypePbo.CSV, StepCsvContainerPbo(header=header, items=csv_items))
145
-
146
- def add_json_input(self, input_data: list[dict[str, Any]]) -> None:
147
- """
148
- Add a JSON input to the next request.
149
- """
150
- self._add_input(DataTypePbo.JSON, StepJsonContainerPbo(items=[json.dumps(x) for x in input_data]))
151
-
152
- def add_image_input(self, input_data: list[bytes], image_format: str = "png") -> None:
153
- """
154
- Add an image input to the next request.
155
- """
156
- self._add_input(DataTypePbo.IMAGE, StepImageContainerPbo(items=input_data, image_format=image_format))
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(DataTypePbo.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, data_type: DataTypePbo, items: Any) -> None:
204
- """
205
- Helper method for adding inputs to the next request.
206
- """
207
- match data_type:
208
- case DataTypePbo.BINARY:
209
- container = StepItemContainerPbo(dataType=data_type, binary_container=items)
210
- case DataTypePbo.CSV:
211
- container = StepItemContainerPbo(dataType=data_type, csv_container=items)
212
- case DataTypePbo.JSON:
213
- container = StepItemContainerPbo(dataType=data_type, json_container=items)
214
- case DataTypePbo.IMAGE:
215
- container = StepItemContainerPbo(dataType=data_type, image_container=items)
216
- case DataTypePbo.TEXT:
217
- container = StepItemContainerPbo(dataType=data_type, text_container=items)
218
- case _:
219
- raise ValueError(f"Unsupported data type: {data_type}")
220
- self._request_inputs.append(container)
221
-
222
- def get_service_details(self) -> ToolDetailsResponsePbo:
223
- """
224
- Get the details of the tools from the server.
225
-
226
- :return: A ToolDetailsResponsePbo object containing the details of the tool service.
227
- """
228
- with grpc.insecure_channel(self.grpc_server_url) as channel:
229
- stub = ToolServiceStub(channel)
230
- return stub.GetToolDetails(ToolDetailsRequestPbo(sapio_conn_info=self.connection))
231
-
232
- def call_tool(self, tool_name: str, is_dry_run: bool = False) -> ToolOutput:
233
- """
234
- Send the request to the tool service for a particular tool name. This will send all the inputs that have been
235
- added using the add_X_input functions.
236
-
237
- :param tool_name: The name of the tool to call on the server.
238
- :param is_dry_run: If True, the tool will not be executed, but the request will be validated.
239
- :return: A ToolOutput object containing the results of the tool service call.
240
- """
241
- with grpc.insecure_channel(self.grpc_server_url) as channel:
242
- stub = ToolServiceStub(channel)
243
-
244
- response: ProcessStepResponsePbo = stub.ProcessData(
245
- ProcessStepRequestPbo(
246
- sapio_user=self.connection,
247
- tool_name=tool_name,
248
- config_field_values=self._config_fields,
249
- dry_run=is_dry_run,
250
- verbose_logging=True,
251
- input=[
252
- StepInputBatchPbo(is_partial=False, item_container=item)
253
- for item in self._request_inputs
254
- ]
255
- )
256
- )
257
-
258
- results = ToolOutput(tool_name)
259
-
260
- match response.status:
261
- case ProcessStepResponseStatusPbo.SUCCESS:
262
- results.status = "Success"
263
- case ProcessStepResponseStatusPbo.FAILURE:
264
- results.status = "Failure"
265
- case _:
266
- results.status = "Unknown"
267
- results.message = response.status_message
268
-
269
- for item in response.output:
270
- container = item.item_container
271
-
272
- results.binary_output.extend(container.binary_container.items)
273
- for header in container.csv_container.header.cells:
274
- output_row: dict[str, Any] = {}
275
- for i, row in enumerate(container.csv_container.items):
276
- output_row[header] = row.cells[i]
277
- results.csv_output.append(output_row)
278
- results.json_output.extend([json.loads(x) for x in container.json_container.items])
279
- results.image_output.extend(container.image_container.items)
280
- results.text_output.extend(container.text_container.items)
281
-
282
- for record in response.new_records:
283
- field_map: dict[str, Any] = {x: ProtobufUtils.field_pbo_to_value(y) for x, y in record.fields.items()}
284
- results.new_records.append(field_map)
285
-
286
- results.logs.extend(response.log)
287
-
288
- return results