Add destructive confirmation and import guidance
This commit is contained in:
@@ -1,12 +1,29 @@
|
||||
use crank_core::{HttpMethod, OperationSafetyClass, OperationSafetyPolicy, Target};
|
||||
use crank_registry::PublishedAgentTool;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub fn tool_definitions(tool: &PublishedAgentTool) -> Vec<Value> {
|
||||
let safety = effective_safety_policy(tool);
|
||||
let requires_confirmation = safety.class.requires_confirmation();
|
||||
let input_schema = if requires_confirmation {
|
||||
add_confirmation_token(schema_to_json_schema(&tool.operation.input_schema))
|
||||
} else {
|
||||
schema_to_json_schema(&tool.operation.input_schema)
|
||||
};
|
||||
let description = if requires_confirmation {
|
||||
format!(
|
||||
"{}\n\nЭта операция выполняется в два шага: первый вызов возвращает токен подтверждения без обращения к внешнему API, второй вызов должен повторить те же аргументы и добавить _crank_confirmation_token.",
|
||||
tool.tool_description
|
||||
)
|
||||
} else {
|
||||
tool.tool_description.clone()
|
||||
};
|
||||
|
||||
vec![tool_definition(
|
||||
&tool.tool_name,
|
||||
&tool.tool_title,
|
||||
&tool.tool_description,
|
||||
schema_to_json_schema(&tool.operation.input_schema),
|
||||
&description,
|
||||
input_schema,
|
||||
)]
|
||||
}
|
||||
|
||||
@@ -19,6 +36,44 @@ pub fn tool_definition(name: &str, title: &str, description: &str, input_schema:
|
||||
})
|
||||
}
|
||||
|
||||
fn effective_safety_policy(tool: &PublishedAgentTool) -> OperationSafetyPolicy {
|
||||
tool.operation
|
||||
.execution_config
|
||||
.safety
|
||||
.clone()
|
||||
.unwrap_or_else(|| OperationSafetyPolicy {
|
||||
class: infer_safety_class(tool),
|
||||
confirmation: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn infer_safety_class(tool: &PublishedAgentTool) -> OperationSafetyClass {
|
||||
match &tool.operation.target {
|
||||
Target::Rest(target) => match target.method {
|
||||
HttpMethod::Get => OperationSafetyClass::Read,
|
||||
HttpMethod::Delete => OperationSafetyClass::Destructive,
|
||||
HttpMethod::Post | HttpMethod::Put | HttpMethod::Patch => OperationSafetyClass::Write,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn add_confirmation_token(mut schema: Value) -> Value {
|
||||
let Some(object) = schema.as_object_mut() else {
|
||||
return schema;
|
||||
};
|
||||
let properties = object.entry("properties").or_insert_with(|| json!({}));
|
||||
if let Some(properties) = properties.as_object_mut() {
|
||||
properties.insert(
|
||||
"_crank_confirmation_token".to_owned(),
|
||||
json!({
|
||||
"type": "string",
|
||||
"description": "Одноразовый токен подтверждения, который Crank возвращает после первого вызова опасной операции."
|
||||
}),
|
||||
);
|
||||
}
|
||||
schema
|
||||
}
|
||||
|
||||
pub fn schema_to_json_schema(schema: &crank_schema::Schema) -> Value {
|
||||
match schema.kind {
|
||||
crank_schema::SchemaKind::Object => {
|
||||
|
||||
Reference in New Issue
Block a user