sapiopycommons 2025.7.30a660__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.30a660.dist-info → sapiopycommons-2025.7.31a664.dist-info}/METADATA +1 -1
  5. {sapiopycommons-2025.7.30a660.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 -506
  31. sapiopycommons/ai/test_client.py +0 -287
  32. sapiopycommons/ai/tool_service_base.py +0 -924
  33. {sapiopycommons-2025.7.30a660.dist-info → sapiopycommons-2025.7.31a664.dist-info}/WHEEL +0 -0
  34. {sapiopycommons-2025.7.30a660.dist-info → sapiopycommons-2025.7.31a664.dist-info}/licenses/LICENSE +0 -0
@@ -1,287 +0,0 @@
1
- import base64
2
- import json
3
- from typing import Any
4
-
5
- import grpc
6
- from sapiopylib.rest.User import SapioUser
7
-
8
- from sapiopycommons.ai.api.plan.tool.proto.entry_pb2 import DataTypePbo, StepBinaryContainerPbo, StepCsvRowPbo, \
9
- StepCsvHeaderRowPbo, StepCsvContainerPbo, StepJsonContainerPbo, StepImageContainerPbo, StepTextContainerPbo, \
10
- StepItemContainerPbo, StepInputBatchPbo
11
- from sapiopycommons.ai.api.plan.tool.proto.tool_pb2 import ProcessStepResponsePbo, ProcessStepRequestPbo, \
12
- ToolDetailsRequestPbo, ToolDetailsResponsePbo, ProcessStepResponseStatusPbo
13
- from sapiopycommons.ai.api.plan.tool.proto.tool_pb2_grpc import ToolServiceStub
14
- from sapiopycommons.ai.api.session.proto.sapio_conn_info_pb2 import SapioConnectionInfoPbo, SapioUserSecretTypePbo
15
- from sapiopycommons.ai.protobuf_utils import ProtobufUtils
16
- from sapiopycommons.general.aliases import FieldValue
17
-
18
-
19
- # FR-47422: Created class.
20
- class ToolOutput:
21
- """
22
- A class for holding the output of a TestClient that calls a ToolService. ToolOutput objects an be
23
- printed to show the output of the tool in a human-readable format.
24
- """
25
- tool_name: str
26
-
27
- status: str
28
- message: str
29
-
30
- binary_output: list[bytes]
31
- csv_output: list[dict[str, Any]]
32
- json_output: list[Any]
33
- image_output: list[bytes]
34
- text_output: list[str]
35
-
36
- new_records: list[dict[str, FieldValue]]
37
-
38
- logs: list[str]
39
-
40
- def __init__(self, tool_name: str):
41
- self.tool_name = tool_name
42
- self.binary_output = []
43
- self.csv_output = []
44
- self.json_output = []
45
- self.image_output = []
46
- self.text_output = []
47
- self.new_records = []
48
- self.logs = []
49
-
50
- def __str__(self):
51
- ret_val: str = f"{self.tool_name} Output:\n"
52
- ret_val += f"\tStatus: {self.status}\n"
53
- ret_val += f"\tMessage: {self.message}\n"
54
- ret_val += "-" * 25 + "\n"
55
-
56
- ret_val += f"Binary Output: {len(self.binary_output)} item(s)\n"
57
- for binary in self.binary_output:
58
- ret_val += f"\t{len(binary)} byte(s)\n"
59
- ret_val += f"\t{binary[:50]}...\n"
60
-
61
- ret_val += f"CSV Output: {len(self.csv_output)} item(s)\n"
62
- if self.csv_output:
63
- ret_val += f"\tHeaders: {', '.join(self.csv_output[0].keys())}\n"
64
- for i, csv_row in enumerate(self.csv_output):
65
- ret_val += f"\t{i}: {', '.join(f'{v}' for k, v in csv_row.items())}\n"
66
-
67
- ret_val += f"JSON Output: {len(self.json_output)} item(s)\n"
68
- if self.json_output:
69
- ret_val += f"{json.dumps(self.json_output, indent=2)}\n"
70
-
71
- ret_val += f"Image Output: {len(self.image_output)} item(s)\n"
72
- for image in self.image_output:
73
- ret_val += f"\t{len(image)} bytes\n"
74
- ret_val += f"\t{image[:50]}...\n"
75
-
76
- ret_val += f"Text Output: {len(self.text_output)} item(s)\n"
77
- for text in self.text_output:
78
- ret_val += f"\t{text}\n"
79
-
80
- ret_val += f"New Records: {len(self.new_records)} item(s)\n"
81
- for record in self.new_records:
82
- ret_val += f"{json.dumps(record, indent=2)}\n"
83
-
84
- ret_val += f"Logs: {len(self.logs)} item(s)\n"
85
- for log in self.logs:
86
- ret_val += f"\t{log}\n"
87
- return ret_val
88
-
89
-
90
- class TestClient:
91
- """
92
- A client for testing a ToolService. This client can be used to send requests to a tool and receive
93
- responses.
94
- """
95
- grpc_server_url: str
96
- connection: SapioConnectionInfoPbo
97
- _request_inputs: list[Any]
98
- _config_fields: dict[str, Any]
99
-
100
- def __init__(self, grpc_server_url: str, user: SapioUser | None = None):
101
- """
102
- :param grpc_server_url: The URL of the gRPC server to connect to.
103
- :param user: Optional SapioUser object to use for the connection. If not provided, a default connection
104
- will be created with test credentials.
105
- """
106
- self.grpc_server_url = grpc_server_url
107
- self._create_connection(user)
108
- self._request_inputs = []
109
- self._config_fields = {}
110
-
111
- def _create_connection(self, user: SapioUser | None = None):
112
- """
113
- Create a SapioConnectionInfoPbo object with test credentials. This method can be overridden to
114
- create a user with specific credentials for testing.
115
- """
116
- self.connection = SapioConnectionInfoPbo()
117
- self.connection.username = user.username if user else "Testing"
118
- self.connection.webservice_url = user.url if user else "https://localhost:8080/webservice/api"
119
- self.connection.app_guid = user.guid if user else "1234567890"
120
- self.connection.rmi_host.append("Testing")
121
- self.connection.rmi_port = 9001
122
- if user and user.password:
123
- self.connection.secret_type = SapioUserSecretTypePbo.PASSWORD
124
- self.connection.secret = "Basic " + base64.b64encode(f'{user.username}:{user.password}'.encode()).decode()
125
- else:
126
- self.connection.secret_type = SapioUserSecretTypePbo.SESSION_TOKEN
127
- self.connection.secret = user.api_token if user and user.api_token else "test_api_token"
128
-
129
- def add_binary_input(self, input_data: list[bytes]) -> None:
130
- """
131
- Add a binary input to the the next request.
132
- """
133
- self._add_input(DataTypePbo.BINARY, StepBinaryContainerPbo(items=input_data))
134
-
135
- def add_csv_input(self, input_data: list[dict[str, Any]]) -> None:
136
- """
137
- Add a CSV input to the next request.
138
- """
139
- csv_items = []
140
- for row in input_data:
141
- csv_items.append(StepCsvRowPbo(cells=[str(value) for value in row.values()]))
142
- header = StepCsvHeaderRowPbo(cells=list(input_data[0].keys()))
143
- self._add_input(DataTypePbo.CSV, StepCsvContainerPbo(header=header, items=csv_items))
144
-
145
- def add_json_input(self, input_data: list[dict[str, Any]]) -> None:
146
- """
147
- Add a JSON input to the next request.
148
- """
149
- self._add_input(DataTypePbo.JSON, StepJsonContainerPbo(items=[json.dumps(x) for x in input_data]))
150
-
151
- def add_image_input(self, input_data: list[bytes], image_format: str = "png") -> None:
152
- """
153
- Add an image input to the next request.
154
- """
155
- self._add_input(DataTypePbo.IMAGE, StepImageContainerPbo(items=input_data, image_format=image_format))
156
-
157
- def add_text_input(self, input_data: list[str]) -> None:
158
- """
159
- Add a text input to the next request.
160
- """
161
- self._add_input(DataTypePbo.TEXT, StepTextContainerPbo(items=input_data))
162
-
163
- def clear_inputs(self) -> None:
164
- """
165
- Clear all inputs that have been added to the next request.
166
- This is useful if you want to start a new request without the previous inputs.
167
- """
168
- self._request_inputs.clear()
169
-
170
- def add_config_field(self, field_name: str, value: Any) -> None:
171
- """
172
- Add a configuration field value to the next request.
173
-
174
- :param field_name: The name of the configuration field.
175
- :param value: The value to set for the configuration field.
176
- """
177
- self._config_fields[field_name] = value
178
-
179
- def add_config_fields(self, config_fields: dict[str, Any]) -> None:
180
- """
181
- Add multiple configuration field values to the next request.
182
-
183
- :param config_fields: A dictionary of configuration field names and their corresponding values.
184
- """
185
- self._config_fields.update(config_fields)
186
-
187
- def clear_configs(self) -> None:
188
- """
189
- Clear all configuration field values that have been added to the next request.
190
- This is useful if you want to start a new request without the previous configurations.
191
- """
192
- self._config_fields.clear()
193
-
194
- def clear_request(self) -> None:
195
- """
196
- Clear all inputs and configuration fields that have been added to the next request.
197
- This is useful if you want to start a new request without the previous inputs and configurations.
198
- """
199
- self.clear_inputs()
200
- self.clear_configs()
201
-
202
- def _add_input(self, data_type: DataTypePbo, items: Any) -> None:
203
- """
204
- Helper method for adding inputs to the next request.
205
- """
206
- match data_type:
207
- case DataTypePbo.BINARY:
208
- container = StepItemContainerPbo(dataType=data_type, binary_container=items)
209
- case DataTypePbo.CSV:
210
- container = StepItemContainerPbo(dataType=data_type, csv_container=items)
211
- case DataTypePbo.JSON:
212
- container = StepItemContainerPbo(dataType=data_type, json_container=items)
213
- case DataTypePbo.IMAGE:
214
- container = StepItemContainerPbo(dataType=data_type, image_container=items)
215
- case DataTypePbo.TEXT:
216
- container = StepItemContainerPbo(dataType=data_type, text_container=items)
217
- case _:
218
- raise ValueError(f"Unsupported data type: {data_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.image_output.extend(container.image_container.items)
279
- results.text_output.extend(container.text_container.items)
280
-
281
- for record in response.new_records:
282
- field_map: dict[str, Any] = {x: ProtobufUtils.field_pbo_to_value(y) for x, y in record.fields.items()}
283
- results.new_records.append(field_map)
284
-
285
- results.logs.extend(response.log)
286
-
287
- return results