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// CLI subprocess client (claude -p / gemini -p / codex exec)
47pub mod cli_client;
48pub use cli_client::{CliBackend, CliClient};
49
50// Llm facade — routes to oxide (primary) or genai (Vertex AI fallback) or cli
51pub mod llm;
52pub use llm::Llm;
53
54// Agent framework (behind feature gate)
55#[cfg(feature = "agent")]
56pub mod agent;
57#[cfg(feature = "agent")]
58pub mod agent_loop;
59#[cfg(feature = "agent")]
60pub mod agent_tool;
61#[cfg(feature = "agent")]
62pub mod agents;
63#[cfg(feature = "agent")]
64pub mod client;
65#[cfg(feature = "agent")]
66pub mod compaction;
67#[cfg(feature = "agent")]
68pub mod context;
69#[cfg(feature = "agent")]
70pub mod discovery;
71#[cfg(feature = "agent")]
72pub mod factory;
73#[cfg(feature = "agent")]
74pub mod prompt_loader;
75#[cfg(feature = "agent")]
76pub mod registry;
77#[cfg(feature = "agent")]
78pub mod retry;
79#[cfg(feature = "agent")]
80pub mod router;
81#[cfg(feature = "agent")]
82pub mod schema_simplifier;
83#[cfg(feature = "agent")]
84pub mod streaming;
85#[cfg(feature = "agent")]
86pub mod swarm;
87#[cfg(feature = "agent")]
88pub mod swarm_tools;
89#[cfg(feature = "agent")]
90pub mod union_schema;
91
92// Session / app modules (from baml-agent migration)
93#[cfg(feature = "session")]
94pub mod app_config;
95#[cfg(feature = "session")]
96pub mod app_loop;
97#[cfg(feature = "session")]
98pub mod doctor;
99#[cfg(feature = "session")]
100pub mod hints;
101pub mod hooks;
102#[cfg(feature = "session")]
103pub mod intent_guard;
104#[cfg(feature = "session")]
105pub mod loop_detect;
106#[cfg(feature = "session")]
107pub mod memory;
108#[cfg(feature = "session")]
109pub mod prompt_template;
110#[cfg(feature = "session")]
111pub mod session;
112#[cfg(feature = "session")]
113pub mod tasks;
114
115#[cfg(feature = "app-tools")]
116pub mod app_tools;
117
118// Universal file-system tools (behind feature gate)
119#[cfg(feature = "tools")]
120pub use sgr_agent_tools as tools;
121
122pub mod benchmark;
123pub mod evolution;
124pub mod openapi;
125pub mod skills;
126
127#[cfg(feature = "providers")]
128pub mod providers;
129
130#[cfg(feature = "telemetry")]
131pub mod telemetry;
132
133#[cfg(feature = "logging")]
134pub mod logging;
135
136// Re-exports from session modules
137#[cfg(feature = "session")]
138pub use app_config::{AgentConfig, AgentConfigError};
139#[cfg(feature = "session")]
140pub use app_loop::{
141    ActionResult, LoopConfig, LoopEvent, SgrAgent, SgrAgentStream, StepDecision, process_step,
142    run_loop, run_loop_stream,
143};
144#[cfg(feature = "session")]
145pub use doctor::{
146    CheckResult, CheckStatus, DoctorCheck, check_gcloud_adc, check_provider_auth,
147    default_tool_checks, fix_missing, format_check, optional_tool_checks, print_doctor_report,
148    run_doctor, run_tool_check,
149};
150#[cfg(feature = "session")]
151pub use hints::{
152    HintContext, HintSource, PatternHints, TaskHints, ToolHints, WorkflowHints, collect_hints,
153    default_sources, default_sources_with_tasks,
154};
155#[cfg(feature = "session")]
156pub use intent_guard::{ActionKind, Intent, IntentCheck, guard_step, intent_allows};
157#[cfg(feature = "logging")]
158pub use logging::init_logging;
159#[cfg(feature = "session")]
160pub use loop_detect::{LoopDetector, LoopStatus, normalize_signature};
161#[cfg(feature = "session")]
162pub use memory::{
163    MemoryContext, action_result_done, action_result_from, action_result_json, load_context_dir,
164    load_manifesto, load_manifesto_from, norm, norm_owned, truncate_json_array,
165};
166#[cfg(feature = "session")]
167pub use prompt_template::{BASE_SYSTEM_PROMPT, build_system_prompt};
168#[cfg(all(feature = "search", feature = "session"))]
169pub use session::search_sessions;
170#[cfg(feature = "session")]
171pub use session::{
172    AgentMessage, EntryType, MessageRole, Session, SessionHeader, SessionMeta, SimpleMsg,
173    SimpleRole, StatefulDelta, StatefulSession, import_claude_session, list_sessions,
174};
175#[cfg(feature = "session")]
176pub use tasks::{
177    Priority, Task, TaskStatus, append_notes, create_task, load_tasks, save_task, tasks_context,
178    tasks_dir, tasks_summary, update_status,
179};
180#[cfg(feature = "telemetry")]
181pub use telemetry::{LlmUsage, TelemetryGuard, annotate_session, init_telemetry, record_llm_span, set_session_id};
182
183pub use coerce::coerce_value;
184pub use flexible_parser::{parse_flexible, parse_flexible_coerced};
185pub use schema::{json_schema_for, response_schema_for};
186#[cfg(feature = "agent")]
187pub use skills::{GetSkillTool, ListSkillsTool};
188pub use skills::{Skill, SkillRegistry, load_skills_from_dir, parse_skill};
189pub use tool::{ToolDef, tool};
190pub use types::*;