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