Skip to main content

sim_lib_openai_server/
lib.rs

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