klieo-a2a 3.3.0

Durable A2A v1.0 protocol layer atop klieo-bus traits.
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
//! A2A v1.0 wire types — `Part`, `Message`, `Artifact`, `Task`, `AgentCard`.
//!
//! Field names are verbatim from the A2A v1.0 spec §6 to preserve
//! wire-compatibility with non-NATS A2A clients. See
//! <https://a2a-protocol.org/latest/specification/>.

#![allow(non_snake_case)] // Spec field names are camelCase.

use serde::{Deserialize, Serialize};

/// Author role of a message — A2A spec §6.3.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// Sent by the human / calling system.
    User,
    /// Sent by the agent.
    Agent,
}

/// Lifecycle states of a long-running task — A2A spec §6.5.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum TaskStatus {
    /// Task accepted, not yet started.
    Submitted,
    /// Task is executing.
    Working,
    /// Task is paused awaiting more user input.
    InputRequired,
    /// Task succeeded.
    Completed,
    /// Task ended with an error.
    Failed,
    /// Task was cancelled by the caller.
    Canceled,
    /// Task was rejected by the server before starting.
    Rejected,
    /// Task is waiting on authentication.
    Authenticated,
    /// An unrecognized status from a newer peer. Deserialization maps any
    /// unknown wire value here instead of failing, so a forward-version peer
    /// can't break parsing. Never terminal.
    #[serde(other)]
    Unknown,
}

impl TaskStatus {
    /// `true` if this status cannot transition further. Stream
    /// consumers close after observing a terminal status.
    pub fn is_terminal(&self) -> bool {
        matches!(
            self,
            TaskStatus::Completed
                | TaskStatus::Failed
                | TaskStatus::Canceled
                | TaskStatus::Rejected
        )
    }
}

/// One element of a multi-part payload — A2A spec §6.2.
///
/// Tagged enum on `"type"`. Optional `metadata`, `filename`, and
/// `mediaType` are present on every variant per the spec.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
#[non_exhaustive]
pub enum Part {
    /// UTF-8 text part.
    Text {
        /// The text body.
        content: String,
        /// Free-form metadata attached to the part.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        metadata: Option<serde_json::Value>,
        /// IANA media type hint (rarely used for text).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mediaType: Option<String>,
        /// Suggested filename when extracting the part.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        filename: Option<String>,
    },
    /// Inline binary blob, base64-encoded.
    Raw {
        /// Base64-encoded bytes.
        bytes: String,
        /// IANA media type of the bytes.
        mediaType: String,
        /// Free-form metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        metadata: Option<serde_json::Value>,
        /// Suggested filename.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        filename: Option<String>,
    },
    /// External resource reference.
    Url {
        /// URL to fetch.
        url: String,
        /// IANA media type hint.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mediaType: Option<String>,
        /// Free-form metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        metadata: Option<serde_json::Value>,
        /// Suggested filename.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        filename: Option<String>,
    },
    /// Arbitrary structured JSON.
    Data {
        /// The JSON value.
        data: serde_json::Value,
        /// Free-form metadata.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        metadata: Option<serde_json::Value>,
        /// IANA media type hint (rarely used).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        mediaType: Option<String>,
        /// Suggested filename.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        filename: Option<String>,
    },
    /// An unrecognized part type from a newer peer. Deserialization maps any
    /// unknown `"type"` here instead of failing, preserving forward-compat.
    #[serde(other)]
    Unknown,
}

/// One message exchanged between user and agent — A2A spec §6.3.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    /// Stable id for this message.
    pub messageId: String,
    /// Conversation/context id grouping related messages.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contextId: Option<String>,
    /// Optional task id this message participates in.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub taskId: Option<String>,
    /// Author of the message.
    pub role: Role,
    /// Parts that make up the message.
    pub parts: Vec<Part>,
    /// Free-form metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
    /// Extension URIs the message uses.
    #[serde(default)]
    pub extensions: Vec<String>,
    /// Tasks this message references but does not own.
    #[serde(default)]
    pub referenceTaskIds: Vec<String>,
}

/// Output produced by an agent run — A2A spec §6.4.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Artifact {
    /// Stable id for this artifact.
    pub artifactId: String,
    /// Optional human-readable name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Optional description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Parts that make up the artifact.
    pub parts: Vec<Part>,
    /// Free-form metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
    /// Extension URIs the artifact uses.
    #[serde(default)]
    pub extensions: Vec<String>,
}

/// Long-running task — A2A spec §6.5.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Task {
    /// Stable id for this task.
    pub id: String,
    /// Conversation/context id this task belongs to.
    pub contextId: String,
    /// Lifecycle status.
    pub status: TaskStatus,
    /// Artifacts produced by the task.
    #[serde(default)]
    pub artifacts: Vec<Artifact>,
    /// Message history of the task.
    #[serde(default)]
    pub history: Vec<Message>,
    /// Free-form metadata.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

/// Provider information for an agent — A2A spec §6.1.2.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentProvider {
    /// Organisation name.
    pub organization: String,
    /// Optional contact URL.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,
}

/// Capabilities the agent supports — A2A spec §6.1.3.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentCapabilities {
    /// Supports `SendStreamingMessage`.
    #[serde(default)]
    pub streaming: bool,
    /// Supports push-notification config ops.
    #[serde(default)]
    pub pushNotifications: bool,
    /// Records state transitions in task history.
    #[serde(default)]
    pub stateTransitionHistory: bool,
}

/// Skill an agent advertises — A2A spec §6.1.4.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentSkill {
    /// Stable skill id.
    pub id: String,
    /// Human-readable name.
    pub name: String,
    /// Free-form description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Tags for discovery.
    #[serde(default)]
    pub tags: Vec<String>,
}

/// Transport interface an agent exposes — A2A spec §6.1.5.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentInterface {
    /// Transport identifier (e.g. `"a2a/1.0/jsonrpc"` or klieo-specific
    /// `"a2a/1.0/nats"`).
    pub transport: String,
    /// URL or NATS subject prefix the transport listens on.
    pub url: String,
}

/// Detached signature for an `AgentCard` — A2A spec §6.1.7.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentCardSignature {
    /// Algorithm identifier (e.g. `"ed25519"`).
    pub alg: String,
    /// Key id used.
    pub kid: String,
    /// Base64-encoded signature bytes.
    pub signature: String,
}

/// Public description of an agent — A2A spec §6.1.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AgentCard {
    /// Stable agent id.
    pub id: String,
    /// Display name.
    pub name: String,
    /// Free-form description.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Provider org info.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider: Option<AgentProvider>,
    /// Supported capabilities.
    pub capabilities: AgentCapabilities,
    /// Advertised skills.
    #[serde(default)]
    pub skills: Vec<AgentSkill>,
    /// Transport interfaces.
    #[serde(default)]
    pub interfaces: Vec<AgentInterface>,
    /// Security schemes (OpenAPI-style).
    #[serde(default)]
    pub securitySchemes: serde_json::Map<String, serde_json::Value>,
    /// Required security scheme references.
    #[serde(default)]
    pub security: Vec<serde_json::Value>,
    /// Extension URIs the agent uses.
    #[serde(default)]
    pub extensions: Vec<String>,
    /// Optional detached signature.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub signature: Option<AgentCardSignature>,
}

/// `SendMessage` params — A2A spec §9.4.1.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SendMessageParams {
    /// The message to send.
    pub message: Message,
    /// Optional configuration knobs (per-call timeout, etc).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub configuration: Option<serde_json::Value>,
}

/// `SendMessage` result.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SendMessageResult {
    /// Server completed synchronously and returned a response message.
    Message(Message),
    /// Server kicked off a long-running task.
    Task(Task),
}

/// `GetTask` params — A2A spec §9.4.3.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetTaskParams {
    /// Task id to fetch.
    pub id: String,
    /// Optional history truncation depth.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub historyLength: Option<usize>,
}

/// `ListTasks` params — A2A spec §9.4.4.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ListTasksParams {
    /// Optional context filter.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub contextId: Option<String>,
    /// Optional pagination cursor.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cursor: Option<String>,
    /// Optional page size.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub limit: Option<usize>,
}

/// `ListTasks` result.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ListTasksResult {
    /// Page of tasks.
    pub tasks: Vec<Task>,
    /// Cursor to use for the next page, if more remain.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nextCursor: Option<String>,
}

/// `CancelTask` params — A2A spec §9.4.5.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelTaskParams {
    /// Task id to cancel.
    pub id: String,
}

/// `SubscribeToTask` params — A2A spec §9.4.6.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscribeToTaskParams {
    /// Task id to subscribe to.
    pub id: String,
}

/// `CreateTaskPushNotificationConfig` params — A2A spec §9.4.7.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PushNotificationConfigParams {
    /// Task id.
    pub taskId: String,
    /// Webhook URL.
    pub url: String,
    /// Optional bearer token.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub token: Option<String>,
}

/// Stored push-notification config.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PushNotificationConfig {
    /// Stable config id.
    pub id: String,
    /// Owning task id.
    pub taskId: String,
    /// Webhook URL.
    pub url: String,
}

/// `GetTaskPushNotificationConfig` params — A2A spec §9.4.8.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetPushNotificationConfigParams {
    /// Task id.
    pub taskId: String,
    /// Config id.
    pub id: String,
}

/// `ListTaskPushNotificationConfigs` params — A2A spec §9.4.9.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ListPushNotificationConfigsParams {
    /// Task id.
    pub taskId: String,
}

/// `ListTaskPushNotificationConfigs` result.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ListPushNotificationConfigsResult {
    /// Stored configs for the task.
    pub configs: Vec<PushNotificationConfig>,
}

/// `DeleteTaskPushNotificationConfig` params — A2A spec §9.4.10.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeletePushNotificationConfigParams {
    /// Task id.
    pub taskId: String,
    /// Config id.
    pub id: String,
}

/// `GetExtendedAgentCard` params — A2A spec §9.4.11. Intentionally empty per spec.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct GetExtendedAgentCardParams {}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn unknown_task_status_deserialises_to_unknown_not_error() {
        let s: TaskStatus = serde_json::from_value(json!("some_future_state")).unwrap();
        assert_eq!(s, TaskStatus::Unknown);
        assert!(!s.is_terminal(), "Unknown status is never terminal");
    }

    #[test]
    fn unknown_part_type_deserialises_to_unknown_not_error() {
        let p: Part = serde_json::from_value(json!({"type": "future_part", "x": 1})).unwrap();
        assert!(matches!(p, Part::Unknown));
    }

    #[test]
    fn part_text_serialises_with_type_text() {
        let p = Part::Text {
            content: "hello".into(),
            metadata: None,
            mediaType: None,
            filename: None,
        };
        let v = serde_json::to_value(&p).unwrap();
        assert_eq!(v["type"], "text");
        assert_eq!(v["content"], "hello");
    }

    #[test]
    fn part_raw_uses_base64_string_field() {
        let p = Part::Raw {
            bytes: "aGVsbG8=".into(),
            mediaType: "text/plain".into(),
            metadata: None,
            filename: None,
        };
        let v = serde_json::to_value(&p).unwrap();
        assert_eq!(v["type"], "raw");
        assert_eq!(v["bytes"], "aGVsbG8=");
        assert_eq!(v["mediaType"], "text/plain");
    }

    #[test]
    fn message_round_trips() {
        let m = Message {
            messageId: "m-1".into(),
            contextId: Some("c-1".into()),
            taskId: Some("t-1".into()),
            role: Role::User,
            parts: vec![Part::Text {
                content: "hi".into(),
                metadata: None,
                mediaType: None,
                filename: None,
            }],
            metadata: None,
            extensions: vec![],
            referenceTaskIds: vec![],
        };
        let v = serde_json::to_value(&m).unwrap();
        let back: Message = serde_json::from_value(v).unwrap();
        assert_eq!(back.messageId, "m-1");
        assert_eq!(back.role, Role::User);
        assert_eq!(back.parts.len(), 1);
    }

    #[test]
    fn task_with_empty_history_serialises_minimally() {
        let t = Task {
            id: "t-1".into(),
            contextId: "c-1".into(),
            status: TaskStatus::Submitted,
            artifacts: vec![],
            history: vec![],
            metadata: None,
        };
        let v = serde_json::to_value(&t).unwrap();
        assert_eq!(v["id"], "t-1");
        assert_eq!(v["status"], "submitted");
    }

    #[test]
    fn agent_card_round_trips() {
        let card = AgentCard {
            id: "agent-1".into(),
            name: "Echo".into(),
            description: Some("Echoes input".into()),
            provider: None,
            capabilities: AgentCapabilities {
                streaming: false,
                pushNotifications: false,
                stateTransitionHistory: false,
            },
            skills: vec![],
            interfaces: vec![],
            securitySchemes: serde_json::Map::new(),
            security: vec![],
            extensions: vec![],
            signature: None,
        };
        let v = serde_json::to_value(&card).unwrap();
        let back: AgentCard = serde_json::from_value(v).unwrap();
        assert_eq!(back.id, "agent-1");
    }

    #[test]
    fn role_agent_serialises_as_lowercase() {
        let v = serde_json::to_value(Role::Agent).unwrap();
        assert_eq!(v, json!("agent"));
    }

    #[test]
    fn task_status_is_terminal_covers_completed_failed_canceled_rejected() {
        assert!(TaskStatus::Completed.is_terminal());
        assert!(TaskStatus::Failed.is_terminal());
        assert!(TaskStatus::Canceled.is_terminal());
        assert!(TaskStatus::Rejected.is_terminal());
    }

    #[test]
    fn task_status_is_terminal_excludes_in_progress() {
        assert!(!TaskStatus::Submitted.is_terminal());
        assert!(!TaskStatus::Working.is_terminal());
        assert!(!TaskStatus::Authenticated.is_terminal());
        assert!(!TaskStatus::InputRequired.is_terminal());
    }
}