config_utils.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from enum import Enum
  2. from types import UnionType
  3. from typing import get_args, get_origin
  4. OH_DEFAULT_AGENT = 'CodeActAgent'
  5. OH_MAX_ITERATIONS = 100
  6. class UndefinedString(str, Enum):
  7. UNDEFINED = 'UNDEFINED'
  8. def get_field_info(f):
  9. """Extract information about a dataclass field: type, optional, and default.
  10. Args:
  11. f: The field to extract information from.
  12. Returns: A dict with the field's type, whether it's optional, and its default value.
  13. """
  14. field_type = f.type
  15. optional = False
  16. # for types like str | None, find the non-None type and set optional to True
  17. # this is useful for the frontend to know if a field is optional
  18. # and to show the correct type in the UI
  19. # Note: this only works for UnionTypes with None as one of the types
  20. if get_origin(field_type) is UnionType:
  21. types = get_args(field_type)
  22. non_none_arg = next((t for t in types if t is not type(None)), None)
  23. if non_none_arg is not None:
  24. field_type = non_none_arg
  25. optional = True
  26. # type name in a pretty format
  27. type_name = (
  28. field_type.__name__ if hasattr(field_type, '__name__') else str(field_type)
  29. )
  30. # default is always present
  31. default = f.default
  32. # return a schema with the useful info for frontend
  33. return {'type': type_name.lower(), 'optional': optional, 'default': default}