sapiopycommons 2025.5.12a519__py3-none-any.whl → 2025.5.13a523__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.
- sapiopycommons/ai/tool_service_base.py +70 -23
- {sapiopycommons-2025.5.12a519.dist-info → sapiopycommons-2025.5.13a523.dist-info}/METADATA +1 -1
- {sapiopycommons-2025.5.12a519.dist-info → sapiopycommons-2025.5.13a523.dist-info}/RECORD +5 -5
- {sapiopycommons-2025.5.12a519.dist-info → sapiopycommons-2025.5.13a523.dist-info}/WHEEL +0 -0
- {sapiopycommons-2025.5.12a519.dist-info → sapiopycommons-2025.5.13a523.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
|
+
import logging
|
|
4
5
|
import traceback
|
|
5
6
|
from abc import abstractmethod, ABC
|
|
7
|
+
from logging import Logger
|
|
6
8
|
from typing import Any, Iterable, Sequence
|
|
7
9
|
|
|
8
10
|
from grpc import ServicerContext
|
|
9
|
-
from sapiopylib.rest.User import SapioUser
|
|
11
|
+
from sapiopylib.rest.User import SapioUser, ensure_logger_initialized
|
|
10
12
|
from sapiopylib.rest.pojo.datatype.FieldDefinition import AbstractVeloxFieldDefinition
|
|
11
13
|
|
|
12
14
|
from sapiopycommons.ai.api.fielddefinitions.proto.fields_pb2 import FieldValueMapPbo, FieldValuePbo
|
|
@@ -305,8 +307,8 @@ class ToolServiceBase(ToolServiceServicer, ABC):
|
|
|
305
307
|
tool.setup(user, request, context)
|
|
306
308
|
results: list[SapioToolResult] = tool.run(user)
|
|
307
309
|
return True, results, tool.logs
|
|
308
|
-
except Exception:
|
|
309
|
-
tool.
|
|
310
|
+
except Exception as e:
|
|
311
|
+
tool.log_exception("Exception occurred during tool execution.", e)
|
|
310
312
|
return False, [], tool.logs
|
|
311
313
|
|
|
312
314
|
|
|
@@ -321,6 +323,7 @@ class ToolBase(ABC):
|
|
|
321
323
|
outputs: list[ToolOutputDetailsPbo]
|
|
322
324
|
configs: list[VeloxFieldDefPbo]
|
|
323
325
|
logs: list[str]
|
|
326
|
+
logger: Logger
|
|
324
327
|
|
|
325
328
|
user: SapioUser
|
|
326
329
|
request: ProcessStepRequestPbo
|
|
@@ -341,6 +344,8 @@ class ToolBase(ABC):
|
|
|
341
344
|
self.outputs = []
|
|
342
345
|
self.configs = []
|
|
343
346
|
self.logs = []
|
|
347
|
+
self.logger = logging.getLogger(f"ToolBase.{self.name}")
|
|
348
|
+
ensure_logger_initialized(self.logger)
|
|
344
349
|
|
|
345
350
|
def setup(self, user: SapioUser, request: ProcessStepRequestPbo, context: ServicerContext) -> None:
|
|
346
351
|
"""
|
|
@@ -432,8 +437,8 @@ class ToolBase(ABC):
|
|
|
432
437
|
"""
|
|
433
438
|
self.configs.append(ProtobufUtils.field_def_to_pbo(field))
|
|
434
439
|
|
|
435
|
-
def add_boolean_config_field(self, field_name: str, display_name: str, description: str, default_value: bool
|
|
436
|
-
|
|
440
|
+
def add_boolean_config_field(self, field_name: str, display_name: str, description: str, default_value: bool,
|
|
441
|
+
optional: bool = False) -> None:
|
|
437
442
|
"""
|
|
438
443
|
Add a boolean configuration field to the tool. This field will be used to configure the tool in the plan
|
|
439
444
|
manager.
|
|
@@ -442,13 +447,14 @@ class ToolBase(ABC):
|
|
|
442
447
|
:param display_name: The display name of the field.
|
|
443
448
|
:param description: The description of the field.
|
|
444
449
|
:param default_value: The default value of the field.
|
|
450
|
+
:param optional: If true, this field is optional. If false, this field is required.
|
|
445
451
|
"""
|
|
446
452
|
self.configs.append(VeloxFieldDefPbo(
|
|
447
453
|
data_field_type=FieldTypePbo.BOOLEAN,
|
|
448
454
|
data_field_name=field_name,
|
|
449
455
|
display_name=display_name,
|
|
450
456
|
description=description,
|
|
451
|
-
required=
|
|
457
|
+
required=not optional,
|
|
452
458
|
editable=True,
|
|
453
459
|
boolean_properties=BooleanPropertiesPbo(
|
|
454
460
|
default_value=default_value
|
|
@@ -456,7 +462,8 @@ class ToolBase(ABC):
|
|
|
456
462
|
))
|
|
457
463
|
|
|
458
464
|
def add_double_config_field(self, field_name: str, display_name: str, description: str, default_value: float,
|
|
459
|
-
min_value: float = -10.**120, max_value: float = 10.**120, precision: int = 2
|
|
465
|
+
min_value: float = -10.**120, max_value: float = 10.**120, precision: int = 2,
|
|
466
|
+
optional: bool = False) -> None:
|
|
460
467
|
"""
|
|
461
468
|
Add a double configuration field to the tool. This field will be used to configure the tool in the plan
|
|
462
469
|
manager.
|
|
@@ -468,13 +475,14 @@ class ToolBase(ABC):
|
|
|
468
475
|
:param min_value: The minimum value of the field.
|
|
469
476
|
:param max_value: The maximum value of the field.
|
|
470
477
|
:param precision: The precision of the field.
|
|
478
|
+
:param optional: If true, this field is optional. If false, this field is required.
|
|
471
479
|
"""
|
|
472
480
|
self.configs.append(VeloxFieldDefPbo(
|
|
473
481
|
data_field_type=FieldTypePbo.DOUBLE,
|
|
474
482
|
data_field_name=field_name,
|
|
475
483
|
display_name=display_name,
|
|
476
484
|
description=description,
|
|
477
|
-
required=
|
|
485
|
+
required=not optional,
|
|
478
486
|
editable=True,
|
|
479
487
|
double_properties=DoublePropertiesPbo(
|
|
480
488
|
default_value=default_value,
|
|
@@ -485,7 +493,8 @@ class ToolBase(ABC):
|
|
|
485
493
|
))
|
|
486
494
|
|
|
487
495
|
def add_integer_config_field(self, field_name: str, display_name: str, description: str,
|
|
488
|
-
default_value: int, min_value: int = -2**31, max_value: int = 2**31-1
|
|
496
|
+
default_value: int, min_value: int = -2**31, max_value: int = 2**31-1,
|
|
497
|
+
optional: bool = False) -> None:
|
|
489
498
|
"""
|
|
490
499
|
Add an integer configuration field to the tool. This field will be used to configure the tool in the plan
|
|
491
500
|
manager.
|
|
@@ -496,13 +505,14 @@ class ToolBase(ABC):
|
|
|
496
505
|
:param default_value: The default value of the field.
|
|
497
506
|
:param min_value: The minimum value of the field.
|
|
498
507
|
:param max_value: The maximum value of the field.
|
|
508
|
+
:param optional: If true, this field is optional. If false, this field is required.
|
|
499
509
|
"""
|
|
500
510
|
self.configs.append(VeloxFieldDefPbo(
|
|
501
511
|
data_field_type=FieldTypePbo.INTEGER,
|
|
502
512
|
data_field_name=field_name,
|
|
503
513
|
display_name=display_name,
|
|
504
514
|
description=description,
|
|
505
|
-
required=
|
|
515
|
+
required=not optional,
|
|
506
516
|
editable=True,
|
|
507
517
|
integer_properties=IntegerPropertiesPbo(
|
|
508
518
|
default_value=default_value,
|
|
@@ -512,7 +522,7 @@ class ToolBase(ABC):
|
|
|
512
522
|
))
|
|
513
523
|
|
|
514
524
|
def add_string_config_field(self, field_name: str, display_name: str, description: str,
|
|
515
|
-
default_value: str, max_length: int = 1000) -> None:
|
|
525
|
+
default_value: str, max_length: int = 1000, optional: bool = False) -> None:
|
|
516
526
|
"""
|
|
517
527
|
Add a string configuration field to the tool. This field will be used to configure the tool in the plan
|
|
518
528
|
manager.
|
|
@@ -522,13 +532,14 @@ class ToolBase(ABC):
|
|
|
522
532
|
:param description: The description of the field.
|
|
523
533
|
:param default_value: The default value of the field.
|
|
524
534
|
:param max_length: The maximum length of the field.
|
|
535
|
+
:param optional: If true, this field is optional. If false, this field is required.
|
|
525
536
|
"""
|
|
526
537
|
self.configs.append(VeloxFieldDefPbo(
|
|
527
538
|
data_field_type=FieldTypePbo.STRING,
|
|
528
539
|
data_field_name=field_name,
|
|
529
540
|
display_name=display_name,
|
|
530
541
|
description=description,
|
|
531
|
-
required=
|
|
542
|
+
required=not optional,
|
|
532
543
|
editable=True,
|
|
533
544
|
string_properties=StringPropertiesPbo(
|
|
534
545
|
default_value=default_value,
|
|
@@ -537,7 +548,7 @@ class ToolBase(ABC):
|
|
|
537
548
|
))
|
|
538
549
|
|
|
539
550
|
def add_list_config_field(self, field_name: str, display_name: str, description: str, default_value: str,
|
|
540
|
-
allowed_values: list[str], direct_edit: bool = False) -> None:
|
|
551
|
+
allowed_values: list[str], direct_edit: bool = False, optional: bool = False) -> None:
|
|
541
552
|
"""
|
|
542
553
|
Add a list configuration field to the tool. This field will be used to configure the tool in the plan
|
|
543
554
|
manager.
|
|
@@ -549,13 +560,14 @@ class ToolBase(ABC):
|
|
|
549
560
|
:param allowed_values: The list of allowed values for the field.
|
|
550
561
|
:param direct_edit: If true, the user can enter a value that is not in the list of allowed values. If false,
|
|
551
562
|
the user can only select from the list of allowed values.
|
|
563
|
+
:param optional: If true, this field is optional. If false, this field is required.
|
|
552
564
|
"""
|
|
553
565
|
self.configs.append(VeloxFieldDefPbo(
|
|
554
566
|
data_field_type=FieldTypePbo.SELECTION,
|
|
555
567
|
data_field_name=field_name,
|
|
556
568
|
display_name=display_name,
|
|
557
569
|
description=description,
|
|
558
|
-
required=
|
|
570
|
+
required=not optional,
|
|
559
571
|
editable=True,
|
|
560
572
|
selection_properties=SelectionPropertiesPbo(
|
|
561
573
|
default_value=default_value,
|
|
@@ -565,8 +577,8 @@ class ToolBase(ABC):
|
|
|
565
577
|
))
|
|
566
578
|
|
|
567
579
|
def add_multi_list_config_field(self, field_name: str, display_name: str, description: str,
|
|
568
|
-
default_value: list[str], allowed_values: list[str], direct_edit: bool = False
|
|
569
|
-
|
|
580
|
+
default_value: list[str], allowed_values: list[str], direct_edit: bool = False,
|
|
581
|
+
optional: bool = False) -> None:
|
|
570
582
|
"""
|
|
571
583
|
Add a multi-select list configuration field to the tool. This field will be used to configure the tool in the
|
|
572
584
|
plan manager.
|
|
@@ -578,13 +590,14 @@ class ToolBase(ABC):
|
|
|
578
590
|
:param allowed_values: The list of allowed values for the field.
|
|
579
591
|
:param direct_edit: If true, the user can enter a value that is not in the list of allowed values. If false,
|
|
580
592
|
the user can only select from the list of allowed values.
|
|
593
|
+
:param optional: If true, this field is optional. If false, this field is required.
|
|
581
594
|
"""
|
|
582
595
|
self.configs.append(VeloxFieldDefPbo(
|
|
583
596
|
data_field_type=FieldTypePbo.SELECTION,
|
|
584
597
|
data_field_name=field_name,
|
|
585
598
|
display_name=display_name,
|
|
586
599
|
description=description,
|
|
587
|
-
required=
|
|
600
|
+
required=not optional,
|
|
588
601
|
editable=True,
|
|
589
602
|
selection_properties=SelectionPropertiesPbo(
|
|
590
603
|
default_value=",".join(default_value),
|
|
@@ -624,16 +637,50 @@ class ToolBase(ABC):
|
|
|
624
637
|
"""
|
|
625
638
|
pass
|
|
626
639
|
|
|
627
|
-
def
|
|
640
|
+
def log_info(self, message: str) -> None:
|
|
641
|
+
"""
|
|
642
|
+
Log an info message for this tool. If verbose logging is enabled, this message will be included in the logs
|
|
643
|
+
returned to the caller.
|
|
644
|
+
.
|
|
645
|
+
|
|
646
|
+
:param message: The message to log.
|
|
647
|
+
"""
|
|
648
|
+
msg = f"INFO: {self.name}: {message}"
|
|
649
|
+
if self.verbose_logging:
|
|
650
|
+
self.logs.append(msg)
|
|
651
|
+
self.logger.info(msg)
|
|
652
|
+
|
|
653
|
+
def log_warning(self, message: str) -> None:
|
|
654
|
+
"""
|
|
655
|
+
Log a warning message for this tool. This message will be included in the logs returned to the caller.
|
|
656
|
+
|
|
657
|
+
:param message: The message to log.
|
|
658
|
+
"""
|
|
659
|
+
msg = f"WARNING: {self.name}: {message}"
|
|
660
|
+
self.logs.append(msg)
|
|
661
|
+
self.logger.warning(msg)
|
|
662
|
+
|
|
663
|
+
def log_error(self, message: str) -> None:
|
|
664
|
+
"""
|
|
665
|
+
Log an error message for this tool. This message will be included in the logs returned to the caller.
|
|
666
|
+
|
|
667
|
+
:param message: The message to log.
|
|
668
|
+
"""
|
|
669
|
+
msg = f"ERROR: {self.name}: {message}"
|
|
670
|
+
self.logs.append(msg)
|
|
671
|
+
self.logger.error(msg)
|
|
672
|
+
|
|
673
|
+
def log_exception(self, message: str, e: Exception) -> None:
|
|
628
674
|
"""
|
|
629
|
-
Log
|
|
675
|
+
Log an exception for this tool. This message will be included in the logs returned to the caller.
|
|
630
676
|
|
|
631
677
|
:param message: The message to log.
|
|
632
|
-
:param
|
|
633
|
-
will be logged regardless of the verbose logging setting.
|
|
678
|
+
:param e: The exception to log.
|
|
634
679
|
"""
|
|
635
|
-
|
|
636
|
-
|
|
680
|
+
msg = f"EXCEPTION: {self.name}: {message} - {e}"
|
|
681
|
+
self.logs.append(msg)
|
|
682
|
+
msg += "\n" + traceback.format_exc()
|
|
683
|
+
self.logger.error(msg)
|
|
637
684
|
|
|
638
685
|
def get_input_binary(self, index: int = 0) -> list[bytes]:
|
|
639
686
|
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: sapiopycommons
|
|
3
|
-
Version: 2025.5.
|
|
3
|
+
Version: 2025.5.13a523
|
|
4
4
|
Summary: Official Sapio Python API Utilities Package
|
|
5
5
|
Project-URL: Homepage, https://github.com/sapiosciences
|
|
6
6
|
Author-email: Jonathan Steck <jsteck@sapiosciences.com>, Yechen Qiao <yqiao@sapiosciences.com>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
sapiopycommons/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
2
|
sapiopycommons/ai/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
3
|
sapiopycommons/ai/protobuf_utils.py,sha256=aKmZtNLrrzUe_LFsdW0zYpG4VI-ZGgukyXVDFtIqfes,22656
|
|
4
|
-
sapiopycommons/ai/tool_service_base.py,sha256=
|
|
4
|
+
sapiopycommons/ai/tool_service_base.py,sha256=adsC1kesRZJGdUmvcp1KrXgQ1RuLE7OMnPwRUsaP8DM,34578
|
|
5
5
|
sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.py,sha256=YcZjb_YM-XeLErM8hEC_S7vGMVGvcXAMGs2b-u5zvOE,2377
|
|
6
6
|
sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2.pyi,sha256=FwtXmNAf7iYGEFm4kbqb04v77jNHbZg18ZmEDhle_bU,1444
|
|
7
7
|
sapiopycommons/ai/api/fielddefinitions/proto/fields_pb2_grpc.py,sha256=wPImJPdCUZNVEVoUWzsba9kGIXjEKPdUkawP5SnVyiU,932
|
|
@@ -85,7 +85,7 @@ sapiopycommons/webhook/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3
|
|
|
85
85
|
sapiopycommons/webhook/webhook_context.py,sha256=D793uLsb1691SalaPnBUk3rOSxn_hYLhdvkaIxjNXss,1909
|
|
86
86
|
sapiopycommons/webhook/webhook_handlers.py,sha256=L0HetSm43NvA5KyW3xbLpGFh2DbAaeZJVtXIEl2fvV8,39689
|
|
87
87
|
sapiopycommons/webhook/webservice_handlers.py,sha256=Y5dHx_UFWFuSqaoPL6Re-fsKYRuxvCWZ8bj6KSZ3jfM,14285
|
|
88
|
-
sapiopycommons-2025.5.
|
|
89
|
-
sapiopycommons-2025.5.
|
|
90
|
-
sapiopycommons-2025.5.
|
|
91
|
-
sapiopycommons-2025.5.
|
|
88
|
+
sapiopycommons-2025.5.13a523.dist-info/METADATA,sha256=QqPGY0nDOqra3OvNj_CbyA5SNwYfSzf8DOZm1HbjPfo,3143
|
|
89
|
+
sapiopycommons-2025.5.13a523.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
90
|
+
sapiopycommons-2025.5.13a523.dist-info/licenses/LICENSE,sha256=HyVuytGSiAUQ6ErWBHTqt1iSGHhLmlC8fO7jTCuR8dU,16725
|
|
91
|
+
sapiopycommons-2025.5.13a523.dist-info/RECORD,,
|
|
File without changes
|
{sapiopycommons-2025.5.12a519.dist-info → sapiopycommons-2025.5.13a523.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|