adk-core 3.0.0

Core traits and types for Rust Agent Development Kit (ADK-Rust) agents, tools, sessions, and events
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! # adk-core
//!
//! Core traits and types for ADK agents, tools, sessions, and events.
//!
//! ## Overview
//!
//! This crate provides the foundational abstractions for the Agent Development Kit:
//!
//! - [`Agent`] - The fundamental trait for all agents
//! - [`Tool`] / [`Toolset`] - For extending agents with custom capabilities
//! - [`Llm`] - The interface every model provider implements
//! - [`Session`] / [`State`] - For managing conversation context
//! - [`Event`] - For streaming agent responses
//! - [`AdkError`] / [`Result`] - Unified error handling
//! - [`SharedState`] / [`SharedStateError`] - Thread-safe key-value store for parallel agent coordination
//! - [`ToolConfirmationPolicy`] / [`ToolConfirmationRequest`] - Human-in-the-loop tool authorization
//! - [`ToolPolicy`] / [`DeclarativePolicy`] / [`authorize_tool_call`] - The governed tool execution path
//! - [`GovernanceControl`] - Organisation-wide kill switch
//! - [`RunBudget`] / [`SpendLedger`] / [`ActionLedger`] - Run limits, spend caps, and at-most-once tool calls
//! - [`AgentId`] / [`AgentPrincipal`] / [`ActingContext`] / [`ActingConfig`] - Agent identity
//! - [`LeaseStore`] / [`DurableRunStore`] / [`ScheduleStore`] / [`GovernanceStateStore`] -
//!   Durable runtime contracts: fenced leases, the run queue, schedules, and freeze state
//! - [`subject::SubjectRights`] / [`subject::ConsentStore`] / [`subject::SubjectRegistry`] -
//!   Visitor ids, consent, and a subject's export, erasure, and links across stores
//!
//! ## Feature Flags
//!
//! | Feature | Enables |
//! |---------|---------|
//! | `rustls` | [`ensure_crypto_provider`] installs the `aws-lc-rs` rustls provider |
//! | `conformance` | The `conformance` module: suites for durable store backends |
//! | `cron` | Cron schedules in IANA time zones |
//! | `webhooks` | The `webhook` module: Standard Webhooks signing and verification |
//! | `egress-dns` | `egress::SystemResolver`: the operating system resolver for egress checks |
//! | `egress-client` | `egress::GuardedClient`: an HTTP client that enforces an `EgressPolicy` |
//! | `webhook-delivery` | The `webhook_delivery` module: signed outbound delivery |
//! | `auth-bridge` | The `request_context` module: `RequestContextExtractor`, the trait an HTTP server authenticates requests through |
//!
//! ## Quick Start
//!
//! ```rust
//! use std::sync::Arc;
//!
//! use adk_core::{Result, Tool, ToolContext, ToolEffect};
//! use async_trait::async_trait;
//! use serde_json::{Value, json};
//!
//! struct Echo;
//!
//! #[async_trait]
//! impl Tool for Echo {
//!     fn name(&self) -> &str {
//!         "echo"
//!     }
//!
//!     fn description(&self) -> &str {
//!         "Returns its arguments"
//!     }
//!
//!     fn effect(&self) -> ToolEffect {
//!         ToolEffect::ReadOnly
//!     }
//!
//!     async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
//!         Ok(json!({ "echo": args }))
//!     }
//! }
//!
//! assert_eq!(Echo.effect(), ToolEffect::ReadOnly);
//! ```
//!
//! ## Core Traits
//!
//! ### Agent
//!
//! The [`Agent`] trait defines the interface for all agents. `name`, `description`, `sub_agents`,
//! and `run` are required; `description` returns `&str`, and a leaf agent returns `&[]` from
//! `sub_agents`.
//!
//! ```rust
//! use std::sync::Arc;
//!
//! use adk_core::{Agent, EventStream, InvocationContext, Result};
//! use async_trait::async_trait;
//!
//! struct Silent;
//!
//! #[async_trait]
//! impl Agent for Silent {
//!     fn name(&self) -> &str {
//!         "silent"
//!     }
//!
//!     fn description(&self) -> &str {
//!         "Produces no events"
//!     }
//!
//!     fn sub_agents(&self) -> &[Arc<dyn Agent>] {
//!         &[]
//!     }
//!
//!     async fn run(&self, _ctx: Arc<dyn InvocationContext>) -> Result<EventStream> {
//!         Ok(Box::pin(futures::stream::empty()))
//!     }
//! }
//! ```
//!
//! ### Tool
//!
//! The [`Tool`] trait defines custom capabilities. [`Tool::effect`] declares whether a call is
//! read-only, idempotent, or non-idempotent, which governs retries and the action ledger.
//!
//! ## State Management
//!
//! State uses typed prefixes for organization:
//!
//! - `user:` - User preferences (persists across sessions)
//! - `app:` - Application state (application-wide)
//! - `temp:` - Visible for the rest of the invocation; session services do not persist it
#![allow(clippy::result_large_err)]
#![deny(missing_docs)]

/// How a run's agents act: the identity model, the run-level acting configuration,
/// delegation, and subject delegation policies.
pub mod acting;
/// Durable record of non-idempotent tool calls.
pub mod action_ledger;
/// Core agent trait and event stream type.
pub mod agent;
/// Starting an agent turn without owning the runner's construction.
pub mod agent_invoker;
/// Dynamic agent loading by name.
pub mod agent_loader;
/// Approval service: an inbox of approval requests with quorum, delegation, escalation,
/// and expiry over an approval store.
pub mod approval;
/// Append-only audit log records and the store that holds them.
pub mod audit_log;
/// Run budgets: limits on model calls, tokens, cost, wall time, and tool calls.
pub mod budget;
/// Callback type aliases for agent, model, and tool lifecycle hooks.
pub mod callbacks;
/// Time sources for stores whose behaviour depends on the current time.
pub mod clock;
/// Provider-neutral computer actuation: actions, screenshots, and the actuator trait.
pub mod computer;
/// Conformance suites that every durable store backend runs against its implementation.
#[cfg(feature = "conformance")]
pub mod conformance;
/// Invocation context traits: state, session, artifacts, memory, and run configuration.
pub mod context;
/// Agent credentials, identity sources, and resource-scoped token brokers.
pub mod credential;
/// Typed decisions: a model picks one of a fixed set of options, with a rationale and a
/// confidence.
pub mod decision;
pub mod egress;
/// Unified structured error type and result alias.
pub mod error;
/// Event types representing agent interactions in a conversation.
pub mod event;
mod event_stream;
/// The LLM summarizer that session and in-run compaction share.
pub mod event_summarizer;
/// Evidence an approver sees with a held call: images by digest and artifact.
pub mod evidence;
/// Governed tool execution: kill switch, durable approvals, and the authorization path.
pub mod governance;
/// Durable freeze state for the kill switch, shared by every replica.
pub mod governance_state;
/// Sources a run retrieved, and the citations an answer makes to them.
pub mod grounding;
/// Building model history from session events: other agents' turns as context, tool
/// branches, and balanced history cuts.
pub mod history;
/// Typed identity primitives for app, user, session, and invocation.
pub mod identity;
/// Template-based instruction injection with session state interpolation.
pub mod instruction_template;
/// Auto-compaction during a run: its threshold, the token estimate, and the agents' hook.
pub mod intra_compaction;
/// Leases with fencing epochs: one owner of a named resource at a time.
pub mod lease;
/// Metering for every model call made on behalf of a run.
pub mod metering;
/// LLM trait, request/response types, and caching configuration.
pub mod model;
/// The process's one ONNX Runtime environment, shared by every crate that runs ONNX models.
#[cfg(feature = "ort")]
pub mod onnx;
/// Tool execution policy: allow, deny, or require approval per call.
pub mod policy;
/// Agent identity: agent identifiers, principals, and acting contexts.
pub mod principal;
pub mod queue_source;
/// HTTP request context extracted by auth middleware.
pub mod request_context;
/// Model-evaluated tool approval: a reviewer that decides held calls before a person.
pub mod review;
/// Durable runs: a queue of runs that workers claim under fenced leases.
pub mod run_store;
/// Schedules that enqueue durable runs, and the firing rule.
pub mod schedule;
/// Provider-aware JSON Schema normalization for tool declarations.
pub mod schema_adapter;
/// Thread-safe schema cache for tool parameter schemas.
pub mod schema_cache;
/// JSON Schema utility functions.
pub mod schema_utils;
/// Thread-safe shared state for parallel agent coordination.
pub mod shared_state;
/// One spend ledger for model calls and payments: reservations, limits, and committed spend.
pub mod spend;
/// User messages that reach a run while it runs: the run's inbox, the receipt and the
/// event marker.
pub mod steering;
/// Background sub-agents: the host contract, their status, the budget slice and tool
/// ceiling they run under, and the notice a parent receives.
pub mod subagent;
pub mod subject;
/// Tool trait, toolset, execution strategy, and registry.
pub mod tool;
/// Semaphore-based tool concurrency management.
pub mod tool_concurrency;
/// Function responses for tool calls that fail, are withheld, or carry undecodable arguments.
pub mod tool_response;
/// Content, Part, and multimodal data types.
pub mod types;
/// Vendor-neutral usage units and per-unit prices.
pub mod usage;
#[cfg(feature = "webhooks")]
pub mod webhook;
#[cfg(feature = "webhook-delivery")]
pub mod webhook_delivery;

pub use acting::{
    ActingConfig, PRINCIPAL_NOT_ALLOWED_CODE, SUBJECT_MISMATCH_CODE, StaticSubjectDelegation,
    SubjectDelegationPolicy, SubjectDelegationRequest, subject_mismatch_error,
};
pub use action_ledger::{
    ActionLedger, ActionOutcome, ActionRecord, InMemoryActionLedger, LedgerAnswer, LedgeredCall,
    NOT_RUN_PREFIX, OUTCOME_UNKNOWN_STATUS, begin_ledgered_call, held_call_ledger_key,
    interrupted_call_response, is_not_run, is_outcome_unknown, json_digest, not_run_text,
    outcome_unknown_response, record_outcome_unknown,
};
pub use agent::{
    Agent, AgentCapabilities, AgentInteractionMode, AgentKind, AgentProfile, AgentRelationshipKind,
    AgentTopology, AgentTopologyMember, AgentTopologyRelationship, AgentTransferDecision,
    AgentTransferRequest, EventStream, ResolvedContext, ToolProfile, screen_user_input_with,
};
pub use agent_invoker::AgentInvoker;
pub use agent_loader::{AgentLoader, MultiAgentLoader, SingleAgentLoader};
pub use approval::{
    APPROVAL_REAPPLY_GRACE, ApprovalAction, ApprovalCall, ApprovalCursor, ApprovalDecision,
    ApprovalDelegation, ApprovalEventKind, ApprovalNotification, ApprovalNotifier, ApprovalPolicy,
    ApprovalRequest, ApprovalRequestQuery, ApprovalResolution, ApprovalRisk, ApprovalRoute,
    ApprovalRouter, ApprovalService, ApprovalState, ApprovalTick, ApproverDirectory,
    ApproverSelector, DEFAULT_APPROVAL_PAGE, DelegationScope, DelegationWindow, EscalationStep,
    ExpiryAction, InboxPage, InboxQuery, StoreApprovalService,
};
pub use budget::{
    BUDGET_LIMIT_KEY, BUDGET_RECORDED_KEY, BudgetExceeded, BudgetLimit, BudgetTracker, BudgetUsage,
    ModelCallMeter, RunBudget, budget_exceeded_event, generate_with_budget, meter_stream,
};
pub use callbacks::{
    AfterAgentCallback, AfterModelCallback, AfterToolCallback, AfterToolCallbackFull,
    BaseEventsSummarizer, BeforeAgentCallback, BeforeModelCallback, BeforeModelResult,
    BeforeToolCallback, CALLBACK_PANICKED_CODE, EventsCompactionConfig, GlobalInstructionProvider,
    HistoryProcessor, InstructionProvider, InvocationHooks, OnToolErrorCallback, guard_callback,
};
pub use clock::{Clock, ManualClock, SystemClock};
pub use computer::{
    ActuationOutput, ComputerAction, ComputerActionKind, ComputerActuator, MouseButton,
    ScreenPoint, Screenshot, ScrollDirection,
};
pub use context::{
    Artifacts, BUILTIN_TOOL_ORIGIN, BackpressurePolicy, BatchCall, CallbackContext,
    IncludeContents, InvocationContext, MAX_STATE_KEY_LEN, Memory, MemoryEntry, ReadonlyContext,
    ReadonlyState, RunConfig, RunConfigBuilder, RuntimeToolset, STATIC_TOOL_ORIGIN, SecretRequest,
    SecretService, Session, State, StreamingMode, ToolCallbackContext, ToolConcurrencyConfig,
    ToolConfirmationDecision, ToolConfirmationHandler, ToolConfirmationPolicy,
    ToolConfirmationRequest, ToolOrigin, ToolOutcome, tool_call_fingerprint, validate_state_key,
};
pub use credential::{
    AgentCredential, CredentialBroker, CredentialKind, CredentialSecret, IdentitySource,
    RevocationScope, ScopedToken, StaticIdentitySource, resource_indicator,
};
pub use decision::{Choice, ChoiceOption, Decider, Decision, decide};
pub use error::{
    AdkError, ErrorCategory, ErrorComponent, ErrorDetails, ErrorDiagnostics, Result, RetryHint,
    UPSTREAM_EXCERPT_BYTES, json_error_summary, upstream_excerpt,
};
pub use event::{
    Event, EventActions, EventCompaction, KEY_PREFIX_APP, KEY_PREFIX_TEMP, KEY_PREFIX_USER,
    TOOL_PROGRESS_CALL_ID_KEY, TOOL_PROGRESS_STREAM_KEY, ToolCallView, ToolResultView,
    WorkflowPosition, event_belongs_to_branch,
};
pub use event_stream::EventTextDeltas;
pub use event_summarizer::LlmEventSummarizer;
pub use evidence::{ArtifactLocator, Evidence, ImageEvidence};
pub use governance::{
    ApprovalScope, ApprovalStore, BUILTIN_TOOLS_WITHHELD_KEY, BuiltinToolGovernor,
    BuiltinToolWithholding, CONFIRMATION_PENDING_STATUS, DEFAULT_TOOL_CONFIRMATION_TIMEOUT,
    DRAW_TOOL_NAMES, GOVERNANCE_FROZEN_CODE, GOVERNANCE_STALE_CODE, GovernanceControl,
    GovernanceMirrorConfig, GovernanceWatch, GovernedCall, HeldToolCall, InMemoryApprovalStore,
    PendingApproval, REQUESTED_CALL_ID_PREFIX, REQUESTED_CALL_METADATA_KEY, RequestedCall,
    RequestedCallSeal, RequestedVia, RequestedViaKind, SealedCall, ToolApproval, ToolAuthorization,
    ToolCallScreen, ToolGate, WithheldBuiltinTool, authorize_tool_call, builtin_tool_configuration,
    confirm_tool_call, confirmation_decision_available, confirmation_pending_response,
    held_call_compaction_limit, held_tool_calls, is_confirmation_pending,
    supersede_pending_responses, untrusted_block, withdraw_held_call, workflow_resume_positions,
};
pub use governance_state::{
    GovernanceScope, GovernanceState, GovernanceStateStore, InMemoryGovernanceStateStore,
};
pub use grounding::{GroundingSource, bind_markdown_citations, record_grounding_sources};
pub use identity::{
    AdkIdentity, AppName, ExecutionIdentity, IdentityError, InvocationId, SessionId, UserId,
};
pub use instruction_template::inject_session_state;
pub use intra_compaction::{
    CompactedHistory, CompactionTrigger, HistoryCompactionRequest, HistoryCompactor,
    IntraCompactionConfig, NativeCompactionRequest,
};
pub use lease::{InMemoryLeaseStore, Lease, LeaseStore};
pub use metering::{
    LlmSpendEstimate, MeteredLlm, MeteredUsage, ModelCallScope, ModelSpendHold,
    UNBOUNDED_HOLD_MICRO_USD, UsageCall, UsageHold, generate_metered, meter_usage,
    vendor_for_model,
};
pub use model::{
    BuiltinToolUse, CacheCapable, CitationMetadata, CitationSource, ContextCacheConfig, Effort,
    FinishReason, GenerateContentConfig, Llm, LlmRequest, LlmResponse, LlmResponseStream,
    ToolDeclarations, ToolDeclarationsIter, ToolDeclarationsKeys, ToolDeclarationsValues,
    UsageMetadata, nullable_count,
};
pub use model::{
    DataDestination, DataLocality, NativeCompaction, NativeWebSearch, NativeWebSearchTool,
};
pub use policy::{
    ArgPredicate, DeclarativePolicy, DeclarativePolicyBuilder, PolicyCall, PolicyDecision,
    PolicyRule, StrictestPolicy, ToolPolicy, ToolPolicyRequest,
};
pub use principal::{
    ActingContext, AgentId, AgentPrincipal, PrincipalError, SubjectKind, SubjectRef,
};
pub use queue_source::{
    InMemoryQueueSource, IngressReport, QueueIngress, QueueMessage, QueueSource,
};
pub use request_context::RequestContext;
pub use review::{
    DEFAULT_REVIEW_TIMEOUT, MODEL_REVIEWER_PREFIX, ModelApprover, REVIEW_SOURCE, ReviewScope,
    ReviewVerdict, ToolReview, ToolReviewer,
};
pub use run_store::{
    ClaimedRun, DurableRunStore, Enqueued, InMemoryRunStore, NewRun, OnConflict, RetryBackoff,
    RunCheckpointRef, RunFilter, RunOutcome, RunRecord, RunState,
};
pub use schedule::{
    FirePlan, InMemoryScheduleStore, Jitter, MissedFirePolicy, Schedule, ScheduleSpec,
    ScheduleStore, firing_idempotency_key, manual_idempotency_key, plan_fires,
};
pub use schema_adapter::{GenericSchemaAdapter, SchemaAdapter};
pub use schema_cache::SchemaCache;
pub use shared_state::{SharedState, SharedStateError};
pub use spend::{
    DEFAULT_RESERVATION_TTL, InMemorySpendLedger, ReservationId, SPEND_LIMIT_EXCEEDED_CODE,
    SpendError, SpendKey, SpendLedger, SpendLimit, SpendLimits, SpendPeriod, usd_to_micro_usd,
};
pub use steering::{
    STEER_INVALID_MESSAGE_CODE, STEER_NO_ACTIVE_RUN_CODE, SteerOutcome, SteerReceipt,
    SteeringInbox, SteeringMarker, SteeringMessage, validate_steering_message,
};
pub use subagent::{
    FollowUp, RunLimits, SubagentHost, SubagentLaunch, SubagentMode, SubagentParent,
    SubagentRecord, SubagentRequest, SubagentScope, SubagentStart, SubagentStatus, SubagentTarget,
    ToolCeiling,
};
pub use tool::{
    BatchGroup, DUPLICATE_TOOL_NAME_CODE, RetryBudget, Tool, ToolContext, ToolEffect,
    ToolExecutionStrategy, ToolPredicate, ToolRegistry, Toolset, ValidationMode,
    ensure_unique_tool_names,
};
pub use tool_concurrency::{ConcurrencyPermit, ToolConcurrencyManager};
pub use tool_response::{
    INVALID_TOOL_ARGUMENTS_KEY, RESULT_WITHHELD_STATUS, TOOL_ERROR_MESSAGE_LIMIT,
    TOOL_INVALID_ARGUMENTS_CODE, TOOL_PERMISSION_DENIED_CODE, TOOL_RESULT_ERROR_CATEGORY_KEY,
    TOOL_RESULT_ERROR_CODE_KEY, TOOL_RESULT_STATUS_KEY, ToolResultScreen, ToolResultStatus,
    invalid_tool_arguments, invalid_tool_arguments_reason, result_blocked_response,
    result_withheld_response, screen_tool_result, tool_error_response,
};
pub use types::{
    BlobResourceContents, Content, EmbeddedResource, FileDataPart, FunctionResponseData,
    GENERATED_CALL_ID_PREFIX, InlineDataPart, MAX_INLINE_DATA_SIZE, Part, TextResourceContents,
};
pub use url::Url;
pub use usage::{UnitRate, UsageUnit, UsageVector};

// Re-export async_trait so the #[tool] macro's generated code can reference it
// via adk_tool::async_trait (adk_tool re-exports from adk_core).
pub use async_trait::async_trait;

/// Enforces the explicit cryptographic provider process-wide.
/// This bypasses any inert 'ring' code lingering in deep dependencies.
///
/// Because we lack a monolithic main() function, we use lazy evaluation
/// to guarantee that aws-lc-rs is installed globally the millisecond
/// an adk-rust consumer attempts to use the network.
pub fn ensure_crypto_provider() {
    #[cfg(feature = "rustls")]
    {
        static CRYPTO_INIT: std::sync::Once = std::sync::Once::new();
        CRYPTO_INIT.call_once(|| {
            // We ignore the Result. If the parent application has already
            // deliberately installed a provider, we respect their sovereign choice.
            let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
        });
    }
}