netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
//! A2A 1.0 agent over HTTP/1.1: agent card, JSON-RPC dispatch, SSE streaming. Rust owns the
//! envelope, ids and version rules; the handler owns every reply.
pub mod actions;
pub mod model;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::logging::emit::Log;
use crate::protocol::{Event, SpawnContext};
use crate::server::accept_bounded::{accept_bounded, ConnectionLimiter, DEFAULT_MAX_CONNECTIONS};
use crate::server::connection::ConnectionId;
use crate::state::server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo};
use anyhow::{bail, ensure, Context, Result};
use bytes::Bytes;
use http_body_util::{BodyExt, Full, Limited};
use hyper::{
    body::Incoming, header, server::conn::http1, service::service_fn, Method, Request, Response,
    StatusCode,
};
use hyper_util::rt::{TokioIo, TokioTimer};
use serde_json::{json, Value};
use std::{convert::Infallible, net::SocketAddr, sync::Arc, time::Duration};

pub const DEFAULT_NAME: &str = "NetGet Agent";
pub const DEFAULT_DESCRIPTION: &str = "An A2A agent served by NetGet";
pub const DEFAULT_STREAMING: bool = true;
const HEADER_TIMEOUT: Duration = Duration::from_secs(30);
const BODY_TIMEOUT: Duration = Duration::from_secs(30);

struct Shared {
    ctx: SpawnContext,
    name: String,
    description: String,
    skills: Vec<Value>,
    streaming: bool,
    local: SocketAddr,
}

pub async fn spawn(ctx: SpawnContext) -> Result<SocketAddr> {
    let p = ctx.startup_params.as_ref();
    let s = |k: &str, d: &str| -> Result<String> {
        Ok(p.map(|p| p.get_optional_string(k))
            .transpose()?
            .flatten()
            .unwrap_or_else(|| d.to_owned()))
    };
    let name = s("agent_name", DEFAULT_NAME)?;
    let description = s("agent_description", DEFAULT_DESCRIPTION)?;
    ensure!(
        !name.is_empty() && name.len() <= 256 && description.len() <= 4096,
        "agent_name/agent_description too long"
    );
    let skills = match p
        .map(|p| p.get_optional_array("skills"))
        .transpose()?
        .flatten()
    {
        None => vec![
            json!({"id": "chat", "name": "Chat", "description": "Converse with the agent", "tags": ["chat"]}),
        ],
        Some(list) => {
            ensure!(list.len() <= 64, "at most 64 skills");
            list.iter()
                .map(|sk| {
                    let id = sk["id"].as_str().context("each skill needs an id")?;
                    let name = sk["name"].as_str().context("each skill needs a name")?;
                    Ok(json!({"id": id, "name": name, "description": sk["description"].as_str().unwrap_or(""), "tags": sk.get("tags").cloned().unwrap_or(json!([])), "examples": sk.get("examples").cloned().unwrap_or(json!([]))}))
                })
                .collect::<Result<_>>()?
        }
    };
    let streaming = p
        .map(|p| p.get_optional_bool("streaming"))
        .transpose()?
        .flatten()
        .unwrap_or(DEFAULT_STREAMING);
    let listener = tokio::net::TcpListener::bind(ctx.legacy_listen_addr()).await?;
    let local = listener.local_addr()?;
    Log::new(Some(&ctx.status_tx)).info(format!(
        "A2A agent '{name}' at http://{local}/ (card {})",
        model::CARD_PATH
    ));
    let shared = Arc::new(Shared {
        ctx: ctx.clone(),
        name,
        description,
        skills,
        streaming,
        local,
    });
    let server_id = ctx.server_id;
    let accept = tokio::spawn(async move {
        let limiter = ConnectionLimiter::new(DEFAULT_MAX_CONNECTIONS);
        loop {
            let (stream, peer, permit) = match accept_bounded(&listener, &limiter, b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\n\r\n", "A2A", Some(&shared.ctx.status_tx)).await {
                Ok(v) => v,
                Err(_) => break,
            };
            let id = ConnectionId::new(shared.ctx.state.get_next_unified_id().await);
            let now = crate::utils::clock::Instant::now();
            shared
                .ctx
                .state
                .add_connection_to_server(
                    server_id,
                    ConnectionState {
                        id,
                        remote_addr: peer,
                        local_addr: local,
                        bytes_sent: 0,
                        bytes_received: 0,
                        packets_sent: 0,
                        packets_received: 0,
                        last_activity: now,
                        status: ConnectionStatus::Active,
                        status_changed_at: now,
                        protocol_info: ProtocolConnectionInfo::empty(),
                    },
                )
                .await;
            let child = shared.clone();
            shared
                .ctx
                .state
                .spawn_server_task(server_id, async move {
                    let _permit = permit;
                    let svc_shared = child.clone();
                    let service = service_fn(move |req| {
                        let shared = svc_shared.clone();
                        async move { Ok::<_, Infallible>(handle(&shared, id, req).await) }
                    });
                    let mut builder = http1::Builder::new();
                    builder
                        .timer(TokioTimer::new())
                        .header_read_timeout(HEADER_TIMEOUT)
                        .max_headers(64)
                        .max_buf_size(32 * 1024);
                    if let Err(e) = builder
                        .serve_connection(TokioIo::new(stream), service)
                        .await
                    {
                        Log::new(Some(&child.ctx.status_tx))
                            .debug(format!("A2A connection {id}: {e}"));
                    }
                    child
                        .ctx
                        .state
                        .update_connection_status(server_id, id, ConnectionStatus::Closed)
                        .await;
                    let _ = child.ctx.status_tx.send("__UPDATE_UI__".into());
                })
                .await;
        }
    });
    ctx.state.register_server_task(server_id, accept).await;
    Ok(local)
}

fn respond(status: u16, content_type: &'static str, body: Vec<u8>) -> Response<Full<Bytes>> {
    let mut r = Response::new(Full::new(Bytes::from(body)));
    *r.status_mut() = StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
    r.headers_mut().insert(
        header::CONTENT_TYPE,
        header::HeaderValue::from_static(content_type),
    );
    r
}
fn json_response(value: &Value) -> Response<Full<Bytes>> {
    respond(
        200,
        "application/json",
        serde_json::to_vec(value).unwrap_or_default(),
    )
}

fn outcome(ctx: &SpawnContext, id: ConnectionId, operation: &str, decision: &str) {
    let summary = format!("A2A connection {id} method={operation} decision={decision}");
    let log = Log::new(Some(&ctx.status_tx));
    if decision.starts_with("fail_closed_") || decision == "model_silent" {
        log.error(summary);
    } else {
        log.info(summary);
    }
}

async fn decide(shared: &Shared, id: ConnectionId, event: Event) -> Result<Value> {
    let ctx = &shared.ctx;
    let result = match call_llm(
        &ctx.llm_client,
        &ctx.state,
        ctx.server_id,
        Some(id),
        &event,
        &actions::A2aProtocol,
    )
    .await
    {
        Ok(r) => r,
        Err(e) => {
            outcome(ctx, id, event.id(), "fail_closed_llm_error");
            return Err(e);
        }
    };
    if !result.failures.is_empty() {
        outcome(ctx, id, event.id(), "fail_closed_invalid_reply");
        bail!("A2A handler supplied an invalid action");
    }
    let mut found = None;
    let mut pending = result.protocol_results;
    while let Some(r) = pending.pop() {
        match r {
            ActionResult::Custom { name, data } if name == "a2a_reply" => {
                if found.replace(data).is_some() {
                    outcome(ctx, id, event.id(), "fail_closed_invalid_reply");
                    bail!("A2A handler supplied more than one reply");
                }
            }
            ActionResult::Multiple(items) => pending.extend(items),
            _ => {}
        }
    }
    found
        .context("A2A handler did not answer")
        .inspect_err(|_| outcome(ctx, id, event.id(), "model_silent"))
}

async fn handle(
    shared: &Shared,
    id: ConnectionId,
    req: Request<Incoming>,
) -> Response<Full<Bytes>> {
    let ctx = &shared.ctx;
    ctx.state
        .update_connection_stats(
            ctx.server_id,
            id,
            Some(req.uri().to_string().len() as u64),
            None,
            Some(1),
            None,
        )
        .await;
    let response = route(shared, id, req).await;
    let sent = hyper::body::Body::size_hint(response.body())
        .exact()
        .unwrap_or(0);
    ctx.state
        .update_connection_stats(ctx.server_id, id, None, Some(sent), None, Some(1))
        .await;
    response
}

async fn route(shared: &Shared, id: ConnectionId, req: Request<Incoming>) -> Response<Full<Bytes>> {
    if req.method() == Method::GET && req.uri().path() == model::CARD_PATH {
        let host = req
            .headers()
            .get(header::HOST)
            .and_then(|h| h.to_str().ok())
            .filter(|h| !h.is_empty() && h.len() <= 255 && !h.contains(['/', ' ']))
            .map(str::to_owned)
            .unwrap_or_else(|| shared.local.to_string());
        let card = model::card(
            &shared.name,
            &shared.description,
            "1.0.0",
            &format!("http://{host}/"),
            shared.streaming,
            &shared.skills,
        );
        return json_response(&card);
    }
    if req.method() != Method::POST || req.uri().path() != "/" {
        return respond(404, "text/plain", b"not found".to_vec());
    }
    let version = req
        .headers()
        .get(model::VERSION_HEADER)
        .and_then(|v| v.to_str().ok())
        .map(str::to_owned);
    let body = match tokio::time::timeout(
        BODY_TIMEOUT,
        Limited::new(req.into_body(), model::MAX_BODY_BYTES).collect(),
    )
    .await
    {
        Ok(Ok(b)) => b.to_bytes(),
        _ => {
            return json_response(&model::rpc_error(
                &Value::Null,
                -32600,
                "request body unreadable or over 1 MiB",
            ))
        }
    };
    let request: Value = match serde_json::from_slice(&body) {
        Ok(v) if model::budget_ok(&v) => v,
        _ => return json_response(&model::rpc_error(&Value::Null, -32700, "parse error")),
    };
    let rpc_id = request.get("id").cloned().unwrap_or(Value::Null);
    if request["jsonrpc"] != "2.0"
        || !matches!(rpc_id, Value::String(_) | Value::Number(_) | Value::Null)
    {
        return json_response(&model::rpc_error(
            &rpc_id,
            -32600,
            "invalid JSON-RPC 2.0 request",
        ));
    }
    let Some(method) = request["method"].as_str() else {
        return json_response(&model::rpc_error(&rpc_id, -32600, "method is required"));
    };
    if !model::version_ok(version.as_deref()) {
        return json_response(&model::rpc_error(
            &rpc_id,
            model::VERSION_NOT_SUPPORTED,
            "A2A-Version 1.0 is required",
        ));
    }
    let params = request.get("params").cloned().unwrap_or(json!({}));
    if !params.is_object() {
        return json_response(&model::rpc_error(
            &rpc_id,
            -32602,
            "params must be an object",
        ));
    }
    match method {
        "SendMessage" | "SendStreamingMessage" => send(shared, id, method, &rpc_id, &params).await,
        "GetTask" | "CancelTask" | "ListTasks" => {
            task_request(shared, id, method, &rpc_id, &params).await
        }
        "SubscribeToTask"
        | "CreateTaskPushNotificationConfig"
        | "GetTaskPushNotificationConfig"
        | "ListTaskPushNotificationConfigs"
        | "DeleteTaskPushNotificationConfig" => json_response(&model::rpc_error(
            &rpc_id,
            if method == "SubscribeToTask" {
                model::UNSUPPORTED_OPERATION
            } else {
                model::PUSH_NOT_SUPPORTED
            },
            "not supported by this agent",
        )),
        "GetExtendedAgentCard" => json_response(&model::rpc_error(
            &rpc_id,
            model::EXTENDED_CARD_NOT_CONFIGURED,
            "no extended agent card",
        )),
        _ => json_response(&model::rpc_error(&rpc_id, -32601, "method not found")),
    }
}

fn internal(rpc_id: &Value, streaming: bool) -> Response<Full<Bytes>> {
    let err = model::rpc_error(rpc_id, -32603, "the agent cannot answer right now");
    if streaming {
        respond(200, "text/event-stream", model::sse(&err).into_bytes())
    } else {
        json_response(&err)
    }
}

fn mapped_error(rpc_id: &Value, v: &Value) -> Value {
    let code = v["error"]["code"].as_str().unwrap_or("");
    let num = actions::ERROR_NAMES
        .iter()
        .find(|(n, _)| *n == code)
        .map(|(_, c)| *c)
        .unwrap_or(-32603);
    model::rpc_error(rpc_id, num, v["error"]["message"].as_str().unwrap_or(code))
}

async fn send(
    shared: &Shared,
    id: ConnectionId,
    method: &str,
    rpc_id: &Value,
    params: &Value,
) -> Response<Full<Bytes>> {
    let ctx = &shared.ctx;
    let streaming = method == "SendStreamingMessage";
    if streaming && !shared.streaming {
        return json_response(&model::rpc_error(
            rpc_id,
            model::UNSUPPORTED_OPERATION,
            "streaming is not offered",
        ));
    }
    let incoming = match model::incoming_message(params) {
        Ok(m) => m,
        Err(e) => {
            outcome(ctx, id, method, "protocol_refusal");
            return json_response(&model::rpc_error(rpc_id, -32602, &e.to_string()));
        }
    };
    let context_id = incoming.context_id.clone().unwrap_or_else(model::new_id);
    let event = Event::new(
        &actions::MESSAGE_EVENT,
        json!({"method": method, "text": incoming.text, "parts": incoming.parts, "message_id": incoming.message_id, "context_id": context_id, "task_id": incoming.task_id}),
    );
    let reply = match decide(shared, id, event).await {
        Ok(v) => v,
        Err(_) => return internal(rpc_id, streaming),
    };
    let built = (|| -> Result<Value> {
        actions::validate_reply(&reply)?;
        if reply.get("error").is_some_and(|e| !e.is_null()) {
            return Ok(json!({"error": mapped_error(rpc_id, &reply)}));
        }
        if reply.get("message").is_some_and(|m| !m.is_null()) {
            return Ok(
                json!({"message": model::agent_message(reply["message"]["text"].as_str(), reply["message"].get("parts"), &context_id, incoming.task_id.as_deref())?}),
            );
        }
        let task = reply
            .get("task")
            .filter(|t| !t.is_null())
            .context("SendMessage is answered with message, task or error")?;
        let default_id = incoming.task_id.clone().unwrap_or_else(model::new_id);
        Ok(json!({"task": model::task(task, &default_id, &context_id)?}))
    })();
    let built = match built {
        Ok(b) => b,
        Err(_) => {
            outcome(ctx, id, method, "fail_closed_invalid_reply");
            return internal(rpc_id, streaming);
        }
    };
    if let Some(err) = built.get("error") {
        outcome(ctx, id, method, "model_reject");
        return if streaming {
            respond(200, "text/event-stream", model::sse(err).into_bytes())
        } else {
            json_response(err)
        };
    }
    outcome(ctx, id, method, "model_answer");
    if !streaming {
        return json_response(&model::rpc_result(rpc_id, built));
    }
    let events = match built.get("task") {
        Some(task) => model::stream_events(rpc_id, task),
        None => vec![model::rpc_result(rpc_id, built)],
    };
    respond(
        200,
        "text/event-stream",
        events
            .iter()
            .map(model::sse)
            .collect::<String>()
            .into_bytes(),
    )
}

async fn task_request(
    shared: &Shared,
    id: ConnectionId,
    method: &str,
    rpc_id: &Value,
    params: &Value,
) -> Response<Full<Bytes>> {
    let ctx = &shared.ctx;
    let task_id = params.get("id").and_then(Value::as_str).map(str::to_owned);
    if method != "ListTasks"
        && task_id
            .as_deref()
            .is_none_or(|t| t.is_empty() || t.len() > 256)
    {
        return json_response(&model::rpc_error(rpc_id, -32602, "params.id is required"));
    }
    let event = Event::new(
        &actions::TASK_EVENT,
        json!({"method": method, "task_id": task_id, "params": params}),
    );
    let reply = match decide(shared, id, event).await {
        Ok(v) => v,
        Err(_) => return internal(rpc_id, false),
    };
    let built = (|| -> Result<Value> {
        actions::validate_reply(&reply)?;
        if reply.get("error").is_some_and(|e| !e.is_null()) {
            return Ok(mapped_error(rpc_id, &reply));
        }
        match method {
            "ListTasks" => {
                let list = reply
                    .get("tasks")
                    .and_then(Value::as_array)
                    .context("ListTasks is answered with tasks or error")?;
                let tasks: Vec<Value> = list
                    .iter()
                    .map(|t| {
                        model::task(t, "unused", t["context_id"].as_str().unwrap_or("default"))
                    })
                    .collect::<Result<_>>()?;
                Ok(model::rpc_result(
                    rpc_id,
                    json!({"tasks": tasks, "pageSize": tasks.len(), "totalSize": tasks.len(), "nextPageToken": ""}),
                ))
            }
            _ => {
                let spec = reply
                    .get("task")
                    .filter(|t| !t.is_null())
                    .context("GetTask/CancelTask are answered with task or error")?;
                let tid = task_id.clone().unwrap_or_default();
                let task =
                    model::task(spec, &tid, spec["context_id"].as_str().unwrap_or("default"))?;
                ensure!(task["id"] == tid, "the answer describes a different task");
                if method == "CancelTask" {
                    ensure!(task["status"]["state"] == "TASK_STATE_CANCELED", "a successful CancelTask returns the task canceled; use task_not_cancelable otherwise");
                }
                Ok(model::rpc_result(rpc_id, task))
            }
        }
    })();
    match built {
        Ok(v) => {
            outcome(
                ctx,
                id,
                method,
                if v.get("error").is_some() {
                    "model_reject"
                } else {
                    "model_answer"
                },
            );
            json_response(&v)
        }
        Err(_) => {
            outcome(ctx, id, method, "fail_closed_invalid_reply");
            internal(rpc_id, false)
        }
    }
}