Skip to main content

slop_ai/
lib.rs

1//! # slop-ai
2//!
3//! Rust SDK for the [SLOP protocol](https://slopai.dev) — let AI observe and
4//! interact with your app's state.
5//!
6//! ## Core types
7//!
8//! - [`SlopServer`] — publish state and register affordances (actions an AI can invoke)
9//! - [`SlopConsumer`] — subscribe to a provider and invoke affordances *(requires `native` feature)*
10//! - [`StateMirror`] — apply JSON patches and maintain a local replica of remote state
11//! - [`SlopNode`], [`Affordance`], [`PatchOp`] — protocol data types
12//!
13//! ## Transports
14//!
15//! Each transport is behind a feature flag:
16//!
17//! | Feature | Transport | Use case |
18//! |---------|-----------|----------|
19//! | `websocket` | [`transport::websocket`], [`WsClientTransport`] | Browser-compatible, cross-network |
20//! | `unix` | [`transport::unix`], [`UnixClientTransport`] | Fast local IPC |
21//! | `stdio` | [`transport::stdio`] | CLI tools, child processes |
22//! | `axum` | [`transport::axum`] | Embed in an Axum HTTP server |
23//!
24//! The `native` feature (on by default) enables `websocket` + `unix` + `stdio`.
25//!
26//! ## LLM integration
27//!
28//! - [`affordances_to_tools`] — convert affordances into OpenAI-compatible tool definitions
29//! - [`format_tree`] — render state as Markdown for LLM context injection
30//! - [`prepare_tree`] / [`auto_compact`] — scale trees to fit token budgets
31//!
32//! ## Quick start
33//!
34//! ```
35//! use slop_ai::{SlopServer, ActionOptions};
36//! use serde_json::json;
37//!
38//! let slop = SlopServer::new("my-app", "My App");
39//!
40//! slop.register("todos", json!({
41//!     "type": "collection",
42//!     "props": {"count": 0},
43//! }));
44//!
45//! slop.action_with("todos", "add", |params| {
46//!     let text = params["text"].as_str().unwrap_or("untitled");
47//!     Ok(Some(json!({ "added": text })))
48//! }, ActionOptions::new().label("Add todo"));
49//!
50//! assert_eq!(slop.version(), 2);
51//! ```
52//!
53//! ## Documentation
54//!
55//! - Project docs: <https://docs.slopai.dev/api/rust>
56//! - Integration guide: <https://docs.slopai.dev/guides/rust>
57//! - crates.io docs: <https://docs.rs/slop-ai>
58
59#![cfg_attr(docsrs, feature(doc_cfg))]
60
61pub mod descriptor;
62pub mod diff;
63pub mod error;
64pub mod scaling;
65pub mod server;
66pub mod state_mirror;
67pub mod tools;
68pub mod transport;
69pub mod tree;
70pub mod types;
71pub mod validate_params;
72
73#[cfg(feature = "native")]
74pub mod consumer;
75
76#[cfg(feature = "native")]
77pub mod discovery;
78
79// Re-export main types at crate root
80pub use error::{Result, SlopError};
81pub use scaling::{
82    auto_compact, count_nodes, filter_tree, get_subtree, prepare_tree, truncate_tree,
83    OutputTreeOptions,
84};
85pub use server::{ActionOptions, Connection, ScopedServer, SlopServer};
86pub use state_mirror::StateMirror;
87pub use tools::{affordances_to_tools, format_tree, LlmFunction, LlmTool, ToolResolution, ToolSet};
88pub use types::{
89    Affordance, ContentRef, ContentType, Estimate, NodeMeta, PatchOp, PatchOpKind, SlopNode,
90    Urgency,
91};
92
93#[cfg(feature = "native")]
94pub use consumer::{ClientTransport, SlopConsumer};
95
96#[cfg(feature = "websocket")]
97pub use transport::ws_client::WsClientTransport;
98
99#[cfg(feature = "websocket")]
100pub use transport::ws_accepted::AcceptedWsTransport;
101
102#[cfg(feature = "unix")]
103pub use transport::unix_client::UnixClientTransport;