Skip to main content

codex_convert_proxy/
lib.rs

1//! Codex Convert Proxy Library
2//!
3//! A library for converting between OpenAI Responses API (used by Codex)
4//! and Chat Completions API (used by Chinese LLM providers).
5//!
6//! # Overview
7//!
8//! Codex 0.118+ only supports the Responses API, but Chinese LLM providers
9//! (GLM, Kimi, DeepSeek, MiniMax) only support the Chat Completions API.
10//! This library provides bidirectional conversion between these formats.
11//!
12//! # Features
13//!
14//! - `lib`: Provider trait + implementations + conversion + types (for embedding
15//!   the conversion logic in other Rust applications)
16//! - `server` (default): Full Pingora-based proxy binary (implies `lib`, adds
17//!   config parsing, the HTTP server, CLI, and logging)
18//! - `telemetry`: OpenTelemetry tracing/metrics exporters
19//!
20//! # Usage (as library)
21//!
22//! ```ignore
23//! use codex_convert_proxy::{
24//!     response_to_chat, chat_to_response, chat_to_response_with_context,
25//!     chat_chunk_to_response_events, event_to_sse,
26//!     Provider, GLMProvider, create_provider,
27//!     StreamState, ResponseRequestContext, ResponseStreamEvent,
28//!     types::response_api::{ResponseRequest, InputItemOrString},
29//!     util::parse_sse,
30//! };
31//! ```
32//!
33//! # Providers
34//!
35//! Each Chinese LLM provider may have slightly different API requirements.
36//! Use the appropriate provider when converting:
37//!
38//! - [`GLMProvider`] - For Zhipu AI GLM models
39//! - [`KimiProvider`] - For Moonshot AI Kimi models
40//! - [`DeepSeekProvider`] - For DeepSeek models
41//! - [`MiniMaxProvider`] - For MiniMax models
42//! - [`DefaultProvider`] - Generic fallback for any OpenAI-compatible provider
43//!   (used automatically by [`create_provider`] when the provider name is
44//!   not in the built-in registry)
45
46// Core modules (always available)
47pub mod constants;
48pub mod error;
49pub mod stats;
50pub mod types;
51pub mod util;
52
53// Conversion modules (require the `lib` feature for the Provider trait)
54#[cfg(feature = "lib")]
55pub mod convert;
56
57// Re-export main types
58pub use error::{ConversionError, ProxyError};
59pub use types::*;
60
61// Re-export convert functions (requires the `lib` feature)
62#[cfg(feature = "lib")]
63pub use convert::{
64    chat_chunk_to_response_events, chat_to_response, chat_to_response_with_context,
65    event_to_sse, response_to_chat,
66};
67
68// Re-export streaming types for library consumers
69#[cfg(feature = "lib")]
70pub use convert::ResponseRequestContext;
71#[cfg(feature = "lib")]
72pub use convert::streaming::{ResponseStreamEvent, StreamState};
73
74// Re-export stats
75pub use stats::{RequestRecord, RequestStats, StatsSummary, TokenUsage};
76
77// Providers module (`lib` feature)
78#[cfg(feature = "lib")]
79pub mod providers;
80
81#[cfg(feature = "lib")]
82pub use providers::{create_provider, DefaultProvider, DeepSeekProvider, GLMProvider, KimiProvider, MiniMaxProvider, Provider};
83
84// Config module (`server` feature - parsing backend definitions)
85#[cfg(feature = "server")]
86pub mod config;
87
88#[cfg(feature = "server")]
89pub use config::{BackendConfig, BackendInfo, BackendRouter, ProxyConfig};
90
91// Server functionality (server feature - Pingora proxy)
92#[cfg(feature = "server")]
93pub mod proxy;
94
95#[cfg(feature = "server")]
96pub mod server;
97
98#[cfg(feature = "server")]
99pub use proxy::CodexProxy;
100
101// CLI binary support (full feature implies binary)
102#[cfg(feature = "server")]
103pub mod cli;
104
105#[cfg(feature = "server")]
106pub use cli::{Cli, Commands, StartArgs};
107
108// Logging (server/binary feature - requires tracing-subscriber and tracing-appender)
109#[cfg(feature = "server")]
110pub mod logger;
111
112// Telemetry (`telemetry` feature)
113#[cfg(feature = "telemetry")]
114pub mod telemetry;
115
116#[cfg(feature = "telemetry")]
117pub use telemetry::TelemetryConfig;