refactor: 大规模模块拆分与代码精简,新增 ai-pipeline/data-contracts 独立 crate

- 新增 aether-ai-pipeline 和 aether-data-contracts crate,将 pipeline 逻辑与数据契约从 gateway 中解耦
- 重构 admin handlers:拆分单体模块为 auth/billing/endpoint/features/model/observability/provider/system 等独立子模块
- 合并 chat/cli 重复代码路径:精简 conversion、finalize、planner 中的 sync/chat/cli 分支
- 重构 scheduler/executor/data 层,引入 facade 模式降低模块间耦合
- 移除冗余的 intent 模块,将 plan_fallback/policy/stream_path/sync_path 迁移至 executor
- 前端适配:调整 admin API 调用和 provider 模型测试对话框
This commit is contained in:
fawney19
2026-04-07 02:50:19 +08:00
parent 763ff03a7b
commit 5d96d6673b
732 changed files with 28593 additions and 20666 deletions

View File

@@ -0,0 +1,48 @@
use base64::Engine as _;
pub fn parse_direct_request_body(
is_json_request: bool,
body_bytes: &[u8],
) -> Option<(serde_json::Value, Option<String>)> {
if is_json_request {
if body_bytes.is_empty() {
Some((serde_json::json!({}), None))
} else {
serde_json::from_slice::<serde_json::Value>(body_bytes)
.ok()
.map(|value| (value, None))
}
} else {
Some((
serde_json::json!({}),
(!body_bytes.is_empty())
.then(|| base64::engine::general_purpose::STANDARD.encode(body_bytes)),
))
}
}
#[cfg(test)]
mod tests {
use super::parse_direct_request_body;
#[test]
fn parses_empty_json_body_as_empty_object() {
assert_eq!(
parse_direct_request_body(true, b""),
Some((serde_json::json!({}), None))
);
}
#[test]
fn rejects_invalid_json_body() {
assert_eq!(parse_direct_request_body(true, b"{invalid"), None);
}
#[test]
fn encodes_non_json_body_as_base64() {
assert_eq!(
parse_direct_request_body(false, b"hello"),
Some((serde_json::json!({}), Some("aGVsbG8=".to_string())))
);
}
}

View File

@@ -0,0 +1 @@
pub use crate::planner::standard::matrix::build_standard_request_body_from_canonical;

View File

@@ -0,0 +1,7 @@
pub mod common;
pub mod matrix;
pub mod openai;
pub mod passthrough;
pub mod route;
pub mod specialized;
pub mod standard;

View File

@@ -0,0 +1,104 @@
use serde_json::{Map, Value};
pub fn parse_openai_stop_sequences(stop: Option<&Value>) -> Option<Vec<Value>> {
match stop {
Some(Value::String(value)) if !value.trim().is_empty() => {
Some(vec![Value::String(value.clone())])
}
Some(Value::Array(values)) => Some(
values
.iter()
.filter_map(|value| value.as_str())
.map(str::trim)
.filter(|value| !value.is_empty())
.map(|value| Value::String(value.to_string()))
.collect::<Vec<_>>(),
)
.filter(|values| !values.is_empty()),
_ => None,
}
}
pub fn resolve_openai_chat_max_tokens(request: &Map<String, Value>) -> u64 {
request
.get("max_completion_tokens")
.and_then(value_as_u64)
.or_else(|| request.get("max_tokens").and_then(value_as_u64))
.unwrap_or(4096)
}
pub fn value_as_u64(value: &Value) -> Option<u64> {
value
.as_u64()
.or_else(|| value.as_i64().and_then(|value| u64::try_from(value).ok()))
}
pub fn copy_request_number_field(
request: &Map<String, Value>,
target: &mut Map<String, Value>,
key: &str,
) {
copy_request_number_field_as(request, target, key, key);
}
pub fn copy_request_number_field_as(
request: &Map<String, Value>,
target: &mut Map<String, Value>,
source_key: &str,
target_key: &str,
) {
if let Some(value) = request.get(source_key).cloned() {
if value.is_number() {
target.insert(target_key.to_string(), value);
}
}
}
pub fn map_openai_reasoning_effort_to_claude_output(value: &str) -> Option<&'static str> {
match value.trim().to_ascii_lowercase().as_str() {
"low" => Some("low"),
"medium" => Some("medium"),
"high" => Some("high"),
"xhigh" => Some("max"),
_ => None,
}
}
pub fn map_openai_reasoning_effort_to_thinking_budget(value: &str) -> Option<u64> {
match value.trim().to_ascii_lowercase().as_str() {
"low" => Some(1280),
"medium" => Some(2048),
"high" => Some(4096),
"xhigh" => Some(8192),
_ => None,
}
}
pub fn map_openai_reasoning_effort_to_gemini_budget(value: &str) -> Option<u64> {
map_openai_reasoning_effort_to_thinking_budget(value)
}
pub fn map_thinking_budget_to_openai_reasoning_effort(value: u64) -> &'static str {
match value {
0..=1664 => "low",
1665..=3072 => "medium",
3073..=6144 => "high",
_ => "xhigh",
}
}
pub fn extract_openai_reasoning_effort(request: &Map<String, Value>) -> Option<String> {
request
.get("reasoning_effort")
.and_then(Value::as_str)
.or_else(|| {
request
.get("reasoning")
.and_then(Value::as_object)
.and_then(|reasoning| reasoning.get("effort"))
.and_then(Value::as_str)
})
.map(str::trim)
.filter(|value| !value.is_empty())
.map(|value| value.to_ascii_lowercase())
}

View File

@@ -0,0 +1 @@
pub mod provider;

View File

@@ -0,0 +1,109 @@
use crate::contracts::{
CLAUDE_CHAT_STREAM_PLAN_KIND, CLAUDE_CHAT_SYNC_PLAN_KIND, CLAUDE_CLI_STREAM_PLAN_KIND,
CLAUDE_CLI_SYNC_PLAN_KIND, GEMINI_CHAT_STREAM_PLAN_KIND, GEMINI_CHAT_SYNC_PLAN_KIND,
GEMINI_CLI_STREAM_PLAN_KIND, GEMINI_CLI_SYNC_PLAN_KIND,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocalSameFormatProviderFamily {
Standard,
Gemini,
}
#[derive(Debug, Clone, Copy)]
pub struct LocalSameFormatProviderSpec {
pub api_format: &'static str,
pub decision_kind: &'static str,
pub report_kind: &'static str,
pub family: LocalSameFormatProviderFamily,
pub require_streaming: bool,
}
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalSameFormatProviderSpec> {
match plan_kind {
CLAUDE_CHAT_SYNC_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "claude:chat",
decision_kind: CLAUDE_CHAT_SYNC_PLAN_KIND,
report_kind: "claude_chat_sync_success",
family: LocalSameFormatProviderFamily::Standard,
require_streaming: false,
}),
CLAUDE_CLI_SYNC_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "claude:cli",
decision_kind: CLAUDE_CLI_SYNC_PLAN_KIND,
report_kind: "claude_cli_sync_success",
family: LocalSameFormatProviderFamily::Standard,
require_streaming: false,
}),
GEMINI_CHAT_SYNC_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "gemini:chat",
decision_kind: GEMINI_CHAT_SYNC_PLAN_KIND,
report_kind: "gemini_chat_sync_success",
family: LocalSameFormatProviderFamily::Gemini,
require_streaming: false,
}),
GEMINI_CLI_SYNC_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "gemini:cli",
decision_kind: GEMINI_CLI_SYNC_PLAN_KIND,
report_kind: "gemini_cli_sync_success",
family: LocalSameFormatProviderFamily::Gemini,
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalSameFormatProviderSpec> {
match plan_kind {
CLAUDE_CHAT_STREAM_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "claude:chat",
decision_kind: CLAUDE_CHAT_STREAM_PLAN_KIND,
report_kind: "claude_chat_stream_success",
family: LocalSameFormatProviderFamily::Standard,
require_streaming: true,
}),
CLAUDE_CLI_STREAM_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "claude:cli",
decision_kind: CLAUDE_CLI_STREAM_PLAN_KIND,
report_kind: "claude_cli_stream_success",
family: LocalSameFormatProviderFamily::Standard,
require_streaming: true,
}),
GEMINI_CHAT_STREAM_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "gemini:chat",
decision_kind: GEMINI_CHAT_STREAM_PLAN_KIND,
report_kind: "gemini_chat_stream_success",
family: LocalSameFormatProviderFamily::Gemini,
require_streaming: true,
}),
GEMINI_CLI_STREAM_PLAN_KIND => Some(LocalSameFormatProviderSpec {
api_format: "gemini:cli",
decision_kind: GEMINI_CLI_STREAM_PLAN_KIND,
report_kind: "gemini_cli_stream_success",
family: LocalSameFormatProviderFamily::Gemini,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_claude_sync_same_format_spec() {
let spec = resolve_sync_spec("claude_chat_sync").expect("spec");
assert_eq!(spec.api_format, "claude:chat");
assert_eq!(spec.report_kind, "claude_chat_sync_success");
assert!(!spec.require_streaming);
}
#[test]
fn resolves_gemini_stream_same_format_spec() {
let spec = resolve_stream_spec("gemini_cli_stream").expect("spec");
assert_eq!(spec.api_format, "gemini:cli");
assert_eq!(spec.report_kind, "gemini_cli_stream_success");
assert!(spec.require_streaming);
}
}

View File

@@ -0,0 +1,356 @@
use http::Method;
use crate::contracts::{
CLAUDE_CHAT_STREAM_PLAN_KIND, CLAUDE_CHAT_SYNC_PLAN_KIND, CLAUDE_CLI_STREAM_PLAN_KIND,
CLAUDE_CLI_SYNC_PLAN_KIND, GEMINI_CHAT_STREAM_PLAN_KIND, GEMINI_CHAT_SYNC_PLAN_KIND,
GEMINI_CLI_STREAM_PLAN_KIND, GEMINI_CLI_SYNC_PLAN_KIND, GEMINI_FILES_DELETE_PLAN_KIND,
GEMINI_FILES_DOWNLOAD_PLAN_KIND, GEMINI_FILES_GET_PLAN_KIND, GEMINI_FILES_LIST_PLAN_KIND,
GEMINI_FILES_UPLOAD_PLAN_KIND, GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND,
GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND, OPENAI_CHAT_STREAM_PLAN_KIND, OPENAI_CHAT_SYNC_PLAN_KIND,
OPENAI_CLI_STREAM_PLAN_KIND, OPENAI_CLI_SYNC_PLAN_KIND, OPENAI_COMPACT_STREAM_PLAN_KIND,
OPENAI_COMPACT_SYNC_PLAN_KIND, OPENAI_VIDEO_CANCEL_SYNC_PLAN_KIND,
OPENAI_VIDEO_CONTENT_PLAN_KIND, OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND,
OPENAI_VIDEO_DELETE_SYNC_PLAN_KIND, OPENAI_VIDEO_REMIX_SYNC_PLAN_KIND,
};
pub fn resolve_execution_runtime_stream_plan_kind(
route_class: Option<&str>,
route_family: Option<&str>,
route_kind: Option<&str>,
method: &Method,
path: &str,
) -> Option<&'static str> {
if route_class != Some("ai_public") {
return None;
}
if route_family == Some("gemini")
&& route_kind == Some("files")
&& *method == Method::GET
&& path.ends_with(":download")
{
return Some(GEMINI_FILES_DOWNLOAD_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("chat")
&& *method == Method::POST
&& path == "/v1/chat/completions"
{
return Some(OPENAI_CHAT_STREAM_PLAN_KIND);
}
if route_family == Some("claude")
&& route_kind == Some("chat")
&& *method == Method::POST
&& path == "/v1/messages"
{
return Some(CLAUDE_CHAT_STREAM_PLAN_KIND);
}
if route_family == Some("claude")
&& route_kind == Some("cli")
&& *method == Method::POST
&& path == "/v1/messages"
{
return Some(CLAUDE_CLI_STREAM_PLAN_KIND);
}
if route_family == Some("gemini")
&& route_kind == Some("chat")
&& *method == Method::POST
&& path.ends_with(":streamGenerateContent")
{
return Some(GEMINI_CHAT_STREAM_PLAN_KIND);
}
if route_family == Some("gemini")
&& route_kind == Some("cli")
&& *method == Method::POST
&& path.ends_with(":streamGenerateContent")
{
return Some(GEMINI_CLI_STREAM_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("cli")
&& *method == Method::POST
&& path == "/v1/responses"
{
return Some(OPENAI_CLI_STREAM_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("compact")
&& *method == Method::POST
&& path == "/v1/responses/compact"
{
return Some(OPENAI_COMPACT_STREAM_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("video")
&& *method == Method::GET
&& path.ends_with("/content")
{
return Some(OPENAI_VIDEO_CONTENT_PLAN_KIND);
}
None
}
pub fn resolve_execution_runtime_sync_plan_kind(
route_class: Option<&str>,
route_family: Option<&str>,
route_kind: Option<&str>,
method: &Method,
path: &str,
) -> Option<&'static str> {
if route_class != Some("ai_public") {
return None;
}
if route_family == Some("openai")
&& route_kind == Some("video")
&& *method == Method::POST
&& path.starts_with("/v1/videos/")
&& path.ends_with("/cancel")
{
return Some(OPENAI_VIDEO_CANCEL_SYNC_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("video")
&& *method == Method::POST
&& path.starts_with("/v1/videos/")
&& path.ends_with("/remix")
{
return Some(OPENAI_VIDEO_REMIX_SYNC_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("video")
&& *method == Method::POST
&& path == "/v1/videos"
{
return Some(OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("video")
&& *method == Method::DELETE
&& path.starts_with("/v1/videos/")
{
return Some(OPENAI_VIDEO_DELETE_SYNC_PLAN_KIND);
}
if route_family == Some("gemini")
&& route_kind == Some("video")
&& *method == Method::POST
&& path.ends_with(":cancel")
{
return Some(GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND);
}
if route_family == Some("gemini")
&& route_kind == Some("video")
&& *method == Method::POST
&& path.ends_with(":predictLongRunning")
{
return Some(GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("chat")
&& *method == Method::POST
&& path == "/v1/chat/completions"
{
return Some(OPENAI_CHAT_SYNC_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("cli")
&& *method == Method::POST
&& path == "/v1/responses"
{
return Some(OPENAI_CLI_SYNC_PLAN_KIND);
}
if route_family == Some("openai")
&& route_kind == Some("compact")
&& *method == Method::POST
&& path == "/v1/responses/compact"
{
return Some(OPENAI_COMPACT_SYNC_PLAN_KIND);
}
if route_family == Some("claude")
&& route_kind == Some("chat")
&& *method == Method::POST
&& path == "/v1/messages"
{
return Some(CLAUDE_CHAT_SYNC_PLAN_KIND);
}
if route_family == Some("claude")
&& route_kind == Some("cli")
&& *method == Method::POST
&& path == "/v1/messages"
{
return Some(CLAUDE_CLI_SYNC_PLAN_KIND);
}
if route_family == Some("gemini")
&& route_kind == Some("chat")
&& *method == Method::POST
&& path.ends_with(":generateContent")
{
return Some(GEMINI_CHAT_SYNC_PLAN_KIND);
}
if route_family == Some("gemini")
&& route_kind == Some("cli")
&& *method == Method::POST
&& path.ends_with(":generateContent")
{
return Some(GEMINI_CLI_SYNC_PLAN_KIND);
}
if route_family == Some("gemini") && route_kind == Some("files") {
if *method == Method::POST && path == "/upload/v1beta/files" {
return Some(GEMINI_FILES_UPLOAD_PLAN_KIND);
}
if *method == Method::GET && path == "/v1beta/files" {
return Some(GEMINI_FILES_LIST_PLAN_KIND);
}
if *method == Method::GET
&& path.starts_with("/v1beta/files/")
&& !path.ends_with(":download")
{
return Some(GEMINI_FILES_GET_PLAN_KIND);
}
if *method == Method::DELETE
&& path.starts_with("/v1beta/files/")
&& !path.ends_with(":download")
{
return Some(GEMINI_FILES_DELETE_PLAN_KIND);
}
}
None
}
pub fn is_matching_stream_request(
plan_kind: &str,
path: &str,
body_json: &serde_json::Value,
) -> bool {
match plan_kind {
OPENAI_CHAT_STREAM_PLAN_KIND
| CLAUDE_CHAT_STREAM_PLAN_KIND
| OPENAI_CLI_STREAM_PLAN_KIND
| OPENAI_COMPACT_STREAM_PLAN_KIND
| CLAUDE_CLI_STREAM_PLAN_KIND => body_json
.get("stream")
.and_then(|value| value.as_bool())
.unwrap_or(false),
GEMINI_CHAT_STREAM_PLAN_KIND | GEMINI_CLI_STREAM_PLAN_KIND => {
path.ends_with(":streamGenerateContent")
}
_ => true,
}
}
pub fn supports_sync_scheduler_decision_kind(plan_kind: &str) -> bool {
matches!(
plan_kind,
OPENAI_CHAT_SYNC_PLAN_KIND
| OPENAI_CLI_SYNC_PLAN_KIND
| OPENAI_COMPACT_SYNC_PLAN_KIND
| CLAUDE_CHAT_SYNC_PLAN_KIND
| CLAUDE_CLI_SYNC_PLAN_KIND
| GEMINI_CHAT_SYNC_PLAN_KIND
| GEMINI_CLI_SYNC_PLAN_KIND
| GEMINI_FILES_UPLOAD_PLAN_KIND
| OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND
| OPENAI_VIDEO_REMIX_SYNC_PLAN_KIND
| OPENAI_VIDEO_CANCEL_SYNC_PLAN_KIND
| OPENAI_VIDEO_DELETE_SYNC_PLAN_KIND
| GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND
| GEMINI_VIDEO_CANCEL_SYNC_PLAN_KIND
| GEMINI_FILES_GET_PLAN_KIND
| GEMINI_FILES_LIST_PLAN_KIND
| GEMINI_FILES_DELETE_PLAN_KIND
)
}
pub fn supports_stream_scheduler_decision_kind(plan_kind: &str) -> bool {
matches!(
plan_kind,
OPENAI_CHAT_STREAM_PLAN_KIND
| CLAUDE_CHAT_STREAM_PLAN_KIND
| GEMINI_CHAT_STREAM_PLAN_KIND
| OPENAI_CLI_STREAM_PLAN_KIND
| OPENAI_COMPACT_STREAM_PLAN_KIND
| CLAUDE_CLI_STREAM_PLAN_KIND
| GEMINI_CLI_STREAM_PLAN_KIND
| GEMINI_FILES_DOWNLOAD_PLAN_KIND
| OPENAI_VIDEO_CONTENT_PLAN_KIND
)
}
#[cfg(test)]
mod tests {
use http::Method;
use super::{
is_matching_stream_request, resolve_execution_runtime_stream_plan_kind,
resolve_execution_runtime_sync_plan_kind, supports_stream_scheduler_decision_kind,
supports_sync_scheduler_decision_kind,
};
use crate::contracts::{OPENAI_CHAT_STREAM_PLAN_KIND, OPENAI_CHAT_SYNC_PLAN_KIND};
#[test]
fn resolves_openai_chat_plan_kinds() {
assert_eq!(
resolve_execution_runtime_sync_plan_kind(
Some("ai_public"),
Some("openai"),
Some("chat"),
&Method::POST,
"/v1/chat/completions",
),
Some(OPENAI_CHAT_SYNC_PLAN_KIND)
);
assert_eq!(
resolve_execution_runtime_stream_plan_kind(
Some("ai_public"),
Some("openai"),
Some("chat"),
&Method::POST,
"/v1/chat/completions",
),
Some(OPENAI_CHAT_STREAM_PLAN_KIND)
);
}
#[test]
fn stream_matching_requires_openai_stream_flag() {
assert!(!is_matching_stream_request(
OPENAI_CHAT_STREAM_PLAN_KIND,
"/v1/chat/completions",
&serde_json::json!({"stream": false}),
));
assert!(is_matching_stream_request(
OPENAI_CHAT_STREAM_PLAN_KIND,
"/v1/chat/completions",
&serde_json::json!({"stream": true}),
));
assert!(supports_sync_scheduler_decision_kind(
OPENAI_CHAT_SYNC_PLAN_KIND
));
assert!(supports_stream_scheduler_decision_kind(
OPENAI_CHAT_STREAM_PLAN_KIND
));
}
}

View File

@@ -0,0 +1,69 @@
use crate::contracts::{
GEMINI_FILES_DELETE_PLAN_KIND, GEMINI_FILES_DOWNLOAD_PLAN_KIND, GEMINI_FILES_GET_PLAN_KIND,
GEMINI_FILES_LIST_PLAN_KIND, GEMINI_FILES_UPLOAD_PLAN_KIND,
};
#[derive(Debug, Clone, Copy)]
pub struct LocalGeminiFilesSpec {
pub decision_kind: &'static str,
pub report_kind: Option<&'static str>,
pub require_streaming: bool,
}
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalGeminiFilesSpec> {
match plan_kind {
GEMINI_FILES_UPLOAD_PLAN_KIND => Some(LocalGeminiFilesSpec {
decision_kind: GEMINI_FILES_UPLOAD_PLAN_KIND,
report_kind: Some("gemini_files_store_mapping"),
require_streaming: false,
}),
GEMINI_FILES_LIST_PLAN_KIND => Some(LocalGeminiFilesSpec {
decision_kind: GEMINI_FILES_LIST_PLAN_KIND,
report_kind: Some("gemini_files_store_mapping"),
require_streaming: false,
}),
GEMINI_FILES_GET_PLAN_KIND => Some(LocalGeminiFilesSpec {
decision_kind: GEMINI_FILES_GET_PLAN_KIND,
report_kind: Some("gemini_files_store_mapping"),
require_streaming: false,
}),
GEMINI_FILES_DELETE_PLAN_KIND => Some(LocalGeminiFilesSpec {
decision_kind: GEMINI_FILES_DELETE_PLAN_KIND,
report_kind: Some("gemini_files_delete_mapping"),
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalGeminiFilesSpec> {
match plan_kind {
GEMINI_FILES_DOWNLOAD_PLAN_KIND => Some(LocalGeminiFilesSpec {
decision_kind: GEMINI_FILES_DOWNLOAD_PLAN_KIND,
report_kind: None,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_sync_gemini_files_specs() {
let spec = resolve_sync_spec("gemini_files_upload").expect("spec");
assert_eq!(spec.decision_kind, "gemini_files_upload");
assert_eq!(spec.report_kind, Some("gemini_files_store_mapping"));
assert!(!spec.require_streaming);
}
#[test]
fn resolves_stream_gemini_files_spec() {
let spec = resolve_stream_spec("gemini_files_download").expect("spec");
assert_eq!(spec.decision_kind, "gemini_files_download");
assert_eq!(spec.report_kind, None);
assert!(spec.require_streaming);
}
}

View File

@@ -0,0 +1,2 @@
pub mod files;
pub mod video;

View File

@@ -0,0 +1,54 @@
use crate::contracts::{GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND, OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocalVideoCreateFamily {
OpenAi,
Gemini,
}
#[derive(Debug, Clone, Copy)]
pub struct LocalVideoCreateSpec {
pub api_format: &'static str,
pub decision_kind: &'static str,
pub report_kind: &'static str,
pub family: LocalVideoCreateFamily,
}
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalVideoCreateSpec> {
match plan_kind {
OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND => Some(LocalVideoCreateSpec {
api_format: "openai:video",
decision_kind: OPENAI_VIDEO_CREATE_SYNC_PLAN_KIND,
report_kind: "openai_video_create_sync_finalize",
family: LocalVideoCreateFamily::OpenAi,
}),
GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND => Some(LocalVideoCreateSpec {
api_format: "gemini:video",
decision_kind: GEMINI_VIDEO_CREATE_SYNC_PLAN_KIND,
report_kind: "gemini_video_create_sync_finalize",
family: LocalVideoCreateFamily::Gemini,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_sync_spec, LocalVideoCreateFamily};
#[test]
fn resolves_openai_video_create_spec() {
let spec = resolve_sync_spec("openai_video_create_sync").expect("spec");
assert_eq!(spec.api_format, "openai:video");
assert_eq!(spec.family, LocalVideoCreateFamily::OpenAi);
assert_eq!(spec.report_kind, "openai_video_create_sync_finalize");
}
#[test]
fn resolves_gemini_video_create_spec() {
let spec = resolve_sync_spec("gemini_video_create_sync").expect("spec");
assert_eq!(spec.api_format, "gemini:video");
assert_eq!(spec.family, LocalVideoCreateFamily::Gemini);
assert_eq!(spec.report_kind, "gemini_video_create_sync_finalize");
}
}

View File

@@ -0,0 +1,53 @@
use crate::contracts::{CLAUDE_CHAT_STREAM_PLAN_KIND, CLAUDE_CHAT_SYNC_PLAN_KIND};
use crate::planner::standard::family::{
LocalStandardSourceFamily, LocalStandardSourceMode, LocalStandardSpec,
};
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
CLAUDE_CHAT_SYNC_PLAN_KIND => Some(LocalStandardSpec {
api_format: "claude:chat",
decision_kind: CLAUDE_CHAT_SYNC_PLAN_KIND,
report_kind: "claude_chat_sync_finalize",
family: LocalStandardSourceFamily::Standard,
mode: LocalStandardSourceMode::Chat,
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
CLAUDE_CHAT_STREAM_PLAN_KIND => Some(LocalStandardSpec {
api_format: "claude:chat",
decision_kind: CLAUDE_CHAT_STREAM_PLAN_KIND,
report_kind: "claude_chat_stream_success",
family: LocalStandardSourceFamily::Standard,
mode: LocalStandardSourceMode::Chat,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_claude_chat_sync_spec() {
let spec = resolve_sync_spec("claude_chat_sync").expect("spec");
assert_eq!(spec.api_format, "claude:chat");
assert_eq!(spec.report_kind, "claude_chat_sync_finalize");
assert!(!spec.require_streaming);
}
#[test]
fn resolves_claude_chat_stream_spec() {
let spec = resolve_stream_spec("claude_chat_stream").expect("spec");
assert_eq!(spec.api_format, "claude:chat");
assert_eq!(spec.report_kind, "claude_chat_stream_success");
assert!(spec.require_streaming);
}
}

View File

@@ -0,0 +1,53 @@
use crate::contracts::{CLAUDE_CLI_STREAM_PLAN_KIND, CLAUDE_CLI_SYNC_PLAN_KIND};
use crate::planner::standard::family::{
LocalStandardSourceFamily, LocalStandardSourceMode, LocalStandardSpec,
};
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
CLAUDE_CLI_SYNC_PLAN_KIND => Some(LocalStandardSpec {
api_format: "claude:cli",
decision_kind: CLAUDE_CLI_SYNC_PLAN_KIND,
report_kind: "claude_cli_sync_finalize",
family: LocalStandardSourceFamily::Standard,
mode: LocalStandardSourceMode::Cli,
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
CLAUDE_CLI_STREAM_PLAN_KIND => Some(LocalStandardSpec {
api_format: "claude:cli",
decision_kind: CLAUDE_CLI_STREAM_PLAN_KIND,
report_kind: "claude_cli_stream_success",
family: LocalStandardSourceFamily::Standard,
mode: LocalStandardSourceMode::Cli,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_claude_cli_sync_spec() {
let spec = resolve_sync_spec("claude_cli_sync").expect("spec");
assert_eq!(spec.api_format, "claude:cli");
assert_eq!(spec.report_kind, "claude_cli_sync_finalize");
assert!(!spec.require_streaming);
}
#[test]
fn resolves_claude_cli_stream_spec() {
let spec = resolve_stream_spec("claude_cli_stream").expect("spec");
assert_eq!(spec.api_format, "claude:cli");
assert_eq!(spec.report_kind, "claude_cli_stream_success");
assert!(spec.require_streaming);
}
}

View File

@@ -0,0 +1,12 @@
pub mod chat;
pub mod cli;
use crate::planner::standard::LocalStandardSpec;
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
chat::resolve_sync_spec(plan_kind).or_else(|| cli::resolve_sync_spec(plan_kind))
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
chat::resolve_stream_spec(plan_kind).or_else(|| cli::resolve_stream_spec(plan_kind))
}

View File

@@ -0,0 +1,21 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocalStandardSourceFamily {
Standard,
Gemini,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LocalStandardSourceMode {
Chat,
Cli,
}
#[derive(Debug, Clone, Copy)]
pub struct LocalStandardSpec {
pub api_format: &'static str,
pub decision_kind: &'static str,
pub report_kind: &'static str,
pub family: LocalStandardSourceFamily,
pub mode: LocalStandardSourceMode,
pub require_streaming: bool,
}

View File

@@ -0,0 +1,53 @@
use crate::contracts::{GEMINI_CHAT_STREAM_PLAN_KIND, GEMINI_CHAT_SYNC_PLAN_KIND};
use crate::planner::standard::family::{
LocalStandardSourceFamily, LocalStandardSourceMode, LocalStandardSpec,
};
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
GEMINI_CHAT_SYNC_PLAN_KIND => Some(LocalStandardSpec {
api_format: "gemini:chat",
decision_kind: GEMINI_CHAT_SYNC_PLAN_KIND,
report_kind: "gemini_chat_sync_finalize",
family: LocalStandardSourceFamily::Gemini,
mode: LocalStandardSourceMode::Chat,
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
GEMINI_CHAT_STREAM_PLAN_KIND => Some(LocalStandardSpec {
api_format: "gemini:chat",
decision_kind: GEMINI_CHAT_STREAM_PLAN_KIND,
report_kind: "gemini_chat_stream_success",
family: LocalStandardSourceFamily::Gemini,
mode: LocalStandardSourceMode::Chat,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_gemini_chat_sync_spec() {
let spec = resolve_sync_spec("gemini_chat_sync").expect("spec");
assert_eq!(spec.api_format, "gemini:chat");
assert_eq!(spec.report_kind, "gemini_chat_sync_finalize");
assert!(!spec.require_streaming);
}
#[test]
fn resolves_gemini_chat_stream_spec() {
let spec = resolve_stream_spec("gemini_chat_stream").expect("spec");
assert_eq!(spec.api_format, "gemini:chat");
assert_eq!(spec.report_kind, "gemini_chat_stream_success");
assert!(spec.require_streaming);
}
}

View File

@@ -0,0 +1,53 @@
use crate::contracts::{GEMINI_CLI_STREAM_PLAN_KIND, GEMINI_CLI_SYNC_PLAN_KIND};
use crate::planner::standard::family::{
LocalStandardSourceFamily, LocalStandardSourceMode, LocalStandardSpec,
};
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
GEMINI_CLI_SYNC_PLAN_KIND => Some(LocalStandardSpec {
api_format: "gemini:cli",
decision_kind: GEMINI_CLI_SYNC_PLAN_KIND,
report_kind: "gemini_cli_sync_finalize",
family: LocalStandardSourceFamily::Gemini,
mode: LocalStandardSourceMode::Cli,
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
match plan_kind {
GEMINI_CLI_STREAM_PLAN_KIND => Some(LocalStandardSpec {
api_format: "gemini:cli",
decision_kind: GEMINI_CLI_STREAM_PLAN_KIND,
report_kind: "gemini_cli_stream_success",
family: LocalStandardSourceFamily::Gemini,
mode: LocalStandardSourceMode::Cli,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_gemini_cli_sync_spec() {
let spec = resolve_sync_spec("gemini_cli_sync").expect("spec");
assert_eq!(spec.api_format, "gemini:cli");
assert_eq!(spec.report_kind, "gemini_cli_sync_finalize");
assert!(!spec.require_streaming);
}
#[test]
fn resolves_gemini_cli_stream_spec() {
let spec = resolve_stream_spec("gemini_cli_stream").expect("spec");
assert_eq!(spec.api_format, "gemini:cli");
assert_eq!(spec.report_kind, "gemini_cli_stream_success");
assert!(spec.require_streaming);
}
}

View File

@@ -0,0 +1,12 @@
pub mod chat;
pub mod cli;
use crate::planner::standard::LocalStandardSpec;
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
chat::resolve_sync_spec(plan_kind).or_else(|| cli::resolve_sync_spec(plan_kind))
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalStandardSpec> {
chat::resolve_stream_spec(plan_kind).or_else(|| cli::resolve_stream_spec(plan_kind))
}

View File

@@ -0,0 +1,376 @@
use serde_json::Value;
use crate::conversion::request::{
convert_openai_chat_request_to_claude_request, convert_openai_chat_request_to_gemini_request,
convert_openai_chat_request_to_openai_cli_request,
normalize_claude_request_to_openai_chat_request,
normalize_gemini_request_to_openai_chat_request,
normalize_openai_cli_request_to_openai_chat_request,
};
pub fn build_standard_request_body(
body_json: &Value,
client_api_format: &str,
mapped_model: &str,
provider_api_format: &str,
request_path: &str,
upstream_is_stream: bool,
) -> Option<Value> {
let canonical_request = normalize_standard_request_to_openai_chat_request(
body_json,
client_api_format,
request_path,
)?;
build_standard_request_body_from_canonical(
&canonical_request,
mapped_model,
provider_api_format,
upstream_is_stream,
)
}
pub fn build_standard_request_body_from_canonical(
canonical_request: &Value,
mapped_model: &str,
provider_api_format: &str,
upstream_is_stream: bool,
) -> Option<Value> {
match provider_api_format.trim().to_ascii_lowercase().as_str() {
"openai:chat" => {
build_openai_chat_request_body(canonical_request, mapped_model, upstream_is_stream)
}
"openai:cli" => convert_openai_chat_request_to_openai_cli_request(
canonical_request,
mapped_model,
upstream_is_stream,
false,
),
"openai:compact" => convert_openai_chat_request_to_openai_cli_request(
canonical_request,
mapped_model,
false,
true,
),
"claude:chat" | "claude:cli" => convert_openai_chat_request_to_claude_request(
canonical_request,
mapped_model,
upstream_is_stream,
),
"gemini:chat" | "gemini:cli" => convert_openai_chat_request_to_gemini_request(
canonical_request,
mapped_model,
upstream_is_stream,
),
_ => None,
}
}
pub fn normalize_standard_request_to_openai_chat_request(
body_json: &Value,
client_api_format: &str,
request_path: &str,
) -> Option<Value> {
match client_api_format.trim().to_ascii_lowercase().as_str() {
"openai:chat" => Some(body_json.clone()),
"openai:cli" | "openai:compact" => {
normalize_openai_cli_request_to_openai_chat_request(body_json)
}
"claude:chat" | "claude:cli" => normalize_claude_request_to_openai_chat_request(body_json),
"gemini:chat" | "gemini:cli" => {
normalize_gemini_request_to_openai_chat_request(body_json, request_path)
}
_ => None,
}
}
fn build_openai_chat_request_body(
body_json: &Value,
mapped_model: &str,
upstream_is_stream: bool,
) -> Option<Value> {
let request_body_object = body_json.as_object()?;
let mut provider_request_body = serde_json::Map::from_iter(
request_body_object
.iter()
.map(|(key, value)| (key.clone(), value.clone())),
);
provider_request_body.insert("model".to_string(), Value::String(mapped_model.to_string()));
if upstream_is_stream {
provider_request_body.insert("stream".to_string(), Value::Bool(true));
}
Some(Value::Object(provider_request_body))
}
#[cfg(test)]
mod tests {
use super::build_standard_request_body;
use serde_json::json;
#[test]
fn builds_openai_chat_request_from_claude_chat_source() {
let request = json!({
"model": "claude-3-7-sonnet",
"system": "You are concise.",
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": "Hello from Claude"}]
}
],
"max_tokens": 128
});
let converted = build_standard_request_body(
&request,
"claude:chat",
"gpt-5",
"openai:chat",
"/v1/messages",
false,
)
.expect("claude chat should convert to openai chat");
assert_eq!(converted["model"], "gpt-5");
assert_eq!(converted["messages"][0]["role"], "system");
assert_eq!(converted["messages"][0]["content"], "You are concise.");
assert_eq!(converted["messages"][1]["role"], "user");
assert_eq!(converted["messages"][1]["content"], "Hello from Claude");
}
#[test]
fn builds_claude_chat_request_from_gemini_chat_source() {
let request = json!({
"systemInstruction": {
"parts": [{"text": "Be brief."}]
},
"contents": [
{
"role": "user",
"parts": [{"text": "Hello from Gemini"}]
}
]
});
let converted = build_standard_request_body(
&request,
"gemini:chat",
"claude-sonnet-4-5",
"claude:chat",
"/v1beta/models/gemini-2.5-pro:generateContent",
false,
)
.expect("gemini chat should convert to claude chat");
assert_eq!(converted["model"], "claude-sonnet-4-5");
assert_eq!(converted["messages"][0]["role"], "user");
assert!(
converted["messages"]
.to_string()
.contains("Hello from Gemini"),
"converted claude payload should retain the gemini user text: {converted}"
);
}
#[test]
fn builds_gemini_cli_request_from_claude_cli_source() {
let request = json!({
"model": "claude-sonnet-4-5",
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": "Need CLI output"}]
}
],
"max_tokens": 64
});
let converted = build_standard_request_body(
&request,
"claude:cli",
"gemini-2.5-pro",
"gemini:cli",
"/v1/messages",
false,
)
.expect("claude cli should convert to gemini cli");
assert_eq!(converted["contents"][0]["role"], "user");
assert_eq!(
converted["contents"][0]["parts"][0]["text"],
"Need CLI output"
);
}
#[test]
fn builds_openai_chat_request_from_openai_responses_source_with_chat_shape() {
let request = json!({
"model": "gpt-5",
"instructions": "You are concise.",
"input": [{
"type": "message",
"role": "user",
"content": [
{
"type": "input_image",
"image_url": "https://example.com/cat.png",
"detail": "high"
},
{
"type": "input_file",
"file_data": "data:application/pdf;base64,JVBERi0x",
"filename": "spec.pdf"
},
{"type": "input_text", "text": "Summarize this"}
]
}],
"reasoning": {"effort": "high"},
"text": {
"format": {
"type": "json_schema",
"json_schema": {
"name": "answer_schema",
"schema": {
"type": "object",
"properties": {"answer": {"type": "string"}}
}
}
}
}
});
let converted = build_standard_request_body(
&request,
"openai:cli",
"gpt-5",
"openai:chat",
"/v1/responses",
false,
)
.expect("responses request should convert to chat completions");
assert_eq!(converted["messages"][0]["role"], "system");
assert_eq!(converted["messages"][0]["content"], "You are concise.");
assert_eq!(converted["reasoning_effort"], "high");
assert_eq!(
converted["response_format"]["json_schema"]["name"],
"answer_schema"
);
assert_eq!(converted["messages"][1]["content"][0]["type"], "image_url");
assert_eq!(
converted["messages"][1]["content"][0]["image_url"]["url"],
"https://example.com/cat.png"
);
assert_eq!(
converted["messages"][1]["content"][0]["image_url"]["detail"],
"high"
);
assert_eq!(converted["messages"][1]["content"][1]["type"], "file");
assert_eq!(
converted["messages"][1]["content"][1]["file"]["filename"],
"spec.pdf"
);
}
#[test]
fn builds_gemini_request_from_openai_chat_with_structured_output_and_images() {
let request = json!({
"model": "gpt-5",
"messages": [{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "data:image/png;base64,iVBORw0KGgo="
}
},
{"type": "text", "text": "Describe it"}
]
}],
"reasoning_effort": "medium",
"n": 2,
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "answer_schema",
"schema": {
"type": "object",
"properties": {"answer": {"type": "string"}}
}
}
},
"web_search_options": {
"search_context_size": "high"
}
});
let converted = build_standard_request_body(
&request,
"openai:chat",
"gemini-2.5-pro",
"gemini:chat",
"/v1/chat/completions",
false,
)
.expect("openai chat should convert to gemini");
assert_eq!(
converted["generationConfig"]["thinkingConfig"]["thinkingBudget"],
2048
);
assert_eq!(converted["generationConfig"]["candidateCount"], 2);
assert_eq!(
converted["generationConfig"]["responseMimeType"],
"application/json"
);
assert_eq!(
converted["generationConfig"]["responseSchema"]["type"],
"object"
);
assert_eq!(
converted["contents"][0]["parts"][0]["inlineData"]["mimeType"],
"image/png"
);
assert_eq!(converted["tools"][0]["googleSearch"], json!({}));
}
#[test]
fn builds_claude_request_from_openai_chat_with_thinking_and_data_url_image() {
let request = json!({
"model": "gpt-5",
"messages": [{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {
"url": "data:image/jpeg;base64,/9j/4AAQSk"
}
},
{"type": "text", "text": "What is this?"}
]
}],
"reasoning_effort": "low"
});
let converted = build_standard_request_body(
&request,
"openai:chat",
"claude-sonnet-4-5",
"claude:chat",
"/v1/chat/completions",
false,
)
.expect("openai chat should convert to claude");
assert_eq!(converted["thinking"]["type"], "enabled");
assert_eq!(converted["thinking"]["budget_tokens"], 1280);
assert_eq!(
converted["messages"][0]["content"][0]["source"]["type"],
"base64"
);
assert_eq!(
converted["messages"][0]["content"][0]["source"]["media_type"],
"image/jpeg"
);
}
}

View File

@@ -0,0 +1,13 @@
pub mod claude;
pub mod family;
pub mod gemini;
pub mod matrix;
pub mod normalize;
pub mod openai_cli;
pub use family::{LocalStandardSourceFamily, LocalStandardSourceMode, LocalStandardSpec};
pub use matrix::{build_standard_request_body, normalize_standard_request_to_openai_chat_request};
pub use normalize::{
build_cross_format_openai_chat_request_body, build_cross_format_openai_cli_request_body,
build_local_openai_chat_request_body, build_local_openai_cli_request_body,
};

View File

@@ -0,0 +1,144 @@
use serde_json::Value;
use crate::conversion::request::{
convert_openai_chat_request_to_claude_request, convert_openai_chat_request_to_gemini_request,
convert_openai_chat_request_to_openai_cli_request,
normalize_openai_cli_request_to_openai_chat_request,
};
use crate::conversion::{request_conversion_kind, RequestConversionKind};
pub fn build_local_openai_chat_request_body(
body_json: &Value,
mapped_model: &str,
upstream_is_stream: bool,
) -> Option<Value> {
let request_body_object = body_json.as_object()?;
let mut provider_request_body = serde_json::Map::from_iter(
request_body_object
.iter()
.map(|(key, value)| (key.clone(), value.clone())),
);
provider_request_body.insert("model".to_string(), Value::String(mapped_model.to_string()));
if upstream_is_stream {
provider_request_body.insert("stream".to_string(), Value::Bool(true));
}
Some(Value::Object(provider_request_body))
}
pub fn build_cross_format_openai_chat_request_body(
body_json: &Value,
mapped_model: &str,
provider_api_format: &str,
upstream_is_stream: bool,
) -> Option<Value> {
let conversion_kind = request_conversion_kind("openai:chat", provider_api_format)?;
match conversion_kind {
RequestConversionKind::ToClaudeStandard => convert_openai_chat_request_to_claude_request(
body_json,
mapped_model,
upstream_is_stream,
),
RequestConversionKind::ToGeminiStandard => convert_openai_chat_request_to_gemini_request(
body_json,
mapped_model,
upstream_is_stream,
),
RequestConversionKind::ToOpenAIFamilyCli => {
convert_openai_chat_request_to_openai_cli_request(
body_json,
mapped_model,
upstream_is_stream,
false,
)
}
RequestConversionKind::ToOpenAICompact => {
convert_openai_chat_request_to_openai_cli_request(body_json, mapped_model, false, true)
}
_ => None,
}
}
pub fn build_local_openai_cli_request_body(
body_json: &Value,
mapped_model: &str,
require_streaming: bool,
) -> Option<Value> {
let request_body_object = body_json.as_object()?;
let mut provider_request_body = serde_json::Map::from_iter(
request_body_object
.iter()
.map(|(key, value)| (key.clone(), value.clone())),
);
provider_request_body.insert("model".to_string(), Value::String(mapped_model.to_string()));
if require_streaming {
provider_request_body.insert("stream".to_string(), Value::Bool(true));
}
Some(Value::Object(provider_request_body))
}
pub fn build_cross_format_openai_cli_request_body(
body_json: &Value,
mapped_model: &str,
client_api_format: &str,
provider_api_format: &str,
upstream_is_stream: bool,
) -> Option<Value> {
let chat_like_request = normalize_openai_cli_request_to_openai_chat_request(body_json)?;
let conversion_kind = request_conversion_kind(client_api_format, provider_api_format)?;
match conversion_kind {
RequestConversionKind::ToOpenAIFamilyCli => {
convert_openai_chat_request_to_openai_cli_request(
&chat_like_request,
mapped_model,
upstream_is_stream,
false,
)
}
RequestConversionKind::ToOpenAICompact => {
convert_openai_chat_request_to_openai_cli_request(
&chat_like_request,
mapped_model,
false,
true,
)
}
RequestConversionKind::ToClaudeStandard => convert_openai_chat_request_to_claude_request(
&chat_like_request,
mapped_model,
upstream_is_stream,
),
RequestConversionKind::ToGeminiStandard => convert_openai_chat_request_to_gemini_request(
&chat_like_request,
mapped_model,
upstream_is_stream,
),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::build_cross_format_openai_cli_request_body;
use serde_json::json;
#[test]
fn builds_openai_family_cross_format_request_body_from_compact_source() {
let body_json = json!({
"model": "gpt-5",
"input": "hello",
});
let provider_request_body = build_cross_format_openai_cli_request_body(
&body_json,
"gpt-5-upstream",
"openai:compact",
"openai:cli",
false,
)
.expect("compact to openai cli body should build");
assert_eq!(provider_request_body["model"], "gpt-5-upstream");
assert_eq!(provider_request_body["input"][0]["type"], "message");
assert_eq!(provider_request_body["input"][0]["role"], "user");
}
}

View File

@@ -0,0 +1,76 @@
use crate::contracts::{
OPENAI_CLI_STREAM_PLAN_KIND, OPENAI_CLI_SYNC_PLAN_KIND, OPENAI_COMPACT_STREAM_PLAN_KIND,
OPENAI_COMPACT_SYNC_PLAN_KIND,
};
#[derive(Debug, Clone, Copy)]
pub struct LocalOpenAiCliSpec {
pub api_format: &'static str,
pub decision_kind: &'static str,
pub report_kind: &'static str,
pub compact: bool,
pub require_streaming: bool,
}
pub fn resolve_sync_spec(plan_kind: &str) -> Option<LocalOpenAiCliSpec> {
match plan_kind {
OPENAI_CLI_SYNC_PLAN_KIND => Some(LocalOpenAiCliSpec {
api_format: "openai:cli",
decision_kind: OPENAI_CLI_SYNC_PLAN_KIND,
report_kind: "openai_cli_sync_success",
compact: false,
require_streaming: false,
}),
OPENAI_COMPACT_SYNC_PLAN_KIND => Some(LocalOpenAiCliSpec {
api_format: "openai:compact",
decision_kind: OPENAI_COMPACT_SYNC_PLAN_KIND,
report_kind: "openai_cli_sync_success",
compact: true,
require_streaming: false,
}),
_ => None,
}
}
pub fn resolve_stream_spec(plan_kind: &str) -> Option<LocalOpenAiCliSpec> {
match plan_kind {
OPENAI_CLI_STREAM_PLAN_KIND => Some(LocalOpenAiCliSpec {
api_format: "openai:cli",
decision_kind: OPENAI_CLI_STREAM_PLAN_KIND,
report_kind: "openai_cli_stream_success",
compact: false,
require_streaming: true,
}),
OPENAI_COMPACT_STREAM_PLAN_KIND => Some(LocalOpenAiCliSpec {
api_format: "openai:compact",
decision_kind: OPENAI_COMPACT_STREAM_PLAN_KIND,
report_kind: "openai_cli_stream_success",
compact: true,
require_streaming: true,
}),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::{resolve_stream_spec, resolve_sync_spec};
#[test]
fn resolves_openai_cli_sync_spec() {
let spec = resolve_sync_spec("openai_cli_sync").expect("spec");
assert_eq!(spec.api_format, "openai:cli");
assert_eq!(spec.report_kind, "openai_cli_sync_success");
assert!(!spec.compact);
assert!(!spec.require_streaming);
}
#[test]
fn resolves_openai_compact_stream_spec() {
let spec = resolve_stream_spec("openai_compact_stream").expect("spec");
assert_eq!(spec.api_format, "openai:compact");
assert_eq!(spec.report_kind, "openai_cli_stream_success");
assert!(spec.compact);
assert!(spec.require_streaming);
}
}