Skip to main content

sgr_agent/
lib.rs

1//! # sgr-agent — LLM client + agent framework
2//!
3//! Pure Rust. No dlopen, no external binaries.
4//! Works on iOS, Android, WASM — anywhere reqwest+rustls compiles.
5//!
6//! ## LLM Client (default)
7//! - **Structured output** — response conforms to JSON Schema (SGR envelope)
8//! - **Function calling** — tools as typed structs, model picks & fills params
9//! - **Flexible parser** — extract JSON from markdown, broken JSON, streaming chunks
10//! - **Backends**: Gemini (Google AI + Vertex AI), OpenAI (+ OpenRouter, Ollama)
11//!
12//! ## Agent Framework (`feature = "agent"`)
13//! - **Tool trait** — define tools with typed args + async execute
14//! - **ToolRegistry** — ordered collection, case-insensitive lookup, fuzzy resolve
15//! - **Agent trait** — decides what tools to call given conversation history
16//! - **3 agent variants**: SgrAgent (structured output), ToolCallingAgent (native FC), FlexibleAgent (text parse)
17//! - **Agent loop** — decide → execute → feed back, with 3-tier loop detection
18//! - **Progressive discovery** — filter tools by relevance (TF-IDF scoring)
19
20pub mod baml_parser;
21pub mod codegen;
22pub mod coerce;
23pub mod flexible_parser;
24pub mod model_info;
25pub mod schema;
26pub mod tool;
27pub mod types;
28
29#[cfg(feature = "gemini")]
30pub mod gemini;
31
32#[cfg(feature = "openai")]
33pub mod openai;
34
35#[cfg(feature = "genai")]
36pub(crate) mod genai_client;
37
38// Oxide is the primary LLM backend (always compiled)
39pub mod oxide_client;
40pub use oxide_client::OxideClient;
41
42// Oxide Chat — Chat Completions API for compat endpoints (Cloudflare, OpenRouter, etc.)
43pub mod oxide_chat_client;
44pub use oxide_chat_client::OxideChatClient;
45
46// Llm facade — routes to oxide (primary) or genai (Vertex AI fallback)
47pub mod llm;
48pub use llm::Llm;
49
50// Agent framework (behind feature gate)
51#[cfg(feature = "agent")]
52pub mod agent;
53#[cfg(feature = "agent")]
54pub mod agent_loop;
55#[cfg(feature = "agent")]
56pub mod agent_tool;
57#[cfg(feature = "agent")]
58pub mod agents;
59#[cfg(feature = "agent")]
60pub mod client;
61#[cfg(feature = "agent")]
62pub mod compaction;
63#[cfg(feature = "agent")]
64pub mod context;
65#[cfg(feature = "agent")]
66pub mod discovery;
67#[cfg(feature = "agent")]
68pub mod factory;
69#[cfg(feature = "agent")]
70pub mod prompt_loader;
71#[cfg(feature = "agent")]
72pub mod registry;
73#[cfg(feature = "agent")]
74pub mod retry;
75#[cfg(feature = "agent")]
76pub mod router;
77#[cfg(feature = "agent")]
78pub mod schema_simplifier;
79#[cfg(feature = "agent")]
80pub mod streaming;
81#[cfg(feature = "agent")]
82pub mod swarm;
83#[cfg(feature = "agent")]
84pub mod swarm_tools;
85#[cfg(feature = "agent")]
86pub mod union_schema;
87
88// Session / app modules (from baml-agent migration)
89#[cfg(feature = "session")]
90pub mod app_config;
91#[cfg(feature = "session")]
92pub mod app_loop;
93#[cfg(feature = "session")]
94pub mod doctor;
95#[cfg(feature = "session")]
96pub mod hints;
97#[cfg(feature = "session")]
98pub mod intent_guard;
99#[cfg(feature = "session")]
100pub mod loop_detect;
101#[cfg(feature = "session")]
102pub mod memory;
103#[cfg(feature = "session")]
104pub mod prompt_template;
105#[cfg(feature = "session")]
106pub mod session;
107#[cfg(feature = "session")]
108pub mod tasks;
109
110#[cfg(feature = "app-tools")]
111pub mod app_tools;
112
113pub mod benchmark;
114pub mod evolution;
115pub mod openapi;
116
117#[cfg(feature = "providers")]
118pub mod providers;
119
120#[cfg(feature = "telemetry")]
121pub mod telemetry;
122
123#[cfg(feature = "logging")]
124pub mod logging;
125
126// Re-exports from session modules
127#[cfg(feature = "session")]
128pub use app_config::{AgentConfig, AgentConfigError};
129#[cfg(feature = "session")]
130pub use app_loop::{
131    ActionResult, LoopConfig, LoopEvent, SgrAgent, SgrAgentStream, StepDecision, process_step,
132    run_loop, run_loop_stream,
133};
134#[cfg(feature = "session")]
135pub use doctor::{
136    CheckResult, CheckStatus, DoctorCheck, check_gcloud_adc, check_provider_auth,
137    default_tool_checks, fix_missing, format_check, optional_tool_checks, print_doctor_report,
138    run_doctor, run_tool_check,
139};
140#[cfg(feature = "session")]
141pub use hints::{
142    HintContext, HintSource, PatternHints, TaskHints, ToolHints, WorkflowHints, collect_hints,
143    default_sources, default_sources_with_tasks,
144};
145#[cfg(feature = "session")]
146pub use intent_guard::{ActionKind, Intent, IntentCheck, guard_step, intent_allows};
147#[cfg(feature = "logging")]
148pub use logging::init_logging;
149#[cfg(feature = "session")]
150pub use loop_detect::{LoopDetector, LoopStatus, normalize_signature};
151#[cfg(feature = "session")]
152pub use memory::{
153    MemoryContext, action_result_done, action_result_from, action_result_json, load_context_dir,
154    load_manifesto, load_manifesto_from, norm, norm_owned, truncate_json_array,
155};
156#[cfg(feature = "session")]
157pub use prompt_template::{BASE_SYSTEM_PROMPT, build_system_prompt};
158#[cfg(all(feature = "search", feature = "session"))]
159pub use session::search_sessions;
160#[cfg(feature = "session")]
161pub use session::{
162    AgentMessage, EntryType, MessageRole, Session, SessionHeader, SessionMeta, SimpleMsg,
163    SimpleRole, StatefulDelta, StatefulSession, import_claude_session, list_sessions,
164};
165#[cfg(feature = "session")]
166pub use tasks::{
167    Priority, Task, TaskStatus, append_notes, create_task, load_tasks, save_task, tasks_context,
168    tasks_dir, tasks_summary, update_status,
169};
170#[cfg(feature = "telemetry")]
171pub use telemetry::{TelemetryGuard, init_telemetry};
172
173pub use coerce::coerce_value;
174pub use flexible_parser::{parse_flexible, parse_flexible_coerced};
175pub use schema::{json_schema_for, response_schema_for};
176pub use tool::{ToolDef, tool};
177pub use types::*;