Skip to main content

sim_lib_openai_server/
lib.rs

1#![forbid(unsafe_code)]
2#![deny(missing_docs)]
3//! OpenAI-shaped gateway routes for SIM.
4//!
5//! This crate provides the OpenAI gateway health route, OpenAI JSON transcript
6//! codec, model discovery, gateway object helpers, plan parsing and fixture atom
7//! execution through `POST /v1/responses` and `POST /v1/chat/completions`,
8//! embeddings through `POST /v1/embeddings`, SSE streaming projection,
9//! memory-backed response retrieval by id, and stored response replay/fork
10//! inspection routes. File, audio, image, and vector-store routes are SIM JSON
11//! fixture/subset surfaces: they use OpenAI-shaped paths and response objects,
12//! while request bodies use the explicit JSON fields handled by these route
13//! modules rather than multipart upload or provider media protocols. The model
14//! list route advertises models from registered `ModelCard` records, so
15//! loadable local placement sites appear beside fixture and remote runner
16//! entries when installed.
17
18/// Capability identifiers and constructors that gate gateway operations.
19pub mod capabilities;
20/// Citizen descriptor types bridging gateway objects into the SIM object graph.
21pub mod citizen;
22/// Compatibility names for the canonical server wall-clock contract.
23pub mod clock;
24/// OpenAI JSON codec surface: decode, encode, streaming, and shared shapes.
25pub mod codec_openai;
26/// Content-id helpers that normalize and hash gateway request expressions.
27pub mod content_id;
28/// Identifier generation for gateway requests, responses, and runs.
29pub mod ids;
30/// Installation glue that registers the gateway library with a runtime.
31pub mod install;
32/// Library manifest naming and export wiring for the gateway.
33pub mod manifest;
34/// Core gateway object records: requests, responses, runs, and events.
35pub mod objects;
36/// Gateway operations exposed to the runtime as callable functions.
37pub mod ops;
38mod ops_admin;
39/// Plan parsing, checking, evaluation, and combinator surface.
40pub mod plan;
41/// HTTP route handlers mapping OpenAI endpoints onto SIM eval and agents.
42pub mod routes;
43/// Runtime support: key tables, plan cache, federation, runners, and fabric.
44pub mod runtime;
45/// Server wiring that registers gateway routes and holds route state.
46pub mod server;
47/// Persistent and in-memory stores for gateway records and content objects.
48pub mod storage;
49/// Translation between OpenAI tool/function shapes and SIM symbols.
50pub mod translate;
51
52pub use capabilities::{
53    AI_RUNNER_CACHE_CAPABILITY, NETWORK_CAPABILITY, OPENAI_GATEWAY_ADMIN_CAPABILITY,
54    OPENAI_GATEWAY_FEDERATE_CAPABILITY, OPENAI_GATEWAY_PLAN_CAPABILITY,
55    OPENAI_GATEWAY_PLAN_REMOTE_CAPABILITY, OPENAI_GATEWAY_SERVE_CAPABILITY,
56    OPENAI_GATEWAY_TOOLS_CAPABILITY, WEBHOOK_SERVE_CAPABILITY, ai_runner_cache_capability,
57    network_capability, openai_gateway_admin_capability, openai_gateway_federate_capability,
58    openai_gateway_plan_capability, openai_gateway_plan_remote_capability,
59    openai_gateway_serve_capabilities, openai_gateway_serve_capability,
60    openai_gateway_tools_capability, require_openai_gateway_serve_capabilities,
61    webhook_serve_capability,
62};
63#[allow(deprecated)]
64pub use clock::{DeterministicGatewayClock, GatewayClock, SystemGatewayClock};
65pub use codec_openai::{
66    ChatTranscript, GatewayEventData, OpenAiCodec, OpenAiCodecOptions, OpenAiRequestOptions,
67    OpenAiSseSurface, StreamSink, decode_openai_request, decode_openai_response,
68    encode_gateway_events_sse, encode_openai_request, encode_openai_response,
69    encode_openai_responses_response, gateway_event_data_from_packet, gateway_event_data_kind,
70    gateway_event_data_packets, openai_codec_symbol,
71};
72pub use content_id::{
73    VOLATILE_REQUEST_FIELDS, content_id_for_expr, normalize_request_expr, request_content_id,
74    request_expr_content_id,
75};
76pub use ids::GatewayIdGenerator;
77pub use install::install_openai_gateway_lib;
78pub use manifest::{OpenAiGatewayLib, manifest_name};
79pub use objects::{
80    GATEWAY_EVENT_OBJECT, GATEWAY_REQUEST_OBJECT, GATEWAY_RESPONSE_OBJECT, GATEWAY_RUN_OBJECT,
81    GatewayEvent, GatewayRequest, GatewayResponse, GatewayResponseValue, GatewayRun,
82    content_id_expr, content_id_hex,
83};
84pub use ops::{
85    OpenAiGatewayFunction, cache_stats_symbol, capability_report_symbol, events_symbol,
86    fabric_symbol, health_symbol, key_add_symbol, key_list_symbol, model_health_symbol,
87    models_symbol, plan_check_symbol, plan_combinators_symbol, plan_explain_symbol,
88    plan_parse_symbol, plan_run_symbol, run_get_symbol, runs_symbol, serve_symbol,
89    storage_stats_symbol,
90};
91pub use plan::{
92    BackendDescriptor, PlanCombinator, PlanEvalEvent, PlanEvalReport, PlanLimits, check_plan,
93    check_plan_with_limits, eval_plan, eval_plan_report, eval_plan_report_with_cache,
94    eval_plan_report_with_cache_and_runners, eval_plan_report_with_cache_runners_and_federation,
95    eval_plan_report_with_federation, explain_plan, parse_plan, plan_combinators,
96    plan_combinators_expr, plan_symbol, provider_prefixes, resolve_atom_address,
97};
98pub use routes::admin::{
99    ADMIN_CACHE_STATS_PATH, ADMIN_CAPABILITY_REPORT_PATH, ADMIN_EVENTS_PATH,
100    ADMIN_MODEL_HEALTH_PATH, ADMIN_RUN_RETRIEVAL_PREFIX, ADMIN_RUN_RETRIEVAL_ROUTE,
101    ADMIN_RUNS_PATH, ADMIN_STORAGE_STATS_PATH,
102};
103pub use routes::audio::{
104    AUDIO_SPEECH_PATH, AUDIO_TRANSCRIPTIONS_PATH, handle_audio_speech, handle_audio_transcriptions,
105};
106pub use routes::batches::{
107    BATCH_CANCEL_ROUTE, BATCH_RETRIEVAL_PREFIX, BATCH_RETRIEVAL_ROUTE, BATCHES_PATH,
108    handle_batch_cancel, handle_batch_retrieval, handle_batches, retrieve_batch,
109};
110pub use routes::chat_completions::{
111    CHAT_COMPLETIONS_PATH, ChatCompletionExecution, execute_chat_completion_request,
112    execute_chat_completion_request_with_runners,
113    execute_chat_completion_request_with_runners_and_federation, handle_chat_completions,
114};
115pub use routes::embeddings::{
116    EMBEDDINGS_PATH, EmbeddingExecution, EmbeddingIdGenerators, TENSOR_F64_SMALL_EMBEDDING_MODEL,
117    execute_embedding_request, handle_embeddings,
118};
119pub use routes::files::{
120    FILE_RETRIEVAL_PREFIX, FILE_RETRIEVAL_ROUTE, FILES_PATH, handle_file_retrieval, handle_files,
121    retrieve_file,
122};
123pub use routes::health::{HEALTH_BODY, HEALTH_PATH, health_response};
124pub use routes::images::{IMAGES_GENERATIONS_PATH, handle_image_generations};
125pub use routes::models::{
126    MODELS_PATH, ModelCatalog, OpenAiModel, models_response, models_response_for_catalog,
127    models_response_for_runner_args,
128};
129pub use routes::replay::{
130    RESPONSE_EVENTS_ROUTE, RESPONSE_EVENTS_SUFFIX, RESPONSE_SIM_ROUTE, RESPONSE_SIM_SUFFIX,
131    SIM_EXTENSION_CAPABILITY, SIM_FORK_PATH, SIM_INSPECTION_CAPABILITY, SIM_REPLAY_PATH,
132    handle_response_events, handle_response_sim, handle_sim_fork, handle_sim_replay,
133    response_events, response_sim,
134};
135pub use routes::responses::{
136    RESPONSE_RETRIEVAL_PREFIX, RESPONSE_RETRIEVAL_ROUTE, RESPONSES_PATH, ResponseExecution,
137    ResponseIdGenerators, ResponseRuntimeTargets, execute_response_request,
138    execute_response_request_with_cache, execute_response_request_with_cache_and_runners,
139    execute_response_request_with_cache_runners_and_federation,
140    execute_response_request_with_runners, handle_response_retrieval, handle_responses,
141    retrieve_response,
142};
143pub use routes::threads::{
144    THREAD_MESSAGES_ROUTE, THREAD_RETRIEVAL_PREFIX, THREAD_RETRIEVAL_ROUTE, THREADS_PATH,
145    handle_thread_get, handle_thread_post, handle_threads, list_messages, retrieve_thread,
146};
147pub use routes::vector_stores::{
148    VECTOR_STORE_SEARCH_PREFIX, VECTOR_STORE_SEARCH_ROUTE, VECTOR_STORE_SEARCH_SUFFIX,
149    VECTOR_STORES_PATH, handle_vector_store_search, handle_vector_stores,
150};
151pub use runtime::{
152    OPENAI_GATEWAY_KEY_OBJECT, OpenAiFederatedGateway, OpenAiFederation, OpenAiFederationPolicy,
153    OpenAiGatewayFabric, OpenAiGatewayKey, OpenAiKeyTable, OpenAiPlanCache, OpenAiRunnerRegistry,
154    PlanCacheKey, PlanCacheMode, PlanCacheWriteTarget, ToolLoopConfig, global_openai_key_table,
155    key_hash, redacted_gateway_request, run_tool_loop_with_cache, run_tool_loop_with_registry,
156};
157pub use server::{
158    GatewayRouteState, GatewayRoutes, GatewayRoutesValue, configure_routes,
159    configure_routes_with_state,
160};
161pub use sim_lib_server::{DeterministicWallClock, SystemWallClock, WallClock, WallTimestamp};
162pub use storage::{
163    GATEWAY_BATCH_KIND, GATEWAY_FILE_KIND, GATEWAY_THREAD_KIND, GATEWAY_THREAD_MESSAGE_KIND,
164    GATEWAY_VECTOR_STORE_ITEM_KIND, GATEWAY_VECTOR_STORE_KIND, GatewayBatch, GatewayBatchCounts,
165    GatewayBatchStatus, GatewayFile, GatewayFileStorageRef, GatewayResponseObjectStore,
166    GatewayStateStore, GatewayStore, GatewayThread, GatewayThreadMessage, GatewayVectorStore,
167    GatewayVectorStoreItem, MemoryGatewayStore, StoredGatewayResponse, TableGatewayStore,
168};
169pub use translate::tools::{
170    OpenAiTool, OpenAiToolCall, OpenAiToolRegistry, OpenAiToolResult, openai_name_to_symbol,
171    symbol_from_text,
172};
173
174/// Cookbook recipes for this lib, embedded at build time.
175pub static RECIPES: sim_cookbook::EmbeddedDir =
176    include!(concat!(env!("OUT_DIR"), "/cookbook_recipes.rs"));
177
178#[cfg(test)]
179mod tests;