feat(import): preserve supported HTTP contracts
This commit is contained in:
1 parent
083974c27f
commit
f8ee7c9bfe
87 files changed
+7496
-534
No files matched your search
@@ -108,8 +108,9 @@ fn search_mode_tool_definitions(policy: &ToolSelectionPolicy) -> Vec<Value> {
|
||||
pub fn tool_definitions(tool: &PublishedAgentTool) -> Vec<Value> {
|
||||
let safety = effective_safety_policy(tool);
|
||||
let requires_confirmation = safety.class.requires_confirmation();
|
||||
let input_schema = operation_input_json_schema(tool);
|
||||
let input_schema = if requires_confirmation {
|
||||
add_confirmation_token(schema_to_json_schema(&tool.operation.input_schema))
|
||||
add_confirmation_token(input_schema)
|
||||
} else {
|
||||
schema_to_json_schema(&tool.operation.input_schema)
|
||||
};
|
||||
@@ -130,6 +131,26 @@ pub fn tool_definitions(tool: &PublishedAgentTool) -> Vec<Value> {
|
||||
)]
|
||||
}
|
||||
|
||||
fn operation_input_json_schema(tool: &PublishedAgentTool) -> Value {
|
||||
let mut projected = schema_to_json_schema(&tool.operation.input_schema);
|
||||
if matches!(&tool.operation.target, Target::Rest(target) if target.request_contract.is_some()) {
|
||||
close_request_objects(&mut projected);
|
||||
}
|
||||
projected
|
||||
}
|
||||
|
||||
fn close_request_objects(schema: &mut Value) {
|
||||
if let Some(variants) = schema.get_mut("anyOf").and_then(Value::as_array_mut) {
|
||||
for variant in variants {
|
||||
close_request_objects(variant);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if schema.get("type").and_then(Value::as_str) == Some("object") {
|
||||
schema["additionalProperties"] = Value::Bool(false);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn analyze_published_tool_catalog(
|
||||
tools: &[PublishedAgentTool],
|
||||
) -> Result<ToolCatalogAnalysis, ToolCatalogAnalysisError> {
|
||||
@@ -187,13 +208,25 @@ fn infer_safety_class(tool: &PublishedAgentTool) -> OperationSafetyClass {
|
||||
HttpMethod::Get => OperationSafetyClass::Read,
|
||||
HttpMethod::Delete => OperationSafetyClass::Destructive,
|
||||
HttpMethod::Post | HttpMethod::Put | HttpMethod::Patch => OperationSafetyClass::Write,
|
||||
HttpMethod::Head | HttpMethod::Options => OperationSafetyClass::Read,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn add_confirmation_token(mut schema: Value) -> Value {
|
||||
let Some(object) = schema.as_object_mut() else {
|
||||
if let Some(variants) = schema.get_mut("anyOf").and_then(Value::as_array_mut) {
|
||||
for variant in variants {
|
||||
add_confirmation_token_to_object(variant);
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
add_confirmation_token_to_object(&mut schema);
|
||||
schema
|
||||
}
|
||||
|
||||
fn add_confirmation_token_to_object(schema: &mut Value) {
|
||||
let Some(object) = schema.as_object_mut() else {
|
||||
return;
|
||||
};
|
||||
let properties = object.entry("properties").or_insert_with(|| json!({}));
|
||||
if let Some(properties) = properties.as_object_mut() {
|
||||
@@ -205,11 +238,10 @@ fn add_confirmation_token(mut schema: Value) -> Value {
|
||||
}),
|
||||
);
|
||||
}
|
||||
schema
|
||||
}
|
||||
|
||||
pub fn schema_to_json_schema(schema: &crank_schema::Schema) -> Value {
|
||||
match schema.kind {
|
||||
let mut projected = match schema.kind {
|
||||
crank_schema::SchemaKind::Object => {
|
||||
let mut properties = serde_json::Map::new();
|
||||
let mut required = Vec::new();
|
||||
@@ -242,11 +274,55 @@ pub fn schema_to_json_schema(schema: &crank_schema::Schema) -> Value {
|
||||
crank_schema::SchemaKind::Boolean => json!({ "type": "boolean" }),
|
||||
crank_schema::SchemaKind::Enum => json!({
|
||||
"type": "string",
|
||||
"enum": schema.enum_values
|
||||
"enum": schema.enum_values.clone()
|
||||
}),
|
||||
crank_schema::SchemaKind::Null => json!({ "type": "null" }),
|
||||
crank_schema::SchemaKind::Oneof => json!({
|
||||
"anyOf": schema.variants.iter().map(schema_to_json_schema).collect::<Vec<_>>()
|
||||
}),
|
||||
};
|
||||
if schema.kind == crank_schema::SchemaKind::Object
|
||||
&& matches!(
|
||||
schema.format.as_deref(),
|
||||
Some("binary" | crank_schema::CLOSED_OBJECT_SCHEMA_FORMAT)
|
||||
)
|
||||
{
|
||||
projected["additionalProperties"] = Value::Bool(false);
|
||||
}
|
||||
if let Some(format) = &schema.format
|
||||
&& format != crank_schema::CLOSED_OBJECT_SCHEMA_FORMAT
|
||||
{
|
||||
projected["format"] = Value::String(format.clone());
|
||||
}
|
||||
if schema.nullable {
|
||||
if schema.kind == crank_schema::SchemaKind::Enum
|
||||
&& let Some(values) = projected.get_mut("enum").and_then(Value::as_array_mut)
|
||||
&& !values.iter().any(Value::is_null)
|
||||
{
|
||||
values.push(Value::Null);
|
||||
}
|
||||
if let Some(kind) = projected.get_mut("type") {
|
||||
match kind {
|
||||
Value::String(name) => {
|
||||
*kind = Value::Array(vec![
|
||||
Value::String(name.clone()),
|
||||
Value::String("null".to_owned()),
|
||||
]);
|
||||
}
|
||||
Value::Array(kinds) if !kinds.iter().any(|kind| kind.as_str() == Some("null")) => {
|
||||
kinds.push(Value::String("null".to_owned()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
} else if let Some(variants) = projected.get_mut("anyOf").and_then(Value::as_array_mut) {
|
||||
variants.push(json!({"type": "null"}));
|
||||
}
|
||||
}
|
||||
if let Some(default) = &schema.default_value {
|
||||
projected["default"] = default.clone();
|
||||
}
|
||||
if let Some(description) = &schema.description {
|
||||
projected["description"] = Value::String(description.clone());
|
||||
}
|
||||
projected
|
||||
}
|
||||
Reference in new issue
Block a user