pub trait Capability:
Send
+ Sync
+ 'static {
// Required methods
fn name(&self) -> &'static str;
fn description(&self) -> &'static str;
fn schema(&self) -> Value;
fn tags(&self) -> &[&'static str];
fn source(&self) -> CapabilitySource;
fn call<'life0, 'async_trait>(
&'life0 self,
args: Value,
) -> Pin<Box<dyn Future<Output = CapResult<Value>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
// Provided methods
fn version(&self) -> &'static str { ... }
fn requires_confirmation(&self) -> bool { ... }
fn validate_args<'life0, 'life1, 'async_trait>(
&'life0 self,
args: &'life1 Value,
) -> Pin<Box<dyn Future<Output = CapResult<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait { ... }
}Expand description
统一能力抽象 trait。
Skills(AI 内置能力)和 Plugins(业务插件)都实现此 trait,
通过 CapabilityRegistry 统一注册、发现和调用。
§实现示例
use async_trait::async_trait;
use serde_json::{json, Value};
use sz_rust_capability::{Capability, CapabilitySource, CapResult};
struct SearchCustomerCapability;
#[async_trait]
impl Capability for SearchCustomerCapability {
fn name(&self) -> &'static str { "crm.search_customer" }
fn description(&self) -> &'static str { "搜索客户" }
fn schema(&self) -> Value {
json!({
"type": "object",
"properties": { "keyword": { "type": "string" } },
"required": ["keyword"]
})
}
fn tags(&self) -> &[&'static str] { &["crm", "search", "read"] }
fn source(&self) -> CapabilitySource { CapabilitySource::Plugin }
async fn call(&self, args: Value) -> CapResult<Value> {
let keyword = args.get("keyword").and_then(|v| v.as_str()).unwrap_or("");
Ok(json!({ "results": [keyword] }))
}
}Required Methods§
Sourcefn description(&self) -> &'static str
fn description(&self) -> &'static str
人类可读的能力描述。
能力标签,用于 find_by_tags 搜索。多标签 AND 逻辑。
Sourcefn source(&self) -> CapabilitySource
fn source(&self) -> CapabilitySource
能力来源类型(Skill/Plugin/Service)。
Provided Methods§
Sourcefn requires_confirmation(&self) -> bool
fn requires_confirmation(&self) -> bool
是否需要人工确认(HITL),默认 false。
Sourcefn validate_args<'life0, 'life1, 'async_trait>(
&'life0 self,
args: &'life1 Value,
) -> Pin<Box<dyn Future<Output = CapResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn validate_args<'life0, 'life1, 'async_trait>(
&'life0 self,
args: &'life1 Value,
) -> Pin<Box<dyn Future<Output = CapResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
参数校验,默认实现委托 validate_json_schema 做轻量校验。
能力可覆盖此方法做完整 JSON Schema 校验。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".