агенты: добавить поиск инструментов по каталогу
This commit is contained in:
1 parent
63f8ee333f
commit
99bd05c145
35 files changed
+2088
-155
No files matched your search
@@ -1,10 +1,110 @@
|
||||
use crank_core::{
|
||||
HttpMethod, OperationSafetyClass, OperationSafetyPolicy, Target, ToolCatalogAnalysis,
|
||||
ToolCatalogAnalysisError, ToolQualityCatalogTool, analyze_tool_catalog,
|
||||
HttpMethod, OperationSafetyClass, OperationSafetyPolicy, SearchableTool, Target,
|
||||
ToolAccessMode, ToolCatalogAnalysis, ToolCatalogAnalysisError, ToolQualityCatalogTool,
|
||||
ToolSelectionPolicy, analyze_tool_catalog,
|
||||
};
|
||||
use crank_registry::PublishedAgentTool;
|
||||
use crank_registry::{PublishedAgentCatalog, PublishedAgentTool};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
pub const SEARCH_TOOLS_NAME: &str = "search_tools";
|
||||
pub const CALL_TOOL_NAME: &str = "call_tool";
|
||||
|
||||
pub fn catalog_tool_definitions(catalog: &PublishedAgentCatalog) -> Vec<Value> {
|
||||
match catalog.tool_selection_policy.mode {
|
||||
ToolAccessMode::Direct => catalog.tools.iter().flat_map(tool_definitions).collect(),
|
||||
ToolAccessMode::Search => search_mode_tool_definitions(&catalog.tool_selection_policy),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn searchable_tools(catalog: &PublishedAgentCatalog) -> Vec<SearchableTool> {
|
||||
catalog
|
||||
.tools
|
||||
.iter()
|
||||
.map(|tool| {
|
||||
let groups = catalog
|
||||
.tool_selection_policy
|
||||
.groups
|
||||
.iter()
|
||||
.filter(|group| group.tool_names.iter().any(|name| name == &tool.tool_name))
|
||||
.collect::<Vec<_>>();
|
||||
SearchableTool {
|
||||
name: tool.tool_name.clone(),
|
||||
title: tool.tool_title.clone(),
|
||||
description: tool.tool_description.clone(),
|
||||
input_schema: tool_definitions(tool)
|
||||
.into_iter()
|
||||
.next()
|
||||
.and_then(|definition| definition.get("inputSchema").cloned())
|
||||
.unwrap_or_else(|| json!({"type": "object"})),
|
||||
group_ids: groups.iter().map(|group| group.id.clone()).collect(),
|
||||
group_context: groups
|
||||
.iter()
|
||||
.map(|group| format!("{} {}", group.name, group.description))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn search_mode_tool_definitions(policy: &ToolSelectionPolicy) -> Vec<Value> {
|
||||
let group_catalog = if policy.groups.is_empty() {
|
||||
"Разделы каталога не заданы; выполняйте поиск по всему каталогу.".to_owned()
|
||||
} else {
|
||||
policy
|
||||
.groups
|
||||
.iter()
|
||||
.map(|group| format!("{} — {}: {}", group.id, group.name, group.description))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
};
|
||||
|
||||
vec![
|
||||
tool_definition(
|
||||
SEARCH_TOOLS_NAME,
|
||||
"Подобрать инструменты",
|
||||
&format!(
|
||||
"Находит подходящие инструменты агента и возвращает их полные входные схемы. Доступные разделы:\n{group_catalog}"
|
||||
),
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "Краткое описание требуемого действия"
|
||||
},
|
||||
"group_ids": {
|
||||
"type": "array",
|
||||
"items": {"type": "string"},
|
||||
"description": "Необязательный список разделов каталога"
|
||||
},
|
||||
"max_results": {
|
||||
"type": "integer",
|
||||
"minimum": 1,
|
||||
"maximum": 20,
|
||||
"default": policy.search.max_results
|
||||
}
|
||||
},
|
||||
"required": ["query"]
|
||||
}),
|
||||
),
|
||||
tool_definition(
|
||||
CALL_TOOL_NAME,
|
||||
"Вызвать найденный инструмент",
|
||||
"Вызывает инструмент, ранее найденный через search_tools. Передайте имя, аргументы по полученной схеме и версию каталога из результата поиска.",
|
||||
json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {"type": "string"},
|
||||
"arguments": {"type": "object"},
|
||||
"catalog_revision": {"type": "string"}
|
||||
},
|
||||
"required": ["name", "arguments", "catalog_revision"]
|
||||
}),
|
||||
),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn tool_definitions(tool: &PublishedAgentTool) -> Vec<Value> {
|
||||
let safety = effective_safety_policy(tool);
|
||||
let requires_confirmation = safety.class.requires_confirmation();
|
||||
|
||||
Reference in new issue
Block a user