#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
clippy::all
)]
use super::super::context::{LoadContext, SaveContext};
use super::super::model::model::Model;
use super::super::core::property::Property;
use super::super::template::template::Template;
use super::super::tools::tool::Tool;
#[derive(Debug, Clone, Default)]
pub struct Prompty {
pub name: String,
pub display_name: Option<String>,
pub description: Option<String>,
pub metadata: serde_json::Value,
pub inputs: Vec<Property>,
pub outputs: Vec<Property>,
pub model: Model,
pub tools: Vec<Tool>,
pub template: Option<Template>,
pub instructions: Option<String>,
}
impl Prompty {
pub fn new() -> Self {
Self::default()
}
pub fn from_json(json: &str, ctx: &LoadContext) -> Result<Self, serde_json::Error> {
let value: serde_json::Value = serde_json::from_str(json)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn from_yaml(yaml: &str, ctx: &LoadContext) -> Result<Self, serde_yaml::Error> {
let value: serde_json::Value = serde_yaml::from_str(yaml)?;
Ok(Self::load_from_value(&value, ctx))
}
pub fn load_from_value(value: &serde_json::Value, ctx: &LoadContext) -> Self {
let value = ctx.process_input(value.clone());
Self {
name: value
.get("name")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string(),
display_name: value
.get("displayName")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
description: value
.get("description")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
metadata: value
.get("metadata")
.cloned()
.unwrap_or(serde_json::Value::Null),
inputs: value
.get("inputs")
.map(|v| Self::load_inputs(v, ctx))
.unwrap_or_default(),
outputs: value
.get("outputs")
.map(|v| Self::load_outputs(v, ctx))
.unwrap_or_default(),
model: value
.get("model")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| Model::load_from_value(v, ctx))
.unwrap_or_default(),
tools: value
.get("tools")
.map(|v| Self::load_tools(v, ctx))
.unwrap_or_default(),
template: value
.get("template")
.filter(|v| v.is_object() || v.is_array() || v.is_string())
.map(|v| Template::load_from_value(v, ctx)),
instructions: value
.get("instructions")
.and_then(|v| v.as_str())
.map(|s| s.to_string()),
}
}
pub fn to_value(&self, ctx: &SaveContext) -> serde_json::Value {
let mut result = serde_json::Map::new();
if !self.name.is_empty() {
result.insert(
"name".to_string(),
serde_json::Value::String(self.name.clone()),
);
}
if let Some(ref val) = self.display_name {
result.insert(
"displayName".to_string(),
serde_json::Value::String(val.clone()),
);
}
if let Some(ref val) = self.description {
result.insert(
"description".to_string(),
serde_json::Value::String(val.clone()),
);
}
if !self.metadata.is_null() {
result.insert("metadata".to_string(), self.metadata.clone());
}
if !self.inputs.is_empty() {
result.insert("inputs".to_string(), Self::save_inputs(&self.inputs, ctx));
}
if !self.outputs.is_empty() {
result.insert(
"outputs".to_string(),
Self::save_outputs(&self.outputs, ctx),
);
}
{
let nested = self.model.to_value(ctx);
if !nested.is_null() {
result.insert("model".to_string(), nested);
}
}
if !self.tools.is_empty() {
result.insert("tools".to_string(), Self::save_tools(&self.tools, ctx));
}
if let Some(ref val) = self.template {
let nested = val.to_value(ctx);
if !nested.is_null() {
result.insert("template".to_string(), nested);
}
}
if let Some(ref val) = self.instructions {
result.insert(
"instructions".to_string(),
serde_json::Value::String(val.clone()),
);
}
ctx.process_dict(serde_json::Value::Object(result))
}
pub fn to_json(&self, ctx: &SaveContext) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(&self.to_value(ctx))
}
pub fn to_yaml(&self, ctx: &SaveContext) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_value(ctx))
}
pub fn as_metadata_dict(&self) -> Option<&serde_json::Map<String, serde_json::Value>> {
self.metadata.as_object()
}
fn load_inputs(data: &serde_json::Value, ctx: &LoadContext) -> Vec<Property> {
match data {
serde_json::Value::Array(arr) => arr
.iter()
.map(|v| Property::load_from_value(v, ctx))
.collect(),
serde_json::Value::Object(obj) => obj
.iter()
.filter_map(|(name, value)| {
if value.is_array() {
return None;
}
let mut v = if value.is_object() {
value.clone()
} else {
serde_json::json!({ "kind": value })
};
if let serde_json::Value::Object(ref mut m) = v {
m.entry("name".to_string())
.or_insert_with(|| serde_json::Value::String(name.clone()));
}
Some(Property::load_from_value(&v, ctx))
})
.collect(),
_ => Vec::new(),
}
}
fn save_inputs(items: &[Property], ctx: &SaveContext) -> serde_json::Value {
if ctx.collection_format == "array" {
return serde_json::Value::Array(
items
.iter()
.map(|item| item.to_value(ctx))
.collect::<Vec<_>>(),
);
}
let mut result = serde_json::Map::new();
for item in items {
let mut item_data = match item.to_value(ctx) {
serde_json::Value::Object(m) => m,
other => {
let mut m = serde_json::Map::new();
m.insert("value".to_string(), other);
m
}
};
if let Some(serde_json::Value::String(name)) = item_data.remove("name") {
result.insert(name, serde_json::Value::Object(item_data));
}
}
serde_json::Value::Object(result)
}
fn load_outputs(data: &serde_json::Value, ctx: &LoadContext) -> Vec<Property> {
match data {
serde_json::Value::Array(arr) => arr
.iter()
.map(|v| Property::load_from_value(v, ctx))
.collect(),
_ => Vec::new(),
}
}
fn save_outputs(items: &[Property], ctx: &SaveContext) -> serde_json::Value {
serde_json::Value::Array(
items
.iter()
.map(|item| item.to_value(ctx))
.collect::<Vec<_>>(),
)
}
fn load_tools(data: &serde_json::Value, ctx: &LoadContext) -> Vec<Tool> {
match data {
serde_json::Value::Array(arr) => {
arr.iter().map(|v| Tool::load_from_value(v, ctx)).collect()
}
serde_json::Value::Object(obj) => obj
.iter()
.filter_map(|(name, value)| {
if value.is_array() {
return None;
}
let mut v = if value.is_object() {
value.clone()
} else {
serde_json::json!({ "kind": value })
};
if let serde_json::Value::Object(ref mut m) = v {
m.entry("name".to_string())
.or_insert_with(|| serde_json::Value::String(name.clone()));
}
Some(Tool::load_from_value(&v, ctx))
})
.collect(),
_ => Vec::new(),
}
}
fn save_tools(items: &[Tool], ctx: &SaveContext) -> serde_json::Value {
if ctx.collection_format == "array" {
return serde_json::Value::Array(
items
.iter()
.map(|item| item.to_value(ctx))
.collect::<Vec<_>>(),
);
}
let mut result = serde_json::Map::new();
for item in items {
let mut item_data = match item.to_value(ctx) {
serde_json::Value::Object(m) => m,
other => {
let mut m = serde_json::Map::new();
m.insert("value".to_string(), other);
m
}
};
if let Some(serde_json::Value::String(name)) = item_data.remove("name") {
result.insert(name, serde_json::Value::Object(item_data));
}
}
serde_json::Value::Object(result)
}
}