11try :
2- from vertexai import types
2+ from vertexai import types as vertexai_types
33except ImportError as e :
44 raise ImportError (
55 'vertex_task_converter requires vertexai. '
2323 TextPart ,
2424)
2525
26+ _TO_SDK_TASK_STATE = {
27+ vertexai_types .State .STATE_UNSPECIFIED : TaskState .unknown ,
28+ vertexai_types .State .SUBMITTED : TaskState .submitted ,
29+ vertexai_types .State .WORKING : TaskState .working ,
30+ vertexai_types .State .COMPLETED : TaskState .completed ,
31+ vertexai_types .State .CANCELLED : TaskState .canceled ,
32+ vertexai_types .State .FAILED : TaskState .failed ,
33+ vertexai_types .State .REJECTED : TaskState .rejected ,
34+ vertexai_types .State .INPUT_REQUIRED : TaskState .input_required ,
35+ vertexai_types .State .AUTH_REQUIRED : TaskState .auth_required ,
36+ }
2637
27- def to_sdk_task_state (stored_state : types .State ) -> TaskState :
38+ _SDK_TO_STORED_TASK_STATE = {v : k for k , v in _TO_SDK_TASK_STATE .items ()}
39+
40+
41+ def to_sdk_task_state (stored_state : vertexai_types .State ) -> TaskState :
2842 """Converts a proto A2aTask.State to a TaskState enum."""
29- return {
30- types .State .STATE_UNSPECIFIED : TaskState .unknown ,
31- types .State .SUBMITTED : TaskState .submitted ,
32- types .State .WORKING : TaskState .working ,
33- types .State .COMPLETED : TaskState .completed ,
34- types .State .CANCELLED : TaskState .canceled ,
35- types .State .FAILED : TaskState .failed ,
36- types .State .REJECTED : TaskState .rejected ,
37- types .State .INPUT_REQUIRED : TaskState .input_required ,
38- types .State .AUTH_REQUIRED : TaskState .auth_required ,
39- }.get (stored_state , TaskState .unknown )
40-
41-
42- def to_stored_task_state (task_state : TaskState ) -> types .State :
43+ return _TO_SDK_TASK_STATE .get (stored_state , TaskState .unknown )
44+
45+
46+ def to_stored_task_state (task_state : TaskState ) -> vertexai_types .State :
4347 """Converts a TaskState enum to a proto A2aTask.State enum value."""
44- return {
45- TaskState .unknown : types .State .STATE_UNSPECIFIED ,
46- TaskState .submitted : types .State .SUBMITTED ,
47- TaskState .working : types .State .WORKING ,
48- TaskState .completed : types .State .COMPLETED ,
49- TaskState .canceled : types .State .CANCELLED ,
50- TaskState .failed : types .State .FAILED ,
51- TaskState .rejected : types .State .REJECTED ,
52- TaskState .input_required : types .State .INPUT_REQUIRED ,
53- TaskState .auth_required : types .State .AUTH_REQUIRED ,
54- }.get (task_state , types .State .STATE_UNSPECIFIED )
55-
56-
57- def to_stored_part (part : Part ) -> types .Part :
48+ return _SDK_TO_STORED_TASK_STATE .get (
49+ task_state , vertexai_types .State .STATE_UNSPECIFIED
50+ )
51+
52+
53+ def to_stored_part (part : Part ) -> vertexai_types .Part :
5854 """Converts a SDK Part to a proto Part."""
5955 if isinstance (part .root , TextPart ):
60- return types .Part (text = part .root .text )
56+ return vertexai_types .Part (text = part .root .text )
6157 if isinstance (part .root , DataPart ):
6258 data_bytes = json .dumps (part .root .data ).encode ('utf-8' )
63- return types .Part (
64- inline_data = types .Blob (
59+ return vertexai_types .Part (
60+ inline_data = vertexai_types .Blob (
6561 mime_type = 'application/json' , data = data_bytes
6662 )
6763 )
6864 if isinstance (part .root , FilePart ):
6965 file_content = part .root .file
7066 if isinstance (file_content , FileWithBytes ):
7167 decoded_bytes = base64 .b64decode (file_content .bytes )
72- return types .Part (
73- inline_data = types .Blob (
68+ return vertexai_types .Part (
69+ inline_data = vertexai_types .Blob (
7470 mime_type = file_content .mime_type or '' , data = decoded_bytes
7571 )
7672 )
7773 if isinstance (file_content , FileWithUri ):
78- return types .Part (
79- file_data = types .FileData (
74+ return vertexai_types .Part (
75+ file_data = vertexai_types .FileData (
8076 mime_type = file_content .mime_type or '' ,
8177 file_uri = file_content .uri ,
8278 )
8379 )
8480 raise ValueError (f'Unsupported part type: { type (part .root )} ' )
8581
8682
87- def to_sdk_part (stored_part : types .Part ) -> Part :
83+ def to_sdk_part (stored_part : vertexai_types .Part ) -> Part :
8884 """Converts a proto Part to a SDK Part."""
8985 if stored_part .text :
9086 return Part (root = TextPart (text = stored_part .text ))
@@ -113,29 +109,29 @@ def to_sdk_part(stored_part: types.Part) -> Part:
113109 return Part (root = TextPart (text = '' ))
114110
115111
116- def to_stored_artifact (artifact : Artifact ) -> types .TaskArtifact :
112+ def to_stored_artifact (artifact : Artifact ) -> vertexai_types .TaskArtifact :
117113 """Converts a SDK Artifact to a proto TaskArtifact."""
118- return types .TaskArtifact (
114+ return vertexai_types .TaskArtifact (
119115 artifact_id = artifact .artifact_id ,
120116 parts = [to_stored_part (part ) for part in artifact .parts ],
121117 )
122118
123119
124- def to_sdk_artifact (stored_artifact : types .TaskArtifact ) -> Artifact :
120+ def to_sdk_artifact (stored_artifact : vertexai_types .TaskArtifact ) -> Artifact :
125121 """Converts a proto TaskArtifact to a SDK Artifact."""
126122 return Artifact (
127123 artifact_id = stored_artifact .artifact_id ,
128124 parts = [to_sdk_part (part ) for part in stored_artifact .parts ],
129125 )
130126
131127
132- def to_stored_task (task : Task ) -> types .A2aTask :
128+ def to_stored_task (task : Task ) -> vertexai_types .A2aTask :
133129 """Converts a SDK Task to a proto A2aTask."""
134- return types .A2aTask (
130+ return vertexai_types .A2aTask (
135131 context_id = task .context_id ,
136132 metadata = task .metadata ,
137133 state = to_stored_task_state (task .status .state ),
138- output = types .TaskOutput (
134+ output = vertexai_types .TaskOutput (
139135 artifacts = [
140136 to_stored_artifact (artifact )
141137 for artifact in task .artifacts or []
@@ -144,7 +140,7 @@ def to_stored_task(task: Task) -> types.A2aTask:
144140 )
145141
146142
147- def to_sdk_task (a2a_task : types .A2aTask ) -> Task :
143+ def to_sdk_task (a2a_task : vertexai_types .A2aTask ) -> Task :
148144 """Converts a proto A2aTask to a SDK Task."""
149145 return Task (
150146 id = a2a_task .name .split ('/' )[- 1 ],
0 commit comments