use serde_json::{Map, Value};
use weirdnet_agent::remote::infer::v2::{EmbedRequest, Request, ResponseFormat};
use weirdnet_agent::{Message, Role, ToolCall, ToolSpec};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BadRequest(pub String);
fn bad(msg: impl Into<String>) -> BadRequest {
BadRequest(msg.into())
}
fn object(body: Value) -> Result<Map<String, Value>, BadRequest> {
match body {
Value::Object(o) => Ok(o),
_ => Err(bad("body must be a JSON object")),
}
}
fn content(v: Option<&Value>) -> Result<String, BadRequest> {
match v {
None | Some(Value::Null) => Ok(String::new()),
Some(Value::String(s)) => Ok(s.clone()),
Some(Value::Array(parts)) => {
let mut out = String::new();
for p in parts {
match p["type"].as_str() {
Some("text") => out.push_str(p["text"].as_str().unwrap_or_default()),
Some(other) => return Err(bad(format!("content parts of type `{other}` are not supported"))),
None => return Err(bad("content parts need a `type`")),
}
}
Ok(out)
}
Some(_) => Err(bad("`content` must be a string or an array of parts")),
}
}
fn tool_calls(v: Option<&Value>) -> Result<Vec<ToolCall>, BadRequest> {
let Some(Value::Array(calls)) = v else { return Ok(Vec::new()) };
calls
.iter()
.map(|c| {
let f = &c["function"];
let name = f["name"].as_str().ok_or_else(|| bad("tool calls need `function.name`"))?;
let arguments = match &f["arguments"] {
Value::String(s) => serde_json::from_str(s).unwrap_or_else(|_| Value::String(s.clone())),
Value::Null => Value::Object(Map::new()),
other => other.clone(),
};
Ok(ToolCall { id: c["id"].as_str().unwrap_or_default().to_string(), name: name.to_string(), arguments })
})
.collect()
}
fn message(m: &Value) -> Result<Message, BadRequest> {
let text = content(m.get("content"))?;
match m["role"].as_str() {
Some("system" | "developer") => Ok(Message::system(text)),
Some("user") => Ok(Message::user(text)),
Some("assistant") => Ok(Message::assistant(text, tool_calls(m.get("tool_calls"))?)),
Some("tool") => Ok(Message::tool(m["tool_call_id"].as_str().ok_or_else(|| bad("tool messages need `tool_call_id`"))?, text)),
Some(other) => Err(bad(format!("unknown role `{other}`"))),
None => Err(bad("messages need a `role`")),
}
}
fn tools(v: Option<&Value>) -> Result<Vec<ToolSpec>, BadRequest> {
let Some(Value::Array(tools)) = v else { return Ok(Vec::new()) };
tools
.iter()
.map(|t| {
if t["type"].as_str().is_some_and(|k| k != "function") {
return Err(bad("only function tools are supported"));
}
let f = &t["function"];
let name = f["name"].as_str().ok_or_else(|| bad("tools need `function.name`"))?;
let mut spec = ToolSpec::new(name, f["description"].as_str().unwrap_or_default());
if !f["parameters"].is_null() {
spec.input_schema = f["parameters"].clone();
}
Ok(spec)
})
.collect()
}
fn response_format(v: Option<&Value>) -> Result<Option<ResponseFormat>, BadRequest> {
let Some(v) = v.filter(|v| !v.is_null()) else { return Ok(None) };
match v["type"].as_str() {
Some("text") => Ok(Some(ResponseFormat::Text)),
Some("json_object") => Ok(Some(ResponseFormat::Json)),
Some("json_schema") => {
let s = &v["json_schema"];
Ok(Some(ResponseFormat::JsonSchema {
name: s["name"].as_str().unwrap_or("response").to_string(),
schema: s["schema"].clone(),
strict: s["strict"].as_bool().unwrap_or(false),
}))
}
_ => Err(bad("`response_format.type` must be text, json_object or json_schema")),
}
}
fn f32_of(o: &Map<String, Value>, key: &str) -> Result<Option<f32>, BadRequest> {
match o.get(key) {
None | Some(Value::Null) => Ok(None),
Some(v) => v.as_f64().map(|f| Some(f as f32)).ok_or_else(|| bad(format!("`{key}` must be a number"))),
}
}
fn u32_of(o: &Map<String, Value>, key: &str) -> Result<Option<u32>, BadRequest> {
match o.get(key) {
None | Some(Value::Null) => Ok(None),
Some(v) => v.as_u64().and_then(|n| u32::try_from(n).ok()).map(Some).ok_or_else(|| bad(format!("`{key}` must be a whole number"))),
}
}
fn model(o: &Map<String, Value>) -> Result<Option<String>, BadRequest> {
match o.get("model") {
None | Some(Value::Null) => Ok(None),
Some(Value::String(m)) if !m.is_empty() => Ok(Some(m.clone())),
_ => Err(bad("`model` must be a non-empty string")),
}
}
fn flag(o: &Map<String, Value>, key: &str) -> Result<bool, BadRequest> {
match o.get(key) {
None | Some(Value::Null) => Ok(false),
Some(Value::Bool(b)) => Ok(*b),
Some(_) => Err(bad(format!("`{key}` must be true or false"))),
}
}
pub fn chat(body: Value) -> Result<(Request, bool), BadRequest> {
let o = object(body)?;
let messages = match o.get("messages") {
Some(Value::Array(m)) if !m.is_empty() => m.iter().map(message).collect::<Result<Vec<_>, _>>()?,
_ => return Err(bad("`messages` must be a non-empty array")),
};
let stream = o.get("stream").and_then(Value::as_bool).unwrap_or(false);
let continues = flag(&o, "continue_final_message")?;
if continues {
if flag(&o, "add_generation_prompt")? {
return Err(bad("`continue_final_message` and `add_generation_prompt` cannot both be true"));
}
if !messages.last().is_some_and(|m| m.role == Role::Assistant && m.tool_calls.is_empty()) {
return Err(bad("`continue_final_message` needs the last message to be an assistant message without tool calls"));
}
}
let mut req = Request::messages(messages);
if continues {
req = req.continuing();
}
sampling_and_seed(&o, &mut req)?;
req.response_format = response_format(o.get("response_format"))?;
req.tools = tools(o.get("tools"))?;
Ok((req, stream))
}
fn sampling_and_seed(o: &Map<String, Value>, req: &mut Request) -> Result<(), BadRequest> {
req.model = model(o)?;
let s = &mut req.sampling;
s.temperature = f32_of(o, "temperature")?;
s.top_p = f32_of(o, "top_p")?;
s.top_k = u32_of(o, "top_k")?;
s.min_p = f32_of(o, "min_p")?;
s.max_tokens = match u32_of(o, "max_completion_tokens")? {
Some(n) => Some(n),
None => u32_of(o, "max_tokens")?,
};
s.stop = match o.get("stop") {
None | Some(Value::Null) => Vec::new(),
Some(Value::String(s)) => vec![s.clone()],
Some(Value::Array(a)) if a.iter().all(Value::is_string) => a.iter().filter_map(|v| v.as_str().map(str::to_string)).collect(),
Some(_) => return Err(bad("`stop` must be a string or an array of strings")),
};
s.presence_penalty = f32_of(o, "presence_penalty")?;
s.frequency_penalty = f32_of(o, "frequency_penalty")?;
s.repeat_penalty = match f32_of(o, "repeat_penalty")? {
Some(p) => Some(p),
None => f32_of(o, "repetition_penalty")?,
};
req.seed = match o.get("seed") {
None | Some(Value::Null) => None,
Some(v) => Some(v.as_u64().ok_or_else(|| bad("`seed` must be a whole number"))?),
};
Ok(())
}
pub fn completion(body: Value) -> Result<(Request, bool), BadRequest> {
let o = object(body)?;
let prompt = match o.get("prompt") {
Some(Value::String(p)) => p.clone(),
Some(Value::Array(a)) if a.len() == 1 && a[0].is_string() => a[0].as_str().unwrap_or_default().to_string(),
Some(Value::Array(a)) if a.iter().all(Value::is_string) => return Err(bad("one prompt per request")),
_ => return Err(bad("`prompt` must be a string")),
};
for key in ["n", "best_of"] {
if u32_of(&o, key)?.is_some_and(|n| n > 1) {
return Err(bad(format!("`{key}` above 1 is not supported")));
}
}
if o.get("echo").and_then(Value::as_bool).unwrap_or(false) {
return Err(bad("`echo` is not supported"));
}
for key in ["suffix", "logprobs"] {
if o.get(key).is_some_and(|v| !v.is_null() && *v != Value::Bool(false)) {
return Err(bad(format!("`{key}` is not supported")));
}
}
let stream = o.get("stream").and_then(Value::as_bool).unwrap_or(false);
let mut req = Request::prompt(prompt);
sampling_and_seed(&o, &mut req)?;
Ok((req, stream))
}
pub fn embed(body: Value) -> Result<EmbedRequest, BadRequest> {
let o = object(body)?;
let inputs = match o.get("input") {
Some(Value::String(s)) => vec![s.clone()],
Some(Value::Array(a)) if !a.is_empty() && a.iter().all(Value::is_string) => a.iter().filter_map(|v| v.as_str().map(str::to_string)).collect(),
_ => return Err(bad("`input` must be a string or a non-empty array of strings")),
};
if o.get("encoding_format").and_then(Value::as_str).is_some_and(|f| f != "float") {
return Err(bad("only encoding_format = \"float\" is supported"));
}
let mut req = EmbedRequest::new(inputs);
req.model = model(&o)?;
Ok(req)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
use weirdnet_agent::Role;
use weirdnet_agent::remote::infer::v2::Input;
#[test]
fn maps_messages_sampling_tools_and_format() {
let (req, stream) = chat(json!({
"model": "m", "stream": true, "temperature": 0, "max_tokens": 9, "stop": "\n", "seed": 7, "repetition_penalty": 1.1,
"messages": [
{"role": "developer", "content": "be brief"},
{"role": "user", "content": [{"type": "text", "text": "a"}, {"type": "text", "text": "b"}]},
{"role": "assistant", "content": null, "tool_calls": [{"id": "c1", "type": "function", "function": {"name": "f", "arguments": "{\"x\":1}"}}]},
{"role": "tool", "tool_call_id": "c1", "content": "ok"}
],
"tools": [{"type": "function", "function": {"name": "f", "description": "d", "parameters": {"type": "object"}}}],
"response_format": {"type": "json_schema", "json_schema": {"name": "r", "schema": {"type": "object"}, "strict": true}},
"logit_bias": {}
}))
.unwrap();
assert!(stream);
assert_eq!(req.model.as_deref(), Some("m"));
let Input::Messages(m) = &req.input else { panic!() };
assert_eq!(m.iter().map(|m| m.role).collect::<Vec<_>>(), [Role::System, Role::User, Role::Assistant, Role::Tool]);
assert_eq!(m[1].content, "ab");
assert_eq!(m[2].tool_calls[0].arguments, json!({"x": 1}));
assert_eq!(m[3].tool_call_id.as_deref(), Some("c1"));
assert_eq!((req.sampling.temperature, req.sampling.max_tokens, req.sampling.repeat_penalty), (Some(0.0), Some(9), Some(1.1)));
assert_eq!((req.sampling.stop.clone(), req.seed), (vec!["\n".to_string()], Some(7)));
assert_eq!(req.tools[0].input_schema, json!({"type": "object"}));
assert!(matches!(req.response_format, Some(ResponseFormat::JsonSchema { strict: true, .. })));
}
#[test]
fn refuses_what_it_cannot_carry() {
assert!(chat(json!({"messages": []})).is_err());
assert!(chat(json!({"messages": [{"role": "user", "content": [{"type": "image_url", "image_url": {}}]}]})).is_err());
assert!(chat(json!({"messages": [{"role": "user", "content": "x"}], "temperature": "hot"})).is_err());
assert!(chat(json!([1])).is_err());
assert!(embed(json!({"input": "x", "encoding_format": "base64"})).is_err());
assert!(completion(json!({"prompt": ["a", "b"]})).is_err());
assert!(completion(json!({"prompt": "a", "n": 2})).is_err());
assert!(completion(json!({"prompt": "a", "echo": true})).is_err());
assert!(completion(json!({"prompt": "a", "logprobs": 2})).is_err());
assert!(completion(json!({"prompt": 3})).is_err());
assert_eq!(embed(json!({"input": ["a", "b"], "model": "e"})).unwrap().inputs, ["a", "b"]);
}
#[test]
fn continue_final_message_moves_the_last_assistant_message_into_the_prefix() {
let msgs = json!([{"role": "user", "content": "q"}, {"role": "assistant", "content": "Hel"}]);
let (req, _) = chat(json!({"messages": msgs, "continue_final_message": true, "add_generation_prompt": false})).unwrap();
assert_eq!(req.inline_messages().unwrap().len(), 1);
assert_eq!(req.assistant_prefix.as_ref().map(|p| p.text.as_str()), Some("Hel"));
let (req, _) = chat(json!({"messages": msgs})).unwrap();
assert_eq!((req.inline_messages().unwrap().len(), req.assistant_prefix.is_none()), (2, true));
assert!(chat(json!({"messages": msgs, "continue_final_message": true, "add_generation_prompt": true})).is_err());
assert!(chat(json!({"messages": [{"role": "user", "content": "q"}], "continue_final_message": true})).is_err());
assert!(chat(json!({"messages": msgs, "continue_final_message": "yes"})).is_err());
}
}
#[cfg(test)]
mod completion_tests {
use super::*;
use serde_json::json;
#[test]
fn maps_a_prompt_and_its_sampling() {
let (req, stream) = completion(
json!({"model": "base", "prompt": ["Once"], "max_tokens": 5, "temperature": 0.5, "stop": ["\n"], "seed": 3, "n": 1, "logprobs": null}),
)
.unwrap();
assert!(!stream);
assert_eq!((req.raw_prompt(), req.model.as_deref()), (Some("Once"), Some("base")));
assert_eq!((req.sampling.max_tokens, req.sampling.temperature, req.seed), (Some(5), Some(0.5), Some(3)));
assert_eq!(req.sampling.stop, ["\n"]);
assert!(req.tools.is_empty());
}
}