mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 17:30:23 +08:00
refactor ai formats registry adapters
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
@@ -0,0 +1,18 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{canonical_to_claude_request, from_claude_to_canonical_request, CanonicalRequest},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalRequest> {
|
||||
from_claude_to_canonical_request(body)
|
||||
}
|
||||
|
||||
pub fn to(request: &CanonicalRequest, ctx: &FormatContext) -> Option<Value> {
|
||||
canonical_to_claude_request(
|
||||
request,
|
||||
ctx.mapped_model_or(request.model.as_str()),
|
||||
ctx.upstream_is_stream,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{
|
||||
canonical_to_claude_response, from_claude_to_canonical_response, CanonicalResponse,
|
||||
},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalResponse> {
|
||||
from_claude_to_canonical_response(body)
|
||||
}
|
||||
|
||||
pub fn to(response: &CanonicalResponse, _ctx: &FormatContext) -> Option<Value> {
|
||||
Some(canonical_to_claude_response(response))
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
@@ -0,0 +1,18 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{canonical_to_gemini_request, from_gemini_to_canonical_request, CanonicalRequest},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, ctx: &FormatContext) -> Option<CanonicalRequest> {
|
||||
from_gemini_to_canonical_request(body, ctx.request_path.as_deref().unwrap_or_default())
|
||||
}
|
||||
|
||||
pub fn to(request: &CanonicalRequest, ctx: &FormatContext) -> Option<Value> {
|
||||
canonical_to_gemini_request(
|
||||
request,
|
||||
ctx.mapped_model_or(request.model.as_str()),
|
||||
ctx.upstream_is_stream,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{
|
||||
canonical_to_gemini_response, from_gemini_to_canonical_response, CanonicalResponse,
|
||||
},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalResponse> {
|
||||
from_gemini_to_canonical_response(body)
|
||||
}
|
||||
|
||||
pub fn to(response: &CanonicalResponse, ctx: &FormatContext) -> Option<Value> {
|
||||
canonical_to_gemini_response(response, &ctx.report_context_value())
|
||||
}
|
||||
2
crates/aether-ai-formats/src/formats/openai_chat/mod.rs
Normal file
2
crates/aether-ai-formats/src/formats/openai_chat/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
41
crates/aether-ai-formats/src/formats/openai_chat/request.rs
Normal file
41
crates/aether-ai-formats/src/formats/openai_chat/request.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::{
|
||||
canonical::{
|
||||
canonical_to_openai_chat_request, from_openai_chat_to_canonical_request, CanonicalRequest,
|
||||
},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalRequest> {
|
||||
from_openai_chat_to_canonical_request(body)
|
||||
}
|
||||
|
||||
pub fn to(request: &CanonicalRequest, ctx: &FormatContext) -> Option<Value> {
|
||||
let mut body = canonical_to_openai_chat_request(request);
|
||||
force_stream_options(&mut body, ctx.upstream_is_stream);
|
||||
Some(body)
|
||||
}
|
||||
|
||||
fn force_stream_options(body: &mut Value, upstream_is_stream: bool) {
|
||||
if !upstream_is_stream {
|
||||
return;
|
||||
}
|
||||
let Some(object) = body.as_object_mut() else {
|
||||
return;
|
||||
};
|
||||
object.insert("stream".to_string(), Value::Bool(true));
|
||||
match object.get_mut("stream_options") {
|
||||
Some(Value::Object(stream_options)) => {
|
||||
stream_options.insert("include_usage".to_string(), Value::Bool(true));
|
||||
}
|
||||
_ => {
|
||||
object.insert(
|
||||
"stream_options".to_string(),
|
||||
json!({
|
||||
"include_usage": true,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
29
crates/aether-ai-formats/src/formats/openai_chat/response.rs
Normal file
29
crates/aether-ai-formats/src/formats/openai_chat/response.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{
|
||||
canonical_to_openai_chat_response, from_openai_chat_to_canonical_response,
|
||||
CanonicalResponse,
|
||||
},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalResponse> {
|
||||
from_openai_chat_to_canonical_response(body)
|
||||
}
|
||||
|
||||
pub fn to(response: &CanonicalResponse, ctx: &FormatContext) -> Option<Value> {
|
||||
let mut body = canonical_to_openai_chat_response(response);
|
||||
if body.get("service_tier").is_none() {
|
||||
if let Some(service_tier) = ctx
|
||||
.report_context_value()
|
||||
.get("original_request_body")
|
||||
.and_then(Value::as_object)
|
||||
.and_then(|request| request.get("service_tier"))
|
||||
.cloned()
|
||||
{
|
||||
body["service_tier"] = service_tier;
|
||||
}
|
||||
}
|
||||
Some(body)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
pub mod request;
|
||||
pub mod response;
|
||||
@@ -0,0 +1,28 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{
|
||||
canonical_to_openai_responses_compact_request, canonical_to_openai_responses_request,
|
||||
from_openai_responses_to_canonical_request, CanonicalRequest,
|
||||
},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalRequest> {
|
||||
from_openai_responses_to_canonical_request(body)
|
||||
}
|
||||
|
||||
pub fn to(request: &CanonicalRequest, ctx: &FormatContext) -> Option<Value> {
|
||||
canonical_to_openai_responses_request(
|
||||
request,
|
||||
ctx.mapped_model_or(request.model.as_str()),
|
||||
ctx.upstream_is_stream,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn to_compact(request: &CanonicalRequest, ctx: &FormatContext) -> Option<Value> {
|
||||
canonical_to_openai_responses_compact_request(
|
||||
request,
|
||||
ctx.mapped_model_or(request.model.as_str()),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
canonical::{
|
||||
canonical_to_openai_responses_compact_response, canonical_to_openai_responses_response,
|
||||
from_openai_responses_to_canonical_response, CanonicalResponse,
|
||||
},
|
||||
context::FormatContext,
|
||||
};
|
||||
|
||||
pub fn from(body: &Value, _ctx: &FormatContext) -> Option<CanonicalResponse> {
|
||||
from_openai_responses_to_canonical_response(body)
|
||||
}
|
||||
|
||||
pub fn to(response: &CanonicalResponse, ctx: &FormatContext) -> Option<Value> {
|
||||
Some(canonical_to_openai_responses_response(
|
||||
response,
|
||||
&ctx.report_context_value(),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn to_compact(response: &CanonicalResponse, ctx: &FormatContext) -> Option<Value> {
|
||||
Some(canonical_to_openai_responses_compact_response(
|
||||
response,
|
||||
&ctx.report_context_value(),
|
||||
))
|
||||
}
|
||||
Reference in New Issue
Block a user