modelrelay/
lib.rs

1//! Minimal Rust SDK for the ModelRelay API.
2#![cfg_attr(docsrs, feature(doc_cfg))]
3// Allow large error types - refactoring to Box<Error> would be a breaking change
4#![allow(clippy::result_large_err)]
5
6/// Generated types from OpenAPI spec.
7/// Run `just generate-sdk-types` to regenerate from api/openapi/api.yaml.
8pub mod generated;
9
10/// Default API base URL.
11pub const DEFAULT_BASE_URL: &str = "https://api.modelrelay.ai/api/v1";
12
13/// Default User-Agent header value.
14pub(crate) const DEFAULT_CLIENT_HEADER: &str =
15    concat!("modelrelay-rust/", env!("CARGO_PKG_VERSION"));
16
17/// Default connection timeout (5 seconds).
18pub const DEFAULT_CONNECT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5);
19
20/// Default request timeout (60 seconds).
21pub const DEFAULT_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);
22
23/// HTTP header name for request ID tracing.
24pub const REQUEST_ID_HEADER: &str = "X-ModelRelay-Request-Id";
25
26/// HTTP header name for API key authentication.
27pub(crate) const API_KEY_HEADER: &str = "X-ModelRelay-Api-Key";
28
29mod api_key;
30mod client;
31mod core;
32mod customers;
33mod errors;
34mod http;
35mod identifiers;
36#[cfg(feature = "mock")]
37mod mock;
38mod models;
39mod responses;
40mod runs;
41mod structured;
42mod telemetry;
43mod tiers;
44mod token_providers;
45pub mod tools;
46mod types;
47mod workflow;
48mod workflow_builder;
49mod workflows;
50
51// Re-export common types used in public API for user convenience
52pub use chrono::{DateTime, Utc};
53pub use uuid::Uuid;
54
55pub use api_key::{ApiKey, PublishableKey, SecretKey};
56pub use errors::{
57    APIError, Error, FieldError, RetryMetadata, TransportError, TransportErrorKind,
58    ValidationError, WorkflowValidationError, WorkflowValidationIssue,
59};
60pub use http::{HeaderEntry, HeaderList, ResponseOptions, RetryConfig};
61#[cfg(all(feature = "mock", feature = "blocking"))]
62pub use mock::MockBlockingResponsesClient;
63#[cfg(feature = "mock")]
64pub use mock::{fixtures, MockAuthClient, MockClient, MockConfig, MockResponsesClient};
65#[cfg(all(feature = "blocking", feature = "streaming"))]
66pub use responses::BlockingStructuredJSONStream;
67#[cfg(feature = "streaming")]
68pub use responses::StructuredJSONStream;
69pub use responses::{ResponseBuilder, ResponseStreamAdapter, CUSTOMER_ID_HEADER};
70pub use responses::{StructuredJSONEvent, StructuredRecordKind};
71pub use telemetry::{
72    HttpRequestMetrics, MetricsCallbacks, RequestContext, StreamFirstTokenMetrics,
73    TokenUsageMetrics,
74};
75pub use tools::{
76    assistant_message_with_tool_calls, create_retry_messages, execute_with_retry,
77    format_tool_error_for_model, get_retryable_errors, has_retryable_errors,
78    parse_and_validate_tool_args, parse_tool_args, respond_to_tool_call, respond_to_tool_call_json,
79    sync_handler, tool_result_message, tool_result_message_json, BoxFuture, ParseResult,
80    ResponseExt, RetryOptions, ToolArgsError, ToolCallAccumulator, ToolExecutionResult,
81    ToolHandler, ToolRegistry, UnknownToolError, ValidateArgs,
82};
83pub use tools::{function_tool_from_type, ToolSchema};
84pub use types::{
85    APIKey, Citation, CodeExecConfig, ContentPart, CustomerToken, CustomerTokenRequest,
86    DeviceFlowErrorKind, DeviceFlowProvider, DeviceStartRequest, DeviceTokenPending,
87    DeviceTokenResponse, DeviceTokenResult, FunctionCall, FunctionCallDelta, FunctionTool,
88    InputItem, JSONSchemaFormat, MessageRole, MessageRoleExt, Model, OutputFormat,
89    OutputFormatKind, OutputItem, Response, StopReason, StreamEvent, StreamEventKind, Tool,
90    ToolCall, ToolCallDelta, ToolChoice, ToolChoiceType, ToolType, Usage, UsageSummary,
91    WebToolConfig, XSearchConfig,
92};
93// Re-export generated DeviceStartResponse for public API (interval polling config)
94pub use generated::DeviceStartResponse;
95
96pub use client::{AuthClient, Client, ClientBuilder, Config, ResponsesClient};
97pub use customers::{
98    CheckoutSession, CheckoutSessionRequest, Customer, CustomerClaimRequest, CustomerCreateRequest,
99    CustomerMetadata, CustomerUpsertRequest, CustomersClient, SubscriptionStatus,
100    SubscriptionStatusKind,
101};
102pub use generated::{RunsPendingToolCallV0, RunsPendingToolsNodeV0, RunsPendingToolsResponse};
103pub use identifiers::TierCode;
104pub use models::{CatalogModel, ModelsClient};
105#[cfg(feature = "streaming")]
106pub use runs::RunEventStreamHandle;
107pub use runs::{RunsClient, RunsCreateResponse, RunsGetResponse};
108pub use tiers::{
109    PriceInterval, Tier, TierCheckoutRequest, TierCheckoutSession, TierModel, TiersClient,
110};
111pub use workflow::{
112    run_node_ref, ArtifactKey, EdgeV0, EnvelopeVersion, ExecutionV0, ModelId, NodeErrorV0, NodeId,
113    NodeResultV0, NodeStatusV0, NodeTypeV0, NodeV0, OutputRefV0, PayloadInfoV0, PlanHash,
114    ProviderId, RequestId, RunCostLineItemV0, RunCostSummaryV0, RunEventEnvelope, RunEventPayload,
115    RunEventTypeV0, RunEventV0, RunId, RunStatusV0, Sha256Hash, WorkflowKind, WorkflowSpecV0,
116    RUN_EVENT_V0_SCHEMA_JSON, WORKFLOW_V0_SCHEMA_JSON,
117};
118pub use workflows::{WorkflowsClient, WorkflowsCompileResponseV0, WorkflowsCompileResultV0};
119
120pub use workflow_builder::{
121    workflow_v0, LlmResponsesBindingEncodingV0, LlmResponsesBindingV0, TransformJsonInputV0,
122    TransformJsonValueV0, WorkflowBuilderV0,
123};
124
125// Structured output API
126pub use structured::{
127    output_format_from_type, AttemptRecord, DefaultRetryHandler, RetryHandler,
128    StructuredDecodeError, StructuredError, StructuredErrorKind, StructuredExhaustedError,
129    StructuredOptions, StructuredResult, ValidationIssue,
130};
131
132// Token providers for backendless auth
133pub use token_providers::{
134    poll_device_token, run_device_flow_for_id_token, start_device_authorization,
135    CustomerTokenResponse, DeviceAuthConfig, DeviceAuthorization, DevicePollConfig, DeviceToken,
136    IdTokenSource, OIDCExchangeConfig, OIDCExchangeTokenProvider, TokenProvider,
137};
138
139#[cfg(feature = "streaming")]
140mod ndjson;
141#[cfg(feature = "streaming")]
142pub use ndjson::StreamHandle;
143
144#[cfg(feature = "blocking")]
145mod blocking;
146#[cfg(all(feature = "blocking", feature = "streaming"))]
147pub use blocking::BlockingRunEventStreamHandle;
148#[cfg(all(feature = "blocking", feature = "streaming"))]
149pub use blocking::BlockingStreamHandle;
150#[cfg(feature = "blocking")]
151pub use blocking::{
152    BlockingAuthClient, BlockingClient, BlockingConfig, BlockingCustomersClient,
153    BlockingResponsesClient, BlockingRunsClient, BlockingTiersClient,
154};