config_utils.py 1.3 KB

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