use crate::llm::actions::{
client_trait::{Client, ClientActionResult},
protocol_trait::Protocol,
ActionDefinition, Parameter,
};
use crate::protocol::EventType;
use crate::state::app_state::AppState;
use anyhow::{Context, Result};
use serde_json::json;
use std::sync::LazyLock;
pub static PEER_HANDSHAKE_EVENT: LazyLock<EventType> = LazyLock::new(|| {
EventType::new(
"peer_handshake",
"Received handshake from BitTorrent peer",
json!({"type": "peer_interested"}),
)
.with_parameters(vec![
Parameter {
name: "info_hash".to_string(),
type_hint: "string".to_string(),
description: "Info hash (hex)".to_string(),
required: true,
},
Parameter {
name: "peer_id".to_string(),
type_hint: "string".to_string(),
description: "Peer ID (hex)".to_string(),
required: true,
},
Parameter {
name: "reserved".to_string(),
type_hint: "string".to_string(),
description: "Reserved bytes (hex)".to_string(),
required: false,
},
])
});
pub static PEER_MESSAGE_EVENT: LazyLock<EventType> = LazyLock::new(|| {
EventType::new(
"peer_message",
"Received message from BitTorrent peer",
json!({"type": "peer_request_piece", "index": 0, "begin": 0, "length": 16384}),
)
.with_parameters(vec![
Parameter {
name: "message_type".to_string(),
type_hint: "number".to_string(),
description: "Message type (0=choke, 1=unchoke, 2=interested, 3=not_interested, 4=have, 5=bitfield, 6=request, 7=piece, 8=cancel, 9=port)".to_string(),
required: true,
},
Parameter {
name: "payload_len".to_string(),
type_hint: "number".to_string(),
description: "Payload length".to_string(),
required: true,
},
Parameter {
name: "payload_hex".to_string(),
type_hint: "string".to_string(),
description: "Payload data (hex)".to_string(),
required: false,
},
])
});
pub struct TorrentPeerClientProtocol;
impl TorrentPeerClientProtocol {
pub fn new() -> Self {
Self
}
}
impl Protocol for TorrentPeerClientProtocol {
fn get_async_actions(&self, _state: &AppState) -> Vec<ActionDefinition> {
vec![
ActionDefinition {
name: "peer_handshake".to_string(),
description: "Send handshake to peer".to_string(),
parameters: vec![
Parameter {
name: "info_hash".to_string(),
type_hint: "string".to_string(),
description: "Info hash (40 char hex)".to_string(),
required: true,
},
Parameter {
name: "peer_id".to_string(),
type_hint: "string".to_string(),
description: "Our peer ID (40 char hex)".to_string(),
required: true,
},
],
example: json!({
"type": "peer_handshake",
"info_hash": "0123456789abcdef0123456789abcdef01234567",
"peer_id": "abcdef0123456789abcdef0123456789abcdef01"
}),
log_template: None,
},
ActionDefinition {
name: "peer_interested".to_string(),
description: "Send interested message".to_string(),
parameters: vec![],
example: json!({
"type": "peer_interested"
}),
log_template: None,
},
ActionDefinition {
name: "peer_not_interested".to_string(),
description: "Send not interested message".to_string(),
parameters: vec![],
example: json!({
"type": "peer_not_interested"
}),
log_template: None,
},
ActionDefinition {
name: "peer_request_piece".to_string(),
description: "Request a piece from peer".to_string(),
parameters: vec![
Parameter {
name: "index".to_string(),
type_hint: "number".to_string(),
description: "Piece index".to_string(),
required: true,
},
Parameter {
name: "begin".to_string(),
type_hint: "number".to_string(),
description: "Byte offset within piece".to_string(),
required: true,
},
Parameter {
name: "length".to_string(),
type_hint: "number".to_string(),
description: "Block length to request".to_string(),
required: true,
},
],
example: json!({
"type": "peer_request_piece",
"index": 0,
"begin": 0,
"length": 16384
}),
log_template: None,
},
ActionDefinition {
name: "peer_send_piece".to_string(),
description: "Send a piece to peer".to_string(),
parameters: vec![
Parameter {
name: "index".to_string(),
type_hint: "number".to_string(),
description: "Piece index".to_string(),
required: true,
},
Parameter {
name: "begin".to_string(),
type_hint: "number".to_string(),
description: "Byte offset within piece".to_string(),
required: true,
},
Parameter {
name: "block".to_string(),
type_hint: "string".to_string(),
description: "Block data (hex)".to_string(),
required: true,
},
],
example: json!({
"type": "peer_send_piece",
"index": 0,
"begin": 0,
"block": "abcdef0123"
}),
log_template: None,
},
ActionDefinition {
name: "disconnect".to_string(),
description: "Disconnect from peer".to_string(),
parameters: vec![],
example: json!({
"type": "disconnect"
}),
log_template: None,
},
]
}
fn get_sync_actions(&self) -> Vec<ActionDefinition> {
vec![]
}
fn protocol_name(&self) -> &'static str {
"BitTorrent Peer Wire"
}
fn get_event_types(&self) -> Vec<EventType> {
vec![PEER_HANDSHAKE_EVENT.clone(), PEER_MESSAGE_EVENT.clone()]
}
fn stack_name(&self) -> &'static str {
"ETH>IP>TCP>BitTorrent-PeerWire"
}
fn keywords(&self) -> Vec<&'static str> {
vec!["bittorrent", "peer", "peer wire", "torrent peer"]
}
fn metadata(&self) -> crate::protocol::metadata::ProtocolMetadataV2 {
use crate::protocol::metadata::{DevelopmentState, ProtocolMetadataV2};
ProtocolMetadataV2::builder()
.state(DevelopmentState::Experimental)
.implementation("TCP-based peer wire protocol with handshake and message framing")
.llm_control("Full control over peer messages (choke, unchoke, interested, request, piece, etc.)")
.e2e_testing("Mock peer server")
.build()
}
fn description(&self) -> &'static str {
"BitTorrent Peer Wire Protocol client for peer-to-peer data transfer"
}
fn example_prompt(&self) -> &'static str {
"Connect to peer at 192.168.1.100:6881 and exchange pieces for info_hash xyz"
}
fn group_name(&self) -> &'static str {
"P2P"
}
fn get_startup_examples(&self) -> crate::llm::actions::StartupExamples {
use crate::llm::actions::StartupExamples;
use serde_json::json;
StartupExamples::new(
json!({
"type": "open_client",
"remote_addr": "192.168.1.100:6881",
"base_stack": "torrent-peer",
"instruction": "Connect to peer and exchange pieces for a torrent"
}),
json!({
"type": "open_client",
"remote_addr": "192.168.1.100:6881",
"base_stack": "torrent-peer",
"event_handlers": [{
"event_pattern": "peer_handshake",
"handler": {
"type": "script",
"language": "python",
"code": "<peer_client_handler>"
}
}]
}),
json!({
"type": "open_client",
"remote_addr": "192.168.1.100:6881",
"base_stack": "torrent-peer",
"event_handlers": [{
"event_pattern": "peer_message",
"handler": {
"type": "static",
"actions": [{
"type": "disconnect"
}]
}
}]
}),
)
}
}
impl Client for TorrentPeerClientProtocol {
fn connect(
&self,
ctx: crate::protocol::ConnectContext,
) -> std::pin::Pin<
Box<dyn std::future::Future<Output = anyhow::Result<std::net::SocketAddr>> + Send>,
> {
Box::pin(async move {
use crate::client::torrent_peer::TorrentPeerClient;
TorrentPeerClient::connect_with_llm_actions(
ctx.remote_addr,
ctx.llm_client,
ctx.state,
ctx.status_tx,
ctx.client_id,
)
.await
})
}
fn execute_action(&self, action: serde_json::Value) -> Result<ClientActionResult> {
fn u32_field(action: &serde_json::Value, name: &str) -> Result<u32> {
let raw = action
.get(name)
.and_then(|v| v.as_u64())
.with_context(|| format!("missing or non-numeric '{}'", name))?;
u32::try_from(raw).with_context(|| format!("'{}' does not fit in u32", name))
}
let action_type = action
.get("type")
.and_then(|v| v.as_str())
.context("Missing 'type' field in action")?;
let framed = match action_type {
"peer_interested" => Some((2u8, String::new())),
"peer_not_interested" => Some((3u8, String::new())),
"peer_request_piece" => {
let index = u32_field(&action, "index")?;
let begin = u32_field(&action, "begin")?;
let length = u32_field(&action, "length")?;
let mut payload = Vec::with_capacity(12);
payload.extend_from_slice(&index.to_be_bytes());
payload.extend_from_slice(&begin.to_be_bytes());
payload.extend_from_slice(&length.to_be_bytes());
Some((6u8, hex::encode(payload)))
}
"peer_send_piece" => {
let index = u32_field(&action, "index")?;
let begin = u32_field(&action, "begin")?;
let block_hex = action
.get("block")
.and_then(|v| v.as_str())
.context("peer_send_piece requires 'block' (hex-encoded block data)")?;
let block = hex::decode(block_hex)
.context("peer_send_piece 'block' must be hex-encoded")?;
let mut payload = Vec::with_capacity(8 + block.len());
payload.extend_from_slice(&index.to_be_bytes());
payload.extend_from_slice(&begin.to_be_bytes());
payload.extend_from_slice(&block);
Some((7u8, hex::encode(payload)))
}
_ => None,
};
if let Some((message_type, payload)) = framed {
return Ok(ClientActionResult::Custom {
name: "peer_message".to_string(),
data: serde_json::json!({
"type": "peer_message",
"message_type": message_type,
"payload": payload,
}),
});
}
match action_type {
"peer_handshake" => Ok(ClientActionResult::Custom {
name: "peer_handshake".to_string(),
data: action,
}),
"peer_message" => Ok(ClientActionResult::Custom {
name: "peer_message".to_string(),
data: action,
}),
"disconnect" => Ok(ClientActionResult::Disconnect),
_ => Err(anyhow::anyhow!(
"Unknown Peer client action: {}",
action_type
)),
}
}
}