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