sapiopycommons 2025.7.22a631__py3-none-any.whl → 2025.7.22a637__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 (33) hide show
  1. sapiopycommons/ai/__init__.py +0 -0
  2. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.py +43 -0
  3. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.pyi +31 -0
  4. sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2_grpc.py +24 -0
  5. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.py +123 -0
  6. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2.pyi +598 -0
  7. sapiopycommons/ai/api/fielddefinitions/proto/velox_field_def_pb2_grpc.py +24 -0
  8. sapiopycommons/ai/api/plan/proto/step_output_pb2.py +45 -0
  9. sapiopycommons/ai/api/plan/proto/step_output_pb2.pyi +42 -0
  10. sapiopycommons/ai/api/plan/proto/step_output_pb2_grpc.py +24 -0
  11. sapiopycommons/ai/api/plan/proto/step_pb2.py +43 -0
  12. sapiopycommons/ai/api/plan/proto/step_pb2.pyi +43 -0
  13. sapiopycommons/ai/api/plan/proto/step_pb2_grpc.py +24 -0
  14. sapiopycommons/ai/api/plan/script/proto/script_pb2.py +55 -0
  15. sapiopycommons/ai/api/plan/script/proto/script_pb2.pyi +115 -0
  16. sapiopycommons/ai/api/plan/script/proto/script_pb2_grpc.py +153 -0
  17. sapiopycommons/ai/api/plan/tool/proto/entry_pb2.py +57 -0
  18. sapiopycommons/ai/api/plan/tool/proto/entry_pb2.pyi +96 -0
  19. sapiopycommons/ai/api/plan/tool/proto/entry_pb2_grpc.py +24 -0
  20. sapiopycommons/ai/api/plan/tool/proto/tool_pb2.py +71 -0
  21. sapiopycommons/ai/api/plan/tool/proto/tool_pb2.pyi +250 -0
  22. sapiopycommons/ai/api/plan/tool/proto/tool_pb2_grpc.py +154 -0
  23. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.py +39 -0
  24. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2.pyi +32 -0
  25. sapiopycommons/ai/api/session/proto/sapio_conn_info_pb2_grpc.py +24 -0
  26. sapiopycommons/ai/protobuf_utils.py +509 -0
  27. sapiopycommons/ai/test_client.py +260 -0
  28. sapiopycommons/ai/tool_service_base.py +919 -0
  29. sapiopycommons/callbacks/callback_util.py +2 -5
  30. {sapiopycommons-2025.7.22a631.dist-info → sapiopycommons-2025.7.22a637.dist-info}/METADATA +1 -1
  31. {sapiopycommons-2025.7.22a631.dist-info → sapiopycommons-2025.7.22a637.dist-info}/RECORD +33 -5
  32. {sapiopycommons-2025.7.22a631.dist-info → sapiopycommons-2025.7.22a637.dist-info}/WHEEL +0 -0
  33. {sapiopycommons-2025.7.22a631.dist-info → sapiopycommons-2025.7.22a637.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,260 @@
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
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
+
17
+
18
+ # FR-47422: Created class.
19
+ class ToolOutput:
20
+ """
21
+ A class for holding the output of a TestClient that calls a ToolService. ToolOutput objects an be
22
+ printed to show the output of the tool in a human-readable format.
23
+ """
24
+ tool_name: str
25
+
26
+ binary_output: list[bytes]
27
+ csv_output: list[dict[str, Any]]
28
+ json_output: list[Any]
29
+ image_output: list[bytes]
30
+ text_output: list[str]
31
+
32
+ new_records: list[Mapping[str, FieldValuePbo]]
33
+
34
+ logs: list[str]
35
+
36
+ def __init__(self, tool_name: str):
37
+ self.tool_name = tool_name
38
+ self.binary_output = []
39
+ self.csv_output = []
40
+ self.json_output = []
41
+ self.image_output = []
42
+ self.text_output = []
43
+ self.new_records = []
44
+ self.logs = []
45
+
46
+ def __str__(self):
47
+ ret_val: str = f"{self.tool_name} Output:\n"
48
+ ret_val += "-" * 25 + "\n"
49
+ ret_val += f"Binary Output: {len(self.binary_output)} item(s)\n"
50
+ for binary in self.binary_output:
51
+ ret_val += f"\t{len(binary)} byte(s)\n"
52
+ ret_val += f"\t{binary[:50]}...\n"
53
+ ret_val += f"CSV Output: {len(self.csv_output)} item(s)\n"
54
+ if self.csv_output:
55
+ ret_val += f"\tHeaders: {', '.join(self.csv_output[0].keys())}\n"
56
+ for i, csv_row in enumerate(self.csv_output):
57
+ ret_val += f"\t{i}: {', '.join(f'{v}' for k, v in csv_row.items())}\n"
58
+ ret_val += f"JSON Output: {len(self.json_output)} item(s)\n"
59
+ if self.json_output:
60
+ ret_val += f"\t{json.dumps(self.json_output, indent=2)}\n"
61
+ ret_val += f"Image Output: {len(self.image_output)} item(s)\n"
62
+ for image in self.image_output:
63
+ ret_val += f"\t{len(image)} bytes\n"
64
+ ret_val += f"\t{image[:50]}...\n"
65
+ ret_val += f"Text Output: {len(self.text_output)} item(s)\n"
66
+ for text in self.text_output:
67
+ ret_val += f"\t{text}...\n"
68
+ ret_val += f"New Records: {len(self.new_records)} item(s)\n"
69
+ for record in self.new_records:
70
+ ret_val += f"\t{json.dumps(record, indent=2)}\n"
71
+ ret_val += f"Logs: {len(self.logs)} item(s)\n"
72
+ for log in self.logs:
73
+ ret_val += f"\t{log}\n"
74
+ return ret_val
75
+
76
+
77
+ class TestClient:
78
+ """
79
+ A client for testing a ToolService. This client can be used to send requests to a tool and receive
80
+ responses.
81
+ """
82
+ grpc_server_url: str
83
+ connection: SapioConnectionInfoPbo
84
+ _request_inputs: list[Any]
85
+ _config_fields: dict[str, Any]
86
+
87
+ def __init__(self, grpc_server_url: str, user: SapioUser | None = None):
88
+ """
89
+ :param grpc_server_url: The URL of the gRPC server to connect to.
90
+ :param user: Optional SapioUser object to use for the connection. If not provided, a default connection
91
+ will be created with test credentials.
92
+ """
93
+ self.grpc_server_url = grpc_server_url
94
+ self._create_connection(user)
95
+ self._request_inputs = []
96
+ self._config_fields = {}
97
+
98
+ def _create_connection(self, user: SapioUser | None = None):
99
+ """
100
+ Create a SapioConnectionInfoPbo object with test credentials. This method can be overridden to
101
+ create a user with specific credentials for testing.
102
+ """
103
+ self.connection = SapioConnectionInfoPbo()
104
+ self.connection.username = user.username if user else "Testing"
105
+ self.connection.webservice_url = user.url if user else "https://localhost:8080/webservice/api"
106
+ self.connection.app_guid = user.guid if user else "1234567890"
107
+ self.connection.rmi_host.append("Testing")
108
+ self.connection.rmi_port = 9001
109
+ if user and user.password:
110
+ self.connection.secret_type = SapioUserSecretTypePbo.PASSWORD
111
+ self.connection.secret = "Basic " + base64.b64encode(f'{user.username}:{user.password}'.encode()).decode()
112
+ else:
113
+ self.connection.secret_type = SapioUserSecretTypePbo.API_TOKEN
114
+ self.connection.secret = user.api_token if user and user.api_token else "test_api_token"
115
+
116
+ def add_binary_input(self, input_data: list[bytes]) -> None:
117
+ """
118
+ Add a binary input to the the next request.
119
+ """
120
+ self._add_input(DataTypePbo.BINARY, StepBinaryContainerPbo(items=input_data))
121
+
122
+ def add_csv_input(self, input_data: list[dict[str, Any]]) -> None:
123
+ """
124
+ Add a CSV input to the next request.
125
+ """
126
+ csv_items = []
127
+ for row in input_data:
128
+ csv_items.append(StepCsvRowPbo(cells=[str(value) for value in row.values()]))
129
+ header = StepCsvHeaderRowPbo(cells=list(input_data[0].keys()))
130
+ self._add_input(DataTypePbo.CSV, StepCsvContainerPbo(header=header, items=csv_items))
131
+
132
+ def add_json_input(self, input_data: list[dict[str, Any]]) -> None:
133
+ """
134
+ Add a JSON input to the next request.
135
+ """
136
+ self._add_input(DataTypePbo.JSON, StepJsonContainerPbo(items=[json.dumps(x) for x in input_data]))
137
+
138
+ def add_image_input(self, input_data: list[bytes], image_format: str = "png") -> None:
139
+ """
140
+ Add an image input to the next request.
141
+ """
142
+ self._add_input(DataTypePbo.IMAGE, StepImageContainerPbo(items=input_data, image_format=image_format))
143
+
144
+ def add_text_input(self, input_data: list[str]) -> None:
145
+ """
146
+ Add a text input to the next request.
147
+ """
148
+ self._add_input(DataTypePbo.TEXT, StepTextContainerPbo(items=input_data))
149
+
150
+ def clear_inputs(self) -> None:
151
+ """
152
+ Clear all inputs that have been added to the next request.
153
+ This is useful if you want to start a new request without the previous inputs.
154
+ """
155
+ self._request_inputs.clear()
156
+
157
+ def add_config_field(self, field_name: str, value: Any) -> None:
158
+ """
159
+ Add a configuration field value to the next request.
160
+
161
+ :param field_name: The name of the configuration field.
162
+ :param value: The value to set for the configuration field.
163
+ """
164
+ self._config_fields[field_name] = value
165
+
166
+ def add_config_fields(self, config_fields: dict[str, Any]) -> None:
167
+ """
168
+ Add multiple configuration field values to the next request.
169
+
170
+ :param config_fields: A dictionary of configuration field names and their corresponding values.
171
+ """
172
+ self._config_fields.update(config_fields)
173
+
174
+ def clear_configs(self) -> None:
175
+ """
176
+ Clear all configuration field values that have been added to the next request.
177
+ This is useful if you want to start a new request without the previous configurations.
178
+ """
179
+ self._config_fields.clear()
180
+
181
+ def clear_request(self) -> None:
182
+ """
183
+ Clear all inputs and configuration fields that have been added to the next request.
184
+ This is useful if you want to start a new request without the previous inputs and configurations.
185
+ """
186
+ self.clear_inputs()
187
+ self.clear_configs()
188
+
189
+ def _add_input(self, data_type: DataTypePbo, items: Any) -> None:
190
+ """
191
+ Helper method for adding inputs to the next request.
192
+ """
193
+ match data_type:
194
+ case DataTypePbo.BINARY:
195
+ container = StepItemContainerPbo(dataType=data_type, binary_container=items)
196
+ case DataTypePbo.CSV:
197
+ container = StepItemContainerPbo(dataType=data_type, csv_container=items)
198
+ case DataTypePbo.JSON:
199
+ container = StepItemContainerPbo(dataType=data_type, json_container=items)
200
+ case DataTypePbo.IMAGE:
201
+ container = StepItemContainerPbo(dataType=data_type, image_container=items)
202
+ case DataTypePbo.TEXT:
203
+ container = StepItemContainerPbo(dataType=data_type, text_container=items)
204
+ case _:
205
+ raise ValueError(f"Unsupported data type: {data_type}")
206
+ self._request_inputs.append(container)
207
+
208
+ def get_service_details(self) -> ToolDetailsResponsePbo:
209
+ """
210
+ Get the details of the tools from the server.
211
+
212
+ :return: A ToolDetailsResponsePbo object containing the details of the tool service.
213
+ """
214
+ with grpc.insecure_channel(self.grpc_server_url) as channel:
215
+ stub = ToolServiceStub(channel)
216
+ return stub.GetToolDetails(ToolDetailsRequestPbo(sapio_conn_info=self.connection))
217
+
218
+ def call_tool(self, tool_name: str) -> ToolOutput:
219
+ """
220
+ Send the request to the tool service for a particular tool name. This will send all the inputs that have been
221
+ added using the add_X_input functions.
222
+
223
+ :param tool_name: The name of the tool to call on the server.
224
+ :return: A ToolOutput object containing the results of the tool service call.
225
+ """
226
+ with grpc.insecure_channel(self.grpc_server_url) as channel:
227
+ stub = ToolServiceStub(channel)
228
+
229
+ response: ProcessStepResponsePbo = stub.ProcessData(
230
+ ProcessStepRequestPbo(
231
+ sapio_user=self.connection,
232
+ tool_name=tool_name,
233
+ config_field_values=self._config_fields,
234
+ input=[
235
+ StepInputBatchPbo(is_partial=False, item_container=item)
236
+ for item in self._request_inputs
237
+ ]
238
+ )
239
+ )
240
+
241
+ results = ToolOutput(tool_name)
242
+ for item in response.output:
243
+ container = item.item_container
244
+
245
+ results.binary_output.extend(container.binary_container.items)
246
+ for header in container.csv_container.header.cells:
247
+ output_row: dict[str, Any] = {}
248
+ for i, row in enumerate(container.csv_container.items):
249
+ output_row[header] = row.cells[i]
250
+ results.csv_output.append(output_row)
251
+ results.json_output.extend([json.loads(x) for x in container.json_container.items])
252
+ results.image_output.extend(container.image_container.items)
253
+ results.text_output.extend(container.text_container.items)
254
+
255
+ for record in response.new_records:
256
+ results.new_records.append(record.fields)
257
+
258
+ results.logs.extend(response.log)
259
+
260
+ return results