Skip to main content

agents_kit/
lib.rs

1//! Portable agent runtimes over Codex App Server, OpenCode, and a
2//! provider-backed Custom implementation.
3//!
4//! The hierarchy is [`Agent`] → [`Workspace`] → [`Conversation`]. Implement
5//! [`AgentBackend`], [`WorkspaceBackend`] and [`ConversationBackend`] directly to
6//! supply your own concrete associated children. The supplied handles implement
7//! those same traits.
8//!
9//! # Choose a task
10//!
11//! - Construct a supplied agent below, then create children with
12//!   [`AgentBackend::new_workspace`] and [`WorkspaceBackend::new_conversation`].
13//! - [Reopen a durable identity](AgentBuilder::reopen) with fresh integrations.
14//! - [Consume a caller-owned conversation](ConversationBackend) without a service.
15//! - [Stream native output and resolve interactions](conversation) with a configured backend.
16//! - Build passive inputs in [`config`], [`workspace`], [`prompt`], [`model`],
17//!   [`tool`] and [`knowledge`]; inspect failures through [`error`].
18//!
19//! Custom retains its direct Provider and Toolset implementations. Each toolset's
20//! Handler stays associated and need not be Clone. Prompt streams observe accepted
21//! work: dropping an observer neither cancels that work nor releases its slot.
22//! Use abort or consuming lifecycle operations and keep the runtime alive through
23//! cleanup. Native builders retain no provider value; Custom receives one through
24//! [`AgentBuilder::with_provider`]. Exact Model continuations live only within an
25//! active prompt loop; durable history contains provider-neutral text.
26//!
27//! # Construct and shut down a Custom agent
28//!
29//! This runs locally with memory Lance. Provider construction makes no model
30//! request; prompting would require the configured Ollama service and exact model.
31//! The result is inspected only after shutdown has been attempted.
32//!
33//! ```
34//! use std::{num::NonZeroUsize, time::Duration};
35//! use agents_kit::{AgentBackend, AgentBuilder, AgentStorage};
36//! use agents_kit::config::{AgentConfig, CustomAgentConfig, CustomLoopLimits, CustomToolApproval};
37//! use agents_kit::{model::ModelSelector, tool::ToolSelection};
38//! use providers_kit::{Provider, ProviderConfig};
39//! use providers_kit::config::{OllamaConfig, ProviderKind, TransportConfig};
40//! use storage_kit::{StateStore, state::config::{LanceStateStoreConfig, StateStoreConfig}};
41//! # tokio::runtime::Builder::new_current_thread().enable_all().build()?.block_on(async {
42//! let custom = CustomAgentConfig::new(
43//!     ModelSelector::new("exact-model")?, ToolSelection::None,
44//!     CustomLoopLimits::new(4, 0, 1)?, CustomToolApproval::Deny,
45//! )?;
46//! let config = AgentConfig::new(custom.into(), Duration::from_secs(10), NonZeroUsize::MIN)?;
47//! let store = StateStore::connect(StateStoreConfig::lance(LanceStateStoreConfig::memory())).await?;
48//! let provider = Provider::from_config(ProviderConfig::new(
49//!     ProviderKind::Ollama(OllamaConfig::local()), TransportConfig::default(),
50//! ))?;
51//! let agent = AgentBuilder::create(config, AgentStorage::new(store))
52//!     .with_provider(provider).build().await?;
53//! let id = agent.id();
54//! let result = agent.info().await;
55//! let cleanup = agent.shutdown().await;
56//! let info = match (result, cleanup) {
57//!     (Ok(info), Ok(())) => Ok(info),
58//!     (Err(primary), Err(cleanup)) => Err(primary.with_supplementary(cleanup)),
59//!     (Err(error), _) | (_, Err(error)) => Err(error),
60//! }?;
61//! assert_eq!(info.id(), &id);
62//! # Ok::<(), Box<dyn std::error::Error>>(())
63//! # })?;
64//! # Ok::<(), Box<dyn std::error::Error>>(())
65//! ```
66
67mod agent;
68mod agent_storage;
69mod backends;
70mod builder;
71/// Agent construction settings and backend runtime policies.
72pub mod config;
73/// Conversation handles, backend contracts, and conversation requests and responses.
74pub mod conversation;
75mod core;
76pub mod error;
77mod file;
78/// Validated identities and agent inventory information.
79pub mod identity;
80/// Knowledge sources, scopes, ingestion requests, and retrieval policies.
81pub mod knowledge;
82/// Model inventory, opaque selectors, and reasoning settings.
83pub mod model;
84/// Durable notifications, caller resolutions, and notification streams.
85pub mod notification;
86/// Prompt requests, responses, and incremental output streams.
87pub mod prompt;
88/// Portable tool selection and managed tool inventory.
89pub mod tool;
90/// Workspace handles, backend contracts, defaults, and workspace requests.
91pub mod workspace;
92
93pub use crate::agent::{Agent, AgentBackend};
94pub use crate::agent_storage::AgentStorage;
95pub use crate::builder::AgentBuilder;
96pub use crate::conversation::{Conversation, ConversationBackend};
97pub use crate::error::KitError;
98pub use crate::workspace::{Workspace, WorkspaceBackend};
99
100pub(crate) use crate::agent::AgentInfo;
101pub(crate) use crate::config::{
102    AgentConfig, BackendConfig, CodexConfig, CustomAgentConfig, CustomLoopLimits,
103    CustomToolApproval, OpenCodeConfig,
104};
105pub(crate) use crate::conversation::{ConversationInfo, FileRead, NewConversation};
106pub(crate) use crate::core::knowledge::ManagedKnowledge;
107pub(crate) use crate::identity::{AgentId, ConversationId, NotificationId, WorkspaceId};
108pub(crate) use crate::knowledge::{
109    IngestKnowledgeRequest, KnowledgeBudget, KnowledgeContext, KnowledgeOwnerId, KnowledgePolicy,
110    KnowledgeProvenance, KnowledgeRetrievalRequest, KnowledgeScope, KnowledgeSnippet,
111    KnowledgeSource,
112};
113pub(crate) use crate::model::{ModelInfo, ModelSelector, PersonalitySelector, ReasoningEffort};
114pub(crate) use crate::notification::{
115    ConversationNotification, ConversationNotificationKind, ConversationNotificationReceiver,
116    NotificationResolution, ProcessState,
117};
118pub(crate) use crate::prompt::{PromptRequest, PromptResponse, StructuredOutputRequest, UsageInfo};
119pub(crate) use crate::tool::{ToolInfo, ToolSelection};
120pub(crate) use crate::workspace::{
121    DeleteWorkspaceRequest, NewWorkspace, UpdateWorkspaceRequest, WorkspaceContext,
122    WorkspaceDefaults, WorkspaceExecutionPolicy, WorkspaceInfo, WorkspaceNetworkAccess,
123    WorkspaceRoot, WorkspaceRootAccess, WorkspaceSandboxMode, WorkspaceUpdateReadiness,
124};