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