Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions crates/challenge-sdk-wasm/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use alloc::boxed::Box;
use alloc::format;
use alloc::string::String;
use core::fmt;

#[derive(Debug)]
pub enum ChallengeError {
Evaluation(String),
Validation(String),
Network(String),
Timeout(String),
Serialization(String),
Storage(String),
Internal(String),
}

impl fmt::Display for ChallengeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ChallengeError::Evaluation(msg) => write!(f, "Evaluation error: {}", msg),
ChallengeError::Validation(msg) => write!(f, "Validation error: {}", msg),
ChallengeError::Network(msg) => write!(f, "Network error: {}", msg),
ChallengeError::Timeout(msg) => write!(f, "Timeout: {}", msg),
ChallengeError::Serialization(msg) => write!(f, "Serialization error: {}", msg),
ChallengeError::Storage(msg) => write!(f, "Storage error: {}", msg),
ChallengeError::Internal(msg) => write!(f, "Internal error: {}", msg),
}
}
}

impl From<Box<bincode::ErrorKind>> for ChallengeError {
fn from(err: Box<bincode::ErrorKind>) -> Self {
ChallengeError::Serialization(format!("{}", err))
}
}
35 changes: 35 additions & 0 deletions crates/challenge-sdk-wasm/src/host_functions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use alloc::format;
use alloc::vec;
use alloc::vec::Vec;

use crate::error::ChallengeError;
use crate::types::{HttpRequest, HttpResponse};

#[link(wasm_import_module = "platform_network")]
extern "C" {
fn http_get(req_ptr: i32, req_len: i32, resp_ptr: i32, resp_len: i32) -> i32;
fn http_post(req_ptr: i32, req_len: i32, resp_ptr: i32, resp_len: i32, extra: i32) -> i32;
fn dns_resolve(req_ptr: i32, req_len: i32, resp_ptr: i32) -> i32;
fn log_message(level: i32, msg_ptr: i32, msg_len: i32);
}

#[link(wasm_import_module = "platform_storage")]
Expand All @@ -14,6 +19,11 @@ extern "C" {
fn storage_set(key_ptr: i32, key_len: i32, value_ptr: i32, value_len: i32) -> i32;
}

#[link(wasm_import_module = "platform_time")]
extern "C" {
fn get_timestamp() -> i64;
}

#[link(wasm_import_module = "platform_terminal")]
extern "C" {
fn terminal_exec(cmd_ptr: i32, cmd_len: i32, result_ptr: i32, result_len: i32) -> i32;
Expand Down Expand Up @@ -106,6 +116,31 @@ pub fn host_storage_set(key: &[u8], value: &[u8]) -> Result<(), i32> {
Ok(())
}

pub fn host_log(level: u8, message: &str) {
unsafe {
log_message(level as i32, message.as_ptr() as i32, message.len() as i32);
}
}

pub fn host_get_timestamp() -> i64 {
unsafe { get_timestamp() }
}

pub fn typed_http_get(request: &HttpRequest) -> Result<HttpResponse, ChallengeError> {
let encoded = bincode::serialize(request).map_err(ChallengeError::from)?;
let raw = host_http_get(&encoded)
.map_err(|code| ChallengeError::Network(format!("http_get failed with code {}", code)))?;
bincode::deserialize(&raw).map_err(ChallengeError::from)
}

pub fn typed_http_post(request: &HttpRequest) -> Result<HttpResponse, ChallengeError> {
let encoded = bincode::serialize(request).map_err(ChallengeError::from)?;
let body = &request.body;
let raw = host_http_post(&encoded, body)
.map_err(|code| ChallengeError::Network(format!("http_post failed with code {}", code)))?;
bincode::deserialize(&raw).map_err(ChallengeError::from)
}

pub fn host_terminal_exec(request: &[u8]) -> Result<Vec<u8>, i32> {
let mut result_buf = vec![0u8; 262144];
let status = unsafe {
Expand Down
6 changes: 5 additions & 1 deletion crates/challenge-sdk-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
extern crate alloc;

pub mod alloc_impl;
pub mod error;
pub mod host_functions;
pub mod term_types;
pub mod types;

pub use error::ChallengeError;
pub use term_types::*;
pub use types::{EvaluationInput, EvaluationOutput};
pub use types::{
DetailedScore, EvaluationInput, EvaluationOutput, HttpRequest, HttpResponse, TaskResult,
};

pub trait Challenge {
fn name(&self) -> &'static str;
Expand Down
35 changes: 35 additions & 0 deletions crates/challenge-sdk-wasm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ pub struct EvaluationInput {
pub agent_data: Vec<u8>,
pub challenge_id: String,
pub params: Vec<u8>,
pub submission_id: Option<String>,
pub participant_id: Option<String>,
pub epoch: Option<u64>,
pub metadata: Vec<u8>,
pub task_definitions: Vec<u8>,
pub task_definition: Option<Vec<u8>>,
pub environment_config: Option<Vec<u8>>,
}
Expand Down Expand Up @@ -43,3 +48,33 @@ impl EvaluationOutput {
self
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HttpRequest {
pub url: String,
pub method: String,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HttpResponse {
pub status_code: u16,
pub headers: Vec<(String, String)>,
pub body: Vec<u8>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TaskResult {
pub passed: bool,
pub name: String,
pub message: String,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DetailedScore {
pub score: f64,
pub tasks_passed: u32,
pub tasks_total: u32,
pub task_results: Vec<TaskResult>,
}
10 changes: 10 additions & 0 deletions crates/wasm-runtime-interface/src/bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ pub fn request_to_input(
agent_data,
challenge_id: challenge_id.to_string(),
params,
submission_id: None,
participant_id: None,
epoch: None,
metadata: Vec::new(),
task_definitions: Vec::new(),
task_definition: None,
environment_config: None,
})
Expand Down Expand Up @@ -162,6 +167,11 @@ mod tests {
agent_data: vec![1, 2, 3],
challenge_id: "test".into(),
params: vec![4, 5, 6],
submission_id: None,
participant_id: None,
epoch: None,
metadata: Vec::new(),
task_definitions: Vec::new(),
task_definition: None,
environment_config: None,
};
Expand Down