Skip to main content

forge_guardrails/
lib.rs

1//! Guarded LLM-agent workflows with step and tool enforcement.
2
3#![warn(missing_docs)]
4
5/// Feature-gated Hugging Face classifier artifact download helpers.
6#[cfg(feature = "classifier")]
7pub mod classifier_download;
8/// LLM backend client adapters and traits.
9pub mod clients;
10/// Context budget tracking and token compaction strategies.
11pub mod context;
12/// Core execution core components including message types and tool specifications.
13pub mod core;
14/// Custom error types for the framework.
15pub mod error;
16/// Response validation and step enforcement guardrails.
17pub mod guardrails;
18/// System and rescue prompt templates.
19pub mod prompts;
20/// HTTP and OpenAI-compatible proxy interface.
21pub mod proxy;
22/// Opt-in tool schema description compression for proxy-intercepted requests.
23pub mod schema_compression;
24/// Default-on secret redaction for proxy-bound request inputs.
25pub mod secret_redaction;
26/// In-process server backend manager.
27pub mod server;
28/// Tool-output compression for proxy-forwarded tool result messages.
29pub mod tool_output;
30/// Opt-in tool-call policy nudges for proxy-intercepted tool calls.
31pub mod tool_policy;
32/// Built-in tools like `respond`.
33pub mod tools;
34
35// Legacy module-level re-exports for backwards compatibility and integration tests
36pub use clients::base as client;
37pub use clients::base as streaming;
38pub use clients::sampling;
39pub use context::hardware;
40pub use context::strategies as compact;
41pub use core::steps;
42pub use core::workflow;
43pub use prompts::nudges;
44pub use tools::respond;
45
46#[cfg(feature = "classifier")]
47pub use classifier_download::{
48    classifier_artifact_needs_download, default_cached_classifier_artifact_dir,
49    default_classifier_cache_root, default_tool_call_classifier_artifact_dir,
50    download_classifier_artifact_tree, ensure_classifier_artifact_dir, required_files_for_model,
51    runtime_required_files, validate_classifier_artifact_dir, ClassifierArtifactKind,
52    ClassifierDownloadPlan, ClassifierDownloadReport,
53};
54pub use clients::{
55    apply_sampling_defaults, format_tool, get_sampling_defaults, AnthropicClient,
56    AnyLlmProxyClient, AnyLlmRuntimeClient, ApiFormat, ChunkStream, ChunkType, LLMCallInfo,
57    LLMClient, LLMRateLimitInfo, LLMRequestOptions, LLMResponse, LLMResponseEnvelope,
58    LLMUsageDetails, LlamafileClient, OllamaClient, SamplingParams, StreamChunk, TextResponse,
59    TokenUsage, ToolCall, MODEL_SAMPLING_DEFAULTS,
60};
61pub use context::{
62    default_context_warning, detect_hardware, CompactEvent, CompactStrategy, ContextManager,
63    HardwareProfile, MemoryKind, NoCompact, SlidingWindowCompact, TieredCompact,
64};
65pub use core::{
66    fold_and_serialize, format_tool_call_id, run_inference, FinalResponseScoreFn, InferenceResult,
67    Message, MessageMeta, MessageRole, MessageType, OnChunkFn, OnMessageFn, ParamModel,
68    PrerequisiteCheck, SlotWorker, StepTracker, ToolCallInfo, ToolCallScoreFn, ToolDef, ToolSpec,
69    Workflow, WorkflowRunner,
70};
71pub use error::{
72    BackendError, BudgetResolutionError, ContextBudgetExceeded, ContextDiscoveryError, ForgeError,
73    HardwareDetectionError, MaxIterationsError, PrerequisiteError, StepEnforcementError,
74    StreamError, ThinkingNotSupportedError, ToolCallError, ToolExecutionError, ToolResolutionError,
75    UnsupportedModelError, WorkflowCancelledError,
76};
77pub use guardrails::{
78    final_response_top_k_from_logits, recent_errors_from_messages, score_final_response_async,
79    score_tool_call_async, serialize_final_response_state_v1, serialize_state_v1,
80    serialize_state_v2, serialize_state_v3, tool_call_top_k_from_logits, validate_tool_arguments,
81    validate_tool_call_batch, ArgValidationError, ArgValidationKind, ArtifactManifest,
82    CandidateCallForScoring, CheckResult, ClassifierAction, ClassifierArtifact,
83    ClassifierModelKind, ClassifierTopKEntry, ErrorTracker, FinalResponseClass,
84    FinalResponseClassifierArtifact, FinalResponseContext, FinalResponseScore, FinalResponseScorer,
85    FinalResponseToolResult, GuardAction, GuardrailDecision, GuardrailHistory, GuardrailState,
86    GuardrailViolation, Guardrails, LabelThreshold, LabelsFile, NoopFinalResponseScorer,
87    NoopToolCallScorer, Nudge, RetryNudgeFn, ScorerMode, ScoringContext, ScoringExecutor,
88    ScoringMetadata, ScoringPipeline, StepCheck, StepEnforcer, StepPrerequisite, TerminalTool,
89    Thresholds, ToolCallClass, ToolCallScore, ToolCallScorer, ToolSpecForScoring, ValidationResult,
90    WorkflowStateForScoring, DEFAULT_CLASSIFIER_REPO, DEFAULT_CLASSIFIER_REVISION,
91    DEFAULT_FINAL_RESPONSE_CLASSIFIER_REPO, DEFAULT_FINAL_RESPONSE_CLASSIFIER_REVISION,
92    EXPECTED_LABELS, FINAL_RESPONSE_ARTIFACT_SCHEMA_VERSION, FINAL_RESPONSE_EXPECTED_LABELS,
93    FINAL_RESPONSE_INPUT_SCHEMA_VERSION, FINAL_RESPONSE_SERIALIZER,
94    FINAL_RESPONSE_THRESHOLDS_SCHEMA_VERSION, LEGACY_EXPECTED_LABELS, NEXT_INPUT_SCHEMA_VERSION,
95    NEXT_SERIALIZER, V3_SERIALIZER,
96};
97#[cfg(feature = "classifier")]
98pub use guardrails::{
99    OnnxFinalResponseScorer, OnnxScorerOptions, OnnxToolCallScorer, MAX_ONNX_SESSION_POOL_SIZE,
100};
101pub use prompts::{
102    build_tool_prompt, classifier_nudge, extract_tool_call, prerequisite_nudge, rescue_tool_call,
103    retry_nudge, step_nudge, unknown_tool_nudge, unsafe_batch_nudge,
104};
105pub use proxy::{
106    extract_passthrough, extract_sampling, handle_anthropic_messages,
107    handle_anthropic_messages_with_scorer, handle_anthropic_messages_with_scorers,
108    handle_anthropic_messages_with_scorers_and_tool_controls,
109    handle_anthropic_messages_with_scorers_and_tool_output_compression,
110    handle_anthropic_messages_with_scorers_tool_controls_and_headers, handle_chat_completions,
111    handle_chat_completions_with_scorer, handle_chat_completions_with_scorers,
112    handle_chat_completions_with_scorers_and_tool_controls,
113    handle_chat_completions_with_scorers_and_tool_output_compression, has_respond_tool,
114    init_proxy_classifier_log_sink_from_env, init_proxy_tool_output_compression_log_sink_from_env,
115    init_proxy_training_capture_sink_from_env, openai_to_messages, respond_tool_openai,
116    shutdown_proxy_classifier_log_sink, shutdown_proxy_tool_output_compression_log_sink,
117    shutdown_proxy_training_capture_sink, strip_respond_calls, text_response_to_openai,
118    text_to_sse_events, tool_calls_to_openai, tool_calls_to_sse_events, AnthropicEventStream,
119    AnthropicHandlerError, AnthropicHandlerResult, HTTPServer, HandlerError, HandlerResult,
120    OpenAiEventStream, OpenAiMessageError,
121};
122pub use schema_compression::{
123    compress_tool_schemas, patch_anthropic_tool_schemas, SchemaCompressionMode,
124    SchemaCompressionStats,
125};
126pub use secret_redaction::{
127    redact_proxy_request_inputs, SecretRedactionError, SecretRedactionSummary,
128    SECRET_REDACTION_MARKER,
129};
130pub use server::{setup_backend, BudgetMode, ServerManager};
131pub use tool_output::{
132    canonical_tool_name, compress_tool_output, detect_family, estimate_tokens,
133    ToolOutputCompressionConfig, ToolOutputCompressionMethod, ToolOutputCompressionMode,
134    ToolOutputCompressionResult, ToolOutputCompressionState, DEFAULT_MAX_DEDUP_ENTRIES_PER_SESSION,
135    DEFAULT_MAX_DEDUP_SESSIONS, DEFAULT_MAX_OUTPUT_BYTES,
136};
137pub use tool_policy::{
138    evaluate_tool_call_policy, ToolCallPolicyConfig, ToolCallPolicyMode, ToolCallPolicyNudge,
139    ToolCallPolicyRequestState, DEFAULT_MAX_WRITE_PAYLOAD_BYTES,
140};
141pub use tools::{respond_spec, respond_tool, RESPOND_TOOL_NAME};