Skip to main content

agent_executor

Macro agent_executor 

Source
macro_rules! agent_executor {
    ($ty:ty, |$this:ident, $ctx:ident, $queue:ident| async $body:block) => { ... };
    ($ty:ty,
        execute: |$this:ident, $ctx:ident, $queue:ident| async $exec_body:block,
        cancel: |$cthis:ident, $cctx:ident, $cqueue:ident| async $cancel_body:block
    ) => { ... };
    ($ty:ty, |$ctx:ident, $queue:ident| async $body:block) => { ... };
    ($ty:ty,
        execute: |$ctx:ident, $queue:ident| async $exec_body:block,
        cancel: |$cctx:ident, $cqueue:ident| async $cancel_body:block
    ) => { ... };
}
Expand description

Generates an AgentExecutor implementation from a closure-like syntax.

§Basic usage (execute only)

use a2a_protocol_server::agent_executor;

struct MyAgent;

agent_executor!(MyAgent, |ctx, queue| async {
    // ctx: &RequestContext, queue: &dyn EventQueueWriter
    Ok(())
});

§With cancel handler

use a2a_protocol_server::agent_executor;

struct CancelableAgent;

agent_executor!(CancelableAgent,
    execute: |ctx, queue| async { Ok(()) },
    cancel: |ctx, queue| async { Ok(()) }
);

§An agent with state

The body of the two forms above is written at the call site, so self is not available in it (a macro cannot name the caller’s self). Name the receiver as a first closure parameter and the body can read the agent’s fields; this is what an agent holding a model client or its configuration needs, and before it such an agent fell back to the full Pin<Box<dyn Future>> signature (audit G-08):

use a2a_protocol_server::agent_executor;

struct Greeter {
    greeting: String,
}

agent_executor!(Greeter, |this, ctx, queue| async {
    let _ = (ctx, queue);
    assert!(!this.greeting.is_empty());
    Ok(())
});

struct Cancelable {
    reason: String,
}

agent_executor!(
    Cancelable,
    execute: |this, ctx, queue| async { let _ = (this, ctx, queue); Ok(()) },
    cancel: |this, ctx, queue| async { let _ = (this.reason.len(), ctx, queue); Ok(()) }
);

Without the receiver parameter, using self in the body does not compile (macro hygiene):

ⓘ
use a2a_protocol_server::agent_executor;

struct Greeter {
    greeting: String,
}

agent_executor!(Greeter, |ctx, queue| async {
    let _ = (ctx, queue, &self.greeting);
    Ok(())
});

The macro cannot implement the trait for a generic type; write the impl out for one.