Skip to main content

Capability

Trait Capability 

Source
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§

Source

fn name(&self) -> &'static str

能力名称,全局唯一,格式建议 {source_prefix}.{capability_name}

Source

fn description(&self) -> &'static str

人类可读的能力描述。

Source

fn schema(&self) -> Value

参数 JSON Schema,描述 call 方法的输入参数格式。

Source

fn tags(&self) -> &[&'static str]

能力标签,用于 find_by_tags 搜索。多标签 AND 逻辑。

Source

fn source(&self) -> CapabilitySource

能力来源类型(Skill/Plugin/Service)。

Source

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,

执行能力,接受 JSON 参数,返回 JSON 结果。

Provided Methods§

Source

fn version(&self) -> &'static str

能力版本,默认 “1.0.0”。

Source

fn requires_confirmation(&self) -> bool

是否需要人工确认(HITL),默认 false。

Source

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".

Implementors§