Skip to main content

AgentCoreApp

Struct AgentCoreApp 

Source
pub struct AgentCoreApp { /* private fields */ }
Expand description

A Rig agent served on AgentCore Runtime’s HTTP protocol contract.

Implementations§

Source§

impl AgentCoreApp

Source

pub fn new(agent: Agent) -> Self

Serve agent.

Source

pub fn max_sessions(self, max: usize) -> Self

Keep at most max sessions’ conversations in memory, forgetting the oldest. This is the default, with DEFAULT_MAX_SESSIONS. It replaces a store set with history_store.

Source

pub fn history_store<S>(self, store: S) -> Self
where S: AsyncStore + WasmCompatSend + WasmCompatSync + 'static,

Keep sessions in store, one session log per session id, so they survive restarts and several apps can share them: any rig_memory store, such as rig_memory::DirStore (feature fs) or the rig-sqlite and rig-postgres stores. Any session id works: one that is 1 to 128 ASCII letters, digits, - or _ names its log as it is, any other is hashed into a safe log name. Two apps, or concurrent invocations of one session, saving at once do not lose either turn: a save that loses a race rereads and retries up to 10 times with a short jittered backoff. If a save still fails, the invocation’s answer is returned anyway, the failure is logged, and that run’s turn is not persisted, so later invocations do not see it. A load that fails is reported to the caller as an error, before the agent runs. The store keeps each session’s whole conversation; history_window limits only what an invocation sends. Replaces the in-memory store set by max_sessions.

Source

pub fn history_window(self, window: impl Recent + 'static) -> Self

Which newest messages of a session each invocation sends the model, such as rig_memory::recent::TokenBudget::new(8_000) or LastMessages(20). Defaults to DEFAULT_HISTORY_TOKENS tokens. A tool result is never sent without its call. An empty window (TokenBudget::new(0), LastMessages(0)) sends no history.

With summarize_history, the summary is sent first, and the window limits only the messages after it.

What the window also does depends on the store. In memory, messages beyond it are forgotten when a run is saved, so memory stays bounded. With history_store, the store keeps the whole conversation and the window only limits what is sent, so a later, wider window sees older turns again.

Source

pub fn summarize_history( self, summarizer: Agent, keep_recent: usize, trigger_messages: usize, ) -> Self

Summarize a session’s older history with summarizer once the session holds more than trigger_messages messages. The messages before the newest keep_recent are replaced by one summary, which becomes the session’s first message: a user message in rig-memory’s compaction frame (“The conversation history before this point was compacted into the following summary:

…”). The newest messages stay verbatim, and the cut never separates a tool call from its result.

The summary is always sent first, before the messages the window keeps, even when the window is narrower than the summary and the kept messages. In a session this option summarizes, a first message that opens with rig-memory’s summary frame text is treated as the summary, even if a caller wrote it. summarizer should be an agent without tools: a tool call in its reply is not used. trigger_messages is raised to keep_recent + 1 when it is not larger, since a cut needs something older than the kept messages.

Summarizing runs before the invocation answers. Two concurrent invocations of one session may both summarize. A summary whose session changed under it (a concurrent summary, or a refused save to the store) is skipped and the history is kept, not lost.

If a summary fails (the summarizer errors or returns no text, or the store refuses the change), a warning is logged, the session keeps its history, and the invocation answers as usual. The next invocation tries again. Without a store, a session whose summaries keep failing keeps at least its newest 2 * trigger_messages messages, so memory stays bounded; older messages are dropped. With history_store, the store keeps the whole conversation as it does without summaries.

Without a store, the summary is kept in memory with the session. With history_store, the summary is written to the store’s log in place of the messages it replaces, which the log keeps in its history.

Each attempt is traced as an agentcore.summarize span under the invocation span. It records the messages summarized and kept, the outcome (summarized, skipped_changed, skipped_nothing_older or failed), and the summarizer’s token usage when the provider reports it. The prompt and the summary text are recorded only with record_content, as the other GenAI content is.

Source

pub fn record_content(self, record: bool) -> Self

Record prompts and replies on the GenAI spans. AgentCore Evaluations needs them to judge a session; they are off by default, as in Rig, because they put conversation content into your telemetry.

This setting always overrides the agent’s own AgentBuilder::record_content_telemetry. The app passes its value to every invocation, so the agent’s setting is ignored when the agent is served through AgentCoreApp. To record content, call .record_content(true) on the app. The default here is false, so an agent built with record_content_telemetry(true) still records nothing unless the app sets it too. This is planned to change in 0.48 (PR #42), so that an app setting left unset keeps the agent’s.

Source

pub fn max_concurrent_tasks(self, max: usize) -> Self

Run at most max invocations at once (at least one). An invocation beyond that gets 429 Too Many Requests before the agent runs; 429 is the conventional throttle status. Without this there is no cap, which is the default: AgentCore Runtime gives each session its own microVM. Outside AgentCore Runtime, a cap around 32 suits most single-process apps.

Source

pub fn max_run_duration(self, max: Duration) -> Self

Stop an invocation that runs longer than max, counting the summarizing step that can run before it. A stopped invocation answers 504 Gateway Timeout (or an {"error"} event when streaming), and its turn is not added to the session. Without this there is no limit, which is the default. Outside AgentCore Runtime, ten minutes suits most agents.

Source

pub fn max_request_bytes(self, bytes: usize) -> Self

Refuse an /invocations body larger than bytes with 413 Payload Too Large, before it is parsed. Defaults to DEFAULT_MAX_REQUEST_BYTES.

Source

pub fn track_task(&self) -> BusyGuard

Mark background work as open until the returned guard drops. While any guard is alive, /ping answers HealthyBusy, so AgentCore Runtime keeps the session running; once they are all dropped it answers Healthy again. Use it for work that outlives an invocation, such as a task the app starts on its own.

Source

pub fn spawn_task<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
where F: Future + Send + 'static, F::Output: Send + 'static,

Run future on the Tokio runtime as background work. /ping answers HealthyBusy until it finishes, or until its task is aborted. Dropping the returned handle does not stop the task. Call it on a running Tokio runtime, as for tokio::spawn.

app.spawn_task(async {
    // Work that outlives the invocation that started it.
});
Source

pub fn router(self) -> Router

The contract’s routes, /invocations, /ping and the WebSocket /ws, as an Axum router.

Source

pub async fn serve(self) -> Result<(), ServeError>

Serve on 0.0.0.0 at PORT (default 8080, what AgentCore sends to) until the process gets Ctrl-C or SIGTERM. A PORT that is set but is not a port number logs a warning and serves on the default.

Trait Implementations§

Source§

impl Clone for AgentCoreApp

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for AgentCoreApp

§

impl !UnwindSafe for AgentCoreApp

§

impl Forget for AgentCoreApp

§

impl Freeze for AgentCoreApp
where *const ArcInner<Inner> is !null: Freeze, PhantomData<ArcInner<Inner>>: Freeze,

§

impl Move for AgentCoreApp

§

impl Send for AgentCoreApp
where Arc<Inner>: Send,

§

impl Sync for AgentCoreApp
where Arc<Inner>: Sync,

§

impl Unpin for AgentCoreApp
where Arc<Inner>: Unpin,

§

impl UnsafeUnpin for AgentCoreApp
where *const ArcInner<Inner> is !null: UnsafeUnpin, PhantomData<ArcInner<Inner>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self> ⓘ

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more