use std::sync::Arc;
use gadk_agent::AgentError;
use gadk_session::Event;
use serde_json::Value;
use crate::error::{WorkflowError, WorkflowErrorKind};
use crate::tool::utils::CompiledSchema;
use crate::workflow::route::go_quote;
use crate::workflow::validation::default_validate_input;
use crate::workflow::{Node, NodeConfig};
#[derive(Clone)]
pub struct BaseNode {
name: String,
description: String,
config: NodeConfig,
input: Option<Arc<CompiledSchema>>,
output: Option<Arc<CompiledSchema>>,
}
pub(crate) fn compile_schema(schema: Option<Value>) -> Result<Option<Arc<CompiledSchema>>, String> {
match schema {
None => Ok(None),
Some(v) => CompiledSchema::new(v).map(|c| Some(Arc::new(c))),
}
}
impl BaseNode {
pub(crate) fn from_compiled(
name: &str,
description: &str,
cfg: NodeConfig,
input: Option<Arc<CompiledSchema>>,
output: Option<Arc<CompiledSchema>>,
) -> Self {
Self {
name: name.to_owned(),
description: description.to_owned(),
config: cfg,
input,
output,
}
}
pub fn with_schemas(
name: &str,
description: &str,
input: Option<Value>,
output: Option<Value>,
cfg: NodeConfig,
) -> Result<Self, WorkflowError> {
let input = compile_schema(input).map_err(|e| {
WorkflowError::new(
WorkflowErrorKind::Other,
format!("resolving input schema for node {}: {e}", go_quote(name)),
)
})?;
let output = compile_schema(output).map_err(|e| {
WorkflowError::new(
WorkflowErrorKind::Other,
format!("resolving output schema for node {}: {e}", go_quote(name)),
)
})?;
Ok(Self::from_compiled(name, description, cfg, input, output))
}
pub fn new(name: &str, description: &str, cfg: NodeConfig) -> Self {
Self::from_compiled(name, description, cfg, None, None)
}
pub fn name(&self) -> &str {
&self.name
}
pub fn description(&self) -> &str {
&self.description
}
pub fn config(&self) -> &NodeConfig {
&self.config
}
pub fn input_schema(&self) -> Option<&Value> {
self.input.as_deref().map(CompiledSchema::schema)
}
pub fn output_schema(&self) -> Option<&Value> {
self.output.as_deref().map(CompiledSchema::schema)
}
pub(crate) fn input_compiled(&self) -> Option<&CompiledSchema> {
self.input.as_deref()
}
pub(crate) fn output_compiled(&self) -> Option<&CompiledSchema> {
self.output.as_deref()
}
pub fn validate_input(&self, v: Value) -> Result<Value, WorkflowError> {
default_validate_input(v, self.input_compiled())
.map_err(|m| WorkflowError::new(WorkflowErrorKind::InputValidation, m))
}
pub fn validate_output(&self, v: Value) -> Result<Value, WorkflowError> {
default_validate_output(v, self.output_compiled())
.map_err(|m| WorkflowError::new(WorkflowErrorKind::OutputValidation, m))
}
}
pub(crate) fn default_validate_output(
out: Value,
schema: Option<&CompiledSchema>,
) -> Result<Value, String> {
let Some(schema) = schema else { return Ok(out) };
if out.is_null() {
return Ok(out);
}
let err = match schema.validate(&out) {
Ok(()) => return Ok(out),
Err(e) => e,
};
if let Value::String(text) = &out
&& let Some(v) = project_text_onto_schema(text, schema)
{
return Ok(v);
}
Err(err)
}
#[cfg(test)]
pub(crate) fn default_validate_content_output(
content: &gadk_genai::Content,
schema: Option<&CompiledSchema>,
) -> Result<Value, String> {
let v = serde_json::to_value(content).map_err(|e| e.to_string())?;
let Some(schema) = schema else { return Ok(v) };
let err = match schema.validate(&v) {
Ok(()) => return Ok(v),
Err(e) => e,
};
let text: String = content
.parts
.iter()
.filter(|p| !p.text.is_empty() && !p.thought)
.map(|p| p.text.as_str())
.collect();
project_text_onto_schema(&text, schema).ok_or(err)
}
fn project_text_onto_schema(s: &str, schema: &CompiledSchema) -> Option<Value> {
if root_schema_is_string(schema) {
let v = Value::String(s.to_owned());
return schema.validate(&v).ok().map(|()| v);
}
if s.trim().is_empty() {
return None;
}
let parsed: Value = serde_json::from_str(s).ok()?;
schema.validate(&parsed).ok().map(|()| parsed)
}
fn root_schema_is_string(schema: &CompiledSchema) -> bool {
match schema.schema().get("type") {
Some(Value::String(t)) => t == "string",
Some(Value::Array(ts)) => ts.iter().any(|t| t == "string"),
_ => false,
}
}
pub(crate) fn validate_and_stamp_output(
n: &dyn Node,
out: Value,
ev: &mut Event,
) -> Result<Value, AgentError> {
let validated = n.validate_output(out).map_err(|e| {
let msg = format!(
"{}: output validation failed for node {}: {}",
WorkflowError::sentinel_text(WorkflowErrorKind::NodeFailed),
go_quote(n.name()),
e
);
AgentError::from(
WorkflowError::new(WorkflowErrorKind::OutputValidation, msg).with_source(e.into()),
)
})?;
if ev.output.is_some() {
ev.output = Some(validated.clone());
}
Ok(validated)
}