a2a-sdk 0.7.0

Unofficial Google A2A SDK. Currently supports v0.3.0 of the specification.
Documentation
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
#![allow(clippy::redundant_closure_call)]
#![allow(clippy::needless_lifetimes)]
#![allow(clippy::match_single_binding)]
#![allow(clippy::clone_on_copy)]

mod jsonrpc;

use std::borrow::Cow;
use std::fmt::{Debug, Display, Formatter};

use serde::{Deserialize, Serialize};

use crate::jsonrpc::*;

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum JsonRpcMessage {
	Request(JsonRpcRequest<A2aRequest>),
	Response(JsonRpcResponse<A2aResponse>),
	Error(JsonRpcError),
}

impl JsonRpcMessage {
	pub fn response(&self) -> Option<&A2aResponse> {
		match self {
			JsonRpcMessage::Request(_) => None,
			JsonRpcMessage::Response(resp) => Some(&resp.result),
			JsonRpcMessage::Error(_) => None,
		}
	}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct JsonRpcError {
	pub jsonrpc: JsonRpcVersion2_0,
	pub id: RequestId,
	pub error: ErrorData,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ErrorCode(pub i32);

/// Error information for JSON-RPC error responses.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct ErrorData {
	/// The error type that occurred.
	pub code: ErrorCode,

	/// A short description of the error. The message SHOULD be limited to a concise single sentence.
	pub message: Cow<'static, str>,

	/// Additional information about the error. The value of this member is defined by the
	/// sender (e.g. detailed error information, nested errors etc.).
	#[serde(skip_serializing_if = "Option::is_none")]
	pub data: Option<serde_json::Value>,
}

// TODO: this is not complete, add the rest
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum A2aRequest {
	// Legacy request types (deprecated but kept for backward compatibility)
	SendTaskRequest(SendTaskRequest),
	SendSubscribeTaskRequest(SendSubscribeTaskRequest),
	TaskPushNotificationGetRequest(TaskPushNotificationGetRequest),
	TaskPushNotificationSetRequest(TaskPushNotificationSetRequest),
	TaskResubscribeRequest(TaskResubscribeRequest),

	// New request types (current version)
	SendMessageRequest(SendMessageRequest),
	SendStreamingMessageRequest(SendStreamingMessageRequest),
	GetTaskRequest(GetTaskRequest),
	CancelTaskRequest(CancelTaskRequest),
	SetTaskPushNotificationConfigRequest(SetTaskPushNotificationConfigRequest),
	GetTaskPushNotificationConfigRequest(GetTaskPushNotificationConfigRequest),
	TaskResubscriptionRequest(TaskResubscriptionRequest),
	ListTaskPushNotificationConfigRequest(ListTaskPushNotificationConfigRequest),
	DeleteTaskPushNotificationConfigRequest(DeleteTaskPushNotificationConfigRequest),
	GetAuthenticatedExtendedCardRequest(GetAuthenticatedExtendedCardRequest),
}

impl A2aRequest {
	pub fn method(&self) -> &'static str {
		match self {
			A2aRequest::SendTaskRequest(i) => i.method.as_string(),
			A2aRequest::SendSubscribeTaskRequest(i) => i.method.as_string(),
			A2aRequest::GetTaskRequest(i) => i.method.as_string(),
			A2aRequest::CancelTaskRequest(i) => i.method.as_string(),
			A2aRequest::TaskPushNotificationGetRequest(i) => i.method.as_string(),
			A2aRequest::TaskPushNotificationSetRequest(i) => i.method.as_string(),
			A2aRequest::TaskResubscribeRequest(i) => i.method.as_string(),
			A2aRequest::SendMessageRequest(i) => i.method.as_string(),
			A2aRequest::SendStreamingMessageRequest(i) => i.method.as_string(),
			A2aRequest::SetTaskPushNotificationConfigRequest(i) => i.method.as_string(),
			A2aRequest::GetTaskPushNotificationConfigRequest(i) => i.method.as_string(),
			A2aRequest::TaskResubscriptionRequest(i) => i.method.as_string(),
			A2aRequest::ListTaskPushNotificationConfigRequest(i) => i.method.as_string(),
			A2aRequest::DeleteTaskPushNotificationConfigRequest(i) => i.method.as_string(),
			A2aRequest::GetAuthenticatedExtendedCardRequest(i) => i.method.as_string(),
		}
	}
}

// TODO: this is not complete, add the rest
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum A2aResponse {
	// Legacy response types (deprecated but kept for backward compatibility)
	SendTaskResponse(Option<Task>),
	SendTaskUpdateResponse(SendTaskStreamingResponseResult),

	// New response types (current version)
	SendMessageResponse(SendMessageResponse),
	SendStreamingMessageResponse(SendStreamingMessageResponse),
	GetTaskResponse(GetTaskResponse),
	CancelTaskResponse(CancelTaskResponse),
	SetTaskPushNotificationConfigResponse(SetTaskPushNotificationConfigResponse),
	GetTaskPushNotificationConfigResponse(GetTaskPushNotificationConfigResponse),
	TaskResubscriptionResponse(TaskResubscriptionResponse),
	ListTaskPushNotificationConfigResponse(ListTaskPushNotificationConfigResponse),
	DeleteTaskPushNotificationConfigResponse(DeleteTaskPushNotificationConfigResponse),
}

impl A2aResponse {
	pub fn id(&self) -> Option<String> {
		match self {
			A2aResponse::SendTaskResponse(i) => i.as_ref().map(|i| i.id.clone()),
			A2aResponse::SendTaskUpdateResponse(SendTaskStreamingResponseResult::Status(i)) => {
				Some(i.id.clone())
			},
			A2aResponse::SendTaskUpdateResponse(SendTaskStreamingResponseResult::Artifact(i)) => {
				Some(i.id.clone())
			},
			A2aResponse::SendTaskUpdateResponse(SendTaskStreamingResponseResult::None) => None,
			A2aResponse::SendMessageResponse(i) => Some(i.id.to_string()),
			A2aResponse::SendStreamingMessageResponse(i) => Some(i.id.to_string()),
			A2aResponse::GetTaskResponse(i) => Some(i.id.to_string()),
			A2aResponse::CancelTaskResponse(i) => Some(i.id.to_string()),
			A2aResponse::SetTaskPushNotificationConfigResponse(i) => Some(i.id.to_string()),
			A2aResponse::GetTaskPushNotificationConfigResponse(i) => Some(i.id.to_string()),
			A2aResponse::TaskResubscriptionResponse(i) => Some(i.id.to_string()),
			A2aResponse::ListTaskPushNotificationConfigResponse(i) => Some(i.id.to_string()),
			A2aResponse::DeleteTaskPushNotificationConfigResponse(i) => Some(i.id.to_string()),
		}
	}
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentAuthentication {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub credentials: Option<String>,
	pub schemes: Vec<String>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, Default)]
pub struct AgentCapabilities {
	#[serde(
		rename = "pushNotifications",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub push_notifications: Option<bool>,
	#[serde(
		rename = "stateTransitionHistory",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub state_transition_history: Option<bool>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub streaming: Option<bool>,
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub extensions: Vec<AgentExtension>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentCard {
	// Legacy fields (deprecated but kept for backward compatibility)
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub authentication: Option<AgentAuthentication>,

	// Current fields
	#[serde(
		rename = "additionalInterfaces",
		default,
		skip_serializing_if = "Vec::is_empty"
	)]
	pub additional_interfaces: Vec<AgentInterface>,
	pub capabilities: AgentCapabilities,
	#[serde(rename = "defaultInputModes")]
	pub default_input_modes: Vec<String>,
	#[serde(rename = "defaultOutputModes")]
	pub default_output_modes: Vec<String>,
	pub description: String,
	#[serde(
		rename = "documentationUrl",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub documentation_url: Option<String>,
	#[serde(rename = "iconUrl", default, skip_serializing_if = "Option::is_none")]
	pub icon_url: Option<String>,
	pub name: String,
	#[serde(
		rename = "preferredTransport",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub preferred_transport: Option<String>,
	#[serde(rename = "protocolVersion")]
	pub protocol_version: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub provider: Option<AgentProvider>,
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub security: Vec<std::collections::HashMap<String, Vec<String>>>,
	#[serde(
		rename = "securitySchemes",
		default,
		skip_serializing_if = "std::collections::HashMap::is_empty"
	)]
	pub security_schemes: std::collections::HashMap<String, SecurityScheme>,
	#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
	pub signatures: ::std::vec::Vec<AgentCardSignature>,
	pub skills: Vec<AgentSkill>,
	#[serde(
		rename = "supportsAuthenticatedExtendedCard",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub supports_authenticated_extended_card: Option<bool>,
	pub url: String,
	pub version: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentCardSignature {
	#[serde(default, skip_serializing_if = "::serde_json::Map::is_empty")]
	pub header: serde_json::Map<String, serde_json::Value>,
	pub protected: String,
	pub signature: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentProvider {
	pub organization: String,
	pub url: String,
}

// New types for current schema
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentInterface {
	pub transport: String,
	pub url: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentExtension {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
	#[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
	pub params: serde_json::Map<String, serde_json::Value>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub required: Option<bool>,
	pub uri: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(tag = "type")]
pub enum SecurityScheme {
	#[serde(rename = "apiKey")]
	ApiKey(ApiKeySecurityScheme),
	#[serde(rename = "http")]
	HttpAuth(HttpAuthSecurityScheme),
	#[serde(rename = "oauth2")]
	OAuth2(Box<OAuth2SecurityScheme>),
	#[serde(rename = "openIdConnect")]
	OpenIdConnect(OpenIdConnectSecurityScheme),
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct ApiKeySecurityScheme {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
	#[serde(rename = "in")]
	pub in_: ApiKeySecuritySchemeIn,
	pub name: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub enum ApiKeySecuritySchemeIn {
	#[serde(rename = "cookie")]
	Cookie,
	#[serde(rename = "header")]
	Header,
	#[serde(rename = "query")]
	Query,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct HttpAuthSecurityScheme {
	#[serde(
		rename = "bearerFormat",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub bearer_format: Option<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
	pub scheme: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct OAuth2SecurityScheme {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
	pub flows: OAuthFlows,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct OAuthFlows {
	#[serde(
		rename = "authorizationCode",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub authorization_code: Option<AuthorizationCodeOAuthFlow>,
	#[serde(
		rename = "clientCredentials",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub client_credentials: Option<ClientCredentialsOAuthFlow>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub implicit: Option<ImplicitOAuthFlow>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub password: Option<PasswordOAuthFlow>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AuthorizationCodeOAuthFlow {
	#[serde(rename = "authorizationUrl")]
	pub authorization_url: String,
	#[serde(
		rename = "refreshUrl",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub refresh_url: Option<String>,
	pub scopes: std::collections::HashMap<String, String>,
	#[serde(rename = "tokenUrl")]
	pub token_url: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct ClientCredentialsOAuthFlow {
	#[serde(
		rename = "refreshUrl",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub refresh_url: Option<String>,
	pub scopes: std::collections::HashMap<String, String>,
	#[serde(rename = "tokenUrl")]
	pub token_url: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct ImplicitOAuthFlow {
	#[serde(rename = "authorizationUrl")]
	pub authorization_url: String,
	#[serde(
		rename = "refreshUrl",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub refresh_url: Option<String>,
	pub scopes: std::collections::HashMap<String, String>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct PasswordOAuthFlow {
	#[serde(
		rename = "refreshUrl",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub refresh_url: Option<String>,
	pub scopes: std::collections::HashMap<String, String>,
	#[serde(rename = "tokenUrl")]
	pub token_url: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct OpenIdConnectSecurityScheme {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
	#[serde(rename = "openIdConnectUrl")]
	pub open_id_connect_url: String,
	#[serde(rename = "type")]
	pub type_: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AgentSkill {
	// Current fields (new schema)
	pub id: String,
	pub name: String,
	#[serde(default, skip_serializing_if = "String::is_empty")]
	pub description: String, // Now required in new schema
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub examples: Vec<String>,
	#[serde(rename = "inputModes", default, skip_serializing_if = "Vec::is_empty")]
	pub input_modes: Vec<String>,
	#[serde(rename = "outputModes", default, skip_serializing_if = "Vec::is_empty")]
	pub output_modes: Vec<String>,
	#[serde(default, skip_serializing_if = "::std::vec::Vec::is_empty")]
	pub security: Vec<std::collections::HashMap<String, Vec<String>>>,
	pub tags: Vec<String>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct Artifact {
	#[serde(rename = "artifactId")]
	pub artifact_id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub description: Option<String>,
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub extensions: Vec<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub name: Option<String>,
	pub parts: Vec<Part>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct AuthenticationInfo {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub credentials: Option<String>,
	pub schemes: Vec<String>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct DataPart {
	pub data: serde_json::Map<String, serde_json::Value>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, Default)]
pub struct FileContent {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub bytes: Option<String>,
	#[serde(rename = "mimeType", default, skip_serializing_if = "Option::is_none")]
	pub mime_type: Option<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub name: Option<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub uri: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct FilePart {
	pub file: FileContent,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum Id {
	Variant0(i64),
	Variant1(String),
	Variant2,
}
impl From<i64> for Id {
	fn from(value: i64) -> Self {
		Self::Variant0(value)
	}
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct InternalError {
	pub code: i64,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub data: Option<serde_json::Value>,
	pub message: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct InvalidParamsError {
	pub code: i64,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub data: Option<serde_json::Value>,
	pub message: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct InvalidRequestError {
	pub code: i64,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub data: Option<serde_json::Value>,
	pub message: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct Message {
	// Legacy fields (deprecated but kept for backward compatibility)
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub content_legacy: Option<Vec<Part>>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub role_legacy: Option<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub timestamp_legacy: Option<chrono::NaiveDateTime>,

	// Current fields (new schema)
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub content: Vec<Part>,
	#[serde(default, skip_serializing_if = "String::is_empty")]
	pub role: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub timestamp: Option<String>, // Changed from chrono::NaiveDateTime to String in new schema
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct MethodNotFoundError {
	pub code: i64,
	pub data: serde_json::Value,
	pub message: String,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(tag = "kind")]
pub enum Part {
	Text(TextPart),
	File(FilePart),
	Data(DataPart),
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct PushNotificationConfig {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub authentication: Option<AuthenticationInfo>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub token: Option<String>,
	pub url: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct PushNotificationNotSupportedError {
	pub code: i64,
	pub data: serde_json::Value,
	pub message: String,
}
#[derive(
	serde::Deserialize, serde::Serialize, Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd,
)]
pub enum Role {
	#[serde(rename = "user")]
	User,
	#[serde(rename = "agent")]
	Agent,
}
impl Display for Role {
	fn fmt(&self, f: &mut Formatter<'_>) -> ::std::fmt::Result {
		match *self {
			Self::User => write!(f, "user"),
			Self::Agent => write!(f, "agent"),
		}
	}
}
impl ::std::str::FromStr for Role {
	type Err = self::error::ConversionError;
	fn from_str(value: &str) -> Result<Self, self::error::ConversionError> {
		match value {
			"user" => Ok(Self::User),
			"agent" => Ok(Self::Agent),
			_ => Err("invalid value".into()),
		}
	}
}
impl TryFrom<&str> for Role {
	type Error = self::error::ConversionError;
	fn try_from(value: &str) -> Result<Self, self::error::ConversionError> {
		value.parse()
	}
}
impl TryFrom<&String> for Role {
	type Error = self::error::ConversionError;
	fn try_from(value: &String) -> Result<Self, self::error::ConversionError> {
		value.parse()
	}
}
impl TryFrom<String> for Role {
	type Error = self::error::ConversionError;
	fn try_from(value: String) -> Result<Self, self::error::ConversionError> {
		value.parse()
	}
}

const_string!(CancelTaskRequestMethod = "tasks/cancel");
pub type CancelTaskRequest = Request<CancelTaskRequestMethod, TaskIdParams>;

const_string!(GetTaskRequestMethod = "tasks/get");
pub type GetTaskRequest = Request<GetTaskRequestMethod, TaskQueryParams>;

const_string!(SendTaskRequestMethod = "tasks/send");
pub type SendTaskRequest = Request<SendTaskRequestMethod, TaskSendParams>;

const_string!(SendSubscribeTaskRequestMethod = "tasks/sendSubscribe");
pub type SendSubscribeTaskRequest = Request<SendSubscribeTaskRequestMethod, TaskSendParams>;

const_string!(TaskResubscribeRequestMethod = "tasks/resubscribe");
pub type TaskResubscribeRequest = Request<TaskResubscribeRequestMethod, TaskQueryParams>;

const_string!(TaskPushNotificationGetRequestMethod = "tasks/pushNotification/get");
pub type TaskPushNotificationGetRequest =
	Request<TaskPushNotificationGetRequestMethod, TaskIdParams>;

const_string!(TaskPushNotificationSetRequestMethod = "tasks/pushNotification/set");
pub type TaskPushNotificationSetRequest =
	Request<TaskPushNotificationSetRequestMethod, TaskPushNotificationConfig>;

// New request types (current version)
const_string!(SendMessageRequestMethod = "message/send");
pub type SendMessageRequest = Request<SendMessageRequestMethod, MessageSendParams>;

const_string!(SendStreamingMessageRequestMethod = "message/stream");
pub type SendStreamingMessageRequest =
	Request<SendStreamingMessageRequestMethod, MessageSendParams>;

const_string!(SetTaskPushNotificationConfigRequestMethod = "tasks/pushNotificationConfig/set");
pub type SetTaskPushNotificationConfigRequest =
	Request<SetTaskPushNotificationConfigRequestMethod, TaskPushNotificationConfig>;

const_string!(GetTaskPushNotificationConfigRequestMethod = "tasks/pushNotificationConfig/get");
pub type GetTaskPushNotificationConfigRequest =
	Request<GetTaskPushNotificationConfigRequestMethod, GetTaskPushNotificationConfigParams>;

const_string!(TaskResubscriptionRequestMethod = "tasks/resubscribe");
pub type TaskResubscriptionRequest = Request<TaskResubscriptionRequestMethod, TaskIdParams>;

const_string!(ListTaskPushNotificationConfigRequestMethod = "tasks/pushNotificationConfig/list");
pub type ListTaskPushNotificationConfigRequest =
	Request<ListTaskPushNotificationConfigRequestMethod, TaskIdParams>;

const_string!(
	DeleteTaskPushNotificationConfigRequestMethod = "tasks/pushNotificationConfig/delete"
);
pub type DeleteTaskPushNotificationConfigRequest =
	Request<DeleteTaskPushNotificationConfigRequestMethod, DeleteTaskPushNotificationConfigParams>;

const_string!(GetAuthenticatedExtendedCardRequestMethod = "agent/getAuthenticatedExtendedCard");
pub type GetAuthenticatedExtendedCardRequest =
	Request<GetAuthenticatedExtendedCardRequestMethod, ()>;

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(untagged)]
#[derive(Default)]
pub enum SendTaskStreamingResponseResult {
	Status(TaskStatusUpdateEvent),
	Artifact(TaskArtifactUpdateEvent),
	#[default]
	None,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct Task {
	// Legacy fields (deprecated but kept for backward compatibility)
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub artifacts_legacy: Option<Vec<Artifact>>,
	#[serde(rename = "sessionId", default, skip_serializing_if = "Option::is_none")]
	pub session_id: Option<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub history_legacy: Option<Vec<Message>>,

	// Current fields (new schema)
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub artifacts: Vec<Artifact>,
	#[serde(rename = "contextId")]
	pub context_id: String,
	#[serde(default, skip_serializing_if = "Vec::is_empty")]
	pub history: Vec<Message>,
	pub id: String,
	#[serde(default, skip_serializing_if = "String::is_empty")]
	pub kind: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
	pub status: TaskStatus,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskArtifactUpdateEvent {
	pub artifact: Artifact,
	pub id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskIdParams {
	pub id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct DeleteTaskPushNotificationConfigParams {
	pub id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
	#[serde(rename = "pushNotificationConfigId")]
	pub push_notification_config_id: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct GetTaskPushNotificationConfigParams {
	pub id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
	#[serde(rename = "pushNotificationConfigId", skip_serializing_if = "Option::is_none")]
	pub push_notification_config_id: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskNotCancelableError {
	pub code: i64,
	pub data: serde_json::Value,
	pub message: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskNotFoundError {
	pub code: i64,
	pub data: serde_json::Value,
	pub message: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskPushNotificationConfig {
	pub id: String,
	#[serde(rename = "pushNotificationConfig")]
	pub push_notification_config: PushNotificationConfig,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskQueryParams {
	#[serde(
		rename = "historyLength",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub history_length: Option<i64>,
	pub id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskSendParams {
	#[serde(
		rename = "historyLength",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub history_length: Option<i64>,
	pub id: String,
	pub message: Message,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
	#[serde(
		rename = "pushNotification",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub push_notification: Option<PushNotificationConfig>,
	#[serde(rename = "sessionId", default, skip_serializing_if = "Option::is_none")]
	pub session_id: Option<String>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct MessageSendParams {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub configuration: Option<MessageSendConfiguration>,
	pub message: Message,
	#[serde(default, skip_serializing_if = "serde_json::Map::is_empty")]
	pub metadata: serde_json::Map<String, serde_json::Value>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct MessageSendConfiguration {
	#[serde(rename = "acceptedOutputModes")]
	pub accepted_output_modes: Vec<String>,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub blocking: Option<bool>,
	#[serde(
		rename = "historyLength",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub history_length: Option<i64>,
	#[serde(
		rename = "pushNotificationConfig",
		default,
		skip_serializing_if = "Option::is_none"
	)]
	pub push_notification_config: Option<PushNotificationConfig>,
}

#[derive(
	serde::Deserialize, serde::Serialize, Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd,
)]
pub enum TaskState {
	#[serde(rename = "submitted")]
	Submitted,
	#[serde(rename = "working")]
	Working,
	#[serde(rename = "input-required")]
	InputRequired,
	#[serde(rename = "completed")]
	Completed,
	#[serde(rename = "canceled")]
	Canceled,
	#[serde(rename = "failed")]
	Failed,
	#[serde(rename = "unknown")]
	Unknown,
	// New states from current schema
	#[serde(rename = "rejected")]
	Rejected,
	#[serde(rename = "auth-required")]
	AuthRequired,
}
impl Display for TaskState {
	fn fmt(&self, f: &mut Formatter<'_>) -> ::std::fmt::Result {
		match *self {
			Self::Submitted => write!(f, "submitted"),
			Self::Working => write!(f, "working"),
			Self::InputRequired => write!(f, "input-required"),
			Self::Completed => write!(f, "completed"),
			Self::Canceled => write!(f, "canceled"),
			Self::Failed => write!(f, "failed"),
			Self::Unknown => write!(f, "unknown"),
			Self::Rejected => write!(f, "rejected"),
			Self::AuthRequired => write!(f, "auth-required"),
		}
	}
}
impl ::std::str::FromStr for TaskState {
	type Err = self::error::ConversionError;
	fn from_str(value: &str) -> Result<Self, self::error::ConversionError> {
		match value {
			"submitted" => Ok(Self::Submitted),
			"working" => Ok(Self::Working),
			"input-required" => Ok(Self::InputRequired),
			"completed" => Ok(Self::Completed),
			"canceled" => Ok(Self::Canceled),
			"failed" => Ok(Self::Failed),
			"unknown" => Ok(Self::Unknown),
			"rejected" => Ok(Self::Rejected),
			"auth-required" => Ok(Self::AuthRequired),
			_ => Err("invalid value".into()),
		}
	}
}
impl TryFrom<&str> for TaskState {
	type Error = self::error::ConversionError;
	fn try_from(value: &str) -> Result<Self, self::error::ConversionError> {
		value.parse()
	}
}
impl TryFrom<&String> for TaskState {
	type Error = self::error::ConversionError;
	fn try_from(value: &String) -> Result<Self, self::error::ConversionError> {
		value.parse()
	}
}
impl TryFrom<String> for TaskState {
	type Error = self::error::ConversionError;
	fn try_from(value: String) -> Result<Self, self::error::ConversionError> {
		value.parse()
	}
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskStatus {
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub message: Option<Message>,
	pub state: TaskState,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub timestamp: Option<String>,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskStatusUpdateEvent {
	#[serde(rename = "final", default)]
	pub final_: bool,
	pub id: String,
	#[serde(default, skip_serializing_if = "Option::is_none")]
	pub metadata: Option<serde_json::Map<String, serde_json::Value>>,
	pub status: TaskStatus,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TextPart {
	pub text: String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct UnsupportedOperationError {
	pub code: i64,
	pub data: serde_json::Value,
	pub message: String,
}

pub mod error {
	use std::fmt::{Debug, Display, Formatter};
	pub struct ConversionError(::std::borrow::Cow<'static, str>);
	impl ::std::error::Error for ConversionError {}
	impl Display for ConversionError {
		fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), ::std::fmt::Error> {
			Display::fmt(&self.0, f)
		}
	}
	impl Debug for ConversionError {
		fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), ::std::fmt::Error> {
			Debug::fmt(&self.0, f)
		}
	}
	impl From<&'static str> for ConversionError {
		fn from(value: &'static str) -> Self {
			Self(value.into())
		}
	}
	impl From<String> for ConversionError {
		fn from(value: String) -> Self {
			Self(value.into())
		}
	}
}

#[cfg(test)]
mod tests {

	#[test]
	fn test_serde() {
		let js = serde_json::json! {
		{
			"jsonrpc": "2.0",
			"id": "d1306567eb364c7ba9e7a7b922dba672",
			"result": {
				"id": "8b34914c735a464986e1d5ce5b6ec478",
				"status": {
					"state": "completed",
					"message": {
						"role": "agent",
						"parts": [
							{
								"type": "text",
								"text": "Hello!"
							}
						]
					},
					"timestamp": "2025-04-10T15:07:15.833777"
				},
				"final": false
			}
		}
		};
		let _: crate::JsonRpcMessage = serde_json::from_value(js).unwrap();
	}
}

// New response types (current version)
pub type SendMessageResponse = JsonRpcResponse<SendMessageSuccessResponse>;
pub type SendStreamingMessageResponse = JsonRpcResponse<SendStreamingMessageSuccessResponse>;
pub type GetTaskResponse = JsonRpcResponse<GetTaskSuccessResponse>;
pub type CancelTaskResponse = JsonRpcResponse<CancelTaskSuccessResponse>;
pub type SetTaskPushNotificationConfigResponse =
	JsonRpcResponse<SetTaskPushNotificationConfigSuccessResponse>;
pub type GetTaskPushNotificationConfigResponse =
	JsonRpcResponse<GetTaskPushNotificationConfigSuccessResponse>;
pub type TaskResubscriptionResponse = JsonRpcResponse<TaskResubscriptionSuccessResponse>;
pub type ListTaskPushNotificationConfigResponse =
	JsonRpcResponse<ListTaskPushNotificationConfigSuccessResponse>;
pub type DeleteTaskPushNotificationConfigResponse =
	JsonRpcResponse<DeleteTaskPushNotificationConfigSuccessResponse>;

// Success response types
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct SendMessageSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: SendMessageSuccessResponseResult,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct SendStreamingMessageSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: SendStreamingMessageSuccessResponseResult,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct GetTaskSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: Task,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct CancelTaskSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: Task,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct SetTaskPushNotificationConfigSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: TaskPushNotificationConfig,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct GetTaskPushNotificationConfigSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: TaskPushNotificationConfig,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct TaskResubscriptionSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: (),
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct ListTaskPushNotificationConfigSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: Vec<TaskPushNotificationConfig>,
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
pub struct DeleteTaskPushNotificationConfigSuccessResponse {
	pub id: NumberOrString,
	pub jsonrpc: String,
	pub result: (),
}

// Result types for success responses
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum SendMessageSuccessResponseResult {
	Task(Box<Task>),
	Message(Message),
}

#[derive(serde::Deserialize, serde::Serialize, Clone, Debug)]
#[serde(untagged)]
pub enum SendStreamingMessageSuccessResponseResult {
	Task(Task),
	Message(Message),
	TaskStatusUpdateEvent(TaskStatusUpdateEvent),
	TaskArtifactUpdateEvent(TaskArtifactUpdateEvent),
}