1
1
""" Type definitions for the simpletool package."""
2
- from typing import Literal , Any , Optional
2
+ from typing import Literal , Any , Optional , Union
3
3
from pydantic import BaseModel , ConfigDict , Field , field_validator , model_validator
4
4
from pydantic .networks import AnyUrl
5
5
import base64
6
6
7
+
7
8
# -------------------------------------------------------------------------------------------------
8
9
# --- CONTENT CLASSES -----------------------------------------------------------------------------
9
10
# -------------------------------------------------------------------------------------------------
@@ -16,12 +17,16 @@ class Content(BaseModel):
16
17
17
18
@model_validator (mode = 'before' )
18
19
@classmethod
19
- def _convert_camel_to_snake_names (cls , data ):
20
- if isinstance (data , dict ) and 'mimeType' in data :
21
- data ['mime_type' ] = data .pop ('mimeType' )
22
- if isinstance (data , dict ) and 'fileName' in data :
23
- data ['file_name' ] = data .pop ('fileName' )
24
- return data
20
+ def convert_field_names (cls , data : dict ) -> dict :
21
+ if not isinstance (data , dict ):
22
+ return data
23
+
24
+ field_mappings = {
25
+ 'mimeType' : 'mime_type' ,
26
+ 'fileName' : 'file_name'
27
+ }
28
+
29
+ return {field_mappings .get (k , k ): v for k , v in data .items ()}
25
30
26
31
27
32
class TextContent (Content ):
@@ -41,66 +46,66 @@ def validate_or_convert(cls, data):
41
46
class ImageContent (Content ):
42
47
"""Image content for a message."""
43
48
type : Literal ["image" ] = "image" # type: ignore
44
- data : str
49
+ image : str
45
50
mime_type : str | None = None
46
51
description : Optional [str ] | None = None
47
52
48
53
@model_validator (mode = 'before' )
49
54
@classmethod
50
- def validate_or_convert (cls , data ):
55
+ def validate_or_convert (cls , image ):
51
56
# If a string is passed directly, assume it's base64 data
52
- if isinstance (data , str ):
57
+ if isinstance (image , str ):
53
58
# Validate base64 encoding
54
59
try :
55
- base64 .b64decode (data , validate = True )
56
- return {"data " : data }
60
+ base64 .b64decode (image , validate = True )
61
+ return {"image " : image }
57
62
except Exception as e :
58
- raise ValueError ("Data must be a valid base64 encoded string" ) from e
59
- return data
63
+ raise ValueError ("Image must be a valid base64 encoded string" ) from e
64
+ return image
60
65
61
- @field_validator ('data ' )
66
+ @field_validator ('image ' )
62
67
@classmethod
63
68
def validate_base64 (cls , value ):
64
69
try :
65
70
base64 .b64decode (value , validate = True )
66
71
return value
67
72
except Exception as e :
68
- raise ValueError ("Data must be a valid base64 encoded string" ) from e
73
+ raise ValueError ("Image must be a valid base64 encoded string" ) from e
69
74
70
75
71
76
class FileContent (Content ):
72
77
type : Literal ["file" ] = "file" # type: ignore
73
- data : str
78
+ file : str
74
79
mime_type : str | None = None
75
80
file_name : Optional [str ] | None = None
76
81
description : Optional [str ] | None = None
77
82
78
83
@model_validator (mode = 'before' )
79
84
@classmethod
80
- def validate_or_convert (cls , data ):
85
+ def validate_or_convert (cls , file ):
81
86
# If a string is passed directly, assume it's base64 data
82
- if isinstance (data , str ):
87
+ if isinstance (file , str ):
83
88
# Validate base64 encoding
84
89
try :
85
- base64 .b64decode (data , validate = True )
86
- return {"data " : data }
90
+ base64 .b64decode (file , validate = True )
91
+ return {"file " : file }
87
92
except Exception as e :
88
- raise ValueError ("Data must be a valid base64 encoded string" ) from e
89
- return data
93
+ raise ValueError ("File must be a valid base64 encoded string" ) from e
94
+ return file
90
95
91
- @field_validator ('data ' )
96
+ @field_validator ('file ' )
92
97
@classmethod
93
98
def validate_base64 (cls , value ):
94
99
try :
95
100
base64 .b64decode (value , validate = True )
96
101
return value
97
102
except Exception as e :
98
- raise ValueError ("Data must be a valid base64 encoded string" ) from e
103
+ raise ValueError ("File must be a valid base64 encoded string" ) from e
99
104
100
105
101
106
class ResourceContent (Content ):
102
107
type : Literal ["resource" ] = "resource" # type: ignore
103
- uri : AnyUrl
108
+ uri : Union [ str , AnyUrl ]
104
109
name : str
105
110
description : Optional [str ] | None = None
106
111
mime_type : str | None = None
@@ -113,12 +118,19 @@ def validate_or_convert(cls, data):
113
118
return {"uri" : data , "name" : str (data )}
114
119
return data
115
120
121
+ @field_validator ('uri' )
122
+ @classmethod
123
+ def validate_uri (cls , v : str ) -> str :
124
+ """Ensure URI is accessible and returns valid status"""
125
+ # Add URI validation logic here
126
+ return v
127
+
116
128
117
129
class ErrorContent (Content ):
118
130
"""Error information for JSON-RPC error responses."""
119
131
type : Literal ["error" ] = "error" # type: ignore
120
132
code : int = Field (description = "A number that indicates the error type that occurred." )
121
- message : str = Field (description = "A short description of the error. The message SHOULD be limited to a concise single sentence." )
133
+ error : str = Field (description = "A short description of the error. The message SHOULD be limited to a concise single sentence." )
122
134
data : Any | None = Field (default = None , description = "Additional information about the error." )
123
135
model_config = ConfigDict (extra = "allow" )
124
136
0 commit comments