forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter.py
More file actions
53 lines (45 loc) · 1.76 KB
/
Copy pathparameter.py
File metadata and controls
53 lines (45 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""Parameter classes for CopilotKit"""
from typing import TypedDict, Optional, Literal, List, Union, cast, Any
from typing_extensions import NotRequired
class SimpleParameter(TypedDict):
"""Simple parameter class"""
name: str
description: NotRequired[str]
required: NotRequired[bool]
type: NotRequired[Literal[
"number",
"boolean",
"number[]",
"boolean[]"
]]
class ObjectParameter(TypedDict):
"""Object parameter class"""
name: str
description: NotRequired[str]
required: NotRequired[bool]
type: Literal["object", "object[]"]
attributes: List['Parameter']
class StringParameter(TypedDict):
"""String parameter class"""
name: str
description: NotRequired[str]
required: NotRequired[bool]
type: Literal["string", "string[]"]
enum: NotRequired[List[str]]
Parameter = Union[SimpleParameter, ObjectParameter, StringParameter]
def normalize_parameters(parameters: Optional[List[Parameter]]) -> List[Parameter]:
"""Normalize the parameters to ensure they have the correct type and format."""
if parameters is None:
return []
return [_normalize_parameter(parameter) for parameter in parameters]
def _normalize_parameter(parameter: Parameter) -> Parameter:
"""Normalize a parameter to ensure it has the correct type and format."""
if not "type" in parameter:
cast(Any, parameter)['type'] = 'string'
if not 'required' in parameter:
parameter['required'] = True
if not 'description' in parameter:
parameter['description'] = ''
if 'type' in parameter and (parameter['type'] == 'object' or parameter['type'] == 'object[]'):
cast(Any, parameter)['attributes'] = normalize_parameters(parameter.get('attributes'))
return parameter