|
1 | 1 | --- |
2 | 2 | title: Runtime |
3 | | -description: Learn about Radkit's Runtime trait and how it provides service abstraction for agents. |
| 3 | +description: Learn about Radkit's AgentRuntime trait and how it provides service abstraction for agents. |
4 | 4 | --- |
5 | 5 |
|
6 | 6 |
|
7 | 7 |
|
8 | | -The core of Radkit's architecture is the `Runtime` trait. It acts as a centralized **service hub** or dependency injection container for your agent. Instead of passing many different service handles (like a database connector, a logger, etc.) to every function, your `SkillHandler`s receive a single, unified reference: `&dyn Runtime`. |
| 8 | +The core of Radkit's architecture is the `AgentRuntime` trait. It acts as a centralized **service hub** or dependency injection container for your agent. Instead of passing many different service handles (like a database connector, a logger, etc.) to every function, your `SkillHandler`s receive a single, unified reference: `&dyn AgentRuntime`. |
9 | 9 |
|
10 | 10 | This design keeps your agent logic clean, decoupled from infrastructure, and highly portable. |
11 | 11 |
|
12 | | -## The `Runtime` Trait |
| 12 | +## The `AgentRuntime` Trait |
13 | 13 |
|
14 | | -All runtime environments, whether for local development or cloud deployment, must implement the `Runtime` trait. This guarantees a consistent interface for your skills. |
| 14 | +All runtime environments, whether for local development or cloud deployment, must implement the `AgentRuntime` trait. This guarantees a consistent interface for your skills. |
15 | 15 |
|
16 | 16 | ```rust |
17 | | -pub trait Runtime { |
18 | | - fn task_manager(&self) -> Arc<dyn TaskManager>; |
19 | | - fn memory_service(&self) -> Arc<dyn MemoryService>; |
20 | | - fn logging_service(&self) -> Arc<dyn LoggingService>; |
21 | | - // ... other services |
| 17 | +pub trait AgentRuntime { |
| 18 | + fn auth(&self) -> Arc<dyn AuthService>; |
| 19 | + fn memory(&self) -> Arc<dyn MemoryService>; |
| 20 | + fn logging(&self) -> Arc<dyn LoggingService>; |
| 21 | + fn default_llm(&self) -> Arc<dyn BaseLlm>; |
22 | 22 | } |
23 | 23 | ``` |
24 | 24 |
|
25 | 25 | Your skill code is written against this abstraction, not a concrete implementation. A skill simply asks the runtime for a service: |
26 | 26 |
|
27 | 27 | ```rust |
28 | | -// Inside a SkillHandler method... |
29 | | -let memory_service = runtime.memory_service(); |
30 | | -memory_service.save(auth_ctx, "some_key", &data).await?; |
| 28 | +let memory = runtime.memory(); |
| 29 | +memory.save(auth_ctx, "some_key", &data).await?; |
31 | 30 | ``` |
32 | 31 |
|
33 | 32 | The skill doesn't know or care if `memory_service` is writing to an in-memory map for local testing or a massive, persistent cloud database. |
34 | 33 |
|
35 | 34 | ## Core Services |
36 | 35 |
|
37 | | -The `Runtime` provides access to a set of essential services, each defined by its own trait. |
| 36 | +`AgentRuntime` provides access to a set of essential services, each defined by its own trait. |
38 | 37 |
|
39 | | -- **`TaskManager`**: A Data Access Object (DAO) for persisting and retrieving the state of A2A `Task`s and their event histories. This is crucial for multi-turn conversations and auditing. |
| 38 | +- **`TaskManager`**: A Data Access Object (DAO) for persisting and retrieving the state of A2A `Task`s and their event histories. This is crucial for multi-turn conversations and auditing. The core runtime uses it internally so skill authors don't have to. |
40 | 39 | - **`MemoryService`**: Provides a persistent, tenant-aware key-value store. This is ideal for giving your agent long-term memory or for implementing Retrieval-Augmented Generation (RAG). |
41 | 40 | - **`LoggingService`**: A structured logging interface that streams logs to the console during local development or to a cloud observability UI in production. |
42 | 41 | - **`AuthService`**: Identifies the current user and tenant, ensuring that all other services are properly namespaced and data is kept secure. |
43 | 42 |
|
44 | 43 | ## Provided Runtimes |
45 | 44 |
|
46 | | -Radkit provides a `DefaultRuntime` for local development, and the architecture allows for custom runtimes for production or specialized environments. |
| 45 | +Radkit provides a `RuntimeBuilder` plus a concrete `Runtime` implementation for local development, and the architecture allows for custom runtimes for production or specialized environments. |
47 | 46 |
|
48 | | -### `DefaultRuntime` |
| 47 | +### `Runtime` (native implementation) |
49 | 48 |
|
50 | | -Included with the `runtime` feature flag, `DefaultRuntime` is an out-of-the-box implementation that works on both native and WASM targets. It provides simple, in-memory versions of all the core services, allowing you to get up and running instantly with no configuration. |
| 49 | +Included with the `runtime` feature flag, the builder returns an out-of-the-box runtime that works on both native and WASM targets. It provides simple, in-memory versions of all the core services, allowing you to get up and running instantly with no configuration. |
| 50 | + |
| 51 | +```rust |
| 52 | +use radkit::models::providers::OpenRouterLlm; |
| 53 | +use radkit::runtime::Runtime; |
| 54 | + |
| 55 | +# fn configure_agent() -> radkit::agent::AgentDefinition { |
| 56 | +# radkit::agent::Agent::builder() |
| 57 | +# .with_id("hr-agent") |
| 58 | +# .with_name("HR Agent") |
| 59 | +# .build() |
| 60 | +# } |
| 61 | + |
| 62 | +#[tokio::main] |
| 63 | +async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 64 | + let llm = OpenRouterLlm::from_env("anthropic/claude-3.5-sonnet")?; |
| 65 | + Runtime::builder(configure_agent(), llm) |
| 66 | + .build() |
| 67 | + .serve("127.0.0.1:8080") |
| 68 | + .await?; |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +The runtime hosts **exactly one agent definition** per process. When `.serve(..)` is called it automatically exposes the A2A HTTP surface at the root of the bound address: |
| 74 | + |
| 75 | +- `/.well-known/agent-card.json` – serves the agent card |
| 76 | +- `/rpc` – JSON-RPC entry point for `message/send`, `message/stream`, `tasks/resubscribe`, etc. |
| 77 | +- `/message:stream` – streaming API for Server-Sent Events |
| 78 | +- `/tasks/{task_id}/subscribe` – SSE resubscribe endpoint that mirrors the A2A spec |
| 79 | + |
| 80 | +If the optional `dev-ui` feature is enabled, the same server also mounts `/ui/*` routes and serves the React console for the single configured agent. |
0 commit comments